drv_uart.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. return RT_EOK;
  53. }
  54. static int pico_uart_putc(struct rt_serial_device *serial, char c)
  55. {
  56. uart_putc_raw(uart0, c);
  57. return 1;
  58. }
  59. static int pico_uart_getc(struct rt_serial_device *serial)
  60. {
  61. int ch;
  62. if (uart_is_readable(uart0))
  63. {
  64. ch = uart_get_hw(uart0)->dr;
  65. }
  66. else
  67. {
  68. ch =-1;
  69. }
  70. return ch;
  71. }
  72. const static struct rt_uart_ops _uart_ops =
  73. {
  74. pico_uart_configure,
  75. pico_uart_control,
  76. pico_uart_putc,
  77. pico_uart_getc,
  78. RT_NULL,
  79. };
  80. /*
  81. * UART Initiation
  82. */
  83. int rt_hw_uart_init(void)
  84. {
  85. rt_err_t ret = RT_EOK;
  86. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  87. uart_init(UART_ID, 115200);
  88. // Set the TX and RX pins by using the function select on the GPIO
  89. // Set datasheet for more information on function select
  90. gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);
  91. gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);
  92. // Actually, we want a different speed
  93. // The call will return the actual baud rate selected, which will be as close as
  94. // possible to that requested
  95. uart_set_baudrate(UART_ID, BAUD_RATE);
  96. // Set UART flow control CTS/RTS, we don't want these, so turn them off
  97. uart_set_hw_flow(UART_ID, false, false);
  98. // Set our data format
  99. uart_set_format(UART_ID, DATA_BITS, STOP_BITS, PARITY);
  100. // Turn off FIFO's - we want to do this character by character
  101. uart_set_fifo_enabled(UART_ID, false);
  102. // Set up a RX interrupt
  103. // We need to set up the handler first
  104. // Select correct interrupt for the UART we are using
  105. int UART_IRQ = UART_ID == uart0 ? UART0_IRQ : UART1_IRQ;
  106. // And set up and enable the interrupt handlers
  107. irq_set_exclusive_handler(UART_IRQ, pico_uart_isr);
  108. irq_set_enabled(UART_IRQ, true);
  109. // Now enable the UART to send interrupts - RX only
  110. uart_set_irq_enables(UART_ID, true, false);
  111. uart0_dev.parent.ops = &_uart_ops;
  112. uart0_dev.parent.config = config;
  113. ret = rt_hw_serial_register(&uart0_dev.parent,
  114. "uart0",
  115. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  116. &uart0_dev);
  117. return ret;
  118. }
  119. // INIT_DEVICE_EXPORT(rt_hw_uart_init);