Skip to content

Instantly share code, notes, and snippets.

@goog
Created April 11, 2019 11:39
Show Gist options
  • Save goog/5e5b484430328d3c1153b474ce147079 to your computer and use it in GitHub Desktop.
Save goog/5e5b484430328d3c1153b474ce147079 to your computer and use it in GitHub Desktop.
rtems stm32f4
stm32f4_gpio_config io_key2 =
{
.fields={
.pin_first = STM32F4_GPIO_PIN(4, 2),
.pin_last = STM32F4_GPIO_PIN(4, 2),
.mode = STM32F4_GPIO_MODE_INPUT,
//.otype = STM32F4_GPIO_OTYPE_PUSH_PULL,
.ospeed = STM32F4_GPIO_OSPEED_2_MHZ,
.pupd = STM32F4_GPIO_PULL_UP,
.output = 0,
.af = 0
}
};
#define DEV_I2C_NAME "/dev/i2c1"
static const char bus_path[] = "/dev/i2c-1";
static const char eeprom_path[] = "/dev/i2c-1.eeprom-0";
/*
int AT24C02_Init(void)
{
#if 1
int ret = i2c_dev_register_eeprom(
bus_path,
eeprom_path,
DEVICE_EEPROM,
1,
8,
256, // size
0
);
if(ret == 0)
{
printf("drive ok\n");
}
else
{
perror("register failed\n");
printf("error %d\n", ret);
}
#endif
int fd = open(DEV_I2C_NAME, O_RDWR);
if(fd < 0)
{
printf("Can't open %s \n", DEV_I2C_NAME);
//return fd;
exit(1);
}
printf("open /dev/i2c/0 success !\n");
if(ioctl(fd, I2C_SLAVE, Address) < 0)
{
printf("fail to set i2c device slave address!\n");
close(fd);
return -1;
}
printf("set slave address to 0x%x success!\n", Address);
#if 0
if(ioctl(fd, I2C_BUS_MODE, 1)<0)
printf("set bus mode fail!\n");
else
printf("set bus mode ok!\n");
#endif
return fd;
}
*/
//extern stm32f4_i2c_bus_entry *const stm32f4_i2c1;
extern int ex_usart_read_polled(int minor);
void uart_poll(int fd, char *buf, int len)
{
int n;
char ch;
int i = 0;
printf("in poll function.\n");
for(;timer_timeout != 1;)
{
n = ex_usart_read_polled(0);
//printf("n %d cc %x\n", n, ch);
if (n < 0)
{
//rtems_task_wake_after (0.1); // delay 10ms
int j;
//for(j = 0; j < 1000; j++) j = j;
}
else //if(n == 1)
{
ch = n;
//printf("%x, %x \n", ch, n);
if(ch == '\r')
continue;
if(ch == '\n')
{
printf("uart %s\n", buf);
break;
}
buf[i] = ch;
//printf("the %d th char %c \n", i, buf[i]);
i++;
if(i == len)
{
printf("uart buf full %s\n", buf);
break;
}
}
}
}
rtems_timer_service_routine Timer_Routine( rtems_id id, void *ignored )
{
//printf("time out \n");
int j = 100000;
timer_timeout = 1;
//LED_ON();
#if 0
(void) rtems_timer_fire_after(
Timer1,
5 * rtems_clock_get_ticks_per_second(),
Timer_Routine,
NULL
);
#endif
}
int ux_uart_init(char *dev_name)
{
printf("ux_uart_init %s begin\n", dev_name);
int fd = open(dev_name, O_RDWR | O_NOCTTY | O_NONBLOCK);
//printf("\nOpened COM3, fd=%d \n", fd);
//LED_ON();
if(fd < 0)
{
//printf("open error\n");
LED_ON();
return -1;
}
struct termios options;
tcgetattr(fd, &options);
bzero(&options, sizeof(options));
/* setting the baud rate */
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_iflag = IGNPAR| ICRNL;
options.c_oflag = 0;
options.c_lflag = ICANON;
options.c_cc[VEOF] = 4;
options.c_cc[VMIN] = 1;
options.c_cc[VTIME] = 0;
//tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &options);
if(0 == isatty(fd))
{
printf("input is not a terminal device\n");
return -1;
}
return fd;
}
rtems_isr EINT2_handler (rtems_vector_number vector)
{
//rtems_event_send (taskid, RTEMS_EVENT_1);
//GPBDAT ^= 1<<7;
//SRCPND = 1<<vector;
//INTPND = 1<<vector;
LED_ON();
printf("abc \n");
(*(uint32_t*)0x40013C14) = (1<<2);
}
void keys_init()
{
stm32f4_gpio_set_config(&io_key2);
//(*(uint32_t*)0x40023830) |= 1<<4; //rcc
// 0x4002 1000
(*(uint32_t*)0x4002100c) |= 0x01; // pull up
// pe2 exit
(*(uint32_t*)0x40013808) |= 0b010000000000;
// rising edge
(*(uint32_t*)0x40013C08) |= (1<<2);
// open it
(*(uint32_t*)0x40013C00) |= (1<<2);
// nvic enable interrupter number
// 0xE000E100
(*(uint32_t*)0xE000E100) |= (1<<8);
}
extern unsigned long Console_Configuration_Count;
rtems_task Init(rtems_task_argument argument)
{
//puts( "\n\n*** i2c test begin ***\n");
rtems_status_code status;
keys_init();
LED_INIT();
LED_OFF();
uint8_t buf[20] = {0};
SCL_INIT();
SDA_INIT();
OLED_Init();
OLED_Fill(0xff);
OLED_Menu_Data();
OLED_Refresh_Gram();
// for uart
char buffer[64] = {0};
int buffer_len = sizeof(buffer);
printf("the console count %d\n", Console_Configuration_Count);
rtems_status_code ret = rtems_timer_create(rtems_build_name( 'T', 'M', 'R', '1' ), &Timer1);
if(ret == RTEMS_SUCCESSFUL)
{
printf("create timer ok\n");
}
//(void) rtems_timer_create(rtems_build_name( 'T', 'M', 'R', '2' ), &Timer2);
status = rtems_interrupt_handler_install(
STM32F4_IRQ_EXTI2,
"EINT2",
RTEMS_INTERRUPT_UNIQUE,
(rtems_interrupt_handler) EINT2_handler,
NULL
);
if(status == RTEMS_SUCCESSFUL)
{
printf("interrupt ok\n");
}
PWR_ENABLE_INIT();
PWR_ENABLE_SET(1);
// power on
// configure pin
PWR_KEY_INIT();
rtems_task_wake_after(1);
PWR_KEY_ON();
rtems_task_wake_after(0.5*rtems_clock_get_ticks_per_second());
PWR_KEY_OFF();
// set 0
GPRS_status = GPRS_PowerUp;
rtems_task_wake_after(5*rtems_clock_get_ticks_per_second());
printf("stack check %d\n", rtems_stack_checker_is_blown());
//printf("before oprator com3\n");
//rtems_stack_checker_report_usage();
// at command starts
#if 0
int g4_fd = -1;
g4_fd = ux_uart_init("/dev/ttyS1");
printf("after init \n");
if(g4_fd < 0)
{
printf("open uart node failed \n");
}
printf("ttys2 fd %d\n", g4_fd);
write(g4_fd, "AT\r\n", 4);
timer_timeout = 0;
rtems_timer_fire_after(Timer1, 1 * rtems_clock_get_ticks_per_second(),
Timer_Routine, NULL);
uart_poll(g4_fd, buffer, buffer_len);
rtems_timer_cancel(Timer1);
printf("buffer len %d\n", strlen(buffer));
if(strstr(buffer, "OK"))
{
printf("uart recv %s\n", buffer);
LED_ON();
}
#endif
int i = 0;
for(i = 0; i < 10; i++)
printf("%x\t", buf[i]);
while(1)
{
printf("in test \n");
rtems_task_wake_after(10);
}
(void)rtems_task_delete( RTEMS_SELF );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment