Supawat Yangdiew / rtc_api_11U6x
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers rtc_time_11U6x.cpp Source File

rtc_time_11U6x.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 "rtc_api_11U6x.h"
00018 #include "rtc_time_11U6x.h"
00019 
00020 #include "platform/mbed_critical.h"
00021 #include "platform/SingletonPtr.h"
00022 #include "platform/PlatformMutex.h"
00023 
00024 static SingletonPtr<PlatformMutex> _mutex;
00025 
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 
00031 #ifdef __cplusplus
00032 extern "C" {
00033 #endif
00034 
00035 time_t time(time_t *timer)
00036 {
00037     _mutex->lock();
00038     if (_rtc_isenabled != NULL) {
00039         if (!(_rtc_isenabled())) {
00040             set_time(0);
00041         }
00042     }
00043 
00044     time_t t = (time_t) -1;
00045     if (_rtc_read != NULL) {
00046         t = _rtc_read();
00047     }
00048 
00049     if (timer != NULL) {
00050         *timer = t;
00051     }
00052     _mutex->unlock();
00053     return t;
00054 }
00055 
00056 void set_time(time_t t)
00057 {
00058     _mutex->lock();
00059     if (_rtc_init != NULL) {
00060         _rtc_init();
00061     }
00062     if (_rtc_write != NULL) {
00063         _rtc_write(t);
00064     }
00065     _mutex->unlock();
00066 }
00067 
00068 void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void))
00069 {
00070     _mutex->lock();
00071     _rtc_read = read_rtc;
00072     _rtc_write = write_rtc;
00073     _rtc_init = init_rtc;
00074     _rtc_isenabled = isenabled_rtc;
00075     _mutex->unlock();
00076 }
00077 
00078 #ifdef __cplusplus
00079 }
00080 #endif