[CC1101] Added timeout in Rx/Tx blocking mode

This commit is contained in:
jgromes 2021-02-12 20:46:10 +01:00
parent da3015f7b8
commit 4461e9a98e
2 changed files with 42 additions and 4 deletions

View file

@ -96,18 +96,35 @@ int16_t CC1101::begin(float freq, float br, float freqDev, float rxBw, int8_t po
}
int16_t CC1101::transmit(uint8_t* data, size_t len, uint8_t addr) {
// calculate timeout (5ms + 500 % of expected time-on-air)
uint32_t timeout = 5000000 + (uint32_t)((((float)(len * 8)) / (_br * 1000.0)) * 5000000.0);
// start transmission
int16_t state = startTransmit(data, len, addr);
RADIOLIB_ASSERT(state);
// wait for transmission start
// wait for transmission start or timeout
uint32_t start = Module::micros();
while(!Module::digitalRead(_mod->getIrq())) {
Module::yield();
if(Module::micros() - start > timeout) {
standby();
SPIsendCommand(CC1101_CMD_FLUSH_TX);
return(ERR_TX_TIMEOUT);
}
}
// wait for transmission end
// wait for transmission end or timeout
start = Module::micros();
while(Module::digitalRead(_mod->getIrq())) {
Module::yield();
if(Module::micros() - start > timeout) {
standby();
SPIsendCommand(CC1101_CMD_FLUSH_TX);
return(ERR_TX_TIMEOUT);
}
}
// set mode to standby
@ -120,18 +137,35 @@ int16_t CC1101::transmit(uint8_t* data, size_t len, uint8_t addr) {
}
int16_t CC1101::receive(uint8_t* data, size_t len) {
// calculate timeout (500 ms + 400 full max-length packets at current bit rate)
uint32_t timeout = 500000 + (1.0/(_br*1000.0))*(CC1101_MAX_PACKET_LENGTH*400.0);
// start reception
int16_t state = startReceive();
RADIOLIB_ASSERT(state);
// wait for sync word
// wait for sync word or timeout
uint32_t start = Module::micros();
while(!Module::digitalRead(_mod->getIrq())) {
Module::yield();
if(Module::micros() - start > timeout) {
standby();
SPIsendCommand(CC1101_CMD_FLUSH_TX);
return(ERR_RX_TIMEOUT);
}
}
// wait for packet end
// wait for packet end or timeout
start = Module::micros();
while(Module::digitalRead(_mod->getIrq())) {
Module::yield();
if(Module::micros() - start > timeout) {
standby();
SPIsendCommand(CC1101_CMD_FLUSH_TX);
return(ERR_RX_TIMEOUT);
}
}
// read packet data
@ -351,6 +385,9 @@ int16_t CC1101::setBitRate(float br) {
// set bit rate value
int16_t state = SPIsetRegValue(CC1101_REG_MDMCFG4, e, 3, 0);
state |= SPIsetRegValue(CC1101_REG_MDMCFG3, m);
if(state == ERR_NONE) {
CC1101::_br = br;
}
return(state);
}

View file

@ -895,6 +895,7 @@ class CC1101: public PhysicalLayer {
Module* _mod;
float _freq = 0;
float _br = 0;
uint8_t _rawRSSI = 0;
uint8_t _rawLQI = 0;
uint8_t _modulation = CC1101_MOD_FORMAT_2_FSK;