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