pankore | 78f204a | 2022-01-25 22:21:09 +0800 | [diff] [blame] | 1 | |
pankore | 544da73c | 2021-10-19 02:51:16 +0800 | [diff] [blame] | 2 | /* |
| 3 | * |
| 4 | * Copyright (c) 2020 Project CHIP Authors |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | |
| 20 | /** |
| 21 | * @file |
| 22 | * Provides implementations of the CHIP System Layer platform |
| 23 | * time/clock functions that are suitable for use on the Ameba platform. |
| 24 | */ |
| 25 | /* this file behaves like a config.h, comes first */ |
| 26 | #include <platform/internal/CHIPDeviceLayerInternal.h> |
| 27 | |
pankore | b79b781 | 2022-01-24 21:03:12 +0800 | [diff] [blame] | 28 | #include <platform/Ameba/SystemTimeSupport.h> |
pankore | 544da73c | 2021-10-19 02:51:16 +0800 | [diff] [blame] | 29 | #include <support/logging/CHIPLogging.h> |
| 30 | |
| 31 | #include "task.h" |
pankore | 395e404 | 2023-06-15 20:59:23 +0800 | [diff] [blame] | 32 | #include <chip_porting.h> |
pankore | 544da73c | 2021-10-19 02:51:16 +0800 | [diff] [blame] | 33 | #include <time.h> |
| 34 | |
pankore | 544da73c | 2021-10-19 02:51:16 +0800 | [diff] [blame] | 35 | struct rtkTimeVal |
| 36 | { |
| 37 | uint32_t tv_sec; /* seconds */ |
| 38 | uint32_t tv_usec; /* microseconds */ |
| 39 | }; |
| 40 | |
| 41 | namespace chip { |
| 42 | namespace System { |
pankore | b79b781 | 2022-01-24 21:03:12 +0800 | [diff] [blame] | 43 | namespace Clock { |
pankore | 544da73c | 2021-10-19 02:51:16 +0800 | [diff] [blame] | 44 | |
pankore | b79b781 | 2022-01-24 21:03:12 +0800 | [diff] [blame] | 45 | namespace Internal { |
| 46 | ClockImpl gClockImpl; |
| 47 | } // namespace Internal |
| 48 | |
| 49 | Microseconds64 ClockImpl::GetMonotonicMicroseconds64(void) |
pankore | 544da73c | 2021-10-19 02:51:16 +0800 | [diff] [blame] | 50 | { |
pankore | b79b781 | 2022-01-24 21:03:12 +0800 | [diff] [blame] | 51 | return (Clock::Microseconds64(xTaskGetTickCount()) * configTICK_RATE_HZ); |
pankore | 544da73c | 2021-10-19 02:51:16 +0800 | [diff] [blame] | 52 | } |
| 53 | |
pankore | b79b781 | 2022-01-24 21:03:12 +0800 | [diff] [blame] | 54 | Milliseconds64 ClockImpl::GetMonotonicMilliseconds64(void) |
pankore | 544da73c | 2021-10-19 02:51:16 +0800 | [diff] [blame] | 55 | { |
pankore | b79b781 | 2022-01-24 21:03:12 +0800 | [diff] [blame] | 56 | return (Clock::Milliseconds64(xTaskGetTickCount())); |
pankore | 544da73c | 2021-10-19 02:51:16 +0800 | [diff] [blame] | 57 | } |
| 58 | |
pankore | b79b781 | 2022-01-24 21:03:12 +0800 | [diff] [blame] | 59 | CHIP_ERROR ClockImpl::GetClock_RealTime(Clock::Microseconds64 & curTime) |
pankore | 544da73c | 2021-10-19 02:51:16 +0800 | [diff] [blame] | 60 | { |
Michael Sandstedt | 530e325 | 2022-06-10 15:17:26 -0500 | [diff] [blame] | 61 | // TODO(19081): This platform does not properly error out if wall clock has |
| 62 | // not been set. For now, short circuit this. |
| 63 | return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; |
| 64 | #if 0 |
pankore | 544da73c | 2021-10-19 02:51:16 +0800 | [diff] [blame] | 65 | time_t seconds; |
| 66 | struct rtkTimeVal tv; |
| 67 | |
pankore | 395e404 | 2023-06-15 20:59:23 +0800 | [diff] [blame] | 68 | seconds = matter_rtc_read(); |
pankore | 544da73c | 2021-10-19 02:51:16 +0800 | [diff] [blame] | 69 | |
| 70 | tv.tv_sec = (uint32_t) seconds; |
| 71 | tv.tv_usec = 0; |
| 72 | |
| 73 | if (tv.tv_sec < CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD) |
| 74 | { |
| 75 | return CHIP_ERROR_REAL_TIME_NOT_SYNCED; |
| 76 | } |
pankore | b79b781 | 2022-01-24 21:03:12 +0800 | [diff] [blame] | 77 | static_assert(CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD >= 0, "We might be letting through negative tv_sec values!"); |
Arkadiusz Bokowy | 410face | 2022-05-31 20:02:00 +0200 | [diff] [blame] | 78 | curTime = Microseconds64((static_cast<uint64_t>(tv.tv_sec) * UINT64_C(1000000)) + static_cast<uint64_t>(tv.tv_usec)); |
pankore | 544da73c | 2021-10-19 02:51:16 +0800 | [diff] [blame] | 79 | |
| 80 | return CHIP_NO_ERROR; |
Michael Sandstedt | 530e325 | 2022-06-10 15:17:26 -0500 | [diff] [blame] | 81 | #endif |
pankore | 544da73c | 2021-10-19 02:51:16 +0800 | [diff] [blame] | 82 | } |
| 83 | |
Arkadiusz Bokowy | 410face | 2022-05-31 20:02:00 +0200 | [diff] [blame] | 84 | CHIP_ERROR ClockImpl::GetClock_RealTimeMS(Milliseconds64 & aCurTime) |
pankore | 544da73c | 2021-10-19 02:51:16 +0800 | [diff] [blame] | 85 | { |
Arkadiusz Bokowy | 410face | 2022-05-31 20:02:00 +0200 | [diff] [blame] | 86 | Microseconds64 curTimeUs; |
| 87 | auto err = GetClock_RealTime(curTimeUs); |
| 88 | aCurTime = std::chrono::duration_cast<Milliseconds64>(curTimeUs); |
| 89 | return err; |
pankore | 544da73c | 2021-10-19 02:51:16 +0800 | [diff] [blame] | 90 | } |
| 91 | |
Arkadiusz Bokowy | 410face | 2022-05-31 20:02:00 +0200 | [diff] [blame] | 92 | CHIP_ERROR ClockImpl::SetClock_RealTime(Microseconds64 aNewCurTime) |
pankore | 544da73c | 2021-10-19 02:51:16 +0800 | [diff] [blame] | 93 | { |
pankore | b79b781 | 2022-01-24 21:03:12 +0800 | [diff] [blame] | 94 | struct rtkTimeVal tv; |
| 95 | tv.tv_sec = static_cast<uint32_t>(aNewCurTime.count() / UINT64_C(1000000)); |
| 96 | tv.tv_usec = static_cast<uint32_t>(aNewCurTime.count() % UINT64_C(1000000)); |
pankore | 395e404 | 2023-06-15 20:59:23 +0800 | [diff] [blame] | 97 | matter_rtc_init(); |
| 98 | matter_rtc_write(tv.tv_sec); |
pankore | 544da73c | 2021-10-19 02:51:16 +0800 | [diff] [blame] | 99 | |
| 100 | return CHIP_NO_ERROR; |
| 101 | } |
| 102 | |
pankore | b79b781 | 2022-01-24 21:03:12 +0800 | [diff] [blame] | 103 | CHIP_ERROR InitClock_RealTime() |
| 104 | { |
| 105 | Clock::Microseconds64 curTime = |
| 106 | Clock::Microseconds64((static_cast<uint64_t>(CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD) * UINT64_C(1000000))); |
| 107 | // Use CHIP_SYSTEM_CONFIG_VALID_REAL_TIME_THRESHOLD as the initial value of RealTime. |
| 108 | // Then the RealTime obtained from GetClock_RealTime will be always valid. |
Michael Sandstedt | 530e325 | 2022-06-10 15:17:26 -0500 | [diff] [blame] | 109 | // |
| 110 | // TODO(19081): This is broken because it causes the platform to report |
| 111 | // that it does have wall clock time when it actually doesn't. |
pankore | b79b781 | 2022-01-24 21:03:12 +0800 | [diff] [blame] | 112 | return System::SystemClock().SetClock_RealTime(curTime); |
| 113 | } |
| 114 | |
| 115 | } // namespace Clock |
pankore | 544da73c | 2021-10-19 02:51:16 +0800 | [diff] [blame] | 116 | } // namespace System |
| 117 | } // namespace chip |