mbed SDK library sources

Fork of mbed-src by mbed official

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Wed Sep 25 11:30:05 2013 +0100
Revision:
31:42176bc3c368
Child:
33:e214068ab66c
Synchronized with git revision f580c008b139a952d38ac5c7c53bbae375739c67

Who changed what in which revision?

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