Skip to content

Commit ad6c483

Browse files
tekka007Yveaux
authored andcommitted
RF24 improvements (#1316)
* RF24 improvements * RF24 improvements
1 parent 896220b commit ad6c483

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

hal/transport/RF24/driver/RF24.cpp

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ LOCAL uint8_t RF24_spiMultiByteTransfer(const uint8_t cmd, uint8_t *buf, uint8_t
105105
*current++ = status;
106106
}
107107
} else {
108-
status = RF24_SPI.transfer(*current++);
108+
(void)RF24_SPI.transfer(*current++);
109109
}
110110
}
111111
#endif
@@ -231,9 +231,9 @@ LOCAL uint8_t RF24_getObserveTX(void)
231231
return RF24_readByteRegister(RF24_REG_OBSERVE_TX);
232232
}
233233

234-
LOCAL void RF24_setStatus(const uint8_t status)
234+
LOCAL uint8_t RF24_setStatus(const uint8_t status)
235235
{
236-
RF24_writeByteRegister(RF24_REG_STATUS, status);
236+
return RF24_writeByteRegister(RF24_REG_STATUS, status);
237237
}
238238

239239
LOCAL void RF24_enableFeatures(void)
@@ -307,34 +307,38 @@ LOCAL void RF24_standBy(void)
307307
LOCAL bool RF24_sendMessage(const uint8_t recipient, const void *buf, const uint8_t len,
308308
const bool noACK)
309309
{
310-
uint8_t RF24_status;
311310
RF24_stopListening();
312-
RF24_openWritingPipe( recipient );
313-
RF24_DEBUG(PSTR("RF24:TXM:TO=%" PRIu8 ",LEN=%" PRIu8 "\n"),recipient,len); // send message
311+
RF24_openWritingPipe(recipient);
312+
RF24_DEBUG(PSTR("RF24:TXM:TO=%" PRIu8 ",LEN=%" PRIu8 "\n"), recipient, len); // send message
314313
// flush TX FIFO
315314
RF24_flushTX();
315+
if (noACK) {
316+
// noACK messages are only sent once
317+
RF24_setRetries(RF24_SET_ARD, 0);
318+
}
316319
// this command is affected in clones (e.g. Si24R1): flipped NoACK bit when using W_TX_PAYLOAD_NO_ACK / W_TX_PAYLOAD
317320
// AutoACK is disabled on the broadcasting pipe - NO_ACK prevents resending
318-
RF24_spiMultiByteTransfer((recipient == RF24_BROADCAST_ADDRESS ||
319-
noACK) ? RF24_CMD_WRITE_TX_PAYLOAD_NO_ACK :
320-
RF24_CMD_WRITE_TX_PAYLOAD, (uint8_t *)buf, len, false );
321+
(void)RF24_spiMultiByteTransfer(RF24_CMD_WRITE_TX_PAYLOAD, (uint8_t *)buf, len, false);
321322
// go, TX starts after ~10us, CE high also enables PA+LNA on supported HW
322323
RF24_ce(HIGH);
323324
// timeout counter to detect HW issues
324325
uint16_t timeout = 0xFFFF;
325-
do {
326-
RF24_status = RF24_getStatus();
327-
} while (!(RF24_status & ( _BV(RF24_MAX_RT) | _BV(RF24_TX_DS) )) && timeout--);
326+
while (!(RF24_getStatus() & (_BV(RF24_MAX_RT) | _BV(RF24_TX_DS))) && timeout--) {
327+
doYield();
328+
}
328329
// timeout value after successful TX on 16Mhz AVR ~ 65500, i.e. msg is transmitted after ~36 loop cycles
329330
RF24_ce(LOW);
330331
// reset interrupts
331-
RF24_setStatus(_BV(RF24_TX_DS) | _BV(RF24_MAX_RT) );
332+
const uint8_t RF24_status = RF24_setStatus(_BV(RF24_RX_DR) | _BV(RF24_TX_DS) | _BV(RF24_MAX_RT));
332333
// Max retries exceeded
333-
if(RF24_status & _BV(RF24_MAX_RT)) {
334+
if (RF24_status & _BV(RF24_MAX_RT)) {
334335
// flush packet
335-
RF24_DEBUG(PSTR("!RF24:TXM:MAX_RT\n")); // max retries, no ACK
336+
RF24_DEBUG(PSTR("?RF24:TXM:MAX_RT\n")); // max retries (normal messages) and noACK messages
336337
RF24_flushTX();
337338
}
339+
if (noACK) {
340+
RF24_setRetries(RF24_SET_ARD, RF24_SET_ARC);
341+
}
338342
RF24_startListening();
339343
// true if message sent
340344
return (RF24_status & _BV(RF24_TX_DS) || noACK);
@@ -354,17 +358,23 @@ LOCAL uint8_t RF24_getDynamicPayloadSize(void)
354358

355359
LOCAL bool RF24_isDataAvailable(void)
356360
{
357-
return (!(RF24_getFIFOStatus() & _BV(0)) );
361+
// prevent debug message flooding
362+
#if defined(MY_DEBUG_VERBOSE_RF24)
363+
const uint8_t value = RF24_spiMultiByteTransfer(RF24_CMD_READ_REGISTER | (RF24_REGISTER_MASK &
364+
(RF24_REG_FIFO_STATUS)), NULL, 1, true);
365+
return (bool)(!(value & _BV(RF24_RX_EMPTY)));
366+
#else
367+
return (bool)(!(RF24_getFIFOStatus() & _BV(RF24_RX_EMPTY)) );
368+
#endif
358369
}
359370

360-
361371
LOCAL uint8_t RF24_readMessage(void *buf)
362372
{
363373
const uint8_t len = RF24_getDynamicPayloadSize();
364374
RF24_DEBUG(PSTR("RF24:RXM:LEN=%" PRIu8 "\n"), len); // read message
365-
RF24_spiMultiByteTransfer(RF24_CMD_READ_RX_PAYLOAD,(uint8_t *)buf,len,true);
375+
RF24_spiMultiByteTransfer(RF24_CMD_READ_RX_PAYLOAD, (uint8_t *)buf, len, true);
366376
// clear RX interrupt
367-
RF24_setStatus(_BV(RF24_RX_DR));
377+
(void)RF24_setStatus(_BV(RF24_RX_DR));
368378
return len;
369379
}
370380

hal/transport/RF24/driver/RF24.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ LOCAL uint8_t RF24_getStatus(void);
197197
* @brief RF24_getFIFOStatus
198198
* @return
199199
*/
200-
LOCAL uint8_t RF24_getFIFOStatus(void);
200+
LOCAL uint8_t RF24_getFIFOStatus(void) __attribute__((unused));
201201
/**
202202
* @brief RF24_openWritingPipe
203203
* @param recipient
@@ -344,8 +344,9 @@ LOCAL uint8_t RF24_getObserveTX(void);
344344
/**
345345
* @brief RF24_setStatus
346346
* @param status
347+
* @return status byte before setting new status
347348
*/
348-
LOCAL void RF24_setStatus(const uint8_t status);
349+
LOCAL uint8_t RF24_setStatus(const uint8_t status);
349350
/**
350351
* @brief RF24_enableFeatures
351352
*/

0 commit comments

Comments
 (0)