Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.68 KB | None | 0 0
  1. stm32f4_gpio_config io_key2 =
  2. {
  3. .fields={
  4. .pin_first = STM32F4_GPIO_PIN(4, 2),
  5. .pin_last = STM32F4_GPIO_PIN(4, 2),
  6. .mode = STM32F4_GPIO_MODE_INPUT,
  7. //.otype = STM32F4_GPIO_OTYPE_PUSH_PULL,
  8. .ospeed = STM32F4_GPIO_OSPEED_2_MHZ,
  9. .pupd = STM32F4_GPIO_PULL_UP,
  10. .output = 0,
  11. .af = 0
  12. }
  13. };
  14.  
  15. #define DEV_I2C_NAME "/dev/i2c1"
  16. static const char bus_path[] = "/dev/i2c-1";
  17. static const char eeprom_path[] = "/dev/i2c-1.eeprom-0";
  18. /*
  19. int AT24C02_Init(void)
  20. {
  21. #if 1
  22. int ret = i2c_dev_register_eeprom(
  23. bus_path,
  24. eeprom_path,
  25. DEVICE_EEPROM,
  26. 1,
  27. 8,
  28. 256, // size
  29. 0
  30. );
  31. if(ret == 0)
  32. {
  33. printf("drive ok\n");
  34. }
  35. else
  36. {
  37. perror("register failed\n");
  38. printf("error %d\n", ret);
  39. }
  40. #endif
  41. int fd = open(DEV_I2C_NAME, O_RDWR);
  42. if(fd < 0)
  43. {
  44. printf("Can't open %s \n", DEV_I2C_NAME);
  45. //return fd;
  46. exit(1);
  47. }
  48.  
  49. printf("open /dev/i2c/0 success !\n");
  50.  
  51. if(ioctl(fd, I2C_SLAVE, Address) < 0)
  52. {
  53. printf("fail to set i2c device slave address!\n");
  54. close(fd);
  55. return -1;
  56. }
  57.  
  58. printf("set slave address to 0x%x success!\n", Address);
  59. #if 0
  60. if(ioctl(fd, I2C_BUS_MODE, 1)<0)
  61. printf("set bus mode fail!\n");
  62. else
  63. printf("set bus mode ok!\n");
  64. #endif
  65.  
  66. return fd;
  67. }
  68. */
  69.  
  70.  
  71.  
  72.  
  73.  
  74. //extern stm32f4_i2c_bus_entry *const stm32f4_i2c1;
  75. extern int ex_usart_read_polled(int minor);
  76.  
  77. void uart_poll(int fd, char *buf, int len)
  78. {
  79. int n;
  80. char ch;
  81. int i = 0;
  82. printf("in poll function.\n");
  83. for(;timer_timeout != 1;)
  84. {
  85. n = ex_usart_read_polled(0);
  86. //printf("n %d cc %x\n", n, ch);
  87. if (n < 0)
  88. {
  89. //rtems_task_wake_after (0.1); // delay 10ms
  90. int j;
  91. //for(j = 0; j < 1000; j++) j = j;
  92. }
  93. else //if(n == 1)
  94. {
  95.  
  96. ch = n;
  97. //printf("%x, %x \n", ch, n);
  98. if(ch == '\r')
  99. continue;
  100.  
  101. if(ch == '\n')
  102. {
  103.  
  104. printf("uart %s\n", buf);
  105. break;
  106. }
  107.  
  108. buf[i] = ch;
  109. //printf("the %d th char %c \n", i, buf[i]);
  110. i++;
  111.  
  112. if(i == len)
  113. {
  114. printf("uart buf full %s\n", buf);
  115. break;
  116.  
  117. }
  118. }
  119. }
  120. }
  121.  
  122.  
  123. rtems_timer_service_routine Timer_Routine( rtems_id id, void *ignored )
  124. {
  125. //printf("time out \n");
  126.  
  127. int j = 100000;
  128. timer_timeout = 1;
  129. //LED_ON();
  130. #if 0
  131. (void) rtems_timer_fire_after(
  132. Timer1,
  133. 5 * rtems_clock_get_ticks_per_second(),
  134. Timer_Routine,
  135. NULL
  136. );
  137. #endif
  138.  
  139.  
  140. }
  141.  
  142. int ux_uart_init(char *dev_name)
  143. {
  144. printf("ux_uart_init %s begin\n", dev_name);
  145. int fd = open(dev_name, O_RDWR | O_NOCTTY | O_NONBLOCK);
  146.  
  147. //printf("\nOpened COM3, fd=%d \n", fd);
  148. //LED_ON();
  149. if(fd < 0)
  150. {
  151. //printf("open error\n");
  152. LED_ON();
  153. return -1;
  154. }
  155.  
  156.  
  157.  
  158. struct termios options;
  159. tcgetattr(fd, &options);
  160. bzero(&options, sizeof(options));
  161. /* setting the baud rate */
  162. cfsetispeed(&options, B115200);
  163. cfsetospeed(&options, B115200);
  164.  
  165.  
  166. options.c_cflag |= (CLOCAL | CREAD);
  167.  
  168. options.c_cflag &= ~PARENB;
  169. options.c_cflag &= ~CSTOPB;
  170. options.c_cflag &= ~CSIZE;
  171. options.c_cflag |= CS8;
  172.  
  173. options.c_iflag = IGNPAR| ICRNL;
  174. options.c_oflag = 0;
  175. options.c_lflag = ICANON;
  176.  
  177. options.c_cc[VEOF] = 4;
  178. options.c_cc[VMIN] = 1;
  179. options.c_cc[VTIME] = 0;
  180.  
  181. //tcflush(fd, TCIFLUSH);
  182. tcsetattr(fd, TCSANOW, &options);
  183.  
  184. if(0 == isatty(fd))
  185. {
  186. printf("input is not a terminal device\n");
  187. return -1;
  188. }
  189.  
  190. return fd;
  191. }
  192.  
  193.  
  194. rtems_isr EINT2_handler (rtems_vector_number vector)
  195. {
  196. //rtems_event_send (taskid, RTEMS_EVENT_1);
  197. //GPBDAT ^= 1<<7;
  198. //SRCPND = 1<<vector;
  199. //INTPND = 1<<vector;
  200. LED_ON();
  201. printf("abc \n");
  202. (*(uint32_t*)0x40013C14) = (1<<2);
  203. }
  204.  
  205.  
  206. void keys_init()
  207. {
  208. stm32f4_gpio_set_config(&io_key2);
  209.  
  210. //(*(uint32_t*)0x40023830) |= 1<<4; //rcc
  211. // 0x4002 1000
  212. (*(uint32_t*)0x4002100c) |= 0x01; // pull up
  213. // pe2 exit
  214. (*(uint32_t*)0x40013808) |= 0b010000000000;
  215.  
  216. // rising edge
  217. (*(uint32_t*)0x40013C08) |= (1<<2);
  218.  
  219.  
  220. // open it
  221. (*(uint32_t*)0x40013C00) |= (1<<2);
  222.  
  223.  
  224. // nvic enable interrupter number
  225. // 0xE000E100
  226. (*(uint32_t*)0xE000E100) |= (1<<8);
  227. }
  228.  
  229. extern unsigned long Console_Configuration_Count;
  230. rtems_task Init(rtems_task_argument argument)
  231. {
  232. //puts( "\n\n*** i2c test begin ***\n");
  233. rtems_status_code status;
  234.  
  235. keys_init();
  236. LED_INIT();
  237. LED_OFF();
  238. uint8_t buf[20] = {0};
  239.  
  240. SCL_INIT();
  241. SDA_INIT();
  242. OLED_Init();
  243. OLED_Fill(0xff);
  244. OLED_Menu_Data();
  245. OLED_Refresh_Gram();
  246.  
  247. // for uart
  248. char buffer[64] = {0};
  249. int buffer_len = sizeof(buffer);
  250. printf("the console count %d\n", Console_Configuration_Count);
  251.  
  252. rtems_status_code ret = rtems_timer_create(rtems_build_name( 'T', 'M', 'R', '1' ), &Timer1);
  253. if(ret == RTEMS_SUCCESSFUL)
  254. {
  255. printf("create timer ok\n");
  256. }
  257. //(void) rtems_timer_create(rtems_build_name( 'T', 'M', 'R', '2' ), &Timer2);
  258.  
  259. status = rtems_interrupt_handler_install(
  260. STM32F4_IRQ_EXTI2,
  261. "EINT2",
  262. RTEMS_INTERRUPT_UNIQUE,
  263. (rtems_interrupt_handler) EINT2_handler,
  264. NULL
  265. );
  266. if(status == RTEMS_SUCCESSFUL)
  267. {
  268. printf("interrupt ok\n");
  269. }
  270.  
  271.  
  272. PWR_ENABLE_INIT();
  273. PWR_ENABLE_SET(1);
  274. // power on
  275. // configure pin
  276. PWR_KEY_INIT();
  277. rtems_task_wake_after(1);
  278. PWR_KEY_ON();
  279. rtems_task_wake_after(0.5*rtems_clock_get_ticks_per_second());
  280. PWR_KEY_OFF();
  281. // set 0
  282. GPRS_status = GPRS_PowerUp;
  283.  
  284. rtems_task_wake_after(5*rtems_clock_get_ticks_per_second());
  285.  
  286. printf("stack check %d\n", rtems_stack_checker_is_blown());
  287. //printf("before oprator com3\n");
  288. //rtems_stack_checker_report_usage();
  289.  
  290.  
  291.  
  292. // at command starts
  293. #if 0
  294. int g4_fd = -1;
  295. g4_fd = ux_uart_init("/dev/ttyS1");
  296. printf("after init \n");
  297. if(g4_fd < 0)
  298. {
  299. printf("open uart node failed \n");
  300. }
  301.  
  302. printf("ttys2 fd %d\n", g4_fd);
  303. write(g4_fd, "AT\r\n", 4);
  304. timer_timeout = 0;
  305. rtems_timer_fire_after(Timer1, 1 * rtems_clock_get_ticks_per_second(),
  306. Timer_Routine, NULL);
  307. uart_poll(g4_fd, buffer, buffer_len);
  308. rtems_timer_cancel(Timer1);
  309.  
  310. printf("buffer len %d\n", strlen(buffer));
  311. if(strstr(buffer, "OK"))
  312. {
  313. printf("uart recv %s\n", buffer);
  314. LED_ON();
  315. }
  316. #endif
  317.  
  318. int i = 0;
  319. for(i = 0; i < 10; i++)
  320. printf("%x\t", buf[i]);
  321.  
  322. while(1)
  323. {
  324. printf("in test \n");
  325. rtems_task_wake_after(10);
  326. }
  327.  
  328. (void)rtems_task_delete( RTEMS_SELF );
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement