usart.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. {
  30. USART_SendData(USER_USART, (u8) ch);
  31. while(USART_GetFlagStatus(USER_USART, USART_FLAG_TXE) == RESET) {}
  32. return ch;
  33. }
  34. #endif
  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_hw_init(USART_TypeDef *uart, uint32_t baud, uint16_t wordlen, uint16_t parity, uint16_t stop) {
  51. uint16_t wordlen_tmp;
  52. if (wordlen == 8)
  53. wordlen_tmp = USART_WordLength_8b;
  54. if (wordlen == 9)
  55. wordlen_tmp = USART_WordLength_9b;
  56. USART_InitStructure.USART_BaudRate = baud;
  57. USART_InitStructure.USART_WordLength = wordlen_tmp;
  58. USART_InitStructure.USART_StopBits = stop;
  59. USART_InitStructure.USART_Parity = parity;
  60. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  61. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  62. USART_DeInit(uart);
  63. if (uart == USART1) {
  64. RCC->APB2ENR |= RCC_APB2Periph_USART1;
  65. NVIC_SetPriority(USART1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 0x05, 0));
  66. NVIC_EnableIRQ(USART1_IRQn);
  67. USART_Init(USART1, &USART_InitStructure);
  68. USART_Cmd(USART1, ENABLE);
  69. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  70. USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
  71. }
  72. if (uart == USART2) {
  73. RCC->APB1ENR |= RCC_APB1Periph_USART2;
  74. NVIC_SetPriority(USART2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 0x05, 0));
  75. NVIC_EnableIRQ(USART2_IRQn);
  76. USART_Init(USART2, &USART_InitStructure);
  77. USART_Cmd(USART2, ENABLE);
  78. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  79. USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
  80. }
  81. if (uart == USART3) {
  82. RCC->APB1ENR |= RCC_APB1Periph_USART3;
  83. NVIC_SetPriority(USART3_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 0x05, 0));
  84. NVIC_EnableIRQ(USART3_IRQn);
  85. USART_Init(USART3, &USART_InitStructure);
  86. USART_Cmd(USART3, ENABLE);
  87. USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
  88. USART_ITConfig(USART3, USART_IT_TXE, DISABLE);
  89. }
  90. }
  91. void init_ups_rbuf(void)
  92. {
  93. rbuf32_init(&ups_tx_rbuf, ups_tx_fifo, UPS_RBUF_SIZE);
  94. rbuf32_init(&ups_rx_rbuf, ups_rx_fifo, UPS_RBUF_SIZE);
  95. }
  96. void InitUSART(void) {
  97. xSem_rx_buf = xSemaphoreCreateCounting( UPS_RBUF_SIZE, 0 );
  98. rbuf32_init(&ups_tx_rbuf, ups_tx_fifo, UPS_RBUF_SIZE);
  99. rbuf32_init(&ups_rx_rbuf, ups_rx_fifo, UPS_RBUF_SIZE);
  100. uart_hw_init(UPS_USART, UPS_USART_BAUD, UPS_USART_WORD_LEN, UPS_USART_PARITY, UPS_USART_STOP_BIT);
  101. xSem_user_rx_buf = xSemaphoreCreateCounting( USER_RBUF_SIZE, 0 );
  102. rbuf32_init(&user_tx_rbuf, user_tx_fifo, USER_RBUF_SIZE);
  103. rbuf32_init(&user_rx_rbuf, user_rx_fifo, USER_RBUF_SIZE);
  104. uart_hw_init(USER_USART, USER_USART_BAUD, USER_USART_WORD_LEN, USER_USART_PARITY, USER_USART_STOP_BIT);
  105. #ifdef PRINTF_CUSTOM
  106. init_printf(NULL, putc_);
  107. #endif
  108. }
  109. void ups_putchar(uint16_t byte) {
  110. UPS_USART->CR1 &= ~USART_CR1_TXEIE;
  111. if (!rbuf_isfull(&ups_tx_rbuf)) {
  112. rbuf32_put(&ups_tx_rbuf, (uint32_t)(byte));
  113. }
  114. UPS_USART->CR1 |= USART_CR1_TXEIE;
  115. }
  116. int ups_send_block(void *data, uint8_t len) {
  117. int i = 0;
  118. uint32_t s_temp = 0;
  119. UPS_USART->CR1 &= ~USART_CR1_TXEIE;
  120. //DBG printf("STOP \r\n");
  121. while (i < len) {
  122. if (!rbuf_isfull(&ups_tx_rbuf)) {
  123. s_temp = ((uint8_t *)data)[i++];
  124. rbuf32_put(&ups_tx_rbuf, s_temp);
  125. }
  126. else
  127. break;
  128. }
  129. UPS_USART->CR1 |= USART_CR1_TXEIE;
  130. return i;
  131. }
  132. int ups_getchar(unsigned int timeout) {
  133. int res;
  134. res = xSemaphoreTake( xSem_rx_buf, (TickType_t)timeout );
  135. if (res == pdFALSE) {
  136. return -1;
  137. }
  138. rbuf32_get(&ups_rx_rbuf, (uint32_t*)&res);
  139. // DBG printf("STOP \r\n");
  140. // DBG printf("wr: %d 0x%x\r\n", rs485_rx_rbuf.read_index,res);
  141. return res;
  142. }
  143. void service_putchar(uint16_t byte) {
  144. USER_USART->CR1 &= ~USART_CR1_TXEIE;
  145. if (!rbuf_isfull(&user_tx_rbuf)) {
  146. rbuf32_put(&user_tx_rbuf, (uint32_t)(byte));
  147. }
  148. USER_USART->CR1 |= USART_CR1_TXEIE;
  149. }
  150. int service_send_block(void *data, uint8_t len) {
  151. int i = 0;
  152. uint32_t s_temp = 0;
  153. USER_USART->CR1 &= ~USART_CR1_TXEIE;
  154. //DBG printf("STOP \r\n");
  155. while (i < len) {
  156. if (!rbuf_isfull(&user_tx_rbuf)) {
  157. s_temp = ((uint8_t *)data)[i++];
  158. rbuf32_put(&user_tx_rbuf, s_temp);
  159. }
  160. else
  161. break;
  162. }
  163. USER_USART->CR1 |= USART_CR1_TXEIE;
  164. return i;
  165. }
  166. int service_getchar(unsigned int timeout) {
  167. int res;
  168. res = xSemaphoreTake( xSem_user_rx_buf, (TickType_t)timeout );
  169. if (res == pdFALSE) {
  170. return -1;
  171. }
  172. rbuf32_get(&user_rx_rbuf, (uint32_t*)&res);
  173. // DBG printf("STOP \r\n");
  174. // DBG printf("wr: %d 0x%x\r\n", rs485_rx_rbuf.read_index,res);
  175. return res;
  176. }
  177. void putchar_(uint8_t c) {
  178. while (!(USER_USART->SR & USART_FLAG_TXE));
  179. USER_USART->DR = (uint16_t)c;
  180. }
  181. void putc_(void* p, char c) {
  182. (void)p;
  183. putchar_(c);
  184. }
  185. //inline void rs232_irq_handler(void)
  186. void service_rs232_irq_handler(void)
  187. {
  188. uint32_t c = 0;
  189. static BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  190. if ((USER_USART->SR & USART_SR_ORE)) {
  191. c = (uint32_t)USER_USART->DR;
  192. //DBG printf("overrunRS485\r\n");
  193. }
  194. if (USART_GetITStatus(USER_USART, USART_IT_TXE) != RESET) {
  195. if(rbuf32_get(&user_tx_rbuf, &c)) {
  196. USER_USART->DR = (uint16_t)c;
  197. //DBG printf("wr: %d 0x%x\r\n", rs485_tx_rbuf.read_index,c);
  198. }
  199. else {
  200. USER_USART->CR1 &= ~USART_CR1_TXEIE;
  201. USER_USART->CR1 |= USART_CR1_RXNEIE;
  202. }
  203. USART_ClearITPendingBit(USER_USART, USART_IT_TXE);
  204. }
  205. if(USART_GetITStatus(USER_USART, USART_IT_RXNE) != RESET)
  206. {
  207. c = (uint32_t)USER_USART->DR;
  208. //DBG printf("read: %d 0x%x\r\n", rs485_rx_rbuf.write_index,c);
  209. if (!rbuf_isfull(&user_rx_rbuf))
  210. rbuf32_put(&user_rx_rbuf, c);
  211. USART_ClearITPendingBit(USER_USART, USART_IT_RXNE);
  212. xSemaphoreGiveFromISR(xSem_user_rx_buf, &xHigherPriorityTaskWoken);
  213. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  214. }
  215. }
  216. //inline void rs232_irq_handler(void)
  217. void rs232_irq_handler(void)
  218. {
  219. uint32_t c = 0;
  220. static BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  221. if ((UPS_USART->SR & USART_SR_ORE)) {
  222. c = (uint32_t)UPS_USART->DR;
  223. //DBG printf("overrunRS485\r\n");
  224. }
  225. if (USART_GetITStatus(UPS_USART, USART_IT_TXE) != RESET) {
  226. if(rbuf32_get(&ups_tx_rbuf, &c)) {
  227. UPS_USART->DR = (uint16_t)c;
  228. //DBG printf("wr: %d 0x%x\r\n", rs485_tx_rbuf.read_index,c);
  229. }
  230. else {
  231. UPS_USART->CR1 &= ~USART_CR1_TXEIE;
  232. UPS_USART->CR1 |= USART_CR1_RXNEIE;
  233. }
  234. USART_ClearITPendingBit(UPS_USART, USART_IT_TXE);
  235. }
  236. if(USART_GetITStatus(UPS_USART, USART_IT_RXNE) != RESET)
  237. {
  238. c = (uint32_t)UPS_USART->DR;
  239. //DBG printf("read: %d 0x%x\r\n", rs485_rx_rbuf.write_index,c);
  240. if (!rbuf_isfull(&ups_rx_rbuf))
  241. rbuf32_put(&ups_rx_rbuf, c);
  242. USART_ClearITPendingBit(UPS_USART, USART_IT_RXNE);
  243. xSemaphoreGiveFromISR(xSem_rx_buf, &xHigherPriorityTaskWoken);
  244. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  245. }
  246. }
  247. void USART1_IRQHandler(void) {
  248. service_rs232_irq_handler();
  249. }
  250. void USART2_IRQHandler(void) {
  251. rs232_irq_handler();
  252. }
  253. void USART3_IRQHandler(void) {
  254. }
  255. /********************************* (C) РОТЕК **********************************/