mbed library sources
Dependents: Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more
Superseded
This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.
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.
targets/hal/TARGET_Silicon_Labs/TARGET_EFM32/rtc_api.c@627:4fa1328d9c60, 2015-09-25 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Sep 25 14:15:10 2015 +0100
- Revision:
- 627:4fa1328d9c60
- Parent:
- 550:787824fdf8f9
Synchronized with git revision fe238a91ab7a4d1d72c4cab9da04967c619d54ad
Full URL: https://github.com/mbedmicro/mbed/commit/fe238a91ab7a4d1d72c4cab9da04967c619d54ad/
Silicon Labs - Add support for low-power async Serial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 627:4fa1328d9c60 | 1 | /***************************************************************************//** |
mbed_official | 627:4fa1328d9c60 | 2 | * @file rtc_api.c |
mbed_official | 627:4fa1328d9c60 | 3 | ******************************************************************************* |
mbed_official | 627:4fa1328d9c60 | 4 | * @section License |
mbed_official | 627:4fa1328d9c60 | 5 | * <b>(C) Copyright 2015 Silicon Labs, http://www.silabs.com</b> |
mbed_official | 627:4fa1328d9c60 | 6 | ******************************************************************************* |
mbed_official | 525:c320967f86b9 | 7 | * |
mbed_official | 627:4fa1328d9c60 | 8 | * Permission is granted to anyone to use this software for any purpose, |
mbed_official | 627:4fa1328d9c60 | 9 | * including commercial applications, and to alter it and redistribute it |
mbed_official | 627:4fa1328d9c60 | 10 | * freely, subject to the following restrictions: |
mbed_official | 525:c320967f86b9 | 11 | * |
mbed_official | 627:4fa1328d9c60 | 12 | * 1. The origin of this software must not be misrepresented; you must not |
mbed_official | 627:4fa1328d9c60 | 13 | * claim that you wrote the original software. |
mbed_official | 627:4fa1328d9c60 | 14 | * 2. Altered source versions must be plainly marked as such, and must not be |
mbed_official | 627:4fa1328d9c60 | 15 | * misrepresented as being the original software. |
mbed_official | 627:4fa1328d9c60 | 16 | * 3. This notice may not be removed or altered from any source distribution. |
mbed_official | 525:c320967f86b9 | 17 | * |
mbed_official | 627:4fa1328d9c60 | 18 | * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no |
mbed_official | 627:4fa1328d9c60 | 19 | * obligation to support this Software. Silicon Labs is providing the |
mbed_official | 627:4fa1328d9c60 | 20 | * Software "AS IS", with no express or implied warranties of any kind, |
mbed_official | 627:4fa1328d9c60 | 21 | * including, but not limited to, any implied warranties of merchantability |
mbed_official | 627:4fa1328d9c60 | 22 | * or fitness for any particular purpose or warranties against infringement |
mbed_official | 627:4fa1328d9c60 | 23 | * of any proprietary rights of a third party. |
mbed_official | 627:4fa1328d9c60 | 24 | * |
mbed_official | 627:4fa1328d9c60 | 25 | * Silicon Labs will not be liable for any consequential, incidental, or |
mbed_official | 627:4fa1328d9c60 | 26 | * special damages, or any other relief, or for any claim by any third party, |
mbed_official | 627:4fa1328d9c60 | 27 | * arising from your use of this Software. |
mbed_official | 627:4fa1328d9c60 | 28 | * |
mbed_official | 627:4fa1328d9c60 | 29 | ******************************************************************************/ |
mbed_official | 525:c320967f86b9 | 30 | |
mbed_official | 525:c320967f86b9 | 31 | #include "device.h" |
mbed_official | 525:c320967f86b9 | 32 | #if DEVICE_RTC |
mbed_official | 525:c320967f86b9 | 33 | |
mbed_official | 525:c320967f86b9 | 34 | #include "rtc_api.h" |
mbed_official | 525:c320967f86b9 | 35 | #include "rtc_api_HAL.h" |
mbed_official | 525:c320967f86b9 | 36 | #include "em_cmu.h" |
mbed_official | 525:c320967f86b9 | 37 | #include "em_rtc.h" |
mbed_official | 525:c320967f86b9 | 38 | #include "sleep_api.h" |
mbed_official | 526:7c4bdfe6a168 | 39 | #include "sleepmodes.h" |
mbed_official | 525:c320967f86b9 | 40 | |
mbed_official | 550:787824fdf8f9 | 41 | static bool rtc_inited = false; |
mbed_official | 550:787824fdf8f9 | 42 | static time_t time_base = 0; |
mbed_official | 550:787824fdf8f9 | 43 | static uint32_t useflags = 0; |
mbed_official | 550:787824fdf8f9 | 44 | static uint32_t time_extend = 0; |
mbed_official | 525:c320967f86b9 | 45 | |
mbed_official | 548:1abac31e188e | 46 | static void (*comp0_handler)(void) = NULL; |
mbed_official | 525:c320967f86b9 | 47 | |
mbed_official | 550:787824fdf8f9 | 48 | #define RTC_LEAST_ACTIVE_SLEEPMODE EM2 |
mbed_official | 550:787824fdf8f9 | 49 | #define RTC_NUM_BITS (24) |
mbed_official | 525:c320967f86b9 | 50 | |
mbed_official | 525:c320967f86b9 | 51 | void RTC_IRQHandler(void) |
mbed_official | 525:c320967f86b9 | 52 | { |
mbed_official | 525:c320967f86b9 | 53 | uint32_t flags; |
mbed_official | 525:c320967f86b9 | 54 | flags = RTC_IntGet(); |
mbed_official | 548:1abac31e188e | 55 | if (flags & RTC_IF_OF) { |
mbed_official | 525:c320967f86b9 | 56 | RTC_IntClear(RTC_IF_OF); |
mbed_official | 550:787824fdf8f9 | 57 | /* RTC has overflowed (24 bits). Use time_extend as software counter for 32 more bits. */ |
mbed_official | 550:787824fdf8f9 | 58 | time_extend += 1; |
mbed_official | 525:c320967f86b9 | 59 | } |
mbed_official | 548:1abac31e188e | 60 | if (flags & RTC_IF_COMP0) { |
mbed_official | 525:c320967f86b9 | 61 | RTC_IntClear(RTC_IF_COMP0); |
mbed_official | 548:1abac31e188e | 62 | if (comp0_handler != NULL) { |
mbed_official | 525:c320967f86b9 | 63 | comp0_handler(); |
mbed_official | 525:c320967f86b9 | 64 | } |
mbed_official | 525:c320967f86b9 | 65 | } |
mbed_official | 525:c320967f86b9 | 66 | } |
mbed_official | 525:c320967f86b9 | 67 | |
mbed_official | 550:787824fdf8f9 | 68 | uint32_t rtc_get_32bit(void) |
mbed_official | 550:787824fdf8f9 | 69 | { |
mbed_official | 550:787824fdf8f9 | 70 | return (RTC_CounterGet() + (time_extend << RTC_NUM_BITS)); |
mbed_official | 550:787824fdf8f9 | 71 | } |
mbed_official | 550:787824fdf8f9 | 72 | |
mbed_official | 550:787824fdf8f9 | 73 | uint64_t rtc_get_full(void) |
mbed_official | 550:787824fdf8f9 | 74 | { |
mbed_official | 550:787824fdf8f9 | 75 | uint64_t ticks = 0; |
mbed_official | 550:787824fdf8f9 | 76 | ticks += time_extend; |
mbed_official | 550:787824fdf8f9 | 77 | ticks = ticks << RTC_NUM_BITS; |
mbed_official | 550:787824fdf8f9 | 78 | ticks += RTC_CounterGet(); |
mbed_official | 550:787824fdf8f9 | 79 | return ticks; |
mbed_official | 550:787824fdf8f9 | 80 | } |
mbed_official | 550:787824fdf8f9 | 81 | |
mbed_official | 525:c320967f86b9 | 82 | void rtc_set_comp0_handler(uint32_t handler) |
mbed_official | 525:c320967f86b9 | 83 | { |
mbed_official | 525:c320967f86b9 | 84 | comp0_handler = (void (*)(void)) handler; |
mbed_official | 525:c320967f86b9 | 85 | } |
mbed_official | 525:c320967f86b9 | 86 | |
mbed_official | 548:1abac31e188e | 87 | void rtc_init(void) |
mbed_official | 525:c320967f86b9 | 88 | { |
mbed_official | 525:c320967f86b9 | 89 | /* Register that the RTC is used for timekeeping. */ |
mbed_official | 525:c320967f86b9 | 90 | rtc_init_real(RTC_INIT_RTC); |
mbed_official | 525:c320967f86b9 | 91 | } |
mbed_official | 525:c320967f86b9 | 92 | |
mbed_official | 525:c320967f86b9 | 93 | |
mbed_official | 525:c320967f86b9 | 94 | void rtc_init_real(uint32_t flags) |
mbed_official | 525:c320967f86b9 | 95 | { |
mbed_official | 525:c320967f86b9 | 96 | useflags |= flags; |
mbed_official | 525:c320967f86b9 | 97 | |
mbed_official | 548:1abac31e188e | 98 | if (!rtc_inited) { |
mbed_official | 525:c320967f86b9 | 99 | CMU_ClockEnable(cmuClock_RTC, true); |
mbed_official | 525:c320967f86b9 | 100 | |
mbed_official | 525:c320967f86b9 | 101 | /* Enable clock to the interface of the low energy modules */ |
mbed_official | 525:c320967f86b9 | 102 | CMU_ClockEnable(cmuClock_CORELE, true); |
mbed_official | 525:c320967f86b9 | 103 | |
mbed_official | 525:c320967f86b9 | 104 | /* Scale clock to save power */ |
mbed_official | 525:c320967f86b9 | 105 | CMU_ClockDivSet(cmuClock_RTC, RTC_CLOCKDIV); |
mbed_official | 525:c320967f86b9 | 106 | |
mbed_official | 525:c320967f86b9 | 107 | /* Initialize RTC */ |
mbed_official | 525:c320967f86b9 | 108 | RTC_Init_TypeDef init = RTC_INIT_DEFAULT; |
mbed_official | 525:c320967f86b9 | 109 | init.enable = 1; |
mbed_official | 525:c320967f86b9 | 110 | /* Don't use compare register 0 as top value */ |
mbed_official | 525:c320967f86b9 | 111 | init.comp0Top = 0; |
mbed_official | 525:c320967f86b9 | 112 | |
mbed_official | 525:c320967f86b9 | 113 | /* Enable Interrupt from RTC */ |
mbed_official | 525:c320967f86b9 | 114 | RTC_IntEnable(RTC_IEN_OF); |
mbed_official | 525:c320967f86b9 | 115 | NVIC_EnableIRQ(RTC_IRQn); |
mbed_official | 525:c320967f86b9 | 116 | NVIC_SetVector(RTC_IRQn, (uint32_t)RTC_IRQHandler); |
mbed_official | 525:c320967f86b9 | 117 | |
mbed_official | 525:c320967f86b9 | 118 | /* Initialize */ |
mbed_official | 525:c320967f86b9 | 119 | RTC_Init(&init); |
mbed_official | 525:c320967f86b9 | 120 | |
mbed_official | 525:c320967f86b9 | 121 | blockSleepMode(RTC_LEAST_ACTIVE_SLEEPMODE); |
mbed_official | 525:c320967f86b9 | 122 | rtc_inited = true; |
mbed_official | 525:c320967f86b9 | 123 | } |
mbed_official | 525:c320967f86b9 | 124 | } |
mbed_official | 525:c320967f86b9 | 125 | |
mbed_official | 548:1abac31e188e | 126 | void rtc_free(void) |
mbed_official | 525:c320967f86b9 | 127 | { |
mbed_official | 525:c320967f86b9 | 128 | rtc_free_real(RTC_INIT_RTC); |
mbed_official | 525:c320967f86b9 | 129 | } |
mbed_official | 525:c320967f86b9 | 130 | |
mbed_official | 525:c320967f86b9 | 131 | void rtc_free_real(uint32_t flags) |
mbed_official | 525:c320967f86b9 | 132 | { |
mbed_official | 525:c320967f86b9 | 133 | /* Clear use flag */ |
mbed_official | 627:4fa1328d9c60 | 134 | useflags &= ~flags; |
mbed_official | 525:c320967f86b9 | 135 | |
mbed_official | 525:c320967f86b9 | 136 | /* Disable the RTC if it was inited and is no longer in use by anyone. */ |
mbed_official | 627:4fa1328d9c60 | 137 | if (rtc_inited && (useflags == 0)) { |
mbed_official | 525:c320967f86b9 | 138 | NVIC_DisableIRQ(RTC_IRQn); |
mbed_official | 525:c320967f86b9 | 139 | RTC_Reset(); |
mbed_official | 525:c320967f86b9 | 140 | CMU_ClockEnable(cmuClock_RTC, false); |
mbed_official | 525:c320967f86b9 | 141 | unblockSleepMode(RTC_LEAST_ACTIVE_SLEEPMODE); |
mbed_official | 525:c320967f86b9 | 142 | rtc_inited = false; |
mbed_official | 525:c320967f86b9 | 143 | } |
mbed_official | 525:c320967f86b9 | 144 | } |
mbed_official | 525:c320967f86b9 | 145 | |
mbed_official | 525:c320967f86b9 | 146 | int rtc_isenabled(void) |
mbed_official | 525:c320967f86b9 | 147 | { |
mbed_official | 525:c320967f86b9 | 148 | return rtc_inited; |
mbed_official | 525:c320967f86b9 | 149 | } |
mbed_official | 525:c320967f86b9 | 150 | |
mbed_official | 525:c320967f86b9 | 151 | time_t rtc_read(void) |
mbed_official | 525:c320967f86b9 | 152 | { |
mbed_official | 550:787824fdf8f9 | 153 | return (time_t) (rtc_get_full() >> RTC_FREQ_SHIFT) + time_base; |
mbed_official | 550:787824fdf8f9 | 154 | } |
mbed_official | 550:787824fdf8f9 | 155 | |
mbed_official | 550:787824fdf8f9 | 156 | time_t rtc_read_uncompensated(void) |
mbed_official | 550:787824fdf8f9 | 157 | { |
mbed_official | 550:787824fdf8f9 | 158 | return (time_t) (rtc_get_full() >> RTC_FREQ_SHIFT); |
mbed_official | 525:c320967f86b9 | 159 | } |
mbed_official | 525:c320967f86b9 | 160 | |
mbed_official | 525:c320967f86b9 | 161 | void rtc_write(time_t t) |
mbed_official | 525:c320967f86b9 | 162 | { |
mbed_official | 525:c320967f86b9 | 163 | /* We have to check that the RTC did not tick while doing this. */ |
mbed_official | 525:c320967f86b9 | 164 | /* If the RTC ticks we just redo this. */ |
mbed_official | 550:787824fdf8f9 | 165 | uint32_t time; |
mbed_official | 525:c320967f86b9 | 166 | do { |
mbed_official | 550:787824fdf8f9 | 167 | time = rtc_read_uncompensated(); |
mbed_official | 550:787824fdf8f9 | 168 | time_base = t - time; |
mbed_official | 550:787824fdf8f9 | 169 | } while (time != rtc_read_uncompensated()); |
mbed_official | 525:c320967f86b9 | 170 | } |
mbed_official | 525:c320967f86b9 | 171 | |
mbed_official | 525:c320967f86b9 | 172 | #endif |