Software ESP32 restart ( ESP.restart()) doesn't seems to fully reset the hardware

Rrobinet
Posts: 29
Joined: Wed Aug 16, 2017 7:05 am

Software ESP32 restart ( ESP.restart()) doesn't seems to fully reset the hardware

Postby Rrobinet » Sun Mar 11, 2018 1:58 pm

    Hi,
    With the following script I am using the Hardware Serial interfaces.
    Practically the simplified script shown below, uses a Nextion LCD display that connects via the Hardware interface 2.
    The script ((see below) writes and reads a Nextion HMI variable, through the hardware interface 2 (pin 16=RX, pin 17=TX baud rate 9600).
    In a more elaborate script I am using the ESP.restart() call to reset the ESP32 (WEMOS LOLIN32) after reconfiguration, however I need to physically restart the ESP to let the hardware interface work again,
    the software restart seems to leave the hardware interface in a unknown state.

    The simplified script below writes and reads 5 times back the data stored by the Nextion MCU.

    The result shows that everything is fine until the "software restart" occurs, only a physical restart or firmware reloads solves the problem
    • !!!!! 1. Loading the script
      ⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮ets Jun 8 2016 00:22:57

      rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
      configsip: 0, SPIWP:0xee
      clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
      mode:DIO, clock div:1
      load:0x3fff0018,len:4
      load:0x3fff001c,len:812
      load:0x40078000,len:0
      load:0x40078000,len:11392
      entry 0x40078a9c
      recvRetCommandFinished ok <-- Nextion Init is OK
      recvRetCommandFinished ok

      Doing Nextion Communication test <-- Communication test 1 is OK
      Now updating COMMV1 value
      recvRetCommandFinished ok
      recvRetNumber :319
      Nextion test is OK

      Doing Nextion Communication test <-- Communication test 2 is OK
      Now updating COMMV1 value
      recvRetCommandFinished ok
      recvRetNumber :267
      Nextion test is OK

      Doing Nextion Communication test <-- Communication test 3 is OK
      Now updating COMMV1 value
      recvRetCommandFinished ok
      recvRetNumber :697
      Nextion test is OK

      Doing Nextion Communication test <-- Communication test 4 is OK
      Now updating COMMV1 value
      recvRetCommandFinished ok
      recvRetNumber :779
      Nextion test is OK

      Doing Nextion Communication test <-- Communication test 5 is OK
      Now updating COMMV1 value
      recvRetCommandFinished ok
      recvRetNumber :29
      Nextion test is OK

      !!!! 2. ESP.restart()
      ets Jun 8 2016 00:22:57

      rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
      configsip: 0, SPIWP:0xee
      clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
      mode:DIO, clock div:1
      load:0x3fff0018,len:4
      load:0x3fff001c,len:812
      load:0x40078000,len:0
      load:0x40078000,len:11392
      entry 0x40078a9c
      recvRetCommandFinished err <-- Nextion Init is NOK
      recvRetCommandFinished err

      Doing Nextion Communication test <-- Communication test 1 is NOK
      Now updating COMMV1 value
      recvRetCommandFinished err
      recvRetNumber err
      Nextion test FAILED
      653
      0

      Doing Nextion Communication test <-- Communication test 2 is NOK
      Now updating COMMV1 value
      recvRetCommandFinished err
      recvRetNumber err
      Nextion test FAILED
      352
      1073488868

      Doing Nextion Communication test <-- Communication test 3 is NOK
      Now updating COMMV1 value
      recvRetCommandFinished err
      recvRetNumber err
      Nextion test FAILED
      200
      1073488868

      Doing Nextion Communication test <-- Communication test 4 is NOK
      Now updating COMMV1 value
      recvRetCommandFinished err
      recvRetNumber err
      Nextion test FAILED
      58
      1073488868

      Doing Nextion Communication test <-- Communication test 5 is NOK
      Now updating COMMV1 value
      recvRetCommandFinished err
      recvRetNumber err
      Nextion test FAILED
      739
      1073488868

      !!! 3. Hardware reset
      rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
      ets Jun 8 2016 00:22:57

      rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
      configsip: 0, SPIWP:0xee
      clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
      mode:DIO, clock div:1
      load:0x3fff0018,len:4
      load:0x3fff001c,len:812
      load:0x40078000,len:0
      load:0x40078000,len:11392
      entry 0x40078a9c
      recvRetCommandFinished err
      recvRetCommandFinished ok <-- Nextion Init is OK

      Doing Nextion Communication test <-- Communication test 1 is OK
      Now updating COMMV1 value
      Now updating COMMV1 value
      recvRetCommandFinished ok
      recvRetNumber :976
      Nextion test is OK

      Doing Nextion Communication test <-- Communication test 2 is OK
      Now updating COMMV1 value
      Now updating COMMV1 value
      recvRetCommandFinished ok
      recvRetNumber :114
      Nextion test is OK

      Doing Nextion Communication test <-- Communication test 3 is OK
      Now updating COMMV1 value
      Now updating COMMV1 value
      recvRetCommandFinished ok
      recvRetNumber :761
      Nextion test is OK

      Doing Nextion Communication test <-- Communication test 4 is OK
      Now updating COMMV1 value
      Now updating COMMV1 value
      recvRetCommandFinished ok
      recvRetNumber :855
      Nextion test is OK
      .....

    Code: Select all

    #include "Nextion.h"                  // Include Nextion library
    HardwareSerial Serial2(2);            // Default pins for Wemos Lolin32 are pin 16=RX, pin 17=TX baud rate 9600
    long int nexPollTime;                 // USed to collected itermediate time  
    NexVariable   COMMV1        =   NexVariable      (0,23,"COMMV1");         // Nextion Communication watchdog variable
    #define DEBUGGING
    
    void setup() {
         nexInit ();        // Init the nextion communication (typically Serial 2 interface  @9600 bps)
         delay (100);       // Settle down delay
    }
    
    void loop() {
      // Call 5 times the Nextion befor restart
      for (int i = 0; i <5; i++) 
      {  
        nexCommCheck (0);   // Check the Nextion connection immediately
        delay (1000);       // Wait 1 second
      }
       ESP.restart();       // Then restart the WeMos
      
    }
    
    void nexCommCheck (int long pollRate)
    {
       if (millis()>nexPollTime+pollRate)
       {
    #ifdef DEBUGGING 
      Serial.println ("\nDoing Nextion Communication test") ;
    #endif      
          nexPollTime = millis();
          uint32_t setvalue = random (1000);
          uint32_t getvalue;
    #ifdef DEBUGGING
      Serial.println ("Now updating COMMV1 value");
    #endif
          COMMV1.setValue (setvalue);
          COMMV1.getValue (&getvalue);
          if (getvalue == setvalue)          // Nextion Write check
         {   
    #ifdef DEBUGGING    
         Serial.println ("Nextion test is OK");
    #endif 
         }
         else 
         {
    #ifdef DEBUGGING
          Serial.println ("Nextion test FAILED"); 
          Serial.println (setvalue);
          Serial.println (getvalue);
    #endif   
         }
       }
    }

    vonnieda
    Posts: 145
    Joined: Tue Nov 07, 2017 3:42 pm

    Re: Software ESP32 restart ( ESP.restart()) doesn't seems to fully reset the hardware

    Postby vonnieda » Mon Mar 12, 2018 12:13 am

    FYI: I've recently posted the same question, and no responses yet: https://esp32.com/viewtopic.php?f=2&t=4949

    Rrobinet
    Posts: 29
    Joined: Wed Aug 16, 2017 7:05 am

    Re: Software ESP32 restart ( ESP.restart()) doesn't seems to fully reset the hardware

    Postby Rrobinet » Tue Mar 13, 2018 1:56 pm

    This problem is solved (see https://github.com/espressif/arduino-esp32/issues/1189)
    by applying the UART patch on esp323_hal_uart.c
    replacing : void uartFlush(uart_t* uart) (on the latest https://github.com/espressif/arduino-esp32) by:

    Code: Select all

    void uartFlush(uart_t* uart)
    {
        if(uart == NULL)
        {
        return;
        }
    
        UART_MUTEX_LOCK();
        while(uart->dev->status.rxfifo_cnt != 0 || (uart->dev->mem_rx_status.wr_addr != uart->dev->mem_rx_status.rd_addr))
        {
           READ_PERI_REG(UART_FIFO_REG(uart->num));
        }
    UART_MUTEX_UNLOCK();
    }
    Robert

    tihomir.9a4gl
    Posts: 2
    Joined: Sun Jul 29, 2018 7:28 pm

    Re: Software ESP32 restart ( ESP.restart()) doesn't seems to fully reset the hardware

    Postby tihomir.9a4gl » Sun Jul 29, 2018 7:31 pm

    The above modification does not help in my case, but...

    I had the same issue and I read somewhere that Arduino HardwareSerial implementation has this issue and the ESP-IDF is fine. So, I tried to implement my HardwareSerial using ESP-IDF and this really works fine, you can do ESP.restart and UART2 works !

    I pushed my implementation called ESP32Serial here https://github.com/9a4gl/ESP32Serial

    Rrobinet
    Posts: 29
    Joined: Wed Aug 16, 2017 7:05 am

    Re: Software ESP32 restart ( ESP.restart()) doesn't seems to fully reset the hardware

    Postby Rrobinet » Mon Jul 30, 2018 7:09 am

    @tihomir.9a4gl
    The issue(s) with the secondary UART interfaces is discussed on several places, and very confusing.
    So as I understand the patch described above was a workaround for me, but not an actual solution.

    Last week I have decided to verify if this problem was not solved by the latest version of the espressif-arduino library and as a matter of fact it is. So please try the last version to verify if this solves your problem too.

    Now there is a undocumented feature; you don't (may) not specify

    Code: Select all

    HardwareSerial Serial2(2)
    in the script or include the

    Code: Select all

    #include HardwareSerial.h
    library , this is implicitly done in the arduino library.
    Just specify

    Code: Select all

    Serial2.begin(115200)
    for instance to set the baud rate for the 2nd interface for example
    I tried my script above by simply remove

    Code: Select all

    //HardwareSerial Serial2(2);            // Default pins for Wemos Lolin32 are pin 16=RX, pin 17=TX baud rate 9600

    which is working fine.
    Note that the baud rate of the Serial2 is defined in the nextInit() function
    Robert

    tihomir.9a4gl
    Posts: 2
    Joined: Sun Jul 29, 2018 7:28 pm

    Re: Software ESP32 restart ( ESP.restart()) doesn't seems to fully reset the hardware

    Postby tihomir.9a4gl » Sun Aug 05, 2018 4:13 pm

    > Last week I have decided to verify if this problem was not solved by the latest version
    > of the espressif-arduino library and as a matter of fact it is. So please try the last version to verify if this solves your problem too.

    I am using atom + platformio, just noticed there is update for Espressif32 platform, 5 days old. New version is 1.2.0. So I upgraded, and I see news is that Serial2 does not need to be created, it is already created by Arduino framework. But bug is still there, Serial2 works for me when plugged to power supply. After ESP.restart() it get stuck when I try to use UART2 and complete platform is blocked. Using my ESP32Serial implementation it still works. So, I am not sure you are doing same as me or we are using different version of espressif-arduino. Anyone else still have issue described here ?

    BR,
    Tihomir

    Rrobinet
    Posts: 29
    Joined: Wed Aug 16, 2017 7:05 am

    Re: Software ESP32 restart ( ESP.restart()) doesn't seems to fully reset the hardware

    Postby Rrobinet » Tue Aug 07, 2018 9:05 am

    thimor,

    You are probably right, looking a the new release of the only difference with the esp32-hal-uart.c is that the patch is now included.
    So if it was not working for you before, it should not work now.
    However it fix my issue
    Now with this patch there is a comment:
    //Due to hardware issue, we can not use fifo_rst to reset uart fifo.
    //See description about UART_TXFIFO_RST and UART_RXFIFO_RST in <<esp32_technical_reference_manual>> v2.6 or later
    So it might be an hardware issue related to ESP32 versions

    Robert

    Who is online

    Users browsing this forum: Baidu [Spider], Google [Bot], Majestic-12 [Bot] and 73 guests