main.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * Copyright (c) 2014 - 2019, 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 "nrf_esb.h"
  41. #include <stdbool.h>
  42. #include <stdint.h>
  43. #include "sdk_common.h"
  44. #include "nrf.h"
  45. #include "nrf_esb_error_codes.h"
  46. #include "nrf_delay.h"
  47. #include "nrf_gpio.h"
  48. #include "nrf_error.h"
  49. #include "boards.h"
  50. #include "nrf_log.h"
  51. #include "nrf_log_ctrl.h"
  52. #include "nrf_log_default_backends.h"
  53. uint8_t led_nr;
  54. nrf_esb_payload_t rx_payload;
  55. /*lint -save -esym(40, BUTTON_1) -esym(40, BUTTON_2) -esym(40, BUTTON_3) -esym(40, BUTTON_4) -esym(40, LED_1) -esym(40, LED_2) -esym(40, LED_3) -esym(40, LED_4) */
  56. void nrf_esb_event_handler(nrf_esb_evt_t const * p_event)
  57. {
  58. switch (p_event->evt_id)
  59. {
  60. case NRF_ESB_EVENT_TX_SUCCESS:
  61. NRF_LOG_DEBUG("TX SUCCESS EVENT");
  62. break;
  63. case NRF_ESB_EVENT_TX_FAILED:
  64. NRF_LOG_DEBUG("TX FAILED EVENT");
  65. break;
  66. case NRF_ESB_EVENT_RX_RECEIVED:
  67. NRF_LOG_DEBUG("RX RECEIVED EVENT");
  68. if (nrf_esb_read_rx_payload(&rx_payload) == NRF_SUCCESS)
  69. {
  70. // Set LEDs identical to the ones on the PTX.
  71. nrf_gpio_pin_write(LED_1, !(rx_payload.data[1]%8>0 && rx_payload.data[1]%8<=4));
  72. nrf_gpio_pin_write(LED_2, !(rx_payload.data[1]%8>1 && rx_payload.data[1]%8<=5));
  73. nrf_gpio_pin_write(LED_3, !(rx_payload.data[1]%8>2 && rx_payload.data[1]%8<=6));
  74. nrf_gpio_pin_write(LED_4, !(rx_payload.data[1]%8>3));
  75. NRF_LOG_DEBUG("Receiving packet: %02x", rx_payload.data[1]);
  76. }
  77. break;
  78. }
  79. }
  80. void clocks_start( void )
  81. {
  82. NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
  83. NRF_CLOCK->TASKS_HFCLKSTART = 1;
  84. while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0);
  85. }
  86. void gpio_init( void )
  87. {
  88. bsp_board_init(BSP_INIT_LEDS);
  89. }
  90. uint32_t esb_init( void )
  91. {
  92. uint32_t err_code;
  93. uint8_t base_addr_0[4] = {0xE7, 0xE7, 0xE7, 0xE7};
  94. uint8_t base_addr_1[4] = {0xC2, 0xC2, 0xC2, 0xC2};
  95. uint8_t addr_prefix[8] = {0xE7, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8 };
  96. nrf_esb_config_t nrf_esb_config = NRF_ESB_DEFAULT_CONFIG;
  97. nrf_esb_config.payload_length = 8;
  98. nrf_esb_config.protocol = NRF_ESB_PROTOCOL_ESB_DPL;
  99. nrf_esb_config.bitrate = NRF_ESB_BITRATE_2MBPS;
  100. nrf_esb_config.mode = NRF_ESB_MODE_PRX;
  101. nrf_esb_config.event_handler = nrf_esb_event_handler;
  102. nrf_esb_config.selective_auto_ack = false;
  103. err_code = nrf_esb_init(&nrf_esb_config);
  104. VERIFY_SUCCESS(err_code);
  105. err_code = nrf_esb_set_base_address_0(base_addr_0);
  106. VERIFY_SUCCESS(err_code);
  107. err_code = nrf_esb_set_base_address_1(base_addr_1);
  108. VERIFY_SUCCESS(err_code);
  109. err_code = nrf_esb_set_prefixes(addr_prefix, 8);
  110. VERIFY_SUCCESS(err_code);
  111. return err_code;
  112. }
  113. int main(void)
  114. {
  115. uint32_t err_code;
  116. gpio_init();
  117. err_code = NRF_LOG_INIT(NULL);
  118. APP_ERROR_CHECK(err_code);
  119. NRF_LOG_DEFAULT_BACKENDS_INIT();
  120. clocks_start();
  121. err_code = esb_init();
  122. APP_ERROR_CHECK(err_code);
  123. NRF_LOG_DEBUG("Enhanced ShockBurst Receiver Example started.");
  124. err_code = nrf_esb_start_rx();
  125. APP_ERROR_CHECK(err_code);
  126. while (true)
  127. {
  128. if (NRF_LOG_PROCESS() == false)
  129. {
  130. __WFE();
  131. }
  132. }
  133. }
  134. /*lint -restore */