usart.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 InitUSART(void) {
  92. xSem_rx_buf = xSemaphoreCreateCounting( UPS_RBUF_SIZE, 0 );
  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. uart_hw_init(UPS_USART, UPS_USART_BAUD, UPS_USART_WORD_LEN, UPS_USART_PARITY, UPS_USART_STOP_BIT);
  96. xSem_user_rx_buf = xSemaphoreCreateCounting( USER_RBUF_SIZE, 0 );
  97. rbuf32_init(&user_tx_rbuf, user_tx_fifo, USER_RBUF_SIZE);
  98. rbuf32_init(&user_rx_rbuf, user_rx_fifo, USER_RBUF_SIZE);
  99. uart_hw_init(USER_USART, USER_USART_BAUD, USER_USART_WORD_LEN, USER_USART_PARITY, USER_USART_STOP_BIT);
  100. #ifdef PRINTF_CUSTOM
  101. init_printf(NULL, putc_);
  102. #endif
  103. }
  104. void ups_putchar(uint16_t byte) {
  105. UPS_USART->CR1 &= ~USART_CR1_TXEIE;
  106. if (!rbuf_isfull(&ups_tx_rbuf)) {
  107. rbuf32_put(&ups_tx_rbuf, (uint32_t)(byte));
  108. }
  109. UPS_USART->CR1 |= USART_CR1_TXEIE;
  110. }
  111. int ups_send_block(void *data, uint8_t len) {
  112. int i = 0;
  113. uint32_t s_temp = 0;
  114. UPS_USART->CR1 &= ~USART_CR1_TXEIE;
  115. //DBG printf("STOP \r\n");
  116. while (i < len) {
  117. if (!rbuf_isfull(&ups_tx_rbuf)) {
  118. s_temp = ((uint8_t *)data)[i++];
  119. rbuf32_put(&ups_tx_rbuf, s_temp);
  120. }
  121. else
  122. break;
  123. }
  124. UPS_USART->CR1 |= USART_CR1_TXEIE;
  125. return i;
  126. }
  127. int ups_getchar(unsigned int timeout) {
  128. int res;
  129. res = xSemaphoreTake( xSem_rx_buf, (TickType_t)timeout );
  130. if (res == pdFALSE) {
  131. return -1;
  132. }
  133. rbuf32_get(&ups_rx_rbuf, (uint32_t*)&res);
  134. // DBG printf("STOP \r\n");
  135. // DBG printf("wr: %d 0x%x\r\n", rs485_rx_rbuf.read_index,res);
  136. return res;
  137. }
  138. void service_putchar(uint16_t byte) {
  139. USER_USART->CR1 &= ~USART_CR1_TXEIE;
  140. if (!rbuf_isfull(&user_tx_rbuf)) {
  141. rbuf32_put(&user_tx_rbuf, (uint32_t)(byte));
  142. }
  143. USER_USART->CR1 |= USART_CR1_TXEIE;
  144. }
  145. int service_send_block(void *data, uint8_t len) {
  146. int i = 0;
  147. uint32_t s_temp = 0;
  148. USER_USART->CR1 &= ~USART_CR1_TXEIE;
  149. //DBG printf("STOP \r\n");
  150. while (i < len) {
  151. if (!rbuf_isfull(&user_tx_rbuf)) {
  152. s_temp = ((uint8_t *)data)[i++];
  153. rbuf32_put(&user_tx_rbuf, s_temp);
  154. }
  155. else
  156. break;
  157. }
  158. USER_USART->CR1 |= USART_CR1_TXEIE;
  159. return i;
  160. }
  161. int service_getchar(unsigned int timeout) {
  162. int res;
  163. res = xSemaphoreTake( xSem_user_rx_buf, (TickType_t)timeout );
  164. if (res == pdFALSE) {
  165. return -1;
  166. }
  167. rbuf32_get(&user_rx_rbuf, (uint32_t*)&res);
  168. // DBG printf("STOP \r\n");
  169. // DBG printf("wr: %d 0x%x\r\n", rs485_rx_rbuf.read_index,res);
  170. return res;
  171. }
  172. void putchar_(uint8_t c) {
  173. while (!(USER_USART->SR & USART_FLAG_TXE));
  174. USER_USART->DR = (uint16_t)c;
  175. }
  176. void putc_(void* p, char c) {
  177. (void)p;
  178. putchar_(c);
  179. }
  180. //inline void rs232_irq_handler(void)
  181. void service_rs232_irq_handler(void)
  182. {
  183. uint32_t c = 0;
  184. static BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  185. if ((USER_USART->SR & USART_SR_ORE)) {
  186. c = (uint32_t)USER_USART->DR;
  187. //DBG printf("overrunRS485\r\n");
  188. }
  189. if (USART_GetITStatus(USER_USART, USART_IT_TXE) != RESET) {
  190. if(rbuf32_get(&user_tx_rbuf, &c)) {
  191. USER_USART->DR = (uint16_t)c;
  192. //DBG printf("wr: %d 0x%x\r\n", rs485_tx_rbuf.read_index,c);
  193. }
  194. else {
  195. USER_USART->CR1 &= ~USART_CR1_TXEIE;
  196. USER_USART->CR1 |= USART_CR1_RXNEIE;
  197. }
  198. USART_ClearITPendingBit(USER_USART, USART_IT_TXE);
  199. }
  200. if(USART_GetITStatus(USER_USART, USART_IT_RXNE) != RESET)
  201. {
  202. c = (uint32_t)USER_USART->DR;
  203. //DBG printf("read: %d 0x%x\r\n", rs485_rx_rbuf.write_index,c);
  204. if (!rbuf_isfull(&user_rx_rbuf))
  205. rbuf32_put(&user_rx_rbuf, c);
  206. USART_ClearITPendingBit(USER_USART, USART_IT_RXNE);
  207. xSemaphoreGiveFromISR(xSem_user_rx_buf, &xHigherPriorityTaskWoken);
  208. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  209. }
  210. }
  211. //inline void rs232_irq_handler(void)
  212. void rs232_irq_handler(void)
  213. {
  214. uint32_t c = 0;
  215. static BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  216. if ((UPS_USART->SR & USART_SR_ORE)) {
  217. c = (uint32_t)UPS_USART->DR;
  218. //DBG printf("overrunRS485\r\n");
  219. }
  220. if (USART_GetITStatus(UPS_USART, USART_IT_TXE) != RESET) {
  221. if(rbuf32_get(&ups_tx_rbuf, &c)) {
  222. UPS_USART->DR = (uint16_t)c;
  223. //DBG printf("wr: %d 0x%x\r\n", rs485_tx_rbuf.read_index,c);
  224. }
  225. else {
  226. UPS_USART->CR1 &= ~USART_CR1_TXEIE;
  227. UPS_USART->CR1 |= USART_CR1_RXNEIE;
  228. }
  229. USART_ClearITPendingBit(UPS_USART, USART_IT_TXE);
  230. }
  231. if(USART_GetITStatus(UPS_USART, USART_IT_RXNE) != RESET)
  232. {
  233. c = (uint32_t)UPS_USART->DR;
  234. //DBG printf("read: %d 0x%x\r\n", rs485_rx_rbuf.write_index,c);
  235. if (!rbuf_isfull(&ups_rx_rbuf))
  236. rbuf32_put(&ups_rx_rbuf, c);
  237. USART_ClearITPendingBit(UPS_USART, USART_IT_RXNE);
  238. xSemaphoreGiveFromISR(xSem_rx_buf, &xHigherPriorityTaskWoken);
  239. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  240. }
  241. }
  242. void USART1_IRQHandler(void) {
  243. service_rs232_irq_handler();
  244. }
  245. void USART2_IRQHandler(void) {
  246. rs232_irq_handler();
  247. }
  248. void USART3_IRQHandler(void) {
  249. }
  250. /********************************* (C) РОТЕК **********************************/