logo elektroda
logo elektroda
X
logo elektroda

How to Implement a Clock in OpenBeken Devices Without Using NTP Protocol

max4elektroda 4134 95
ADVERTISEMENT
  • #61 21634968
    p.kaczmarek2
    Moderator Smart Home
    @insmod what do you think? Should we merge it, and if so, at which stage?
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #62 21634973
    insmod
    Level 26  
    >>21634968
    Which one? If #1729, then it lgtm, and i think that #1727 can be merged too.
    DS3231 and driver start failure probably better it they are in separate prs.
  • #63 21634977
    p.kaczmarek2
    Moderator Smart Home
    I'm a bit worried about flash size, if we keep merging features that are used rarely we will run into more issues like OTA ovewriting LFS soon. Still, maybe we can indeed merge clock stuff now and try to make up for it, we still have long strings in the code, even the flag descriptions, they are super long. Maybe we should edit flags page to fetch flash descriptions from Github.
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #64 21634978
    insmod
    Level 26  
    Well, it was reported that bk ota binaries are about 6kb lighter.

    Added after 1 [hours] 15 [minutes]:

    tr6260 size increase is probably because ntp is required(?), and so it wasn't disabled.
    There is a wpa_get_ntp_timestamp function and a separate sntp_tr component.
  • #65 21635354
    max4elektroda
    Level 20  
    Let's agree on some final tests, I'm busy this weekend but can spare some time next week.
    One thing I just saw: I changed the call to set DST, I'll have to change it back to the actual command or add an alias for actual call.
    I can also, like it's done for tasmota, compile a list with all possible calls per timezone for our approach (like this one https://tasmota.github.io/docs/Timezone-Table/ )
  • #66 21638397
    max4elektroda
    Level 20  
    Changed to allow compatibility CMD.
    Now, what would we need to test?
    I would propose

    Start NTP
    set TZ
    Set DST
    Test DST changes
    Set latitude and longitude and check sunrise and sunset

    Anything else?

    Did a first try for DCF77 decoder, tested working on W800 and ESP32.
    Problem is it's using a very slow code one bit per second.
    Actually I use interrupts, but it needs both edges to detect length of the "bit", so e.g. Beken is out.
    Can I start an own task testing level of an GPIO pin e.g every 10ms as a driver?
    The requirements are not particularly strict; it is sufficient to distinguish 100ms and 200ms pulses and almost 2 seconds without pulse.
  • #67 21638403
    insmod
    Level 26  
    >>21638397
    For beken, did you try change edge directly in interrupt? E.g. gpio_int_enable to opposite edge.
    I think i'll eventually write a separate HAL for interrupts.
  • #68 21638410
    max4elektroda
    Level 20  
    insmod wrote:
    >>21638397
    For beken, did you try change edge directly in interrupt? E.g. gpio_int_enable to opposite edge.
    I think i'll eventually write a separate HAL for interrupts.

    No, but I'll try that. Thanks!
  • #69 21638411
    p.kaczmarek2
    Moderator Smart Home
    Give it a try. We also have gpio_int_enable source, btw:
    https://github.com/openshwprojects/OpenBK7231...t/bk7231t_os/beken378/driver/gpio/gpio.c#L536
    Maybe some nested calls could be omitted for speed?
    Code: C / C++
    Log in, to see the code
    Helpful post? Buy me a coffee.
  • #70 21638498
    max4elektroda
    Level 20  
    insmod wrote:
    I think i'll eventually write a separate HAL for interrupts.

    That would really be a big step to simplify some code. Thinking of BL0937 driver and it's interrupt code actually needing 1:1 code and ifdefs ;-)
  • #71 21638663
    p.kaczmarek2
    Moderator Smart Home
    I've did basic OTA separation and I could try interrupt HAL cleanup as well, but I am a bit limited by testing. I can try making PR and leave it for @divadiow to test. Altough.... if I do it, maybe I can do also "Counter" pin role and try to check it myself, even with a capacitor and a button (capacitor for debouncing, otherwise I would get multiple edges...)
    Helpful post? Buy me a coffee.
  • #72 21638698
    divadiow
    Level 35  
    p.kaczmarek2 wrote:
    I can try making PR and leave it for @divadiow to test

    can do. just need to know what a test actually is for this. I don't know much (anything?) about IRQs and edges
  • #73 21638802
    p.kaczmarek2
    Moderator Smart Home
    I've started playing around interrupts.
    It seems that on BL602, we need to pass pin index to the interrupt to clear the flag - it can be done via user data.
    First step:
    https://github.com/openshwprojects/OpenBK7231T_App/pull/1768
    I assume that we would do a wrapper call via function pointer in HAL...

    Added after 3 [minutes]:

    Same for W600
    How to Implement a Clock in OpenBeken Devices Without Using NTP Protocol

    Added after 33 [seconds]:

    Those platforms seem easier - no flag clear needed:
    How to Implement a Clock in OpenBeken Devices Without Using NTP Protocol

    Added after 1 [minutes]:

    It seems that only PLATFORM_LN882H is tricky

    Added after 39 [minutes]:

    Update: currently not sure about gpio_install_isr_service, need to read up on it, seems like it will be called globally once?

    Added after 1 [hours] 28 [minutes]:

    Not sure about Realtek - how to access last argument (NULL), user data?
    Code: C / C++
    Log in, to see the code

    hmm event is an enumeration:
    Code: C / C++
    Log in, to see the code


    Added after 46 [seconds]:

    ah last argument is ID:
    Code: C / C++
    Log in, to see the code

    so it shall be passed as first one to interrupt

    Added after 10 [hours] 28 [minutes]:

    Almost ready, I will ask for feedback tomorrow

    Added after 6 [hours] 43 [minutes]:

    I didn't split it into multiple files (I will move it to HAL pins?) but it should be ready for testing:
    https://github.com/openshwprojects/OpenBK7231T_App/pull/1768
    @insmod can you check? @divadiow do you have any devices with BL0937?
    The most uncertain part is passing GPIO index to interrupt in HAL so it can call external handler.
    Helpful post? Buy me a coffee.
  • #74 21639409
    divadiow
    Level 35  
    p.kaczmarek2 wrote:
    @divadiow do you have any devices with BL0937

    not all platforms but yes to at least LN882H, BK7231N, BK7231T, ESP8266, RTL8710B, BL602
  • ADVERTISEMENT
  • #75 21639411
    p.kaczmarek2
    Moderator Smart Home
    So the most simple check would be to see if BL0937 works there on main release and then on my PR, each separately
    Helpful post? Buy me a coffee.
  • #76 21639787
    max4elektroda
    Level 20  
    I can confirm LN882H working:

    Screenshot showing code or a technical forum post discussing timekeeping issues on the W800 device. Screenshot of a forum post discussing W800 microcontroller clock inaccuracy with g_secondsElapsed function.

    Added after 1 [hours]:

    A question regarding the "original" topic ;-)

    If we want to use a generic clock without a reliable source to constantly set the clock (like ntp) we suffer from the fact that "g_secondsElapsed" might be quite unreliable.
    Especially my W800 is a bad example: g_secondsElapsed will be off for ~ 5 seconds after only one minute! So after 12 hours our clock is about 1 hour off.

    We can easily fix that by using rtos tics to get a quite reliable uptime.

    My question: Shall I use this exact uptime (only) for the clock or should we "set" g_secondsElapsed to the correct value regularly?

    In the case of W800 to avoid permanent "jumps" in time we would need to do that quite often, every 5 seconds or so ...
  • #77 21639876
    p.kaczmarek2
    Moderator Smart Home
    You can't move g_secondsElapsed back at runtime, it could break drivers logic. The best you can do is try rewriting g_secondsElapsed core mechanism to be more accurate, maybe with rtos ticks.
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #78 21639884
    max4elektroda
    Level 20  
    The code is really simple (might need to check on recently added platforms):

    
    #if PLATFORM_W600 || PLATFORM_W800
    #define TimeOut_t xTimeOutType 
    #endif
    uint32_t getUptime(){
       TimeOut_t myTimeout;
       /*
       // we use vTaskSetTimeOutState to get the number of xTicks (we could get them with xTaskGetTickCount(), too)
       // but the number of overflows ("xNumOfOverflows") is private and hence not accessable else ...
       void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
       {
          configASSERT( pxTimeOut );
          pxTimeOut->xOverflowCount = xNumOfOverflows;
          pxTimeOut->xTimeOnEntering = xTickCount;
       }
       */
       vTaskSetTimeOutState( &myTimeout );
       // for most platforms, ticks are ms, but we can't be sure (e.g. its not on W600/W800) so we use factor portTICK_RATE_MS to make sure we get ms
       uint32_t my_secondsElapsed = (uint32_t)((((uint64_t) myTimeout.xOverflowCount << (sizeof(portTickType)*8) | myTimeout.xTimeOnEntering)*portTICK_RATE_MS ) / 1000 );
    // this would set g_secondsElapsed
    //   g_secondsElapsed=my_secondsElapsed;
       ADDLOG_DEBUG(LOG_FEATURE_RAW, "g_secondsElapsed=%i   / my_secondsElapsed=%i (differs: %i)\r\n", g_secondsElapsed, my_secondsElapsed, (g_secondsElapsed - my_secondsElapsed));
       return my_secondsElapsed;
    }


    Added after 2 [minutes]:

    I'll try using this instead of g_secondsElapsed++

    Added after 30 [minutes]:

    Using this for now - working on W800, will test with others soon ...

    diff --git a/src/user_main.c b/src/user_main.c
    index a0ea6322..dbee5483 100644
    --- a/src/user_main.c
    +++ b/src/user_main.c
    @@ -583,6 +583,11 @@ float g_wifi_temperature = 0;
     static byte g_secondsSpentInLowMemoryWarning = 0;
     void Main_OnEverySecond()
     {
    +#if PLATFORM_W600 || PLATFORM_W800
    +#define TimeOut_t xTimeOutType 
    +#endif
    +       TimeOut_t myTimeout;    // to get uptime from xTicks 
    +
            int newMQTTState;
            const char* safe;
            int i;
    @@ -754,8 +759,9 @@ void Main_OnEverySecond()
                            }
                    }
            }
    -
    -       g_secondsElapsed++;
    +//     g_secondsElapsed++;
    +       vTaskSetTimeOutState( &myTimeout );
    +       g_secondsElapsed = (int)((((uint64_t) myTimeout.xOverflowCount << (sizeof(portTickType)*8) | myTimeout.xTimeOnEntering)*portTICK_RATE_MS ) / 1000 );
            if (bSafeMode) {
                    safe = "[SAFE] ";
            }


    Added after 2 [minutes]:

    insmod wrote:
    For beken, did you try change edge directly in interrupt? E.g. gpio_int_enable to opposite edge.

    Not tested for Beken yet, but its working om BL602, thanks @insmod !
  • #79 21639910
    p.kaczmarek2
    Moderator Smart Home
    We will integrate this "on change" interrupt into HAL later, as soon as basics are tested
    Helpful post? Buy me a coffee.
  • #80 21639912
    max4elektroda
    Level 20  
    p.kaczmarek2 wrote:
    We will integrate this "on change" interrupt into HAL later, as soon as basics are tested

    That hal-approach will really make things much easier, thanks a lot!
  • #82 21639922
    p.kaczmarek2
    Moderator Smart Home
    The next step is "Counter" role from Tasmota. Should be very easy. Do anyone know how it works in TAS? Is it "on change", or "on rising", or "on falling"?
    I guess we need :
    - Counter_any
    - Counter_h
    - Counter_l ? or how should we name it?

    BTW: Little offtopic, but I also got ESP32-S3-ETH-8DI-8RO, I'm trying to put together a TCA9554 driver for it quickly
    https://github.com/openshwprojects/OpenBK7231T_App/pull/1770
    We need 8MB ESP partitions soon..
    Helpful post? Buy me a coffee.
  • #83 21639923
    insmod
    Level 26  
    >>21639922
    Why? 4MB is enough for everything.
  • #84 21639966
    max4elektroda
    Level 20  
    p.kaczmarek2 wrote:
    The next step is "Counter" role from Tasmota. Should be very easy. Do anyone know how it works in TAS? Is it "on change", or "on rising", or "on falling"?
    I guess we need :
    - Counter_any
    - Counter_h
    - Counter_l ? or how should we name it?

    Don't know TAS behavior, so I'm no help here.

    What would you count? States? Then _h or _l are fine.
    If you count edges, I would propose _r(aising) and _f(alling)
  • #85 21639968
    p.kaczmarek2
    Moderator Smart Home
    Does 4MB/8MB partitions are taken into account as LFS limit or is limit outside the partitions? On OpenESP32.
    Helpful post? Buy me a coffee.
  • #86 21639974
    insmod
    Level 26  
    >>21639968
    Limit is partition size. So 8MB with 4MB firmware - LFS partition is the same as if it was on 4MB device.
    But isn't 320KB enough?
  • #87 21640066
    divadiow
    Level 35  
    >>21639409

    animated images

    BK7231N 1.18.156
    How to Implement a Clock in OpenBeken Devices Without Using NTP Protocol

    BK7231N 1768_merge_cf30e28012eb ✅
    How to Implement a Clock in OpenBeken Devices Without Using NTP Protocol

    BK7231N_ALT 1.18.156
    How to Implement a Clock in OpenBeken Devices Without Using NTP Protocol

    BK7231N_ALT 1768_merge_cf30e28012eb ✅
    How to Implement a Clock in OpenBeken Devices Without Using NTP Protocol

    BK7231T 1.18.156
    How to Implement a Clock in OpenBeken Devices Without Using NTP Protocol

    BK7231T 1768_merge_cf30e28012eb ✅
    How to Implement a Clock in OpenBeken Devices Without Using NTP Protocol

    BK7231T_ALT 1.18.156
    How to Implement a Clock in OpenBeken Devices Without Using NTP Protocol

    BK7231T_ALT 1768_merge_cf30e28012eb ✅
    How to Implement a Clock in OpenBeken Devices Without Using NTP Protocol

    (new SDK still does its spikey/jumpy reading thing on any release)
  • #88 21640070
    insmod
    Level 26  
    >>21640066
    Set loglevel to 0 and spikes will 'magically' disappear.
  • #89 21640079
    divadiow
    Level 35  
    insmod wrote:
    Set loglevel to 0 and spikes will 'magically' disappear.

    ooh ok.

    damn. ESP8266 BL0937 device is 1mb.
  • #90 21640087
    p.kaczmarek2
    Moderator Smart Home
    I'm not sure about 8MB usage yet, so it's probably not a priority.

    Does HTTPS works on ESP build?

    I added counter_f:
    https://github.com/openshwprojects/OpenBK7231...mits/f99158f21f5ae95c2155209256e92ed3674d414d
    Still , one silly bug fix to come.

    I hope to be able to use counter_f along with button and capacitor (to debounce) to test interrupts
    Helpful post? Buy me a coffee.

Topic summary

The discussion revolves around implementing a local clock in OpenBeken devices without relying on the NTP protocol. The initial approach involved using a variable to track the time since startup, allowing for time calculations based on elapsed seconds. While this method lacks the accuracy provided by NTP, it offers advantages such as independence from network connectivity and reduced resource requirements. Participants provided feedback on code structure, suggested improvements for compatibility, and discussed the potential for using real-time clocks (RTC) for enhanced accuracy. Testing across various platforms, including LN882H, BL602, and W600, was conducted to evaluate the clock's performance and reliability. The conversation also touched on daylight saving time adjustments and the possibility of remote clock synchronization.
Summary generated by the language model.
ADVERTISEMENT