Initial commit
mbed-dev-master/platform/mbed_rtc_time.cpp@0:bb348c97df44, 2020-09-16 (annotated)
- Committer:
- lypinator
- Date:
- Wed Sep 16 01:11:49 2020 +0000
- Revision:
- 0:bb348c97df44
Added PWM
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lypinator | 0:bb348c97df44 | 1 | /* mbed Microcontroller Library |
lypinator | 0:bb348c97df44 | 2 | * Copyright (c) 2006-2013 ARM Limited |
lypinator | 0:bb348c97df44 | 3 | * |
lypinator | 0:bb348c97df44 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
lypinator | 0:bb348c97df44 | 5 | * you may not use this file except in compliance with the License. |
lypinator | 0:bb348c97df44 | 6 | * You may obtain a copy of the License at |
lypinator | 0:bb348c97df44 | 7 | * |
lypinator | 0:bb348c97df44 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
lypinator | 0:bb348c97df44 | 9 | * |
lypinator | 0:bb348c97df44 | 10 | * Unless required by applicable law or agreed to in writing, software |
lypinator | 0:bb348c97df44 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
lypinator | 0:bb348c97df44 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
lypinator | 0:bb348c97df44 | 13 | * See the License for the specific language governing permissions and |
lypinator | 0:bb348c97df44 | 14 | * limitations under the License. |
lypinator | 0:bb348c97df44 | 15 | */ |
lypinator | 0:bb348c97df44 | 16 | #include "hal/rtc_api.h" |
lypinator | 0:bb348c97df44 | 17 | |
lypinator | 0:bb348c97df44 | 18 | #include "platform/mbed_critical.h" |
lypinator | 0:bb348c97df44 | 19 | #include "platform/mbed_rtc_time.h" |
lypinator | 0:bb348c97df44 | 20 | #include "platform/SingletonPtr.h" |
lypinator | 0:bb348c97df44 | 21 | #include "platform/PlatformMutex.h" |
lypinator | 0:bb348c97df44 | 22 | |
lypinator | 0:bb348c97df44 | 23 | static SingletonPtr<PlatformMutex> _mutex; |
lypinator | 0:bb348c97df44 | 24 | |
lypinator | 0:bb348c97df44 | 25 | #if DEVICE_RTC |
lypinator | 0:bb348c97df44 | 26 | |
lypinator | 0:bb348c97df44 | 27 | static void (*_rtc_init)(void) = rtc_init; |
lypinator | 0:bb348c97df44 | 28 | static int (*_rtc_isenabled)(void) = rtc_isenabled; |
lypinator | 0:bb348c97df44 | 29 | static time_t (*_rtc_read)(void) = rtc_read; |
lypinator | 0:bb348c97df44 | 30 | static void (*_rtc_write)(time_t t) = rtc_write; |
lypinator | 0:bb348c97df44 | 31 | |
lypinator | 0:bb348c97df44 | 32 | #elif DEVICE_LPTICKER |
lypinator | 0:bb348c97df44 | 33 | |
lypinator | 0:bb348c97df44 | 34 | #include "drivers/LowPowerTimer.h" |
lypinator | 0:bb348c97df44 | 35 | |
lypinator | 0:bb348c97df44 | 36 | static SingletonPtr<mbed::LowPowerTimer> _rtc_lp_timer; |
lypinator | 0:bb348c97df44 | 37 | static uint64_t _rtc_lp_base; |
lypinator | 0:bb348c97df44 | 38 | static bool _rtc_enabled; |
lypinator | 0:bb348c97df44 | 39 | |
lypinator | 0:bb348c97df44 | 40 | static void _rtc_lpticker_init(void) |
lypinator | 0:bb348c97df44 | 41 | { |
lypinator | 0:bb348c97df44 | 42 | _rtc_lp_timer->start(); |
lypinator | 0:bb348c97df44 | 43 | _rtc_enabled = true; |
lypinator | 0:bb348c97df44 | 44 | } |
lypinator | 0:bb348c97df44 | 45 | |
lypinator | 0:bb348c97df44 | 46 | static int _rtc_lpticker_isenabled(void) |
lypinator | 0:bb348c97df44 | 47 | { |
lypinator | 0:bb348c97df44 | 48 | return (_rtc_enabled == true); |
lypinator | 0:bb348c97df44 | 49 | } |
lypinator | 0:bb348c97df44 | 50 | |
lypinator | 0:bb348c97df44 | 51 | static time_t _rtc_lpticker_read(void) |
lypinator | 0:bb348c97df44 | 52 | { |
lypinator | 0:bb348c97df44 | 53 | return (uint64_t)_rtc_lp_timer->read() + _rtc_lp_base; |
lypinator | 0:bb348c97df44 | 54 | } |
lypinator | 0:bb348c97df44 | 55 | |
lypinator | 0:bb348c97df44 | 56 | static void _rtc_lpticker_write(time_t t) |
lypinator | 0:bb348c97df44 | 57 | { |
lypinator | 0:bb348c97df44 | 58 | _rtc_lp_base = t; |
lypinator | 0:bb348c97df44 | 59 | } |
lypinator | 0:bb348c97df44 | 60 | |
lypinator | 0:bb348c97df44 | 61 | static void (*_rtc_init)(void) = _rtc_lpticker_init; |
lypinator | 0:bb348c97df44 | 62 | static int (*_rtc_isenabled)(void) = _rtc_lpticker_isenabled; |
lypinator | 0:bb348c97df44 | 63 | static time_t (*_rtc_read)(void) = _rtc_lpticker_read; |
lypinator | 0:bb348c97df44 | 64 | static void (*_rtc_write)(time_t t) = _rtc_lpticker_write; |
lypinator | 0:bb348c97df44 | 65 | |
lypinator | 0:bb348c97df44 | 66 | #else /* DEVICE_LPTICKER */ |
lypinator | 0:bb348c97df44 | 67 | |
lypinator | 0:bb348c97df44 | 68 | static void (*_rtc_init)(void) = NULL; |
lypinator | 0:bb348c97df44 | 69 | static int (*_rtc_isenabled)(void) = NULL; |
lypinator | 0:bb348c97df44 | 70 | static time_t (*_rtc_read)(void) = NULL; |
lypinator | 0:bb348c97df44 | 71 | static void (*_rtc_write)(time_t t) = NULL; |
lypinator | 0:bb348c97df44 | 72 | #endif /* DEVICE_LPTICKER */ |
lypinator | 0:bb348c97df44 | 73 | |
lypinator | 0:bb348c97df44 | 74 | #ifdef __cplusplus |
lypinator | 0:bb348c97df44 | 75 | extern "C" { |
lypinator | 0:bb348c97df44 | 76 | #endif |
lypinator | 0:bb348c97df44 | 77 | #if defined (__ICCARM__) |
lypinator | 0:bb348c97df44 | 78 | time_t __time32(time_t *timer) |
lypinator | 0:bb348c97df44 | 79 | #else |
lypinator | 0:bb348c97df44 | 80 | time_t time(time_t *timer) |
lypinator | 0:bb348c97df44 | 81 | #endif |
lypinator | 0:bb348c97df44 | 82 | |
lypinator | 0:bb348c97df44 | 83 | { |
lypinator | 0:bb348c97df44 | 84 | _mutex->lock(); |
lypinator | 0:bb348c97df44 | 85 | if (_rtc_isenabled != NULL) { |
lypinator | 0:bb348c97df44 | 86 | if (!(_rtc_isenabled())) { |
lypinator | 0:bb348c97df44 | 87 | set_time(0); |
lypinator | 0:bb348c97df44 | 88 | } |
lypinator | 0:bb348c97df44 | 89 | } |
lypinator | 0:bb348c97df44 | 90 | |
lypinator | 0:bb348c97df44 | 91 | time_t t = (time_t) -1; |
lypinator | 0:bb348c97df44 | 92 | if (_rtc_read != NULL) { |
lypinator | 0:bb348c97df44 | 93 | t = _rtc_read(); |
lypinator | 0:bb348c97df44 | 94 | } |
lypinator | 0:bb348c97df44 | 95 | |
lypinator | 0:bb348c97df44 | 96 | if (timer != NULL) { |
lypinator | 0:bb348c97df44 | 97 | *timer = t; |
lypinator | 0:bb348c97df44 | 98 | } |
lypinator | 0:bb348c97df44 | 99 | _mutex->unlock(); |
lypinator | 0:bb348c97df44 | 100 | return t; |
lypinator | 0:bb348c97df44 | 101 | } |
lypinator | 0:bb348c97df44 | 102 | |
lypinator | 0:bb348c97df44 | 103 | void set_time(time_t t) |
lypinator | 0:bb348c97df44 | 104 | { |
lypinator | 0:bb348c97df44 | 105 | _mutex->lock(); |
lypinator | 0:bb348c97df44 | 106 | if (_rtc_init != NULL) { |
lypinator | 0:bb348c97df44 | 107 | _rtc_init(); |
lypinator | 0:bb348c97df44 | 108 | } |
lypinator | 0:bb348c97df44 | 109 | if (_rtc_write != NULL) { |
lypinator | 0:bb348c97df44 | 110 | _rtc_write(t); |
lypinator | 0:bb348c97df44 | 111 | } |
lypinator | 0:bb348c97df44 | 112 | _mutex->unlock(); |
lypinator | 0:bb348c97df44 | 113 | } |
lypinator | 0:bb348c97df44 | 114 | |
lypinator | 0:bb348c97df44 | 115 | void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void)) |
lypinator | 0:bb348c97df44 | 116 | { |
lypinator | 0:bb348c97df44 | 117 | _mutex->lock(); |
lypinator | 0:bb348c97df44 | 118 | _rtc_read = read_rtc; |
lypinator | 0:bb348c97df44 | 119 | _rtc_write = write_rtc; |
lypinator | 0:bb348c97df44 | 120 | _rtc_init = init_rtc; |
lypinator | 0:bb348c97df44 | 121 | _rtc_isenabled = isenabled_rtc; |
lypinator | 0:bb348c97df44 | 122 | _mutex->unlock(); |
lypinator | 0:bb348c97df44 | 123 | } |
lypinator | 0:bb348c97df44 | 124 | |
lypinator | 0:bb348c97df44 | 125 | |
lypinator | 0:bb348c97df44 | 126 | |
lypinator | 0:bb348c97df44 | 127 | #ifdef __cplusplus |
lypinator | 0:bb348c97df44 | 128 | } |
lypinator | 0:bb348c97df44 | 129 | #endif |