pulse_counter: ULP maintain total pulse-count through deep sleep

Note that the ULP program is not able to count pulse rate through deep-sleep yet due to the loss of timing over deep-sleep
This commit is contained in:
brisk 2024-04-13 14:55:08 +09:30
parent a7c68f4e08
commit 68b0e145f8
2 changed files with 20 additions and 3 deletions

View file

@ -5,6 +5,7 @@
#include "ulp_main.h"
#include "soc/rtc_periph.h"
#include "driver/rtc_io.h"
#include <esp_sleep.h>
#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));

View file

@ -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