]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC176X/rtc_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_NXP / TARGET_LPC176X / 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 #include "rtc_api.h"
17
18 // ensure rtc is running (unchanged if already running)
19
20 /* Setup the RTC based on a time structure, ensuring RTC is enabled
21  *
22  * Can be clocked by a 32.768KHz oscillator or prescale divider based on the APB clock
23  * - We want to use the 32khz clock, allowing for sleep mode
24  *
25  * Most registers are not changed by a Reset
26  * - We must initialize these registers between power-on and setting the RTC into operation
27
28  * Clock Control Register
29  *  RTC_CCR[0] : Enable - 0 = Disabled, 1 = Enabled
30  *  RTC_CCR[1] : Reset - 0 = Normal, 1 = Reset
31  *  RTC_CCR[4] : Clock Source - 0 = Prescaler, 1 = 32k Xtal
32  *
33  * The RTC may already be running, so we should set it up
34  * without impacting if it is the case
35  */
36 void rtc_init(void) {
37     LPC_SC->PCONP |= 0x200; // Ensure power is on
38     LPC_RTC->CCR = 0x00;
39     
40     LPC_RTC->CCR |= 1 << 0; // Ensure the RTC is enabled
41 }
42
43 void rtc_free(void) {
44     // [TODO]
45 }
46
47 /*
48  * Little check routine to see if the RTC has been enabled
49  *
50  * Clock Control Register
51  *  RTC_CCR[0] : 0 = Disabled, 1 = Enabled
52  *
53  */
54 int rtc_isenabled(void) {
55     return(((LPC_RTC->CCR) & 0x01) != 0);
56 }
57
58 /*
59  * RTC Registers
60  *  RTC_SEC        Seconds 0-59
61  *  RTC_MIN        Minutes 0-59
62  *  RTC_HOUR    Hour 0-23
63  *  RTC_DOM        Day of Month 1-28..31
64  *  RTC_DOW        Day of Week 0-6
65  *  RTC_DOY        Day of Year 1-365
66  *  RTC_MONTH    Month 1-12
67  *  RTC_YEAR    Year 0-4095
68  *
69  * struct tm
70  *  tm_sec        seconds after the minute 0-61
71  *  tm_min        minutes after the hour 0-59
72  *  tm_hour        hours since midnight 0-23
73  *  tm_mday        day of the month 1-31
74  *  tm_mon        months since January 0-11
75  *  tm_year        years since 1900
76  *  tm_wday        days since Sunday 0-6
77  *  tm_yday        days since January 1 0-365
78  *  tm_isdst    Daylight Saving Time flag
79  */
80 time_t rtc_read(void) {
81     // Setup a tm structure based on the RTC
82     struct tm timeinfo;
83     timeinfo.tm_sec = LPC_RTC->SEC;
84     timeinfo.tm_min = LPC_RTC->MIN;
85     timeinfo.tm_hour = LPC_RTC->HOUR;
86     timeinfo.tm_mday = LPC_RTC->DOM;
87     timeinfo.tm_mon = LPC_RTC->MONTH - 1;
88     timeinfo.tm_year = LPC_RTC->YEAR - 1900;
89     
90     // Convert to timestamp
91     time_t t = mktime(&timeinfo);
92     
93     return t;
94 }
95
96 void rtc_write(time_t t) {
97     // Convert the time in to a tm
98     struct tm *timeinfo = localtime(&t);
99     
100     // Pause clock, and clear counter register (clears us count)
101     LPC_RTC->CCR |= 2;
102     
103     // Set the RTC
104     LPC_RTC->SEC = timeinfo->tm_sec;
105     LPC_RTC->MIN = timeinfo->tm_min;
106     LPC_RTC->HOUR = timeinfo->tm_hour;
107     LPC_RTC->DOM = timeinfo->tm_mday;
108     LPC_RTC->MONTH = timeinfo->tm_mon + 1;
109     LPC_RTC->YEAR = timeinfo->tm_year + 1900;
110     
111     // Restart clock
112     LPC_RTC->CCR &= ~((uint32_t)2);
113 }