【简介】
PWM(Pulse Width Modulation , 脉冲宽度调制) ,在 Rt-thread 把PWM 设备抽象为PWM 设备驱动框架,设备驱动代码为components\drivers\misc\rt_drv_pwm.c ,应用层通过PWM设备驱动层的 rt_pwm_enable,rt_pwm_disable ,rt_pwm_set ,rt_pwm_set_period ,rt_pwm_set_pulse ,rt_pwm_set_dead_time ,rt_pwm_set_phase ,rt_pwm_get 接口调用底层提供的服务接口,rt_device_pwm_register 接口可以将PWM 设备注册到系统中。
rt-thread pwm 设备device 结构体,编写PWM设备驱动基于该结构体派生出新的PWM设备结构体。
struct rt_device_pwm; struct rt_pwm_ops { rt_err_t (*control)(struct rt_device_pwm *device, int cmd, void *arg); }; struct rt_device_pwm { struct rt_device parent; const struct rt_pwm_ops *ops; };
PWM 设备的配置通过以下结构体进行配置
struct rt_pwm_configuration { rt_uint32_t channel; /* 0 ~ n or 0 ~ -n, which depends on specific MCU requirements */ rt_uint32_t period; /* unit:ns 1ns~4.29s:1Ghz~0.23hz */ rt_uint32_t pulse; /* unit:ns (pulse<=period) */ rt_uint32_t dead_time; /* unit:ns */ rt_uint32_t phase; /*unit: degree, 0~360, which is the phase of pwm output, */ /* * RT_TRUE : The channel of pwm is complememtary. * RT_FALSE : The channel of pwm is nomal. */ rt_bool_t complementary; };
PWM 设备驱动的启用在env 的menuconfig 下开启
生成工程后,pwm 驱动框架的已经被加入工程
PWM 驱动适配底层和芯片的接口我们在Kconfig 中添加开启PWM驱动的配置开关
menuconfig BSP_USING_PWM bool "Enable PWM" default n select RT_USING_PWM
并在SConscript 文件中加入编译选项的配置,我们在drv_pwm 文件中适配PWM驱动
# add pwm driver if GetDepend('BSP_USING_PWM'): src += ['drv_pwm.c']
在menuconfig 中开启PWM 驱动