mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /* mbed Microcontroller Library
elessair 0:f269e3021894 2 * Copyright (c) 2014, STMicroelectronics
elessair 0:f269e3021894 3 * All rights reserved.
elessair 0:f269e3021894 4 *
elessair 0:f269e3021894 5 * Redistribution and use in source and binary forms, with or without
elessair 0:f269e3021894 6 * modification, are permitted provided that the following conditions are met:
elessair 0:f269e3021894 7 *
elessair 0:f269e3021894 8 * 1. Redistributions of source code must retain the above copyright notice,
elessair 0:f269e3021894 9 * this list of conditions and the following disclaimer.
elessair 0:f269e3021894 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
elessair 0:f269e3021894 11 * this list of conditions and the following disclaimer in the documentation
elessair 0:f269e3021894 12 * and/or other materials provided with the distribution.
elessair 0:f269e3021894 13 * 3. Neither the name of STMicroelectronics nor the names of its contributors
elessair 0:f269e3021894 14 * may be used to endorse or promote products derived from this software
elessair 0:f269e3021894 15 * without specific prior written permission.
elessair 0:f269e3021894 16 *
elessair 0:f269e3021894 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
elessair 0:f269e3021894 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
elessair 0:f269e3021894 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
elessair 0:f269e3021894 20 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
elessair 0:f269e3021894 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
elessair 0:f269e3021894 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
elessair 0:f269e3021894 23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
elessair 0:f269e3021894 24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
elessair 0:f269e3021894 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
elessair 0:f269e3021894 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
elessair 0:f269e3021894 27 */
elessair 0:f269e3021894 28 #include <stddef.h>
elessair 0:f269e3021894 29 #include "us_ticker_api.h"
elessair 0:f269e3021894 30 #include "PeripheralNames.h"
elessair 0:f269e3021894 31
elessair 0:f269e3021894 32
elessair 0:f269e3021894 33 #if defined(TARGET_STM32F030R8) || defined(TARGET_STM32F070RB)
elessair 0:f269e3021894 34
elessair 0:f269e3021894 35 // Timer selection
elessair 0:f269e3021894 36 #define TIM_MST TIM1
elessair 0:f269e3021894 37
elessair 0:f269e3021894 38 static TIM_HandleTypeDef TimMasterHandle;
elessair 0:f269e3021894 39 static int us_ticker_inited = 0;
elessair 0:f269e3021894 40
elessair 0:f269e3021894 41 volatile uint32_t SlaveCounter = 0;
elessair 0:f269e3021894 42 volatile uint32_t oc_int_part = 0;
elessair 0:f269e3021894 43 volatile uint16_t oc_rem_part = 0;
elessair 0:f269e3021894 44
elessair 0:f269e3021894 45 void set_compare(uint16_t count) {
elessair 0:f269e3021894 46 TimMasterHandle.Instance = TIM_MST;
elessair 0:f269e3021894 47 // Set new output compare value
elessair 0:f269e3021894 48 __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, count);
elessair 0:f269e3021894 49 // Enable IT
elessair 0:f269e3021894 50 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1);
elessair 0:f269e3021894 51 }
elessair 0:f269e3021894 52
elessair 0:f269e3021894 53 void us_ticker_init(void) {
elessair 0:f269e3021894 54 if (us_ticker_inited) return;
elessair 0:f269e3021894 55 us_ticker_inited = 1;
elessair 0:f269e3021894 56
elessair 0:f269e3021894 57 HAL_InitTick(0); // The passed value is not used
elessair 0:f269e3021894 58 }
elessair 0:f269e3021894 59
elessair 0:f269e3021894 60 uint32_t us_ticker_read() {
elessair 0:f269e3021894 61 uint32_t counter, counter2;
elessair 0:f269e3021894 62 if (!us_ticker_inited) us_ticker_init();
elessair 0:f269e3021894 63 // A situation might appear when Master overflows right after Slave is read and before the
elessair 0:f269e3021894 64 // new (overflowed) value of Master is read. Which would make the code below consider the
elessair 0:f269e3021894 65 // previous (incorrect) value of Slave and the new value of Master, which would return a
elessair 0:f269e3021894 66 // value in the past. Avoid this by computing consecutive values of the timer until they
elessair 0:f269e3021894 67 // are properly ordered.
elessair 0:f269e3021894 68 counter = (uint32_t)(SlaveCounter << 16);
elessair 0:f269e3021894 69 counter += TIM_MST->CNT;
elessair 0:f269e3021894 70 while (1) {
elessair 0:f269e3021894 71 counter2 = (uint32_t)(SlaveCounter << 16);
elessair 0:f269e3021894 72 counter2 += TIM_MST->CNT;
elessair 0:f269e3021894 73 if (counter2 > counter) {
elessair 0:f269e3021894 74 break;
elessair 0:f269e3021894 75 }
elessair 0:f269e3021894 76 counter = counter2;
elessair 0:f269e3021894 77 }
elessair 0:f269e3021894 78 return counter2;
elessair 0:f269e3021894 79 }
elessair 0:f269e3021894 80
elessair 0:f269e3021894 81 void us_ticker_set_interrupt(timestamp_t timestamp) {
elessair 0:f269e3021894 82 int delta = (int)((uint32_t)timestamp - us_ticker_read());
elessair 0:f269e3021894 83 uint16_t cval = TIM_MST->CNT;
elessair 0:f269e3021894 84
elessair 0:f269e3021894 85 if (delta <= 0) { // This event was in the past
elessair 0:f269e3021894 86 us_ticker_irq_handler();
elessair 0:f269e3021894 87 } else {
elessair 0:f269e3021894 88 oc_int_part = (uint32_t)(delta >> 16);
elessair 0:f269e3021894 89 oc_rem_part = (uint16_t)(delta & 0xFFFF);
elessair 0:f269e3021894 90 if (oc_rem_part <= (0xFFFF - cval)) {
elessair 0:f269e3021894 91 set_compare(cval + oc_rem_part);
elessair 0:f269e3021894 92 oc_rem_part = 0;
elessair 0:f269e3021894 93 } else {
elessair 0:f269e3021894 94 set_compare(0xFFFF);
elessair 0:f269e3021894 95 oc_rem_part = oc_rem_part - (0xFFFF - cval);
elessair 0:f269e3021894 96 }
elessair 0:f269e3021894 97 }
elessair 0:f269e3021894 98 }
elessair 0:f269e3021894 99
elessair 0:f269e3021894 100 void us_ticker_disable_interrupt(void) {
elessair 0:f269e3021894 101 TimMasterHandle.Instance = TIM_MST;
elessair 0:f269e3021894 102 __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
elessair 0:f269e3021894 103 }
elessair 0:f269e3021894 104
elessair 0:f269e3021894 105 void us_ticker_clear_interrupt(void) {
elessair 0:f269e3021894 106 TimMasterHandle.Instance = TIM_MST;
elessair 0:f269e3021894 107 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) {
elessair 0:f269e3021894 108 __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1);
elessair 0:f269e3021894 109 }
elessair 0:f269e3021894 110 }
elessair 0:f269e3021894 111
elessair 0:f269e3021894 112 #elif defined (TARGET_STM32F051R8)
elessair 0:f269e3021894 113
elessair 0:f269e3021894 114 // Timer selection:
elessair 0:f269e3021894 115 #define TIM_MST TIM1
elessair 0:f269e3021894 116 #define TIM_MST_UP_IRQ TIM1_BRK_UP_TRG_COM_IRQn
elessair 0:f269e3021894 117 #define TIM_MST_OC_IRQ TIM1_CC_IRQn
elessair 0:f269e3021894 118 #define TIM_MST_RCC __TIM1_CLK_ENABLE()
elessair 0:f269e3021894 119
elessair 0:f269e3021894 120 static TIM_HandleTypeDef TimMasterHandle;
elessair 0:f269e3021894 121
elessair 0:f269e3021894 122
elessair 0:f269e3021894 123 static int us_ticker_inited = 0;
elessair 0:f269e3021894 124 static volatile uint32_t SlaveCounter = 0;
elessair 0:f269e3021894 125 static volatile uint32_t oc_int_part = 0;
elessair 0:f269e3021894 126 static volatile uint16_t oc_rem_part = 0;
elessair 0:f269e3021894 127
elessair 0:f269e3021894 128 void set_compare(uint16_t count) {
elessair 0:f269e3021894 129 TimMasterHandle.Instance = TIM_MST;
elessair 0:f269e3021894 130
elessair 0:f269e3021894 131 // Set new output compare value
elessair 0:f269e3021894 132 __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, count);
elessair 0:f269e3021894 133 // Enable IT
elessair 0:f269e3021894 134 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1);
elessair 0:f269e3021894 135 }
elessair 0:f269e3021894 136
elessair 0:f269e3021894 137 // Used to increment the slave counter
elessair 0:f269e3021894 138 static void tim_update_irq_handler(void) {
elessair 0:f269e3021894 139 TimMasterHandle.Instance = TIM_MST;
elessair 0:f269e3021894 140
elessair 0:f269e3021894 141 // Clear Update interrupt flag
elessair 0:f269e3021894 142 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_UPDATE) == SET) {
elessair 0:f269e3021894 143 __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_UPDATE);
elessair 0:f269e3021894 144 SlaveCounter++;
elessair 0:f269e3021894 145 }
elessair 0:f269e3021894 146 }
elessair 0:f269e3021894 147
elessair 0:f269e3021894 148 // Used by interrupt system
elessair 0:f269e3021894 149 static void tim_oc_irq_handler(void) {
elessair 0:f269e3021894 150 uint16_t cval = TIM_MST->CNT;
elessair 0:f269e3021894 151 TimMasterHandle.Instance = TIM_MST;
elessair 0:f269e3021894 152
elessair 0:f269e3021894 153 // Clear CC1 interrupt flag
elessair 0:f269e3021894 154 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) {
elessair 0:f269e3021894 155 __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1);
elessair 0:f269e3021894 156 }
elessair 0:f269e3021894 157 if (oc_rem_part > 0) {
elessair 0:f269e3021894 158 set_compare(oc_rem_part); // Finish the remaining time left
elessair 0:f269e3021894 159 oc_rem_part = 0;
elessair 0:f269e3021894 160 } else {
elessair 0:f269e3021894 161 if (oc_int_part > 0) {
elessair 0:f269e3021894 162 set_compare(0xFFFF);
elessair 0:f269e3021894 163 oc_rem_part = cval; // To finish the counter loop the next time
elessair 0:f269e3021894 164 oc_int_part--;
elessair 0:f269e3021894 165 } else {
elessair 0:f269e3021894 166 us_ticker_irq_handler();
elessair 0:f269e3021894 167 }
elessair 0:f269e3021894 168 }
elessair 0:f269e3021894 169
elessair 0:f269e3021894 170 }
elessair 0:f269e3021894 171
elessair 0:f269e3021894 172 void us_ticker_init(void) {
elessair 0:f269e3021894 173
elessair 0:f269e3021894 174 if (us_ticker_inited) return;
elessair 0:f269e3021894 175 us_ticker_inited = 1;
elessair 0:f269e3021894 176
elessair 0:f269e3021894 177 // Enable timer clock
elessair 0:f269e3021894 178 TIM_MST_RCC;
elessair 0:f269e3021894 179
elessair 0:f269e3021894 180 // Configure time base
elessair 0:f269e3021894 181 TimMasterHandle.Instance = TIM_MST;
elessair 0:f269e3021894 182 TimMasterHandle.Init.Period = 0xFFFF;
elessair 0:f269e3021894 183 TimMasterHandle.Init.Prescaler = (uint32_t)(SystemCoreClock / 1000000) - 1; // 1 us tick
elessair 0:f269e3021894 184 TimMasterHandle.Init.ClockDivision = 0;
elessair 0:f269e3021894 185 TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
elessair 0:f269e3021894 186 HAL_TIM_Base_Init(&TimMasterHandle);
elessair 0:f269e3021894 187
elessair 0:f269e3021894 188 // Configure interrupts
elessair 0:f269e3021894 189 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_UPDATE);
elessair 0:f269e3021894 190
elessair 0:f269e3021894 191 // Update interrupt used for 32-bit counter
elessair 0:f269e3021894 192 NVIC_SetVector(TIM_MST_UP_IRQ, (uint32_t)tim_update_irq_handler);
elessair 0:f269e3021894 193 NVIC_EnableIRQ(TIM_MST_UP_IRQ);
elessair 0:f269e3021894 194
elessair 0:f269e3021894 195 // Output compare interrupt used for timeout feature
elessair 0:f269e3021894 196 NVIC_SetVector(TIM_MST_OC_IRQ, (uint32_t)tim_oc_irq_handler);
elessair 0:f269e3021894 197 NVIC_EnableIRQ(TIM_MST_OC_IRQ);
elessair 0:f269e3021894 198
elessair 0:f269e3021894 199 // Enable timer
elessair 0:f269e3021894 200 HAL_TIM_Base_Start(&TimMasterHandle);
elessair 0:f269e3021894 201 }
elessair 0:f269e3021894 202
elessair 0:f269e3021894 203 uint32_t us_ticker_read() {
elessair 0:f269e3021894 204 uint32_t counter, counter2;
elessair 0:f269e3021894 205 if (!us_ticker_inited) us_ticker_init();
elessair 0:f269e3021894 206 // A situation might appear when Master overflows right after Slave is read and before the
elessair 0:f269e3021894 207 // new (overflowed) value of Master is read. Which would make the code below consider the
elessair 0:f269e3021894 208 // previous (incorrect) value of Slave and the new value of Master, which would return a
elessair 0:f269e3021894 209 // value in the past. Avoid this by computing consecutive values of the timer until they
elessair 0:f269e3021894 210 // are properly ordered.
elessair 0:f269e3021894 211 counter = (uint32_t)(SlaveCounter << 16);
elessair 0:f269e3021894 212 counter += TIM_MST->CNT;
elessair 0:f269e3021894 213 while (1) {
elessair 0:f269e3021894 214 counter2 = (uint32_t)(SlaveCounter << 16);
elessair 0:f269e3021894 215 counter2 += TIM_MST->CNT;
elessair 0:f269e3021894 216 if (counter2 > counter) {
elessair 0:f269e3021894 217 break;
elessair 0:f269e3021894 218 }
elessair 0:f269e3021894 219 counter = counter2;
elessair 0:f269e3021894 220 }
elessair 0:f269e3021894 221 return counter2;
elessair 0:f269e3021894 222 }
elessair 0:f269e3021894 223
elessair 0:f269e3021894 224 void us_ticker_set_interrupt(timestamp_t timestamp) {
elessair 0:f269e3021894 225 int delta = (int)((uint32_t)timestamp - us_ticker_read());
elessair 0:f269e3021894 226 uint16_t cval = TIM_MST->CNT;
elessair 0:f269e3021894 227
elessair 0:f269e3021894 228 if (delta <= 0) { // This event was in the past
elessair 0:f269e3021894 229 us_ticker_irq_handler();
elessair 0:f269e3021894 230 } else {
elessair 0:f269e3021894 231 oc_int_part = (uint32_t)(delta >> 16);
elessair 0:f269e3021894 232 oc_rem_part = (uint16_t)(delta & 0xFFFF);
elessair 0:f269e3021894 233 if (oc_rem_part <= (0xFFFF - cval)) {
elessair 0:f269e3021894 234 set_compare(cval + oc_rem_part);
elessair 0:f269e3021894 235 oc_rem_part = 0;
elessair 0:f269e3021894 236 } else {
elessair 0:f269e3021894 237 set_compare(0xFFFF);
elessair 0:f269e3021894 238 oc_rem_part = oc_rem_part - (0xFFFF - cval);
elessair 0:f269e3021894 239 }
elessair 0:f269e3021894 240 }
elessair 0:f269e3021894 241 }
elessair 0:f269e3021894 242
elessair 0:f269e3021894 243 void us_ticker_disable_interrupt(void) {
elessair 0:f269e3021894 244 TimMasterHandle.Instance = TIM_MST;
elessair 0:f269e3021894 245 __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
elessair 0:f269e3021894 246 }
elessair 0:f269e3021894 247
elessair 0:f269e3021894 248 void us_ticker_clear_interrupt(void) {
elessair 0:f269e3021894 249 TimMasterHandle.Instance = TIM_MST;
elessair 0:f269e3021894 250 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) {
elessair 0:f269e3021894 251 __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1);
elessair 0:f269e3021894 252 }
elessair 0:f269e3021894 253 }
elessair 0:f269e3021894 254
elessair 0:f269e3021894 255 #else
elessair 0:f269e3021894 256
elessair 0:f269e3021894 257 // 32-bit timer selection
elessair 0:f269e3021894 258 #define TIM_MST TIM2
elessair 0:f269e3021894 259
elessair 0:f269e3021894 260 static TIM_HandleTypeDef TimMasterHandle;
elessair 0:f269e3021894 261 static int us_ticker_inited = 0;
elessair 0:f269e3021894 262
elessair 0:f269e3021894 263 void us_ticker_init(void) {
elessair 0:f269e3021894 264 if (us_ticker_inited) return;
elessair 0:f269e3021894 265 us_ticker_inited = 1;
elessair 0:f269e3021894 266
elessair 0:f269e3021894 267 TimMasterHandle.Instance = TIM_MST;
elessair 0:f269e3021894 268
elessair 0:f269e3021894 269 HAL_InitTick(0); // The passed value is not used
elessair 0:f269e3021894 270 }
elessair 0:f269e3021894 271
elessair 0:f269e3021894 272 uint32_t us_ticker_read() {
elessair 0:f269e3021894 273 if (!us_ticker_inited) us_ticker_init();
elessair 0:f269e3021894 274 return TIM_MST->CNT;
elessair 0:f269e3021894 275 }
elessair 0:f269e3021894 276
elessair 0:f269e3021894 277 void us_ticker_set_interrupt(timestamp_t timestamp) {
elessair 0:f269e3021894 278 // Set new output compare value
elessair 0:f269e3021894 279 __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, (uint32_t)timestamp);
elessair 0:f269e3021894 280 // Enable IT
elessair 0:f269e3021894 281 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1);
elessair 0:f269e3021894 282 }
elessair 0:f269e3021894 283
elessair 0:f269e3021894 284 void us_ticker_disable_interrupt(void) {
elessair 0:f269e3021894 285 __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
elessair 0:f269e3021894 286 }
elessair 0:f269e3021894 287
elessair 0:f269e3021894 288 void us_ticker_clear_interrupt(void) {
elessair 0:f269e3021894 289 __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1);
elessair 0:f269e3021894 290 }
elessair 0:f269e3021894 291 #endif