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