nrf24.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. // Functions to manage the nRF24L01+ transceiver
  2. #include "nrf24.h"
  3. Nrf24L01::Nrf24L01(NrfSettings_t *settings, SpiManager *spi_manager,
  4. GPIO_TypeDef *port_ce, uint16_t pin_ce){
  5. _spiManager = spi_manager;
  6. _ce_port = port_ce;
  7. _ce_pin = pin_ce;
  8. _settings = settings;
  9. PIN_LOW(_ce_port, _ce_pin);
  10. Delay_ms(100); // power-on delay
  11. uint8_t shot = 0;
  12. _init_state = 0;
  13. do {
  14. _init_state = this->doCheck();
  15. if( _init_state == 1) {
  16. break;
  17. }
  18. shot++;
  19. Delay_ms(3);
  20. }while(shot<5);
  21. // init fail
  22. if (_init_state == 0) {
  23. return;
  24. }
  25. this->init();
  26. this->disableAA(settings->disableShockBurstChannels);
  27. this->setRFChannel(settings->channel);
  28. this->setDataRate(settings->datarate);
  29. this->setCRCScheme(settings->crcScheme);
  30. this->setAddrWidth(settings->addrWidth);
  31. this->setAddr(nRF24_PIPETX, settings->address_tx); // set TX addr
  32. this->setAddr(settings->pipe_A, settings->address_rx_A);
  33. // if(settings->operationalMode == nRF24_MODE_RX) {
  34. this->setRXPipe(settings->pipe_A, nRF24_AA_OFF, settings->payoladLength);
  35. // }
  36. if (settings->pipe_B != nRF24_PIPE_None) {
  37. this->setAddr(settings->pipe_B, settings->address_rx_B);
  38. this->setRXPipe(settings->pipe_B, nRF24_AA_OFF, settings->payoladLength);
  39. }
  40. this->setTXPower(settings->txPower);
  41. this->setOperationalMode(settings->operationalMode);
  42. this->clearIRQFlags();
  43. this->flushRX();
  44. this->flushTX();
  45. this->disable();
  46. this->setPowerMode(nRF24_PWR_UP);
  47. }
  48. /**
  49. * Change operational mode:
  50. * For nRF24L01+ to go from power down mode to TX or RX mode it must first pass through stand-by mode.
  51. * There must be a delay of Tpd2stby (see Table 16.) after the nRF24L01+ leaves power down mode before
  52. * the CEis set high. - Tpd2stby can be up to 5ms per the 1.0 datasheet
  53. */
  54. void Nrf24L01::changeMode(NrfOperationalMode mode) {
  55. this->disable();
  56. this->setPowerMode(nRF24_PWR_DOWN);
  57. // if(mode == nRF24_MODE_RX) { // TODO toto dat prec.
  58. // this->setRXPipe(_settings->pipe_A, nRF24_AA_OFF, _settings->payoladLength);
  59. // if (_settings->pipe_B != nRF24_PIPE_None) {
  60. // this->setAddr(_settings->pipe_B, _settings->address_rx_B);
  61. // this->setRXPipe(_settings->pipe_B, nRF24_AA_OFF, _settings->payoladLength);
  62. // }
  63. // }
  64. this->clearIRQFlags();
  65. this->setOperationalMode(mode);
  66. // this->enable();
  67. this->setPowerMode(nRF24_PWR_UP);
  68. }
  69. uint8_t Nrf24L01::init() {
  70. // Write to registers their initial values
  71. writeReg(nRF24_REG_CONFIG, 0x08);
  72. writeReg(nRF24_REG_EN_AA, 0x3F);
  73. writeReg(nRF24_REG_EN_RXADDR, 0x03);
  74. writeReg(nRF24_REG_SETUP_AW, 0x03);
  75. writeReg(nRF24_REG_SETUP_RETR, 0x03);
  76. writeReg(nRF24_REG_RF_CH, 0x02);
  77. writeReg(nRF24_REG_RF_SETUP, 0x0E);
  78. writeReg(nRF24_REG_STATUS, 0x00);
  79. writeReg(nRF24_REG_RX_PW_P0, 0x00);
  80. writeReg(nRF24_REG_RX_PW_P1, 0x00);
  81. writeReg(nRF24_REG_RX_PW_P2, 0x00);
  82. writeReg(nRF24_REG_RX_PW_P3, 0x00);
  83. writeReg(nRF24_REG_RX_PW_P4, 0x00);
  84. writeReg(nRF24_REG_RX_PW_P5, 0x00);
  85. writeReg(nRF24_REG_DYNPD, 0x00);
  86. writeReg(nRF24_REG_FEATURE, 0x00);
  87. // Clear the FIFO's
  88. this->flushRX();
  89. this->flushTX();
  90. // Clear any pending interrupt flags
  91. this->clearIRQFlags();
  92. return 0;
  93. }
  94. // Read a register
  95. // input:
  96. // reg - number of register to read
  97. // return: value of register
  98. uint8_t Nrf24L01::readReg(uint8_t reg) {
  99. return _spiManager->SPI_ReadReg(reg & nRF24_MASK_REG_MAP);
  100. }
  101. // Write a new value to register
  102. // input:
  103. // reg - number of register to write
  104. // value - value to write
  105. void Nrf24L01::writeReg(uint8_t reg, uint8_t value) {
  106. PIN_LOW(_spiManager->_csn_port, _spiManager->_csn_pin);
  107. if (reg < nRF24_CMD_W_REGISTER) {
  108. // This is a register access
  109. _spiManager->SPI_WritedRegNoCSN((nRF24_CMD_W_REGISTER | (reg & nRF24_MASK_REG_MAP)), value);
  110. } else {
  111. // This is a single byte command or future command/register
  112. _spiManager->SPI_ReadWriteSingle(reg);
  113. if ((reg != nRF24_CMD_FLUSH_TX) && (reg != nRF24_CMD_FLUSH_RX) && \
  114. (reg != nRF24_CMD_REUSE_TX_PL) && (reg != nRF24_CMD_NOP)) {
  115. // Send register value
  116. _spiManager->SPI_ReadWriteSingle(value);
  117. }
  118. }
  119. PIN_HIGH(_spiManager->_csn_port, _spiManager->_csn_pin);
  120. }
  121. // 1 - nRF24L01 is online and responding
  122. // 0 - received sequence differs from original
  123. uint8_t Nrf24L01::check(void) {
  124. return _init_state;
  125. }
  126. // Check if the nRF24L01 present
  127. // return:
  128. // 1 - nRF24L01 is online and responding
  129. // 0 - received sequence differs from original
  130. uint8_t Nrf24L01::doCheck(void) {
  131. uint8_t rxbuf[5];
  132. uint8_t i;
  133. uint8_t *ptr = (uint8_t *)nRF24_TEST_ADDR;
  134. // Write test TX address and read TX_ADDR register
  135. _spiManager->SPI_WriteRegMulti(nRF24_CMD_W_REGISTER | nRF24_REG_TX_ADDR, ptr, 5);
  136. _spiManager->SPI_ReadRegMulti(nRF24_CMD_R_REGISTER | nRF24_REG_TX_ADDR, rxbuf, nRF24_CMD_NOP, 5);
  137. // Compare buffers, return error on first mismatch
  138. for (i = 0; i < 5; i++) {
  139. if (rxbuf[i] != *ptr++) return 0;
  140. }
  141. return 1;
  142. }
  143. // Control transceiver power mode
  144. // input:
  145. // mode - new state of power mode, one of nRF24_PWR_xx values
  146. void Nrf24L01::setPowerMode(NrfPowerMode mode) {
  147. uint8_t reg;
  148. reg = readReg(nRF24_REG_CONFIG);
  149. if (mode == nRF24_PWR_UP) {
  150. // Set the PWR_UP bit of CONFIG register to wake the transceiver
  151. // It goes into Stanby-I mode with consumption about 26uA
  152. reg |= nRF24_CONFIG_PWR_UP;
  153. } else {
  154. // Clear the PWR_UP bit of CONFIG register to put the transceiver
  155. // into power down mode with consumption about 900nA
  156. reg &= ~nRF24_CONFIG_PWR_UP;
  157. }
  158. writeReg(nRF24_REG_CONFIG, reg);
  159. Delay_ms(2);
  160. }
  161. // Set transceiver operational mode
  162. // input:
  163. // mode - operational mode, one of nRF24_MODE_xx values
  164. void Nrf24L01::setOperationalMode(NrfOperationalMode mode) {
  165. uint8_t reg;
  166. // Configure PRIM_RX bit of the CONFIG register
  167. reg = readReg(nRF24_REG_CONFIG);
  168. reg &= ~nRF24_CONFIG_PRIM_RX;
  169. reg |= (mode & nRF24_CONFIG_PRIM_RX);
  170. writeReg(nRF24_REG_CONFIG, reg);
  171. }
  172. // Set transceiver DynamicPayloadLength feature for all the pipes
  173. // input:
  174. // mode - status, one of nRF24_DPL_ON/nRF24_DPL_OFF values
  175. void Nrf24L01::setDynamicPayloadLength(NrfDPLStatus mode) {
  176. uint8_t reg;
  177. reg = readReg(nRF24_REG_FEATURE);
  178. if(mode) {
  179. writeReg(nRF24_REG_FEATURE, reg | nRF24_FEATURE_EN_DPL);
  180. writeReg(nRF24_REG_DYNPD, 0x1F);
  181. } else {
  182. writeReg(nRF24_REG_FEATURE, reg &~ nRF24_FEATURE_EN_DPL);
  183. writeReg(nRF24_REG_DYNPD, 0x0);
  184. }
  185. }
  186. // Enables Payload With Ack. NB Refer to the datasheet for proper retransmit timing.
  187. // input:
  188. // mode - status, 1 or 0
  189. void Nrf24L01::setPayloadWithAck(uint8_t mode) {
  190. uint8_t reg;
  191. reg = readReg(nRF24_REG_FEATURE);
  192. if(mode) {
  193. writeReg(nRF24_REG_FEATURE, reg | nRF24_FEATURE_EN_ACK_PAY);
  194. } else {
  195. writeReg(nRF24_REG_FEATURE, reg &~ nRF24_FEATURE_EN_ACK_PAY);
  196. }
  197. }
  198. // Configure transceiver CRC scheme
  199. // input:
  200. // scheme - CRC scheme, one of nRF24_CRC_xx values
  201. // note: transceiver will forcibly turn on the CRC in case if auto acknowledgment
  202. // enabled for at least one RX pipe
  203. void Nrf24L01::setCRCScheme(NrfCrcScheme scheme) {
  204. uint8_t reg;
  205. // Configure EN_CRC[3] and CRCO[2] bits of the CONFIG register
  206. reg = readReg(nRF24_REG_CONFIG);
  207. reg &= ~nRF24_MASK_CRC;
  208. reg |= (scheme & nRF24_MASK_CRC);
  209. writeReg(nRF24_REG_CONFIG, reg);
  210. }
  211. // Set frequency channel
  212. // input:
  213. // channel - radio frequency channel, value from 0 to 127
  214. // note: frequency will be (2400 + channel)MHz
  215. // note: PLOS_CNT[7:4] bits of the OBSERVER_TX register will be reset
  216. void Nrf24L01::setRFChannel(uint8_t channel) {
  217. writeReg(nRF24_REG_RF_CH, channel);
  218. }
  219. // Set automatic retransmission parameters
  220. // input:
  221. // ard - auto retransmit delay, one of nRF24_ARD_xx values
  222. // arc - count of auto retransmits, value form 0 to 15
  223. // note: zero arc value means that the automatic retransmission disabled
  224. void Nrf24L01::nRF24_SetAutoRetr(uint8_t ard, uint8_t arc) {
  225. // Set auto retransmit settings (SETUP_RETR register)
  226. writeReg(nRF24_REG_SETUP_RETR, (uint8_t)((ard << 4) | (arc & nRF24_MASK_RETR_ARC)));
  227. }
  228. // Set of address widths
  229. // input:
  230. // addr_width - RX/TX address field width, value from 3 to 5
  231. // note: this setting is common for all pipes
  232. void Nrf24L01::setAddrWidth(uint8_t addr_width) {
  233. writeReg(nRF24_REG_SETUP_AW, addr_width - 2);
  234. }
  235. // Set static RX address for a specified pipe
  236. // input:
  237. // pipe - pipe to configure address, one of nRF24_PIPEx values
  238. // addr - pointer to the buffer with address
  239. // note: pipe can be a number from 0 to 5 (RX pipes) and 6 (TX pipe)
  240. // note: buffer length must be equal to current address width of transceiver
  241. // note: for pipes[2..5] only first byte of address will be written because
  242. // other bytes of address equals to pipe1
  243. // note: for pipes[2..5] only first byte of address will be written because
  244. // pipes 1-5 share the four most significant address bytes
  245. void Nrf24L01::setAddr(uint8_t pipe, const uint8_t *addr) {
  246. uint8_t addr_width;
  247. // RX_ADDR_Px register
  248. switch (pipe) {
  249. case nRF24_PIPETX:
  250. case nRF24_PIPE0:
  251. case nRF24_PIPE1:
  252. // Get address width
  253. addr_width = readReg(nRF24_REG_SETUP_AW) + 1;
  254. // Write address in reverse order (LSByte first)
  255. addr += addr_width;
  256. PIN_LOW(_spiManager->_csn_port, _spiManager->_csn_pin);
  257. _spiManager->SPI_ReadWriteSingle(nRF24_CMD_W_REGISTER | nRF24_ADDR_REGS[pipe]);
  258. do {
  259. _spiManager->SPI_ReadWriteSingle(*addr--);
  260. } while (addr_width--);
  261. PIN_HIGH(_spiManager->_csn_port, _spiManager->_csn_pin);
  262. break;
  263. case nRF24_PIPE2:
  264. case nRF24_PIPE3:
  265. case nRF24_PIPE4:
  266. case nRF24_PIPE5:
  267. // Write address LSBbyte (only first byte from the addr buffer)
  268. writeReg(nRF24_ADDR_REGS[pipe], *addr);
  269. break;
  270. default:
  271. // Incorrect pipe number -> do nothing
  272. break;
  273. }
  274. }
  275. // Configure RF output power in TX mode
  276. // input:
  277. // tx_pwr - RF output power, one of nRF24_TXPWR_xx values
  278. void Nrf24L01::setTXPower(NrfTxPower tx_pwr) {
  279. uint8_t reg;
  280. // Configure RF_PWR[2:1] bits of the RF_SETUP register
  281. reg = readReg(nRF24_REG_RF_SETUP);
  282. reg &= ~nRF24_MASK_RF_PWR;
  283. reg |= tx_pwr;
  284. writeReg(nRF24_REG_RF_SETUP, reg);
  285. }
  286. // Configure transceiver data rate
  287. // input:
  288. // data_rate - data rate, one of nRF24_DR_xx values
  289. void Nrf24L01::setDataRate(NrfDataRate data_rate) {
  290. uint8_t reg;
  291. // Configure RF_DR_LOW[5] and RF_DR_HIGH[3] bits of the RF_SETUP register
  292. reg = readReg(nRF24_REG_RF_SETUP);
  293. reg &= ~nRF24_MASK_DATARATE;
  294. reg |= data_rate;
  295. writeReg(nRF24_REG_RF_SETUP, reg);
  296. }
  297. // Configure a specified RX pipe
  298. // input:
  299. // pipe - number of the RX pipe, value from 0 to 5
  300. // aa_state - state of auto acknowledgment, one of nRF24_AA_xx values
  301. // payload_len - payload length in bytes
  302. void Nrf24L01::setRXPipe(NrfPipe pipe, NrfAcknowledgementState aa_state, uint8_t payload_len) {
  303. uint8_t reg;
  304. // Enable the specified pipe (EN_RXADDR register)
  305. reg = (readReg(nRF24_REG_EN_RXADDR) | (1 << pipe)) & nRF24_MASK_EN_RX;
  306. writeReg(nRF24_REG_EN_RXADDR, reg);
  307. // Set RX payload length (RX_PW_Px register)
  308. writeReg(nRF24_RX_PW_PIPE[pipe], payload_len & nRF24_MASK_RX_PW);
  309. // Set auto acknowledgment for a specified pipe (EN_AA register)
  310. reg = readReg(nRF24_REG_EN_AA);
  311. if (aa_state == nRF24_AA_ON) {
  312. reg |= (1 << pipe);
  313. } else {
  314. reg &= ~(1 << pipe);
  315. }
  316. writeReg(nRF24_REG_EN_AA, reg);
  317. }
  318. // Disable specified RX pipe
  319. // input:
  320. // PIPE - number of RX pipe, value from 0 to 5
  321. void Nrf24L01::closePipe(uint8_t pipe) {
  322. uint8_t reg;
  323. reg = readReg(nRF24_REG_EN_RXADDR);
  324. reg &= ~(1 << pipe);
  325. reg &= nRF24_MASK_EN_RX;
  326. writeReg(nRF24_REG_EN_RXADDR, reg);
  327. }
  328. // Enable the auto retransmit (a.k.a. enhanced ShockBurst) for the specified RX pipe
  329. // input:
  330. // pipe - number of the RX pipe, value from 0 to 5
  331. void Nrf24L01::enableAA(uint8_t pipe) {
  332. uint8_t reg;
  333. // Set bit in EN_AA register
  334. reg = readReg(nRF24_REG_EN_AA);
  335. reg |= (1 << pipe);
  336. writeReg(nRF24_REG_EN_AA, reg);
  337. }
  338. // Disable the auto retransmit (a.k.a. enhanced ShockBurst) for one or all RX pipes
  339. // input:
  340. // pipe - number of the RX pipe, value from 0 to 5, any other value will disable AA for all RX pipes
  341. void Nrf24L01::disableAA(uint8_t pipe) {
  342. uint8_t reg;
  343. if (pipe > 5) {
  344. // Disable Auto-ACK for ALL pipes
  345. writeReg(nRF24_REG_EN_AA, 0x00);
  346. } else {
  347. // Clear bit in the EN_AA register
  348. reg = readReg(nRF24_REG_EN_AA);
  349. reg &= ~(1 << pipe);
  350. writeReg(nRF24_REG_EN_AA, reg);
  351. }
  352. }
  353. // Get value of the STATUS register
  354. // return: value of STATUS register
  355. uint8_t Nrf24L01::getStatus(void) {
  356. return readReg(nRF24_REG_STATUS);
  357. }
  358. // Get pending IRQ flags
  359. // return: current status of RX_DR, TX_DS and MAX_RT bits of the STATUS register
  360. uint8_t Nrf24L01::getIRQFlags(void) {
  361. return (readReg(nRF24_REG_STATUS) & nRF24_MASK_STATUS_IRQ);
  362. }
  363. // Get status of the RX FIFO
  364. // return: one of the nRF24_STATUS_RXFIFO_xx values
  365. uint8_t Nrf24L01::getStatus_RXFIFO(void) {
  366. return (readReg(nRF24_REG_FIFO_STATUS) & nRF24_MASK_RXFIFO);
  367. }
  368. // Get status of the TX FIFO
  369. // return: one of the nRF24_STATUS_TXFIFO_xx values
  370. // note: the TX_REUSE bit ignored
  371. uint8_t Nrf24L01::getStatus_TXFIFO(void) {
  372. return ((readReg(nRF24_REG_FIFO_STATUS) & nRF24_MASK_TXFIFO) >> 4);
  373. }
  374. // Get pipe number for the payload available for reading from RX FIFO
  375. // return: pipe number or 0x07 if the RX FIFO is empty
  376. uint8_t Nrf24L01::getRXSource(void) {
  377. return ((readReg(nRF24_REG_STATUS) & nRF24_MASK_RX_P_NO) >> 1);
  378. }
  379. // Get auto retransmit statistic
  380. // return: value of OBSERVE_TX register which contains two counters encoded in nibbles:
  381. // high - lost packets count (max value 15, can be reseted by write to RF_CH register)
  382. // low - retransmitted packets count (max value 15, reseted when new transmission starts)
  383. uint8_t Nrf24L01::getRetransmitCounters(void) {
  384. return (readReg(nRF24_REG_OBSERVE_TX));
  385. }
  386. // Reset packet lost counter (PLOS_CNT bits in OBSERVER_TX register)
  387. void Nrf24L01::resetPLOS(void) {
  388. uint8_t reg;
  389. // The PLOS counter is reset after write to RF_CH register
  390. reg = readReg(nRF24_REG_RF_CH);
  391. writeReg(nRF24_REG_RF_CH, reg);
  392. }
  393. // Flush the TX FIFO
  394. void Nrf24L01::flushTX(void) {
  395. writeReg(nRF24_CMD_FLUSH_TX, nRF24_CMD_NOP);
  396. }
  397. // Flush the RX FIFO
  398. void Nrf24L01::flushRX(void) {
  399. writeReg(nRF24_CMD_FLUSH_RX, nRF24_CMD_NOP);
  400. }
  401. // Clear any pending IRQ flags
  402. void Nrf24L01::clearIRQFlags(void) {
  403. uint8_t reg;
  404. // Clear RX_DR, TX_DS and MAX_RT bits of the STATUS register
  405. reg = readReg(nRF24_REG_STATUS);
  406. reg |= nRF24_MASK_STATUS_IRQ;
  407. writeReg(nRF24_REG_STATUS, reg);
  408. }
  409. // Write TX payload
  410. // input:
  411. // pBuf - pointer to the buffer with payload data
  412. // length - payload length in bytes
  413. void Nrf24L01::writePayload(uint8_t *pBuf, uint8_t length) {
  414. _spiManager->SPI_WriteRegMulti(nRF24_CMD_W_TX_PAYLOAD, pBuf, length);
  415. }
  416. uint8_t Nrf24L01::getRxDplPayloadWidth() {
  417. return _spiManager->SPI_ReadReg(nRF24_CMD_R_RX_PL_WID);
  418. }
  419. nRF24_RXResult Nrf24L01::readPayloadGeneric(uint8_t *pBuf, uint8_t *length, uint8_t dynamicPayloadLength) {
  420. uint8_t pipe;
  421. // Extract a payload pipe number from the STATUS register
  422. pipe = (readReg(nRF24_REG_STATUS) & nRF24_MASK_RX_P_NO) >> 1;
  423. // RX FIFO empty?
  424. if (pipe < 6) {
  425. // Get payload length
  426. if(dynamicPayloadLength) {
  427. *length = getRxDplPayloadWidth();
  428. if(*length>32) { //broken packet
  429. *length = 0;
  430. this->flushRX();
  431. }
  432. } else {
  433. *length = readReg(nRF24_RX_PW_PIPE[pipe]);
  434. }
  435. // Read a payload from the RX FIFO
  436. if (*length) {
  437. _spiManager->SPI_ReadRegMulti(nRF24_CMD_R_RX_PAYLOAD, pBuf, nRF24_CMD_NOP, *length);
  438. }
  439. return ((nRF24_RXResult)pipe);
  440. }
  441. // The RX FIFO is empty
  442. *length = 0;
  443. return nRF24_RX_EMPTY;
  444. }
  445. // Read top level payload available in the RX FIFO
  446. // input:
  447. // pBuf - pointer to the buffer to store a payload data
  448. // length - pointer to variable to store a payload length
  449. // return: one of nRF24_RX_xx values
  450. // nRF24_RX_PIPEX - packet has been received from the pipe number X
  451. // nRF24_RX_EMPTY - the RX FIFO is empty
  452. nRF24_RXResult Nrf24L01::receivePayload(uint8_t *pBuf, uint8_t *length) {
  453. return readPayloadGeneric(pBuf, length, 0);
  454. }
  455. nRF24_RXResult Nrf24L01::receivePayloadDpl(uint8_t *pBuf, uint8_t *length) {
  456. return readPayloadGeneric(pBuf, length, 1);
  457. }
  458. uint8_t Nrf24L01::getFeatures() {
  459. return readReg(nRF24_REG_FEATURE);
  460. }
  461. void Nrf24L01::activateFeatures() {
  462. _spiManager->SPI_WritedReg(nRF24_CMD_ACTIVATE, 0x73);
  463. }
  464. void Nrf24L01::transmitPayloadAck(nRF24_RXResult pipe, char *payload, uint8_t length) {
  465. PIN_LOW(_spiManager->_csn_port, _spiManager->_csn_pin); // TODO prehodit do spiManagera
  466. _spiManager->SPI_ReadWriteSingle(nRF24_CMD_W_ACK_PAYLOAD | pipe);
  467. while (length--) {
  468. _spiManager->SPI_ReadWriteSingle((uint8_t) *payload++);
  469. }
  470. PIN_HIGH(_spiManager->_csn_port, _spiManager->_csn_pin);
  471. }
  472. nRF24_TXResult Nrf24L01::transmitPayload(uint8_t *pBuf, uint8_t length) {
  473. volatile uint32_t wait = nRF24_WAIT_TIMEOUT;
  474. uint8_t status;
  475. // Deassert the CE pin (in case if it still high)
  476. PIN_LOW(_ce_port, _ce_pin);
  477. // Transfer a data from the specified buffer to the TX FIFO
  478. writePayload(pBuf, length);
  479. // Start a transmission by asserting CE pin (must be held at least 10us)
  480. PIN_HIGH(_ce_port, _ce_pin);
  481. do {
  482. if(_spiManager->hasIrqCallback()) {
  483. status = _spiManager->irqCallback();
  484. } else {
  485. // Poll the transceiver status register until one of the following flags will be set:
  486. // TX_DS - means the packet has been transmitted
  487. // MAX_RT - means the maximum number of TX retransmits happened
  488. status = getStatus(); // SW pooling
  489. }
  490. if (status & (nRF24_FLAG_TX_DS | nRF24_FLAG_MAX_RT)) {
  491. // It will not work, when hasIrqClearCallback is not defined
  492. if(_spiManager->hasIrqClearCallback()) {
  493. _spiManager->irqCallbackClear();
  494. }
  495. break;
  496. }
  497. } while (wait--);
  498. // Deassert the CE pin (Standby-II --> Standby-I)
  499. PIN_LOW(_ce_port, _ce_pin);
  500. if (!wait) {
  501. // Timeout
  502. return nRF24_TX_TIMEOUT;
  503. }
  504. // Clear pending IRQ flags
  505. clearIRQFlags();
  506. if (status & nRF24_FLAG_MAX_RT) {
  507. // Auto retransmit counter exceeds the programmed maximum limit (FIFO is not removed)
  508. return nRF24_TX_MAXRT;
  509. }
  510. if (status & nRF24_FLAG_TX_DS) {
  511. // Successful transmission
  512. return nRF24_TX_SUCCESS;
  513. }
  514. // Some banana happens, a payload remains in the TX FIFO, flush it
  515. flushTX();
  516. return nRF24_TX_ERROR;
  517. }
  518. void Nrf24L01::enable(void)
  519. {
  520. PIN_HIGH(_ce_port, _ce_pin);
  521. }
  522. void Nrf24L01::disable(void)
  523. {
  524. PIN_LOW(_ce_port, _ce_pin);
  525. }