main.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * Copyright (c) 2018 - 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. /** @file
  41. * @defgroup libuarte_example_main main.c
  42. * @{
  43. * @ingroup libuarte_example
  44. * @brief Libuarte Example Application main file.
  45. *
  46. * This file contains the source code for a sample application using libuarte.
  47. *
  48. */
  49. #include <stdbool.h>
  50. #include <stdint.h>
  51. #include <stdio.h>
  52. #include "nrf_libuarte_async.h"
  53. #include "nrf_drv_clock.h"
  54. #include <bsp.h>
  55. #include "nrf_log_ctrl.h"
  56. #include "nrf_log_default_backends.h"
  57. #include "nrf_queue.h"
  58. NRF_LIBUARTE_ASYNC_DEFINE(libuarte, 0, 0, 0, NRF_LIBUARTE_PERIPHERAL_NOT_USED, 255, 3);
  59. static uint8_t text[] = "UART example started.\r\n Loopback:\r\n";
  60. static uint8_t text_size = sizeof(text);
  61. static volatile bool m_loopback_phase;
  62. typedef struct {
  63. uint8_t * p_data;
  64. uint32_t length;
  65. } buffer_t;
  66. NRF_QUEUE_DEF(buffer_t, m_buf_queue, 10, NRF_QUEUE_MODE_NO_OVERFLOW);
  67. void uart_event_handler(void * context, nrf_libuarte_async_evt_t * p_evt)
  68. {
  69. nrf_libuarte_async_t * p_libuarte = (nrf_libuarte_async_t *)context;
  70. ret_code_t ret;
  71. switch (p_evt->type)
  72. {
  73. case NRF_LIBUARTE_ASYNC_EVT_ERROR:
  74. bsp_board_led_invert(0);
  75. break;
  76. case NRF_LIBUARTE_ASYNC_EVT_RX_DATA:
  77. ret = nrf_libuarte_async_tx(p_libuarte,p_evt->data.rxtx.p_data, p_evt->data.rxtx.length);
  78. if (ret == NRF_ERROR_BUSY)
  79. {
  80. buffer_t buf = {
  81. .p_data = p_evt->data.rxtx.p_data,
  82. .length = p_evt->data.rxtx.length,
  83. };
  84. ret = nrf_queue_push(&m_buf_queue, &buf);
  85. APP_ERROR_CHECK(ret);
  86. }
  87. else
  88. {
  89. APP_ERROR_CHECK(ret);
  90. }
  91. bsp_board_led_invert(1);
  92. m_loopback_phase = true;
  93. break;
  94. case NRF_LIBUARTE_ASYNC_EVT_TX_DONE:
  95. if (m_loopback_phase)
  96. {
  97. nrf_libuarte_async_rx_free(p_libuarte, p_evt->data.rxtx.p_data, p_evt->data.rxtx.length);
  98. if (!nrf_queue_is_empty(&m_buf_queue))
  99. {
  100. buffer_t buf;
  101. ret = nrf_queue_pop(&m_buf_queue, &buf);
  102. APP_ERROR_CHECK(ret);
  103. UNUSED_RETURN_VALUE(nrf_libuarte_async_tx(p_libuarte, buf.p_data, buf.length));
  104. }
  105. }
  106. bsp_board_led_invert(2);
  107. break;
  108. default:
  109. break;
  110. }
  111. }
  112. /**
  113. * @brief Function for main application entry.
  114. */
  115. int main(void)
  116. {
  117. bsp_board_init(BSP_INIT_LEDS);
  118. ret_code_t ret = nrf_drv_clock_init();
  119. APP_ERROR_CHECK(ret);
  120. nrf_drv_clock_lfclk_request(NULL);
  121. ret_code_t err_code = NRF_LOG_INIT(app_timer_cnt_get);
  122. APP_ERROR_CHECK(err_code);
  123. NRF_LOG_DEFAULT_BACKENDS_INIT();
  124. nrf_libuarte_async_config_t nrf_libuarte_async_config = {
  125. .tx_pin = TX_PIN_NUMBER,
  126. .rx_pin = RX_PIN_NUMBER,
  127. .baudrate = NRF_UARTE_BAUDRATE_115200,
  128. .parity = NRF_UARTE_PARITY_EXCLUDED,
  129. .hwfc = NRF_UARTE_HWFC_DISABLED,
  130. .timeout_us = 100,
  131. .int_prio = APP_IRQ_PRIORITY_LOW
  132. };
  133. err_code = nrf_libuarte_async_init(&libuarte, &nrf_libuarte_async_config, uart_event_handler, (void *)&libuarte);
  134. APP_ERROR_CHECK(err_code);
  135. nrf_libuarte_async_enable(&libuarte);
  136. err_code = nrf_libuarte_async_tx(&libuarte, text, text_size);
  137. APP_ERROR_CHECK(err_code);
  138. while (true)
  139. {
  140. NRF_LOG_FLUSH();
  141. }
  142. }
  143. /** @} */