Maxim mbed development library

Dependents:   sensomed

Committer:
switches
Date:
Tue Nov 08 18:27:11 2016 +0000
Revision:
0:0e018d759a2a
Initial commit

Who changed what in which revision?

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