I2C & SPI fixes. Global interface.

This commit is contained in:
Blaz Kristan 2022-08-14 13:05:59 +02:00
parent 74b6a78a9b
commit 1de009a80d
12 changed files with 113 additions and 133 deletions

View File

@ -26,15 +26,10 @@ private:
bool HomeAssistantDiscovery = false; // Publish Home Assistant Device Information bool HomeAssistantDiscovery = false; // Publish Home Assistant Device Information
// set the default pins based on the architecture, these get overridden by Usermod menu settings // set the default pins based on the architecture, these get overridden by Usermod menu settings
#ifdef ARDUINO_ARCH_ESP32 // ESP32 boards #ifdef ESP8266
#define HW_PIN_SCL 22
#define HW_PIN_SDA 21
#else // ESP8266 boards
#define HW_PIN_SCL 5
#define HW_PIN_SDA 4
//uint8_t RST_PIN = 16; // Uncoment for Heltec WiFi-Kit-8 //uint8_t RST_PIN = 16; // Uncoment for Heltec WiFi-Kit-8
#endif #endif
int8_t ioPin[2] = {HW_PIN_SCL, HW_PIN_SDA}; // I2C pins: SCL, SDA...defaults to Arch hardware pins but overridden at setup() int8_t ioPin[2] = {i2c_scl, i2c_sda}; // I2C pins: SCL, SDA...defaults to Arch hardware pins but overridden at setup()
bool initDone = false; bool initDone = false;
// BME280 sensor settings // BME280 sensor settings
@ -177,7 +172,7 @@ private:
public: public:
void setup() void setup()
{ {
bool HW_Pins_Used = (ioPin[0]==HW_PIN_SCL && ioPin[1]==HW_PIN_SDA); // note whether architecture-based hardware SCL/SDA pins used bool HW_Pins_Used = (ioPin[0]==i2c_scl && ioPin[1]==i2c_sda); // note whether architecture-based hardware SCL/SDA pins used
PinOwner po = PinOwner::UM_BME280; // defaults to being pinowner for SCL/SDA pins PinOwner po = PinOwner::UM_BME280; // defaults to being pinowner for SCL/SDA pins
PinManagerPinType pins[2] = { { ioPin[0], true }, { ioPin[1], true } }; // allocate pins PinManagerPinType pins[2] = { { ioPin[0], true }, { ioPin[1], true } }; // allocate pins
if (HW_Pins_Used) po = PinOwner::HW_I2C; // allow multiple allocations of HW I2C bus pins if (HW_Pins_Used) po = PinOwner::HW_I2C; // allow multiple allocations of HW I2C bus pins
@ -444,7 +439,7 @@ public:
for (byte i=0; i<2; i++) if (ioPin[i] != newPin[i]) { pinsChanged = true; break; } // check if any pins changed for (byte i=0; i<2; i++) if (ioPin[i] != newPin[i]) { pinsChanged = true; break; } // check if any pins changed
if (pinsChanged) { //if pins changed, deallocate old pins and allocate new ones if (pinsChanged) { //if pins changed, deallocate old pins and allocate new ones
PinOwner po = PinOwner::UM_BME280; PinOwner po = PinOwner::UM_BME280;
if (ioPin[0]==HW_PIN_SCL && ioPin[1]==HW_PIN_SDA) po = PinOwner::HW_I2C; // allow multiple allocations of HW I2C bus pins if (ioPin[0]==i2c_scl && ioPin[1]==i2c_sda) po = PinOwner::HW_I2C; // allow multiple allocations of HW I2C bus pins
pinManager.deallocateMultiplePins((const uint8_t *)ioPin, 2, po); // deallocate pins pinManager.deallocateMultiplePins((const uint8_t *)ioPin, 2, po); // deallocate pins
for (byte i=0; i<2; i++) ioPin[i] = newPin[i]; for (byte i=0; i<2; i++) ioPin[i] = newPin[i];
setup(); setup();

View File

@ -3,14 +3,6 @@
#include "src/dependencies/time/DS1307RTC.h" #include "src/dependencies/time/DS1307RTC.h"
#include "wled.h" #include "wled.h"
#ifdef ARDUINO_ARCH_ESP32
#define HW_PIN_SCL 22
#define HW_PIN_SDA 21
#else
#define HW_PIN_SCL 5
#define HW_PIN_SDA 4
#endif
//Connect DS1307 to standard I2C pins (ESP32: GPIO 21 (SDA)/GPIO 22 (SCL)) //Connect DS1307 to standard I2C pins (ESP32: GPIO 21 (SDA)/GPIO 22 (SCL))
class RTCUsermod : public Usermod { class RTCUsermod : public Usermod {
@ -20,7 +12,7 @@ class RTCUsermod : public Usermod {
public: public:
void setup() { void setup() {
PinManagerPinType pins[2] = { { HW_PIN_SCL, true }, { HW_PIN_SDA, true } }; PinManagerPinType pins[2] = { { i2c_scl, true }, { i2c_sda, true } };
if (!pinManager.allocateMultiplePins(pins, 2, PinOwner::HW_I2C)) { disabled = true; return; } if (!pinManager.allocateMultiplePins(pins, 2, PinOwner::HW_I2C)) { disabled = true; return; }
RTC.begin(); RTC.begin();
time_t rtcTime = RTC.get(); time_t rtcTime = RTC.get();
@ -45,13 +37,13 @@ class RTCUsermod : public Usermod {
* It will be called by WLED when settings are actually saved (for example, LED settings are saved) * It will be called by WLED when settings are actually saved (for example, LED settings are saved)
* I highly recommend checking out the basics of ArduinoJson serialization and deserialization in order to use custom settings! * I highly recommend checking out the basics of ArduinoJson serialization and deserialization in order to use custom settings!
*/ */
void addToConfig(JsonObject& root) // void addToConfig(JsonObject& root)
{ // {
JsonObject top = root.createNestedObject("RTC"); // JsonObject top = root.createNestedObject("RTC");
JsonArray pins = top.createNestedArray("pin"); // JsonArray pins = top.createNestedArray("pin");
pins.add(HW_PIN_SCL); // pins.add(i2c_scl);
pins.add(HW_PIN_SDA); // pins.add(i2c_sda);
} // }
uint16_t getId() uint16_t getId()
{ {

View File

@ -21,14 +21,6 @@
#include <Wire.h> #include <Wire.h>
#include <VL53L0X.h> #include <VL53L0X.h>
#ifdef ARDUINO_ARCH_ESP32
#define HW_PIN_SCL 22
#define HW_PIN_SDA 21
#else
#define HW_PIN_SCL 5
#define HW_PIN_SDA 4
#endif
#ifndef VL53L0X_MAX_RANGE_MM #ifndef VL53L0X_MAX_RANGE_MM
#define VL53L0X_MAX_RANGE_MM 230 // max height in millimiters to react for motions #define VL53L0X_MAX_RANGE_MM 230 // max height in millimiters to react for motions
#endif #endif
@ -59,7 +51,7 @@ class UsermodVL53L0XGestures : public Usermod {
public: public:
void setup() { void setup() {
PinManagerPinType pins[2] = { { HW_PIN_SCL, true }, { HW_PIN_SDA, true } }; PinManagerPinType pins[2] = { { i2c_scl, true }, { i2c_sda, true } };
if (!pinManager.allocateMultiplePins(pins, 2, PinOwner::HW_I2C)) { enabled = false; return; } if (!pinManager.allocateMultiplePins(pins, 2, PinOwner::HW_I2C)) { enabled = false; return; }
Wire.begin(); Wire.begin();
@ -127,13 +119,13 @@ class UsermodVL53L0XGestures : public Usermod {
* It will be called by WLED when settings are actually saved (for example, LED settings are saved) * It will be called by WLED when settings are actually saved (for example, LED settings are saved)
* I highly recommend checking out the basics of ArduinoJson serialization and deserialization in order to use custom settings! * I highly recommend checking out the basics of ArduinoJson serialization and deserialization in order to use custom settings!
*/ */
void addToConfig(JsonObject& root) // void addToConfig(JsonObject& root)
{ // {
JsonObject top = root.createNestedObject("VL53L0x"); // JsonObject top = root.createNestedObject("VL53L0x");
JsonArray pins = top.createNestedArray("pin"); // JsonArray pins = top.createNestedArray("pin");
pins.add(HW_PIN_SCL); // pins.add(i2c_scl);
pins.add(HW_PIN_SDA); // pins.add(i2c_sda);
} // }
/* /*
* getId() allows you to optionally give your V2 usermod an unique ID (please define it in const.h!). * getId() allows you to optionally give your V2 usermod an unique ID (please define it in const.h!).

View File

@ -42,14 +42,6 @@
#include "Wire.h" #include "Wire.h"
#endif #endif
#ifdef ARDUINO_ARCH_ESP32
#define HW_PIN_SCL 22
#define HW_PIN_SDA 21
#else
#define HW_PIN_SCL 5
#define HW_PIN_SDA 4
#endif
// ================================================================ // ================================================================
// === INTERRUPT DETECTION ROUTINE === // === INTERRUPT DETECTION ROUTINE ===
// ================================================================ // ================================================================
@ -93,7 +85,7 @@ class MPU6050Driver : public Usermod {
* setup() is called once at boot. WiFi is not yet connected at this point. * setup() is called once at boot. WiFi is not yet connected at this point.
*/ */
void setup() { void setup() {
PinManagerPinType pins[2] = { { HW_PIN_SCL, true }, { HW_PIN_SDA, true } }; PinManagerPinType pins[2] = { { i2c_scl, true }, { i2c_sda, true } };
if (!pinManager.allocateMultiplePins(pins, 2, PinOwner::HW_I2C)) { enabled = false; return; } if (!pinManager.allocateMultiplePins(pins, 2, PinOwner::HW_I2C)) { enabled = false; return; }
// join I2C bus (I2Cdev library doesn't do this automatically) // join I2C bus (I2Cdev library doesn't do this automatically)
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
@ -258,20 +250,20 @@ class MPU6050Driver : public Usermod {
* addToJsonState() can be used to add custom entries to the /json/state part of the JSON API (state object). * addToJsonState() can be used to add custom entries to the /json/state part of the JSON API (state object).
* Values in the state object may be modified by connected clients * Values in the state object may be modified by connected clients
*/ */
void addToJsonState(JsonObject& root) //void addToJsonState(JsonObject& root)
{ //{
//root["user0"] = userVar0; //root["user0"] = userVar0;
} //}
/* /*
* readFromJsonState() can be used to receive data clients send to the /json/state part of the JSON API (state object). * readFromJsonState() can be used to receive data clients send to the /json/state part of the JSON API (state object).
* Values in the state object may be modified by connected clients * Values in the state object may be modified by connected clients
*/ */
void readFromJsonState(JsonObject& root) //void readFromJsonState(JsonObject& root)
{ //{
//if (root["bri"] == 255) DEBUG_PRINTLN(F("Don't burn down your garage!")); //if (root["bri"] == 255) DEBUG_PRINTLN(F("Don't burn down your garage!"));
} //}
/* /*
@ -279,13 +271,13 @@ class MPU6050Driver : public Usermod {
* It will be called by WLED when settings are actually saved (for example, LED settings are saved) * It will be called by WLED when settings are actually saved (for example, LED settings are saved)
* I highly recommend checking out the basics of ArduinoJson serialization and deserialization in order to use custom settings! * I highly recommend checking out the basics of ArduinoJson serialization and deserialization in order to use custom settings!
*/ */
void addToConfig(JsonObject& root) // void addToConfig(JsonObject& root)
{ // {
JsonObject top = root.createNestedObject("MPU6050_IMU"); // JsonObject top = root.createNestedObject("MPU6050_IMU");
JsonArray pins = top.createNestedArray("pin"); // JsonArray pins = top.createNestedArray("pin");
pins.add(HW_PIN_SCL); // pins.add(HW_PIN_SCL);
pins.add(HW_PIN_SDA); // pins.add(HW_PIN_SDA);
} // }
/* /*
* getId() allows you to optionally give your V2 usermod an unique ID (please define it in const.h!). * getId() allows you to optionally give your V2 usermod an unique ID (please define it in const.h!).

View File

@ -24,54 +24,31 @@
// //
//The SCL and SDA pins are defined here. //The SCL and SDA pins are defined here.
#ifdef ARDUINO_ARCH_ESP32
#define HW_PIN_SCL 22
#define HW_PIN_SDA 21
#define HW_PIN_CLOCKSPI 18
#define HW_PIN_DATASPI 23
#ifndef FLD_PIN_SCL #ifndef FLD_PIN_SCL
#define FLD_PIN_SCL 22 #define FLD_PIN_SCL i2c_scl
#endif #endif
#ifndef FLD_PIN_SDA #ifndef FLD_PIN_SDA
#define FLD_PIN_SDA 21 #define FLD_PIN_SDA i2c_sda
#endif #endif
#ifndef FLD_PIN_CLOCKSPI #ifndef FLD_PIN_CLOCKSPI
#define FLD_PIN_CLOCKSPI 18 #define FLD_PIN_CLOCKSPI spi_sclk
#endif #endif
#ifndef FLD_PIN_DATASPI #ifndef FLD_PIN_DATASPI
#define FLD_PIN_DATASPI 23 #define FLD_PIN_DATASPI spi_mosi
#endif
#ifndef FLD_PIN_DC
#define FLD_PIN_DC 19
#endif #endif
#ifndef FLD_PIN_CS #ifndef FLD_PIN_CS
#define FLD_PIN_CS 5 #define FLD_PIN_CS spi_cs
#endif
#ifdef ARDUINO_ARCH_ESP32
#ifndef FLD_PIN_DC
#define FLD_PIN_DC 19
#endif #endif
#ifndef FLD_PIN_RESET #ifndef FLD_PIN_RESET
#define FLD_PIN_RESET 26 #define FLD_PIN_RESET 26
#endif #endif
#else #else
#define HW_PIN_SCL 5
#define HW_PIN_SDA 4
#define HW_PIN_CLOCKSPI 14
#define HW_PIN_DATASPI 13
#ifndef FLD_PIN_SCL
#define FLD_PIN_SCL 5
#endif
#ifndef FLD_PIN_SDA
#define FLD_PIN_SDA 4
#endif
#ifndef FLD_PIN_CLOCKSPI
#define FLD_PIN_CLOCKSPI 14
#endif
#ifndef FLD_PIN_DATASPI
#define FLD_PIN_DATASPI 13
#endif
#ifndef FLD_PIN_DC #ifndef FLD_PIN_DC
#define FLD_PIN_DC 12 #define FLD_PIN_DC 12
#endif
#ifndef FLD_PIN_CS
#define FLD_PIN_CS 15
#endif #endif
#ifndef FLD_PIN_RESET #ifndef FLD_PIN_RESET
#define FLD_PIN_RESET 16 #define FLD_PIN_RESET 16
@ -192,13 +169,14 @@ class FourLineDisplayUsermod : public Usermod {
bool isHW; bool isHW;
PinOwner po = PinOwner::UM_FourLineDisplay; PinOwner po = PinOwner::UM_FourLineDisplay;
if (type == SSD1306_SPI || type == SSD1306_SPI64) { if (type == SSD1306_SPI || type == SSD1306_SPI64) {
isHW = (ioPin[0]==HW_PIN_CLOCKSPI && ioPin[1]==HW_PIN_DATASPI); isHW = (ioPin[0]==spi_sclk && ioPin[1]==spi_mosi);
if (isHW) po = PinOwner::HW_SPI; // allow multiple allocations of HW I2C bus pins
PinManagerPinType pins[5] = { { ioPin[0], true }, { ioPin[1], true }, { ioPin[2], true }, { ioPin[3], true }, { ioPin[4], true }}; PinManagerPinType pins[5] = { { ioPin[0], true }, { ioPin[1], true }, { ioPin[2], true }, { ioPin[3], true }, { ioPin[4], true }};
if (!pinManager.allocateMultiplePins(pins, 5, po)) { type=NONE; return; } if (!pinManager.allocateMultiplePins(pins, 5, po)) { type=NONE; return; }
} else { } else {
isHW = (ioPin[0]==HW_PIN_SCL && ioPin[1]==HW_PIN_SDA); isHW = (ioPin[0]==i2c_scl && ioPin[1]==i2c_sda);
if (isHW) po = PinOwner::HW_I2C; // allow multiple allocations of HW I2C bus pins
PinManagerPinType pins[2] = { { ioPin[0], true }, { ioPin[1], true } }; PinManagerPinType pins[2] = { { ioPin[0], true }, { ioPin[1], true } };
if (ioPin[0]==HW_PIN_SCL && ioPin[1]==HW_PIN_SDA) po = PinOwner::HW_I2C; // allow multiple allocations of HW I2C bus pins
if (!pinManager.allocateMultiplePins(pins, 2, po)) { type=NONE; return; } if (!pinManager.allocateMultiplePins(pins, 2, po)) { type=NONE; return; }
} }
@ -718,8 +696,14 @@ class FourLineDisplayUsermod : public Usermod {
if (pinsChanged || type!=newType) { if (pinsChanged || type!=newType) {
if (type != NONE) delete u8x8; if (type != NONE) delete u8x8;
PinOwner po = PinOwner::UM_FourLineDisplay; PinOwner po = PinOwner::UM_FourLineDisplay;
if (ioPin[0]==HW_PIN_SCL && ioPin[1]==HW_PIN_SDA) po = PinOwner::HW_I2C; // allow multiple allocations of HW I2C bus pins bool isSPI = (type == SSD1306_SPI || type == SSD1306_SPI64);
pinManager.deallocateMultiplePins((const uint8_t *)ioPin, (type == SSD1306_SPI || type == SSD1306_SPI64) ? 5 : 2, po); if (isSPI) {
if (ioPin[0]==spi_sclk && ioPin[1]==spi_mosi) po = PinOwner::HW_SPI; // allow multiple allocations of HW SPI bus pins
pinManager.deallocateMultiplePins((const uint8_t *)ioPin, 5, po);
} else {
if (ioPin[0]==i2c_scl && ioPin[1]==i2c_sda) po = PinOwner::HW_I2C; // allow multiple allocations of HW I2C bus pins
pinManager.deallocateMultiplePins((const uint8_t *)ioPin, 2, po);
}
for (byte i=0; i<5; i++) ioPin[i] = newPin[i]; for (byte i=0; i<5; i++) ioPin[i] = newPin[i];
if (ioPin[0]<0 || ioPin[1]<0) { // data & clock must be > -1 if (ioPin[0]<0 || ioPin[1]<0) { // data & clock must be > -1
type = NONE; type = NONE;

View File

@ -26,19 +26,19 @@
//The SCL and SDA pins are defined here. //The SCL and SDA pins are defined here.
#ifndef FLD_PIN_SCL #ifndef FLD_PIN_SCL
#define FLD_PIN_SCL HW_PIN_SCL #define FLD_PIN_SCL i2c_scl
#endif #endif
#ifndef FLD_PIN_SDA #ifndef FLD_PIN_SDA
#define FLD_PIN_SDA HW_PIN_SDA #define FLD_PIN_SDA i2c_sda
#endif #endif
#ifndef FLD_PIN_CLOCKSPI #ifndef FLD_PIN_CLOCKSPI
#define FLD_PIN_CLOCKSPI HW_PIN_CLOCKSPI #define FLD_PIN_CLOCKSPI spi_sclk
#endif #endif
#ifndef FLD_PIN_DATASPI #ifndef FLD_PIN_DATASPI
#define FLD_PIN_DATASPI HW_PIN_DATASPI #define FLD_PIN_DATASPI spi_mosi
#endif #endif
#ifndef FLD_PIN_CS #ifndef FLD_PIN_CS
#define FLD_PIN_CS HW_PIN_CSSPI #define FLD_PIN_CS spi_cs
#endif #endif
#ifdef ARDUINO_ARCH_ESP32 #ifdef ARDUINO_ARCH_ESP32
@ -195,14 +195,14 @@ class FourLineDisplayUsermod : public Usermod {
bool isHW, isSPI = (type == SSD1306_SPI || type == SSD1306_SPI64); bool isHW, isSPI = (type == SSD1306_SPI || type == SSD1306_SPI64);
PinOwner po = PinOwner::UM_FourLineDisplay; PinOwner po = PinOwner::UM_FourLineDisplay;
if (isSPI) { if (isSPI) {
isHW = (ioPin[0]==HW_PIN_CLOCKSPI && ioPin[1]==HW_PIN_DATASPI); isHW = (ioPin[0]==spi_sclk && ioPin[1]==spi_mosi && ioPin[2]==spi_cs);
if (isHW) po = PinOwner::HW_SPI; // allow multiple allocations of HW I2C bus pins
PinManagerPinType pins[5] = { { ioPin[0], true }, { ioPin[1], true }, { ioPin[2], true }, { ioPin[3], true }, { ioPin[4], true }}; PinManagerPinType pins[5] = { { ioPin[0], true }, { ioPin[1], true }, { ioPin[2], true }, { ioPin[3], true }, { ioPin[4], true }};
if (ioPin[0]==HW_PIN_CLOCKSPI && ioPin[1]==HW_PIN_DATASPI && ioPin[2]==HW_PIN_CSSPI) po = PinOwner::HW_SPI; // allow multiple allocations of HW SPI bus pins
if (!pinManager.allocateMultiplePins(pins, 5, po)) { type=NONE; return; } if (!pinManager.allocateMultiplePins(pins, 5, po)) { type=NONE; return; }
} else { } else {
isHW = (ioPin[0]==HW_PIN_SCL && ioPin[1]==HW_PIN_SDA); isHW = (ioPin[0]==i2c_scl && ioPin[1]==i2c_sda);
if (isHW) po = PinOwner::HW_I2C; // allow multiple allocations of HW I2C bus pins
PinManagerPinType pins[2] = { { ioPin[0], true }, { ioPin[1], true } }; PinManagerPinType pins[2] = { { ioPin[0], true }, { ioPin[1], true } };
if (ioPin[0]==HW_PIN_SCL && ioPin[1]==HW_PIN_SDA) po = PinOwner::HW_I2C; // allow multiple allocations of HW I2C bus pins
if (!pinManager.allocateMultiplePins(pins, 2, po)) { type=NONE; return; } if (!pinManager.allocateMultiplePins(pins, 2, po)) { type=NONE; return; }
} }
@ -1107,8 +1107,8 @@ class FourLineDisplayUsermod : public Usermod {
if (type != NONE) delete u8x8; if (type != NONE) delete u8x8;
PinOwner po = PinOwner::UM_FourLineDisplay; PinOwner po = PinOwner::UM_FourLineDisplay;
bool isSPI = (type == SSD1306_SPI || type == SSD1306_SPI64); bool isSPI = (type == SSD1306_SPI || type == SSD1306_SPI64);
if (!isSPI && ioPin[0]==HW_PIN_SCL && ioPin[1]==HW_PIN_SDA) po = PinOwner::HW_I2C; // allow multiple allocations of HW I2C bus pins if (!isSPI && ioPin[0]==i2c_scl && ioPin[1]==i2c_sda) po = PinOwner::HW_I2C; // allow multiple allocations of HW I2C bus pins
if (isSPI && ioPin[0]==HW_PIN_CLOCKSPI && ioPin[1]==HW_PIN_DATASPI && ioPin[2]==HW_PIN_CSSPI) po = PinOwner::HW_SPI; // allow multiple allocations of HW SPI bus pins if (isSPI && ioPin[0]==spi_sclk && ioPin[1]==spi_mosi && ioPin[2]==spi_cs) po = PinOwner::HW_SPI; // allow multiple allocations of HW SPI bus pins
pinManager.deallocateMultiplePins((const uint8_t *)ioPin, isSPI ? 5 : 2, po); pinManager.deallocateMultiplePins((const uint8_t *)ioPin, isSPI ? 5 : 2, po);
for (byte i=0; i<5; i++) ioPin[i] = newPin[i]; for (byte i=0; i<5; i++) ioPin[i] = newPin[i];
if (ioPin[0]<0 || ioPin[1]<0) { // data & clock must be > -1 if (ioPin[0]<0 || ioPin[1]<0) { // data & clock must be > -1

View File

@ -276,7 +276,6 @@ bool deserializeConfig(JsonObject doc, bool fromFS) {
Wire.setPins(i2c_sda, i2c_scl); // this will fail if Wire is initilised (Wire.begin() called prior) Wire.setPins(i2c_sda, i2c_scl); // this will fail if Wire is initilised (Wire.begin() called prior)
#endif #endif
Wire.begin(); Wire.begin();
uint8_t i2c[2] = {i2c_sda, i2c_scl};
pinManager.deallocateMultiplePins(i2c, 2, PinOwner::HW_I2C); pinManager.deallocateMultiplePins(i2c, 2, PinOwner::HW_I2C);
} else { } else {
i2c_sda = -1; i2c_sda = -1;
@ -293,7 +292,6 @@ bool deserializeConfig(JsonObject doc, bool fromFS) {
#else #else
SPI.begin(spi_sclk, (int8_t)-1, spi_mosi, spi_cs); SPI.begin(spi_sclk, (int8_t)-1, spi_mosi, spi_cs);
#endif #endif
uint8_t spi[3] = { spi_mosi, spi_sclk, spi_cs };
pinManager.deallocateMultiplePins(spi, 3, PinOwner::HW_SPI); pinManager.deallocateMultiplePins(spi, 3, PinOwner::HW_SPI);
} else { } else {
spi_mosi = -1; spi_mosi = -1;

View File

@ -626,6 +626,16 @@ void serializeInfo(JsonObject root)
root[F("noaudio")] = true; root[F("noaudio")] = true;
#endif #endif
#ifdef WLED_DEBUG
JsonArray i2c = root.createNestedArray(F("i2c"));
i2c.add(i2c_sda);
i2c.add(i2c_scl);
JsonArray spi = root.createNestedArray(F("spi"));
spi.add(spi_mosi);
spi.add(spi_sclk);
spi.add(spi_cs);
#endif
root[F("str")] = syncToggleReceive; root[F("str")] = syncToggleReceive;
root[F("name")] = serverDescription; root[F("name")] = serverDescription;

View File

@ -75,12 +75,25 @@ bool PinManagerClass::deallocateMultiplePins(const uint8_t *pinArray, byte array
return true; return true;
} }
} }
if (tag==PinOwner::HW_SPI) {
if (spiAllocCount && --spiAllocCount>0) {
// no deallocation done until last owner releases pins
return true;
}
}
for (int i = 0; i < arrayElementCount; i++) { for (int i = 0; i < arrayElementCount; i++) {
deallocatePin(pinArray[i], tag); deallocatePin(pinArray[i], tag);
} }
return true; return true;
} }
bool PinManagerClass::deallocateMultiplePins(const managed_pin_type * mptArray, byte arrayElementCount, PinOwner tag)
{
uint8_t pins[arrayElementCount];
for (int i=0; i<arrayElementCount; i++) pins[i] = mptArray[i].pin;
return deallocateMultiplePins(pins, arrayElementCount, tag);
}
bool PinManagerClass::allocateMultiplePins(const managed_pin_type * mptArray, byte arrayElementCount, PinOwner tag) bool PinManagerClass::allocateMultiplePins(const managed_pin_type * mptArray, byte arrayElementCount, PinOwner tag)
{ {
bool shouldFail = false; bool shouldFail = false;
@ -100,8 +113,8 @@ bool PinManagerClass::allocateMultiplePins(const managed_pin_type * mptArray, by
#endif #endif
shouldFail = true; shouldFail = true;
} }
if (tag==PinOwner::HW_I2C && isPinAllocated(gpio, tag)) { if ((tag==PinOwner::HW_I2C || tag==PinOwner::HW_SPI) && isPinAllocated(gpio, tag)) {
// allow multiple "allocations" of HW I2C bus pins // allow multiple "allocations" of HW I2C & SPI bus pins
continue; continue;
} else if (isPinAllocated(gpio)) { } else if (isPinAllocated(gpio)) {
#ifdef WLED_DEBUG #ifdef WLED_DEBUG
@ -119,6 +132,7 @@ bool PinManagerClass::allocateMultiplePins(const managed_pin_type * mptArray, by
} }
if (tag==PinOwner::HW_I2C) i2cAllocCount++; if (tag==PinOwner::HW_I2C) i2cAllocCount++;
if (tag==PinOwner::HW_SPI) spiAllocCount++;
// all pins are available .. track each one // all pins are available .. track each one
for (int i = 0; i < arrayElementCount; i++) { for (int i = 0; i < arrayElementCount; i++) {
@ -145,8 +159,8 @@ bool PinManagerClass::allocateMultiplePins(const managed_pin_type * mptArray, by
bool PinManagerClass::allocatePin(byte gpio, bool output, PinOwner tag) bool PinManagerClass::allocatePin(byte gpio, bool output, PinOwner tag)
{ {
// HW I2C pins have to be allocated using allocateMultiplePins variant since there is always SCL/SDA pair // HW I2C & SPI pins have to be allocated using allocateMultiplePins variant since there is always SCL/SDA pair
if (!isPinOk(gpio, output) || tag==PinOwner::HW_I2C) return false; if (!isPinOk(gpio, output) || tag==PinOwner::HW_I2C || tag==PinOwner::HW_SPI) return false;
if (isPinAllocated(gpio)) { if (isPinAllocated(gpio)) {
#ifdef WLED_DEBUG #ifdef WLED_DEBUG
DEBUG_PRINT(F("PIN ALLOC: Pin ")); DEBUG_PRINT(F("PIN ALLOC: Pin "));

View File

@ -34,7 +34,7 @@ enum struct PinOwner : uint8_t {
DebugOut = 0x89, // 'Dbg' == debug output always IO1 DebugOut = 0x89, // 'Dbg' == debug output always IO1
DMX = 0x8A, // 'DMX' == hard-coded to IO2 DMX = 0x8A, // 'DMX' == hard-coded to IO2
HW_I2C = 0x8B, // 'I2C' == hardware I2C pins (4&5 on ESP8266, 21&22 on ESP32) HW_I2C = 0x8B, // 'I2C' == hardware I2C pins (4&5 on ESP8266, 21&22 on ESP32)
HW_SPI = 0x8C, // 'SPI' == hardware SPI pins (13,14&15 on ESP8266, 5,18&23 on ESP32) HW_SPI = 0x8C, // 'SPI' == hardware (V)SPI pins (13,14&15 on ESP8266, 5,18&23 on ESP32)
// Use UserMod IDs from const.h here // Use UserMod IDs from const.h here
UM_Unspecified = USERMOD_ID_UNSPECIFIED, // 0x01 UM_Unspecified = USERMOD_ID_UNSPECIFIED, // 0x01
UM_Example = USERMOD_ID_EXAMPLE, // 0x02 // Usermod "usermod_v2_example.h" UM_Example = USERMOD_ID_EXAMPLE, // 0x02 // Usermod "usermod_v2_example.h"
@ -70,13 +70,18 @@ class PinManagerClass {
uint8_t ledcAlloc[2] = {0x00, 0x00}; //16 LEDC channels uint8_t ledcAlloc[2] = {0x00, 0x00}; //16 LEDC channels
PinOwner ownerTag[40] = { PinOwner::None }; PinOwner ownerTag[40] = { PinOwner::None };
#endif #endif
uint8_t i2cAllocCount = 0; // allow multiple allocation of I2C bus pins but keep track of allocations struct {
uint8_t i2cAllocCount : 4; // allow multiple allocation of I2C bus pins but keep track of allocations
uint8_t spiAllocCount : 4; // allow multiple allocation of SPI bus pins but keep track of allocations
};
public: public:
PinManagerClass() : i2cAllocCount(0), spiAllocCount(0) {}
// De-allocates a single pin // De-allocates a single pin
bool deallocatePin(byte gpio, PinOwner tag); bool deallocatePin(byte gpio, PinOwner tag);
// De-allocates multiple pins but only if all can be deallocated (PinOwner has to be specified) // De-allocates multiple pins but only if all can be deallocated (PinOwner has to be specified)
bool deallocateMultiplePins(const uint8_t *pinArray, byte arrayElementCount, PinOwner tag); bool deallocateMultiplePins(const uint8_t *pinArray, byte arrayElementCount, PinOwner tag);
bool deallocateMultiplePins(const managed_pin_type *pinArray, byte arrayElementCount, PinOwner tag);
// Allocates a single pin, with an owner tag. // Allocates a single pin, with an owner tag.
// De-allocation requires the same owner tag (or override) // De-allocation requires the same owner tag (or override)
bool allocatePin(byte gpio, bool output, PinOwner tag); bool allocatePin(byte gpio, bool output, PinOwner tag);

View File

@ -486,8 +486,8 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
pinManager.deallocateMultiplePins(oldpins, 2, PinOwner::HW_I2C); pinManager.deallocateMultiplePins(oldpins, 2, PinOwner::HW_I2C);
#ifdef ESP8266 #ifdef ESP8266
// cannot change pins on ESP8266 // cannot change pins on ESP8266
if (hw_sda_pin != HW_PIN_SDA) hw_sda_pin = -1; if (hw_sda_pin != HW_PIN_SDA) hw_sda_pin = HW_PIN_SDA;
if (hw_scl_pin != HW_PIN_SCL) hw_scl_pin = -1; if (hw_scl_pin != HW_PIN_SCL) hw_scl_pin = HW_PIN_SCL;
#endif #endif
PinManagerPinType i2c[2] = { { hw_sda_pin, true }, { hw_scl_pin, true } }; PinManagerPinType i2c[2] = { { hw_sda_pin, true }, { hw_scl_pin, true } };
if (pinManager.allocateMultiplePins(i2c, 2, PinOwner::HW_I2C)) { if (pinManager.allocateMultiplePins(i2c, 2, PinOwner::HW_I2C)) {
@ -495,7 +495,6 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
i2c_scl = hw_scl_pin; i2c_scl = hw_scl_pin;
#ifdef ESP32 #ifdef ESP32
Wire.setPins(i2c_sda, i2c_scl); // this will fail if Wire is initilised (Wire.begin() called) Wire.setPins(i2c_sda, i2c_scl); // this will fail if Wire is initilised (Wire.begin() called)
uint8_t i2c[2] = {i2c_sda, i2c_scl};
pinManager.deallocateMultiplePins(i2c, 2, PinOwner::HW_I2C); pinManager.deallocateMultiplePins(i2c, 2, PinOwner::HW_I2C);
#endif #endif
} else { } else {
@ -511,16 +510,15 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
pinManager.deallocateMultiplePins(oldpins, 3, PinOwner::HW_SPI); pinManager.deallocateMultiplePins(oldpins, 3, PinOwner::HW_SPI);
#ifdef ESP8266 #ifdef ESP8266
// cannot change pins on ESP8266 // cannot change pins on ESP8266
if (hw_mosi_pin != HW_PIN_DATASPI) hw_mosi_pin = -1; if (hw_mosi_pin != HW_PIN_DATASPI) hw_mosi_pin = HW_PIN_DATASPI;
if (hw_sclk_pin != HW_PIN_CLOCKSPI) hw_sclk_pin = -1; if (hw_sclk_pin != HW_PIN_CLOCKSPI) hw_sclk_pin = HW_PIN_CLOCKSPI;
if (hw_cs_pin != HW_PIN_CSSPI) hw_cs_pin = -1; if (hw_cs_pin != HW_PIN_CSSPI) hw_cs_pin = HW_PIN_CSSPI;
#endif #endif
PinManagerPinType spi[3] = { { hw_mosi_pin, true }, { hw_sclk_pin, true }, { hw_cs_pin, true } }; PinManagerPinType spi[3] = { { hw_mosi_pin, true }, { hw_sclk_pin, true }, { hw_cs_pin, true } };
if (pinManager.allocateMultiplePins(spi, 3, PinOwner::HW_SPI)) { if (pinManager.allocateMultiplePins(spi, 3, PinOwner::HW_SPI)) {
spi_mosi = hw_mosi_pin; spi_mosi = hw_mosi_pin;
spi_sclk = hw_sclk_pin; spi_sclk = hw_sclk_pin;
spi_cs = hw_cs_pin; spi_cs = hw_cs_pin;
uint8_t spi[3] = { hw_mosi_pin, hw_sclk_pin, hw_cs_pin };
pinManager.deallocateMultiplePins(spi, 3, PinOwner::HW_SPI); pinManager.deallocateMultiplePins(spi, 3, PinOwner::HW_SPI);
} else { } else {
spi_mosi = -1; spi_mosi = -1;

View File

@ -8,7 +8,7 @@
*/ */
// version code in format yymmddb (b = daily build) // version code in format yymmddb (b = daily build)
#define VERSION 2208121 #define VERSION 2208141
//uncomment this if you have a "my_config.h" file you'd like to use //uncomment this if you have a "my_config.h" file you'd like to use
//#define WLED_USE_MY_CONFIG //#define WLED_USE_MY_CONFIG