[CC1101] Reduced code redundancies
This commit is contained in:
parent
af292e9a69
commit
55459f5271
2 changed files with 96 additions and 197 deletions
|
@ -82,38 +82,12 @@ int16_t CC1101::begin(float freq, float br, float rxBw, float freqDev, int8_t po
|
||||||
}
|
}
|
||||||
|
|
||||||
int16_t CC1101::transmit(uint8_t* data, size_t len, uint8_t addr) {
|
int16_t CC1101::transmit(uint8_t* data, size_t len, uint8_t addr) {
|
||||||
// check packet length
|
// start transmission
|
||||||
if(len > 63) {
|
int16_t state = startTransmit(data, len, addr);
|
||||||
return(ERR_PACKET_TOO_LONG);
|
|
||||||
}
|
|
||||||
|
|
||||||
// set mode to standby
|
|
||||||
standby();
|
|
||||||
|
|
||||||
// flush Tx FIFO
|
|
||||||
SPIsendCommand(CC1101_CMD_FLUSH_TX);
|
|
||||||
|
|
||||||
// set GDO0 mapping
|
|
||||||
int state = SPIsetRegValue(CC1101_REG_IOCFG0, CC1101_GDOX_SYNC_WORD_SENT_OR_RECEIVED);
|
|
||||||
if(state != ERR_NONE) {
|
if(state != ERR_NONE) {
|
||||||
return(state);
|
return(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
// write packet length
|
|
||||||
SPIwriteRegister(CC1101_REG_FIFO, len);
|
|
||||||
|
|
||||||
// check address filtering
|
|
||||||
uint8_t filter = SPIgetRegValue(CC1101_REG_PKTCTRL1, 1, 0);
|
|
||||||
if(filter != CC1101_ADR_CHK_NONE) {
|
|
||||||
SPIwriteRegister(CC1101_REG_FIFO, addr);
|
|
||||||
}
|
|
||||||
|
|
||||||
// write packet to FIFO
|
|
||||||
SPIwriteRegisterBurst(CC1101_REG_FIFO, data, len);
|
|
||||||
|
|
||||||
// set mode to transmit
|
|
||||||
SPIsendCommand(CC1101_CMD_TX);
|
|
||||||
|
|
||||||
// wait for transmission start
|
// wait for transmission start
|
||||||
while(!digitalRead(_mod->getInt0()));
|
while(!digitalRead(_mod->getInt0()));
|
||||||
|
|
||||||
|
@ -130,67 +104,20 @@ int16_t CC1101::transmit(uint8_t* data, size_t len, uint8_t addr) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int16_t CC1101::receive(uint8_t* data, size_t len) {
|
int16_t CC1101::receive(uint8_t* data, size_t len) {
|
||||||
// set mode to standby
|
// start reception
|
||||||
standby();
|
int16_t state = startReceive();
|
||||||
|
|
||||||
// flush Rx FIFO
|
|
||||||
SPIsendCommand(CC1101_CMD_FLUSH_RX);
|
|
||||||
|
|
||||||
// set GDO0 mapping
|
|
||||||
int state = SPIsetRegValue(CC1101_REG_IOCFG0, CC1101_GDOX_SYNC_WORD_SENT_OR_RECEIVED);
|
|
||||||
if(state != ERR_NONE) {
|
if(state != ERR_NONE) {
|
||||||
return(state);
|
return(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
// set mode to receive
|
|
||||||
SPIsendCommand(CC1101_CMD_RX);
|
|
||||||
|
|
||||||
// wait for sync word
|
// wait for sync word
|
||||||
while(!digitalRead(_mod->getInt0()));
|
while(!digitalRead(_mod->getInt0()));
|
||||||
|
|
||||||
// wait for packet end
|
// wait for packet end
|
||||||
while(digitalRead(_mod->getInt0()));
|
while(digitalRead(_mod->getInt0()));
|
||||||
|
|
||||||
// get packet length
|
|
||||||
size_t length = SPIreadRegister(CC1101_REG_RXBYTES) - 2;
|
|
||||||
|
|
||||||
// check address filtering
|
|
||||||
uint8_t filter = SPIgetRegValue(CC1101_REG_PKTCTRL1, 1, 0);
|
|
||||||
if(filter != CC1101_ADR_CHK_NONE) {
|
|
||||||
SPIreadRegister(CC1101_REG_FIFO);
|
|
||||||
}
|
|
||||||
|
|
||||||
// read packet data
|
// read packet data
|
||||||
if(len == 0) {
|
return(readData(data, len));
|
||||||
// argument len equal to zero indicates String call, which means dynamically allocated data array
|
|
||||||
// dispose of the original and create a new one
|
|
||||||
delete[] data;
|
|
||||||
data = new uint8_t[length + 1];
|
|
||||||
}
|
|
||||||
SPIreadRegisterBurst(CC1101_REG_FIFO, length, data);
|
|
||||||
|
|
||||||
// read RSSI byte
|
|
||||||
_rawRSSI = SPIgetRegValue(CC1101_REG_FIFO);
|
|
||||||
|
|
||||||
// read LQI and CRC byte
|
|
||||||
uint8_t val = SPIgetRegValue(CC1101_REG_FIFO);
|
|
||||||
_rawLQI = val & 0x7F;
|
|
||||||
|
|
||||||
// add terminating null
|
|
||||||
data[length] = 0;
|
|
||||||
|
|
||||||
// flush Rx FIFO
|
|
||||||
SPIsendCommand(CC1101_CMD_FLUSH_RX);
|
|
||||||
|
|
||||||
// set mode to standby
|
|
||||||
standby();
|
|
||||||
|
|
||||||
// check CRC
|
|
||||||
if((val & 0b10000000) == 0b00000000) {
|
|
||||||
return(ERR_CRC_MISMATCH);
|
|
||||||
}
|
|
||||||
|
|
||||||
return(state);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int16_t CC1101::standby() {
|
int16_t CC1101::standby() {
|
||||||
|
@ -239,14 +166,6 @@ void CC1101::setGdo2Action(void (*func)(void), uint8_t dir) {
|
||||||
attachInterrupt(digitalPinToInterrupt(_mod->getInt1()), func, dir);
|
attachInterrupt(digitalPinToInterrupt(_mod->getInt1()), func, dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
int16_t CC1101::startTransmit(String& str, uint8_t addr) {
|
|
||||||
return(CC1101::startTransmit(str.c_str(), addr));
|
|
||||||
}
|
|
||||||
|
|
||||||
int16_t CC1101::startTransmit(const char* str, uint8_t addr) {
|
|
||||||
return(CC1101::startTransmit((uint8_t*)str, strlen(str), addr));
|
|
||||||
}
|
|
||||||
|
|
||||||
int16_t CC1101::startTransmit(uint8_t* data, size_t len, uint8_t addr) {
|
int16_t CC1101::startTransmit(uint8_t* data, size_t len, uint8_t addr) {
|
||||||
// check packet length
|
// check packet length
|
||||||
if(len > 63) {
|
if(len > 63) {
|
||||||
|
@ -302,20 +221,6 @@ int16_t CC1101::startReceive() {
|
||||||
return(state);
|
return(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
int16_t CC1101::readData(String& str, size_t len) {
|
|
||||||
// create temporary array to store received data
|
|
||||||
char* data = new char[len];
|
|
||||||
int16_t state = CC1101::readData((uint8_t*)data, len);
|
|
||||||
|
|
||||||
// if packet was received successfully, copy data into String
|
|
||||||
if(state == ERR_NONE) {
|
|
||||||
str = String(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
delete[] data;
|
|
||||||
return(state);
|
|
||||||
}
|
|
||||||
|
|
||||||
int16_t CC1101::readData(uint8_t* data, size_t len) {
|
int16_t CC1101::readData(uint8_t* data, size_t len) {
|
||||||
// get packet length
|
// get packet length
|
||||||
size_t length = SPIreadRegister(CC1101_REG_RXBYTES) - 2;
|
size_t length = SPIreadRegister(CC1101_REG_RXBYTES) - 2;
|
||||||
|
@ -343,15 +248,13 @@ int16_t CC1101::readData(uint8_t* data, size_t len) {
|
||||||
_rawLQI = val & 0x7F;
|
_rawLQI = val & 0x7F;
|
||||||
|
|
||||||
// add terminating null
|
// add terminating null
|
||||||
if(len == 0) {
|
data[length] = 0;
|
||||||
data[length] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// flush Rx FIFO
|
// flush Rx FIFO
|
||||||
SPIsendCommand(CC1101_CMD_FLUSH_RX);
|
SPIsendCommand(CC1101_CMD_FLUSH_RX);
|
||||||
|
|
||||||
// set mode to receive
|
// set mode to standby
|
||||||
SPIsendCommand(CC1101_CMD_RX);
|
standby();
|
||||||
|
|
||||||
// check CRC
|
// check CRC
|
||||||
if((val & 0b10000000) == 0b00000000) {
|
if((val & 0b10000000) == 0b00000000) {
|
||||||
|
@ -361,7 +264,6 @@ int16_t CC1101::readData(uint8_t* data, size_t len) {
|
||||||
return(ERR_NONE);
|
return(ERR_NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int16_t CC1101::setFrequency(float freq) {
|
int16_t CC1101::setFrequency(float freq) {
|
||||||
// check allowed frequency range
|
// check allowed frequency range
|
||||||
if(!(((freq > 300.0) && (freq < 348.0)) ||
|
if(!(((freq > 300.0) && (freq < 348.0)) ||
|
||||||
|
@ -622,8 +524,6 @@ void CC1101::getExpMant(float target, uint16_t mantOffset, uint8_t divExp, uint8
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int16_t CC1101::SPIgetRegValue(uint8_t reg, uint8_t msb, uint8_t lsb) {
|
int16_t CC1101::SPIgetRegValue(uint8_t reg, uint8_t msb, uint8_t lsb) {
|
||||||
// status registers require special command
|
// status registers require special command
|
||||||
if(reg > CC1101_REG_TEST0) {
|
if(reg > CC1101_REG_TEST0) {
|
||||||
|
|
|
@ -499,6 +499,8 @@ class CC1101: public PhysicalLayer {
|
||||||
// introduce PhysicalLayer overloads
|
// introduce PhysicalLayer overloads
|
||||||
using PhysicalLayer::transmit;
|
using PhysicalLayer::transmit;
|
||||||
using PhysicalLayer::receive;
|
using PhysicalLayer::receive;
|
||||||
|
using PhysicalLayer::startTransmit;
|
||||||
|
using PhysicalLayer::readData;
|
||||||
|
|
||||||
// constructor
|
// constructor
|
||||||
CC1101(Module* module);
|
CC1101(Module* module);
|
||||||
|
@ -514,11 +516,8 @@ class CC1101: public PhysicalLayer {
|
||||||
// interrupt methods
|
// interrupt methods
|
||||||
void setGdo0Action(void (*func)(void), uint8_t dir = FALLING);
|
void setGdo0Action(void (*func)(void), uint8_t dir = FALLING);
|
||||||
void setGdo2Action(void (*func)(void), uint8_t dir = FALLING);
|
void setGdo2Action(void (*func)(void), uint8_t dir = FALLING);
|
||||||
int16_t startTransmit(String& str, uint8_t addr = 0);
|
|
||||||
int16_t startTransmit(const char* str, uint8_t addr = 0);
|
|
||||||
int16_t startTransmit(uint8_t* data, size_t len, uint8_t addr = 0);
|
int16_t startTransmit(uint8_t* data, size_t len, uint8_t addr = 0);
|
||||||
int16_t startReceive();
|
int16_t startReceive();
|
||||||
int16_t readData(String& str, size_t len = 0);
|
|
||||||
int16_t readData(uint8_t* data, size_t len);
|
int16_t readData(uint8_t* data, size_t len);
|
||||||
|
|
||||||
// configuration methods
|
// configuration methods
|
||||||
|
|
Loading…
Add table
Reference in a new issue