From b2bc0dcd16f1b51b8d615a9961765f95b34c54ee Mon Sep 17 00:00:00 2001 From: BreederBai Date: Mon, 4 Jul 2022 10:40:42 +0800 Subject: [rt_drv_pwm]增加API:增加单独设置PWM频率和脉宽的函数 (#6130) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [rt_drv_pwm]增加API:增加单独设置PWM频率和脉宽的函数 --- components/drivers/include/drivers/rt_drv_pwm.h | 4 +++ components/drivers/misc/rt_drv_pwm.c | 34 +++++++++++++++++++++++++ 2 files changed, 38 insertions(+) (limited to 'components') diff --git a/components/drivers/include/drivers/rt_drv_pwm.h b/components/drivers/include/drivers/rt_drv_pwm.h index 2570887d71..597842be79 100644 --- a/components/drivers/include/drivers/rt_drv_pwm.h +++ b/components/drivers/include/drivers/rt_drv_pwm.h @@ -19,6 +19,8 @@ #define PWM_CMD_GET (RT_DEVICE_CTRL_BASE(PWM) + 3) #define PWMN_CMD_ENABLE (RT_DEVICE_CTRL_BASE(PWM) + 4) #define PWMN_CMD_DISABLE (RT_DEVICE_CTRL_BASE(PWM) + 5) +#define PWM_CMD_SET_PERIOD (RT_DEVICE_CTRL_BASE(PWM) + 6) +#define PWM_CMD_SET_PULSE (RT_DEVICE_CTRL_BASE(PWM) + 7) struct rt_pwm_configuration { @@ -50,5 +52,7 @@ rt_err_t rt_device_pwm_register(struct rt_device_pwm *device, const char *name, rt_err_t rt_pwm_enable(struct rt_device_pwm *device, int channel); rt_err_t rt_pwm_disable(struct rt_device_pwm *device, int channel); rt_err_t rt_pwm_set(struct rt_device_pwm *device, int channel, rt_uint32_t period, rt_uint32_t pulse); +rt_err_t rt_pwm_set_period(struct rt_device_pwm *device, int channel, rt_uint32_t period); +rt_err_t rt_pwm_set_pulse(struct rt_device_pwm *device, int channel, rt_uint32_t pulse); #endif /* __DRV_PWM_H_INCLUDE__ */ diff --git a/components/drivers/misc/rt_drv_pwm.c b/components/drivers/misc/rt_drv_pwm.c index 64eb613a84..53aaad8223 100644 --- a/components/drivers/misc/rt_drv_pwm.c +++ b/components/drivers/misc/rt_drv_pwm.c @@ -177,6 +177,40 @@ rt_err_t rt_pwm_set(struct rt_device_pwm *device, int channel, rt_uint32_t perio return result; } +rt_err_t rt_pwm_set_period(struct rt_device_pwm *device, int channel, rt_uint32_t period) +{ + rt_err_t result = RT_EOK; + struct rt_pwm_configuration configuration = {0}; + + if (!device) + { + return -RT_EIO; + } + + configuration.channel = channel; + configuration.period = period; + result = rt_device_control(&device->parent, PWM_CMD_SET_PERIOD, &configuration); + + return result; +} + +rt_err_t rt_pwm_set_pulse(struct rt_device_pwm *device, int channel, rt_uint32_t pulse) +{ + rt_err_t result = RT_EOK; + struct rt_pwm_configuration configuration = {0}; + + if (!device) + { + return -RT_EIO; + } + + configuration.channel = channel; + configuration.pulse = pulse; + result = rt_device_control(&device->parent, PWM_CMD_SET_PULSE, &configuration); + + return result; +} + rt_err_t rt_pwm_get(struct rt_device_pwm *device, struct rt_pwm_configuration *cfg) { rt_err_t result = RT_EOK; -- cgit v1.3.1