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