Mouse code for the MacroRat
mbed-dev/targets/TARGET_NUVOTON/TARGET_NUC472/rtc_api.c@18:6a4db94011d3, 2017-05-14 (annotated)
- Committer:
- sahilmgandhi
- Date:
- Sun May 14 23:18:57 2017 +0000
- Revision:
- 18:6a4db94011d3
Publishing again
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sahilmgandhi | 18:6a4db94011d3 | 1 | /* mbed Microcontroller Library |
sahilmgandhi | 18:6a4db94011d3 | 2 | * Copyright (c) 2015-2016 Nuvoton |
sahilmgandhi | 18:6a4db94011d3 | 3 | * |
sahilmgandhi | 18:6a4db94011d3 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sahilmgandhi | 18:6a4db94011d3 | 5 | * you may not use this file except in compliance with the License. |
sahilmgandhi | 18:6a4db94011d3 | 6 | * You may obtain a copy of the License at |
sahilmgandhi | 18:6a4db94011d3 | 7 | * |
sahilmgandhi | 18:6a4db94011d3 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
sahilmgandhi | 18:6a4db94011d3 | 9 | * |
sahilmgandhi | 18:6a4db94011d3 | 10 | * Unless required by applicable law or agreed to in writing, software |
sahilmgandhi | 18:6a4db94011d3 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
sahilmgandhi | 18:6a4db94011d3 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sahilmgandhi | 18:6a4db94011d3 | 13 | * See the License for the specific language governing permissions and |
sahilmgandhi | 18:6a4db94011d3 | 14 | * limitations under the License. |
sahilmgandhi | 18:6a4db94011d3 | 15 | */ |
sahilmgandhi | 18:6a4db94011d3 | 16 | |
sahilmgandhi | 18:6a4db94011d3 | 17 | #include "rtc_api.h" |
sahilmgandhi | 18:6a4db94011d3 | 18 | |
sahilmgandhi | 18:6a4db94011d3 | 19 | #if DEVICE_RTC |
sahilmgandhi | 18:6a4db94011d3 | 20 | |
sahilmgandhi | 18:6a4db94011d3 | 21 | #include "mbed_wait_api.h" |
sahilmgandhi | 18:6a4db94011d3 | 22 | #include "mbed_error.h" |
sahilmgandhi | 18:6a4db94011d3 | 23 | #include "nu_modutil.h" |
sahilmgandhi | 18:6a4db94011d3 | 24 | #include "nu_miscutil.h" |
sahilmgandhi | 18:6a4db94011d3 | 25 | |
sahilmgandhi | 18:6a4db94011d3 | 26 | #define YEAR0 1900 |
sahilmgandhi | 18:6a4db94011d3 | 27 | //#define EPOCH_YR 1970 |
sahilmgandhi | 18:6a4db94011d3 | 28 | static int rtc_inited = 0; |
sahilmgandhi | 18:6a4db94011d3 | 29 | |
sahilmgandhi | 18:6a4db94011d3 | 30 | static const struct nu_modinit_s rtc_modinit = {RTC_0, RTC_MODULE, 0, 0, 0, RTC_IRQn, NULL}; |
sahilmgandhi | 18:6a4db94011d3 | 31 | |
sahilmgandhi | 18:6a4db94011d3 | 32 | void rtc_init(void) |
sahilmgandhi | 18:6a4db94011d3 | 33 | { |
sahilmgandhi | 18:6a4db94011d3 | 34 | if (rtc_inited) { |
sahilmgandhi | 18:6a4db94011d3 | 35 | return; |
sahilmgandhi | 18:6a4db94011d3 | 36 | } |
sahilmgandhi | 18:6a4db94011d3 | 37 | rtc_inited = 1; |
sahilmgandhi | 18:6a4db94011d3 | 38 | |
sahilmgandhi | 18:6a4db94011d3 | 39 | // Enable IP clock |
sahilmgandhi | 18:6a4db94011d3 | 40 | CLK_EnableModuleClock(rtc_modinit.clkidx); |
sahilmgandhi | 18:6a4db94011d3 | 41 | |
sahilmgandhi | 18:6a4db94011d3 | 42 | RTC_Open(NULL); |
sahilmgandhi | 18:6a4db94011d3 | 43 | } |
sahilmgandhi | 18:6a4db94011d3 | 44 | |
sahilmgandhi | 18:6a4db94011d3 | 45 | void rtc_free(void) |
sahilmgandhi | 18:6a4db94011d3 | 46 | { |
sahilmgandhi | 18:6a4db94011d3 | 47 | // FIXME |
sahilmgandhi | 18:6a4db94011d3 | 48 | } |
sahilmgandhi | 18:6a4db94011d3 | 49 | |
sahilmgandhi | 18:6a4db94011d3 | 50 | int rtc_isenabled(void) |
sahilmgandhi | 18:6a4db94011d3 | 51 | { |
sahilmgandhi | 18:6a4db94011d3 | 52 | return rtc_inited; |
sahilmgandhi | 18:6a4db94011d3 | 53 | } |
sahilmgandhi | 18:6a4db94011d3 | 54 | |
sahilmgandhi | 18:6a4db94011d3 | 55 | /* |
sahilmgandhi | 18:6a4db94011d3 | 56 | struct tm |
sahilmgandhi | 18:6a4db94011d3 | 57 | tm_sec seconds after the minute 0-61 |
sahilmgandhi | 18:6a4db94011d3 | 58 | tm_min minutes after the hour 0-59 |
sahilmgandhi | 18:6a4db94011d3 | 59 | tm_hour hours since midnight 0-23 |
sahilmgandhi | 18:6a4db94011d3 | 60 | tm_mday day of the month 1-31 |
sahilmgandhi | 18:6a4db94011d3 | 61 | tm_mon months since January 0-11 |
sahilmgandhi | 18:6a4db94011d3 | 62 | tm_year years since 1900 |
sahilmgandhi | 18:6a4db94011d3 | 63 | tm_wday days since Sunday 0-6 |
sahilmgandhi | 18:6a4db94011d3 | 64 | tm_yday days since January 1 0-365 |
sahilmgandhi | 18:6a4db94011d3 | 65 | tm_isdst Daylight Saving Time flag |
sahilmgandhi | 18:6a4db94011d3 | 66 | */ |
sahilmgandhi | 18:6a4db94011d3 | 67 | |
sahilmgandhi | 18:6a4db94011d3 | 68 | time_t rtc_read(void) |
sahilmgandhi | 18:6a4db94011d3 | 69 | { |
sahilmgandhi | 18:6a4db94011d3 | 70 | if (! rtc_inited) { |
sahilmgandhi | 18:6a4db94011d3 | 71 | rtc_init(); |
sahilmgandhi | 18:6a4db94011d3 | 72 | } |
sahilmgandhi | 18:6a4db94011d3 | 73 | |
sahilmgandhi | 18:6a4db94011d3 | 74 | S_RTC_TIME_DATA_T rtc_datetime; |
sahilmgandhi | 18:6a4db94011d3 | 75 | RTC_GetDateAndTime(&rtc_datetime); |
sahilmgandhi | 18:6a4db94011d3 | 76 | |
sahilmgandhi | 18:6a4db94011d3 | 77 | struct tm timeinfo; |
sahilmgandhi | 18:6a4db94011d3 | 78 | |
sahilmgandhi | 18:6a4db94011d3 | 79 | // Convert struct tm to S_RTC_TIME_DATA_T |
sahilmgandhi | 18:6a4db94011d3 | 80 | timeinfo.tm_year = rtc_datetime.u32Year - YEAR0; |
sahilmgandhi | 18:6a4db94011d3 | 81 | timeinfo.tm_mon = rtc_datetime.u32Month - 1; |
sahilmgandhi | 18:6a4db94011d3 | 82 | timeinfo.tm_mday = rtc_datetime.u32Day; |
sahilmgandhi | 18:6a4db94011d3 | 83 | timeinfo.tm_wday = rtc_datetime.u32DayOfWeek; |
sahilmgandhi | 18:6a4db94011d3 | 84 | timeinfo.tm_hour = rtc_datetime.u32Hour; |
sahilmgandhi | 18:6a4db94011d3 | 85 | timeinfo.tm_min = rtc_datetime.u32Minute; |
sahilmgandhi | 18:6a4db94011d3 | 86 | timeinfo.tm_sec = rtc_datetime.u32Second; |
sahilmgandhi | 18:6a4db94011d3 | 87 | |
sahilmgandhi | 18:6a4db94011d3 | 88 | // Convert to timestamp |
sahilmgandhi | 18:6a4db94011d3 | 89 | time_t t = mktime(&timeinfo); |
sahilmgandhi | 18:6a4db94011d3 | 90 | |
sahilmgandhi | 18:6a4db94011d3 | 91 | return t; |
sahilmgandhi | 18:6a4db94011d3 | 92 | } |
sahilmgandhi | 18:6a4db94011d3 | 93 | |
sahilmgandhi | 18:6a4db94011d3 | 94 | void rtc_write(time_t t) |
sahilmgandhi | 18:6a4db94011d3 | 95 | { |
sahilmgandhi | 18:6a4db94011d3 | 96 | if (! rtc_inited) { |
sahilmgandhi | 18:6a4db94011d3 | 97 | rtc_init(); |
sahilmgandhi | 18:6a4db94011d3 | 98 | } |
sahilmgandhi | 18:6a4db94011d3 | 99 | |
sahilmgandhi | 18:6a4db94011d3 | 100 | // Convert timestamp to struct tm |
sahilmgandhi | 18:6a4db94011d3 | 101 | struct tm *timeinfo = localtime(&t); |
sahilmgandhi | 18:6a4db94011d3 | 102 | |
sahilmgandhi | 18:6a4db94011d3 | 103 | S_RTC_TIME_DATA_T rtc_datetime; |
sahilmgandhi | 18:6a4db94011d3 | 104 | |
sahilmgandhi | 18:6a4db94011d3 | 105 | // Convert S_RTC_TIME_DATA_T to struct tm |
sahilmgandhi | 18:6a4db94011d3 | 106 | rtc_datetime.u32Year = timeinfo->tm_year + YEAR0; |
sahilmgandhi | 18:6a4db94011d3 | 107 | rtc_datetime.u32Month = timeinfo->tm_mon + 1; |
sahilmgandhi | 18:6a4db94011d3 | 108 | rtc_datetime.u32Day = timeinfo->tm_mday; |
sahilmgandhi | 18:6a4db94011d3 | 109 | rtc_datetime.u32DayOfWeek = timeinfo->tm_wday; |
sahilmgandhi | 18:6a4db94011d3 | 110 | rtc_datetime.u32Hour = timeinfo->tm_hour; |
sahilmgandhi | 18:6a4db94011d3 | 111 | rtc_datetime.u32Minute = timeinfo->tm_min; |
sahilmgandhi | 18:6a4db94011d3 | 112 | rtc_datetime.u32Second = timeinfo->tm_sec; |
sahilmgandhi | 18:6a4db94011d3 | 113 | rtc_datetime.u32TimeScale = RTC_CLOCK_24; |
sahilmgandhi | 18:6a4db94011d3 | 114 | |
sahilmgandhi | 18:6a4db94011d3 | 115 | // NOTE: Timing issue with write to RTC registers. This delay is empirical, not rational. |
sahilmgandhi | 18:6a4db94011d3 | 116 | RTC_SetDateAndTime(&rtc_datetime); |
sahilmgandhi | 18:6a4db94011d3 | 117 | //nu_nop(6000); |
sahilmgandhi | 18:6a4db94011d3 | 118 | wait_us(100); |
sahilmgandhi | 18:6a4db94011d3 | 119 | } |
sahilmgandhi | 18:6a4db94011d3 | 120 | |
sahilmgandhi | 18:6a4db94011d3 | 121 | #endif |