diff --git a/esphome/components/pulse_counter/pulse_counter_sensor.cpp b/esphome/components/pulse_counter/pulse_counter_sensor.cpp index eaf424fe50..ef782531c9 100644 --- a/esphome/components/pulse_counter/pulse_counter_sensor.cpp +++ b/esphome/components/pulse_counter/pulse_counter_sensor.cpp @@ -5,6 +5,7 @@ #include "ulp_main.h" #include "soc/rtc_periph.h" #include "driver/rtc_io.h" +#include #endif namespace esphome { @@ -144,8 +145,7 @@ pulse_counter_t HwPulseCounterStorage::read_raw_value() { #ifdef CONF_USE_ULP extern const uint8_t ulp_main_bin_start[] asm("_binary_ulp_main_bin_start"); -extern const uint8_t ulp_main_bin_end[] asm("_binary_ulp_main_bin_end"); - +extern const uint8_t ulp_main_bin_end[] asm("_binary_ulp_main_bin_end"); bool UlpPulseCounterStorage::pulse_counter_setup(InternalGPIOPin *pin) { this->pin = pin; @@ -176,6 +176,13 @@ bool UlpPulseCounterStorage::pulse_counter_setup(InternalGPIOPin *pin) { break; } + if (esp_sleep_get_wakeup_cause() == ESP_SLEEP_WAKEUP_UNDEFINED) { + ESP_LOGD(TAG, "Did not wake up from sleep, assuming restart or first boot and setting up ULP program"); + } else { + ESP_LOGD(TAG, "Woke up from sleep, skipping set-up of ULP program"); + return true; + } + esp_err_t error = ulp_load_binary(0, ulp_main_bin_start, (ulp_main_bin_end - ulp_main_bin_start) / sizeof(uint32_t)); if (error != ESP_OK) { ESP_LOGE(TAG, "Loading ULP binary failed: %s", esp_err_to_name(error)); diff --git a/esphome/components/pulse_counter/ulp/pulse_cnt.S b/esphome/components/pulse_counter/ulp/pulse_cnt.S index aec9bd3401..b6625a8db4 100644 --- a/esphome/components/pulse_counter/ulp/pulse_cnt.S +++ b/esphome/components/pulse_counter/ulp/pulse_cnt.S @@ -50,11 +50,16 @@ debounce_counter: debounce_max_count: .long 0 - /* Total number of signal edges acquired */ + /* Number of signal edges acquired since last read */ .global edge_count edge_count: .long 0 + /* Total number of signal edges acquired */ + .global edge_count_total +edge_count_total: + .long 0 + /* RTC IO number used to sample the input signal. Set by main program. */ .global io_number @@ -144,5 +149,10 @@ edge_detected: ld r2, r3, 0 add r2, r2, 1 st r2, r3, 0 + /* Increment edge_count_total */ + move r3, edge_count_total + ld r2, r3, 0 + add r2, r2, 1 + st r2, r3, 0 /* End program */ halt