mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /* mbed Microcontroller Library
elessair 0:f269e3021894 2 * Copyright (c) 2015-2016 Nuvoton
elessair 0:f269e3021894 3 *
elessair 0:f269e3021894 4 * Licensed under the Apache License, Version 2.0 (the "License");
elessair 0:f269e3021894 5 * you may not use this file except in compliance with the License.
elessair 0:f269e3021894 6 * You may obtain a copy of the License at
elessair 0:f269e3021894 7 *
elessair 0:f269e3021894 8 * http://www.apache.org/licenses/LICENSE-2.0
elessair 0:f269e3021894 9 *
elessair 0:f269e3021894 10 * Unless required by applicable law or agreed to in writing, software
elessair 0:f269e3021894 11 * distributed under the License is distributed on an "AS IS" BASIS,
elessair 0:f269e3021894 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elessair 0:f269e3021894 13 * See the License for the specific language governing permissions and
elessair 0:f269e3021894 14 * limitations under the License.
elessair 0:f269e3021894 15 */
elessair 0:f269e3021894 16
elessair 0:f269e3021894 17 #include "rtc_api.h"
elessair 0:f269e3021894 18
elessair 0:f269e3021894 19 #if DEVICE_RTC
elessair 0:f269e3021894 20
elessair 0:f269e3021894 21 #include "wait_api.h"
elessair 0:f269e3021894 22 #include "mbed_error.h"
elessair 0:f269e3021894 23 #include "nu_modutil.h"
elessair 0:f269e3021894 24 #include "nu_miscutil.h"
elessair 0:f269e3021894 25
elessair 0:f269e3021894 26 #define YEAR0 1900
elessair 0:f269e3021894 27 //#define EPOCH_YR 1970
elessair 0:f269e3021894 28 static int rtc_inited = 0;
elessair 0:f269e3021894 29
elessair 0:f269e3021894 30 static const struct nu_modinit_s rtc_modinit = {RTC_0, RTC_MODULE, 0, 0, 0, RTC_IRQn, NULL};
elessair 0:f269e3021894 31
elessair 0:f269e3021894 32 void rtc_init(void)
elessair 0:f269e3021894 33 {
elessair 0:f269e3021894 34 if (rtc_inited) {
elessair 0:f269e3021894 35 return;
elessair 0:f269e3021894 36 }
elessair 0:f269e3021894 37 rtc_inited = 1;
elessair 0:f269e3021894 38
elessair 0:f269e3021894 39 // Enable IP clock
elessair 0:f269e3021894 40 CLK_EnableModuleClock(rtc_modinit.clkidx);
elessair 0:f269e3021894 41
elessair 0:f269e3021894 42 RTC_Open(NULL);
elessair 0:f269e3021894 43 }
elessair 0:f269e3021894 44
elessair 0:f269e3021894 45 void rtc_free(void)
elessair 0:f269e3021894 46 {
elessair 0:f269e3021894 47 // FIXME
elessair 0:f269e3021894 48 }
elessair 0:f269e3021894 49
elessair 0:f269e3021894 50 int rtc_isenabled(void)
elessair 0:f269e3021894 51 {
elessair 0:f269e3021894 52 return rtc_inited;
elessair 0:f269e3021894 53 }
elessair 0:f269e3021894 54
elessair 0:f269e3021894 55 /*
elessair 0:f269e3021894 56 struct tm
elessair 0:f269e3021894 57 tm_sec seconds after the minute 0-61
elessair 0:f269e3021894 58 tm_min minutes after the hour 0-59
elessair 0:f269e3021894 59 tm_hour hours since midnight 0-23
elessair 0:f269e3021894 60 tm_mday day of the month 1-31
elessair 0:f269e3021894 61 tm_mon months since January 0-11
elessair 0:f269e3021894 62 tm_year years since 1900
elessair 0:f269e3021894 63 tm_wday days since Sunday 0-6
elessair 0:f269e3021894 64 tm_yday days since January 1 0-365
elessair 0:f269e3021894 65 tm_isdst Daylight Saving Time flag
elessair 0:f269e3021894 66 */
elessair 0:f269e3021894 67
elessair 0:f269e3021894 68 time_t rtc_read(void)
elessair 0:f269e3021894 69 {
elessair 0:f269e3021894 70 if (! rtc_inited) {
elessair 0:f269e3021894 71 rtc_init();
elessair 0:f269e3021894 72 }
elessair 0:f269e3021894 73
elessair 0:f269e3021894 74 S_RTC_TIME_DATA_T rtc_datetime;
elessair 0:f269e3021894 75 RTC_GetDateAndTime(&rtc_datetime);
elessair 0:f269e3021894 76
elessair 0:f269e3021894 77 struct tm timeinfo;
elessair 0:f269e3021894 78
elessair 0:f269e3021894 79 // Convert struct tm to S_RTC_TIME_DATA_T
elessair 0:f269e3021894 80 timeinfo.tm_year = rtc_datetime.u32Year - YEAR0;
elessair 0:f269e3021894 81 timeinfo.tm_mon = rtc_datetime.u32Month - 1;
elessair 0:f269e3021894 82 timeinfo.tm_mday = rtc_datetime.u32Day;
elessair 0:f269e3021894 83 timeinfo.tm_wday = rtc_datetime.u32DayOfWeek;
elessair 0:f269e3021894 84 timeinfo.tm_hour = rtc_datetime.u32Hour;
elessair 0:f269e3021894 85 timeinfo.tm_min = rtc_datetime.u32Minute;
elessair 0:f269e3021894 86 timeinfo.tm_sec = rtc_datetime.u32Second;
elessair 0:f269e3021894 87
elessair 0:f269e3021894 88 // Convert to timestamp
elessair 0:f269e3021894 89 time_t t = mktime(&timeinfo);
elessair 0:f269e3021894 90
elessair 0:f269e3021894 91 return t;
elessair 0:f269e3021894 92 }
elessair 0:f269e3021894 93
elessair 0:f269e3021894 94 void rtc_write(time_t t)
elessair 0:f269e3021894 95 {
elessair 0:f269e3021894 96 if (! rtc_inited) {
elessair 0:f269e3021894 97 rtc_init();
elessair 0:f269e3021894 98 }
elessair 0:f269e3021894 99
elessair 0:f269e3021894 100 // Convert timestamp to struct tm
elessair 0:f269e3021894 101 struct tm *timeinfo = localtime(&t);
elessair 0:f269e3021894 102
elessair 0:f269e3021894 103 S_RTC_TIME_DATA_T rtc_datetime;
elessair 0:f269e3021894 104
elessair 0:f269e3021894 105 // Convert S_RTC_TIME_DATA_T to struct tm
elessair 0:f269e3021894 106 rtc_datetime.u32Year = timeinfo->tm_year + YEAR0;
elessair 0:f269e3021894 107 rtc_datetime.u32Month = timeinfo->tm_mon + 1;
elessair 0:f269e3021894 108 rtc_datetime.u32Day = timeinfo->tm_mday;
elessair 0:f269e3021894 109 rtc_datetime.u32DayOfWeek = timeinfo->tm_wday;
elessair 0:f269e3021894 110 rtc_datetime.u32Hour = timeinfo->tm_hour;
elessair 0:f269e3021894 111 rtc_datetime.u32Minute = timeinfo->tm_min;
elessair 0:f269e3021894 112 rtc_datetime.u32Second = timeinfo->tm_sec;
elessair 0:f269e3021894 113 rtc_datetime.u32TimeScale = RTC_CLOCK_24;
elessair 0:f269e3021894 114
elessair 0:f269e3021894 115 // NOTE: Timing issue with write to RTC registers. This delay is empirical, not rational.
elessair 0:f269e3021894 116 RTC_SetDateAndTime(&rtc_datetime);
elessair 0:f269e3021894 117 //nu_nop(6000);
elessair 0:f269e3021894 118 wait_us(100);
elessair 0:f269e3021894 119 }
elessair 0:f269e3021894 120
elessair 0:f269e3021894 121 #endif