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:
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();
}
}
Statistics: Posted by AgentSmithers — Tue Jun 01, 2021 4:41 am
Statistics: Posted by AgentSmithers — Mon May 31, 2021 3:37 am