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