]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC43XX/rtc_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_NXP / TARGET_LPC43XX / rtc_api.c
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 ARM Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * Ported to NXP LPC43XX by Micromint USA <support@micromint.com>
17  */
18 #include "rtc_api.h"
19
20 // ensure rtc is running (unchanged if already running)
21
22 /* Setup the RTC based on a time structure, ensuring RTC is enabled
23  *
24  * Can be clocked by a 32.768KHz oscillator or prescale divider based on the APB clock
25  * - We want to use the 32khz clock, allowing for sleep mode
26  *
27  * Most registers are not changed by a Reset
28  * - We must initialize these registers between power-on and setting the RTC into operation
29
30  * Clock Control Register
31  *  RTC_CCR[0] : Enable - 0 = Disabled, 1 = Enabled
32  *  RTC_CCR[1] : Reset - 0 = Normal, 1 = Reset
33  *  RTC_CCR[4] : Clock Source - 0 = Prescaler, 1 = 32k Xtal
34  *
35  * The RTC may already be running, so we should set it up
36  * without impacting if it is the case
37  */
38
39 void rtc_init(void) {
40     // Return, if already enabled
41     if (LPC_RTC->CCR & 1)
42         return;
43
44     // Enable 1kHz output of 32kHz oscillator
45     LPC_CREG->CREG0 &= ~((1 << 3) | (1 << 2));
46     LPC_CREG->CREG0 |= (0x03 << 6) | (1 << 1) | (1 << 0);
47
48     // Enable RTC
49     do {
50         LPC_RTC->CCR |= 1 << 0;
51     } while ((LPC_RTC->CCR & 1) == 0);
52 }
53
54 void rtc_free(void) {
55     // [TODO]
56 }
57
58 /*
59  * Little check routine to see if the RTC has been enabled
60  *
61  * Clock Control Register
62  *  RTC_CCR[0] : 0 = Disabled, 1 = Enabled
63  *
64  */
65 int rtc_isenabled(void) {
66     return(((LPC_RTC->CCR) & 0x01) != 0);
67 }
68
69 /*
70  * RTC Registers
71  *  RTC_SEC        Seconds 0-59
72  *  RTC_MIN        Minutes 0-59
73  *  RTC_HOUR    Hour 0-23
74  *  RTC_DOM        Day of Month 1-28..31
75  *  RTC_DOW        Day of Week 0-6
76  *  RTC_DOY        Day of Year 1-365
77  *  RTC_MONTH    Month 1-12
78  *  RTC_YEAR    Year 0-4095
79  *
80  * struct tm
81  *  tm_sec        seconds after the minute 0-61
82  *  tm_min        minutes after the hour 0-59
83  *  tm_hour        hours since midnight 0-23
84  *  tm_mday        day of the month 1-31
85  *  tm_mon        months since January 0-11
86  *  tm_year        years since 1900
87  *  tm_wday        days since Sunday 0-6
88  *  tm_yday        days since January 1 0-365
89  *  tm_isdst    Daylight Saving Time flag
90  */
91 time_t rtc_read(void) {
92     // Setup a tm structure based on the RTC
93     struct tm timeinfo;
94     timeinfo.tm_sec = LPC_RTC->TIME[RTC_TIMETYPE_SECOND];
95     timeinfo.tm_min = LPC_RTC->TIME[RTC_TIMETYPE_MINUTE];
96     timeinfo.tm_hour = LPC_RTC->TIME[RTC_TIMETYPE_HOUR];
97     timeinfo.tm_mday = LPC_RTC->TIME[RTC_TIMETYPE_DAYOFMONTH];
98     timeinfo.tm_wday = LPC_RTC->TIME[RTC_TIMETYPE_DAYOFWEEK];
99     timeinfo.tm_yday = LPC_RTC->TIME[RTC_TIMETYPE_DAYOFYEAR];
100     timeinfo.tm_mon = LPC_RTC->TIME[RTC_TIMETYPE_MONTH] - 1;
101     timeinfo.tm_year = LPC_RTC->TIME[RTC_TIMETYPE_YEAR] - 1900;
102     
103     // Convert to timestamp
104     time_t t = mktime(&timeinfo);
105     
106     return t;
107 }
108
109 void rtc_write(time_t t) {
110     // Convert the time in to a tm
111     struct tm *timeinfo = localtime(&t);
112     
113     // Pause clock, and clear counter register (clears us count)
114     LPC_RTC->CCR |= 2;
115     
116     // Set the RTC
117     LPC_RTC->TIME[RTC_TIMETYPE_SECOND] = timeinfo->tm_sec;
118     LPC_RTC->TIME[RTC_TIMETYPE_MINUTE] = timeinfo->tm_min;
119     LPC_RTC->TIME[RTC_TIMETYPE_HOUR] = timeinfo->tm_hour;
120     LPC_RTC->TIME[RTC_TIMETYPE_DAYOFMONTH] = timeinfo->tm_mday;
121     LPC_RTC->TIME[RTC_TIMETYPE_DAYOFWEEK] = timeinfo->tm_wday;
122     LPC_RTC->TIME[RTC_TIMETYPE_DAYOFYEAR] = timeinfo->tm_yday;
123     LPC_RTC->TIME[RTC_TIMETYPE_MONTH] = timeinfo->tm_mon + 1;
124     LPC_RTC->TIME[RTC_TIMETYPE_YEAR] = timeinfo->tm_year + 1900;
125     
126     // Restart clock
127     LPC_RTC->CCR &= ~((uint32_t)2);
128 }