Is possible to check from Software if Pin D0 and RST is shorted before entering sleep mode?

AgentSmithers
Posts: 181
Joined: Sat Apr 01, 2017 1:21 am
Contact:

Is possible to check from Software if Pin D0 and RST is shorted before entering sleep mode?

Postby AgentSmithers » Mon May 31, 2021 3:37 am

Hi Everyone,
I was wondering if anyone had any ideas on how to check and confirm if a pin is shorted and my thought was to set one as output and the other as input IO and to toggle the pin on and off and read it from the other side to see if the value matches but Im just getting back from a long break and I think that in and of itself will toggle a reset unless I can temporarily disable the RST function of the chip while testing this out. Also any concerns with shorting these pins in the first place, from what I recall these only put out max of 20ma but I think that's still enough to burn the chip without a mild resistor or something in-between or am I incorrect on that point?
Has anyone solved this issue some other way before, I want to prevent the chip from going into deepsleep forever if it is unable to wake.

Thanks!

*Update* I was thinking maybe if there is no other solution to write something to the Rom chip and then set GPIO as an input and bring it low perhaps, then if the chip resets read the Rom and take a different code path?

Anyone have a snippet on how ton configure GPIO16/XPD_DCDC as a GPIO, it seems to be missing from my header file and difficult to find on the net.

AgentSmithers
Posts: 181
Joined: Sat Apr 01, 2017 1:21 am
Contact:

Re: Is possible to check from Software if Pin D0 and RST is shorted before entering sleep mode?

Postby AgentSmithers » Tue Jun 01, 2021 4:41 am

I solved it, Just kinda getting back into the string of things. I used this code to store and read back values set in flash. if a reboot takes place then it will take a different code path. If the pin is not shorted then it will set a flag in the Rom which I later pull to see if Deepsleep is an option, if not I use a system reboot instead after sleeping for the requested duration.

Hope this helps someone else..


*Update* I noticed I wrote this to the RTC, This data from what I recall gets wiped on deep sleep, Ill have to confirm and if thats the case move this to the SPI flash memory.

RTC Data [ Each block holds 4 bytes of data ex. 64,65,66,67 can hold a total of 16 bytes of data]:
64 = RTC / Uptime
68 = NTP Data, This may need to be swapped over to 64, this may be a dupe
72 = Exception RST Data
76 = Station Data

RTC: The data only stays in the buffer during a System.restart, deepsleep(TESTED AND CONFIRMED TRUE 4/22/19), or crash (REASON_EXT_SYS_RST, tested) (, All other actions will clear the RTC such as a Powerloss
System time will return to zero because of system_restart, but RTC still goes on.
If an external hardware reset the chip via EXT_RST or CHIP_EN (such as timed
wakeup from Deep-sleep), the RTC timer will be reset.
• Reset by pin EXT_RST: RTC memory won’t change; RTC timer starts to zero.
• Watchdog reset: RTC memory won’t change; RTC timer won’t change.
• system_restart: RTC memory won’t change; RTC timer won’t change.
• Power-on: RTC memory contains a random value; RTC timer starts from zero.
• Reset by pin CHIP_EN: RTC memory contains a random value; RTC timer
starts from zero.


Code: Select all

bool ICACHE_FLASH_ATTR InitRSTStruct()
{
   os_printf("[%s][%s][%d] - Init RST Struct\r\n", __FILE__ ,__func__, __LINE__);      
   RTC_RST_STRUCT rtc_rst;
   rtc_rst.magic   = 0xDEADBEEF;
   rtc_rst.rst_count = 0;
   rtc_rst.GPIO16RSTEnabled = 1;
   system_rtc_mem_write(72, &rtc_rst, sizeof(rtc_rst));   
   return true;
}

bool ICACHE_FLASH_ATTR DeInitRSTStruct()
{
   os_printf("[%s][%s][%d] - Init RST Struct\r\n", __FILE__ ,__func__, __LINE__);      
   RTC_RST_STRUCT rtc_rst;
   rtc_rst.magic   = 0x10000000;
   rtc_rst.rst_count = 0;
   rtc_rst.GPIO16RSTEnabled = 0;
   system_rtc_mem_write(72, &rtc_rst, sizeof(rtc_rst));   
   return true;
}

bool ICACHE_FLASH_ATTR IsRSTStructInit(void)
{
   os_printf("[%s][%s][%d]\r\n", __FILE__ ,__func__, __LINE__);   
   RTC_RST_STRUCT rtc_rst;
   system_rtc_mem_read(72,   &rtc_rst, sizeof(rtc_rst));
   if(rtc_rst.magic==0xDEADBEEF)
   {
      os_printf("[%s][%s][%d] - IsRSTStructInit=Yes GetRSTCount = %u and GPIOResetEnabled = %u\r\n", __FILE__ ,__func__, __LINE__, rtc_rst.rst_count, rtc_rst.GPIO16RSTEnabled);
      return true;
   }
   else
   {
      os_printf("[%s][%s][%d] - IsRSTStructInit=No\r\n", __FILE__ ,__func__, __LINE__);
      return false;
   }
}

bool ICACHE_FLASH_ATTR StoreRSTCount(uint32 Count)
{
   os_printf("[%s][%s][%d] - creating with: %u\r\n", __FILE__ ,__func__, __LINE__, Count);   
   //system_rtc_mem_write(68, &Count, sizeof(Count));      
   RTC_RST_STRUCT rtc_rst;
   rtc_rst.rst_count = Count;
   system_rtc_mem_write(72, &rtc_rst, sizeof(rtc_rst));      
}

uint32_t ICACHE_FLASH_ATTR GetRSTCount(void)
{
   os_printf("[%s][%s][%d]\r\n", __FILE__ ,__func__, __LINE__);   
   uint32 count;
   //system_rtc_mem_read(68,   &count, sizeof(count));
   RTC_RST_STRUCT rtc_rst;
   system_rtc_mem_read(72,   &rtc_rst, sizeof(rtc_rst));
   if(rtc_rst.magic==0xDEADBEEF)
   {
      os_printf("[%s][%s][%d] - GET GetRSTCount %u\r\n", __FILE__ ,__func__, __LINE__, rtc_rst.rst_count);
      return rtc_rst.rst_count;
   }
   else
   {
      return NULL;
   }
}

bool ICACHE_FLASH_ATTR StoreGPIORstEnabled(uint32 Enabled)
{
   os_printf("[%s][%s][%d] - storing enabled with: %u\r\n", __FILE__ ,__func__, __LINE__, Enabled);   
   //system_rtc_mem_write(68, &Count, sizeof(Count));      
   RTC_RST_STRUCT rtc_rst;
   rtc_rst.GPIO16RSTEnabled = Enabled;
   system_rtc_mem_write(72, &rtc_rst, sizeof(rtc_rst));      
}

bool ICACHE_FLASH_ATTR GetGPIO16RSTEnabled(void)
{
   os_printf("[%s][%s][%d]\r\n", __FILE__ ,__func__, __LINE__);   
   uint32 Enabled;
   //system_rtc_mem_read(68,   &count, sizeof(count));
   RTC_RST_STRUCT rtc_rst;
   system_rtc_mem_read(72,   &rtc_rst, sizeof(rtc_rst));
   if(rtc_rst.magic==0xDEADBEEF)
   {
      os_printf("[%s][%s][%d] - GET GPIO16RSTEnabled %u\r\n", __FILE__ ,__func__, __LINE__, rtc_rst.GPIO16RSTEnabled);
      return (bool)rtc_rst.GPIO16RSTEnabled;
   }
   else
   {
      return NULL;
   }
}


...




if (!IsRSTStructInit())
         {
            delay_5seconds();
            delay_5seconds();
            InitRSTStruct();
            TriggerResetByGPIO16();
            delay_5seconds();
            StoreGPIORstEnabled(0); //Marks it as disabled if it gets to this point.
         }
         
         if (GetGPIO16RSTEnabled())
         {
            os_printf("Deepsleep is tested and enabled\r\n");
         }
         else
         {
            os_printf("Deepsleep is disabled as GPIO16 is not shorted with RST.\r\n");
         }
         
         
...



static ETSTimer DeepSleep_timer;
void ICACHE_FLASH_ATTR DeepSleep(uint64 lTimeInUs, uint32_t kickoffinseconds) //https://stackoverflow.com/questions/14600504/how-many-days-can-i-store-in-4-bytes-with-millisecond-precision
{
   os_printf("[%s][%s][%d] - Sending system into deepsleep for %" PRId64 "US (%" PRId64 " min(s)) in %u seconds (%u min(s) or %ums), starting now...\r\n", __FILE__ ,__func__, __LINE__, lTimeInUs, (lTimeInUs / 1000 / 1000 / 60), kickoffinseconds, (kickoffinseconds / 60), kickoffinseconds * 1000);
   
   if (GetGPIO16RSTEnabled())
   {
      os_timer_disarm(&DeepSleep_timer);
      os_timer_setfn(&DeepSleep_timer, (os_timer_func_t *)DeepSleepNow_timerfunc, lTimeInUs);
      os_timer_arm(&DeepSleep_timer, kickoffinseconds * 1000, 0);

      /*
      deepsleepsetoption(0): Radio calibration after deep-sleep wake up depends on init data byte 108.
      deepsleepsetoption(1): Radio calibration is done after deep-sleep wake up; this increases the current consumption.
      deepsleepsetoption(2): No radio calibration after deep-sleep wake up; this reduces the current consumption.
      deepsleepsetoption(4): Disable RF after deep-sleep wake up, just like modem sleep; this has the least current consumption; the device is not able to transmit or receive data after wake up.
      */
      StoreEXCCount(0); //Reset EXC time to Zero
      deep_sleep_set_option(2); //No RF Recal
   }
   else
   {
      os_printf("Deepsleep is disabled as GPIO16 is not shorted. Using Sleep reboot method\r\n");
      delay_ms(lTimeInUs / 1000);
      Reboot();
   }
}


Who is online

Users browsing this forum: No registered users and 14 guests