Browse Source

relay: try to preserve state on reboot

ref. #2547, tell gpio side that we don't need any init

may do weird stuff after runtime pin changes, since we won't know our
new pins due to the order of setup() functions
pull/2558/head
Maxim Prokhorov 1 year ago
parent
commit
824e121499
4 changed files with 38 additions and 14 deletions
  1. +10
    -0
      code/espurna/gpio.cpp
  2. +4
    -1
      code/espurna/gpio.h
  3. +23
    -13
      code/espurna/relay.cpp
  4. +1
    -0
      code/espurna/rtcmem.h

+ 10
- 0
code/espurna/gpio.cpp View File

@ -15,6 +15,7 @@ Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
#include "mcp23s08_pin.h"
#include "rtcmem.h"
#include "terminal.h"
#include "ws.h"
@ -836,6 +837,11 @@ void gpioSetup() {
#endif
}
void hardwareGpioIgnore(unsigned char gpio) {
const auto value = Rtcmem->gpio_ignore;
Rtcmem->gpio_ignore = value | (1 << gpio);
}
void gpioLockOrigin(espurna::gpio::Origin origin) {
espurna::gpio::origin::add(origin);
}
@ -880,11 +886,15 @@ void pinMode(uint8_t pin, uint8_t mode) {
// Special override for Core, allows us to skip init for certain pins when needed
void resetPins() {
const auto& hardware = hardwareGpio();
const auto ignore = Rtcmem->gpio_ignore;
for (size_t pin = 0; pin < espurna::gpio::Hardware::Pins; ++pin) {
if (!hardware.valid(pin)) {
continue;
}
if ((ignore & (1 << pin)) > 0) {
continue;
}
#if DEBUG_SERIAL_SUPPORT
// TODO: actually check which pin is set as TX via function select
if (pin == 1) {


+ 4
- 1
code/espurna/gpio.h View File

@ -50,13 +50,16 @@ public:
virtual BasePinPtr pin(unsigned char index) = 0;
};
GpioBase& hardwareGpio();
GpioBase* gpioBase(GpioType);
GpioBase& hardwareGpio();
void hardwareGpioIgnore(unsigned char gpio);
BasePinPtr gpioRegister(GpioBase& base, unsigned char gpio);
BasePinPtr gpioRegister(unsigned char gpio);
void gpioLockOrigin(espurna::gpio::Origin);
void gpioSetup();
inline size_t gpioPins(const GpioBase& base) {


+ 23
- 13
code/espurna/relay.cpp View File

@ -2771,32 +2771,42 @@ constexpr size_t _relayAdhocPins() {
}
struct RelayGpioProviderCfg {
GpioBase* base;
GpioType type;
uint8_t main;
uint8_t reset;
};
RelayGpioProviderCfg _relayGpioProviderCfg(size_t index) {
return {
gpioBase(espurna::relay::settings::pinType(index)),
espurna::relay::settings::pin(index),
espurna::relay::settings::resetPin(index)};
return RelayGpioProviderCfg{
.type = espurna::relay::settings::pinType(index),
.main = espurna::relay::settings::pin(index),
.reset = espurna::relay::settings::resetPin(index),
};
}
std::unique_ptr<GpioProvider> _relayGpioProvider(size_t index, RelayType type) {
auto cfg = _relayGpioProviderCfg(index);
if (!cfg.base) {
const auto cfg = _relayGpioProviderCfg(index);
auto* base = gpioBase(cfg.type);
if (!base) {
return nullptr;
}
auto main = gpioRegister(*base, cfg.main);
if (!main) {
return nullptr;
}
auto main = gpioRegister(*cfg.base, cfg.main);
if (main) {
auto reset = gpioRegister(*cfg.base, cfg.reset);
return std::make_unique<GpioProvider>(
type, std::move(main), std::move(reset));
auto reset = gpioRegister(*base, cfg.reset);
if (GpioType::Hardware == cfg.type) {
hardwareGpioIgnore(cfg.main);
if (GPIO_NONE != cfg.reset) {
hardwareGpioIgnore(cfg.reset);
}
}
return nullptr;
return std::make_unique<GpioProvider>(
type, std::move(main), std::move(reset));
}
RelayProviderBasePtr _relaySetupProvider(size_t index) {


+ 1
- 0
code/espurna/rtcmem.h View File

@ -54,6 +54,7 @@ struct RtcmemData {
uint32_t mqtt;
uint64_t light;
RtcmemEnergy energy[4];
uint32_t gpio_ignore;
};
static_assert(sizeof(RtcmemData) <= (RTCMEM_BLOCKS * 4u), "RTCMEM struct is too big");


Loading…
Cancel
Save