HF Interface Wrapper 0.1.0-dev
Embedded C++ hardware abstraction layer
Loading...
Searching...
No Matches
StmPeriodicTimer Class Reference

STM32 Periodic Timer implementation. More...

#include <StmPeriodicTimer.h>

Inheritance diagram for StmPeriodicTimer:
[legend]
Collaboration diagram for StmPeriodicTimer:
[legend]

Classes

struct  DispatchEntry
 

Public Member Functions

 StmPeriodicTimer (TIM_HandleTypeDef *htim, hf_u32_t timer_clock_hz, hf_timer_callback_t callback=nullptr, void *user_data=nullptr) noexcept
 Construct from CubeMX timer handle.
 
 StmPeriodicTimer (const hf_stm32_timer_config_t &config, hf_timer_callback_t callback=nullptr, void *user_data=nullptr) noexcept
 Construct from config struct.
 
 ~StmPeriodicTimer () noexcept override
 
 StmPeriodicTimer (const StmPeriodicTimer &)=delete
 
StmPeriodicTimeroperator= (const StmPeriodicTimer &)=delete
 
hf_timer_err_t Initialize () noexcept override
 Initialize the timer hardware/resources.
 
hf_timer_err_t Deinitialize () noexcept override
 Deinitialize the timer and free resources.
 
hf_timer_err_t Start (hf_u64_t period_us) noexcept override
 Start the periodic timer with specified period.
 
hf_timer_err_t Stop () noexcept override
 Stop the periodic timer.
 
hf_timer_err_t SetPeriod (hf_u64_t period_us) noexcept override
 Change the timer period while running.
 
hf_timer_err_t GetPeriod (hf_u64_t &period_us) noexcept override
 Get the current timer period.
 
hf_timer_err_t GetStats (hf_u64_t &callback_count, hf_u64_t &missed_callbacks, hf_timer_err_t &last_error) noexcept override
 Get timer statistics and status information.
 
hf_timer_err_t ResetStats () noexcept override
 Reset timer statistics.
 
const char * GetDescription () const noexcept override
 Get description of this timer implementation.
 
hf_u64_t GetMaxPeriod () const noexcept override
 Get minimum supported timer period.
 
hf_u64_t GetMinPeriod () const noexcept override
 Get maximum supported timer period.
 
hf_u64_t GetResolution () const noexcept override
 Get timer resolution.
 
hf_timer_err_t GetStatistics (hf_timer_statistics_t &statistics) const noexcept override
 Get timer operation statistics.
 
hf_timer_err_t GetDiagnostics (hf_timer_diagnostics_t &diagnostics) const noexcept override
 Get timer diagnostic information.
 
- Public Member Functions inherited from BasePeriodicTimer
virtual ~BasePeriodicTimer () noexcept=default
 Virtual destructor to ensure proper cleanup.
 
 BasePeriodicTimer (const BasePeriodicTimer &)=delete
 
BasePeriodicTimeroperator= (const BasePeriodicTimer &)=delete
 
bool IsInitialized () const noexcept
 Check if timer is initialized.
 
bool IsRunning () const noexcept
 Check if timer is currently running.
 
hf_timer_err_t SetCallback (hf_timer_callback_t callback, void *user_data=nullptr) noexcept
 Set new callback function.
 
void * GetUserData () const noexcept
 Get current user data pointer.
 
virtual hf_timer_err_t ResetStatistics () noexcept
 Reset timer operation statistics.
 
virtual hf_timer_err_t ResetDiagnostics () noexcept
 Reset timer diagnostic information.
 

Static Public Member Functions

static void IsrDispatch (TIM_HandleTypeDef *htim) noexcept
 Call from HAL_TIM_PeriodElapsedCallback to dispatch to instances.
 

Private Member Functions

bool ApplyPeriod (hf_u64_t period_us) noexcept
 Recalculate and apply PSC+ARR for the requested period.
 
void RegisterDispatch () noexcept
 
void UnregisterDispatch () noexcept
 

Private Attributes

TIM_HandleTypeDef * htim_
 HAL timer handle.
 
hf_u32_t timer_clock_hz_
 Input clock to the timer.
 
hf_u64_t period_us_
 Current period in µs.
 
hf_timer_stats_t stats_
 Internal stats.
 

Static Private Attributes

static constexpr int kMaxTimerInstances = 8
 
static DispatchEntry s_dispatch_ [kMaxTimerInstances] {}
 
static int s_dispatch_count_ = 0
 

Additional Inherited Members

- Protected Member Functions inherited from BasePeriodicTimer
 BasePeriodicTimer (hf_timer_callback_t callback, void *user_data=nullptr) noexcept
 Protected constructor with callback specification.
 
void SetInitialized (bool initialized) noexcept
 Set the initialized state.
 
void SetRunning (bool running) noexcept
 Set the running state.
 
void ExecuteCallback () noexcept
 Execute the timer callback (called by implementations).
 
bool HasValidCallback () const noexcept
 Check if callback is valid.
 
- Protected Attributes inherited from BasePeriodicTimer
hf_timer_callback_t callback_
 Timer callback function.
 
void * user_data_
 User data passed to callback.
 
bool initialized_
 Initialization state flag.
 
bool running_
 Running state flag.
 
hf_timer_statistics_t statistics_
 Timer operation statistics.
 
hf_timer_diagnostics_t diagnostics_
 Timer diagnostic information.
 

Detailed Description

STM32 Periodic Timer implementation.

Design:

  • Accepts a TIM_HandleTypeDef* already initialised by CubeMX.
  • Uses HAL_TIM_Base_Start_IT / Stop_IT.
  • Provides a static dispatch table so the HAL_TIM_PeriodElapsedCallback (extern "C") can route to the correct C++ instance.
  • Period changes recalculate ARR+PSC from the timer's input clock.

Constructor & Destructor Documentation

◆ StmPeriodicTimer() [1/3]

StmPeriodicTimer::StmPeriodicTimer ( TIM_HandleTypeDef * htim,
hf_u32_t timer_clock_hz,
hf_timer_callback_t callback = nullptr,
void * user_data = nullptr )
explicitnoexcept

Construct from CubeMX timer handle.

Parameters
htimPointer to HAL timer handle (CubeMX‑initialised).
timer_clock_hzAPB timer clock feeding this TIM (e.g. 84 MHz).
callbackUser callback (may also be set later via SetCallback).
user_dataOpaque pointer forwarded to callback.

◆ StmPeriodicTimer() [2/3]

StmPeriodicTimer::StmPeriodicTimer ( const hf_stm32_timer_config_t & config,
hf_timer_callback_t callback = nullptr,
void * user_data = nullptr )
explicitnoexcept

Construct from config struct.

◆ ~StmPeriodicTimer()

StmPeriodicTimer::~StmPeriodicTimer ( )
overridenoexcept

◆ StmPeriodicTimer() [3/3]

StmPeriodicTimer::StmPeriodicTimer ( const StmPeriodicTimer & )
delete

Member Function Documentation

◆ ApplyPeriod()

bool StmPeriodicTimer::ApplyPeriod ( hf_u64_t period_us)
privatenoexcept

Recalculate and apply PSC+ARR for the requested period.

◆ Deinitialize()

hf_timer_err_t StmPeriodicTimer::Deinitialize ( )
overridevirtualnoexcept

Deinitialize the timer and free resources.

Returns
hf_timer_err_t::TIMER_SUCCESS if successful, error code otherwise

Implements BasePeriodicTimer.

◆ GetDescription()

const char * StmPeriodicTimer::GetDescription ( ) const
overridevirtualnoexcept

Get description of this timer implementation.

Returns
Description string

Implements BasePeriodicTimer.

◆ GetDiagnostics()

hf_timer_err_t StmPeriodicTimer::GetDiagnostics ( hf_timer_diagnostics_t & diagnostics) const
overridevirtualnoexcept

Get timer diagnostic information.

Parameters
diagnosticsReference to store diagnostics data
Returns
hf_timer_err_t::TIMER_SUCCESS if successful, TIMER_ERR_NOT_SUPPORTED if not implemented

Reimplemented from BasePeriodicTimer.

◆ GetMaxPeriod()

hf_u64_t StmPeriodicTimer::GetMaxPeriod ( ) const
overridevirtualnoexcept

Get minimum supported timer period.

Returns
Minimum period in microseconds

Implements BasePeriodicTimer.

◆ GetMinPeriod()

hf_u64_t StmPeriodicTimer::GetMinPeriod ( ) const
overridevirtualnoexcept

Get maximum supported timer period.

Returns
Maximum period in microseconds

Implements BasePeriodicTimer.

◆ GetPeriod()

hf_timer_err_t StmPeriodicTimer::GetPeriod ( hf_u64_t & period_us)
overridevirtualnoexcept

Get the current timer period.

Parameters
period_usReference to store the current period
Returns
hf_timer_err_t::TIMER_SUCCESS if successful, error code otherwise

Implements BasePeriodicTimer.

◆ GetResolution()

hf_u64_t StmPeriodicTimer::GetResolution ( ) const
overridevirtualnoexcept

Get timer resolution.

Returns
Timer resolution in microseconds

Implements BasePeriodicTimer.

◆ GetStatistics()

hf_timer_err_t StmPeriodicTimer::GetStatistics ( hf_timer_statistics_t & statistics) const
overridevirtualnoexcept

Get timer operation statistics.

Parameters
statisticsReference to store statistics data
Returns
hf_timer_err_t::TIMER_SUCCESS if successful, TIMER_ERR_NOT_SUPPORTED if not implemented

Reimplemented from BasePeriodicTimer.

◆ GetStats()

hf_timer_err_t StmPeriodicTimer::GetStats ( hf_u64_t & callback_count,
hf_u64_t & missed_callbacks,
hf_timer_err_t & last_error )
overridevirtualnoexcept

Get timer statistics and status information.

Parameters
callback_countNumber of callbacks executed
missed_callbacksNumber of missed callbacks (if supported)
last_errorLast error that occurred
Returns
hf_timer_err_t::TIMER_SUCCESS if successful, error code otherwise

Implements BasePeriodicTimer.

◆ Initialize()

hf_timer_err_t StmPeriodicTimer::Initialize ( )
overridevirtualnoexcept

Initialize the timer hardware/resources.

Returns
hf_timer_err_t::TIMER_SUCCESS if successful, error code otherwise

Implements BasePeriodicTimer.

◆ IsrDispatch()

void StmPeriodicTimer::IsrDispatch ( TIM_HandleTypeDef * htim)
staticnoexcept

Call from HAL_TIM_PeriodElapsedCallback to dispatch to instances.

Usage in user code:

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim) {
}
static void IsrDispatch(TIM_HandleTypeDef *htim) noexcept
Call from HAL_TIM_PeriodElapsedCallback to dispatch to instances.
Definition StmPeriodicTimer.cpp:282

◆ operator=()

StmPeriodicTimer & StmPeriodicTimer::operator= ( const StmPeriodicTimer & )
delete

◆ RegisterDispatch()

void StmPeriodicTimer::RegisterDispatch ( )
privatenoexcept

◆ ResetStats()

hf_timer_err_t StmPeriodicTimer::ResetStats ( )
overridevirtualnoexcept

Reset timer statistics.

Returns
hf_timer_err_t::TIMER_SUCCESS if successful, error code otherwise

Implements BasePeriodicTimer.

◆ SetPeriod()

hf_timer_err_t StmPeriodicTimer::SetPeriod ( hf_u64_t period_us)
overridevirtualnoexcept

Change the timer period while running.

Parameters
period_usNew timer period in microseconds
Returns
hf_timer_err_t::TIMER_SUCCESS if successful, error code otherwise

Implements BasePeriodicTimer.

◆ Start()

hf_timer_err_t StmPeriodicTimer::Start ( hf_u64_t period_us)
overridevirtualnoexcept

Start the periodic timer with specified period.

Parameters
period_usTimer period in microseconds
Returns
hf_timer_err_t::TIMER_SUCCESS if successful, error code otherwise

Implements BasePeriodicTimer.

◆ Stop()

hf_timer_err_t StmPeriodicTimer::Stop ( )
overridevirtualnoexcept

Stop the periodic timer.

Returns
hf_timer_err_t::TIMER_SUCCESS if successful, error code otherwise

Implements BasePeriodicTimer.

◆ UnregisterDispatch()

void StmPeriodicTimer::UnregisterDispatch ( )
privatenoexcept

Member Data Documentation

◆ htim_

TIM_HandleTypeDef* StmPeriodicTimer::htim_
private

HAL timer handle.

◆ kMaxTimerInstances

constexpr int StmPeriodicTimer::kMaxTimerInstances = 8
staticconstexprprivate

◆ period_us_

hf_u64_t StmPeriodicTimer::period_us_
private

Current period in µs.

◆ s_dispatch_

StmPeriodicTimer::DispatchEntry StmPeriodicTimer::s_dispatch_ {}
staticprivate

◆ s_dispatch_count_

int StmPeriodicTimer::s_dispatch_count_ = 0
staticprivate

◆ stats_

hf_timer_stats_t StmPeriodicTimer::stats_
private

Internal stats.

◆ timer_clock_hz_

hf_u32_t StmPeriodicTimer::timer_clock_hz_
private

Input clock to the timer.


The documentation for this class was generated from the following files: