Skip to content

Commit

Permalink
Update system time (#192)
Browse files Browse the repository at this point in the history
* [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.
  • Loading branch information
xshuqun authored Apr 25, 2024
1 parent a4089f3 commit 5679576
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 4 deletions.
30 changes: 30 additions & 0 deletions component/common/application/matter/common/port/matter_timers.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions component/common/application/matter/core/matter_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
1 change: 1 addition & 0 deletions component/common/application/matter/core/matter_events.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <platform/CHIPDeviceLayer.h>
#include <app/ConcreteAttributePath.h>

struct AppEvent;
typedef void (*EventHandler)(AppEvent *);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#pragma once

#include <app/clusters/mode-base-server/mode-base-server.h>
#include <app/util/af.h>
#include <app/util/attribute-table.h>
#include <app/util/config.h>
#include <cstring>
#include <utility>
Expand Down
2 changes: 1 addition & 1 deletion component/common/application/matter/driver/fan_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <platform_stdlib.h>
#include "pwmout_api.h"
#include <app/util/af.h>
#include <app/util/attribute-table.h>

class MatterFan
{
Expand Down
2 changes: 1 addition & 1 deletion component/common/application/matter/driver/tcc_mode.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#pragma once

#include <app/clusters/mode-base-server/mode-base-server.h>
#include <app/util/af.h>
#include <app/util/attribute-table.h>
#include <app/util/config.h>
#include <cstring>
#include <utility>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 5679576

Please sign in to comment.