@@ -360,12 +360,20 @@ static void gpio_config_pwmout(const struct gd32_pwm *pwm,
360360 }
361361 if (configuration -> complementary )
362362 {
363- if (channel_num > 3 ) channel_num = 3 ;
363+ if (channel_num > 3 )
364+ {
365+ LOG_E ("GD32 PWM complementary channel max 3!\n" );
366+ return ;
367+ };
364368 channel = pwm -> nchannels [channel_num - 1 ];
365369 }
366370 else
367371 {
368- if (channel_num > 4 ) channel_num = 4 ;
372+ if (channel_num > 4 )
373+ {
374+ LOG_E ("GD32 PWM complementary channel max 4!\n" );
375+ return ;
376+ };
369377 channel = pwm -> channels [channel_num - 1 ];
370378 }
371379 gpio_mode_set (channel .gpio_port , GPIO_MODE_AF , GPIO_PUPD_NONE , channel .gpio_pin );
@@ -385,26 +393,31 @@ static void gpio_config_input(const struct gd32_pwm *pwm,
385393 uint8_t channel_num = configuration -> channel ;
386394 if (channel_num == 0 )
387395 {
388- LOG_E ("PWM channel starts from 1!\n" );
389396 return ;
390397 }
391398 if (configuration -> complementary )
392399 {
393- if (channel_num > 3 ) channel_num = 3 ;
400+ if (channel_num > 3 )
401+ {
402+ LOG_E ("GD32 PWM complementary channel max 3!\n" );
403+ return ;
404+ };
394405 channel = pwm -> nchannels [channel_num - 1 ];
395406 }
396407 else
397408 {
398- if (channel_num > 4 ) channel_num = 4 ;
409+ if (channel_num > 4 )
410+ {
411+ LOG_E ("GD32 PWM complementary channel max 4!\n" );
412+ return ;
413+ };
399414 channel = pwm -> channels [channel_num - 1 ];
400415 }
401416 gpio_mode_set (channel .gpio_port , GPIO_MODE_INPUT , GPIO_PUPD_NONE , channel .gpio_pin );
402417}
403418
404419static void channel_output_config (rt_uint32_t timer_periph , timer_oc_parameter_struct * ocpara )
405420{
406- rt_int16_t i ;
407-
408421 switch (timer_periph )
409422 {
410423 /* Advanced timer */
@@ -504,6 +517,28 @@ static void timer_config(void)
504517static rt_err_t drv_pwm_enable (struct gd32_pwm * pwm , const struct rt_pwm_configuration * configuration ,
505518 rt_bool_t enable )
506519{
520+ if (channel_num == 0 )
521+ {
522+ LOG_E ("GD32 PWM channel starts from 1!\n" );
523+ return - RT_EINVAL ;
524+ }
525+ if (configuration -> complementary )
526+ {
527+ if (channel_num > 3 )
528+ {
529+ LOG_E ("GD32 PWM complementary channel max 3!\n" );
530+ return - RT_EINVAL ;
531+ };
532+ }
533+ else
534+ {
535+ if (channel_num > 4 )
536+ {
537+ LOG_E ("GD32 PWM complementary channel max 4!\n" );
538+ return - RT_EINVAL ;
539+ };
540+ }
541+
507542 if (!enable )
508543 {
509544 gpio_config_input (pwm , configuration );
@@ -541,6 +576,11 @@ static rt_err_t drv_pwm_get(const struct gd32_pwm *pwm, struct rt_pwm_configurat
541576 rt_uint64_t tim_clock ;
542577 rt_uint16_t psc ;
543578 rt_uint32_t chxcv ;
579+
580+ if (configuration -> channel == 0 )
581+ {
582+ return - RT_EINVAL ;
583+ }
544584
545585 rt_uint8_t coef = (RCU_CFG1 & RCU_CFG1_TIMERSEL )?4 :2 ;
546586 tim_clock = rcu_clock_freq_get (pwm -> apb_of )* coef ;
@@ -572,6 +612,11 @@ static rt_err_t drv_pwm_set(struct gd32_pwm *pwm, struct rt_pwm_configuration *c
572612 rt_uint8_t coef = (RCU_CFG1 & RCU_CFG1_TIMERSEL )?4 :2 ;
573613 tim_clock = rcu_clock_freq_get (pwm -> apb_of )* coef ;
574614
615+ if (configuration -> channel == 0 )
616+ {
617+ return - RT_EINVAL ;
618+ }
619+
575620 /* Convert nanosecond to frequency and duty cycle. 1s = 1 * 1000 * 1000 * 1000 ns */
576621 tim_clock /= 1000000UL ;
577622 period = (unsigned long long )configuration -> period * tim_clock / 1000ULL ;
@@ -609,7 +654,7 @@ static rt_err_t drv_pwm_set(struct gd32_pwm *pwm, struct rt_pwm_configuration *c
609654static rt_err_t drv_pwm_control (struct rt_device_pwm * device , int cmd , void * arg )
610655{
611656 struct rt_pwm_configuration * configuration = (struct rt_pwm_configuration * )arg ;
612- struct gd32_pwm * pwm = ( struct gd32_pwm * ) device ;
657+ struct gd32_pwm * pwm = rt_container_of ( device , struct gd32_pwm , pwm_device ) ;
613658
614659 switch (cmd )
615660 {
0 commit comments