Rtos API example

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 static void (*_rtc_init)(void) = rtc_init;
00027 static int (*_rtc_isenabled)(void) = rtc_isenabled;
00028 static time_t (*_rtc_read)(void) = rtc_read;
00029 static void (*_rtc_write)(time_t t) = rtc_write;
00030 #else
00031 static void (*_rtc_init)(void) = NULL;
00032 static int (*_rtc_isenabled)(void) = NULL;
00033 static time_t (*_rtc_read)(void) = NULL;
00034 static void (*_rtc_write)(time_t t) = NULL;
00035 #endif
00036 
00037 #ifdef __cplusplus
00038 extern "C" {
00039 #endif
00040 #if defined (__ICCARM__)
00041 time_t __time32(time_t *timer)
00042 #else
00043 time_t time(time_t *timer)
00044 #endif
00045 
00046 {
00047     _mutex->lock();
00048     if (_rtc_isenabled != NULL) {
00049         if (!(_rtc_isenabled())) {
00050             set_time(0);
00051         }
00052     }
00053     
00054     time_t t = (time_t)-1;
00055     if (_rtc_read != NULL) {
00056         t = _rtc_read();
00057     }
00058 
00059     if (timer != NULL) {
00060         *timer = t;
00061     }
00062     _mutex->unlock();
00063     return t;
00064 }
00065 
00066 void set_time(time_t t) {
00067     _mutex->lock();
00068     if (_rtc_init != NULL) {
00069         _rtc_init();
00070     }
00071     if (_rtc_write != NULL) {
00072         _rtc_write(t);
00073     }
00074     _mutex->unlock();
00075 }
00076 
00077 void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void)) {
00078     _mutex->lock();
00079     _rtc_read = read_rtc;
00080     _rtc_write = write_rtc;
00081     _rtc_init = init_rtc;
00082     _rtc_isenabled = isenabled_rtc;
00083     _mutex->unlock();
00084 }
00085 
00086 
00087 
00088 #ifdef __cplusplus
00089 }
00090 #endif