Improve hardware abstraction layer

This commit is contained in:
Mestery 2023-04-11 04:51:29 +02:00
parent 88549baf83
commit ec3d4eaf20
56 changed files with 1002 additions and 1853 deletions

View file

@ -49,7 +49,7 @@ void setup() {
// set the function that will be called
// when new packet is received
radio.setGdo0Action(setFlag);
radio.setGdo0Action(setFlag, RISING);
// start listening for packets
Serial.print(F("[CC1101] Starting to listen ... "));

View file

@ -51,7 +51,7 @@ void setup() {
// NOTE: Unlike other modules (such as SX127x),
// different GDOx pins are used for
// transmit and receive interrupts!
radio.setGdo2Action(setFlag);
radio.setGdo2Action(setFlag, FALLING);
// start transmitting the first packet
Serial.print(F("[CC1101] Sending first packet ... "));

View file

@ -32,7 +32,7 @@ STM32WLx radio = new STM32WLx_Module();
// set RF switch configuration for Nucleo WL55JC1
// NOTE: other boards may be different!
static const RADIOLIB_PIN_TYPE rfswitch_pins[] =
static const uint8_t rfswitch_pins[] =
{PC3, PC4, PC5};
static const Module::RfSwitchMode_t rfswitch_table[] = {
{STM32WLx::MODE_IDLE, {LOW, LOW, LOW}},

View file

@ -31,7 +31,7 @@ STM32WLx radio = new STM32WLx_Module();
// set RF switch configuration for Nucleo WL55JC1
// NOTE: other boards may be different!
static const RADIOLIB_PIN_TYPE rfswitch_pins[] =
static const uint8_t rfswitch_pins[] =
{PC3, PC4, PC5};
static const Module::RfSwitchMode_t rfswitch_table[] = {
{STM32WLx::MODE_IDLE, {LOW, LOW, LOW}},

View file

@ -28,7 +28,7 @@ STM32WLx radio = new STM32WLx_Module();
// set RF switch configuration for Nucleo WL55JC1
// NOTE: other boards may be different!
static const RADIOLIB_PIN_TYPE rfswitch_pins[] =
static const uint8_t rfswitch_pins[] =
{PC3, PC4, PC5};
static const Module::RfSwitchMode_t rfswitch_table[] = {
{STM32WLx::MODE_IDLE, {LOW, LOW, LOW}},

View file

@ -23,7 +23,7 @@ STM32WLx radio = new STM32WLx_Module();
// set RF switch configuration for Nucleo WL55JC1
// NOTE: other boards may be different!
static const RADIOLIB_PIN_TYPE rfswitch_pins[] =
static const uint8_t rfswitch_pins[] =
{PC3, PC4, PC5};
static const Module::RfSwitchMode_t rfswitch_table[] = {
{STM32WLx::MODE_IDLE, {LOW, LOW, LOW}},

View file

@ -47,11 +47,11 @@ void setup() {
// set the function that will be called
// when LoRa preamble is not detected within CAD timeout period
radio.setDio0Action(setFlagTimeout);
radio.setDio0Action(setFlagTimeout, RISING);
// set the function that will be called
// when LoRa preamble is detected
radio.setDio1Action(setFlagDetected);
radio.setDio1Action(setFlagDetected, RISING);
// start scanning the channel
Serial.print(F("[SX1278] Starting scan for LoRa preamble ... "));

View file

@ -53,11 +53,11 @@ void setup() {
// set the function that will be called
// when LoRa preamble is not detected within CAD timeout period
// or when a packet is received
radio.setDio0Action(setFlagTimeout);
radio.setDio0Action(setFlagTimeout, RISING);
// set the function that will be called
// when LoRa preamble is detected
radio.setDio1Action(setFlagDetected);
radio.setDio1Action(setFlagDetected, RISING);
// start scanning the channel
Serial.print(F("[SX1278] Starting scan for LoRa preamble ... "));

View file

@ -60,7 +60,7 @@ void setup() {
// set the function that will be called
// when new packet is received
radio.setDio0Action(setFlag);
radio.setDio0Action(setFlag, RISING);
#if defined(INITIATING_NODE)
// send the first packet on this node

View file

@ -96,10 +96,10 @@ void setup() {
}
// set the function to call when reception is finished
radio.setDio0Action(setRxFlag);
radio.setDio0Action(setRxFlag, RISING);
// set the function to call when we need to change frequency
radio.setDio1Action(setFHSSFlag);
radio.setDio1Action(setFHSSFlag, RISING);
// start listening for LoRa packets
Serial.print(F("[SX1278] Starting to listen ... "));

View file

@ -51,7 +51,7 @@ void setup() {
// set the function that will be called
// when new packet is received
radio.setDio0Action(setFlag);
radio.setDio0Action(setFlag, RISING);
// start listening for LoRa packets
Serial.print(F("[SX1278] Starting to listen ... "));

View file

@ -108,10 +108,10 @@ void setup() {
}
// set the function to call when transmission is finished
radio.setDio0Action(setTxFlag);
radio.setDio0Action(setTxFlag, RISING);
// set the function to call when we need to change frequency
radio.setDio1Action(setFHSSFlag);
radio.setDio1Action(setFHSSFlag, RISING);
// start transmitting the first packet
Serial.print(F("[SX1278] Sending first packet ... "));

View file

@ -50,7 +50,7 @@ void setup() {
// set the function that will be called
// when packet transmission is finished
radio.setDio0Action(setFlag);
radio.setDio0Action(setFlag, RISING);
// start transmitting the first packet
Serial.print(F("[SX1278] Sending first packet ... "));

146
src/ArduinoHal.cpp Normal file
View file

@ -0,0 +1,146 @@
#include "ArduinoHal.h"
#if defined(RADIOLIB_BUILD_ARDUINO)
ArduinoHal::ArduinoHal(SPIClass& spi, SPISettings spiSettings): Hal(INPUT, OUTPUT, LOW, HIGH, RISING, FALLING), _spi(&spi), _spiSettings(spiSettings) {}
ArduinoHal::ArduinoHal(): Hal(INPUT, OUTPUT, LOW, HIGH, RISING, FALLING), _spi(&RADIOLIB_DEFAULT_SPI), _initInterface(true) {}
void ArduinoHal::init() {
if(_initInterface) {
spiBegin();
}
}
void ArduinoHal::term() {
if(_initInterface) {
spiEnd();
}
}
void inline ArduinoHal::pinMode(uint8_t pin, uint8_t mode) {
if (pin == RADIOLIB_NC) {
return;
}
::pinMode(pin, RADIOLIB_ARDUINOHAL_PIN_MODE_CAST mode);
}
void inline ArduinoHal::digitalWrite(uint8_t pin, uint8_t value) {
if (pin == RADIOLIB_NC) {
return;
}
::digitalWrite(pin, RADIOLIB_ARDUINOHAL_PIN_STATUS_CAST value);
}
uint8_t inline ArduinoHal::digitalRead(uint8_t pin) {
if (pin == RADIOLIB_NC) {
return 0;
}
return ::digitalRead(pin);
}
void inline ArduinoHal::attachInterrupt(uint8_t interruptNum, void (*interruptCb)(void), uint8_t mode) {
if (interruptNum == RADIOLIB_NC) {
return;
}
::attachInterrupt(interruptNum, interruptCb, RADIOLIB_ARDUINOHAL_INTERRUPT_MODE_CAST mode);
}
void inline ArduinoHal::detachInterrupt(uint8_t interruptNum) {
if (interruptNum == RADIOLIB_NC) {
return;
}
::detachInterrupt(interruptNum);
}
void inline ArduinoHal::delay(unsigned long ms) {
::delay(ms);
}
void inline ArduinoHal::delayMicroseconds(unsigned long us) {
::delayMicroseconds(us);
}
unsigned long inline ArduinoHal::millis() {
return ::millis();
}
unsigned long inline ArduinoHal::micros() {
return ::micros();
}
long inline ArduinoHal::pulseIn(uint8_t pin, uint8_t state, unsigned long timeout) {
if (pin == RADIOLIB_NC) {
return 0;
}
return ::pulseIn(pin, state, timeout);
}
void inline ArduinoHal::spiBegin() {
_spi->begin();
}
void inline ArduinoHal::spiBeginTransaction() {
_spi->beginTransaction(_spiSettings);
}
uint8_t inline ArduinoHal::spiTransfer(uint8_t b) {
return _spi->transfer(b);
}
void inline ArduinoHal::spiEndTransaction() {
_spi->endTransaction();
}
void inline ArduinoHal::spiEnd() {
_spi->end();
}
void inline ArduinoHal::tone(uint8_t pin, unsigned int frequency, unsigned long duration) {
#if !defined(RADIOLIB_TONE_UNSUPPORTED)
if (pin == RADIOLIB_NC) {
return 0;
}
::tone(pin, frequency, duration);
#elif defined(ESP32)
// ESP32 tone() emulation
(void)duration;
if(_prev == -1) {
ledcAttachPin(pin, RADIOLIB_TONE_ESP32_CHANNEL);
}
if(_prev != frequency) {
ledcWriteTone(RADIOLIB_TONE_ESP32_CHANNEL, frequency);
}
_prev = frequency;
#elif defined(RADIOLIB_MBED_TONE_OVERRIDE)
// better tone for mbed OS boards
(void)duration;
if(!pwmPin) {
pwmPin = new mbed::PwmOut(digitalPinToPinName(pin));
}
pwmPin->period(1.0 / frequency);
pwmPin->write(0.5);
#endif
}
void inline ArduinoHal::noTone(uint8_t pin) {
#if !defined(RADIOLIB_TONE_UNSUPPORTED) and defined(ARDUINO_ARCH_STM32)
if (pin == RADIOLIB_NC) {
return 0;
}
::noTone(pin, false);
#elif !defined(RADIOLIB_TONE_UNSUPPORTED)
if (pin == RADIOLIB_NC) {
return 0;
}
::noTone(pin);
#elif defined(ESP32)
if (pin == RADIOLIB_NC) {
return 0;
}
// ESP32 tone() emulation
ledcDetachPin(pin);
ledcWrite(RADIOLIB_TONE_ESP32_CHANNEL, 0);
_prev = -1;
#elif defined(RADIOLIB_MBED_TONE_OVERRIDE)
if (pin == RADIOLIB_NC) {
return 0;
}
// better tone for mbed OS boards
(void)pin;
pwmPin->suspend();
#endif
}
void inline ArduinoHal::yield() {
#if !defined(RADIOLIB_YIELD_UNSUPPORTED)
::yield();
#endif
}
uint8_t inline ArduinoHal::pinToInterrupt(uint8_t pin) {
return digitalPinToInterrupt(pin);
}
#endif

62
src/ArduinoHal.h Normal file
View file

@ -0,0 +1,62 @@
#include "TypeDef.h"
#if !defined(_RADIOLIB_ARDUINOHAL_H)
#define _RADIOLIB_ARDUINOHAL_H
#if defined(RADIOLIB_BUILD_ARDUINO)
#if defined(RADIOLIB_MBED_TONE_OVERRIDE)
#include "mbed.h"
#endif
#include "Hal.h"
#include <stdint.h>
#include <SPI.h>
class ArduinoHal : public Hal {
public:
ArduinoHal();
ArduinoHal(SPIClass& spi, SPISettings spiSettings = RADIOLIB_DEFAULT_SPI_SETTINGS);
void init() override;
void term() override;
void pinMode(uint8_t pin, uint8_t mode) override;
void digitalWrite(uint8_t pin, uint8_t value) override;
uint8_t digitalRead(uint8_t pin) override;
void attachInterrupt(uint8_t interruptNum, void (*interruptCb)(void), uint8_t mode) override;
void detachInterrupt(uint8_t interruptNum) override;
void delay(unsigned long ms) override;
void delayMicroseconds(unsigned long us) override;
unsigned long millis() override;
unsigned long micros() override;
long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout) override;
void spiBegin() override;
void spiBeginTransaction() override;
uint8_t spiTransfer(uint8_t b) override;
void spiEndTransaction() override;
void spiEnd() override;
void tone(uint8_t pin, unsigned int frequency, unsigned long duration = 0) override;
void noTone(uint8_t pin) override;
void yield() override;
uint8_t pinToInterrupt(uint8_t pin) override;
#if !defined(RADIOLIB_GODMODE)
private:
#endif
SPIClass* _spi = NULL;
SPISettings _spiSettings = RADIOLIB_DEFAULT_SPI_SETTINGS;
bool _initInterface = false;
#if defined(RADIOLIB_MBED_TONE_OVERRIDE)
mbed::PwmOut *pwmPin = NULL;
#endif
#if defined(ESP32)
int32_t _prev = -1;
#endif
};
#endif
#endif

File diff suppressed because it is too large Load diff

17
src/Hal.cpp Normal file
View file

@ -0,0 +1,17 @@
#include <stdint.h>
#include "Hal.h"
Hal::Hal(const uint8_t input, const uint8_t output, const uint8_t low, const uint8_t high, const uint8_t rising, const uint8_t falling)
: GpioModeInput(input),
GpioModeOutput(output),
GpioLevelLow(high),
GpioLevelHigh(low),
GpioInterruptRising(rising),
GpioInterruptFalling(falling) {}
void Hal::init(){};
void Hal::term(){};
void Hal::tone(uint8_t pin, unsigned int frequency, unsigned long duration){};
void Hal::noTone(uint8_t pin){};
void Hal::yield(){};
uint8_t Hal::pinToInterrupt(uint8_t pin) { return pin; };

42
src/Hal.h Normal file
View file

@ -0,0 +1,42 @@
#include <stdint.h>
#include <stddef.h>
#if !defined(_RADIOLIB_HAL_H)
#define _RADIOLIB_HAL_H
class Hal {
public:
const uint8_t GpioModeInput;
const uint8_t GpioModeOutput;
const uint8_t GpioLevelLow;
const uint8_t GpioLevelHigh;
const uint8_t GpioInterruptRising;
const uint8_t GpioInterruptFalling;
Hal(const uint8_t input, const uint8_t output, const uint8_t low, const uint8_t high, const uint8_t rising, const uint8_t falling);
virtual void init();
virtual void term();
virtual void pinMode(uint8_t pin, uint8_t mode) = 0;
virtual void digitalWrite(uint8_t pin, uint8_t value) = 0;
virtual uint8_t digitalRead(uint8_t pin) = 0;
virtual void attachInterrupt(uint8_t interruptNum, void (*interruptCb)(void), uint8_t mode) = 0;
virtual void detachInterrupt(uint8_t interruptNum) = 0;
virtual void delay(unsigned long ms) = 0;
virtual void delayMicroseconds(unsigned long us) = 0;
virtual unsigned long millis() = 0;
virtual unsigned long micros() = 0;
virtual long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout) = 0;
virtual void spiBegin() = 0;
virtual void spiBeginTransaction() = 0;
virtual uint8_t spiTransfer(uint8_t b) = 0;
virtual void spiEndTransaction() = 0;
virtual void spiEnd() = 0;
virtual void tone(uint8_t pin, unsigned int frequency, unsigned long duration = 0);
virtual void noTone(uint8_t pin);
virtual void yield();
virtual uint8_t pinToInterrupt(uint8_t pin);
};
#endif

View file

@ -1,94 +1,24 @@
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include "Module.h"
#if defined(RADIOLIB_BUILD_ARDUINO)
#include "ArduinoHal.h"
Module::Module(uint8_t cs, uint8_t irq, uint8_t rst, uint8_t gpio) : _cs(cs), _irq(irq), _rst(rst), _gpio(gpio) {
this->hal = new ArduinoHal;
}
// we need this to emulate tone() on mbed Arduino boards
#if defined(RADIOLIB_MBED_TONE_OVERRIDE)
#include "mbed.h"
mbed::PwmOut *pwmPin = NULL;
Module::Module(uint8_t cs, uint8_t irq, uint8_t rst, uint8_t gpio, SPIClass& spi, SPISettings spiSettings) : _cs(cs), _irq(irq), _rst(rst), _gpio(gpio) {
this->hal = new ArduinoHal(spi, spiSettings);
}
#endif
Module::Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE gpio):
_cs(cs),
_irq(irq),
_rst(rst),
_gpio(gpio)
{
_spi = &RADIOLIB_DEFAULT_SPI;
_initInterface = true;
// this is Arduino build, pre-set callbacks
setCb_pinMode(::pinMode);
setCb_digitalRead(::digitalRead);
setCb_digitalWrite(::digitalWrite);
#if !defined(RADIOLIB_TONE_UNSUPPORTED)
setCb_tone(::tone);
setCb_noTone(::noTone);
#endif
setCb_attachInterrupt(::attachInterrupt);
setCb_detachInterrupt(::detachInterrupt);
#if !defined(RADIOLIB_YIELD_UNSUPPORTED)
setCb_yield(::yield);
#endif
setCb_delay(::delay);
setCb_delayMicroseconds(::delayMicroseconds);
setCb_millis(::millis);
setCb_micros(::micros);
setCb_pulseIn(::pulseIn);
setCb_SPIbegin(&Module::SPIbegin);
setCb_SPIbeginTransaction(&Module::SPIbeginTransaction);
setCb_SPItransfer(&Module::SPItransfer);
setCb_SPIendTransaction(&Module::SPIendTransaction);
setCb_SPIend(&Module::SPIend);
Module::Module(Hal *hal, uint8_t cs, uint8_t irq, uint8_t rst, uint8_t gpio) : _cs(cs), _irq(irq), _rst(rst), _gpio(gpio) {
this->hal = hal;
}
Module::Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE gpio, SPIClass& spi, SPISettings spiSettings):
_cs(cs),
_irq(irq),
_rst(rst),
_gpio(gpio),
_spiSettings(spiSettings)
{
_spi = &spi;
_initInterface = false;
// this is Arduino build, pre-set callbacks
setCb_pinMode(::pinMode);
setCb_digitalRead(::digitalRead);
setCb_digitalWrite(::digitalWrite);
#if !defined(RADIOLIB_TONE_UNSUPPORTED)
setCb_tone(::tone);
setCb_noTone(::noTone);
#endif
setCb_attachInterrupt(::attachInterrupt);
setCb_detachInterrupt(::detachInterrupt);
#if !defined(RADIOLIB_YIELD_UNSUPPORTED)
setCb_yield(::yield);
#endif
setCb_delay(::delay);
setCb_delayMicroseconds(::delayMicroseconds);
setCb_millis(::millis);
setCb_micros(::micros);
setCb_pulseIn(::pulseIn);
setCb_SPIbegin(&Module::SPIbegin);
setCb_SPIbeginTransaction(&Module::SPIbeginTransaction);
setCb_SPItransfer(&Module::SPItransfer);
setCb_SPIendTransaction(&Module::SPIendTransaction);
setCb_SPIend(&Module::SPIend);
}
#else
Module::Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE gpio):
_cs(cs),
_irq(irq),
_rst(rst),
_gpio(gpio)
{
// not an Arduino build, it's up to the user to set all callbacks
}
#endif
Module::Module(const Module& mod) {
*this = mod;
}
@ -96,35 +26,23 @@ Module::Module(const Module& mod) {
Module& Module::operator=(const Module& mod) {
this->SPIreadCommand = mod.SPIreadCommand;
this->SPIwriteCommand = mod.SPIwriteCommand;
this->_cs = mod.getCs();
this->_irq = mod.getIrq();
this->_rst = mod.getRst();
this->_gpio = mod.getGpio();
this->_cs = mod._cs;
this->_irq = mod._irq;
this->_rst = mod._rst;
this->_gpio = mod._gpio;
return(*this);
}
void Module::init() {
this->pinMode(_cs, OUTPUT);
this->digitalWrite(_cs, HIGH);
#if defined(RADIOLIB_BUILD_ARDUINO)
if(_initInterface) {
(this->*cb_SPIbegin)();
}
#endif
this->hal->init();
this->hal->pinMode(_cs, this->hal->GpioModeOutput);
this->hal->digitalWrite(_cs, this->hal->GpioLevelHigh);
}
void Module::term() {
// stop hardware interfaces (if they were initialized by the library)
#if defined(RADIOLIB_BUILD_ARDUINO)
if(!_initInterface) {
return;
}
if(_spi != nullptr) {
this->SPIend();
}
#endif
this->hal->term();
}
int16_t Module::SPIgetRegValue(uint16_t reg, uint8_t msb, uint8_t lsb) {
@ -150,9 +68,9 @@ int16_t Module::SPIsetRegValue(uint16_t reg, uint8_t value, uint8_t msb, uint8_t
#if defined(RADIOLIB_SPI_PARANOID)
// check register value each millisecond until check interval is reached
// some registers need a bit of time to process the change (e.g. SX127X_REG_OP_MODE)
uint32_t start = this->micros();
uint32_t start = this->hal->micros();
uint8_t readValue = 0x00;
while(this->micros() - start < (checkInterval * 1000)) {
while(this->hal->micros() - start < (checkInterval * 1000)) {
readValue = SPIreadRegister(reg);
if((readValue & checkMask) == (newValue & checkMask)) {
// check passed, we can stop the loop
@ -216,17 +134,17 @@ void Module::SPIwriteRegister(uint16_t reg, uint8_t data) {
void Module::SPItransfer(uint8_t cmd, uint16_t reg, uint8_t* dataOut, uint8_t* dataIn, size_t numBytes) {
// start SPI transaction
this->beginTransaction();
this->hal->spiBeginTransaction();
// pull CS low
this->digitalWrite(_cs, LOW);
this->hal->digitalWrite(this->_cs, this->hal->GpioLevelLow);
// send SPI register address with access command
if(this->SPIaddrWidth <= 8) {
this->transfer(reg | cmd);
this->hal->spiTransfer(reg | cmd);
} else {
this->transfer((reg >> 8) | cmd);
this->transfer(reg & 0xFF);
this->hal->spiTransfer((reg >> 8) | cmd);
this->hal->spiTransfer(reg & 0xFF);
}
#if defined(RADIOLIB_VERBOSE)
@ -242,14 +160,14 @@ void Module::SPItransfer(uint8_t cmd, uint16_t reg, uint8_t* dataOut, uint8_t* d
if(cmd == SPIwriteCommand) {
if(dataOut != NULL) {
for(size_t n = 0; n < numBytes; n++) {
this->transfer(dataOut[n]);
this->hal->spiTransfer(dataOut[n]);
RADIOLIB_VERBOSE_PRINT("%X\t", dataOut[n]);
}
}
} else if (cmd == SPIreadCommand) {
if(dataIn != NULL) {
for(size_t n = 0; n < numBytes; n++) {
dataIn[n] = this->transfer(0x00);
dataIn[n] = this->hal->spiTransfer(0x00);
RADIOLIB_VERBOSE_PRINT("%X\t", dataIn[n]);
}
}
@ -257,10 +175,10 @@ void Module::SPItransfer(uint8_t cmd, uint16_t reg, uint8_t* dataOut, uint8_t* d
RADIOLIB_VERBOSE_PRINTLN();
// release CS
this->digitalWrite(_cs, HIGH);
this->hal->digitalWrite(this->_cs, this->hal->GpioLevelHigh);
// end SPI transaction
this->endTransaction();
this->hal->spiEndTransaction();
}
int16_t Module::SPIreadStream(uint8_t cmd, uint8_t* data, size_t numBytes, bool waitForGpio, bool verify) {
@ -311,7 +229,6 @@ int16_t Module::SPIcheckStream() {
if(this->SPIparseStatusCb != nullptr) {
this->SPIstreamError = this->SPIparseStatusCb(spiStatus);
}
#endif
return(state);
@ -323,24 +240,24 @@ int16_t Module::SPItransferStream(uint8_t* cmd, uint8_t cmdLen, bool write, uint
#endif
// ensure GPIO is low
uint32_t start = this->millis();
while(this->digitalRead(this->getGpio())) {
this->yield();
if(this->millis() - start >= timeout) {
this->digitalWrite(this->getCs(), HIGH);
uint32_t start = this->hal->millis();
while(this->hal->digitalRead(this->_gpio)) {
this->hal->yield();
if(this->hal->millis() - start >= timeout) {
this->hal->digitalWrite(this->_cs, this->hal->GpioLevelLow);
return(RADIOLIB_ERR_SPI_CMD_TIMEOUT);
}
}
// pull NSS low
this->digitalWrite(this->getCs(), LOW);
this->hal->digitalWrite(this->_cs, this->hal->GpioLevelLow);
// start transfer
this->beginTransaction();
this->hal->spiBeginTransaction();
// send command byte(s)
for(uint8_t n = 0; n < cmdLen; n++) {
this->transfer(cmd[n]);
this->hal->spiTransfer(cmd[n]);
}
// variable to save error during SPI transfer
@ -350,7 +267,7 @@ int16_t Module::SPItransferStream(uint8_t* cmd, uint8_t cmdLen, bool write, uint
if(write) {
for(size_t n = 0; n < numBytes; n++) {
// send byte
uint8_t in = this->transfer(dataOut[n]);
uint8_t in = this->hal->spiTransfer(dataOut[n]);
#if defined(RADIOLIB_VERBOSE)
debugBuff[n] = in;
#endif
@ -363,7 +280,7 @@ int16_t Module::SPItransferStream(uint8_t* cmd, uint8_t cmdLen, bool write, uint
} else {
// skip the first byte for read-type commands (status-only)
uint8_t in = this->transfer(this->SPInopCommand);
uint8_t in = this->hal->spiTransfer(this->SPInopCommand);
#if defined(RADIOLIB_VERBOSE)
debugBuff[0] = in;
#endif
@ -378,22 +295,22 @@ int16_t Module::SPItransferStream(uint8_t* cmd, uint8_t cmdLen, bool write, uint
// read the data
if(state == RADIOLIB_ERR_NONE) {
for(size_t n = 0; n < numBytes; n++) {
dataIn[n] = this->transfer(this->SPInopCommand);
dataIn[n] = this->hal->spiTransfer(this->SPInopCommand);
}
}
}
// stop transfer
this->endTransaction();
this->digitalWrite(this->getCs(), HIGH);
this->hal->spiEndTransaction();
this->hal->digitalWrite(this->_cs, this->hal->GpioLevelHigh);
// wait for GPIO to go high and then low
if(waitForGpio) {
this->delayMicroseconds(1);
uint32_t start = this->millis();
while(this->digitalRead(this->getGpio())) {
this->yield();
if(this->millis() - start >= timeout) {
this->hal->delayMicroseconds(1);
uint32_t start = this->hal->millis();
while(this->hal->digitalRead(this->_gpio)) {
this->hal->yield();
if(this->hal->millis() - start >= timeout) {
state = RADIOLIB_ERR_SPI_CMD_TIMEOUT;
break;
}
@ -440,238 +357,15 @@ void Module::waitForMicroseconds(uint32_t start, uint32_t len) {
}
this->TimerFlag = false;
while(!this->TimerFlag) {
this->yield();
this->hal->yield();
}
#else
while(this->micros() - start < len) {
this->yield();
while(this->hal->micros() - start < len) {
this->hal->yield();
}
#endif
}
void Module::pinMode(RADIOLIB_PIN_TYPE pin, RADIOLIB_PIN_MODE mode) {
if((pin == RADIOLIB_NC) || (cb_pinMode == nullptr)) {
return;
}
cb_pinMode(pin, mode);
}
void Module::digitalWrite(RADIOLIB_PIN_TYPE pin, RADIOLIB_PIN_STATUS value) {
if((pin == RADIOLIB_NC) || (cb_digitalWrite == nullptr)) {
return;
}
cb_digitalWrite(pin, value);
}
RADIOLIB_PIN_STATUS Module::digitalRead(RADIOLIB_PIN_TYPE pin) {
if((pin == RADIOLIB_NC) || (cb_digitalRead == nullptr)) {
return((RADIOLIB_PIN_STATUS)0);
}
return(cb_digitalRead(pin));
}
#if defined(ESP32)
// we need to cache the previous tone value for emulation on ESP32
int32_t prev = -1;
#endif
void Module::tone(RADIOLIB_PIN_TYPE pin, uint16_t value, uint32_t duration) {
#if !defined(RADIOLIB_TONE_UNSUPPORTED)
if((pin == RADIOLIB_NC) || (cb_tone == nullptr)) {
return;
}
cb_tone(pin, value, duration);
#else
if(pin == RADIOLIB_NC) {
return;
}
#if defined(ESP32)
// ESP32 tone() emulation
(void)duration;
if(prev == -1) {
ledcAttachPin(pin, RADIOLIB_TONE_ESP32_CHANNEL);
}
if(prev != value) {
ledcWriteTone(RADIOLIB_TONE_ESP32_CHANNEL, value);
}
prev = value;
#elif defined(RADIOLIB_MBED_TONE_OVERRIDE)
// better tone for mbed OS boards
(void)duration;
if(!pwmPin) {
pwmPin = new mbed::PwmOut(digitalPinToPinName(pin));
}
pwmPin->period(1.0 / value);
pwmPin->write(0.5);
#else
(void)value;
(void)duration;
#endif
#endif
}
void Module::noTone(RADIOLIB_PIN_TYPE pin) {
#if !defined(RADIOLIB_TONE_UNSUPPORTED)
if((pin == RADIOLIB_NC) || (cb_noTone == nullptr)) {
return;
}
#if defined(ARDUINO_ARCH_STM32)
cb_noTone(pin, false);
#else
cb_noTone(pin);
#endif
#else
if(pin == RADIOLIB_NC) {
return;
}
#if defined(ESP32)
// ESP32 tone() emulation
ledcDetachPin(pin);
ledcWrite(RADIOLIB_TONE_ESP32_CHANNEL, 0);
prev = -1;
#elif defined(RADIOLIB_MBED_TONE_OVERRIDE)
// better tone for mbed OS boards
(void)pin;
pwmPin->suspend();
#endif
#endif
}
void Module::attachInterrupt(RADIOLIB_PIN_TYPE interruptNum, void (*userFunc)(void), RADIOLIB_INTERRUPT_STATUS mode) {
if((interruptNum == RADIOLIB_NC) || (cb_attachInterrupt == nullptr)) {
return;
}
cb_attachInterrupt(interruptNum, userFunc, mode);
}
void Module::detachInterrupt(RADIOLIB_PIN_TYPE interruptNum) {
if((interruptNum == RADIOLIB_NC) || (cb_detachInterrupt == nullptr)) {
return;
}
cb_detachInterrupt(interruptNum);
}
void Module::yield() {
if(cb_yield == nullptr) {
return;
}
#if !defined(RADIOLIB_YIELD_UNSUPPORTED)
cb_yield();
#endif
}
void Module::delay(uint32_t ms) {
if(cb_delay == nullptr) {
return;
}
cb_delay(ms);
}
void Module::delayMicroseconds(uint32_t us) {
if(cb_delayMicroseconds == nullptr) {
return;
}
cb_delayMicroseconds(us);
}
uint32_t Module::millis() {
if(cb_millis == nullptr) {
return(0);
}
return(cb_millis());
}
uint32_t Module::micros() {
if(cb_micros == nullptr) {
return(0);
}
return(cb_micros());
}
uint32_t Module::pulseIn(RADIOLIB_PIN_TYPE pin, RADIOLIB_PIN_STATUS state, uint32_t timeout) {
if(cb_pulseIn == nullptr) {
return(0);
}
return(cb_pulseIn(pin, state, timeout));
}
void Module::begin() {
if(cb_SPIbegin == nullptr) {
return;
}
#if defined(RADIOLIB_BUILD_ARDUINO)
(this->*cb_SPIbegin)();
#else
cb_SPIbegin();
#endif
}
void Module::beginTransaction() {
if(cb_SPIbeginTransaction == nullptr) {
return;
}
#if defined(RADIOLIB_BUILD_ARDUINO)
(this->*cb_SPIbeginTransaction)();
#else
cb_SPIbeginTransaction();
#endif
}
uint8_t Module::transfer(uint8_t b) {
if(cb_SPItransfer == nullptr) {
return(0xFF);
}
#if defined(RADIOLIB_BUILD_ARDUINO)
return((this->*cb_SPItransfer)(b));
#else
return(cb_SPItransfer(b));
#endif
}
void Module::endTransaction() {
if(cb_SPIendTransaction == nullptr) {
return;
}
#if defined(RADIOLIB_BUILD_ARDUINO)
(this->*cb_SPIendTransaction)();
#else
cb_SPIendTransaction();
#endif
}
void Module::end() {
if(cb_SPIend == nullptr) {
return;
}
#if defined(RADIOLIB_BUILD_ARDUINO)
(this->*cb_SPIend)();
#else
cb_SPIend();
#endif
}
#if defined(RADIOLIB_BUILD_ARDUINO)
void Module::SPIbegin() {
_spi->begin();
}
void Module::SPIbeginTransaction() {
_spi->beginTransaction(_spiSettings);
}
uint8_t Module::SPItransfer(uint8_t b) {
return(_spi->transfer(b));
}
void Module::SPIendTransaction() {
_spi->endTransaction();
}
void Module::SPIend() {
_spi->end();
}
#endif
uint8_t Module::flipBits(uint8_t b) {
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
@ -771,26 +465,26 @@ size_t Module::serialPrintf(const char* format, ...) {
}
#endif
void Module::setRfSwitchPins(RADIOLIB_PIN_TYPE rxEn, RADIOLIB_PIN_TYPE txEn) {
void Module::setRfSwitchPins(uint8_t rxEn, uint8_t txEn) {
// This can be on the stack, setRfSwitchTable copies the contents
const RADIOLIB_PIN_TYPE pins[] = {
const uint8_t pins[] = {
rxEn, txEn, RADIOLIB_NC,
};
// This must be static, since setRfSwitchTable stores a reference.
static constexpr RfSwitchMode_t table[] = {
{MODE_IDLE, {LOW, LOW}},
{MODE_RX, {HIGH, LOW}},
{MODE_TX, {LOW, HIGH}},
static const RfSwitchMode_t table[] = {
{MODE_IDLE, {this->hal->GpioLevelLow, this->hal->GpioLevelLow}},
{MODE_RX, {this->hal->GpioLevelHigh, this->hal->GpioLevelLow}},
{MODE_TX, {this->hal->GpioLevelLow, this->hal->GpioLevelHigh}},
END_OF_MODE_TABLE,
};
setRfSwitchTable(pins, table);
}
void Module::setRfSwitchTable(const RADIOLIB_PIN_TYPE (&pins)[3], const RfSwitchMode_t table[]) {
void Module::setRfSwitchTable(const uint8_t (&pins)[3], const RfSwitchMode_t table[]) {
memcpy(_rfSwitchPins, pins, sizeof(_rfSwitchPins));
_rfSwitchTable = table;
for(size_t i = 0; i < RFSWITCH_MAX_PINS; i++)
this->pinMode(pins[i], OUTPUT);
this->hal->pinMode(pins[i], this->hal->GpioModeOutput);
}
const Module::RfSwitchMode_t *Module::findRfSwitchMode(uint8_t mode) const {
@ -811,11 +505,11 @@ void Module::setRfSwitchState(uint8_t mode) {
}
// set pins
const RADIOLIB_PIN_STATUS *value = &row->values[0];
const uint8_t *value = &row->values[0];
for(size_t i = 0; i < RFSWITCH_MAX_PINS; i++) {
RADIOLIB_PIN_TYPE pin = _rfSwitchPins[i];
uint8_t pin = _rfSwitchPins[i];
if (pin != RADIOLIB_NC)
this->digitalWrite(pin, *value);
this->hal->digitalWrite(pin, *value);
++value;
}
}

View file

@ -1,8 +1,8 @@
#include "TypeDef.h"
#include "Hal.h"
#if !defined(_RADIOLIB_MODULE_H)
#define _RADIOLIB_MODULE_H
#include "TypeDef.h"
#if defined(RADIOLIB_BUILD_ARDUINO)
#include <SPI.h>
#endif
@ -45,7 +45,7 @@ class Module {
*/
struct RfSwitchMode_t {
uint8_t mode;
RADIOLIB_PIN_STATUS values[RFSWITCH_MAX_PINS];
uint8_t values[RFSWITCH_MAX_PINS];
};
/*!
@ -69,9 +69,8 @@ class Module {
};
#if defined(RADIOLIB_BUILD_ARDUINO)
/*!
\brief Arduino Module constructor. Will use the default SPI interface and automatically initialize it
\brief Arduino Module constructor. Will use the default SPI interface and automatically initialize it.
\param cs Arduino pin to be used as chip select.
@ -81,7 +80,7 @@ class Module {
\param gpio Arduino pin to be used as additional interrupt/GPIO.
*/
Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE gpio = RADIOLIB_NC);
Module(uint8_t cs, uint8_t irq, uint8_t rst, uint8_t gpio = RADIOLIB_NC);
/*!
\brief Arduino Module constructor. Will not attempt SPI interface initialization.
@ -98,24 +97,23 @@ class Module {
\param spiSettings SPI interface settings.
*/
Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE gpio, SPIClass& spi, SPISettings spiSettings = RADIOLIB_DEFAULT_SPI_SETTINGS);
#else
Module(uint8_t cs, uint8_t irq, uint8_t rst, uint8_t gpio, SPIClass& spi, SPISettings spiSettings = RADIOLIB_DEFAULT_SPI_SETTINGS);
#endif
/*!
\brief Default constructor.
\brief Module constructor.
\param cs Pin to be used as chip select.
\param hal A Hardware abstraction layer instance. An ArduinoHal instance for example.
\param irq Pin to be used as interrupt/GPIO.
\param cs Arduino pin to be used as chip select.
\param rst Pin to be used as hardware reset for the module.
\param irq Arduino pin to be used as interrupt/GPIO.
\param gpio Pin to be used as additional interrupt/GPIO.
\param rst Arduino pin to be used as hardware reset for the module.
\param gpio Arduino pin to be used as additional interrupt/GPIO.
*/
Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE gpio = RADIOLIB_NC);
#endif
Module(Hal *hal, uint8_t cs, uint8_t irq, uint8_t rst, uint8_t gpio = RADIOLIB_NC);
/*!
\brief Copy constructor.
@ -133,6 +131,8 @@ class Module {
// public member variables
Hal* hal = NULL;
/*!
\brief Basic SPI read command. Defaults to 0x00.
*/
@ -409,28 +409,28 @@ class Module {
\returns Pin number of SPI chip select configured in the constructor.
*/
RADIOLIB_PIN_TYPE getCs() const { return(_cs); }
uint8_t getCs() const { return(_cs); }
/*!
\brief Access method to get the pin number of interrupt/GPIO.
\returns Pin number of interrupt/GPIO configured in the constructor.
*/
RADIOLIB_PIN_TYPE getIrq() const { return(_irq); }
uint8_t getIrq() const { return(_irq); }
/*!
\brief Access method to get the pin number of hardware reset pin.
\returns Pin number of hardware reset pin configured in the constructor.
*/
RADIOLIB_PIN_TYPE getRst() const { return(_rst); }
uint8_t getRst() const { return(_rst); }
/*!
\brief Access method to get the pin number of second interrupt/GPIO.
\returns Pin number of second interrupt/GPIO configured in the constructor.
*/
RADIOLIB_PIN_TYPE getGpio() const { return(_gpio); }
uint8_t getGpio() const { return(_gpio); }
/*!
\brief Some modules contain external RF switch controlled by pins.
@ -448,7 +448,7 @@ class Module {
\param rxEn RX enable pin.
\param txEn TX enable pin.
*/
void setRfSwitchPins(RADIOLIB_PIN_TYPE rxEn, RADIOLIB_PIN_TYPE txEn);
void setRfSwitchPins(uint8_t rxEn, uint8_t txEn);
/*!
\brief Some modules contain external RF switch controlled by pins.
@ -494,7 +494,7 @@ class Module {
\code
// In global scope, define the pin array and mode table
static const RADIOLIB_PIN_TYPE rfswitch_pins[] =
static const uint8_t rfswitch_pins[] =
{PA0, PA1, RADIOLIB_NC};
static const Module::RfSwitchMode_t rfswitch_table[] = {
{Module::MODE_IDLE, {LOW, LOW}},
@ -512,7 +512,7 @@ class Module {
\endcode
*/
void setRfSwitchTable(const RADIOLIB_PIN_TYPE (&pins)[RFSWITCH_MAX_PINS], const RfSwitchMode_t table[]);
void setRfSwitchTable(const uint8_t (&pins)[RFSWITCH_MAX_PINS], const RfSwitchMode_t table[]);
/*!
* \brief Find a mode in the RfSwitchTable.
@ -541,137 +541,6 @@ class Module {
*/
void waitForMicroseconds(uint32_t start, uint32_t len);
// Arduino core overrides
/*!
\brief Arduino core pinMode override that checks RADIOLIB_NC as alias for unused pin.
\param pin Pin to change the mode of.
\param mode Which mode to set.
*/
void pinMode(RADIOLIB_PIN_TYPE pin, RADIOLIB_PIN_MODE mode);
/*!
\brief Arduino core digitalWrite override that checks RADIOLIB_NC as alias for unused pin.
\param pin Pin to write to.
\param value Whether to set the pin high or low.
*/
void digitalWrite(RADIOLIB_PIN_TYPE pin, RADIOLIB_PIN_STATUS value);
/*!
\brief Arduino core digitalWrite override that checks RADIOLIB_NC as alias for unused pin.
\param pin Pin to read from.
\returns Pin value.
*/
RADIOLIB_PIN_STATUS digitalRead(RADIOLIB_PIN_TYPE pin);
/*!
\brief Arduino core tone override that checks RADIOLIB_NC as alias for unused pin and RADIOLIB_TONE_UNSUPPORTED to make sure the platform does support tone.
\param pin Pin to write to.
\param value Frequency to output.
*/
void tone(RADIOLIB_PIN_TYPE pin, uint16_t value, uint32_t duration = 0);
/*!
\brief Arduino core noTone override that checks RADIOLIB_NC as alias for unused pin and RADIOLIB_TONE_UNSUPPORTED to make sure the platform does support tone.
\param pin Pin to write to.
*/
void noTone(RADIOLIB_PIN_TYPE pin);
/*!
\brief Arduino core attachInterrupt override.
\param interruptNum Interrupt number.
\param userFunc Interrupt service routine.
\param mode Pin hcange direction.
*/
void attachInterrupt(RADIOLIB_PIN_TYPE interruptNum, void (*userFunc)(void), RADIOLIB_INTERRUPT_STATUS mode);
/*!
\brief Arduino core detachInterrupt override.
\param interruptNum Interrupt number.
*/
void detachInterrupt(RADIOLIB_PIN_TYPE interruptNum);
/*!
\brief Arduino core yield override.
*/
void yield();
/*!
\brief Arduino core delay override.
\param ms Delay length in milliseconds.
*/
void delay(uint32_t ms);
/*!
\brief Arduino core delayMicroseconds override.
\param us Delay length in microseconds.
*/
void delayMicroseconds(uint32_t us);
/*!
\brief Arduino core millis override.
*/
uint32_t millis();
/*!
\brief Arduino core micros override.
*/
uint32_t micros();
/*!
\brief Arduino core pulseIn override.
*/
uint32_t pulseIn(RADIOLIB_PIN_TYPE pin, RADIOLIB_PIN_STATUS state, uint32_t timeout);
/*!
\brief Arduino core SPI begin override.
*/
void begin();
/*!
\brief Arduino core SPI beginTransaction override.
*/
void beginTransaction();
/*!
\brief Arduino core SPI transfer override.
*/
uint8_t transfer(uint8_t b);
/*!
\brief Arduino core SPI endTransaction override.
*/
void endTransaction();
/*!
\brief Arduino core SPI end override.
*/
void end();
// helper functions to set up SPI overrides on Arduino
#if defined(RADIOLIB_BUILD_ARDUINO)
void SPIbegin();
void SPIend();
virtual void SPIbeginTransaction();
virtual uint8_t SPItransfer(uint8_t b);
virtual void SPIendTransaction();
#endif
/*!
\brief Function to reflect bits within a byte.
*/
@ -711,58 +580,18 @@ class Module {
#if !defined(RADIOLIB_GODMODE)
private:
#endif
// pins
RADIOLIB_PIN_TYPE _cs = RADIOLIB_NC;
RADIOLIB_PIN_TYPE _irq = RADIOLIB_NC;
RADIOLIB_PIN_TYPE _rst = RADIOLIB_NC;
RADIOLIB_PIN_TYPE _gpio = RADIOLIB_NC;
// SPI interface (Arduino only)
#if defined(RADIOLIB_BUILD_ARDUINO)
SPIClass* _spi = NULL;
SPISettings _spiSettings = RADIOLIB_DEFAULT_SPI_SETTINGS;
bool _initInterface = false;
#endif
uint8_t _cs = RADIOLIB_NC;
uint8_t _irq = RADIOLIB_NC;
uint8_t _rst = RADIOLIB_NC;
uint8_t _gpio = RADIOLIB_NC;
// RF switch pins and table
RADIOLIB_PIN_TYPE _rfSwitchPins[RFSWITCH_MAX_PINS] = { RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC };
uint8_t _rfSwitchPins[RFSWITCH_MAX_PINS] = { RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC };
const RfSwitchMode_t *_rfSwitchTable = nullptr;
#if defined(RADIOLIB_INTERRUPT_TIMING)
uint32_t _prevTimingLen = 0;
#endif
// hardware abstraction layer callbacks
// this is placed at the end of Module class because the callback generator macros
// screw with the private/public access specifiers
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_PIN_MODE);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_DIGITAL_WRITE);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_DIGITAL_READ);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_TONE);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_NO_TONE);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_ATTACH_INTERRUPT);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_DETACH_INTERRUPT);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_YIELD);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_DELAY);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_DELAY_MICROSECONDS);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_MILLIS);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_MICROS);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_PULSE_IN);
#if defined(RADIOLIB_BUILD_ARDUINO)
RADIOLIB_GENERATE_CALLBACK_SPI(RADIOLIB_CB_ARGS_SPI_BEGIN);
RADIOLIB_GENERATE_CALLBACK_SPI(RADIOLIB_CB_ARGS_SPI_BEGIN_TRANSACTION);
RADIOLIB_GENERATE_CALLBACK_SPI(RADIOLIB_CB_ARGS_SPI_TRANSFER);
RADIOLIB_GENERATE_CALLBACK_SPI(RADIOLIB_CB_ARGS_SPI_END_TRANSACTION);
RADIOLIB_GENERATE_CALLBACK_SPI(RADIOLIB_CB_ARGS_SPI_END);
#else
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_SPI_BEGIN);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_SPI_BEGIN_TRANSACTION);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_SPI_TRANSFER);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_SPI_END_TRANSACTION);
RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_SPI_END);
#endif
};
#endif

View file

@ -38,6 +38,12 @@
#include "TypeDef.h"
#include "Module.h"
#include "Hal.h"
#if defined(RADIOLIB_BUILD_ARDUINO)
#include "ArduinoHal.h"
#endif
// warnings are printed in this file since BuildOpt.h is compiled in multiple places
// check God mode

View file

@ -1,4 +1,5 @@
#include "CC1101.h"
#include <math.h>
#if !defined(RADIOLIB_EXCLUDE_CC1101)
CC1101::CC1101(Module* module) : PhysicalLayer(RADIOLIB_CC1101_FREQUENCY_STEP_SIZE, RADIOLIB_CC1101_MAX_PACKET_LENGTH) {
@ -14,7 +15,7 @@ int16_t CC1101::begin(float freq, float br, float freqDev, float rxBw, int8_t po
_mod->SPIreadCommand = RADIOLIB_CC1101_CMD_READ;
_mod->SPIwriteCommand = RADIOLIB_CC1101_CMD_WRITE;
_mod->init();
_mod->pinMode(_mod->getIrq(), INPUT);
_mod->hal->pinMode(_mod->getIrq(), _mod->hal->GpioModeInput);
// try to find the CC1101 chip
uint8_t i = 0;
@ -25,7 +26,7 @@ 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->delay(10);
_mod->hal->delay(10);
i++;
}
}
@ -99,22 +100,22 @@ int16_t CC1101::transmit(uint8_t* data, size_t len, uint8_t addr) {
RADIOLIB_ASSERT(state);
// wait for transmission start or timeout
uint32_t start = _mod->micros();
while(!_mod->digitalRead(_mod->getGpio())) {
_mod->yield();
uint32_t start = _mod->hal->micros();
while(!_mod->hal->digitalRead(_mod->getGpio())) {
_mod->hal->yield();
if(_mod->micros() - start > timeout) {
if(_mod->hal->micros() - start > timeout) {
finishTransmit();
return(RADIOLIB_ERR_TX_TIMEOUT);
}
}
// wait for transmission end or timeout
start = _mod->micros();
while(_mod->digitalRead(_mod->getGpio())) {
_mod->yield();
start = _mod->hal->micros();
while(_mod->hal->digitalRead(_mod->getGpio())) {
_mod->hal->yield();
if(_mod->micros() - start > timeout) {
if(_mod->hal->micros() - start > timeout) {
finishTransmit();
return(RADIOLIB_ERR_TX_TIMEOUT);
}
@ -132,11 +133,11 @@ int16_t CC1101::receive(uint8_t* data, size_t len) {
RADIOLIB_ASSERT(state);
// wait for packet or timeout
uint32_t start = _mod->micros();
while(!_mod->digitalRead(_mod->getIrq())) {
_mod->yield();
uint32_t start = _mod->hal->micros();
while(!_mod->hal->digitalRead(_mod->getIrq())) {
_mod->hal->yield();
if(_mod->micros() - start > timeout) {
if(_mod->hal->micros() - start > timeout) {
standby();
SPIsendCommand(RADIOLIB_CC1101_CMD_FLUSH_RX);
return(RADIOLIB_ERR_RX_TIMEOUT);
@ -219,27 +220,27 @@ int16_t CC1101::packetMode() {
return(state);
}
void CC1101::setGdo0Action(void (*func)(void), RADIOLIB_INTERRUPT_STATUS dir) {
_mod->attachInterrupt(RADIOLIB_DIGITAL_PIN_TO_INTERRUPT(_mod->getIrq()), func, dir);
void CC1101::setGdo0Action(void (*func)(void), uint8_t dir) {
_mod->hal->attachInterrupt(_mod->hal->pinToInterrupt(_mod->getIrq()), func, dir);
}
void CC1101::clearGdo0Action() {
_mod->detachInterrupt(RADIOLIB_DIGITAL_PIN_TO_INTERRUPT(_mod->getIrq()));
_mod->hal->detachInterrupt(_mod->hal->pinToInterrupt(_mod->getIrq()));
}
void CC1101::setGdo2Action(void (*func)(void), RADIOLIB_INTERRUPT_STATUS dir) {
void CC1101::setGdo2Action(void (*func)(void), uint8_t dir) {
if(_mod->getGpio() == RADIOLIB_NC) {
return;
}
_mod->pinMode(_mod->getGpio(), INPUT);
_mod->attachInterrupt(RADIOLIB_DIGITAL_PIN_TO_INTERRUPT(_mod->getGpio()), func, dir);
_mod->hal->pinMode(_mod->getGpio(), _mod->hal->GpioModeInput);
_mod->hal->attachInterrupt(_mod->hal->pinToInterrupt(_mod->getGpio()), func, dir);
}
void CC1101::clearGdo2Action() {
if(_mod->getGpio() == RADIOLIB_NC) {
return;
}
_mod->detachInterrupt(RADIOLIB_DIGITAL_PIN_TO_INTERRUPT(_mod->getGpio()));
_mod->hal->detachInterrupt(_mod->hal->pinToInterrupt(_mod->getGpio()));
}
int16_t CC1101::startTransmit(uint8_t* data, size_t len, uint8_t addr) {
@ -309,7 +310,7 @@ int16_t CC1101::startTransmit(uint8_t* data, size_t len, uint8_t addr) {
*
* TODO: test this on real hardware
*/
_mod->delayMicroseconds(250);
_mod->hal->delayMicroseconds(250);
}
}
@ -371,17 +372,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 = millis();
uint32_t lastPop = _mod->hal->millis();
// keep reading from FIFO until we get all the packet.
while (readBytes < length) {
if (bytesInFIFO == 0) {
if (millis() - lastPop > 5) {
if (_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 {
delay(1);
_mod->hal->delay(1);
bytesInFIFO = SPIgetRegValue(RADIOLIB_CC1101_REG_RXBYTES, 6, 0);
continue;
}
@ -391,7 +392,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 = millis();
lastPop = _mod->hal->millis();
// Get how many bytes are left in FIFO.
bytesInFIFO = SPIgetRegValue(RADIOLIB_CC1101_REG_RXBYTES, 6, 0);
@ -401,7 +402,7 @@ 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
delay(3);
_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) {
@ -898,11 +899,11 @@ int16_t CC1101::setEncoding(uint8_t encoding) {
}
}
void CC1101::setRfSwitchPins(RADIOLIB_PIN_TYPE rxEn, RADIOLIB_PIN_TYPE txEn) {
void CC1101::setRfSwitchPins(uint8_t rxEn, uint8_t txEn) {
_mod->setRfSwitchPins(rxEn, txEn);
}
void CC1101::setRfSwitchTable(const RADIOLIB_PIN_TYPE (&pins)[Module::RFSWITCH_MAX_PINS], const Module::RfSwitchMode_t table[]) {
void CC1101::setRfSwitchTable(const uint8_t (&pins)[Module::RFSWITCH_MAX_PINS], const Module::RfSwitchMode_t table[]) {
_mod->setRfSwitchTable(pins, table);
}
@ -912,7 +913,7 @@ uint8_t CC1101::randomByte() {
RADIOLIB_DEBUG_PRINTLN("CC1101::randomByte");
// wait a bit for the RSSI reading to stabilise
_mod->delay(10);
_mod->hal->delay(10);
// read RSSI value 8 times, always keep just the least significant bit
uint8_t randByte = 0x00;
@ -932,15 +933,15 @@ int16_t CC1101::getChipVersion() {
#if !defined(RADIOLIB_EXCLUDE_DIRECT_RECEIVE)
void CC1101::setDirectAction(void (*func)(void)) {
setGdo0Action(func);
setGdo0Action(func, _mod->hal->GpioInterruptRising);
}
void CC1101::readBit(RADIOLIB_PIN_TYPE pin) {
updateDirectBuffer((uint8_t)_mod->digitalRead(pin));
void CC1101::readBit(uint8_t pin) {
updateDirectBuffer((uint8_t)_mod->hal->digitalRead(pin));
}
#endif
int16_t CC1101::setDIOMapping(RADIOLIB_PIN_TYPE pin, uint8_t value) {
int16_t CC1101::setDIOMapping(uint8_t pin, uint8_t value) {
if (pin > 2)
return RADIOLIB_ERR_INVALID_DIO_PIN;
@ -952,7 +953,7 @@ int16_t CC1101::config() {
SPIsendCommand(RADIOLIB_CC1101_CMD_RESET);
// Wait a ridiculous amount of time to be sure radio is ready.
_mod->delay(150);
_mod->hal->delay(150);
// enable automatic frequency synthesizer calibration
int16_t state = SPIsetRegValue(RADIOLIB_CC1101_REG_MCSM0, RADIOLIB_CC1101_FS_AUTOCAL_IDLE_TO_RXTX, 5, 4);
@ -1082,17 +1083,17 @@ void CC1101::SPIwriteRegisterBurst(uint8_t reg, uint8_t* data, size_t len) {
void CC1101::SPIsendCommand(uint8_t cmd) {
// pull NSS low
_mod->digitalWrite(_mod->getCs(), LOW);
_mod->hal->digitalWrite(_mod->getCs(), _mod->hal->GpioLevelLow);
// start transfer
_mod->beginTransaction();
_mod->hal->spiBeginTransaction();
// send the command byte
_mod->transfer(cmd);
_mod->hal->spiTransfer(cmd);
// stop transfer
_mod->endTransaction();
_mod->digitalWrite(_mod->getCs(), HIGH);
_mod->hal->spiEndTransaction();
_mod->hal->digitalWrite(_mod->getCs(), _mod->hal->GpioLevelHigh);
}
#endif

View file

@ -644,9 +644,9 @@ class CC1101: public PhysicalLayer {
\param func ISR to call.
\param dir Signal change direction. Defaults to RISING.
\param dir Signal change direction.
*/
void setGdo0Action(void (*func)(void), RADIOLIB_INTERRUPT_STATUS dir = RISING);
void setGdo0Action(void (*func)(void), uint8_t dir);
/*!
\brief Clears interrupt service routine to call when GDO0 activates.
@ -658,9 +658,9 @@ class CC1101: public PhysicalLayer {
\param func ISR to call.
\param dir Signal change direction. Defaults to FALLING.
\param dir Signal change direction.
*/
void setGdo2Action(void (*func)(void), RADIOLIB_INTERRUPT_STATUS dir = FALLING);
void setGdo2Action(void (*func)(void), uint8_t dir);
/*!
\brief Clears interrupt service routine to call when GDO0 activates.
@ -953,10 +953,10 @@ class CC1101: public PhysicalLayer {
int16_t setEncoding(uint8_t encoding) override;
/*! \copydoc Module::setRfSwitchPins */
void setRfSwitchPins(RADIOLIB_PIN_TYPE rxEn, RADIOLIB_PIN_TYPE txEn);
void setRfSwitchPins(uint8_t rxEn, uint8_t txEn);
/*! \copydoc Module::setRfSwitchTable */
void setRfSwitchTable(const RADIOLIB_PIN_TYPE (&pins)[Module::RFSWITCH_MAX_PINS], const Module::RfSwitchMode_t table[]);
void setRfSwitchTable(const uint8_t (&pins)[Module::RFSWITCH_MAX_PINS], const Module::RfSwitchMode_t table[]);
/*!
\brief Get one truly random byte from RSSI noise.
@ -985,7 +985,7 @@ class CC1101: public PhysicalLayer {
\param pin Pin on which to read.
*/
void readBit(RADIOLIB_PIN_TYPE pin);
void readBit(uint8_t pin);
#endif
/*!
@ -997,7 +997,7 @@ class CC1101: public PhysicalLayer {
\returns \ref status_codes
*/
int16_t setDIOMapping(RADIOLIB_PIN_TYPE pin, uint8_t value);
int16_t setDIOMapping(uint8_t pin, uint8_t value);
#if !defined(RADIOLIB_GODMODE) && !defined(RADIOLIB_LOW_LEVEL)
protected:

View file

@ -1,4 +1,5 @@
#include "RF69.h"
#include <math.h>
#if !defined(RADIOLIB_EXCLUDE_RF69)
RF69::RF69(Module* module) : PhysicalLayer(RADIOLIB_RF69_FREQUENCY_STEP_SIZE, RADIOLIB_RF69_MAX_PACKET_LENGTH) {
@ -12,7 +13,7 @@ Module* RF69::getMod() {
int16_t RF69::begin(float freq, float br, float freqDev, float rxBw, int8_t power, uint8_t preambleLen) {
// set module properties
_mod->init();
_mod->pinMode(_mod->getIrq(), INPUT);
_mod->hal->pinMode(_mod->getIrq(), _mod->hal->GpioModeInput);
// try to find the RF69 chip
uint8_t i = 0;
@ -27,7 +28,7 @@ int16_t RF69::begin(float freq, float br, float freqDev, float rxBw, int8_t powe
flagFound = true;
} else {
RADIOLIB_DEBUG_PRINTLN("RF69 not found! (%d of 10 tries) RADIOLIB_RF69_REG_VERSION == 0x%04X, expected 0x0024", i + 1, version);
_mod->delay(10);
_mod->hal->delay(10);
i++;
}
}
@ -94,11 +95,11 @@ int16_t RF69::begin(float freq, float br, float freqDev, float rxBw, int8_t powe
}
void RF69::reset() {
_mod->pinMode(_mod->getRst(), OUTPUT);
_mod->digitalWrite(_mod->getRst(), HIGH);
_mod->delay(1);
_mod->digitalWrite(_mod->getRst(), LOW);
_mod->delay(10);
_mod->hal->pinMode(_mod->getRst(), _mod->hal->GpioModeOutput);
_mod->hal->digitalWrite(_mod->getRst(), _mod->hal->GpioLevelHigh);
_mod->hal->delay(1);
_mod->hal->digitalWrite(_mod->getRst(), _mod->hal->GpioLevelLow);
_mod->hal->delay(10);
}
int16_t RF69::transmit(uint8_t* data, size_t len, uint8_t addr) {
@ -110,11 +111,11 @@ int16_t RF69::transmit(uint8_t* data, size_t len, uint8_t addr) {
RADIOLIB_ASSERT(state);
// wait for transmission end or timeout
uint32_t start = _mod->micros();
while(!_mod->digitalRead(_mod->getIrq())) {
_mod->yield();
uint32_t start = _mod->hal->micros();
while(!_mod->hal->digitalRead(_mod->getIrq())) {
_mod->hal->yield();
if(_mod->micros() - start > timeout) {
if(_mod->hal->micros() - start > timeout) {
finishTransmit();
return(RADIOLIB_ERR_TX_TIMEOUT);
}
@ -132,11 +133,11 @@ int16_t RF69::receive(uint8_t* data, size_t len) {
RADIOLIB_ASSERT(state);
// wait for packet reception or timeout
uint32_t start = _mod->micros();
while(!_mod->digitalRead(_mod->getIrq())) {
_mod->yield();
uint32_t start = _mod->hal->micros();
while(!_mod->hal->digitalRead(_mod->getIrq())) {
_mod->hal->yield();
if(_mod->micros() - start > timeout) {
if(_mod->hal->micros() - start > timeout) {
standby();
clearIRQFlags();
return(RADIOLIB_ERR_RX_TIMEOUT);
@ -271,26 +272,26 @@ int16_t RF69::startReceive(uint32_t timeout, uint16_t irqFlags, uint16_t irqMask
}
void RF69::setDio0Action(void (*func)(void)) {
_mod->attachInterrupt(RADIOLIB_DIGITAL_PIN_TO_INTERRUPT(_mod->getIrq()), func, RISING);
_mod->hal->attachInterrupt(_mod->hal->pinToInterrupt(_mod->getIrq()), func, _mod->hal->GpioInterruptRising);
}
void RF69::clearDio0Action() {
_mod->detachInterrupt(RADIOLIB_DIGITAL_PIN_TO_INTERRUPT(_mod->getIrq()));
_mod->hal->detachInterrupt(_mod->hal->pinToInterrupt(_mod->getIrq()));
}
void RF69::setDio1Action(void (*func)(void)) {
if(_mod->getGpio() == RADIOLIB_NC) {
return;
}
_mod->pinMode(_mod->getGpio(), INPUT);
_mod->attachInterrupt(RADIOLIB_DIGITAL_PIN_TO_INTERRUPT(_mod->getGpio()), func, RISING);
_mod->hal->pinMode(_mod->getGpio(), _mod->hal->GpioModeInput);
_mod->hal->attachInterrupt(_mod->hal->pinToInterrupt(_mod->getGpio()), func, _mod->hal->GpioInterruptRising);
}
void RF69::clearDio1Action() {
if(_mod->getGpio() == RADIOLIB_NC) {
return;
}
_mod->detachInterrupt(RADIOLIB_DIGITAL_PIN_TO_INTERRUPT(_mod->getGpio()));
_mod->hal->detachInterrupt(_mod->hal->pinToInterrupt(_mod->getGpio()));
}
void RF69::setFifoEmptyAction(void (*func)(void)) {
@ -298,10 +299,10 @@ void RF69::setFifoEmptyAction(void (*func)(void)) {
if(_mod->getGpio() == RADIOLIB_NC) {
return;
}
_mod->pinMode(_mod->getGpio(), INPUT);
_mod->hal->pinMode(_mod->getGpio(), _mod->hal->GpioModeInput);
// we need to invert the logic here (as compared to setDio1Action), since we are using the "FIFO not empty interrupt"
_mod->attachInterrupt(RADIOLIB_DIGITAL_PIN_TO_INTERRUPT(_mod->getGpio()), func, FALLING);
_mod->hal->attachInterrupt(_mod->hal->pinToInterrupt(_mod->getGpio()), func, _mod->hal->GpioInterruptFalling);
}
void RF69::clearFifoEmptyAction() {
@ -758,7 +759,7 @@ int16_t RF69::getTemperature() {
// wait until measurement is finished
while(_mod->SPIgetRegValue(RADIOLIB_RF69_REG_TEMP_1, 2, 2) == RADIOLIB_RF69_TEMP_MEAS_RUNNING) {
// check every 10 us
_mod->delay(10);
_mod->hal->delay(10);
}
int8_t rawTemp = _mod->SPIgetRegValue(RADIOLIB_RF69_REG_TEMP_2);
@ -916,11 +917,11 @@ int16_t RF69::setRSSIThreshold(float dbm) {
return _mod->SPIsetRegValue(RADIOLIB_RF69_REG_RSSI_THRESH, (uint8_t)(-2.0 * dbm), 7, 0);
}
void RF69::setRfSwitchPins(RADIOLIB_PIN_TYPE rxEn, RADIOLIB_PIN_TYPE txEn) {
void RF69::setRfSwitchPins(uint8_t rxEn, uint8_t txEn) {
_mod->setRfSwitchPins(rxEn, txEn);
}
void RF69::setRfSwitchTable(const RADIOLIB_PIN_TYPE (&pins)[Module::RFSWITCH_MAX_PINS], const Module::RfSwitchMode_t table[]) {
void RF69::setRfSwitchTable(const uint8_t (&pins)[Module::RFSWITCH_MAX_PINS], const Module::RfSwitchMode_t table[]) {
_mod->setRfSwitchTable(pins, table);
}
@ -929,7 +930,7 @@ uint8_t RF69::randomByte() {
setMode(RADIOLIB_RF69_RX);
// wait a bit for the RSSI reading to stabilise
_mod->delay(10);
_mod->hal->delay(10);
// read RSSI value 8 times, always keep just the least significant bit
uint8_t randByte = 0x00;
@ -948,12 +949,12 @@ void RF69::setDirectAction(void (*func)(void)) {
setDio1Action(func);
}
void RF69::readBit(RADIOLIB_PIN_TYPE pin) {
updateDirectBuffer((uint8_t)_mod->digitalRead(pin));
void RF69::readBit(uint8_t pin) {
updateDirectBuffer((uint8_t)_mod->hal->digitalRead(pin));
}
#endif
int16_t RF69::setDIOMapping(RADIOLIB_PIN_TYPE pin, uint8_t value) {
int16_t RF69::setDIOMapping(uint8_t pin, uint8_t value) {
if(pin > 5) {
return(RADIOLIB_ERR_INVALID_DIO_PIN);
}

View file

@ -1035,10 +1035,10 @@ class RF69: public PhysicalLayer {
int16_t setRSSIThreshold(float dbm);
/*! \copydoc Module::setRfSwitchPins */
void setRfSwitchPins(RADIOLIB_PIN_TYPE rxEn, RADIOLIB_PIN_TYPE txEn);
void setRfSwitchPins(uint8_t rxEn, uint8_t txEn);
/*! \copydoc Module::setRfSwitchTable */
void setRfSwitchTable(const RADIOLIB_PIN_TYPE (&pins)[Module::RFSWITCH_MAX_PINS], const Module::RfSwitchMode_t table[]);
void setRfSwitchTable(const uint8_t (&pins)[Module::RFSWITCH_MAX_PINS], const Module::RfSwitchMode_t table[]);
/*!
\brief Get one truly random byte from RSSI noise.
@ -1067,7 +1067,7 @@ class RF69: public PhysicalLayer {
\param pin Pin on which to read.
*/
void readBit(RADIOLIB_PIN_TYPE pin);
void readBit(uint8_t pin);
#endif
/*!
@ -1079,7 +1079,7 @@ class RF69: public PhysicalLayer {
\returns \ref status_codes
*/
int16_t setDIOMapping(RADIOLIB_PIN_TYPE pin, uint8_t value);
int16_t setDIOMapping(uint8_t pin, uint8_t value);
#if !defined(RADIOLIB_GODMODE) && !defined(RADIOLIB_LOW_LEVEL)
protected:

View file

@ -8,8 +8,8 @@ SX1231::SX1231(Module* mod) : RF69(mod) {
int16_t SX1231::begin(float freq, float br, float freqDev, float rxBw, int8_t power, uint8_t preambleLen) {
// set module properties
_mod->init();
_mod->pinMode(_mod->getIrq(), INPUT);
_mod->pinMode(_mod->getRst(), OUTPUT);
_mod->hal->pinMode(_mod->getIrq(), _mod->hal->GpioModeInput);
_mod->hal->pinMode(_mod->getRst(), _mod->hal->GpioModeOutput);
// try to find the SX1231 chip
uint8_t i = 0;
@ -21,7 +21,7 @@ int16_t SX1231::begin(float freq, float br, float freqDev, float rxBw, int8_t po
_chipRevision = version;
} else {
RADIOLIB_DEBUG_PRINTLN("SX1231 not found! (%d of 10 tries) RF69_REG_VERSION == 0x%04X, expected 0x0021 / 0x0022 / 0x0023", i + 1, version);
_mod->delay(10);
_mod->hal->delay(10);
i++;
}
}

View file

@ -9,6 +9,8 @@ This file is licensed under the MIT License: https://opensource.org/licenses/MIT
#if !defined(RADIOLIB_EXCLUDE_STM32WLX)
#include "../../ArduinoHal.h"
// This defines some dummy pin numbers (starting at NUM_DIGITAL_PINS to
// guarantee these are not valid regular pin numbers) that can be passed
// to the parent Module class, to be stored here and then passed back to
@ -20,87 +22,85 @@ enum {
RADIOLIB_STM32WLx_VIRTUAL_PIN_RESET,
};
class Stm32wlxHal : public ArduinoHal {
public:
Stm32wlxHal(): ArduinoHal(SubGhz.SPI, SubGhz.spi_settings) {}
void pinMode(uint8_t dwPin, uint8_t dwMode) {
switch(dwPin) {
case RADIOLIB_STM32WLx_VIRTUAL_PIN_NSS:
case RADIOLIB_STM32WLx_VIRTUAL_PIN_BUSY:
case RADIOLIB_STM32WLx_VIRTUAL_PIN_IRQ:
case RADIOLIB_STM32WLx_VIRTUAL_PIN_RESET:
// Nothing to do
break;
default:
::pinMode(dwPin, dwMode);
break;
}
}
void digitalWrite(uint8_t dwPin, uint8_t dwVal) {
switch (dwPin) {
case RADIOLIB_STM32WLx_VIRTUAL_PIN_NSS:
SubGhz.setNssActive(dwVal == LOW);
break;
case RADIOLIB_STM32WLx_VIRTUAL_PIN_RESET:
SubGhz.setResetActive(dwVal == LOW);
break;
case RADIOLIB_STM32WLx_VIRTUAL_PIN_BUSY:
case RADIOLIB_STM32WLx_VIRTUAL_PIN_IRQ:
// Should not (and cannot) be written, just ignore
break;
default:
::digitalWrite(dwPin, dwVal);
break;
}
}
uint8_t digitalRead(uint8_t ulPin) {
switch (ulPin) {
case RADIOLIB_STM32WLx_VIRTUAL_PIN_BUSY:
return(SubGhz.isBusy() ? HIGH : LOW);
case RADIOLIB_STM32WLx_VIRTUAL_PIN_IRQ:
// We cannot use the radio IRQ output directly, but since:
// - the pending flag will be set whenever the IRQ output is set,
// and
// - the pending flag will be cleared (by
// STM32WLx::clearIrqStatus()) whenever the radio IRQ output is
// cleared,
// the pending flag should always reflect the current radio IRQ
// output. There is one exception: when the ISR starts the pending
// flag is cleared by hardware and not set again until after the
// ISR finishes, so the value is incorrect *inside* the ISR, but
// running RadioLib code inside the ISR (especially code that
// polls the IRQ flag) is not supported and probably broken in
// other ways too.
return(SubGhz.isInterruptPending() ? HIGH : LOW);
case RADIOLIB_STM32WLx_VIRTUAL_PIN_NSS:
return(SubGhz.isNssActive() ? LOW : HIGH);
case RADIOLIB_STM32WLx_VIRTUAL_PIN_RESET:
return(SubGhz.isResetActive() ? LOW : HIGH);
default:
return(::digitalRead(ulPin));
}
}
};
STM32WLx_Module::STM32WLx_Module():
Module(
new Stm32wlxHal,
RADIOLIB_STM32WLx_VIRTUAL_PIN_NSS,
RADIOLIB_STM32WLx_VIRTUAL_PIN_IRQ,
RADIOLIB_STM32WLx_VIRTUAL_PIN_RESET,
RADIOLIB_STM32WLx_VIRTUAL_PIN_BUSY,
SubGhz.SPI,
SubGhz.spi_settings
)
{
setCb_pinMode(virtualPinMode);
setCb_digitalWrite(virtualDigitalWrite);
setCb_digitalRead(virtualDigitalRead);
}
void STM32WLx_Module::virtualPinMode(uint32_t dwPin, uint32_t dwMode) {
switch(dwPin) {
case RADIOLIB_STM32WLx_VIRTUAL_PIN_NSS:
case RADIOLIB_STM32WLx_VIRTUAL_PIN_BUSY:
case RADIOLIB_STM32WLx_VIRTUAL_PIN_IRQ:
case RADIOLIB_STM32WLx_VIRTUAL_PIN_RESET:
// Nothing to do
break;
default:
::pinMode(dwPin, dwMode);
break;
}
}
void STM32WLx_Module::virtualDigitalWrite(uint32_t dwPin, uint32_t dwVal) {
switch (dwPin) {
case RADIOLIB_STM32WLx_VIRTUAL_PIN_NSS:
SubGhz.setNssActive(dwVal == LOW);
break;
case RADIOLIB_STM32WLx_VIRTUAL_PIN_RESET:
SubGhz.setResetActive(dwVal == LOW);
break;
case RADIOLIB_STM32WLx_VIRTUAL_PIN_BUSY:
case RADIOLIB_STM32WLx_VIRTUAL_PIN_IRQ:
// Should not (and cannot) be written, just ignore
break;
default:
::digitalWrite(dwPin, dwVal);
break;
}
}
int STM32WLx_Module::virtualDigitalRead(uint32_t ulPin) {
switch (ulPin) {
case RADIOLIB_STM32WLx_VIRTUAL_PIN_BUSY:
return(SubGhz.isBusy() ? HIGH : LOW);
case RADIOLIB_STM32WLx_VIRTUAL_PIN_IRQ:
// We cannot use the radio IRQ output directly, but since:
// - the pending flag will be set whenever the IRQ output is set,
// and
// - the pending flag will be cleared (by
// STM32WLx::clearIrqStatus()) whenever the radio IRQ output is
// cleared,
// the pending flag should always reflect the current radio IRQ
// output. There is one exception: when the ISR starts the pending
// flag is cleared by hardware and not set again until after the
// ISR finishes, so the value is incorrect *inside* the ISR, but
// running RadioLib code inside the ISR (especially code that
// polls the IRQ flag) is not supported and probably broken in
// other ways too.
return(SubGhz.isInterruptPending() ? HIGH : LOW);
case RADIOLIB_STM32WLx_VIRTUAL_PIN_NSS:
return(SubGhz.isNssActive() ? LOW : HIGH);
case RADIOLIB_STM32WLx_VIRTUAL_PIN_RESET:
return(SubGhz.isResetActive() ? LOW : HIGH);
default:
return(::digitalRead(ulPin));
}
}
RADIOLIB_STM32WLx_VIRTUAL_PIN_BUSY
) {}
#endif // !defined(RADIOLIB_EXCLUDE_STM32WLX)

View file

@ -31,17 +31,6 @@ class STM32WLx_Module : public Module {
public:
STM32WLx_Module();
#if !defined(RADIOLIB_GODMODE)
private:
#endif
// Replacement callbacks to handle virtual pins. These are static,
// since they replace global functions that cannot take any this
// pointer for context.
static void virtualPinMode(uint32_t dwPin, uint32_t dwMode);
static void virtualDigitalWrite(uint32_t dwPin, uint32_t dwVal);
static int virtualDigitalRead(uint32_t ulPin);
};
#endif // !defined(RADIOLIB_EXCLUDE_STM32WLX)

View file

@ -1,4 +1,6 @@
#include "SX126x.h"
#include <string.h>
#include <math.h>
#if !defined(RADIOLIB_EXCLUDE_SX126X)
SX126x::SX126x(Module* mod) : PhysicalLayer(RADIOLIB_SX126X_FREQUENCY_STEP_SIZE, RADIOLIB_SX126X_MAX_PACKET_LENGTH) {
@ -13,8 +15,8 @@ Module* SX126x::getMod() {
int16_t SX126x::begin(uint8_t cr, uint8_t syncWord, uint16_t preambleLength, float tcxoVoltage, bool useRegulatorLDO) {
// set module properties
_mod->init();
_mod->pinMode(_mod->getIrq(), INPUT);
_mod->pinMode(_mod->getGpio(), INPUT);
_mod->hal->pinMode(_mod->getIrq(), _mod->hal->GpioModeInput);
_mod->hal->pinMode(_mod->getGpio(), _mod->hal->GpioModeInput);
_mod->SPIreadCommand = RADIOLIB_SX126X_CMD_READ_REGISTER;
_mod->SPIwriteCommand = RADIOLIB_SX126X_CMD_WRITE_REGISTER;
_mod->SPInopCommand = RADIOLIB_SX126X_CMD_NOP;
@ -95,8 +97,8 @@ int16_t SX126x::begin(uint8_t cr, uint8_t syncWord, uint16_t preambleLength, flo
int16_t SX126x::beginFSK(float br, float freqDev, float rxBw, uint16_t preambleLength, float tcxoVoltage, bool useRegulatorLDO) {
// set module properties
_mod->init();
_mod->pinMode(_mod->getIrq(), INPUT);
_mod->pinMode(_mod->getGpio(), INPUT);
_mod->hal->pinMode(_mod->getIrq(), _mod->hal->GpioModeInput);
_mod->hal->pinMode(_mod->getGpio(), _mod->hal->GpioModeInput);
_mod->SPIreadCommand = RADIOLIB_SX126X_CMD_READ_REGISTER;
_mod->SPIwriteCommand = RADIOLIB_SX126X_CMD_WRITE_REGISTER;
_mod->SPInopCommand = RADIOLIB_SX126X_CMD_NOP;
@ -188,10 +190,10 @@ int16_t SX126x::beginFSK(float br, float freqDev, float rxBw, uint16_t preambleL
int16_t SX126x::reset(bool verify) {
// run the reset sequence
_mod->pinMode(_mod->getRst(), OUTPUT);
_mod->digitalWrite(_mod->getRst(), LOW);
_mod->delay(1);
_mod->digitalWrite(_mod->getRst(), HIGH);
_mod->hal->pinMode(_mod->getRst(), _mod->hal->GpioModeOutput);
_mod->hal->digitalWrite(_mod->getRst(), _mod->hal->GpioLevelLow);
_mod->hal->delay(1);
_mod->hal->digitalWrite(_mod->getRst(), _mod->hal->GpioLevelHigh);
// return immediately when verification is disabled
if(!verify) {
@ -199,7 +201,7 @@ int16_t SX126x::reset(bool verify) {
}
// set mode to standby - SX126x often refuses first few commands after reset
uint32_t start = _mod->millis();
uint32_t start = _mod->hal->millis();
while(true) {
// try to set mode to standby
int16_t state = standby();
@ -209,13 +211,13 @@ int16_t SX126x::reset(bool verify) {
}
// standby command failed, check timeout and try again
if(_mod->millis() - start >= 1000) {
if(_mod->hal->millis() - start >= 1000) {
// timed out, possibly incorrect wiring
return(state);
}
// wait a bit to not spam the module
_mod->delay(100);
_mod->hal->delay(100);
}
}
@ -252,15 +254,15 @@ int16_t SX126x::transmit(uint8_t* data, size_t len, uint8_t addr) {
RADIOLIB_ASSERT(state);
// wait for packet transmission or timeout
uint32_t start = _mod->micros();
while(!_mod->digitalRead(_mod->getIrq())) {
_mod->yield();
if(_mod->micros() - start > timeout) {
uint32_t start = _mod->hal->micros();
while(!_mod->hal->digitalRead(_mod->getIrq())) {
_mod->hal->yield();
if(_mod->hal->micros() - start > timeout) {
finishTransmit();
return(RADIOLIB_ERR_TX_TIMEOUT);
}
}
uint32_t elapsed = _mod->micros() - start;
uint32_t elapsed = _mod->hal->micros() - start;
// update data rate
_dataRate = (len*8.0)/((float)elapsed/1000000.0);
@ -302,10 +304,10 @@ int16_t SX126x::receive(uint8_t* data, size_t len) {
RADIOLIB_ASSERT(state);
// wait for packet reception or timeout
uint32_t start = _mod->micros();
while(!_mod->digitalRead(_mod->getIrq())) {
_mod->yield();
if(_mod->micros() - start > timeout) {
uint32_t start = _mod->hal->micros();
while(!_mod->hal->digitalRead(_mod->getIrq())) {
_mod->hal->yield();
if(_mod->hal->micros() - start > timeout) {
fixImplicitTimeout();
clearIrqStatus();
standby();
@ -419,8 +421,8 @@ int16_t SX126x::scanChannel(uint8_t symbolNum, uint8_t detPeak, uint8_t detMin)
RADIOLIB_ASSERT(state);
// wait for channel activity detected or timeout
while(!_mod->digitalRead(_mod->getIrq())) {
_mod->yield();
while(!_mod->hal->digitalRead(_mod->getIrq())) {
_mod->hal->yield();
}
// check CAD result
@ -438,7 +440,7 @@ int16_t SX126x::sleep(bool retainConfig) {
int16_t state = _mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_SLEEP, &sleepMode, 1, false, false);
// wait for SX126x to safely enter sleep mode
_mod->delay(1);
_mod->hal->delay(1);
return(state);
}
@ -456,11 +458,11 @@ int16_t SX126x::standby(uint8_t mode) {
}
void SX126x::setDio1Action(void (*func)(void)) {
_mod->attachInterrupt(RADIOLIB_DIGITAL_PIN_TO_INTERRUPT(_mod->getIrq()), func, RISING);
_mod->hal->attachInterrupt(_mod->hal->pinToInterrupt(_mod->getIrq()), func, _mod->hal->GpioInterruptRising);
}
void SX126x::clearDio1Action() {
_mod->detachInterrupt(RADIOLIB_DIGITAL_PIN_TO_INTERRUPT(_mod->getIrq()));
_mod->hal->detachInterrupt(_mod->hal->pinToInterrupt(_mod->getIrq()));
}
int16_t SX126x::startTransmit(uint8_t* data, size_t len, uint8_t addr) {
@ -517,8 +519,8 @@ int16_t SX126x::startTransmit(uint8_t* data, size_t len, uint8_t addr) {
RADIOLIB_ASSERT(state);
// wait for BUSY to go low (= PA ramp up done)
while(_mod->digitalRead(_mod->getGpio())) {
_mod->yield();
while(_mod->hal->digitalRead(_mod->getGpio())) {
_mod->hal->yield();
}
return(state);
@ -1357,11 +1359,11 @@ int16_t SX126x::setEncoding(uint8_t encoding) {
return(setWhitening(encoding));
}
void SX126x::setRfSwitchPins(RADIOLIB_PIN_TYPE rxEn, RADIOLIB_PIN_TYPE txEn) {
void SX126x::setRfSwitchPins(uint8_t rxEn, uint8_t txEn) {
_mod->setRfSwitchPins(rxEn, txEn);
}
void SX126x::setRfSwitchTable(const RADIOLIB_PIN_TYPE (&pins)[Module::RFSWITCH_MAX_PINS], const Module::RfSwitchMode_t table[]) {
void SX126x::setRfSwitchTable(const uint8_t (&pins)[Module::RFSWITCH_MAX_PINS], const Module::RfSwitchMode_t table[]) {
_mod->setRfSwitchTable(pins, table);
}
@ -1395,7 +1397,7 @@ uint8_t SX126x::randomByte() {
setRx(RADIOLIB_SX126X_RX_TIMEOUT_INF);
// wait a bit for the RSSI reading to stabilise
_mod->delay(10);
_mod->hal->delay(10);
// read RSSI value 8 times, always keep just the least significant bit
uint8_t randByte = 0x00;
@ -1433,8 +1435,8 @@ void SX126x::setDirectAction(void (*func)(void)) {
setDio1Action(func);
}
void SX126x::readBit(RADIOLIB_PIN_TYPE pin) {
updateDirectBuffer((uint8_t)_mod->digitalRead(pin));
void SX126x::readBit(uint8_t pin) {
updateDirectBuffer((uint8_t)_mod->hal->digitalRead(pin));
}
#endif
@ -1953,9 +1955,9 @@ int16_t SX126x::config(uint8_t modem) {
RADIOLIB_ASSERT(state);
// wait for calibration completion
_mod->delay(5);
while(_mod->digitalRead(_mod->getGpio())) {
_mod->yield();
_mod->hal->delay(5);
while(_mod->hal->digitalRead(_mod->getGpio())) {
_mod->hal->yield();
}
// check calibration result
@ -2010,7 +2012,7 @@ bool SX126x::findChip(const char* verStr) {
_mod->hexdump((uint8_t*)version, 16, RADIOLIB_SX126X_REG_VERSION_STRING);
RADIOLIB_DEBUG_PRINTLN("Expected string: %s", verStr);
#endif
_mod->delay(10);
_mod->hal->delay(10);
i++;
}
}

View file

@ -1031,10 +1031,10 @@ class SX126x: public PhysicalLayer {
int16_t setEncoding(uint8_t encoding) override;
/*! \copydoc Module::setRfSwitchPins */
void setRfSwitchPins(RADIOLIB_PIN_TYPE rxEn, RADIOLIB_PIN_TYPE txEn);
void setRfSwitchPins(uint8_t rxEn, uint8_t txEn);
/*! \copydoc Module::setRfSwitchTable */
void setRfSwitchTable(const RADIOLIB_PIN_TYPE (&pins)[Module::RFSWITCH_MAX_PINS], const Module::RfSwitchMode_t table[]);
void setRfSwitchTable(const uint8_t (&pins)[Module::RFSWITCH_MAX_PINS], const Module::RfSwitchMode_t table[]);
/*!
\brief Forces LoRa low data rate optimization. Only available in LoRa mode. After calling this method, LDRO will always be set to
@ -1083,7 +1083,7 @@ class SX126x: public PhysicalLayer {
\param pin Pin on which to read.
*/
void readBit(RADIOLIB_PIN_TYPE pin);
void readBit(uint8_t pin);
#endif
/*!

View file

@ -1,4 +1,5 @@
#include "SX1272.h"
#include <math.h>
#if !defined(RADIOLIB_EXCLUDE_SX127X)
SX1272::SX1272(Module* mod) : SX127x(mod) {
@ -71,11 +72,11 @@ int16_t SX1272::beginFSK(float freq, float br, float freqDev, float rxBw, int8_t
}
void SX1272::reset() {
_mod->pinMode(_mod->getRst(), OUTPUT);
_mod->digitalWrite(_mod->getRst(), HIGH);
_mod->delay(1);
_mod->digitalWrite(_mod->getRst(), LOW);
_mod->delay(5);
_mod->hal->pinMode(_mod->getRst(), _mod->hal->GpioModeOutput);
_mod->hal->digitalWrite(_mod->getRst(), _mod->hal->GpioLevelHigh);
_mod->hal->delay(1);
_mod->hal->digitalWrite(_mod->getRst(), _mod->hal->GpioLevelLow);
_mod->hal->delay(5);
}
int16_t SX1272::setFrequency(float freq) {

View file

@ -1,4 +1,5 @@
#include "SX1278.h"
#include <math.h>
#if !defined(RADIOLIB_EXCLUDE_SX127X)
SX1278::SX1278(Module* mod) : SX127x(mod) {
@ -71,11 +72,11 @@ int16_t SX1278::beginFSK(float freq, float br, float freqDev, float rxBw, int8_t
}
void SX1278::reset() {
_mod->pinMode(_mod->getRst(), OUTPUT);
_mod->digitalWrite(_mod->getRst(), LOW);
_mod->delay(1);
_mod->digitalWrite(_mod->getRst(), HIGH);
_mod->delay(5);
_mod->hal->pinMode(_mod->getRst(), _mod->hal->GpioModeOutput);
_mod->hal->digitalWrite(_mod->getRst(), _mod->hal->GpioLevelLow);
_mod->hal->delay(1);
_mod->hal->digitalWrite(_mod->getRst(), _mod->hal->GpioLevelHigh);
_mod->hal->delay(5);
}
int16_t SX1278::setFrequency(float freq) {

View file

@ -1,4 +1,5 @@
#include "SX127x.h"
#include <math.h>
#if !defined(RADIOLIB_EXCLUDE_SX127X)
SX127x::SX127x(Module* mod) : PhysicalLayer(RADIOLIB_SX127X_FREQUENCY_STEP_SIZE, RADIOLIB_SX127X_MAX_PACKET_LENGTH) {
@ -12,8 +13,8 @@ Module* SX127x::getMod() {
int16_t SX127x::begin(uint8_t chipVersion, uint8_t syncWord, uint16_t preambleLength) {
// set module properties
_mod->init();
_mod->pinMode(_mod->getIrq(), INPUT);
_mod->pinMode(_mod->getGpio(), INPUT);
_mod->hal->pinMode(_mod->getIrq(), _mod->hal->GpioModeInput);
_mod->hal->pinMode(_mod->getGpio(), _mod->hal->GpioModeInput);
// try to find the SX127x chip
if(!SX127x::findChip(chipVersion)) {
@ -59,8 +60,8 @@ int16_t SX127x::begin(uint8_t chipVersion, uint8_t syncWord, uint16_t preambleLe
int16_t SX127x::beginFSK(uint8_t chipVersion, float freqDev, float rxBw, uint16_t preambleLength, bool enableOOK) {
// set module properties
_mod->init();
_mod->pinMode(_mod->getIrq(), INPUT);
_mod->pinMode(_mod->getGpio(), INPUT);
_mod->hal->pinMode(_mod->getIrq(), _mod->hal->GpioModeInput);
_mod->hal->pinMode(_mod->getGpio(), _mod->hal->GpioModeInput);
// try to find the SX127x chip
if(!SX127x::findChip(chipVersion)) {
@ -152,10 +153,10 @@ int16_t SX127x::transmit(uint8_t* data, size_t len, uint8_t addr) {
RADIOLIB_ASSERT(state);
// wait for packet transmission or timeout
start = _mod->micros();
while(!_mod->digitalRead(_mod->getIrq())) {
_mod->yield();
if(_mod->micros() - start > timeout) {
start = _mod->hal->micros();
while(!_mod->hal->digitalRead(_mod->getIrq())) {
_mod->hal->yield();
if(_mod->hal->micros() - start > timeout) {
finishTransmit();
return(RADIOLIB_ERR_TX_TIMEOUT);
}
@ -170,10 +171,10 @@ int16_t SX127x::transmit(uint8_t* data, size_t len, uint8_t addr) {
RADIOLIB_ASSERT(state);
// wait for transmission end or timeout
start = _mod->micros();
while(!_mod->digitalRead(_mod->getIrq())) {
_mod->yield();
if(_mod->micros() - start > timeout) {
start = _mod->hal->micros();
while(!_mod->hal->digitalRead(_mod->getIrq())) {
_mod->hal->yield();
if(_mod->hal->micros() - start > timeout) {
finishTransmit();
return(RADIOLIB_ERR_TX_TIMEOUT);
}
@ -183,7 +184,7 @@ int16_t SX127x::transmit(uint8_t* data, size_t len, uint8_t addr) {
}
// update data rate
uint32_t elapsed = _mod->micros() - start;
uint32_t elapsed = _mod->hal->micros() - start;
_dataRate = (len*8.0)/((float)elapsed/1000000.0);
return(finishTransmit());
@ -208,19 +209,19 @@ int16_t SX127x::receive(uint8_t* data, size_t len) {
}
// wait for packet reception or timeout
uint32_t start = _mod->micros();
while(!_mod->digitalRead(_mod->getIrq())) {
_mod->yield();
uint32_t start = _mod->hal->micros();
while(!_mod->hal->digitalRead(_mod->getIrq())) {
_mod->hal->yield();
if(_mod->getGpio() == RADIOLIB_NC) {
// no GPIO pin provided, use software timeout
if(_mod->micros() - start > timeout) {
if(_mod->hal->micros() - start > timeout) {
clearIRQFlags();
return(RADIOLIB_ERR_RX_TIMEOUT);
}
} else {
// GPIO provided, use that
if(_mod->digitalRead(_mod->getGpio())) {
if(_mod->hal->digitalRead(_mod->getGpio())) {
clearIRQFlags();
return(RADIOLIB_ERR_RX_TIMEOUT);
}
@ -237,10 +238,10 @@ int16_t SX127x::receive(uint8_t* data, size_t len) {
RADIOLIB_ASSERT(state);
// wait for packet reception or timeout
uint32_t start = _mod->micros();
while(!_mod->digitalRead(_mod->getIrq())) {
_mod->yield();
if(_mod->micros() - start > timeout) {
uint32_t start = _mod->hal->micros();
while(!_mod->hal->digitalRead(_mod->getIrq())) {
_mod->hal->yield();
if(_mod->hal->micros() - start > timeout) {
clearIRQFlags();
return(RADIOLIB_ERR_RX_TIMEOUT);
}
@ -259,9 +260,9 @@ int16_t SX127x::scanChannel() {
RADIOLIB_ASSERT(state);
// wait for channel activity detected or timeout
while(!_mod->digitalRead(_mod->getIrq())) {
_mod->yield();
if(_mod->digitalRead(_mod->getGpio())) {
while(!_mod->hal->digitalRead(_mod->getIrq())) {
_mod->hal->yield();
if(_mod->hal->digitalRead(_mod->getGpio())) {
return(RADIOLIB_PREAMBLE_DETECTED);
}
}
@ -425,31 +426,31 @@ int16_t SX127x::startReceive(uint32_t mode, uint16_t irqFlags, uint16_t irqMask,
return(startReceive((uint8_t)len, (uint8_t)mode));
}
void SX127x::setDio0Action(void (*func)(void), RADIOLIB_INTERRUPT_STATUS dir) {
_mod->attachInterrupt(RADIOLIB_DIGITAL_PIN_TO_INTERRUPT(_mod->getIrq()), func, dir);
void SX127x::setDio0Action(void (*func)(void), uint8_t dir) {
_mod->hal->attachInterrupt(_mod->hal->pinToInterrupt(_mod->getIrq()), func, dir);
}
void SX127x::clearDio0Action() {
_mod->detachInterrupt(RADIOLIB_DIGITAL_PIN_TO_INTERRUPT(_mod->getIrq()));
_mod->hal->detachInterrupt(_mod->hal->pinToInterrupt(_mod->getIrq()));
}
void SX127x::setDio1Action(void (*func)(void), RADIOLIB_INTERRUPT_STATUS dir) {
void SX127x::setDio1Action(void (*func)(void), uint8_t dir) {
if(_mod->getGpio() == RADIOLIB_NC) {
return;
}
_mod->attachInterrupt(RADIOLIB_DIGITAL_PIN_TO_INTERRUPT(_mod->getGpio()), func, dir);
_mod->hal->attachInterrupt(_mod->hal->pinToInterrupt(_mod->getGpio()), func, dir);
}
void SX127x::clearDio1Action() {
if(_mod->getGpio() == RADIOLIB_NC) {
return;
}
_mod->detachInterrupt(RADIOLIB_DIGITAL_PIN_TO_INTERRUPT(_mod->getGpio()));
_mod->hal->detachInterrupt(_mod->hal->pinToInterrupt(_mod->getGpio()));
}
void SX127x::setFifoEmptyAction(void (*func)(void)) {
// set DIO1 to the FIFO empty event (the register setting is done in startTransmit)
setDio1Action(func);
setDio1Action(func, _mod->hal->GpioInterruptRising);
}
void SX127x::clearFifoEmptyAction() {
@ -462,7 +463,7 @@ void SX127x::setFifoFullAction(void (*func)(void)) {
_mod->SPIsetRegValue(RADIOLIB_SX127X_REG_DIO_MAPPING_1, RADIOLIB_SX127X_DIO1_PACK_FIFO_LEVEL, 5, 4);
// set DIO1 to the FIFO full event
setDio1Action(func);
setDio1Action(func, _mod->hal->GpioInterruptRising);
}
void SX127x::clearFifoFullAction() {
@ -1266,11 +1267,11 @@ uint8_t SX127x::getModemStatus() {
return(_mod->SPIreadRegister(RADIOLIB_SX127X_REG_MODEM_STAT));
}
void SX127x::setRfSwitchPins(RADIOLIB_PIN_TYPE rxEn, RADIOLIB_PIN_TYPE txEn) {
void SX127x::setRfSwitchPins(uint8_t rxEn, uint8_t txEn) {
_mod->setRfSwitchPins(rxEn, txEn);
}
void SX127x::setRfSwitchTable(const RADIOLIB_PIN_TYPE (&pins)[Module::RFSWITCH_MAX_PINS], const Module::RfSwitchMode_t table[]) {
void SX127x::setRfSwitchTable(const uint8_t (&pins)[Module::RFSWITCH_MAX_PINS], const Module::RfSwitchMode_t table[]) {
_mod->setRfSwitchTable(pins, table);
}
@ -1285,7 +1286,7 @@ uint8_t SX127x::randomByte() {
setMode(RADIOLIB_SX127X_RX);
// wait a bit for the RSSI reading to stabilise
_mod->delay(10);
_mod->hal->delay(10);
// read RSSI value 8 times, always keep just the least significant bit
uint8_t randByte = 0x00;
@ -1326,7 +1327,7 @@ int8_t SX127x::getTempRaw() {
_mod->SPIsetRegValue(RADIOLIB_SX127X_REG_IMAGE_CAL, RADIOLIB_SX127X_TEMP_MONITOR_ON, 0, 0);
// wait
_mod->delayMicroseconds(200);
_mod->hal->delayMicroseconds(200);
// disable temperature reading
_mod->SPIsetRegValue(RADIOLIB_SX127X_REG_IMAGE_CAL, RADIOLIB_SX127X_TEMP_MONITOR_OFF, 0, 0);
@ -1432,7 +1433,7 @@ bool SX127x::findChip(uint8_t ver) {
flagFound = true;
} else {
RADIOLIB_DEBUG_PRINTLN("SX127x not found! (%d of 10 tries) RADIOLIB_SX127X_REG_VERSION == 0x%04X, expected 0x00%X", i + 1, version, ver);
_mod->delay(10);
_mod->hal->delay(10);
i++;
}
}
@ -1504,11 +1505,11 @@ int16_t SX127x::invertIQ(bool invertIQ) {
#if !defined(RADIOLIB_EXCLUDE_DIRECT_RECEIVE)
void SX127x::setDirectAction(void (*func)(void)) {
setDio1Action(func);
setDio1Action(func, _mod->hal->GpioInterruptRising);
}
void SX127x::readBit(RADIOLIB_PIN_TYPE pin) {
updateDirectBuffer((uint8_t)_mod->digitalRead(pin));
void SX127x::readBit(uint8_t pin) {
updateDirectBuffer((uint8_t)_mod->hal->digitalRead(pin));
}
#endif
@ -1533,7 +1534,7 @@ void SX127x::clearFHSSInt(void) {
}
}
int16_t SX127x::setDIOMapping(RADIOLIB_PIN_TYPE pin, uint8_t value) {
int16_t SX127x::setDIOMapping(uint8_t pin, uint8_t value) {
if (pin > 5)
return RADIOLIB_ERR_INVALID_DIO_PIN;

View file

@ -725,9 +725,9 @@ class SX127x: public PhysicalLayer {
\param func Pointer to interrupt service routine.
\param dir Signal change direction. Defaults to RISING.
\param dir Signal change direction.
*/
void setDio0Action(void (*func)(void), RADIOLIB_INTERRUPT_STATUS dir = RISING);
void setDio0Action(void (*func)(void), uint8_t dir);
/*!
\brief Clears interrupt service routine to call when DIO0 activates.
@ -739,9 +739,9 @@ class SX127x: public PhysicalLayer {
\param func Pointer to interrupt service routine.
\param dir Signal change direction. Defaults to RISING.
\param dir Signal change direction.
*/
void setDio1Action(void (*func)(void), RADIOLIB_INTERRUPT_STATUS dir = RISING);
void setDio1Action(void (*func)(void), uint8_t dir);
/*!
\brief Clears interrupt service routine to call when DIO1 activates.
@ -1155,10 +1155,10 @@ class SX127x: public PhysicalLayer {
int8_t getTempRaw();
/*! \copydoc Module::setRfSwitchPins */
void setRfSwitchPins(RADIOLIB_PIN_TYPE rxEn, RADIOLIB_PIN_TYPE txEn);
void setRfSwitchPins(uint8_t rxEn, uint8_t txEn);
/*! \copydoc Module::setRfSwitchTable */
void setRfSwitchTable(const RADIOLIB_PIN_TYPE (&pins)[Module::RFSWITCH_MAX_PINS], const Module::RfSwitchMode_t table[]);
void setRfSwitchTable(const uint8_t (&pins)[Module::RFSWITCH_MAX_PINS], const Module::RfSwitchMode_t table[]);
/*!
\brief Get one truly random byte from RSSI noise.
@ -1196,7 +1196,7 @@ class SX127x: public PhysicalLayer {
\param pin Pin on which to read.
*/
void readBit(RADIOLIB_PIN_TYPE pin);
void readBit(uint8_t pin);
#endif
/*!
@ -1236,7 +1236,7 @@ class SX127x: public PhysicalLayer {
\returns \ref status_codes
*/
int16_t setDIOMapping(RADIOLIB_PIN_TYPE pin, uint8_t value);
int16_t setDIOMapping(uint8_t pin, uint8_t value);
/*!
\brief Configure DIO mapping to use RSSI or Preamble Detect for pins that support it.

View file

@ -1,4 +1,5 @@
#include "SX1280.h"
#include <string.h>
#if !defined(RADIOLIB_EXCLUDE_SX128X)
SX1280::SX1280(Module* mod) : SX1281(mod) {
@ -11,10 +12,10 @@ int16_t SX1280::range(bool master, uint32_t addr, uint16_t calTable[3][6]) {
RADIOLIB_ASSERT(state);
// wait until ranging is finished
uint32_t start = _mod->millis();
while(!_mod->digitalRead(_mod->getIrq())) {
_mod->yield();
if(_mod->millis() - start > 10000) {
uint32_t start = _mod->hal->millis();
while(!_mod->hal->digitalRead(_mod->getIrq())) {
_mod->hal->yield();
if(_mod->hal->millis() - start > 10000) {
clearIrqStatus();
standby();
return(RADIOLIB_ERR_RANGING_TIMEOUT);

View file

@ -1,4 +1,5 @@
#include "SX128x.h"
#include <math.h>
#if !defined(RADIOLIB_EXCLUDE_SX128X)
SX128x::SX128x(Module* mod) : PhysicalLayer(RADIOLIB_SX128X_FREQUENCY_STEP_SIZE, RADIOLIB_SX128X_MAX_PACKET_LENGTH) {
@ -12,8 +13,8 @@ Module* SX128x::getMod() {
int16_t SX128x::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, int8_t power, uint16_t preambleLength) {
// set module properties
_mod->init();
_mod->pinMode(_mod->getIrq(), INPUT);
_mod->pinMode(_mod->getGpio(), INPUT);
_mod->hal->pinMode(_mod->getIrq(), _mod->hal->GpioModeInput);
_mod->hal->pinMode(_mod->getGpio(), _mod->hal->GpioModeInput);
_mod->SPIreadCommand = RADIOLIB_SX128X_CMD_READ_REGISTER;
_mod->SPIwriteCommand = RADIOLIB_SX128X_CMD_WRITE_REGISTER;
_mod->SPInopCommand = RADIOLIB_SX128X_CMD_NOP;
@ -73,8 +74,8 @@ int16_t SX128x::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t sync
int16_t SX128x::beginGFSK(float freq, uint16_t br, float freqDev, int8_t power, uint16_t preambleLength) {
// set module properties
_mod->init();
_mod->pinMode(_mod->getIrq(), INPUT);
_mod->pinMode(_mod->getGpio(), INPUT);
_mod->hal->pinMode(_mod->getIrq(), _mod->hal->GpioModeInput);
_mod->hal->pinMode(_mod->getGpio(), _mod->hal->GpioModeInput);
_mod->SPIreadCommand = RADIOLIB_SX128X_CMD_READ_REGISTER;
_mod->SPIwriteCommand = RADIOLIB_SX128X_CMD_WRITE_REGISTER;
_mod->SPInopCommand = RADIOLIB_SX128X_CMD_NOP;
@ -142,8 +143,8 @@ int16_t SX128x::beginGFSK(float freq, uint16_t br, float freqDev, int8_t power,
int16_t SX128x::beginBLE(float freq, uint16_t br, float freqDev, int8_t power, uint8_t dataShaping) {
// set module properties
_mod->init();
_mod->pinMode(_mod->getIrq(), INPUT);
_mod->pinMode(_mod->getGpio(), INPUT);
_mod->hal->pinMode(_mod->getIrq(), _mod->hal->GpioModeInput);
_mod->hal->pinMode(_mod->getGpio(), _mod->hal->GpioModeInput);
_mod->SPIreadCommand = RADIOLIB_SX128X_CMD_READ_REGISTER;
_mod->SPIwriteCommand = RADIOLIB_SX128X_CMD_WRITE_REGISTER;
_mod->SPInopCommand = RADIOLIB_SX128X_CMD_NOP;
@ -197,8 +198,8 @@ int16_t SX128x::beginBLE(float freq, uint16_t br, float freqDev, int8_t power, u
int16_t SX128x::beginFLRC(float freq, uint16_t br, uint8_t cr, int8_t power, uint16_t preambleLength, uint8_t dataShaping) {
// set module properties
_mod->init();
_mod->pinMode(_mod->getIrq(), INPUT);
_mod->pinMode(_mod->getGpio(), INPUT);
_mod->hal->pinMode(_mod->getIrq(), _mod->hal->GpioModeInput);
_mod->hal->pinMode(_mod->getGpio(), _mod->hal->GpioModeInput);
_mod->SPIreadCommand = RADIOLIB_SX128X_CMD_READ_REGISTER;
_mod->SPIwriteCommand = RADIOLIB_SX128X_CMD_WRITE_REGISTER;
_mod->SPInopCommand = RADIOLIB_SX128X_CMD_NOP;
@ -261,10 +262,10 @@ int16_t SX128x::beginFLRC(float freq, uint16_t br, uint8_t cr, int8_t power, uin
int16_t SX128x::reset(bool verify) {
// run the reset sequence - same as SX126x, as SX128x docs don't seem to mention this
_mod->pinMode(_mod->getRst(), OUTPUT);
_mod->digitalWrite(_mod->getRst(), LOW);
_mod->delay(1);
_mod->digitalWrite(_mod->getRst(), HIGH);
_mod->hal->pinMode(_mod->getRst(), _mod->hal->GpioModeOutput);
_mod->hal->digitalWrite(_mod->getRst(), _mod->hal->GpioLevelLow);
_mod->hal->delay(1);
_mod->hal->digitalWrite(_mod->getRst(), _mod->hal->GpioLevelHigh);
// return immediately when verification is disabled
if(!verify) {
@ -272,7 +273,7 @@ int16_t SX128x::reset(bool verify) {
}
// set mode to standby
uint32_t start = _mod->millis();
uint32_t start = _mod->hal->millis();
while(true) {
// try to set mode to standby
int16_t state = standby();
@ -282,13 +283,13 @@ int16_t SX128x::reset(bool verify) {
}
// standby command failed, check timeout and try again
if(_mod->millis() - start >= 3000) {
if(_mod->hal->millis() - start >= 3000) {
// timed out, possibly incorrect wiring
return(state);
}
// wait a bit to not spam the module
_mod->delay(10);
_mod->hal->delay(10);
}
}
@ -318,10 +319,10 @@ int16_t SX128x::transmit(uint8_t* data, size_t len, uint8_t addr) {
RADIOLIB_ASSERT(state);
// wait for packet transmission or timeout
uint32_t start = _mod->micros();
while(!_mod->digitalRead(_mod->getIrq())) {
_mod->yield();
if(_mod->micros() - start > timeout) {
uint32_t start = _mod->hal->micros();
while(!_mod->hal->digitalRead(_mod->getIrq())) {
_mod->hal->yield();
if(_mod->hal->micros() - start > timeout) {
finishTransmit();
return(RADIOLIB_ERR_TX_TIMEOUT);
}
@ -352,10 +353,10 @@ int16_t SX128x::receive(uint8_t* data, size_t len) {
RADIOLIB_ASSERT(state);
// wait for packet reception or timeout
uint32_t start = _mod->micros();
while(!_mod->digitalRead(_mod->getIrq())) {
_mod->yield();
if(_mod->micros() - start > timeout) {
uint32_t start = _mod->hal->micros();
while(!_mod->hal->digitalRead(_mod->getIrq())) {
_mod->hal->yield();
if(_mod->hal->micros() - start > timeout) {
clearIrqStatus();
standby();
return(RADIOLIB_ERR_RX_TIMEOUT);
@ -415,8 +416,8 @@ int16_t SX128x::scanChannel() {
RADIOLIB_ASSERT(state);
// wait for channel activity detected or timeout
while(!_mod->digitalRead(_mod->getIrq())) {
_mod->yield();
while(!_mod->hal->digitalRead(_mod->getIrq())) {
_mod->hal->yield();
}
// check CAD result
@ -445,7 +446,7 @@ int16_t SX128x::sleep(bool retainConfig) {
int16_t state = _mod->SPIwriteStream(RADIOLIB_SX128X_CMD_SET_SLEEP, &sleepConfig, 1, false, false);
// wait for SX128x to safely enter sleep mode
_mod->delay(1);
_mod->hal->delay(1);
return(state);
}
@ -463,11 +464,11 @@ int16_t SX128x::standby(uint8_t mode) {
}
void SX128x::setDio1Action(void (*func)(void)) {
_mod->attachInterrupt(RADIOLIB_DIGITAL_PIN_TO_INTERRUPT(_mod->getIrq()), func, RISING);
_mod->hal->attachInterrupt(_mod->hal->pinToInterrupt(_mod->getIrq()), func, _mod->hal->GpioInterruptRising);
}
void SX128x::clearDio1Action() {
_mod->detachInterrupt(RADIOLIB_DIGITAL_PIN_TO_INTERRUPT(_mod->getIrq()));
_mod->hal->detachInterrupt(_mod->hal->pinToInterrupt(_mod->getIrq()));
}
int16_t SX128x::startTransmit(uint8_t* data, size_t len, uint8_t addr) {
@ -527,8 +528,8 @@ int16_t SX128x::startTransmit(uint8_t* data, size_t len, uint8_t addr) {
RADIOLIB_ASSERT(state);
// wait for BUSY to go low (= PA ramp up done)
while(_mod->digitalRead(_mod->getGpio())) {
_mod->yield();
while(_mod->hal->digitalRead(_mod->getGpio())) {
_mod->hal->yield();
}
return(state);
@ -1265,11 +1266,11 @@ int16_t SX128x::setEncoding(uint8_t encoding) {
return(setWhitening(encoding));
}
void SX128x::setRfSwitchPins(RADIOLIB_PIN_TYPE rxEn, RADIOLIB_PIN_TYPE txEn) {
void SX128x::setRfSwitchPins(uint8_t rxEn, uint8_t txEn) {
_mod->setRfSwitchPins(rxEn, txEn);
}
void SX128x::setRfSwitchTable(const RADIOLIB_PIN_TYPE (&pins)[Module::RFSWITCH_MAX_PINS], const Module::RfSwitchMode_t table[]) {
void SX128x::setRfSwitchTable(const uint8_t (&pins)[Module::RFSWITCH_MAX_PINS], const Module::RfSwitchMode_t table[]) {
_mod->setRfSwitchTable(pins, table);
}
@ -1299,7 +1300,7 @@ void SX128x::setDirectAction(void (*func)(void)) {
(void)func;
}
void SX128x::readBit(RADIOLIB_PIN_TYPE pin) {
void SX128x::readBit(uint8_t pin) {
// SX128x is unable to perform direct mode reception
// this method is implemented only for PhysicalLayer compatibility
(void)pin;

View file

@ -817,10 +817,10 @@ class SX128x: public PhysicalLayer {
int16_t setEncoding(uint8_t encoding) override;
/*! \copydoc Module::setRfSwitchPins */
void setRfSwitchPins(RADIOLIB_PIN_TYPE rxEn, RADIOLIB_PIN_TYPE txEn);
void setRfSwitchPins(uint8_t rxEn, uint8_t txEn);
/*! \copydoc Module::setRfSwitchTable */
void setRfSwitchTable(const RADIOLIB_PIN_TYPE (&pins)[Module::RFSWITCH_MAX_PINS], const Module::RfSwitchMode_t table[]);
void setRfSwitchTable(const uint8_t (&pins)[Module::RFSWITCH_MAX_PINS], const Module::RfSwitchMode_t table[]);
/*!
\brief Dummy random method, to ensure PhysicalLayer compatibility.
@ -851,7 +851,7 @@ class SX128x: public PhysicalLayer {
\param pin Ignored.
*/
void readBit(RADIOLIB_PIN_TYPE pin);
void readBit(uint8_t pin);
#endif
#if !defined(RADIOLIB_GODMODE) && !defined(RADIOLIB_LOW_LEVEL)

View file

@ -1,4 +1,5 @@
#include "Si443x.h"
#include <math.h>
#if !defined(RADIOLIB_EXCLUDE_SI443X)
Si443x::Si443x(Module* mod) : PhysicalLayer(RADIOLIB_SI443X_FREQUENCY_STEP_SIZE, RADIOLIB_SI443X_MAX_PACKET_LENGTH) {
@ -12,9 +13,9 @@ Module* Si443x::getMod() {
int16_t Si443x::begin(float br, float freqDev, float rxBw, uint8_t preambleLen) {
// set module properties
_mod->init();
_mod->pinMode(_mod->getIrq(), INPUT);
_mod->pinMode(_mod->getRst(), OUTPUT);
_mod->digitalWrite(_mod->getRst(), LOW);
_mod->hal->pinMode(_mod->getIrq(), _mod->hal->GpioModeInput);
_mod->hal->pinMode(_mod->getRst(), _mod->hal->GpioModeOutput);
_mod->hal->digitalWrite(_mod->getRst(), _mod->hal->GpioLevelLow);
// try to find the Si443x chip
if(!Si443x::findChip()) {
@ -67,11 +68,11 @@ int16_t Si443x::begin(float br, float freqDev, float rxBw, uint8_t preambleLen)
}
void Si443x::reset() {
_mod->pinMode(_mod->getRst(), OUTPUT);
_mod->digitalWrite(_mod->getRst(), HIGH);
_mod->delay(1);
_mod->digitalWrite(_mod->getRst(), LOW);
_mod->delay(100);
_mod->hal->pinMode(_mod->getRst(), _mod->hal->GpioModeOutput);
_mod->hal->digitalWrite(_mod->getRst(), _mod->hal->GpioLevelHigh);
_mod->hal->delay(1);
_mod->hal->digitalWrite(_mod->getRst(), _mod->hal->GpioLevelLow);
_mod->hal->delay(100);
}
int16_t Si443x::transmit(uint8_t* data, size_t len, uint8_t addr) {
@ -83,10 +84,10 @@ int16_t Si443x::transmit(uint8_t* data, size_t len, uint8_t addr) {
RADIOLIB_ASSERT(state);
// wait for transmission end or timeout
uint32_t start = _mod->micros();
while(_mod->digitalRead(_mod->getIrq())) {
_mod->yield();
if(_mod->micros() - start > timeout) {
uint32_t start = _mod->hal->micros();
while(_mod->hal->digitalRead(_mod->getIrq())) {
_mod->hal->yield();
if(_mod->hal->micros() - start > timeout) {
finishTransmit();
return(RADIOLIB_ERR_TX_TIMEOUT);
}
@ -104,9 +105,9 @@ int16_t Si443x::receive(uint8_t* data, size_t len) {
RADIOLIB_ASSERT(state);
// wait for packet reception or timeout
uint32_t start = _mod->micros();
while(_mod->digitalRead(_mod->getIrq())) {
if(_mod->micros() - start > timeout) {
uint32_t start = _mod->hal->micros();
while(_mod->hal->digitalRead(_mod->getIrq())) {
if(_mod->hal->micros() - start > timeout) {
standby();
clearIRQFlags();
return(RADIOLIB_ERR_RX_TIMEOUT);
@ -206,11 +207,11 @@ int16_t Si443x::packetMode() {
}
void Si443x::setIrqAction(void (*func)(void)) {
_mod->attachInterrupt(RADIOLIB_DIGITAL_PIN_TO_INTERRUPT(_mod->getIrq()), func, FALLING);
_mod->hal->attachInterrupt(_mod->hal->pinToInterrupt(_mod->getIrq()), func, _mod->hal->GpioInterruptFalling);
}
void Si443x::clearIrqAction() {
_mod->detachInterrupt(RADIOLIB_DIGITAL_PIN_TO_INTERRUPT(_mod->getIrq()));
_mod->hal->detachInterrupt(_mod->hal->pinToInterrupt(_mod->getIrq()));
}
int16_t Si443x::startTransmit(uint8_t* data, size_t len, uint8_t addr) {
@ -575,11 +576,11 @@ int16_t Si443x::setDataShaping(uint8_t sh) {
}
}
void Si443x::setRfSwitchPins(RADIOLIB_PIN_TYPE rxEn, RADIOLIB_PIN_TYPE txEn) {
void Si443x::setRfSwitchPins(uint8_t rxEn, uint8_t txEn) {
_mod->setRfSwitchPins(rxEn, txEn);
}
void Si443x::setRfSwitchTable(const RADIOLIB_PIN_TYPE (&pins)[Module::RFSWITCH_MAX_PINS], const Module::RfSwitchMode_t table[]) {
void Si443x::setRfSwitchTable(const uint8_t (&pins)[Module::RFSWITCH_MAX_PINS], const Module::RfSwitchMode_t table[]) {
_mod->setRfSwitchTable(pins, table);
}
@ -588,7 +589,7 @@ uint8_t Si443x::randomByte() {
_mod->SPIwriteRegister(RADIOLIB_SI443X_REG_OP_FUNC_CONTROL_1, RADIOLIB_SI443X_RX_ON | RADIOLIB_SI443X_XTAL_ON);
// wait a bit for the RSSI reading to stabilise
_mod->delay(10);
_mod->hal->delay(10);
// read RSSI value 8 times, always keep just the least significant bit
uint8_t randByte = 0x00;
@ -611,8 +612,8 @@ void Si443x::setDirectAction(void (*func)(void)) {
setIrqAction(func);
}
void Si443x::readBit(RADIOLIB_PIN_TYPE pin) {
updateDirectBuffer((uint8_t)_mod->digitalRead(pin));
void Si443x::readBit(uint8_t pin) {
updateDirectBuffer((uint8_t)_mod->hal->digitalRead(pin));
}
#endif
@ -684,7 +685,7 @@ bool Si443x::findChip() {
flagFound = true;
} else {
RADIOLIB_DEBUG_PRINTLN("Si443x not found! (%d of 10 tries) RADIOLIB_SI443X_REG_DEVICE_VERSION == 0x%02X, expected 0x0%X", i + 1, version, RADIOLIB_SI443X_DEVICE_VERSION);
_mod->delay(10);
_mod->hal->delay(10);
i++;
}
}

View file

@ -810,10 +810,10 @@ class Si443x: public PhysicalLayer {
int16_t setDataShaping(uint8_t sh) override;
/*! \copydoc Module::setRfSwitchPins */
void setRfSwitchPins(RADIOLIB_PIN_TYPE rxEn, RADIOLIB_PIN_TYPE txEn);
void setRfSwitchPins(uint8_t rxEn, uint8_t txEn);
/*! \copydoc Module::setRfSwitchTable */
void setRfSwitchTable(const RADIOLIB_PIN_TYPE (&pins)[Module::RFSWITCH_MAX_PINS], const Module::RfSwitchMode_t table[]);
void setRfSwitchTable(const uint8_t (&pins)[Module::RFSWITCH_MAX_PINS], const Module::RfSwitchMode_t table[]);
/*!
\brief Get one truly random byte from RSSI noise.
@ -842,7 +842,7 @@ class Si443x: public PhysicalLayer {
\param pin Pin on which to read.
*/
void readBit(RADIOLIB_PIN_TYPE pin);
void readBit(uint8_t pin);
#endif
/*!

View file

@ -1,4 +1,5 @@
#include "nRF24.h"
#include <string.h>
#if !defined(RADIOLIB_EXCLUDE_NRF24)
nRF24::nRF24(Module* mod) : PhysicalLayer(RADIOLIB_NRF24_FREQUENCY_STEP_SIZE, RADIOLIB_NRF24_MAX_PACKET_LENGTH) {
@ -14,14 +15,14 @@ int16_t nRF24::begin(int16_t freq, int16_t dataRate, int8_t power, uint8_t addrW
_mod->SPIreadCommand = RADIOLIB_NRF24_CMD_READ;
_mod->SPIwriteCommand = RADIOLIB_NRF24_CMD_WRITE;
_mod->init();
_mod->pinMode(_mod->getIrq(), INPUT);
_mod->hal->pinMode(_mod->getIrq(), _mod->hal->GpioModeInput);
// set pin mode on RST (connected to nRF24 CE pin)
_mod->pinMode(_mod->getRst(), OUTPUT);
_mod->digitalWrite(_mod->getRst(), LOW);
_mod->hal->pinMode(_mod->getRst(), _mod->hal->GpioModeOutput);
_mod->hal->digitalWrite(_mod->getRst(), _mod->hal->GpioLevelLow);
// wait for minimum power-on reset duration
_mod->delay(100);
_mod->hal->delay(100);
// check SPI connection
int16_t val = _mod->SPIgetRegValue(RADIOLIB_NRF24_REG_SETUP_AW);
@ -79,7 +80,7 @@ int16_t nRF24::standby(uint8_t mode) {
// make sure carrier output is disabled
_mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RF_SETUP, RADIOLIB_NRF24_CONT_WAVE_OFF, 7, 7);
_mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RF_SETUP, RADIOLIB_NRF24_PLL_LOCK_OFF, 4, 4);
_mod->digitalWrite(_mod->getRst(), LOW);
_mod->hal->digitalWrite(_mod->getRst(), _mod->hal->GpioLevelLow);
// use standby-1 mode
return(_mod->SPIsetRegValue(RADIOLIB_NRF24_REG_CONFIG, mode, 1, 1));
@ -91,9 +92,9 @@ int16_t nRF24::transmit(uint8_t* data, size_t len, uint8_t addr) {
RADIOLIB_ASSERT(state);
// wait until transmission is finished
uint32_t start = _mod->micros();
while(_mod->digitalRead(_mod->getIrq())) {
_mod->yield();
uint32_t start = _mod->hal->micros();
while(_mod->hal->digitalRead(_mod->getIrq())) {
_mod->hal->yield();
// check maximum number of retransmits
if(getStatus(RADIOLIB_NRF24_MAX_RT)) {
@ -102,7 +103,7 @@ int16_t nRF24::transmit(uint8_t* data, size_t len, uint8_t addr) {
}
// check timeout: 15 retries * 4ms (max Tx time as per datasheet)
if(_mod->micros() - start >= 60000) {
if(_mod->hal->micros() - start >= 60000) {
finishTransmit();
return(RADIOLIB_ERR_TX_TIMEOUT);
}
@ -117,12 +118,12 @@ int16_t nRF24::receive(uint8_t* data, size_t len) {
RADIOLIB_ASSERT(state);
// wait for Rx_DataReady or timeout
uint32_t start = _mod->micros();
while(_mod->digitalRead(_mod->getIrq())) {
_mod->yield();
uint32_t start = _mod->hal->micros();
while(_mod->hal->digitalRead(_mod->getIrq())) {
_mod->hal->yield();
// check timeout: 15 retries * 4ms (max Tx time as per datasheet)
if(_mod->micros() - start >= 60000) {
if(_mod->hal->micros() - start >= 60000) {
standby();
clearIRQ();
return(RADIOLIB_ERR_RX_TIMEOUT);
@ -144,7 +145,7 @@ int16_t nRF24::transmitDirect(uint32_t frf) {
int16_t state = _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_CONFIG, RADIOLIB_NRF24_PTX, 0, 0);
state |= _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RF_SETUP, RADIOLIB_NRF24_CONT_WAVE_ON, 7, 7);
state |= _mod->SPIsetRegValue(RADIOLIB_NRF24_REG_RF_SETUP, RADIOLIB_NRF24_PLL_LOCK_ON, 4, 4);
_mod->digitalWrite(_mod->getRst(), HIGH);
_mod->hal->digitalWrite(_mod->getRst(), _mod->hal->GpioLevelHigh);
return(state);
}
@ -155,7 +156,7 @@ int16_t nRF24::receiveDirect() {
}
void nRF24::setIrqAction(void (*func)(void)) {
_mod->attachInterrupt(RADIOLIB_DIGITAL_PIN_TO_INTERRUPT(_mod->getIrq()), func, FALLING);
_mod->hal->attachInterrupt(_mod->hal->pinToInterrupt(_mod->getIrq()), func, _mod->hal->GpioInterruptFalling);
}
int16_t nRF24::startTransmit(uint8_t* data, size_t len, uint8_t addr) {
@ -191,9 +192,9 @@ int16_t nRF24::startTransmit(uint8_t* data, size_t len, uint8_t addr) {
SPIwriteTxPayload(data, len);
// CE high to start transmitting
_mod->digitalWrite(_mod->getRst(), HIGH);
_mod->delay(1);
_mod->digitalWrite(_mod->getRst(), LOW);
_mod->hal->digitalWrite(_mod->getRst(), _mod->hal->GpioLevelHigh);
_mod->hal->delay(1);
_mod->hal->digitalWrite(_mod->getRst(), _mod->hal->GpioLevelLow);
return(state);
}
@ -224,10 +225,10 @@ int16_t nRF24::startReceive() {
SPItransfer(RADIOLIB_NRF24_CMD_FLUSH_RX);
// CE high to start receiving
_mod->digitalWrite(_mod->getRst(), HIGH);
_mod->hal->digitalWrite(_mod->getRst(), _mod->hal->GpioLevelHigh);
// wait to enter Rx state
_mod->delay(1);
_mod->hal->delay(1);
return(state);
}
@ -558,7 +559,7 @@ void nRF24::setDirectAction(void (*func)(void)) {
(void)func;
}
void nRF24::readBit(RADIOLIB_PIN_TYPE pin) {
void nRF24::readBit(uint8_t pin) {
// nRF24 is unable to perform direct mode actions
// this method is implemented only for PhysicalLayer compatibility
(void)pin;
@ -601,7 +602,7 @@ int16_t nRF24::config() {
// power up
_mod->SPIsetRegValue(RADIOLIB_NRF24_REG_CONFIG, RADIOLIB_NRF24_POWER_UP, 1, 1);
_mod->delay(5);
_mod->hal->delay(5);
return(state);
}
@ -616,26 +617,26 @@ void nRF24::SPIwriteTxPayload(uint8_t* data, uint8_t numBytes) {
void nRF24::SPItransfer(uint8_t cmd, bool write, uint8_t* dataOut, uint8_t* dataIn, uint8_t numBytes) {
// start transfer
_mod->digitalWrite(_mod->getCs(), LOW);
_mod->beginTransaction();
_mod->hal->digitalWrite(_mod->getCs(), _mod->hal->GpioLevelLow);
_mod->hal->spiBeginTransaction();
// send command
_mod->transfer(cmd);
_mod->hal->spiTransfer(cmd);
// send data
if(write) {
for(uint8_t i = 0; i < numBytes; i++) {
_mod->transfer(dataOut[i]);
_mod->hal->spiTransfer(dataOut[i]);
}
} else {
for(uint8_t i = 0; i < numBytes; i++) {
dataIn[i] = _mod->transfer(0x00);
dataIn[i] = _mod->hal->spiTransfer(0x00);
}
}
// stop transfer
_mod->endTransaction();
_mod->digitalWrite(_mod->getCs(), HIGH);
_mod->hal->spiEndTransaction();
_mod->hal->digitalWrite(_mod->getCs(), _mod->hal->GpioLevelHigh);
}
#endif

View file

@ -528,7 +528,7 @@ class nRF24: public PhysicalLayer {
\param pin Ignored.
*/
void readBit(RADIOLIB_PIN_TYPE pin);
void readBit(uint8_t pin);
#endif
#if !defined(RADIOLIB_GODMODE) && !defined(RADIOLIB_LOW_LEVEL)

View file

@ -1,7 +1,7 @@
#include "AFSK.h"
#if !defined(RADIOLIB_EXCLUDE_AFSK)
AFSKClient::AFSKClient(PhysicalLayer* phy, RADIOLIB_PIN_TYPE pin): _pin(pin) {
AFSKClient::AFSKClient(PhysicalLayer* phy, uint8_t pin): _pin(pin) {
_phy = phy;
}
@ -20,13 +20,13 @@ int16_t AFSKClient::tone(uint16_t freq, bool autoStart) {
}
Module* mod = _phy->getMod();
mod->tone(_pin, freq);
mod->hal->tone(_pin, freq);
return(RADIOLIB_ERR_NONE);
}
int16_t AFSKClient::noTone(bool keepOn) {
Module* mod = _phy->getMod();
mod->noTone(_pin);
mod->hal->noTone(_pin);
if(keepOn) {
return(0);
}

View file

@ -23,7 +23,7 @@ class AFSKClient {
\param pin The pin that will be used for audio output.
*/
AFSKClient(PhysicalLayer* phy, RADIOLIB_PIN_TYPE pin);
AFSKClient(PhysicalLayer* phy, uint8_t pin);
/*!
\brief Initialization method.
@ -56,7 +56,7 @@ class AFSKClient {
private:
#endif
PhysicalLayer* _phy;
RADIOLIB_PIN_TYPE _pin;
uint8_t _pin;
// allow specific classes access the private PhysicalLayer pointer
friend class RTTYClient;

View file

@ -1,4 +1,7 @@
#include "APRS.h"
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#if !defined(RADIOLIB_EXCLUDE_APRS)
APRSClient::APRSClient(AX25Client* ax) {

View file

@ -1,4 +1,5 @@
#include "AX25.h"
#include <string.h>
#if !defined(RADIOLIB_EXCLUDE_AX25)
AX25Frame::AX25Frame(const char* destCallsign, uint8_t destSSID, const char* srcCallsign, uint8_t srcSSID, uint8_t control)
@ -409,7 +410,7 @@ int16_t AX25Client::sendFrame(AX25Frame* frame) {
// check each bit
for(uint16_t mask = 0x80; mask >= 0x01; mask >>= 1) {
uint32_t start = mod->micros();
uint32_t start = mod->hal->micros();
if(stuffedFrameBuff[i] & mask) {
_audio->tone(_afskMark, false);
} else {

View file

@ -1,8 +1,14 @@
#include "ExternalRadio.h"
#if defined(RADIOLIB_BUILD_ARDUINO)
ExternalRadio::ExternalRadio() : PhysicalLayer(1, 0) {
mod = new Module(RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC);
}
#endif
ExternalRadio::ExternalRadio(Hal *hal) : PhysicalLayer(1, 0) {
mod = new Module(hal, RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC, RADIOLIB_NC);
}
Module* ExternalRadio::getMod() {
return(mod);

View file

@ -3,12 +3,17 @@
#include "../../TypeDef.h"
#include "../../Module.h"
#include "../../ArduinoHal.h"
#include "../PhysicalLayer/PhysicalLayer.h"
class ExternalRadio: public PhysicalLayer {
public:
#if defined(RADIOLIB_BUILD_ARDUINO)
ExternalRadio();
#endif
ExternalRadio(Hal *hal);
Module* getMod();
private:
Module* mod;

View file

@ -1,4 +1,5 @@
#include "FSK4.h"
#include <math.h>
#if !defined(RADIOLIB_EXCLUDE_FSK4)
FSK4Client::FSK4Client(PhysicalLayer* phy) {
@ -80,7 +81,7 @@ size_t FSK4Client::write(uint8_t b) {
void FSK4Client::tone(uint8_t i) {
Module* mod = _phy->getMod();
uint32_t start = mod->micros();
uint32_t start = mod->hal->micros();
transmitDirect(_base + _tones[i], _baseHz + _tonesHz[i]);
mod->waitForMicroseconds(start, _bitDuration);
}

View file

@ -1,4 +1,6 @@
#include "Hellschreiber.h"
#include <string.h>
#include <math.h>
#if !defined(RADIOLIB_EXCLUDE_HELLSCHREIBER)
HellClient::HellClient(PhysicalLayer* phy) {
@ -34,7 +36,7 @@ size_t HellClient::printGlyph(uint8_t* buff) {
bool transmitting = false;
for(uint8_t mask = 0x40; mask >= 0x01; mask >>= 1) {
for(int8_t i = RADIOLIB_HELL_FONT_HEIGHT - 1; i >= 0; i--) {
uint32_t start = mod->micros();
uint32_t start = mod->hal->micros();
if((buff[i] & mask) && (!transmitting)) {
transmitting = true;
transmitDirect(_base, _baseHz);

View file

@ -1,4 +1,7 @@
#include "Morse.h"
#include <string.h>
#include <ctype.h>
#include <math.h>
#if !defined(RADIOLIB_EXCLUDE_MORSE)
MorseClient::MorseClient(PhysicalLayer* phy) {
@ -59,7 +62,7 @@ int MorseClient::read(byte* symbol, byte* len, float low, float high) {
Module* mod = _phy->getMod();
// measure pulse duration in us
uint32_t duration = mod->pulseIn(_audio->_pin, LOW, 4*_basePeriod);
uint32_t duration = mod->hal->pulseIn(_audio->_pin, mod->hal->GpioLevelLow, 4*_basePeriod);
// decide if this is a signal, or pause
if((duration > low*_basePeriod) && (duration < high*_basePeriod)) {
@ -74,8 +77,8 @@ int MorseClient::read(byte* symbol, byte* len, float low, float high) {
if((pauseCounter > 0) && (signalCounter == 1)) {
// start of dot or dash
pauseCounter = 0;
signalStart = mod->millis();
uint32_t pauseLen = mod->millis() - pauseStart;
signalStart = mod->hal->millis();
uint32_t pauseLen = mod->hal->millis() - pauseStart;
if((pauseLen >= low*(float)_letterSpace) && (pauseLen <= high*(float)_letterSpace)) {
return(RADIOLIB_MORSE_CHAR_COMPLETE);
@ -87,8 +90,8 @@ int MorseClient::read(byte* symbol, byte* len, float low, float high) {
} else if((signalCounter > 0) && (pauseCounter == 1)) {
// end of dot or dash
signalCounter = 0;
pauseStart = mod->millis();
uint32_t signalLen = mod->millis() - signalStart;
pauseStart = mod->hal->millis();
uint32_t signalLen = mod->hal->millis() - signalStart;
if((signalLen >= low*(float)_dotLength) && (signalLen <= high*(float)_dotLength)) {
RADIOLIB_DEBUG_PRINT(".");
@ -135,7 +138,7 @@ size_t MorseClient::write(uint8_t b) {
if(b == ' ') {
RADIOLIB_DEBUG_PRINTLN("space");
standby();
mod->waitForMicroseconds(mod->micros(), _wordSpace*1000);
mod->waitForMicroseconds(mod->hal->micros(), _wordSpace*1000);
return(1);
}
@ -154,16 +157,16 @@ size_t MorseClient::write(uint8_t b) {
if (code & RADIOLIB_MORSE_DASH) {
RADIOLIB_DEBUG_PRINT("-");
transmitDirect(_base, _baseHz);
mod->waitForMicroseconds(mod->micros(), _dashLength*1000);
mod->waitForMicroseconds(mod->hal->micros(), _dashLength*1000);
} else {
RADIOLIB_DEBUG_PRINT(".");
transmitDirect(_base, _baseHz);
mod->waitForMicroseconds(mod->micros(), _dotLength*1000);
mod->waitForMicroseconds(mod->hal->micros(), _dotLength*1000);
}
// symbol space
standby();
mod->waitForMicroseconds(mod->micros(), _dotLength*1000);
mod->waitForMicroseconds(mod->hal->micros(), _dotLength*1000);
// move onto the next bit
code >>= 1;
@ -171,7 +174,7 @@ size_t MorseClient::write(uint8_t b) {
// letter space
standby();
mod->waitForMicroseconds(mod->micros(), _letterSpace*1000 - _dotLength*1000);
mod->waitForMicroseconds(mod->hal->micros(), _letterSpace*1000 - _dotLength*1000);
RADIOLIB_DEBUG_PRINTLN();
return(1);

View file

@ -1,11 +1,13 @@
#include "Pager.h"
#include <string.h>
#include <math.h>
#if !defined(RADIOLIB_EXCLUDE_PAGER)
#if !defined(RADIOLIB_EXCLUDE_DIRECT_RECEIVE)
// this is a massive hack, but we need a global-scope ISR to manage the bit reading
// let's hope nobody ever tries running two POCSAG receivers at the same time
static PhysicalLayer* _readBitInstance = NULL;
static RADIOLIB_PIN_TYPE _readBitPin = RADIOLIB_NC;
static uint8_t _readBitPin = RADIOLIB_NC;
#if defined(ESP8266) || defined(ESP32)
ICACHE_RAM_ATTR
@ -221,7 +223,7 @@ int16_t PagerClient::transmit(uint8_t* data, size_t len, uint32_t addr, uint8_t
}
#if !defined(RADIOLIB_EXCLUDE_DIRECT_RECEIVE)
int16_t PagerClient::startReceive(RADIOLIB_PIN_TYPE pin, uint32_t addr, uint32_t mask) {
int16_t PagerClient::startReceive(uint8_t pin, uint32_t addr, uint32_t mask) {
// save the variables
_readBitPin = pin;
_filterAddr = addr;
@ -241,7 +243,7 @@ int16_t PagerClient::startReceive(RADIOLIB_PIN_TYPE pin, uint32_t addr, uint32_t
// now set up the direct mode reception
Module* mod = _phy->getMod();
mod->pinMode(pin, INPUT);
mod->hal->pinMode(pin, mod->hal->GpioModeInput);
// set direct sync word to the frame sync word
// the logic here is inverted, because modules like SX1278
@ -453,7 +455,7 @@ void PagerClient::write(uint32_t codeWord) {
Module* mod = _phy->getMod();
for(int8_t i = 31; i >= 0; i--) {
uint32_t mask = (uint32_t)0x01 << i;
uint32_t start = mod->micros();
uint32_t start = mod->hal->micros();
// figure out the shift direction - start by assuming the bit is 0
int16_t change = _shift;
@ -471,12 +473,12 @@ void PagerClient::write(uint32_t codeWord) {
// now transmit the shifted frequency
_phy->transmitDirect(_baseRaw + change);
// this is pretty silly, while(mod->micros() ... ) would be enough
// this is pretty silly, while(mod->hal->micros() ... ) would be enough
// but for some reason, MegaCore throws a linker error on it
// "relocation truncated to fit: R_AVR_7_PCREL against `no symbol'"
uint32_t now = mod->micros();
uint32_t now = mod->hal->micros();
while(now - start < _bitDuration) {
now = mod->micros();
now = mod->hal->micros();
}
}
}

View file

@ -157,7 +157,7 @@ class PagerClient {
\returns \ref status_codes
*/
int16_t startReceive(RADIOLIB_PIN_TYPE pin, uint32_t addr, uint32_t mask = 0xFFFFF);
int16_t startReceive(uint8_t pin, uint32_t addr, uint32_t mask = 0xFFFFF);
/*!
\brief Get the number of POCSAG batches available in buffer. Limited by the size of direct mode buffer!

View file

@ -1,4 +1,5 @@
#include "PhysicalLayer.h"
#include <string.h>
PhysicalLayer::PhysicalLayer(float freqStep, size_t maxPacketLength) {
_freqStep = freqStep;
@ -379,13 +380,13 @@ void PhysicalLayer::setDirectAction(void (*func)(void)) {
(void)func;
}
void PhysicalLayer::readBit(RADIOLIB_PIN_TYPE pin) {
void PhysicalLayer::readBit(uint8_t pin) {
(void)pin;
}
#endif
int16_t PhysicalLayer::setDIOMapping(RADIOLIB_PIN_TYPE pin, uint8_t value) {
int16_t PhysicalLayer::setDIOMapping(uint8_t pin, uint8_t value) {
(void)pin;
(void)value;
return(RADIOLIB_ERR_UNSUPPORTED);

View file

@ -360,7 +360,7 @@ class PhysicalLayer {
\param pin Pin on which to read.
*/
virtual void readBit(RADIOLIB_PIN_TYPE pin);
virtual void readBit(uint8_t pin);
/*!
\brief Get the number of direct mode bytes currently available in buffer.
@ -393,7 +393,7 @@ class PhysicalLayer {
\returns \ref status_codes
*/
virtual int16_t setDIOMapping(RADIOLIB_PIN_TYPE pin, uint8_t value);
virtual int16_t setDIOMapping(uint8_t pin, uint8_t value);
/*!
\brief Sets interrupt service routine to call when DIO1 activates.

View file

@ -1,4 +1,6 @@
#include "RTTY.h"
#include <string.h>
#include <math.h>
#if !defined(RADIOLIB_EXCLUDE_RTTY)
ITA2String::ITA2String(char c) {
@ -409,14 +411,14 @@ size_t RTTYClient::println(double d, int digits) {
void RTTYClient::mark() {
Module* mod = _phy->getMod();
uint32_t start = mod->micros();
uint32_t start = mod->hal->micros();
transmitDirect(_base + _shift, _baseHz + _shiftHz);
mod->waitForMicroseconds(start, _bitDuration);
}
void RTTYClient::space() {
Module* mod = _phy->getMod();
uint32_t start = mod->micros();
uint32_t start = mod->hal->micros();
transmitDirect(_base, _baseHz);
mod->waitForMicroseconds(start, _bitDuration);
}

View file

@ -291,7 +291,7 @@ uint16_t SSTVClient::getPictureHeight() const {
void SSTVClient::tone(float freq, uint32_t len) {
Module* mod = _phy->getMod();
uint32_t start = mod->micros();
uint32_t start = mod->hal->micros();
#if !defined(RADIOLIB_EXCLUDE_AFSK)
if(_audio != nullptr) {
_audio->tone(freq, false);