Açıklama Yok
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 8.1KB

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