app_uart_fifo.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /**
  2. * Copyright (c) 2015 - 2020, 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. // If 0, then this is a RXTO event with no new bytes.
  67. if(p_event->data.rxtx.bytes == 0)
  68. {
  69. // A new start RX is needed to continue to receive data
  70. (void)nrf_drv_uart_rx(&app_uart_inst, rx_buffer, 1);
  71. break;
  72. }
  73. // Write received byte to FIFO.
  74. err_code = app_fifo_put(&m_rx_fifo, p_event->data.rxtx.p_data[0]);
  75. if (err_code != NRF_SUCCESS)
  76. {
  77. app_uart_event.evt_type = APP_UART_FIFO_ERROR;
  78. app_uart_event.data.error_code = err_code;
  79. m_event_handler(&app_uart_event);
  80. }
  81. // Notify that there are data available.
  82. else if (FIFO_LENGTH(m_rx_fifo) != 0)
  83. {
  84. app_uart_event.evt_type = APP_UART_DATA_READY;
  85. m_event_handler(&app_uart_event);
  86. }
  87. // Start new RX if size in buffer.
  88. if (FIFO_LENGTH(m_rx_fifo) <= m_rx_fifo.buf_size_mask)
  89. {
  90. (void)nrf_drv_uart_rx(&app_uart_inst, rx_buffer, 1);
  91. }
  92. else
  93. {
  94. // Overflow in RX FIFO.
  95. m_rx_ovf = true;
  96. }
  97. break;
  98. case NRF_DRV_UART_EVT_ERROR:
  99. app_uart_event.evt_type = APP_UART_COMMUNICATION_ERROR;
  100. app_uart_event.data.error_communication = p_event->data.error.error_mask;
  101. (void)nrf_drv_uart_rx(&app_uart_inst, rx_buffer, 1);
  102. m_event_handler(&app_uart_event);
  103. break;
  104. case NRF_DRV_UART_EVT_TX_DONE:
  105. // Get next byte from FIFO.
  106. if (app_fifo_get(&m_tx_fifo, tx_buffer) == NRF_SUCCESS)
  107. {
  108. (void)nrf_drv_uart_tx(&app_uart_inst, tx_buffer, 1);
  109. }
  110. else
  111. {
  112. // Last byte from FIFO transmitted, notify the application.
  113. app_uart_event.evt_type = APP_UART_TX_EMPTY;
  114. m_event_handler(&app_uart_event);
  115. }
  116. break;
  117. default:
  118. break;
  119. }
  120. }
  121. uint32_t app_uart_init(const app_uart_comm_params_t * p_comm_params,
  122. app_uart_buffers_t * p_buffers,
  123. app_uart_event_handler_t event_handler,
  124. app_irq_priority_t irq_priority)
  125. {
  126. uint32_t err_code;
  127. m_event_handler = event_handler;
  128. if (p_buffers == NULL)
  129. {
  130. return NRF_ERROR_INVALID_PARAM;
  131. }
  132. // Configure buffer RX buffer.
  133. err_code = app_fifo_init(&m_rx_fifo, p_buffers->rx_buf, p_buffers->rx_buf_size);
  134. VERIFY_SUCCESS(err_code);
  135. // Configure buffer TX buffer.
  136. err_code = app_fifo_init(&m_tx_fifo, p_buffers->tx_buf, p_buffers->tx_buf_size);
  137. VERIFY_SUCCESS(err_code);
  138. nrf_drv_uart_config_t config = NRF_DRV_UART_DEFAULT_CONFIG;
  139. config.baudrate = (nrf_uart_baudrate_t)p_comm_params->baud_rate;
  140. config.hwfc = (p_comm_params->flow_control == APP_UART_FLOW_CONTROL_DISABLED) ?
  141. NRF_UART_HWFC_DISABLED : NRF_UART_HWFC_ENABLED;
  142. config.interrupt_priority = irq_priority;
  143. config.parity = p_comm_params->use_parity ? NRF_UART_PARITY_INCLUDED : NRF_UART_PARITY_EXCLUDED;
  144. config.pselcts = p_comm_params->cts_pin_no;
  145. config.pselrts = p_comm_params->rts_pin_no;
  146. config.pselrxd = p_comm_params->rx_pin_no;
  147. config.pseltxd = p_comm_params->tx_pin_no;
  148. err_code = nrf_drv_uart_init(&app_uart_inst, &config, uart_event_handler);
  149. VERIFY_SUCCESS(err_code);
  150. m_rx_ovf = false;
  151. // Turn on receiver if RX pin is connected
  152. if (p_comm_params->rx_pin_no != UART_PIN_DISCONNECTED)
  153. {
  154. return nrf_drv_uart_rx(&app_uart_inst, rx_buffer,1);
  155. }
  156. else
  157. {
  158. return NRF_SUCCESS;
  159. }
  160. }
  161. uint32_t app_uart_flush(void)
  162. {
  163. uint32_t err_code;
  164. err_code = app_fifo_flush(&m_rx_fifo);
  165. VERIFY_SUCCESS(err_code);
  166. err_code = app_fifo_flush(&m_tx_fifo);
  167. VERIFY_SUCCESS(err_code);
  168. return NRF_SUCCESS;
  169. }
  170. uint32_t app_uart_get(uint8_t * p_byte)
  171. {
  172. ASSERT(p_byte);
  173. bool rx_ovf = m_rx_ovf;
  174. ret_code_t err_code = app_fifo_get(&m_rx_fifo, p_byte);
  175. // If FIFO was full new request to receive one byte was not scheduled. Must be done here.
  176. if (rx_ovf)
  177. {
  178. m_rx_ovf = false;
  179. uint32_t uart_err_code = nrf_drv_uart_rx(&app_uart_inst, rx_buffer, 1);
  180. // RX resume should never fail.
  181. APP_ERROR_CHECK(uart_err_code);
  182. }
  183. return err_code;
  184. }
  185. uint32_t app_uart_put(uint8_t byte)
  186. {
  187. uint32_t err_code;
  188. err_code = app_fifo_put(&m_tx_fifo, byte);
  189. if (err_code == NRF_SUCCESS)
  190. {
  191. // The new byte has been added to FIFO. It will be picked up from there
  192. // (in 'uart_event_handler') when all preceding bytes are transmitted.
  193. // But if UART is not transmitting anything at the moment, we must start
  194. // a new transmission here.
  195. if (!nrf_drv_uart_tx_in_progress(&app_uart_inst))
  196. {
  197. // This operation should be almost always successful, since we've
  198. // just added a byte to FIFO, but if some bigger delay occurred
  199. // (some heavy interrupt handler routine has been executed) since
  200. // that time, FIFO might be empty already.
  201. if (app_fifo_get(&m_tx_fifo, tx_buffer) == NRF_SUCCESS)
  202. {
  203. err_code = nrf_drv_uart_tx(&app_uart_inst, tx_buffer, 1);
  204. }
  205. }
  206. }
  207. return err_code;
  208. }
  209. uint32_t app_uart_close(void)
  210. {
  211. nrf_drv_uart_uninit(&app_uart_inst);
  212. return NRF_SUCCESS;
  213. }
  214. #endif //NRF_MODULE_ENABLED(APP_UART)