Weird problem when using "wifi_fpm_set_sleep_type(LIGHT_SLEEP_T)"

wogo
Posts: 3
Joined: Tue Mar 15, 2016 6:18 am
Location: Munich, Germany
Contact:

Weird problem when using "wifi_fpm_set_sleep_type(LIGHT_SLEEP_T)"

Postby wogo » Thu Jul 07, 2016 5:40 am

I've written a program that reads several sensors and sends the
result every 15 minutes via TCP to a server in my local net.
The sleep phases are done by calling
wifi_fpm_set_sleep_type(LIGHT_SLEEP_T) and
wifi_fpm_do_sleep(sleep_time).

Since sleep_time has a maximal value of about 4 minutes 28 seconds
(268.435.455 mmsec) I had to program a loop with several sleep calls.
Everything works fine but after exactly 4 hours 34 minutes and 40
seconds
the system stops working.
This timing is independent of the sleep_time value in the loop
(I tried 3 minutes/5 loops, 4 minutes/4 loops and other values) and
always after 4 hours 34 minutes and 40 seconds I got a "silent death".
I stripped down the program, eliminating all handling except the
sleep calls and after the same time as before: silent death.
Very very weird !

The source of the test program is attached below (add uart.c from SDK)
The test takes a long time (3.6 hours!) until the error happens!

Code: Select all

/*
 * user_main.c - Testprogram for
 *    error in wifi_fpm_set_wakeup_cb()
 *
 *  Author: Wolfgang Gothier
 *  Created on: 2016-07-05
 */

#include "user_config.h"
#include <c_types.h>
#include <os_type.h>
#include <osapi.h>
#include <user_interface.h>

#define user_procTaskPrio        0
#define user_procTaskQueueLen    1

#define sleepLoops   5         // 5 loops for 15 minutes sleeping
#define sleep_time   180000000   // 3 minutes
#define action_time   8000      // 8 seconds


extern int ets_uart_printf(const char *fmt, ...);
int (*console_printf)(const char *fmt, ...) = ets_uart_printf;

void intr_loop_sleep(void *arg);
void intr_loop_last(void *arg);
void action(void);

static os_timer_t action_evaluate;
os_event_t user_procTaskQueue[user_procTaskQueueLen];


LOCAL uint8_t sleepcount;
LOCAL uint32_t timeroverflow = 0;
LOCAL uint32_t last_time = 0;
LOCAL uint32_t time_overflow_value = (0xFFFFFFFFF / 10000);

const char *FlashSizeMap[] = { "512 KB (256 KB + 256 KB)",   // 0x00
      "256 KB",         // 0x01
      "1024 KB (512 KB + 512 KB)",    // 0x02
      "2048 KB (512 KB + 512 KB)"   // 0x03
            "4096 KB (512 KB + 512 KB)"// 0x04
            "2048 KB (1024 KB + 1024 KB)"// 0x05
            "4096 KB (1024 KB + 1024 KB)"   // 0x06
      };

/* ------ subroutine print time ------ */
void print_time(void) {
   char buffer[32];
   uint32_t time;
   time = system_get_time() / 10000 + timeroverflow;
   if ( time < last_time ) {
      timeroverflow += time_overflow_value;
      time += time_overflow_value;
   }
   last_time = time;
   console_printf("Time = %5d.%02d sec\r\n", time / 100,
         (time - ((int) (time / 100)) * 100));
}

void intr_loop_sleep(void *arg) {
   /* ------ wakeup callback routine for multiple sleep calls ------ */
   sint8 retcode;
   os_delay_us(500000);
   console_printf("awaken at");
   print_time();
   wifi_fpm_close();                     // disable force sleep function
   wifi_fpm_set_sleep_type(LIGHT_SLEEP_T); // light sleep
   wifi_fpm_open();                        // enable force sleep
   sleepcount -= 1;                  // decrement loop counter
   if (sleepcount > 0) {
      wifi_fpm_set_wakeup_cb(intr_loop_sleep);      // loop again
   } else {
      wifi_fpm_set_wakeup_cb(intr_loop_last);      // awake for measure
   }
   do {
      retcode = wifi_fpm_do_sleep(sleep_time);
      if (retcode != 0) {
         console_printf("error %d on wifi_fpm_do_sleep\r\n", retcode);
         os_delay_us(1000000);   // wait a second
      }
   } while (retcode != 0);
}

void intr_loop_last(void *arg) {
   /* ------ wakeup callback routine after last sleep call ------ */
   char buff[10];
   console_printf("awaken (last) at ");
   print_time();
   wifi_fpm_close();      // disable force sleep function
   os_delay_us(500000);
   action();
}

void intr_action_finished(void *arg) {
   /* ------ subroutine start sleep loop after action  ------ */
   os_delay_us(500000);
   console_printf("start sleep loop at ");
   print_time();
   os_timer_disarm(&action_evaluate);
   sleepcount = sleepLoops - 1; // except first and last
   /* the wifi_fpm_do_sleep(time) has a maximum time of
    * about 268 seconds. In order to sleep 15 Minutes
    * we must use a sleep loop here */

   wifi_set_opmode(NULL_MODE);          // set WiFi mode to null mode.
   wifi_fpm_set_sleep_type(LIGHT_SLEEP_T); // light sleep
   wifi_fpm_open();                       // enable force sleep
   wifi_fpm_set_wakeup_cb(intr_loop_sleep);      // Set wakeup callback
   sint8 retcode;
   do {
      retcode = wifi_fpm_do_sleep(sleep_time); // first sleep
      if (retcode != 0) {
         console_printf("error %d on wifi_fpm_do_sleep\r\n",
               retcode);
         os_delay_us(1000000);   // wait a second
      }
   } while (retcode != 0);
}


void action(void) {
   console_printf("simulate actions now\r\n");
   os_timer_setfn(&action_evaluate, (os_timer_func_t *) intr_action_finished,
   NULL);
   os_timer_arm(&action_evaluate, action_time, 0);
}

LOCAL void initDone() {
   gpio_init();

   console_printf("==== System info: ====\r\n");
   console_printf("SDK version:%s rom %d\r\n", system_get_sdk_version(),
         system_upgrade_userbin_check());
   console_printf("Time = %ld\r\n", system_get_time());
   console_printf("Chip id = 0x%x\r\n", system_get_chip_id());
   console_printf("CPU freq = %d MHz\r\n", system_get_cpu_freq());
   console_printf("Flash size map = %s\r\n",
         FlashSizeMap[system_get_flash_size_map()]);
   console_printf("Free heap size = %d\r\n", system_get_free_heap_size());
   action();
} // End of initDone

static void ICACHE_FLASH_ATTR user_procTask(os_event_t *events) {
   os_delay_us(5000);
}

void ICACHE_FLASH_ATTR user_init(void) {
   uart_init(115200, 115200);
   console_printf("\r\n");
   console_printf("Start\r\n");
   os_delay_us(1000);
   system_init_done_cb(initDone);
   system_os_task(user_procTask, 0, user_procTaskQueue, 1);
}

wogo
Posts: 3
Joined: Tue Mar 15, 2016 6:18 am
Location: Munich, Germany
Contact:

Re: Weird problem when using "wifi_fpm_set_sleep_type(LIGHT_SLEEP_T)"

Postby wogo » Thu Jul 07, 2016 12:05 pm

Sorry, there is a typing error in the code concerning the print_time routine: line 37 should read:
LOCAL uint32_t time_overflow_value = (0xFFFFFFFF / 10000);
instead of
LOCAL uint32_t time_overflow_value = (0xFFFFFFFFF / 10000);

wogo
Posts: 3
Joined: Tue Mar 15, 2016 6:18 am
Location: Munich, Germany
Contact:

Re: Weird problem when using "wifi_fpm_set_sleep_type(LIGHT_SLEEP_T)"

Postby wogo » Thu Jul 07, 2016 12:22 pm

Here is a listing of the UART output produced by the test program:

Code: Select all

=~=~=~=~=~=~=~=~=~=~=~= PuTTY log 2016.07.07 00:00:02 =~=~=~=~=~=~=~=~=~=~=~=
Start
==== System info: ====
SDK version:1.5.0 rom 0
Time = 283811
Chip id = 0x2008
CPU freq = 80 MHz
Flash size map = 512 KB (256 KB + 256 KB)
Free heap size = 54872
simulate actions now
start sleep loop at Time =     8.79 sec
awaken at Time =   189.29 sec
awaken at Time =   369.79 sec
awaken at Time =   550.29 sec
awaken at Time =   730.79 sec
awaken (last) at Time =   910.79 sec
simulate actions now
start sleep loop at Time =   919.80 sec
awaken at Time =  1100.30 sec
awaken at Time =  1280.80 sec
awaken at Time =  1461.30 sec
awaken at Time =  1641.80 sec
awaken (last) at Time =  1821.80 sec
simulate actions now
start sleep loop at Time =  1830.81 sec
awaken at Time =  2011.31 sec
awaken at Time =  2191.81 sec
awaken at Time =  2372.31 sec

... 70 lines deleted ...

awaken at Time = 11662.87 sec
awaken (last) at Time = 11842.87 sec
simulate actions now
start sleep loop at Time = 11851.88 sec
awaken at Time = 12032.38 sec
awaken at Time = 12212.88 sec
awaken at Time = 12393.38 sec
awaken at Time = 12573.88 sec
awaken (last) at Time = 12753.88 sec
simulate actions now
start sleep loop at Time = 12762.89 sec
awaken at Time = 12943.38 sec
awaken at Time = 13123.88 sec
awaken at Time = 13304.38 sec
awaken at Time = 13484.88 sec
awaken (last) at Time = 13664.88 sec
simulate actions now
start sleep loop at Time = 13673.89 sec
awaken at Time = 13675.08 sec

... silent death, no output any more

Who is online

Users browsing this forum: No registered users and 3 guests