001

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ganlikun 0:13413ea9a877 1 /* mbed Microcontroller Library
ganlikun 0:13413ea9a877 2 * Copyright (c) 2006-2016 ARM Limited
ganlikun 0:13413ea9a877 3 *
ganlikun 0:13413ea9a877 4 * Licensed under the Apache License, Version 2.0 (the "License");
ganlikun 0:13413ea9a877 5 * you may not use this file except in compliance with the License.
ganlikun 0:13413ea9a877 6 * You may obtain a copy of the License at
ganlikun 0:13413ea9a877 7 *
ganlikun 0:13413ea9a877 8 * http://www.apache.org/licenses/LICENSE-2.0
ganlikun 0:13413ea9a877 9 *
ganlikun 0:13413ea9a877 10 * Unless required by applicable law or agreed to in writing, software
ganlikun 0:13413ea9a877 11 * distributed under the License is distributed on an "AS IS" BASIS,
ganlikun 0:13413ea9a877 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ganlikun 0:13413ea9a877 13 * See the License for the specific language governing permissions and
ganlikun 0:13413ea9a877 14 * limitations under the License.
ganlikun 0:13413ea9a877 15 */
ganlikun 0:13413ea9a877 16 #include "hal_tick.h"
ganlikun 0:13413ea9a877 17
ganlikun 0:13413ea9a877 18 // A 32-bit timer is used
ganlikun 0:13413ea9a877 19 #if !TIM_MST_16BIT
ganlikun 0:13413ea9a877 20
ganlikun 0:13413ea9a877 21 #define DEBUG_TICK 0 // Set to 1 to toggle a pin (see below which pin) at each tick
ganlikun 0:13413ea9a877 22
ganlikun 0:13413ea9a877 23 extern TIM_HandleTypeDef TimMasterHandle;
ganlikun 0:13413ea9a877 24
ganlikun 0:13413ea9a877 25 extern void HAL_IncTick(void);
ganlikun 0:13413ea9a877 26
ganlikun 0:13413ea9a877 27 volatile uint32_t PreviousVal = 0;
ganlikun 0:13413ea9a877 28
ganlikun 0:13413ea9a877 29 void us_ticker_irq_handler(void);
ganlikun 0:13413ea9a877 30
ganlikun 0:13413ea9a877 31 void timer_irq_handler(void)
ganlikun 0:13413ea9a877 32 {
ganlikun 0:13413ea9a877 33 // Channel 1 for mbed timeout
ganlikun 0:13413ea9a877 34 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) {
ganlikun 0:13413ea9a877 35 if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC1) == SET) {
ganlikun 0:13413ea9a877 36 __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1);
ganlikun 0:13413ea9a877 37 us_ticker_irq_handler();
ganlikun 0:13413ea9a877 38 }
ganlikun 0:13413ea9a877 39 }
ganlikun 0:13413ea9a877 40
ganlikun 0:13413ea9a877 41 // Channel 2 for HAL tick
ganlikun 0:13413ea9a877 42 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC2) == SET) {
ganlikun 0:13413ea9a877 43 if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC2) == SET) {
ganlikun 0:13413ea9a877 44 __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC2);
ganlikun 0:13413ea9a877 45 uint32_t val = __HAL_TIM_GetCounter(&TimMasterHandle);
ganlikun 0:13413ea9a877 46 if ((val - PreviousVal) >= HAL_TICK_DELAY) {
ganlikun 0:13413ea9a877 47 // Increment HAL variable
ganlikun 0:13413ea9a877 48 HAL_IncTick();
ganlikun 0:13413ea9a877 49 // Prepare next interrupt
ganlikun 0:13413ea9a877 50 __HAL_TIM_SET_COMPARE(&TimMasterHandle, TIM_CHANNEL_2, val + HAL_TICK_DELAY);
ganlikun 0:13413ea9a877 51 PreviousVal = val;
ganlikun 0:13413ea9a877 52 #if DEBUG_TICK > 0
ganlikun 0:13413ea9a877 53 HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_6);
ganlikun 0:13413ea9a877 54 #endif
ganlikun 0:13413ea9a877 55 }
ganlikun 0:13413ea9a877 56 }
ganlikun 0:13413ea9a877 57 }
ganlikun 0:13413ea9a877 58 }
ganlikun 0:13413ea9a877 59
ganlikun 0:13413ea9a877 60 // Reconfigure the HAL tick using a standard timer instead of systick.
ganlikun 0:13413ea9a877 61 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
ganlikun 0:13413ea9a877 62 {
ganlikun 0:13413ea9a877 63 RCC_ClkInitTypeDef RCC_ClkInitStruct;
ganlikun 0:13413ea9a877 64 uint32_t PclkFreq;
ganlikun 0:13413ea9a877 65
ganlikun 0:13413ea9a877 66 // Get clock configuration
ganlikun 0:13413ea9a877 67 // Note: PclkFreq contains here the Latency (not used after)
ganlikun 0:13413ea9a877 68 HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq);
ganlikun 0:13413ea9a877 69
ganlikun 0:13413ea9a877 70 // Get timer clock value
ganlikun 0:13413ea9a877 71 #if TIM_MST_PCLK == 1
ganlikun 0:13413ea9a877 72 PclkFreq = HAL_RCC_GetPCLK1Freq();
ganlikun 0:13413ea9a877 73 #else
ganlikun 0:13413ea9a877 74 PclkFreq = HAL_RCC_GetPCLK2Freq();
ganlikun 0:13413ea9a877 75 #endif
ganlikun 0:13413ea9a877 76
ganlikun 0:13413ea9a877 77 // Enable timer clock
ganlikun 0:13413ea9a877 78 TIM_MST_RCC;
ganlikun 0:13413ea9a877 79
ganlikun 0:13413ea9a877 80 // Reset timer
ganlikun 0:13413ea9a877 81 TIM_MST_RESET_ON;
ganlikun 0:13413ea9a877 82 TIM_MST_RESET_OFF;
ganlikun 0:13413ea9a877 83
ganlikun 0:13413ea9a877 84 // Configure time base
ganlikun 0:13413ea9a877 85 TimMasterHandle.Instance = TIM_MST;
ganlikun 0:13413ea9a877 86 TimMasterHandle.Init.Period = 0xFFFFFFFF;
ganlikun 0:13413ea9a877 87
ganlikun 0:13413ea9a877 88 // TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx
ganlikun 0:13413ea9a877 89 #if TIM_MST_PCLK == 1
ganlikun 0:13413ea9a877 90 if (RCC_ClkInitStruct.APB1CLKDivider == RCC_HCLK_DIV1) {
ganlikun 0:13413ea9a877 91 #else
ganlikun 0:13413ea9a877 92 if (RCC_ClkInitStruct.APB2CLKDivider == RCC_HCLK_DIV1) {
ganlikun 0:13413ea9a877 93 #endif
ganlikun 0:13413ea9a877 94 TimMasterHandle.Init.Prescaler = (uint16_t)((PclkFreq) / 1000000) - 1; // 1 us tick
ganlikun 0:13413ea9a877 95 } else {
ganlikun 0:13413ea9a877 96 TimMasterHandle.Init.Prescaler = (uint16_t)((PclkFreq * 2) / 1000000) - 1; // 1 us tick
ganlikun 0:13413ea9a877 97 }
ganlikun 0:13413ea9a877 98
ganlikun 0:13413ea9a877 99 TimMasterHandle.Init.ClockDivision = 0;
ganlikun 0:13413ea9a877 100 TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
ganlikun 0:13413ea9a877 101 #if !TARGET_STM32L1
ganlikun 0:13413ea9a877 102 TimMasterHandle.Init.RepetitionCounter = 0;
ganlikun 0:13413ea9a877 103 #endif
ganlikun 0:13413ea9a877 104 #ifdef TIM_AUTORELOAD_PRELOAD_DISABLE
ganlikun 0:13413ea9a877 105 TimMasterHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
ganlikun 0:13413ea9a877 106 #endif
ganlikun 0:13413ea9a877 107 HAL_TIM_OC_Init(&TimMasterHandle);
ganlikun 0:13413ea9a877 108
ganlikun 0:13413ea9a877 109 NVIC_SetVector(TIM_MST_IRQ, (uint32_t)timer_irq_handler);
ganlikun 0:13413ea9a877 110 NVIC_EnableIRQ(TIM_MST_IRQ);
ganlikun 0:13413ea9a877 111
ganlikun 0:13413ea9a877 112 // Channel 1 for mbed timeout
ganlikun 0:13413ea9a877 113 HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1);
ganlikun 0:13413ea9a877 114
ganlikun 0:13413ea9a877 115 // Channel 2 for HAL tick
ganlikun 0:13413ea9a877 116 HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_2);
ganlikun 0:13413ea9a877 117 PreviousVal = __HAL_TIM_GetCounter(&TimMasterHandle);
ganlikun 0:13413ea9a877 118 __HAL_TIM_SET_COMPARE(&TimMasterHandle, TIM_CHANNEL_2, PreviousVal + HAL_TICK_DELAY);
ganlikun 0:13413ea9a877 119 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2);
ganlikun 0:13413ea9a877 120
ganlikun 0:13413ea9a877 121 // Freeze timer on stop/breakpoint
ganlikun 0:13413ea9a877 122 // Define the FREEZE_TIMER_ON_DEBUG macro in mbed_app.json for example
ganlikun 0:13413ea9a877 123 #if !defined(NDEBUG) && defined(FREEZE_TIMER_ON_DEBUG) && defined(TIM_MST_DBGMCU_FREEZE)
ganlikun 0:13413ea9a877 124 TIM_MST_DBGMCU_FREEZE;
ganlikun 0:13413ea9a877 125 #endif
ganlikun 0:13413ea9a877 126
ganlikun 0:13413ea9a877 127 #if DEBUG_TICK > 0
ganlikun 0:13413ea9a877 128 __HAL_RCC_GPIOB_CLK_ENABLE();
ganlikun 0:13413ea9a877 129 GPIO_InitTypeDef GPIO_InitStruct;
ganlikun 0:13413ea9a877 130 GPIO_InitStruct.Pin = GPIO_PIN_6;
ganlikun 0:13413ea9a877 131 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
ganlikun 0:13413ea9a877 132 GPIO_InitStruct.Pull = GPIO_PULLUP;
ganlikun 0:13413ea9a877 133 GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
ganlikun 0:13413ea9a877 134 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
ganlikun 0:13413ea9a877 135 #endif
ganlikun 0:13413ea9a877 136
ganlikun 0:13413ea9a877 137 return HAL_OK;
ganlikun 0:13413ea9a877 138 }
ganlikun 0:13413ea9a877 139
ganlikun 0:13413ea9a877 140 /* NOTE: must be called with interrupts disabled! */
ganlikun 0:13413ea9a877 141 void HAL_SuspendTick(void)
ganlikun 0:13413ea9a877 142 {
ganlikun 0:13413ea9a877 143 __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC2);
ganlikun 0:13413ea9a877 144 }
ganlikun 0:13413ea9a877 145
ganlikun 0:13413ea9a877 146 /* NOTE: must be called with interrupts disabled! */
ganlikun 0:13413ea9a877 147 void HAL_ResumeTick(void)
ganlikun 0:13413ea9a877 148 {
ganlikun 0:13413ea9a877 149 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2);
ganlikun 0:13413ea9a877 150 }
ganlikun 0:13413ea9a877 151
ganlikun 0:13413ea9a877 152 #endif // !TIM_MST_16BIT
ganlikun 0:13413ea9a877 153