main.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 <stdbool.h>
  41. #include <stdint.h>
  42. #include <string.h>
  43. #include "sdk_common.h"
  44. #include "nrf.h"
  45. #include "nrf_esb.h"
  46. #include "nrf_error.h"
  47. #include "nrf_esb_error_codes.h"
  48. #include "nrf_delay.h"
  49. #include "nrf_gpio.h"
  50. #include "boards.h"
  51. #include "nrf_delay.h"
  52. #include "app_util.h"
  53. #include "nrf_log.h"
  54. #include "nrf_log_ctrl.h"
  55. #include "nrf_log_default_backends.h"
  56. static nrf_esb_payload_t tx_payload = NRF_ESB_CREATE_PAYLOAD(0, 0x01, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00);
  57. static nrf_esb_payload_t rx_payload;
  58. void nrf_esb_event_handler(nrf_esb_evt_t const * p_event)
  59. {
  60. switch (p_event->evt_id)
  61. {
  62. case NRF_ESB_EVENT_TX_SUCCESS:
  63. NRF_LOG_DEBUG("TX SUCCESS EVENT");
  64. break;
  65. case NRF_ESB_EVENT_TX_FAILED:
  66. NRF_LOG_DEBUG("TX FAILED EVENT");
  67. (void) nrf_esb_flush_tx();
  68. (void) nrf_esb_start_tx();
  69. break;
  70. case NRF_ESB_EVENT_RX_RECEIVED:
  71. NRF_LOG_DEBUG("RX RECEIVED EVENT");
  72. while (nrf_esb_read_rx_payload(&rx_payload) == NRF_SUCCESS)
  73. {
  74. if (rx_payload.length > 0)
  75. {
  76. NRF_LOG_DEBUG("RX RECEIVED PAYLOAD");
  77. }
  78. }
  79. break;
  80. }
  81. }
  82. void clocks_start( void )
  83. {
  84. NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
  85. NRF_CLOCK->TASKS_HFCLKSTART = 1;
  86. while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0);
  87. }
  88. void gpio_init( void )
  89. {
  90. nrf_gpio_range_cfg_output(8, 15);
  91. bsp_board_init(BSP_INIT_LEDS);
  92. }
  93. uint32_t esb_init( void )
  94. {
  95. uint32_t err_code;
  96. uint8_t base_addr_0[4] = {0xE7, 0xE7, 0xE7, 0xE7};
  97. uint8_t base_addr_1[4] = {0xC2, 0xC2, 0xC2, 0xC2};
  98. uint8_t addr_prefix[8] = {0xE7, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8 };
  99. nrf_esb_config_t nrf_esb_config = NRF_ESB_DEFAULT_CONFIG;
  100. nrf_esb_config.protocol = NRF_ESB_PROTOCOL_ESB_DPL;
  101. nrf_esb_config.retransmit_delay = 600;
  102. nrf_esb_config.bitrate = NRF_ESB_BITRATE_2MBPS;
  103. nrf_esb_config.event_handler = nrf_esb_event_handler;
  104. nrf_esb_config.mode = NRF_ESB_MODE_PTX;
  105. nrf_esb_config.selective_auto_ack = false;
  106. err_code = nrf_esb_init(&nrf_esb_config);
  107. VERIFY_SUCCESS(err_code);
  108. err_code = nrf_esb_set_base_address_0(base_addr_0);
  109. VERIFY_SUCCESS(err_code);
  110. err_code = nrf_esb_set_base_address_1(base_addr_1);
  111. VERIFY_SUCCESS(err_code);
  112. err_code = nrf_esb_set_prefixes(addr_prefix, NRF_ESB_PIPE_COUNT);
  113. VERIFY_SUCCESS(err_code);
  114. return err_code;
  115. }
  116. int main(void)
  117. {
  118. ret_code_t err_code;
  119. gpio_init();
  120. err_code = NRF_LOG_INIT(NULL);
  121. APP_ERROR_CHECK(err_code);
  122. NRF_LOG_DEFAULT_BACKENDS_INIT();
  123. clocks_start();
  124. err_code = esb_init();
  125. APP_ERROR_CHECK(err_code);
  126. NRF_LOG_DEBUG("Enhanced ShockBurst Transmitter Example started.");
  127. while (true)
  128. {
  129. NRF_LOG_DEBUG("Transmitting packet %02x", tx_payload.data[1]);
  130. tx_payload.noack = false;
  131. if (nrf_esb_write_payload(&tx_payload) == NRF_SUCCESS)
  132. {
  133. // Toggle one of the LEDs.
  134. nrf_gpio_pin_write(LED_1, !(tx_payload.data[1]%8>0 && tx_payload.data[1]%8<=4));
  135. nrf_gpio_pin_write(LED_2, !(tx_payload.data[1]%8>1 && tx_payload.data[1]%8<=5));
  136. nrf_gpio_pin_write(LED_3, !(tx_payload.data[1]%8>2 && tx_payload.data[1]%8<=6));
  137. nrf_gpio_pin_write(LED_4, !(tx_payload.data[1]%8>3));
  138. tx_payload.data[1]++;
  139. }
  140. else
  141. {
  142. NRF_LOG_WARNING("Sending packet failed");
  143. }
  144. nrf_delay_us(50000);
  145. }
  146. }