EL4121 Embedded System / mbed-os

Dependents:   cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more

Committer:
be_bryan
Date:
Mon Dec 11 17:54:04 2017 +0000
Revision:
0:b74591d5ab33
motor ++

Who changed what in which revision?

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