mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
targets/TARGET_NUVOTON/TARGET_NUC472/rtc_api.c@160:d5399cc887bb, 2017-03-14 (annotated)
- Committer:
- <>
- Date:
- Tue Mar 14 16:40:56 2017 +0000
- Revision:
- 160:d5399cc887bb
- Parent:
- 149:156823d33999
- Child:
- 167:e84263d55307
This updates the lib to the mbed lib v138
Who changed what in which revision?
User | Revision | Line number | New 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" |
<> | 144:ef7eb2e8f9f7 | 25 | |
<> | 144:ef7eb2e8f9f7 | 26 | #define YEAR0 1900 |
<> | 144:ef7eb2e8f9f7 | 27 | //#define EPOCH_YR 1970 |
<> | 144:ef7eb2e8f9f7 | 28 | static int rtc_inited = 0; |
<> | 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 | { |
<> | 144:ef7eb2e8f9f7 | 34 | if (rtc_inited) { |
<> | 144:ef7eb2e8f9f7 | 35 | return; |
<> | 144:ef7eb2e8f9f7 | 36 | } |
<> | 144:ef7eb2e8f9f7 | 37 | rtc_inited = 1; |
<> | 144:ef7eb2e8f9f7 | 38 | |
<> | 144:ef7eb2e8f9f7 | 39 | // Enable IP clock |
<> | 144:ef7eb2e8f9f7 | 40 | CLK_EnableModuleClock(rtc_modinit.clkidx); |
<> | 144:ef7eb2e8f9f7 | 41 | |
<> | 144:ef7eb2e8f9f7 | 42 | RTC_Open(NULL); |
<> | 144:ef7eb2e8f9f7 | 43 | } |
<> | 144:ef7eb2e8f9f7 | 44 | |
<> | 144:ef7eb2e8f9f7 | 45 | void rtc_free(void) |
<> | 144:ef7eb2e8f9f7 | 46 | { |
<> | 144:ef7eb2e8f9f7 | 47 | // FIXME |
<> | 144:ef7eb2e8f9f7 | 48 | } |
<> | 144:ef7eb2e8f9f7 | 49 | |
<> | 144:ef7eb2e8f9f7 | 50 | int rtc_isenabled(void) |
<> | 144:ef7eb2e8f9f7 | 51 | { |
<> | 144:ef7eb2e8f9f7 | 52 | return rtc_inited; |
<> | 144:ef7eb2e8f9f7 | 53 | } |
<> | 144:ef7eb2e8f9f7 | 54 | |
<> | 144:ef7eb2e8f9f7 | 55 | /* |
<> | 144:ef7eb2e8f9f7 | 56 | struct tm |
<> | 144:ef7eb2e8f9f7 | 57 | tm_sec seconds after the minute 0-61 |
<> | 144:ef7eb2e8f9f7 | 58 | tm_min minutes after the hour 0-59 |
<> | 144:ef7eb2e8f9f7 | 59 | tm_hour hours since midnight 0-23 |
<> | 144:ef7eb2e8f9f7 | 60 | tm_mday day of the month 1-31 |
<> | 144:ef7eb2e8f9f7 | 61 | tm_mon months since January 0-11 |
<> | 144:ef7eb2e8f9f7 | 62 | tm_year years since 1900 |
<> | 144:ef7eb2e8f9f7 | 63 | tm_wday days since Sunday 0-6 |
<> | 144:ef7eb2e8f9f7 | 64 | tm_yday days since January 1 0-365 |
<> | 144:ef7eb2e8f9f7 | 65 | tm_isdst Daylight Saving Time flag |
<> | 144:ef7eb2e8f9f7 | 66 | */ |
<> | 144:ef7eb2e8f9f7 | 67 | |
<> | 144:ef7eb2e8f9f7 | 68 | time_t rtc_read(void) |
<> | 144:ef7eb2e8f9f7 | 69 | { |
<> | 144:ef7eb2e8f9f7 | 70 | if (! rtc_inited) { |
<> | 144:ef7eb2e8f9f7 | 71 | rtc_init(); |
<> | 144:ef7eb2e8f9f7 | 72 | } |
<> | 144:ef7eb2e8f9f7 | 73 | |
<> | 144:ef7eb2e8f9f7 | 74 | S_RTC_TIME_DATA_T rtc_datetime; |
<> | 144:ef7eb2e8f9f7 | 75 | RTC_GetDateAndTime(&rtc_datetime); |
<> | 144:ef7eb2e8f9f7 | 76 | |
<> | 144:ef7eb2e8f9f7 | 77 | struct tm timeinfo; |
<> | 144:ef7eb2e8f9f7 | 78 | |
<> | 144:ef7eb2e8f9f7 | 79 | // Convert struct tm to S_RTC_TIME_DATA_T |
<> | 144:ef7eb2e8f9f7 | 80 | timeinfo.tm_year = rtc_datetime.u32Year - YEAR0; |
<> | 144:ef7eb2e8f9f7 | 81 | timeinfo.tm_mon = rtc_datetime.u32Month - 1; |
<> | 144:ef7eb2e8f9f7 | 82 | timeinfo.tm_mday = rtc_datetime.u32Day; |
<> | 144:ef7eb2e8f9f7 | 83 | timeinfo.tm_wday = rtc_datetime.u32DayOfWeek; |
<> | 144:ef7eb2e8f9f7 | 84 | timeinfo.tm_hour = rtc_datetime.u32Hour; |
<> | 144:ef7eb2e8f9f7 | 85 | timeinfo.tm_min = rtc_datetime.u32Minute; |
<> | 144:ef7eb2e8f9f7 | 86 | timeinfo.tm_sec = rtc_datetime.u32Second; |
<> | 144:ef7eb2e8f9f7 | 87 | |
<> | 144:ef7eb2e8f9f7 | 88 | // Convert to timestamp |
<> | 144:ef7eb2e8f9f7 | 89 | time_t t = mktime(&timeinfo); |
<> | 144:ef7eb2e8f9f7 | 90 | |
<> | 144:ef7eb2e8f9f7 | 91 | return t; |
<> | 144:ef7eb2e8f9f7 | 92 | } |
<> | 144:ef7eb2e8f9f7 | 93 | |
<> | 144:ef7eb2e8f9f7 | 94 | void rtc_write(time_t t) |
<> | 144:ef7eb2e8f9f7 | 95 | { |
<> | 144:ef7eb2e8f9f7 | 96 | if (! rtc_inited) { |
<> | 144:ef7eb2e8f9f7 | 97 | rtc_init(); |
<> | 144:ef7eb2e8f9f7 | 98 | } |
<> | 144:ef7eb2e8f9f7 | 99 | |
<> | 144:ef7eb2e8f9f7 | 100 | // Convert timestamp to struct tm |
<> | 144:ef7eb2e8f9f7 | 101 | struct tm *timeinfo = localtime(&t); |
<> | 144:ef7eb2e8f9f7 | 102 | |
<> | 144:ef7eb2e8f9f7 | 103 | S_RTC_TIME_DATA_T rtc_datetime; |
<> | 144:ef7eb2e8f9f7 | 104 | |
<> | 144:ef7eb2e8f9f7 | 105 | // Convert S_RTC_TIME_DATA_T to struct tm |
<> | 144:ef7eb2e8f9f7 | 106 | rtc_datetime.u32Year = timeinfo->tm_year + YEAR0; |
<> | 144:ef7eb2e8f9f7 | 107 | rtc_datetime.u32Month = timeinfo->tm_mon + 1; |
<> | 144:ef7eb2e8f9f7 | 108 | rtc_datetime.u32Day = timeinfo->tm_mday; |
<> | 144:ef7eb2e8f9f7 | 109 | rtc_datetime.u32DayOfWeek = timeinfo->tm_wday; |
<> | 144:ef7eb2e8f9f7 | 110 | rtc_datetime.u32Hour = timeinfo->tm_hour; |
<> | 144:ef7eb2e8f9f7 | 111 | rtc_datetime.u32Minute = timeinfo->tm_min; |
<> | 144:ef7eb2e8f9f7 | 112 | rtc_datetime.u32Second = timeinfo->tm_sec; |
<> | 144:ef7eb2e8f9f7 | 113 | rtc_datetime.u32TimeScale = RTC_CLOCK_24; |
<> | 144:ef7eb2e8f9f7 | 114 | |
<> | 144:ef7eb2e8f9f7 | 115 | // NOTE: Timing issue with write to RTC registers. This delay is empirical, not rational. |
<> | 144:ef7eb2e8f9f7 | 116 | RTC_SetDateAndTime(&rtc_datetime); |
<> | 144:ef7eb2e8f9f7 | 117 | //nu_nop(6000); |
<> | 144:ef7eb2e8f9f7 | 118 | wait_us(100); |
<> | 144:ef7eb2e8f9f7 | 119 | } |
<> | 144:ef7eb2e8f9f7 | 120 | |
<> | 144:ef7eb2e8f9f7 | 121 | #endif |