Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. /* 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_NRF24L01_Pins();
  23. /* Wait more than 10.3 ms to make sure the nrf24l01 is running */
  24. _delay_ms(11);
  25. /* Write the PWR_UP bit of the CONFIG register (EN_CRC is also set) */
  26. configRegisterContents.bits.EN_CRC = 0x1;
  27. configRegisterContents.bits.PWR_UP = 0x1;
  28. Write_NRF_Register(CONFIG_ADDRESS, configRegisterContents.byte);
  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 Set_NRF24L01_Pins(void)
  34. {
  35. /* Set up the NRF24L01 */
  36. NRF_CE_DDR |= (1 << NRF_CE_PIN);
  37. NRF_CSN_DDR |= (1 << NRF_CSN_PIN);
  38. /* Set the chip select pin to not selected */
  39. NRF_CSN_PORT |= (1 << NRF_CSN_PIN);
  40. /* Ensure that the CE pin is set to 0*/
  41. NRF_CE_PORT &= ~(1 << NRF_CE_PIN);
  42. }
  43. void Configure_Transmission(void)
  44. {
  45. FEATURE_REGISTER featureRegisterContents = {.byte = 0x0};
  46. DYNPD_REGISTER dyndpRegisterContents = {.byte = 0x0};
  47. SETUP_RETR_REGISTER setupRetrRegisterContents = {.byte = 0x0};
  48. /*
  49. * - Length of CRC (CRCO in CONFIG)
  50. * - Enable auto acknowledgment (EN_AA)
  51. * -> Register already set correctly after reset
  52. * - Enable data pipes (EN_RXADDR)?
  53. * -> Two pipes are already enabled on reset
  54. * - Set up address width (SETUP_AW)
  55. * -> 3 bytes
  56. * - Automatic Retransmission (SETUP_RETR)
  57. * -> ARD = 0b0000
  58. * -> 3 retransmits -> ARC = 0b0011
  59. * -> Register already set correctly after reset
  60. * - RF Channel (RF_CH)
  61. * -> RF_CH = 0b1010000
  62. * - RF Setup (RF_SETUP)
  63. * -> first use reset values, can be fine tuned later
  64. * - Enable dynamic payload length (DYNPD) -> command activate + 0x73, then set bits in FEATURE?
  65. */
  66. /* Set the address width to 3 bytes */
  67. //Write_NRF_Register(0x03, 0x1);
  68. /* Set the frequency to 1450 MHz */
  69. Write_NRF_Register(RF_CH_ADDRESS, 0x32);
  70. /* Enable dynamic payload length */
  71. Send_Activate_Command();
  72. featureRegisterContents.bits.EN_DPL = 1; // enable dynamic payload length
  73. Write_NRF_Register(FEATURE_ADDRESS, featureRegisterContents.byte);
  74. /* */
  75. setupRetrRegisterContents.bits.ARC = 0x3;
  76. setupRetrRegisterContents.bits.ARD = 0xF;
  77. Write_NRF_Register(SETUP_RETR_ADDRESS, setupRetrRegisterContents.byte);
  78. /* set dynamic payload length for all data pipes */
  79. dyndpRegisterContents.bits.DPL_P0 = 1;
  80. dyndpRegisterContents.bits.DPL_P1 = 1;
  81. dyndpRegisterContents.bits.DPL_P2 = 1;
  82. dyndpRegisterContents.bits.DPL_P3 = 1;
  83. dyndpRegisterContents.bits.DPL_P4 = 1;
  84. dyndpRegisterContents.bits.DPL_P5 = 1;
  85. Write_NRF_Register(DYNPD_ADDRESS, dyndpRegisterContents.byte);
  86. /* Set the TX address */
  87. Set_TX_Address(0x123456);
  88. Set_RX_P0_Address(0x123456);
  89. // TODO: set addresses for all data pipes
  90. }
  91. void NRF24L01_Send_Message(uint8_t *buffer, uint8_t length)
  92. {
  93. bool transmissionFinished = false;
  94. STATUS_REGISTER statusRegisterContents = {.byte = 0x0};
  95. uint32_t timeout = 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. /* TODO: messages with more than 32 byte length */
  105. if ((length > 32) || (length == 0))
  106. {
  107. return;
  108. }
  109. Write_Message_To_TX_FIFO(length, buffer);
  110. /* Set CE = 1 for more than 10 us */
  111. NRF_CE_PORT |= (1 << NRF_CE_PIN);
  112. _delay_us(15);
  113. NRF_CE_PORT &= ~(1 << NRF_CE_PIN);
  114. do
  115. {
  116. _delay_ms(1);
  117. /* TODO: instead of polling the status register use the IRQ to spawn an interrupt as the
  118. * constant polling may induce transmission errors:
  119. * https://forum.mysensors.org/topic/10452/nrf24l01-communication-failure-root-cause-and-solution
  120. */
  121. statusRegisterContents.byte = Read_NRF_Status_Register();
  122. if (statusRegisterContents.bits.TX_DS == 1)
  123. {
  124. transmissionFinished = true;
  125. }
  126. if (statusRegisterContents.bits.MAX_RT == 1)
  127. {
  128. transmissionFinished = true; //TODO: indicate failure
  129. Send_TX_Flush_Command(); /* Remove the packet from the TX FIFO as it is not done automatically */
  130. }
  131. timeout ++; // TODO: this should work without the time out, as MAX_RT should be triggered if no ACK is received
  132. } while ((transmissionFinished == false) && (timeout < 0xFF));
  133. /* Reset the interrupts */
  134. statusRegisterContents.byte = Read_NRF_Status_Register();
  135. statusRegisterContents.bits.TX_DS = 1;
  136. statusRegisterContents.bits.MAX_RT = 1;
  137. Write_NRF_Register(STATUS_ADDRESS, statusRegisterContents.byte);
  138. }
  139. void Print_Register_Contents(uint8_t address)
  140. {
  141. uint8_t registerContent[5];
  142. uint8_t lengthRead;
  143. char registerContentString[30];
  144. lengthRead = Read_NRF_Register(address, registerContent);
  145. registerContentString[0] = '\0';
  146. for (uint8_t i = 0; i < lengthRead; i++)
  147. {
  148. sprintf(registerContentString, "%s0x%x ", registerContentString, registerContent[i]);
  149. }
  150. }
  151. /* Send a message:
  152. * - Set PRIM_RX = 0 and add one message to the TX-FIFO
  153. * - Set CE=1 for more than 10 us
  154. * - The NRF takes 130 us to enter the TX Mode
  155. * - An Interrupt is generated once the
  156. * -
  157. */
  158. /* Set the NRF to RX Mode */
  159. /* Disable the RX Mode */
  160. uint8_t Read_NRF_Status_Register(void)
  161. {
  162. uint8_t registerContents;
  163. SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  164. registerContents = SPI_Transfer_Byte(0x00);
  165. SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  166. return registerContents;
  167. }
  168. uint8_t Read_NRF_Register(uint8_t address, uint8_t * registerContents)
  169. {
  170. /* TODO: simplify this function, as the registers with more than one byte are accessed with other functions */
  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. SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  184. SPI_Transfer_Byte(address);
  185. /* Read the register bytes */
  186. for (uint8_t i = 0; i < numberOfBytes; i++)
  187. {
  188. /* Write dummy data to shift in the register content */
  189. registerContents[i] = SPI_Transfer_Byte(0x0);
  190. }
  191. SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  192. return numberOfBytes;
  193. }
  194. void Write_NRF_Register(uint8_t address, uint8_t registerContents)
  195. {
  196. /* First write the write command with the address */
  197. SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  198. SPI_Transfer_Byte(address | 0x20);
  199. /* Write the data byte */
  200. SPI_Transfer_Byte(registerContents);
  201. SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  202. }
  203. // TODO: clean up functions
  204. void Send_Activate_Command(void)
  205. {
  206. /* First write the write command with the address */
  207. SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  208. SPI_Transfer_Byte(0x50);
  209. /* Write the data byte */
  210. SPI_Transfer_Byte(0x73);
  211. SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  212. }
  213. void Send_TX_Flush_Command(void)
  214. {
  215. /* First write the write command with the address */
  216. SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  217. SPI_Transfer_Byte(0xE1);
  218. SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  219. }
  220. void Write_Message_To_TX_FIFO(uint8_t length, uint8_t * buffer)
  221. {
  222. SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  223. /* Issue the write command: */
  224. SPI_Transfer_Byte(0xA0);
  225. /* Write the data bytes */
  226. for (uint8_t i = 0; i < length; i++)
  227. {
  228. SPI_Transfer_Byte(buffer[i]);
  229. }
  230. SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  231. }
  232. void Set_TX_Address(uint32_t txAddress)
  233. {
  234. uint8_t * buffer = (uint8_t*) &txAddress;
  235. SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  236. SPI_Transfer_Byte(TX_ADDR_ADDRESS | 0x20);
  237. /* Write the data byte */
  238. for (uint8_t i = 0; i < 4; i ++)
  239. {
  240. SPI_Transfer_Byte(buffer[i]);
  241. }
  242. SPI_Transfer_Byte(0x0);
  243. SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  244. }
  245. void Set_RX_P0_Address(uint32_t rxAddress)
  246. {
  247. uint8_t * buffer = (uint8_t*) &rxAddress;
  248. SPI_Start_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  249. SPI_Transfer_Byte(RX_ADDR_P0_ADDRESS | 0x20);
  250. /* Write the data byte */
  251. for (uint8_t i = 0; i < 4; i ++)
  252. {
  253. SPI_Transfer_Byte(buffer[i]);
  254. }
  255. SPI_Transfer_Byte(0x0);
  256. SPI_Stop_Transmission(&NRF_CSN_PORT, NRF_CSN_PIN);
  257. }
  258. //TODO: only write the used bytes into the address registers & add generic write functions