test

Committer:
elijahsj
Date:
Mon Nov 09 00:02:47 2020 -0500
Revision:
1:8a094db1347f
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elijahsj 1:8a094db1347f 1 /* mbed Microcontroller Library
elijahsj 1:8a094db1347f 2 * Copyright (c) 2015-2016 Nuvoton
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
elijahsj 1:8a094db1347f 17 #include "rtc_api.h"
elijahsj 1:8a094db1347f 18
elijahsj 1:8a094db1347f 19 #if DEVICE_RTC
elijahsj 1:8a094db1347f 20
elijahsj 1:8a094db1347f 21 #include "mbed_wait_api.h"
elijahsj 1:8a094db1347f 22 #include "mbed_error.h"
elijahsj 1:8a094db1347f 23 #include "nu_modutil.h"
elijahsj 1:8a094db1347f 24 #include "nu_miscutil.h"
elijahsj 1:8a094db1347f 25 #include "mbed_mktime.h"
elijahsj 1:8a094db1347f 26
elijahsj 1:8a094db1347f 27 #define YEAR0 1900
elijahsj 1:8a094db1347f 28 //#define EPOCH_YR 1970
elijahsj 1:8a094db1347f 29
elijahsj 1:8a094db1347f 30 static const struct nu_modinit_s rtc_modinit = {RTC_0, RTC_MODULE, 0, 0, 0, RTC_IRQn, NULL};
elijahsj 1:8a094db1347f 31
elijahsj 1:8a094db1347f 32 void rtc_init(void)
elijahsj 1:8a094db1347f 33 {
elijahsj 1:8a094db1347f 34 if (rtc_isenabled()) {
elijahsj 1:8a094db1347f 35 return;
elijahsj 1:8a094db1347f 36 }
elijahsj 1:8a094db1347f 37
elijahsj 1:8a094db1347f 38 RTC_Open(NULL);
elijahsj 1:8a094db1347f 39 }
elijahsj 1:8a094db1347f 40
elijahsj 1:8a094db1347f 41 void rtc_free(void)
elijahsj 1:8a094db1347f 42 {
elijahsj 1:8a094db1347f 43 // N/A
elijahsj 1:8a094db1347f 44 }
elijahsj 1:8a094db1347f 45
elijahsj 1:8a094db1347f 46 int rtc_isenabled(void)
elijahsj 1:8a094db1347f 47 {
elijahsj 1:8a094db1347f 48 // NOTE: To access (RTC) registers, clock must be enabled first.
elijahsj 1:8a094db1347f 49 if (! (CLK->APBCLK0 & CLK_APBCLK0_RTCCKEN_Msk)) {
elijahsj 1:8a094db1347f 50 // Enable IP clock
elijahsj 1:8a094db1347f 51 CLK_EnableModuleClock(rtc_modinit.clkidx);
elijahsj 1:8a094db1347f 52 }
elijahsj 1:8a094db1347f 53
elijahsj 1:8a094db1347f 54 // NOTE: Check RTC Init Active flag to support crossing reset cycle.
elijahsj 1:8a094db1347f 55 return !! (RTC->INIT & RTC_INIT_ACTIVE_Msk);
elijahsj 1:8a094db1347f 56 }
elijahsj 1:8a094db1347f 57
elijahsj 1:8a094db1347f 58 /*
elijahsj 1:8a094db1347f 59 struct tm
elijahsj 1:8a094db1347f 60 tm_sec seconds after the minute 0-61
elijahsj 1:8a094db1347f 61 tm_min minutes after the hour 0-59
elijahsj 1:8a094db1347f 62 tm_hour hours since midnight 0-23
elijahsj 1:8a094db1347f 63 tm_mday day of the month 1-31
elijahsj 1:8a094db1347f 64 tm_mon months since January 0-11
elijahsj 1:8a094db1347f 65 tm_year years since 1900
elijahsj 1:8a094db1347f 66 tm_wday days since Sunday 0-6
elijahsj 1:8a094db1347f 67 tm_yday days since January 1 0-365
elijahsj 1:8a094db1347f 68 tm_isdst Daylight Saving Time flag
elijahsj 1:8a094db1347f 69 */
elijahsj 1:8a094db1347f 70
elijahsj 1:8a094db1347f 71 time_t rtc_read(void)
elijahsj 1:8a094db1347f 72 {
elijahsj 1:8a094db1347f 73 // NOTE: After boot, RTC time registers are not synced immediately, about 1 sec latency.
elijahsj 1:8a094db1347f 74 // RTC time got (through RTC_GetDateAndTime()) in this sec would be last-synced and incorrect.
elijahsj 1:8a094db1347f 75 if (! rtc_isenabled()) {
elijahsj 1:8a094db1347f 76 rtc_init();
elijahsj 1:8a094db1347f 77 }
elijahsj 1:8a094db1347f 78
elijahsj 1:8a094db1347f 79 S_RTC_TIME_DATA_T rtc_datetime;
elijahsj 1:8a094db1347f 80 RTC_GetDateAndTime(&rtc_datetime);
elijahsj 1:8a094db1347f 81
elijahsj 1:8a094db1347f 82 struct tm timeinfo;
elijahsj 1:8a094db1347f 83
elijahsj 1:8a094db1347f 84 // Convert struct tm to S_RTC_TIME_DATA_T
elijahsj 1:8a094db1347f 85 timeinfo.tm_year = rtc_datetime.u32Year - YEAR0;
elijahsj 1:8a094db1347f 86 timeinfo.tm_mon = rtc_datetime.u32Month - 1;
elijahsj 1:8a094db1347f 87 timeinfo.tm_mday = rtc_datetime.u32Day;
elijahsj 1:8a094db1347f 88 timeinfo.tm_wday = rtc_datetime.u32DayOfWeek;
elijahsj 1:8a094db1347f 89 timeinfo.tm_hour = rtc_datetime.u32Hour;
elijahsj 1:8a094db1347f 90 timeinfo.tm_min = rtc_datetime.u32Minute;
elijahsj 1:8a094db1347f 91 timeinfo.tm_sec = rtc_datetime.u32Second;
elijahsj 1:8a094db1347f 92
elijahsj 1:8a094db1347f 93 // Convert to timestamp
elijahsj 1:8a094db1347f 94 time_t t = _rtc_mktime(&timeinfo);
elijahsj 1:8a094db1347f 95
elijahsj 1:8a094db1347f 96 return t;
elijahsj 1:8a094db1347f 97 }
elijahsj 1:8a094db1347f 98
elijahsj 1:8a094db1347f 99 void rtc_write(time_t t)
elijahsj 1:8a094db1347f 100 {
elijahsj 1:8a094db1347f 101 if (! rtc_isenabled()) {
elijahsj 1:8a094db1347f 102 rtc_init();
elijahsj 1:8a094db1347f 103 }
elijahsj 1:8a094db1347f 104
elijahsj 1:8a094db1347f 105 // Convert timestamp to struct tm
elijahsj 1:8a094db1347f 106 struct tm timeinfo;
elijahsj 1:8a094db1347f 107 if (_rtc_localtime(t, &timeinfo) == false) {
elijahsj 1:8a094db1347f 108 return;
elijahsj 1:8a094db1347f 109 }
elijahsj 1:8a094db1347f 110
elijahsj 1:8a094db1347f 111 S_RTC_TIME_DATA_T rtc_datetime;
elijahsj 1:8a094db1347f 112
elijahsj 1:8a094db1347f 113 // Convert S_RTC_TIME_DATA_T to struct tm
elijahsj 1:8a094db1347f 114 rtc_datetime.u32Year = timeinfo.tm_year + YEAR0;
elijahsj 1:8a094db1347f 115 rtc_datetime.u32Month = timeinfo.tm_mon + 1;
elijahsj 1:8a094db1347f 116 rtc_datetime.u32Day = timeinfo.tm_mday;
elijahsj 1:8a094db1347f 117 rtc_datetime.u32DayOfWeek = timeinfo.tm_wday;
elijahsj 1:8a094db1347f 118 rtc_datetime.u32Hour = timeinfo.tm_hour;
elijahsj 1:8a094db1347f 119 rtc_datetime.u32Minute = timeinfo.tm_min;
elijahsj 1:8a094db1347f 120 rtc_datetime.u32Second = timeinfo.tm_sec;
elijahsj 1:8a094db1347f 121 rtc_datetime.u32TimeScale = RTC_CLOCK_24;
elijahsj 1:8a094db1347f 122
elijahsj 1:8a094db1347f 123 // NOTE: Timing issue with write to RTC registers. This delay is empirical, not rational.
elijahsj 1:8a094db1347f 124 RTC_SetDateAndTime(&rtc_datetime);
elijahsj 1:8a094db1347f 125 //nu_nop(6000);
elijahsj 1:8a094db1347f 126 wait_us(100);
elijahsj 1:8a094db1347f 127 }
elijahsj 1:8a094db1347f 128
elijahsj 1:8a094db1347f 129 #endif