NRF24.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // NRF24.cpp
  2. //
  3. // Copyright (C) 2012 Mike McCauley
  4. // $Id: NRF24.cpp,v 1.2 2014/05/20 06:00:55 mikem Exp mikem $
  5. #include <NRF24.h>
  6. #include "SoftSpi.h"
  7. NRF24::NRF24(uint8_t chipEnablePin, uint8_t chipSelectPin)
  8. {
  9. _configuration = NRF24_EN_CRC; // Default: 1 byte CRC enabled
  10. _chipEnablePin = chipEnablePin;
  11. _chipSelectPin = chipSelectPin;
  12. }
  13. boolean NRF24::init()
  14. {
  15. // Initialise the slave select pin
  16. pinMode(_chipEnablePin, OUTPUT);
  17. digitalWrite(_chipEnablePin, LOW);
  18. pinMode(_chipSelectPin, OUTPUT);
  19. digitalWrite(_chipSelectPin, HIGH);
  20. // Added code to initilize the SPI interface and wait 100 ms
  21. // to allow NRF24 device to "settle". 100 ms may be overkill.
  22. pinMode(SCK, OUTPUT);
  23. pinMode(MOSI, OUTPUT);
  24. // Wait for NRF24 POR (up to 100msec)
  25. delay(100);
  26. // start the SPI library:
  27. // Note the NRF24 wants mode 0, MSB first and default to 1 Mbps
  28. SPI.begin();
  29. SPI.setDataMode(SPI_MODE0);
  30. SPI.setBitOrder(MSBFIRST);
  31. // SPI.setClockDivider(SPI_2XCLOCK_MASK); // 1 MHz SPI clock
  32. SPI.setClockDivider(SPI_CLOCK_DIV2); // 8MHz SPI clock
  33. // Clear interrupts
  34. if (!spiWriteRegister(NRF24_REG_07_STATUS, NRF24_RX_DR | NRF24_TX_DS | NRF24_MAX_RT))
  35. return false; // Could not write to device. Not connected?
  36. // Make sure we are powered down
  37. powerDown();
  38. // Flush FIFOs
  39. flushTx();
  40. flushRx();
  41. return powerUpRx();
  42. }
  43. // Low level commands for interfacing with the device
  44. uint8_t NRF24::spiCommand(uint8_t command)
  45. {
  46. digitalWrite(_chipSelectPin, LOW);
  47. uint8_t status = SPI.transfer(command);
  48. digitalWrite(_chipSelectPin, HIGH);
  49. return status;
  50. }
  51. // Read and write commands
  52. uint8_t NRF24::spiRead(uint8_t command)
  53. {
  54. digitalWrite(_chipSelectPin, LOW);
  55. SPI.transfer(command); // Send the address, discard status
  56. uint8_t val = SPI.transfer(0); // The MOSI value is ignored, value is read
  57. digitalWrite(_chipSelectPin, HIGH);
  58. return val;
  59. }
  60. uint8_t NRF24::spiWrite(uint8_t command, uint8_t val)
  61. {
  62. digitalWrite(_chipSelectPin, LOW);
  63. uint8_t status = SPI.transfer(command);
  64. SPI.transfer(val); // New register value follows
  65. digitalWrite(_chipSelectPin, HIGH);
  66. return status;
  67. }
  68. void NRF24::spiBurstRead(uint8_t command, uint8_t* dest, uint8_t len)
  69. {
  70. digitalWrite(_chipSelectPin, LOW);
  71. SPI.transfer(command); // Send the start address, discard status
  72. while (len--)
  73. *dest++ = SPI.transfer(0); // The MOSI value is ignored, value is read
  74. digitalWrite(_chipSelectPin, HIGH);
  75. // 300 microsecs for 32 octet payload
  76. }
  77. uint8_t NRF24::spiBurstWrite(uint8_t command, uint8_t* src, uint8_t len)
  78. {
  79. digitalWrite(_chipSelectPin, LOW);
  80. uint8_t status = SPI.transfer(command);
  81. while (len--)
  82. SPI.transfer(*src++);
  83. digitalWrite(_chipSelectPin, HIGH);
  84. return status;
  85. }
  86. // Use the register commands to read and write the registers
  87. uint8_t NRF24::spiReadRegister(uint8_t reg)
  88. {
  89. return spiRead((reg & NRF24_REGISTER_MASK) | NRF24_COMMAND_R_REGISTER);
  90. }
  91. uint8_t NRF24::spiWriteRegister(uint8_t reg, uint8_t val)
  92. {
  93. return spiWrite((reg & NRF24_REGISTER_MASK) | NRF24_COMMAND_W_REGISTER, val);
  94. }
  95. void NRF24::spiBurstReadRegister(uint8_t reg, uint8_t* dest, uint8_t len)
  96. {
  97. return spiBurstRead((reg & NRF24_REGISTER_MASK) | NRF24_COMMAND_R_REGISTER, dest, len);
  98. }
  99. uint8_t NRF24::spiBurstWriteRegister(uint8_t reg, uint8_t* src, uint8_t len)
  100. {
  101. return spiBurstWrite((reg & NRF24_REGISTER_MASK) | NRF24_COMMAND_W_REGISTER, src, len);
  102. }
  103. uint8_t NRF24::statusRead()
  104. {
  105. return spiReadRegister(NRF24_REG_07_STATUS);
  106. // return spiCommand(NRF24_COMMAND_NOP); // Side effect is to read status
  107. }
  108. uint8_t NRF24::flushTx()
  109. {
  110. return spiCommand(NRF24_COMMAND_FLUSH_TX);
  111. }
  112. uint8_t NRF24::flushRx()
  113. {
  114. return spiCommand(NRF24_COMMAND_FLUSH_RX);
  115. }
  116. boolean NRF24::setChannel(uint8_t channel)
  117. {
  118. spiWriteRegister(NRF24_REG_05_RF_CH, channel & NRF24_RF_CH);
  119. return true;
  120. }
  121. boolean NRF24::setConfiguration(uint8_t configuration)
  122. {
  123. _configuration = configuration;
  124. return true;
  125. }
  126. boolean NRF24::setPipeAddress(uint8_t pipe, uint8_t* address, uint8_t len)
  127. {
  128. spiBurstWriteRegister(NRF24_REG_0A_RX_ADDR_P0 + pipe, address, len);
  129. return true;
  130. }
  131. boolean NRF24::setRetry(uint8_t delay, uint8_t count)
  132. {
  133. spiWriteRegister(NRF24_REG_04_SETUP_RETR, ((delay << 4) & NRF24_ARD) | (count & NRF24_ARC));
  134. return true;
  135. }
  136. boolean NRF24::setThisAddress(uint8_t* address, uint8_t len)
  137. {
  138. // Set pipe 1 for this address
  139. setPipeAddress(1, address, len);
  140. // RX_ADDR_P2 is set to RX_ADDR_P1 with the LSbyte set to 0xff, for use as a broadcast address
  141. return true;
  142. }
  143. boolean NRF24::setTransmitAddress(uint8_t* address, uint8_t len)
  144. {
  145. // Set both TX_ADDR and RX_ADDR_P0 for auto-ack with Enhanced shockwave
  146. spiBurstWriteRegister(NRF24_REG_0A_RX_ADDR_P0, address, len);
  147. spiBurstWriteRegister(NRF24_REG_10_TX_ADDR, address, len);
  148. return true;
  149. }
  150. boolean NRF24::setPayloadSize(uint8_t size)
  151. {
  152. spiWriteRegister(NRF24_REG_11_RX_PW_P0, size);
  153. spiWriteRegister(NRF24_REG_12_RX_PW_P1, size);
  154. return true;
  155. }
  156. boolean NRF24::setRF(uint8_t data_rate, uint8_t power)
  157. {
  158. uint8_t value = (power << 1) & NRF24_PWR;
  159. // Ugly mapping of data rates to noncontiguous 2 bits:
  160. if (data_rate == NRF24DataRate250kbps)
  161. value |= NRF24_RF_DR_LOW;
  162. else if (data_rate == NRF24DataRate2Mbps)
  163. value |= NRF24_RF_DR_HIGH;
  164. // else NRF24DataRate1Mbps, 00
  165. spiWriteRegister(NRF24_REG_06_RF_SETUP, value);
  166. if (data_rate == NRF24DataRate250kbps)
  167. spiWriteRegister(NRF24_REG_04_SETUP_RETR, 0x43); // 1250usecs, 3 retries
  168. else
  169. spiWriteRegister(NRF24_REG_04_SETUP_RETR, 0x03); // 250us, 3 retries
  170. return true;
  171. }
  172. boolean NRF24::powerDown()
  173. {
  174. spiWriteRegister(NRF24_REG_00_CONFIG, _configuration);
  175. digitalWrite(_chipEnablePin, LOW);
  176. return true;
  177. }
  178. boolean NRF24::powerUpRx()
  179. {
  180. boolean status = spiWriteRegister(NRF24_REG_00_CONFIG, _configuration | NRF24_PWR_UP | NRF24_PRIM_RX);
  181. digitalWrite(_chipEnablePin, HIGH);
  182. return status;
  183. }
  184. boolean NRF24::powerUpTx()
  185. {
  186. // Its the pulse high that puts us into TX mode
  187. digitalWrite(_chipEnablePin, LOW);
  188. boolean status = spiWriteRegister(NRF24_REG_00_CONFIG, _configuration | NRF24_PWR_UP);
  189. digitalWrite(_chipEnablePin, HIGH);
  190. return status;
  191. }
  192. boolean NRF24::send(uint8_t* data, uint8_t len, boolean noack)
  193. {
  194. powerUpTx();
  195. spiBurstWrite(noack ? NRF24_COMMAND_W_TX_PAYLOAD_NOACK : NRF24_COMMAND_W_TX_PAYLOAD, data, len);
  196. // Radio will return to Standby II mode after transmission is complete
  197. return true;
  198. }
  199. boolean NRF24::waitPacketSent()
  200. {
  201. // If we are currently in receive mode, then there is no packet to wait for
  202. if (spiReadRegister(NRF24_REG_00_CONFIG) & NRF24_PRIM_RX)
  203. return false;
  204. // Wait for either the Data Sent or Max ReTries flag, signalling the
  205. // end of transmission
  206. uint8_t status;
  207. while (!((status = statusRead()) & (NRF24_TX_DS | NRF24_MAX_RT)))
  208. ;
  209. // Must clear NRF24_MAX_RT if it is set, else no further comm
  210. spiWriteRegister(NRF24_REG_07_STATUS, NRF24_TX_DS | NRF24_MAX_RT);
  211. if (status & NRF24_MAX_RT)
  212. flushTx();
  213. // Return true if data sent, false if MAX_RT
  214. return status & NRF24_TX_DS;
  215. }
  216. boolean NRF24::isSending()
  217. {
  218. return !(spiReadRegister(NRF24_REG_00_CONFIG) & NRF24_PRIM_RX) && !(statusRead() & (NRF24_TX_DS | NRF24_MAX_RT));
  219. }
  220. boolean NRF24::printRegisters()
  221. {
  222. /*uint8_t registers[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0d, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x1c, 0x1d};
  223. uint8_t i;
  224. for (i = 0; i < sizeof(registers); i++)
  225. {
  226. Serial.print(i, HEX);
  227. Serial.print(": ");
  228. Serial.println(spiReadRegister(i), HEX);
  229. }
  230. */
  231. return true;
  232. }
  233. boolean NRF24::available()
  234. {
  235. if (spiReadRegister(NRF24_REG_17_FIFO_STATUS) & NRF24_RX_EMPTY)
  236. return false;
  237. // Manual says that messages > 32 octets should be discarded
  238. if (spiRead(NRF24_COMMAND_R_RX_PL_WID) > 32)
  239. {
  240. flushRx();
  241. return false;
  242. }
  243. return true;
  244. }
  245. void NRF24::waitAvailable()
  246. {
  247. powerUpRx();
  248. while (!available())
  249. ;
  250. }
  251. // Blocks until a valid message is received or timeout expires
  252. // Return true if there is a message available
  253. // Works correctly even on millis() rollover
  254. bool NRF24::waitAvailableTimeout(uint16_t timeout)
  255. {
  256. powerUpRx();
  257. unsigned long starttime = millis();
  258. while ((millis() - starttime) < timeout)
  259. if (available())
  260. return true;
  261. return false;
  262. }
  263. boolean NRF24::recv(uint8_t* buf, uint8_t* len)
  264. {
  265. // Clear read interrupt
  266. spiWriteRegister(NRF24_REG_07_STATUS, NRF24_RX_DR);
  267. // 0 microsecs @ 8MHz SPI clock
  268. if (!available())
  269. return false;
  270. // 32 microsecs (if immediately available)
  271. *len = spiRead(NRF24_COMMAND_R_RX_PL_WID);
  272. // 44 microsecs
  273. spiBurstRead(NRF24_COMMAND_R_RX_PAYLOAD, buf, *len);
  274. // 140 microsecs (32 octet payload)
  275. return true;
  276. }