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_DB_11); adc1_config_width(ADC_WIDTH_BIT_12); 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 20ms */ ulp_set_wakeup_period(0, 20000); /* Disconnect GPIO12 and GPIO15 to remove current drain through * pullup/pulldown resistors. * GPIO15 may be connected to ground to suppress boot messages. * GPIO12 may be pulled high to select flash voltage. */ rtc_gpio_isolate(GPIO_NUM_12); rtc_gpio_isolate(GPIO_NUM_15); }
void ulp_monitor_test() { if (esp_deep_sleep_get_wakeup_cause() == ESP_DEEP_SLEEP_WAKEUP_ULP) { struct timeval now; gettimeofday(&now, NULL); int sleep_time_ms = (now.tv_sec - sleep_enter_time.tv_sec) * 1000 + (now.tv_usec - sleep_enter_time.tv_usec) / 1000; ESP_LOGI("ulp_monitor_test", "Time speet in deep sleep: %dms", sleep_time_ms); for (int i = 0; i < ADC_DATA_NUM+2; i++) { printf("the data %d of adc is:%d\n", i, iot_ulp_data_read(ULP_DATA_ADDR + ADC_DATA_OFFSET + i)); } for (int i = 0; i < TEMP_DATA_NUM+2; i++) { printf("the data %d of temprature is:%d\n", i, iot_ulp_data_read(ULP_DATA_ADDR + TEMP_DATA_OFFSET + i)); } } dac_output_enable(DAC_CHANNEL_1); dac_output_voltage(DAC_CHANNEL_1, 127); ESP_LOGI("ulp_monitor_test", "ulp deep sleep wakeup test"); memset(RTC_SLOW_MEM, 0, CONFIG_ULP_COPROC_RESERVE_MEM); iot_ulp_monitor_init(ULP_PROGRAM_ADDR, ULP_DATA_ADDR); //todo: to use the new-style definition of ADC_WIDTH_12Bit. adc1_config_width(ADC_WIDTH_12Bit); //todo: to use the new-style definition of ADC_ATTEN_11db adc1_config_channel_atten(ADC1_CHANNEL_6, ADC_ATTEN_11db); iot_ulp_add_adc_monitor(ADC1_CHANNEL_6, 10, 4000, ADC_DATA_OFFSET, ADC_DATA_NUM, true); //iot_ulp_add_temprature_monitor(0, 300, TEMP_DATA_OFFSET, TEMP_DATA_NUM, true); iot_ulp_monitor_start(3600 / 10); gettimeofday(&sleep_enter_time, NULL); 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); }
void app_main() { //Check if Two Point or Vref are burned into eFuse check_efuse(); //Configure ADC if (unit == ADC_UNIT_1) { adc1_config_width(ADC_WIDTH_BIT_12); adc1_config_channel_atten(channel, atten); } else { adc2_config_channel_atten((adc2_channel_t)channel, atten); } //Characterize ADC adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t)); esp_adc_cal_value_t val_type = esp_adc_cal_characterize(unit, atten, ADC_WIDTH_BIT_12, DEFAULT_VREF, adc_chars); print_char_val_type(val_type); //Continuously sample ADC1 while (1) { uint32_t adc_reading = 0; //Multisampling for (int i = 0; i < NO_OF_SAMPLES; i++) { if (unit == ADC_UNIT_1) { adc_reading += adc1_get_raw((adc1_channel_t)channel); } else { int raw; adc2_get_raw((adc2_channel_t)channel, ADC_WIDTH_BIT_12, &raw); adc_reading += raw; } } adc_reading /= NO_OF_SAMPLES; //Convert adc_reading to voltage in mV uint32_t voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars); printf("Raw: %d\tVoltage: %dmV\n", adc_reading, voltage); vTaskDelay(pdMS_TO_TICKS(1000)); } }