Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

nrf24l01.c 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. #include <stdint.h>
  2. #include <avr/io.h>
  3. #include <util/delay.h>
  4. #include <stdio.h>
  5. #include <stdbool.h>
  6. #include "nrf24l01.h"
  7. #include "nrf24l01_definitions.h"
  8. #include "uart_debug.h"
  9. /* TODO
  10. * - Build a state machine that tracks the mode the NRF is set to
  11. * - Configuration of NRF24L01 and startup
  12. * - Send and Receive functions
  13. * - Interrupt handling for Send and Receive
  14. */
  15. void Print_Register_Contents(uint8_t address);
  16. void Send_TX_Flush_Command(void);
  17. /* Startup and initial configuration of the NRF24L01 */
  18. void Initialize_NRF24L01(void)
  19. {
  20. CONFIG_REGISTER configRegisterContents = {.byte = 0x0};
  21. /* Configure the AVR pins for the nrf24l01 */
  22. /* Set up the NRF24L01 */
  23. NRF_CE_DDR |= (1 << NRF_CE_PIN);
  24. NRF_CSN_DDR |= (1 << NRF_CSN_PIN);
  25. /* Set the chip select pin to not selected */
  26. NRF_CSN_PORT |= (1 << NRF_CSN_PIN);
  27. /* Ensure that the CE pin is set to 0*/
  28. NRF_CE_PORT &= ~(1 << NRF_CE_PIN);
  29. /* Wait more than 10.3 ms to make sure the nrf24l01 is running */
  30. _delay_ms(11);
  31. /* Write the PWR_UP bit of the CONFIG register (EN_CRC is also set) */
  32. configRegisterContents.bits.EN_CRC = 0x1;
  33. configRegisterContents.bits.PWR_UP = 0x1;
  34. Write_NRF_Register(CONFIG_ADDRESS, configRegisterContents.byte);
  35. /* Wait more than 1.5 ms for the change to take effect */
  36. _delay_ms(2);
  37. /* The NRF24L01 is now in the mode Standby-I */
  38. }
  39. void Configure_Transmission(void)
  40. {
  41. FEATURE_REGISTER featureRegisterContents = {.byte = 0x0};
  42. DYNPD_REGISTER dyndpRegisterContents = {.byte = 0x0};
  43. /*
  44. * - Length of CRC (CRCO in CONFIG)
  45. * - Enable auto acknowledgment (EN_AA)
  46. * -> Register already set correctly after reset
  47. * - Enable data pipes (EN_RXADDR)?
  48. * -> Two pipes are already enabled on reset
  49. * - Set up address width (SETUP_AW)
  50. * -> 3 bytes
  51. * - Automatic Retransmission (SETUP_RETR)
  52. * -> ARD = 0b0000
  53. * -> 3 retransmits -> ARC = 0b0011
  54. * -> Register already set correctly after reset
  55. * - RF Channel (RF_CH)
  56. * -> RF_CH = 0b1010000
  57. * - RF Setup (RF_SETUP)
  58. * -> first use reset values, can be fine tuned later
  59. * - Enable dynamic payload length (DYNPD) -> command activate + 0x73, then set bits in FEATURE?
  60. */
  61. /* Set the address width to 3 bytes */
  62. //Write_NRF_Register(0x03, 0x1);
  63. /* Set the frequency to 1450 MHz */
  64. Write_NRF_Register(RF_CH_ADDRESS, 0x32);
  65. /* Enable dynamic payload length */
  66. Send_Activate_Command();
  67. featureRegisterContents.bits.EN_DPL = 1; // enable dynamic payload length
  68. Write_NRF_Register(FEATURE_ADDRESS, featureRegisterContents.byte);
  69. /* set dynamic payload length for all data pipes */
  70. dyndpRegisterContents.bits.DPL_P0 = 1;
  71. dyndpRegisterContents.bits.DPL_P1 = 1;
  72. dyndpRegisterContents.bits.DPL_P2 = 1;
  73. dyndpRegisterContents.bits.DPL_P3 = 1;
  74. dyndpRegisterContents.bits.DPL_P4 = 1;
  75. dyndpRegisterContents.bits.DPL_P5 = 1;
  76. Write_NRF_Register(DYNPD_ADDRESS, dyndpRegisterContents.byte);
  77. /* Set the TX address */
  78. Set_TX_Address(0x123456);
  79. Set_RX_P0_Address(0x123456);
  80. // TODO: set addresses for all data pipes
  81. }
  82. void Send_Test_Message(void)
  83. {
  84. uint8_t buffer[4] = {0xDE, 0xAD, 0xBE, 0xEF};
  85. bool transmissionFinished = false;
  86. STATUS_REGISTER statusRegisterContents = {.byte = 0x0};
  87. uint8_t lengthRead;
  88. char debugString[50] = "";
  89. uint32_t timeout = 0;
  90. /* TODO:
  91. * - if needed: PRIM_RX = 0
  92. * - Set CE = 1 for more than 10 us
  93. * - Wait until the transmission is finished
  94. * - Read number of retries for debug purposes
  95. * - Check if the FIFO is empty -> if not, flush it
  96. * - reset the interupts of the STATUS
  97. */
  98. Write_Message_To_TX_FIFO(4, buffer);
  99. /* Set CE = 1 for more than 10 us */
  100. NRF_CE_PORT |= (1 << NRF_CE_PIN);
  101. _delay_us(15);
  102. NRF_CE_PORT &= ~(1 << NRF_CE_PIN);
  103. do
  104. {
  105. _delay_ms(1);
  106. lengthRead = Read_NRF_Register(STATUS_ADDRESS, &(statusRegisterContents.byte)); /* TODO: use funtion to read status register -> no overflow possible, only one NOP transfer needed, not two */
  107. if (lengthRead > 1)
  108. {
  109. sprintf(debugString, "%s\r\n", "Read error");
  110. Print_Debug_String(debugString);
  111. }
  112. if (statusRegisterContents.bits.TX_DS == 1)
  113. {
  114. transmissionFinished = true;
  115. sprintf(debugString, "%s\r\n", "TX fin");
  116. Print_Debug_String(debugString);
  117. }
  118. if (statusRegisterContents.bits.MAX_RT == 1)
  119. {
  120. transmissionFinished = true; //TODO: indicate failure
  121. sprintf(debugString, "%s\r\n", "max ret");
  122. Print_Debug_String(debugString);
  123. Send_TX_Flush_Command(); /* Remove the packet from the TX FIFO as it is not done automatically */
  124. }
  125. timeout ++; // TODO: this should work without the time out, as MAX_RT should be triggered if no ACK is received
  126. } while ((transmissionFinished == false) && (timeout < 0xFF));
  127. if (timeout >= 0xFF)
  128. {
  129. sprintf(debugString, "%s\r\n", "timeout");
  130. Print_Debug_String(debugString);
  131. }
  132. /* Reset the interrupts */
  133. lengthRead = Read_NRF_Register(STATUS_ADDRESS, &(statusRegisterContents.byte)); /* TODO: use status register read function */
  134. statusRegisterContents.bits.TX_DS = 1;
  135. statusRegisterContents.bits.MAX_RT = 1;
  136. Write_NRF_Register(STATUS_ADDRESS, statusRegisterContents.byte);
  137. // TODO: flush FIFO if an error occured
  138. }
  139. void Print_Register_Contents(uint8_t address)
  140. {
  141. uint8_t registerContent[5];
  142. uint8_t lengthRead;
  143. char debugString[50] = "";
  144. char registerContentString[30];
  145. lengthRead = Read_NRF_Register(address, registerContent);
  146. registerContentString[0] = '\0';
  147. for (uint8_t i = 0; i < lengthRead; i++)
  148. {
  149. sprintf(registerContentString, "%s0x%x ", registerContentString, registerContent[i]);
  150. }
  151. sprintf(debugString, "%s\r\n", registerContentString);
  152. Print_Debug_String(debugString);
  153. }
  154. /* Send a message:
  155. * - Set PRIM_RX = 0 and add one message to the TX-FIFO
  156. * - Set CE=1 for more than 10 us
  157. * - The NRF takes 130 us to enter the TX Mode
  158. * - An Interrupt is generated once the
  159. * -
  160. */
  161. /* Set the NRF to RX Mode */
  162. /* Disable the RX Mode */
  163. uint8_t Read_NRF_Status_Register(void)
  164. {
  165. NRF_CSN_PORT &= ~(1 << NRF_CSN_PIN); // Start the transmission
  166. SPDR = 0XFF;
  167. while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
  168. NRF_CSN_PORT |= (1 << NRF_CSN_PIN); // Stop the transmission
  169. return SPDR;
  170. }
  171. uint8_t Read_NRF_Register(uint8_t address, uint8_t * registerContents)
  172. {
  173. uint8_t numberOfBytes = 0;
  174. if ((address == 0x0A) ||
  175. (address == 0x0B) ||
  176. (address == 0x10))
  177. {
  178. numberOfBytes = 5;
  179. }
  180. else
  181. {
  182. numberOfBytes = 1;
  183. }
  184. /* First write the address */
  185. NRF_CSN_PORT &= ~(1 << NRF_CSN_PIN); // Start the transmission
  186. SPDR = address;
  187. while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
  188. /* Read the register bytes */
  189. for (uint8_t i = 0; i < numberOfBytes; i++)
  190. {
  191. /* Write dummy data to shift in the register content */
  192. SPDR = 0x0;
  193. while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
  194. registerContents[i] = SPDR;
  195. }
  196. NRF_CSN_PORT |= (1 << NRF_CSN_PIN); // Stop the transmission
  197. // TODO: registers with more than one byte
  198. return numberOfBytes;
  199. }
  200. void Write_NRF_Register(uint8_t address, uint8_t registerContents)
  201. {
  202. /* First write the write command with the address */
  203. NRF_CSN_PORT &= ~(1 << NRF_CSN_PIN); // Start the transmission
  204. SPDR = address | 0x20;
  205. while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
  206. /* Write the data byte */
  207. SPDR = registerContents;
  208. while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
  209. NRF_CSN_PORT |= (1 << NRF_CSN_PIN); // Stop the transmission
  210. }
  211. // TODO: clean up functions
  212. void Send_Activate_Command(void)
  213. {
  214. /* First write the write command with the address */
  215. NRF_CSN_PORT &= ~(1 << NRF_CSN_PIN); // Start the transmission
  216. SPDR = 0x50;
  217. while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
  218. /* Write the data byte */
  219. SPDR = 0x73;
  220. while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
  221. NRF_CSN_PORT |= (1 << NRF_CSN_PIN); // Stop the transmission
  222. }
  223. void Send_TX_Flush_Command(void)
  224. {
  225. /* First write the write command with the address */
  226. NRF_CSN_PORT &= ~(1 << NRF_CSN_PIN); // Start the transmission
  227. SPDR = 0xE1;
  228. while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
  229. NRF_CSN_PORT |= (1 << NRF_CSN_PIN); // Stop the transmission
  230. }
  231. void Write_Message_To_TX_FIFO(uint8_t length, uint8_t * buffer)
  232. {
  233. NRF_CSN_PORT &= ~(1 << NRF_CSN_PIN); // Start the transmission
  234. /* Issue the write command: */
  235. SPDR = 0xA0;
  236. while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
  237. /* Write the data bytes */
  238. for (uint8_t i = 0; i < length; i++)
  239. {
  240. SPDR = buffer[i];
  241. while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
  242. }
  243. NRF_CSN_PORT |= (1 << NRF_CSN_PIN); // Stop the transmission
  244. }
  245. void Set_TX_Address(uint32_t txAddress)
  246. {
  247. uint8_t * buffer = (uint8_t*) &txAddress;
  248. /* First write the write command with the address */
  249. NRF_CSN_PORT &= ~(1 << NRF_CSN_PIN); // Start the transmission
  250. SPDR = 0x10 | 0x20;
  251. while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
  252. /* Write the data byte */
  253. for (uint8_t i = 0; i < 4; i ++)
  254. {
  255. SPDR = buffer[i];
  256. while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
  257. }
  258. SPDR = 0x0;
  259. while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
  260. NRF_CSN_PORT |= (1 << NRF_CSN_PIN); // Stop the transmission
  261. }
  262. void Set_RX_P0_Address(uint32_t rxAddress)
  263. {
  264. uint8_t * buffer = (uint8_t*) &rxAddress;
  265. /* First write the write command with the address */
  266. NRF_CSN_PORT &= ~(1 << NRF_CSN_PIN); // Start the transmission
  267. SPDR = 0x0A | 0x20;
  268. while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
  269. /* Write the data byte */
  270. for (uint8_t i = 0; i < 4; i ++)
  271. {
  272. SPDR = buffer[i];
  273. while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
  274. }
  275. SPDR = 0x0;
  276. while(!(SPSR & (1<<SPIF))); // Wait for transmission complete
  277. NRF_CSN_PORT |= (1 << NRF_CSN_PIN); // Stop the transmission
  278. }
  279. //TODO: only write the used bytes into the address registers