mbed library sources

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Fri Aug 29 17:15:07 2014 +0100
Revision:
304:89b9c3a9a045
Parent:
216:577900467c9e
Synchronized with git revision 734f365d7da26ef199751f4b0d91611479b495ea

Full URL: https://github.com/mbedmicro/mbed/commit/734f365d7da26ef199751f4b0d91611479b495ea/

1. timestamp_t as an abstraction for time values managed by
Ticker. Using uint64_t for timestamp_t allows a wraparound-free
Ticker. This change forces us to update the definitions of usTicker
for all platforms; but the changes beyond nRF51822 aren't major.

2. reduce power consumption on the nRF51822 by removing the need for
the high-frequency processor timer; and reimplementing it using the
RTC.

I've also replaced high-frequency clock with low-frequency external
clock during system startup of the nRF51822. This brings a major win
in power consumption.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 76:aeb1df146756 1 /* mbed Microcontroller Library
mbed_official 76:aeb1df146756 2 * Copyright (c) 2014, STMicroelectronics
mbed_official 76:aeb1df146756 3 * All rights reserved.
mbed_official 76:aeb1df146756 4 *
mbed_official 76:aeb1df146756 5 * Redistribution and use in source and binary forms, with or without
mbed_official 76:aeb1df146756 6 * modification, are permitted provided that the following conditions are met:
mbed_official 76:aeb1df146756 7 *
mbed_official 76:aeb1df146756 8 * 1. Redistributions of source code must retain the above copyright notice,
mbed_official 76:aeb1df146756 9 * this list of conditions and the following disclaimer.
mbed_official 76:aeb1df146756 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
mbed_official 76:aeb1df146756 11 * this list of conditions and the following disclaimer in the documentation
mbed_official 76:aeb1df146756 12 * and/or other materials provided with the distribution.
mbed_official 76:aeb1df146756 13 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mbed_official 76:aeb1df146756 14 * may be used to endorse or promote products derived from this software
mbed_official 76:aeb1df146756 15 * without specific prior written permission.
mbed_official 76:aeb1df146756 16 *
mbed_official 76:aeb1df146756 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 76:aeb1df146756 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mbed_official 76:aeb1df146756 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mbed_official 76:aeb1df146756 20 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mbed_official 76:aeb1df146756 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mbed_official 76:aeb1df146756 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mbed_official 76:aeb1df146756 23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mbed_official 76:aeb1df146756 24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mbed_official 76:aeb1df146756 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mbed_official 76:aeb1df146756 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mbed_official 76:aeb1df146756 27 */
mbed_official 76:aeb1df146756 28 #include <stddef.h>
mbed_official 76:aeb1df146756 29 #include "us_ticker_api.h"
mbed_official 76:aeb1df146756 30 #include "PeripheralNames.h"
mbed_official 76:aeb1df146756 31
mbed_official 84:f54042cbc282 32 // Timer selection:
mbed_official 84:f54042cbc282 33 #define TIM_MST TIM1
mbed_official 84:f54042cbc282 34 #define TIM_MST_UP_IRQ TIM1_BRK_UP_TRG_COM_IRQn
mbed_official 84:f54042cbc282 35 #define TIM_MST_OC_IRQ TIM1_CC_IRQn
mbed_official 84:f54042cbc282 36 #define TIM_MST_RCC RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE)
mbed_official 76:aeb1df146756 37
mbed_official 84:f54042cbc282 38 static int us_ticker_inited = 0;
mbed_official 92:05f19f05c134 39 static volatile uint32_t SlaveCounter = 0;
mbed_official 96:c359415e941f 40 static volatile uint32_t oc_int_part = 0;
mbed_official 96:c359415e941f 41 static volatile uint16_t oc_rem_part = 0;
mbed_official 91:0a39e62a0464 42
mbed_official 91:0a39e62a0464 43 void set_compare(uint16_t count) {
mbed_official 91:0a39e62a0464 44 // Set new output compare value
mbed_official 91:0a39e62a0464 45 TIM_SetCompare1(TIM_MST, count);
mbed_official 91:0a39e62a0464 46 // Enable IT
mbed_official 91:0a39e62a0464 47 TIM_ITConfig(TIM_MST, TIM_IT_CC1, ENABLE);
mbed_official 91:0a39e62a0464 48 }
mbed_official 84:f54042cbc282 49
mbed_official 84:f54042cbc282 50 // Used to increment the slave counter
mbed_official 84:f54042cbc282 51 static void tim_update_irq_handler(void) {
mbed_official 84:f54042cbc282 52 if (TIM_GetITStatus(TIM_MST, TIM_IT_Update) == SET) {
mbed_official 84:f54042cbc282 53 TIM_ClearITPendingBit(TIM_MST, TIM_IT_Update);
mbed_official 94:6519f69185ce 54 SlaveCounter++;
mbed_official 84:f54042cbc282 55 }
mbed_official 84:f54042cbc282 56 }
mbed_official 76:aeb1df146756 57
mbed_official 84:f54042cbc282 58 // Used by interrupt system
mbed_official 84:f54042cbc282 59 static void tim_oc_irq_handler(void) {
mbed_official 91:0a39e62a0464 60 uint16_t cval = TIM_MST->CNT;
mbed_official 216:577900467c9e 61
mbed_official 84:f54042cbc282 62 // Clear interrupt flag
mbed_official 84:f54042cbc282 63 if (TIM_GetITStatus(TIM_MST, TIM_IT_CC1) == SET) {
mbed_official 84:f54042cbc282 64 TIM_ClearITPendingBit(TIM_MST, TIM_IT_CC1);
mbed_official 84:f54042cbc282 65 }
mbed_official 91:0a39e62a0464 66
mbed_official 91:0a39e62a0464 67 if (oc_rem_part > 0) {
mbed_official 91:0a39e62a0464 68 set_compare(oc_rem_part); // Finish the remaining time left
mbed_official 91:0a39e62a0464 69 oc_rem_part = 0;
mbed_official 216:577900467c9e 70 } else {
mbed_official 91:0a39e62a0464 71 if (oc_int_part > 0) {
mbed_official 91:0a39e62a0464 72 set_compare(0xFFFF);
mbed_official 91:0a39e62a0464 73 oc_rem_part = cval; // To finish the counter loop the next time
mbed_official 91:0a39e62a0464 74 oc_int_part--;
mbed_official 216:577900467c9e 75 } else {
mbed_official 84:f54042cbc282 76 us_ticker_irq_handler();
mbed_official 84:f54042cbc282 77 }
mbed_official 91:0a39e62a0464 78 }
mbed_official 84:f54042cbc282 79 }
mbed_official 76:aeb1df146756 80
mbed_official 91:0a39e62a0464 81 void us_ticker_init(void) {
mbed_official 76:aeb1df146756 82 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
mbed_official 216:577900467c9e 83
mbed_official 76:aeb1df146756 84 if (us_ticker_inited) return;
mbed_official 76:aeb1df146756 85 us_ticker_inited = 1;
mbed_official 216:577900467c9e 86
mbed_official 84:f54042cbc282 87 // Enable Timer clock
mbed_official 76:aeb1df146756 88 TIM_MST_RCC;
mbed_official 216:577900467c9e 89
mbed_official 84:f54042cbc282 90 // Configure time base
mbed_official 76:aeb1df146756 91 TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
mbed_official 76:aeb1df146756 92 TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
mbed_official 304:89b9c3a9a045 93 TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 �s tick
mbed_official 76:aeb1df146756 94 TIM_TimeBaseStructure.TIM_ClockDivision = 0;
mbed_official 76:aeb1df146756 95 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
mbed_official 76:aeb1df146756 96 TIM_TimeBaseInit(TIM_MST, &TIM_TimeBaseStructure);
mbed_official 216:577900467c9e 97
mbed_official 84:f54042cbc282 98 // Configure interrupts
mbed_official 84:f54042cbc282 99 TIM_ITConfig(TIM_MST, TIM_IT_Update, ENABLE);
mbed_official 216:577900467c9e 100
mbed_official 91:0a39e62a0464 101 // Update interrupt used for 32-bit counter
mbed_official 84:f54042cbc282 102 NVIC_SetVector(TIM_MST_UP_IRQ, (uint32_t)tim_update_irq_handler);
mbed_official 84:f54042cbc282 103 NVIC_EnableIRQ(TIM_MST_UP_IRQ);
mbed_official 216:577900467c9e 104
mbed_official 91:0a39e62a0464 105 // Output compare interrupt used for timeout feature
mbed_official 84:f54042cbc282 106 NVIC_SetVector(TIM_MST_OC_IRQ, (uint32_t)tim_oc_irq_handler);
mbed_official 84:f54042cbc282 107 NVIC_EnableIRQ(TIM_MST_OC_IRQ);
mbed_official 216:577900467c9e 108
mbed_official 84:f54042cbc282 109 // Enable timer
mbed_official 76:aeb1df146756 110 TIM_Cmd(TIM_MST, ENABLE);
mbed_official 76:aeb1df146756 111 }
mbed_official 76:aeb1df146756 112
mbed_official 76:aeb1df146756 113 uint32_t us_ticker_read() {
mbed_official 76:aeb1df146756 114 uint32_t counter, counter2;
mbed_official 76:aeb1df146756 115 if (!us_ticker_inited) us_ticker_init();
mbed_official 76:aeb1df146756 116 // A situation might appear when Master overflows right after Slave is read and before the
mbed_official 76:aeb1df146756 117 // new (overflowed) value of Master is read. Which would make the code below consider the
mbed_official 76:aeb1df146756 118 // previous (incorrect) value of Slave and the new value of Master, which would return a
mbed_official 76:aeb1df146756 119 // value in the past. Avoid this by computing consecutive values of the timer until they
mbed_official 76:aeb1df146756 120 // are properly ordered.
mbed_official 84:f54042cbc282 121 counter = (uint32_t)(SlaveCounter << 16);
mbed_official 91:0a39e62a0464 122 counter += TIM_MST->CNT;
mbed_official 76:aeb1df146756 123 while (1) {
mbed_official 84:f54042cbc282 124 counter2 = (uint32_t)(SlaveCounter << 16);
mbed_official 91:0a39e62a0464 125 counter2 += TIM_MST->CNT;
mbed_official 76:aeb1df146756 126 if (counter2 > counter) {
mbed_official 76:aeb1df146756 127 break;
mbed_official 76:aeb1df146756 128 }
mbed_official 76:aeb1df146756 129 counter = counter2;
mbed_official 76:aeb1df146756 130 }
mbed_official 76:aeb1df146756 131 return counter2;
mbed_official 76:aeb1df146756 132 }
mbed_official 76:aeb1df146756 133
mbed_official 304:89b9c3a9a045 134 void us_ticker_set_interrupt(timestamp_t timestamp) {
mbed_official 304:89b9c3a9a045 135 int delta = (int)((uint32_t)timestamp - us_ticker_read());
mbed_official 91:0a39e62a0464 136 uint16_t cval = TIM_MST->CNT;
mbed_official 84:f54042cbc282 137
mbed_official 84:f54042cbc282 138 if (delta <= 0) { // This event was in the past
mbed_official 84:f54042cbc282 139 us_ticker_irq_handler();
mbed_official 216:577900467c9e 140 } else {
mbed_official 91:0a39e62a0464 141 oc_int_part = (uint32_t)(delta >> 16);
mbed_official 91:0a39e62a0464 142 oc_rem_part = (uint16_t)(delta & 0xFFFF);
mbed_official 91:0a39e62a0464 143 if (oc_rem_part <= (0xFFFF - cval)) {
mbed_official 91:0a39e62a0464 144 set_compare(cval + oc_rem_part);
mbed_official 91:0a39e62a0464 145 oc_rem_part = 0;
mbed_official 84:f54042cbc282 146 } else {
mbed_official 91:0a39e62a0464 147 set_compare(0xFFFF);
mbed_official 91:0a39e62a0464 148 oc_rem_part = oc_rem_part - (0xFFFF - cval);
mbed_official 84:f54042cbc282 149 }
mbed_official 76:aeb1df146756 150 }
mbed_official 76:aeb1df146756 151 }
mbed_official 76:aeb1df146756 152
mbed_official 76:aeb1df146756 153 void us_ticker_disable_interrupt(void) {
mbed_official 76:aeb1df146756 154 TIM_ITConfig(TIM_MST, TIM_IT_CC1, DISABLE);
mbed_official 76:aeb1df146756 155 }
mbed_official 76:aeb1df146756 156
mbed_official 76:aeb1df146756 157 void us_ticker_clear_interrupt(void) {
mbed_official 84:f54042cbc282 158 if (TIM_GetITStatus(TIM_MST, TIM_IT_CC1) == SET) {
mbed_official 84:f54042cbc282 159 TIM_ClearITPendingBit(TIM_MST, TIM_IT_CC1);
mbed_official 84:f54042cbc282 160 }
mbed_official 76:aeb1df146756 161 }