usart.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /********************************* (C) РОТЕК ***********************************
  2. * @module usart
  3. * @file usart.c
  4. * @version 1.0.0
  5. * @date XX.XX.XXXX
  6. * $brief Template
  7. *******************************************************************************
  8. * @history Version Author Comment
  9. * XX.XX.XXXX 1.0.0 Telenkov D.A. First release.
  10. *******************************************************************************
  11. */
  12. #include "stm32f4xx.h"
  13. #include "usart.h"
  14. //#include "port_microrl.h"
  15. #include "main.h"
  16. #include "ring_buf.h"
  17. //#include <stdio.h>
  18. #include "FreeRTOS.h"
  19. #include "semphr.h"
  20. #ifdef PRINTF_STDLIB
  21. #include <stdio.h>
  22. #endif
  23. #ifdef PRINTF_CUSTOM
  24. #include "tinystdio.h"
  25. #endif
  26. #ifdef __ICCARM__
  27. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  28. PUTCHAR_PROTOTYPE {
  29. USART_SendData(USER_USART, (u8) ch);
  30. while (USART_GetFlagStatus(USER_USART, USART_FLAG_TXE) == RESET) {}
  31. return ch;
  32. }
  33. #endif
  34. #define RS485_RBUF_SIZE 512
  35. #ifdef BT6702_SERVICE
  36. #define UPS_RBUF_SIZE 1024
  37. #else
  38. #define UPS_RBUF_SIZE 100
  39. #endif
  40. rbuf_t ups_rx_rbuf, ups_tx_rbuf;
  41. uint32_t ups_rx_fifo[UPS_RBUF_SIZE];
  42. uint32_t ups_tx_fifo[UPS_RBUF_SIZE];
  43. SemaphoreHandle_t xSem_rx_buf;
  44. #define USER_RBUF_SIZE 256
  45. rbuf_t user_rx_rbuf, user_tx_rbuf;
  46. uint32_t user_rx_fifo[USER_RBUF_SIZE];
  47. uint32_t user_tx_fifo[USER_RBUF_SIZE];
  48. SemaphoreHandle_t xSem_user_rx_buf;
  49. USART_InitTypeDef USART_InitStructure;
  50. void uart_config_reinit(USART_TypeDef *uart, uint32_t baud, uint16_t wordlen, uint16_t parity, uint16_t stop)
  51. {
  52. uint16_t wordlen_tmp;
  53. if (wordlen == 8) {
  54. wordlen_tmp = USART_WordLength_8b;
  55. }
  56. if (wordlen == 9) {
  57. wordlen_tmp = USART_WordLength_9b;
  58. }
  59. USART_InitStructure.USART_BaudRate = baud;
  60. USART_InitStructure.USART_WordLength = wordlen_tmp;
  61. USART_InitStructure.USART_StopBits = stop;
  62. USART_InitStructure.USART_Parity = parity;
  63. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  64. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  65. USART_Init(uart, &USART_InitStructure);
  66. }
  67. void uart_hw_init(USART_TypeDef *uart, uint32_t baud, uint16_t wordlen, uint16_t parity, uint16_t stop)
  68. {
  69. uint16_t wordlen_tmp;
  70. if (wordlen == 8) {
  71. wordlen_tmp = USART_WordLength_8b;
  72. }
  73. if (wordlen == 9) {
  74. wordlen_tmp = USART_WordLength_9b;
  75. }
  76. USART_InitStructure.USART_BaudRate = baud;
  77. USART_InitStructure.USART_WordLength = wordlen_tmp;
  78. USART_InitStructure.USART_StopBits = stop;
  79. USART_InitStructure.USART_Parity = parity;
  80. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  81. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  82. USART_DeInit(uart);
  83. if (uart == USART1) {
  84. RCC->APB2ENR |= RCC_APB2Periph_USART1;
  85. NVIC_SetPriority(USART1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 0x05, 0));
  86. NVIC_EnableIRQ(USART1_IRQn);
  87. USART_Init(USART1, &USART_InitStructure);
  88. USART_Cmd(USART1, ENABLE);
  89. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  90. USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
  91. }
  92. if (uart == USART2) {
  93. RCC->APB1ENR |= RCC_APB1Periph_USART2;
  94. NVIC_SetPriority(USART2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 0x05, 0));
  95. NVIC_EnableIRQ(USART2_IRQn);
  96. USART_Init(USART2, &USART_InitStructure);
  97. USART_Cmd(USART2, ENABLE);
  98. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  99. USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
  100. }
  101. if (uart == USART3) {
  102. RCC->APB1ENR |= RCC_APB1Periph_USART3;
  103. NVIC_SetPriority(USART3_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 0x05, 0));
  104. NVIC_EnableIRQ(USART3_IRQn);
  105. USART_Init(USART3, &USART_InitStructure);
  106. USART_Cmd(USART3, ENABLE);
  107. USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
  108. USART_ITConfig(USART3, USART_IT_TXE, DISABLE);
  109. }
  110. if (uart == UART4) {
  111. RCC->APB1ENR |= RCC_APB1Periph_UART4;
  112. USART_Init(UART4, &USART_InitStructure);
  113. USART_Cmd(UART4, ENABLE);
  114. USART_HalfDuplexCmd(UART4, ENABLE);
  115. }
  116. if (uart == UART5) {
  117. RCC->APB1ENR |= RCC_APB1Periph_UART5;
  118. USART_Init(UART5, &USART_InitStructure);
  119. USART_Cmd(UART5, ENABLE);
  120. USART_HalfDuplexCmd(UART5, ENABLE);
  121. }
  122. }
  123. void init_ups_rbuf(void)
  124. {
  125. rbuf32_init(&ups_tx_rbuf, ups_tx_fifo, UPS_RBUF_SIZE);
  126. rbuf32_init(&ups_rx_rbuf, ups_rx_fifo, UPS_RBUF_SIZE);
  127. }
  128. void InitUSART(void)
  129. {
  130. xSem_rx_buf = xSemaphoreCreateCounting( UPS_RBUF_SIZE, 0 );
  131. rbuf32_init(&ups_tx_rbuf, ups_tx_fifo, UPS_RBUF_SIZE);
  132. rbuf32_init(&ups_rx_rbuf, ups_rx_fifo, UPS_RBUF_SIZE);
  133. uart_hw_init(UPS_USART, UPS_USART_BAUD, UPS_USART_WORD_LEN, UPS_USART_PARITY, UPS_USART_STOP_BIT);
  134. xSem_user_rx_buf = xSemaphoreCreateCounting( USER_RBUF_SIZE, 0 );
  135. rbuf32_init(&user_tx_rbuf, user_tx_fifo, USER_RBUF_SIZE);
  136. rbuf32_init(&user_rx_rbuf, user_rx_fifo, USER_RBUF_SIZE);
  137. uart_hw_init(USER_USART, USER_USART_BAUD, USER_USART_WORD_LEN, USER_USART_PARITY, USER_USART_STOP_BIT);
  138. #ifdef RS485_USART
  139. rs485TxQ = xQueueCreate(RS485_RBUF_SIZE, 1);
  140. rs485RxQ = xQueueCreate(RS485_RBUF_SIZE, 1);
  141. /* Initial configuration. Port will be reconfigured later according to settings */
  142. uart_hw_init(RS485_USART, RS485_USART_BAUD, RS485_USART_WORD_LEN, RS485_USART_PARITY, RS485_USART_STOP_BIT);
  143. #endif
  144. #ifdef PRINTF_CUSTOM
  145. init_printf(NULL, putc_);
  146. #endif
  147. }
  148. void ups_putchar(uint16_t byte)
  149. {
  150. UPS_USART->CR1 &= ~USART_CR1_TXEIE;
  151. if (!rbuf_isfull(&ups_tx_rbuf)) {
  152. rbuf32_put(&ups_tx_rbuf, (uint32_t)(byte));
  153. }
  154. UPS_USART->CR1 |= USART_CR1_TXEIE;
  155. }
  156. int ups_send_block(void *data, uint8_t len)
  157. {
  158. int i = 0;
  159. uint32_t s_temp = 0;
  160. UPS_USART->CR1 &= ~USART_CR1_TXEIE;
  161. //DBG printf("STOP \r\n");
  162. while (i < len) {
  163. if (!rbuf_isfull(&ups_tx_rbuf)) {
  164. s_temp = ((uint8_t *)data)[i++];
  165. rbuf32_put(&ups_tx_rbuf, s_temp);
  166. } else {
  167. break;
  168. }
  169. }
  170. UPS_USART->CR1 |= USART_CR1_TXEIE;
  171. return i;
  172. }
  173. int ups_getchar(unsigned int timeout)
  174. {
  175. int res;
  176. res = xSemaphoreTake( xSem_rx_buf, (TickType_t)timeout );
  177. if (res == pdFALSE) {
  178. return -1;
  179. }
  180. rbuf32_get(&ups_rx_rbuf, (uint32_t *)&res);
  181. // DBG printf("STOP \r\n");
  182. // DBG printf("wr: %d 0x%x\r\n", rs485_rx_rbuf.read_index,res);
  183. return res;
  184. }
  185. void service_putchar(uint16_t byte)
  186. {
  187. USER_USART->CR1 &= ~USART_CR1_TXEIE;
  188. if (!rbuf_isfull(&user_tx_rbuf)) {
  189. rbuf32_put(&user_tx_rbuf, (uint32_t)(byte));
  190. }
  191. USER_USART->CR1 |= USART_CR1_TXEIE;
  192. }
  193. int service_send_block(void *data, uint8_t len)
  194. {
  195. int i = 0;
  196. uint32_t s_temp = 0;
  197. USER_USART->CR1 &= ~USART_CR1_TXEIE;
  198. //DBG printf("STOP \r\n");
  199. while (i < len) {
  200. if (!rbuf_isfull(&user_tx_rbuf)) {
  201. s_temp = ((uint8_t *)data)[i++];
  202. rbuf32_put(&user_tx_rbuf, s_temp);
  203. } else {
  204. break;
  205. }
  206. }
  207. USER_USART->CR1 |= USART_CR1_TXEIE;
  208. return i;
  209. }
  210. int service_getchar(unsigned int timeout)
  211. {
  212. int res;
  213. res = xSemaphoreTake( xSem_user_rx_buf, (TickType_t)timeout );
  214. if (res == pdFALSE) {
  215. return -1;
  216. }
  217. rbuf32_get(&user_rx_rbuf, (uint32_t *)&res);
  218. // DBG printf("STOP \r\n");
  219. // DBG printf("wr: %d 0x%x\r\n", rs485_rx_rbuf.read_index,res);
  220. return res;
  221. }
  222. void putchar_(uint8_t c)
  223. {
  224. while (!(USER_USART->SR & USART_FLAG_TXE));
  225. USER_USART->DR = (uint16_t)c;
  226. }
  227. void putc_(void *p, char c)
  228. {
  229. (void)p;
  230. putchar_(c);
  231. }
  232. //inline void rs232_irq_handler(void)
  233. void service_rs232_irq_handler(void)
  234. {
  235. uint32_t c = 0;
  236. static BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  237. if ((USER_USART->SR & USART_SR_ORE)) {
  238. c = (uint32_t)USER_USART->DR;
  239. //DBG printf("overrunRS485\r\n");
  240. }
  241. if (USART_GetITStatus(USER_USART, USART_IT_TXE) != RESET) {
  242. if (rbuf32_get(&user_tx_rbuf, &c)) {
  243. USER_USART->DR = (uint16_t)c;
  244. //DBG printf("wr: %d 0x%x\r\n", rs485_tx_rbuf.read_index,c);
  245. } else {
  246. USER_USART->CR1 &= ~USART_CR1_TXEIE;
  247. USER_USART->CR1 |= USART_CR1_RXNEIE;
  248. }
  249. USART_ClearITPendingBit(USER_USART, USART_IT_TXE);
  250. }
  251. if (USART_GetITStatus(USER_USART, USART_IT_RXNE) != RESET) {
  252. c = (uint32_t)USER_USART->DR;
  253. //DBG printf("read: %d 0x%x\r\n", rs485_rx_rbuf.write_index,c);
  254. if (!rbuf_isfull(&user_rx_rbuf)) {
  255. rbuf32_put(&user_rx_rbuf, c);
  256. }
  257. USART_ClearITPendingBit(USER_USART, USART_IT_RXNE);
  258. xSemaphoreGiveFromISR(xSem_user_rx_buf, &xHigherPriorityTaskWoken);
  259. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  260. }
  261. }
  262. //inline void rs232_irq_handler(void)
  263. void rs232_irq_handler(void)
  264. {
  265. uint32_t c = 0;
  266. static BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  267. if ((UPS_USART->SR & USART_SR_ORE)) {
  268. c = (uint32_t)UPS_USART->DR;
  269. //DBG printf("overrunRS485\r\n");
  270. }
  271. if (USART_GetITStatus(UPS_USART, USART_IT_TXE) != RESET) {
  272. if (rbuf32_get(&ups_tx_rbuf, &c)) {
  273. UPS_USART->DR = (uint16_t)c;
  274. //DBG printf("wr: %d 0x%x\r\n", rs485_tx_rbuf.read_index,c);
  275. } else {
  276. UPS_USART->CR1 &= ~USART_CR1_TXEIE;
  277. UPS_USART->CR1 |= USART_CR1_RXNEIE;
  278. }
  279. USART_ClearITPendingBit(UPS_USART, USART_IT_TXE);
  280. }
  281. if (USART_GetITStatus(UPS_USART, USART_IT_RXNE) != RESET) {
  282. c = (uint32_t)UPS_USART->DR;
  283. //DBG printf("read: %d 0x%x\r\n", rs485_rx_rbuf.write_index,c);
  284. if (!rbuf_isfull(&ups_rx_rbuf)) {
  285. rbuf32_put(&ups_rx_rbuf, c);
  286. }
  287. USART_ClearITPendingBit(UPS_USART, USART_IT_RXNE);
  288. xSemaphoreGiveFromISR(xSem_rx_buf, &xHigherPriorityTaskWoken);
  289. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  290. }
  291. }
  292. #ifdef PORTGW_ENABLE
  293. uint32_t rs485_send_block(uint8_t *data, uint16_t len) {
  294. uint16_t i = 0;
  295. while (i < len) {
  296. if (xQueueSend(rs485TxQ, &data[i++], 1000) != pdTRUE) {
  297. return i;
  298. }
  299. }
  300. rs485_enable_tx();
  301. return i;
  302. }
  303. inline void rs485_enable_tx(void)
  304. {
  305. RS485_USART->CR1 |= USART_CR1_TXEIE;
  306. }
  307. inline void rs485_irq_handler(void)
  308. {
  309. uint16_t c;
  310. static BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  311. if (RS485_USART->SR & USART_SR_ORE) {
  312. c = RS485_USART->DR;
  313. DBG printf("[rs485] overrun\r\n");
  314. }
  315. if (RS485_USART->SR & USART_SR_PE) {
  316. c = RS485_USART->DR;
  317. DBG printf("[rs485] parity fail: 0x%X\r\n", c);
  318. }
  319. if (USART_GetITStatus(RS485_USART, USART_IT_TXE) != RESET) {
  320. if (xQueueReceiveFromISR(rs485TxQ, &c, &xHigherPriorityTaskWoken) == pdTRUE) {
  321. RS485_USART->DR = c;
  322. // DBG printf("tx: 0x%x\r\n", c);
  323. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  324. } else {
  325. RS485_USART->CR1 &= ~USART_CR1_TXEIE;
  326. RS485_USART->CR1 |= USART_CR1_RXNEIE;
  327. }
  328. USART_ClearITPendingBit(RS485_USART, USART_IT_TXE);
  329. }
  330. if (USART_GetITStatus(RS485_USART, USART_IT_RXNE) != RESET) {
  331. c = RS485_USART->DR;
  332. /* Mask parity bits */
  333. if (RS485_USART->CR1 & USART_CR1_PCE) {
  334. if (RS485_USART->CR1 & USART_CR1_M) {
  335. /* 8-bit data */
  336. c &= 0xFF;
  337. } else {
  338. /* 7-bit data */
  339. c &= 0x7F;
  340. }
  341. }
  342. if (xQueueSendFromISR(rs485RxQ, &c, &xHigherPriorityTaskWoken) == pdTRUE) {
  343. // DBG printf("rx: 0x%x\r\n", c);
  344. } else {
  345. DBG printf("[rs485] rx overflow\r\n");
  346. }
  347. USART_ClearITPendingBit(RS485_USART, USART_IT_RXNE);
  348. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  349. }
  350. }
  351. #endif /* RS485_USART */
  352. void USART1_IRQHandler(void)
  353. {
  354. service_rs232_irq_handler();
  355. }
  356. void USART2_IRQHandler(void)
  357. {
  358. rs232_irq_handler();
  359. }
  360. void USART3_IRQHandler(void)
  361. {
  362. #ifdef PORTGW_ENABLE
  363. rs485_irq_handler();
  364. #endif /* RS485_USART */
  365. }
  366. /********************************* (C) РОТЕК **********************************/