RMUL2025/hw/bsp/c-mini/drivers/bsp_pwm.c

68 lines
1.6 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "bsp_pwm.h"
#include "main.h"
extern TIM_HandleTypeDef htim3;
typedef struct {
TIM_HandleTypeDef *tim;
uint16_t channel;
} bsp_pwm_config_t;
static bsp_pwm_config_t bsp_pwm_map[BSP_PWM_NUMBER] = {
[BSP_PWM_SERVO_A] = {&htim3, TIM_CHANNEL_2},
[BSP_PWM_SERVO_B] = {&htim3, TIM_CHANNEL_1},
[BSP_PWM_SERVO_C] = {&htim3, TIM_CHANNEL_4},
[BSP_PWM_SERVO_D] = {&htim3, TIM_CHANNEL_3},
[BSP_PWM_IMU_HEAT] = {NULL, 0},
};
bsp_status_t bsp_pwm_start(bsp_pwm_channel_t ch) {
if (ch == BSP_PWM_IMU_HEAT) {
return BSP_ERR;
}
HAL_TIM_PWM_Start(bsp_pwm_map[ch].tim, bsp_pwm_map[ch].channel);
return BSP_OK;
}
bsp_status_t bsp_pwm_set_comp(bsp_pwm_channel_t ch, float duty_cycle) {
if (ch == BSP_PWM_IMU_HEAT) {
return BSP_ERR;
}
if (duty_cycle > 1.0f) {
return BSP_ERR;
}
if (duty_cycle < 0.0f) {
duty_cycle = 0.f;
}
/* 通过PWM通道对应定时器重载值和给定占空比计算PWM周期值 */
uint16_t pulse = (uint16_t)(duty_cycle * (float)__HAL_TIM_GET_AUTORELOAD(
bsp_pwm_map[ch].tim));
__HAL_TIM_SET_COMPARE(bsp_pwm_map[ch].tim, bsp_pwm_map[ch].channel, pulse);
return BSP_OK;
}
bsp_status_t bsp_pwm_set_freq(bsp_pwm_channel_t ch, float freq) {
uint16_t reload = (uint16_t)(1000000U / freq);
if (reload > 0) {
__HAL_TIM_PRESCALER(bsp_pwm_map[ch].tim, reload);
} else {
return BSP_ERR;
}
return BSP_OK;
}
bsp_status_t bsp_pwm_stop(bsp_pwm_channel_t ch) {
HAL_TIM_PWM_Stop(bsp_pwm_map[ch].tim, bsp_pwm_map[ch].channel);
return BSP_OK;
}