app_uart_fifo.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /**
  2. * Copyright (c) 2015 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "sdk_common.h"
  41. #if NRF_MODULE_ENABLED(APP_UART)
  42. #include "app_uart.h"
  43. #include "app_fifo.h"
  44. #include "nrf_drv_uart.h"
  45. #include "nrf_assert.h"
  46. static nrf_drv_uart_t app_uart_inst = NRF_DRV_UART_INSTANCE(APP_UART_DRIVER_INSTANCE);
  47. static __INLINE uint32_t fifo_length(app_fifo_t * const fifo)
  48. {
  49. uint32_t tmp = fifo->read_pos;
  50. return fifo->write_pos - tmp;
  51. }
  52. #define FIFO_LENGTH(F) fifo_length(&F) /**< Macro to calculate length of a FIFO. */
  53. static app_uart_event_handler_t m_event_handler; /**< Event handler function. */
  54. static uint8_t tx_buffer[1];
  55. static uint8_t rx_buffer[1];
  56. static bool m_rx_ovf;
  57. 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(). */
  58. 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(). */
  59. static void uart_event_handler(nrf_drv_uart_event_t * p_event, void* p_context)
  60. {
  61. app_uart_evt_t app_uart_event;
  62. uint32_t err_code;
  63. switch (p_event->type)
  64. {
  65. case NRF_DRV_UART_EVT_RX_DONE:
  66. // Write received byte to FIFO.
  67. err_code = app_fifo_put(&m_rx_fifo, p_event->data.rxtx.p_data[0]);
  68. if (err_code != NRF_SUCCESS)
  69. {
  70. app_uart_event.evt_type = APP_UART_FIFO_ERROR;
  71. app_uart_event.data.error_code = err_code;
  72. m_event_handler(&app_uart_event);
  73. }
  74. // Notify that there are data available.
  75. else if (FIFO_LENGTH(m_rx_fifo) != 0)
  76. {
  77. app_uart_event.evt_type = APP_UART_DATA_READY;
  78. m_event_handler(&app_uart_event);
  79. }
  80. // Start new RX if size in buffer.
  81. if (FIFO_LENGTH(m_rx_fifo) <= m_rx_fifo.buf_size_mask)
  82. {
  83. (void)nrf_drv_uart_rx(&app_uart_inst, rx_buffer, 1);
  84. }
  85. else
  86. {
  87. // Overflow in RX FIFO.
  88. m_rx_ovf = true;
  89. }
  90. break;
  91. case NRF_DRV_UART_EVT_ERROR:
  92. app_uart_event.evt_type = APP_UART_COMMUNICATION_ERROR;
  93. app_uart_event.data.error_communication = p_event->data.error.error_mask;
  94. (void)nrf_drv_uart_rx(&app_uart_inst, rx_buffer, 1);
  95. m_event_handler(&app_uart_event);
  96. break;
  97. case NRF_DRV_UART_EVT_TX_DONE:
  98. // Get next byte from FIFO.
  99. if (app_fifo_get(&m_tx_fifo, tx_buffer) == NRF_SUCCESS)
  100. {
  101. (void)nrf_drv_uart_tx(&app_uart_inst, tx_buffer, 1);
  102. }
  103. else
  104. {
  105. // Last byte from FIFO transmitted, notify the application.
  106. app_uart_event.evt_type = APP_UART_TX_EMPTY;
  107. m_event_handler(&app_uart_event);
  108. }
  109. break;
  110. default:
  111. break;
  112. }
  113. }
  114. uint32_t app_uart_init(const app_uart_comm_params_t * p_comm_params,
  115. app_uart_buffers_t * p_buffers,
  116. app_uart_event_handler_t event_handler,
  117. app_irq_priority_t irq_priority)
  118. {
  119. uint32_t err_code;
  120. m_event_handler = event_handler;
  121. if (p_buffers == NULL)
  122. {
  123. return NRF_ERROR_INVALID_PARAM;
  124. }
  125. // Configure buffer RX buffer.
  126. err_code = app_fifo_init(&m_rx_fifo, p_buffers->rx_buf, p_buffers->rx_buf_size);
  127. VERIFY_SUCCESS(err_code);
  128. // Configure buffer TX buffer.
  129. err_code = app_fifo_init(&m_tx_fifo, p_buffers->tx_buf, p_buffers->tx_buf_size);
  130. VERIFY_SUCCESS(err_code);
  131. nrf_drv_uart_config_t config = NRF_DRV_UART_DEFAULT_CONFIG;
  132. config.baudrate = (nrf_uart_baudrate_t)p_comm_params->baud_rate;
  133. config.hwfc = (p_comm_params->flow_control == APP_UART_FLOW_CONTROL_DISABLED) ?
  134. NRF_UART_HWFC_DISABLED : NRF_UART_HWFC_ENABLED;
  135. config.interrupt_priority = irq_priority;
  136. config.parity = p_comm_params->use_parity ? NRF_UART_PARITY_INCLUDED : NRF_UART_PARITY_EXCLUDED;
  137. config.pselcts = p_comm_params->cts_pin_no;
  138. config.pselrts = p_comm_params->rts_pin_no;
  139. config.pselrxd = p_comm_params->rx_pin_no;
  140. config.pseltxd = p_comm_params->tx_pin_no;
  141. err_code = nrf_drv_uart_init(&app_uart_inst, &config, uart_event_handler);
  142. VERIFY_SUCCESS(err_code);
  143. m_rx_ovf = false;
  144. // Turn on receiver if RX pin is connected
  145. if (p_comm_params->rx_pin_no != UART_PIN_DISCONNECTED)
  146. {
  147. return nrf_drv_uart_rx(&app_uart_inst, rx_buffer,1);
  148. }
  149. else
  150. {
  151. return NRF_SUCCESS;
  152. }
  153. }
  154. uint32_t app_uart_flush(void)
  155. {
  156. uint32_t err_code;
  157. err_code = app_fifo_flush(&m_rx_fifo);
  158. VERIFY_SUCCESS(err_code);
  159. err_code = app_fifo_flush(&m_tx_fifo);
  160. VERIFY_SUCCESS(err_code);
  161. return NRF_SUCCESS;
  162. }
  163. uint32_t app_uart_get(uint8_t * p_byte)
  164. {
  165. ASSERT(p_byte);
  166. bool rx_ovf = m_rx_ovf;
  167. ret_code_t err_code = app_fifo_get(&m_rx_fifo, p_byte);
  168. // If FIFO was full new request to receive one byte was not scheduled. Must be done here.
  169. if (rx_ovf)
  170. {
  171. m_rx_ovf = false;
  172. uint32_t uart_err_code = nrf_drv_uart_rx(&app_uart_inst, rx_buffer, 1);
  173. // RX resume should never fail.
  174. APP_ERROR_CHECK(uart_err_code);
  175. }
  176. return err_code;
  177. }
  178. uint32_t app_uart_put(uint8_t byte)
  179. {
  180. uint32_t err_code;
  181. err_code = app_fifo_put(&m_tx_fifo, byte);
  182. if (err_code == NRF_SUCCESS)
  183. {
  184. // The new byte has been added to FIFO. It will be picked up from there
  185. // (in 'uart_event_handler') when all preceding bytes are transmitted.
  186. // But if UART is not transmitting anything at the moment, we must start
  187. // a new transmission here.
  188. if (!nrf_drv_uart_tx_in_progress(&app_uart_inst))
  189. {
  190. // This operation should be almost always successful, since we've
  191. // just added a byte to FIFO, but if some bigger delay occurred
  192. // (some heavy interrupt handler routine has been executed) since
  193. // that time, FIFO might be empty already.
  194. if (app_fifo_get(&m_tx_fifo, tx_buffer) == NRF_SUCCESS)
  195. {
  196. err_code = nrf_drv_uart_tx(&app_uart_inst, tx_buffer, 1);
  197. }
  198. }
  199. }
  200. return err_code;
  201. }
  202. uint32_t app_uart_close(void)
  203. {
  204. nrf_drv_uart_uninit(&app_uart_inst);
  205. return NRF_SUCCESS;
  206. }
  207. #endif //NRF_MODULE_ENABLED(APP_UART)