drv_uart.c 5.7 KB

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