]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/common/rtc_time.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / common / rtc_time.c
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 ARM Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "rtc_api.h"
17
18 #include <time.h>
19 #include "rtc_time.h"
20 #include "us_ticker_api.h"
21
22 #if DEVICE_RTC
23 static void (*_rtc_init)(void) = rtc_init;
24 static int (*_rtc_isenabled)(void) = rtc_isenabled;
25 static time_t (*_rtc_read)(void) = rtc_read;
26 static void (*_rtc_write)(time_t t) = rtc_write;
27 #else
28 static void (*_rtc_init)(void) = NULL;
29 static int (*_rtc_isenabled)(void) = NULL;
30 static time_t (*_rtc_read)(void) = NULL;
31 static void (*_rtc_write)(time_t t) = NULL;
32 #endif
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 #if defined (__ICCARM__)
38 time_t __time32(time_t *timer)
39 #else
40 time_t time(time_t *timer)
41 #endif
42
43 {
44     if (_rtc_isenabled != NULL) {
45         if (!(_rtc_isenabled())) {
46             set_time(0);
47         }
48     }
49     
50     time_t t = 0;
51     if (_rtc_read != NULL) {
52         t = _rtc_read();
53     }
54
55     if (timer != NULL) {
56         *timer = t;
57     }
58     return t;
59 }
60
61 void set_time(time_t t) {
62     if (_rtc_init != NULL) {
63         _rtc_init();
64     }
65     if (_rtc_write != NULL) {
66         _rtc_write(t);
67     }
68 }
69
70 clock_t clock() {
71     clock_t t = us_ticker_read();
72     t /= 1000000 / CLOCKS_PER_SEC; // convert to processor time
73     return t;
74 }
75
76 void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void)) {
77     __disable_irq();
78     _rtc_read = read_rtc;
79     _rtc_write = write_rtc;
80     _rtc_init = init_rtc;
81     _rtc_isenabled = isenabled_rtc;
82     __enable_irq();
83 }
84
85
86
87 #ifdef __cplusplus
88 }
89 #endif