mbed library sources

Dependents:   RPC_Serial_V_mac

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers rtc_time.c Source File

rtc_time.c

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 "rtc_api.h"
00017 
00018 #include <time.h>
00019 #include "rtc_time.h"
00020 #include "us_ticker_api.h"
00021 
00022 #if DEVICE_RTC
00023 static void (*_rtc_init)(void) = rtc_init;
00024 static int (*_rtc_isenabled)(void) = rtc_isenabled;
00025 static time_t (*_rtc_read)(void) = rtc_read;
00026 static void (*_rtc_write)(time_t t) = rtc_write;
00027 #else
00028 static void (*_rtc_init)(void) = NULL;
00029 static int (*_rtc_isenabled)(void) = NULL;
00030 static time_t (*_rtc_read)(void) = NULL;
00031 static void (*_rtc_write)(time_t t) = NULL;
00032 #endif
00033 
00034 #ifdef __cplusplus
00035 extern "C" {
00036 #endif
00037 #if defined (__ICCARM__)
00038 time_t __time32(time_t *timer)
00039 #else
00040 time_t time(time_t *timer)
00041 #endif
00042 
00043 {
00044     if (_rtc_isenabled != NULL) {
00045         if (!(_rtc_isenabled())) {
00046             set_time(0);
00047         }
00048     }
00049     
00050     time_t t = 0;
00051     if (_rtc_read != NULL) {
00052         t = _rtc_read();
00053     }
00054 
00055     if (timer != NULL) {
00056         *timer = t;
00057     }
00058     return t;
00059 }
00060 
00061 void set_time(time_t t) {
00062     if (_rtc_init != NULL) {
00063         _rtc_init();
00064     }
00065     if (_rtc_write != NULL) {
00066         _rtc_write(t);
00067     }
00068 }
00069 
00070 clock_t clock() {
00071     clock_t t = us_ticker_read();
00072     t /= 1000000 / CLOCKS_PER_SEC; // convert to processor time
00073     return t;
00074 }
00075 
00076 void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void)) {
00077     __disable_irq();
00078     _rtc_read = read_rtc;
00079     _rtc_write = write_rtc;
00080     _rtc_init = init_rtc;
00081     _rtc_isenabled = isenabled_rtc;
00082     __enable_irq();
00083 }
00084 
00085 
00086 
00087 #ifdef __cplusplus
00088 }
00089 #endif