forked
Embed:
(wiki syntax)
Show/hide line numbers
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 <time.h> 00019 #include "platform/mbed_critical.h" 00020 #include "platform/mbed_rtc_time.h" 00021 #include "hal/us_ticker_api.h" 00022 #include "platform/SingletonPtr.h" 00023 #include "platform/PlatformMutex.h" 00024 00025 static SingletonPtr<PlatformMutex> _mutex; 00026 00027 #if DEVICE_RTC 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 #else 00033 static void (*_rtc_init)(void) = NULL; 00034 static int (*_rtc_isenabled)(void) = NULL; 00035 static time_t (*_rtc_read)(void) = NULL; 00036 static void (*_rtc_write)(time_t t) = NULL; 00037 #endif 00038 00039 #ifdef __cplusplus 00040 extern "C" { 00041 #endif 00042 #if defined (__ICCARM__) 00043 time_t __time32(time_t *timer) 00044 #else 00045 time_t time(time_t *timer) 00046 #endif 00047 00048 { 00049 _mutex->lock(); 00050 if (_rtc_isenabled != NULL) { 00051 if (!(_rtc_isenabled())) { 00052 set_time(0); 00053 } 00054 } 00055 00056 time_t t = (time_t)-1; 00057 if (_rtc_read != NULL) { 00058 t = _rtc_read(); 00059 } 00060 00061 if (timer != NULL) { 00062 *timer = t; 00063 } 00064 _mutex->unlock(); 00065 return t; 00066 } 00067 00068 void set_time(time_t t) { 00069 _mutex->lock(); 00070 if (_rtc_init != NULL) { 00071 _rtc_init(); 00072 } 00073 if (_rtc_write != NULL) { 00074 _rtc_write(t); 00075 } 00076 _mutex->unlock(); 00077 } 00078 00079 clock_t clock() { 00080 _mutex->lock(); 00081 clock_t t = us_ticker_read(); 00082 t /= 1000000 / CLOCKS_PER_SEC; // convert to processor time 00083 _mutex->unlock(); 00084 return t; 00085 } 00086 00087 void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void)) { 00088 _mutex->lock(); 00089 _rtc_read = read_rtc; 00090 _rtc_write = write_rtc; 00091 _rtc_init = init_rtc; 00092 _rtc_isenabled = isenabled_rtc; 00093 _mutex->unlock(); 00094 } 00095 00096 00097 00098 #ifdef __cplusplus 00099 } 00100 #endif
Generated on Tue Jul 12 2022 16:02:32 by 1.7.2