From 609ade713085e211a4ca9f7eaa1da0200ad48dd9 Mon Sep 17 00:00:00 2001 From: bcostm Date: Wed, 20 Jun 2018 13:40:28 +0200 Subject: [PATCH] stm32 ticker: rename files and move functions - rename hal_tick_common.c in hal_tick_overrides.c - move 16 and 32bits timer functions in us_ticker.c --- targets/TARGET_STM/hal_tick_16b.c | 120 ------------ targets/TARGET_STM/hal_tick_32b.c | 107 ----------- ...hal_tick_common.c => hal_tick_overrides.c} | 0 targets/TARGET_STM/us_ticker.c | 172 +++++++++++++++++- 4 files changed, 170 insertions(+), 229 deletions(-) delete mode 100644 targets/TARGET_STM/hal_tick_16b.c delete mode 100644 targets/TARGET_STM/hal_tick_32b.c rename targets/TARGET_STM/{hal_tick_common.c => hal_tick_overrides.c} (100%) diff --git a/targets/TARGET_STM/hal_tick_16b.c b/targets/TARGET_STM/hal_tick_16b.c deleted file mode 100644 index 8b0a578431..0000000000 --- a/targets/TARGET_STM/hal_tick_16b.c +++ /dev/null @@ -1,120 +0,0 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2016 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "hal_tick.h" - -// A 16-bit timer is used -#if TIM_MST_16BIT - -extern TIM_HandleTypeDef TimMasterHandle; -extern uint32_t prev_time; -extern uint32_t elapsed_time; - -volatile uint32_t PreviousVal = 0; - -void us_ticker_irq_handler(void); - -#if defined(TARGET_STM32F0) -void timer_update_irq_handler(void) -{ -#else -void timer_irq_handler(void) -{ -#endif - TimMasterHandle.Instance = TIM_MST; - -#if defined(TARGET_STM32F0) -} // end timer_update_irq_handler function - -// Channel 1 used for mbed timeout -void timer_oc_irq_handler(void) -{ - TimMasterHandle.Instance = TIM_MST; -#endif - - // Channel 1 for mbed timeout - if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) { - if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC1) == SET) { - __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1); - us_ticker_irq_handler(); - } - } -} - -// Overwrite the default ST HAL function (defined as "weak") in order to configure an HW timer -// used for mbed timeouts only (not used for the Systick configuration). -// Additional notes: -// - The default ST HAL_InitTick function initializes the Systick to 1 ms and this is not correct for mbed -// as the mbed Systick as to be configured to 1 us instead. -// - Furthermore the Systick is configured by mbed RTOS directly. -HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) -{ - // Enable timer clock - TIM_MST_RCC; - - // Reset timer - TIM_MST_RESET_ON; - TIM_MST_RESET_OFF; - - // Update the SystemCoreClock variable - SystemCoreClockUpdate(); - - // Configure time base - TimMasterHandle.Instance = TIM_MST; - TimMasterHandle.Init.Period = 0xFFFF; - TimMasterHandle.Init.Prescaler = (uint32_t)(SystemCoreClock / 1000000) - 1; // 1 us tick - TimMasterHandle.Init.ClockDivision = 0; - TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP; -#if !defined(TARGET_STM32L0) && !defined(TARGET_STM32L1) - TimMasterHandle.Init.RepetitionCounter = 0; -#endif -#ifdef TIM_AUTORELOAD_PRELOAD_DISABLE - TimMasterHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; -#endif - HAL_TIM_Base_Init(&TimMasterHandle); - - // Configure output compare channel 1 for mbed timeout (enabled later when used) - HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1); - - // Output compare channel 1 interrupt for mbed timeout -#if defined(TARGET_STM32F0) - NVIC_SetVector(TIM_MST_UP_IRQ, (uint32_t)timer_update_irq_handler); - NVIC_EnableIRQ(TIM_MST_UP_IRQ); - NVIC_SetPriority(TIM_MST_UP_IRQ, 0); - NVIC_SetVector(TIM_MST_OC_IRQ, (uint32_t)timer_oc_irq_handler); - NVIC_EnableIRQ(TIM_MST_OC_IRQ); - NVIC_SetPriority(TIM_MST_OC_IRQ, 1); -#else - NVIC_SetVector(TIM_MST_IRQ, (uint32_t)timer_irq_handler); - NVIC_EnableIRQ(TIM_MST_IRQ); -#endif - - // Enable timer - HAL_TIM_Base_Start(&TimMasterHandle); - - // Freeze timer on stop/breakpoint - // Define the FREEZE_TIMER_ON_DEBUG macro in mbed_app.json for example -#if !defined(NDEBUG) && defined(FREEZE_TIMER_ON_DEBUG) && defined(TIM_MST_DBGMCU_FREEZE) - TIM_MST_DBGMCU_FREEZE; -#endif - - // Used by HAL_GetTick() - prev_time = 0; - elapsed_time = 0; - - return HAL_OK; -} - -#endif // TIM_MST_16BIT diff --git a/targets/TARGET_STM/hal_tick_32b.c b/targets/TARGET_STM/hal_tick_32b.c deleted file mode 100644 index d056268c2e..0000000000 --- a/targets/TARGET_STM/hal_tick_32b.c +++ /dev/null @@ -1,107 +0,0 @@ -/* mbed Microcontroller Library - * Copyright (c) 2006-2016 ARM Limited - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "hal_tick.h" - -// A 32-bit timer is used -#if !TIM_MST_16BIT - -extern TIM_HandleTypeDef TimMasterHandle; - -volatile uint32_t PreviousVal = 0; - -void us_ticker_irq_handler(void); - -void timer_irq_handler(void) -{ - // Channel 1 for mbed timeout - if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) { - if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC1) == SET) { - __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1); - us_ticker_irq_handler(); - } - } -} - -// Overwrite the default ST HAL function (defined as "weak") in order to configure an HW timer -// used for mbed timeouts only (not used for the Systick configuration). -// Additional notes: -// - The default ST HAL_InitTick function initializes the Systick to 1 ms and this is not correct for mbed -// as the mbed Systick as to be configured to 1 us instead. -// - Furthermore the Systick is configured by mbed RTOS directly. -HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) -{ - RCC_ClkInitTypeDef RCC_ClkInitStruct; - uint32_t PclkFreq; - - // Get clock configuration - // Note: PclkFreq contains here the Latency (not used after) - HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq); - - // Get timer clock value -#if TIM_MST_PCLK == 1 - PclkFreq = HAL_RCC_GetPCLK1Freq(); -#else - PclkFreq = HAL_RCC_GetPCLK2Freq(); -#endif - - // Enable timer clock - TIM_MST_RCC; - - // Reset timer - TIM_MST_RESET_ON; - TIM_MST_RESET_OFF; - - // Configure time base - TimMasterHandle.Instance = TIM_MST; - TimMasterHandle.Init.Period = 0xFFFFFFFF; - - // TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx -#if TIM_MST_PCLK == 1 - if (RCC_ClkInitStruct.APB1CLKDivider == RCC_HCLK_DIV1) { -#else - if (RCC_ClkInitStruct.APB2CLKDivider == RCC_HCLK_DIV1) { -#endif - TimMasterHandle.Init.Prescaler = (uint16_t)((PclkFreq) / 1000000) - 1; // 1 us tick - } else { - TimMasterHandle.Init.Prescaler = (uint16_t)((PclkFreq * 2) / 1000000) - 1; // 1 us tick - } - - TimMasterHandle.Init.ClockDivision = 0; - TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP; -#if !TARGET_STM32L1 - TimMasterHandle.Init.RepetitionCounter = 0; -#endif -#ifdef TIM_AUTORELOAD_PRELOAD_DISABLE - TimMasterHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; -#endif - HAL_TIM_OC_Init(&TimMasterHandle); - - NVIC_SetVector(TIM_MST_IRQ, (uint32_t)timer_irq_handler); - NVIC_EnableIRQ(TIM_MST_IRQ); - - // Channel 1 for mbed timeout - HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1); - - // Freeze timer on stop/breakpoint - // Define the FREEZE_TIMER_ON_DEBUG macro in mbed_app.json for example -#if !defined(NDEBUG) && defined(FREEZE_TIMER_ON_DEBUG) && defined(TIM_MST_DBGMCU_FREEZE) - TIM_MST_DBGMCU_FREEZE; -#endif - - return HAL_OK; -} - -#endif // !TIM_MST_16BIT diff --git a/targets/TARGET_STM/hal_tick_common.c b/targets/TARGET_STM/hal_tick_overrides.c similarity index 100% rename from targets/TARGET_STM/hal_tick_common.c rename to targets/TARGET_STM/hal_tick_overrides.c diff --git a/targets/TARGET_STM/us_ticker.c b/targets/TARGET_STM/us_ticker.c index ff1e777a8b..c31f4ea501 100644 --- a/targets/TARGET_STM/us_ticker.c +++ b/targets/TARGET_STM/us_ticker.c @@ -18,8 +18,10 @@ #include "PeripheralNames.h" #include "hal_tick.h" +// ************************************ 16-bit timer ************************************ #if TIM_MST_16BIT #define TIMER_TICKER_BIT_WIDTH 16 +// ************************************ 32-bit timer ************************************ #else #define TIMER_TICKER_BIT_WIDTH 32 #endif @@ -35,10 +37,176 @@ const ticker_info_t *us_ticker_get_info() return &info; } +volatile uint32_t PreviousVal = 0; + +void us_ticker_irq_handler(void); + +// ************************************ 16-bit timer ************************************ +#if TIM_MST_16BIT + +#if defined(TARGET_STM32F0) +void timer_update_irq_handler(void) { +#else +void timer_irq_handler(void) +{ +#endif + TimMasterHandle.Instance = TIM_MST; + +#if defined(TARGET_STM32F0) +} // end timer_update_irq_handler function + +// Channel 1 used for mbed timeout +void timer_oc_irq_handler(void) +{ + TimMasterHandle.Instance = TIM_MST; +#endif + + // Channel 1 for mbed timeout + if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) { + if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC1) == SET) { + __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1); + us_ticker_irq_handler(); + } + } +} + +// ************************************ 32-bit timer ************************************ +#else + +void timer_irq_handler(void) +{ + // Channel 1 for mbed timeout + if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) { + if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC1) == SET) { + __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1); + us_ticker_irq_handler(); + } + } +} + +#endif // 16-bit/32-bit timer + void us_ticker_init(void) { - /* NOTE: assuming that HAL tick has already been initialized! */ + +// ************************************ 16-bit timer ************************************ +#if TIM_MST_16BIT + // Enable timer clock + TIM_MST_RCC; + + // Reset timer + TIM_MST_RESET_ON; + TIM_MST_RESET_OFF; + + // Update the SystemCoreClock variable + SystemCoreClockUpdate(); + + // Configure time base + TimMasterHandle.Instance = TIM_MST; + TimMasterHandle.Init.Period = 0xFFFF; + TimMasterHandle.Init.Prescaler = (uint32_t)(SystemCoreClock / 1000000) - 1; // 1 us tick + TimMasterHandle.Init.ClockDivision = 0; + TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP; +#if !defined(TARGET_STM32L0) && !defined(TARGET_STM32L1) + TimMasterHandle.Init.RepetitionCounter = 0; +#endif +#ifdef TIM_AUTORELOAD_PRELOAD_DISABLE + TimMasterHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; +#endif + HAL_TIM_Base_Init(&TimMasterHandle); + + // Configure output compare channel 1 for mbed timeout (enabled later when used) + HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1); + + // Output compare channel 1 interrupt for mbed timeout +#if defined(TARGET_STM32F0) + NVIC_SetVector(TIM_MST_UP_IRQ, (uint32_t)timer_update_irq_handler); + NVIC_EnableIRQ(TIM_MST_UP_IRQ); + NVIC_SetPriority(TIM_MST_UP_IRQ, 0); + NVIC_SetVector(TIM_MST_OC_IRQ, (uint32_t)timer_oc_irq_handler); + NVIC_EnableIRQ(TIM_MST_OC_IRQ); + NVIC_SetPriority(TIM_MST_OC_IRQ, 1); +#else + NVIC_SetVector(TIM_MST_IRQ, (uint32_t)timer_irq_handler); + NVIC_EnableIRQ(TIM_MST_IRQ); +#endif + + // Enable timer + HAL_TIM_Base_Start(&TimMasterHandle); + + // Freeze timer on stop/breakpoint + // Define the FREEZE_TIMER_ON_DEBUG macro in mbed_app.json for example +#if !defined(NDEBUG) && defined(FREEZE_TIMER_ON_DEBUG) && defined(TIM_MST_DBGMCU_FREEZE) + TIM_MST_DBGMCU_FREEZE; +#endif + __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1); + +// ************************************ 32-bit timer ************************************ +#else + + RCC_ClkInitTypeDef RCC_ClkInitStruct; + uint32_t PclkFreq; + + // Get clock configuration + // Note: PclkFreq contains here the Latency (not used after) + HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq); + + // Get timer clock value +#if TIM_MST_PCLK == 1 + PclkFreq = HAL_RCC_GetPCLK1Freq(); +#else + PclkFreq = HAL_RCC_GetPCLK2Freq(); +#endif + + // Enable timer clock + TIM_MST_RCC; + + // Reset timer + TIM_MST_RESET_ON; + TIM_MST_RESET_OFF; + + // Configure time base + TimMasterHandle.Instance = TIM_MST; + TimMasterHandle.Init.Period = 0xFFFFFFFF; + + // TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx +#if TIM_MST_PCLK == 1 + if (RCC_ClkInitStruct.APB1CLKDivider == RCC_HCLK_DIV1) { +#else + if (RCC_ClkInitStruct.APB2CLKDivider == RCC_HCLK_DIV1) { +#endif + TimMasterHandle.Init.Prescaler = (uint16_t)((PclkFreq) / 1000000) - 1; // 1 us tick + } else { + TimMasterHandle.Init.Prescaler = (uint16_t)((PclkFreq * 2) / 1000000) - 1; // 1 us tick + } + + TimMasterHandle.Init.ClockDivision = 0; + TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP; +#if !TARGET_STM32L1 + TimMasterHandle.Init.RepetitionCounter = 0; +#endif +#ifdef TIM_AUTORELOAD_PRELOAD_DISABLE + TimMasterHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; +#endif + HAL_TIM_OC_Init(&TimMasterHandle); + + NVIC_SetVector(TIM_MST_IRQ, (uint32_t)timer_irq_handler); + NVIC_EnableIRQ(TIM_MST_IRQ); + + // Channel 1 for mbed timeout + HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1); + + // Freeze timer on stop/breakpoint + // Define the FREEZE_TIMER_ON_DEBUG macro in mbed_app.json for example +#if !defined(NDEBUG) && defined(FREEZE_TIMER_ON_DEBUG) && defined(TIM_MST_DBGMCU_FREEZE) + TIM_MST_DBGMCU_FREEZE; +#endif + + __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1); + +#endif // 16-bit/32-bit timer + } uint32_t us_ticker_read() @@ -94,4 +262,4 @@ void restore_timer_ctx(void) __HAL_TIM_SET_COUNTER(&TimMasterHandle, timer_cnt_reg); __HAL_TIM_SET_COMPARE(&TimMasterHandle, TIM_CHANNEL_1, timer_ccr1_reg); TIM_MST->DIER = timer_dier_reg; -} +} \ No newline at end of file