uart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. /* 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. #define UART_BUFFER_SIZE 256
  40. uint8_t UartRxBuffer[UART_BUFFER_SIZE];
  41. uint8_t UartTxBuffer[UART_BUFFER_SIZE];
  42. /* AM uart driver */
  43. struct am_uart
  44. {
  45. uint32_t uart_device;
  46. uint32_t uart_interrupt;
  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 | AM_HAL_GPIO_PULL24K);
  66. am_hal_gpio_pin_config(UART0_GPIO_RX, UART0_GPIO_CFG_RX | AM_HAL_GPIO_PULL24K);
  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 | AM_HAL_GPIO_PULL24K);
  71. am_hal_gpio_pin_config(UART1_GPIO_RX, UART1_GPIO_CFG_RX | AM_HAL_GPIO_PULL24K);
  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. //connect am drv to rt drv.
  119. static rt_err_t am_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  120. {
  121. struct am_uart* uart;
  122. am_hal_uart_config_t uart_cfg;
  123. RT_ASSERT(serial != RT_NULL);
  124. RT_ASSERT(cfg != RT_NULL);
  125. uart = (struct am_uart *)serial->parent.user_data;
  126. RT_ASSERT(uart != RT_NULL);
  127. /* Get the configure */
  128. uart_cfg.ui32BaudRate = cfg->baud_rate;
  129. if (cfg->data_bits == DATA_BITS_5)
  130. uart_cfg.ui32DataBits = AM_HAL_UART_DATA_BITS_5;
  131. else if (cfg->data_bits == DATA_BITS_6)
  132. uart_cfg.ui32DataBits = AM_HAL_UART_DATA_BITS_6;
  133. else if (cfg->data_bits == DATA_BITS_7)
  134. uart_cfg.ui32DataBits = AM_HAL_UART_DATA_BITS_7;
  135. else if (cfg->data_bits == DATA_BITS_8)
  136. uart_cfg.ui32DataBits = AM_HAL_UART_DATA_BITS_8;
  137. if (cfg->stop_bits == STOP_BITS_1)
  138. uart_cfg.bTwoStopBits = false;
  139. else if (cfg->stop_bits == STOP_BITS_2)
  140. uart_cfg.bTwoStopBits = true;
  141. if (cfg->parity == PARITY_NONE)
  142. uart_cfg.ui32Parity = AM_HAL_UART_PARITY_NONE;
  143. else if (cfg->parity == PARITY_ODD)
  144. uart_cfg.ui32Parity = AM_HAL_UART_PARITY_ODD;
  145. else if (cfg->parity == PARITY_EVEN)
  146. uart_cfg.ui32Parity = AM_HAL_UART_PARITY_EVEN;
  147. uart_cfg.ui32FlowCtrl = AM_HAL_UART_FLOW_CTRL_NONE;
  148. /* UART Config */
  149. am_hal_uart_config(uart->uart_device, &uart_cfg);
  150. /* Enable the UART FIFO */
  151. am_hal_uart_fifo_config(uart->uart_device, AM_HAL_UART_RX_FIFO_7_8 | AM_HAL_UART_RX_FIFO_7_8);
  152. /* Initialize the UART queues */
  153. am_hal_uart_init_buffered(uart->uart_device, UartRxBuffer, UART_BUFFER_SIZE, UartTxBuffer, UART_BUFFER_SIZE);
  154. /* Enable the UART */
  155. am_hal_uart_enable(uart->uart_device);
  156. /* Enable interrupts */
  157. am_hal_uart_int_enable(uart->uart_device, AM_HAL_UART_INT_RX_TMOUT | AM_HAL_UART_INT_RX);
  158. /* Enable the uart interrupt in the NVIC */
  159. am_hal_interrupt_enable(uart->uart_interrupt);
  160. return RT_EOK;
  161. }
  162. static rt_err_t am_control(struct rt_serial_device *serial, int cmd, void *arg)
  163. {
  164. struct am_uart* uart;
  165. //rt_uint32_t ctrl_arg = (rt_uint32_t)(arg);
  166. RT_ASSERT(serial != RT_NULL);
  167. uart = (struct am_uart *)serial->parent.user_data;
  168. RT_ASSERT(uart != RT_NULL);
  169. switch (cmd)
  170. {
  171. /* disable interrupt */
  172. case RT_DEVICE_CTRL_CLR_INT:
  173. rt_hw_uart_disable(uart);
  174. break;
  175. /* enable interrupt */
  176. case RT_DEVICE_CTRL_SET_INT:
  177. rt_hw_uart_enable(uart);
  178. break;
  179. /* UART config */
  180. case RT_DEVICE_CTRL_CONFIG :
  181. break;
  182. }
  183. return RT_EOK;
  184. }
  185. static int am_putc(struct rt_serial_device *serial, char c)
  186. {
  187. uint32_t rxsize, txsize;
  188. struct am_uart* uart;
  189. RT_ASSERT(serial != RT_NULL);
  190. uart = (struct am_uart *)serial->parent.user_data;
  191. RT_ASSERT(uart != RT_NULL);
  192. am_hal_uart_get_status_buffered(uart->uart_device, &rxsize, &txsize);
  193. //if (txsize > 0)
  194. {
  195. am_hal_uart_char_transmit_buffered(uart->uart_device, c);
  196. }
  197. /* Wait until busy bit clears to make sure UART fully transmitted last byte */
  198. while ( am_hal_uart_flags_get(uart->uart_device) & AM_HAL_UART_FR_BUSY );
  199. return 1;
  200. }
  201. static int am_getc(struct rt_serial_device *serial)
  202. {
  203. char c;
  204. int ch;
  205. uint32_t rxsize, txsize;
  206. struct am_uart* uart;
  207. RT_ASSERT(serial != RT_NULL);
  208. uart = (struct am_uart *)serial->parent.user_data;
  209. RT_ASSERT(uart != RT_NULL);
  210. ch = -1;
  211. am_hal_uart_get_status_buffered(uart->uart_device, &rxsize, &txsize);
  212. if (rxsize > 0)
  213. {
  214. am_hal_uart_char_receive_buffered(uart->uart_device, &c, 1);
  215. ch = c & 0xff;
  216. }
  217. return ch;
  218. }
  219. /**
  220. * Uart common interrupt process. This need add to uart ISR.
  221. *
  222. * @param serial serial device
  223. */
  224. static void uart_isr(struct rt_serial_device *serial)
  225. {
  226. uint32_t status;
  227. struct am_uart* uart;
  228. RT_ASSERT(serial != RT_NULL);
  229. uart = (struct am_uart *) serial->parent.user_data;
  230. RT_ASSERT(uart != RT_NULL);
  231. /* Read the interrupt status */
  232. status = am_hal_uart_int_status_get(uart->uart_device, false);
  233. //rt_kprintf("status is %d\r\n", status);
  234. /* Clear the UART interrupt */
  235. am_hal_uart_int_clear(uart->uart_device, status);
  236. if (status & (AM_HAL_UART_INT_RX_TMOUT | AM_HAL_UART_INT_TX | AM_HAL_UART_INT_RX))
  237. {
  238. am_hal_uart_service_buffered_timeout_save(uart->uart_device, status);
  239. }
  240. if (status & (AM_HAL_UART_INT_RX_TMOUT))
  241. {
  242. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  243. }
  244. if (status & AM_HAL_UART_INT_RX)
  245. {
  246. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  247. }
  248. if (status & AM_HAL_UART_INT_TX)
  249. {
  250. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_TX_DONE);
  251. }
  252. }
  253. static const struct rt_uart_ops am_uart_ops =
  254. {
  255. am_configure,
  256. am_control,
  257. am_putc,
  258. am_getc,
  259. };
  260. #if defined(RT_USING_UART0)
  261. /* UART0 device driver structure */
  262. struct am_uart uart0 =
  263. {
  264. AM_UART0_INST,
  265. AM_HAL_INTERRUPT_UART0
  266. };
  267. static struct rt_serial_device serial0;
  268. void am_uart0_isr(void)
  269. {
  270. /* enter interrupt */
  271. rt_interrupt_enter();
  272. uart_isr(&serial0);
  273. /* leave interrupt */
  274. rt_interrupt_leave();
  275. }
  276. #endif /* RT_USING_UART0 */
  277. #if defined(RT_USING_UART1)
  278. /* UART1 device driver structure */
  279. struct am_uart uart1 =
  280. {
  281. AM_UART1_INST,
  282. AM_HAL_INTERRUPT_UART1
  283. };
  284. static struct rt_serial_device serial1;
  285. void am_uart1_isr(void)
  286. {
  287. /* enter interrupt */
  288. rt_interrupt_enter();
  289. uart_isr(&serial1);
  290. /* leave interrupt */
  291. rt_interrupt_leave();
  292. }
  293. #endif /* RT_USING_UART1 */
  294. static void GPIO_Configuration(void)
  295. {
  296. #if defined(RT_USING_UART0)
  297. /* Make sure the UART RX and TX pins are enabled */
  298. am_hal_gpio_pin_config(UART0_GPIO_TX, UART0_GPIO_CFG_TX | AM_HAL_GPIO_PULL24K);
  299. am_hal_gpio_pin_config(UART0_GPIO_RX, UART0_GPIO_CFG_RX | AM_HAL_GPIO_PULL24K);
  300. #endif /* RT_USING_UART0 */
  301. #if defined(RT_USING_UART1)
  302. /* Make sure the UART RX and TX pins are enabled */
  303. am_hal_gpio_pin_config(UART1_GPIO_TX, UART1_GPIO_CFG_TX | AM_HAL_GPIO_PULL24K);
  304. am_hal_gpio_pin_config(UART1_GPIO_RX, UART1_GPIO_CFG_RX | AM_HAL_GPIO_PULL24K);
  305. #endif /* RT_USING_UART1 */
  306. }
  307. static void RCC_Configuration(struct am_uart* uart)
  308. {
  309. /* Power on the selected UART */
  310. am_hal_uart_pwrctrl_enable(uart->uart_device);
  311. /* Start the UART interface, apply the desired configuration settings */
  312. am_hal_uart_clock_enable(uart->uart_device);
  313. /* Disable the UART before configuring it */
  314. am_hal_uart_disable(uart->uart_device);
  315. }
  316. /**
  317. * @brief Initialize the UART
  318. *
  319. * This function initialize the UART
  320. *
  321. * @return None.
  322. */
  323. int rt_hw_uart_init(void)
  324. {
  325. struct am_uart* uart;
  326. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  327. GPIO_Configuration();
  328. #if defined(RT_USING_UART0)
  329. uart = &uart0;
  330. config.baud_rate = BAUD_RATE_115200;
  331. RCC_Configuration(uart);
  332. serial0.ops = &am_uart_ops;
  333. serial0.config = config;
  334. /* register UART0 device */
  335. rt_hw_serial_register(&serial0, "uart0",
  336. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX |
  337. RT_DEVICE_FLAG_INT_TX, uart);
  338. #endif /* RT_USING_UART0 */
  339. #if defined(RT_USING_UART1)
  340. uart = &uart1;
  341. config.baud_rate = BAUD_RATE_115200;
  342. RCC_Configuration(uart);
  343. serial1.ops = &am_uart_ops;
  344. serial1.config = config;
  345. /* register UART1 device */
  346. rt_hw_serial_register(&serial1, "uart1",
  347. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX |
  348. RT_DEVICE_FLAG_INT_TX, uart);
  349. #endif /* RT_USING_UART1 */
  350. return 0;
  351. }
  352. /*@}*/