radio_config.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* Copyright (c) 2009 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. /** @file
  13. * @addtogroup nrf_dev_radio_rx_example_main nrf_dev_radio_tx_example_main
  14. * @{
  15. */
  16. #include "radio_config.h"
  17. #include "nrf_delay.h"
  18. /* These are set to zero as Shockburst packets don't have corresponding fields. */
  19. #define PACKET_S1_FIELD_SIZE (0UL) /**< Packet S1 field size in bits. */
  20. #define PACKET_S0_FIELD_SIZE (0UL) /**< Packet S0 field size in bits. */
  21. #define PACKET_LENGTH_FIELD_SIZE (0UL) /**< Packet length field size in bits. */
  22. /**
  23. * @brief Function for swapping/mirroring bits in a byte.
  24. *
  25. *@verbatim
  26. * output_bit_7 = input_bit_0
  27. * output_bit_6 = input_bit_1
  28. * :
  29. * output_bit_0 = input_bit_7
  30. *@endverbatim
  31. *
  32. * @param[in] inp is the input byte to be swapped.
  33. *
  34. * @return
  35. * Returns the swapped/mirrored input byte.
  36. */
  37. static uint32_t swap_bits(uint32_t inp);
  38. /**
  39. * @brief Function for swapping bits in a 32 bit word for each byte individually.
  40. *
  41. * The bits are swapped as follows:
  42. * @verbatim
  43. * output[31:24] = input[24:31]
  44. * output[23:16] = input[16:23]
  45. * output[15:8] = input[8:15]
  46. * output[7:0] = input[0:7]
  47. * @endverbatim
  48. * @param[in] input is the input word to be swapped.
  49. *
  50. * @return
  51. * Returns the swapped input byte.
  52. */
  53. static uint32_t bytewise_bitswap(uint32_t inp);
  54. static uint32_t swap_bits(uint32_t inp)
  55. {
  56. uint32_t i;
  57. uint32_t retval = 0;
  58. inp = (inp & 0x000000FFUL);
  59. for (i = 0; i < 8; i++)
  60. {
  61. retval |= ((inp >> i) & 0x01) << (7 - i);
  62. }
  63. return retval;
  64. }
  65. static uint32_t bytewise_bitswap(uint32_t inp)
  66. {
  67. return (swap_bits(inp >> 24) << 24)
  68. | (swap_bits(inp >> 16) << 16)
  69. | (swap_bits(inp >> 8) << 8)
  70. | (swap_bits(inp));
  71. }
  72. /**
  73. * @brief Function for configuring the radio to operate in Shockburst compatible mode.
  74. *
  75. * To configure the application running on nRF24L series devices:
  76. *
  77. * @verbatim
  78. * uint8_t tx_address[5] = { 0xC0, 0x01, 0x23, 0x45, 0x67 };
  79. * hal_nrf_set_rf_channel(7);
  80. * hal_nrf_set_address_width(HAL_NRF_AW_5BYTES);
  81. * hal_nrf_set_address(HAL_NRF_TX, tx_address);
  82. * hal_nrf_set_address(HAL_NRF_PIPE0, tx_address);
  83. * hal_nrf_open_pipe(0, false);
  84. * hal_nrf_set_datarate(HAL_NRF_1MBPS);
  85. * hal_nrf_set_crc_mode(HAL_NRF_CRC_16BIT);
  86. * hal_nrf_setup_dynamic_payload(0xFF);
  87. * hal_nrf_enable_dynamic_payload(false);
  88. * @endverbatim
  89. *
  90. * When transmitting packets with hal_nrf_write_tx_payload(const uint8_t *tx_pload, uint8_t length),
  91. * match the length with PACKET_STATIC_LENGTH.
  92. * hal_nrf_write_tx_payload(payload, PACKET_STATIC_LENGTH);
  93. *
  94. */
  95. void radio_configure()
  96. {
  97. // Radio config
  98. NRF_RADIO->TXPOWER = (RADIO_TXPOWER_TXPOWER_0dBm << RADIO_TXPOWER_TXPOWER_Pos);
  99. NRF_RADIO->FREQUENCY = 7UL; // Frequency bin 7, 2407MHz
  100. NRF_RADIO->MODE = (RADIO_MODE_MODE_Nrf_1Mbit << RADIO_MODE_MODE_Pos);
  101. // Radio address config
  102. NRF_RADIO->PREFIX0 =
  103. ((uint32_t)swap_bits(0xC3) << 24) // Prefix byte of address 3 converted to nRF24L series format
  104. | ((uint32_t)swap_bits(0xC2) << 16) // Prefix byte of address 2 converted to nRF24L series format
  105. | ((uint32_t)swap_bits(0xC1) << 8) // Prefix byte of address 1 converted to nRF24L series format
  106. | ((uint32_t)swap_bits(0xC0) << 0); // Prefix byte of address 0 converted to nRF24L series format
  107. NRF_RADIO->PREFIX1 =
  108. ((uint32_t)swap_bits(0xC7) << 24) // Prefix byte of address 7 converted to nRF24L series format
  109. | ((uint32_t)swap_bits(0xC6) << 16) // Prefix byte of address 6 converted to nRF24L series format
  110. | ((uint32_t)swap_bits(0xC4) << 0); // Prefix byte of address 4 converted to nRF24L series format
  111. NRF_RADIO->BASE0 = bytewise_bitswap(0x01234567UL); // Base address for prefix 0 converted to nRF24L series format
  112. NRF_RADIO->BASE1 = bytewise_bitswap(0x89ABCDEFUL); // Base address for prefix 1-7 converted to nRF24L series format
  113. NRF_RADIO->TXADDRESS = 0x00UL; // Set device address 0 to use when transmitting
  114. NRF_RADIO->RXADDRESSES = 0x01UL; // Enable device address 0 to use to select which addresses to receive
  115. // Packet configuration
  116. NRF_RADIO->PCNF0 = (PACKET_S1_FIELD_SIZE << RADIO_PCNF0_S1LEN_Pos) |
  117. (PACKET_S0_FIELD_SIZE << RADIO_PCNF0_S0LEN_Pos) |
  118. (PACKET_LENGTH_FIELD_SIZE << RADIO_PCNF0_LFLEN_Pos); //lint !e845 "The right argument to operator '|' is certain to be 0"
  119. // Packet configuration
  120. NRF_RADIO->PCNF1 = (RADIO_PCNF1_WHITEEN_Disabled << RADIO_PCNF1_WHITEEN_Pos) |
  121. (RADIO_PCNF1_ENDIAN_Big << RADIO_PCNF1_ENDIAN_Pos) |
  122. (PACKET_BASE_ADDRESS_LENGTH << RADIO_PCNF1_BALEN_Pos) |
  123. (PACKET_STATIC_LENGTH << RADIO_PCNF1_STATLEN_Pos) |
  124. (PACKET_PAYLOAD_MAXSIZE << RADIO_PCNF1_MAXLEN_Pos); //lint !e845 "The right argument to operator '|' is certain to be 0"
  125. // CRC Config
  126. NRF_RADIO->CRCCNF = (RADIO_CRCCNF_LEN_Two << RADIO_CRCCNF_LEN_Pos); // Number of checksum bits
  127. if ((NRF_RADIO->CRCCNF & RADIO_CRCCNF_LEN_Msk) == (RADIO_CRCCNF_LEN_Two << RADIO_CRCCNF_LEN_Pos))
  128. {
  129. NRF_RADIO->CRCINIT = 0xFFFFUL; // Initial value
  130. NRF_RADIO->CRCPOLY = 0x11021UL; // CRC poly: x^16+x^12^x^5+1
  131. }
  132. else if ((NRF_RADIO->CRCCNF & RADIO_CRCCNF_LEN_Msk) == (RADIO_CRCCNF_LEN_One << RADIO_CRCCNF_LEN_Pos))
  133. {
  134. NRF_RADIO->CRCINIT = 0xFFUL; // Initial value
  135. NRF_RADIO->CRCPOLY = 0x107UL; // CRC poly: x^8+x^2^x^1+1
  136. }
  137. }
  138. /**
  139. * @}
  140. */