mbed library for NZ32-SC151

Committer:
modtronix-com
Date:
Fri Aug 19 15:46:42 2016 +1000
Revision:
17:639ed60ce759
Parent:
1:71204b8406f2
Added tag v1.1 for changeset 076cbe3e55be

Who changed what in which revision?

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