main.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. *
  42. * @defgroup nrf_dev_button_radio_tx_example_main main.c
  43. * @{
  44. * @ingroup nrf_dev_button_radio_tx_example
  45. *
  46. * @brief Radio Transceiver Example Application main file.
  47. *
  48. * This file contains the source code for a sample application using the NRF_RADIO peripheral.
  49. *
  50. */
  51. #include <stdint.h>
  52. #include <stdbool.h>
  53. #include <stdio.h>
  54. #include "radio_config.h"
  55. #include "nrf_gpio.h"
  56. #include "app_timer.h"
  57. #include "boards.h"
  58. #include "bsp.h"
  59. #include "nordic_common.h"
  60. #include "nrf_error.h"
  61. #include "nrf_log.h"
  62. #include "nrf_log_ctrl.h"
  63. #include "nrf_log_default_backends.h"
  64. static uint32_t packet; /**< Packet to transmit. */
  65. /**@brief Function for sending packet.
  66. */
  67. void send_packet()
  68. {
  69. // send the packet:
  70. NRF_RADIO->EVENTS_READY = 0U;
  71. NRF_RADIO->TASKS_TXEN = 1;
  72. while (NRF_RADIO->EVENTS_READY == 0U)
  73. {
  74. // wait
  75. }
  76. NRF_RADIO->EVENTS_END = 0U;
  77. NRF_RADIO->TASKS_START = 1U;
  78. while (NRF_RADIO->EVENTS_END == 0U)
  79. {
  80. // wait
  81. }
  82. uint32_t err_code = bsp_indication_set(BSP_INDICATE_SENT_OK);
  83. NRF_LOG_INFO("The packet was sent");
  84. APP_ERROR_CHECK(err_code);
  85. NRF_RADIO->EVENTS_DISABLED = 0U;
  86. // Disable radio
  87. NRF_RADIO->TASKS_DISABLE = 1U;
  88. while (NRF_RADIO->EVENTS_DISABLED == 0U)
  89. {
  90. // wait
  91. }
  92. }
  93. /**@brief Function for handling bsp events.
  94. */
  95. void bsp_evt_handler(bsp_event_t evt)
  96. {
  97. uint32_t prep_packet = 0;
  98. switch (evt)
  99. {
  100. case BSP_EVENT_KEY_0:
  101. /* Fall through. */
  102. case BSP_EVENT_KEY_1:
  103. /* Fall through. */
  104. case BSP_EVENT_KEY_2:
  105. /* Fall through. */
  106. case BSP_EVENT_KEY_3:
  107. /* Fall through. */
  108. case BSP_EVENT_KEY_4:
  109. /* Fall through. */
  110. case BSP_EVENT_KEY_5:
  111. /* Fall through. */
  112. case BSP_EVENT_KEY_6:
  113. /* Fall through. */
  114. case BSP_EVENT_KEY_7:
  115. /* Get actual button state. */
  116. for (int i = 0; i < BUTTONS_NUMBER; i++)
  117. {
  118. prep_packet |= (bsp_board_button_state_get(i) ? (1 << i) : 0);
  119. }
  120. break;
  121. default:
  122. /* No implementation needed. */
  123. break;
  124. }
  125. packet = prep_packet;
  126. }
  127. /**@brief Function for initialization oscillators.
  128. */
  129. void clock_initialization()
  130. {
  131. /* Start 16 MHz crystal oscillator */
  132. NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
  133. NRF_CLOCK->TASKS_HFCLKSTART = 1;
  134. /* Wait for the external oscillator to start up */
  135. while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0)
  136. {
  137. // Do nothing.
  138. }
  139. /* Start low frequency crystal oscillator for app_timer(used by bsp)*/
  140. NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos);
  141. NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
  142. NRF_CLOCK->TASKS_LFCLKSTART = 1;
  143. while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0)
  144. {
  145. // Do nothing.
  146. }
  147. }
  148. /**
  149. * @brief Function for application main entry.
  150. * @return 0. int return type required by ANSI/ISO standard.
  151. */
  152. int main(void)
  153. {
  154. uint32_t err_code = NRF_SUCCESS;
  155. clock_initialization();
  156. err_code = app_timer_init();
  157. APP_ERROR_CHECK(err_code);
  158. err_code = NRF_LOG_INIT(NULL);
  159. APP_ERROR_CHECK(err_code);
  160. NRF_LOG_DEFAULT_BACKENDS_INIT();
  161. err_code = bsp_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS, bsp_evt_handler);
  162. APP_ERROR_CHECK(err_code);
  163. // Set radio configuration parameters
  164. radio_configure();
  165. // Set payload pointer
  166. NRF_RADIO->PACKETPTR = (uint32_t)&packet;
  167. err_code = bsp_indication_set(BSP_INDICATE_USER_STATE_OFF);
  168. NRF_LOG_INFO("Radio transmitter example started.");
  169. NRF_LOG_INFO("Press Any Button");
  170. APP_ERROR_CHECK(err_code);
  171. while (true)
  172. {
  173. if (packet != 0)
  174. {
  175. send_packet();
  176. NRF_LOG_INFO("The contents of the package was %u", (unsigned int)packet);
  177. packet = 0;
  178. }
  179. NRF_LOG_FLUSH();
  180. __WFE();
  181. }
  182. }
  183. /**
  184. *@}
  185. **/