Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

nrf24l01.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. #include <stdint.h>
  2. #include <avr/io.h>
  3. #include <util/delay.h>
  4. #include <stdio.h>
  5. #include <stdbool.h>
  6. #include "spi.h"
  7. #include "nrf24l01.h"
  8. #include "nrf24l01_definitions.h"
  9. extern volatile bool nrfInterruptRaised;
  10. void Print_Register_Contents(uint8_t address);
  11. void Send_TX_Flush_Command(void);
  12. void Send_RX_Flush_Command(void);
  13. static uint8_t Write_One_Byte(uint8_t byte1);
  14. static uint8_t Write_Two_Bytes(uint8_t byte1, uint8_t byte2);
  15. static void Write_Byte_And_Buffer(uint8_t byte, uint8_t * buffer, uint8_t length);
  16. /* Startup and initial configuration of the NRF24L01 */
  17. void Initialize_NRF24L01(void)
  18. {
  19. CONFIG_REGISTER configRegisterContents = {.byte = 0x0};
  20. /* Configure the AVR pins for the nrf24l01 */
  21. Set_NRF24L01_Pins();
  22. /* Wait more than 10.3 ms to make sure the nrf24l01 is running */
  23. _delay_ms(11);
  24. /* Write the PWR_UP bit of the CONFIG register (EN_CRC is also set) */
  25. configRegisterContents.bits.EN_CRC = 0x1;
  26. configRegisterContents.bits.PWR_UP = 0x1;
  27. Write_NRF_Register(CONFIG_ADDRESS, configRegisterContents.byte);
  28. /* Wait more than 1.5 ms for the change to take effect */
  29. _delay_ms(2);
  30. /* The NRF24L01 is now in the mode Standby-I */
  31. }
  32. void Set_NRF24L01_Pins(void)
  33. {
  34. /* Set up the NRF24L01 */
  35. NRF_CE_DDR |= (1 << NRF_CE_PIN);
  36. NRF_CSN_DDR |= (1 << NRF_CSN_PIN);
  37. /* Set the chip select pin to not selected */
  38. NRF_CSN_PORT |= (1 << NRF_CSN_PIN);
  39. /* Ensure that the CE pin is set to 0*/
  40. NRF_CE_PORT &= ~(1 << NRF_CE_PIN);
  41. /* Set the interrupt pin */
  42. NRF_IRQ_DDR &= ~(1 << NRF_IRQ_PIN); // Set the pin as input
  43. NRF_IRQ_PORT |= (1 << NRF_IRQ_PORT); // Enable the pullup for the pin
  44. }
  45. void Configure_Transmission(uint8_t moduleId)
  46. {
  47. SETUP_RETR_REGISTER setupRetrRegisterContents = {.byte = 0x0};
  48. EN_RXADDR_REGISTER enableRxAddressesRegisterContents = {.byte = 0x0};
  49. RX_PW_Pn_REGISTER rxPwPnRegisterContents = {.byte = 0x0};
  50. EN_AA_REGISTER enAaRegister = {.byte = 0x0};
  51. FEATURE_REGISTER featureRegisterContents = {.byte = 0x0};
  52. DYNPD_REGISTER dyndpRegisterContents = {.byte = 0x0};
  53. uint8_t txAddress[5] = {0xB3, 0xB3, 0xB3, 0xB3, 0x00};
  54. uint8_t rx1Address[5] = {0xB3, 0xB3, 0xB3, 0xB3, 0x00};
  55. /* Set the frequency to 1450 MHz */
  56. Write_NRF_Register(RF_CH_ADDRESS, 0x32);
  57. /* Enable dynamic payload length */
  58. Send_Activate_Command();
  59. featureRegisterContents.bits.EN_DPL = 1; // enable dynamic payload length
  60. Write_NRF_Register(FEATURE_ADDRESS, featureRegisterContents.byte);
  61. /* set dynamic payload length for all data pipes
  62. * When the dynamic payload length is not set the module cannot receive packets from some
  63. * stations. This is probably due to counterfeit NRF24L01+ chips.
  64. */
  65. dyndpRegisterContents.bits.DPL_P0 = 1;
  66. dyndpRegisterContents.bits.DPL_P1 = 1;
  67. dyndpRegisterContents.bits.DPL_P2 = 1;
  68. dyndpRegisterContents.bits.DPL_P3 = 1;
  69. dyndpRegisterContents.bits.DPL_P4 = 1;
  70. dyndpRegisterContents.bits.DPL_P5 = 1;
  71. Write_NRF_Register(DYNPD_ADDRESS, dyndpRegisterContents.byte);
  72. /* Set up the auto retries */
  73. setupRetrRegisterContents.bits.ARC = 0x3;
  74. setupRetrRegisterContents.bits.ARD = 0xF;
  75. Write_NRF_Register(SETUP_RETR_ADDRESS, setupRetrRegisterContents.byte);
  76. /* Set the TX address */
  77. Set_TX_Address(txAddress, MAX_ADDRESS_LENGTH);
  78. /* Set the address of the RX pipe 0 to the one of the base station to receive acks */
  79. Set_RX_P0_Address(txAddress, MAX_ADDRESS_LENGTH);
  80. /* Set the address of the RX pipe 1 to the own address to receive messages */
  81. rx1Address[4] = moduleId; // The last byte of the address corresponds to the Id set by the pin programming
  82. Set_RX_P1_Address(rx1Address, MAX_ADDRESS_LENGTH);
  83. /* Enable the rx addresses for pipe 0 and pipe 1*/
  84. enableRxAddressesRegisterContents.bits.ERX_P0 = 1;
  85. enableRxAddressesRegisterContents.bits.ERX_P1 = 1;
  86. Write_NRF_Register(EN_RXADDR_ADDRESS, enableRxAddressesRegisterContents.byte);
  87. /* Set the payload witth for pipe 1 */
  88. rxPwPnRegisterContents.bits.RX_PW_Pn = 32;
  89. Write_NRF_Register(RX_PW_P1_ADDRESS, rxPwPnRegisterContents.byte);
  90. rxPwPnRegisterContents.bits.RX_PW_Pn = 0;
  91. Write_NRF_Register(RX_PW_P0_ADDRESS, rxPwPnRegisterContents.byte); // auto-ack pipe
  92. Write_NRF_Register(RX_PW_P2_ADDRESS, rxPwPnRegisterContents.byte); // not used
  93. Write_NRF_Register(RX_PW_P3_ADDRESS, rxPwPnRegisterContents.byte); // not used
  94. Write_NRF_Register(RX_PW_P4_ADDRESS, rxPwPnRegisterContents.byte); // not used
  95. Write_NRF_Register(RX_PW_P5_ADDRESS, rxPwPnRegisterContents.byte); // not used
  96. /* Enable auto acknowledge for pipe 1 */
  97. enAaRegister.bits.ENAA_P0 = 1;
  98. enAaRegister.bits.ENAA_P1 = 1;
  99. enAaRegister.bits.ENAA_P2 = 1;
  100. enAaRegister.bits.ENAA_P3 = 1;
  101. enAaRegister.bits.ENAA_P4 = 1;
  102. enAaRegister.bits.ENAA_P5 = 1;
  103. Write_NRF_Register(EN_AA_ADDRESS, enAaRegister.byte);
  104. /* Flush FIFOs */
  105. Send_TX_Flush_Command();
  106. Send_RX_Flush_Command();
  107. PCMSK2 |= (1<<PCINT21); // Set the external interrupt for PD5
  108. }
  109. bool NRF24L01_Send_Message(uint8_t *buffer, uint8_t length)
  110. {
  111. bool success = false;
  112. STATUS_REGISTER statusRegisterContents = {.byte = 0x0};
  113. if ((length > 32) || (length == 0))
  114. {
  115. return success;
  116. }
  117. PCICR |= (1<<PCIE2); // Enable the interrupt for the IRQ signal
  118. Write_Message_To_TX_FIFO(length, buffer);
  119. /* Set CE = 1 for more than 10 us */
  120. NRF_CE_PORT |= (1 << NRF_CE_PIN);
  121. _delay_us(15);
  122. NRF_CE_PORT &= ~(1 << NRF_CE_PIN);
  123. while (nrfInterruptRaised == false); // Wait until the transmission is complete
  124. /* An interrupt instead of polling the status register is used to avoid transmission errors
  125. * induced by the SPI:
  126. * https://forum.mysensors.org/topic/10452/nrf24l01-communication-failure-root-cause-and-solution
  127. */
  128. statusRegisterContents.byte = Read_NRF_Status_Register();
  129. if (statusRegisterContents.bits.MAX_RT == 1)
  130. {
  131. Send_TX_Flush_Command(); /* Remove the packet from the TX FIFO as it is not done automatically */
  132. }
  133. else
  134. {
  135. success = true;
  136. }
  137. /* Reset the interrupts */
  138. statusRegisterContents.bits.TX_DS = 1;
  139. statusRegisterContents.bits.MAX_RT = 1;
  140. statusRegisterContents.bits.RX_DR = 1;
  141. Write_NRF_Register(STATUS_ADDRESS, statusRegisterContents.byte);
  142. PCICR &= ~(1<<PCIE2); // Disable the interrupt for the IRQ signal
  143. nrfInterruptRaised = false;
  144. return success;
  145. }
  146. bool NRF24L01_Receive_Message(uint8_t *buffer, uint8_t duration)
  147. {
  148. uint8_t messageReceived = false;
  149. CONFIG_REGISTER configRegisterContents = {.byte = 0x0};
  150. STATUS_REGISTER statusRegisterContents = {.byte = 0x0};
  151. FIFO_STATUS_REGISTER fifoStatusRegisterContents = {.byte = 0x0};
  152. PCICR |= (1<<PCIE2); // Enable the interrupt for the IRQ signal
  153. // Enable the receive mode
  154. configRegisterContents.byte = Read_NRF_Register(CONFIG_ADDRESS);
  155. configRegisterContents.bits.PRIM_RX = 0x1;
  156. Write_NRF_Register(CONFIG_ADDRESS, configRegisterContents.byte);
  157. NRF_CE_PORT |= (1 << NRF_CE_PIN);
  158. _delay_ms(10);
  159. while ((nrfInterruptRaised == false) && (duration > 0))
  160. {
  161. _delay_ms(1);
  162. duration --;
  163. };
  164. if (nrfInterruptRaised == true) // check if a message was received
  165. {
  166. /* A message was received */
  167. statusRegisterContents.byte = Read_NRF_Status_Register();
  168. fifoStatusRegisterContents.byte = Read_NRF_Register(FIFO_STATUS_ADDRESS);
  169. if (fifoStatusRegisterContents.bits.RX_EMPTY != 1)
  170. {
  171. messageReceived = true;
  172. }
  173. nrfInterruptRaised = false;
  174. }
  175. // Set the NRF to standby
  176. NRF_CE_PORT &= ~(1 << NRF_CE_PIN);
  177. configRegisterContents.byte = Read_NRF_Register(CONFIG_ADDRESS);
  178. configRegisterContents.bits.PRIM_RX = 0x0;
  179. Write_NRF_Register(CONFIG_ADDRESS, configRegisterContents.byte);
  180. PCICR &= ~(1<<PCIE2); // Disable the interrupt for the IRQ signal
  181. if (messageReceived == true)
  182. {
  183. Read_Message_From_RX_FIFO(PACKET_LENGTH, buffer);
  184. }
  185. /* Reset the interrupts */
  186. statusRegisterContents.bits.TX_DS = 1;
  187. statusRegisterContents.bits.MAX_RT = 1;
  188. statusRegisterContents.bits.RX_DR = 1;
  189. Write_NRF_Register(STATUS_ADDRESS, statusRegisterContents.byte);
  190. return messageReceived;
  191. }
  192. uint8_t Read_NRF_Status_Register(void)
  193. {
  194. uint8_t registerContents;
  195. registerContents = Write_One_Byte(0x0);
  196. return registerContents;
  197. }
  198. uint8_t Read_NRF_Register(uint8_t address)
  199. {
  200. uint8_t registerContents;
  201. registerContents = Write_Two_Bytes(address, 0x0);
  202. return registerContents;
  203. }
  204. void Write_NRF_Register(uint8_t address, uint8_t registerContents)
  205. {
  206. Write_Two_Bytes(address | 0x20, registerContents);
  207. }
  208. void Send_Activate_Command(void)
  209. {
  210. Write_Two_Bytes(0x50, 0x73);
  211. }
  212. void Send_TX_Flush_Command(void)
  213. {
  214. Write_One_Byte(FLUSH_TX_COMMAND);
  215. }
  216. void Send_RX_Flush_Command(void)
  217. {
  218. Write_One_Byte(FLUSH_RX_COMMAND);
  219. }
  220. static uint8_t Write_One_Byte(uint8_t byte1)
  221. {
  222. uint8_t registerContents = 0;
  223. SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  224. registerContents = SPI_Transfer_Byte(byte1);
  225. SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  226. return registerContents;
  227. }
  228. static uint8_t Write_Two_Bytes(uint8_t byte1, uint8_t byte2)
  229. {
  230. uint8_t registerContents = 0;
  231. SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  232. SPI_Transfer_Byte(byte1);
  233. registerContents = SPI_Transfer_Byte(byte2);
  234. SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  235. return registerContents;
  236. }
  237. void Write_Message_To_TX_FIFO(uint8_t length, uint8_t * buffer)
  238. {
  239. Write_Byte_And_Buffer(W_TX_PAYLOAD_COMMAND, buffer, length);
  240. }
  241. void Read_Message_From_RX_FIFO(uint8_t length, uint8_t * buffer)
  242. {
  243. SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  244. SPI_Transfer_Byte(R_RX_PAYLOAD_COMMAND);
  245. /* Write the data byte */
  246. for (uint8_t i = 0; i < length; i ++)
  247. {
  248. buffer[i] = SPI_Transfer_Byte(0x0);
  249. }
  250. SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  251. }
  252. void Set_TX_Address(uint8_t * txAddress, uint8_t length)
  253. {
  254. Write_Byte_And_Buffer(TX_ADDR_ADDRESS | 0x20, txAddress, length);
  255. }
  256. void Set_RX_P0_Address(uint8_t * rxAddress, uint8_t length)
  257. {
  258. Write_Byte_And_Buffer(RX_ADDR_P0_ADDRESS | 0x20, rxAddress, length);
  259. }
  260. void Set_RX_P1_Address(uint8_t * rxAddress, uint8_t length)
  261. {
  262. Write_Byte_And_Buffer(RX_ADDR_P1_ADDRESS | 0x20, rxAddress, length);
  263. }
  264. static void Write_Byte_And_Buffer(uint8_t byte, uint8_t * buffer, uint8_t length)
  265. {
  266. SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  267. SPI_Transfer_Byte(byte);
  268. /* Write the data byte */
  269. for (uint8_t i = 0; i < length; i ++)
  270. {
  271. SPI_Transfer_Byte(buffer[i]);
  272. }
  273. SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  274. }