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
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 // clock source on 2368 is special test mode on 1768! 00041 LPC_RTC->CCR |= 1 << 4; // Ensure clock source is 32KHz Xtal 00042 00043 LPC_RTC->CCR |= 1 << 0; // Ensure the RTC is enabled 00044 } 00045 00046 void rtc_free(void) { 00047 // [TODO] 00048 } 00049 00050 /* 00051 * Little check routine to see if the RTC has been enabled 00052 * 00053 * Clock Control Register 00054 * RTC_CCR[0] : 0 = Disabled, 1 = Enabled 00055 * 00056 */ 00057 00058 int rtc_isenabled(void) { 00059 return(((LPC_RTC->CCR) & 0x01) != 0); 00060 } 00061 00062 /* 00063 * RTC Registers 00064 * RTC_SEC Seconds 0-59 00065 * RTC_MIN Minutes 0-59 00066 * RTC_HOUR Hour 0-23 00067 * RTC_DOM Day of Month 1-28..31 00068 * RTC_DOW Day of Week 0-6 00069 * RTC_DOY Day of Year 1-365 00070 * RTC_MONTH Month 1-12 00071 * RTC_YEAR Year 0-4095 00072 * 00073 * struct tm 00074 * tm_sec seconds after the minute 0-61 00075 * tm_min minutes after the hour 0-59 00076 * tm_hour hours since midnight 0-23 00077 * tm_mday day of the month 1-31 00078 * tm_mon months since January 0-11 00079 * tm_year years since 1900 00080 * tm_wday days since Sunday 0-6 00081 * tm_yday days since January 1 0-365 00082 * tm_isdst Daylight Saving Time flag 00083 */ 00084 time_t rtc_read(void) { 00085 // Setup a tm structure based on the RTC 00086 struct tm timeinfo; 00087 timeinfo.tm_sec = LPC_RTC->SEC; 00088 timeinfo.tm_min = LPC_RTC->MIN; 00089 timeinfo.tm_hour = LPC_RTC->HOUR; 00090 timeinfo.tm_mday = LPC_RTC->DOM; 00091 timeinfo.tm_mon = LPC_RTC->MONTH - 1; 00092 timeinfo.tm_year = LPC_RTC->YEAR - 1900; 00093 00094 // Convert to timestamp 00095 time_t t = mktime(&timeinfo); 00096 00097 return t; 00098 } 00099 00100 void rtc_write(time_t t) { 00101 // Convert the time in to a tm 00102 struct tm *timeinfo = localtime(&t); 00103 00104 // Pause clock, and clear counter register (clears us count) 00105 LPC_RTC->CCR |= 2; 00106 00107 // Set the RTC 00108 LPC_RTC->SEC = timeinfo->tm_sec; 00109 LPC_RTC->MIN = timeinfo->tm_min; 00110 LPC_RTC->HOUR = timeinfo->tm_hour; 00111 LPC_RTC->DOM = timeinfo->tm_mday; 00112 LPC_RTC->MONTH = timeinfo->tm_mon + 1; 00113 LPC_RTC->YEAR = timeinfo->tm_year + 1900; 00114 00115 // Restart clock 00116 LPC_RTC->CCR &= ~((uint32_t)2); 00117 }
Generated on Tue Jul 12 2022 13:47:01 by 1.7.2