drv_uart.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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);