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.

Committer:
mbed_official
Date:
Tue Apr 28 13:00:10 2015 +0100
Revision:
526:7c4bdfe6a168
Parent:
525:c320967f86b9
Child:
627:4fa1328d9c60
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 #include <stddef.h>
mbed_official 525:c320967f86b9 17 #include "us_ticker_api.h"
mbed_official 525:c320967f86b9 18 #include "cmsis.h"
mbed_official 525:c320967f86b9 19 #include "mbed_assert.h"
mbed_official 525:c320967f86b9 20 #include "em_cmu.h"
mbed_official 525:c320967f86b9 21 #include "em_timer.h"
mbed_official 525:c320967f86b9 22 #include "device_peripherals.h"
mbed_official 525:c320967f86b9 23 #include "device.h"
mbed_official 525:c320967f86b9 24 #include "clocking.h"
mbed_official 525:c320967f86b9 25 #include "sleep_api.h"
mbed_official 526:7c4bdfe6a168 26 #include "sleepmodes.h"
mbed_official 525:c320967f86b9 27
mbed_official 525:c320967f86b9 28 #define TIMER_LEAST_ACTIVE_SLEEPMODE EM1
mbed_official 525:c320967f86b9 29 /**
mbed_official 525:c320967f86b9 30 * Timer functions for microsecond ticker.
mbed_official 525:c320967f86b9 31 * mbed expects a 32-bit timer. Since the EFM32 only has 16-bit timers,
mbed_official 525:c320967f86b9 32 * the upper 16 bits are implemented in software.
mbed_official 525:c320967f86b9 33 */
mbed_official 525:c320967f86b9 34
mbed_official 525:c320967f86b9 35 static uint8_t us_ticker_inited = 0; // Is ticker initialized yet
mbed_official 525:c320967f86b9 36
mbed_official 525:c320967f86b9 37 static volatile uint32_t ticker_cnt = 0x2ff00; //Internal overflow count, used to extend internal 16-bit counter to (MHz * 32-bit)
mbed_official 525:c320967f86b9 38 static volatile uint16_t ticker_int_rem = 0; //Timer match value for user interrupt
mbed_official 525:c320967f86b9 39 static volatile uint32_t ticker_int_cnt = 0; //Amount of overflows until user interrupt
mbed_official 525:c320967f86b9 40 static volatile uint8_t ticker_freq_mhz = 0; //Frequency of timer in MHz
mbed_official 525:c320967f86b9 41
mbed_official 525:c320967f86b9 42 void us_ticker_irq_handler_internal(void)
mbed_official 525:c320967f86b9 43 {
mbed_official 525:c320967f86b9 44 /* Check for user interrupt expiration */
mbed_official 525:c320967f86b9 45 if (TIMER_IntGet(US_TICKER_TIMER) & TIMER_IF_CC0) {
mbed_official 525:c320967f86b9 46 if (ticker_int_rem > 0) {
mbed_official 525:c320967f86b9 47 TIMER_CompareSet(US_TICKER_TIMER, 0, ticker_int_rem);
mbed_official 525:c320967f86b9 48 ticker_int_rem = 0;
mbed_official 525:c320967f86b9 49 TIMER_IntClear(US_TICKER_TIMER, TIMER_IF_CC0);
mbed_official 525:c320967f86b9 50 } else if (ticker_int_cnt > 0) {
mbed_official 525:c320967f86b9 51 ticker_int_cnt--;
mbed_official 525:c320967f86b9 52 TIMER_IntClear(US_TICKER_TIMER, TIMER_IF_CC0);
mbed_official 525:c320967f86b9 53 } else {
mbed_official 525:c320967f86b9 54 us_ticker_irq_handler();
mbed_official 525:c320967f86b9 55 }
mbed_official 525:c320967f86b9 56 }
mbed_official 525:c320967f86b9 57
mbed_official 525:c320967f86b9 58 /* Handle timer overflow */
mbed_official 525:c320967f86b9 59 if (TIMER_IntGet(US_TICKER_TIMER) & TIMER_IF_OF) {
mbed_official 525:c320967f86b9 60 ticker_cnt++;
mbed_official 525:c320967f86b9 61 if(ticker_cnt >= (((uint32_t)ticker_freq_mhz) << 16)) ticker_cnt = 0;
mbed_official 525:c320967f86b9 62 TIMER_IntClear(US_TICKER_TIMER, TIMER_IF_OF);
mbed_official 525:c320967f86b9 63 }
mbed_official 525:c320967f86b9 64 }
mbed_official 525:c320967f86b9 65
mbed_official 525:c320967f86b9 66 void us_ticker_init(void)
mbed_official 525:c320967f86b9 67 {
mbed_official 525:c320967f86b9 68 if (us_ticker_inited) {
mbed_official 525:c320967f86b9 69 return;
mbed_official 525:c320967f86b9 70 }
mbed_official 525:c320967f86b9 71 us_ticker_inited = 1;
mbed_official 525:c320967f86b9 72
mbed_official 525:c320967f86b9 73 /* Enable clock for TIMERs */
mbed_official 525:c320967f86b9 74 CMU_ClockEnable(US_TICKER_TIMER_CLOCK, true);
mbed_official 525:c320967f86b9 75
mbed_official 525:c320967f86b9 76 /* Clear TIMER counter value */
mbed_official 525:c320967f86b9 77 TIMER_CounterSet(US_TICKER_TIMER, 0);
mbed_official 525:c320967f86b9 78
mbed_official 525:c320967f86b9 79 /* Get frequency of clock in MHz for scaling ticks to microseconds */
mbed_official 525:c320967f86b9 80 ticker_freq_mhz = (REFERENCE_FREQUENCY / 1000000);
mbed_official 525:c320967f86b9 81 MBED_ASSERT(ticker_freq_mhz > 0);
mbed_official 525:c320967f86b9 82
mbed_official 525:c320967f86b9 83 /*
mbed_official 525:c320967f86b9 84 * Calculate maximum prescaler that gives at least 1 MHz frequency, while keeping clock as an integer multiple of 1 MHz.
mbed_official 525:c320967f86b9 85 * Example: 14 MHz => prescaler = 1 (i.e. DIV2), ticker_freq_mhz = 7;
mbed_official 525:c320967f86b9 86 * 24 MHz => prescaler = 3 (i.e. DIV8), ticker_freq_mhz = 3;
mbed_official 525:c320967f86b9 87 * 48 MHz => prescaler = 4 (i.e. DIV16), ticker_freq_mhz = 3;
mbed_official 525:c320967f86b9 88 * Limit prescaling to maximum prescaler value, which is 10 (DIV1024).
mbed_official 525:c320967f86b9 89 */
mbed_official 525:c320967f86b9 90 uint32_t prescaler = 0;
mbed_official 525:c320967f86b9 91 while((ticker_freq_mhz & 1) == 0 && prescaler <= 10) {
mbed_official 525:c320967f86b9 92 ticker_freq_mhz = ticker_freq_mhz >> 1;
mbed_official 525:c320967f86b9 93 prescaler++;
mbed_official 525:c320967f86b9 94 }
mbed_official 525:c320967f86b9 95
mbed_official 525:c320967f86b9 96 /* Set prescaler */
mbed_official 525:c320967f86b9 97 US_TICKER_TIMER->CTRL = (US_TICKER_TIMER->CTRL & ~_TIMER_CTRL_PRESC_MASK) | (prescaler << _TIMER_CTRL_PRESC_SHIFT);
mbed_official 525:c320967f86b9 98
mbed_official 525:c320967f86b9 99 /* Select Compare Channel parameters */
mbed_official 525:c320967f86b9 100 TIMER_InitCC_TypeDef timerCCInit = TIMER_INITCC_DEFAULT;
mbed_official 525:c320967f86b9 101 timerCCInit.mode = timerCCModeCompare;
mbed_official 525:c320967f86b9 102
mbed_official 525:c320967f86b9 103 /* Configure Compare Channel 0 */
mbed_official 525:c320967f86b9 104 TIMER_InitCC(US_TICKER_TIMER, 0, &timerCCInit);
mbed_official 525:c320967f86b9 105
mbed_official 525:c320967f86b9 106 /* Enable interrupt vector in NVIC */
mbed_official 525:c320967f86b9 107 TIMER_IntEnable(US_TICKER_TIMER, TIMER_IEN_OF);
mbed_official 525:c320967f86b9 108 NVIC_SetVector(US_TICKER_TIMER_IRQ, (uint32_t) us_ticker_irq_handler_internal);
mbed_official 525:c320967f86b9 109 NVIC_EnableIRQ(US_TICKER_TIMER_IRQ);
mbed_official 525:c320967f86b9 110
mbed_official 525:c320967f86b9 111 /* Set top value */
mbed_official 525:c320967f86b9 112 TIMER_TopSet(US_TICKER_TIMER, 0xFFFF);
mbed_official 525:c320967f86b9 113
mbed_official 525:c320967f86b9 114 /* Start TIMER */
mbed_official 525:c320967f86b9 115 TIMER_Enable(US_TICKER_TIMER, true);
mbed_official 525:c320967f86b9 116 }
mbed_official 525:c320967f86b9 117
mbed_official 525:c320967f86b9 118 uint32_t us_ticker_read()
mbed_official 525:c320967f86b9 119 {
mbed_official 525:c320967f86b9 120 uint32_t volatile countH_old, countH, countL;
mbed_official 525:c320967f86b9 121
mbed_official 525:c320967f86b9 122 if (!us_ticker_inited) {
mbed_official 525:c320967f86b9 123 us_ticker_init();
mbed_official 525:c320967f86b9 124 }
mbed_official 525:c320967f86b9 125
mbed_official 525:c320967f86b9 126 /* Avoid jumping in time by reading high bits twice */
mbed_official 525:c320967f86b9 127 do {
mbed_official 525:c320967f86b9 128 countH_old = ticker_cnt;
mbed_official 525:c320967f86b9 129 /* If the counter overflowed while in the IRQ handler for the CC0 interrupt,
mbed_official 525:c320967f86b9 130 * it hasn't had time to update ticker_cnt yet. Take this into account here. */
mbed_official 525:c320967f86b9 131 if (TIMER_IntGet(US_TICKER_TIMER) & TIMER_IF_OF) {
mbed_official 525:c320967f86b9 132 countH_old += 1;
mbed_official 525:c320967f86b9 133 }
mbed_official 525:c320967f86b9 134 countL = US_TICKER_TIMER->CNT;
mbed_official 525:c320967f86b9 135 countH = ticker_cnt;
mbed_official 525:c320967f86b9 136 /* If the counter overflowed while in the IRQ handler for the CC0 interrupt,
mbed_official 525:c320967f86b9 137 * it hasn't had time to update ticker_cnt yet. Take this into account here. */
mbed_official 525:c320967f86b9 138 if (TIMER_IntGet(US_TICKER_TIMER) & TIMER_IF_OF) {
mbed_official 525:c320967f86b9 139 countH += 1;
mbed_official 525:c320967f86b9 140 }
mbed_official 525:c320967f86b9 141 } while (countH_old != countH);
mbed_official 525:c320967f86b9 142
mbed_official 525:c320967f86b9 143 /* Merge upper (mhz * 16-bit) and lower 16-bit into 64bit */
mbed_official 525:c320967f86b9 144 uint64_t count = ((uint64_t)countH << 16) | (uint64_t)countL;
mbed_official 525:c320967f86b9 145 /* Divide by ticker_freq_mhz to get 32-bit 1MHz timestamp */
mbed_official 525:c320967f86b9 146 return (count / ticker_freq_mhz);
mbed_official 525:c320967f86b9 147 }
mbed_official 525:c320967f86b9 148
mbed_official 525:c320967f86b9 149 void us_ticker_set_interrupt(timestamp_t timestamp)
mbed_official 525:c320967f86b9 150 {
mbed_official 525:c320967f86b9 151 int32_t delta = 0, ts = timestamp, time = us_ticker_read();
mbed_official 525:c320967f86b9 152
mbed_official 525:c320967f86b9 153 if((US_TICKER_TIMER->IEN & TIMER_IEN_CC0) == 0) {
mbed_official 525:c320967f86b9 154 //Timer was disabled, but is going to be enabled. Set sleep mode.
mbed_official 525:c320967f86b9 155 blockSleepMode(TIMER_LEAST_ACTIVE_SLEEPMODE);
mbed_official 525:c320967f86b9 156 }
mbed_official 525:c320967f86b9 157 TIMER_IntDisable(US_TICKER_TIMER, TIMER_IEN_CC0);
mbed_official 525:c320967f86b9 158
mbed_official 525:c320967f86b9 159 delta = ts - time;
mbed_official 525:c320967f86b9 160 if(delta <= ticker_freq_mhz) {
mbed_official 525:c320967f86b9 161 delta = ticker_freq_mhz;
mbed_official 525:c320967f86b9 162 timestamp = us_ticker_read() + 0x100;
mbed_official 525:c320967f86b9 163 }
mbed_official 525:c320967f86b9 164
mbed_official 525:c320967f86b9 165 /* Multiply by ticker_freq_mhz to get clock ticks */
mbed_official 525:c320967f86b9 166 delta *= ticker_freq_mhz;
mbed_official 525:c320967f86b9 167 /* Overflowing this doesn't matter, since we only need the lower 16 bits */
mbed_official 525:c320967f86b9 168 ts *= ticker_freq_mhz;
mbed_official 525:c320967f86b9 169
mbed_official 525:c320967f86b9 170 /* Split delta between timers */
mbed_official 525:c320967f86b9 171 ticker_int_cnt = (((uint64_t)delta) >> 16) & 0xFFFFFFFF;
mbed_official 525:c320967f86b9 172 ticker_int_rem = ts & 0xFFFF;
mbed_official 525:c320967f86b9 173
mbed_official 525:c320967f86b9 174 /* Set compare channel 0 to (current position + lower 16 bits of delta).
mbed_official 525:c320967f86b9 175 * If lower 16 bits is a small number, we a do one compare of (current + lower 16 + 0x8000)
mbed_official 525:c320967f86b9 176 * and then one of (current + lower 16). Else, we simply use (current + lower 16).
mbed_official 525:c320967f86b9 177 *
mbed_official 525:c320967f86b9 178 * When time from lower 16 bits have elapsed, run complete cycles with ticker_int_rem as
mbed_official 525:c320967f86b9 179 * reference ticker_int_cnt times. */
mbed_official 525:c320967f86b9 180 if ((delta & 0xFFFF) < 0x8000 && ticker_int_cnt > 0) {
mbed_official 525:c320967f86b9 181 TIMER_CompareSet(US_TICKER_TIMER, 0, ticker_int_rem + 0x8000);
mbed_official 525:c320967f86b9 182 ticker_int_cnt--;
mbed_official 525:c320967f86b9 183 } else {
mbed_official 525:c320967f86b9 184 TIMER_CompareSet(US_TICKER_TIMER, 0, ticker_int_rem);
mbed_official 525:c320967f86b9 185 ticker_int_rem = 0;
mbed_official 525:c320967f86b9 186 }
mbed_official 525:c320967f86b9 187 TIMER_IntClear(US_TICKER_TIMER, TIMER_IFC_CC0);
mbed_official 525:c320967f86b9 188 TIMER_IntEnable(US_TICKER_TIMER, TIMER_IEN_CC0);
mbed_official 525:c320967f86b9 189 }
mbed_official 525:c320967f86b9 190
mbed_official 525:c320967f86b9 191 void us_ticker_disable_interrupt(void)
mbed_official 525:c320967f86b9 192 {
mbed_official 525:c320967f86b9 193 if((US_TICKER_TIMER->IEN & TIMER_IEN_CC0) != 0) {
mbed_official 525:c320967f86b9 194 //Timer was enabled, but is going to get disabled. Clear sleepmode.
mbed_official 525:c320967f86b9 195 unblockSleepMode(TIMER_LEAST_ACTIVE_SLEEPMODE);
mbed_official 525:c320967f86b9 196 }
mbed_official 525:c320967f86b9 197 /* Disable compare channel interrupts */
mbed_official 525:c320967f86b9 198 TIMER_IntDisable(US_TICKER_TIMER, TIMER_IEN_CC0);
mbed_official 525:c320967f86b9 199 }
mbed_official 525:c320967f86b9 200
mbed_official 525:c320967f86b9 201 void us_ticker_clear_interrupt(void)
mbed_official 525:c320967f86b9 202 {
mbed_official 525:c320967f86b9 203 /* Clear compare channel interrupts */
mbed_official 525:c320967f86b9 204 TIMER_IntClear(US_TICKER_TIMER, TIMER_IFC_CC0);
mbed_official 525:c320967f86b9 205 }