uart.c 10 KB

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