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