mbed-os

Dependents:   cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more

Committer:
be_bryan
Date:
Mon Dec 11 17:54:04 2017 +0000
Revision:
0:b74591d5ab33
motor ++

Who changed what in which revision?

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