app_uart_fifo.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include "app_uart.h"
  13. #include "app_fifo.h"
  14. #include "nrf_drv_uart.h"
  15. #include "nrf_assert.h"
  16. #include "sdk_common.h"
  17. static __INLINE uint32_t fifo_length(app_fifo_t * const fifo)
  18. {
  19. uint32_t tmp = fifo->read_pos;
  20. return fifo->write_pos - tmp;
  21. }
  22. #define FIFO_LENGTH(F) fifo_length(&F) /**< Macro to calculate length of a FIFO. */
  23. static app_uart_event_handler_t m_event_handler; /**< Event handler function. */
  24. static uint8_t tx_buffer[1];
  25. static uint8_t rx_buffer[1];
  26. static app_fifo_t m_rx_fifo; /**< RX FIFO buffer for storing data received on the UART until the application fetches them using app_uart_get(). */
  27. static app_fifo_t m_tx_fifo; /**< TX FIFO buffer for storing data to be transmitted on the UART when TXD is ready. Data is put to the buffer on using app_uart_put(). */
  28. static void uart_event_handler(nrf_drv_uart_event_t * p_event, void* p_context)
  29. {
  30. app_uart_evt_t app_uart_event;
  31. if (p_event->type == NRF_DRV_UART_EVT_RX_DONE)
  32. {
  33. // Write received byte to FIFO
  34. uint32_t err_code = app_fifo_put(&m_rx_fifo, p_event->data.rxtx.p_data[0]);
  35. if (err_code != NRF_SUCCESS)
  36. {
  37. app_uart_event.evt_type = APP_UART_FIFO_ERROR;
  38. app_uart_event.data.error_code = err_code;
  39. m_event_handler(&app_uart_event);
  40. }
  41. // Notify that new data is available if this was first byte put in the buffer.
  42. else if (FIFO_LENGTH(m_rx_fifo) == 1)
  43. {
  44. app_uart_event.evt_type = APP_UART_DATA_READY;
  45. m_event_handler(&app_uart_event);
  46. }
  47. else
  48. {
  49. // Do nothing, only send event if first byte was added or overflow in FIFO occurred.
  50. }
  51. if (FIFO_LENGTH(m_rx_fifo) <= m_rx_fifo.buf_size_mask)
  52. {
  53. (void)nrf_drv_uart_rx(rx_buffer, 1);
  54. }
  55. }
  56. else if (p_event->type == NRF_DRV_UART_EVT_ERROR)
  57. {
  58. app_uart_event.evt_type = APP_UART_COMMUNICATION_ERROR;
  59. app_uart_event.data.error_communication = p_event->data.error.error_mask;
  60. (void)nrf_drv_uart_rx(rx_buffer, 1);
  61. m_event_handler(&app_uart_event);
  62. }
  63. else if (p_event->type == NRF_DRV_UART_EVT_TX_DONE)
  64. {
  65. // Get next byte from FIFO.
  66. if (app_fifo_get(&m_tx_fifo, tx_buffer) == NRF_SUCCESS)
  67. {
  68. (void)nrf_drv_uart_tx(tx_buffer, 1);
  69. }
  70. if (FIFO_LENGTH(m_tx_fifo) == 0)
  71. {
  72. // Last byte from FIFO transmitted, notify the application.
  73. app_uart_event.evt_type = APP_UART_TX_EMPTY;
  74. m_event_handler(&app_uart_event);
  75. }
  76. }
  77. }
  78. uint32_t app_uart_init(const app_uart_comm_params_t * p_comm_params,
  79. app_uart_buffers_t * p_buffers,
  80. app_uart_event_handler_t event_handler,
  81. app_irq_priority_t irq_priority)
  82. {
  83. uint32_t err_code;
  84. m_event_handler = event_handler;
  85. if (p_buffers == NULL)
  86. {
  87. return NRF_ERROR_INVALID_PARAM;
  88. }
  89. // Configure buffer RX buffer.
  90. err_code = app_fifo_init(&m_rx_fifo, p_buffers->rx_buf, p_buffers->rx_buf_size);
  91. VERIFY_SUCCESS(err_code);
  92. // Configure buffer TX buffer.
  93. err_code = app_fifo_init(&m_tx_fifo, p_buffers->tx_buf, p_buffers->tx_buf_size);
  94. VERIFY_SUCCESS(err_code);
  95. nrf_drv_uart_config_t config = NRF_DRV_UART_DEFAULT_CONFIG;
  96. config.baudrate = (nrf_uart_baudrate_t)p_comm_params->baud_rate;
  97. config.hwfc = (p_comm_params->flow_control == APP_UART_FLOW_CONTROL_DISABLED) ?
  98. NRF_UART_HWFC_DISABLED : NRF_UART_HWFC_ENABLED;
  99. config.interrupt_priority = irq_priority;
  100. config.parity = p_comm_params->use_parity ? NRF_UART_PARITY_INCLUDED : NRF_UART_PARITY_EXCLUDED;
  101. config.pselcts = p_comm_params->cts_pin_no;
  102. config.pselrts = p_comm_params->rts_pin_no;
  103. config.pselrxd = p_comm_params->rx_pin_no;
  104. config.pseltxd = p_comm_params->tx_pin_no;
  105. err_code = nrf_drv_uart_init(&config, uart_event_handler);
  106. VERIFY_SUCCESS(err_code);
  107. #ifdef NRF52
  108. if (!config.use_easy_dma)
  109. #endif
  110. {
  111. nrf_drv_uart_rx_enable();
  112. }
  113. return nrf_drv_uart_rx(rx_buffer,1);
  114. }
  115. uint32_t app_uart_flush(void)
  116. {
  117. uint32_t err_code;
  118. err_code = app_fifo_flush(&m_rx_fifo);
  119. VERIFY_SUCCESS(err_code);
  120. err_code = app_fifo_flush(&m_tx_fifo);
  121. VERIFY_SUCCESS(err_code);
  122. return NRF_SUCCESS;
  123. }
  124. uint32_t app_uart_get(uint8_t * p_byte)
  125. {
  126. ASSERT(p_byte);
  127. // If FIFO was full new request to receive one byte was not scheduled. Must be done here.
  128. if (FIFO_LENGTH(m_rx_fifo) == m_rx_fifo.buf_size_mask)
  129. {
  130. uint32_t err_code = nrf_drv_uart_rx(rx_buffer,1);
  131. if (err_code != NRF_SUCCESS)
  132. {
  133. return NRF_ERROR_NOT_FOUND;
  134. }
  135. }
  136. return app_fifo_get(&m_rx_fifo, p_byte);
  137. }
  138. uint32_t app_uart_put(uint8_t byte)
  139. {
  140. uint32_t err_code;
  141. err_code = app_fifo_put(&m_tx_fifo, byte);
  142. if (err_code == NRF_SUCCESS)
  143. {
  144. // The new byte has been added to FIFO. It will be picked up from there
  145. // (in 'uart_event_handler') when all preceding bytes are transmitted.
  146. // But if UART is not transmitting anything at the moment, we must start
  147. // a new transmission here.
  148. if (!nrf_drv_uart_tx_in_progress())
  149. {
  150. // This operation should be almost always successful, since we've
  151. // just added a byte to FIFO, but if some bigger delay occurred
  152. // (some heavy interrupt handler routine has been executed) since
  153. // that time, FIFO might be empty already.
  154. if (app_fifo_get(&m_tx_fifo, tx_buffer) == NRF_SUCCESS)
  155. {
  156. err_code = nrf_drv_uart_tx(tx_buffer, 1);
  157. }
  158. }
  159. }
  160. return err_code;
  161. }
  162. uint32_t app_uart_close(void)
  163. {
  164. nrf_drv_uart_uninit();
  165. return NRF_SUCCESS;
  166. }