Chat here about code rewrites, mods, etc... with respect to the github project https://github.com/esp8266/Arduino

Moderator: igrr

User avatar
By M15H
#72865 Basically I'm just putting this here to have a brief discussion. I'm pretty sure I know what is going on behind the scenes but I would like to verify or disprove my assumption.

I know this is sh*t code since it is blocking and I'm going to rewrite it asynchronously but I found something weird and I'm just wondering if anyone has an explanation.

Basically I have a loop that uses the current time to perform a countdown and display the info on a neopixel matrix.

Inside of this loop, if my time is set to pull from the attached RTC then all is good but if the time is set to pull from the time() function the time never updates...

Digging into the core time.c library I see very clearly that it uses sntp_get_current_timestamp(); from the SDK core sntp.c file where the realtime_stamp is incremented by sntp_time_inc() and that is called by os_timer_setfn as a callback. Digging deeper it seems that the os_timer functions are remapped to ets_timer functions which are defined in ets_sys.h. My guess is that these ets functions are mapped directly to hardware at that point.

Now my thoughts are that this hardware should be interrupt driven which should update the time but it appears more likely that the time is not incremented until after the user loop completes a cycle.

In the snippet below the function get time returns a time either from the RTC or from time() depending on what flags are set. Again if the time() function is used the loop becomes infinite.

Code: Select all    void displayCountdown(){
        time_t cur = getTime();
        Serial.printf("displayCountdown %d\n\r",ESP.getFreeHeap());
        for (int x = 1; x < 60; x++){
          Serial.printf("0:%02d\n\r", 60-x);
          while (second(cur) < x){
            ESP.wdtFeed();
            cur = getTime();
            Serial.printf("%d, %d, %s",second(cur), x, ctime(&cur));
          }
          setSeconds(60-x);
          setPixels();
          pixels.show();
        }
    }


Does anyone here know intimately how the ets_timer functions and where they are executed?