eunkyoung kim / mbed-src

Dependents:   SSD1306_smart_watch

Fork of mbed-src by eunkyoung kim

Committer:
mbed_official
Date:
Thu Mar 12 14:30:49 2015 +0000
Revision:
489:119543c9f674
Parent:
445:3312ed629f01
Synchronized with git revision 051854181516992fb498d51f9ee6e70cbad9e083

Full URL: https://github.com/mbedmicro/mbed/commit/051854181516992fb498d51f9ee6e70cbad9e083/

Fix ksdk mcu HAL - stopbit

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 489:119543c9f674 32
mbed_official 445:3312ed629f01 33 // Enable the oscillator
mbed_official 489:119543c9f674 34 #if defined (TARGET_K20D50M)
mbed_official 445:3312ed629f01 35 RTC->CR |= RTC_CR_OSCE_MASK;
mbed_official 489:119543c9f674 36 #else
mbed_official 489:119543c9f674 37 // Teensy3.1 requires 20pF MCU loading capacitors for 32KHz RTC oscillator
mbed_official 489:119543c9f674 38 /* RTC->CR: SC2P=0,SC4P=1,SC8P=0,SC16P=1,CLKO=0,OSCE=1,UM=0,SUP=0,SPE=0,SWR=0 */
mbed_official 489:119543c9f674 39 RTC->CR |= RTC_CR_OSCE_MASK |RTC_CR_SC16P_MASK | RTC_CR_SC4P_MASK;
mbed_official 489:119543c9f674 40 #endif
mbed_official 445:3312ed629f01 41
mbed_official 445:3312ed629f01 42 //Configure the TSR. default value: 1
mbed_official 445:3312ed629f01 43 RTC->TSR = 1;
mbed_official 445:3312ed629f01 44
mbed_official 445:3312ed629f01 45 // enable counter
mbed_official 445:3312ed629f01 46 RTC->SR |= RTC_SR_TCE_MASK;
mbed_official 445:3312ed629f01 47 }
mbed_official 445:3312ed629f01 48
mbed_official 445:3312ed629f01 49 void rtc_free(void) {
mbed_official 445:3312ed629f01 50 // [TODO]
mbed_official 445:3312ed629f01 51 }
mbed_official 445:3312ed629f01 52
mbed_official 445:3312ed629f01 53 /*
mbed_official 445:3312ed629f01 54 * Little check routine to see if the RTC has been enabled
mbed_official 445:3312ed629f01 55 * 0 = Disabled, 1 = Enabled
mbed_official 445:3312ed629f01 56 */
mbed_official 445:3312ed629f01 57 int rtc_isenabled(void) {
mbed_official 445:3312ed629f01 58 // even if the RTC module is enabled,
mbed_official 445:3312ed629f01 59 // as we use RTC_CLKIN and an external clock,
mbed_official 445:3312ed629f01 60 // we need to reconfigure the pins. That is why we
mbed_official 445:3312ed629f01 61 // call init() if the rtc is enabled
mbed_official 445:3312ed629f01 62
mbed_official 445:3312ed629f01 63 // if RTC not enabled return 0
mbed_official 445:3312ed629f01 64 SIM->SCGC5 |= SIM_SCGC5_PORTC_MASK;
mbed_official 445:3312ed629f01 65 SIM->SCGC6 |= SIM_SCGC6_RTC_MASK;
mbed_official 445:3312ed629f01 66 if ((RTC->SR & RTC_SR_TCE_MASK) == 0)
mbed_official 445:3312ed629f01 67 return 0;
mbed_official 445:3312ed629f01 68
mbed_official 445:3312ed629f01 69 init();
mbed_official 445:3312ed629f01 70 return 1;
mbed_official 445:3312ed629f01 71 }
mbed_official 445:3312ed629f01 72
mbed_official 445:3312ed629f01 73 time_t rtc_read(void) {
mbed_official 445:3312ed629f01 74 return RTC->TSR;
mbed_official 445:3312ed629f01 75 }
mbed_official 445:3312ed629f01 76
mbed_official 445:3312ed629f01 77 void rtc_write(time_t t) {
mbed_official 445:3312ed629f01 78 // disable counter
mbed_official 445:3312ed629f01 79 RTC->SR &= ~RTC_SR_TCE_MASK;
mbed_official 445:3312ed629f01 80
mbed_official 445:3312ed629f01 81 // we do not write 0 into TSR
mbed_official 445:3312ed629f01 82 // to avoid invalid time
mbed_official 445:3312ed629f01 83 if (t == 0)
mbed_official 445:3312ed629f01 84 t = 1;
mbed_official 445:3312ed629f01 85
mbed_official 445:3312ed629f01 86 // write seconds
mbed_official 445:3312ed629f01 87 RTC->TSR = t;
mbed_official 445:3312ed629f01 88
mbed_official 445:3312ed629f01 89 // re-enable counter
mbed_official 445:3312ed629f01 90 RTC->SR |= RTC_SR_TCE_MASK;
mbed_official 445:3312ed629f01 91 }