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