drv_uart.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2009-02-05 Bernard first version
  9. * 2009-10-25 Bernard fix rt_serial_read bug when there is no data
  10. * in the buffer.
  11. * 2010-03-29 Bernard cleanup code.
  12. * 2010-03-30 Kyle Ported from STM32 to AVR32.
  13. * 2023-10-25 Raman Gopalan UART driver for at32uc3a: Initial version
  14. * 2023-11-06 Raman Gopalan Driver abstractions for uc3a and uc3b devices
  15. */
  16. #include <rthw.h>
  17. #include <rtthread.h>
  18. #include <rtdevice.h>
  19. #include "drv_uart.h"
  20. #ifdef RT_USING_SERIAL
  21. #include "compiler.h"
  22. #include "usart.h"
  23. #include "gpio.h"
  24. #include "intc.h"
  25. #if !defined(BSP_USING_UART0) && !defined(BSP_USING_UART1)
  26. #error "Please define at least one BSP_USING_UARTx"
  27. /* this driver can be disabled at menuconfig -> RT-Thread Components -> Device Drivers */
  28. #endif
  29. #ifdef BSP_USING_UART0
  30. void avr32uc3_uart0_isr(void);
  31. #endif
  32. #ifdef BSP_USING_UART1
  33. void avr32uc3_uart1_isr(void);
  34. #endif
  35. #ifdef BSP_USING_UART2
  36. void avr32uc3_uart2_isr(void);
  37. #endif
  38. /* AVR32UC3B uart driver */
  39. struct avr32uc3_uart_dev
  40. {
  41. rt_serial_t parent;
  42. avr32_usart_t *instance;
  43. rt_uint32_t irqno;
  44. rt_uint32_t irq_level;
  45. rt_uint32_t tx_pin;
  46. rt_uint32_t tx_pin_function;
  47. rt_uint32_t rx_pin;
  48. rt_uint32_t rx_pin_function;
  49. void (*uart_isr)(void);
  50. };
  51. static struct avr32uc3_uart_dev uart_dev[] =
  52. {
  53. #ifdef BSP_USING_UART0
  54. {
  55. .instance = (avr32_usart_t *)&AVR32_USART0,
  56. .irqno = AVR32_USART0_IRQ,
  57. .irq_level = AVR32_INTC_INT0,
  58. .tx_pin = BSP_UART0_TX_PIN,
  59. .tx_pin_function = BSP_UART0_TX_PIN_FUNCTION,
  60. .rx_pin = BSP_UART0_RX_PIN,
  61. .rx_pin_function = BSP_UART0_RX_PIN_FUNCTION,
  62. .uart_isr = avr32uc3_uart0_isr,
  63. },
  64. #endif
  65. #ifdef BSP_USING_UART1
  66. {
  67. .instance = (avr32_usart_t *)&AVR32_USART1,
  68. .irqno = AVR32_USART1_IRQ,
  69. .irq_level = AVR32_INTC_INT0,
  70. .tx_pin = BSP_UART1_TX_PIN,
  71. .tx_pin_function = BSP_UART1_TX_PIN_FUNCTION,
  72. .rx_pin = BSP_UART1_RX_PIN,
  73. .rx_pin_function = BSP_UART1_RX_PIN_FUNCTION,
  74. .uart_isr = avr32uc3_uart1_isr,
  75. },
  76. #endif
  77. #ifdef BSP_USING_UART2
  78. {
  79. .instance = (avr32_usart_t *)&AVR32_USART2,
  80. .irqno = AVR32_USART2_IRQ,
  81. .irq_level = AVR32_INTC_INT0,
  82. .tx_pin = BSP_UART2_TX_PIN,
  83. .tx_pin_function = BSP_UART2_TX_PIN_FUNCTION,
  84. .rx_pin = BSP_UART2_RX_PIN,
  85. .rx_pin_function = BSP_UART2_RX_PIN_FUNCTION,
  86. .uart_isr = avr32uc3_uart2_isr,
  87. },
  88. #endif
  89. };
  90. enum
  91. {
  92. #ifdef BSP_USING_UART0
  93. UART0_INDEX,
  94. #endif
  95. #ifdef BSP_USING_UART1
  96. UART1_INDEX,
  97. #endif
  98. #ifdef BSP_USING_UART2
  99. UART2_INDEX,
  100. #endif
  101. };
  102. #ifdef BSP_USING_UART0
  103. void avr32uc3_uart0_isr(void)
  104. {
  105. rt_interrupt_enter();
  106. /* read interrupt status and clear it */
  107. if (usart_test_hit(&AVR32_USART0)) /* rx ind */
  108. {
  109. rt_hw_serial_isr(&uart_dev[UART0_INDEX].parent, RT_SERIAL_EVENT_RX_IND);
  110. }
  111. rt_interrupt_leave();
  112. }
  113. #endif
  114. #ifdef BSP_USING_UART1
  115. void avr32uc3_uart1_isr(void)
  116. {
  117. rt_interrupt_enter();
  118. /* read interrupt status and clear it */
  119. if (usart_test_hit(&AVR32_USART1)) /* rx ind */
  120. {
  121. rt_hw_serial_isr(&uart_dev[UART1_INDEX].parent, RT_SERIAL_EVENT_RX_IND);
  122. }
  123. rt_interrupt_leave();
  124. }
  125. #endif
  126. #ifdef BSP_USING_UART2
  127. void avr32uc3_uart2_isr(void)
  128. {
  129. rt_interrupt_enter();
  130. /* read interrupt status and clear it */
  131. if (usart_test_hit(&AVR32_USART2)) /* rx ind */
  132. {
  133. rt_hw_serial_isr(&uart_dev[UART2_INDEX].parent, RT_SERIAL_EVENT_RX_IND);
  134. }
  135. rt_interrupt_leave();
  136. }
  137. #endif
  138. /**
  139. * @addtogroup AVR32UC3
  140. */
  141. /*@{*/
  142. static rt_err_t avr32uc3_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  143. {
  144. struct avr32uc3_uart_dev *uart = RT_NULL;
  145. unsigned char l_parity;
  146. unsigned short l_stop;
  147. unsigned long l_baud;
  148. unsigned char l_data_bits;
  149. RT_ASSERT(serial != RT_NULL);
  150. RT_ASSERT(cfg != RT_NULL);
  151. uart = rt_container_of(serial, struct avr32uc3_uart_dev, parent);
  152. // Set the TX and RX pins by using the function select on the GPIO
  153. // Set datasheet for more information on function select
  154. gpio_enable_module_pin(uart->tx_pin, uart->tx_pin_function);
  155. gpio_enable_module_pin(uart->rx_pin, uart->rx_pin_function);
  156. /* Parity settings */
  157. if (cfg->parity == PARITY_ODD)
  158. l_parity = USART_ODD_PARITY;
  159. else if (cfg->parity == PARITY_EVEN)
  160. l_parity = USART_EVEN_PARITY;
  161. else
  162. l_parity = USART_NO_PARITY;
  163. /* Stopbit settings */
  164. if (cfg->stop_bits == STOP_BITS_1)
  165. l_stop = USART_1_STOPBIT;
  166. else
  167. l_stop = USART_2_STOPBITS;
  168. l_baud = cfg->baud_rate;
  169. l_data_bits = cfg->data_bits;
  170. /* Populate */
  171. usart_options_t usart_options = {
  172. .baudrate = l_baud,
  173. .charlength = l_data_bits,
  174. .paritytype = l_parity,
  175. .stopbits = l_stop,
  176. .channelmode = USART_NORMAL_CHMODE
  177. };
  178. usart_init_rs232(uart->instance, &usart_options, FPBA);
  179. return RT_EOK;
  180. }
  181. static rt_err_t avr32uc3_uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  182. {
  183. struct avr32uc3_uart_dev *uart = RT_NULL;
  184. RT_ASSERT(serial != RT_NULL);
  185. uart = rt_container_of(serial, struct avr32uc3_uart_dev, parent);
  186. switch (cmd)
  187. {
  188. /* enable interrupt */
  189. case RT_DEVICE_CTRL_SET_INT:
  190. // Set up a RX interrupt
  191. // We need to set up the handler first
  192. // And set up and enable the interrupt handlers
  193. //INTC_init_interrupts();
  194. INTC_register_interrupt(uart->uart_isr, uart->irqno, uart->irq_level);
  195. uart->instance->ier = AVR32_USART_IER_RXRDY_MASK;
  196. break;
  197. }
  198. return RT_EOK;
  199. }
  200. static int avr32uc3_uart_putc(struct rt_serial_device *serial, char c)
  201. {
  202. struct avr32uc3_uart_dev *uart = RT_NULL;
  203. RT_ASSERT(serial != RT_NULL);
  204. uart = rt_container_of(serial, struct avr32uc3_uart_dev, parent);
  205. usart_putchar(uart->instance, c);
  206. return 1;
  207. }
  208. static int avr32uc3_uart_getc(struct rt_serial_device *serial)
  209. {
  210. struct avr32uc3_uart_dev *uart = RT_NULL;
  211. RT_ASSERT(serial != RT_NULL);
  212. uart = rt_container_of(serial, struct avr32uc3_uart_dev, parent);
  213. int ch;
  214. if (usart_read_char(uart->instance, &ch) == USART_SUCCESS)
  215. return ch;
  216. return -1;
  217. }
  218. const static struct rt_uart_ops _uart_ops =
  219. {
  220. avr32uc3_uart_configure,
  221. avr32uc3_uart_control,
  222. avr32uc3_uart_putc,
  223. avr32uc3_uart_getc,
  224. RT_NULL,
  225. };
  226. /*
  227. * UART Initiation
  228. */
  229. int rt_hw_uart_init(void)
  230. {
  231. rt_err_t ret = RT_EOK;
  232. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  233. #ifdef BSP_USING_UART0
  234. uart_dev[UART0_INDEX].parent.ops = &_uart_ops;
  235. uart_dev[UART0_INDEX].parent.config = config;
  236. ret = rt_hw_serial_register(&uart_dev[UART0_INDEX].parent,
  237. "uart0",
  238. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  239. &uart_dev[UART0_INDEX]);
  240. RT_ASSERT(ret == RT_EOK);
  241. #endif
  242. #ifdef BSP_USING_UART1
  243. uart_dev[UART1_INDEX].parent.ops = &_uart_ops;
  244. uart_dev[UART1_INDEX].parent.config = config;
  245. ret = rt_hw_serial_register(&uart_dev[UART1_INDEX].parent,
  246. "uart1",
  247. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  248. &uart_dev[UART1_INDEX]);
  249. RT_ASSERT(ret == RT_EOK);
  250. #endif
  251. #ifdef BSP_USING_UART2
  252. uart_dev[UART2_INDEX].parent.ops = &_uart_ops;
  253. uart_dev[UART2_INDEX].parent.config = config;
  254. ret = rt_hw_serial_register(&uart_dev[UART2_INDEX].parent,
  255. "uart2",
  256. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  257. &uart_dev[UART2_INDEX]);
  258. RT_ASSERT(ret == RT_EOK);
  259. #endif
  260. return ret;
  261. }
  262. #endif /* RT_USING_SERIAL */
  263. /*@}*/