Nessuna descrizione
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nrf24l01.c 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #include <stdint.h>
  2. #include <avr/io.h>
  3. #include <util/delay.h>
  4. #include <stdio.h>
  5. #include <stdbool.h>
  6. #include "led.h" // TODO: for debugging
  7. #include "spi.h"
  8. #include "nrf24l01.h"
  9. #include "nrf24l01_definitions.h"
  10. /* TODO
  11. * - Send functions
  12. * - Interrupt handling for Send
  13. */
  14. extern volatile bool nrfInterruptRaised;
  15. void Print_Register_Contents(uint8_t address);
  16. void Send_TX_Flush_Command(void);
  17. static void Write_Two_Bytes(uint8_t byte1, uint8_t byte2);
  18. static void Write_Byte_And_Buffer(uint8_t byte, uint8_t * buffer, uint8_t length);
  19. /* Startup and initial configuration of the NRF24L01 */
  20. void Initialize_NRF24L01(void)
  21. {
  22. CONFIG_REGISTER configRegisterContents = {.byte = 0x0};
  23. /* Configure the AVR pins for the nrf24l01 */
  24. Set_NRF24L01_Pins();
  25. /* Wait more than 10.3 ms to make sure the nrf24l01 is running */
  26. _delay_ms(11);
  27. /* Write the PWR_UP bit of the CONFIG register (EN_CRC is also set) */
  28. configRegisterContents.bits.EN_CRC = 0x1;
  29. configRegisterContents.bits.PWR_UP = 0x1;
  30. Write_NRF_Register(CONFIG_ADDRESS, configRegisterContents.byte);
  31. /* Wait more than 1.5 ms for the change to take effect */
  32. _delay_ms(2);
  33. /* The NRF24L01 is now in the mode Standby-I */
  34. }
  35. void Set_NRF24L01_Pins(void)
  36. {
  37. /* Set up the NRF24L01 */
  38. NRF_CE_DDR |= (1 << NRF_CE_PIN);
  39. NRF_CSN_DDR |= (1 << NRF_CSN_PIN);
  40. /* Set the chip select pin to not selected */
  41. NRF_CSN_PORT |= (1 << NRF_CSN_PIN);
  42. /* Ensure that the CE pin is set to 0*/
  43. NRF_CE_PORT &= ~(1 << NRF_CE_PIN);
  44. /* Set the interrupt pin */
  45. /* TODO: PCINT21 -> PCINT2 */
  46. NRF_IRQ_DDR &= ~(1 << NRF_IRQ_PIN); // Set the pin as input
  47. NRF_IRQ_PORT |= (1 << NRF_IRQ_PORT); // Enable the pullup for the pin
  48. }
  49. void Configure_Transmission(uint8_t moduleId)
  50. {
  51. SETUP_RETR_REGISTER setupRetrRegisterContents = {.byte = 0x0};
  52. EN_RXADDR_REGISTER enableRxAddressesRegisterContents = {.byte = 0x0};
  53. RX_PW_Pn_REGISTER rxPwPnRegisterContents = {.byte = 0x0};
  54. EN_AA_REGISTER enAaRegister = {.byte = 0x0};
  55. FEATURE_REGISTER featureRegisterContents = {.byte = 0x0};
  56. DYNPD_REGISTER dyndpRegisterContents = {.byte = 0x0};
  57. uint8_t txAddress[5] = {0xB3, 0xB3, 0xB3, 0xB3, 0x00};
  58. uint8_t rx0Address[5] = {0xB3, 0xB3, 0xB3, 0xB3, 0x00};
  59. /*
  60. * - Length of CRC (CRCO in CONFIG)
  61. * - Enable auto acknowledgment (EN_AA)
  62. * -> Register already set correctly after reset
  63. * - Enable data pipes (EN_RXADDR)?
  64. * -> Two pipes are already enabled on reset
  65. * - Set up address width (SETUP_AW)
  66. * -> 3 bytes
  67. * - Automatic Retransmission (SETUP_RETR)
  68. * -> ARD = 0b0000
  69. * -> 3 retransmits -> ARC = 0b0011
  70. * -> Register already set correctly after reset
  71. * - RF Channel (RF_CH)
  72. * -> RF_CH = 0b1010000
  73. * - RF Setup (RF_SETUP)
  74. * -> first use reset values, can be fine tuned later
  75. * - Enable dynamic payload length (DYNPD) -> command activate + 0x73, then set bits in FEATURE?
  76. */
  77. /* Set the address width to 3 bytes */
  78. //Write_NRF_Register(0x03, 0x1);
  79. /* Set the frequency to 1450 MHz */
  80. Write_NRF_Register(RF_CH_ADDRESS, 0x32);
  81. /* Enable dynamic payload length */
  82. Send_Activate_Command();
  83. featureRegisterContents.bits.EN_DPL = 1; // enable dynamic payload length
  84. Write_NRF_Register(FEATURE_ADDRESS, featureRegisterContents.byte);
  85. /* set dynamic payload length for all data pipes
  86. * When the dynamic payload length is not set the module cannot receive packets from some
  87. * stations. This is probably due to counterfeit NRF24L01+ chips.
  88. */
  89. dyndpRegisterContents.bits.DPL_P0 = 1;
  90. dyndpRegisterContents.bits.DPL_P1 = 1;
  91. dyndpRegisterContents.bits.DPL_P2 = 1;
  92. dyndpRegisterContents.bits.DPL_P3 = 1;
  93. dyndpRegisterContents.bits.DPL_P4 = 1;
  94. dyndpRegisterContents.bits.DPL_P5 = 1;
  95. Write_NRF_Register(DYNPD_ADDRESS, dyndpRegisterContents.byte);
  96. /* Set up the auto retries */
  97. /* */
  98. setupRetrRegisterContents.bits.ARC = 0x3;
  99. setupRetrRegisterContents.bits.ARD = 0xF;
  100. Write_NRF_Register(SETUP_RETR_ADDRESS, setupRetrRegisterContents.byte);
  101. /* Set the TX address */
  102. Set_TX_Address(txAddress, MAX_ADDRESS_LENGTH);
  103. /* Set the RX_P0 address to the one of the base station to receive acks */
  104. Set_RX_P0_Address(rx0Address, MAX_ADDRESS_LENGTH);
  105. PCMSK2 |= (1<<PCINT21); // Set the external interrupt for PD5
  106. }
  107. void NRF24L01_Send_Message(uint8_t *buffer, uint8_t length)
  108. {
  109. STATUS_REGISTER statusRegisterContents = {.byte = 0x0};
  110. if ((length > 32) || (length == 0))
  111. {
  112. return;
  113. }
  114. PCICR |= (1<<PCIE2); // Enable the interrupt for the IRQ signal
  115. Write_Message_To_TX_FIFO(length, buffer);
  116. /* Set CE = 1 for more than 10 us */
  117. NRF_CE_PORT |= (1 << NRF_CE_PIN);
  118. _delay_us(15);
  119. NRF_CE_PORT &= ~(1 << NRF_CE_PIN);
  120. while (nrfInterruptRaised == false); // Wait until the transmission is complete
  121. /* An interrupt instead of polling the status register is used to avoid transmission errors
  122. * induced by the SPI:
  123. * https://forum.mysensors.org/topic/10452/nrf24l01-communication-failure-root-cause-and-solution
  124. */
  125. LED_PORT |= (1 << LED_PIN);
  126. statusRegisterContents.byte = Read_NRF_Status_Register();
  127. if (statusRegisterContents.bits.MAX_RT == 1)
  128. {
  129. Send_TX_Flush_Command(); /* Remove the packet from the TX FIFO as it is not done automatically */
  130. }
  131. /* Reset the interrupts */
  132. statusRegisterContents.bits.TX_DS = 1;
  133. statusRegisterContents.bits.MAX_RT = 1;
  134. statusRegisterContents.bits.RX_DR = 1;
  135. Write_NRF_Register(STATUS_ADDRESS, statusRegisterContents.byte);
  136. PCICR &= ~(1<<PCIE2); // Disable the interrupt for the IRQ signal
  137. nrfInterruptRaised = false;
  138. return;
  139. }
  140. uint8_t Read_NRF_Status_Register(void)
  141. {
  142. uint8_t registerContents;
  143. SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  144. registerContents = SPI_Transfer_Byte(0x00);
  145. SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  146. return registerContents;
  147. }
  148. /* TODO: rewrite the read register function if it is needed (remove the read operations for the 5-byte registers)*/
  149. #if 0
  150. uint8_t Read_NRF_Register(uint8_t address, uint8_t * registerContents)
  151. {
  152. /* TODO: simplify this function, as the registers with more than one byte are accessed with other functions */
  153. uint8_t numberOfBytes = 0;
  154. if ((address == 0x0A) ||
  155. (address == 0x0B) ||
  156. (address == 0x10))
  157. {
  158. numberOfBytes = 5;
  159. }
  160. else
  161. {
  162. numberOfBytes = 1;
  163. }
  164. /* First write the address */
  165. SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  166. SPI_Transfer_Byte(address);
  167. /* Read the register bytes */
  168. for (uint8_t i = 0; i < numberOfBytes; i++)
  169. {
  170. /* Write dummy data to shift in the register content */
  171. registerContents[i] = SPI_Transfer_Byte(0x0);
  172. }
  173. SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  174. return numberOfBytes;
  175. }
  176. #endif
  177. void Write_NRF_Register(uint8_t address, uint8_t registerContents)
  178. {
  179. Write_Two_Bytes(address | 0x20, registerContents);
  180. }
  181. void Send_Activate_Command(void)
  182. {
  183. Write_Two_Bytes(0x50, 0x73);
  184. }
  185. static void Write_Two_Bytes(uint8_t byte1, uint8_t byte2)
  186. {
  187. SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  188. SPI_Transfer_Byte(byte1);
  189. SPI_Transfer_Byte(byte2);
  190. SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  191. }
  192. void Send_TX_Flush_Command(void)
  193. {
  194. /* First write the write command with the address */
  195. SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  196. SPI_Transfer_Byte(0xE1);
  197. SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  198. }
  199. void Write_Message_To_TX_FIFO(uint8_t length, uint8_t * buffer)
  200. {
  201. Write_Byte_And_Buffer(0xA0, buffer, length);
  202. }
  203. void Set_TX_Address(uint8_t * txAddress, uint8_t length)
  204. {
  205. Write_Byte_And_Buffer(TX_ADDR_ADDRESS | 0x20, txAddress, length);
  206. }
  207. void Set_RX_P0_Address(uint8_t * rxAddress, uint8_t length)
  208. {
  209. Write_Byte_And_Buffer(RX_ADDR_P0_ADDRESS | 0x20, rxAddress, length);
  210. }
  211. static void Write_Byte_And_Buffer(uint8_t byte, uint8_t * buffer, uint8_t length)
  212. {
  213. SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  214. SPI_Transfer_Byte(byte);
  215. /* Write the data byte */
  216. for (uint8_t i = 0; i < length; i ++)
  217. {
  218. SPI_Transfer_Byte(buffer[i]);
  219. }
  220. SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  221. }
  222. //TODO: only write the used bytes into the address registers & add generic write functions