Skip to main content

ESPCLOCK4 - A Simpler Reset Module

In ESPCLOCK3, the following reset circuitry was used:

This generates a short low pulse for the RST pin on the ESP8266 even if the switch is held down, so that the program can read the switch pin after reset. If it it found that the switch is still held down, it can perform a factory reset.

In the ESPCLOCK4, I wanted to try a software-only approach with the ULP. The main idea is the ULP will check the switch (with a little software debounce processing). If it finds the switch depressed, it will wake the main processor, which will then perform a software reset. 

This means we will only need a single pushbutton, with one end connected to an input pin (pulled up), and the other end connected to GND. No need for additional resistors/capacitors.

I connect the reset button to GPIO4/GND, and add the following definitions:

#define RESETBTN_PIN            RTCIO_GPIO4_CHANNEL
#define RESETBTN_PIN_GPIO       GPIO_NUM_4
#define WAKE_RESET_BUTTON       1

The following code is added to init_gpio() to configure the pin as pulled-up input:

void init_gpio() {
  ...
  init_gpio_pin(RESETBTN_PIN_GPIO, RTC_GPIO_MODE_INPUT_OUTPUT, HIGH);
}

Another RTC_SLOW_MEM variable is added to store the wake up reason, which the ULP will use to indicate to the main CPU why it has been woken up.

// Named indices into RTC_SLOW_MEM
enum { 
  ...
  WAKE_REASON, // Reason for waking up main CPU
};

Also add a macro to read the state of a given GPIO pin:

// Read GPIO pin value into R0
#define X_GPIO_GET(pin) \
  I_RD_REG(RTC_GPIO_IN_REG, RTC_GPIO_IN_NEXT_S+pin, RTC_GPIO_IN_NEXT_S+pin)

Now in the ULP, add the following code:

    // Is reset button pressed (active low)?
    X_GPIO_GET(RESETBTN_PIN),
    M_BGE(_RESETBTN_CHECKED, 1),
    I_DELAY(8000), // 1ms debounce
    X_GPIO_GET(RESETBTN_PIN),
    M_BGE(_RESETBTN_CHECKED, 1),
    X_RTC_SAVEI(WAKE_REASON, WAKE_RESET_BUTTON),
  M_LABEL(_WAIT_WAKEUP_RDY),
    I_RD_REG(RTC_CNTL_LOW_POWER_ST_REG, RTC_CNTL_RDY_FOR_WAKEUP_S, RTC_CNTL_RDY_FOR_WAKEUP_S),
    M_BL(_WAIT_WAKEUP_RDY, 1),
    I_WAKE(),
    I_END(),
    I_HALT(),
  M_LABEL(_RESETBTN_CHECKED),
    ...

What the code basically does is to read GPIO4. If it is low, it will wait for a debouncing period of 1ms and read the pin again. If it it still low, it writes WAKE_RESET_BUTTON (1) as the reason code to WAKE_REASON. Then it waits for the RTC_CNTL_RDY_FOR_WAKEUP bit to become 1, which indicates that the main CPU is ready for wake up. At this point, we issue:

- I_WAKE() to wake the main CPU

- I_END() to stop the RTC timer, because we don't want to ULP code to be run again

- I_HALT() to stop the ULP code

In the setup() code, an additional line is added to allow the ULP code to wake the main CPU:

  esp_sleep_enable_ulp_wakeup();
  esp_deep_sleep_start();

The code to handle the wakeup reason is this:

  esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
  if (cause != ESP_SLEEP_WAKEUP_ULP) {
    // Initial startup
    ...
    // If reset detected, start in config mode
    delay(1000); // allow time for reset button to be released
    pinMode(RESETBTN_PIN_GPIO, INPUT_PULLUP);
    bool reset = !digitalRead(RESETBTN_PIN_GPIO);
    if (reset) { 
       // Reset button is still held down
       // Perform factory reset
    }
    ...
  } else {
    // Wake up due to ULP
    uint16_t reason = RTCVAR(WAKE_REASON); 
    if (reason == WAKE_RESET_BUTTON) {
      esp_restart();
    }
  }

The cause value will tell us whether the main CPU is just starting up, or it is being woken up by the ULP. If it is starting up, it checks GPIO4 to see if the button is still being held down. If so, it can initiate the factory reset logic.

If it is woken up by the ULP, it checks the wake reason to determine the action to take. So if the wake reason is WAKE_RESET_BUTTON, we call esp_restart() to reset the ESP32.

Issue with esp_restart()

After playing around with the code above, it was found that esp_restart() does not always perform a full reset of the ESP32. This resulted in the ULP code not running, or running once and stopping, in a unpredictable manner. This behaviour can be observed every 2 or 3 resets using esp_restart().

I googled around, and this post seems to fit the bill. It also appears esp_restart() has a long history of problems: example, example, example, example. This is because esp_restart() performs a software reset of the unit, which is different from pulling the EN pin low, and sometimes it misses certain things.

After much digging around and hair pulling, I finally found a reliable way to perform a hardware reset of the MCU:

void esp_hardware_restart() {
  rtc_wdt_protect_off();
  rtc_wdt_disable();
  rtc_wdt_set_length_of_reset_signal(RTC_WDT_SYS_RESET_SIG, RTC_WDT_LENGTH_3_2us);
  rtc_wdt_set_stage(RTC_WDT_STAGE0, RTC_WDT_STAGE_ACTION_RESET_RTC);
  rtc_wdt_set_time(RTC_WDT_STAGE0, 500);
  rtc_wdt_enable();
  rtc_wdt_protect_on();
  while(true);
}

This uses the RTC watchdog timer to induce a hardware reset. The idea for this solution is taken from this post and this post

The above code sets the stage 0 timeout for the RTC WDT to 500ms. If the WDT is not "fed" or disabled by the code within that time, a hardware reset signal is initiated. You can see that a hardware reset is performed by connecting the ESP32 to a serial monitor. The same status messages as powering on the ESP32 are displayed when the reset occurs. 

When this solution is adopted, everything works as expected. The ULP code runs without a hitch after each reset.

Comments

Popular posts from this blog

Adding "Stereo Mixer" to Windows 7 with Conexant sound card

This procedure worked for my laptop (Thinkpad E530) with a Conexant 20671 sound card, but I suspect it will work for other sound cards in the Conexant family. I was playing with CamStudio to do a video capture of a Flash-based cartoon so that I can put it on the WDTV media player and play it on the big screen in the living room for my kids. The video capture worked brilliantly, but to do a sound capture, I needed to do some hacking. Apparently, there was this recording device called "Stereo Mixer" that was pretty standard in the Windows XP days. This allowed you to capture whatever was played to the speaker in all its digital glory. Then under pressure from various organizations on the dark side of the force, Microsoft and soundcard makers starting disabling this wonderful feature from Windows Vista onwards. So after much Googling around, I found out that for most sound cards, the hardware feature is still there, just not enabled on the software side. Unfortunately, to

Attiny85 timer programming using Timer1

This Arduino sketch uses Timer1 to drive the LED blinker: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 /* * Program ATTiny85 to blink LED connected to PB1 at 1s interval. * Assumes ATTiny85 is running at 1MHz internal clock speed. */ #include <avr/io.h> #include <avr/wdt.h> #include <avr/sleep.h> #include <avr/interrupt.h> bool timer1 = false , led = true ; // Interrupt service routine for timer1 ISR(TIMER1_COMPA_vect) { timer1 = true ; } void setup() { // Setup output pins pinMode( 1 , OUTPUT); digitalWrite( 1 , led); set_sleep_mode(SLEEP_MODE_IDLE); // Setup timer1 to interrupt every second TCCR1 = 0 ; // Stop timer TCNT1 = 0 ; // Zero timer GTCCR = _BV(PSR1); // Reset prescaler OCR1A = 243 ; // T = prescaler / 1MHz = 0.004096s; OCR1A = (1s/T) - 1 = 243 OCR1C = 243 ; // Set to same value to reset timer1 to

Hacking an analog clock to sync with NTP - Part 5

This is how it looks after I have put everything together. The Arduino sketch is available here . The 2 jumper wires soldered to the clock mechanism are connected to pins D0 and D1 on the ESP-12 (in any order). When the device first boots up, it presents an access point which can be connected to via the PC or smartphone. Once connected, the captive portal redirects the web browser to the configuration page:     A custom field has been added to the WiFi configuration page to enter the current clock time in HHMMSS format. Try to set the clock time to as close to the current time as possible using the radial dial at the back of the clock so the clock will have less work to do catching up. In the config page, the HTML5 Geolocation API is also used to obtain your current location (so if your web browser asks if you would like to share your location, answer "yes"). This is then passed to the Google Time Zone API to obtain the time and DST offset of your time z