drv_uart.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. */
  9. #include <rthw.h>
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "board.h"
  13. #include "drv_uart.h"
  14. #include "hardware/uart.h"
  15. #include "hardware/irq.h"
  16. #define UART_ID uart0
  17. #define BAUD_RATE 115200
  18. #define DATA_BITS 8
  19. #define STOP_BITS 1
  20. #define PARITY UART_PARITY_NONE
  21. // We are using pins 0 and 1, but see the GPIO function select table in the
  22. // datasheet for information on which other pins can be used.
  23. #define UART_TX_PIN 0
  24. #define UART_RX_PIN 1
  25. #define PICO_UART_DEVICE(uart) (struct pico_uart_dev *)(uart)
  26. static struct pico_uart_dev uart0_dev;
  27. struct pico_uart_dev
  28. {
  29. struct rt_serial_device parent;
  30. rt_uint32_t uart_periph;
  31. rt_uint32_t irqno;
  32. };
  33. void pico_uart_isr(void)
  34. {
  35. rt_interrupt_enter();
  36. /* read interrupt status and clear it */
  37. if (uart_is_readable(uart0)) /* rx ind */
  38. {
  39. rt_hw_serial_isr(&uart0_dev.parent, RT_SERIAL_EVENT_RX_IND);
  40. }
  41. rt_interrupt_leave();
  42. }
  43. /*
  44. * UART interface
  45. */
  46. static rt_err_t pico_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  47. {
  48. return RT_EOK;
  49. }
  50. static rt_err_t pico_uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  51. {
  52. // Select correct interrupt for the UART we are using
  53. int UART_IRQ = UART_ID == uart0 ? UART0_IRQ : UART1_IRQ;
  54. switch (cmd)
  55. {
  56. /* enable interrupt */
  57. case RT_DEVICE_CTRL_SET_INT:
  58. // Set up a RX interrupt
  59. // We need to set up the handler first
  60. // And set up and enable the interrupt handlers
  61. irq_set_exclusive_handler(UART_IRQ, pico_uart_isr);
  62. irq_set_enabled(UART_IRQ, true);
  63. // Now enable the UART to send interrupts - RX only
  64. uart_set_irq_enables(UART_ID, true, false);
  65. break;
  66. }
  67. return RT_EOK;
  68. }
  69. static int pico_uart_putc(struct rt_serial_device *serial, char c)
  70. {
  71. uart_putc_raw(uart0, c);
  72. return 1;
  73. }
  74. static int pico_uart_getc(struct rt_serial_device *serial)
  75. {
  76. int ch;
  77. if (uart_is_readable(uart0))
  78. {
  79. ch = uart_get_hw(uart0)->dr;
  80. }
  81. else
  82. {
  83. ch =-1;
  84. }
  85. return ch;
  86. }
  87. const static struct rt_uart_ops _uart_ops =
  88. {
  89. pico_uart_configure,
  90. pico_uart_control,
  91. pico_uart_putc,
  92. pico_uart_getc,
  93. RT_NULL,
  94. };
  95. /*
  96. * UART Initiation
  97. */
  98. int rt_hw_uart_init(void)
  99. {
  100. rt_err_t ret = RT_EOK;
  101. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  102. uart_init(UART_ID, 115200);
  103. // Set the TX and RX pins by using the function select on the GPIO
  104. // Set datasheet for more information on function select
  105. gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);
  106. gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);
  107. // Actually, we want a different speed
  108. // The call will return the actual baud rate selected, which will be as close as
  109. // possible to that requested
  110. uart_set_baudrate(UART_ID, BAUD_RATE);
  111. // Set UART flow control CTS/RTS, we don't want these, so turn them off
  112. uart_set_hw_flow(UART_ID, false, false);
  113. // Set our data format
  114. uart_set_format(UART_ID, DATA_BITS, STOP_BITS, PARITY);
  115. // Turn off FIFO's - we want to do this character by character
  116. uart_set_fifo_enabled(UART_ID, false);
  117. uart0_dev.parent.ops = &_uart_ops;
  118. uart0_dev.parent.config = config;
  119. ret = rt_hw_serial_register(&uart0_dev.parent,
  120. "uart0",
  121. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  122. &uart0_dev);
  123. return ret;
  124. }
  125. // INIT_DEVICE_EXPORT(rt_hw_uart_init);
  126. // We are using pins 0 and 1, but see the GPIO function select table in the
  127. // datasheet for information on which other pins can be used.
  128. #define UART1_TX_PIN 4
  129. #define UART1_RX_PIN 5
  130. static struct pico_uart1_dev uart1_dev;
  131. struct pico_uart1_dev
  132. {
  133. struct rt_serial_device parent;
  134. rt_uint32_t uart_periph;
  135. rt_uint32_t irqno;
  136. };
  137. void pico_uart1_isr(void)
  138. {
  139. rt_interrupt_enter();
  140. /* read interrupt status and clear it */
  141. if (uart_is_readable(uart1)) /* rx ind */
  142. {
  143. rt_hw_serial_isr(&uart1_dev.parent, RT_SERIAL_EVENT_RX_IND);
  144. }
  145. rt_interrupt_leave();
  146. }
  147. /*
  148. * UART interface
  149. */
  150. static rt_err_t pico_uart1_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  151. {
  152. return RT_EOK;
  153. }
  154. static rt_err_t pico_uart1_control(struct rt_serial_device *serial, int cmd, void *arg)
  155. {
  156. switch (cmd)
  157. {
  158. /* enable interrupt */
  159. case RT_DEVICE_CTRL_SET_INT:
  160. // Set up a RX interrupt
  161. // We need to set up the handler first
  162. // And set up and enable the interrupt handlers
  163. irq_set_exclusive_handler(UART1_IRQ, pico_uart1_isr);
  164. irq_set_enabled(UART1_IRQ, true);
  165. // Now enable the UART to send interrupts - RX only
  166. uart_set_irq_enables(uart1, true, false);
  167. break;
  168. }
  169. return RT_EOK;
  170. }
  171. static int pico_uart1_putc(struct rt_serial_device *serial, char c)
  172. {
  173. uart_putc_raw(uart1, c);
  174. return 1;
  175. }
  176. static int pico_uart1_getc(struct rt_serial_device *serial)
  177. {
  178. int ch;
  179. if (uart_is_readable(uart1))
  180. {
  181. ch = uart_get_hw(uart1)->dr;
  182. }
  183. else
  184. {
  185. ch =-1;
  186. }
  187. return ch;
  188. }
  189. const static struct rt_uart_ops _uart1_ops =
  190. {
  191. pico_uart1_configure,
  192. pico_uart1_control,
  193. pico_uart1_putc,
  194. pico_uart1_getc,
  195. RT_NULL,
  196. };
  197. /*
  198. * UART Initiation
  199. */
  200. int rt_hw_uart1_init(void)
  201. {
  202. rt_err_t ret = RT_EOK;
  203. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  204. uart_init(uart1, 115200);
  205. // Set the TX and RX pins by using the function select on the GPIO
  206. // Set datasheet for more information on function select
  207. gpio_set_function(UART1_TX_PIN, GPIO_FUNC_UART);
  208. gpio_set_function(UART1_RX_PIN, GPIO_FUNC_UART);
  209. // Actually, we want a different speed
  210. // The call will return the actual baud rate selected, which will be as close as
  211. // possible to that requested
  212. uart_set_baudrate(uart1, BAUD_RATE);
  213. // Set UART flow control CTS/RTS, we don't want these, so turn them off
  214. uart_set_hw_flow(uart1, false, false);
  215. // Set our data format
  216. uart_set_format(uart1, DATA_BITS, STOP_BITS, PARITY);
  217. // Turn off FIFO's - we want to do this character by character
  218. uart_set_fifo_enabled(uart1, false);
  219. uart1_dev.parent.ops = &_uart1_ops;
  220. uart1_dev.parent.config = config;
  221. ret = rt_hw_serial_register(&uart1_dev.parent,
  222. "uart1",
  223. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  224. &uart1_dev);
  225. return ret;
  226. }
  227. INIT_DEVICE_EXPORT(rt_hw_uart1_init);