mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
shaoziyang
Date:
Sat Sep 13 14:25:46 2014 +0000
Revision:
323:9e901b0a5aa1
Parent:
15:4892fe388435
test with CLOCK_SETUP = 0

Who changed what in which revision?

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