Skip to content

Add DeInit Function in hrtim backend and SPIN PwmHal#155

Open
EdamAbdelhedi wants to merge 3 commits into
owntech-foundation:mainfrom
EdamAbdelhedi:add_deinit_hrtim
Open

Add DeInit Function in hrtim backend and SPIN PwmHal#155
EdamAbdelhedi wants to merge 3 commits into
owntech-foundation:mainfrom
EdamAbdelhedi:add_deinit_hrtim

Conversation

@EdamAbdelhedi
Copy link
Copy Markdown
Contributor

Add HRTIM deinitialization API to allow PWM reconfiguration

Problem

The HRTIM peripheral can currently be initialized, but there is no public API to deinitialize it.

This creates a limitation for applications that need to change the PWM frequency range after a first initialization. In particular, the HRTIM prescaler is computed during initialization and cannot be safely changed while the peripheral is already configured and running.

As a result, once the HRTIM has been initialized, applications cannot fully reconfigure the PWM timing setup without resetting the board.

Why this is needed

Some applications need to switch between different PWM frequency ranges at runtime or between operating modes. These ranges may require a different HRTIM prescaler.

Today, functions such as frequency initialization are effectively one-shot because the peripheral and timing units remain marked as initialized. Even if outputs are stopped, the software state and hardware configuration do not return to a clean pre-initialization state.

Adding deinitialization functions allows applications to:

  • Safely stop HRTIM outputs and counters
  • Clear timing unit configuration
  • Reset the HRTIM peripheral state
  • Reinitialize PWM with a new frequency range
  • Recompute and apply a new prescaler
  • Avoid requiring a full MCU reset

Proposed API

Add a low-level HRTIM driver API:

void hrtim_tu_deinit(hrtim_tu_number_t tu_number);
void hrtim_deinit(void);

@Ayoub-Farah Ayoub-Farah self-requested a review May 18, 2026 12:12
@Ayoub-Farah
Copy link
Copy Markdown
Member

From our discussion, the goal of the global deinit is to allow a full HRTIM reinitialization so the prescaler can be computed again and the minimum frequency can be changed. For that, deinitializing only the timing units is not sufficient: the master timer must also be stopped/reset and its software state must be marked as UNIT_OFF. Otherwise, the next initialization will not call _init_master(), and the prescaler/min-frequency configuration will not be recomputed.

My suggestion

  • keep hrtim_tu_deinit() as a per-timing-unit cleanup API, for cases where we want to stop/free one PWM unit while keeping the HRTIM master configuration and minimum frequency unchanged.
  • use hrtim_deinit() as the global HRTIM deinit API. It should disable all outputs, deinitialize all timing units, reset/disable the master timer, and mark timerMaster.pwm_conf.unit_on = UNIT_OFF.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants