drv_usart.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-08-20 Abbcc first version
  9. */
  10. #include "board.h"
  11. #include "drv_usart.h"
  12. #ifdef RT_USING_SERIAL
  13. #if !defined(BSP_USING_UART1) && !defined(BSP_USING_UART2)
  14. #error "Please define at least one BSP_USING_UARTx"
  15. /* this driver can be disabled at menuconfig -> RT-Thread Components -> Device Drivers */
  16. #endif
  17. struct apm32_usart
  18. {
  19. const char *name;
  20. USART_T *usartx;
  21. IRQn_Type irq_type;
  22. struct rt_serial_device serial;
  23. };
  24. enum
  25. {
  26. #ifdef BSP_USING_UART1
  27. UART1_INDEX,
  28. #endif
  29. #ifdef BSP_USING_UART2
  30. UART2_INDEX,
  31. #endif
  32. };
  33. static struct apm32_usart usart_config[] =
  34. {
  35. #ifdef BSP_USING_UART1
  36. {
  37. "uart1",
  38. USART1,
  39. USART1_IRQn,
  40. },
  41. #endif
  42. #ifdef BSP_USING_UART2
  43. {
  44. "uart2",
  45. USART2,
  46. USART2_IRQn,
  47. },
  48. #endif
  49. };
  50. static rt_err_t _uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  51. {
  52. USART_Config_T USART_ConfigStruct;
  53. RT_ASSERT(serial != RT_NULL);
  54. RT_ASSERT(cfg != RT_NULL);
  55. struct apm32_usart *usart_instance = (struct apm32_usart *) serial->parent.user_data;
  56. apm32_usart_init();
  57. USART_ConfigStruct.baudRate = cfg->baud_rate;;
  58. USART_ConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE;
  59. USART_ConfigStruct.mode = USART_MODE_TX_RX;
  60. USART_ConfigStruct.parity = USART_PARITY_NONE;
  61. switch (cfg->data_bits)
  62. {
  63. case DATA_BITS_8:
  64. if (cfg->parity == PARITY_ODD || cfg->parity == PARITY_EVEN)
  65. USART_ConfigStruct.wordLength = USART_WORD_LEN_9B;
  66. else
  67. USART_ConfigStruct.wordLength = USART_WORD_LEN_8B;
  68. break;
  69. case DATA_BITS_9:
  70. USART_ConfigStruct.wordLength = USART_WORD_LEN_9B;
  71. break;
  72. default:
  73. USART_ConfigStruct.wordLength = USART_WORD_LEN_8B;
  74. break;
  75. }
  76. switch (cfg->stop_bits)
  77. {
  78. case STOP_BITS_1:
  79. USART_ConfigStruct.stopBits = USART_STOP_BIT_1;
  80. break;
  81. case STOP_BITS_2:
  82. USART_ConfigStruct.stopBits = USART_STOP_BIT_2;
  83. break;
  84. default:
  85. USART_ConfigStruct.stopBits = USART_STOP_BIT_1;
  86. break;
  87. }
  88. switch (cfg->parity)
  89. {
  90. case PARITY_NONE:
  91. USART_ConfigStruct.parity = USART_PARITY_NONE;
  92. break;
  93. case PARITY_ODD:
  94. USART_ConfigStruct.parity = USART_PARITY_ODD;
  95. break;
  96. case PARITY_EVEN:
  97. USART_ConfigStruct.parity = USART_PARITY_EVEN;
  98. break;
  99. default:
  100. USART_ConfigStruct.parity = USART_PARITY_NONE;
  101. break;
  102. }
  103. USART_Config(usart_instance->usartx, &USART_ConfigStruct);
  104. USART_Enable(usart_instance->usartx);
  105. return RT_EOK;
  106. }
  107. static rt_err_t _uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  108. {
  109. struct apm32_usart *usart;
  110. RT_ASSERT(serial != RT_NULL);
  111. usart = (struct apm32_usart *) serial->parent.user_data;
  112. RT_ASSERT(usart != RT_NULL);
  113. switch (cmd)
  114. {
  115. /* disable interrupt */
  116. case RT_DEVICE_CTRL_CLR_INT:
  117. /* disable rx irq */
  118. NVIC_DisableIRQRequest(usart->irq_type);
  119. /* disable interrupt */
  120. USART_DisableInterrupt(usart->usartx, USART_INT_RXBNE);
  121. break;
  122. /* enable interrupt */
  123. case RT_DEVICE_CTRL_SET_INT:
  124. /* enable rx irq */
  125. NVIC_EnableIRQRequest(usart->irq_type, 1, 0);
  126. /* enable interrupt */
  127. USART_EnableInterrupt(usart->usartx, USART_INT_RXBNE);
  128. break;
  129. }
  130. return RT_EOK;
  131. }
  132. static int _uart_putc(struct rt_serial_device *serial, char c)
  133. {
  134. struct apm32_usart *usart;
  135. RT_ASSERT(serial != RT_NULL);
  136. usart = (struct apm32_usart *) serial->parent.user_data;
  137. RT_ASSERT(usart != RT_NULL);
  138. USART_TxData(usart->usartx, (uint8_t) c);
  139. while (USART_ReadStatusFlag(usart->usartx, USART_FLAG_TXC) == RESET);
  140. return 1;
  141. }
  142. static int _uart_getc(struct rt_serial_device *serial)
  143. {
  144. int ch;
  145. struct apm32_usart *usart;
  146. RT_ASSERT(serial != RT_NULL);
  147. usart = (struct apm32_usart *) serial->parent.user_data;
  148. RT_ASSERT(usart != RT_NULL);
  149. ch = -1;
  150. if (USART_ReadStatusFlag(usart->usartx, USART_FLAG_RXBNE) != RESET)
  151. {
  152. ch = USART_RxData(usart->usartx);
  153. }
  154. return ch;
  155. }
  156. /**
  157. * Uart common interrupt process. This need add to usart ISR.
  158. *
  159. * @param serial serial device
  160. */
  161. static void usart_isr(struct rt_serial_device *serial)
  162. {
  163. struct apm32_usart *usart;
  164. RT_ASSERT(serial != RT_NULL);
  165. RT_ASSERT(serial != RT_NULL);
  166. usart = (struct apm32_usart *) serial->parent.user_data;
  167. RT_ASSERT(usart != RT_NULL);
  168. /* UART in mode Receiver */
  169. if ((USART_ReadStatusFlag(usart->usartx, USART_FLAG_RXBNE) != RESET) &&
  170. (USART_ReadIntFlag(usart->usartx, USART_INT_RXBNE) != RESET))
  171. {
  172. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  173. USART_ClearStatusFlag(usart->usartx, USART_FLAG_RXBNE);
  174. USART_ClearIntFlag(usart->usartx, USART_INT_RXBNE);
  175. }
  176. else
  177. {
  178. if (USART_ReadStatusFlag(usart->usartx, USART_FLAG_CTS) != RESET)
  179. {
  180. USART_ClearStatusFlag(usart->usartx, USART_FLAG_CTS);
  181. }
  182. if (USART_ReadStatusFlag(usart->usartx, USART_FLAG_LBD) != RESET)
  183. {
  184. USART_ClearStatusFlag(usart->usartx, USART_FLAG_LBD);
  185. }
  186. if (USART_ReadStatusFlag(usart->usartx, USART_FLAG_TXBE) != RESET)
  187. {
  188. USART_ClearStatusFlag(usart->usartx, USART_FLAG_TXBE);
  189. }
  190. }
  191. }
  192. #if defined(BSP_USING_UART1)
  193. void USART1_IRQHandler(void)
  194. {
  195. /* enter interrupt */
  196. rt_interrupt_enter();
  197. usart_isr(&(usart_config[UART1_INDEX].serial));
  198. /* leave interrupt */
  199. rt_interrupt_leave();
  200. }
  201. #endif /* BSP_USING_UART1 */
  202. #if defined(BSP_USING_UART2)
  203. void USART2_IRQHandler(void)
  204. {
  205. /* enter interrupt */
  206. rt_interrupt_enter();
  207. usart_isr(&(usart_config[UART2_INDEX].serial));
  208. /* leave interrupt */
  209. rt_interrupt_leave();
  210. }
  211. #endif /* BSP_USING_UART2 */
  212. static const struct rt_uart_ops apm32_usart_ops =
  213. {
  214. .configure = _uart_configure,
  215. .control = _uart_control,
  216. .putc = _uart_putc,
  217. .getc = _uart_getc,
  218. .dma_transmit = RT_NULL
  219. };
  220. int rt_hw_usart_init(void)
  221. {
  222. rt_size_t obj_num;
  223. int index;
  224. obj_num = sizeof(usart_config) / sizeof(struct apm32_usart);
  225. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  226. rt_err_t result = 0;
  227. for (index = 0; index < obj_num; index++)
  228. {
  229. usart_config[index].serial.ops = &apm32_usart_ops;
  230. usart_config[index].serial.config = config;
  231. /* register USART device */
  232. result = rt_hw_serial_register(&usart_config[index].serial,
  233. usart_config[index].name,
  234. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX
  235. | RT_DEVICE_FLAG_INT_TX, &usart_config[index]);
  236. RT_ASSERT(result == RT_EOK);
  237. }
  238. return result;
  239. }
  240. #endif /* RT_USING_SERIAL */