[P118] Enable support for configurable SPI bus

This commit is contained in:
Ton Huisman
2025-08-13 11:55:13 +02:00
parent 55fab58823
commit 9e560d2c52
7 changed files with 64 additions and 46 deletions
+17 -17
View File
@@ -5,7 +5,7 @@
#include "CC1101.h"
// default constructor
CC1101::CC1101(int8_t CSpin, int8_t MISOpin) : _CSpin(CSpin), _MISOpin(MISOpin)
CC1101::CC1101(int8_t CSpin, int8_t MISOpin, SPIClass& spi) : _CSpin(CSpin), _MISOpin(MISOpin), _spi(spi)
{
// SPI.begin(); // Done already by ESPEasy
pinMode(_CSpin, OUTPUT);
@@ -51,7 +51,7 @@ void CC1101::reset()
select();
spi_waitMiso();
SPI.transfer(CC1101_SRES);
_spi.transfer(CC1101_SRES);
delay(10);
spi_waitMiso();
deselect();
@@ -63,7 +63,7 @@ uint8_t CC1101::writeCommand(uint8_t command)
select();
spi_waitMiso();
result = SPI.transfer(command);
result = _spi.transfer(command);
deselect();
return result;
@@ -73,8 +73,8 @@ void CC1101::writeRegister(uint8_t address, uint8_t data)
{
select();
spi_waitMiso();
SPI.transfer(address);
SPI.transfer(data);
_spi.transfer(address);
_spi.transfer(data);
deselect();
}
@@ -84,8 +84,8 @@ uint8_t CC1101::readRegister(uint8_t address)
select();
spi_waitMiso();
SPI.transfer(address);
val = SPI.transfer(0);
_spi.transfer(address);
val = _spi.transfer(0);
deselect();
return val;
@@ -97,12 +97,12 @@ uint8_t CC1101::readRegisterMedian3(uint8_t address)
select();
spi_waitMiso();
SPI.transfer(address);
val1 = SPI.transfer(0);
SPI.transfer(address);
val2 = SPI.transfer(0);
SPI.transfer(address);
val3 = SPI.transfer(0);
_spi.transfer(address);
val1 = _spi.transfer(0);
_spi.transfer(address);
val2 = _spi.transfer(0);
_spi.transfer(address);
val3 = _spi.transfer(0);
deselect();
// reverse sort (largest in val1) because this is te expected order for TX_BUFFER
@@ -162,10 +162,10 @@ void CC1101::writeBurstRegister(uint8_t address, uint8_t *data, uint8_t length)
select();
spi_waitMiso();
SPI.transfer(address | CC1101_WRITE_BURST);
_spi.transfer(address | CC1101_WRITE_BURST);
for (i = 0; i < length; i++) {
SPI.transfer(pgm_read_byte(&(data[i])));
_spi.transfer(pgm_read_byte(&(data[i])));
}
deselect();
}
@@ -176,10 +176,10 @@ void CC1101::readBurstRegister(uint8_t *buffer, uint8_t address, uint8_t length)
select();
spi_waitMiso();
SPI.transfer(address | CC1101_READ_BURST);
_spi.transfer(address | CC1101_READ_BURST);
for (i = 0; i < length; i++) {
buffer[i] = SPI.transfer(0x00);
buffer[i] = _spi.transfer(0x00);
}
deselect();
+4 -2
View File
@@ -191,8 +191,9 @@ protected:
public:
CC1101(int8_t CSpin = PIN_SPI_SS,
int8_t MISOpin = MISO);
CC1101(int8_t CSpin = PIN_SPI_SS,
int8_t MISOpin = MISO,
SPIClass& spi = SPI);
virtual ~CC1101();
// spi
@@ -230,6 +231,7 @@ private:
int8_t _CSpin = PIN_SPI_SS;
int8_t _MISOpin;
SPIClass& _spi = SPI;
protected:
+1 -1
View File
@@ -42,7 +42,7 @@
#define MDMCFG2 0x02 // 16bit sync word / 16bit specific
// default constructor
IthoCC1101::IthoCC1101(int8_t CSpin, int8_t MISOpin, uint8_t counter, uint8_t sendTries) : CC1101(CSpin, MISOpin)
IthoCC1101::IthoCC1101(int8_t CSpin, int8_t MISOpin, SPIClass& spi, uint8_t counter, uint8_t sendTries) : CC1101(CSpin, MISOpin, spi)
{
this->outIthoPacket.counter = counter;
this->sendTries = sendTries;
+13 -10
View File
@@ -78,10 +78,11 @@ private:
public:
IthoCC1101(int8_t CSpin = PIN_SPI_SS,
int8_t MISOpin = MISO,
uint8_t counter = 0,
uint8_t sendTries = 3); // set initial counter value
IthoCC1101(int8_t CSpin = PIN_SPI_SS,
int8_t MISOpin = MISO,
SPIClass& spi = SPI,
uint8_t counter = 0,
uint8_t sendTries = 3); // set initial counter value
~IthoCC1101();
// init
@@ -90,7 +91,8 @@ public:
initReceive();
} // init,reset CC1101
void initReceive();
void initReceive();
// uint8_t getLastCounter() const {
// return outIthoPacket.counter;
// } // counter is increased before sending a command
@@ -104,7 +106,7 @@ public:
}
// receive
bool checkForNewPacket(); // check RX fifo for new data
bool checkForNewPacket(); // check RX fifo for new data
// IthoPacket getLastPacket() const {
// return inIthoPacket;
// } // retrieve last received/parsed packet from remote
@@ -120,14 +122,15 @@ public:
// uint8_t ReadRSSI();
// bool checkID(const uint8_t *id) const;
// int* getLastID();
String getLastIDstr(bool ashex = true);
String getLastIDstr(bool ashex = true);
// String getLastMessagestr(bool ashex = true);
// String LastMessageDecoded() const;
// send
void sendCommand(IthoCommand command,
uint8_t srcId[3] = 0,
uint8_t destId[3] = 0);
void sendCommand(IthoCommand command,
uint8_t srcId[3] = 0,
uint8_t destId[3] = 0);
void enableOrcon(bool state);
+5 -1
View File
@@ -17,6 +17,7 @@
// https://github.com/arjenhiemstra/IthoEcoFanRFT
/** Changelog:
* 2025-08-13 tonhuisman: Enable use of secondary SPI bus
* 2025-01-12 tonhuisman: Add support for MQTT AutoDiscovery (not supported for ITHO)
* Changelog is reverted and reformatted!
* 05-03-2023 tonhuisman: Deprecate 'state' command, and add support for 'itho' as the main command
@@ -135,6 +136,7 @@ boolean Plugin_118(uint8_t function, struct EventStruct *event, String& string)
dev.VType = Sensor_VType::SENSOR_TYPE_TRIPLE;
dev.ValueCount = 3;
dev.SendDataOption = true;
dev.SpiBusSelect = true;
break;
}
@@ -198,10 +200,12 @@ boolean Plugin_118(uint8_t function, struct EventStruct *event, String& string)
# endif // ifdef P118_DEBUG_LOG
if (validGpio(P118_CSPIN) && (P118_IRQPIN != P118_CSPIN)) {
const uint8_t spi_bus = Settings.getSPIBusForTask(event->TaskIndex);
initPluginTaskData(event->TaskIndex, new (std::nothrow) P118_data_struct(P118_CSPIN,
P118_IRQPIN,
P118_CONFIG_LOG == 1,
P118_CONFIG_RF_LOG == 1));
P118_CONFIG_RF_LOG == 1,
spi_bus));
P118_data_struct *P118_data = static_cast<P118_data_struct *>(getPluginTaskData(event->TaskIndex));
success = (nullptr != P118_data) && P118_data->plugin_init(event);
+12 -7
View File
@@ -5,11 +5,12 @@
// **************************************************************************/
// Constructor
// **************************************************************************/
P118_data_struct::P118_data_struct(int8_t csPin,
int8_t irqPin,
bool logData,
bool rfLog)
: _csPin(csPin), _irqPin(irqPin), _log(logData), _rfLog(rfLog) {}
P118_data_struct::P118_data_struct(int8_t csPin,
int8_t irqPin,
bool logData,
bool rfLog,
uint8_t spi_bus)
: _csPin(csPin), _irqPin(irqPin), _log(logData), _rfLog(rfLog), _spi_bus(spi_bus) {}
// **************************************************************************/
// Destructor
@@ -30,9 +31,13 @@ bool P118_data_struct::plugin_init(struct EventStruct *event) {
int8_t spi_pins[3];
uint32_t startInit = 0;
if (Settings.getSPI_pins(spi_pins) && validGpio(spi_pins[1])) {
if (Settings.getSPI_pins(spi_pins, _spi_bus) && validGpio(spi_pins[1])) {
startInit = millis();
_rf = new (std::nothrow) IthoCC1101(_csPin, spi_pins[1]); // Pass CS and MISO
_rf = new (std::nothrow) IthoCC1101(_csPin, spi_pins[1]
# ifdef ESP32
, 0 == _spi_bus ? SPI : SPI1 // defaults and SPI bus for ESP32 only
# endif // ifdef ESP32
); // Pass CS and MISO
} else {
addLog(LOG_LEVEL_ERROR, F("ITHO: SPI configuration not correct!"));
}
+12 -8
View File
@@ -9,6 +9,8 @@
# include "IthoCC1101.h"
# include "IthoPacket.h"
# include "../Globals/SPI1.h"
# define P118_DEBUG_LOG // Enable for some (extra) logging
# ifndef P118_FEATURE_ORCON
# define P118_FEATURE_ORCON 1 // Enable use of Orcon commands
@@ -60,10 +62,11 @@ struct PLUGIN_118_ExtraSettingsStruct {
struct P118_data_struct : public PluginTaskData_base {
public:
P118_data_struct(int8_t csPin,
int8_t irqPin,
bool logData,
bool rfLog);
P118_data_struct(int8_t csPin,
int8_t irqPin,
bool logData,
bool rfLog,
uint8_t spi_bus);
P118_data_struct() = delete;
virtual ~P118_data_struct();
@@ -104,10 +107,11 @@ private:
int _OldLastIDindex = 0;
bool _InitRunned = false;
int8_t _csPin = -1;
int8_t _irqPin = -1;
bool _log = false;
bool _rfLog = false;
int8_t _csPin = -1;
int8_t _irqPin = -1;
bool _log = false;
bool _rfLog = false;
uint8_t _spi_bus;
PLUGIN_118_ExtraSettingsStruct _ExtraSettings;