inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
NYX 0:85b3fd62ea1a 1 /* mbed Microcontroller Library
NYX 0:85b3fd62ea1a 2 * Copyright (c) 2006-2016 ARM Limited
NYX 0:85b3fd62ea1a 3 *
NYX 0:85b3fd62ea1a 4 * Licensed under the Apache License, Version 2.0 (the "License");
NYX 0:85b3fd62ea1a 5 * you may not use this file except in compliance with the License.
NYX 0:85b3fd62ea1a 6 * You may obtain a copy of the License at
NYX 0:85b3fd62ea1a 7 *
NYX 0:85b3fd62ea1a 8 * http://www.apache.org/licenses/LICENSE-2.0
NYX 0:85b3fd62ea1a 9 *
NYX 0:85b3fd62ea1a 10 * Unless required by applicable law or agreed to in writing, software
NYX 0:85b3fd62ea1a 11 * distributed under the License is distributed on an "AS IS" BASIS,
NYX 0:85b3fd62ea1a 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
NYX 0:85b3fd62ea1a 13 * See the License for the specific language governing permissions and
NYX 0:85b3fd62ea1a 14 * limitations under the License.
NYX 0:85b3fd62ea1a 15 */
NYX 0:85b3fd62ea1a 16 #include <stddef.h>
NYX 0:85b3fd62ea1a 17 #include "us_ticker_api.h"
NYX 0:85b3fd62ea1a 18 #include "PeripheralNames.h"
NYX 0:85b3fd62ea1a 19 #include "hal_tick.h"
NYX 0:85b3fd62ea1a 20
NYX 0:85b3fd62ea1a 21 // A 16-bit timer is used
NYX 0:85b3fd62ea1a 22 #if TIM_MST_16BIT
NYX 0:85b3fd62ea1a 23
NYX 0:85b3fd62ea1a 24 TIM_HandleTypeDef TimMasterHandle;
NYX 0:85b3fd62ea1a 25
NYX 0:85b3fd62ea1a 26 volatile uint32_t SlaveCounter = 0;
NYX 0:85b3fd62ea1a 27 volatile uint32_t oc_int_part = 0;
NYX 0:85b3fd62ea1a 28
NYX 0:85b3fd62ea1a 29 void us_ticker_init(void)
NYX 0:85b3fd62ea1a 30 {
NYX 0:85b3fd62ea1a 31 /* NOTE: assuming that HAL tick has already been initialized! */
NYX 0:85b3fd62ea1a 32 }
NYX 0:85b3fd62ea1a 33
NYX 0:85b3fd62ea1a 34 uint32_t us_ticker_read()
NYX 0:85b3fd62ea1a 35 {
NYX 0:85b3fd62ea1a 36 uint16_t cntH_old, cntH, cntL;
NYX 0:85b3fd62ea1a 37 do {
NYX 0:85b3fd62ea1a 38 cntH_old = SlaveCounter;
NYX 0:85b3fd62ea1a 39 /* SlaveCounter needs to be checked before AND after we read the
NYX 0:85b3fd62ea1a 40 * current counter TIM_MST->CNT, in case it wraps around.
NYX 0:85b3fd62ea1a 41 * there are 2 possible cases of wrap around
NYX 0:85b3fd62ea1a 42 * 1) in case this function is interrupted by timer_irq_handler and
NYX 0:85b3fd62ea1a 43 * the SlaveCounter is updated. In that case we will loop again.
NYX 0:85b3fd62ea1a 44 * 2) in case this function is called from interrupt context during
NYX 0:85b3fd62ea1a 45 * wrap-around condtion. That would prevent/delay the timer_irq_handler
NYX 0:85b3fd62ea1a 46 * from being called so we need to locally check the FLAG_UPDATE and
NYX 0:85b3fd62ea1a 47 * update the cntH accordingly. The SlaveCounter variable itself will
NYX 0:85b3fd62ea1a 48 * be updated in the interrupt handler just after ...
NYX 0:85b3fd62ea1a 49 */
NYX 0:85b3fd62ea1a 50 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_UPDATE) == SET) {
NYX 0:85b3fd62ea1a 51 cntH_old += 1;
NYX 0:85b3fd62ea1a 52 }
NYX 0:85b3fd62ea1a 53 cntL = TIM_MST->CNT;
NYX 0:85b3fd62ea1a 54 cntH = SlaveCounter;
NYX 0:85b3fd62ea1a 55 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_UPDATE) == SET) {
NYX 0:85b3fd62ea1a 56 cntH += 1;
NYX 0:85b3fd62ea1a 57 }
NYX 0:85b3fd62ea1a 58 } while(cntH_old != cntH);
NYX 0:85b3fd62ea1a 59 // Glue the upper and lower part together to get a 32 bit timer
NYX 0:85b3fd62ea1a 60 return (uint32_t)(cntH << 16 | cntL);
NYX 0:85b3fd62ea1a 61 }
NYX 0:85b3fd62ea1a 62
NYX 0:85b3fd62ea1a 63 void us_ticker_set_interrupt(timestamp_t timestamp)
NYX 0:85b3fd62ea1a 64 {
NYX 0:85b3fd62ea1a 65 // NOTE: This function must be called with interrupts disabled to keep our
NYX 0:85b3fd62ea1a 66 // timer interrupt setup atomic
NYX 0:85b3fd62ea1a 67
NYX 0:85b3fd62ea1a 68 // Set new output compare value
NYX 0:85b3fd62ea1a 69 __HAL_TIM_SET_COMPARE(&TimMasterHandle, TIM_CHANNEL_1, timestamp & 0xFFFF);
NYX 0:85b3fd62ea1a 70 // Ensure the compare event starts clear
NYX 0:85b3fd62ea1a 71 __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1);
NYX 0:85b3fd62ea1a 72 // Enable IT
NYX 0:85b3fd62ea1a 73 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1);
NYX 0:85b3fd62ea1a 74
NYX 0:85b3fd62ea1a 75 /* Set the number of timer wrap-around loops before the actual timestamp
NYX 0:85b3fd62ea1a 76 * is reached. If the calculated delta time is more than halfway to the
NYX 0:85b3fd62ea1a 77 * next compare event, check to see if a compare event has already been
NYX 0:85b3fd62ea1a 78 * set, and if so, add one to the wrap-around count. This is done to
NYX 0:85b3fd62ea1a 79 * ensure the correct wrap count is used in the corner cases where the
NYX 0:85b3fd62ea1a 80 * 16 bit counter passes the compare value during the process of
NYX 0:85b3fd62ea1a 81 * configuring this interrupt.
NYX 0:85b3fd62ea1a 82 *
NYX 0:85b3fd62ea1a 83 * Assumption: The time to execute this function is less than 32ms
NYX 0:85b3fd62ea1a 84 * (otherwise incorrect behaviour could result)
NYX 0:85b3fd62ea1a 85 *
NYX 0:85b3fd62ea1a 86 * Consider the following corner cases:
NYX 0:85b3fd62ea1a 87 * 1) timestamp is 1 us in the future:
NYX 0:85b3fd62ea1a 88 * oc_int_part = 0 initially
NYX 0:85b3fd62ea1a 89 * oc_int_part left at 0 because ((delta - 1) & 0xFFFF) < 0x8000
NYX 0:85b3fd62ea1a 90 * Compare event should happen in 1 us and us_ticker_irq_handler()
NYX 0:85b3fd62ea1a 91 * called
NYX 0:85b3fd62ea1a 92 * 2) timestamp is 0x8000 us in the future:
NYX 0:85b3fd62ea1a 93 * oc_int_part = 0 initially
NYX 0:85b3fd62ea1a 94 * oc_int_part left at 0 because ((delta - 1) & 0xFFFF) < 0x8000
NYX 0:85b3fd62ea1a 95 * There should be no possibility of the CC1 flag being set yet
NYX 0:85b3fd62ea1a 96 * (see assumption above). When the compare event does occur in
NYX 0:85b3fd62ea1a 97 * 32768 us, us_ticker_irq_handler() will be called
NYX 0:85b3fd62ea1a 98 * 3) timestamp is 0x8001 us in the future:
NYX 0:85b3fd62ea1a 99 * oc_int_part = 0 initially
NYX 0:85b3fd62ea1a 100 * ((delta - 1) & 0xFFFF) >= 0x8000 but there should be no
NYX 0:85b3fd62ea1a 101 * possibility of the CC1 flag being set yet (see assumption above),
NYX 0:85b3fd62ea1a 102 * so oc_int_part will be left at 0, and when the compare event
NYX 0:85b3fd62ea1a 103 * does occur in 32769 us, us_ticker_irq_handler() will be called
NYX 0:85b3fd62ea1a 104 * 4) timestamp is 0x10000 us in the future:
NYX 0:85b3fd62ea1a 105 * oc_int_part = 0 initially
NYX 0:85b3fd62ea1a 106 * ((delta - 1) & 0xFFFF) >= 0x8000
NYX 0:85b3fd62ea1a 107 * There are two subcases:
NYX 0:85b3fd62ea1a 108 * a) The timer counter has not incremented past the compare
NYX 0:85b3fd62ea1a 109 * value while setting up the interrupt. In this case, the
NYX 0:85b3fd62ea1a 110 * CC1 flag will not be set, so oc_int_part will be
NYX 0:85b3fd62ea1a 111 * left at 0, and when the compare event occurs in 65536 us,
NYX 0:85b3fd62ea1a 112 * us_ticker_irq_handler() will be called
NYX 0:85b3fd62ea1a 113 * b) The timer counter has JUST incremented past the compare
NYX 0:85b3fd62ea1a 114 * value. In this case, the CC1 flag will be set, so
NYX 0:85b3fd62ea1a 115 * oc_int_part will be incremented to 1, and the interrupt will
NYX 0:85b3fd62ea1a 116 * occur immediately after this function returns, where
NYX 0:85b3fd62ea1a 117 * oc_int_part will decrement to 0 without calling
NYX 0:85b3fd62ea1a 118 * us_ticker_irq_handler(). Then about 65536 us later, the
NYX 0:85b3fd62ea1a 119 * compare event will occur again, and us_ticker_irq_handler()
NYX 0:85b3fd62ea1a 120 * will be called
NYX 0:85b3fd62ea1a 121 * 5) timestamp is 0x10001 us in the future:
NYX 0:85b3fd62ea1a 122 * oc_int_part = 1 initially
NYX 0:85b3fd62ea1a 123 * oc_int_part left at 1 because ((delta - 1) & 0xFFFF) < 0x8000
NYX 0:85b3fd62ea1a 124 * CC1 flag will not be set (see assumption above). In 1 us the
NYX 0:85b3fd62ea1a 125 * compare event will cause an interrupt, where oc_int_part will be
NYX 0:85b3fd62ea1a 126 * decremented to 0 without calling us_ticker_irq_handler(). Then
NYX 0:85b3fd62ea1a 127 * about 65536 us later, the compare event will occur again, and
NYX 0:85b3fd62ea1a 128 * us_ticker_irq_handler() will be called
NYX 0:85b3fd62ea1a 129 * 6) timestamp is 0x18000 us in the future:
NYX 0:85b3fd62ea1a 130 * oc_int_part = 1 initially
NYX 0:85b3fd62ea1a 131 * oc_int_part left at 1 because ((delta - 1) & 0xFFFF) < 0x8000
NYX 0:85b3fd62ea1a 132 * There should be no possibility of the CC1 flag being set yet
NYX 0:85b3fd62ea1a 133 * (see assumption above). When the compare event does occur in
NYX 0:85b3fd62ea1a 134 * 32768 us, oc_int_part will be decremented to 0 without calling
NYX 0:85b3fd62ea1a 135 * us_ticker_irq_handler(). Then about 65536 us later, the
NYX 0:85b3fd62ea1a 136 * compare event will occur again, and us_ticker_irq_handler() will
NYX 0:85b3fd62ea1a 137 * be called
NYX 0:85b3fd62ea1a 138 * 7) timestamp is 0x18001 us in the future:
NYX 0:85b3fd62ea1a 139 * oc_int_part = 1 initially
NYX 0:85b3fd62ea1a 140 * ((delta - 1) & 0xFFFF) >= 0x8000 but there should be no
NYX 0:85b3fd62ea1a 141 * possibility of the CC1 flag being set yet (see assumption above),
NYX 0:85b3fd62ea1a 142 * so oc_int_part will be left at 1, and when the compare event
NYX 0:85b3fd62ea1a 143 * does occur in 32769 us, oc_int_part will be decremented to 0
NYX 0:85b3fd62ea1a 144 * without calling us_ticker_irq_handler(). Then about 65536 us
NYX 0:85b3fd62ea1a 145 * later, the compare event will occur again, and
NYX 0:85b3fd62ea1a 146 * us_ticker_irq_handler() will be called
NYX 0:85b3fd62ea1a 147 *
NYX 0:85b3fd62ea1a 148 * delta - 1 is used because the timer compare event happens on the
NYX 0:85b3fd62ea1a 149 * counter incrementing to match the compare value, and it won't occur
NYX 0:85b3fd62ea1a 150 * immediately when the compare value is set to the current counter
NYX 0:85b3fd62ea1a 151 * value.
NYX 0:85b3fd62ea1a 152 */
NYX 0:85b3fd62ea1a 153 uint32_t current_time = us_ticker_read();
NYX 0:85b3fd62ea1a 154 uint32_t delta = timestamp - current_time;
NYX 0:85b3fd62ea1a 155 /* Note: The case of delta <= 0 is handled in MBED upper layer */
NYX 0:85b3fd62ea1a 156 oc_int_part = (delta - 1) >> 16;
NYX 0:85b3fd62ea1a 157 if ( ((delta - 1) & 0xFFFF) >= 0x8000 &&
NYX 0:85b3fd62ea1a 158 __HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET ) {
NYX 0:85b3fd62ea1a 159 ++oc_int_part;
NYX 0:85b3fd62ea1a 160 /* NOTE: Instead of incrementing oc_int_part here, we could clear
NYX 0:85b3fd62ea1a 161 * the CC1 flag, but then you'd have to wait to ensure the
NYX 0:85b3fd62ea1a 162 * interrupt is knocked down before returning and reenabling
NYX 0:85b3fd62ea1a 163 * interrupts. Since this is a rare case, it's not worth it
NYX 0:85b3fd62ea1a 164 * to try and optimize it, and it keeps the code simpler and
NYX 0:85b3fd62ea1a 165 * safer to just do this increment instead.
NYX 0:85b3fd62ea1a 166 */
NYX 0:85b3fd62ea1a 167 }
NYX 0:85b3fd62ea1a 168
NYX 0:85b3fd62ea1a 169 }
NYX 0:85b3fd62ea1a 170
NYX 0:85b3fd62ea1a 171 void us_ticker_fire_interrupt(void)
NYX 0:85b3fd62ea1a 172 {
NYX 0:85b3fd62ea1a 173 /* When firing the event, the number of 16 bits counter wrap-ups (oc_int)
NYX 0:85b3fd62ea1a 174 * must be re-initialized */
NYX 0:85b3fd62ea1a 175 oc_int_part = 0;
NYX 0:85b3fd62ea1a 176 HAL_TIM_GenerateEvent(&TimMasterHandle, TIM_EVENTSOURCE_CC1);
NYX 0:85b3fd62ea1a 177 }
NYX 0:85b3fd62ea1a 178
NYX 0:85b3fd62ea1a 179 /* NOTE: must be called with interrupts disabled! */
NYX 0:85b3fd62ea1a 180 void us_ticker_disable_interrupt(void)
NYX 0:85b3fd62ea1a 181 {
NYX 0:85b3fd62ea1a 182 __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
NYX 0:85b3fd62ea1a 183 }
NYX 0:85b3fd62ea1a 184
NYX 0:85b3fd62ea1a 185 /* NOTE: must be called with interrupts disabled! */
NYX 0:85b3fd62ea1a 186 void us_ticker_clear_interrupt(void)
NYX 0:85b3fd62ea1a 187 {
NYX 0:85b3fd62ea1a 188 __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1);
NYX 0:85b3fd62ea1a 189 }
NYX 0:85b3fd62ea1a 190
NYX 0:85b3fd62ea1a 191 #endif // TIM_MST_16BIT