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