From 567957643066d1705f786f2cd78f08fafc429020 Mon Sep 17 00:00:00 2001 From: xshuqun <77948861+xshuqun@users.noreply.github.com> Date: Thu, 25 Apr 2024 09:51:07 +0800 Subject: [PATCH] Update system time (#192) * [sdk] use gtimer for system time * Use gtimer instead of freertos time as freertos only covers until 32-bits, but matter requires 64-bits * [sdk] fix build issue with af.h * Fix build issue with af.h as header has been removed and declaration is moved to attribute-table.h * [sdk] change timer used for system time * Change TIMER4 to TIMER5 as TIMER4 is already used in rtc_init(). To prevent conflict, change to TIMER5. --- .../matter/common/port/matter_timers.c | 30 +++++++++++++++++++ .../matter/common/port/matter_timers.h | 2 ++ .../application/matter/core/matter_core.cpp | 1 + .../application/matter/core/matter_events.h | 1 + .../matter/driver/dishwasher_mode.h | 2 +- .../application/matter/driver/fan_driver.h | 2 +- .../application/matter/driver/tcc_mode.h | 2 +- .../matter/example/chiptest/example_matter.c | 2 +- 8 files changed, 38 insertions(+), 4 deletions(-) diff --git a/component/common/application/matter/common/port/matter_timers.c b/component/common/application/matter/common/port/matter_timers.c index 075432d4..3b938770 100644 --- a/component/common/application/matter/common/port/matter_timers.c +++ b/component/common/application/matter/common/port/matter_timers.c @@ -13,15 +13,25 @@ #include "errno.h" #include "FreeRTOS.h" #include "chip_porting.h" +#include "time.h" #include "rtc_api.h" +#include "timer_api.h" +#include "task.h" #define MICROSECONDS_PER_SECOND ( 1000000LL ) /**< Microseconds per second. */ #define NANOSECONDS_PER_SECOND ( 1000000000LL ) /**< Nanoseconds per second. */ #define NANOSECONDS_PER_TICK ( NANOSECONDS_PER_SECOND / configTICK_RATE_HZ ) /**< Nanoseconds per FreeRTOS tick. */ +#define US_OVERFLOW_MAX (0xFFFFFFFF) +#define MATTER_SW_RTC_TIMER_ID TIMER5 + extern int FreeRTOS_errno; #define errno FreeRTOS_errno +static gtimer_t matter_rtc_timer; +static uint64_t current_us = 0; +static volatile uint32_t rtc_counter = 0; + BOOL UTILS_ValidateTimespec( const struct timespec * const pxTimespec ) { BOOL xReturn = FALSE; @@ -137,6 +147,26 @@ void matter_rtc_write(long long time) rtc_write(time); } +uint64_t ameba_get_clock_time(void) +{ + uint64_t global_us = 0; + current_us = gtimer_read_us(&matter_rtc_timer); + global_us = ((uint64_t)rtc_counter * US_OVERFLOW_MAX) + (current_us); + return global_us; +} + +static void matter_timer_rtc_callback(void) +{ + rtc_counter++; +} + +void matter_timer_init(void) +{ + gtimer_init(&matter_rtc_timer, MATTER_SW_RTC_TIMER_ID); + hal_timer_set_cntmode(&matter_rtc_timer.timer_adp, 0); //use count up + gtimer_start_periodical(&matter_rtc_timer, US_OVERFLOW_MAX, (void *)matter_timer_rtc_callback, (uint32_t) &matter_rtc_timer); +} + #ifdef __cplusplus } #endif diff --git a/component/common/application/matter/common/port/matter_timers.h b/component/common/application/matter/common/port/matter_timers.h index 6bf24c36..591b7564 100644 --- a/component/common/application/matter/common/port/matter_timers.h +++ b/component/common/application/matter/common/port/matter_timers.h @@ -19,6 +19,8 @@ time_t _time( time_t * tloc ); void matter_rtc_init(void); long long matter_rtc_read(void); void matter_rtc_write(long long time); +uint64_t ameba_get_clock_time(void); +void matter_timer_init(void); #ifdef __cplusplus } diff --git a/component/common/application/matter/core/matter_core.cpp b/component/common/application/matter/core/matter_core.cpp index e62d8d5b..d3930680 100644 --- a/component/common/application/matter/core/matter_core.cpp +++ b/component/common/application/matter/core/matter_core.cpp @@ -201,6 +201,7 @@ CHIP_ERROR matter_core_init() CHIP_ERROR matter_core_start() { + matter_timer_init(); return matter_core_init(); // matter_core_init_server(); } diff --git a/component/common/application/matter/core/matter_events.h b/component/common/application/matter/core/matter_events.h index 91b1ef7a..e0773e83 100644 --- a/component/common/application/matter/core/matter_events.h +++ b/component/common/application/matter/core/matter_events.h @@ -1,6 +1,7 @@ #pragma once #include +#include struct AppEvent; typedef void (*EventHandler)(AppEvent *); diff --git a/component/common/application/matter/driver/dishwasher_mode.h b/component/common/application/matter/driver/dishwasher_mode.h index bd4eda05..dec8b600 100644 --- a/component/common/application/matter/driver/dishwasher_mode.h +++ b/component/common/application/matter/driver/dishwasher_mode.h @@ -19,7 +19,7 @@ #pragma once #include -#include +#include #include #include #include diff --git a/component/common/application/matter/driver/fan_driver.h b/component/common/application/matter/driver/fan_driver.h index 7db04bb4..b0becc53 100644 --- a/component/common/application/matter/driver/fan_driver.h +++ b/component/common/application/matter/driver/fan_driver.h @@ -2,7 +2,7 @@ #include #include "pwmout_api.h" -#include +#include class MatterFan { diff --git a/component/common/application/matter/driver/tcc_mode.h b/component/common/application/matter/driver/tcc_mode.h index 0e545b41..353bf991 100644 --- a/component/common/application/matter/driver/tcc_mode.h +++ b/component/common/application/matter/driver/tcc_mode.h @@ -19,7 +19,7 @@ #pragma once #include -#include +#include #include #include #include diff --git a/component/common/application/matter/example/chiptest/example_matter.c b/component/common/application/matter/example/chiptest/example_matter.c index 673ca17f..8642cf2b 100644 --- a/component/common/application/matter/example/chiptest/example_matter.c +++ b/component/common/application/matter/example/chiptest/example_matter.c @@ -17,7 +17,7 @@ static void example_matter_task_thread(void *pvParameters) while(!(wifi_is_up(RTW_STA_INTERFACE) || wifi_is_up(RTW_AP_INTERFACE))) { //waiting for Wifi to be initialized } - + matter_timer_init(); ChipTest(); vTaskDelete(NULL);