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
targets/TARGET_STM/hal_tick_16b.c@0:083111ae2a11, 2020-11-26 (annotated)
- 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?
User | Revision | Line number | New 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 "hal_tick.h" |
saloutos | 0:083111ae2a11 | 17 | |
saloutos | 0:083111ae2a11 | 18 | // A 16-bit timer is used |
saloutos | 0:083111ae2a11 | 19 | #if TIM_MST_16BIT |
saloutos | 0:083111ae2a11 | 20 | |
saloutos | 0:083111ae2a11 | 21 | #define DEBUG_TICK 0 // Set to 1 to toggle a pin (see below which pin) at each tick |
saloutos | 0:083111ae2a11 | 22 | |
saloutos | 0:083111ae2a11 | 23 | extern TIM_HandleTypeDef TimMasterHandle; |
saloutos | 0:083111ae2a11 | 24 | |
saloutos | 0:083111ae2a11 | 25 | extern volatile uint32_t SlaveCounter; |
saloutos | 0:083111ae2a11 | 26 | extern volatile uint32_t oc_int_part; |
saloutos | 0:083111ae2a11 | 27 | |
saloutos | 0:083111ae2a11 | 28 | volatile uint32_t PreviousVal = 0; |
saloutos | 0:083111ae2a11 | 29 | |
saloutos | 0:083111ae2a11 | 30 | void us_ticker_irq_handler(void); |
saloutos | 0:083111ae2a11 | 31 | |
saloutos | 0:083111ae2a11 | 32 | #if defined(TARGET_STM32F0) |
saloutos | 0:083111ae2a11 | 33 | void timer_update_irq_handler(void) { |
saloutos | 0:083111ae2a11 | 34 | #else |
saloutos | 0:083111ae2a11 | 35 | void timer_irq_handler(void) |
saloutos | 0:083111ae2a11 | 36 | { |
saloutos | 0:083111ae2a11 | 37 | #endif |
saloutos | 0:083111ae2a11 | 38 | TimMasterHandle.Instance = TIM_MST; |
saloutos | 0:083111ae2a11 | 39 | |
saloutos | 0:083111ae2a11 | 40 | // Clear Update interrupt flag |
saloutos | 0:083111ae2a11 | 41 | if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_UPDATE) == SET) { |
saloutos | 0:083111ae2a11 | 42 | if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_UPDATE) == SET) { |
saloutos | 0:083111ae2a11 | 43 | __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_UPDATE); |
saloutos | 0:083111ae2a11 | 44 | SlaveCounter++; |
saloutos | 0:083111ae2a11 | 45 | } |
saloutos | 0:083111ae2a11 | 46 | } |
saloutos | 0:083111ae2a11 | 47 | |
saloutos | 0:083111ae2a11 | 48 | #if defined(TARGET_STM32F0) |
saloutos | 0:083111ae2a11 | 49 | } // end timer_update_irq_handler function |
saloutos | 0:083111ae2a11 | 50 | // Used for mbed timeout (channel 1) and HAL tick (channel 2) |
saloutos | 0:083111ae2a11 | 51 | void timer_oc_irq_handler(void) |
saloutos | 0:083111ae2a11 | 52 | { |
saloutos | 0:083111ae2a11 | 53 | TimMasterHandle.Instance = TIM_MST; |
saloutos | 0:083111ae2a11 | 54 | #endif |
saloutos | 0:083111ae2a11 | 55 | |
saloutos | 0:083111ae2a11 | 56 | // Channel 1 for mbed timeout |
saloutos | 0:083111ae2a11 | 57 | if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) { |
saloutos | 0:083111ae2a11 | 58 | if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC1) == SET) { |
saloutos | 0:083111ae2a11 | 59 | __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1); |
saloutos | 0:083111ae2a11 | 60 | |
saloutos | 0:083111ae2a11 | 61 | if (oc_int_part > 0) { |
saloutos | 0:083111ae2a11 | 62 | oc_int_part--; |
saloutos | 0:083111ae2a11 | 63 | } else { |
saloutos | 0:083111ae2a11 | 64 | us_ticker_irq_handler(); |
saloutos | 0:083111ae2a11 | 65 | } |
saloutos | 0:083111ae2a11 | 66 | } |
saloutos | 0:083111ae2a11 | 67 | } |
saloutos | 0:083111ae2a11 | 68 | |
saloutos | 0:083111ae2a11 | 69 | // Channel 2 for HAL tick |
saloutos | 0:083111ae2a11 | 70 | if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC2) == SET) { |
saloutos | 0:083111ae2a11 | 71 | |
saloutos | 0:083111ae2a11 | 72 | if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC2) == SET) { |
saloutos | 0:083111ae2a11 | 73 | __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC2); |
saloutos | 0:083111ae2a11 | 74 | uint32_t val = __HAL_TIM_GET_COUNTER(&TimMasterHandle); |
saloutos | 0:083111ae2a11 | 75 | if ((val - PreviousVal) >= HAL_TICK_DELAY) { |
saloutos | 0:083111ae2a11 | 76 | // Increment HAL variable |
saloutos | 0:083111ae2a11 | 77 | HAL_IncTick(); |
saloutos | 0:083111ae2a11 | 78 | // Prepare next interrupt |
saloutos | 0:083111ae2a11 | 79 | __HAL_TIM_SET_COMPARE(&TimMasterHandle, TIM_CHANNEL_2, val + HAL_TICK_DELAY); |
saloutos | 0:083111ae2a11 | 80 | PreviousVal = val; |
saloutos | 0:083111ae2a11 | 81 | #if DEBUG_TICK > 0 |
saloutos | 0:083111ae2a11 | 82 | HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_6); |
saloutos | 0:083111ae2a11 | 83 | #endif |
saloutos | 0:083111ae2a11 | 84 | } |
saloutos | 0:083111ae2a11 | 85 | } |
saloutos | 0:083111ae2a11 | 86 | } |
saloutos | 0:083111ae2a11 | 87 | } |
saloutos | 0:083111ae2a11 | 88 | |
saloutos | 0:083111ae2a11 | 89 | // Reconfigure the HAL tick using a standard timer instead of systick. |
saloutos | 0:083111ae2a11 | 90 | HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) |
saloutos | 0:083111ae2a11 | 91 | { |
saloutos | 0:083111ae2a11 | 92 | // Enable timer clock |
saloutos | 0:083111ae2a11 | 93 | TIM_MST_RCC; |
saloutos | 0:083111ae2a11 | 94 | |
saloutos | 0:083111ae2a11 | 95 | // Reset timer |
saloutos | 0:083111ae2a11 | 96 | TIM_MST_RESET_ON; |
saloutos | 0:083111ae2a11 | 97 | TIM_MST_RESET_OFF; |
saloutos | 0:083111ae2a11 | 98 | |
saloutos | 0:083111ae2a11 | 99 | // Update the SystemCoreClock variable |
saloutos | 0:083111ae2a11 | 100 | SystemCoreClockUpdate(); |
saloutos | 0:083111ae2a11 | 101 | |
saloutos | 0:083111ae2a11 | 102 | // Configure time base |
saloutos | 0:083111ae2a11 | 103 | TimMasterHandle.Instance = TIM_MST; |
saloutos | 0:083111ae2a11 | 104 | TimMasterHandle.Init.Period = 0xFFFF; |
saloutos | 0:083111ae2a11 | 105 | TimMasterHandle.Init.Prescaler = (uint32_t)(SystemCoreClock / 1000000) - 1; // 1 us tick |
saloutos | 0:083111ae2a11 | 106 | TimMasterHandle.Init.ClockDivision = 0; |
saloutos | 0:083111ae2a11 | 107 | TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP; |
saloutos | 0:083111ae2a11 | 108 | #if !defined(TARGET_STM32L0) |
saloutos | 0:083111ae2a11 | 109 | TimMasterHandle.Init.RepetitionCounter = 0; |
saloutos | 0:083111ae2a11 | 110 | #endif |
saloutos | 0:083111ae2a11 | 111 | #ifdef TIM_AUTORELOAD_PRELOAD_DISABLE |
saloutos | 0:083111ae2a11 | 112 | TimMasterHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
saloutos | 0:083111ae2a11 | 113 | #endif |
saloutos | 0:083111ae2a11 | 114 | HAL_TIM_Base_Init(&TimMasterHandle); |
saloutos | 0:083111ae2a11 | 115 | |
saloutos | 0:083111ae2a11 | 116 | //LL_TIM_EnableUpdateEvent(TimMasterHandle.Instance); |
saloutos | 0:083111ae2a11 | 117 | |
saloutos | 0:083111ae2a11 | 118 | // Configure output compare channel 1 for mbed timeout (enabled later when used) |
saloutos | 0:083111ae2a11 | 119 | HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1); |
saloutos | 0:083111ae2a11 | 120 | |
saloutos | 0:083111ae2a11 | 121 | // Configure output compare channel 2 for HAL tick |
saloutos | 0:083111ae2a11 | 122 | HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_2); |
saloutos | 0:083111ae2a11 | 123 | PreviousVal = __HAL_TIM_GET_COUNTER(&TimMasterHandle); |
saloutos | 0:083111ae2a11 | 124 | __HAL_TIM_SET_COMPARE(&TimMasterHandle, TIM_CHANNEL_2, PreviousVal + HAL_TICK_DELAY); |
saloutos | 0:083111ae2a11 | 125 | |
saloutos | 0:083111ae2a11 | 126 | |
saloutos | 0:083111ae2a11 | 127 | |
saloutos | 0:083111ae2a11 | 128 | // Configure interrupts |
saloutos | 0:083111ae2a11 | 129 | // Update interrupt used for 32-bit counter |
saloutos | 0:083111ae2a11 | 130 | // Output compare channel 1 interrupt for mbed timeout |
saloutos | 0:083111ae2a11 | 131 | // Output compare channel 2 interrupt for HAL tick |
saloutos | 0:083111ae2a11 | 132 | #if defined(TARGET_STM32F0) |
saloutos | 0:083111ae2a11 | 133 | NVIC_SetVector(TIM_MST_UP_IRQ, (uint32_t)timer_update_irq_handler); |
saloutos | 0:083111ae2a11 | 134 | NVIC_EnableIRQ(TIM_MST_UP_IRQ); |
saloutos | 0:083111ae2a11 | 135 | NVIC_SetPriority(TIM_MST_UP_IRQ, 0); |
saloutos | 0:083111ae2a11 | 136 | NVIC_SetVector(TIM_MST_OC_IRQ, (uint32_t)timer_oc_irq_handler); |
saloutos | 0:083111ae2a11 | 137 | NVIC_EnableIRQ(TIM_MST_OC_IRQ); |
saloutos | 0:083111ae2a11 | 138 | NVIC_SetPriority(TIM_MST_OC_IRQ, 1); |
saloutos | 0:083111ae2a11 | 139 | #else |
saloutos | 0:083111ae2a11 | 140 | NVIC_SetVector(TIM_MST_IRQ, (uint32_t)timer_irq_handler); |
saloutos | 0:083111ae2a11 | 141 | NVIC_EnableIRQ(TIM_MST_IRQ); |
saloutos | 0:083111ae2a11 | 142 | #endif |
saloutos | 0:083111ae2a11 | 143 | |
saloutos | 0:083111ae2a11 | 144 | // Enable interrupts |
saloutos | 0:083111ae2a11 | 145 | __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_UPDATE); // For 32-bit counter |
saloutos | 0:083111ae2a11 | 146 | __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2); // For HAL tick |
saloutos | 0:083111ae2a11 | 147 | |
saloutos | 0:083111ae2a11 | 148 | // Enable timer |
saloutos | 0:083111ae2a11 | 149 | HAL_TIM_Base_Start(&TimMasterHandle); |
saloutos | 0:083111ae2a11 | 150 | |
saloutos | 0:083111ae2a11 | 151 | // Freeze timer on stop/breakpoint |
saloutos | 0:083111ae2a11 | 152 | // Define the FREEZE_TIMER_ON_DEBUG macro in mbed_app.json for example |
saloutos | 0:083111ae2a11 | 153 | #if !defined(NDEBUG) && defined(FREEZE_TIMER_ON_DEBUG) && defined(TIM_MST_DBGMCU_FREEZE) |
saloutos | 0:083111ae2a11 | 154 | TIM_MST_DBGMCU_FREEZE; |
saloutos | 0:083111ae2a11 | 155 | #endif |
saloutos | 0:083111ae2a11 | 156 | |
saloutos | 0:083111ae2a11 | 157 | #if DEBUG_TICK > 0 |
saloutos | 0:083111ae2a11 | 158 | __HAL_RCC_GPIOB_CLK_ENABLE(); |
saloutos | 0:083111ae2a11 | 159 | GPIO_InitTypeDef GPIO_InitStruct; |
saloutos | 0:083111ae2a11 | 160 | GPIO_InitStruct.Pin = GPIO_PIN_6; |
saloutos | 0:083111ae2a11 | 161 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
saloutos | 0:083111ae2a11 | 162 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
saloutos | 0:083111ae2a11 | 163 | GPIO_InitStruct.Speed = GPIO_SPEED_FAST; |
saloutos | 0:083111ae2a11 | 164 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
saloutos | 0:083111ae2a11 | 165 | #endif |
saloutos | 0:083111ae2a11 | 166 | |
saloutos | 0:083111ae2a11 | 167 | return HAL_OK; |
saloutos | 0:083111ae2a11 | 168 | } |
saloutos | 0:083111ae2a11 | 169 | |
saloutos | 0:083111ae2a11 | 170 | /* NOTE: must be called with interrupts disabled! */ |
saloutos | 0:083111ae2a11 | 171 | void HAL_SuspendTick(void) |
saloutos | 0:083111ae2a11 | 172 | { |
saloutos | 0:083111ae2a11 | 173 | __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC2); |
saloutos | 0:083111ae2a11 | 174 | } |
saloutos | 0:083111ae2a11 | 175 | |
saloutos | 0:083111ae2a11 | 176 | /* NOTE: must be called with interrupts disabled! */ |
saloutos | 0:083111ae2a11 | 177 | void HAL_ResumeTick(void) |
saloutos | 0:083111ae2a11 | 178 | { |
saloutos | 0:083111ae2a11 | 179 | __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2); |
saloutos | 0:083111ae2a11 | 180 | } |
saloutos | 0:083111ae2a11 | 181 | |
saloutos | 0:083111ae2a11 | 182 | #endif // TIM_MST_16BIT |