增加了大量注释

This commit is contained in:
NeoZng
2023-01-01 17:32:22 +08:00
parent c2f8b5c8c3
commit c05513587c
56 changed files with 773 additions and 598 deletions

View File

@@ -13,9 +13,8 @@ void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
if (pwm_instance[i]->htim == htim && htim->Channel == pwm_instance[i]->channel)
{
if (pwm_instance[i]->callback) // 如果有回调函数
{
pwm_instance[i]->callback(pwm_instance[i]);
}
return; // 一次只能有一个通道的中断,所以直接返回
}
}
@@ -23,6 +22,9 @@ void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
PWMInstance *PWMRegister(PWM_Init_Config_s *config)
{
if (idx >= PWM_DEVICE_CNT) // 超过最大实例数,考虑增加或查看是否有内存泄漏
while (1)
;
PWMInstance *pwm = (PWMInstance *)malloc(sizeof(PWMInstance));
memset(pwm, 0, sizeof(PWMInstance));
@@ -33,8 +35,8 @@ PWMInstance *PWMRegister(PWM_Init_Config_s *config)
pwm->callback = config->callback;
pwm->id = config->id;
HAL_TIM_PWM_Start(pwm->htim, pwm->channel);
__HAL_TIM_SetCompare(pwm->htim, pwm->channel, pwm->pulse);
HAL_TIM_PWM_Start(pwm->htim, pwm->channel); // 启动PWM
__HAL_TIM_SetCompare(pwm->htim, pwm->channel, pwm->pulse); // 设置占空比,初始为0
pwm_instance[idx++] = pwm;
return pwm;

View File

@@ -4,7 +4,7 @@
#include "tim.h"
#include "stdint.h"
#define PWM_DEVICE_CNT 16 // PWM实例数量
#define PWM_DEVICE_CNT 16 // 最大支持的PWM实例数量
/* pwm实例结构体 */
typedef struct pwm_ins_temp
@@ -15,6 +15,9 @@ typedef struct pwm_ins_temp
uint32_t pulse; // 脉宽
void (*callback)(struct pwm_ins_temp *); // DMA传输完成回调函数
void *id; // 实例ID
// 后续还要添加更多的参数,以提供更直观的封装,比如直接按照百分比设置占空比,直接设置频率等
// ...
} PWMInstance;
typedef struct
@@ -49,6 +52,7 @@ void PWMStart(PWMInstance *pwm);
*/
void PWMStop(PWMInstance *pwm);
// @todo 这三个函数还需要进一步封装,协调好三者之间的关系
/**
* @brief 设置pwm脉宽
*
@@ -56,6 +60,8 @@ void PWMStop(PWMInstance *pwm);
* @param pulse 脉宽
*/
void PWMSetPulse(PWMInstance *pwm, uint32_t pulse);
void PWMSetPeriod(PWMInstance *pwm, uint32_t period); // 未实现
void PWMSetPrescaler(PWMInstance *pwm, uint32_t prescaler); // 未实现
/**
* @brief 启动pwm dma传输