Skip to content

Commit fe9c6e1

Browse files
committed
fix(bsp/gd32): 优化部分代码,提高兼容性和健壮性
1 parent e9b89d5 commit fe9c6e1

File tree

2 files changed

+66
-8
lines changed

2 files changed

+66
-8
lines changed

bsp/gd32/arm/libraries/gd32_drivers/Kconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ if BSP_USING_USBD
2424
# "ULPI: UTMI+ Low Pin Interface"
2525
endif
2626

27+
if BSP_USING_HARD_I2C
28+
choice
29+
prompt "Select I2C Receiving Scheme"
30+
default BSP_USING_RECEIVING_A
31+
32+
config BSP_USING_RECEIVING_A
33+
bool "master receiving secheme A --- requires that the software be capable of responding quickly to the 12C event."
34+
35+
config BSP_USING_RECEIVING_B
36+
bool "master receiving secheme B --- don't requires that the software be capable of responding quickly to the 12C event."
37+
endchoice
38+
endif
39+
2740
config BSP_USING_GD_DBG
2841
bool
2942
default y

bsp/gd32/arm/libraries/gd32_drivers/drv_pwm.c

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

404419
static 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)
504517
static 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
609654
static 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

Comments
 (0)