Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

提交stm32 bsp drv_pwm.c里面的一个小建议 #6517

Merged
merged 8 commits into from
Oct 28, 2022
Merged
6 changes: 3 additions & 3 deletions bsp/stm32/libraries/HAL_Drivers/config/f0/pwm_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ extern "C" {
#ifndef PWM2_CONFIG
#define PWM2_CONFIG \
{ \
.tim_handle.Instance = TIM2, \
.name = "pwm2", \
.channel = 0 \
.tim_handle.Instance = TIM2, \ /* TIM1/2/3... */
.name = "pwm2", \ /* your pwm device name */
.channel = 0 \ /* range: 1--4 */
}
#endif /* PWM2_CONFIG */
#endif /* BSP_USING_PWM2 */
Expand Down
6 changes: 3 additions & 3 deletions bsp/stm32/libraries/HAL_Drivers/config/f1/pwm_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ extern "C" {
#ifndef PWM1_CONFIG
#define PWM1_CONFIG \
{ \
.tim_handle.Instance = TIM1, \
.name = "pwm1", \
.channel = 0 \
.tim_handle.Instance = TIM1, \ /* TIM1/2/3... */
.name = "pwm1", \ /* your pwm device name */
.channel = 0 \ /* range: 1--4 */
}
#endif /* PWM1_CONFIG */
#endif /* BSP_USING_PWM1 */
Expand Down
6 changes: 3 additions & 3 deletions bsp/stm32/libraries/HAL_Drivers/config/f2/pwm_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ extern "C" {
#ifndef PWM2_CONFIG
#define PWM2_CONFIG \
{ \
.tim_handle.Instance = TIM2, \
.name = "pwm2", \
.channel = 0 \
.tim_handle.Instance = TIM2, \ /* TIM1/2/3... */
.name = "pwm2", \ /* your pwm device name */
.channel = 0 \ /* range: 1--4 */
}
#endif /* PWM2_CONFIG */
#endif /* BSP_USING_PWM2 */
Expand Down
6 changes: 3 additions & 3 deletions bsp/stm32/libraries/HAL_Drivers/config/f3/pwm_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ extern "C" {
#ifndef PWM1_CONFIG
#define PWM1_CONFIG \
{ \
.tim_handle.Instance = TIM1, \
.name = "pwm1", \
.channel = 0 \
.tim_handle.Instance = TIM1, \ /* TIM1/2/3... */
.name = "pwm1", \ /* your pwm device name */
.channel = 0 \ /* range: 1--4 */
}
#endif /* PWM1_CONFIG */
#endif /* BSP_USING_PWM1 */
Expand Down
6 changes: 3 additions & 3 deletions bsp/stm32/libraries/HAL_Drivers/config/f4/pwm_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ extern "C" {
#ifndef PWM1_CONFIG
#define PWM1_CONFIG \
{ \
.tim_handle.Instance = TIM1, \
.name = "pwm1", \
.channel = 0 \
.tim_handle.Instance = TIM1, \ /* TIM1/2/3... */
.name = "pwm1", \ /* your pwm device name */
.channel = 0 \ /* range: 1--4 */
}
#endif /* PWM1_CONFIG */
#endif /* BSP_USING_PWM1 */
Expand Down
6 changes: 3 additions & 3 deletions bsp/stm32/libraries/HAL_Drivers/config/f7/pwm_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ extern "C" {
#ifndef PWM2_CONFIG
#define PWM2_CONFIG \
{ \
.tim_handle.Instance = TIM2, \
.name = "pwm2", \
.channel = 0 \
.tim_handle.Instance = TIM2, \ /* TIM1/2/3... */
.name = "pwm2", \ /* your pwm device name */
.channel = 0 \ /* range: 1--4 */
}
#endif /* PWM2_CONFIG */
#endif /* BSP_USING_PWM2 */
Expand Down
6 changes: 3 additions & 3 deletions bsp/stm32/libraries/HAL_Drivers/config/g0/pwm_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ extern "C" {
#ifndef PWM2_CONFIG
#define PWM2_CONFIG \
{ \
.tim_handle.Instance = TIM2, \
.name = "pwm2", \
.channel = 0 \
.tim_handle.Instance = TIM2, \ /* TIM1/2/3... */
.name = "pwm2", \ /* your pwm device name */
.channel = 0 \ /* range: 1--4 */
}
#endif /* PWM2_CONFIG */
#endif /* BSP_USING_PWM2 */
Expand Down
6 changes: 3 additions & 3 deletions bsp/stm32/libraries/HAL_Drivers/config/g4/pwm_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ extern "C" {
#ifndef PWM2_CONFIG
#define PWM2_CONFIG \
{ \
.tim_handle.Instance = TIM2, \
.name = "pwm2", \
.channel = 0 \
.tim_handle.Instance = TIM2, \ /* TIM1/2/3... */
.name = "pwm2", \ /* your pwm device name */
.channel = 0 \ /* range: 1--4 */
}
#endif /* PWM2_CONFIG */
#endif /* BSP_USING_PWM2 */
Expand Down
6 changes: 3 additions & 3 deletions bsp/stm32/libraries/HAL_Drivers/config/h7/pwm_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ extern "C" {
#ifndef PWM1_CONFIG
#define PWM1_CONFIG \
{ \
.tim_handle.Instance = TIM1, \
.name = "pwm1", \
.channel = 0 \
.tim_handle.Instance = TIM1, \ /* TIM1/2/3... */
.name = "pwm1", \ /* your pwm device name */
.channel = 0 \ /* range: 1--4 */
}
#endif /* PWM1_CONFIG */
#endif /* BSP_USING_PWM1 */
Expand Down
6 changes: 3 additions & 3 deletions bsp/stm32/libraries/HAL_Drivers/config/l1/pwm_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ extern "C" {
#ifndef PWM2_CONFIG
#define PWM2_CONFIG \
{ \
.tim_handle.Instance = TIM2, \
.name = "pwm2", \
.channel = 0 \
.tim_handle.Instance = TIM2, \ /* TIM1/2/3... */
.name = "pwm2", \ /* your pwm device name */
.channel = 0 \ /* range: 1--4 */
}
#endif /* PWM2_CONFIG */
#endif /* BSP_USING_PWM2 */
Expand Down
6 changes: 3 additions & 3 deletions bsp/stm32/libraries/HAL_Drivers/config/l4/pwm_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ extern "C" {
#ifndef PWM1_CONFIG
#define PWM1_CONFIG \
{ \
.tim_handle.Instance = TIM1, \
.name = "pwm1", \
.channel = 0 \
.tim_handle.Instance = TIM1, \ /* TIM1/2/3... */
.name = "pwm1", \ /* your pwm device name */
.channel = 0 \ /* range: 1--4 */
}
#endif /* PWM1_CONFIG */
#endif /* BSP_USING_PWM1 */
Expand Down
6 changes: 3 additions & 3 deletions bsp/stm32/libraries/HAL_Drivers/config/l5/pwm_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ extern "C" {
#ifndef PWM1_CONFIG
#define PWM1_CONFIG \
{ \
.tim_handle.Instance = TIM1, \
.name = "pwm1", \
.channel = 0 \
.tim_handle.Instance = TIM1, \ /* TIM1/2/3... */
.name = "pwm1", \ /* your pwm device name */
.channel = 0 \ /* range: 1--4 */
}
#endif /* PWM1_CONFIG */
#endif /* BSP_USING_PWM1 */
Expand Down
6 changes: 3 additions & 3 deletions bsp/stm32/libraries/HAL_Drivers/config/mp1/pwm_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ extern "C" {
#ifndef PWM2_CONFIG
#define PWM2_CONFIG \
{ \
.tim_handle.Instance = TIM2, \
.name = "pwm2", \
.channel = 0 \
.tim_handle.Instance = TIM2, \ /* TIM1/2/3... */
.name = "pwm2", \ /* your pwm device name */
.channel = 0 \ /* range: 1--4 */
}
#endif /* PWM2_CONFIG */
#endif /* BSP_USING_PWM2 */
Expand Down
6 changes: 3 additions & 3 deletions bsp/stm32/libraries/HAL_Drivers/config/u5/pwm_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ extern "C" {
#ifndef PWM1_CONFIG
#define PWM1_CONFIG \
{ \
.tim_handle.Instance = TIM1, \
.name = "pwm1", \
.channel = 0 \
.tim_handle.Instance = TIM1, \ /* TIM1/2/3... */
.name = "pwm1", \ /* your pwm device name */
.channel = 0 \ /* range: 1--4 */
}
#endif /* PWM1_CONFIG */
#endif /* BSP_USING_PWM1 */
Expand Down
6 changes: 3 additions & 3 deletions bsp/stm32/libraries/HAL_Drivers/config/wb/pwm_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ extern "C" {
#ifndef PWM1_CONFIG
#define PWM1_CONFIG \
{ \
.tim_handle.Instance = TIM1, \
.name = "pwm1", \
.channel = 0 \
.tim_handle.Instance = TIM1, \ /* TIM1/2/3... */
.name = "pwm1", \ /* your pwm device name */
.channel = 0 \ /* range: 1--4 */
}
#endif /* PWM1_CONFIG */
#endif /* BSP_USING_PWM1 */
Expand Down
82 changes: 40 additions & 42 deletions bsp/stm32/libraries/HAL_Drivers/drv_pwm.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,13 @@ static struct rt_pwm_ops drv_ops =
static rt_err_t drv_pwm_enable(TIM_HandleTypeDef *htim, struct rt_pwm_configuration *configuration, rt_bool_t enable)
{
/* Converts the channel number to the channel number of Hal library */
rt_uint32_t channel = 0x04 * (configuration->channel - 1);
rt_uint32_t channel = configuration->channel;
if(channel<1 || channel >4)
{
LOG_E("pwm channel %d is invalid, failed to enable", configuration->channel);
return -RT_ERROR;
}
channel = 0x04 * (configuration->channel - 1);

if (!configuration->complementary)
{
Expand Down Expand Up @@ -265,7 +271,13 @@ static rt_err_t drv_pwm_enable(TIM_HandleTypeDef *htim, struct rt_pwm_configurat
static rt_err_t drv_pwm_get(TIM_HandleTypeDef *htim, struct rt_pwm_configuration *configuration)
{
/* Converts the channel number to the channel number of Hal library */
rt_uint32_t channel = 0x04 * (configuration->channel - 1);
rt_uint32_t channel = configuration->channel;
if(channel<1 || channel >4)
{
LOG_E("pwm channel %d is invalid, failed to get", configuration->channel);
return -RT_ERROR;
}
channel = 0x04 * (configuration->channel - 1);
rt_uint64_t tim_clock;

tim_clock = tim_clock_get(htim);
Expand All @@ -291,7 +303,13 @@ static rt_err_t drv_pwm_set(TIM_HandleTypeDef *htim, struct rt_pwm_configuration
rt_uint32_t period, pulse;
rt_uint64_t tim_clock, psc;
/* Converts the channel number to the channel number of Hal library */
rt_uint32_t channel = 0x04 * (configuration->channel - 1);
rt_uint32_t channel = configuration->channel;
if(channel<1 || channel >4)
{
LOG_E("pwm channel %d is invalid, failed to set", configuration->channel);
return -RT_ERROR;
}
channel = 0x04 * (configuration->channel - 1);

tim_clock = tim_clock_get(htim);
/* Convert nanosecond to frequency and duty cycle. 1s = 1 * 1000 * 1000 * 1000 ns */
Expand Down Expand Up @@ -354,7 +372,13 @@ static rt_err_t drv_pwm_set_pulse(TIM_HandleTypeDef *htim, struct rt_pwm_configu
rt_uint32_t period, pulse;
rt_uint64_t tim_clock;
/* Converts the channel number to the channel number of Hal library */
rt_uint32_t channel = 0x04 * (configuration->channel - 1);
rt_uint32_t channel = configuration->channel;
if(channel<1 || channel >4)
{
LOG_E("pwm channel %d is invalid, failed to set pulse", configuration->channel);
return -RT_ERROR;
}
channel = 0x04 * (configuration->channel - 1);

tim_clock = tim_clock_get(htim);
/* Convert nanosecond to frequency and duty cycle. 1s = 1 * 1000 * 1000 * 1000 ns */
Expand Down Expand Up @@ -408,6 +432,14 @@ static rt_err_t stm32_hw_pwm_init(struct stm32_pwm *device)
TIM_MasterConfigTypeDef master_config = {0};
TIM_ClockConfigTypeDef clock_config = {0};

rt_uint32_t channel = device->channel;
if(channel<1 || channel >4)
{
LOG_E("%s pwm channel %d is invalid, failed to init", device->name, device->channel);
return -RT_ERROR;
}
channel = 0x04 * (device->channel - 1);

RT_ASSERT(device != RT_NULL);

tim = (TIM_HandleTypeDef *)&device->tim_handle;
Expand Down Expand Up @@ -458,45 +490,11 @@ static rt_err_t stm32_hw_pwm_init(struct stm32_pwm *device)
oc_config.OCNIdleState = TIM_OCNIDLESTATE_RESET;
oc_config.OCIdleState = TIM_OCIDLESTATE_RESET;

/* config pwm channel */
if (device->channel & 0x01)
{
if (HAL_TIM_PWM_ConfigChannel(tim, &oc_config, TIM_CHANNEL_1) != HAL_OK)
{
LOG_E("%s channel1 config failed", device->name);
result = -RT_ERROR;
goto __exit;
}
}

if (device->channel & 0x02)
{
if (HAL_TIM_PWM_ConfigChannel(tim, &oc_config, TIM_CHANNEL_2) != HAL_OK)
{
LOG_E("%s channel2 config failed", device->name);
result = -RT_ERROR;
goto __exit;
}
}

if (device->channel & 0x04)
if (HAL_TIM_PWM_ConfigChannel(tim, &oc_config, channel) != HAL_OK)
{
if (HAL_TIM_PWM_ConfigChannel(tim, &oc_config, TIM_CHANNEL_3) != HAL_OK)
{
LOG_E("%s channel3 config failed", device->name);
result = -RT_ERROR;
goto __exit;
}
}

if (device->channel & 0x08)
{
if (HAL_TIM_PWM_ConfigChannel(tim, &oc_config, TIM_CHANNEL_4) != HAL_OK)
{
LOG_E("%s channel4 config failed", device->name);
result = -RT_ERROR;
goto __exit;
}
LOG_E("%s %d config failed", device->name, device->channel);
result = -RT_ERROR;
goto __exit;
}

/* pwm pin configuration */
Expand Down