app_uart.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * Copyright (c) 2015 - 2018, 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. app_uart_evt_t app_uart_event;
  55. app_uart_event.evt_type = APP_UART_DATA;
  56. app_uart_event.data.value = p_event->data.rxtx.p_data[0];
  57. (void)nrf_drv_uart_rx(&app_uart_inst, rx_buffer, 1);
  58. rx_done = true;
  59. m_event_handler(&app_uart_event);
  60. }
  61. else if (p_event->type == NRF_DRV_UART_EVT_ERROR)
  62. {
  63. app_uart_evt_t app_uart_event;
  64. app_uart_event.evt_type = APP_UART_COMMUNICATION_ERROR;
  65. app_uart_event.data.error_communication = p_event->data.error.error_mask;
  66. (void)nrf_drv_uart_rx(&app_uart_inst, rx_buffer, 1);
  67. m_event_handler(&app_uart_event);
  68. }
  69. else if (p_event->type == NRF_DRV_UART_EVT_TX_DONE)
  70. {
  71. // Last byte from FIFO transmitted, notify the application.
  72. // Notify that new data is available if this was first byte put in the buffer.
  73. app_uart_evt_t app_uart_event;
  74. app_uart_event.evt_type = APP_UART_TX_EMPTY;
  75. m_event_handler(&app_uart_event);
  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. nrf_drv_uart_config_t config = NRF_DRV_UART_DEFAULT_CONFIG;
  84. config.baudrate = (nrf_uart_baudrate_t)p_comm_params->baud_rate;
  85. config.hwfc = (p_comm_params->flow_control == APP_UART_FLOW_CONTROL_DISABLED) ?
  86. NRF_UART_HWFC_DISABLED : NRF_UART_HWFC_ENABLED;
  87. config.interrupt_priority = irq_priority;
  88. config.parity = p_comm_params->use_parity ? NRF_UART_PARITY_INCLUDED : NRF_UART_PARITY_EXCLUDED;
  89. config.pselcts = p_comm_params->cts_pin_no;
  90. config.pselrts = p_comm_params->rts_pin_no;
  91. config.pselrxd = p_comm_params->rx_pin_no;
  92. config.pseltxd = p_comm_params->tx_pin_no;
  93. m_event_handler = event_handler;
  94. rx_done = false;
  95. uint32_t err_code = nrf_drv_uart_init(&app_uart_inst, &config, uart_event_handler);
  96. VERIFY_SUCCESS(err_code);
  97. // Turn on receiver if RX pin is connected
  98. if (p_comm_params->rx_pin_no != UART_PIN_DISCONNECTED)
  99. {
  100. return nrf_drv_uart_rx(&app_uart_inst, rx_buffer,1);
  101. }
  102. else
  103. {
  104. return NRF_SUCCESS;
  105. }
  106. }
  107. uint32_t app_uart_get(uint8_t * p_byte)
  108. {
  109. ASSERT(p_byte);
  110. uint32_t err_code = NRF_SUCCESS;
  111. if (rx_done)
  112. {
  113. *p_byte = rx_buffer[0];
  114. rx_done = false;
  115. }
  116. else
  117. {
  118. err_code = NRF_ERROR_NOT_FOUND;
  119. }
  120. return err_code;
  121. }
  122. uint32_t app_uart_put(uint8_t byte)
  123. {
  124. tx_buffer[0] = byte;
  125. ret_code_t ret = nrf_drv_uart_tx(&app_uart_inst, tx_buffer, 1);
  126. if (NRF_ERROR_BUSY == ret)
  127. {
  128. return NRF_ERROR_NO_MEM;
  129. }
  130. else if (ret != NRF_SUCCESS)
  131. {
  132. return NRF_ERROR_INTERNAL;
  133. }
  134. else
  135. {
  136. return NRF_SUCCESS;
  137. }
  138. }
  139. uint32_t app_uart_flush(void)
  140. {
  141. return NRF_SUCCESS;
  142. }
  143. uint32_t app_uart_close(void)
  144. {
  145. nrf_drv_uart_uninit(&app_uart_inst);
  146. return NRF_SUCCESS;
  147. }
  148. #endif //NRF_MODULE_ENABLED(APP_UART)