No Description
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 9.1KB

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