/* * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ #include #include #include "sdkconfig.h" #include "ulp_lp_core.h" #include "ulp_lp_core_utils.h" #include "ulp_lp_core_gpio.h" #include "ulp_lp_core_interrupts.h" #include "ulp_lp_core_print.h" #include "riscv/csr.h" #define DEBOUNCE_INTERVAL_CYCLES 16000000 // 1000 ms at 16 MHz uint32_t pulse_count = 0; static uint32_t last_trigger_time_cycles = 0; /* Interrupt handler - not currently used (polling mode) */ int main (void) { /* Configure GPIO as input in LP Core */ ulp_lp_core_gpio_init(CONFIG_PULSE_COUNT_PIN); ulp_lp_core_gpio_input_enable(CONFIG_PULSE_COUNT_PIN); ulp_lp_core_gpio_output_disable(CONFIG_PULSE_COUNT_PIN); ulp_lp_core_gpio_pullup_enable(CONFIG_PULSE_COUNT_PIN); ulp_lp_core_gpio_pulldown_disable(CONFIG_PULSE_COUNT_PIN); /* Poll GPIO for rising edges (LOW -> HIGH transitions when reed switch releases from GND) */ int last_gpio_state = ulp_lp_core_gpio_get_level(CONFIG_PULSE_COUNT_PIN); while(1) { int current_gpio_state = ulp_lp_core_gpio_get_level(CONFIG_PULSE_COUNT_PIN); /* Detect rising edge only (LOW -> HIGH transition) */ if (last_gpio_state == 0 && current_gpio_state == 1) { uint32_t edge_time = RV_READ_CSR(mcycle); uint32_t time_since_last = edge_time - last_trigger_time_cycles; /* Debounce: only count if enough time has passed (200ms) */ if (time_since_last > DEBOUNCE_INTERVAL_CYCLES) { pulse_count++; last_trigger_time_cycles = edge_time; /* Wake main CPU when threshold is reached */ if (pulse_count % CONFIG_PULSE_COUNT_WAKEUP_LIMIT == 0) { ulp_lp_core_wakeup_main_processor(); } } } last_gpio_state = current_gpio_state; ulp_lp_core_delay_us(100000); // Poll every 100ms } return 0; }