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