[SX126x] Added Module overrides for all Arduino core functions

This commit is contained in:
jgromes 2020-08-01 16:34:27 +02:00
parent 71045e829b
commit 882ec98563

View file

@ -159,7 +159,7 @@ int16_t SX126x::reset(bool verify) {
// run the reset sequence // run the reset sequence
Module::pinMode(_mod->getRst(), OUTPUT); Module::pinMode(_mod->getRst(), OUTPUT);
Module::digitalWrite(_mod->getRst(), LOW); Module::digitalWrite(_mod->getRst(), LOW);
delay(1); Module::delay(1);
Module::digitalWrite(_mod->getRst(), HIGH); Module::digitalWrite(_mod->getRst(), HIGH);
// return immediately when verification is disabled // return immediately when verification is disabled
@ -168,7 +168,7 @@ int16_t SX126x::reset(bool verify) {
} }
// set mode to standby - SX126x often refuses first few commands after reset // set mode to standby - SX126x often refuses first few commands after reset
uint32_t start = millis(); uint32_t start = Module::millis();
while(true) { while(true) {
// try to set mode to standby // try to set mode to standby
int16_t state = standby(); int16_t state = standby();
@ -178,13 +178,13 @@ int16_t SX126x::reset(bool verify) {
} }
// standby command failed, check timeout and try again // standby command failed, check timeout and try again
if(millis() - start >= 3000) { if(Module::millis() - start >= 3000) {
// timed out, possibly incorrect wiring // timed out, possibly incorrect wiring
return(state); return(state);
} }
// wait a bit to not spam the module // wait a bit to not spam the module
delay(10); Module::delay(10);
} }
} }
@ -223,16 +223,16 @@ int16_t SX126x::transmit(uint8_t* data, size_t len, uint8_t addr) {
RADIOLIB_ASSERT(state); RADIOLIB_ASSERT(state);
// wait for packet transmission or timeout // wait for packet transmission or timeout
uint32_t start = micros(); uint32_t start = Module::micros();
while(!digitalRead(_mod->getIrq())) { while(!Module::digitalRead(_mod->getIrq())) {
yield(); Module::yield();
if(micros() - start > timeout) { if(Module::micros() - start > timeout) {
clearIrqStatus(); clearIrqStatus();
standby(); standby();
return(ERR_TX_TIMEOUT); return(ERR_TX_TIMEOUT);
} }
} }
uint32_t elapsed = micros() - start; uint32_t elapsed = Module::micros() - start;
// update data rate // update data rate
_dataRate = (len*8.0)/((float)elapsed/1000000.0); _dataRate = (len*8.0)/((float)elapsed/1000000.0);
@ -283,10 +283,10 @@ int16_t SX126x::receive(uint8_t* data, size_t len) {
RADIOLIB_ASSERT(state); RADIOLIB_ASSERT(state);
// wait for packet reception or timeout // wait for packet reception or timeout
uint32_t start = micros(); uint32_t start = Module::micros();
while(!digitalRead(_mod->getIrq())) { while(!Module::digitalRead(_mod->getIrq())) {
yield(); Module::yield();
if(micros() - start > timeout) { if(Module::micros() - start > timeout) {
fixImplicitTimeout(); fixImplicitTimeout();
clearIrqStatus(); clearIrqStatus();
standby(); standby();
@ -354,8 +354,8 @@ int16_t SX126x::scanChannel() {
RADIOLIB_ASSERT(state); RADIOLIB_ASSERT(state);
// wait for channel activity detected or timeout // wait for channel activity detected or timeout
while(!digitalRead(_mod->getIrq())) { while(!Module::digitalRead(_mod->getIrq())) {
yield(); Module::yield();
} }
// check CAD result // check CAD result
@ -384,7 +384,7 @@ int16_t SX126x::sleep(bool retainConfig) {
int16_t state = SPIwriteCommand(SX126X_CMD_SET_SLEEP, &sleepMode, 1, false); int16_t state = SPIwriteCommand(SX126X_CMD_SET_SLEEP, &sleepMode, 1, false);
// wait for SX126x to safely enter sleep mode // wait for SX126x to safely enter sleep mode
delay(1); Module::delay(1);
return(state); return(state);
} }
@ -402,11 +402,11 @@ int16_t SX126x::standby(uint8_t mode) {
} }
void SX126x::setDio1Action(void (*func)(void)) { void SX126x::setDio1Action(void (*func)(void)) {
attachInterrupt(RADIOLIB_DIGITAL_PIN_TO_INTERRUPT(_mod->getIrq()), func, RISING); Module::attachInterrupt(RADIOLIB_DIGITAL_PIN_TO_INTERRUPT(_mod->getIrq()), func, RISING);
} }
void SX126x::clearDio1Action() { void SX126x::clearDio1Action() {
detachInterrupt(RADIOLIB_DIGITAL_PIN_TO_INTERRUPT(_mod->getIrq())); Module::detachInterrupt(RADIOLIB_DIGITAL_PIN_TO_INTERRUPT(_mod->getIrq()));
} }
int16_t SX126x::startTransmit(uint8_t* data, size_t len, uint8_t addr) { int16_t SX126x::startTransmit(uint8_t* data, size_t len, uint8_t addr) {
@ -463,8 +463,8 @@ int16_t SX126x::startTransmit(uint8_t* data, size_t len, uint8_t addr) {
RADIOLIB_ASSERT(state); RADIOLIB_ASSERT(state);
// wait for BUSY to go low (= PA ramp up done) // wait for BUSY to go low (= PA ramp up done)
while(digitalRead(_mod->getGpio())) { while(Module::digitalRead(_mod->getGpio())) {
yield(); Module::yield();
} }
return(state); return(state);
@ -1531,9 +1531,9 @@ int16_t SX126x::config(uint8_t modem) {
RADIOLIB_ASSERT(state); RADIOLIB_ASSERT(state);
// wait for calibration completion // wait for calibration completion
delay(5); Module::delay(5);
while(digitalRead(_mod->getGpio())) { while(Module::digitalRead(_mod->getGpio())) {
yield(); Module::yield();
} }
return(ERR_NONE); return(ERR_NONE);
@ -1565,14 +1565,14 @@ int16_t SX126x::SPItransfer(uint8_t* cmd, uint8_t cmdLen, bool write, uint8_t* d
#endif #endif
// pull NSS low // pull NSS low
digitalWrite(_mod->getCs(), LOW); Module::digitalWrite(_mod->getCs(), LOW);
// ensure BUSY is low (state machine ready) // ensure BUSY is low (state machine ready)
uint32_t start = millis(); uint32_t start = Module::millis();
while(digitalRead(_mod->getGpio())) { while(Module::digitalRead(_mod->getGpio())) {
yield(); Module::yield();
if(millis() - start >= timeout) { if(Module::millis() - start >= timeout) {
digitalWrite(_mod->getCs(), HIGH); Module::digitalWrite(_mod->getCs(), HIGH);
return(ERR_SPI_CMD_TIMEOUT); return(ERR_SPI_CMD_TIMEOUT);
} }
} }
@ -1632,15 +1632,15 @@ int16_t SX126x::SPItransfer(uint8_t* cmd, uint8_t cmdLen, bool write, uint8_t* d
// stop transfer // stop transfer
spi->endTransaction(); spi->endTransaction();
digitalWrite(_mod->getCs(), HIGH); Module::digitalWrite(_mod->getCs(), HIGH);
// wait for BUSY to go high and then low // wait for BUSY to go high and then low
if(waitForBusy) { if(waitForBusy) {
delayMicroseconds(1); Module::delayMicroseconds(1);
start = millis(); start = Module::millis();
while(digitalRead(_mod->getGpio())) { while(Module::digitalRead(_mod->getGpio())) {
yield(); Module::yield();
if(millis() - start >= timeout) { if(Module::millis() - start >= timeout) {
status = SX126X_STATUS_CMD_TIMEOUT; status = SX126X_STATUS_CMD_TIMEOUT;
break; break;
} }
@ -1690,7 +1690,7 @@ int16_t SX126x::SPItransfer(uint8_t* cmd, uint8_t cmdLen, bool write, uint8_t* d
// not sure why, but it seems that long enough SPI transaction // not sure why, but it seems that long enough SPI transaction
// (e.g. setPacketParams for GFSK) will fail without it // (e.g. setPacketParams for GFSK) will fail without it
#if defined(ARDUINO_ARCH_STM32) || defined(SAMD_SERIES) #if defined(ARDUINO_ARCH_STM32) || defined(SAMD_SERIES)
delay(1); Module::delay(1);
#endif #endif
#endif #endif