mirror of
https://github.com/esphome/esphome.git
synced 2026-10-26 16:02:13 +00:00
Compare commits
7
Commits
dev
...
gpio-reset-pulse
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c1bab82766 | ||
|
|
ebdf105939 | ||
|
|
4de4238069 | ||
|
|
2ac96ec0d7 | ||
|
|
2aeb54f285 | ||
|
|
1ba837a2f4 | ||
|
|
30bfba9ce1 |
No files matched your search
@@ -1,4 +1,5 @@
|
||||
#include "esphome/core/gpio.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
@@ -18,4 +19,18 @@ void log_pin(const char *tag, const char *prefix, GPIOPin *pin) {
|
||||
}
|
||||
#endif
|
||||
|
||||
namespace internal {
|
||||
void pulse_reset_ms(GPIOPin *pin, uint32_t high_before_ms, uint32_t low_ms, uint32_t high_after_ms) {
|
||||
if (pin == nullptr)
|
||||
return;
|
||||
pin->setup();
|
||||
const uint32_t waits[] = {high_before_ms, low_ms, high_after_ms};
|
||||
// Level for each step is high, low, high; a zero high_before_ms skips the first step
|
||||
for (size_t i = high_before_ms == 0; i < 3; i++) {
|
||||
pin->digital_write(i != 1);
|
||||
delay(waits[i]);
|
||||
}
|
||||
}
|
||||
} // namespace internal
|
||||
|
||||
} // namespace esphome
|
||||
@@ -144,4 +144,24 @@ void log_pin(const char *tag, const __FlashStringHelper *prefix, GPIOPin *pin);
|
||||
void log_pin(const char *tag, const char *prefix, GPIOPin *pin);
|
||||
#endif
|
||||
|
||||
/// Waveform for pulse_reset(): high, low, then high again. Name the fields at the call site, e.g.
|
||||
/// pulse_reset(this->reset_pin_, {.high_before_ms = 1, .low_ms = 10}).
|
||||
struct ResetPulse {
|
||||
uint32_t high_before_ms{0}; ///< Drive high this long first; skipped when 0
|
||||
uint32_t low_ms{0}; ///< Hold the pin low (in reset) this long
|
||||
uint32_t high_after_ms{0}; ///< Wait this long after releasing the pin
|
||||
};
|
||||
|
||||
namespace internal {
|
||||
/// Out-of-line body of pulse_reset(); call that instead.
|
||||
void pulse_reset_ms(GPIOPin *pin, uint32_t high_before_ms, uint32_t low_ms, uint32_t high_after_ms);
|
||||
} // namespace internal
|
||||
|
||||
/// Set up an active-low reset pin and drive it through \p pulse. Blocks for the whole sequence;
|
||||
/// does nothing when pin is null. Inline so the fields reach pulse_reset_ms() in registers
|
||||
/// (RISC-V would otherwise pass the struct by reference).
|
||||
ESPHOME_ALWAYS_INLINE inline void pulse_reset(GPIOPin *pin, ResetPulse pulse) {
|
||||
internal::pulse_reset_ms(pin, pulse.high_before_ms, pulse.low_ms, pulse.high_after_ms);
|
||||
}
|
||||
|
||||
} // namespace esphome
|
||||
@@ -0,0 +1,51 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "esphome/core/gpio.h"
|
||||
#include "esphome/core/hal.h"
|
||||
|
||||
namespace esphome::core::testing {
|
||||
|
||||
class RecordingPin : public GPIOPin {
|
||||
public:
|
||||
void setup() override { this->events += 'S'; }
|
||||
void pin_mode(gpio::Flags /*flags*/) override {}
|
||||
gpio::Flags get_flags() const override { return gpio::FLAG_OUTPUT; }
|
||||
bool digital_read() override { return false; }
|
||||
void digital_write(bool value) override {
|
||||
this->events += value ? 'H' : 'L';
|
||||
this->write_ms.push_back(millis());
|
||||
}
|
||||
|
||||
std::string events;
|
||||
std::vector<uint32_t> write_ms;
|
||||
};
|
||||
|
||||
TEST(PulseReset, NullPinIsIgnored) { pulse_reset(nullptr, {.low_ms = 1}); }
|
||||
|
||||
TEST(PulseReset, LowThenHigh) {
|
||||
RecordingPin pin;
|
||||
pulse_reset(&pin, {.low_ms = 1});
|
||||
EXPECT_EQ(pin.events, "SLH");
|
||||
}
|
||||
|
||||
TEST(PulseReset, PreHighDrivesHighFirst) {
|
||||
RecordingPin pin;
|
||||
pulse_reset(&pin, {.high_before_ms = 1, .low_ms = 1, .high_after_ms = 1});
|
||||
EXPECT_EQ(pin.events, "SHLH");
|
||||
}
|
||||
|
||||
// Distinct waits, so dropping or swapping one shows up; only lower bounds, a busy host can run long
|
||||
TEST(PulseReset, HoldsEachLevelForItsTime) {
|
||||
RecordingPin pin;
|
||||
pulse_reset(&pin, {.high_before_ms = 10, .low_ms = 20, .high_after_ms = 30});
|
||||
const uint32_t end = millis();
|
||||
ASSERT_EQ(pin.events, "SHLH");
|
||||
EXPECT_GE(pin.write_ms[1] - pin.write_ms[0], 10u);
|
||||
EXPECT_GE(pin.write_ms[2] - pin.write_ms[1], 20u);
|
||||
EXPECT_GE(end - pin.write_ms[2], 30u);
|
||||
}
|
||||
|
||||
} // namespace esphome::core::testing
|
||||
Reference in new issue
Block a user