Committer:
ganlikun
Date:
Mon Oct 24 15:19:39 2022 +0000
Revision:
0:06036f8bee2d
11

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ganlikun 0:06036f8bee2d 1 /* mbed Microcontroller Library
ganlikun 0:06036f8bee2d 2 * Copyright (c) 2006-2013 ARM Limited
ganlikun 0:06036f8bee2d 3 *
ganlikun 0:06036f8bee2d 4 * Licensed under the Apache License, Version 2.0 (the "License");
ganlikun 0:06036f8bee2d 5 * you may not use this file except in compliance with the License.
ganlikun 0:06036f8bee2d 6 * You may obtain a copy of the License at
ganlikun 0:06036f8bee2d 7 *
ganlikun 0:06036f8bee2d 8 * http://www.apache.org/licenses/LICENSE-2.0
ganlikun 0:06036f8bee2d 9 *
ganlikun 0:06036f8bee2d 10 * Unless required by applicable law or agreed to in writing, software
ganlikun 0:06036f8bee2d 11 * distributed under the License is distributed on an "AS IS" BASIS,
ganlikun 0:06036f8bee2d 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ganlikun 0:06036f8bee2d 13 * See the License for the specific language governing permissions and
ganlikun 0:06036f8bee2d 14 * limitations under the License.
ganlikun 0:06036f8bee2d 15 */
ganlikun 0:06036f8bee2d 16 #include "hal/rtc_api.h"
ganlikun 0:06036f8bee2d 17
ganlikun 0:06036f8bee2d 18 #include "platform/mbed_critical.h"
ganlikun 0:06036f8bee2d 19 #include "platform/mbed_rtc_time.h"
ganlikun 0:06036f8bee2d 20 #include "platform/SingletonPtr.h"
ganlikun 0:06036f8bee2d 21 #include "platform/PlatformMutex.h"
ganlikun 0:06036f8bee2d 22
ganlikun 0:06036f8bee2d 23 static SingletonPtr<PlatformMutex> _mutex;
ganlikun 0:06036f8bee2d 24
ganlikun 0:06036f8bee2d 25 #if DEVICE_RTC
ganlikun 0:06036f8bee2d 26 static void (*_rtc_init)(void) = rtc_init;
ganlikun 0:06036f8bee2d 27 static int (*_rtc_isenabled)(void) = rtc_isenabled;
ganlikun 0:06036f8bee2d 28 static time_t (*_rtc_read)(void) = rtc_read;
ganlikun 0:06036f8bee2d 29 static void (*_rtc_write)(time_t t) = rtc_write;
ganlikun 0:06036f8bee2d 30 #else
ganlikun 0:06036f8bee2d 31 static void (*_rtc_init)(void) = NULL;
ganlikun 0:06036f8bee2d 32 static int (*_rtc_isenabled)(void) = NULL;
ganlikun 0:06036f8bee2d 33 static time_t (*_rtc_read)(void) = NULL;
ganlikun 0:06036f8bee2d 34 static void (*_rtc_write)(time_t t) = NULL;
ganlikun 0:06036f8bee2d 35 #endif
ganlikun 0:06036f8bee2d 36
ganlikun 0:06036f8bee2d 37 #ifdef __cplusplus
ganlikun 0:06036f8bee2d 38 extern "C" {
ganlikun 0:06036f8bee2d 39 #endif
ganlikun 0:06036f8bee2d 40 #if defined (__ICCARM__)
ganlikun 0:06036f8bee2d 41 time_t __time32(time_t *timer)
ganlikun 0:06036f8bee2d 42 #else
ganlikun 0:06036f8bee2d 43 time_t time(time_t *timer)
ganlikun 0:06036f8bee2d 44 #endif
ganlikun 0:06036f8bee2d 45
ganlikun 0:06036f8bee2d 46 {
ganlikun 0:06036f8bee2d 47 _mutex->lock();
ganlikun 0:06036f8bee2d 48 if (_rtc_isenabled != NULL) {
ganlikun 0:06036f8bee2d 49 if (!(_rtc_isenabled())) {
ganlikun 0:06036f8bee2d 50 set_time(0);
ganlikun 0:06036f8bee2d 51 }
ganlikun 0:06036f8bee2d 52 }
ganlikun 0:06036f8bee2d 53
ganlikun 0:06036f8bee2d 54 time_t t = (time_t)-1;
ganlikun 0:06036f8bee2d 55 if (_rtc_read != NULL) {
ganlikun 0:06036f8bee2d 56 t = _rtc_read();
ganlikun 0:06036f8bee2d 57 }
ganlikun 0:06036f8bee2d 58
ganlikun 0:06036f8bee2d 59 if (timer != NULL) {
ganlikun 0:06036f8bee2d 60 *timer = t;
ganlikun 0:06036f8bee2d 61 }
ganlikun 0:06036f8bee2d 62 _mutex->unlock();
ganlikun 0:06036f8bee2d 63 return t;
ganlikun 0:06036f8bee2d 64 }
ganlikun 0:06036f8bee2d 65
ganlikun 0:06036f8bee2d 66 void set_time(time_t t) {
ganlikun 0:06036f8bee2d 67 _mutex->lock();
ganlikun 0:06036f8bee2d 68 if (_rtc_init != NULL) {
ganlikun 0:06036f8bee2d 69 _rtc_init();
ganlikun 0:06036f8bee2d 70 }
ganlikun 0:06036f8bee2d 71 if (_rtc_write != NULL) {
ganlikun 0:06036f8bee2d 72 _rtc_write(t);
ganlikun 0:06036f8bee2d 73 }
ganlikun 0:06036f8bee2d 74 _mutex->unlock();
ganlikun 0:06036f8bee2d 75 }
ganlikun 0:06036f8bee2d 76
ganlikun 0:06036f8bee2d 77 void attach_rtc(time_t (*read_rtc)(void), void (*write_rtc)(time_t), void (*init_rtc)(void), int (*isenabled_rtc)(void)) {
ganlikun 0:06036f8bee2d 78 _mutex->lock();
ganlikun 0:06036f8bee2d 79 _rtc_read = read_rtc;
ganlikun 0:06036f8bee2d 80 _rtc_write = write_rtc;
ganlikun 0:06036f8bee2d 81 _rtc_init = init_rtc;
ganlikun 0:06036f8bee2d 82 _rtc_isenabled = isenabled_rtc;
ganlikun 0:06036f8bee2d 83 _mutex->unlock();
ganlikun 0:06036f8bee2d 84 }
ganlikun 0:06036f8bee2d 85
ganlikun 0:06036f8bee2d 86
ganlikun 0:06036f8bee2d 87
ganlikun 0:06036f8bee2d 88 #ifdef __cplusplus
ganlikun 0:06036f8bee2d 89 }
ganlikun 0:06036f8bee2d 90 #endif
ganlikun 0:06036f8bee2d 91