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