helpfor studient

Dependents:   STM32_F103-C8T6basecanblink_led

Fork of mbed-dev by mbed official

Committer:
Anna Bridge
Date:
Fri Apr 20 11:31:35 2018 +0100
Revision:
186:9c2029bfadbe
Parent:
185:08ed48f1de7f
Update to latest version of mbed lib

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 144:ef7eb2e8f9f7 1 /* mbed Microcontroller Library
<> 144:ef7eb2e8f9f7 2 * Copyright (c) 2015-2016 Nuvoton
<> 144:ef7eb2e8f9f7 3 *
<> 144:ef7eb2e8f9f7 4 * Licensed under the Apache License, Version 2.0 (the "License");
<> 144:ef7eb2e8f9f7 5 * you may not use this file except in compliance with the License.
<> 144:ef7eb2e8f9f7 6 * You may obtain a copy of the License at
<> 144:ef7eb2e8f9f7 7 *
<> 144:ef7eb2e8f9f7 8 * http://www.apache.org/licenses/LICENSE-2.0
<> 144:ef7eb2e8f9f7 9 *
<> 144:ef7eb2e8f9f7 10 * Unless required by applicable law or agreed to in writing, software
<> 144:ef7eb2e8f9f7 11 * distributed under the License is distributed on an "AS IS" BASIS,
<> 144:ef7eb2e8f9f7 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<> 144:ef7eb2e8f9f7 13 * See the License for the specific language governing permissions and
<> 144:ef7eb2e8f9f7 14 * limitations under the License.
<> 144:ef7eb2e8f9f7 15 */
AnnaBridge 185:08ed48f1de7f 16
<> 144:ef7eb2e8f9f7 17 #include "rtc_api.h"
<> 144:ef7eb2e8f9f7 18
<> 144:ef7eb2e8f9f7 19 #if DEVICE_RTC
<> 144:ef7eb2e8f9f7 20
<> 160:d5399cc887bb 21 #include "mbed_wait_api.h"
<> 144:ef7eb2e8f9f7 22 #include "mbed_error.h"
<> 144:ef7eb2e8f9f7 23 #include "nu_modutil.h"
<> 144:ef7eb2e8f9f7 24 #include "nu_miscutil.h"
AnnaBridge 167:e84263d55307 25 #include "mbed_mktime.h"
<> 144:ef7eb2e8f9f7 26
AnnaBridge 185:08ed48f1de7f 27 /* Micro seconds per second */
AnnaBridge 185:08ed48f1de7f 28 #define NU_US_PER_SEC 1000000
AnnaBridge 185:08ed48f1de7f 29 /* Timer clock per second
AnnaBridge 185:08ed48f1de7f 30 *
AnnaBridge 185:08ed48f1de7f 31 * NOTE: This dependents on real hardware.
AnnaBridge 185:08ed48f1de7f 32 */
AnnaBridge 185:08ed48f1de7f 33 #define NU_RTCCLK_PER_SEC (__LXT)
AnnaBridge 185:08ed48f1de7f 34
AnnaBridge 185:08ed48f1de7f 35 /* Strategy for implementation of RTC HAL
AnnaBridge 185:08ed48f1de7f 36 *
AnnaBridge 185:08ed48f1de7f 37 * H/W RTC just supports year range 2000~2099, which cannot fully cover POSIX time (starting since 2970)
AnnaBridge 185:08ed48f1de7f 38 * and date time of struct TM (starting since 1900).
AnnaBridge 185:08ed48f1de7f 39 *
AnnaBridge 185:08ed48f1de7f 40 * To conquer the difficulty, we don't use H/W RTC to keep real date time. Instead, we use it to keep
AnnaBridge 185:08ed48f1de7f 41 * elapsed time in seconds since one reference time point. The strategy would be:
AnnaBridge 185:08ed48f1de7f 42 *
AnnaBridge 185:08ed48f1de7f 43 * 1. Choose DATETIME_HWRTC_ORIGIN (00:00:00 UTC, Saturday, 1 January 2000) as reference time point of H/W RTC.
AnnaBridge 185:08ed48f1de7f 44 * 2. t_hwrtc_origin = DATETIME_HWRTC_ORIGIN in POSIX time
AnnaBridge 185:08ed48f1de7f 45 * 3. t_hwrtc_elapsed = t_hwrtc_origin + elapsed time since t_hwrtc_origin
AnnaBridge 185:08ed48f1de7f 46 * 4. t_write = POSIX time set by rtc_write().
AnnaBridge 185:08ed48f1de7f 47 * 5. t_present = rtc_read() = t_write + (t_hwrtc_elapsed - t_hwrtc_origin)
AnnaBridge 185:08ed48f1de7f 48 *
AnnaBridge 185:08ed48f1de7f 49 * 1900
AnnaBridge 185:08ed48f1de7f 50 * |---------------------------------------------------------------------------------|
AnnaBridge 185:08ed48f1de7f 51 * 1970 t_write t_present
AnnaBridge 185:08ed48f1de7f 52 * |---------|-------|-----------------|---------------------------------------------|
AnnaBridge 185:08ed48f1de7f 53 *
AnnaBridge 185:08ed48f1de7f 54 * 2000
AnnaBridge 185:08ed48f1de7f 55 * |-----------------|---------------------------------------------------------------|
AnnaBridge 185:08ed48f1de7f 56 * t_hwrtc_origin t_hwrtc_elapsed
AnnaBridge 185:08ed48f1de7f 57 *
AnnaBridge 185:08ed48f1de7f 58 */
AnnaBridge 185:08ed48f1de7f 59 /* Start year of struct TM*/
AnnaBridge 185:08ed48f1de7f 60 #define NU_TM_YEAR0 1900
AnnaBridge 185:08ed48f1de7f 61 /* Start year of POSIX time (set_time()/time()) */
AnnaBridge 185:08ed48f1de7f 62 #define NU_POSIX_YEAR0 1970
AnnaBridge 185:08ed48f1de7f 63 /* Start year of H/W RTC */
AnnaBridge 185:08ed48f1de7f 64 #define NU_HWRTC_YEAR0 2000
AnnaBridge 185:08ed48f1de7f 65
AnnaBridge 185:08ed48f1de7f 66 /* RTC H/W origin time: 00:00:00 UTC, Saturday, 1 January 2000 */
AnnaBridge 185:08ed48f1de7f 67 static const S_RTC_TIME_DATA_T DATETIME_HWRTC_ORIGIN = {
AnnaBridge 185:08ed48f1de7f 68 2000, /* Year value, range between 2000 ~ 2099 */
AnnaBridge 185:08ed48f1de7f 69 1, /* Month value, range between 1 ~ 12 */
AnnaBridge 185:08ed48f1de7f 70 1, /* Day value, range between 1 ~ 31 */
AnnaBridge 185:08ed48f1de7f 71 RTC_SATURDAY, /* Day of the week */
AnnaBridge 185:08ed48f1de7f 72 0, /* Hour value, range between 0 ~ 23 */
AnnaBridge 185:08ed48f1de7f 73 0, /* Minute value, range between 0 ~ 59 */
AnnaBridge 185:08ed48f1de7f 74 0, /* Second value, range between 0 ~ 59 */
AnnaBridge 185:08ed48f1de7f 75 RTC_CLOCK_24, /* 12-Hour (RTC_CLOCK_12) / 24-Hour (RTC_CLOCK_24) */
AnnaBridge 185:08ed48f1de7f 76 0 /* RTC_AM / RTC_PM (used only for 12-Hour) */
AnnaBridge 185:08ed48f1de7f 77 };
AnnaBridge 185:08ed48f1de7f 78 /* t_hwrtc_origin initialized or not? */
AnnaBridge 185:08ed48f1de7f 79 static bool t_hwrtc_origin_inited = 0;
AnnaBridge 185:08ed48f1de7f 80 /* POSIX time of DATETIME_HWRTC_ORIGIN (since 00:00:00 UTC, Thursday, 1 January 1970) */
AnnaBridge 185:08ed48f1de7f 81 static time_t t_hwrtc_origin = 0;
AnnaBridge 185:08ed48f1de7f 82 /* POSIX time set by rtc_write() */
AnnaBridge 185:08ed48f1de7f 83 static time_t t_write = 0;
AnnaBridge 185:08ed48f1de7f 84 /* Convert date time from H/W RTC to struct TM */
AnnaBridge 185:08ed48f1de7f 85 static void rtc_convert_datetime_hwrtc_to_tm(struct tm *datetime_tm, const S_RTC_TIME_DATA_T *datetime_hwrtc);
<> 144:ef7eb2e8f9f7 86
<> 144:ef7eb2e8f9f7 87 static const struct nu_modinit_s rtc_modinit = {RTC_0, RTC_MODULE, 0, 0, 0, RTC_IRQn, NULL};
<> 144:ef7eb2e8f9f7 88
<> 144:ef7eb2e8f9f7 89 void rtc_init(void)
<> 144:ef7eb2e8f9f7 90 {
AnnaBridge 172:7d866c31b3c5 91 if (rtc_isenabled()) {
<> 144:ef7eb2e8f9f7 92 return;
<> 144:ef7eb2e8f9f7 93 }
AnnaBridge 185:08ed48f1de7f 94
<> 144:ef7eb2e8f9f7 95 RTC_Open(NULL);
AnnaBridge 185:08ed48f1de7f 96
AnnaBridge 185:08ed48f1de7f 97 /* POSIX time origin (00:00:00 UTC, Thursday, 1 January 1970) */
AnnaBridge 185:08ed48f1de7f 98 rtc_write(0);
<> 144:ef7eb2e8f9f7 99 }
<> 144:ef7eb2e8f9f7 100
<> 144:ef7eb2e8f9f7 101 void rtc_free(void)
<> 144:ef7eb2e8f9f7 102 {
AnnaBridge 172:7d866c31b3c5 103 // N/A
<> 144:ef7eb2e8f9f7 104 }
<> 144:ef7eb2e8f9f7 105
<> 144:ef7eb2e8f9f7 106 int rtc_isenabled(void)
<> 144:ef7eb2e8f9f7 107 {
AnnaBridge 172:7d866c31b3c5 108 // NOTE: To access (RTC) registers, clock must be enabled first.
AnnaBridge 172:7d866c31b3c5 109 if (! (CLK->APBCLK0 & CLK_APBCLK0_RTCCKEN_Msk)) {
AnnaBridge 172:7d866c31b3c5 110 // Enable IP clock
AnnaBridge 172:7d866c31b3c5 111 CLK_EnableModuleClock(rtc_modinit.clkidx);
AnnaBridge 172:7d866c31b3c5 112 }
AnnaBridge 172:7d866c31b3c5 113
AnnaBridge 172:7d866c31b3c5 114 // NOTE: Check RTC Init Active flag to support crossing reset cycle.
AnnaBridge 172:7d866c31b3c5 115 return !! (RTC->INIT & RTC_INIT_INIT_Active_Msk);
<> 144:ef7eb2e8f9f7 116 }
<> 144:ef7eb2e8f9f7 117
AnnaBridge 185:08ed48f1de7f 118 time_t rtc_read(void)
AnnaBridge 185:08ed48f1de7f 119 {
AnnaBridge 185:08ed48f1de7f 120 /* NOTE: After boot, RTC time registers are not synced immediately, about 1 sec latency.
AnnaBridge 185:08ed48f1de7f 121 * RTC time got (through RTC_GetDateAndTime()) in this sec would be last-synced and incorrect.
AnnaBridge 185:08ed48f1de7f 122 */
AnnaBridge 185:08ed48f1de7f 123 if (! rtc_isenabled()) {
AnnaBridge 185:08ed48f1de7f 124 rtc_init();
AnnaBridge 185:08ed48f1de7f 125 }
AnnaBridge 185:08ed48f1de7f 126
AnnaBridge 185:08ed48f1de7f 127 /* Used for intermediary between date time of H/W RTC and POSIX time */
AnnaBridge 185:08ed48f1de7f 128 struct tm datetime_tm;
AnnaBridge 185:08ed48f1de7f 129
AnnaBridge 185:08ed48f1de7f 130 if (! t_hwrtc_origin_inited) {
AnnaBridge 185:08ed48f1de7f 131 t_hwrtc_origin_inited = 1;
AnnaBridge 185:08ed48f1de7f 132
AnnaBridge 185:08ed48f1de7f 133 /* Convert date time from H/W RTC to struct TM */
AnnaBridge 185:08ed48f1de7f 134 rtc_convert_datetime_hwrtc_to_tm(&datetime_tm, &DATETIME_HWRTC_ORIGIN);
AnnaBridge 185:08ed48f1de7f 135 /* Convert date time of struct TM to POSIX time */
AnnaBridge 185:08ed48f1de7f 136 if (! _rtc_maketime(&datetime_tm, &t_hwrtc_origin, RTC_FULL_LEAP_YEAR_SUPPORT)) {
AnnaBridge 185:08ed48f1de7f 137 return 0;
AnnaBridge 185:08ed48f1de7f 138 }
AnnaBridge 185:08ed48f1de7f 139 }
AnnaBridge 185:08ed48f1de7f 140
AnnaBridge 185:08ed48f1de7f 141 S_RTC_TIME_DATA_T hwrtc_datetime_2K_present;
AnnaBridge 185:08ed48f1de7f 142 RTC_GetDateAndTime(&hwrtc_datetime_2K_present);
AnnaBridge 185:08ed48f1de7f 143 /* Convert date time from H/W RTC to struct TM */
AnnaBridge 185:08ed48f1de7f 144 rtc_convert_datetime_hwrtc_to_tm(&datetime_tm, &hwrtc_datetime_2K_present);
AnnaBridge 185:08ed48f1de7f 145 /* Convert date time of struct TM to POSIX time */
AnnaBridge 185:08ed48f1de7f 146 time_t t_hwrtc_elapsed;
AnnaBridge 185:08ed48f1de7f 147 if (! _rtc_maketime(&datetime_tm, &t_hwrtc_elapsed, RTC_FULL_LEAP_YEAR_SUPPORT)) {
AnnaBridge 185:08ed48f1de7f 148 return 0;
AnnaBridge 185:08ed48f1de7f 149 }
AnnaBridge 185:08ed48f1de7f 150
AnnaBridge 185:08ed48f1de7f 151 /* Present time in POSIX time */
AnnaBridge 185:08ed48f1de7f 152 time_t t_present = t_write + (t_hwrtc_elapsed - t_hwrtc_origin);
AnnaBridge 185:08ed48f1de7f 153 return t_present;
AnnaBridge 185:08ed48f1de7f 154 }
AnnaBridge 185:08ed48f1de7f 155
AnnaBridge 185:08ed48f1de7f 156 void rtc_write(time_t t)
AnnaBridge 185:08ed48f1de7f 157 {
AnnaBridge 185:08ed48f1de7f 158 if (! rtc_isenabled()) {
AnnaBridge 185:08ed48f1de7f 159 rtc_init();
AnnaBridge 185:08ed48f1de7f 160 }
AnnaBridge 185:08ed48f1de7f 161
AnnaBridge 185:08ed48f1de7f 162 t_write = t;
AnnaBridge 185:08ed48f1de7f 163
AnnaBridge 185:08ed48f1de7f 164 RTC_SetDateAndTime((S_RTC_TIME_DATA_T *) &DATETIME_HWRTC_ORIGIN);
AnnaBridge 185:08ed48f1de7f 165 /* NOTE: When engine is clocked by low power clock source (LXT/LIRC), we need to wait for 3 engine clocks. */
AnnaBridge 185:08ed48f1de7f 166 wait_us((NU_US_PER_SEC / NU_RTCCLK_PER_SEC) * 3);
AnnaBridge 185:08ed48f1de7f 167 }
AnnaBridge 185:08ed48f1de7f 168
<> 144:ef7eb2e8f9f7 169 /*
<> 144:ef7eb2e8f9f7 170 struct tm
<> 144:ef7eb2e8f9f7 171 tm_sec seconds after the minute 0-61
<> 144:ef7eb2e8f9f7 172 tm_min minutes after the hour 0-59
<> 144:ef7eb2e8f9f7 173 tm_hour hours since midnight 0-23
<> 144:ef7eb2e8f9f7 174 tm_mday day of the month 1-31
<> 144:ef7eb2e8f9f7 175 tm_mon months since January 0-11
<> 144:ef7eb2e8f9f7 176 tm_year years since 1900
<> 144:ef7eb2e8f9f7 177 tm_wday days since Sunday 0-6
<> 144:ef7eb2e8f9f7 178 tm_yday days since January 1 0-365
<> 144:ef7eb2e8f9f7 179 tm_isdst Daylight Saving Time flag
<> 144:ef7eb2e8f9f7 180 */
AnnaBridge 185:08ed48f1de7f 181 static void rtc_convert_datetime_hwrtc_to_tm(struct tm *datetime_tm, const S_RTC_TIME_DATA_T *datetime_hwrtc)
<> 144:ef7eb2e8f9f7 182 {
AnnaBridge 185:08ed48f1de7f 183 datetime_tm->tm_year = datetime_hwrtc->u32Year - NU_TM_YEAR0;
AnnaBridge 185:08ed48f1de7f 184 datetime_tm->tm_mon = datetime_hwrtc->u32Month - 1;
AnnaBridge 185:08ed48f1de7f 185 datetime_tm->tm_mday = datetime_hwrtc->u32Day;
AnnaBridge 185:08ed48f1de7f 186 datetime_tm->tm_wday = datetime_hwrtc->u32DayOfWeek;
AnnaBridge 185:08ed48f1de7f 187 datetime_tm->tm_hour = datetime_hwrtc->u32Hour;
AnnaBridge 185:08ed48f1de7f 188 if (datetime_hwrtc->u32TimeScale == RTC_CLOCK_12 && datetime_hwrtc->u32AmPm == RTC_PM) {
AnnaBridge 185:08ed48f1de7f 189 datetime_tm->tm_hour += 12;
AnnaBridge 177:447f873cad2f 190 }
AnnaBridge 185:08ed48f1de7f 191 datetime_tm->tm_min = datetime_hwrtc->u32Minute;
AnnaBridge 185:08ed48f1de7f 192 datetime_tm->tm_sec = datetime_hwrtc->u32Second;
<> 144:ef7eb2e8f9f7 193 }
<> 144:ef7eb2e8f9f7 194
<> 144:ef7eb2e8f9f7 195 #endif