ports/stm32: Add timer helper functions.

Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
This commit is contained in:
iabdalkader 2025-07-08 19:38:52 +02:00
parent 07d3e3f559
commit cb0b0bc224
2 changed files with 160 additions and 0 deletions

123
ports/stm32/stm_pwm.c Normal file
View File

@ -0,0 +1,123 @@
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include STM32_HAL_H
#include "fmath.h"
#include "stm_pwm.h"
typedef struct _tim_info {
uint32_t period;
uint32_t pulse;
} tim_info_t;
static uint32_t stm_tim_get_source_clock(TIM_TypeDef *inst) {
uint32_t source = 0;
#if defined (STM32F4) || defined(STM32F7) || defined(STM32H7)
uintptr_t base = ((uintptr_t) inst) & 0xFFFF0000u;
#endif
#if defined (STM32F4) || defined(STM32F7)
// Timer clock on F4, F7, H7 == APBx * 2.
if (base == APB1PERIPH_BASE) {
source = HAL_RCC_GetPCLK1Freq() * 2;
} else if (base == APB2PERIPH_BASE) {
source = HAL_RCC_GetPCLK2Freq() * 2;
}
#elif defined(STM32H7)
// Timer clock on F4, F7, H7 == APBx * 2.
if (base == D2_APB1PERIPH_BASE) {
source = HAL_RCC_GetPCLK1Freq() * 2;
} else if (base == D2_APB2PERIPH_BASE) {
source = HAL_RCC_GetPCLK2Freq() * 2;
}
#elif defined(STM32N6)
source = HAL_RCC_GetSysClockFreq() >> LL_RCC_GetTIMPrescaler();
#endif
return source;
}
static void stm_tim_calc_period_pulse(TIM_TypeDef *inst, uint32_t frequency,
uint32_t *period, uint32_t *pulse) {
uint32_t tclk = stm_tim_get_source_clock(inst);
*period = fast_ceilf(tclk / ((float) frequency)) - 1;
*pulse = (*period + 1) / 2;
}
int stm_pwm_start(TIM_HandleTypeDef *tim, TIM_TypeDef *inst, uint32_t channel, uint32_t frequency) {
if (frequency == 0) {
// If frequency == 0, stop the timer.
stm_pwm_stop(tim, channel);
} else if (tim->Instance) {
// The timer has been initialized, update the frequency and return.
if (stm_pwm_set_frequency(tim, channel, frequency)) {
return -1;
}
} else {
// Otherwise, initialize timer and start it.
uint32_t period, pulse;
// Calculate period and pulse.
stm_tim_calc_period_pulse(inst, frequency, &period, &pulse);
// Timer base configuration
tim->Instance = inst;
tim->Init.Period = period;
tim->Init.Prescaler = 0;
tim->Init.CounterMode = TIM_COUNTERMODE_UP;
tim->Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
tim->Init.RepetitionCounter = 0;
tim->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
// Timer channel configuration
TIM_OC_InitTypeDef TIMOCHandle;
TIMOCHandle.Pulse = pulse;
TIMOCHandle.OCMode = TIM_OCMODE_PWM1;
TIMOCHandle.OCPolarity = TIM_OCPOLARITY_HIGH;
TIMOCHandle.OCNPolarity = TIM_OCNPOLARITY_HIGH;
TIMOCHandle.OCFastMode = TIM_OCFAST_DISABLE;
TIMOCHandle.OCIdleState = TIM_OCIDLESTATE_RESET;
TIMOCHandle.OCNIdleState = TIM_OCNIDLESTATE_RESET;
if (HAL_TIM_PWM_Init(tim) != HAL_OK ||
HAL_TIM_PWM_ConfigChannel(tim, &TIMOCHandle, channel) != HAL_OK ||
HAL_TIM_PWM_Start(tim, channel != HAL_OK)) {
return -1;
}
}
return 0;
}
int stm_pwm_stop(TIM_HandleTypeDef *tim, uint32_t channel) {
if (tim->Instance) {
HAL_TIM_PWM_Stop(tim, channel);
HAL_TIM_PWM_DeInit(tim);
memset(tim, 0, sizeof(TIM_HandleTypeDef));
}
return 0;
}
int stm_pwm_set_frequency(TIM_HandleTypeDef *tim, uint32_t channel, uint32_t frequency) {
uint32_t period, pulse;
if (tim->Instance) {
// Calculate period and pulse.
stm_tim_calc_period_pulse(tim->Instance, frequency, &period, &pulse);
__HAL_TIM_SET_AUTORELOAD(tim, period);
__HAL_TIM_SET_COMPARE(tim, channel, pulse);
}
return 0;
}
uint32_t stm_pwm_get_frequency(TIM_HandleTypeDef *tim, uint32_t channel) {
if (tim->Instance) {
uint32_t tclk = stm_tim_get_source_clock(tim->Instance);
return tclk / (tim->Init.Period + 1);
}
return 0;
}

37
ports/stm32/stm_pwm.h Normal file
View File

@ -0,0 +1,37 @@
/*
* SPDX-License-Identifier: MIT
*
* Copyright (C) 2023-2024 OpenMV, LLC.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* STM32 Timer helper functions.
*/
#ifndef __STM_TIM_H__
#define __STM_TIM_H__
#include <stdint.h>
#include <stdbool.h>
#include STM32_HAL_H
int stm_pwm_start(TIM_HandleTypeDef *tim, TIM_TypeDef *inst, uint32_t channel, uint32_t frequency);
int stm_pwm_stop(TIM_HandleTypeDef *tim, uint32_t channel);
uint32_t stm_pwm_get_frequency(TIM_HandleTypeDef *tim, uint32_t channel);
int stm_pwm_set_frequency(TIM_HandleTypeDef *tim, uint32_t channel, uint32_t frequency);
#endif // __STM_TIM_H__