Bug 2064, 2063 - PinManager usage (#2066)

* Fix 2063 - Do not free pins unless allocated

* Fix 2064: Allocate pins used for Ethernet

* Fix obvious compilation errors.

* Fix multiple bugs...

* pinsAllocated[2] set twice due to copy/paste bug.
* wrong pin allocated for ETH_CLOCK_GPIO17_OUT due to copy/paste bug

* Stylistic change per PR review

* Stylistic change per PR review

* attempt to allocate pin for "default" button

* remove extra local variable

* check return value from ETH.begin()

Co-authored-by: Christian Schwinne <dev.aircoookie@gmail.com>
This commit is contained in:
Henry Gabryjelski 2021-07-09 11:06:48 -07:00 committed by GitHub
parent a17f83cedd
commit 3ad336a1eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 17 deletions

View File

@ -119,13 +119,13 @@ class BusDigital : public Bus {
public:
BusDigital(BusConfig &bc, uint8_t nr) : Bus(bc.type, bc.start) {
if (!IS_DIGITAL(bc.type) || !bc.count) return;
if (!pinManager.allocatePin(bc.pins[0])) return;
_pins[0] = bc.pins[0];
if (!pinManager.allocatePin(_pins[0])) return;
if (IS_2PIN(bc.type)) {
_pins[1] = bc.pins[1];
if (!pinManager.allocatePin(_pins[1])) {
if (!pinManager.allocatePin(bc.pins[1])) {
cleanup(); return;
}
_pins[1] = bc.pins[1];
}
reversed = bc.reversed;
_skip = bc.skipAmount; //sacrificial pixels
@ -241,10 +241,11 @@ class BusPwm : public Bus {
#endif
for (uint8_t i = 0; i < numPins; i++) {
_pins[i] = bc.pins[i];
if (!pinManager.allocatePin(_pins[i])) {
uint8_t currentPin = bc.pins[i];
if (!pinManager.allocatePin(currentPin)) {
deallocatePins(); return;
}
_pins[i] = currentPin; // store only after allocatePin() succeeds
#ifdef ESP8266
pinMode(_pins[i], OUTPUT);
#else

View File

@ -152,14 +152,20 @@ bool deserializeConfig(JsonObject doc, bool fromFS) {
}
} else {
// new install/missing configuration (button 0 has defaults)
if (fromFS)
for (uint8_t s=1; s<WLED_MAX_BUTTONS; s++) {
if (fromFS) {
// relies upon only being called once with fromFS == true, which is currently true.
uint8_t s = 0;
if (pinManager.allocatePin(btnPin[0],false)) { // initialized to #define value BTNPIN, or zero if not defined(!)
++s; // do not clear default button if allocated successfully
}
for (; s<WLED_MAX_BUTTONS; s++) {
btnPin[s] = -1;
buttonType[s] = BTN_TYPE_NONE;
macroButton[s] = 0;
macroLongPress[s] = 0;
macroDoublePress[s] = 0;
}
}
}
CJSON(touchThreshold,btn_obj[F("tt")]);
CJSON(buttonPublishMqtt,btn_obj["mqtt"]);

View File

@ -446,14 +446,60 @@ void WLED::initConnection()
// Only initialize ethernet board if not NONE
if (ethernetType != WLED_ETH_NONE && ethernetType < WLED_NUM_ETH_TYPES) {
ethernet_settings es = ethernetBoards[ethernetType];
ETH.begin(
(uint8_t) es.eth_address,
(int) es.eth_power,
(int) es.eth_mdc,
(int) es.eth_mdio,
(eth_phy_type_t) es.eth_type,
(eth_clock_mode_t) es.eth_clk_mode
);
// Use PinManager to ensure pins are available for
// ethernet AND to prevent other uses of these pins.
bool s = true;
byte pinsAllocated[4] { 255, 255, 255, 255 };
if (s && (s = pinManager.allocatePin((byte)es.eth_power))) {
pinsAllocated[0] = (byte)es.eth_power;
}
if (s && (s = pinManager.allocatePin((byte)es.eth_mdc))) {
pinsAllocated[1] = (byte)es.eth_mdc;
}
if (s && (s = pinManager.allocatePin((byte)es.eth_mdio))) {
pinsAllocated[2] = (byte)es.eth_mdio;
}
switch(es.eth_clk_mode) {
case ETH_CLOCK_GPIO0_IN:
s = pinManager.allocatePin(0, false);
pinsAllocated[3] = 0;
break;
case ETH_CLOCK_GPIO0_OUT:
s = pinManager.allocatePin(0);
pinsAllocated[3] = 0;
break;
case ETH_CLOCK_GPIO16_OUT:
s = pinManager.allocatePin(16);
pinsAllocated[3] = 16;
break;
case ETH_CLOCK_GPIO17_OUT:
s = pinManager.allocatePin(17);
pinsAllocated[3] = 17;
break;
default:
s = false;
break;
}
if (s) {
s = ETH.begin(
(uint8_t) es.eth_address,
(int) es.eth_power,
(int) es.eth_mdc,
(int) es.eth_mdio,
(eth_phy_type_t) es.eth_type,
(eth_clock_mode_t) es.eth_clk_mode
);
}
if (!s) {
DEBUG_PRINTLN(F("Ethernet init failed"));
// de-allocate only those pins allocated before the failure
for (byte p : pinsAllocated) {
pinManager.deallocatePin(p);
}
}
}
#endif

View File

@ -20,8 +20,16 @@ ethernet_settings ethernetBoards[] = {
// WT32-EHT01
// Please note, from my testing only these pins work for LED outputs:
// IO2, IO4, IO12, IO14, IO15
// These pins do not appear to work from my testing:
// IO35, IO36, IO39
// Pins IO34 through IO39 are input-only on ESP32, so
// the following exposed pins won't work to drive WLEDs
// IO35(*), IO36, IO39
// The following pins are also exposed via the headers,
// and likely can be used as general purpose IO:
// IO05(*), IO32 (CFG), IO33 (485_EN), IO33 (TXD)
//
// (*) silkscreen on board revision v1.2 may be wrong:
// IO5 silkscreen on v1.2 says IO35
// IO35 silkscreen on v1.2 says RXD
{
1, // eth_address,
16, // eth_power,