Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

nrf24l01.c 9.3KB

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