app_uart.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 "nrf_drv_uart.h"
  44. #include "nrf_assert.h"
  45. static uint8_t tx_buffer[1];
  46. static uint8_t rx_buffer[1];
  47. static volatile bool rx_done;
  48. static app_uart_event_handler_t m_event_handler; /**< Event handler function. */
  49. static nrf_drv_uart_t app_uart_inst = NRF_DRV_UART_INSTANCE(APP_UART_DRIVER_INSTANCE);
  50. static void uart_event_handler(nrf_drv_uart_event_t * p_event, void* p_context)
  51. {
  52. if (p_event->type == NRF_DRV_UART_EVT_RX_DONE)
  53. {
  54. // Received bytes counter has to be checked, because there could be event from RXTO interrupt
  55. if (p_event->data.rxtx.bytes)
  56. {
  57. app_uart_evt_t app_uart_event;
  58. app_uart_event.evt_type = APP_UART_DATA;
  59. app_uart_event.data.value = p_event->data.rxtx.p_data[0];
  60. rx_done = true;
  61. m_event_handler(&app_uart_event);
  62. }
  63. (void)nrf_drv_uart_rx(&app_uart_inst, rx_buffer, 1);
  64. }
  65. else if (p_event->type == NRF_DRV_UART_EVT_ERROR)
  66. {
  67. app_uart_evt_t app_uart_event;
  68. app_uart_event.evt_type = APP_UART_COMMUNICATION_ERROR;
  69. app_uart_event.data.error_communication = p_event->data.error.error_mask;
  70. (void)nrf_drv_uart_rx(&app_uart_inst, rx_buffer, 1);
  71. m_event_handler(&app_uart_event);
  72. }
  73. else if (p_event->type == NRF_DRV_UART_EVT_TX_DONE)
  74. {
  75. // Last byte from FIFO transmitted, notify the application.
  76. // Notify that new data is available if this was first byte put in the buffer.
  77. app_uart_evt_t app_uart_event;
  78. app_uart_event.evt_type = APP_UART_TX_EMPTY;
  79. m_event_handler(&app_uart_event);
  80. }
  81. }
  82. uint32_t app_uart_init(const app_uart_comm_params_t * p_comm_params,
  83. app_uart_buffers_t * p_buffers,
  84. app_uart_event_handler_t event_handler,
  85. app_irq_priority_t irq_priority)
  86. {
  87. nrf_drv_uart_config_t config = NRF_DRV_UART_DEFAULT_CONFIG;
  88. config.baudrate = (nrf_uart_baudrate_t)p_comm_params->baud_rate;
  89. config.hwfc = (p_comm_params->flow_control == APP_UART_FLOW_CONTROL_DISABLED) ?
  90. NRF_UART_HWFC_DISABLED : NRF_UART_HWFC_ENABLED;
  91. config.interrupt_priority = irq_priority;
  92. config.parity = p_comm_params->use_parity ? NRF_UART_PARITY_INCLUDED : NRF_UART_PARITY_EXCLUDED;
  93. config.pselcts = p_comm_params->cts_pin_no;
  94. config.pselrts = p_comm_params->rts_pin_no;
  95. config.pselrxd = p_comm_params->rx_pin_no;
  96. config.pseltxd = p_comm_params->tx_pin_no;
  97. m_event_handler = event_handler;
  98. rx_done = false;
  99. uint32_t err_code = nrf_drv_uart_init(&app_uart_inst, &config, uart_event_handler);
  100. VERIFY_SUCCESS(err_code);
  101. // Turn on receiver if RX pin is connected
  102. if (p_comm_params->rx_pin_no != UART_PIN_DISCONNECTED)
  103. {
  104. return nrf_drv_uart_rx(&app_uart_inst, rx_buffer,1);
  105. }
  106. else
  107. {
  108. return NRF_SUCCESS;
  109. }
  110. }
  111. uint32_t app_uart_get(uint8_t * p_byte)
  112. {
  113. ASSERT(p_byte);
  114. uint32_t err_code = NRF_SUCCESS;
  115. if (rx_done)
  116. {
  117. *p_byte = rx_buffer[0];
  118. rx_done = false;
  119. }
  120. else
  121. {
  122. err_code = NRF_ERROR_NOT_FOUND;
  123. }
  124. return err_code;
  125. }
  126. uint32_t app_uart_put(uint8_t byte)
  127. {
  128. tx_buffer[0] = byte;
  129. ret_code_t ret = nrf_drv_uart_tx(&app_uart_inst, tx_buffer, 1);
  130. if (NRF_ERROR_BUSY == ret)
  131. {
  132. return NRF_ERROR_NO_MEM;
  133. }
  134. else if (ret != NRF_SUCCESS)
  135. {
  136. return NRF_ERROR_INTERNAL;
  137. }
  138. else
  139. {
  140. return NRF_SUCCESS;
  141. }
  142. }
  143. uint32_t app_uart_flush(void)
  144. {
  145. return NRF_SUCCESS;
  146. }
  147. uint32_t app_uart_close(void)
  148. {
  149. nrf_drv_uart_uninit(&app_uart_inst);
  150. return NRF_SUCCESS;
  151. }
  152. #endif //NRF_MODULE_ENABLED(APP_UART)