Skip to content

Commit f42f86c

Browse files
authored
Revert "提交stm32 bsp drv_pwm.c里面的一个小建议 (#6517)"
This reverts commit a4eb5a5.
1 parent a4eb5a5 commit f42f86c

File tree

16 files changed

+42
-130
lines changed

16 files changed

+42
-130
lines changed

bsp/stm32/libraries/HAL_Drivers/config/f0/pwm_config.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717
extern "C" {
1818
#endif
1919

20-
/*
21-
* .tim_handle.Instance = TIM1/2/3...,
22-
* .name = "your pwm device name",
23-
* .channel = 1/2/3/4
24-
*/
25-
2620
#ifdef BSP_USING_PWM2
2721
#ifndef PWM2_CONFIG
2822
#define PWM2_CONFIG \

bsp/stm32/libraries/HAL_Drivers/config/f1/pwm_config.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717
extern "C" {
1818
#endif
1919

20-
/*
21-
* .tim_handle.Instance = TIM1/2/3...,
22-
* .name = "your pwm device name",
23-
* .channel = 1/2/3/4
24-
*/
25-
2620
#ifdef BSP_USING_PWM1
2721
#ifndef PWM1_CONFIG
2822
#define PWM1_CONFIG \

bsp/stm32/libraries/HAL_Drivers/config/f2/pwm_config.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717
extern "C" {
1818
#endif
1919

20-
/*
21-
* .tim_handle.Instance = TIM1/2/3...,
22-
* .name = "your pwm device name",
23-
* .channel = 1/2/3/4
24-
*/
25-
2620
#ifdef BSP_USING_PWM2
2721
#ifndef PWM2_CONFIG
2822
#define PWM2_CONFIG \

bsp/stm32/libraries/HAL_Drivers/config/f3/pwm_config.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717
extern "C" {
1818
#endif
1919

20-
/*
21-
* .tim_handle.Instance = TIM1/2/3...,
22-
* .name = "your pwm device name",
23-
* .channel = 1/2/3/4
24-
*/
25-
2620
#ifdef BSP_USING_PWM1
2721
#ifndef PWM1_CONFIG
2822
#define PWM1_CONFIG \

bsp/stm32/libraries/HAL_Drivers/config/f4/pwm_config.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717
extern "C" {
1818
#endif
1919

20-
/*
21-
* .tim_handle.Instance = TIM1/2/3...,
22-
* .name = "your pwm device name",
23-
* .channel = 1/2/3/4
24-
*/
25-
2620
#ifdef BSP_USING_PWM1
2721
#ifndef PWM1_CONFIG
2822
#define PWM1_CONFIG \

bsp/stm32/libraries/HAL_Drivers/config/f7/pwm_config.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717
extern "C" {
1818
#endif
1919

20-
/*
21-
* .tim_handle.Instance = TIM1/2/3...,
22-
* .name = "your pwm device name",
23-
* .channel = 1/2/3/4
24-
*/
25-
2620
#ifdef BSP_USING_PWM2
2721
#ifndef PWM2_CONFIG
2822
#define PWM2_CONFIG \

bsp/stm32/libraries/HAL_Drivers/config/g0/pwm_config.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@
1818
extern "C" {
1919
#endif
2020

21-
/*
22-
* .tim_handle.Instance = TIM1/2/3...,
23-
* .name = "your pwm device name",
24-
* .channel = 1/2/3/4
25-
*/
26-
2721
#ifdef BSP_USING_PWM2
2822
#ifndef PWM2_CONFIG
2923
#define PWM2_CONFIG \

bsp/stm32/libraries/HAL_Drivers/config/g4/pwm_config.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717
extern "C" {
1818
#endif
1919

20-
/*
21-
* .tim_handle.Instance = TIM1/2/3...,
22-
* .name = "your pwm device name",
23-
* .channel = 1/2/3/4
24-
*/
25-
2620
#ifdef BSP_USING_PWM2
2721
#ifndef PWM2_CONFIG
2822
#define PWM2_CONFIG \

bsp/stm32/libraries/HAL_Drivers/config/h7/pwm_config.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@
1818
extern "C" {
1919
#endif
2020

21-
/*
22-
* .tim_handle.Instance = TIM1/2/3...,
23-
* .name = "your pwm device name",
24-
* .channel = 1/2/3/4
25-
*/
26-
2721
#ifdef BSP_USING_PWM1
2822
#ifndef PWM1_CONFIG
2923
#define PWM1_CONFIG \

bsp/stm32/libraries/HAL_Drivers/config/l1/pwm_config.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717
extern "C" {
1818
#endif
1919

20-
/*
21-
* .tim_handle.Instance = TIM1/2/3...,
22-
* .name = "your pwm device name",
23-
* .channel = 1/2/3/4
24-
*/
25-
2620
#ifdef BSP_USING_PWM2
2721
#ifndef PWM2_CONFIG
2822
#define PWM2_CONFIG \

bsp/stm32/libraries/HAL_Drivers/config/l4/pwm_config.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717
extern "C" {
1818
#endif
1919

20-
/*
21-
* .tim_handle.Instance = TIM1/2/3...,
22-
* .name = "your pwm device name",
23-
* .channel = 1/2/3/4
24-
*/
25-
2620
#ifdef BSP_USING_PWM1
2721
#ifndef PWM1_CONFIG
2822
#define PWM1_CONFIG \

bsp/stm32/libraries/HAL_Drivers/config/l5/pwm_config.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717
extern "C" {
1818
#endif
1919

20-
/*
21-
* .tim_handle.Instance = TIM1/2/3...,
22-
* .name = "your pwm device name",
23-
* .channel = 1/2/3/4
24-
*/
25-
2620
#ifdef BSP_USING_PWM1
2721
#ifndef PWM1_CONFIG
2822
#define PWM1_CONFIG \

bsp/stm32/libraries/HAL_Drivers/config/mp1/pwm_config.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717
extern "C" {
1818
#endif
1919

20-
/*
21-
* .tim_handle.Instance = TIM1/2/3...,
22-
* .name = "your pwm device name",
23-
* .channel = 1/2/3/4
24-
*/
25-
2620
#ifdef BSP_USING_PWM2
2721
#ifndef PWM2_CONFIG
2822
#define PWM2_CONFIG \

bsp/stm32/libraries/HAL_Drivers/config/u5/pwm_config.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717
extern "C" {
1818
#endif
1919

20-
/*
21-
* .tim_handle.Instance = TIM1/2/3...,
22-
* .name = "your pwm device name",
23-
* .channel = 1/2/3/4
24-
*/
25-
2620
#ifdef BSP_USING_PWM1
2721
#ifndef PWM1_CONFIG
2822
#define PWM1_CONFIG \

bsp/stm32/libraries/HAL_Drivers/config/wb/pwm_config.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@
1717
extern "C" {
1818
#endif
1919

20-
/*
21-
* .tim_handle.Instance = TIM1/2/3...,
22-
* .name = "your pwm device name",
23-
* .channel = 1/2/3/4
24-
*/
25-
2620
#ifdef BSP_USING_PWM1
2721
#ifndef PWM1_CONFIG
2822
#define PWM1_CONFIG \

bsp/stm32/libraries/HAL_Drivers/drv_pwm.c

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,7 @@ static struct rt_pwm_ops drv_ops =
234234
static rt_err_t drv_pwm_enable(TIM_HandleTypeDef *htim, struct rt_pwm_configuration *configuration, rt_bool_t enable)
235235
{
236236
/* Converts the channel number to the channel number of Hal library */
237-
rt_uint32_t channel = configuration->channel;
238-
if(channel < 1 || channel > 4)
239-
{
240-
LOG_E("pwm channel %d is invalid, failed to enable", configuration->channel);
241-
return -RT_ERROR;
242-
}
243-
channel = 0x04 * (configuration->channel - 1);
237+
rt_uint32_t channel = 0x04 * (configuration->channel - 1);
244238

245239
if (!configuration->complementary)
246240
{
@@ -271,13 +265,7 @@ static rt_err_t drv_pwm_enable(TIM_HandleTypeDef *htim, struct rt_pwm_configurat
271265
static rt_err_t drv_pwm_get(TIM_HandleTypeDef *htim, struct rt_pwm_configuration *configuration)
272266
{
273267
/* Converts the channel number to the channel number of Hal library */
274-
rt_uint32_t channel = configuration->channel;
275-
if(channel < 1 || channel > 4)
276-
{
277-
LOG_E("pwm channel %d is invalid, failed to get", configuration->channel);
278-
return -RT_ERROR;
279-
}
280-
channel = 0x04 * (configuration->channel - 1);
268+
rt_uint32_t channel = 0x04 * (configuration->channel - 1);
281269
rt_uint64_t tim_clock;
282270

283271
tim_clock = tim_clock_get(htim);
@@ -303,13 +291,7 @@ static rt_err_t drv_pwm_set(TIM_HandleTypeDef *htim, struct rt_pwm_configuration
303291
rt_uint32_t period, pulse;
304292
rt_uint64_t tim_clock, psc;
305293
/* Converts the channel number to the channel number of Hal library */
306-
rt_uint32_t channel = configuration->channel;
307-
if(channel < 1 || channel > 4)
308-
{
309-
LOG_E("pwm channel %d is invalid, failed to set", configuration->channel);
310-
return -RT_ERROR;
311-
}
312-
channel = 0x04 * (configuration->channel - 1);
294+
rt_uint32_t channel = 0x04 * (configuration->channel - 1);
313295

314296
tim_clock = tim_clock_get(htim);
315297
/* Convert nanosecond to frequency and duty cycle. 1s = 1 * 1000 * 1000 * 1000 ns */
@@ -372,13 +354,7 @@ static rt_err_t drv_pwm_set_pulse(TIM_HandleTypeDef *htim, struct rt_pwm_configu
372354
rt_uint32_t period, pulse;
373355
rt_uint64_t tim_clock;
374356
/* Converts the channel number to the channel number of Hal library */
375-
rt_uint32_t channel = configuration->channel;
376-
if(channel < 1 || channel > 4)
377-
{
378-
LOG_E("pwm channel %d is invalid, failed to set pulse", configuration->channel);
379-
return -RT_ERROR;
380-
}
381-
channel = 0x04 * (configuration->channel - 1);
357+
rt_uint32_t channel = 0x04 * (configuration->channel - 1);
382358

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

435-
rt_uint32_t channel = device->channel;
436-
if(channel < 1 || channel > 4)
437-
{
438-
LOG_E("%s pwm channel %d is invalid, failed to init", device->name, device->channel);
439-
return -RT_ERROR;
440-
}
441-
channel = 0x04 * (device->channel - 1);
442-
443411
RT_ASSERT(device != RT_NULL);
444412

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

493-
if (HAL_TIM_PWM_ConfigChannel(tim, &oc_config, channel) != HAL_OK)
461+
/* config pwm channel */
462+
if (device->channel & 0x01)
494463
{
495-
LOG_E("%s %d config failed", device->name, device->channel);
496-
result = -RT_ERROR;
497-
goto __exit;
464+
if (HAL_TIM_PWM_ConfigChannel(tim, &oc_config, TIM_CHANNEL_1) != HAL_OK)
465+
{
466+
LOG_E("%s channel1 config failed", device->name);
467+
result = -RT_ERROR;
468+
goto __exit;
469+
}
470+
}
471+
472+
if (device->channel & 0x02)
473+
{
474+
if (HAL_TIM_PWM_ConfigChannel(tim, &oc_config, TIM_CHANNEL_2) != HAL_OK)
475+
{
476+
LOG_E("%s channel2 config failed", device->name);
477+
result = -RT_ERROR;
478+
goto __exit;
479+
}
480+
}
481+
482+
if (device->channel & 0x04)
483+
{
484+
if (HAL_TIM_PWM_ConfigChannel(tim, &oc_config, TIM_CHANNEL_3) != HAL_OK)
485+
{
486+
LOG_E("%s channel3 config failed", device->name);
487+
result = -RT_ERROR;
488+
goto __exit;
489+
}
490+
}
491+
492+
if (device->channel & 0x08)
493+
{
494+
if (HAL_TIM_PWM_ConfigChannel(tim, &oc_config, TIM_CHANNEL_4) != HAL_OK)
495+
{
496+
LOG_E("%s channel4 config failed", device->name);
497+
result = -RT_ERROR;
498+
goto __exit;
499+
}
498500
}
499501

500502
/* pwm pin configuration */

0 commit comments

Comments
 (0)