mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
common/rtc_time.c@144:56ef597cc9e4, 2016-08-17 (annotated)
- Committer:
- cho45
- Date:
- Wed Aug 17 00:54:41 2016 +0000
- Revision:
- 144:56ef597cc9e4
- Parent:
- 0:9b334a45a8ff
ignore error; https://devzone.nordicsemi.com/question/88239/struct-has-no-field-nrffw/?answer=88247#post-id-88247
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 0:9b334a45a8ff | 1 | /* mbed Microcontroller Library |
bogdanm | 0:9b334a45a8ff | 2 | * Copyright (c) 2006-2013 ARM Limited |
bogdanm | 0:9b334a45a8ff | 3 | * |
bogdanm | 0:9b334a45a8ff | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
bogdanm | 0:9b334a45a8ff | 5 | * you may not use this file except in compliance with the License. |
bogdanm | 0:9b334a45a8ff | 6 | * You may obtain a copy of the License at |
bogdanm | 0:9b334a45a8ff | 7 | * |
bogdanm | 0:9b334a45a8ff | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bogdanm | 0:9b334a45a8ff | 9 | * |
bogdanm | 0:9b334a45a8ff | 10 | * Unless required by applicable law or agreed to in writing, software |
bogdanm | 0:9b334a45a8ff | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
bogdanm | 0:9b334a45a8ff | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bogdanm | 0:9b334a45a8ff | 13 | * See the License for the specific language governing permissions and |
bogdanm | 0:9b334a45a8ff | 14 | * limitations under the License. |
bogdanm | 0:9b334a45a8ff | 15 | */ |
bogdanm | 0:9b334a45a8ff | 16 | #include "rtc_api.h" |
bogdanm | 0:9b334a45a8ff | 17 | |
bogdanm | 0:9b334a45a8ff | 18 | #include <time.h> |
bogdanm | 0:9b334a45a8ff | 19 | #include "rtc_time.h" |
bogdanm | 0:9b334a45a8ff | 20 | #include "us_ticker_api.h" |
bogdanm | 0:9b334a45a8ff | 21 | |
bogdanm | 0:9b334a45a8ff | 22 | #if DEVICE_RTC |
bogdanm | 0:9b334a45a8ff | 23 | static void (*_rtc_init)(void) = rtc_init; |
bogdanm | 0:9b334a45a8ff | 24 | static int (*_rtc_isenabled)(void) = rtc_isenabled; |
bogdanm | 0:9b334a45a8ff | 25 | static time_t (*_rtc_read)(void) = rtc_read; |
bogdanm | 0:9b334a45a8ff | 26 | static void (*_rtc_write)(time_t t) = rtc_write; |
bogdanm | 0:9b334a45a8ff | 27 | #else |
bogdanm | 0:9b334a45a8ff | 28 | static void (*_rtc_init)(void) = NULL; |
bogdanm | 0:9b334a45a8ff | 29 | static int (*_rtc_isenabled)(void) = NULL; |
bogdanm | 0:9b334a45a8ff | 30 | static time_t (*_rtc_read)(void) = NULL; |
bogdanm | 0:9b334a45a8ff | 31 | static void (*_rtc_write)(time_t t) = NULL; |
bogdanm | 0:9b334a45a8ff | 32 | #endif |
bogdanm | 0:9b334a45a8ff | 33 | |
bogdanm | 0:9b334a45a8ff | 34 | #ifdef __cplusplus |
bogdanm | 0:9b334a45a8ff | 35 | extern "C" { |
bogdanm | 0:9b334a45a8ff | 36 | #endif |
bogdanm | 0:9b334a45a8ff | 37 | #if defined (__ICCARM__) |
bogdanm | 0:9b334a45a8ff | 38 | time_t __time32(time_t *timer) |
bogdanm | 0:9b334a45a8ff | 39 | #else |
bogdanm | 0:9b334a45a8ff | 40 | time_t time(time_t *timer) |
bogdanm | 0:9b334a45a8ff | 41 | #endif |
bogdanm | 0:9b334a45a8ff | 42 | |
bogdanm | 0:9b334a45a8ff | 43 | { |
bogdanm | 0:9b334a45a8ff | 44 | if (_rtc_isenabled != NULL) { |
bogdanm | 0:9b334a45a8ff | 45 | if (!(_rtc_isenabled())) { |
bogdanm | 0:9b334a45a8ff | 46 | set_time(0); |
bogdanm | 0:9b334a45a8ff | 47 | } |
bogdanm | 0:9b334a45a8ff | 48 | } |
bogdanm | 0:9b334a45a8ff | 49 | |
bogdanm | 0:9b334a45a8ff | 50 | time_t t = 0; |
bogdanm | 0:9b334a45a8ff | 51 | if (_rtc_read != NULL) { |
bogdanm | 0:9b334a45a8ff | 52 | t = _rtc_read(); |
bogdanm | 0:9b334a45a8ff | 53 | } |
bogdanm | 0:9b334a45a8ff | 54 | |
bogdanm | 0:9b334a45a8ff | 55 | if (timer != NULL) { |
bogdanm | 0:9b334a45a8ff | 56 | *timer = t; |
bogdanm | 0:9b334a45a8ff | 57 | } |
bogdanm | 0:9b334a45a8ff | 58 | return t; |
bogdanm | 0:9b334a45a8ff | 59 | } |
bogdanm | 0:9b334a45a8ff | 60 | |
bogdanm | 0:9b334a45a8ff | 61 | void set_time(time_t t) { |
bogdanm | 0:9b334a45a8ff | 62 | if (_rtc_init != NULL) { |
bogdanm | 0:9b334a45a8ff | 63 | _rtc_init(); |
bogdanm | 0:9b334a45a8ff | 64 | } |
bogdanm | 0:9b334a45a8ff | 65 | if (_rtc_write != NULL) { |
bogdanm | 0:9b334a45a8ff | 66 | _rtc_write(t); |
bogdanm | 0:9b334a45a8ff | 67 | } |
bogdanm | 0:9b334a45a8ff | 68 | } |
bogdanm | 0:9b334a45a8ff | 69 | |
bogdanm | 0:9b334a45a8ff | 70 | clock_t clock() { |
bogdanm | 0:9b334a45a8ff | 71 | clock_t t = us_ticker_read(); |
bogdanm | 0:9b334a45a8ff | 72 | t /= 1000000 / CLOCKS_PER_SEC; // convert to processor time |
bogdanm | 0:9b334a45a8ff | 73 | return t; |
bogdanm | 0:9b334a45a8ff | 74 | } |
bogdanm | 0:9b334a45a8ff | 75 | |
bogdanm | 0:9b334a45a8ff | 76 | void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void)) { |
bogdanm | 0:9b334a45a8ff | 77 | __disable_irq(); |
bogdanm | 0:9b334a45a8ff | 78 | _rtc_read = read_rtc; |
bogdanm | 0:9b334a45a8ff | 79 | _rtc_write = write_rtc; |
bogdanm | 0:9b334a45a8ff | 80 | _rtc_init = init_rtc; |
bogdanm | 0:9b334a45a8ff | 81 | _rtc_isenabled = isenabled_rtc; |
bogdanm | 0:9b334a45a8ff | 82 | __enable_irq(); |
bogdanm | 0:9b334a45a8ff | 83 | } |
bogdanm | 0:9b334a45a8ff | 84 | |
bogdanm | 0:9b334a45a8ff | 85 | |
bogdanm | 0:9b334a45a8ff | 86 | |
bogdanm | 0:9b334a45a8ff | 87 | #ifdef __cplusplus |
bogdanm | 0:9b334a45a8ff | 88 | } |
bogdanm | 0:9b334a45a8ff | 89 | #endif |