Elijah Stanger-Jones / mbed-dev-f303
Committer:
elijahsj
Date:
Mon Nov 09 00:33:19 2020 -0500
Revision:
2:4364577b5ad8
Parent:
1:8a094db1347f
copied mbed library

Who changed what in which revision?

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