The prosthetic control(MIT)
targets/TARGET_STM/hal_tick_16b.c@0:20e0c61e0684, 2022-06-23 (annotated)
- Committer:
- ganlikun
- Date:
- Thu Jun 23 05:23:34 2022 +0000
- Revision:
- 0:20e0c61e0684
01
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ganlikun | 0:20e0c61e0684 | 1 | /* mbed Microcontroller Library |
ganlikun | 0:20e0c61e0684 | 2 | * Copyright (c) 2006-2016 ARM Limited |
ganlikun | 0:20e0c61e0684 | 3 | * |
ganlikun | 0:20e0c61e0684 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ganlikun | 0:20e0c61e0684 | 5 | * you may not use this file except in compliance with the License. |
ganlikun | 0:20e0c61e0684 | 6 | * You may obtain a copy of the License at |
ganlikun | 0:20e0c61e0684 | 7 | * |
ganlikun | 0:20e0c61e0684 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
ganlikun | 0:20e0c61e0684 | 9 | * |
ganlikun | 0:20e0c61e0684 | 10 | * Unless required by applicable law or agreed to in writing, software |
ganlikun | 0:20e0c61e0684 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
ganlikun | 0:20e0c61e0684 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ganlikun | 0:20e0c61e0684 | 13 | * See the License for the specific language governing permissions and |
ganlikun | 0:20e0c61e0684 | 14 | * limitations under the License. |
ganlikun | 0:20e0c61e0684 | 15 | */ |
ganlikun | 0:20e0c61e0684 | 16 | #include "hal_tick.h" |
ganlikun | 0:20e0c61e0684 | 17 | |
ganlikun | 0:20e0c61e0684 | 18 | // A 16-bit timer is used |
ganlikun | 0:20e0c61e0684 | 19 | #if TIM_MST_16BIT |
ganlikun | 0:20e0c61e0684 | 20 | |
ganlikun | 0:20e0c61e0684 | 21 | #define DEBUG_TICK 0 // Set to 1 to toggle a pin (see below which pin) at each tick |
ganlikun | 0:20e0c61e0684 | 22 | |
ganlikun | 0:20e0c61e0684 | 23 | extern TIM_HandleTypeDef TimMasterHandle; |
ganlikun | 0:20e0c61e0684 | 24 | |
ganlikun | 0:20e0c61e0684 | 25 | extern volatile uint32_t SlaveCounter; |
ganlikun | 0:20e0c61e0684 | 26 | extern volatile uint32_t oc_int_part; |
ganlikun | 0:20e0c61e0684 | 27 | |
ganlikun | 0:20e0c61e0684 | 28 | volatile uint32_t PreviousVal = 0; |
ganlikun | 0:20e0c61e0684 | 29 | |
ganlikun | 0:20e0c61e0684 | 30 | void us_ticker_irq_handler(void); |
ganlikun | 0:20e0c61e0684 | 31 | |
ganlikun | 0:20e0c61e0684 | 32 | #if defined(TARGET_STM32F0) |
ganlikun | 0:20e0c61e0684 | 33 | void timer_update_irq_handler(void) { |
ganlikun | 0:20e0c61e0684 | 34 | #else |
ganlikun | 0:20e0c61e0684 | 35 | void timer_irq_handler(void) |
ganlikun | 0:20e0c61e0684 | 36 | { |
ganlikun | 0:20e0c61e0684 | 37 | #endif |
ganlikun | 0:20e0c61e0684 | 38 | TimMasterHandle.Instance = TIM_MST; |
ganlikun | 0:20e0c61e0684 | 39 | |
ganlikun | 0:20e0c61e0684 | 40 | // Clear Update interrupt flag |
ganlikun | 0:20e0c61e0684 | 41 | if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_UPDATE) == SET) { |
ganlikun | 0:20e0c61e0684 | 42 | if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_UPDATE) == SET) { |
ganlikun | 0:20e0c61e0684 | 43 | __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_UPDATE); |
ganlikun | 0:20e0c61e0684 | 44 | SlaveCounter++; |
ganlikun | 0:20e0c61e0684 | 45 | } |
ganlikun | 0:20e0c61e0684 | 46 | } |
ganlikun | 0:20e0c61e0684 | 47 | |
ganlikun | 0:20e0c61e0684 | 48 | #if defined(TARGET_STM32F0) |
ganlikun | 0:20e0c61e0684 | 49 | } // end timer_update_irq_handler function |
ganlikun | 0:20e0c61e0684 | 50 | // Used for mbed timeout (channel 1) and HAL tick (channel 2) |
ganlikun | 0:20e0c61e0684 | 51 | void timer_oc_irq_handler(void) |
ganlikun | 0:20e0c61e0684 | 52 | { |
ganlikun | 0:20e0c61e0684 | 53 | TimMasterHandle.Instance = TIM_MST; |
ganlikun | 0:20e0c61e0684 | 54 | #endif |
ganlikun | 0:20e0c61e0684 | 55 | |
ganlikun | 0:20e0c61e0684 | 56 | // Channel 1 for mbed timeout |
ganlikun | 0:20e0c61e0684 | 57 | if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) { |
ganlikun | 0:20e0c61e0684 | 58 | if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC1) == SET) { |
ganlikun | 0:20e0c61e0684 | 59 | __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1); |
ganlikun | 0:20e0c61e0684 | 60 | |
ganlikun | 0:20e0c61e0684 | 61 | if (oc_int_part > 0) { |
ganlikun | 0:20e0c61e0684 | 62 | oc_int_part--; |
ganlikun | 0:20e0c61e0684 | 63 | } else { |
ganlikun | 0:20e0c61e0684 | 64 | us_ticker_irq_handler(); |
ganlikun | 0:20e0c61e0684 | 65 | } |
ganlikun | 0:20e0c61e0684 | 66 | } |
ganlikun | 0:20e0c61e0684 | 67 | } |
ganlikun | 0:20e0c61e0684 | 68 | |
ganlikun | 0:20e0c61e0684 | 69 | // Channel 2 for HAL tick |
ganlikun | 0:20e0c61e0684 | 70 | if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC2) == SET) { |
ganlikun | 0:20e0c61e0684 | 71 | |
ganlikun | 0:20e0c61e0684 | 72 | if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC2) == SET) { |
ganlikun | 0:20e0c61e0684 | 73 | __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC2); |
ganlikun | 0:20e0c61e0684 | 74 | uint32_t val = __HAL_TIM_GET_COUNTER(&TimMasterHandle); |
ganlikun | 0:20e0c61e0684 | 75 | if ((val - PreviousVal) >= HAL_TICK_DELAY) { |
ganlikun | 0:20e0c61e0684 | 76 | // Increment HAL variable |
ganlikun | 0:20e0c61e0684 | 77 | HAL_IncTick(); |
ganlikun | 0:20e0c61e0684 | 78 | // Prepare next interrupt |
ganlikun | 0:20e0c61e0684 | 79 | __HAL_TIM_SET_COMPARE(&TimMasterHandle, TIM_CHANNEL_2, val + HAL_TICK_DELAY); |
ganlikun | 0:20e0c61e0684 | 80 | PreviousVal = val; |
ganlikun | 0:20e0c61e0684 | 81 | #if DEBUG_TICK > 0 |
ganlikun | 0:20e0c61e0684 | 82 | HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_6); |
ganlikun | 0:20e0c61e0684 | 83 | #endif |
ganlikun | 0:20e0c61e0684 | 84 | } |
ganlikun | 0:20e0c61e0684 | 85 | } |
ganlikun | 0:20e0c61e0684 | 86 | } |
ganlikun | 0:20e0c61e0684 | 87 | } |
ganlikun | 0:20e0c61e0684 | 88 | |
ganlikun | 0:20e0c61e0684 | 89 | // Reconfigure the HAL tick using a standard timer instead of systick. |
ganlikun | 0:20e0c61e0684 | 90 | HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) |
ganlikun | 0:20e0c61e0684 | 91 | { |
ganlikun | 0:20e0c61e0684 | 92 | // Enable timer clock |
ganlikun | 0:20e0c61e0684 | 93 | TIM_MST_RCC; |
ganlikun | 0:20e0c61e0684 | 94 | |
ganlikun | 0:20e0c61e0684 | 95 | // Reset timer |
ganlikun | 0:20e0c61e0684 | 96 | TIM_MST_RESET_ON; |
ganlikun | 0:20e0c61e0684 | 97 | TIM_MST_RESET_OFF; |
ganlikun | 0:20e0c61e0684 | 98 | |
ganlikun | 0:20e0c61e0684 | 99 | // Update the SystemCoreClock variable |
ganlikun | 0:20e0c61e0684 | 100 | SystemCoreClockUpdate(); |
ganlikun | 0:20e0c61e0684 | 101 | |
ganlikun | 0:20e0c61e0684 | 102 | // Configure time base |
ganlikun | 0:20e0c61e0684 | 103 | TimMasterHandle.Instance = TIM_MST; |
ganlikun | 0:20e0c61e0684 | 104 | TimMasterHandle.Init.Period = 0xFFFF; |
ganlikun | 0:20e0c61e0684 | 105 | TimMasterHandle.Init.Prescaler = (uint32_t)(SystemCoreClock / 1000000) - 1; // 1 us tick |
ganlikun | 0:20e0c61e0684 | 106 | TimMasterHandle.Init.ClockDivision = 0; |
ganlikun | 0:20e0c61e0684 | 107 | TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP; |
ganlikun | 0:20e0c61e0684 | 108 | #if !defined(TARGET_STM32L0) |
ganlikun | 0:20e0c61e0684 | 109 | TimMasterHandle.Init.RepetitionCounter = 0; |
ganlikun | 0:20e0c61e0684 | 110 | #endif |
ganlikun | 0:20e0c61e0684 | 111 | #ifdef TIM_AUTORELOAD_PRELOAD_DISABLE |
ganlikun | 0:20e0c61e0684 | 112 | TimMasterHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
ganlikun | 0:20e0c61e0684 | 113 | #endif |
ganlikun | 0:20e0c61e0684 | 114 | HAL_TIM_Base_Init(&TimMasterHandle); |
ganlikun | 0:20e0c61e0684 | 115 | |
ganlikun | 0:20e0c61e0684 | 116 | //LL_TIM_EnableUpdateEvent(TimMasterHandle.Instance); |
ganlikun | 0:20e0c61e0684 | 117 | |
ganlikun | 0:20e0c61e0684 | 118 | // Configure output compare channel 1 for mbed timeout (enabled later when used) |
ganlikun | 0:20e0c61e0684 | 119 | HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1); |
ganlikun | 0:20e0c61e0684 | 120 | |
ganlikun | 0:20e0c61e0684 | 121 | // Configure output compare channel 2 for HAL tick |
ganlikun | 0:20e0c61e0684 | 122 | HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_2); |
ganlikun | 0:20e0c61e0684 | 123 | PreviousVal = __HAL_TIM_GET_COUNTER(&TimMasterHandle); |
ganlikun | 0:20e0c61e0684 | 124 | __HAL_TIM_SET_COMPARE(&TimMasterHandle, TIM_CHANNEL_2, PreviousVal + HAL_TICK_DELAY); |
ganlikun | 0:20e0c61e0684 | 125 | |
ganlikun | 0:20e0c61e0684 | 126 | |
ganlikun | 0:20e0c61e0684 | 127 | |
ganlikun | 0:20e0c61e0684 | 128 | // Configure interrupts |
ganlikun | 0:20e0c61e0684 | 129 | // Update interrupt used for 32-bit counter |
ganlikun | 0:20e0c61e0684 | 130 | // Output compare channel 1 interrupt for mbed timeout |
ganlikun | 0:20e0c61e0684 | 131 | // Output compare channel 2 interrupt for HAL tick |
ganlikun | 0:20e0c61e0684 | 132 | #if defined(TARGET_STM32F0) |
ganlikun | 0:20e0c61e0684 | 133 | NVIC_SetVector(TIM_MST_UP_IRQ, (uint32_t)timer_update_irq_handler); |
ganlikun | 0:20e0c61e0684 | 134 | NVIC_EnableIRQ(TIM_MST_UP_IRQ); |
ganlikun | 0:20e0c61e0684 | 135 | NVIC_SetPriority(TIM_MST_UP_IRQ, 0); |
ganlikun | 0:20e0c61e0684 | 136 | NVIC_SetVector(TIM_MST_OC_IRQ, (uint32_t)timer_oc_irq_handler); |
ganlikun | 0:20e0c61e0684 | 137 | NVIC_EnableIRQ(TIM_MST_OC_IRQ); |
ganlikun | 0:20e0c61e0684 | 138 | NVIC_SetPriority(TIM_MST_OC_IRQ, 1); |
ganlikun | 0:20e0c61e0684 | 139 | #else |
ganlikun | 0:20e0c61e0684 | 140 | NVIC_SetVector(TIM_MST_IRQ, (uint32_t)timer_irq_handler); |
ganlikun | 0:20e0c61e0684 | 141 | NVIC_EnableIRQ(TIM_MST_IRQ); |
ganlikun | 0:20e0c61e0684 | 142 | #endif |
ganlikun | 0:20e0c61e0684 | 143 | |
ganlikun | 0:20e0c61e0684 | 144 | // Enable interrupts |
ganlikun | 0:20e0c61e0684 | 145 | __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_UPDATE); // For 32-bit counter |
ganlikun | 0:20e0c61e0684 | 146 | __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2); // For HAL tick |
ganlikun | 0:20e0c61e0684 | 147 | |
ganlikun | 0:20e0c61e0684 | 148 | // Enable timer |
ganlikun | 0:20e0c61e0684 | 149 | HAL_TIM_Base_Start(&TimMasterHandle); |
ganlikun | 0:20e0c61e0684 | 150 | |
ganlikun | 0:20e0c61e0684 | 151 | // Freeze timer on stop/breakpoint |
ganlikun | 0:20e0c61e0684 | 152 | // Define the FREEZE_TIMER_ON_DEBUG macro in mbed_app.json for example |
ganlikun | 0:20e0c61e0684 | 153 | #if !defined(NDEBUG) && defined(FREEZE_TIMER_ON_DEBUG) && defined(TIM_MST_DBGMCU_FREEZE) |
ganlikun | 0:20e0c61e0684 | 154 | TIM_MST_DBGMCU_FREEZE; |
ganlikun | 0:20e0c61e0684 | 155 | #endif |
ganlikun | 0:20e0c61e0684 | 156 | |
ganlikun | 0:20e0c61e0684 | 157 | #if DEBUG_TICK > 0 |
ganlikun | 0:20e0c61e0684 | 158 | __HAL_RCC_GPIOB_CLK_ENABLE(); |
ganlikun | 0:20e0c61e0684 | 159 | GPIO_InitTypeDef GPIO_InitStruct; |
ganlikun | 0:20e0c61e0684 | 160 | GPIO_InitStruct.Pin = GPIO_PIN_6; |
ganlikun | 0:20e0c61e0684 | 161 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
ganlikun | 0:20e0c61e0684 | 162 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
ganlikun | 0:20e0c61e0684 | 163 | GPIO_InitStruct.Speed = GPIO_SPEED_FAST; |
ganlikun | 0:20e0c61e0684 | 164 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
ganlikun | 0:20e0c61e0684 | 165 | #endif |
ganlikun | 0:20e0c61e0684 | 166 | |
ganlikun | 0:20e0c61e0684 | 167 | return HAL_OK; |
ganlikun | 0:20e0c61e0684 | 168 | } |
ganlikun | 0:20e0c61e0684 | 169 | |
ganlikun | 0:20e0c61e0684 | 170 | /* NOTE: must be called with interrupts disabled! */ |
ganlikun | 0:20e0c61e0684 | 171 | void HAL_SuspendTick(void) |
ganlikun | 0:20e0c61e0684 | 172 | { |
ganlikun | 0:20e0c61e0684 | 173 | __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC2); |
ganlikun | 0:20e0c61e0684 | 174 | } |
ganlikun | 0:20e0c61e0684 | 175 | |
ganlikun | 0:20e0c61e0684 | 176 | /* NOTE: must be called with interrupts disabled! */ |
ganlikun | 0:20e0c61e0684 | 177 | void HAL_ResumeTick(void) |
ganlikun | 0:20e0c61e0684 | 178 | { |
ganlikun | 0:20e0c61e0684 | 179 | __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2); |
ganlikun | 0:20e0c61e0684 | 180 | } |
ganlikun | 0:20e0c61e0684 | 181 | |
ganlikun | 0:20e0c61e0684 | 182 | #endif // TIM_MST_16BIT |
ganlikun | 0:20e0c61e0684 | 183 |