DongEun Koak / mbed-src

Dependents:   Freedman_v2 Nucleo_i2c_OLED_BME280_copy

Fork of mbed-src by mbed official

Committer:
kaizen
Date:
Mon Aug 24 00:12:39 2015 +0000
Revision:
614:d1b257119699
Parent:
554:edd95c0879f8
Added UART_2 of WIZwiki_W7500 for test

Who changed what in which revision?

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