drv_uart.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * File : drv_uart.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-02-22 Tanek first version.
  23. */
  24. #include <rtthread.h>
  25. #include <rthw.h>
  26. #include <SMM_MPS2.h>
  27. #ifdef RT_USING_UART
  28. #ifndef RT_USING_DEVICE
  29. #error "you must define RT_USING_DEVICE with uart device"
  30. #else
  31. #include <rtdevice.h>
  32. #endif
  33. /* uart driver */
  34. struct v2m_uart
  35. {
  36. struct rt_serial_device serial;
  37. CMSDK_UART_TypeDef * uart_base;
  38. CMSDK_GPIO_TypeDef * rx_pingpio; // Pin GPIO
  39. CMSDK_GPIO_TypeDef * tx_pingpio;
  40. uint8_t rx_pinnum; // Pin Number
  41. uint8_t tx_pinnum;
  42. IRQn_Type uart_irq_rx;
  43. //IRQn_Type uart_irq_tx;
  44. };
  45. #ifdef RT_USING_UART0
  46. struct v2m_uart uart0_device;
  47. #endif
  48. #ifdef RT_USING_UART1
  49. struct v2m_uart uart1_device;
  50. #endif
  51. #ifdef RT_USING_UART2
  52. struct v2m_uart uart2_device;
  53. #endif
  54. static void uart_irq_handler(struct rt_serial_device *serial)
  55. {
  56. uint32_t status;
  57. struct v2m_uart *uart;
  58. uart = (struct v2m_uart *)serial->parent.user_data;
  59. /* enter interrupt */
  60. rt_interrupt_enter();
  61. status = uart->uart_base->INTSTATUS;
  62. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  63. uart->uart_base->INTCLEAR = status;
  64. /* leave interrupt */
  65. rt_interrupt_leave();
  66. }
  67. #ifdef RT_USING_UART0
  68. void UART0RX_Handler(void)
  69. {
  70. uart_irq_handler(&uart0_device.serial);
  71. }
  72. #endif
  73. #ifdef RT_USING_UART1
  74. void UART1RX_Handler(void)
  75. {
  76. uart_irq_handler(&uart1_device.serial);
  77. }
  78. #endif
  79. #ifdef RT_USING_UART2
  80. void UART2RX_Handler(void)
  81. {
  82. uart_irq_handler(&uart2_device.serial);
  83. }
  84. #endif
  85. static rt_err_t v2m_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  86. {
  87. struct v2m_uart *uart;
  88. RT_ASSERT(serial != RT_NULL);
  89. RT_ASSERT(cfg != RT_NULL);
  90. uart = (struct v2m_uart *)serial->parent.user_data;
  91. uart->uart_base->BAUDDIV = SystemCoreClock / cfg->baud_rate;
  92. uart->uart_base->CTRL = CMSDK_UART_CTRL_TXEN_Msk | CMSDK_UART_CTRL_RXEN_Msk | CMSDK_UART_CTRL_RXIRQEN_Msk;
  93. uart->rx_pingpio->ALTFUNCSET |= (1u << uart->rx_pinnum);
  94. uart->tx_pingpio->ALTFUNCSET |= (1u << uart->tx_pinnum);
  95. return RT_EOK;
  96. }
  97. static rt_err_t v2m_control(struct rt_serial_device *serial, int cmd, void *arg)
  98. {
  99. struct v2m_uart *uart;
  100. RT_ASSERT(serial != RT_NULL);
  101. uart = (struct v2m_uart *)serial->parent.user_data;
  102. switch (cmd)
  103. {
  104. case RT_DEVICE_CTRL_CLR_INT:
  105. /* disable rx irq */
  106. NVIC_DisableIRQ(uart->uart_irq_rx);
  107. break;
  108. case RT_DEVICE_CTRL_SET_INT:
  109. /* enable rx irq */
  110. NVIC_EnableIRQ(uart->uart_irq_rx);
  111. break;
  112. }
  113. return RT_EOK;
  114. }
  115. static int v2m_putc(struct rt_serial_device *serial, char c)
  116. {
  117. struct v2m_uart *uart;
  118. RT_ASSERT(serial != RT_NULL);
  119. uart = (struct v2m_uart *)serial->parent.user_data;
  120. while (uart->uart_base->STATE & CMSDK_UART_STATE_TXBF_Msk);
  121. uart->uart_base->DATA = c;
  122. return 1;
  123. }
  124. static int v2m_getc(struct rt_serial_device *serial)
  125. {
  126. int ch;
  127. struct v2m_uart *uart;
  128. RT_ASSERT(serial != RT_NULL);
  129. uart = (struct v2m_uart *)serial->parent.user_data;
  130. ch = -1;
  131. if (uart->uart_base->STATE & CMSDK_UART_STATE_RXBF_Msk)
  132. ch = uart->uart_base->DATA & 0xff;
  133. return ch;
  134. }
  135. static const struct rt_uart_ops v2m_uart_ops =
  136. {
  137. v2m_configure,
  138. v2m_control,
  139. v2m_putc,
  140. v2m_getc,
  141. };
  142. int rt_hw_usart_init(void)
  143. {
  144. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  145. #ifdef RT_USING_UART0
  146. {
  147. struct v2m_uart* uart;
  148. /* get uart device */
  149. uart = &uart0_device;
  150. /* device initialization */
  151. uart->uart_base = CMSDK_UART0;
  152. uart->uart_irq_rx = UART0RX_IRQn;
  153. uart->serial.ops = &v2m_uart_ops;
  154. uart->serial.config = config;
  155. rt_hw_serial_register(&uart->serial,
  156. "uart0",
  157. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  158. uart);
  159. }
  160. #endif /* RT_USING_UART0 */
  161. #ifdef RT_USING_UART1
  162. {
  163. struct v2m_uart* uart;
  164. /* get uart device */
  165. uart = &uart1_device;
  166. /* device initialization */
  167. uart->uart_base = CMSDK_UART1;
  168. uart->uart_irq_rx = UART1RX_IRQn;
  169. rt_hw_serial_register(&uart->serial,
  170. "uart1",
  171. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  172. uart);
  173. }
  174. #endif /* RT_USING_UART1 */
  175. #ifdef RT_USING_UART2
  176. {
  177. struct v2m_uart* uart;
  178. /* get uart device */
  179. uart = &uart2_device;
  180. /* device initialization */
  181. uart->uart_base = CMSDK_UART2;
  182. uart->uart_irq_rx = UART2RX_IRQn;
  183. rt_hw_serial_register(&uart->serial,
  184. "uart2",
  185. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  186. uart);
  187. }
  188. #endif /* RT_USING_UART2 */
  189. return 0;
  190. }
  191. INIT_BOARD_EXPORT(rt_hw_usart_init);
  192. #endif /*RT_USING_UART*/