Touch pad wake up from deep sleep only works once if Brownout detector is turned off

scottsh
Posts: 12
Joined: Wed Dec 09, 2020 6:58 am

Touch pad wake up from deep sleep only works once if Brownout detector is turned off

Postby scottsh » Wed Jan 27, 2021 6:58 am

Following the suggestion on to solve my sleep/reset issues on this thread: https://github.com/espressif/esp-idf/issues/6179,
I turned off the brownout detector. It did seem to solve the problem of spontaneous resets during deep sleep, but I found a new problem.

This code below is an almost identical copy of the touch sample, just with the an added timer wakeup every 24 hours (for daily phone home) and the touch_pad_set_meas_time call to adjust the duty cycle for minimum power usage during deep sleep.
This code was working great when I had the browout detector enabled (as is the default), and it would sleep and wake up on touch events as expected, with the only issue being the occasional random resets (the resets seem to be at least partially hardware dependent and occur at different rates on different boards, so some devices were nearly perfect).

But, once the brownout detector is disabled, it will sleep once, but as soon as the touch sensor is touched once, every subsequent deep sleep wakes up immediately. Thereafter it sits in a pretty tight loop trying to go to sleep then immediately waking up with a touch wakeup reason. Clearly this is related to the change that disabled the brownout detector, but it's not clear why. The only thing that I can think of is that there is some other register state unrelated to brownout detection that is initialized when the brownout detection is on. When brownout detection is disabled, esp_brownout_init never gets called and it looks like the relevant registers will not be touched at all, leaving them at the reset state. In looking at the register layouts, I don't see anything obvious that should be causing this symptom, so I thought I'd report it here and see if anyone else has seen something similar or knows how to address this issue.

This issue repros on the Soala device with the code below. Simply touch pin 13 to wake up.

Code and logs from the repro are below. Full project is attached. Unzip and run on Soala to see the normal wakeup behavior, then run idf.py menuconfig and disable the brownout detector (under Components | ESP32S2 Specific), and run again to see the broken behavior.

main.c:

Code: Select all

#include <string.h>
#include <stdio.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "esp_system.h"
#include "esp_log.h"
#include "driver/rtc_io.h"

#include "sleep.h"

static const char *TAG = "Main";

extern "C" void app_main(void)
{
    int reason = Wakeup();
    
    ESP_LOGI(TAG, "Wake reason=%d", reason);
    GoToSleep();
}
sleep.c:

Code: Select all

// sleep.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_sleep.h"
#include "esp_log.h"
#include "driver/touch_pad.h"
#include "driver/rtc_io.h"
#include "driver/touch_pad.h"
#include "driver/adc.h"
#include "esp_wifi.h"
#include "soc/rtc.h"

static const char *TAG = "Sleep";

static RTC_DATA_ATTR struct timeval sleep_enter_time;

int Wakeup()
{
    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;
    
    int cause = esp_sleep_get_wakeup_cause();
    esp_reset_reason_t reset_reason = esp_reset_reason();

    ESP_LOGI(TAG, "esp_reset_reason=%d  Timespent in deep sleep=%dms", reset_reason, sleep_time_ms);

    switch (cause) {
        case ESP_SLEEP_WAKEUP_TIMER: {
            ESP_LOGI(TAG, "Wake up from timer. Time spent in deep sleep: %dms", sleep_time_ms);
            break;
        }
        case ESP_SLEEP_WAKEUP_TOUCHPAD: {
            ESP_LOGI(TAG, "Wake up from touch on pad %d", esp_sleep_get_touchpad_wakeup_status());
            break;
        }
        case ESP_SLEEP_WAKEUP_ULP: {
            ESP_LOGI(TAG, "Wake up from ULP. Time spent in deep sleep: %dms", sleep_time_ms);
            break;
        }

        case ESP_SLEEP_WAKEUP_UNDEFINED:
        default:
            ESP_LOGI(TAG, "Not a deep sleep reset");
    }
    return cause;
}

void GoToSleep() 
{
    /* Initialize touch pad peripheral. */
    touch_pad_init();
    /* Only support one touch channel in sleep mode. */
    touch_pad_config(TOUCH_PAD_NUM13);

    /* Denoise setting at TouchSensor 0. */
    touch_pad_denoise_t denoise = {
        /* The bits to be cancelled are determined according to the noise level. */
        .grade = TOUCH_PAD_DENOISE_BIT4,
        .cap_level = TOUCH_PAD_DENOISE_CAP_L4,
    };
    touch_pad_denoise_set_config(&denoise);
    touch_pad_denoise_enable();

    /* Filter setting */
    touch_filter_config_t filter_info = {
        .mode = TOUCH_PAD_FILTER_IIR_16,
        .debounce_cnt = 1,      // 1 time count.
        .noise_thr = 0,         // 50%
        .jitter_step = 4,       // use for jitter mode.
        .smh_lvl = TOUCH_PAD_SMOOTH_IIR_2,        
    };
    touch_pad_filter_set_config(&filter_info);
    touch_pad_filter_enable();

    /* Set sleep touch pad. */
    touch_pad_sleep_channel_enable(TOUCH_PAD_NUM13, true);
    touch_pad_sleep_channel_enable_proximity(TOUCH_PAD_NUM13, false);

    /* Enable touch sensor clock. Work mode is "timer trigger". */
    touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
    touch_pad_fsm_start();
    vTaskDelay(100 / portTICK_RATE_MS);
    /* read sleep touch pad value */
    uint32_t touch_value;
    touch_pad_sleep_channel_read_smooth(TOUCH_PAD_NUM13, &touch_value);
        touch_pad_sleep_set_threshold(TOUCH_PAD_NUM13, touch_value * 0.1); //10%
    ESP_LOGI(TAG, "test init: touch pad [%d] slp %d, thresh %d",
        TOUCH_PAD_NUM13, touch_value, (uint32_t)(touch_value * 0.1));

    uint32_t sleep_time_in_ms = 1000;
    uint32_t touchpad_sleep_ticks = (uint32_t)((uint64_t)sleep_time_in_ms * rtc_clk_slow_freq_get_hz() / 1000);

    uint16_t oldslp, oldmeas;
    touch_pad_get_meas_time(&oldslp, &oldmeas);
    ESP_LOGI(TAG, "oldslp=%d  newslp=%d oldmeas=%d", oldslp, touchpad_sleep_ticks, oldmeas);

    touch_pad_set_meas_time(touchpad_sleep_ticks, 500);

    ESP_LOGI(TAG, "Enabling touch pad wakeup");
    esp_sleep_enable_touchpad_wakeup();

    // Enable timer wake up 
    const uint64_t wakeup_time_sec = 3600*24;         // 1 day
    ESP_LOGI(TAG, "Enabling timer wakeup, %llds", wakeup_time_sec);
    esp_sleep_enable_timer_wakeup(wakeup_time_sec * 1000000);

    esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON);

    esp_wifi_stop();
    adc_power_off();
    ESP_LOGI(TAG, "Entering deep sleep\n");
    gettimeofday(&sleep_enter_time, NULL);

    esp_deep_sleep_start();
}
i:\w\sleeponly>idf.py monitor
Executing action: monitor
Choosing default port b'COM3' (use '-p PORT' option to set a specific serial port)
Running idf_monitor in directory i:\w\sleeponly
Executing "C:\Users\scott\.espressif\python_env\idf4.2_py3.7_env\Scripts\python.exe i:\repos\esp-idf-fork\tools/idf_monitor.py -p COM3 -b 115200 --toolchain-prefix xtensa-esp32s2-elf- i:\w\sleeponly\build\remote-scale.elf -m 'C:\Users\scott\.espressif\python_env\idf4.2_py3.7_env\Scripts\python.exe' 'i:\repos\esp-idf-fork\tools\idf.py'"...
←[0;33m--- WARNING: GDB cannot open serial ports accessed as COMx←[0m
←[0;33m--- Using \\.\COM3 instead...←[0m
--- idf_monitor on \\.\COM3 115200 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
ESP-ROM:esp32s2-rc4-20191025
Build:Oct 25 2019
rst:0x1 (POWERON),boot:0x8 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
load:0x3ffe6100,len:0x4
load:0x3ffe6104,len:0x1868
load:0x4004c000,len:0x1688
load:0x40050000,len:0x20fc
entry 0x4004c324
I (47) boot: ESP-IDF v4.2-dev-2488-gce5608c8f 2nd stage bootloader
I (48) boot: compile time 17:38:09
I (48) boot: chip revision: 0
I (51) boot.esp32s2: SPI Speed : 80MHz
I (56) boot.esp32s2: SPI Mode : DIO
I (60) boot.esp32s2: SPI Flash Size : 4MB
I (65) boot: Enabling RNG early entropy source...
I (70) boot: Partition Table:
I (74) boot: ## Label Usage Type ST Offset Length
I (81) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (89) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (96) boot: 2 factory factory app 00 00 00010000 00100000
I (104) boot: End of partition table
I (108) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f000020 size=0x0cad8 ( 51928) map
I (128) esp_image: segment 1: paddr=0x0001cb00 vaddr=0x3ffc46d0 size=0x02eb0 ( 11952) load
I (131) esp_image: segment 2: paddr=0x0001f9b8 vaddr=0x40024000 size=0x00404 ( 1028) load
0x40024000: _WindowOverflow4 at I:/repos/esp-idf-fork/components/freertos/xtensa/xtensa_vectors.S:1730

I (135) esp_image: segment 3: paddr=0x0001fdc4 vaddr=0x40024404 size=0x00254 ( 596) load
I (144) esp_image: segment 4: paddr=0x00020020 vaddr=0x40080020 size=0x34724 (214820) map
0x40080020: _stext at ??:?

I (197) esp_image: segment 5: paddr=0x0005474c vaddr=0x40024658 size=0x10078 ( 65656) load
I (215) esp_image: segment 6: paddr=0x000647cc vaddr=0x40070000 size=0x0001c ( 28) load
I (216) esp_image: segment 7: paddr=0x000647f0 vaddr=0x50000000 size=0x00008 ( 8) load
I (231) boot: Loaded app from partition at offset 0x10000
I (231) boot: Disabling RNG early entropy source...
I (234) cache: Instruction cache : size 8KB, 4Ways, cache line size 32Byte
I (241) cpu_start: Pro cpu up.
I (245) cpu_start: Application information:
I (250) cpu_start: Project name: remote-scale
I (255) cpu_start: App version: 1
I (260) cpu_start: Compile time: Jan 25 2021 17:38:05
I (266) cpu_start: ELF file SHA256: 9bdc21c4abeebd43...
I (272) cpu_start: ESP-IDF: v4.2-dev-2488-gce5608c8f
I (278) cpu_start: Single core mode
I (282) heap_init: Initializing. RAM available for dynamic allocation:
I (289) heap_init: At 3FFCA688 len 00031978 (198 KiB): DRAM
I (296) heap_init: At 3FFFC000 len 00003A10 (14 KiB): DRAM
I (302) cpu_start: Pro cpu start user code
I (368) spi_flash: detected chip: generic
I (368) spi_flash: flash io: dio
I (368) cpu_start: Starting scheduler on PRO CPU.
I (372) Sleep: esp_reset_reason=1 Timespent in deep sleep=8ms
I (372) Sleep: Not a deep sleep reset
I (382) Main: Wake reason=0
I (482) Sleep: test init: touch pad [13] slp 21120, thresh 2112
I (482) Sleep: oldslp=15 newslp=90000 oldmeas=500
I (482) Sleep: Enabling touch pad wakeup
I (482) Sleep: Enabling timer wakeup, 86400s
I (492) Sleep: Entering deep sleep

ESP-ROM:esp32s2-rc4-20191025
Build:Oct 25 2019
rst:0x5 (DSLEEP),boot:0x8 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
load:0x3ffe6100,len:0x4
load:0x3ffe6104,len:0x1868
load:0x4004c000,len:0x1688
load:0x40050000,len:0x20fc
entry 0x4004c324
I (47) boot: ESP-IDF v4.2-dev-2488-gce5608c8f 2nd stage bootloader
I (47) boot: compile time 17:38:09
I (48) boot: chip revision: 0
I (51) boot.esp32s2: SPI Speed : 80MHz
I (56) boot.esp32s2: SPI Mode : DIO
I (60) boot.esp32s2: SPI Flash Size : 4MB
I (65) boot: Enabling RNG early entropy source...
I (70) boot: Partition Table:
I (74) boot: ## Label Usage Type ST Offset Length
I (81) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (89) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (96) boot: 2 factory factory app 00 00 00010000 00100000
I (104) boot: End of partition table
I (108) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f000020 size=0x0cad8 ( 51928) map
I (128) esp_image: segment 1: paddr=0x0001cb00 vaddr=0x3ffc46d0 size=0x02eb0 ( 11952) load
I (131) esp_image: segment 2: paddr=0x0001f9b8 vaddr=0x40024000 size=0x00404 ( 1028) load
0x40024000: _WindowOverflow4 at I:/repos/esp-idf-fork/components/freertos/xtensa/xtensa_vectors.S:1730

I (135) esp_image: segment 3: paddr=0x0001fdc4 vaddr=0x40024404 size=0x00254 ( 596) load
I (144) esp_image: segment 4: paddr=0x00020020 vaddr=0x40080020 size=0x34724 (214820) map
0x40080020: _stext at ??:?

I (197) esp_image: segment 5: paddr=0x0005474c vaddr=0x40024658 size=0x10078 ( 65656) load
I (215) esp_image: segment 6: paddr=0x000647cc vaddr=0x40070000 size=0x0001c ( 28)
I (215) esp_image: segment 7: paddr=0x000647f0 vaddr=0x50000000 size=0x00008 ( 8)
I (230) boot: Loaded app from partition at offset 0x10000
I (230) boot: Disabling RNG early entropy source...
I (233) cache: Instruction cache : size 8KB, 4Ways, cache line size 32Byte
I (240) cpu_start: Pro cpu up.
I (244) cpu_start: Application information:
I (249) cpu_start: Project name: remote-scale
I (254) cpu_start: App version: 1
I (259) cpu_start: Compile time: Jan 25 2021 17:38:05
I (265) cpu_start: ELF file SHA256: 9bdc21c4abeebd43...
I (271) cpu_start: ESP-IDF: v4.2-dev-2488-gce5608c8f
I (277) cpu_start: Single core mode
I (282) heap_init: Initializing. RAM available for dynamic allocation:
I (289) heap_init: At 3FFCA688 len 00031978 (198 KiB): DRAM
I (295) heap_init: At 3FFFC000 len 00003A10 (14 KiB): DRAM
I (301) cpu_start: Pro cpu start user code
I (365) spi_flash: detected chip: generic
I (366) spi_flash: flash io: dio
I (366) cpu_start: Starting scheduler on PRO CPU.
I (369) Sleep: esp_reset_reason=8 Timespent in deep sleep=336352ms
I (369) Sleep: Wake up from touch on pad 13
I (379) Main: Wake reason=5
I (479) Sleep: test init: touch pad [13] slp 21357, thresh 2135
I (479) Sleep: oldslp=15 newslp=90000 oldmeas=500
I (479) Sleep: Enabling touch pad wakeup
I (479) Sleep: Enabling timer wakeup, 86400s
I (489) Sleep: Entering deep sleep

ESP-ROM:esp32s2-rc4-20191025
Build:Oct 25 2019
rst:0x5 (DSLEEP),boot:0x8 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
load:0x3ffe6100,len:0x4
load:0x3ffe6104,len:0x1868
load:0x4004c000,len:0x1688
load:0x40050000,len:0x20fc
entry 0x4004c324
I (47) boot: ESP-IDF v4.2-dev-2488-gce5608c8f 2nd stage bootloader
I (47) boot: compile time 17:38:09
I (48) boot: chip revision: 0
I (51) boot.esp32s2: SPI Speed : 80MHz
I (56) boot.esp32s2: SPI Mode : DIO
I (60) boot.esp32s2: SPI Flash Size : 4MB
I (65) boot: Enabling RNG early entropy source...
I (70) boot: Partition Table:
I (74) boot: ## Label Usage Type ST Offset Length
I (81) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (89) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (96) boot: 2 factory factory app 00 00 00010000 00100000
I (104) boot: End of partition table
I (108) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f000020 size=0x0cad8 ( 51928) map
I (128) esp_image: segment 1: paddr=0x0001cb00 vaddr=0x3ffc46d0 size=0x02eb0 ( 11952) load
I (131) esp_image: segment 2: paddr=0x0001f9b8 vaddr=0x40024000 size=0x00404 ( 1028) load
0x40024000: _WindowOverflow4 at I:/repos/esp-idf-fork/components/freertos/xtensa/xtensa_vectors.S:1730

I (135) esp_image: segment 3: paddr=0x0001fdc4 vaddr=0x40024404 size=0x00254 ( 596) load
I (144) esp_image: segment 4: paddr=0x00020020 vaddr=0x40080020 size=0x34724 (214820) map
0x40080020: _stext at ??:?

I (197) esp_image: segment 5: paddr=0x0005474c vaddr=0x40024658 size=0x10078 ( 65656) load
I (215) esp_image: segment 6: paddr=0x000647cc vaddr=0x40070000 size=0x0001c ( 28)
I (215) esp_image: segment 7: paddr=0x000647f0 vaddr=0x50000000 size=0x00008 ( 8)
I (230) boot: Loaded app from partition at offset 0x10000
I (230) boot: Disabling RNG early entropy source...
I (233) cache: Instruction cache : size 8KB, 4Ways, cache line size 32Byte
I (240) cpu_start: Pro cpu up.
I (244) cpu_start: Application information:
I (249) cpu_start: Project name: remote-scale
I (254) cpu_start: App version: 1
I (259) cpu_start: Compile time: Jan 25 2021 17:38:05
I (265) cpu_start: ELF file SHA256: 9bdc21c4abeebd43...
I (271) cpu_start: ESP-IDF: v4.2-dev-2488-gce5608c8f
I (277) cpu_start: Single core mode
I (282) heap_init: Initializing. RAM available for dynamic allocation:
I (289) heap_init: At 3FFCA688 len 00031978 (198 KiB): DRAM
I (295) heap_init: At 3FFFC000 len 00003A10 (14 KiB): DRAM
I (301) cpu_start: Pro cpu start user code
I (365) spi_flash: detected chip: generic
I (366) spi_flash: flash io: dio
I (366) cpu_start: Starting scheduler on PRO CPU.
I (369) Sleep: esp_reset_reason=8 Timespent in deep sleep=382ms
I (369) Sleep: Wake up from touch on pad 13
I (379) Main: Wake reason=5
I (479) Sleep: test init: touch pad [13] slp 21323, thresh 2132
I (479) Sleep: oldslp=15 newslp=90000 oldmeas=500
I (479) Sleep: Enabling touch pad wakeup
I (479) Sleep: Enabling timer wakeup, 86400s
I (489) Sleep: Entering deep sleep
Thanks,

- Scott
Attachments
sleeponly.zip
(14.78 KiB) Downloaded 349 times

boarchuz
Posts: 601
Joined: Tue Aug 21, 2018 5:28 am

Re: Touch pad wake up from deep sleep only works once if Brownout detector is turned off

Postby boarchuz » Thu Jan 28, 2021 1:31 am

Isn't it likely that you still have power supply problems? That could cause odd behaviour with touch sensors, but you won't know about it now with brownout detection disabled.
If you have a reliable reproduction for the brownout reset issue you should reply to @igrr: https://github.com/adafruit/circuitpyth ... -767419836 (I think it's you in that thread already?)

To satisfy your curiosity here, maybe set brownout config back to default, and try something like this to then disable it later from your app:

Code: Select all

brownout_hal_intr_enable(false);
ets_delay_us(10);
brownout_hal_intr_clear();

scottsh
Posts: 12
Joined: Wed Dec 09, 2020 6:58 am

Re: Touch pad wake up from deep sleep only works once if Brownout detector is turned off

Postby scottsh » Thu Jan 28, 2021 5:03 am

I have a support case with Espressif and have sent them the schematics and source... They are debating internally whether they want me to send them a board that repros the issue.

It looks like that sample will just disable the brownout interrupt, but won't it still reset the system? My theory for this issue is that the brownout detector is overly sensitive some times, particularly when the chip is drawing VERY little power. Looking at the code, the reason code for brownout gets set in the interrupt handler, which would normally catch the interrupt. However when the xtensa core is asleep, that interrupt isn't waking up the CPU, or at least not before the system gets reset, which is why it looks like a power on from the registers.

Disabling the interrupts didn't do anything, but this seems to be working quite well:

CLEAR_PERI_REG_MASK(RTC_CNTL_BROWN_OUT_REG, RTC_CNTL_BROWN_OUT_RST_ENA);

With that, it seems to be not randomly resetting and also waking up and sleeping correctly!!! :)

Thanks for the help! :)

Who is online

Users browsing this forum: No registered users and 86 guests