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