帮我优化一下这段代码配置2M波特率的CANFD :#include "can.h" #include "gd32c10x.h" #include "gd32c10x_eval.h" void can_gpio_config(void) { rcu_periph_clock_enable(RCU_CAN0); rcu_periph_clock_enable(RCU_CAN1); rcu_periph_clock_enable(RCU_GPIOB); rcu_periph_clock_enable(RCU_AF); gpio_init(GPIOB,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_8); gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_9); gpio_init(GPIOB, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_5); gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6); gpio_pin_remap_config(GPIO_CAN0_PARTIAL_REMAP , ENABLE); gpio_pin_remap_config(GPIO_CAN1_REMAP, ENABLE); } void can_config(void) { can_parameter_struct can_parameter; can_fdframe_struct can_fd_parameter; can_fd_tdc_struct can_fd_tdc_parameter; can_struct_para_init(CAN_INIT_STRUCT, &can_parameter); can_deinit(CAN0); can_deinit(CAN1); can_parameter.time_triggered = DISABLE; can_parameter.auto_bus_off_recovery = DISABLE; can_parameter.auto_wake_up = DISABLE; can_parameter.auto_retrans = ENABLE; can_parameter.rec_fifo_overwrite = DISABLE; can_parameter.trans_fifo_order = DISABLE; can_parameter.working_mode = CAN_NORMAL_MODE; can_init(CAN0, &can_parameter); can_init(CAN1, &can_parameter); can_frequency_set(CAN0, CAN_BAUD_RATE); can_frequency_set(CAN1, CAN_BAUD_RATE); can_struct_para_init(CAN_FD_FRAME_STRUCT, &can_fd_parameter); can_fd_parameter.fd_frame = ENABLE; can_fd_parameter.excp_event_detect = ENABLE; can_fd_parameter.delay_compensation = ENABLE; can_fd_tdc_parameter.tdc_filter = 0x04; can_fd_tdc_parameter.tdc_mode = CAN_TDCMOD_CALC_AND_OFFSET; can_fd_tdc_parameter.tdc_offset = 0x04; can_fd_parameter.p_delay_compensation = &can_fd_tdc_parameter; can_fd_parameter.iso_bosch = CAN_FDMOD_ISO; can_fd_parameter.esi_mode = CAN_ESIMOD_HARDWARE; can_fd_init(CAN0, &can_fd_parameter); can_fd_init(CAN1, &can_fd_parameter); can_fd_frequency_set(CAN0, CANFD_BAUD_RATE); can_fd_frequency_set(CAN1, CANFD_BAUD_RATE); can1_filter_start_bank(14); can_filter_mask_mode_init(DEV_CAN0_ID, DEV_CAN0_MASK, CAN_EXTENDED_FIFO0, 0); can_filter_mask_mode_init(DEV_CAN1_ID, DEV_CAN1_MASK, CAN_EXTENDED_FIFO0, 15); nvic_irq_enable(CAN0_RX0_IRQn, 7, 0); nvic_irq_enable(CAN1_RX0_IRQn, 7, 0); can_interrupt_enable(CAN0, CAN_INTEN_RFNEIE0); can_interrupt_enable(CAN1, CAN_INTEN_RFNEIE0); }
时间: 2023-08-14 20:07:29 浏览: 220
兆易GD32C10X系列单片机固件库GD32C10x_Firmware_Library_V1.0.2+说明文档.zip
5星 · 资源好评率100%
可以将函数can_gpio_config()和can_config()的代码分别进行优化,具体优化如下:
1. can_gpio_config()优化:
将rcu_periph_clock_enable(RCU_GPIOB)和rcu_periph_clock_enable(RCU_AF)放到函数外部进行调用,减少函数内部的代码量。
将GPIO初始化的代码放到一个数组中,使用循环语句进行初始化,减少代码行数,提高代码可读性。
优化后的代码如下:
```
void can_gpio_config(void) {
/* 这两个语句可以放到函数外部进行调用 */
rcu_periph_clock_enable(RCU_CAN0);
rcu_periph_clock_enable(RCU_CAN1);
/* GPIOB时钟和复用功能时钟在函数外部调用 */
gpio_init_parameter_struct gpio_init_struct[4] = {
{GPIOB, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_8},
{GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9},
{GPIOB, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_5},
{GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6}
};
for(int i = 0; i < 4; i++) {
gpio_init(&gpio_init_struct[i]);
}
gpio_pin_remap_config(GPIO_CAN0_PARTIAL_REMAP , ENABLE);
gpio_pin_remap_config(GPIO_CAN1_REMAP, ENABLE);
}
```
2. can_config()优化:
将can_init()和can_frequency_set()合并为一个函数进行调用。
将can_fd_init()和can_fd_frequency_set()合并为一个函数进行调用。
将can_filter_mask_mode_init()和can1_filter_start_bank()合并为一个函数进行调用。
优化后的代码如下:
```
void can_config(void) {
can_parameter_struct can_parameter;
can_fdframe_struct can_fd_parameter;
can_fd_tdc_struct can_fd_tdc_parameter;
can_struct_para_init(CAN_INIT_STRUCT, &can_parameter);
can_deinit(CAN0);
can_deinit(CAN1);
can_parameter.time_triggered = DISABLE;
can_parameter.auto_bus_off_recovery = DISABLE;
can_parameter.auto_wake_up = DISABLE;
can_parameter.auto_retrans = ENABLE;
can_parameter.rec_fifo_overwrite = DISABLE;
can_parameter.trans_fifo_order = DISABLE;
can_parameter.working_mode = CAN_NORMAL_MODE;
can_init_parameter_struct can_init_struct[2] = {
{CAN0, &can_parameter, CAN_BAUD_RATE},
{CAN1, &can_parameter, CAN_BAUD_RATE},
};
for(int i = 0; i < 2; i++) {
can_struct_para_init(CAN_FD_FRAME_STRUCT, &can_fd_parameter);
can_fd_parameter.fd_frame = ENABLE;
can_fd_parameter.excp_event_detect = ENABLE;
can_fd_parameter.delay_compensation = ENABLE;
can_fd_tdc_parameter.tdc_filter = 0x04;
can_fd_tdc_parameter.tdc_mode = CAN_TDCMOD_CALC_AND_OFFSET;
can_fd_tdc_parameter.tdc_offset = 0x04;
can_fd_parameter.p_delay_compensation = &can_fd_tdc_parameter;
can_fd_parameter.iso_bosch = CAN_FDMOD_ISO;
can_fd_parameter.esi_mode = CAN_ESIMOD_HARDWARE;
can_fd_init(can_init_struct[i].can_periph, &can_fd_parameter);
can_frequency_set(can_init_struct[i].can_periph, can_init_struct[i].baud_rate);
can_fd_frequency_set(can_init_struct[i].can_periph, CANFD_BAUD_RATE);
}
can_filter_mask_mode_struct can_filter_struct[2] = {
{DEV_CAN0_ID, DEV_CAN0_MASK, CAN_EXTENDED_FIFO0, 0},
{DEV_CAN1_ID, DEV_CAN1_MASK, CAN_EXTENDED_FIFO0, 15},
};
for(int i = 0; i < 2; i++) {
can_filter_mask_mode_init(can_filter_struct[i].can_id, can_filter_struct[i].can_mask, can_filter_struct[i].can_fifo, can_filter_struct[i].can_bank);
}
can1_filter_start_bank(14);
nvic_irq_enable(CAN0_RX0_IRQn, 7, 0);
nvic_irq_enable(CAN1_RX0_IRQn, 7, 0);
can_interrupt_enable(CAN0, CAN_INTEN_RFNEIE0);
can_interrupt_enable(CAN1, CAN_INTEN_RFNEIE0);
}
```
经过以上的优化,代码可读性更强,代码行数更少,也更易于维护。
阅读全文