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