[Module] Only initializing default SPI interface (#121)

pull/127/head
jgromes 2020-03-02 13:25:35 +01:00
rodzic b7c21ffac6
commit 5846c9d9d9
2 zmienionych plików z 70 dodań i 12 usunięć

Wyświetl plik

@ -1,11 +1,34 @@
#include "Module.h"
Module::Module(int16_t cs, int16_t irq, int16_t rst) {
_cs = cs;
_rx = NC;
_tx = NC;
_irq = irq;
_rst = rst;
_spi = &SPI;
_spiSettings = SPISettings(2000000, MSBFIRST, SPI_MODE0);
_initInterface = true;
}
Module::Module(int16_t cs, int16_t irq, int16_t rst, int16_t gpio) {
_cs = cs;
_rx = gpio;
_tx = NC;
_irq = irq;
_rst = rst;
_spi = &SPI;
_spiSettings = SPISettings(2000000, MSBFIRST, SPI_MODE0);
_initInterface = true;
}
Module::Module(int16_t rx, int16_t tx, HardwareSerial* useSer, int16_t rst) {
_cs = NC;
_rx = rx;
_tx = tx;
_irq = NC;
_rst = rst;
_initInterface = true;
#ifdef RADIOLIB_SOFTWARE_SERIAL_UNSUPPORTED
ModuleSerial = useSer;
@ -23,6 +46,7 @@ Module::Module(int16_t cs, int16_t irq, int16_t rst, SPIClass& spi, SPISettings
_rst = rst;
_spi = &spi;
_spiSettings = spiSettings;
_initInterface = false;
}
Module::Module(int16_t cs, int16_t irq, int16_t rst, int16_t gpio, SPIClass& spi, SPISettings spiSettings) {
@ -33,6 +57,7 @@ Module::Module(int16_t cs, int16_t irq, int16_t rst, int16_t gpio, SPIClass& spi
_rst = rst;
_spi = &spi;
_spiSettings = spiSettings;
_initInterface = false;
}
Module::Module(int16_t cs, int16_t irq, int16_t rst, int16_t rx, int16_t tx, SPIClass& spi, SPISettings spiSettings, HardwareSerial* useSer) {
@ -43,6 +68,7 @@ Module::Module(int16_t cs, int16_t irq, int16_t rst, int16_t rx, int16_t tx, SPI
_rst = rst;
_spi = &spi;
_spiSettings = spiSettings;
_initInterface = false;
#ifdef RADIOLIB_SOFTWARE_SERIAL_UNSUPPORTED
ModuleSerial = useSer;
@ -58,14 +84,18 @@ void Module::init(uint8_t interface) {
case RADIOLIB_USE_SPI:
Module::pinMode(_cs, OUTPUT);
Module::digitalWrite(_cs, HIGH);
_spi->begin();
if(_initInterface) {
_spi->begin();
}
break;
case RADIOLIB_USE_UART:
if(_initInterface) {
#if defined(ESP32)
ModuleSerial->begin(baudrate, SERIAL_8N1, _rx, _tx);
ModuleSerial->begin(baudrate, SERIAL_8N1, _rx, _tx);
#else
ModuleSerial->begin(baudrate);
ModuleSerial->begin(baudrate);
#endif
}
break;
case RADIOLIB_USE_I2C:
break;
@ -211,6 +241,7 @@ void Module::SPItransfer(uint8_t cmd, uint8_t reg, uint8_t* dataOut, uint8_t* da
// send SPI register address with access command
_spi->transfer(reg | cmd);
delayMicroseconds(120);
#ifdef RADIOLIB_VERBOSE
if(cmd == SPIwriteCommand) {
RADIOLIB_VERBOSE_PRINT('W');
@ -226,12 +257,14 @@ void Module::SPItransfer(uint8_t cmd, uint8_t reg, uint8_t* dataOut, uint8_t* da
if(cmd == SPIwriteCommand) {
for(size_t n = 0; n < numBytes; n++) {
_spi->transfer(dataOut[n]);
delayMicroseconds(120);
RADIOLIB_VERBOSE_PRINT(dataOut[n], HEX);
RADIOLIB_VERBOSE_PRINT('\t');
}
} else if (cmd == SPIreadCommand) {
for(size_t n = 0; n < numBytes; n++) {
dataIn[n] = _spi->transfer(0x00);
delayMicroseconds(120);
RADIOLIB_VERBOSE_PRINT(dataIn[n], HEX);
RADIOLIB_VERBOSE_PRINT('\t');
}

Wyświetl plik

@ -27,14 +27,38 @@ class Module {
\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 -1 (unused).
\param rst Arduino pin to be used as hardware reset for the module. Defaults to NC (unused).
*/
#ifdef RADIOLIB_SOFTWARE_SERIAL_UNSUPPORTED
Module(int16_t tx, int16_t rx, HardwareSerial* serial = &Serial1, int16_t rst = -1);
Module(int16_t tx, int16_t rx, HardwareSerial* serial = &Serial1, int16_t rst = NC);
#else
Module(int16_t tx, int16_t rx, HardwareSerial* serial = nullptr, int16_t rst = -1);
Module(int16_t tx, int16_t rx, HardwareSerial* serial = nullptr, int16_t rst = NC);
#endif
/*!
\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.
*/
Module(int16_t cs, int16_t irq, int16_t 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.
*/
Module(int16_t cs, int16_t irq, int16_t rst, int16_t gpio);
/*!
\brief SPI-based module constructor.
@ -44,11 +68,11 @@ class Module {
\param rst Arduino pin to be used as hardware reset for the module.
\param spi SPI interface to be used. Defaults to Arduino hardware SPI interface, can also use software SPI implementations.
\param spi SPI interface to be used, can also use software SPI implementations.
\param spiSettings SPI interface settings. Defaults to 2 MHz clock, MSB first, mode 0.
\param spiSettings SPI interface settings.
*/
Module(int16_t cs, int16_t irq, int16_t rst, SPIClass& spi = SPI, SPISettings spiSettings = SPISettings(2000000, MSBFIRST, SPI_MODE0));
Module(int16_t cs, int16_t irq, int16_t rst, SPIClass& spi, SPISettings spiSettings);
/*!
\brief Extended SPI-based module constructor.
@ -61,11 +85,11 @@ class Module {
\param gpio Arduino pin to be used as additional interrupt/GPIO.
\param spi SPI interface to be used. Defaults to Arduino hardware SPI interface, can also use software SPI implementations.
\param spi SPI interface to be used, can also use software SPI implementations.
\param spiSettings SPI interface settings. Defaults to 2 MHz clock, MSB first, mode 0.
\param spiSettings SPI interface settings.
*/
Module(int16_t cs, int16_t irq, int16_t rst, int16_t gpio, SPIClass& spi = SPI, SPISettings spiSettings = SPISettings(2000000, MSBFIRST, SPI_MODE0));
Module(int16_t cs, int16_t irq, int16_t rst, int16_t gpio, SPIClass& spi, SPISettings spiSettings);
/*!
\brief Generic module constructor.
@ -344,6 +368,7 @@ class Module {
int16_t _irq;
int16_t _rst;
bool _initInterface;
SPIClass* _spi;
SPISettings _spiSettings;