Skip to content

STM32: fix HRTIM pwm init and corner case #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 29, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 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 Expand Up @@ -444,6 +458,15 @@ float pwmout_read(pwmout_t *obj)
if (obj->period > 0) {
value = (float)(obj->pulse) / (float)(obj->period);
}

if (obj->pwm == PWM_I) {
if (value <= (float)0.0) {
value = 1.0;
} else if (value >= (float)1.0) {
value = 0.0;
}
}

return ((value > (float)1.0) ? (float)(1.0) : (value));
}

Expand Down