mbed

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Thu Aug 31 17:27:04 2017 +0100
Revision:
173:7d866c31b3c5
Child:
177:447f873cad2f
This updates the lib to the mbed lib v 150

Who changed what in which revision?

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