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