mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_Freescale/TARGET_KLXX/rtc_api.c@323:9e901b0a5aa1, 2014-09-13 (annotated)
- Committer:
- shaoziyang
- Date:
- Sat Sep 13 14:25:46 2014 +0000
- Revision:
- 323:9e901b0a5aa1
- Parent:
- 82:0b31dbcd4769
test with CLOCK_SETUP = 0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 82:0b31dbcd4769 | 1 | /* mbed Microcontroller Library |
mbed_official | 82:0b31dbcd4769 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mbed_official | 82:0b31dbcd4769 | 3 | * |
mbed_official | 82:0b31dbcd4769 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 82:0b31dbcd4769 | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 82:0b31dbcd4769 | 6 | * You may obtain a copy of the License at |
mbed_official | 82:0b31dbcd4769 | 7 | * |
mbed_official | 82:0b31dbcd4769 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 82:0b31dbcd4769 | 9 | * |
mbed_official | 82:0b31dbcd4769 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 82:0b31dbcd4769 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 82:0b31dbcd4769 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 82:0b31dbcd4769 | 13 | * See the License for the specific language governing permissions and |
mbed_official | 82:0b31dbcd4769 | 14 | * limitations under the License. |
mbed_official | 82:0b31dbcd4769 | 15 | */ |
mbed_official | 82:0b31dbcd4769 | 16 | #include "rtc_api.h" |
mbed_official | 82:0b31dbcd4769 | 17 | #include "PeripheralPins.h" |
mbed_official | 82:0b31dbcd4769 | 18 | |
mbed_official | 82:0b31dbcd4769 | 19 | static void init(void) { |
mbed_official | 82:0b31dbcd4769 | 20 | // enable RTC clock |
mbed_official | 82:0b31dbcd4769 | 21 | SIM->SCGC6 |= SIM_SCGC6_RTC_MASK; |
mbed_official | 82:0b31dbcd4769 | 22 | |
mbed_official | 82:0b31dbcd4769 | 23 | pinmap_pinout(PinMap_RTC[0].pin, PinMap_RTC); //Map RTC clk input (if not NC) |
mbed_official | 82:0b31dbcd4769 | 24 | |
mbed_official | 82:0b31dbcd4769 | 25 | // select RTC clock source |
mbed_official | 82:0b31dbcd4769 | 26 | SIM->SOPT1 &= ~SIM_SOPT1_OSC32KSEL_MASK; |
mbed_official | 82:0b31dbcd4769 | 27 | SIM->SOPT1 |= SIM_SOPT1_OSC32KSEL(PinMap_RTC[0].peripheral); |
mbed_official | 82:0b31dbcd4769 | 28 | } |
mbed_official | 82:0b31dbcd4769 | 29 | |
mbed_official | 82:0b31dbcd4769 | 30 | void rtc_init(void) { |
mbed_official | 82:0b31dbcd4769 | 31 | init(); |
mbed_official | 82:0b31dbcd4769 | 32 | |
mbed_official | 82:0b31dbcd4769 | 33 | //Configure the TSR. default value: 1 |
mbed_official | 82:0b31dbcd4769 | 34 | RTC->TSR = 1; |
mbed_official | 82:0b31dbcd4769 | 35 | |
mbed_official | 82:0b31dbcd4769 | 36 | if (PinMap_RTC[0].pin == NC) { //Use OSC32K |
mbed_official | 82:0b31dbcd4769 | 37 | RTC->CR |= RTC_CR_OSCE_MASK; |
mbed_official | 82:0b31dbcd4769 | 38 | //delay for OSCE stabilization |
mbed_official | 82:0b31dbcd4769 | 39 | for(int i=0; i<0x1000; i++) __NOP(); |
mbed_official | 82:0b31dbcd4769 | 40 | } |
mbed_official | 82:0b31dbcd4769 | 41 | |
mbed_official | 82:0b31dbcd4769 | 42 | // enable counter |
mbed_official | 82:0b31dbcd4769 | 43 | RTC->SR |= RTC_SR_TCE_MASK; |
mbed_official | 82:0b31dbcd4769 | 44 | } |
mbed_official | 82:0b31dbcd4769 | 45 | |
mbed_official | 82:0b31dbcd4769 | 46 | void rtc_free(void) { |
mbed_official | 82:0b31dbcd4769 | 47 | // [TODO] |
mbed_official | 82:0b31dbcd4769 | 48 | } |
mbed_official | 82:0b31dbcd4769 | 49 | |
mbed_official | 82:0b31dbcd4769 | 50 | /* |
mbed_official | 82:0b31dbcd4769 | 51 | * Little check routine to see if the RTC has been enabled |
mbed_official | 82:0b31dbcd4769 | 52 | * 0 = Disabled, 1 = Enabled |
mbed_official | 82:0b31dbcd4769 | 53 | */ |
mbed_official | 82:0b31dbcd4769 | 54 | int rtc_isenabled(void) { |
mbed_official | 82:0b31dbcd4769 | 55 | // even if the RTC module is enabled, |
mbed_official | 82:0b31dbcd4769 | 56 | // as we use RTC_CLKIN and an external clock, |
mbed_official | 82:0b31dbcd4769 | 57 | // we need to reconfigure the pins. That is why we |
mbed_official | 82:0b31dbcd4769 | 58 | // call init() if the rtc is enabled |
mbed_official | 82:0b31dbcd4769 | 59 | |
mbed_official | 82:0b31dbcd4769 | 60 | // if RTC not enabled return 0 |
mbed_official | 82:0b31dbcd4769 | 61 | SIM->SCGC5 |= SIM_SCGC5_PORTA_MASK; |
mbed_official | 82:0b31dbcd4769 | 62 | SIM->SCGC6 |= SIM_SCGC6_RTC_MASK; |
mbed_official | 82:0b31dbcd4769 | 63 | if ((RTC->SR & RTC_SR_TCE_MASK) == 0) |
mbed_official | 82:0b31dbcd4769 | 64 | return 0; |
mbed_official | 82:0b31dbcd4769 | 65 | |
mbed_official | 82:0b31dbcd4769 | 66 | init(); |
mbed_official | 82:0b31dbcd4769 | 67 | return 1; |
mbed_official | 82:0b31dbcd4769 | 68 | } |
mbed_official | 82:0b31dbcd4769 | 69 | |
mbed_official | 82:0b31dbcd4769 | 70 | time_t rtc_read(void) { |
mbed_official | 82:0b31dbcd4769 | 71 | return RTC->TSR; |
mbed_official | 82:0b31dbcd4769 | 72 | } |
mbed_official | 82:0b31dbcd4769 | 73 | |
mbed_official | 82:0b31dbcd4769 | 74 | void rtc_write(time_t t) { |
mbed_official | 82:0b31dbcd4769 | 75 | // disable counter |
mbed_official | 82:0b31dbcd4769 | 76 | RTC->SR &= ~RTC_SR_TCE_MASK; |
mbed_official | 82:0b31dbcd4769 | 77 | |
mbed_official | 82:0b31dbcd4769 | 78 | // we do not write 0 into TSR |
mbed_official | 82:0b31dbcd4769 | 79 | // to avoid invalid time |
mbed_official | 82:0b31dbcd4769 | 80 | if (t == 0) |
mbed_official | 82:0b31dbcd4769 | 81 | t = 1; |
mbed_official | 82:0b31dbcd4769 | 82 | |
mbed_official | 82:0b31dbcd4769 | 83 | // write seconds |
mbed_official | 82:0b31dbcd4769 | 84 | RTC->TSR = t; |
mbed_official | 82:0b31dbcd4769 | 85 | |
mbed_official | 82:0b31dbcd4769 | 86 | // re-enable counter |
mbed_official | 82:0b31dbcd4769 | 87 | RTC->SR |= RTC_SR_TCE_MASK; |
mbed_official | 82:0b31dbcd4769 | 88 | } |