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