This is an automated email from the ASF dual-hosted git repository. acassis pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 0c52787e1244c8befa1740e26a2e39079171738f Author: raiden00pl <[email protected]> AuthorDate: Fri Jun 5 15:29:56 2026 +0200 arch/arm/src/stm32: unify TIM free-running wrapper into common/stm32 Merge the per-IP and per-family free-running timer wrappers into a single common/stm32/stm32_freerun.c, following the timer-driver unification. - Rename stm32_freerun_m3m4_v1.c -> common/stm32/stm32_freerun.c (the most complete variant: CONFIG_CLOCK_TIMEKEEPING handling, timer-width based period, GTIM_SR_UIF/GTIM_DIER_UIE). - Delete the stm32l4_freerun.{c,h} and stm32wb_freerun.{c,h} copies - Pass GTIM_SR_UIF to CHECKINT in stm32_freerun_counter(); with the unified timer driver a 0 mask never reports a pending overflow. Signed-off-by: raiden00pl <[email protected]> Assisted-by: Claude Code --- .github/CODEOWNERS | 6 +- arch/arm/src/common/stm32/CMakeLists.txt | 7 +- arch/arm/src/common/stm32/Make.defs | 6 +- .../{stm32_freerun_m3m4_v1.c => stm32_freerun.c} | 6 +- arch/arm/src/stm32l4/CMakeLists.txt | 4 - arch/arm/src/stm32l4/Make.defs | 4 - arch/arm/src/stm32l4/stm32l4_freerun.c | 275 ------------------- arch/arm/src/stm32l4/stm32l4_freerun.h | 144 ---------- arch/arm/src/stm32l4/stm32l4_tickless.c | 2 +- arch/arm/src/stm32wb/CMakeLists.txt | 4 - arch/arm/src/stm32wb/Make.defs | 4 - arch/arm/src/stm32wb/stm32wb_freerun.c | 300 --------------------- arch/arm/src/stm32wb/stm32wb_freerun.h | 162 ----------- 13 files changed, 12 insertions(+), 912 deletions(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 9e313eecb15..80ae0e94770 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1753,8 +1753,8 @@ arch/arm/src/common/stm32/stm32_fmc_m3m4_v1.h [email protected] Nobuto.Ko arch/arm/src/common/stm32/stm32_foc.h [email protected] [email protected] [email protected] [email protected] [email protected] arch/arm/src/common/stm32/stm32_foc_m3m4_v1.c [email protected] [email protected] [email protected] [email protected] [email protected] arch/arm/src/common/stm32/stm32_foc_m3m4_v1.h [email protected] [email protected] [email protected] [email protected] [email protected] +arch/arm/src/common/stm32/stm32_freerun.c [email protected] [email protected] [email protected] [email protected] [email protected] arch/arm/src/common/stm32/stm32_freerun.h [email protected] [email protected] [email protected] [email protected] [email protected] -arch/arm/src/common/stm32/stm32_freerun_m3m4_v1.c [email protected] [email protected] [email protected] [email protected] [email protected] arch/arm/src/common/stm32/stm32_fsmc.h [email protected] [email protected] [email protected] [email protected] [email protected] arch/arm/src/common/stm32/stm32_fsmc_m3m4_v1.c [email protected] [email protected] [email protected] [email protected] [email protected] arch/arm/src/common/stm32/stm32_fsmc_m3m4_v1.h [email protected] [email protected] [email protected] [email protected] [email protected] @@ -4417,8 +4417,6 @@ arch/arm/src/stm32l4/stm32l4_firewall.c [email protected] alin.jerpelea@sony. arch/arm/src/stm32l4/stm32l4_firewall.h [email protected] [email protected] [email protected] [email protected] arch/arm/src/stm32l4/stm32l4_flash.c [email protected] [email protected] [email protected] [email protected] arch/arm/src/stm32l4/stm32l4_flash.h [email protected] [email protected] [email protected] [email protected] -arch/arm/src/stm32l4/stm32l4_freerun.c [email protected] [email protected] [email protected] [email protected] [email protected] -arch/arm/src/stm32l4/stm32l4_freerun.h [email protected] [email protected] [email protected] [email protected] [email protected] arch/arm/src/stm32l4/stm32l4_gpio.c [email protected] [email protected] [email protected] [email protected] arch/arm/src/stm32l4/stm32l4_gpio.h [email protected] [email protected] [email protected] [email protected] arch/arm/src/stm32l4/stm32l4_hsi48.c [email protected] [email protected] [email protected] [email protected] @@ -4631,8 +4629,6 @@ arch/arm/src/stm32wb/stm32wb_exti_pwr.c [email protected] [email protected] arch/arm/src/stm32wb/stm32wb_exti_wakeup.c [email protected] [email protected] arch/arm/src/stm32wb/stm32wb_flash.c [email protected] [email protected] [email protected] [email protected] arch/arm/src/stm32wb/stm32wb_flash.h [email protected] [email protected] -arch/arm/src/stm32wb/stm32wb_freerun.c [email protected] [email protected] -arch/arm/src/stm32wb/stm32wb_freerun.h [email protected] [email protected] arch/arm/src/stm32wb/stm32wb_gpio.c [email protected] [email protected] [email protected] [email protected] [email protected] arch/arm/src/stm32wb/stm32wb_gpio.h [email protected] [email protected] arch/arm/src/stm32wb/stm32wb_i2c.c [email protected] [email protected] [email protected] [email protected] [email protected] diff --git a/arch/arm/src/common/stm32/CMakeLists.txt b/arch/arm/src/common/stm32/CMakeLists.txt index d4ec5408deb..27a1d5d61c0 100644 --- a/arch/arm/src/common/stm32/CMakeLists.txt +++ b/arch/arm/src/common/stm32/CMakeLists.txt @@ -73,6 +73,10 @@ if(CONFIG_STM32_TIM) list(APPEND SRCS stm32_tim.c) endif() +if(CONFIG_STM32_FREERUN) + list(APPEND SRCS stm32_freerun.c) +endif() + if(CONFIG_STM32_PULSECOUNT) list(APPEND SRCS stm32_pulsecount.c) endif() @@ -182,9 +186,6 @@ if(CONFIG_STM32_COMMON_LEGACY) list(APPEND SRCS stm32_oneshot_m3m4_v1.c stm32_oneshot_m3m4_v1_lowerhalf.c) endif() - if(CONFIG_STM32_FREERUN) - list(APPEND SRCS stm32_freerun_m3m4_v1.c) - endif() if(CONFIG_STM32_HAVE_IP_I2C_M3M4_V1) if(CONFIG_STM32_I2C_ALT) diff --git a/arch/arm/src/common/stm32/Make.defs b/arch/arm/src/common/stm32/Make.defs index c944d5430ca..696a80cc4e4 100644 --- a/arch/arm/src/common/stm32/Make.defs +++ b/arch/arm/src/common/stm32/Make.defs @@ -84,6 +84,9 @@ endif ifeq ($(CONFIG_STM32_TIM),y) CHIP_CSRCS += stm32_tim.c endif +ifeq ($(CONFIG_STM32_FREERUN),y) +CHIP_CSRCS += stm32_freerun.c +endif ifeq ($(CONFIG_STM32_PULSECOUNT),y) CHIP_CSRCS += stm32_pulsecount.c @@ -188,9 +191,6 @@ CHIP_CSRCS += stm32_oneshot_m3m4_v1.c CHIP_CSRCS += stm32_oneshot_m3m4_v1_lowerhalf.c endif -ifeq ($(CONFIG_STM32_FREERUN),y) -CHIP_CSRCS += stm32_freerun_m3m4_v1.c -endif ifeq ($(CONFIG_STM32_HAVE_IP_I2C_M3M4_V1),y) ifeq ($(CONFIG_STM32_I2C_ALT),y) diff --git a/arch/arm/src/common/stm32/stm32_freerun_m3m4_v1.c b/arch/arm/src/common/stm32/stm32_freerun.c similarity index 98% rename from arch/arm/src/common/stm32/stm32_freerun_m3m4_v1.c rename to arch/arm/src/common/stm32/stm32_freerun.c index e8122d5ef41..e88870c05f4 100644 --- a/arch/arm/src/common/stm32/stm32_freerun_m3m4_v1.c +++ b/arch/arm/src/common/stm32/stm32_freerun.c @@ -1,5 +1,5 @@ /**************************************************************************** - * arch/arm/src/common/stm32/stm32_freerun_m3m4_v1.c + * arch/arm/src/common/stm32/stm32_freerun.c * * SPDX-License-Identifier: Apache-2.0 * @@ -200,7 +200,7 @@ int stm32_freerun_counter(struct stm32_freerun_s *freerun, overflow = freerun->overflow; counter = STM32_TIM_GETCOUNTER(freerun->tch); - pending = STM32_TIM_CHECKINT(freerun->tch, 0); + pending = STM32_TIM_CHECKINT(freerun->tch, GTIM_SR_UIF); verify = STM32_TIM_GETCOUNTER(freerun->tch); /* If an interrupt was pending before we re-enabled interrupts, @@ -298,4 +298,4 @@ int stm32_freerun_uninitialize(struct stm32_freerun_s *freerun) return OK; } -#endif /* CONFIG_STM32_ONESHOT */ +#endif /* CONFIG_STM32_FREERUN */ diff --git a/arch/arm/src/stm32l4/CMakeLists.txt b/arch/arm/src/stm32l4/CMakeLists.txt index 5be8e4d39e1..aa0f20571c1 100644 --- a/arch/arm/src/stm32l4/CMakeLists.txt +++ b/arch/arm/src/stm32l4/CMakeLists.txt @@ -54,10 +54,6 @@ if(CONFIG_STM32_ONESHOT) list(APPEND SRCS stm32l4_oneshot.c stm32l4_oneshot_lowerhalf.c) endif() -if(CONFIG_STM32_FREERUN) - list(APPEND SRCS stm32l4_freerun.c) -endif() - if(CONFIG_BUILD_PROTECTED) list(APPEND SRCS stm32l4_userspace.c stm32l4_mpuinit.c) endif() diff --git a/arch/arm/src/stm32l4/Make.defs b/arch/arm/src/stm32l4/Make.defs index 2c05d3ed2de..d4d7bed93cb 100644 --- a/arch/arm/src/stm32l4/Make.defs +++ b/arch/arm/src/stm32l4/Make.defs @@ -51,10 +51,6 @@ ifeq ($(CONFIG_STM32_ONESHOT),y) CHIP_CSRCS += stm32l4_oneshot.c stm32l4_oneshot_lowerhalf.c endif -ifeq ($(CONFIG_STM32_FREERUN),y) -CHIP_CSRCS += stm32l4_freerun.c -endif - ifeq ($(CONFIG_BUILD_PROTECTED),y) CHIP_CSRCS += stm32l4_userspace.c stm32l4_mpuinit.c endif diff --git a/arch/arm/src/stm32l4/stm32l4_freerun.c b/arch/arm/src/stm32l4/stm32l4_freerun.c deleted file mode 100644 index e156ec04bf5..00000000000 --- a/arch/arm/src/stm32l4/stm32l4_freerun.c +++ /dev/null @@ -1,275 +0,0 @@ -/**************************************************************************** - * arch/arm/src/stm32l4/stm32l4_freerun.c - * - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. The - * ASF licenses this file to you 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. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include <nuttx/config.h> - -#include <sys/types.h> -#include <stdint.h> -#include <stdbool.h> -#include <assert.h> -#include <errno.h> - -#include <nuttx/irq.h> -#include <nuttx/clock.h> - -#include "stm32l4_freerun.h" - -#ifdef CONFIG_STM32_FREERUN - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: stm32_freerun_handler - * - * Description: - * Timer interrupt callback. When the freerun timer counter overflows, - * this interrupt will occur. We will just increment an overflow count. - * - * Input Parameters: - * tch - The handle that represents the timer state - * arg - An opaque argument provided when the interrupt was registered - * sr - The value of the timer interrupt status register at the time - * that the interrupt occurred. - * - * Returned Value: - * None - * - ****************************************************************************/ - -static int stm32_freerun_handler(int irq, void *context, void *arg) -{ - struct stm32_freerun_s *freerun = - (struct stm32_freerun_s *)arg; - - DEBUGASSERT(freerun != NULL && freerun->overflow < UINT32_MAX); - freerun->overflow++; - - STM32_TIM_ACKINT(freerun->tch, GTIM_SR_UIF); - return OK; -} - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: stm32_freerun_initialize - * - * Description: - * Initialize the freerun timer wrapper - * - * Input Parameters: - * freerun Caller allocated instance of the freerun state structure - * chan Timer counter channel to be used. - * resolution The required resolution of the timer in units of - * microseconds. NOTE that the range is restricted to the - * range of uint16_t (excluding zero). - * - * Returned Value: - * Zero (OK) is returned on success; a negated errno value is returned - * on failure. - * - ****************************************************************************/ - -int stm32_freerun_initialize(struct stm32_freerun_s *freerun, - int chan, - uint16_t resolution) -{ - uint32_t frequency; - - tmrinfo("chan=%d resolution=%d usec\n", chan, resolution); - DEBUGASSERT(freerun != NULL && resolution > 0); - - /* Get the TC frequency the corresponds to the requested resolution */ - - frequency = USEC_PER_SEC / (uint32_t)resolution; - freerun->frequency = frequency; - - freerun->tch = stm32_tim_init(chan); - if (!freerun->tch) - { - tmrerr("ERROR: Failed to allocate TIM%d\n", chan); - return -EBUSY; - } - - STM32_TIM_SETCLOCK(freerun->tch, frequency); - - /* Initialize the remaining fields in the state structure and return - * success. - */ - - freerun->chan = chan; - freerun->overflow = 0; - - /* Set up to receive the callback when the counter overflow occurs */ - - STM32_TIM_SETISR(freerun->tch, stm32_freerun_handler, freerun, 0); - - /* Set timer period */ - - STM32_TIM_SETPERIOD(freerun->tch, UINT32_MAX); - - /* Start the counter */ - - STM32_TIM_SETMODE(freerun->tch, STM32_TIM_MODE_UP); - STM32_TIM_ACKINT(freerun->tch, GTIM_SR_UIF); - STM32_TIM_ENABLEINT(freerun->tch, GTIM_DIER_UIE); - - return OK; -} - -/**************************************************************************** - * Name: stm32_freerun_counter - * - * Description: - * Read the counter register of the free-running timer. - * - * Input Parameters: - * freerun Caller allocated instance of the freerun state structure. This - * structure must have been previously initialized via a call to - * stm32_freerun_initialize(); - * ts The location in which to return the time from the free-running - * timer. - * - * Returned Value: - * Zero (OK) is returned on success; a negated errno value is returned - * on failure. - * - ****************************************************************************/ - -int stm32_freerun_counter(struct stm32_freerun_s *freerun, - struct timespec *ts) -{ - uint64_t usec; - uint32_t counter; - uint32_t verify; - uint32_t overflow; - uint32_t sec; - int pending; - irqstate_t flags; - - DEBUGASSERT(freerun && freerun->tch && ts); - - /* Temporarily disable the overflow counter. NOTE that we have to be - * careful here because stm32_tc_getpending() will reset the pending - * interrupt status. If we do not handle the overflow here then, it will - * be lost. - */ - - flags = enter_critical_section(); - - overflow = freerun->overflow; - counter = STM32_TIM_GETCOUNTER(freerun->tch); - pending = STM32_TIM_CHECKINT(freerun->tch, GTIM_SR_UIF); - verify = STM32_TIM_GETCOUNTER(freerun->tch); - - /* If an interrupt was pending before we re-enabled interrupts, - * then the overflow needs to be incremented. - */ - - if (pending) - { - STM32_TIM_ACKINT(freerun->tch, GTIM_SR_UIF); - - /* Increment the overflow count and use the value of the - * guaranteed to be AFTER the overflow occurred. - */ - - overflow++; - counter = verify; - - /* Update freerun overflow counter. */ - - freerun->overflow = overflow; - } - - leave_critical_section(flags); - - tmrinfo("counter=%lu (%lu) overflow=%lu, pending=%i\n", - (unsigned long)counter, (unsigned long)verify, - (unsigned long)overflow, pending); - tmrinfo("frequency=%u\n", freerun->frequency); - - /* Convert the whole thing to units of microseconds. - * - * frequency = ticks / second - * seconds = ticks * frequency - * usecs = (ticks * USEC_PER_SEC) / frequency; - */ - - usec = ((((uint64_t)overflow << 32) + (uint64_t)counter) * USEC_PER_SEC) / - freerun->frequency; - - /* And return the value of the timer */ - - sec = (uint32_t)(usec / USEC_PER_SEC); - ts->tv_sec = sec; - ts->tv_nsec = (usec - (sec * USEC_PER_SEC)) * NSEC_PER_USEC; - - tmrinfo("usec=%llu ts=(%jd, %ld)\n", - usec, (intmax_t)ts->tv_sec, ts->tv_nsec); - - return OK; -} - -/**************************************************************************** - * Name: stm32_freerun_uninitialize - * - * Description: - * Stop the free-running timer and release all resources that it uses. - * - * Input Parameters: - * freerun Caller allocated instance of the freerun state structure. This - * structure must have been previously initialized via a call to - * stm32_freerun_initialize(); - * - * Returned Value: - * Zero (OK) is returned on success; a negated errno value is returned - * on failure. - * - ****************************************************************************/ - -int stm32_freerun_uninitialize(struct stm32_freerun_s *freerun) -{ - DEBUGASSERT(freerun && freerun->tch); - - /* Now we can disable the timer interrupt and disable the timer. */ - - STM32_TIM_DISABLEINT(freerun->tch, GTIM_DIER_UIE); - STM32_TIM_SETMODE(freerun->tch, STM32_TIM_MODE_DISABLED); - STM32_TIM_SETISR(freerun->tch, NULL, NULL, 0); - - /* Free the timer */ - - stm32_tim_deinit(freerun->tch); - freerun->tch = NULL; - - return OK; -} - -#endif /* CONFIG_STM32_FREERUN */ diff --git a/arch/arm/src/stm32l4/stm32l4_freerun.h b/arch/arm/src/stm32l4/stm32l4_freerun.h deleted file mode 100644 index 74fbb860d28..00000000000 --- a/arch/arm/src/stm32l4/stm32l4_freerun.h +++ /dev/null @@ -1,144 +0,0 @@ -/**************************************************************************** - * arch/arm/src/stm32l4/stm32l4_freerun.h - * - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. The - * ASF licenses this file to you 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. - * - ****************************************************************************/ - -#ifndef __ARCH_ARM_SRC_STM32L4_STM32L4_FREERUN_H -#define __ARCH_ARM_SRC_STM32L4_STM32L4_FREERUN_H - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include <nuttx/config.h> - -#include <stdint.h> -#include <time.h> -#include <nuttx/debug.h> - -#include "stm32_tim.h" - -#ifdef CONFIG_STM32_FREERUN - -/**************************************************************************** - * Public Types - ****************************************************************************/ - -/* The freerun client must allocate an instance of this structure and called - * stm32_freerun_initialize() before using the freerun facilities. The - * client should not access the contents of this structure directly since the - * contents are subject to change. - */ - -struct stm32_freerun_s -{ - uint8_t chan; /* The timer/counter in use */ - uint32_t overflow; /* Timer counter overflow */ - struct stm32_tim_dev_s *tch; /* Handle returned by stm32_tim_init() */ - uint32_t frequency; -}; - -/**************************************************************************** - * Public Data - ****************************************************************************/ - -#undef EXTERN -#if defined(__cplusplus) -#define EXTERN extern "C" -extern "C" -{ -#else -#define EXTERN extern -#endif - -/**************************************************************************** - * Public Function Prototypes - ****************************************************************************/ - -/**************************************************************************** - * Name: stm32_freerun_initialize - * - * Description: - * Initialize the freerun timer wrapper - * - * Input Parameters: - * freerun Caller allocated instance of the freerun state structure - * chan Timer counter channel to be used. - * resolution The required resolution of the timer in units of - * microseconds. NOTE that the range is restricted to the - * range of uint16_t (excluding zero). - * - * Returned Value: - * Zero (OK) is returned on success; a negated errno value is returned - * on failure. - * - ****************************************************************************/ - -int stm32_freerun_initialize(struct stm32_freerun_s *freerun, int chan, - uint16_t resolution); - -/**************************************************************************** - * Name: stm32_freerun_counter - * - * Description: - * Read the counter register of the free-running timer. - * - * Input Parameters: - * freerun Caller allocated instance of the freerun state structure. This - * structure must have been previously initialized via a call to - * stm32_freerun_initialize(); - * ts The location in which to return the time remaining on the - * oneshot timer. - * - * Returned Value: - * Zero (OK) is returned on success; a negated errno value is returned - * on failure. - * - ****************************************************************************/ - -int stm32_freerun_counter(struct stm32_freerun_s *freerun, - struct timespec *ts); - -/**************************************************************************** - * Name: stm32_freerun_uninitialize - * - * Description: - * Stop the free-running timer and release all resources that it uses. - * - * Input Parameters: - * freerun Caller allocated instance of the freerun state structure. This - * structure must have been previously initialized via a call to - * stm32_freerun_initialize(); - * - * Returned Value: - * Zero (OK) is returned on success; a negated errno value is returned - * on failure. - * - ****************************************************************************/ - -int stm32_freerun_uninitialize(struct stm32_freerun_s *freerun); - -#undef EXTERN -#ifdef __cplusplus -} -#endif - -#endif /* CONFIG_STM32_FREERUN */ -#endif /* __ARCH_ARM_SRC_STM32L4_STM32L4_FREERUN_H */ diff --git a/arch/arm/src/stm32l4/stm32l4_tickless.c b/arch/arm/src/stm32l4/stm32l4_tickless.c index 541399ba038..d89d00a753d 100644 --- a/arch/arm/src/stm32l4/stm32l4_tickless.c +++ b/arch/arm/src/stm32l4/stm32l4_tickless.c @@ -79,7 +79,7 @@ #include <nuttx/debug.h> #include "stm32l4_oneshot.h" -#include "stm32l4_freerun.h" +#include "stm32_freerun.h" #ifdef CONFIG_SCHED_TICKLESS diff --git a/arch/arm/src/stm32wb/CMakeLists.txt b/arch/arm/src/stm32wb/CMakeLists.txt index c64386700fb..51e1fa267f7 100644 --- a/arch/arm/src/stm32wb/CMakeLists.txt +++ b/arch/arm/src/stm32wb/CMakeLists.txt @@ -51,10 +51,6 @@ if(CONFIG_STM32_ONESHOT) list(APPEND SRCS stm32wb_oneshot.c stm32wb_oneshot_lowerhalf.c) endif() -if(CONFIG_STM32_FREERUN) - list(APPEND SRCS stm32wb_freerun.c) -endif() - if(CONFIG_BUILD_PROTECTED) list(APPEND SRCS stm32wb_userspace.c stm32wb_mpuinit.c) endif() diff --git a/arch/arm/src/stm32wb/Make.defs b/arch/arm/src/stm32wb/Make.defs index 705ae0dc030..369d25f058d 100644 --- a/arch/arm/src/stm32wb/Make.defs +++ b/arch/arm/src/stm32wb/Make.defs @@ -51,10 +51,6 @@ ifeq ($(CONFIG_STM32_ONESHOT),y) CHIP_CSRCS += stm32wb_oneshot.c stm32wb_oneshot_lowerhalf.c endif -ifeq ($(CONFIG_STM32_FREERUN),y) -CHIP_CSRCS += stm32wb_freerun.c -endif - ifeq ($(CONFIG_BUILD_PROTECTED),y) CHIP_CSRCS += stm32wb_userspace.c stm32wb_mpuinit.c endif diff --git a/arch/arm/src/stm32wb/stm32wb_freerun.c b/arch/arm/src/stm32wb/stm32wb_freerun.c deleted file mode 100644 index 9bea9f12e57..00000000000 --- a/arch/arm/src/stm32wb/stm32wb_freerun.c +++ /dev/null @@ -1,300 +0,0 @@ -/**************************************************************************** - * arch/arm/src/stm32wb/stm32wb_freerun.c - * - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. The - * ASF licenses this file to you 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. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include <nuttx/config.h> - -#include <sys/types.h> -#include <stdint.h> -#include <stdbool.h> -#include <assert.h> -#include <errno.h> - -#include <nuttx/irq.h> -#include <nuttx/clock.h> - -#include "stm32wb_freerun.h" - -#ifdef CONFIG_STM32_FREERUN - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: stm32_freerun_handler - * - * Description: - * Timer interrupt callback. When the freerun timer counter overflows, - * this interrupt will occur. We will just increment an overflow count. - * - * Input Parameters: - * tch - The handle that represents the timer state - * arg - An opaque argument provided when the interrupt was registered - * sr - The value of the timer interrupt status register at the time - * that the interrupt occurred. - * - * Returned Value: - * None - * - ****************************************************************************/ - -#ifndef CONFIG_CLOCK_TIMEKEEPING -static int stm32_freerun_handler(int irq, void *context, void *arg) -{ - struct stm32_freerun_s *freerun = (struct stm32_freerun_s *)arg; - - DEBUGASSERT(freerun != NULL && freerun->overflow < UINT32_MAX); - freerun->overflow++; - - STM32_TIM_ACKINT(freerun->tch, GTIM_SR_UIF); - return OK; -} -#endif /* CONFIG_CLOCK_TIMEKEEPING */ - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: stm32_freerun_initialize - * - * Description: - * Initialize the freerun timer wrapper - * - * Input Parameters: - * freerun Caller allocated instance of the freerun state structure - * chan Timer counter channel to be used. - * resolution The required resolution of the timer in units of - * microseconds. NOTE that the range is restricted to the - * range of uint16_t (excluding zero). - * - * Returned Value: - * Zero (OK) is returned on success; a negated errno value is returned - * on failure. - * - ****************************************************************************/ - -int stm32_freerun_initialize(struct stm32_freerun_s *freerun, int chan, - uint16_t resolution) -{ - uint32_t frequency; - - tmrinfo("chan=%d resolution=%d usec\n", chan, resolution); - DEBUGASSERT(freerun != NULL && resolution > 0); - - /* Get the TC frequency the corresponds to the requested resolution */ - - frequency = USEC_PER_SEC / (uint32_t)resolution; - freerun->frequency = frequency; - - freerun->tch = stm32_tim_init(chan); - if (!freerun->tch) - { - tmrerr("ERROR: Failed to allocate TIM%d\n", chan); - return -EBUSY; - } - - STM32_TIM_SETCLOCK(freerun->tch, frequency); - - /* Initialize the remaining fields in the state structure and return - * success. - */ - - freerun->chan = chan; - freerun->width = STM32_TIM_GETWIDTH(freerun->tch); - -#ifdef CONFIG_CLOCK_TIMEKEEPING - freerun->counter_mask = 0xffffffff; -#endif - -#ifndef CONFIG_CLOCK_TIMEKEEPING - freerun->overflow = 0; - - /* Set up to receive the callback when the counter overflow occurs */ - - STM32_TIM_SETISR(freerun->tch, stm32_freerun_handler, freerun, 0); -#endif - - /* Set timer period */ - - STM32_TIM_SETPERIOD(freerun->tch, - (uint32_t)((1ull << freerun->width) - 1)); - - /* Start the counter */ - - STM32_TIM_SETMODE(freerun->tch, STM32_TIM_MODE_UP); - -#ifndef CONFIG_CLOCK_TIMEKEEPING - STM32_TIM_ACKINT(freerun->tch, GTIM_SR_UIF); - STM32_TIM_ENABLEINT(freerun->tch, GTIM_DIER_UIE); -#endif - - return OK; -} - -/**************************************************************************** - * Name: stm32_freerun_counter - * - * Description: - * Read the counter register of the free-running timer. - * - * Input Parameters: - * freerun Caller allocated instance of the freerun state structure. This - * structure must have been previously initialized via a call to - * stm32_freerun_initialize(); - * ts The location in which to return the time from the free-running - * timer. - * - * Returned Value: - * Zero (OK) is returned on success; a negated errno value is returned - * on failure. - * - ****************************************************************************/ - -#ifndef CONFIG_CLOCK_TIMEKEEPING - -int stm32_freerun_counter(struct stm32_freerun_s *freerun, - struct timespec *ts) -{ - uint64_t usec; - uint32_t counter; - uint32_t verify; - uint32_t overflow; - uint32_t sec; - int pending; - irqstate_t flags; - - DEBUGASSERT(freerun && freerun->tch && ts); - - /* Temporarily disable the overflow counter. NOTE that we have to be - * careful here because stm32_tc_getpending() will reset the pending - * interrupt status. If we do not handle the overflow here then, it will - * be lost. - */ - - flags = enter_critical_section(); - - overflow = freerun->overflow; - counter = STM32_TIM_GETCOUNTER(freerun->tch); - pending = STM32_TIM_CHECKINT(freerun->tch, GTIM_SR_UIF); - verify = STM32_TIM_GETCOUNTER(freerun->tch); - - /* If an interrupt was pending before we re-enabled interrupts, - * then the overflow needs to be incremented. - */ - - if (pending) - { - STM32_TIM_ACKINT(freerun->tch, GTIM_SR_UIF); - - /* Increment the overflow count and use the value of the - * guaranteed to be AFTER the overflow occurred. - */ - - overflow++; - counter = verify; - - /* Update freerun overflow counter. */ - - freerun->overflow = overflow; - } - - leave_critical_section(flags); - - tmrinfo("counter=%" PRIu32 " (%" PRIu32 ") overflow=%" PRIu32 - ", pending=%i\n", - counter, verify, overflow, pending); - tmrinfo("frequency=%" PRIu32 "\n", freerun->frequency); - - /* Convert the whole thing to units of microseconds. - * - * frequency = ticks / second - * seconds = ticks * frequency - * usecs = (ticks * USEC_PER_SEC) / frequency; - */ - - usec = ((((uint64_t)overflow << freerun->width) + - (uint64_t)counter) * USEC_PER_SEC) / freerun->frequency; - - /* And return the value of the timer */ - - sec = (uint32_t)(usec / USEC_PER_SEC); - ts->tv_sec = sec; - ts->tv_nsec = (usec - (sec * USEC_PER_SEC)) * NSEC_PER_USEC; - - tmrinfo("usec=%llu ts=(%jd, %ld)\n", - usec, (intmax_t)ts->tv_sec, ts->tv_nsec); - - return OK; -} - -#else /* CONFIG_CLOCK_TIMEKEEPING */ - -int stm32_freerun_counter(struct stm32_freerun_s *freerun, - uint64_t *counter) -{ - *counter = STM32_TIM_GETCOUNTER(freerun->tch); - return OK; -} - -#endif /* CONFIG_CLOCK_TIMEKEEPING */ - -/**************************************************************************** - * Name: stm32_freerun_uninitialize - * - * Description: - * Stop the free-running timer and release all resources that it uses. - * - * Input Parameters: - * freerun Caller allocated instance of the freerun state structure. This - * structure must have been previously initialized via a call to - * stm32_freerun_initialize(); - * - * Returned Value: - * Zero (OK) is returned on success; a negated errno value is returned - * on failure. - * - ****************************************************************************/ - -int stm32_freerun_uninitialize(struct stm32_freerun_s *freerun) -{ - DEBUGASSERT(freerun && freerun->tch); - - /* Now we can disable the timer interrupt and disable the timer. */ - - STM32_TIM_DISABLEINT(freerun->tch, GTIM_DIER_UIE); - STM32_TIM_SETMODE(freerun->tch, STM32_TIM_MODE_DISABLED); - STM32_TIM_SETISR(freerun->tch, NULL, NULL, 0); - - /* Free the timer */ - - stm32_tim_deinit(freerun->tch); - freerun->tch = NULL; - - return OK; -} - -#endif /* CONFIG_STM32_FREERUN */ diff --git a/arch/arm/src/stm32wb/stm32wb_freerun.h b/arch/arm/src/stm32wb/stm32wb_freerun.h deleted file mode 100644 index b0a3186ed8f..00000000000 --- a/arch/arm/src/stm32wb/stm32wb_freerun.h +++ /dev/null @@ -1,162 +0,0 @@ -/**************************************************************************** - * arch/arm/src/stm32wb/stm32wb_freerun.h - * - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. The - * ASF licenses this file to you 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. - * - ****************************************************************************/ - -#ifndef __ARCH_ARM_SRC_STM32WB_STM32WB_FREERUN_H -#define __ARCH_ARM_SRC_STM32WB_STM32WB_FREERUN_H - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include <nuttx/config.h> - -#include <stdint.h> -#include <time.h> -#include <nuttx/debug.h> - -#include "stm32_tim.h" - -#ifdef CONFIG_STM32_FREERUN - -/**************************************************************************** - * Public Types - ****************************************************************************/ - -/* The freerun client must allocate an instance of this structure and called - * stm32_freerun_initialize() before using the freerun facilities. The - * client should not access the contents of this structure directly since - * the contents are subject to change. - */ - -struct stm32_freerun_s -{ - uint8_t chan; /* The timer/counter in use */ - uint8_t width; /* Width of timer (16- or 32-bits) */ - struct stm32_tim_dev_s *tch; /* Pointer returned by - * stm32_tim_init() */ - uint32_t frequency; - -#ifndef CONFIG_CLOCK_TIMEKEEPING - uint32_t overflow; /* Timer counter overflow */ -#endif - -#ifdef CONFIG_CLOCK_TIMEKEEPING - uint64_t counter_mask; -#endif -}; - -/**************************************************************************** - * Public Data - ****************************************************************************/ - -#undef EXTERN -#if defined(__cplusplus) -#define EXTERN extern "C" -extern "C" -{ -#else -#define EXTERN extern -#endif - -/**************************************************************************** - * Public Function Prototypes - ****************************************************************************/ - -/**************************************************************************** - * Name: stm32_freerun_initialize - * - * Description: - * Initialize the freerun timer wrapper - * - * Input Parameters: - * freerun Caller allocated instance of the freerun state structure - * chan Timer counter channel to be used. - * resolution The required resolution of the timer in units of - * microseconds. NOTE that the range is restricted to the - * range of uint16_t (excluding zero). - * - * Returned Value: - * Zero (OK) is returned on success; a negated errno value is returned - * on failure. - * - ****************************************************************************/ - -int stm32_freerun_initialize(struct stm32_freerun_s *freerun, int chan, - uint16_t resolution); - -/**************************************************************************** - * Name: stm32_freerun_counter - * - * Description: - * Read the counter register of the free-running timer. - * - * Input Parameters: - * freerun Caller allocated instance of the freerun state structure. This - * structure must have been previously initialized via a call to - * stm32_freerun_initialize(); - * ts The location in which to return the time remaining on the - * oneshot timer. - * - * Returned Value: - * Zero (OK) is returned on success; a negated errno value is returned - * on failure. - * - ****************************************************************************/ - -#ifndef CONFIG_CLOCK_TIMEKEEPING - -int stm32_freerun_counter(struct stm32_freerun_s *freerun, - struct timespec *ts); - -#else /* CONFIG_CLOCK_TIMEKEEPING */ - -int stm32_freerun_counter(struct stm32_freerun_s *freerun, - uint64_t *counter); - -#endif /* CONFIG_CLOCK_TIMEKEEPING */ - -/**************************************************************************** - * Name: stm32_freerun_uninitialize - * - * Description: - * Stop the free-running timer and release all resources that it uses. - * - * Input Parameters: - * freerun Caller allocated instance of the freerun state structure. This - * structure must have been previously initialized via a call to - * stm32_freerun_initialize(); - * - * Returned Value: - * Zero (OK) is returned on success; a negated errno value is returned - * on failure. - * - ****************************************************************************/ - -int stm32_freerun_uninitialize(struct stm32_freerun_s *freerun); - -#undef EXTERN -#ifdef __cplusplus -} -#endif - -#endif /* CONFIG_STM32_FREERUN */ -#endif /* __ARCH_ARM_SRC_STM32WB_STM32WB_FREERUN_H */
