uart.c 11 KB

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