Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
mbed_rtc_time.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #include "hal/rtc_api.h" 00017 00018 #include "platform/mbed_critical.h" 00019 #include "platform/mbed_rtc_time.h" 00020 #include "platform/SingletonPtr.h" 00021 #include "platform/PlatformMutex.h" 00022 00023 static SingletonPtr<PlatformMutex> _mutex; 00024 00025 #if DEVICE_RTC 00026 00027 static void (*_rtc_init)(void) = rtc_init; 00028 static int (*_rtc_isenabled)(void) = rtc_isenabled; 00029 static time_t (*_rtc_read)(void) = rtc_read; 00030 static void (*_rtc_write)(time_t t) = rtc_write; 00031 00032 #elif DEVICE_LPTICKER 00033 00034 #include "drivers/LowPowerTimer.h" 00035 00036 #define US_PER_SEC 1000000 00037 00038 static SingletonPtr<mbed::LowPowerTimer> _rtc_lp_timer; 00039 static uint64_t _rtc_lp_base; 00040 static bool _rtc_enabled; 00041 00042 static void _rtc_lpticker_init(void) 00043 { 00044 _rtc_lp_timer->start(); 00045 _rtc_enabled = true; 00046 } 00047 00048 static int _rtc_lpticker_isenabled(void) 00049 { 00050 return (_rtc_enabled == true); 00051 } 00052 00053 static time_t _rtc_lpticker_read(void) 00054 { 00055 return _rtc_lp_timer->read_high_resolution_us() / US_PER_SEC + _rtc_lp_base; 00056 } 00057 00058 static void _rtc_lpticker_write(time_t t) 00059 { 00060 _rtc_lp_base = t; 00061 } 00062 00063 static void (*_rtc_init)(void) = _rtc_lpticker_init; 00064 static int (*_rtc_isenabled)(void) = _rtc_lpticker_isenabled; 00065 static time_t (*_rtc_read)(void) = _rtc_lpticker_read; 00066 static void (*_rtc_write)(time_t t) = _rtc_lpticker_write; 00067 00068 #else /* DEVICE_LPTICKER */ 00069 00070 static void (*_rtc_init)(void) = NULL; 00071 static int (*_rtc_isenabled)(void) = NULL; 00072 static time_t (*_rtc_read)(void) = NULL; 00073 static void (*_rtc_write)(time_t t) = NULL; 00074 #endif /* DEVICE_LPTICKER */ 00075 00076 #ifdef __cplusplus 00077 extern "C" { 00078 #endif 00079 #if defined (__ICCARM__) 00080 time_t __time32(time_t *timer) 00081 #else 00082 time_t time(time_t *timer) 00083 #endif 00084 00085 { 00086 _mutex->lock(); 00087 if (_rtc_isenabled != NULL) { 00088 if (!(_rtc_isenabled())) { 00089 set_time(0); 00090 } 00091 } 00092 00093 time_t t = (time_t) -1; 00094 if (_rtc_read != NULL) { 00095 t = _rtc_read(); 00096 } 00097 00098 if (timer != NULL) { 00099 *timer = t; 00100 } 00101 _mutex->unlock(); 00102 return t; 00103 } 00104 00105 void set_time(time_t t) 00106 { 00107 _mutex->lock(); 00108 if (_rtc_init != NULL) { 00109 _rtc_init(); 00110 } 00111 if (_rtc_write != NULL) { 00112 _rtc_write(t); 00113 } 00114 _mutex->unlock(); 00115 } 00116 00117 void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void)) 00118 { 00119 _mutex->lock(); 00120 _rtc_read = read_rtc; 00121 _rtc_write = write_rtc; 00122 _rtc_init = init_rtc; 00123 _rtc_isenabled = isenabled_rtc; 00124 _mutex->unlock(); 00125 } 00126 00127 00128 00129 #ifdef __cplusplus 00130 } 00131 #endif
Generated on Tue Aug 9 2022 00:37:15 by
