Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_rtc_time.cpp Source File

mbed_rtc_time.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2019 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include "hal/rtc_api.h"
00018 
00019 #include "platform/mbed_rtc_time.h"
00020 #include "platform/SingletonPtr.h"
00021 #include "platform/PlatformMutex.h"
00022 
00023 static SingletonPtr<PlatformMutex>  _mutex;
00024 
00025 #if DEVICE_RTC
00026 
00027 static void (*_rtc_init)(void) = rtc_init;
00028 static int (*_rtc_isenabled)(void) = rtc_isenabled;
00029 static time_t (*_rtc_read)(void) = rtc_read;
00030 static void (*_rtc_write)(time_t t) = rtc_write;
00031 
00032 #elif DEVICE_LPTICKER
00033 
00034 #include "drivers/LowPowerTimer.h"
00035 
00036 #define US_PER_SEC 1000000
00037 
00038 static SingletonPtr<mbed::LowPowerTimer> _rtc_lp_timer;
00039 static uint64_t _rtc_lp_base;
00040 static bool _rtc_enabled;
00041 
00042 static void _rtc_lpticker_init(void)
00043 {
00044     _rtc_lp_timer->start();
00045     _rtc_enabled = true;
00046 }
00047 
00048 static int _rtc_lpticker_isenabled(void)
00049 {
00050     return (_rtc_enabled == true);
00051 }
00052 
00053 static time_t _rtc_lpticker_read(void)
00054 {
00055     return _rtc_lp_timer->read_high_resolution_us() / US_PER_SEC + _rtc_lp_base;
00056 }
00057 
00058 static void _rtc_lpticker_write(time_t t)
00059 {
00060     _rtc_lp_timer->reset();
00061     _rtc_lp_base = t;
00062 }
00063 
00064 static void (*_rtc_init)(void) = _rtc_lpticker_init;
00065 static int (*_rtc_isenabled)(void) = _rtc_lpticker_isenabled;
00066 static time_t (*_rtc_read)(void) = _rtc_lpticker_read;
00067 static void (*_rtc_write)(time_t t) = _rtc_lpticker_write;
00068 
00069 #else /* DEVICE_LPTICKER */
00070 
00071 static void (*_rtc_init)(void) = NULL;
00072 static int (*_rtc_isenabled)(void) = NULL;
00073 static time_t (*_rtc_read)(void) = NULL;
00074 static void (*_rtc_write)(time_t t) = NULL;
00075 #endif /* DEVICE_LPTICKER */
00076 
00077 #ifdef __cplusplus
00078 extern "C" {
00079 #endif
00080 
00081 int settimeofday(const struct timeval *tv, MBED_UNUSED const struct timezone *tz)
00082 {
00083     _mutex->lock();
00084     if (_rtc_init != NULL) {
00085         _rtc_init();
00086     }
00087     if (_rtc_write != NULL) {
00088         _rtc_write(tv->tv_sec);
00089     }
00090     _mutex->unlock();
00091 
00092     return 0;
00093 }
00094 
00095 int gettimeofday(struct timeval *tv, MBED_UNUSED void *tz)
00096 {
00097     _mutex->lock();
00098     if (_rtc_isenabled != NULL) {
00099         if (!(_rtc_isenabled())) {
00100             set_time(0);
00101         }
00102     }
00103 
00104     time_t t = (time_t) - 1;
00105     if (_rtc_read != NULL) {
00106         t = _rtc_read();
00107     }
00108 
00109     tv->tv_sec  = t;
00110     tv->tv_usec = 0;
00111 
00112     _mutex->unlock();
00113 
00114     return 0;
00115 }
00116 
00117 #if defined (__ICCARM__)
00118 time_t __time32(time_t *timer)
00119 #else
00120 time_t time(time_t *timer)
00121 #endif
00122 {
00123     struct timeval tv;
00124     gettimeofday(&tv, NULL);
00125 
00126     if (timer != NULL) {
00127         *timer = tv.tv_sec;
00128     }
00129 
00130     return tv.tv_sec;
00131 }
00132 
00133 
00134 void set_time(time_t t)
00135 {
00136     const struct timeval tv = { t, 0 };
00137     settimeofday(&tv, NULL);
00138 }
00139 
00140 void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void))
00141 {
00142     _mutex->lock();
00143     _rtc_read = read_rtc;
00144     _rtc_write = write_rtc;
00145     _rtc_init = init_rtc;
00146     _rtc_isenabled = isenabled_rtc;
00147     _mutex->unlock();
00148 }
00149 
00150 
00151 #ifdef __cplusplus
00152 }
00153 #endif