,,
Fork of Application by
mbed-dev/platform/mbed_rtc_time.cpp@10:41552d038a69, 2017-01-10 (annotated)
- Committer:
- Zaitsev
- Date:
- Tue Jan 10 20:42:26 2017 +0000
- Revision:
- 10:41552d038a69
USB Serial bi-directional bridge
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Zaitsev | 10:41552d038a69 | 1 | /* mbed Microcontroller Library |
Zaitsev | 10:41552d038a69 | 2 | * Copyright (c) 2006-2013 ARM Limited |
Zaitsev | 10:41552d038a69 | 3 | * |
Zaitsev | 10:41552d038a69 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Zaitsev | 10:41552d038a69 | 5 | * you may not use this file except in compliance with the License. |
Zaitsev | 10:41552d038a69 | 6 | * You may obtain a copy of the License at |
Zaitsev | 10:41552d038a69 | 7 | * |
Zaitsev | 10:41552d038a69 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Zaitsev | 10:41552d038a69 | 9 | * |
Zaitsev | 10:41552d038a69 | 10 | * Unless required by applicable law or agreed to in writing, software |
Zaitsev | 10:41552d038a69 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Zaitsev | 10:41552d038a69 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Zaitsev | 10:41552d038a69 | 13 | * See the License for the specific language governing permissions and |
Zaitsev | 10:41552d038a69 | 14 | * limitations under the License. |
Zaitsev | 10:41552d038a69 | 15 | */ |
Zaitsev | 10:41552d038a69 | 16 | #include "hal/rtc_api.h" |
Zaitsev | 10:41552d038a69 | 17 | |
Zaitsev | 10:41552d038a69 | 18 | #include <time.h> |
Zaitsev | 10:41552d038a69 | 19 | #include "platform/critical.h" |
Zaitsev | 10:41552d038a69 | 20 | #include "platform/rtc_time.h" |
Zaitsev | 10:41552d038a69 | 21 | #include "hal/us_ticker_api.h" |
Zaitsev | 10:41552d038a69 | 22 | #include "platform/SingletonPtr.h" |
Zaitsev | 10:41552d038a69 | 23 | #include "platform/PlatformMutex.h" |
Zaitsev | 10:41552d038a69 | 24 | |
Zaitsev | 10:41552d038a69 | 25 | static SingletonPtr<PlatformMutex> _mutex; |
Zaitsev | 10:41552d038a69 | 26 | |
Zaitsev | 10:41552d038a69 | 27 | #if DEVICE_RTC |
Zaitsev | 10:41552d038a69 | 28 | static void (*_rtc_init)(void) = rtc_init; |
Zaitsev | 10:41552d038a69 | 29 | static int (*_rtc_isenabled)(void) = rtc_isenabled; |
Zaitsev | 10:41552d038a69 | 30 | static time_t (*_rtc_read)(void) = rtc_read; |
Zaitsev | 10:41552d038a69 | 31 | static void (*_rtc_write)(time_t t) = rtc_write; |
Zaitsev | 10:41552d038a69 | 32 | #else |
Zaitsev | 10:41552d038a69 | 33 | static void (*_rtc_init)(void) = NULL; |
Zaitsev | 10:41552d038a69 | 34 | static int (*_rtc_isenabled)(void) = NULL; |
Zaitsev | 10:41552d038a69 | 35 | static time_t (*_rtc_read)(void) = NULL; |
Zaitsev | 10:41552d038a69 | 36 | static void (*_rtc_write)(time_t t) = NULL; |
Zaitsev | 10:41552d038a69 | 37 | #endif |
Zaitsev | 10:41552d038a69 | 38 | |
Zaitsev | 10:41552d038a69 | 39 | #ifdef __cplusplus |
Zaitsev | 10:41552d038a69 | 40 | extern "C" { |
Zaitsev | 10:41552d038a69 | 41 | #endif |
Zaitsev | 10:41552d038a69 | 42 | #if defined (__ICCARM__) |
Zaitsev | 10:41552d038a69 | 43 | time_t __time32(time_t *timer) |
Zaitsev | 10:41552d038a69 | 44 | #else |
Zaitsev | 10:41552d038a69 | 45 | time_t time(time_t *timer) |
Zaitsev | 10:41552d038a69 | 46 | #endif |
Zaitsev | 10:41552d038a69 | 47 | |
Zaitsev | 10:41552d038a69 | 48 | { |
Zaitsev | 10:41552d038a69 | 49 | _mutex->lock(); |
Zaitsev | 10:41552d038a69 | 50 | if (_rtc_isenabled != NULL) { |
Zaitsev | 10:41552d038a69 | 51 | if (!(_rtc_isenabled())) { |
Zaitsev | 10:41552d038a69 | 52 | set_time(0); |
Zaitsev | 10:41552d038a69 | 53 | } |
Zaitsev | 10:41552d038a69 | 54 | } |
Zaitsev | 10:41552d038a69 | 55 | |
Zaitsev | 10:41552d038a69 | 56 | time_t t = 0; |
Zaitsev | 10:41552d038a69 | 57 | if (_rtc_read != NULL) { |
Zaitsev | 10:41552d038a69 | 58 | t = _rtc_read(); |
Zaitsev | 10:41552d038a69 | 59 | } |
Zaitsev | 10:41552d038a69 | 60 | |
Zaitsev | 10:41552d038a69 | 61 | if (timer != NULL) { |
Zaitsev | 10:41552d038a69 | 62 | *timer = t; |
Zaitsev | 10:41552d038a69 | 63 | } |
Zaitsev | 10:41552d038a69 | 64 | _mutex->unlock(); |
Zaitsev | 10:41552d038a69 | 65 | return t; |
Zaitsev | 10:41552d038a69 | 66 | } |
Zaitsev | 10:41552d038a69 | 67 | |
Zaitsev | 10:41552d038a69 | 68 | void set_time(time_t t) { |
Zaitsev | 10:41552d038a69 | 69 | _mutex->lock(); |
Zaitsev | 10:41552d038a69 | 70 | if (_rtc_init != NULL) { |
Zaitsev | 10:41552d038a69 | 71 | _rtc_init(); |
Zaitsev | 10:41552d038a69 | 72 | } |
Zaitsev | 10:41552d038a69 | 73 | if (_rtc_write != NULL) { |
Zaitsev | 10:41552d038a69 | 74 | _rtc_write(t); |
Zaitsev | 10:41552d038a69 | 75 | } |
Zaitsev | 10:41552d038a69 | 76 | _mutex->unlock(); |
Zaitsev | 10:41552d038a69 | 77 | } |
Zaitsev | 10:41552d038a69 | 78 | |
Zaitsev | 10:41552d038a69 | 79 | clock_t clock() { |
Zaitsev | 10:41552d038a69 | 80 | _mutex->lock(); |
Zaitsev | 10:41552d038a69 | 81 | clock_t t = us_ticker_read(); |
Zaitsev | 10:41552d038a69 | 82 | t /= 1000000 / CLOCKS_PER_SEC; // convert to processor time |
Zaitsev | 10:41552d038a69 | 83 | _mutex->unlock(); |
Zaitsev | 10:41552d038a69 | 84 | return t; |
Zaitsev | 10:41552d038a69 | 85 | } |
Zaitsev | 10:41552d038a69 | 86 | |
Zaitsev | 10:41552d038a69 | 87 | void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void)) { |
Zaitsev | 10:41552d038a69 | 88 | _mutex->lock(); |
Zaitsev | 10:41552d038a69 | 89 | _rtc_read = read_rtc; |
Zaitsev | 10:41552d038a69 | 90 | _rtc_write = write_rtc; |
Zaitsev | 10:41552d038a69 | 91 | _rtc_init = init_rtc; |
Zaitsev | 10:41552d038a69 | 92 | _rtc_isenabled = isenabled_rtc; |
Zaitsev | 10:41552d038a69 | 93 | _mutex->unlock(); |
Zaitsev | 10:41552d038a69 | 94 | } |
Zaitsev | 10:41552d038a69 | 95 | |
Zaitsev | 10:41552d038a69 | 96 | |
Zaitsev | 10:41552d038a69 | 97 | |
Zaitsev | 10:41552d038a69 | 98 | #ifdef __cplusplus |
Zaitsev | 10:41552d038a69 | 99 | } |
Zaitsev | 10:41552d038a69 | 100 | #endif |