Knight KE / Mbed OS Game_Master
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 static SingletonPtr<mbed::LowPowerTimer> _rtc_lp_timer;
00037 static uint64_t _rtc_lp_base;
00038 static bool _rtc_enabled;
00039 
00040 static void _rtc_lpticker_init(void)
00041 {
00042     _rtc_lp_timer->start();
00043     _rtc_enabled = true;
00044 }
00045 
00046 static int _rtc_lpticker_isenabled(void)
00047 {
00048     return (_rtc_enabled == true);
00049 }
00050 
00051 static time_t _rtc_lpticker_read(void)
00052 {
00053     return (uint64_t)_rtc_lp_timer->read() + _rtc_lp_base;
00054 }
00055 
00056 static void _rtc_lpticker_write(time_t t)
00057 {
00058     _rtc_lp_base = t;
00059 }
00060 
00061 static void (*_rtc_init)(void) = _rtc_lpticker_init;
00062 static int (*_rtc_isenabled)(void) = _rtc_lpticker_isenabled;
00063 static time_t (*_rtc_read)(void) = _rtc_lpticker_read;
00064 static void (*_rtc_write)(time_t t) = _rtc_lpticker_write;
00065 
00066 #else /* DEVICE_LPTICKER */
00067 
00068 static void (*_rtc_init)(void) = NULL;
00069 static int (*_rtc_isenabled)(void) = NULL;
00070 static time_t (*_rtc_read)(void) = NULL;
00071 static void (*_rtc_write)(time_t t) = NULL;
00072 #endif /* DEVICE_LPTICKER */
00073 
00074 #ifdef __cplusplus
00075 extern "C" {
00076 #endif
00077 #if defined (__ICCARM__)
00078 time_t __time32(time_t *timer)
00079 #else
00080 time_t time(time_t *timer)
00081 #endif
00082 
00083 {
00084     _mutex->lock();
00085     if (_rtc_isenabled != NULL) {
00086         if (!(_rtc_isenabled())) {
00087             set_time(0);
00088         }
00089     }
00090     
00091     time_t t = (time_t)-1;
00092     if (_rtc_read != NULL) {
00093         t = _rtc_read();
00094     }
00095 
00096     if (timer != NULL) {
00097         *timer = t;
00098     }
00099     _mutex->unlock();
00100     return t;
00101 }
00102 
00103 void set_time(time_t t) {
00104     _mutex->lock();
00105     if (_rtc_init != NULL) {
00106         _rtc_init();
00107     }
00108     if (_rtc_write != NULL) {
00109         _rtc_write(t);
00110     }
00111     _mutex->unlock();
00112 }
00113 
00114 void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void)) {
00115     _mutex->lock();
00116     _rtc_read = read_rtc;
00117     _rtc_write = write_rtc;
00118     _rtc_init = init_rtc;
00119     _rtc_isenabled = isenabled_rtc;
00120     _mutex->unlock();
00121 }
00122 
00123 
00124 
00125 #ifdef __cplusplus
00126 }
00127 #endif