main.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**
  2. * Copyright (c) 2014 - 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 uart_example_main main.c
  42. * @{
  43. * @ingroup uart_example
  44. * @brief UART Example Application main file.
  45. *
  46. * This file contains the source code for a sample application using UART.
  47. *
  48. */
  49. #include <stdbool.h>
  50. #include <stdint.h>
  51. #include <stdio.h>
  52. #include "app_uart.h"
  53. #include "app_error.h"
  54. #include "nrf_delay.h"
  55. #include "nrf.h"
  56. #include "bsp.h"
  57. #if defined (UART_PRESENT)
  58. #include "nrf_uart.h"
  59. #endif
  60. #if defined (UARTE_PRESENT)
  61. #include "nrf_uarte.h"
  62. #endif
  63. //#define ENABLE_LOOPBACK_TEST /**< if defined, then this example will be a loopback test, which means that TX should be connected to RX to get data loopback. */
  64. #define MAX_TEST_DATA_BYTES (15U) /**< max number of test bytes to be used for tx and rx. */
  65. #define UART_TX_BUF_SIZE 256 /**< UART TX buffer size. */
  66. #define UART_RX_BUF_SIZE 256 /**< UART RX buffer size. */
  67. void uart_error_handle(app_uart_evt_t * p_event)
  68. {
  69. if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
  70. {
  71. APP_ERROR_HANDLER(p_event->data.error_communication);
  72. }
  73. else if (p_event->evt_type == APP_UART_FIFO_ERROR)
  74. {
  75. APP_ERROR_HANDLER(p_event->data.error_code);
  76. }
  77. }
  78. #ifdef ENABLE_LOOPBACK_TEST
  79. /* Use flow control in loopback test. */
  80. #define UART_HWFC APP_UART_FLOW_CONTROL_ENABLED
  81. /** @brief Function for setting the @ref ERROR_PIN high, and then enter an infinite loop.
  82. */
  83. static void show_error(void)
  84. {
  85. bsp_board_leds_on();
  86. while (true)
  87. {
  88. // Do nothing.
  89. }
  90. }
  91. /** @brief Function for testing UART loop back.
  92. * @details Transmitts one character at a time to check if the data received from the loopback is same as the transmitted data.
  93. * @note @ref TX_PIN_NUMBER must be connected to @ref RX_PIN_NUMBER)
  94. */
  95. static void uart_loopback_test()
  96. {
  97. uint8_t * tx_data = (uint8_t *)("\r\nLOOPBACK_TEST\r\n");
  98. uint8_t rx_data;
  99. // Start sending one byte and see if you get the same
  100. for (uint32_t i = 0; i < MAX_TEST_DATA_BYTES; i++)
  101. {
  102. uint32_t err_code;
  103. while (app_uart_put(tx_data[i]) != NRF_SUCCESS);
  104. nrf_delay_ms(10);
  105. err_code = app_uart_get(&rx_data);
  106. if ((rx_data != tx_data[i]) || (err_code != NRF_SUCCESS))
  107. {
  108. show_error();
  109. }
  110. }
  111. return;
  112. }
  113. #else
  114. /* When UART is used for communication with the host do not use flow control.*/
  115. #define UART_HWFC APP_UART_FLOW_CONTROL_DISABLED
  116. #endif
  117. /**
  118. * @brief Function for main application entry.
  119. */
  120. int main(void)
  121. {
  122. uint32_t err_code;
  123. bsp_board_init(BSP_INIT_LEDS);
  124. const app_uart_comm_params_t comm_params =
  125. {
  126. RX_PIN_NUMBER,
  127. TX_PIN_NUMBER,
  128. RTS_PIN_NUMBER,
  129. CTS_PIN_NUMBER,
  130. UART_HWFC,
  131. false,
  132. #if defined (UART_PRESENT)
  133. NRF_UART_BAUDRATE_115200
  134. #else
  135. NRF_UARTE_BAUDRATE_115200
  136. #endif
  137. };
  138. APP_UART_FIFO_INIT(&comm_params,
  139. UART_RX_BUF_SIZE,
  140. UART_TX_BUF_SIZE,
  141. uart_error_handle,
  142. APP_IRQ_PRIORITY_LOWEST,
  143. err_code);
  144. APP_ERROR_CHECK(err_code);
  145. #ifndef ENABLE_LOOPBACK_TEST
  146. printf("\r\nUART example started.\r\n");
  147. while (true)
  148. {
  149. uint8_t cr;
  150. while (app_uart_get(&cr) != NRF_SUCCESS);
  151. while (app_uart_put(cr) != NRF_SUCCESS);
  152. if (cr == 'q' || cr == 'Q')
  153. {
  154. printf(" \r\nExit!\r\n");
  155. while (true)
  156. {
  157. // Do nothing.
  158. }
  159. }
  160. }
  161. #else
  162. // This part of the example is just for testing the loopback .
  163. while (true)
  164. {
  165. uart_loopback_test();
  166. }
  167. #endif
  168. }
  169. /** @} */