initial

Dependencies:   mbed

Committer:
yihui
Date:
Mon Jan 11 02:32:24 2016 +0000
Revision:
0:638edba3adf6
initial

Who changed what in which revision?

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