uart.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * File : uart.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2015 RT-Thread Develop Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. */
  11. #include <stdbool.h>
  12. #include <stdint.h>
  13. #include <stdio.h>
  14. #include <rthw.h>
  15. #include <rtdevice.h>
  16. #include "board.h"
  17. #include "uart.h"
  18. #include <nrf_gpio.h>
  19. #define UART_RX_BUFSZ 512
  20. rt_uint8_t rx_buffer[UART_RX_BUFSZ];
  21. struct nrf52832_uart
  22. {
  23. struct rt_device parent;
  24. struct rt_ringbuffer rx_rb;
  25. } uart_device;
  26. void UART0_IRQHandler(void)
  27. {
  28. rt_ubase_t level;
  29. struct nrf52832_uart* uart = &uart_device;
  30. level = rt_hw_interrupt_disable();
  31. // Wait for RXD data to be received
  32. while (NRF_UART0->EVENTS_RXDRDY != 1) ;
  33. NRF_UART0->EVENTS_RXDRDY = 0;
  34. rt_hw_interrupt_enable(level);
  35. /* [Handling the data received over UART] */
  36. rt_ringbuffer_putchar_force(&(uart->rx_rb), (rt_uint8_t)NRF_UART0->RXD);
  37. /* invoke callback */
  38. if(uart->parent.rx_indicate != RT_NULL)
  39. {
  40. uart->parent.rx_indicate(&uart->parent, rt_ringbuffer_data_len(&uart->rx_rb));
  41. }
  42. }
  43. static rt_err_t rt_uart_init (rt_device_t dev)
  44. {
  45. /* UART Initialization and Enable */
  46. /** @snippet [Configure UART RX and TX pin] */
  47. nrf_gpio_cfg_output(TX_PIN_NUMBER);
  48. nrf_gpio_cfg_input(RX_PIN_NUMBER, NRF_GPIO_PIN_NOPULL);
  49. NRF_UART0->PSELTXD = TX_PIN_NUMBER;
  50. NRF_UART0->PSELRXD = RX_PIN_NUMBER;
  51. /** @snippet [Configure UART RX and TX pin] */
  52. if (HWFC)
  53. {
  54. nrf_gpio_cfg_output(RTS_PIN_NUMBER);
  55. nrf_gpio_cfg_input(CTS_PIN_NUMBER, NRF_GPIO_PIN_NOPULL);
  56. NRF_UART0->PSELCTS = CTS_PIN_NUMBER;
  57. NRF_UART0->PSELRTS = RTS_PIN_NUMBER;
  58. NRF_UART0->CONFIG = (UART_CONFIG_HWFC_Enabled << UART_CONFIG_HWFC_Pos);
  59. }
  60. NRF_UART0->BAUDRATE = (UART_BAUDRATE_BAUDRATE_Baud38400 << UART_BAUDRATE_BAUDRATE_Pos);
  61. NRF_UART0->ENABLE = (UART_ENABLE_ENABLE_Enabled << UART_ENABLE_ENABLE_Pos);
  62. NRF_UART0->TASKS_STARTTX = 1;
  63. NRF_UART0->TASKS_STARTRX = 1;
  64. NRF_UART0->EVENTS_RXDRDY = 0;
  65. NRF_UART0->INTENSET = (UART_INTENSET_RXDRDY_Enabled << UART_INTENSET_RXDRDY_Pos);
  66. NVIC_EnableIRQ(UART0_IRQn);
  67. return RT_EOK;
  68. }
  69. static rt_err_t rt_uart_open(rt_device_t dev, rt_uint16_t oflag)
  70. {
  71. RT_ASSERT(dev != RT_NULL);
  72. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  73. {
  74. /* Enable the UART Interrupt */
  75. NVIC_EnableIRQ(UART0_IRQn);
  76. }
  77. return RT_EOK;
  78. }
  79. static rt_err_t rt_uart_close(rt_device_t dev)
  80. {
  81. RT_ASSERT(dev != RT_NULL);
  82. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  83. {
  84. /* Disable the UART Interrupt */
  85. NVIC_DisableIRQ(UART0_IRQn);
  86. }
  87. return RT_EOK;
  88. }
  89. static rt_size_t rt_uart_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  90. {
  91. rt_size_t length;
  92. struct nrf52832_uart *uart = (struct nrf52832_uart*)dev;
  93. /* interrupt receive */
  94. rt_base_t level;
  95. RT_ASSERT(uart != RT_NULL);
  96. /* disable interrupt */
  97. level = rt_hw_interrupt_disable();
  98. length = rt_ringbuffer_get(&(uart->rx_rb), buffer, size);
  99. /* enable interrupt */
  100. rt_hw_interrupt_enable(level);
  101. return length;
  102. }
  103. static rt_size_t rt_uart_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  104. {
  105. char *ptr;
  106. ptr = (char*) buffer;
  107. if (dev->open_flag & RT_DEVICE_FLAG_STREAM)
  108. {
  109. /* stream mode */
  110. while (size)
  111. {
  112. if (*ptr == '\n')
  113. {
  114. NRF_UART0->TXD = (uint8_t)'\r';
  115. // Wait for TXD data to be sent.
  116. while (NRF_UART0->EVENTS_TXDRDY != 1) ;
  117. NRF_UART0->EVENTS_TXDRDY = 0;
  118. }
  119. NRF_UART0->TXD = (uint8_t)(*ptr);
  120. // Wait for TXD data to be sent.
  121. while (NRF_UART0->EVENTS_TXDRDY != 1) ;
  122. NRF_UART0->EVENTS_TXDRDY = 0;
  123. ptr ++;
  124. size --;
  125. }
  126. }
  127. else
  128. {
  129. while ( size != 0 )
  130. {
  131. NRF_UART0->TXD = (uint8_t)(*ptr);
  132. // Wait for TXD data to be sent.
  133. while (NRF_UART0->EVENTS_TXDRDY != 1) ;
  134. NRF_UART0->EVENTS_TXDRDY = 0;
  135. ptr++;
  136. size--;
  137. }
  138. }
  139. return (rt_size_t) ptr - (rt_size_t) buffer;
  140. }
  141. void rt_hw_uart_init(void)
  142. {
  143. struct nrf52832_uart* uart;
  144. /* get uart device */
  145. uart = &uart_device;
  146. /* device initialization */
  147. uart->parent.type = RT_Device_Class_Char;
  148. rt_ringbuffer_init(&(uart->rx_rb), rx_buffer, sizeof(rx_buffer));
  149. /* device interface */
  150. uart->parent.init = rt_uart_init;
  151. uart->parent.open = rt_uart_open;
  152. uart->parent.close = rt_uart_close;
  153. uart->parent.read = rt_uart_read;
  154. uart->parent.write = rt_uart_write;
  155. uart->parent.control = RT_NULL;
  156. uart->parent.user_data = RT_NULL;
  157. rt_device_register(&uart->parent, "uart0", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
  158. }