mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
87 lines
2.6 KiB
C
87 lines
2.6 KiB
C
/* 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>
|
|
#include <string.h>
|
|
#include "esp_deep_sleep.h"
|
|
#include "nvs.h"
|
|
#include "nvs_flash.h"
|
|
#include "soc/rtc_cntl_reg.h"
|
|
#include "soc/sens_reg.h"
|
|
#include "driver/gpio.h"
|
|
#include "driver/rtc_io.h"
|
|
#include "driver/adc.h"
|
|
#include "driver/dac.h"
|
|
#include "esp32/ulp.h"
|
|
#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");
|
|
|
|
/* This function is called once after power-on reset, to load ULP program into
|
|
* RTC memory and configure the ADC.
|
|
*/
|
|
static void init_ulp_program();
|
|
|
|
/* This function is called every time before going into deep sleep.
|
|
* It starts the ULP program and resets measurement counter.
|
|
*/
|
|
static void start_ulp_program();
|
|
|
|
void app_main()
|
|
{
|
|
esp_deep_sleep_wakeup_cause_t cause = esp_deep_sleep_get_wakeup_cause();
|
|
if (cause != ESP_DEEP_SLEEP_WAKEUP_ULP) {
|
|
printf("Not ULP wakeup\n");
|
|
init_ulp_program();
|
|
} else {
|
|
printf("Deep sleep wakeup\n");
|
|
printf("ULP did %d measurements since last reset\n", ulp_sample_counter & UINT16_MAX);
|
|
printf("Thresholds: low=%d high=%d\n", ulp_low_thr, ulp_high_thr);
|
|
ulp_last_result &= UINT16_MAX;
|
|
printf("Value=%d was %s threshold\n", ulp_last_result,
|
|
ulp_last_result < ulp_low_thr ? "below" : "above");
|
|
}
|
|
printf("Entering deep sleep\n\n");
|
|
start_ulp_program();
|
|
ESP_ERROR_CHECK( esp_deep_sleep_enable_ulp_wakeup() );
|
|
esp_deep_sleep_start();
|
|
}
|
|
|
|
static void init_ulp_program()
|
|
{
|
|
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);
|
|
|
|
/* Configure ADC channel */
|
|
/* Note: when changing channel here, also change 'adc_channel' constant
|
|
in adc.S */
|
|
adc1_config_channel_atten(ADC1_CHANNEL_6, ADC_ATTEN_11db);
|
|
adc1_config_width(ADC_WIDTH_12Bit);
|
|
adc1_ulp_enable();
|
|
|
|
/* Set low and high thresholds, approx. 1.35V - 1.75V*/
|
|
ulp_low_thr = 1500;
|
|
ulp_high_thr = 2000;
|
|
|
|
/* Set ULP wake up period to 100ms */
|
|
ulp_set_wakeup_period(0, 100000);
|
|
}
|
|
|
|
static void start_ulp_program()
|
|
{
|
|
/* Reset sample counter */
|
|
ulp_sample_counter = 0;
|
|
|
|
/* Start the program */
|
|
esp_err_t err = ulp_run((&ulp_entry - RTC_SLOW_MEM) / sizeof(uint32_t));
|
|
ESP_ERROR_CHECK(err);
|
|
}
|