[CC1101] General reformatting

This commit is contained in:
jgromes 2023-04-23 09:47:42 +02:00
parent 78a576df12
commit 5c6628b6eb
2 changed files with 667 additions and 702 deletions

View file

@ -3,19 +3,19 @@
#if !defined(RADIOLIB_EXCLUDE_CC1101)
CC1101::CC1101(Module* module) : PhysicalLayer(RADIOLIB_CC1101_FREQUENCY_STEP_SIZE, RADIOLIB_CC1101_MAX_PACKET_LENGTH) {
_mod = module;
this->mod = module;
}
Module* CC1101::getMod() {
return(_mod);
return(this->mod);
}
int16_t CC1101::begin(float freq, float br, float freqDev, float rxBw, int8_t power, uint8_t preambleLength) {
// set module properties
_mod->SPIreadCommand = RADIOLIB_CC1101_CMD_READ;
_mod->SPIwriteCommand = RADIOLIB_CC1101_CMD_WRITE;
_mod->init();
_mod->hal->pinMode(_mod->getIrq(), _mod->hal->GpioModeInput);
this->mod->SPIreadCommand = RADIOLIB_CC1101_CMD_READ;
this->mod->SPIwriteCommand = RADIOLIB_CC1101_CMD_WRITE;
this->mod->init();
this->mod->hal->pinMode(this->mod->getIrq(), this->mod->hal->GpioModeInput);
// try to find the CC1101 chip
uint8_t i = 0;
@ -26,14 +26,14 @@ int16_t CC1101::begin(float freq, float br, float freqDev, float rxBw, int8_t po
flagFound = true;
} else {
RADIOLIB_DEBUG_PRINTLN("CC1101 not found! (%d of 10 tries) RADIOLIB_CC1101_REG_VERSION == 0x%04X, expected 0x0004/0x0014", i + 1, version);
_mod->hal->delay(10);
this->mod->hal->delay(10);
i++;
}
}
if(!flagFound) {
RADIOLIB_DEBUG_PRINTLN("No CC1101 found!");
_mod->term();
this->mod->term();
return(RADIOLIB_ERR_CHIP_NOT_FOUND);
} else {
RADIOLIB_DEBUG_PRINTLN("M\tCC1101");
@ -68,7 +68,7 @@ int16_t CC1101::begin(float freq, float br, float freqDev, float rxBw, int8_t po
RADIOLIB_ASSERT(state);
// configure default preamble length
state = setPreambleLength(preambleLength);
state = setPreambleLength(preambleLength, preambleLength - 4);
RADIOLIB_ASSERT(state);
// set default data shaping
@ -85,37 +85,48 @@ int16_t CC1101::begin(float freq, float br, float freqDev, float rxBw, int8_t po
RADIOLIB_ASSERT(state);
// flush FIFOs
SPIsendCommand(RADIOLIB_CC1101_CMD_FLUSH_RX);
SPIsendCommand(RADIOLIB_CC1101_CMD_FLUSH_RX | RADIOLIB_CC1101_CMD_READ);
SPIsendCommand(RADIOLIB_CC1101_CMD_FLUSH_TX);
return(state);
}
void CC1101::reset() {
// this is the manual power-on-reset sequence
this->mod->hal->digitalWrite(this->mod->getCs(), LOW);
this->mod->hal->delayMicroseconds(5);
this->mod->hal->digitalWrite(this->mod->getCs(), HIGH);
this->mod->hal->delayMicroseconds(40);
this->mod->hal->digitalWrite(this->mod->getCs(), LOW);
this->mod->hal->delay(10);
SPIsendCommand(RADIOLIB_CC1101_CMD_RESET);
}
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);
uint32_t timeout = 5000000 + (uint32_t)((((float)(len * 8)) / (this->bitRate * 1000.0)) * 5000000.0);
// start transmission
int16_t state = startTransmit(data, len, addr);
RADIOLIB_ASSERT(state);
// wait for transmission start or timeout
uint32_t start = _mod->hal->micros();
while(!_mod->hal->digitalRead(_mod->getGpio())) {
_mod->hal->yield();
uint32_t start = this->mod->hal->micros();
while(!this->mod->hal->digitalRead(this->mod->getGpio())) {
this->mod->hal->yield();
if(_mod->hal->micros() - start > timeout) {
if(this->mod->hal->micros() - start > timeout) {
finishTransmit();
return(RADIOLIB_ERR_TX_TIMEOUT);
}
}
// wait for transmission end or timeout
start = _mod->hal->micros();
while(_mod->hal->digitalRead(_mod->getGpio())) {
_mod->hal->yield();
start = this->mod->hal->micros();
while(this->mod->hal->digitalRead(this->mod->getGpio())) {
this->mod->hal->yield();
if(_mod->hal->micros() - start > timeout) {
if(this->mod->hal->micros() - start > timeout) {
finishTransmit();
return(RADIOLIB_ERR_TX_TIMEOUT);
}
@ -126,18 +137,18 @@ 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))*(RADIOLIB_CC1101_MAX_PACKET_LENGTH*400.0);
uint32_t timeout = 500000 + (1.0/(this->bitRate*1000.0))*(RADIOLIB_CC1101_MAX_PACKET_LENGTH*400.0);
// start reception
int16_t state = startReceive();
RADIOLIB_ASSERT(state);
// wait for packet or timeout
uint32_t start = _mod->hal->micros();
while(!_mod->hal->digitalRead(_mod->getIrq())) {
_mod->hal->yield();
uint32_t start = this->mod->hal->micros();
while(!this->mod->hal->digitalRead(this->mod->getIrq())) {
this->mod->hal->yield();
if(_mod->hal->micros() - start > timeout) {
if(this->mod->hal->micros() - start > timeout) {
standby();
SPIsendCommand(RADIOLIB_CC1101_CMD_FLUSH_RX);
return(RADIOLIB_ERR_RX_TIMEOUT);
@ -153,7 +164,7 @@ int16_t CC1101::standby() {
SPIsendCommand(RADIOLIB_CC1101_CMD_IDLE);
// set RF switch (if present)
_mod->setRfSwitchState(Module::MODE_IDLE);
this->mod->setRfSwitchState(Module::MODE_IDLE);
return(RADIOLIB_ERR_NONE);
}
@ -172,7 +183,7 @@ int16_t CC1101::transmitDirectAsync(uint32_t frf) {
int16_t CC1101::transmitDirect(bool sync, uint32_t frf) {
// set RF switch (if present)
_mod->setRfSwitchState(Module::MODE_TX);
this->mod->setRfSwitchState(Module::MODE_TX);
// user requested to start transmitting immediately (required for RTTY)
if(frf != 0) {
@ -202,7 +213,7 @@ int16_t CC1101::receiveDirectAsync() {
int16_t CC1101::receiveDirect(bool sync) {
// set RF switch (if present)
_mod->setRfSwitchState(Module::MODE_RX);
this->mod->setRfSwitchState(Module::MODE_RX);
// activate direct mode
int16_t state = directMode(sync);
@ -216,31 +227,33 @@ int16_t CC1101::receiveDirect(bool sync) {
int16_t CC1101::packetMode() {
int16_t state = SPIsetRegValue(RADIOLIB_CC1101_REG_PKTCTRL1, RADIOLIB_CC1101_CRC_AUTOFLUSH_OFF | RADIOLIB_CC1101_APPEND_STATUS_ON | RADIOLIB_CC1101_ADR_CHK_NONE, 3, 0);
state |= SPIsetRegValue(RADIOLIB_CC1101_REG_PKTCTRL0, RADIOLIB_CC1101_WHITE_DATA_OFF | RADIOLIB_CC1101_PKT_FORMAT_NORMAL, 6, 4);
state |= SPIsetRegValue(RADIOLIB_CC1101_REG_PKTCTRL0, RADIOLIB_CC1101_CRC_ON | _packetLengthConfig, 2, 0);
state |= SPIsetRegValue(RADIOLIB_CC1101_REG_PKTCTRL0, RADIOLIB_CC1101_CRC_ON | this->packetLengthConfig, 2, 0);
return(state);
}
void CC1101::setGdo0Action(void (*func)(void), uint32_t dir) {
_mod->hal->attachInterrupt(_mod->hal->pinToInterrupt(_mod->getIrq()), func, dir);
this->mod->hal->attachInterrupt(this->mod->hal->pinToInterrupt(this->mod->getIrq()), func, dir);
}
void CC1101::clearGdo0Action() {
_mod->hal->detachInterrupt(_mod->hal->pinToInterrupt(_mod->getIrq()));
this->mod->hal->detachInterrupt(this->mod->hal->pinToInterrupt(this->mod->getIrq()));
}
}
void CC1101::setGdo2Action(void (*func)(void), uint32_t dir) {
if(_mod->getGpio() == RADIOLIB_NC) {
if(this->mod->getGpio() == RADIOLIB_NC) {
return;
}
_mod->hal->pinMode(_mod->getGpio(), _mod->hal->GpioModeInput);
_mod->hal->attachInterrupt(_mod->hal->pinToInterrupt(_mod->getGpio()), func, dir);
this->mod->hal->pinMode(this->mod->getGpio(), this->mod->hal->GpioModeInput);
this->mod->hal->attachInterrupt(this->mod->hal->pinToInterrupt(this->mod->getGpio()), func, dir);
}
void CC1101::clearGdo2Action() {
if(_mod->getGpio() == RADIOLIB_NC) {
if(this->mod->getGpio() == RADIOLIB_NC) {
return;
}
_mod->hal->detachInterrupt(_mod->hal->pinToInterrupt(_mod->getGpio()));
this->mod->hal->detachInterrupt(this->mod->hal->pinToInterrupt(this->mod->getGpio()));
}
int16_t CC1101::startTransmit(uint8_t* data, size_t len, uint8_t addr) {
@ -256,16 +269,16 @@ int16_t CC1101::startTransmit(uint8_t* data, size_t len, uint8_t addr) {
SPIsendCommand(RADIOLIB_CC1101_CMD_FLUSH_TX);
// set GDO0 mapping
int16_t state = SPIsetRegValue(RADIOLIB_CC1101_REG_IOCFG2, RADIOLIB_CC1101_GDOX_SYNC_WORD_SENT_OR_RECEIVED, 5, 0);
int16_t state = SPIsetRegValue(RADIOLIB_CC1101_REG_IOCFG2, RADIOLIB_CC1101_GDOX_SYNC_WORD_SENT_OR_PKT_RECEIVED, 5, 0);
RADIOLIB_ASSERT(state);
// data put on FIFO.
// data put on FIFO
uint8_t dataSent = 0;
// optionally write packet length
if (_packetLengthConfig == RADIOLIB_CC1101_LENGTH_CONFIG_VARIABLE) {
if (this->packetLengthConfig == RADIOLIB_CC1101_LENGTH_CONFIG_VARIABLE) {
// enforce variable len limit.
// enforce variable len limit
if (len > RADIOLIB_CC1101_MAX_PACKET_LENGTH - 1) {
return (RADIOLIB_ERR_PACKET_TOO_LONG);
}
@ -281,36 +294,37 @@ int16_t CC1101::startTransmit(uint8_t* data, size_t len, uint8_t addr) {
dataSent += 1;
}
// fill the FIFO.
// fill the FIFO
uint8_t initialWrite = min((uint8_t)len, (uint8_t)(RADIOLIB_CC1101_FIFO_SIZE - dataSent));
SPIwriteRegisterBurst(RADIOLIB_CC1101_REG_FIFO, data, initialWrite);
dataSent += initialWrite;
// set RF switch (if present)
_mod->setRfSwitchState(Module::MODE_TX);
this->mod->setRfSwitchState(Module::MODE_TX);
// set mode to transmit
SPIsendCommand(RADIOLIB_CC1101_CMD_TX);
// keep feeding the FIFO until the packet is over.
// keep feeding the FIFO until the packet is over
while (dataSent < len) {
// get number of bytes in FIFO.
// get number of bytes in FIFO
uint8_t bytesInFIFO = SPIgetRegValue(RADIOLIB_CC1101_REG_TXBYTES, 6, 0);
// if there's room then put other data.
// if there's room then put other data
if (bytesInFIFO < RADIOLIB_CC1101_FIFO_SIZE) {
uint8_t bytesToWrite = min((uint8_t)(RADIOLIB_CC1101_FIFO_SIZE - bytesInFIFO), (uint8_t)(len - dataSent));
SPIwriteRegisterBurst(RADIOLIB_CC1101_REG_FIFO, &data[dataSent], bytesToWrite);
dataSent += bytesToWrite;
} else {
// wait for radio to send some data.
// wait for radio to send some data
/*
* Does this work for all rates? If 1 ms is longer than the 1ms delay
* then the entire FIFO will be transmitted during that delay.
*
* TODO: test this on real hardware
*/
_mod->hal->delayMicroseconds(250);
this->mod->hal->delayMicroseconds(250);
}
}
@ -320,6 +334,7 @@ int16_t CC1101::startTransmit(uint8_t* data, size_t len, uint8_t addr) {
int16_t CC1101::finishTransmit() {
// set mode to standby to disable transmitter/RF switch
int16_t state = standby();
RADIOLIB_ASSERT(state);
// flush Tx FIFO
SPIsendCommand(RADIOLIB_CC1101_CMD_FLUSH_TX);
@ -329,18 +344,19 @@ int16_t CC1101::finishTransmit() {
int16_t CC1101::startReceive() {
// set mode to standby
standby();
int16_t state = standby();
RADIOLIB_ASSERT(state);
// flush Rx FIFO
SPIsendCommand(RADIOLIB_CC1101_CMD_FLUSH_RX);
// set GDO0 mapping: Asserted when RX FIFO > 4 bytes.
int16_t state = SPIsetRegValue(RADIOLIB_CC1101_REG_IOCFG0, RADIOLIB_CC1101_GDOX_RX_FIFO_FULL_OR_PKT_END);
state = SPIsetRegValue(RADIOLIB_CC1101_REG_IOCFG0, RADIOLIB_CC1101_GDOX_RX_FIFO_FULL_OR_PKT_END);
state |= SPIsetRegValue(RADIOLIB_CC1101_REG_FIFOTHR, RADIOLIB_CC1101_FIFO_THR_TX_61_RX_4, 3, 0);
RADIOLIB_ASSERT(state);
// set RF switch (if present)
_mod->setRfSwitchState(Module::MODE_RX);
this->mod->setRfSwitchState(Module::MODE_RX);
// set mode to receive
SPIsendCommand(RADIOLIB_CC1101_CMD_RX);
@ -372,17 +388,17 @@ int16_t CC1101::readData(uint8_t* data, size_t len) {
uint8_t bytesInFIFO = SPIgetRegValue(RADIOLIB_CC1101_REG_RXBYTES, 6, 0);
size_t readBytes = 0;
uint32_t lastPop = _mod->hal->millis();
uint32_t lastPop = this->mod->hal->millis();
// keep reading from FIFO until we get all the packet.
while (readBytes < length) {
if (bytesInFIFO == 0) {
if (_mod->hal->millis() - lastPop > 5) {
if (this->mod->hal->millis() - lastPop > 5) {
// readData was required to read a packet longer than the one received.
RADIOLIB_DEBUG_PRINTLN("No data for more than 5mS. Stop here.");
break;
} else {
_mod->hal->delay(1);
this->mod->hal->delay(1);
bytesInFIFO = SPIgetRegValue(RADIOLIB_CC1101_REG_RXBYTES, 6, 0);
continue;
}
@ -392,7 +408,7 @@ int16_t CC1101::readData(uint8_t* data, size_t len) {
uint8_t bytesToRead = min((uint8_t)(length - readBytes), bytesInFIFO);
SPIreadRegisterBurst(RADIOLIB_CC1101_REG_FIFO, bytesToRead, &(data[readBytes]));
readBytes += bytesToRead;
lastPop = _mod->hal->millis();
lastPop = this->mod->hal->millis();
// Get how many bytes are left in FIFO.
bytesInFIFO = SPIgetRegValue(RADIOLIB_CC1101_REG_RXBYTES, 6, 0);
@ -402,26 +418,26 @@ int16_t CC1101::readData(uint8_t* data, size_t len) {
bool isAppendStatus = SPIgetRegValue(RADIOLIB_CC1101_REG_PKTCTRL1, 2, 2) == RADIOLIB_CC1101_APPEND_STATUS_ON;
// for some reason, we need this delay here to get the correct status bytes
_mod->hal->delay(3);
this->mod->hal->delay(3);
// If status byte is enabled at least 2 bytes (2 status bytes + any following packet) will remain in FIFO.
if (isAppendStatus) {
// read RSSI byte
_rawRSSI = SPIgetRegValue(RADIOLIB_CC1101_REG_FIFO);
this->rawRSSI = SPIgetRegValue(RADIOLIB_CC1101_REG_FIFO);
// read LQI and CRC byte
uint8_t val = SPIgetRegValue(RADIOLIB_CC1101_REG_FIFO);
_rawLQI = val & 0x7F;
this->rawLQI = val & 0x7F;
// check CRC
if (_crcOn && (val & RADIOLIB_CC1101_CRC_OK) == RADIOLIB_CC1101_CRC_ERROR) {
_packetLengthQueried = false;
if (this->crcOn && (val & RADIOLIB_CC1101_CRC_OK) == RADIOLIB_CC1101_CRC_ERROR) {
this->packetLengthQueried = false;
return (RADIOLIB_ERR_CRC_MISMATCH);
}
}
// clear internal flag so getPacketLength can return the new packet length
_packetLengthQueried = false;
this->packetLengthQueried = false;
// Flush then standby according to RXOFF_MODE (default: RADIOLIB_CC1101_RXOFF_IDLE)
if (SPIgetRegValue(RADIOLIB_CC1101_REG_MCSM1, 3, 2) == RADIOLIB_CC1101_RXOFF_IDLE) {
@ -455,11 +471,11 @@ int16_t CC1101::setFrequency(float freq) {
state |= SPIsetRegValue(RADIOLIB_CC1101_REG_FREQ0, FRF & 0x0000FF, 7, 0);
if(state == RADIOLIB_ERR_NONE) {
_freq = freq;
this->freq = freq;
}
// Update the TX power accordingly to new freq. (PA values depend on chosen freq)
return(setOutputPower(_power));
return(setOutputPower(this->power));
}
int16_t CC1101::setBitRate(float br) {
@ -477,7 +493,7 @@ int16_t CC1101::setBitRate(float br) {
int16_t state = SPIsetRegValue(RADIOLIB_CC1101_REG_MDMCFG4, e, 3, 0);
state |= SPIsetRegValue(RADIOLIB_CC1101_REG_MDMCFG3, m);
if(state == RADIOLIB_ERR_NONE) {
_br = br;
this->bitRate = br;
}
return(state);
}
@ -536,7 +552,7 @@ int16_t CC1101::getFrequencyDeviation(float *freqDev) {
}
// if ASK/OOK, deviation makes no sense
if (_modulation == RADIOLIB_CC1101_MOD_FORMAT_ASK_OOK) {
if (this->modulation == RADIOLIB_CC1101_MOD_FORMAT_ASK_OOK) {
*freqDev = 0.0;
return(RADIOLIB_ERR_NONE);
@ -558,13 +574,13 @@ int16_t CC1101::getFrequencyDeviation(float *freqDev) {
int16_t CC1101::setOutputPower(int8_t power) {
// round to the known frequency settings
uint8_t f;
if(_freq < 374.0) {
if(this->freq < 374.0) {
// 315 MHz
f = 0;
} else if(_freq < 650.5) {
} else if(this->freq < 650.5) {
// 434 MHz
f = 1;
} else if(_freq < 891.5) {
} else if(this->freq < 891.5) {
// 868 MHz
f = 2;
} else {
@ -613,9 +629,9 @@ int16_t CC1101::setOutputPower(int8_t power) {
}
// store the value
_power = power;
this->power = power;
if(_modulation == RADIOLIB_CC1101_MOD_FORMAT_ASK_OOK){
if(this->modulation == RADIOLIB_CC1101_MOD_FORMAT_ASK_OOK){
// Amplitude modulation:
// PA_TABLE[0] is the power to be used when transmitting a 0 (no power)
// PA_TABLE[1] is the power to be used when transmitting a 1 (full power)
@ -659,10 +675,10 @@ int16_t CC1101::setSyncWord(uint8_t syncH, uint8_t syncL, uint8_t maxErrBits, bo
return(setSyncWord(syncWord, sizeof(syncWord), maxErrBits, requireCarrierSense));
}
int16_t CC1101::setPreambleLength(uint8_t preambleLength) {
int16_t CC1101::setPreambleLength(uint8_t preambleLength, uint8_t qualityThreshold) {
// check allowed values
uint8_t value;
switch(preambleLength){
switch(preambleLength) {
case 16:
value = RADIOLIB_CC1101_NUM_PREAMBLE_2;
break;
@ -691,7 +707,14 @@ int16_t CC1101::setPreambleLength(uint8_t preambleLength) {
return(RADIOLIB_ERR_INVALID_PREAMBLE_LENGTH);
}
return(SPIsetRegValue(RADIOLIB_CC1101_REG_MDMCFG1, value, 6, 4));
// set preabmble quality threshold and the actual length
uint8_t pqt = qualityThreshold/4;
if(pqt > 7) {
pqt = 7;
}
int16_t state = SPIsetRegValue(RADIOLIB_CC1101_REG_PKTCTRL1, pqt << 5, 7, 5);
state |= SPIsetRegValue(RADIOLIB_CC1101_REG_MDMCFG1, value, 6, 4);
return(state);
}
int16_t CC1101::setNodeAddress(uint8_t nodeAddr, uint8_t numBroadcastAddrs) {
@ -727,7 +750,7 @@ int16_t CC1101::setOOK(bool enableOOK) {
RADIOLIB_ASSERT(state);
// update current modulation
_modulation = RADIOLIB_CC1101_MOD_FORMAT_ASK_OOK;
this->modulation = RADIOLIB_CC1101_MOD_FORMAT_ASK_OOK;
} else {
int16_t state = SPIsetRegValue(RADIOLIB_CC1101_REG_MDMCFG2, RADIOLIB_CC1101_MOD_FORMAT_2_FSK, 6, 4);
RADIOLIB_ASSERT(state);
@ -737,21 +760,21 @@ int16_t CC1101::setOOK(bool enableOOK) {
RADIOLIB_ASSERT(state);
// update current modulation
_modulation = RADIOLIB_CC1101_MOD_FORMAT_2_FSK;
this->modulation = RADIOLIB_CC1101_MOD_FORMAT_2_FSK;
}
// Update PA_TABLE values according to the new _modulation.
return(setOutputPower(_power));
// Update PA_TABLE values according to the new this->modulation.
return(setOutputPower(this->power));
}
float CC1101::getRSSI() {
float rssi;
if (_directMode) {
if(_rawRSSI >= 128) {
rssi = (((float)_rawRSSI - 256.0)/2.0) - 74.0;
if (this->directModeEnabled) {
if(this->rawRSSI >= 128) {
rssi = (((float)this->rawRSSI - 256.0)/2.0) - 74.0;
} else {
rssi = (((float)_rawRSSI)/2.0) - 74.0;
rssi = (((float)this->rawRSSI)/2.0) - 74.0;
}
} else {
uint8_t rawRssi = SPIreadRegister(RADIOLIB_CC1101_REG_RSSI);
@ -768,21 +791,25 @@ float CC1101::getRSSI() {
}
uint8_t CC1101::getLQI() const {
return(_rawLQI);
return(this->rawLQI);
}
size_t CC1101::getPacketLength(bool update) {
if(!_packetLengthQueried && update) {
if (_packetLengthConfig == RADIOLIB_CC1101_LENGTH_CONFIG_VARIABLE) {
_packetLength = SPIreadRegister(RADIOLIB_CC1101_REG_FIFO);
if(!this->packetLengthQueried && update) {
if (this->packetLengthConfig == RADIOLIB_CC1101_LENGTH_CONFIG_VARIABLE) {
this->packetLength = 0;
while(this->packetLength == 0) {
this->packetLength = SPIreadRegister(RADIOLIB_CC1101_REG_FIFO);
}
} else {
_packetLength = SPIreadRegister(RADIOLIB_CC1101_REG_PKTLEN);
this->packetLength = SPIreadRegister(RADIOLIB_CC1101_REG_PKTLEN);
}
_packetLengthQueried = true;
this->packetLengthQueried = true;
}
return(_packetLength);
return(this->packetLength);
}
int16_t CC1101::fixedPacketLengthMode(uint8_t len) {
@ -794,20 +821,27 @@ int16_t CC1101::variablePacketLengthMode(uint8_t maxLen) {
}
int16_t CC1101::enableSyncWordFiltering(uint8_t maxErrBits, bool requireCarrierSense) {
switch(maxErrBits){
int16_t state = RADIOLIB_ERR_NONE;
switch(maxErrBits) {
case 0:
// in 16 bit sync word, expect all 16 bits
return(SPIsetRegValue(RADIOLIB_CC1101_REG_MDMCFG2, (requireCarrierSense ? RADIOLIB_CC1101_SYNC_MODE_16_16_THR : RADIOLIB_CC1101_SYNC_MODE_16_16), 2, 0));
state |= SPIsetRegValue(RADIOLIB_CC1101_REG_MDMCFG2, (requireCarrierSense ? RADIOLIB_CC1101_SYNC_MODE_16_16_THR : RADIOLIB_CC1101_SYNC_MODE_16_16), 2, 0);
break;
case 1:
// in 16 bit sync word, expect at least 15 bits
return(SPIsetRegValue(RADIOLIB_CC1101_REG_MDMCFG2, (requireCarrierSense ? RADIOLIB_CC1101_SYNC_MODE_15_16_THR : RADIOLIB_CC1101_SYNC_MODE_15_16), 2, 0));
state |= SPIsetRegValue(RADIOLIB_CC1101_REG_MDMCFG2, (requireCarrierSense ? RADIOLIB_CC1101_SYNC_MODE_15_16_THR : RADIOLIB_CC1101_SYNC_MODE_15_16), 2, 0);
break;
default:
return(RADIOLIB_ERR_INVALID_SYNC_WORD);
state = RADIOLIB_ERR_INVALID_SYNC_WORD;
break;
}
return(state);
}
int16_t CC1101::disableSyncWordFiltering(bool requireCarrierSense) {
return(SPIsetRegValue(RADIOLIB_CC1101_REG_MDMCFG2, (requireCarrierSense ? RADIOLIB_CC1101_SYNC_MODE_NONE_THR : RADIOLIB_CC1101_SYNC_MODE_NONE), 2, 0));
int16_t state = SPIsetRegValue(RADIOLIB_CC1101_REG_MDMCFG2, (requireCarrierSense ? RADIOLIB_CC1101_SYNC_MODE_NONE_THR : RADIOLIB_CC1101_SYNC_MODE_NONE), 2, 0);
return(state);
}
int16_t CC1101::setCrcFiltering(bool crcOn) {
@ -820,14 +854,14 @@ int16_t CC1101::setCrcFiltering(bool crcOn) {
}
}
int16_t CC1101::setPromiscuousMode(bool promiscuous) {
int16_t CC1101::setPromiscuousMode(bool enable) {
int16_t state = RADIOLIB_ERR_NONE;
if (_promiscuous == promiscuous) {
if(this->promiscuous == enable) {
return(state);
}
if (promiscuous == true) {
if(enable) {
// disable sync word filtering and insertion
// this also disables preamble
state = disableSyncWordFiltering();
@ -836,7 +870,7 @@ int16_t CC1101::setPromiscuousMode(bool promiscuous) {
// disable CRC filtering
state = setCrcFiltering(false);
} else {
state = setPreambleLength(RADIOLIB_CC1101_DEFAULT_PREAMBLELEN);
state = setPreambleLength(RADIOLIB_CC1101_DEFAULT_PREAMBLELEN, RADIOLIB_CC1101_DEFAULT_PREAMBLELEN/4);
RADIOLIB_ASSERT(state);
// enable sync word filtering and insertion
@ -847,13 +881,13 @@ int16_t CC1101::setPromiscuousMode(bool promiscuous) {
state = setCrcFiltering(true);
}
_promiscuous = promiscuous;
this->promiscuous = enable;
return(state);
}
bool CC1101::getPromiscuousMode() {
return (_promiscuous);
return (this->promiscuous);
}
int16_t CC1101::setDataShaping(uint8_t sh) {
@ -900,11 +934,11 @@ int16_t CC1101::setEncoding(uint8_t encoding) {
}
void CC1101::setRfSwitchPins(uint32_t rxEn, uint32_t txEn) {
_mod->setRfSwitchPins(rxEn, txEn);
this->mod->setRfSwitchPins(rxEn, txEn);
}
void CC1101::setRfSwitchTable(const uint32_t (&pins)[Module::RFSWITCH_MAX_PINS], const Module::RfSwitchMode_t table[]) {
_mod->setRfSwitchTable(pins, table);
this->mod->setRfSwitchTable(pins, table);
}
uint8_t CC1101::randomByte() {
@ -913,7 +947,7 @@ uint8_t CC1101::randomByte() {
RADIOLIB_DEBUG_PRINTLN("CC1101::randomByte");
// wait a bit for the RSSI reading to stabilise
_mod->hal->delay(10);
this->mod->hal->delay(10);
// read RSSI value 8 times, always keep just the least significant bit
uint8_t randByte = 0x00;
@ -933,30 +967,39 @@ int16_t CC1101::getChipVersion() {
#if !defined(RADIOLIB_EXCLUDE_DIRECT_RECEIVE)
void CC1101::setDirectAction(void (*func)(void)) {
setGdo0Action(func, _mod->hal->GpioInterruptRising);
setGdo0Action(func, this->mod->hal->GpioInterruptRising);
}
void CC1101::readBit(uint32_t pin) {
updateDirectBuffer((uint8_t)_mod->hal->digitalRead(pin));
updateDirectBuffer((uint8_t)this->mod->hal->digitalRead(pin));
}
#endif
int16_t CC1101::setDIOMapping(uint32_t pin, uint32_t value) {
if (pin > 2)
return RADIOLIB_ERR_INVALID_DIO_PIN;
if(pin > 2) {
return(RADIOLIB_ERR_INVALID_DIO_PIN);
}
return(SPIsetRegValue(RADIOLIB_CC1101_REG_IOCFG0 - pin, value));
}
int16_t CC1101::config() {
// Reset the radio. Registers may be dirty from previous usage.
SPIsendCommand(RADIOLIB_CC1101_CMD_RESET);
reset();
// Wait a ridiculous amount of time to be sure radio is ready.
_mod->hal->delay(150);
this->mod->hal->delay(150);
// enable automatic frequency synthesizer calibration
standby();
// enable automatic frequency synthesizer calibration and disable pin control
int16_t state = SPIsetRegValue(RADIOLIB_CC1101_REG_MCSM0, RADIOLIB_CC1101_FS_AUTOCAL_IDLE_TO_RXTX, 5, 4);
state |= SPIsetRegValue(RADIOLIB_CC1101_REG_MCSM0, RADIOLIB_CC1101_PIN_CTRL_OFF, 1, 1);
RADIOLIB_ASSERT(state);
// set GDOs to Hi-Z so that it doesn't output clock on startup (might confuse GDO0 action)
state = SPIsetRegValue(RADIOLIB_CC1101_REG_IOCFG0, RADIOLIB_CC1101_GDOX_HIGH_Z, 5, 0);
state |= SPIsetRegValue(RADIOLIB_CC1101_REG_IOCFG2, RADIOLIB_CC1101_GDOX_HIGH_Z, 5, 0);
RADIOLIB_ASSERT(state);
// set packet mode
@ -970,16 +1013,15 @@ int16_t CC1101::directMode(bool sync) {
SPIsendCommand(RADIOLIB_CC1101_CMD_IDLE);
int16_t state = 0;
_directMode = sync;
if (sync) {
this->directModeEnabled = sync;
if(sync) {
// set GDO0 and GDO2 mapping
state |= SPIsetRegValue(RADIOLIB_CC1101_REG_IOCFG0, RADIOLIB_CC1101_GDOX_SERIAL_CLOCK , 5, 0);
state |= SPIsetRegValue(RADIOLIB_CC1101_REG_IOCFG2, RADIOLIB_CC1101_GDOX_SERIAL_DATA_SYNC , 5, 0);
// set continuous mode
state |= SPIsetRegValue(RADIOLIB_CC1101_REG_PKTCTRL0, RADIOLIB_CC1101_PKT_FORMAT_SYNCHRONOUS, 5, 4);
}
else {
} else {
// set GDO0 mapping
state |= SPIsetRegValue(RADIOLIB_CC1101_REG_IOCFG0, RADIOLIB_CC1101_GDOX_SERIAL_DATA_ASYNC , 5, 0);
@ -1032,8 +1074,8 @@ int16_t CC1101::setPacketMode(uint8_t mode, uint16_t len) {
RADIOLIB_ASSERT(state);
// update the cached value
_packetLength = len;
_packetLengthConfig = mode;
this->packetLength = len;
this->packetLengthConfig = mode;
return(state);
}
@ -1043,7 +1085,7 @@ int16_t CC1101::SPIgetRegValue(uint8_t reg, uint8_t msb, uint8_t lsb) {
reg |= RADIOLIB_CC1101_CMD_ACCESS_STATUS_REG;
}
return(_mod->SPIgetRegValue(reg, msb, lsb));
return(this->mod->SPIgetRegValue(reg, msb, lsb));
}
int16_t CC1101::SPIsetRegValue(uint8_t reg, uint8_t value, uint8_t msb, uint8_t lsb, uint8_t checkInterval) {
@ -1052,11 +1094,11 @@ int16_t CC1101::SPIsetRegValue(uint8_t reg, uint8_t value, uint8_t msb, uint8_t
reg |= RADIOLIB_CC1101_CMD_ACCESS_STATUS_REG;
}
return(_mod->SPIsetRegValue(reg, value, msb, lsb, checkInterval));
return(this->mod->SPIsetRegValue(reg, value, msb, lsb, checkInterval));
}
void CC1101::SPIreadRegisterBurst(uint8_t reg, uint8_t numBytes, uint8_t* inBytes) {
_mod->SPIreadRegisterBurst(reg | RADIOLIB_CC1101_CMD_BURST, numBytes, inBytes);
this->mod->SPIreadRegisterBurst(reg | RADIOLIB_CC1101_CMD_BURST, numBytes, inBytes);
}
uint8_t CC1101::SPIreadRegister(uint8_t reg) {
@ -1065,7 +1107,7 @@ uint8_t CC1101::SPIreadRegister(uint8_t reg) {
reg |= RADIOLIB_CC1101_CMD_ACCESS_STATUS_REG;
}
return(_mod->SPIreadRegister(reg));
return(this->mod->SPIreadRegister(reg));
}
void CC1101::SPIwriteRegister(uint8_t reg, uint8_t data) {
@ -1074,26 +1116,28 @@ void CC1101::SPIwriteRegister(uint8_t reg, uint8_t data) {
reg |= RADIOLIB_CC1101_CMD_ACCESS_STATUS_REG;
}
return(_mod->SPIwriteRegister(reg, data));
return(this->mod->SPIwriteRegister(reg, data));
}
void CC1101::SPIwriteRegisterBurst(uint8_t reg, uint8_t* data, size_t len) {
_mod->SPIwriteRegisterBurst(reg | RADIOLIB_CC1101_CMD_BURST, data, len);
this->mod->SPIwriteRegisterBurst(reg | RADIOLIB_CC1101_CMD_BURST, data, len);
}
void CC1101::SPIsendCommand(uint8_t cmd) {
// pull NSS low
_mod->hal->digitalWrite(_mod->getCs(), _mod->hal->GpioLevelLow);
this->mod->hal->digitalWrite(this->mod->getCs(), this->mod->hal->GpioLevelLow);
// start transfer
_mod->hal->spiBeginTransaction();
this->mod->hal->spiBeginTransaction();
// send the command byte
_mod->hal->spiTransfer(cmd);
uint8_t status = this->mod->hal->spiTransfer(cmd);
// stop transfer
_mod->hal->spiEndTransaction();
_mod->hal->digitalWrite(_mod->getCs(), _mod->hal->GpioLevelHigh);
this->mod->hal->spiEndTransaction();
this->mod->hal->digitalWrite(this->mod->getCs(), this->mod->hal->GpioLevelHigh);
RADIOLIB_VERBOSE_PRINTLN("CMD\tW\t%02X\t%02X", cmd, status);
(void)status;
}
#endif

File diff suppressed because it is too large Load diff