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