001

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

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