drv_uart.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-09-26 1ridic Integrate with RT-Thread driver framework.
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include "board.h"
  14. #include "drv_uart.h"
  15. #ifdef RT_USING_SERIAL
  16. #include "hardware/uart.h"
  17. #include "hardware/irq.h"
  18. #if !defined(BSP_USING_UART0) && !defined(BSP_USING_UART1)
  19. #error "Please define at least one BSP_USING_UARTx"
  20. /* this driver can be disabled at menuconfig -> RT-Thread Components -> Device Drivers */
  21. #endif
  22. #ifdef BSP_USING_UART0
  23. void pico_uart0_isr(void);
  24. #endif
  25. #ifdef BSP_USING_UART1
  26. void pico_uart1_isr(void);
  27. #endif
  28. struct pico_uart_dev
  29. {
  30. rt_serial_t parent;
  31. uart_inst_t *instance;
  32. rt_uint32_t irqno;
  33. rt_uint32_t tx_pin;
  34. rt_uint32_t rx_pin;
  35. void (*uart_isr)(void);
  36. };
  37. static struct pico_uart_dev uart_dev[] =
  38. {
  39. #ifdef BSP_USING_UART0
  40. {
  41. .instance = uart0,
  42. .irqno = UART0_IRQ,
  43. .tx_pin = BSP_UART0_TX_PIN,
  44. .rx_pin = BSP_UART0_RX_PIN,
  45. .uart_isr = pico_uart0_isr,
  46. },
  47. #endif
  48. #ifdef BSP_USING_UART1
  49. {
  50. .instance = uart1,
  51. .irqno = UART1_IRQ,
  52. .tx_pin = BSP_UART1_TX_PIN,
  53. .rx_pin = BSP_UART1_RX_PIN,
  54. .uart_isr = pico_uart1_isr,
  55. },
  56. #endif
  57. };
  58. enum
  59. {
  60. #ifdef BSP_USING_UART0
  61. UART0_INDEX,
  62. #endif
  63. #ifdef BSP_USING_UART1
  64. UART1_INDEX,
  65. #endif
  66. };
  67. #ifdef BSP_USING_UART0
  68. void pico_uart0_isr(void)
  69. {
  70. rt_interrupt_enter();
  71. /* read interrupt status and clear it */
  72. if (uart_is_readable(uart0)) /* rx ind */
  73. {
  74. rt_hw_serial_isr(&uart_dev[UART0_INDEX].parent, RT_SERIAL_EVENT_RX_IND);
  75. }
  76. rt_interrupt_leave();
  77. }
  78. #endif
  79. #ifdef BSP_USING_UART1
  80. void pico_uart1_isr(void)
  81. {
  82. rt_interrupt_enter();
  83. /* read interrupt status and clear it */
  84. if (uart_is_readable(uart1)) /* rx ind */
  85. {
  86. rt_hw_serial_isr(&uart_dev[UART1_INDEX].parent, RT_SERIAL_EVENT_RX_IND);
  87. }
  88. rt_interrupt_leave();
  89. }
  90. #endif
  91. /*
  92. * UART interface
  93. */
  94. static rt_err_t pico_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  95. {
  96. struct pico_uart_dev *uart = RT_NULL;
  97. RT_ASSERT(serial != RT_NULL);
  98. RT_ASSERT(cfg != RT_NULL);
  99. uart = rt_container_of(serial, struct pico_uart_dev, parent);
  100. uart_init(uart->instance, cfg->baud_rate);
  101. // Set the TX and RX pins by using the function select on the GPIO
  102. // Set datasheet for more information on function select
  103. gpio_set_function(uart->rx_pin, GPIO_FUNC_UART);
  104. gpio_set_function(uart->tx_pin, GPIO_FUNC_UART);
  105. // Set UART flow control CTS/RTS
  106. if (cfg->flowcontrol == RT_SERIAL_FLOWCONTROL_CTSRTS)
  107. uart_set_hw_flow(uart->instance, true, true);
  108. else
  109. uart_set_hw_flow(uart->instance, false, false);
  110. // Set our data format
  111. uart_parity_t uart_parity = UART_PARITY_NONE;
  112. if (cfg->parity == PARITY_ODD)
  113. uart_parity = UART_PARITY_ODD;
  114. else if (cfg->parity == PARITY_EVEN)
  115. uart_parity = UART_PARITY_EVEN;
  116. uart_set_format(uart->instance, cfg->data_bits, cfg->stop_bits, uart_parity);
  117. // Turn off FIFO's - we want to do this character by character
  118. uart_set_fifo_enabled(uart->instance, false);
  119. return RT_EOK;
  120. }
  121. static rt_err_t pico_uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  122. {
  123. struct pico_uart_dev *uart = RT_NULL;
  124. RT_ASSERT(serial != RT_NULL);
  125. uart = rt_container_of(serial, struct pico_uart_dev, parent);
  126. switch (cmd)
  127. {
  128. /* enable interrupt */
  129. case RT_DEVICE_CTRL_SET_INT:
  130. // Set up a RX interrupt
  131. // We need to set up the handler first
  132. // And set up and enable the interrupt handlers
  133. irq_set_exclusive_handler(uart->irqno, uart->uart_isr);
  134. irq_set_enabled(uart->irqno, true);
  135. // Now enable the UART to send interrupts - RX only
  136. uart_set_irq_enables(uart->instance, true, false);
  137. break;
  138. }
  139. return RT_EOK;
  140. }
  141. static int pico_uart_putc(struct rt_serial_device *serial, char c)
  142. {
  143. struct pico_uart_dev *uart = RT_NULL;
  144. RT_ASSERT(serial != RT_NULL);
  145. uart = rt_container_of(serial, struct pico_uart_dev, parent);
  146. uart_putc_raw(uart->instance, c);
  147. return 1;
  148. }
  149. static int pico_uart_getc(struct rt_serial_device *serial)
  150. {
  151. struct pico_uart_dev *uart = RT_NULL;
  152. RT_ASSERT(serial != RT_NULL);
  153. uart = rt_container_of(serial, struct pico_uart_dev, parent);
  154. int ch;
  155. if (uart_is_readable(uart->instance))
  156. {
  157. ch = uart_get_hw(uart->instance)->dr;
  158. }
  159. else
  160. {
  161. ch = -1;
  162. }
  163. return ch;
  164. }
  165. const static struct rt_uart_ops _uart_ops =
  166. {
  167. pico_uart_configure,
  168. pico_uart_control,
  169. pico_uart_putc,
  170. pico_uart_getc,
  171. RT_NULL,
  172. };
  173. /*
  174. * UART Initiation
  175. */
  176. int rt_hw_uart_init(void)
  177. {
  178. rt_err_t ret = RT_EOK;
  179. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  180. #ifdef BSP_USING_UART0
  181. uart_dev[UART0_INDEX].parent.ops = &_uart_ops;
  182. uart_dev[UART0_INDEX].parent.config = config;
  183. ret = rt_hw_serial_register(&uart_dev[UART0_INDEX].parent,
  184. "uart0",
  185. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  186. &uart_dev[UART0_INDEX]);
  187. RT_ASSERT(ret == RT_EOK);
  188. #endif
  189. #ifdef BSP_USING_UART1
  190. uart_dev[UART1_INDEX].parent.ops = &_uart_ops;
  191. uart_dev[UART1_INDEX].parent.config = config;
  192. ret = rt_hw_serial_register(&uart_dev[UART1_INDEX].parent,
  193. "uart1",
  194. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  195. &uart_dev[UART1_INDEX]);
  196. RT_ASSERT(ret == RT_EOK);
  197. #endif
  198. return ret;
  199. }
  200. #endif /* RT_USING_SERIAL */