mbed library sources. Supersedes mbed-src.
Dependents: Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more
targets/TARGET_STM/hal_tick_32b.c@175:af195413fb11, 2017-10-11 (annotated)
- Committer:
- AnnaBridge
- Date:
- Wed Oct 11 12:45:49 2017 +0100
- Revision:
- 175:af195413fb11
- Parent:
- 174:b96e65c34a4d
- Child:
- 180:96ed750bd169
This updates the lib to the mbed lib v 153
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 32-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 | |
<> | 160:d5399cc887bb | 25 | extern void HAL_IncTick(void); |
<> | 160:d5399cc887bb | 26 | |
<> | 153:fa9ff456f731 | 27 | volatile uint32_t PreviousVal = 0; |
<> | 153:fa9ff456f731 | 28 | |
<> | 153:fa9ff456f731 | 29 | void us_ticker_irq_handler(void); |
<> | 153:fa9ff456f731 | 30 | |
<> | 153:fa9ff456f731 | 31 | void timer_irq_handler(void) |
<> | 153:fa9ff456f731 | 32 | { |
<> | 153:fa9ff456f731 | 33 | // Channel 1 for mbed timeout |
<> | 153:fa9ff456f731 | 34 | if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) { |
<> | 153:fa9ff456f731 | 35 | if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC1) == SET) { |
<> | 153:fa9ff456f731 | 36 | __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1); |
<> | 153:fa9ff456f731 | 37 | us_ticker_irq_handler(); |
<> | 153:fa9ff456f731 | 38 | } |
<> | 153:fa9ff456f731 | 39 | } |
<> | 153:fa9ff456f731 | 40 | |
<> | 153:fa9ff456f731 | 41 | // Channel 2 for HAL tick |
<> | 153:fa9ff456f731 | 42 | if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC2) == SET) { |
<> | 153:fa9ff456f731 | 43 | if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC2) == SET) { |
<> | 153:fa9ff456f731 | 44 | __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC2); |
<> | 153:fa9ff456f731 | 45 | uint32_t val = __HAL_TIM_GetCounter(&TimMasterHandle); |
<> | 153:fa9ff456f731 | 46 | if ((val - PreviousVal) >= HAL_TICK_DELAY) { |
<> | 153:fa9ff456f731 | 47 | // Increment HAL variable |
<> | 153:fa9ff456f731 | 48 | HAL_IncTick(); |
<> | 153:fa9ff456f731 | 49 | // Prepare next interrupt |
AnnaBridge | 165:e614a9f1c9e2 | 50 | __HAL_TIM_SET_COMPARE(&TimMasterHandle, TIM_CHANNEL_2, val + HAL_TICK_DELAY); |
<> | 153:fa9ff456f731 | 51 | PreviousVal = val; |
<> | 153:fa9ff456f731 | 52 | #if DEBUG_TICK > 0 |
<> | 153:fa9ff456f731 | 53 | HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_6); |
<> | 153:fa9ff456f731 | 54 | #endif |
<> | 153:fa9ff456f731 | 55 | } |
<> | 153:fa9ff456f731 | 56 | } |
<> | 153:fa9ff456f731 | 57 | } |
<> | 153:fa9ff456f731 | 58 | } |
<> | 153:fa9ff456f731 | 59 | |
<> | 153:fa9ff456f731 | 60 | // Reconfigure the HAL tick using a standard timer instead of systick. |
<> | 153:fa9ff456f731 | 61 | HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) |
<> | 153:fa9ff456f731 | 62 | { |
<> | 153:fa9ff456f731 | 63 | RCC_ClkInitTypeDef RCC_ClkInitStruct; |
<> | 153:fa9ff456f731 | 64 | uint32_t PclkFreq; |
<> | 153:fa9ff456f731 | 65 | |
<> | 153:fa9ff456f731 | 66 | // Get clock configuration |
<> | 153:fa9ff456f731 | 67 | // Note: PclkFreq contains here the Latency (not used after) |
<> | 153:fa9ff456f731 | 68 | HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq); |
<> | 153:fa9ff456f731 | 69 | |
<> | 153:fa9ff456f731 | 70 | // Get timer clock value |
<> | 153:fa9ff456f731 | 71 | #if TIM_MST_PCLK == 1 |
<> | 153:fa9ff456f731 | 72 | PclkFreq = HAL_RCC_GetPCLK1Freq(); |
<> | 153:fa9ff456f731 | 73 | #else |
<> | 153:fa9ff456f731 | 74 | PclkFreq = HAL_RCC_GetPCLK2Freq(); |
<> | 153:fa9ff456f731 | 75 | #endif |
<> | 153:fa9ff456f731 | 76 | |
<> | 153:fa9ff456f731 | 77 | // Enable timer clock |
<> | 153:fa9ff456f731 | 78 | TIM_MST_RCC; |
<> | 153:fa9ff456f731 | 79 | |
<> | 153:fa9ff456f731 | 80 | // Reset timer |
<> | 153:fa9ff456f731 | 81 | TIM_MST_RESET_ON; |
<> | 153:fa9ff456f731 | 82 | TIM_MST_RESET_OFF; |
<> | 153:fa9ff456f731 | 83 | |
<> | 153:fa9ff456f731 | 84 | // Configure time base |
<> | 153:fa9ff456f731 | 85 | TimMasterHandle.Instance = TIM_MST; |
<> | 153:fa9ff456f731 | 86 | TimMasterHandle.Init.Period = 0xFFFFFFFF; |
<> | 153:fa9ff456f731 | 87 | |
<> | 153:fa9ff456f731 | 88 | // TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx |
<> | 153:fa9ff456f731 | 89 | #if TIM_MST_PCLK == 1 |
<> | 153:fa9ff456f731 | 90 | if (RCC_ClkInitStruct.APB1CLKDivider == RCC_HCLK_DIV1) { |
<> | 153:fa9ff456f731 | 91 | #else |
<> | 153:fa9ff456f731 | 92 | if (RCC_ClkInitStruct.APB2CLKDivider == RCC_HCLK_DIV1) { |
<> | 153:fa9ff456f731 | 93 | #endif |
<> | 153:fa9ff456f731 | 94 | TimMasterHandle.Init.Prescaler = (uint16_t)((PclkFreq) / 1000000) - 1; // 1 us tick |
<> | 160:d5399cc887bb | 95 | } else { |
<> | 153:fa9ff456f731 | 96 | TimMasterHandle.Init.Prescaler = (uint16_t)((PclkFreq * 2) / 1000000) - 1; // 1 us tick |
<> | 153:fa9ff456f731 | 97 | } |
<> | 153:fa9ff456f731 | 98 | |
<> | 153:fa9ff456f731 | 99 | TimMasterHandle.Init.ClockDivision = 0; |
<> | 153:fa9ff456f731 | 100 | TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP; |
<> | 153:fa9ff456f731 | 101 | #if !TARGET_STM32L1 |
<> | 153:fa9ff456f731 | 102 | TimMasterHandle.Init.RepetitionCounter = 0; |
<> | 153:fa9ff456f731 | 103 | #endif |
AnnaBridge | 167:e84263d55307 | 104 | #ifdef TIM_AUTORELOAD_PRELOAD_DISABLE |
<> | 156:95d6b41a828b | 105 | TimMasterHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; |
<> | 156:95d6b41a828b | 106 | #endif |
<> | 153:fa9ff456f731 | 107 | HAL_TIM_OC_Init(&TimMasterHandle); |
<> | 153:fa9ff456f731 | 108 | |
<> | 153:fa9ff456f731 | 109 | NVIC_SetVector(TIM_MST_IRQ, (uint32_t)timer_irq_handler); |
<> | 153:fa9ff456f731 | 110 | NVIC_EnableIRQ(TIM_MST_IRQ); |
<> | 153:fa9ff456f731 | 111 | |
<> | 153:fa9ff456f731 | 112 | // Channel 1 for mbed timeout |
<> | 153:fa9ff456f731 | 113 | HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1); |
<> | 153:fa9ff456f731 | 114 | |
<> | 153:fa9ff456f731 | 115 | // Channel 2 for HAL tick |
<> | 153:fa9ff456f731 | 116 | HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_2); |
<> | 153:fa9ff456f731 | 117 | PreviousVal = __HAL_TIM_GetCounter(&TimMasterHandle); |
AnnaBridge | 165:e614a9f1c9e2 | 118 | __HAL_TIM_SET_COMPARE(&TimMasterHandle, TIM_CHANNEL_2, PreviousVal + HAL_TICK_DELAY); |
<> | 153:fa9ff456f731 | 119 | __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2); |
<> | 153:fa9ff456f731 | 120 | |
AnnaBridge | 173:e131a1973e81 | 121 | // Freeze timer on stop/breakpoint |
AnnaBridge | 175:af195413fb11 | 122 | // Define the FREEZE_TIMER_ON_DEBUG macro in mbed_app.json for example |
AnnaBridge | 175:af195413fb11 | 123 | #if !defined(NDEBUG) && defined(FREEZE_TIMER_ON_DEBUG) && defined(TIM_MST_DBGMCU_FREEZE) |
AnnaBridge | 173:e131a1973e81 | 124 | TIM_MST_DBGMCU_FREEZE; |
AnnaBridge | 173:e131a1973e81 | 125 | #endif |
AnnaBridge | 173:e131a1973e81 | 126 | |
<> | 153:fa9ff456f731 | 127 | #if DEBUG_TICK > 0 |
AnnaBridge | 165:e614a9f1c9e2 | 128 | __HAL_RCC_GPIOB_CLK_ENABLE(); |
<> | 153:fa9ff456f731 | 129 | GPIO_InitTypeDef GPIO_InitStruct; |
<> | 153:fa9ff456f731 | 130 | GPIO_InitStruct.Pin = GPIO_PIN_6; |
<> | 153:fa9ff456f731 | 131 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
<> | 153:fa9ff456f731 | 132 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
<> | 153:fa9ff456f731 | 133 | GPIO_InitStruct.Speed = GPIO_SPEED_FAST; |
<> | 153:fa9ff456f731 | 134 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
<> | 153:fa9ff456f731 | 135 | #endif |
<> | 153:fa9ff456f731 | 136 | |
<> | 153:fa9ff456f731 | 137 | return HAL_OK; |
<> | 153:fa9ff456f731 | 138 | } |
<> | 153:fa9ff456f731 | 139 | |
AnnaBridge | 174:b96e65c34a4d | 140 | /* NOTE: must be called with interrupts disabled! */ |
<> | 153:fa9ff456f731 | 141 | void HAL_SuspendTick(void) |
<> | 153:fa9ff456f731 | 142 | { |
<> | 153:fa9ff456f731 | 143 | __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC2); |
<> | 153:fa9ff456f731 | 144 | } |
<> | 153:fa9ff456f731 | 145 | |
AnnaBridge | 174:b96e65c34a4d | 146 | /* NOTE: must be called with interrupts disabled! */ |
<> | 153:fa9ff456f731 | 147 | void HAL_ResumeTick(void) |
<> | 153:fa9ff456f731 | 148 | { |
<> | 153:fa9ff456f731 | 149 | __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2); |
<> | 153:fa9ff456f731 | 150 | } |
<> | 153:fa9ff456f731 | 151 | |
<> | 153:fa9ff456f731 | 152 | #endif // !TIM_MST_16BIT |