mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 31 06:02:27 2019 +0000
Revision:
1:9db0e321a9f4
updated based on mbed-os5.15.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 1:9db0e321a9f4 1 /* mbed Microcontroller Library
kenjiArai 1:9db0e321a9f4 2 * Copyright (c) 2006-2019 ARM Limited
kenjiArai 1:9db0e321a9f4 3 * SPDX-License-Identifier: Apache-2.0
kenjiArai 1:9db0e321a9f4 4 *
kenjiArai 1:9db0e321a9f4 5 * Licensed under the Apache License, Version 2.0 (the "License");
kenjiArai 1:9db0e321a9f4 6 * you may not use this file except in compliance with the License.
kenjiArai 1:9db0e321a9f4 7 * You may obtain a copy of the License at
kenjiArai 1:9db0e321a9f4 8 *
kenjiArai 1:9db0e321a9f4 9 * http://www.apache.org/licenses/LICENSE-2.0
kenjiArai 1:9db0e321a9f4 10 *
kenjiArai 1:9db0e321a9f4 11 * Unless required by applicable law or agreed to in writing, software
kenjiArai 1:9db0e321a9f4 12 * distributed under the License is distributed on an "AS IS" BASIS,
kenjiArai 1:9db0e321a9f4 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kenjiArai 1:9db0e321a9f4 14 * See the License for the specific language governing permissions and
kenjiArai 1:9db0e321a9f4 15 * limitations under the License.
kenjiArai 1:9db0e321a9f4 16 */
kenjiArai 1:9db0e321a9f4 17 #include "hal/rtc_api.h"
kenjiArai 1:9db0e321a9f4 18
kenjiArai 1:9db0e321a9f4 19 #include "platform/mbed_rtc_time.h"
kenjiArai 1:9db0e321a9f4 20 #include "platform/SingletonPtr.h"
kenjiArai 1:9db0e321a9f4 21 #include "platform/PlatformMutex.h"
kenjiArai 1:9db0e321a9f4 22
kenjiArai 1:9db0e321a9f4 23 static SingletonPtr<PlatformMutex> _mutex;
kenjiArai 1:9db0e321a9f4 24
kenjiArai 1:9db0e321a9f4 25 #if DEVICE_RTC
kenjiArai 1:9db0e321a9f4 26
kenjiArai 1:9db0e321a9f4 27 static void (*_rtc_init)(void) = rtc_init;
kenjiArai 1:9db0e321a9f4 28 static int (*_rtc_isenabled)(void) = rtc_isenabled;
kenjiArai 1:9db0e321a9f4 29 static time_t (*_rtc_read)(void) = rtc_read;
kenjiArai 1:9db0e321a9f4 30 static void (*_rtc_write)(time_t t) = rtc_write;
kenjiArai 1:9db0e321a9f4 31
kenjiArai 1:9db0e321a9f4 32 #elif DEVICE_LPTICKER
kenjiArai 1:9db0e321a9f4 33
kenjiArai 1:9db0e321a9f4 34 #include "drivers/LowPowerTimer.h"
kenjiArai 1:9db0e321a9f4 35
kenjiArai 1:9db0e321a9f4 36 #define US_PER_SEC 1000000
kenjiArai 1:9db0e321a9f4 37
kenjiArai 1:9db0e321a9f4 38 static SingletonPtr<mbed::LowPowerTimer> _rtc_lp_timer;
kenjiArai 1:9db0e321a9f4 39 static uint64_t _rtc_lp_base;
kenjiArai 1:9db0e321a9f4 40 static bool _rtc_enabled;
kenjiArai 1:9db0e321a9f4 41
kenjiArai 1:9db0e321a9f4 42 static void _rtc_lpticker_init(void)
kenjiArai 1:9db0e321a9f4 43 {
kenjiArai 1:9db0e321a9f4 44 _rtc_lp_timer->start();
kenjiArai 1:9db0e321a9f4 45 _rtc_enabled = true;
kenjiArai 1:9db0e321a9f4 46 }
kenjiArai 1:9db0e321a9f4 47
kenjiArai 1:9db0e321a9f4 48 static int _rtc_lpticker_isenabled(void)
kenjiArai 1:9db0e321a9f4 49 {
kenjiArai 1:9db0e321a9f4 50 return (_rtc_enabled == true);
kenjiArai 1:9db0e321a9f4 51 }
kenjiArai 1:9db0e321a9f4 52
kenjiArai 1:9db0e321a9f4 53 static time_t _rtc_lpticker_read(void)
kenjiArai 1:9db0e321a9f4 54 {
kenjiArai 1:9db0e321a9f4 55 return _rtc_lp_timer->read_high_resolution_us() / US_PER_SEC + _rtc_lp_base;
kenjiArai 1:9db0e321a9f4 56 }
kenjiArai 1:9db0e321a9f4 57
kenjiArai 1:9db0e321a9f4 58 static void _rtc_lpticker_write(time_t t)
kenjiArai 1:9db0e321a9f4 59 {
kenjiArai 1:9db0e321a9f4 60 _rtc_lp_timer->reset();
kenjiArai 1:9db0e321a9f4 61 _rtc_lp_base = t;
kenjiArai 1:9db0e321a9f4 62 }
kenjiArai 1:9db0e321a9f4 63
kenjiArai 1:9db0e321a9f4 64 static void (*_rtc_init)(void) = _rtc_lpticker_init;
kenjiArai 1:9db0e321a9f4 65 static int (*_rtc_isenabled)(void) = _rtc_lpticker_isenabled;
kenjiArai 1:9db0e321a9f4 66 static time_t (*_rtc_read)(void) = _rtc_lpticker_read;
kenjiArai 1:9db0e321a9f4 67 static void (*_rtc_write)(time_t t) = _rtc_lpticker_write;
kenjiArai 1:9db0e321a9f4 68
kenjiArai 1:9db0e321a9f4 69 #else /* DEVICE_LPTICKER */
kenjiArai 1:9db0e321a9f4 70
kenjiArai 1:9db0e321a9f4 71 static void (*_rtc_init)(void) = NULL;
kenjiArai 1:9db0e321a9f4 72 static int (*_rtc_isenabled)(void) = NULL;
kenjiArai 1:9db0e321a9f4 73 static time_t (*_rtc_read)(void) = NULL;
kenjiArai 1:9db0e321a9f4 74 static void (*_rtc_write)(time_t t) = NULL;
kenjiArai 1:9db0e321a9f4 75 #endif /* DEVICE_LPTICKER */
kenjiArai 1:9db0e321a9f4 76
kenjiArai 1:9db0e321a9f4 77 #ifdef __cplusplus
kenjiArai 1:9db0e321a9f4 78 extern "C" {
kenjiArai 1:9db0e321a9f4 79 #endif
kenjiArai 1:9db0e321a9f4 80
kenjiArai 1:9db0e321a9f4 81 int settimeofday(const struct timeval *tv, MBED_UNUSED const struct timezone *tz)
kenjiArai 1:9db0e321a9f4 82 {
kenjiArai 1:9db0e321a9f4 83 _mutex->lock();
kenjiArai 1:9db0e321a9f4 84 if (_rtc_init != NULL) {
kenjiArai 1:9db0e321a9f4 85 _rtc_init();
kenjiArai 1:9db0e321a9f4 86 }
kenjiArai 1:9db0e321a9f4 87 if (_rtc_write != NULL) {
kenjiArai 1:9db0e321a9f4 88 _rtc_write(tv->tv_sec);
kenjiArai 1:9db0e321a9f4 89 }
kenjiArai 1:9db0e321a9f4 90 _mutex->unlock();
kenjiArai 1:9db0e321a9f4 91
kenjiArai 1:9db0e321a9f4 92 return 0;
kenjiArai 1:9db0e321a9f4 93 }
kenjiArai 1:9db0e321a9f4 94
kenjiArai 1:9db0e321a9f4 95 int gettimeofday(struct timeval *tv, MBED_UNUSED void *tz)
kenjiArai 1:9db0e321a9f4 96 {
kenjiArai 1:9db0e321a9f4 97 _mutex->lock();
kenjiArai 1:9db0e321a9f4 98 if (_rtc_isenabled != NULL) {
kenjiArai 1:9db0e321a9f4 99 if (!(_rtc_isenabled())) {
kenjiArai 1:9db0e321a9f4 100 set_time(0);
kenjiArai 1:9db0e321a9f4 101 }
kenjiArai 1:9db0e321a9f4 102 }
kenjiArai 1:9db0e321a9f4 103
kenjiArai 1:9db0e321a9f4 104 time_t t = (time_t) - 1;
kenjiArai 1:9db0e321a9f4 105 if (_rtc_read != NULL) {
kenjiArai 1:9db0e321a9f4 106 t = _rtc_read();
kenjiArai 1:9db0e321a9f4 107 }
kenjiArai 1:9db0e321a9f4 108
kenjiArai 1:9db0e321a9f4 109 tv->tv_sec = t;
kenjiArai 1:9db0e321a9f4 110 tv->tv_usec = 0;
kenjiArai 1:9db0e321a9f4 111
kenjiArai 1:9db0e321a9f4 112 _mutex->unlock();
kenjiArai 1:9db0e321a9f4 113
kenjiArai 1:9db0e321a9f4 114 return 0;
kenjiArai 1:9db0e321a9f4 115 }
kenjiArai 1:9db0e321a9f4 116
kenjiArai 1:9db0e321a9f4 117 #if defined (__ICCARM__)
kenjiArai 1:9db0e321a9f4 118 time_t __time32(time_t *timer)
kenjiArai 1:9db0e321a9f4 119 #else
kenjiArai 1:9db0e321a9f4 120 time_t time(time_t *timer)
kenjiArai 1:9db0e321a9f4 121 #endif
kenjiArai 1:9db0e321a9f4 122 {
kenjiArai 1:9db0e321a9f4 123 struct timeval tv;
kenjiArai 1:9db0e321a9f4 124 gettimeofday(&tv, NULL);
kenjiArai 1:9db0e321a9f4 125
kenjiArai 1:9db0e321a9f4 126 if (timer != NULL) {
kenjiArai 1:9db0e321a9f4 127 *timer = tv.tv_sec;
kenjiArai 1:9db0e321a9f4 128 }
kenjiArai 1:9db0e321a9f4 129
kenjiArai 1:9db0e321a9f4 130 return tv.tv_sec;
kenjiArai 1:9db0e321a9f4 131 }
kenjiArai 1:9db0e321a9f4 132
kenjiArai 1:9db0e321a9f4 133
kenjiArai 1:9db0e321a9f4 134 void set_time(time_t t)
kenjiArai 1:9db0e321a9f4 135 {
kenjiArai 1:9db0e321a9f4 136 const struct timeval tv = { t, 0 };
kenjiArai 1:9db0e321a9f4 137 settimeofday(&tv, NULL);
kenjiArai 1:9db0e321a9f4 138 }
kenjiArai 1:9db0e321a9f4 139
kenjiArai 1:9db0e321a9f4 140 void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void))
kenjiArai 1:9db0e321a9f4 141 {
kenjiArai 1:9db0e321a9f4 142 _mutex->lock();
kenjiArai 1:9db0e321a9f4 143 _rtc_read = read_rtc;
kenjiArai 1:9db0e321a9f4 144 _rtc_write = write_rtc;
kenjiArai 1:9db0e321a9f4 145 _rtc_init = init_rtc;
kenjiArai 1:9db0e321a9f4 146 _rtc_isenabled = isenabled_rtc;
kenjiArai 1:9db0e321a9f4 147 _mutex->unlock();
kenjiArai 1:9db0e321a9f4 148 }
kenjiArai 1:9db0e321a9f4 149
kenjiArai 1:9db0e321a9f4 150
kenjiArai 1:9db0e321a9f4 151 #ifdef __cplusplus
kenjiArai 1:9db0e321a9f4 152 }
kenjiArai 1:9db0e321a9f4 153 #endif