Merge pull request #1845 from Guozhanxin/master
update PWM driver | 完善pwm驱动
This commit is contained in:
commit
3a6e67961b
@ -107,7 +107,7 @@ static rt_err_t drv_pwm_set(TIM_HandleTypeDef * htim, struct rt_pwm_configuratio
|
||||
period = MIN_PERIOD;
|
||||
}
|
||||
__HAL_TIM_SET_AUTORELOAD(htim, period - 1);
|
||||
pulse = configuration->pulse * tim_clock / psc / 1000UL;
|
||||
pulse = (unsigned long long)configuration->pulse * tim_clock / psc / 1000ULL;
|
||||
if (pulse < MIN_PULSE)
|
||||
{
|
||||
pulse = MIN_PULSE;
|
||||
@ -117,6 +117,7 @@ static rt_err_t drv_pwm_set(TIM_HandleTypeDef * htim, struct rt_pwm_configuratio
|
||||
pulse = period;
|
||||
}
|
||||
__HAL_TIM_SET_COMPARE(htim, channel, pulse - 1);
|
||||
__HAL_TIM_SET_COUNTER(htim, 0);
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
@ -494,100 +495,179 @@ static void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)
|
||||
GPIO_InitTypeDef GPIO_InitStruct;
|
||||
if (timHandle->Instance == TIM1)
|
||||
{
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
/**TIM1 GPIO Configuration
|
||||
PA8 ------> TIM1_CH1
|
||||
PA9 ------> TIM1_CH2
|
||||
PA10 ------> TIM1_CH3
|
||||
PA11 ------> TIM1_CH4
|
||||
*/
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11;
|
||||
#if defined(BSP_USING_PWM1_CH1) || defined(BSP_USING_PWM1_CH2) || defined(BSP_USING_PWM1_CH3) || defined(BSP_USING_PWM1_CH4)
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
|
||||
GPIO_InitStruct.Pin = 0x00;
|
||||
#ifdef BSP_USING_PWM1_CH1
|
||||
GPIO_InitStruct.Pin |= GPIO_PIN_8;
|
||||
#endif
|
||||
#ifdef BSP_USING_PWM1_CH2
|
||||
GPIO_InitStruct.Pin |= GPIO_PIN_9;
|
||||
#endif
|
||||
#ifdef BSP_USING_PWM1_CH3
|
||||
GPIO_InitStruct.Pin |= GPIO_PIN_10;
|
||||
#endif
|
||||
#ifdef BSP_USING_PWM1_CH4
|
||||
GPIO_InitStruct.Pin |= GPIO_PIN_11;
|
||||
#endif
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
#endif
|
||||
}
|
||||
else if (timHandle->Instance == TIM2)
|
||||
{
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
/**TIM2 GPIO Configuration
|
||||
PA3 ------> TIM2_CH4
|
||||
PA5 ------> TIM2_CH1
|
||||
PB10 ------> TIM2_CH3
|
||||
PB3 ------> TIM2_CH2
|
||||
*/
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_5;
|
||||
#if defined(BSP_USING_PWM2_CH1) || defined(BSP_USING_PWM2_CH4)
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
|
||||
GPIO_InitStruct.Pin = 0x00;
|
||||
#ifdef BSP_USING_PWM2_CH4
|
||||
GPIO_InitStruct.Pin |= GPIO_PIN_3;
|
||||
#endif
|
||||
#ifdef BSP_USING_PWM2_CH1
|
||||
GPIO_InitStruct.Pin |= GPIO_PIN_5;
|
||||
#endif
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
#endif
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_3;
|
||||
#if defined(BSP_USING_PWM2_CH2) || defined(BSP_USING_PWM2_CH3)
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
|
||||
GPIO_InitStruct.Pin = 0x00;
|
||||
#ifdef BSP_USING_PWM2_CH2
|
||||
GPIO_InitStruct.Pin |= GPIO_PIN_3;
|
||||
#endif
|
||||
#ifdef BSP_USING_PWM2_CH3
|
||||
GPIO_InitStruct.Pin |= GPIO_PIN_10;
|
||||
#endif
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF1_TIM2;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
#endif
|
||||
}
|
||||
else if (timHandle->Instance == TIM3)
|
||||
{
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
/**TIM3 GPIO Configuration
|
||||
PA6 ------> TIM3_CH1
|
||||
PA7 ------> TIM3_CH2
|
||||
PB0 ------> TIM3_CH3
|
||||
PB1 ------> TIM3_CH4
|
||||
*/
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
|
||||
#if defined(BSP_USING_PWM3_CH1) || defined(BSP_USING_PWM3_CH2)
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
|
||||
GPIO_InitStruct.Pin = 0x00;
|
||||
#ifdef BSP_USING_PWM3_CH1
|
||||
GPIO_InitStruct.Pin |= GPIO_PIN_6;
|
||||
#endif
|
||||
#ifdef BSP_USING_PWM3_CH2
|
||||
GPIO_InitStruct.Pin |= GPIO_PIN_7;
|
||||
#endif
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
GPIO_InitStruct.Pin = 0x00;
|
||||
#endif
|
||||
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
|
||||
#if defined(BSP_USING_PWM3_CH3) || defined(BSP_USING_PWM3_CH4)
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
|
||||
GPIO_InitStruct.Pin = 0x00;
|
||||
#ifdef BSP_USING_PWM3_CH3
|
||||
GPIO_InitStruct.Pin |= GPIO_PIN_0;
|
||||
#endif
|
||||
#ifdef BSP_USING_PWM3_CH4
|
||||
GPIO_InitStruct.Pin |= GPIO_PIN_1;
|
||||
#endif
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
#endif
|
||||
}
|
||||
else if (timHandle->Instance == TIM4)
|
||||
{
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
/**TIM4 GPIO Configuration
|
||||
PB6 ------> TIM4_CH1
|
||||
PB7 ------> TIM4_CH2
|
||||
PB8 ------> TIM4_CH3
|
||||
PB9 ------> TIM4_CH4
|
||||
*/
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
|
||||
#if defined(BSP_USING_PWM4_CH1) || defined(BSP_USING_PWM4_CH2) || defined(BSP_USING_PWM4_CH3) || defined(BSP_USING_PWM4_CH4)
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
|
||||
GPIO_InitStruct.Pin = 0x00;
|
||||
#ifdef BSP_USING_PWM4_CH1
|
||||
GPIO_InitStruct.Pin |= GPIO_PIN_6;
|
||||
#endif
|
||||
#ifdef BSP_USING_PWM4_CH2
|
||||
GPIO_InitStruct.Pin |= GPIO_PIN_7;
|
||||
#endif
|
||||
#ifdef BSP_USING_PWM4_CH3
|
||||
GPIO_InitStruct.Pin |= GPIO_PIN_8;
|
||||
#endif
|
||||
#ifdef BSP_USING_PWM4_CH4
|
||||
GPIO_InitStruct.Pin |= GPIO_PIN_9;
|
||||
#endif
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF2_TIM4;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
#endif
|
||||
}
|
||||
else if (timHandle->Instance == TIM5)
|
||||
{
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
/**TIM5 GPIO Configuration
|
||||
PA0-WKUP ------> TIM5_CH1
|
||||
PA1 ------> TIM5_CH2
|
||||
PA2 ------> TIM5_CH3
|
||||
*/
|
||||
#if defined(BSP_USING_PWM5_CH1) || defined(BSP_USING_PWM5_CH2) || defined(BSP_USING_PWM5_CH3)
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
|
||||
GPIO_InitStruct.Pin = 0x00;
|
||||
#ifdef BSP_USING_PWM5_CH1
|
||||
GPIO_InitStruct.Pin |= GPIO_PIN_0;
|
||||
#endif
|
||||
#ifdef BSP_USING_PWM5_CH2
|
||||
GPIO_InitStruct.Pin |= GPIO_PIN_1;
|
||||
#endif
|
||||
#ifdef BSP_USING_PWM5_CH3
|
||||
GPIO_InitStruct.Pin |= GPIO_PIN_2;
|
||||
#endif
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF2_TIM5;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef *tim_pwmHandle)
|
||||
|
@ -25,6 +25,9 @@
|
||||
#ifndef __DRV_PWM_H_INCLUDE__
|
||||
#define __DRV_PWM_H_INCLUDE__
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
#define PWM_CMD_ENABLE (128 + 0)
|
||||
#define PWM_CMD_DISABLE (128 + 1)
|
||||
#define PWM_CMD_SET (128 + 2)
|
||||
@ -49,6 +52,10 @@ struct rt_device_pwm
|
||||
const struct rt_pwm_ops *ops;
|
||||
};
|
||||
|
||||
extern 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_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_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__ */
|
||||
|
@ -24,9 +24,7 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtdevice.h>
|
||||
|
||||
#include <drivers/rt_drv_pwm.h>
|
||||
|
||||
static rt_err_t _pwm_control(rt_device_t dev, int cmd, void *args)
|
||||
{
|
||||
@ -99,7 +97,6 @@ static rt_size_t _pwm_write(rt_device_t dev, rt_off_t pos, const void *buffer, r
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return size;
|
||||
@ -128,10 +125,9 @@ rt_err_t rt_device_pwm_register(struct rt_device_pwm *device, const char *name,
|
||||
return result;
|
||||
}
|
||||
|
||||
rt_err_t rt_pwm_enable(int channel)
|
||||
rt_err_t rt_pwm_enable(struct rt_device_pwm *device, int channel)
|
||||
{
|
||||
rt_err_t result = RT_EOK;
|
||||
struct rt_device *device = rt_device_find("pwm");
|
||||
struct rt_pwm_configuration configuration = {0};
|
||||
|
||||
if (!device)
|
||||
@ -140,15 +136,30 @@ rt_err_t rt_pwm_enable(int channel)
|
||||
}
|
||||
|
||||
configuration.channel = channel;
|
||||
result = rt_device_control(device, PWM_CMD_ENABLE, &configuration);
|
||||
result = rt_device_control(&device->parent, PWM_CMD_ENABLE, &configuration);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
rt_err_t rt_pwm_set(int channel, rt_uint32_t period, rt_uint32_t pulse)
|
||||
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};
|
||||
|
||||
if (!device)
|
||||
{
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
configuration.channel = channel;
|
||||
result = rt_device_control(&device->parent, PWM_CMD_DISABLE, &configuration);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
rt_err_t rt_pwm_set(struct rt_device_pwm *device, int channel, rt_uint32_t period, rt_uint32_t pulse)
|
||||
{
|
||||
rt_err_t result = RT_EOK;
|
||||
struct rt_device *device = rt_device_find("pwm");
|
||||
struct rt_pwm_configuration configuration = {0};
|
||||
|
||||
if (!device)
|
||||
@ -159,12 +170,11 @@ rt_err_t rt_pwm_set(int channel, rt_uint32_t period, rt_uint32_t pulse)
|
||||
configuration.channel = channel;
|
||||
configuration.period = period;
|
||||
configuration.pulse = pulse;
|
||||
result = rt_device_control(device, PWM_CMD_SET, &configuration);
|
||||
result = rt_device_control(&device->parent, PWM_CMD_SET, &configuration);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
#ifdef RT_USING_FINSH
|
||||
#include <finsh.h>
|
||||
|
||||
@ -175,33 +185,75 @@ FINSH_FUNCTION_EXPORT_ALIAS(rt_pwm_set, pwm_set, set pwm.);
|
||||
static int pwm_enable(int argc, char **argv)
|
||||
{
|
||||
int result = 0;
|
||||
struct rt_device_pwm *device = RT_NULL;
|
||||
|
||||
if (argc != 2)
|
||||
if (argc != 3)
|
||||
{
|
||||
rt_kprintf("Usage: pwm_enable 1\n");
|
||||
rt_kprintf("Usage: pwm_enable pwm1 1\n");
|
||||
result = -RT_ERROR;
|
||||
goto _exit;
|
||||
}
|
||||
|
||||
result = rt_pwm_enable(atoi(argv[1]));
|
||||
device = (struct rt_device_pwm *)rt_device_find(argv[1]);
|
||||
if (!device)
|
||||
{
|
||||
result = -RT_EIO;
|
||||
goto _exit;
|
||||
}
|
||||
|
||||
result = rt_pwm_enable(device, atoi(argv[2]));
|
||||
|
||||
_exit:
|
||||
return result;
|
||||
}
|
||||
MSH_CMD_EXPORT(pwm_enable, pwm_enable 1);
|
||||
MSH_CMD_EXPORT(pwm_enable, pwm_enable pwm1 1);
|
||||
|
||||
static int pwm_set(int argc, char **argv)
|
||||
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_set 1 100 50\n");
|
||||
rt_kprintf("Usage: pwm_enable pwm1 1\n");
|
||||
result = -RT_ERROR;
|
||||
goto _exit;
|
||||
}
|
||||
|
||||
result = rt_pwm_set(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]));
|
||||
device = (struct rt_device_pwm *)rt_device_find(argv[1]);
|
||||
if (!device)
|
||||
{
|
||||
result = -RT_EIO;
|
||||
goto _exit;
|
||||
}
|
||||
|
||||
result = rt_pwm_disable(device, atoi(argv[2]));
|
||||
|
||||
_exit:
|
||||
return result;
|
||||
}
|
||||
MSH_CMD_EXPORT(pwm_disable, pwm_disable pwm1 1);
|
||||
|
||||
static int pwm_set(int argc, char **argv)
|
||||
{
|
||||
int result = 0;
|
||||
struct rt_device_pwm *device = RT_NULL;
|
||||
|
||||
if (argc != 5)
|
||||
{
|
||||
rt_kprintf("Usage: pwm_set pwm1 1 100 50\n");
|
||||
result = -RT_ERROR;
|
||||
goto _exit;
|
||||
}
|
||||
|
||||
device = (struct rt_device_pwm *)rt_device_find(argv[1]);
|
||||
if (!device)
|
||||
{
|
||||
result = -RT_EIO;
|
||||
goto _exit;
|
||||
}
|
||||
|
||||
result = rt_pwm_set(device, atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
|
||||
|
||||
_exit:
|
||||
return result;
|
||||
@ -209,6 +261,4 @@ _exit:
|
||||
MSH_CMD_EXPORT(pwm_set, pwm_set 1 100 50);
|
||||
|
||||
#endif /* FINSH_USING_MSH */
|
||||
|
||||
|
||||
#endif /* RT_USING_FINSH */
|
||||
|
Loading…
x
Reference in New Issue
Block a user