説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

nrf24l01.c 9.9KB

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