[11U68]fix P0_11 to use GPIO
Fork of mbed-src by
targets/hal/TARGET_Silicon_Labs/TARGET_EFM32/rtc_api.c@548:1abac31e188e, 2015-05-22 (annotated)
- Committer:
- mbed_official
- Date:
- Fri May 22 10:45:46 2015 +0100
- Revision:
- 548:1abac31e188e
- Parent:
- 526:7c4bdfe6a168
- Child:
- 550:787824fdf8f9
Synchronized with git revision 88d158e43b54f97c5e94da305ea9a096889cc81b
Full URL: https://github.com/mbedmicro/mbed/commit/88d158e43b54f97c5e94da305ea9a096889cc81b/
Silicon Labs - Cosmetic: apply mbed coding style to HAL
Who changed what in which revision?
User | Revision | Line number | New 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 | 548:1abac31e188e | 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 | 548:1abac31e188e | 40 | if (flags & RTC_IF_OF) { |
mbed_official | 525:c320967f86b9 | 41 | RTC_IntClear(RTC_IF_OF); |
mbed_official | 525:c320967f86b9 | 42 | /* RTC has overflowed (24 bits). Use time_base as software counter for upper 8 bits. */ |
mbed_official | 525:c320967f86b9 | 43 | time_base += 1 << 24; |
mbed_official | 525:c320967f86b9 | 44 | } |
mbed_official | 548:1abac31e188e | 45 | if (flags & RTC_IF_COMP0) { |
mbed_official | 525:c320967f86b9 | 46 | RTC_IntClear(RTC_IF_COMP0); |
mbed_official | 548:1abac31e188e | 47 | if (comp0_handler != NULL) { |
mbed_official | 525:c320967f86b9 | 48 | comp0_handler(); |
mbed_official | 525:c320967f86b9 | 49 | } |
mbed_official | 525:c320967f86b9 | 50 | } |
mbed_official | 525:c320967f86b9 | 51 | } |
mbed_official | 525:c320967f86b9 | 52 | |
mbed_official | 525:c320967f86b9 | 53 | void rtc_set_comp0_handler(uint32_t handler) |
mbed_official | 525:c320967f86b9 | 54 | { |
mbed_official | 525:c320967f86b9 | 55 | comp0_handler = (void (*)(void)) handler; |
mbed_official | 525:c320967f86b9 | 56 | } |
mbed_official | 525:c320967f86b9 | 57 | |
mbed_official | 548:1abac31e188e | 58 | void rtc_init(void) |
mbed_official | 525:c320967f86b9 | 59 | { |
mbed_official | 525:c320967f86b9 | 60 | /* Register that the RTC is used for timekeeping. */ |
mbed_official | 525:c320967f86b9 | 61 | rtc_init_real(RTC_INIT_RTC); |
mbed_official | 525:c320967f86b9 | 62 | } |
mbed_official | 525:c320967f86b9 | 63 | |
mbed_official | 525:c320967f86b9 | 64 | |
mbed_official | 525:c320967f86b9 | 65 | void rtc_init_real(uint32_t flags) |
mbed_official | 525:c320967f86b9 | 66 | { |
mbed_official | 525:c320967f86b9 | 67 | useflags |= flags; |
mbed_official | 525:c320967f86b9 | 68 | |
mbed_official | 548:1abac31e188e | 69 | if (!rtc_inited) { |
mbed_official | 525:c320967f86b9 | 70 | /* Start LFXO and wait until it is stable */ |
mbed_official | 525:c320967f86b9 | 71 | CMU_OscillatorEnable(cmuOsc_LFXO, true, true); |
mbed_official | 525:c320967f86b9 | 72 | |
mbed_official | 525:c320967f86b9 | 73 | /* Route the LFXO clock to the RTC */ |
mbed_official | 525:c320967f86b9 | 74 | CMU_ClockSelectSet(cmuClock_LFA, LOW_ENERGY_CLOCK_SOURCE); |
mbed_official | 525:c320967f86b9 | 75 | CMU_ClockEnable(cmuClock_RTC, true); |
mbed_official | 525:c320967f86b9 | 76 | |
mbed_official | 525:c320967f86b9 | 77 | /* Enable clock to the interface of the low energy modules */ |
mbed_official | 525:c320967f86b9 | 78 | CMU_ClockEnable(cmuClock_CORELE, true); |
mbed_official | 525:c320967f86b9 | 79 | |
mbed_official | 525:c320967f86b9 | 80 | /* Scale clock to save power */ |
mbed_official | 525:c320967f86b9 | 81 | CMU_ClockDivSet(cmuClock_RTC, RTC_CLOCKDIV); |
mbed_official | 525:c320967f86b9 | 82 | |
mbed_official | 525:c320967f86b9 | 83 | /* Initialize RTC */ |
mbed_official | 525:c320967f86b9 | 84 | RTC_Init_TypeDef init = RTC_INIT_DEFAULT; |
mbed_official | 525:c320967f86b9 | 85 | init.enable = 1; |
mbed_official | 525:c320967f86b9 | 86 | /* Don't use compare register 0 as top value */ |
mbed_official | 525:c320967f86b9 | 87 | init.comp0Top = 0; |
mbed_official | 525:c320967f86b9 | 88 | |
mbed_official | 525:c320967f86b9 | 89 | /* Enable Interrupt from RTC */ |
mbed_official | 525:c320967f86b9 | 90 | RTC_IntEnable(RTC_IEN_OF); |
mbed_official | 525:c320967f86b9 | 91 | NVIC_EnableIRQ(RTC_IRQn); |
mbed_official | 525:c320967f86b9 | 92 | NVIC_SetVector(RTC_IRQn, (uint32_t)RTC_IRQHandler); |
mbed_official | 525:c320967f86b9 | 93 | |
mbed_official | 525:c320967f86b9 | 94 | /* Initialize */ |
mbed_official | 525:c320967f86b9 | 95 | RTC_Init(&init); |
mbed_official | 525:c320967f86b9 | 96 | |
mbed_official | 525:c320967f86b9 | 97 | blockSleepMode(RTC_LEAST_ACTIVE_SLEEPMODE); |
mbed_official | 525:c320967f86b9 | 98 | rtc_inited = true; |
mbed_official | 525:c320967f86b9 | 99 | } |
mbed_official | 525:c320967f86b9 | 100 | } |
mbed_official | 525:c320967f86b9 | 101 | |
mbed_official | 548:1abac31e188e | 102 | void rtc_free(void) |
mbed_official | 525:c320967f86b9 | 103 | { |
mbed_official | 525:c320967f86b9 | 104 | rtc_free_real(RTC_INIT_RTC); |
mbed_official | 525:c320967f86b9 | 105 | } |
mbed_official | 525:c320967f86b9 | 106 | |
mbed_official | 525:c320967f86b9 | 107 | void rtc_free_real(uint32_t flags) |
mbed_official | 525:c320967f86b9 | 108 | { |
mbed_official | 525:c320967f86b9 | 109 | /* Clear use flag */ |
mbed_official | 548:1abac31e188e | 110 | flags &= ~flags; |
mbed_official | 525:c320967f86b9 | 111 | |
mbed_official | 525:c320967f86b9 | 112 | /* Disable the RTC if it was inited and is no longer in use by anyone. */ |
mbed_official | 548:1abac31e188e | 113 | if (rtc_inited && (flags == 0)) { |
mbed_official | 525:c320967f86b9 | 114 | NVIC_DisableIRQ(RTC_IRQn); |
mbed_official | 525:c320967f86b9 | 115 | RTC_Reset(); |
mbed_official | 525:c320967f86b9 | 116 | CMU_ClockEnable(cmuClock_RTC, false); |
mbed_official | 525:c320967f86b9 | 117 | unblockSleepMode(RTC_LEAST_ACTIVE_SLEEPMODE); |
mbed_official | 525:c320967f86b9 | 118 | rtc_inited = false; |
mbed_official | 525:c320967f86b9 | 119 | } |
mbed_official | 525:c320967f86b9 | 120 | } |
mbed_official | 525:c320967f86b9 | 121 | |
mbed_official | 525:c320967f86b9 | 122 | int rtc_isenabled(void) |
mbed_official | 525:c320967f86b9 | 123 | { |
mbed_official | 525:c320967f86b9 | 124 | return rtc_inited; |
mbed_official | 525:c320967f86b9 | 125 | } |
mbed_official | 525:c320967f86b9 | 126 | |
mbed_official | 525:c320967f86b9 | 127 | time_t rtc_read(void) |
mbed_official | 525:c320967f86b9 | 128 | { |
mbed_official | 525:c320967f86b9 | 129 | return (time_t) ((RTC_CounterGet() + time_base) >> RTC_FREQ_SHIFT); |
mbed_official | 525:c320967f86b9 | 130 | } |
mbed_official | 525:c320967f86b9 | 131 | |
mbed_official | 525:c320967f86b9 | 132 | void rtc_write(time_t t) |
mbed_official | 525:c320967f86b9 | 133 | { |
mbed_official | 525:c320967f86b9 | 134 | /* We have to check that the RTC did not tick while doing this. */ |
mbed_official | 525:c320967f86b9 | 135 | /* If the RTC ticks we just redo this. */ |
mbed_official | 525:c320967f86b9 | 136 | uint32_t rtc_count; |
mbed_official | 525:c320967f86b9 | 137 | do { |
mbed_official | 525:c320967f86b9 | 138 | rtc_count = RTC_CounterGet(); |
mbed_official | 525:c320967f86b9 | 139 | time_base = (t << RTC_FREQ_SHIFT) - rtc_count; |
mbed_official | 525:c320967f86b9 | 140 | } while (rtc_count != RTC_CounterGet()); |
mbed_official | 525:c320967f86b9 | 141 | } |
mbed_official | 525:c320967f86b9 | 142 | |
mbed_official | 525:c320967f86b9 | 143 | #endif |