fix for mbed lib issue 3 (i2c problem) see also https://mbed.org/users/mbed_official/code/mbed/issues/3 affected implementations: LPC812, LPC11U24, LPC1768, LPC2368, LPC4088

Fork of mbed-src by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers rtc_api.c Source File

rtc_api.c

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "rtc_api.h"
00017 
00018 // ensure rtc is running (unchanged if already running)
00019 
00020 /* Setup the RTC based on a time structure, ensuring RTC is enabled
00021  *
00022  * Can be clocked by a 32.768KHz oscillator or prescale divider based on the APB clock
00023  * - We want to use the 32khz clock, allowing for sleep mode
00024  *
00025  * Most registers are not changed by a Reset
00026  * - We must initialize these registers between power-on and setting the RTC into operation
00027 
00028  * Clock Control Register
00029  *  RTC_CCR[0] : Enable - 0 = Disabled, 1 = Enabled
00030  *  RTC_CCR[1] : Reset - 0 = Normal, 1 = Reset
00031  *  RTC_CCR[4] : Clock Source - 0 = Prescaler, 1 = 32k Xtal
00032  *
00033  * The RTC may already be running, so we should set it up
00034  * without impacting if it is the case
00035  */
00036 void rtc_init(void) {
00037     LPC_SC->PCONP |= 0x200; // Ensure power is on
00038     LPC_RTC->CCR = 0x00;
00039     
00040     LPC_RTC->CCR |= 1 << 0; // Ensure the RTC is enabled
00041 }
00042 
00043 void rtc_free(void) {
00044     // [TODO]
00045 }
00046 
00047 /*
00048  * Little check routine to see if the RTC has been enabled
00049  *
00050  * Clock Control Register
00051  *  RTC_CCR[0] : 0 = Disabled, 1 = Enabled
00052  *
00053  */
00054 int rtc_isenabled(void) {
00055     return(((LPC_RTC->CCR) & 0x01) != 0);
00056 }
00057 
00058 /*
00059  * RTC Registers
00060  *  RTC_SEC        Seconds 0-59
00061  *  RTC_MIN        Minutes 0-59
00062  *  RTC_HOUR    Hour 0-23
00063  *  RTC_DOM        Day of Month 1-28..31
00064  *  RTC_DOW        Day of Week 0-6
00065  *  RTC_DOY        Day of Year 1-365
00066  *  RTC_MONTH    Month 1-12
00067  *  RTC_YEAR    Year 0-4095
00068  *
00069  * struct tm
00070  *  tm_sec        seconds after the minute 0-61
00071  *  tm_min        minutes after the hour 0-59
00072  *  tm_hour        hours since midnight 0-23
00073  *  tm_mday        day of the month 1-31
00074  *  tm_mon        months since January 0-11
00075  *  tm_year        years since 1900
00076  *  tm_wday        days since Sunday 0-6
00077  *  tm_yday        days since January 1 0-365
00078  *  tm_isdst    Daylight Saving Time flag
00079  */
00080 time_t rtc_read(void) {
00081     // Setup a tm structure based on the RTC
00082     struct tm timeinfo;
00083     timeinfo.tm_sec = LPC_RTC->SEC;
00084     timeinfo.tm_min = LPC_RTC->MIN;
00085     timeinfo.tm_hour = LPC_RTC->HOUR;
00086     timeinfo.tm_mday = LPC_RTC->DOM;
00087     timeinfo.tm_mon = LPC_RTC->MONTH - 1;
00088     timeinfo.tm_year = LPC_RTC->YEAR - 1900;
00089     
00090     // Convert to timestamp
00091     time_t t = mktime(&timeinfo);
00092     
00093     return t;
00094 }
00095 
00096 void rtc_write(time_t t) {
00097     // Convert the time in to a tm
00098     struct tm *timeinfo = localtime(&t);
00099     
00100     // Pause clock, and clear counter register (clears us count)
00101     LPC_RTC->CCR |= 2;
00102     
00103     // Set the RTC
00104     LPC_RTC->SEC = timeinfo->tm_sec;
00105     LPC_RTC->MIN = timeinfo->tm_min;
00106     LPC_RTC->HOUR = timeinfo->tm_hour;
00107     LPC_RTC->DOM = timeinfo->tm_mday;
00108     LPC_RTC->MONTH = timeinfo->tm_mon + 1;
00109     LPC_RTC->YEAR = timeinfo->tm_year + 1900;
00110     
00111     // Restart clock
00112     LPC_RTC->CCR &= ~((uint32_t)2);
00113 }