helpfor studient

Dependents:   STM32_F103-C8T6basecanblink_led

Fork of mbed-dev by mbed official

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

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