usart.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * File : usart.c
  3. * This file is part of RT-Thread RTOS
  4. *
  5. * The license and distribution terms for this file may be
  6. * found in the file LICENSE in this distribution or at
  7. * http://www.rt-thread.org/license/LICENSE
  8. *
  9. * Change Logs:
  10. * Date Author Notes
  11. * 2017-12-12 Bluebear233 first implementation
  12. */
  13. #include <rtconfig.h>
  14. #include <rtdevice.h>
  15. #include <usart.h>
  16. #include <NUC472_442.h>
  17. /* Private Define ---------------------------------------------------------------*/
  18. #define USEING_UART0 //Tx:PG2 Rx:PG1
  19. /* Private Typedef --------------------------------------------------------------*/
  20. struct usart
  21. {
  22. rt_serial_t dev;
  23. UART_T *usart_base;
  24. };
  25. typedef struct usart* usart_t;
  26. /* Private functions ------------------------------------------------------------*/
  27. static rt_err_t usart_gpio_configure(struct rt_serial_device *serial);
  28. static rt_err_t usart_configure(struct rt_serial_device *serial, struct serial_configure *cfg);
  29. static rt_err_t usart_control(struct rt_serial_device *serial, int cmd, void *arg);
  30. static int usart_send(struct rt_serial_device *serial, char c);
  31. static int usart_receive(struct rt_serial_device *serial);
  32. static void rt_hw_uart_register(usart_t uart, UART_T *uart_base,
  33. char *name);
  34. static void uart_isr(usart_t serial);
  35. //static rt_err_t stm32_uart_tx_dma_configure(rt_stm32_uart_t *serial, rt_bool_t bool);
  36. //static rt_err_t stm32_uart_tx_dma_nvic(rt_stm32_uart_t *serial, rt_bool_t bool);
  37. /* Private Variables ------------------------------------------------------------*/
  38. static const struct rt_uart_ops stm32_uart_ops =
  39. { usart_configure, usart_control, usart_send, usart_receive,
  40. RT_NULL };
  41. static const struct serial_configure stm32_uart_default_config =
  42. RT_SERIAL_CONFIG_DEFAULT;
  43. #ifdef USEING_UART0
  44. struct usart uart0;
  45. #endif
  46. /* Interrupt Handle Funtion ----------------------------------------------------*/
  47. #ifdef USEING_UART0
  48. /* 串口0 中断入口 */
  49. void UART0_IRQHandler(void)
  50. {
  51. uart_isr(&uart0);
  52. }
  53. #endif
  54. /**
  55. * 中断处理函数
  56. */
  57. static void uart_isr(usart_t serial)
  58. {
  59. // 获取串口基地址
  60. UART_T *uart_base = ((usart_t)serial)->usart_base;
  61. // 获取中断事件
  62. uint32_t u32IntSts= uart_base->INTSTS;
  63. // 接收中断
  64. if(u32IntSts & (UART_INTSTS_RDAINT_Msk | UART_INTSTS_RXTOINT_Msk))
  65. {
  66. rt_hw_serial_isr(&serial->dev, RT_SERIAL_EVENT_RX_IND);
  67. }
  68. }
  69. /**
  70. * 串口端口配置
  71. */
  72. static rt_err_t usart_gpio_configure(struct rt_serial_device *serial)
  73. {
  74. // 获取串口基地址
  75. UART_T *uart_base = ((usart_t)serial)->usart_base;
  76. switch((uint32_t)uart_base)
  77. {
  78. case UART0_BASE:
  79. SYS->GPG_MFPL = SYS_GPG_MFPL_PG1MFP_UART0_RXD | SYS_GPG_MFPL_PG2MFP_UART0_TXD;
  80. break;
  81. default:
  82. rt_kprintf("unknow uart module\n");
  83. RT_ASSERT(0);
  84. }
  85. return RT_EOK;
  86. }
  87. /**
  88. * 串口配置
  89. */
  90. static rt_err_t usart_configure(struct rt_serial_device *serial,
  91. struct serial_configure *cfg)
  92. {
  93. // 获取串口基地址
  94. UART_T *uart_base = ((usart_t)serial)->usart_base;
  95. uint32_t uart_module = 0;
  96. uint32_t uart_word_len = 0;
  97. uint32_t uart_stop_bit = 0;
  98. uint32_t uart_parity = 0;
  99. uint32_t uart_irq_channel = 0;
  100. switch((uint32_t)uart_base)
  101. {
  102. case UART0_BASE:
  103. uart_module = UART0_MODULE;
  104. uart_irq_channel = UART0_IRQn;
  105. break;
  106. default:
  107. rt_kprintf("unknow uart module\n");
  108. RT_ASSERT(0);
  109. }
  110. /* Enable IP clock */
  111. CLK_EnableModuleClock(uart_module);
  112. /* Select IP clock source */
  113. CLK_SetModuleClock(uart_module, CLK_CLKSEL1_UARTSEL_HXT, CLK_CLKDIV0_UART(1));
  114. /* check baudrate */
  115. RT_ASSERT(cfg->baud_rate != 0);
  116. /* check word len */
  117. switch(cfg->data_bits)
  118. {
  119. case DATA_BITS_5:
  120. uart_word_len = UART_WORD_LEN_5;
  121. break;
  122. case DATA_BITS_6:
  123. uart_word_len = UART_WORD_LEN_6;
  124. break;
  125. case DATA_BITS_7:
  126. uart_word_len = UART_WORD_LEN_7;
  127. break;
  128. case DATA_BITS_8:
  129. uart_word_len = UART_WORD_LEN_8;
  130. break;
  131. default:
  132. rt_kprintf("unsupose data len");
  133. RT_ASSERT(0);
  134. }
  135. /* check stop bit */
  136. switch(cfg->stop_bits)
  137. {
  138. case STOP_BITS_1:
  139. uart_stop_bit = UART_STOP_BIT_1;
  140. break;
  141. case STOP_BITS_2:
  142. uart_stop_bit = UART_STOP_BIT_2;
  143. break;
  144. default:
  145. rt_kprintf("unsupose stop bit");
  146. RT_ASSERT(0);
  147. }
  148. /* check stop bit */
  149. switch(cfg->parity)
  150. {
  151. case PARITY_NONE:
  152. uart_parity = UART_PARITY_NONE;
  153. break;
  154. case PARITY_ODD:
  155. uart_parity = UART_PARITY_ODD;
  156. break;
  157. case PARITY_EVEN:
  158. uart_parity = UART_PARITY_EVEN;
  159. break;
  160. default:
  161. rt_kprintf("unsupose parity");
  162. RT_ASSERT(0);
  163. }
  164. /* Open uart */
  165. {
  166. uint8_t u8UartClkSrcSel;
  167. uint32_t u32ClkTbl[4] = {__HXT, 0, __HIRC, __HIRC};
  168. uint32_t u32Clk;
  169. uint32_t u32Baud_Div;
  170. u32ClkTbl[1] = CLK_GetPLLClockFreq();
  171. u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UARTSEL_Msk) >> CLK_CLKSEL1_UARTSEL_Pos;
  172. uart_base->FUNCSEL = UART_FUNCSEL_UART;
  173. uart_base->LINE = uart_word_len | uart_stop_bit | uart_parity;
  174. uart_base->FIFO = 6 << UART_FIFO_RFITL_Pos;
  175. uart_base->TOUT = (1 << UART_TOUT_DLY_Pos) | (40 << UART_TOUT_TOIC_Pos);
  176. u32Clk = (u32ClkTbl[u8UartClkSrcSel]) / (((CLK->CLKDIV0 & CLK_CLKDIV0_UARTDIV_Msk) >> CLK_CLKDIV0_UARTDIV_Pos) + 1);
  177. u32Baud_Div = UART_BAUD_MODE2_DIVIDER(u32Clk, cfg->baud_rate);
  178. if (u32Baud_Div > 0xFFFF)
  179. uart_base->BAUD = (UART_BAUD_MODE0 | UART_BAUD_MODE0_DIVIDER(u32Clk, cfg->baud_rate));
  180. else
  181. uart_base->BAUD = (UART_BAUD_MODE2 | u32Baud_Div);
  182. }
  183. /* config nvic */
  184. NVIC_EnableIRQ(uart_irq_channel);
  185. /* config gpio */
  186. usart_gpio_configure(serial);
  187. return RT_EOK;
  188. }
  189. /**
  190. * 串口中断控制
  191. */
  192. static rt_err_t usart_control(struct rt_serial_device *serial,
  193. int cmd, void *arg)
  194. {
  195. rt_err_t result = RT_EOK;
  196. rt_uint32_t flag;
  197. // 获取串口基地址
  198. UART_T *uart_base = ((usart_t)serial)->usart_base;
  199. switch ((uint32_t) arg)
  200. {
  201. case RT_DEVICE_FLAG_INT_RX:
  202. flag = UART_INTEN_RDAIEN_Msk | UART_INTEN_RXTOIEN_Msk | UART_INTEN_TOCNTEN_Msk;
  203. switch (cmd)
  204. {
  205. case RT_DEVICE_CTRL_CLR_INT:
  206. UART_DISABLE_INT(uart_base, flag);
  207. break;
  208. case RT_DEVICE_CTRL_SET_INT:
  209. UART_ENABLE_INT(uart_base, flag);
  210. break;
  211. default:
  212. RT_ASSERT(0);
  213. }
  214. break;
  215. // TODO 完善DMA接口
  216. // case RT_DEVICE_FLAG_DMA_TX:
  217. // USART_DMACmd(dev->usart_base, USART_DMAReq_Tx, ENABLE);
  218. // stm32_uart_tx_dma_configure(dev, RT_TRUE);
  219. // stm32_uart_tx_dma_nvic(dev, RT_TRUE);
  220. // break;
  221. default:
  222. RT_ASSERT(0)
  223. ;
  224. }
  225. return result;
  226. return RT_EOK;
  227. }
  228. /**
  229. * 串口发送函数
  230. */
  231. static int usart_send(struct rt_serial_device *serial, char c)
  232. {
  233. // 获取串口基地址
  234. UART_T *uart_base = ((usart_t)serial)->usart_base;
  235. // 等待FIFO 发送
  236. while(uart_base->FIFOSTS & UART_FIFOSTS_TXFULL_Msk);
  237. // 发送字符
  238. uart_base->DAT = c;
  239. return 1;
  240. }
  241. /**
  242. * 串口接收函数
  243. */
  244. static int usart_receive(struct rt_serial_device *serial)
  245. {
  246. // 获取串口基地址
  247. UART_T *uart_base = ((usart_t)serial)->usart_base;
  248. // 如果FIFO 为空返回
  249. if(uart_base->FIFOSTS & UART_FIFOSTS_RXEMPTY_Msk)
  250. {
  251. return -1;
  252. }
  253. return UART_READ(uart_base);
  254. }
  255. /**
  256. * @brief 串口设备注册
  257. * @param uart : UART设备结构体
  258. * @param uart_base : STM32 UART外设基地址
  259. * @param name : STM32 UART设备名
  260. * @param tx_dma_channel : STM32 UART TX的DMA通道基地址(可选)
  261. */
  262. static void rt_hw_uart_register(usart_t usart, UART_T * uart_base, char *name)
  263. {
  264. rt_uint32_t flag;
  265. RT_ASSERT(usart != RT_NULL);
  266. RT_ASSERT(uart_base != RT_NULL);
  267. // 没有定义对应的硬件I2C
  268. if (!(uart_base == UART0 || uart_base == UART1 || uart_base == UART2
  269. || uart_base == UART3 || uart_base == UART4 || uart_base == UART5))
  270. {
  271. RT_ASSERT(0);
  272. }
  273. usart->usart_base = uart_base;
  274. usart->dev.ops = &stm32_uart_ops;
  275. usart->dev.config = stm32_uart_default_config;
  276. flag = RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX;
  277. rt_hw_serial_register(&usart->dev, name,
  278. flag, RT_NULL);
  279. }
  280. /**
  281. * 硬件串口注册
  282. */
  283. void rt_hw_usart_init(void)
  284. {
  285. #ifdef USEING_UART0
  286. rt_hw_uart_register(&uart0, UART0, "uart0");
  287. #endif
  288. }