hw_uart.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * File : hw_uart.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2017, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2017-09-15 Haley the first version
  13. */
  14. #include "am_mcu_apollo.h"
  15. #include "hw_uart.h"
  16. #include "board.h"
  17. #include <rtdevice.h>
  18. /* USART0 */
  19. #define AM_UART0_INST 0
  20. #define UART0_GPIO_RX 2
  21. #define UART0_GPIO_CFG_RX AM_HAL_PIN_2_UART0RX
  22. #define UART0_GPIO_TX 1
  23. #define UART0_GPIO_CFG_TX AM_HAL_PIN_1_UART0TX
  24. /* USART1 */
  25. #define AM_UART1_INST 1
  26. #define UART1_GPIO_RX 9
  27. #define UART1_GPIO_CFG_RX AM_HAL_PIN_9_UART1RX
  28. #define UART1_GPIO_TX 8
  29. #define UART1_GPIO_CFG_TX AM_HAL_PIN_8_UART1TX
  30. /* AM uart driver */
  31. struct am_uart
  32. {
  33. uint32_t uart_device;
  34. uint32_t uart_interrupt;
  35. };
  36. /**
  37. * @brief UART configuration settings
  38. *
  39. */
  40. am_hal_uart_config_t g_sUartConfig =
  41. {
  42. .ui32BaudRate = 115200,
  43. .ui32DataBits = AM_HAL_UART_DATA_BITS_8,
  44. .bTwoStopBits = false,
  45. .ui32Parity = AM_HAL_UART_PARITY_NONE,
  46. .ui32FlowCtrl = AM_HAL_UART_FLOW_CTRL_NONE,
  47. };
  48. /**
  49. * @brief Enable the UART
  50. *
  51. * @param Uart driver
  52. *
  53. * This function is Enable the UART
  54. *
  55. * @return None.
  56. */
  57. static void rt_hw_uart_enable(struct am_uart* uart)
  58. {
  59. /* Enable the UART clock */
  60. am_hal_uart_clock_enable(uart->uart_device);
  61. /* Enable the UART */
  62. am_hal_uart_enable(uart->uart_device);
  63. #if defined(RT_USING_UART0)
  64. /* Make sure the UART RX and TX pins are enabled */
  65. am_hal_gpio_pin_config(UART0_GPIO_TX, UART0_GPIO_CFG_TX);
  66. am_hal_gpio_pin_config(UART0_GPIO_RX, UART0_GPIO_CFG_RX | AM_HAL_GPIO_PULL12K);
  67. #endif /* RT_USING_UART0 */
  68. #if defined(RT_USING_UART1)
  69. /* Make sure the UART RX and TX pins are enabled */
  70. am_hal_gpio_pin_config(UART1_GPIO_TX, UART1_GPIO_CFG_TX);
  71. am_hal_gpio_pin_config(UART1_GPIO_RX, UART1_GPIO_CFG_RX | AM_HAL_GPIO_PULL12K);
  72. #endif /* RT_USING_UART1 */
  73. }
  74. /**
  75. * @brief Disable the UART
  76. *
  77. * @param Uart driver
  78. *
  79. * This function is Disable the UART
  80. *
  81. * @return None.
  82. */
  83. static void rt_hw_uart_disable(struct am_uart* uart)
  84. {
  85. /* Clear all interrupts before sleeping as having a pending UART interrupt burns power */
  86. am_hal_uart_int_clear(uart->uart_device, 0xFFFFFFFF);
  87. /* Disable the UART */
  88. am_hal_uart_disable(uart->uart_device);
  89. #if defined(RT_USING_UART0)
  90. /* Disable the UART pins */
  91. am_hal_gpio_pin_config(UART0_GPIO_TX, AM_HAL_PIN_DISABLE);
  92. am_hal_gpio_pin_config(UART0_GPIO_RX, AM_HAL_PIN_DISABLE);
  93. #endif /* RT_USING_UART0 */
  94. #if defined(RT_USING_UART1)
  95. /* Disable the UART pins */
  96. am_hal_gpio_pin_config(UART1_GPIO_TX, AM_HAL_PIN_DISABLE);
  97. am_hal_gpio_pin_config(UART1_GPIO_RX, AM_HAL_PIN_DISABLE);
  98. #endif /* RT_USING_UART1 */
  99. /* Disable the UART clock */
  100. am_hal_uart_clock_disable(uart->uart_device);
  101. }
  102. /**
  103. * @brief UART-based string print function.
  104. *
  105. * @param Send buff
  106. *
  107. * This function is used for printing a string via the UART, which for some
  108. * MCU devices may be multi-module.
  109. *
  110. * @return None.
  111. */
  112. void rt_hw_uart_send_string(char *pcString)
  113. {
  114. am_hal_uart_string_transmit_polled(AM_UART0_INST, pcString);
  115. /* Wait until busy bit clears to make sure UART fully transmitted last byte */
  116. while ( am_hal_uart_flags_get(AM_UART0_INST) & AM_HAL_UART_FR_BUSY );
  117. }
  118. static rt_err_t am_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  119. {
  120. struct am_uart* uart;
  121. RT_ASSERT(serial != RT_NULL);
  122. RT_ASSERT(cfg != RT_NULL);
  123. uart = (struct am_uart *)serial->parent.user_data;
  124. RT_ASSERT(uart != RT_NULL);
  125. /* Get the configure */
  126. g_sUartConfig.ui32BaudRate = cfg->baud_rate;
  127. g_sUartConfig.ui32DataBits = cfg->data_bits;
  128. if (cfg->stop_bits == STOP_BITS_1)
  129. g_sUartConfig.bTwoStopBits = false;
  130. else if (cfg->stop_bits == STOP_BITS_2)
  131. g_sUartConfig.bTwoStopBits = true;
  132. g_sUartConfig.ui32Parity = cfg->parity;
  133. g_sUartConfig.ui32FlowCtrl = AM_HAL_UART_PARITY_NONE;
  134. /* Configure the UART */
  135. am_hal_uart_config(uart->uart_device, &g_sUartConfig);
  136. /* Enable the UART */
  137. am_hal_uart_enable(uart->uart_device);
  138. return RT_EOK;
  139. }
  140. static rt_err_t am_control(struct rt_serial_device *serial, int cmd, void *arg)
  141. {
  142. struct am_uart* uart;
  143. //rt_uint32_t ctrl_arg = (rt_uint32_t)(arg);
  144. RT_ASSERT(serial != RT_NULL);
  145. uart = (struct am_uart *)serial->parent.user_data;
  146. RT_ASSERT(uart != RT_NULL);
  147. switch (cmd)
  148. {
  149. /* disable interrupt */
  150. case RT_DEVICE_CTRL_CLR_INT:
  151. rt_hw_uart_disable(uart);
  152. break;
  153. /* enable interrupt */
  154. case RT_DEVICE_CTRL_SET_INT:
  155. rt_hw_uart_enable(uart);
  156. break;
  157. /* UART config */
  158. case RT_DEVICE_CTRL_CONFIG :
  159. break;
  160. }
  161. return RT_EOK;
  162. }
  163. static int am_putc(struct rt_serial_device *serial, char c)
  164. {
  165. struct am_uart* uart;
  166. RT_ASSERT(serial != RT_NULL);
  167. uart = (struct am_uart *)serial->parent.user_data;
  168. RT_ASSERT(uart != RT_NULL);
  169. am_hal_uart_char_transmit_polled(uart->uart_device, c);
  170. return 1;
  171. }
  172. static int am_getc(struct rt_serial_device *serial)
  173. {
  174. char c;
  175. int ch;
  176. struct am_uart* uart;
  177. RT_ASSERT(serial != RT_NULL);
  178. uart = (struct am_uart *)serial->parent.user_data;
  179. RT_ASSERT(uart != RT_NULL);
  180. ch = -1;
  181. if (am_hal_uart_flags_get(uart->uart_device) & AM_HAL_UART_FR_RX_FULL)
  182. {
  183. am_hal_uart_char_receive_polled(uart->uart_device, &c);
  184. ch = c & 0xff;
  185. }
  186. return ch;
  187. }
  188. /**
  189. * Uart common interrupt process. This need add to uart ISR.
  190. *
  191. * @param serial serial device
  192. */
  193. static void uart_isr(struct rt_serial_device *serial)
  194. {
  195. uint32_t status;
  196. RT_ASSERT(serial != RT_NULL);
  197. struct am_uart *uart = (struct am_uart *) serial->parent.user_data;
  198. RT_ASSERT(uart != RT_NULL);
  199. /* Read the interrupt status */
  200. status = am_hal_uart_int_status_get(uart->uart_device, false);
  201. //rt_kprintf("status is %d\r\n", status);
  202. /* Clear the UART interrupt */
  203. am_hal_uart_int_clear(uart->uart_device, status);
  204. if (status & (AM_HAL_UART_INT_RX_TMOUT))
  205. {
  206. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_TIMEOUT);
  207. }
  208. if (status & AM_HAL_UART_INT_RX)
  209. {
  210. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  211. }
  212. if (status & AM_HAL_UART_INT_TX)
  213. {
  214. //rt_hw_serial_isr(serial, RT_SERIAL_EVENT_TX_DONE);
  215. }
  216. }
  217. static const struct rt_uart_ops am_uart_ops =
  218. {
  219. am_configure,
  220. am_control,
  221. am_putc,
  222. am_getc,
  223. };
  224. #if defined(RT_USING_UART0)
  225. /* UART0 device driver structure */
  226. struct am_uart uart0 =
  227. {
  228. AM_UART0_INST,
  229. AM_HAL_INTERRUPT_UART0
  230. };
  231. static struct rt_serial_device serial0;
  232. void am_uart0_isr(void)
  233. {
  234. /* enter interrupt */
  235. rt_interrupt_enter();
  236. uart_isr(&serial0);
  237. /* leave interrupt */
  238. rt_interrupt_leave();
  239. }
  240. #endif /* RT_USING_UART0 */
  241. #if defined(RT_USING_UART1)
  242. /* UART1 device driver structure */
  243. struct am_uart uart1 =
  244. {
  245. AM_UART1_INST,
  246. AM_HAL_INTERRUPT_UART1
  247. };
  248. static struct rt_serial_device serial1;
  249. void am_uart1_isr(void)
  250. {
  251. /* enter interrupt */
  252. rt_interrupt_enter();
  253. uart_isr(&serial1);
  254. /* leave interrupt */
  255. rt_interrupt_leave();
  256. }
  257. #endif /* RT_USING_UART1 */
  258. static void GPIO_Configuration(void)
  259. {
  260. #if defined(RT_USING_UART0)
  261. /* Make sure the UART RX and TX pins are enabled */
  262. am_hal_gpio_pin_config(UART0_GPIO_TX, UART0_GPIO_CFG_TX);
  263. am_hal_gpio_pin_config(UART0_GPIO_RX, UART0_GPIO_CFG_RX | AM_HAL_GPIO_PULL12K);
  264. #endif /* RT_USING_UART0 */
  265. #if defined(RT_USING_UART1)
  266. /* Make sure the UART RX and TX pins are enabled */
  267. am_hal_gpio_pin_config(UART1_GPIO_TX, UART1_GPIO_CFG_TX);
  268. am_hal_gpio_pin_config(UART1_GPIO_RX, UART1_GPIO_CFG_RX | AM_HAL_GPIO_PULL12K);
  269. #endif /* RT_USING_UART1 */
  270. }
  271. static void RCC_Configuration(struct am_uart* uart)
  272. {
  273. /* Power on the selected UART */
  274. am_hal_uart_pwrctrl_enable(uart->uart_device);
  275. /* Start the UART interface, apply the desired configuration settings */
  276. am_hal_uart_clock_enable(uart->uart_device);
  277. /* Disable the UART before configuring it */
  278. am_hal_uart_disable(uart->uart_device);
  279. /* Configure the UART */
  280. am_hal_uart_config(uart->uart_device, &g_sUartConfig);
  281. /* Enable the UART */
  282. am_hal_uart_enable(uart->uart_device);
  283. /* Enable the UART FIFO */
  284. //am_hal_uart_fifo_config(uart->uart_device, AM_HAL_UART_TX_FIFO_1_2 | AM_HAL_UART_RX_FIFO_1_2);
  285. }
  286. static void NVIC_Configuration(struct am_uart* uart)
  287. {
  288. /* Enable interrupts */
  289. am_hal_uart_int_enable(uart->uart_device, AM_HAL_UART_INT_RX);
  290. /* Enable the uart interrupt in the NVIC */
  291. am_hal_interrupt_enable(uart->uart_interrupt);
  292. am_hal_uart_int_clear(uart->uart_device, 0xFFFFFFFF);
  293. }
  294. /**
  295. * @brief Initialize the UART
  296. *
  297. * This function initialize the UART
  298. *
  299. * @return None.
  300. */
  301. void rt_hw_uart_init(void)
  302. {
  303. struct am_uart* uart;
  304. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  305. GPIO_Configuration();
  306. #if defined(RT_USING_UART0)
  307. uart = &uart0;
  308. config.baud_rate = BAUD_RATE_115200;
  309. RCC_Configuration(uart);
  310. NVIC_Configuration(uart);
  311. serial0.ops = &am_uart_ops;
  312. serial0.config = config;
  313. /* register UART1 device */
  314. rt_hw_serial_register(&serial0, "uart0",
  315. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX |
  316. RT_DEVICE_FLAG_INT_TX, uart);
  317. #endif /* RT_USING_UART0 */
  318. #if defined(RT_USING_UART1)
  319. uart = &uart1;
  320. config.baud_rate = BAUD_RATE_115200;
  321. RCC_Configuration(uart);
  322. NVIC_Configuration(uart);
  323. serial1.ops = &am_uart_ops;
  324. serial1.config = config;
  325. /* register UART1 device */
  326. rt_hw_serial_register(&serial1, "uart1",
  327. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX |
  328. RT_DEVICE_FLAG_INT_TX, uart);
  329. #endif /* RT_USING_UART1 */
  330. }
  331. /*@}*/