usart.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. #include "usart.h"
  2. #include "common.h"
  3. #include "parameters.h"
  4. #include "portgw.h"
  5. #define DEBUG_RBUF_SIZE 256 // gcc-4.7 cannot handle const variable sizes
  6. #ifdef DEBUG
  7. static uint8_t debug_rx_fifo[DEBUG_RBUF_SIZE];
  8. static uint8_t debug_tx_fifo[DEBUG_RBUF_SIZE];
  9. #endif
  10. #define FIN { 0 }, { 0 }, 0, 0
  11. #define UARTS_LEN 5
  12. static uart_t uarts[UARTS_LEN] = {
  13. {
  14. // RS485_1
  15. true,
  16. {
  17. BRD_9600,
  18. NO_PAR,
  19. DATABITS_8,
  20. STOP_1
  21. },
  22. UART7,
  23. NULL,
  24. NULL,
  25. 0,
  26. GetUsartSettingsInt,
  27. {
  28. NULL,
  29. NULL,
  30. },
  31. SetUsartTransportQueue,
  32. FIN
  33. },
  34. {
  35. // GSM
  36. false,
  37. {
  38. BRD_9600,
  39. NO_PAR,
  40. DATABITS_8,
  41. STOP_1
  42. },
  43. USART2,
  44. NULL,
  45. NULL,
  46. 0,
  47. GetUsartSettingsInt,
  48. {
  49. NULL,
  50. NULL,
  51. },
  52. SetUsartTransportQueue,
  53. FIN
  54. },
  55. {
  56. // RS485_2
  57. true,
  58. {
  59. BRD_9600,
  60. NO_PAR,
  61. DATABITS_8,
  62. STOP_1
  63. },
  64. USART3,
  65. NULL,
  66. NULL,
  67. 0,
  68. GetUsartSettingsInt,
  69. {
  70. NULL,
  71. NULL,
  72. },
  73. SetUsartTransportQueue,
  74. FIN
  75. },
  76. {
  77. // RS232_1
  78. true,
  79. {
  80. BRD_9600,
  81. NO_PAR,
  82. DATABITS_8,
  83. STOP_1
  84. },
  85. UART4,
  86. NULL,
  87. NULL,
  88. 0,
  89. GetUsartSettingsInt,
  90. {
  91. NULL,
  92. NULL,
  93. },
  94. SetUsartTransportQueue,
  95. FIN
  96. },
  97. {
  98. // RS232_2
  99. true,
  100. {
  101. BRD_9600,
  102. NO_PAR,
  103. DATABITS_8,
  104. STOP_1
  105. },
  106. UART5,
  107. NULL,
  108. NULL,
  109. 0,
  110. GetUsartSettingsInt,
  111. {
  112. NULL,
  113. NULL,
  114. },
  115. SetUsartTransportQueue,
  116. FIN
  117. }
  118. };
  119. #undef FIN
  120. void uart_hw_init(usart_type *uart, uart_settings_t *sett);
  121. void uart_proddefault_init(uint8_t indx_uarts, uint8_t portgw_queue) {
  122. uarts[indx_uarts].setting.baud = BRD_9600;
  123. uarts[indx_uarts].setting.databits = DATABITS_8;
  124. uarts[indx_uarts].setting.parity = NO_PAR;
  125. uarts[indx_uarts].setting.stopbits = STOP_1;
  126. uart_hw_init( uarts[indx_uarts].addr, &uarts[indx_uarts].setting);
  127. uarts[indx_uarts].trans_queue.rxQ = &PortGwRxQ[portgw_queue];;
  128. uarts[indx_uarts].trans_queue.txQ = &PortGwTxQ[portgw_queue];;
  129. }
  130. void uart_hw_init(usart_type *uart, uart_settings_t *sett) {
  131. gpio_init_type gpio_initstructure;
  132. uint32_t baud;
  133. usart_data_bit_num_type wordlen;
  134. usart_parity_selection_type parity;
  135. usart_stop_bit_num_type stop;
  136. switch(sett->baud)
  137. {
  138. case BRD_1200:
  139. baud = 1200;
  140. break;
  141. case BRD_2400:
  142. baud = 2400;
  143. break;
  144. case BRD_4800:
  145. baud = 4800;
  146. break;
  147. case BRD_9600:
  148. baud = 9600;
  149. break;
  150. case BRD_19200:
  151. baud = 19200;
  152. break;
  153. case BRD_38400:
  154. baud = 38400;
  155. break;
  156. case BRD_57600:
  157. baud = 57600;
  158. break;
  159. case BRD_115200:
  160. baud = 115200;
  161. break;
  162. case BRD_230400:
  163. baud = 230400;
  164. break;
  165. case BRD_460800:
  166. baud = 460800;
  167. break;
  168. default:
  169. break;
  170. }
  171. switch(sett->databits)
  172. {
  173. case DATABITS_7:
  174. wordlen = USART_DATA_8BITS;
  175. break;
  176. case DATABITS_8:
  177. if ((sett->parity == EVEN_PAR) ||
  178. (sett->parity == ODD_PAR)) {
  179. wordlen = USART_DATA_9BITS;
  180. } else {
  181. wordlen = USART_DATA_8BITS;
  182. }
  183. break;
  184. default:
  185. break;
  186. }
  187. switch(sett->parity)
  188. {
  189. case NO_PAR:
  190. parity = USART_PARITY_NONE;
  191. break;
  192. case ODD_PAR:
  193. parity = USART_PARITY_ODD;
  194. break;
  195. case EVEN_PAR:
  196. parity = USART_PARITY_EVEN;
  197. break;
  198. default:
  199. break;
  200. }
  201. switch(sett->stopbits)
  202. {
  203. case STOP_1:
  204. stop = USART_STOP_1_BIT;
  205. break;
  206. case STOP_2:
  207. stop = USART_STOP_2_BIT;
  208. break;
  209. default:
  210. break;
  211. }
  212. // RS485_1
  213. if (uart == UART7) {
  214. crm_periph_clock_enable(CRM_GPIOE_PERIPH_CLOCK, TRUE);
  215. crm_periph_clock_enable(CRM_UART7_PERIPH_CLOCK, TRUE);
  216. gpio_default_para_init(&gpio_initstructure);
  217. gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  218. gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  219. gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
  220. gpio_initstructure.gpio_pins = GPIO_PINS_8;
  221. gpio_initstructure.gpio_pull = GPIO_PULL_NONE;
  222. gpio_init(GPIOE, &gpio_initstructure);
  223. gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  224. gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  225. gpio_initstructure.gpio_mode = GPIO_MODE_INPUT;
  226. gpio_initstructure.gpio_pins = GPIO_PINS_7;
  227. gpio_initstructure.gpio_pull = GPIO_PULL_UP;
  228. gpio_init(GPIOE, &gpio_initstructure);
  229. nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
  230. nvic_irq_enable(UART7_IRQn, 6, 0);
  231. }
  232. // RS485_2
  233. if (uart == USART3) {
  234. crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, TRUE);
  235. crm_periph_clock_enable(CRM_USART3_PERIPH_CLOCK, TRUE);
  236. crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
  237. gpio_pin_remap_config(USART3_GMUX_0011, TRUE);
  238. gpio_default_para_init(&gpio_initstructure);
  239. gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  240. gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  241. gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
  242. gpio_initstructure.gpio_pins = GPIO_PINS_8;
  243. gpio_initstructure.gpio_pull = GPIO_PULL_NONE;
  244. gpio_init(GPIOD, &gpio_initstructure);
  245. gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  246. gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  247. gpio_initstructure.gpio_mode = GPIO_MODE_INPUT;
  248. gpio_initstructure.gpio_pins = GPIO_PINS_9;
  249. gpio_initstructure.gpio_pull = GPIO_PULL_UP;
  250. gpio_init(GPIOD, &gpio_initstructure);
  251. nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
  252. nvic_irq_enable(USART3_IRQn, 6, 0);
  253. }
  254. // RS232_1
  255. if (uart == UART4) {
  256. crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
  257. crm_periph_clock_enable(CRM_UART4_PERIPH_CLOCK, TRUE);
  258. gpio_default_para_init(&gpio_initstructure);
  259. gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  260. gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  261. gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
  262. gpio_initstructure.gpio_pins = GPIO_PINS_10;
  263. gpio_initstructure.gpio_pull = GPIO_PULL_NONE;
  264. gpio_init(GPIOC, &gpio_initstructure);
  265. gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  266. gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  267. gpio_initstructure.gpio_mode = GPIO_MODE_INPUT;
  268. gpio_initstructure.gpio_pins = GPIO_PINS_11;
  269. gpio_initstructure.gpio_pull = GPIO_PULL_UP;
  270. gpio_init(GPIOC, &gpio_initstructure);
  271. nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
  272. nvic_irq_enable(UART4_IRQn, 6, 0);
  273. }
  274. // RS232_2
  275. if (uart == UART5) {
  276. crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
  277. crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, TRUE);
  278. crm_periph_clock_enable(CRM_UART5_PERIPH_CLOCK, TRUE);
  279. gpio_default_para_init(&gpio_initstructure);
  280. // configure the uart tx pin
  281. gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  282. gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  283. gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
  284. gpio_initstructure.gpio_pins = GPIO_PINS_12;
  285. gpio_initstructure.gpio_pull = GPIO_PULL_NONE;
  286. gpio_init(GPIOC, &gpio_initstructure);
  287. gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  288. gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  289. gpio_initstructure.gpio_mode = GPIO_MODE_INPUT;
  290. gpio_initstructure.gpio_pins = GPIO_PINS_2;
  291. gpio_initstructure.gpio_pull = GPIO_PULL_UP;
  292. gpio_init(GPIOD, &gpio_initstructure);
  293. nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
  294. nvic_irq_enable(UART5_IRQn, 6, 0);
  295. }
  296. usart_init(uart, baud, wordlen, stop);
  297. usart_parity_selection_config(uart, parity);
  298. usart_transmitter_enable(uart, TRUE);
  299. usart_receiver_enable(uart, TRUE);
  300. usart_enable(uart, TRUE);
  301. usart_interrupt_enable(uart, USART_RDBF_INT, TRUE);
  302. usart_interrupt_enable(uart, USART_TDC_INT, FALSE);
  303. usart_enable(uart, TRUE);
  304. }
  305. void usart_init_struct(void) {
  306. for (int i = 0; i < UARTS_LEN; ++i) {
  307. if (!uarts[i].enabled) continue;
  308. if(uarts[i].tx_fifo != NULL && uarts[i].rx_fifo != NULL){
  309. uarts[i].rx_buf_sem = xSemaphoreCreateCounting(uarts[i].buf_size, 0);
  310. uarts[i].tx_buf_sem = xSemaphoreCreateCounting(uarts[i].buf_size, (uarts[i].buf_size-1));
  311. rbuf8_init(&uarts[i].tx_buf, uarts[i].tx_fifo, uarts[i].buf_size);
  312. rbuf8_init(&uarts[i].rx_buf, uarts[i].rx_fifo, uarts[i].buf_size);
  313. }
  314. if(uarts[i].set_uart_queue != NULL){
  315. uarts[i].set_uart_queue(&uarts[i].trans_queue, i);
  316. }
  317. uart_hw_init(uarts[i].addr, &uarts[i].setting);
  318. }
  319. #ifdef PRINTF_CUSTOM
  320. init_printf(NULL, putc_);
  321. #endif
  322. }
  323. void usart_reinit(void)
  324. {
  325. uart_settings_t uart_settings_new;
  326. for (int i = 0; i < UARTS_LEN; ++i) {
  327. if (!uarts[i].enabled) continue;
  328. if(uarts[i].get_uart_settings != NULL){
  329. if(uarts[i].set_uart_queue != NULL){
  330. uarts[i].set_uart_queue(&uarts[i].trans_queue, i);
  331. }
  332. if(uarts[i].get_uart_settings(&uart_settings_new, i)){
  333. if(uart_settings_new.baud != uarts[i].setting.baud
  334. || uart_settings_new.databits != uarts[i].setting.databits
  335. || uart_settings_new.parity != uarts[i].setting.parity
  336. || uart_settings_new.stopbits != uarts[i].setting.stopbits){
  337. uarts[i].setting.baud = uart_settings_new.baud;
  338. uarts[i].setting.databits = uart_settings_new.databits;
  339. uarts[i].setting.parity = uart_settings_new.parity;
  340. uarts[i].setting.stopbits = uart_settings_new.stopbits;
  341. uart_hw_init(uarts[i].addr, &uarts[i].setting);
  342. }
  343. }
  344. }
  345. }
  346. }
  347. void transport_enable_tx(uint8_t indx_uarts) {
  348. usart_interrupt_enable(uarts[indx_uarts].addr, USART_TDBE_INT, TRUE);
  349. }
  350. static inline void transport_irq_handler(uint8_t indx_uarts) {
  351. uint16_t c;
  352. static BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  353. if (uarts[indx_uarts].addr->sts & USART_ROERR_FLAG) {
  354. c = uarts[indx_uarts].addr->dt;
  355. //DBG printf("overrunRS485\r\n");
  356. }
  357. if (uarts[indx_uarts].addr->sts & USART_PERR_FLAG) {
  358. c = uarts[indx_uarts].addr->dt;
  359. //DBG printf("[rs485] parity fail: 0x%X\r\n", c);
  360. }
  361. if (usart_flag_get(uarts[indx_uarts].addr, USART_TDBE_FLAG) != RESET) {
  362. if(uarts[indx_uarts].trans_queue.txQ != NULL){
  363. if (xQueueReceiveFromISR(*(uarts[indx_uarts].trans_queue.txQ), &c, &xHigherPriorityTaskWoken) == pdTRUE) {
  364. uarts[indx_uarts].addr->dt = c;
  365. //DBG printf("wr: 0x%x\r\n", c);
  366. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  367. } else {
  368. uarts[indx_uarts].addr->ctrl1_bit.tdbeien = FALSE;
  369. uarts[indx_uarts].addr->ctrl1_bit.rdbfien = TRUE;
  370. }
  371. }
  372. else{
  373. uarts[indx_uarts].addr->ctrl1_bit.tdbeien = FALSE;
  374. uarts[indx_uarts].addr->ctrl1_bit.rdbfien = TRUE;
  375. }
  376. //USART_ClearITPendingBit(uarts[indx_uarts].addr, USART_IT_TXE);
  377. }
  378. if (usart_flag_get(uarts[indx_uarts].addr, USART_RDBF_FLAG) != RESET) {
  379. c = uarts[indx_uarts].addr->dt;
  380. if (uarts[indx_uarts].addr->ctrl1_bit.pen == TRUE) {
  381. if (uarts[indx_uarts].addr->ctrl1_bit.dbn == TRUE) {
  382. /* 8-bit data */
  383. c &= 0xFF;
  384. } else {
  385. /* 7-bit data */
  386. c &= 0x7F;
  387. }
  388. }
  389. if(uarts[indx_uarts].trans_queue.rxQ != NULL){
  390. if (xQueueSendFromISR(*(uarts[indx_uarts].trans_queue.rxQ), &c, &xHigherPriorityTaskWoken) == pdTRUE) {
  391. //DBG printf("wr: 0x%x\r\n", c);
  392. } else {
  393. //DBG printf("[uart] rx overflow\r\n");
  394. }
  395. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  396. }
  397. //USART_ClearITPendingBit(uarts[indx_uarts].addr, USART_IT_RXNE);
  398. }
  399. }
  400. // RS485_1
  401. void UART7_IRQHandler(void)
  402. {
  403. transport_irq_handler(0);
  404. }
  405. // RS485_2
  406. void USART3_IRQHandler(void)
  407. {
  408. transport_irq_handler(2);
  409. }
  410. // RS232_1
  411. void UART4_IRQHandler(void)
  412. {
  413. transport_irq_handler(3);
  414. }
  415. // RS232_2
  416. void UART5_IRQHandler(void)
  417. {
  418. transport_irq_handler(4);
  419. }