Kev Mann / mbed-dev-OS5_10_4
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_rtc_time.cpp Source File

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_timer->reset();
00061     _rtc_lp_base = t;
00062 }
00063 
00064 static void (*_rtc_init)(void) = _rtc_lpticker_init;
00065 static int (*_rtc_isenabled)(void) = _rtc_lpticker_isenabled;
00066 static time_t (*_rtc_read)(void) = _rtc_lpticker_read;
00067 static void (*_rtc_write)(time_t t) = _rtc_lpticker_write;
00068 
00069 #else /* DEVICE_LPTICKER */
00070 
00071 static void (*_rtc_init)(void) = NULL;
00072 static int (*_rtc_isenabled)(void) = NULL;
00073 static time_t (*_rtc_read)(void) = NULL;
00074 static void (*_rtc_write)(time_t t) = NULL;
00075 #endif /* DEVICE_LPTICKER */
00076 
00077 #ifdef __cplusplus
00078 extern "C" {
00079 #endif
00080 #if defined (__ICCARM__)
00081 time_t __time32(time_t *timer)
00082 #else
00083 time_t time(time_t *timer)
00084 #endif
00085 
00086 {
00087     _mutex->lock();
00088     if (_rtc_isenabled != NULL) {
00089         if (!(_rtc_isenabled())) {
00090             set_time(0);
00091         }
00092     }
00093 
00094     time_t t = (time_t) -1;
00095     if (_rtc_read != NULL) {
00096         t = _rtc_read();
00097     }
00098 
00099     if (timer != NULL) {
00100         *timer = t;
00101     }
00102     _mutex->unlock();
00103     return t;
00104 }
00105 
00106 void set_time(time_t t)
00107 {
00108     _mutex->lock();
00109     if (_rtc_init != NULL) {
00110         _rtc_init();
00111     }
00112     if (_rtc_write != NULL) {
00113         _rtc_write(t);
00114     }
00115     _mutex->unlock();
00116 }
00117 
00118 void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void))
00119 {
00120     _mutex->lock();
00121     _rtc_read = read_rtc;
00122     _rtc_write = write_rtc;
00123     _rtc_init = init_rtc;
00124     _rtc_isenabled = isenabled_rtc;
00125     _mutex->unlock();
00126 }
00127 
00128 
00129 
00130 #ifdef __cplusplus
00131 }
00132 #endif