Update to new build system.
[clinton/Smoothieware.git] / mbed / src / vendor / NXP / capi / 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 #if DEVICE_RTC
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 void rtc_init(void) {
39 LPC_SC->PCONP |= 0x200; // Ensure power is on
40 LPC_RTC->CCR = 0x00;
41
42 // clock source on 2368 is special test mode on 1768!
43 #ifdef TARGET_LPC2368
44 LPC_RTC->CCR |= 1 << 4; // Ensure clock source is 32KHz Xtal
45 #endif
46
47 LPC_RTC->CCR |= 1 << 0; // Ensure the RTC is enabled
48 }
49
50 void rtc_free(void) {
51 // [TODO]
52 }
53
54 /*
55 * Little check routine to see if the RTC has been enabled
56 *
57 * Clock Control Register
58 * RTC_CCR[0] : 0 = Disabled, 1 = Enabled
59 *
60 */
61
62 int rtc_isenabled(void) {
63 return(((LPC_RTC->CCR) & 0x01) != 0);
64 }
65
66 /*
67 * RTC Registers
68 * RTC_SEC Seconds 0-59
69 * RTC_MIN Minutes 0-59
70 * RTC_HOUR Hour 0-23
71 * RTC_DOM Day of Month 1-28..31
72 * RTC_DOW Day of Week 0-6
73 * RTC_DOY Day of Year 1-365
74 * RTC_MONTH Month 1-12
75 * RTC_YEAR Year 0-4095
76 *
77 * struct tm
78 * tm_sec seconds after the minute 0-61
79 * tm_min minutes after the hour 0-59
80 * tm_hour hours since midnight 0-23
81 * tm_mday day of the month 1-31
82 * tm_mon months since January 0-11
83 * tm_year years since 1900
84 * tm_wday days since Sunday 0-6
85 * tm_yday days since January 1 0-365
86 * tm_isdst Daylight Saving Time flag
87 */
88 time_t rtc_read(void) {
89 // Setup a tm structure based on the RTC
90 struct tm timeinfo;
91 timeinfo.tm_sec = LPC_RTC->SEC;
92 timeinfo.tm_min = LPC_RTC->MIN;
93 timeinfo.tm_hour = LPC_RTC->HOUR;
94 timeinfo.tm_mday = LPC_RTC->DOM;
95 timeinfo.tm_mon = LPC_RTC->MONTH - 1;
96 timeinfo.tm_year = LPC_RTC->YEAR - 1900;
97
98 // Convert to timestamp
99 time_t t = mktime(&timeinfo);
100
101 return t;
102 }
103
104 void rtc_write(time_t t) {
105 // Convert the time in to a tm
106 struct tm *timeinfo = localtime(&t);
107
108 // Pause clock, and clear counter register (clears us count)
109 LPC_RTC->CCR |= 2;
110
111 // Set the RTC
112 LPC_RTC->SEC = timeinfo->tm_sec;
113 LPC_RTC->MIN = timeinfo->tm_min;
114 LPC_RTC->HOUR = timeinfo->tm_hour;
115 LPC_RTC->DOM = timeinfo->tm_mday;
116 LPC_RTC->MONTH = timeinfo->tm_mon + 1;
117 LPC_RTC->YEAR = timeinfo->tm_year + 1900;
118
119 // Restart clock
120 LPC_RTC->CCR &= ~((uint32_t)2);
121 }
122
123 #endif