Czy wolisz polską wersję strony elektroda?
Nie, dziękuję Przekieruj mnie taminsmod 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.
insmod wrote:I think i'll eventually write a separate HAL for interrupts.
#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;
}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] ";
}insmod wrote:For beken, did you try change edge directly in interrupt? E.g. gpio_int_enable to opposite edge.
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?
TL;DR: With 1–2 seconds/day drift after switching to RTOS-tick-based uptime, the thread’s core advice is: "split clock from NTP" and keep a local OpenBeken clock from startup time plus corrected elapsed seconds. This helps OpenBeken users who need offline energy stats, local scheduling, or RTC integration without internet or NTP. [#21031093]
Why it matters: A separable device clock lets OpenBeken keep usable time in AP mode, offline LANs, and future RTC-based builds, while reducing dependence on always-on internet time.
| Approach | Internet required | Reported accuracy in thread | Best fit | Main limitation |
|---|---|---|---|---|
| Local clock from startup + corrected uptime | No | about 1–2 s/day | daily energy totals, basic schedules | loses time after power loss |
| NTP-based clock | Yes, at least for sync | network-synced | precise schedules, sunrise/sunset | needs reachable time source |
| DS3231 RTC | No | about 1 minute/year | remote installs, outages, offline switching | needs extra hardware and space |
Key insight: The breakthrough was not “manual time entry” alone. It was replacing naive
g_secondsElapsed++timing with RTOS-tick-derived uptime, then separating generic clock functions from the NTP protocol layer. [#21031093]
#define ENABLE_DRIVER_DS3231 1. [#21865147]g_secondsElapsed; the improved version recalculated elapsed time from RTOS ticks every 10 seconds. That lets the clock run in AP mode and without any network. It is accurate enough for daily energy totals, even when exact wall-clock precision is unnecessary. [#21031093]g_secondsElapsed drifts because a simple one-second increment is not equally accurate on all SDKs and platforms. The fix is to derive uptime from RTOS ticks, including platform-specific tick-to-millisecond factors such as portTICK_RATE_MS or a ratio of 2 on some Beken and WinnerMicro targets. That change fixed slow clocks on BK7231N, BK7231T, W600, and W800, and brought reported drift down to about 1–2 seconds per day. [#21046836]g_secondsElapsed is OpenBeken’s uptime counter, used for status displays, JSON output, driver timing, and offline clock math. "g_secondsElapsed is an uptime counter that tracks seconds since boot, a core timing value reused for status pages, sensor intervals, and local timekeeping when combined with a saved start time." The local-clock approach turns uptime into wall time by adding a stored startup epoch. [#21036229]drv_ntp.c into a separate clock layer, then leave only protocol-specific sync logic in NTP. In the thread, this meant adding files such as drv_deviceclock.c and renaming event logic toward drv_clock_events.c, while keeping feature flags optional in obk_config.h. That design also makes sunrise, DST, manual clock setting, and future RTC sources usable without forcing the NTP driver to own all time functions. [#21031093]ENABLE_LOCAL_CLOCK 1; optionally set ENABLE_LOCAL_CLOCK_ADVANCED 1 for DST support. 2. Rebuild and flash, because the feature was submitted disabled by default. 3. Confirm that the config page shows a browser-time clock option and that status output reports time. The thread used ENABLE_LOCAL_CLOCK for basic clocking and ENABLE_LOCAL_CLOCK_ADVANCED for DST, with the advanced flag requiring the basic one. [#21032907]2; on W600 and W800, portTICK_RATE_MS also equaled 2. After changing the uptime calculation to multiply xTaskGetTickCount() by the correct tick period, the slow-clock behavior was reported fixed across those platforms. [#21039505]STATUS 8, or send HTTP requests to read or set time remotely. The thread showed a shell example using wget against /cm?cmnd=STATUS%208, returning JSON with "StatusSNS":{"Time":"2024-04-07T15:05:09"}. That makes drift testing easy: poll the device at intervals and compare its reported time with a known-good router or Linux host clock. [#21036320]DST.bin was also proposed as a scalable middle ground for per-zone data without hardcoding every rule into firmware. [#21036097]DST.bin and read it as a float array or fetch values as needed. That would let users upload zone-specific DST data separately, instead of recompiling for each timezone. [#21036097]startdriver DS3231 <CLK-Pin> <DATA-Pin> <optional sync>. 2. Use sync 1 to set device time from RTC at startup or 2 to refresh device time regularly. 3. Set RTC time with DS3231_SetEpoch <epoch> or a Linux request such as wget "http://<ip>/cmd_tool?cmd=DS3231_SetEpoch $(date +%s)". The thread also listed DS3231_GetTime and DS3231_GetEpoch. [#21633562]