Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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 #if DEVICE_RTC 00019 00020 // ensure rtc is running (unchanged if already running) 00021 00022 /* Setup the RTC based on a time structure, ensuring RTC is enabled 00023 * 00024 * Can be clocked by a 32.768KHz oscillator or prescale divider based on the APB clock 00025 * - We want to use the 32khz clock, allowing for sleep mode 00026 * 00027 * Most registers are not changed by a Reset 00028 * - We must initialize these registers between power-on and setting the RTC into operation 00029 00030 * Clock Control Register 00031 * RTC_CCR[0] : Enable - 0 = Disabled, 1 = Enabled 00032 * RTC_CCR[1] : Reset - 0 = Normal, 1 = Reset 00033 * RTC_CCR[4] : Clock Source - 0 = Prescaler, 1 = 32k Xtal 00034 * 00035 * The RTC may already be running, so we should set it up 00036 * without impacting if it is the case 00037 */ 00038 void rtc_init(void) { 00039 LPC_SC->PCONP |= 0x200; // Ensure power is on 00040 LPC_RTC->CCR = 0x00; 00041 00042 // clock source on 2368 is special test mode on 1768! 00043 #ifdef TARGET_LPC2368 00044 LPC_RTC->CCR |= 1 << 4; // Ensure clock source is 32KHz Xtal 00045 #endif 00046 00047 LPC_RTC->CCR |= 1 << 0; // Ensure the RTC is enabled 00048 } 00049 00050 void rtc_free(void) { 00051 // [TODO] 00052 } 00053 00054 /* 00055 * Little check routine to see if the RTC has been enabled 00056 * 00057 * Clock Control Register 00058 * RTC_CCR[0] : 0 = Disabled, 1 = Enabled 00059 * 00060 */ 00061 00062 int rtc_isenabled(void) { 00063 return(((LPC_RTC->CCR) & 0x01) != 0); 00064 } 00065 00066 /* 00067 * RTC Registers 00068 * RTC_SEC Seconds 0-59 00069 * RTC_MIN Minutes 0-59 00070 * RTC_HOUR Hour 0-23 00071 * RTC_DOM Day of Month 1-28..31 00072 * RTC_DOW Day of Week 0-6 00073 * RTC_DOY Day of Year 1-365 00074 * RTC_MONTH Month 1-12 00075 * RTC_YEAR Year 0-4095 00076 * 00077 * struct tm 00078 * tm_sec seconds after the minute 0-61 00079 * tm_min minutes after the hour 0-59 00080 * tm_hour hours since midnight 0-23 00081 * tm_mday day of the month 1-31 00082 * tm_mon months since January 0-11 00083 * tm_year years since 1900 00084 * tm_wday days since Sunday 0-6 00085 * tm_yday days since January 1 0-365 00086 * tm_isdst Daylight Saving Time flag 00087 */ 00088 time_t rtc_read(void) { 00089 // Setup a tm structure based on the RTC 00090 struct tm timeinfo; 00091 timeinfo.tm_sec = LPC_RTC->SEC; 00092 timeinfo.tm_min = LPC_RTC->MIN; 00093 timeinfo.tm_hour = LPC_RTC->HOUR; 00094 timeinfo.tm_mday = LPC_RTC->DOM; 00095 timeinfo.tm_mon = LPC_RTC->MONTH - 1; 00096 timeinfo.tm_year = LPC_RTC->YEAR - 1900; 00097 00098 // Convert to timestamp 00099 time_t t = mktime(&timeinfo); 00100 00101 return t; 00102 } 00103 00104 void rtc_write(time_t t) { 00105 // Convert the time in to a tm 00106 struct tm *timeinfo = localtime(&t); 00107 00108 // Pause clock, and clear counter register (clears us count) 00109 LPC_RTC->CCR |= 2; 00110 00111 // Set the RTC 00112 LPC_RTC->SEC = timeinfo->tm_sec; 00113 LPC_RTC->MIN = timeinfo->tm_min; 00114 LPC_RTC->HOUR = timeinfo->tm_hour; 00115 LPC_RTC->DOM = timeinfo->tm_mday; 00116 LPC_RTC->MONTH = timeinfo->tm_mon + 1; 00117 LPC_RTC->YEAR = timeinfo->tm_year + 1900; 00118 00119 // Restart clock 00120 LPC_RTC->CCR &= ~((uint32_t)2); 00121 } 00122 00123 #endif
Generated on Thu Jul 14 2022 07:43:04 by
1.7.2