转载请注明:https://mp.csdn.net/postedit/84571003
经过一天的实践,将基于标准库+RT-Thread的pwm驱动写完并通过示波器验证,此驱动目前不支持TIM1和TIM8,其他的定时器通道可以在rtconfig.h文件中配置,然后再在驱动文件中加上配置,此驱动目前只实现了TIM3的四个通道
typedef struct
{
TIM_TypeDef *Instance; /*!< Register base address */
TIM_TimeBaseInitTypeDef Init; /*!< TIM Time Base required parameters */
uint16_t Channel; /*!< Active channel */
}TIM_HandleTypeDef;
static rt_err_t drv_pwm_control(struct rt_device_pwm *device, int cmd, void *arg);
static struct rt_pwm_ops drv_ops =
{
drv_pwm_control
};
static rt_err_t drv_pwm_enable(TIM_TypeDef *htim, struct rt_pwm_configuration *configuration, rt_bool_t enable)
{
if((htim == TIM1) || (htim == TIM8))
{
rt_kprintf("The TIMX error\n");
return RT_ERROR;
}
if((configuration->channel != TIM_Channel_1) && (configuration->channel != TIM_Channel_2) &&
(configuration->channel != TIM_Channel_3) && (configuration->channel != TIM_Channel_4))
{
rt_kprintf("The TIMX Chx %d error\n",configuration->channel);
return RT_ERROR;
}
if(enable)
{