Flotsam / mbed-modifed

Dependents:   EEPROMWrite Full-Project

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Tue Apr 28 13:00:10 2015 +0100
Revision:
526:7c4bdfe6a168
Parent:
525:c320967f86b9
Child:
548:1abac31e188e
Synchronized with git revision cdff09e6b6c486eb58fb5e968f3191c8480b0567

Full URL: https://github.com/mbedmicro/mbed/commit/cdff09e6b6c486eb58fb5e968f3191c8480b0567/

Silabs - Fix unblock/block declaration, implement gpio_is_connected

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 525:c320967f86b9 1 /* mbed Microcontroller Library
mbed_official 525:c320967f86b9 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 525:c320967f86b9 3 *
mbed_official 525:c320967f86b9 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 525:c320967f86b9 5 * you may not use this file except in compliance with the License.
mbed_official 525:c320967f86b9 6 * You may obtain a copy of the License at
mbed_official 525:c320967f86b9 7 *
mbed_official 525:c320967f86b9 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 525:c320967f86b9 9 *
mbed_official 525:c320967f86b9 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 525:c320967f86b9 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 525:c320967f86b9 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 525:c320967f86b9 13 * See the License for the specific language governing permissions and
mbed_official 525:c320967f86b9 14 * limitations under the License.
mbed_official 525:c320967f86b9 15 */
mbed_official 525:c320967f86b9 16
mbed_official 525:c320967f86b9 17 #include "device.h"
mbed_official 525:c320967f86b9 18 #if DEVICE_RTC
mbed_official 525:c320967f86b9 19
mbed_official 525:c320967f86b9 20 #include "rtc_api.h"
mbed_official 525:c320967f86b9 21 #include "rtc_api_HAL.h"
mbed_official 525:c320967f86b9 22 #include "em_cmu.h"
mbed_official 525:c320967f86b9 23 #include "em_rtc.h"
mbed_official 525:c320967f86b9 24 #include "sleep_api.h"
mbed_official 526:7c4bdfe6a168 25 #include "sleepmodes.h"
mbed_official 525:c320967f86b9 26
mbed_official 525:c320967f86b9 27 static bool rtc_inited = false;
mbed_official 525:c320967f86b9 28 static time_t time_base = 0;
mbed_official 525:c320967f86b9 29 static uint32_t useflags = 0;
mbed_official 525:c320967f86b9 30
mbed_official 525:c320967f86b9 31 static void (*comp0_handler)(void) = NULL;
mbed_official 525:c320967f86b9 32
mbed_official 525:c320967f86b9 33 #define RTC_LEAST_ACTIVE_SLEEPMODE EM2
mbed_official 525:c320967f86b9 34
mbed_official 525:c320967f86b9 35
mbed_official 525:c320967f86b9 36 void RTC_IRQHandler(void)
mbed_official 525:c320967f86b9 37 {
mbed_official 525:c320967f86b9 38 uint32_t flags;
mbed_official 525:c320967f86b9 39 flags = RTC_IntGet();
mbed_official 525:c320967f86b9 40 if (flags & RTC_IF_OF)
mbed_official 525:c320967f86b9 41 {
mbed_official 525:c320967f86b9 42 RTC_IntClear(RTC_IF_OF);
mbed_official 525:c320967f86b9 43 /* RTC has overflowed (24 bits). Use time_base as software counter for upper 8 bits. */
mbed_official 525:c320967f86b9 44 time_base += 1 << 24;
mbed_official 525:c320967f86b9 45 }
mbed_official 525:c320967f86b9 46 if (flags & RTC_IF_COMP0)
mbed_official 525:c320967f86b9 47 {
mbed_official 525:c320967f86b9 48 RTC_IntClear(RTC_IF_COMP0);
mbed_official 525:c320967f86b9 49 if (comp0_handler != NULL)
mbed_official 525:c320967f86b9 50 {
mbed_official 525:c320967f86b9 51 comp0_handler();
mbed_official 525:c320967f86b9 52 }
mbed_official 525:c320967f86b9 53 }
mbed_official 525:c320967f86b9 54 }
mbed_official 525:c320967f86b9 55
mbed_official 525:c320967f86b9 56 void rtc_set_comp0_handler(uint32_t handler)
mbed_official 525:c320967f86b9 57 {
mbed_official 525:c320967f86b9 58 comp0_handler = (void (*)(void)) handler;
mbed_official 525:c320967f86b9 59 }
mbed_official 525:c320967f86b9 60
mbed_official 525:c320967f86b9 61 void rtc_init(void)
mbed_official 525:c320967f86b9 62 {
mbed_official 525:c320967f86b9 63 /* Register that the RTC is used for timekeeping. */
mbed_official 525:c320967f86b9 64 rtc_init_real(RTC_INIT_RTC);
mbed_official 525:c320967f86b9 65 }
mbed_official 525:c320967f86b9 66
mbed_official 525:c320967f86b9 67
mbed_official 525:c320967f86b9 68 void rtc_init_real(uint32_t flags)
mbed_official 525:c320967f86b9 69 {
mbed_official 525:c320967f86b9 70 useflags |= flags;
mbed_official 525:c320967f86b9 71
mbed_official 525:c320967f86b9 72 if (!rtc_inited)
mbed_official 525:c320967f86b9 73 {
mbed_official 525:c320967f86b9 74 /* Start LFXO and wait until it is stable */
mbed_official 525:c320967f86b9 75 CMU_OscillatorEnable(cmuOsc_LFXO, true, true);
mbed_official 525:c320967f86b9 76
mbed_official 525:c320967f86b9 77 /* Route the LFXO clock to the RTC */
mbed_official 525:c320967f86b9 78 CMU_ClockSelectSet(cmuClock_LFA, LOW_ENERGY_CLOCK_SOURCE);
mbed_official 525:c320967f86b9 79 CMU_ClockEnable(cmuClock_RTC, true);
mbed_official 525:c320967f86b9 80
mbed_official 525:c320967f86b9 81 /* Enable clock to the interface of the low energy modules */
mbed_official 525:c320967f86b9 82 CMU_ClockEnable(cmuClock_CORELE, true);
mbed_official 525:c320967f86b9 83
mbed_official 525:c320967f86b9 84 /* Scale clock to save power */
mbed_official 525:c320967f86b9 85 CMU_ClockDivSet(cmuClock_RTC, RTC_CLOCKDIV);
mbed_official 525:c320967f86b9 86
mbed_official 525:c320967f86b9 87 /* Initialize RTC */
mbed_official 525:c320967f86b9 88 RTC_Init_TypeDef init = RTC_INIT_DEFAULT;
mbed_official 525:c320967f86b9 89 init.enable = 1;
mbed_official 525:c320967f86b9 90 /* Don't use compare register 0 as top value */
mbed_official 525:c320967f86b9 91 init.comp0Top = 0;
mbed_official 525:c320967f86b9 92
mbed_official 525:c320967f86b9 93 /* Enable Interrupt from RTC */
mbed_official 525:c320967f86b9 94 RTC_IntEnable(RTC_IEN_OF);
mbed_official 525:c320967f86b9 95 NVIC_EnableIRQ(RTC_IRQn);
mbed_official 525:c320967f86b9 96 NVIC_SetVector(RTC_IRQn, (uint32_t)RTC_IRQHandler);
mbed_official 525:c320967f86b9 97
mbed_official 525:c320967f86b9 98 /* Initialize */
mbed_official 525:c320967f86b9 99 RTC_Init(&init);
mbed_official 525:c320967f86b9 100
mbed_official 525:c320967f86b9 101 blockSleepMode(RTC_LEAST_ACTIVE_SLEEPMODE);
mbed_official 525:c320967f86b9 102 rtc_inited = true;
mbed_official 525:c320967f86b9 103 }
mbed_official 525:c320967f86b9 104 }
mbed_official 525:c320967f86b9 105
mbed_official 525:c320967f86b9 106 void rtc_free(void)
mbed_official 525:c320967f86b9 107 {
mbed_official 525:c320967f86b9 108 rtc_free_real(RTC_INIT_RTC);
mbed_official 525:c320967f86b9 109 }
mbed_official 525:c320967f86b9 110
mbed_official 525:c320967f86b9 111 void rtc_free_real(uint32_t flags)
mbed_official 525:c320967f86b9 112 {
mbed_official 525:c320967f86b9 113 /* Clear use flag */
mbed_official 525:c320967f86b9 114 flags &= ~flags;
mbed_official 525:c320967f86b9 115
mbed_official 525:c320967f86b9 116 /* Disable the RTC if it was inited and is no longer in use by anyone. */
mbed_official 525:c320967f86b9 117 if (rtc_inited && (flags == 0))
mbed_official 525:c320967f86b9 118 {
mbed_official 525:c320967f86b9 119 NVIC_DisableIRQ(RTC_IRQn);
mbed_official 525:c320967f86b9 120 RTC_Reset();
mbed_official 525:c320967f86b9 121 CMU_ClockEnable(cmuClock_RTC, false);
mbed_official 525:c320967f86b9 122 unblockSleepMode(RTC_LEAST_ACTIVE_SLEEPMODE);
mbed_official 525:c320967f86b9 123 rtc_inited = false;
mbed_official 525:c320967f86b9 124 }
mbed_official 525:c320967f86b9 125 }
mbed_official 525:c320967f86b9 126
mbed_official 525:c320967f86b9 127 int rtc_isenabled(void)
mbed_official 525:c320967f86b9 128 {
mbed_official 525:c320967f86b9 129 return rtc_inited;
mbed_official 525:c320967f86b9 130 }
mbed_official 525:c320967f86b9 131
mbed_official 525:c320967f86b9 132 time_t rtc_read(void)
mbed_official 525:c320967f86b9 133 {
mbed_official 525:c320967f86b9 134 return (time_t) ((RTC_CounterGet() + time_base) >> RTC_FREQ_SHIFT);
mbed_official 525:c320967f86b9 135 }
mbed_official 525:c320967f86b9 136
mbed_official 525:c320967f86b9 137 void rtc_write(time_t t)
mbed_official 525:c320967f86b9 138 {
mbed_official 525:c320967f86b9 139 /* We have to check that the RTC did not tick while doing this. */
mbed_official 525:c320967f86b9 140 /* If the RTC ticks we just redo this. */
mbed_official 525:c320967f86b9 141 uint32_t rtc_count;
mbed_official 525:c320967f86b9 142 do {
mbed_official 525:c320967f86b9 143 rtc_count = RTC_CounterGet();
mbed_official 525:c320967f86b9 144 time_base = (t << RTC_FREQ_SHIFT) - rtc_count;
mbed_official 525:c320967f86b9 145 } while (rtc_count != RTC_CounterGet());
mbed_official 525:c320967f86b9 146 }
mbed_official 525:c320967f86b9 147
mbed_official 525:c320967f86b9 148 #endif