bsp_serial.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #include "bsp_serial.h"
  2. #ifdef BSP_USING_UART1
  3. Serial serial1;
  4. void USART1_IRQHandler( void )
  5. {
  6. _uartIsr( &serial1 );
  7. }
  8. #endif
  9. #ifdef BSP_USING_UART2
  10. Serial serial2;
  11. //void USART2_IRQHandler(void)
  12. //{
  13. // _uartIsr(&serial2);
  14. // }
  15. #endif
  16. #ifdef BSP_USING_UART3
  17. Serial serial3;
  18. void USART3_IRQHandler( void )
  19. {
  20. _uartIsr( &serial3 );
  21. }
  22. #endif
  23. #ifdef BSP_USING_UART4
  24. Serial serial4;
  25. void UART4_IRQHandler( void )
  26. {
  27. _uartIsr( &serial4 );
  28. }
  29. #endif
  30. #ifdef BSP_USING_UART5
  31. Serial serial5;
  32. void UART5_IRQHandler( void )
  33. {
  34. _uartIsr( &serial5 );
  35. }
  36. #endif
  37. /*
  38. static int _serialInit(Serial *serial, UART_HandleTypeDef *huart)
  39. {
  40. rkfifo_init(&serial->_rxFifo, serial->_pRxBuff, sizeof(serial->_pRxBuff), 1);
  41. rkfifo_init(&serial->_txFifo, serial->_pTxBuff, sizeof(serial->_pTxBuff), 1);
  42. #ifdef BSP_UART_USING_DMA
  43. serial->_dmarx_remaining_cnt = sizeof(serial->_pRxBuff);
  44. if (HAL_UART_Receive_DMA(serial->huart, serial->_rxFifo.data, sizeof(serial->_rxFifo)) != HAL_OK)
  45. {
  46. Error_Handler();
  47. }
  48. CLEAR_BIT(serial->huart->Instance->CR3, USART_CR3_EIE);
  49. __HAL_UART_ENABLE_IT(serial->huart, UART_IT_IDLE);
  50. #endif
  51. serial->huart = huart;
  52. __HAL_UART_ENABLE_IT(serial->huart, UART_IT_RXNE);
  53. return 0;
  54. }
  55. static int _serialDeinit(Serial *serial)
  56. {
  57. assert_param(serial != 0);
  58. __HAL_UART_DISABLE_IT(serial->huart, UART_IT_RXNE);
  59. return 0;
  60. }
  61. */
  62. /**
  63. * @brief 串口发送数据,非阻塞式发送
  64. *
  65. * @param serial 串口对象
  66. * @param pTxData 发送数据指针
  67. * @param txLen 要发送长度
  68. * @return uint32_t 发送成功字节数
  69. */
  70. uint32_t bspSerialWrite( Serial *serial, const uint8_t *pTxData, uint32_t txLen )
  71. {
  72. uint32_t ret = 0;
  73. ret = rkfifo_in( &serial->_txFifo, pTxData, txLen );
  74. if ( ret )
  75. {
  76. __HAL_UART_ENABLE_IT( serial->huart, UART_IT_TXE );
  77. }
  78. return ret;
  79. }
  80. int bspSerialReadReady( Serial *serial, uint32_t timeOut )
  81. {
  82. int ret = 0;
  83. return ret;
  84. }
  85. int bspSerialWriteDone( Serial *serial, uint32_t timeOut )
  86. {
  87. int ret = 0;
  88. return ret;
  89. }
  90. /**
  91. * @brief 串口读取数据
  92. *
  93. * @param serial 串口对象
  94. * @param pRxData 读取数据指针
  95. * @param rxLen 要读取长度
  96. * @return uint32_t 读取成功字节数
  97. */
  98. uint32_t bspSerialRead( Serial *serial, uint8_t *pRxData, uint32_t rxLen )
  99. {
  100. uint32_t rnum = rkfifo_out( &serial->_rxFifo, pRxData, rxLen );
  101. return rnum;
  102. }
  103. static inline int _stm32_getc( Serial *serial )
  104. {
  105. int ch = -1;
  106. UART_HandleTypeDef *handle = serial->huart;
  107. if ( __HAL_UART_GET_FLAG( handle, UART_FLAG_RXNE ) != RESET )
  108. {
  109. ch = handle->Instance->DR & 0xff;
  110. }
  111. return ch;
  112. }
  113. static inline int _stm32_putc( Serial *serial, uint8_t ch )
  114. {
  115. UART_HandleTypeDef *handle = serial->huart;
  116. while ( __HAL_UART_GET_FLAG( handle, UART_FLAG_TC ) == RESET )
  117. ;
  118. serial->huart->Instance->DR = ch;
  119. return 0;
  120. }
  121. static int _serialRxneIsrCallback( Serial *serial )
  122. {
  123. int ret = 0;
  124. int ch = _stm32_getc( serial );
  125. if ( ch != -1 && serial )
  126. {
  127. uint8_t c = ch & 0xff;
  128. ret = rkfifo_in( &serial->_rxFifo, &c, 1 );
  129. }
  130. return ret;
  131. }
  132. static int _serialTxeIsrCallback( Serial *serial )
  133. {
  134. int ret = 0;
  135. uint8_t c;
  136. if ( rkfifo_out( &serial->_txFifo, &c, 1 ) )
  137. {
  138. _stm32_putc( serial, c );
  139. ret = 1;
  140. }
  141. return ret;
  142. }
  143. static int _serialTcIsrCallback( Serial *serial )
  144. {
  145. int ret = 0;
  146. return ret;
  147. }
  148. #ifdef BSP_UART_USING_DMA
  149. static void _serialDmaRecvIsr( Serial *serial )
  150. {
  151. }
  152. #endif
  153. void _uartIsr( Serial *serial )
  154. {
  155. UART_HandleTypeDef *handle = serial->huart;
  156. if ( ( __HAL_UART_GET_FLAG( handle, UART_FLAG_RXNE ) != RESET ) &&
  157. ( __HAL_UART_GET_IT_SOURCE( handle, UART_IT_RXNE ) != RESET ) )
  158. {
  159. _serialRxneIsrCallback( serial );
  160. __HAL_UART_CLEAR_FLAG( handle, UART_FLAG_RXNE );
  161. }
  162. else if ( ( __HAL_UART_GET_FLAG( handle, UART_FLAG_TXE ) != RESET ) &&
  163. ( __HAL_UART_GET_IT_SOURCE( handle, UART_IT_TXE ) ) != RESET )
  164. {
  165. int ret = _serialTxeIsrCallback( serial );
  166. if ( ret == 0 )
  167. {
  168. __HAL_UART_DISABLE_IT( serial->huart, UART_IT_TXE );
  169. __HAL_UART_ENABLE_IT( serial->huart, UART_IT_TC );
  170. }
  171. __HAL_UART_CLEAR_FLAG( handle, UART_IT_TXE );
  172. }
  173. else if ( __HAL_UART_GET_FLAG( handle, UART_FLAG_TC ) &&
  174. ( __HAL_UART_GET_IT_SOURCE( handle, UART_IT_TC ) != RESET ) )
  175. {
  176. _serialTcIsrCallback( serial );
  177. __HAL_UART_CLEAR_FLAG( handle, UART_IT_TC );
  178. __HAL_UART_DISABLE_IT( handle, UART_IT_TC );
  179. }
  180. else
  181. {
  182. if ( __HAL_UART_GET_FLAG( handle, UART_FLAG_ORE ) != RESET )
  183. {
  184. __HAL_UART_CLEAR_OREFLAG( handle );
  185. }
  186. if ( __HAL_UART_GET_FLAG( handle, UART_FLAG_NE ) != RESET )
  187. {
  188. __HAL_UART_CLEAR_NEFLAG( handle );
  189. }
  190. if ( __HAL_UART_GET_FLAG( handle, UART_FLAG_FE ) != RESET )
  191. {
  192. __HAL_UART_CLEAR_FEFLAG( handle );
  193. }
  194. if ( __HAL_UART_GET_FLAG( handle, UART_FLAG_PE ) != RESET )
  195. {
  196. __HAL_UART_CLEAR_PEFLAG( handle );
  197. }
  198. if ( __HAL_UART_GET_FLAG( handle, UART_FLAG_IDLE ) != RESET )
  199. {
  200. __HAL_UART_CLEAR_IDLEFLAG( handle );
  201. }
  202. if ( __HAL_UART_GET_FLAG( handle, UART_FLAG_CTS ) != RESET )
  203. {
  204. __HAL_UART_CLEAR_FLAG( handle, UART_FLAG_CTS );
  205. }
  206. if ( __HAL_UART_GET_FLAG( handle, UART_FLAG_TXE ) != RESET )
  207. {
  208. __HAL_UART_CLEAR_FLAG( handle, UART_FLAG_TXE );
  209. }
  210. if ( __HAL_UART_GET_FLAG( handle, UART_FLAG_TC ) != RESET )
  211. {
  212. __HAL_UART_CLEAR_FLAG( handle, UART_FLAG_TC );
  213. }
  214. if ( __HAL_UART_GET_FLAG( handle, UART_FLAG_RXNE ) != RESET )
  215. {
  216. __HAL_UART_CLEAR_FLAG( handle, UART_FLAG_RXNE );
  217. }
  218. }
  219. }
  220. void bspSerialInit( void )
  221. {
  222. #ifdef BSP_USING_UART1
  223. extern UART_HandleTypeDef huart1;
  224. _serialInit( &serial1, &huart1 );
  225. #endif
  226. #ifdef BSP_USING_UART2
  227. extern UART_HandleTypeDef huart2;
  228. _serialInit( &serial2, &huart2 );
  229. #endif
  230. #ifdef BSP_USING_UART3
  231. extern UART_HandleTypeDef huart3;
  232. _serialInit( &serial3, &huart3 );
  233. #endif
  234. #ifdef BSP_USING_UART4
  235. extern UART_HandleTypeDef huart4;
  236. _serialInit( &serial4, &huart4 );
  237. #endif
  238. #ifdef BSP_USING_UART5
  239. extern UART_HandleTypeDef huart5;
  240. _serialInit( &serial5, &huart5 );
  241. #endif
  242. }