eunkyoung kim / mbed-src

Dependents:   SSD1306_smart_watch

Fork of mbed-src by eunkyoung kim

Committer:
mbed_official
Date:
Thu Jan 15 19:30:07 2015 +0000
Revision:
445:3312ed629f01
Child:
489:119543c9f674
Synchronized with git revision e21c65041d9a718f866ed6438e5791b13f9c6d91

Full URL: https://github.com/mbedmicro/mbed/commit/e21c65041d9a718f866ed6438e5791b13f9c6d91/

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 445:3312ed629f01 1 /* mbed Microcontroller Library
mbed_official 445:3312ed629f01 2 * Copyright (c) 2006-2015 ARM Limited
mbed_official 445:3312ed629f01 3 *
mbed_official 445:3312ed629f01 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 445:3312ed629f01 5 * you may not use this file except in compliance with the License.
mbed_official 445:3312ed629f01 6 * You may obtain a copy of the License at
mbed_official 445:3312ed629f01 7 *
mbed_official 445:3312ed629f01 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 445:3312ed629f01 9 *
mbed_official 445:3312ed629f01 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 445:3312ed629f01 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 445:3312ed629f01 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 445:3312ed629f01 13 * See the License for the specific language governing permissions and
mbed_official 445:3312ed629f01 14 * limitations under the License.
mbed_official 445:3312ed629f01 15 */
mbed_official 445:3312ed629f01 16 #include "rtc_api.h"
mbed_official 445:3312ed629f01 17
mbed_official 445:3312ed629f01 18 static void init(void) {
mbed_official 445:3312ed629f01 19 // enable PORTC clock
mbed_official 445:3312ed629f01 20 SIM->SCGC5 |= SIM_SCGC5_PORTC_MASK;
mbed_official 445:3312ed629f01 21
mbed_official 445:3312ed629f01 22 // enable RTC clock
mbed_official 445:3312ed629f01 23 SIM->SCGC6 |= SIM_SCGC6_RTC_MASK;
mbed_official 445:3312ed629f01 24
mbed_official 445:3312ed629f01 25 // OSC32 as source
mbed_official 445:3312ed629f01 26 SIM->SOPT1 &= ~SIM_SOPT1_OSC32KSEL_MASK;
mbed_official 445:3312ed629f01 27 SIM->SOPT1 |= SIM_SOPT1_OSC32KSEL(0);
mbed_official 445:3312ed629f01 28 }
mbed_official 445:3312ed629f01 29
mbed_official 445:3312ed629f01 30 void rtc_init(void) {
mbed_official 445:3312ed629f01 31 init();
mbed_official 445:3312ed629f01 32
mbed_official 445:3312ed629f01 33 // Enable the oscillator
mbed_official 445:3312ed629f01 34 RTC->CR |= RTC_CR_OSCE_MASK;
mbed_official 445:3312ed629f01 35
mbed_official 445:3312ed629f01 36 //Configure the TSR. default value: 1
mbed_official 445:3312ed629f01 37 RTC->TSR = 1;
mbed_official 445:3312ed629f01 38
mbed_official 445:3312ed629f01 39 // enable counter
mbed_official 445:3312ed629f01 40 RTC->SR |= RTC_SR_TCE_MASK;
mbed_official 445:3312ed629f01 41 }
mbed_official 445:3312ed629f01 42
mbed_official 445:3312ed629f01 43 void rtc_free(void) {
mbed_official 445:3312ed629f01 44 // [TODO]
mbed_official 445:3312ed629f01 45 }
mbed_official 445:3312ed629f01 46
mbed_official 445:3312ed629f01 47 /*
mbed_official 445:3312ed629f01 48 * Little check routine to see if the RTC has been enabled
mbed_official 445:3312ed629f01 49 * 0 = Disabled, 1 = Enabled
mbed_official 445:3312ed629f01 50 */
mbed_official 445:3312ed629f01 51 int rtc_isenabled(void) {
mbed_official 445:3312ed629f01 52 // even if the RTC module is enabled,
mbed_official 445:3312ed629f01 53 // as we use RTC_CLKIN and an external clock,
mbed_official 445:3312ed629f01 54 // we need to reconfigure the pins. That is why we
mbed_official 445:3312ed629f01 55 // call init() if the rtc is enabled
mbed_official 445:3312ed629f01 56
mbed_official 445:3312ed629f01 57 // if RTC not enabled return 0
mbed_official 445:3312ed629f01 58 SIM->SCGC5 |= SIM_SCGC5_PORTC_MASK;
mbed_official 445:3312ed629f01 59 SIM->SCGC6 |= SIM_SCGC6_RTC_MASK;
mbed_official 445:3312ed629f01 60 if ((RTC->SR & RTC_SR_TCE_MASK) == 0)
mbed_official 445:3312ed629f01 61 return 0;
mbed_official 445:3312ed629f01 62
mbed_official 445:3312ed629f01 63 init();
mbed_official 445:3312ed629f01 64 return 1;
mbed_official 445:3312ed629f01 65 }
mbed_official 445:3312ed629f01 66
mbed_official 445:3312ed629f01 67 time_t rtc_read(void) {
mbed_official 445:3312ed629f01 68 return RTC->TSR;
mbed_official 445:3312ed629f01 69 }
mbed_official 445:3312ed629f01 70
mbed_official 445:3312ed629f01 71 void rtc_write(time_t t) {
mbed_official 445:3312ed629f01 72 // disable counter
mbed_official 445:3312ed629f01 73 RTC->SR &= ~RTC_SR_TCE_MASK;
mbed_official 445:3312ed629f01 74
mbed_official 445:3312ed629f01 75 // we do not write 0 into TSR
mbed_official 445:3312ed629f01 76 // to avoid invalid time
mbed_official 445:3312ed629f01 77 if (t == 0)
mbed_official 445:3312ed629f01 78 t = 1;
mbed_official 445:3312ed629f01 79
mbed_official 445:3312ed629f01 80 // write seconds
mbed_official 445:3312ed629f01 81 RTC->TSR = t;
mbed_official 445:3312ed629f01 82
mbed_official 445:3312ed629f01 83 // re-enable counter
mbed_official 445:3312ed629f01 84 RTC->SR |= RTC_SR_TCE_MASK;
mbed_official 445:3312ed629f01 85 }