From aed3cc162fa83971c9cd1afe42809746de4246b0 Mon Sep 17 00:00:00 2001 From: Trisuborn <553070703@qq.com> Date: Tue, 19 Jan 2021 17:14:32 +0800 Subject: 为stm32的PWM驱动添加对STM32H7的支持、添加互补PWM功能的支持,补全pwm_get命令 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bsp/stm32/libraries/HAL_Drivers/drv_pwm.c | 38 +++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 9 deletions(-) (limited to 'bsp') diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_pwm.c b/bsp/stm32/libraries/HAL_Drivers/drv_pwm.c index 64f283dabd..ab3897c569 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_pwm.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_pwm.c @@ -9,8 +9,10 @@ */ #include + #ifdef RT_USING_PWM #include "drv_config.h" +#include //#define DRV_DEBUG #define LOG_TAG "drv.pwm" @@ -167,13 +169,27 @@ static rt_err_t drv_pwm_enable(TIM_HandleTypeDef *htim, struct rt_pwm_configurat /* Converts the channel number to the channel number of Hal library */ rt_uint32_t channel = 0x04 * (configuration->channel - 1); - if (!enable) + if (!configuration->complementary) { - HAL_TIM_PWM_Stop(htim, channel); + if (!enable) + { + HAL_TIM_PWM_Stop(htim, channel); + } + else + { + HAL_TIM_PWM_Start(htim, channel); + } } - else + else if (configuration->complementary) { - HAL_TIM_PWM_Start(htim, channel); + if (!enable) + { + HAL_TIMEx_PWMN_Stop(htim, channel); + } + else + { + HAL_TIMEx_PWMN_Start(htim, channel); + } } return RT_EOK; @@ -187,21 +203,23 @@ static rt_err_t drv_pwm_get(TIM_HandleTypeDef *htim, struct rt_pwm_configuration #if defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) if (htim->Instance == TIM9 || htim->Instance == TIM10 || htim->Instance == TIM11) -#elif defined(SOC_SERIES_STM32L4) +#elif defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32H7) if (htim->Instance == TIM15 || htim->Instance == TIM16 || htim->Instance == TIM17) #elif defined(SOC_SERIES_STM32MP1) - if (htim->Instance == TIM4) + if (htim->Instance == TIM4) #elif defined(SOC_SERIES_STM32F1) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32G0) if (0) #endif { #if !defined(SOC_SERIES_STM32F0) && !defined(SOC_SERIES_STM32G0) tim_clock = HAL_RCC_GetPCLK2Freq() * 2; +#else + tim_clock = HAL_RCC_GetPCLK2Freq(); #endif } else { -#if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32G0) +#if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32H7) tim_clock = HAL_RCC_GetPCLK1Freq(); #else tim_clock = HAL_RCC_GetPCLK1Freq() * 2; @@ -234,7 +252,7 @@ static rt_err_t drv_pwm_set(TIM_HandleTypeDef *htim, struct rt_pwm_configuration #if defined(SOC_SERIES_STM32F2) || defined(SOC_SERIES_STM32F4) || defined(SOC_SERIES_STM32F7) if (htim->Instance == TIM9 || htim->Instance == TIM10 || htim->Instance == TIM11) -#elif defined(SOC_SERIES_STM32L4) +#elif defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32H7) if (htim->Instance == TIM15 || htim->Instance == TIM16 || htim->Instance == TIM17) #elif defined(SOC_SERIES_STM32MP1) if (htim->Instance == TIM4) @@ -244,11 +262,13 @@ static rt_err_t drv_pwm_set(TIM_HandleTypeDef *htim, struct rt_pwm_configuration { #if !defined(SOC_SERIES_STM32F0) && !defined(SOC_SERIES_STM32G0) tim_clock = HAL_RCC_GetPCLK2Freq() * 2; +#else + tim_clock = HAL_RCC_GetPCLK2Freq(); #endif } else { -#if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32G0) +#if defined(SOC_SERIES_STM32L4) || defined(SOC_SERIES_STM32F0) || defined(SOC_SERIES_STM32G0) || defined(SOC_SERIES_STM32H7) tim_clock = HAL_RCC_GetPCLK1Freq(); #else tim_clock = HAL_RCC_GetPCLK1Freq() * 2; -- cgit v1.3.1 From 63239801d9a2abe1b210faf6cd9f3b126924f7e0 Mon Sep 17 00:00:00 2001 From: Trisuborn <553070703@qq.com> Date: Wed, 20 Jan 2021 11:24:00 +0800 Subject: 修改无意义代码 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bsp/stm32/libraries/HAL_Drivers/drv_pwm.c | 4 ---- components/drivers/include/drivers/rt_drv_pwm.h | 6 +++--- 2 files changed, 3 insertions(+), 7 deletions(-) (limited to 'bsp') diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_pwm.c b/bsp/stm32/libraries/HAL_Drivers/drv_pwm.c index ab3897c569..38958dddd4 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_pwm.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_pwm.c @@ -213,8 +213,6 @@ static rt_err_t drv_pwm_get(TIM_HandleTypeDef *htim, struct rt_pwm_configuration { #if !defined(SOC_SERIES_STM32F0) && !defined(SOC_SERIES_STM32G0) tim_clock = HAL_RCC_GetPCLK2Freq() * 2; -#else - tim_clock = HAL_RCC_GetPCLK2Freq(); #endif } else @@ -262,8 +260,6 @@ static rt_err_t drv_pwm_set(TIM_HandleTypeDef *htim, struct rt_pwm_configuration { #if !defined(SOC_SERIES_STM32F0) && !defined(SOC_SERIES_STM32G0) tim_clock = HAL_RCC_GetPCLK2Freq() * 2; -#else - tim_clock = HAL_RCC_GetPCLK2Freq(); #endif } else diff --git a/components/drivers/include/drivers/rt_drv_pwm.h b/components/drivers/include/drivers/rt_drv_pwm.h index 4b436758d9..a4d64fbc12 100644 --- a/components/drivers/include/drivers/rt_drv_pwm.h +++ b/components/drivers/include/drivers/rt_drv_pwm.h @@ -21,9 +21,9 @@ struct rt_pwm_configuration { - rt_uint32_t channel; /* 0-n */ - rt_uint32_t period; /* unit:ns 1ns~4.29s:1Ghz~0.23hz */ - rt_uint32_t pulse; /* unit:ns (pulse<=period) */ + rt_uint32_t channel; /* 0-n */ + rt_uint32_t period; /* unit:ns 1ns~4.29s:1Ghz~0.23hz */ + rt_uint32_t pulse; /* unit:ns (pulse<=period) */ /* * RT_TRUE : The channel of pwm is complememtary. -- cgit v1.3.1 From 55402e19db41f6c8a706782572a177f61b4e93ef Mon Sep 17 00:00:00 2001 From: Trisuborn <553070703@qq.com> Date: Wed, 20 Jan 2021 23:47:18 +0800 Subject: 优化互补PWM输出功能的实现逻辑,兼容原本API,增加互补PWM使能/失能命令 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bsp/stm32/libraries/HAL_Drivers/drv_pwm.c | 4 +++ components/drivers/include/drivers/rt_drv_pwm.h | 6 +++-- components/drivers/misc/rt_drv_pwm.c | 34 +++++++++++++------------ 3 files changed, 26 insertions(+), 18 deletions(-) (limited to 'bsp') diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_pwm.c b/bsp/stm32/libraries/HAL_Drivers/drv_pwm.c index 38958dddd4..b6caafe05c 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_pwm.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_pwm.c @@ -309,8 +309,12 @@ static rt_err_t drv_pwm_control(struct rt_device_pwm *device, int cmd, void *arg switch (cmd) { + case PWMN_CMD_ENABLE: + configuration->complementary = RT_TRUE; case PWM_CMD_ENABLE: return drv_pwm_enable(htim, configuration, RT_TRUE); + case PWMN_CMD_DISABLE: + configuration->complementary = RT_FALSE; case PWM_CMD_DISABLE: return drv_pwm_enable(htim, configuration, RT_FALSE); case PWM_CMD_SET: diff --git a/components/drivers/include/drivers/rt_drv_pwm.h b/components/drivers/include/drivers/rt_drv_pwm.h index a4d64fbc12..59d670714d 100644 --- a/components/drivers/include/drivers/rt_drv_pwm.h +++ b/components/drivers/include/drivers/rt_drv_pwm.h @@ -18,6 +18,8 @@ #define PWM_CMD_DISABLE (128 + 1) #define PWM_CMD_SET (128 + 2) #define PWM_CMD_GET (128 + 3) +#define PWMN_CMD_ENABLE (128 + 4) +#define PWMN_CMD_DISABLE (128 + 5)/ struct rt_pwm_configuration { @@ -46,8 +48,8 @@ struct rt_device_pwm rt_err_t rt_device_pwm_register(struct rt_device_pwm *device, const char *name, const struct rt_pwm_ops *ops, const void *user_data); -rt_err_t rt_pwm_enable(struct rt_device_pwm *device, int channel, rt_uint8_t complementary); -rt_err_t rt_pwm_disable(struct rt_device_pwm *device, int channel, rt_uint8_t complementary); +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); #endif /* __DRV_PWM_H_INCLUDE__ */ diff --git a/components/drivers/misc/rt_drv_pwm.c b/components/drivers/misc/rt_drv_pwm.c index 03ef3353a3..572580c45c 100644 --- a/components/drivers/misc/rt_drv_pwm.c +++ b/components/drivers/misc/rt_drv_pwm.c @@ -126,7 +126,7 @@ rt_err_t rt_device_pwm_register(struct rt_device_pwm *device, const char *name, return result; } -rt_err_t rt_pwm_enable(struct rt_device_pwm *device, int channel, rt_uint8_t complementary) +rt_err_t rt_pwm_enable(struct rt_device_pwm *device, int channel) { rt_err_t result = RT_EOK; struct rt_pwm_configuration configuration = {0}; @@ -136,14 +136,14 @@ rt_err_t rt_pwm_enable(struct rt_device_pwm *device, int channel, rt_uint8_t com return -RT_EIO; } - configuration.channel = channel; - configuration.complementary = complementary ? (RT_TRUE) : (RT_FALSE); + configuration.channel = (channel > 0) ? (channel) : (-channel); /* Make it is positive num forever */ + configuration.complementary = (channel > 0) ? (RT_FALSE) : (RT_TRUE); /* If nagetive, it's complementary */ result = rt_device_control(&device->parent, PWM_CMD_ENABLE, &configuration); return result; } -rt_err_t rt_pwm_disable(struct rt_device_pwm *device, int channel, rt_uint8_t complementary) +rt_err_t rt_pwm_disable(struct rt_device_pwm *device, int channel) { rt_err_t result = RT_EOK; struct rt_pwm_configuration configuration = {0}; @@ -153,8 +153,8 @@ rt_err_t rt_pwm_disable(struct rt_device_pwm *device, int channel, rt_uint8_t co return -RT_EIO; } - configuration.channel = channel; - configuration.complementary = complementary ? (RT_TRUE) : (RT_FALSE); + configuration.channel = (channel > 0) ? (channel) : (-channel); /* Make it is positive num forever */ + configuration.complementary = (channel > 0) ? (RT_FALSE) : (RT_TRUE); /* If nagetive, it's complementary */ result = rt_device_control(&device->parent, PWM_CMD_DISABLE, &configuration); return result; @@ -204,10 +204,10 @@ static int pwm_enable(int argc, char **argv) int result = 0; struct rt_device_pwm *device = RT_NULL; - if (argc != 4) + if (argc != 3) { - rt_kprintf("Usage: pwm_enable pwm1 1 1\n"); - rt_kprintf(" pwm_enable \n"); + rt_kprintf("Usage: pwm_enable pwm1 1\n"); + rt_kprintf(" pwm_enable \n"); result = -RT_ERROR; goto _exit; } @@ -219,22 +219,23 @@ static int pwm_enable(int argc, char **argv) goto _exit; } - result = rt_pwm_enable(device, atoi(argv[2]), atoi(argv[3])); + /* If channel is complementary(1), make the channel number to nagetive */ + result = rt_pwm_enable(device, atoi(argv[2])); _exit: return result; } -MSH_CMD_EXPORT(pwm_enable, pwm_enable ); +MSH_CMD_EXPORT(pwm_enable, pwm_enable ); static int pwm_disable(int argc, char **argv) { int result = 0; struct rt_device_pwm *device = RT_NULL; - if (argc != 4) + if (argc != 3) { - rt_kprintf("Usage: pwm_enable pwm1 1 1\n"); - rt_kprintf(" pwm_disable \n"); + rt_kprintf("Usage: pwm_disable pwm1 1\n"); + rt_kprintf(" pwm_disable \n"); result = -RT_ERROR; goto _exit; } @@ -246,12 +247,13 @@ static int pwm_disable(int argc, char **argv) goto _exit; } - result = rt_pwm_disable(device, atoi(argv[2]), atoi(argv[3])); + /* If channel is complementary(1), make the channel number to nagetive */ + result = rt_pwm_disable(device, atoi(argv[2])); _exit: return result; } -MSH_CMD_EXPORT(pwm_disable, pwm_disable ); +MSH_CMD_EXPORT(pwm_disable, pwm_disable ); static int pwm_set(int argc, char **argv) { -- cgit v1.3.1 From 68e5c6f511859df77af5f32eed999e04525d3fa2 Mon Sep 17 00:00:00 2001 From: Trisuborn <553070703@qq.com> Date: Thu, 21 Jan 2021 06:37:42 +0800 Subject: Logical error --- bsp/stm32/libraries/HAL_Drivers/drv_pwm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bsp') diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_pwm.c b/bsp/stm32/libraries/HAL_Drivers/drv_pwm.c index b6caafe05c..56937a4818 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_pwm.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_pwm.c @@ -314,7 +314,7 @@ static rt_err_t drv_pwm_control(struct rt_device_pwm *device, int cmd, void *arg case PWM_CMD_ENABLE: return drv_pwm_enable(htim, configuration, RT_TRUE); case PWMN_CMD_DISABLE: - configuration->complementary = RT_FALSE; + configuration->complementary = RT_TRUE; case PWM_CMD_DISABLE: return drv_pwm_enable(htim, configuration, RT_FALSE); case PWM_CMD_SET: -- cgit v1.3.1