2017-01-08 23:38:20 -05:00
|
|
|
/* ULP Example
|
|
|
|
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2022-12-09 02:04:55 -05:00
|
|
|
#include <inttypes.h>
|
2017-04-21 00:32:50 -04:00
|
|
|
#include "esp_sleep.h"
|
2017-01-08 23:38:20 -05:00
|
|
|
#include "nvs.h"
|
|
|
|
#include "nvs_flash.h"
|
|
|
|
#include "soc/rtc_cntl_reg.h"
|
|
|
|
#include "soc/sens_reg.h"
|
2018-07-13 05:44:57 -04:00
|
|
|
#include "soc/rtc_periph.h"
|
2017-01-08 23:38:20 -05:00
|
|
|
#include "driver/gpio.h"
|
|
|
|
#include "driver/rtc_io.h"
|
2022-02-17 01:14:34 -05:00
|
|
|
#include "ulp.h"
|
2017-01-08 23:38:20 -05:00
|
|
|
#include "ulp_main.h"
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
static void init_ulp_program(void);
|
|
|
|
static void update_pulse_count(void);
|
2017-01-08 23:38:20 -05:00
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void app_main(void)
|
2017-01-08 23:38:20 -05:00
|
|
|
{
|
2017-04-21 00:32:50 -04:00
|
|
|
esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
|
|
|
|
if (cause != ESP_SLEEP_WAKEUP_ULP) {
|
2017-01-08 23:38:20 -05:00
|
|
|
printf("Not ULP wakeup, initializing ULP\n");
|
|
|
|
init_ulp_program();
|
|
|
|
} else {
|
|
|
|
printf("ULP wakeup, saving pulse count\n");
|
|
|
|
update_pulse_count();
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Entering deep sleep\n\n");
|
2017-04-21 00:32:50 -04:00
|
|
|
ESP_ERROR_CHECK( esp_sleep_enable_ulp_wakeup() );
|
2017-01-08 23:38:20 -05:00
|
|
|
esp_deep_sleep_start();
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
static void init_ulp_program(void)
|
2017-01-08 23:38:20 -05:00
|
|
|
{
|
|
|
|
esp_err_t err = ulp_load_binary(0, ulp_main_bin_start,
|
|
|
|
(ulp_main_bin_end - ulp_main_bin_start) / sizeof(uint32_t));
|
|
|
|
ESP_ERROR_CHECK(err);
|
|
|
|
|
2018-07-13 05:44:57 -04:00
|
|
|
/* GPIO used for pulse counting. */
|
|
|
|
gpio_num_t gpio_num = GPIO_NUM_0;
|
2019-07-25 11:11:31 -04:00
|
|
|
int rtcio_num = rtc_io_number_get(gpio_num);
|
|
|
|
assert(rtc_gpio_is_valid_gpio(gpio_num) && "GPIO used for pulse counting must be an RTC IO");
|
2018-07-13 05:44:57 -04:00
|
|
|
|
2017-01-08 23:38:20 -05:00
|
|
|
/* Initialize some variables used by ULP program.
|
|
|
|
* Each 'ulp_xyz' variable corresponds to 'xyz' variable in the ULP program.
|
|
|
|
* These variables are declared in an auto generated header file,
|
|
|
|
* 'ulp_main.h', name of this file is defined in component.mk as ULP_APP_NAME.
|
|
|
|
* These variables are located in RTC_SLOW_MEM and can be accessed both by the
|
|
|
|
* ULP and the main CPUs.
|
|
|
|
*
|
|
|
|
* Note that the ULP reads only the lower 16 bits of these variables.
|
|
|
|
*/
|
|
|
|
ulp_debounce_counter = 3;
|
|
|
|
ulp_debounce_max_count = 3;
|
|
|
|
ulp_next_edge = 0;
|
2019-07-25 11:11:31 -04:00
|
|
|
ulp_io_number = rtcio_num; /* map from GPIO# to RTC_IO# */
|
2017-01-08 23:38:20 -05:00
|
|
|
ulp_edge_count_to_wake_up = 10;
|
|
|
|
|
2018-07-13 05:44:57 -04:00
|
|
|
/* Initialize selected GPIO as RTC IO, enable input, disable pullup and pulldown */
|
|
|
|
rtc_gpio_init(gpio_num);
|
2017-01-08 23:38:20 -05:00
|
|
|
rtc_gpio_set_direction(gpio_num, RTC_GPIO_MODE_INPUT_ONLY);
|
|
|
|
rtc_gpio_pulldown_dis(gpio_num);
|
|
|
|
rtc_gpio_pullup_dis(gpio_num);
|
|
|
|
rtc_gpio_hold_en(gpio_num);
|
|
|
|
|
2022-02-17 01:14:34 -05:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2018-02-09 11:34:38 -05:00
|
|
|
/* Disconnect GPIO12 and GPIO15 to remove current drain through
|
2022-02-17 01:14:34 -05:00
|
|
|
* pullup/pulldown resistors on modules which have these (e.g. ESP32-WROVER)
|
2018-02-09 11:34:38 -05:00
|
|
|
* GPIO12 may be pulled high to select flash voltage.
|
2017-11-03 02:01:33 -04:00
|
|
|
*/
|
2018-02-09 11:34:38 -05:00
|
|
|
rtc_gpio_isolate(GPIO_NUM_12);
|
|
|
|
rtc_gpio_isolate(GPIO_NUM_15);
|
2022-02-17 01:14:34 -05:00
|
|
|
#endif // CONFIG_IDF_TARGET_ESP32
|
|
|
|
|
2018-09-04 06:21:34 -04:00
|
|
|
esp_deep_sleep_disable_rom_logging(); // suppress boot messages
|
2017-11-03 02:01:33 -04:00
|
|
|
|
2018-07-13 05:44:57 -04:00
|
|
|
/* Set ULP wake up period to T = 20ms.
|
2017-01-08 23:38:20 -05:00
|
|
|
* Minimum pulse width has to be T * (ulp_debounce_counter + 1) = 80ms.
|
|
|
|
*/
|
2018-07-13 05:44:57 -04:00
|
|
|
ulp_set_wakeup_period(0, 20000);
|
2017-01-08 23:38:20 -05:00
|
|
|
|
|
|
|
/* Start the program */
|
2018-08-07 09:03:23 -04:00
|
|
|
err = ulp_run(&ulp_entry - RTC_SLOW_MEM);
|
2017-01-08 23:38:20 -05:00
|
|
|
ESP_ERROR_CHECK(err);
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
static void update_pulse_count(void)
|
2017-01-08 23:38:20 -05:00
|
|
|
{
|
2023-03-20 14:41:43 -04:00
|
|
|
const char* nvs_namespace = "plusecnt";
|
2017-01-08 23:38:20 -05:00
|
|
|
const char* count_key = "count";
|
|
|
|
|
|
|
|
ESP_ERROR_CHECK( nvs_flash_init() );
|
2019-04-01 10:13:55 -04:00
|
|
|
nvs_handle_t handle;
|
2023-03-20 14:41:43 -04:00
|
|
|
ESP_ERROR_CHECK( nvs_open(nvs_namespace, NVS_READWRITE, &handle));
|
2017-01-08 23:38:20 -05:00
|
|
|
uint32_t pulse_count = 0;
|
|
|
|
esp_err_t err = nvs_get_u32(handle, count_key, &pulse_count);
|
|
|
|
assert(err == ESP_OK || err == ESP_ERR_NVS_NOT_FOUND);
|
2022-12-09 02:04:55 -05:00
|
|
|
printf("Read pulse count from NVS: %5"PRIu32"\n", pulse_count);
|
2017-01-08 23:38:20 -05:00
|
|
|
|
|
|
|
/* ULP program counts signal edges, convert that to the number of pulses */
|
|
|
|
uint32_t pulse_count_from_ulp = (ulp_edge_count & UINT16_MAX) / 2;
|
|
|
|
/* In case of an odd number of edges, keep one until next time */
|
|
|
|
ulp_edge_count = ulp_edge_count % 2;
|
2022-12-09 02:04:55 -05:00
|
|
|
printf("Pulse count from ULP: %5"PRIu32"\n", pulse_count_from_ulp);
|
2017-01-08 23:38:20 -05:00
|
|
|
|
|
|
|
/* Save the new pulse count to NVS */
|
|
|
|
pulse_count += pulse_count_from_ulp;
|
|
|
|
ESP_ERROR_CHECK(nvs_set_u32(handle, count_key, pulse_count));
|
|
|
|
ESP_ERROR_CHECK(nvs_commit(handle));
|
|
|
|
nvs_close(handle);
|
2022-12-09 02:04:55 -05:00
|
|
|
printf("Wrote updated pulse count to NVS: %5"PRIu32"\n", pulse_count);
|
2017-01-08 23:38:20 -05:00
|
|
|
}
|