From Ben Katz mbed-dev library. Removed unnecessary target files to reduce the overall size by a factor of 10 to make it easier to import into the online IDE.

Dependents:   motor_driver motor_driver_screaming_fix

Committer:
saloutos
Date:
Thu Nov 26 04:08:56 2020 +0000
Revision:
0:083111ae2a11
first commit of leaned mbed dev lib

Who changed what in which revision?

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