Skip to content

Commit

Permalink
relay: try to preserve state on reboot
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mcspr committed Nov 25, 2022
1 parent d78d783 commit 824e121
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
10 changes: 10 additions & 0 deletions code/espurna/gpio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion code/espurna/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,16 @@ class GpioBase {
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) {
Expand Down
36 changes: 23 additions & 13 deletions code/espurna/relay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions code/espurna/rtcmem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 824e121

Please sign in to comment.