Skip to content

Commit

Permalink
STM32: fix HRTIM pwm init with default values
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardocavagnis committed Sep 27, 2023
1 parent e7c36e9 commit 8dab782
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions targets/TARGET_STM/pwmout_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,23 @@ static void _pwmout_init_direct(pwmout_t *obj, const PinMap *pinmap)
pin_function(pinmap->pin, pinmap->function);
pin_mode(pinmap->pin, PullNone);

obj->period = 0;
obj->pulse = 0;
obj->prescaler = 0;
// Initialize obj with default values (period 550Hz, duty 0%)
uint32_t frequency;
uint32_t clocksource = __HAL_RCC_GET_HRTIM1_SOURCE();
switch (clocksource) {
case RCC_HRTIM1CLK_TIMCLK:
frequency = HAL_RCC_GetHCLKFreq();
break;
case RCC_HRTIM1CLK_CPUCLK:
frequency = HAL_RCC_GetSysClockFreq();
break;
}
obj->period = 18000 * (frequency / 1000000) / 4;
if (obj->period > 0xFFDFU) {
obj->period = 0xFFDFU;
}
obj->pulse = (uint32_t)((float)obj->period * 1.0 + 0.5);
obj->prescaler = HRTIM_PRESCALERRATIO_DIV4;

// Initialize the HRTIM structure
HrtimHandle.Instance = HRTIM1;
Expand Down

0 comments on commit 8dab782

Please sign in to comment.