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.2KB

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