mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by mbed official

Committer:
AnnaBridge
Date:
Wed Jun 21 17:46:44 2017 +0100
Revision:
167:e84263d55307
Parent:
160:d5399cc887bb
Child:
172:7d866c31b3c5
This updates the lib to the mbed lib v 145

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