CANFD介绍
FD全称是 Flexible Data-Rate,顾名思义,表示CAN-FD 的帧报文具有数据场波特率可变的特性,即仲裁场合数据控制场使用标准的通信波特率,而到数据场就会切换为更高的通信波特率,车端常用的为2Mbit/s和5Mbit/s,从而达到提高通信速率的目的
CAN
CAN FD
GD32C103/GD32C113介绍
GD32C113是GD32C103的升级版,带两路CANFD
GD32C113 CAN引脚
can0
can0
示例
extern can_trasnmit_message_struct g_transmit_message;
extern can_receive_message_struct g_receive_message;
extern FlagStatus can0_receive_flag;
extern FlagStatus can1_receive_flag;
can_parameter_struct can_parameter;
can_fdframe_struct can_fd_parameter;
can_fd_tdc_struct can_fd_tdc_parameter;
启用时钟
// 启用GPIO时钟
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_GPIOB);
// 启用CAN时钟
rcu_periph_clock_enable(RCU_CAN0);
rcu_periph_clock_enable(RCU_CAN1);
// 根据实际配置复用时钟
// rcu_periph_clock_enable(RCU_AF);
初始化CAN FD
/* configure CAN0 GPIO */
gpio_init(GPIOA, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_0); // rx
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1); // tx
/* configure CAN1 GPIO */
gpio_init(GPIOB, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_12); // rx
gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13); // tx
// 根据实际配置复用CAN GPIO
// gpio_pin_remap_config(GPIO_CAN0_FULL_REMAP, ENABLE);
// gpio_pin_remap_config(GPIO_CAN1_REMAP, ENABLE);
// 复位can配置
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 = DISABLE;
can_parameter.rec_fifo_overwrite = DISABLE;
can_parameter.trans_fifo_order = DISABLE;
can_parameter.working_mode = CAN_NORMAL_MODE;
/* configure CAN0/1 baud rate 1MBps, sample point at 80% */
can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
can_parameter.time_segment_1 = CAN_BT_BS1_7TQ;
can_parameter.time_segment_2 = CAN_BT_BS2_2TQ;
can_parameter.prescaler = 6;
// 初始化CAN
can_init(CAN0, &can_parameter);
can_init(CAN1, &can_parameter);
// 配置CANFD
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;
/* FD mode data baudrate 2Mbps, sample piont at 80% */
can_fd_parameter.data_resync_jump_width = CAN_BT_SJW_1TQ;
can_fd_parameter.data_time_segment_1 = CAN_BT_BS1_3TQ;
can_fd_parameter.data_time_segment_2 = CAN_BT_BS2_1TQ;
/* CAN-FD data segement prescaler should be the same as non-data segement prescaler */
can_fd_parameter.data_prescaler = 6;
can_fd_init(CAN0, &can_fd_parameter);
can_fd_init(CAN1, &can_fd_parameter);
/* initialize filter */
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);
// 启用CAN接收中断
nvic_irq_enable(CAN0_RX0_IRQn, 0, 0);
nvic_irq_enable(CAN1_RX0_IRQn, 1, 0);
/* enable can receive FIFO0 not empty interrupt */
can_interrupt_enable(CAN0, CAN_INTEN_RFNEIE0);
can_interrupt_enable(CAN1, CAN_INTEN_RFNEIE0);
can_struct_para_init(CAN_TX_MESSAGE_STRUCT, &g_transmit_message);
g_transmit_message.tx_sfid = 0x00;
g_transmit_message.tx_efid = 0x00;
g_transmit_message.tx_ft = CAN_FT_DATA;
g_transmit_message.tx_ff = CAN_FF_EXTENDED;
g_transmit_message.tx_dlen = 8;
// 设置CAN FD长度
g_transmit_message.tx_dlen = 12;
g_transmit_message.fd_flag = 1;
g_transmit_message.fd_brs = 1;
g_transmit_message.fd_esi = 0;
/* initialize receive message */
can_struct_para_init(CAN_RX_MESSAGE_STRUCT, &g_receive_message);
发送can fd
g_transmit_message.tx_efid = DEV_CAN1_ID;
g_transmit_message.tx_data[0] = 0x00;
g_transmit_message.tx_data[1] = 0xA1;
g_transmit_message.tx_data[2] = 0xA2;
g_transmit_message.tx_data[3] = 0xA3;
g_transmit_message.tx_data[4] = 0xA4;
g_transmit_message.tx_data[5] = 0xA1;
g_transmit_message.tx_data[6] = 0xA2;
g_transmit_message.tx_data[7] = 0xA3;
g_transmit_message.tx_data[8] = 0xA4;
g_transmit_message.tx_data[9] = 0xA1;
g_transmit_message.tx_data[10] = 0X12;
g_transmit_message.tx_data[11] = 0X03;
can_message_transmit(CAN0, &g_transmit_message);
can_message_transmit(CAN1, &g_transmit_message);