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