RadioLib/src/Module.h

502 wiersze
15 KiB
C
Czysty Zwykły widok Historia

2019-02-08 14:58:29 +00:00
#ifndef _RADIOLIB_MODULE_H
#define _RADIOLIB_MODULE_H
2018-03-05 16:08:42 +00:00
#include "TypeDef.h"
2018-03-05 16:08:42 +00:00
#include <SPI.h>
2019-11-20 15:22:50 +00:00
#ifndef RADIOLIB_SOFTWARE_SERIAL_UNSUPPORTED
2018-03-05 16:08:42 +00:00
#include <SoftwareSerial.h>
#endif
2018-03-05 16:08:42 +00:00
2019-05-24 15:32:11 +00:00
/*!
\class Module
\brief Implements all common low-level SPI/UART/I2C methods to control the wireless module.
Every module class contains one private instance of this class.
*/
2018-03-05 16:08:42 +00:00
class Module {
public:
2019-05-24 15:32:11 +00:00
/*!
\brief UART-based module constructor.
\param rx Arduino pin to be used as Rx pin for SoftwareSerial communication.
2020-07-04 11:43:39 +00:00
\param tx Arduino pin to be used as Tx pin for SoftwareSerial communication.
\param serial HardwareSerial to be used on platforms that do not support SoftwareSerial. Defaults to Serial1.
\param rst Arduino pin to be used as hardware reset for the module. Defaults to NC (unused).
2019-05-24 15:32:11 +00:00
*/
2019-11-20 15:22:50 +00:00
#ifdef RADIOLIB_SOFTWARE_SERIAL_UNSUPPORTED
2020-07-04 11:43:39 +00:00
Module(RADIOLIB_PIN_TYPE rx, RADIOLIB_PIN_TYPE tx, HardwareSerial* serial = &RADIOLIB_HARDWARE_SERIAL_PORT, RADIOLIB_PIN_TYPE rst = RADIOLIB_NC);
#else
2020-07-04 11:43:39 +00:00
Module(RADIOLIB_PIN_TYPE rx, RADIOLIB_PIN_TYPE tx, HardwareSerial* serial = nullptr, RADIOLIB_PIN_TYPE rst = RADIOLIB_NC);
#endif
2019-05-24 15:32:11 +00:00
/*!
\brief SPI-based module constructor. Will use the default SPI interface automatically initialize it.
\param cs Arduino pin to be used as chip select.
\param irq Arduino pin to be used as interrupt/GPIO.
\param rst Arduino pin to be used as hardware reset for the module.
*/
2020-03-27 13:10:45 +00:00
Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst);
/*!
\brief Extended SPI-based module constructor. Will use the default SPI interface automatically initialize it.
\param cs Arduino pin to be used as chip select.
\param irq Arduino pin to be used as 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.
*/
2020-03-27 13:10:45 +00:00
Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE gpio);
2019-05-24 15:32:11 +00:00
/*!
\brief SPI-based module constructor.
\param cs Arduino pin to be used as chip select.
\param irq Arduino pin to be used as interrupt/GPIO.
2019-05-24 15:32:11 +00:00
\param rst Arduino pin to be used as hardware reset for the module.
2019-05-24 15:32:11 +00:00
\param spi SPI interface to be used, can also use software SPI implementations.
2019-05-24 15:32:11 +00:00
\param spiSettings SPI interface settings.
2019-05-24 15:32:11 +00:00
*/
2020-03-27 13:10:45 +00:00
Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, SPIClass& spi, SPISettings spiSettings);
2019-05-24 15:32:11 +00:00
/*!
\brief Extended SPI-based module constructor.
\param cs Arduino pin to be used as chip select.
\param irq Arduino pin to be used as interrupt/GPIO.
2019-05-24 15:32:11 +00:00
\param rst Arduino pin to be used as hardware reset for the module.
2019-05-24 15:32:11 +00:00
\param gpio Arduino pin to be used as additional interrupt/GPIO.
2019-05-24 15:32:11 +00:00
\param spi SPI interface to be used, can also use software SPI implementations.
2019-05-24 15:32:11 +00:00
\param spiSettings SPI interface settings. Defaults to 2 MHz clock, MSB first, mode 0.
2019-05-24 15:32:11 +00:00
*/
Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE gpio, SPIClass& spi, SPISettings spiSettings = SPISettings(2000000, MSBFIRST, SPI_MODE0));
2019-05-24 15:32:11 +00:00
/*!
\brief Generic module constructor.
\param cs Arduino pin to be used as chip select.
\param irq Arduino pin to be used as interrupt/GPIO.
2019-05-24 15:32:11 +00:00
\param rst Arduino pin to be used as hardware reset for the module.
2019-05-24 15:32:11 +00:00
\param tx Arduino pin to be used as Tx pin for SoftwareSerial communication.
\param rx Arduino pin to be used as Rx pin for SoftwareSerial communication.
\param spi SPI interface to be used. Defaults to Arduino hardware SPI interface, can also use software SPI implementations.
\param spiSettings SPI interface settings. Defaults to 2 MHz clock, MSB first, mode 0.
\param serial HardwareSerial to be used on ESP32 and SAMD. Defaults to 1
2019-05-24 15:32:11 +00:00
*/
2019-11-20 15:22:50 +00:00
#ifdef RADIOLIB_SOFTWARE_SERIAL_UNSUPPORTED
Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE rx, RADIOLIB_PIN_TYPE tx, SPIClass& spi = RADIOLIB_DEFAULT_SPI, SPISettings spiSettings = SPISettings(2000000, MSBFIRST, SPI_MODE0), HardwareSerial* serial = &RADIOLIB_HARDWARE_SERIAL_PORT);
#else
Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE rx, RADIOLIB_PIN_TYPE tx, SPIClass& spi = RADIOLIB_DEFAULT_SPI, SPISettings spiSettings = SPISettings(2000000, MSBFIRST, SPI_MODE0), HardwareSerial* serial = nullptr);
#endif
/*!
\brief Copy constructor.
\param mod Module instance to copy.
*/
Module(const Module& mod);
/*!
\brief Overload for assignment operator.
\param frame rvalue Module.
*/
Module& operator=(const Module& mod);
2019-05-13 13:03:09 +00:00
2019-05-24 15:32:11 +00:00
// public member variables
/*!
\brief Internal SoftwareSerial instance.
*/
2019-11-20 15:22:50 +00:00
#ifdef RADIOLIB_SOFTWARE_SERIAL_UNSUPPORTED
HardwareSerial* ModuleSerial;
#else
2018-03-05 16:08:42 +00:00
SoftwareSerial* ModuleSerial;
#endif
2019-05-13 13:03:09 +00:00
2019-05-24 15:32:11 +00:00
/*!
\brief Baud rate of SoftwareSerial UART communication. Defaults to 9600 baud.
*/
2018-03-05 16:08:42 +00:00
uint32_t baudrate = 9600;
2019-05-24 15:32:11 +00:00
/*!
\brief Line feed to be used when sending AT commands. Defaults to CR+LF.
*/
char AtLineFeed[3] = {'\r', '\n'};
2019-05-13 13:03:09 +00:00
2019-05-24 15:32:11 +00:00
/*!
\brief Basic SPI read command. Defaults to 0x00.
*/
2018-09-29 16:56:50 +00:00
uint8_t SPIreadCommand = 0b00000000;
2019-05-24 15:32:11 +00:00
/*!
\brief Basic SPI write command. Defaults to 0x80.
*/
2018-09-29 16:56:50 +00:00
uint8_t SPIwriteCommand = 0b10000000;
2019-05-13 13:03:09 +00:00
2019-05-24 15:32:11 +00:00
// basic methods
/*!
\brief Initialize low-level module control.
\param interface Interface to be used on the module. See \ref shield_config for details.
*/
void init(uint8_t interface);
2019-05-24 15:32:11 +00:00
/*!
\brief Terminate low-level module control.
\param interface Interface to be terminated. See \ref shield_config for details.
2019-05-24 15:32:11 +00:00
*/
void term(uint8_t interface);
2019-05-13 13:03:09 +00:00
2019-05-24 15:32:11 +00:00
// AT methods
/*!
\brief Empty internal AT buffer.
*/
2018-03-05 16:08:42 +00:00
void ATemptyBuffer();
2019-05-24 15:32:11 +00:00
/*!
\brief Get response after sending AT command.
\returns True if AT response contains the string "OK", false otherwise.
2019-05-24 15:32:11 +00:00
*/
2018-03-05 16:08:42 +00:00
bool ATgetResponse();
2019-05-24 15:32:11 +00:00
/*!
\brief Send AT command. Will also call ATgetResponse.
\param cmd AT command to be sent. Line feed characters are added automatically.
\returns True if AT response contains the string "OK", false otherwise.
*/
2018-03-05 16:08:42 +00:00
bool ATsendCommand(const char* cmd);
2019-05-24 15:32:11 +00:00
/*!
\brief Send raw AT data. Will also call ATgetResponse.
\param data Data to be sent.
\param len Number of bytes to send.
\returns True if AT response contains the string "OK", false otherwise.
*/
bool ATsendData(uint8_t* data, uint32_t len);
2019-05-13 13:03:09 +00:00
2019-05-24 15:32:11 +00:00
// SPI methods
/*!
\brief SPI read method that automatically masks unused bits. This method is the preferred SPI read mechanism.
\param reg Address of SPI register to read.
\param msb Most significant bit of the register variable. Bits above this one will be masked out.
\param lsb Least significant bit of the register variable. Bits below this one will be masked out.
\returns Masked register value or status code.
*/
2018-07-23 09:19:34 +00:00
int16_t SPIgetRegValue(uint8_t reg, uint8_t msb = 7, uint8_t lsb = 0);
2019-05-24 15:32:11 +00:00
/*!
\brief Overwrite-safe SPI write method with verification. This method is the preferred SPI write mechanism.
\param reg Address of SPI register to write.
\param value Single byte value that will be written to the SPI register.
\param msb Most significant bit of the register variable. Bits above this one will not be affected by the write operation.
\param lsb Least significant bit of the register variable. Bits below this one will not be affected by the write operation.
\param checkInterval Number of milliseconds between register writing and verification reading. Some registers need up to 10ms to process the change.
2021-04-15 17:34:53 +00:00
\param checkMask Mask of bits to check, only bits set to 1 will be verified.
2019-05-24 15:32:11 +00:00
\returns \ref status_codes
*/
2021-04-15 17:34:53 +00:00
int16_t SPIsetRegValue(uint8_t reg, uint8_t value, uint8_t msb = 7, uint8_t lsb = 0, uint8_t checkInterval = 2, uint8_t checkMask = 0xFF);
2019-05-13 13:03:09 +00:00
2019-05-24 15:32:11 +00:00
/*!
\brief SPI burst read method.
\param reg Address of SPI register to read.
\param numBytes Number of bytes that will be read.
\param inBytes Pointer to array that will hold the read data.
*/
2018-07-23 09:19:34 +00:00
void SPIreadRegisterBurst(uint8_t reg, uint8_t numBytes, uint8_t* inBytes);
2019-05-24 15:32:11 +00:00
/*!
\brief SPI basic read method. Use of this method is reserved for special cases, SPIgetRegValue should be used instead.
\param reg Address of SPI register to read.
\returns Value that was read from register.
*/
2018-03-05 16:08:42 +00:00
uint8_t SPIreadRegister(uint8_t reg);
2019-05-13 13:03:09 +00:00
2019-05-24 15:32:11 +00:00
/*!
\brief SPI burst write method.
\param reg Address of SPI register to write.
\param data Pointer to array that holds the data that will be written.
\param numBytes Number of bytes that will be written.
*/
2018-03-05 16:08:42 +00:00
void SPIwriteRegisterBurst(uint8_t reg, uint8_t* data, uint8_t numBytes);
2019-05-24 15:32:11 +00:00
/*!
\brief SPI basic write method. Use of this method is reserved for special cases, SPIsetRegValue should be used instead.
\param reg Address of SPI register to write.
\param data Value that will be written to the register.
*/
2018-03-05 16:08:42 +00:00
void SPIwriteRegister(uint8_t reg, uint8_t data);
2019-05-13 13:03:09 +00:00
2019-05-24 15:32:11 +00:00
/*!
\brief SPI single transfer method.
\param cmd SPI access command (read/write/burst/...).
\param reg Address of SPI register to transfer to/from.
\param dataOut Data that will be transfered from master to slave.
\param dataIn Data that was transfered from slave to master.
\param numBytes Number of bytes to transfer.
*/
2019-03-22 18:01:56 +00:00
void SPItransfer(uint8_t cmd, uint8_t reg, uint8_t* dataOut, uint8_t* dataIn, uint8_t numBytes);
2019-05-13 13:03:09 +00:00
2019-05-24 15:32:11 +00:00
// pin number access methods
/*!
\brief Access method to get the pin number of SPI chip select.
\returns Pin number of SPI chip select configured in the constructor.
*/
2020-03-27 13:10:45 +00:00
RADIOLIB_PIN_TYPE getCs() const { return(_cs); }
2019-05-24 15:32:11 +00:00
/*!
\brief Access method to get the pin number of interrupt/GPIO.
2019-05-24 15:32:11 +00:00
\returns Pin number of interrupt/GPIO configured in the constructor.
2019-05-24 15:32:11 +00:00
*/
2020-03-27 13:10:45 +00:00
RADIOLIB_PIN_TYPE getIrq() const { return(_irq); }
2019-05-24 15:32:11 +00:00
/*!
\brief Access method to get the pin number of hardware reset pin.
2019-05-24 15:32:11 +00:00
\returns Pin number of hardware reset pin configured in the constructor.
2019-05-24 15:32:11 +00:00
*/
2020-03-27 13:10:45 +00:00
RADIOLIB_PIN_TYPE 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.
*/
2020-03-27 13:10:45 +00:00
RADIOLIB_PIN_TYPE getGpio() const { return(_rx); }
2019-05-24 15:32:11 +00:00
/*!
\brief Access method to get the pin number of UART Rx.
\returns Pin number of UART Rx configured in the constructor.
*/
2020-03-27 13:10:45 +00:00
RADIOLIB_PIN_TYPE getRx() const { return(_rx); }
2019-05-24 15:32:11 +00:00
/*!
\brief Access method to get the pin number of UART Rx.
\returns Pin number of UART Rx configured in the constructor.
*/
2020-03-27 13:10:45 +00:00
RADIOLIB_PIN_TYPE getTx() const { return(_tx); }
2019-05-24 15:32:11 +00:00
/*!
\brief Access method to get the SPI interface.
\returns SPI interface configured in the constructor.
*/
SPIClass* getSpi() const { return(_spi); }
2019-05-24 15:32:11 +00:00
/*!
\brief Access method to get the SPI interface settings.
\returns SPI interface settings configured in the constructor.
*/
SPISettings getSpiSettings() const { return(_spiSettings); }
2019-05-13 13:03:09 +00:00
/*!
\brief Some modules contain external RF switch controlled by two pins. This function gives RadioLib control over those two pins to automatically switch Rx and Tx state.
When using automatic RF switch control, DO NOT change the pin mode of rxEn or txEn from Arduino sketch!
\param rxEn RX enable pin.
\param txEn TX enable pin.
*/
void setRfSwitchPins(RADIOLIB_PIN_TYPE rxEn, RADIOLIB_PIN_TYPE txEn);
/*!
\brief Set RF switch state.
\param rxPinState Pin state to set on Tx enable pin (usually high to transmit).
\param txPinState Pin state to set on Rx enable pin (usually high to receive).
*/
void setRfSwitchState(RADIOLIB_PIN_STATUS rxPinState, RADIOLIB_PIN_STATUS txPinState);
// Arduino core overrides
/*!
2020-03-27 13:10:45 +00:00
\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.
*/
2020-03-27 13:10:45 +00:00
static void pinMode(RADIOLIB_PIN_TYPE pin, RADIOLIB_PIN_MODE mode);
/*!
2020-03-27 13:10:45 +00:00
\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.
*/
2020-03-27 13:10:45 +00:00
static 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.
*/
static RADIOLIB_PIN_STATUS digitalRead(RADIOLIB_PIN_TYPE pin);
2020-04-30 15:07:28 +00:00
/*!
\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.
*/
static void tone(RADIOLIB_PIN_TYPE pin, uint16_t value);
/*!
\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.
*/
static void noTone(RADIOLIB_PIN_TYPE pin);
2020-06-18 14:31:38 +00:00
/*!
\brief Arduino core attachInterrupt override.
2020-06-18 14:31:38 +00:00
\param interruptNum Interrupt number.
2020-06-18 14:31:38 +00:00
\param userFunc Interrupt service routine.
\param mode Pin hcange direction.
2020-06-18 14:31:38 +00:00
*/
static void attachInterrupt(RADIOLIB_PIN_TYPE interruptNum, void (*userFunc)(void), RADIOLIB_INTERRUPT_STATUS mode);
2020-06-18 14:31:38 +00:00
/*!
\brief Arduino core detachInterrupt override.
2020-06-18 14:31:38 +00:00
\param interruptNum Interrupt number.
*/
static void detachInterrupt(RADIOLIB_PIN_TYPE interruptNum);
/*!
\brief Arduino core yield override.
2020-06-18 14:31:38 +00:00
*/
static void yield();
/*!
\brief Arduino core delay override.
\param ms Delay length in milliseconds.
*/
static void delay(uint32_t ms);
/*!
\brief Arduino core delayMicroseconds override.
\param us Delay length in microseconds.
*/
static void delayMicroseconds(uint32_t us);
/*!
\brief Arduino core millis override.
*/
static uint32_t millis();
/*!
\brief Arduino core micros override.
*/
static uint32_t micros();
2020-06-18 14:31:38 +00:00
2021-06-14 18:59:16 +00:00
/*!
\brief Function to reflect bits within a byte.
*/
static uint8_t flipBits(uint8_t b);
/*!
\brief Function to reflect bits within an integer.
*/
static uint16_t flipBits16(uint16_t i);
2019-11-20 15:55:11 +00:00
#ifndef RADIOLIB_GODMODE
2018-03-05 16:08:42 +00:00
private:
2019-11-20 15:55:11 +00:00
#endif
2020-07-04 11:43:39 +00:00
RADIOLIB_PIN_TYPE _cs = RADIOLIB_NC;
RADIOLIB_PIN_TYPE _irq = RADIOLIB_NC;
RADIOLIB_PIN_TYPE _rst = RADIOLIB_NC;
2020-07-04 14:05:56 +00:00
RADIOLIB_PIN_TYPE _rx = RADIOLIB_NC;
RADIOLIB_PIN_TYPE _tx = RADIOLIB_NC;
SPISettings _spiSettings = SPISettings(2000000, MSBFIRST, SPI_MODE0);
2020-07-04 11:43:39 +00:00
bool _initInterface = false;
SPIClass* _spi = NULL;
2019-05-13 13:03:09 +00:00
2020-06-18 14:31:38 +00:00
bool _useRfSwitch = false;
2020-07-04 11:43:39 +00:00
RADIOLIB_PIN_TYPE _rxEn = RADIOLIB_NC, _txEn = RADIOLIB_NC;
2020-06-18 14:31:38 +00:00
2018-03-05 16:08:42 +00:00
uint32_t _ATtimeout = 15000;
};
#endif