用于发送数据缓冲的发送结构体
can_trasnmit_message_struct txMsg = {
.tx_sfid = 0x00,
.tx_efid = 0x00,
.tx_ft = CAN_FT_DATA,
.tx_ff = CAN_FF_EXTENDED,
.tx_dlen = 0,
.fd_flag = 1,
.fd_brs = 0,
.fd_esi = 1
};
CAN1的初始化
void BSP_CAN_Init(void)
{
rcu_periph_clock_enable(RCU_CAN1);
gpio_init(GPIOB, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_12);
gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13);
//gpio_pin_remap_config(GPIO_CAN1_REMAP, ENABLE);
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(CAN1);
/* initialize CAN parameters */
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 = ENABLE;
can_parameter.trans_fifo_order = ENABLE;
can_parameter.working_mode = CAN_NORMAL_MODE;
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_init(CANX, &can_parameter);
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(CANX, &can_fd_parameter);
can1_filter_start_bank(14);
can_filter_mask_mode_init(0xaabb, 0x0000, CAN_EXTENDED_FIFO0, 15);
nvic_irq_enable(CAN1_RX0_IRQn, 1, 0);
can_interrupt_enable(CANX, CAN_INTEN_RFNEIE0);
}
从队列取数据发送
void BSP_CAN_Background(void)
{
if(!util_FIFO_Any(&tx_fifo))
{
return;
}
const uint32_t available = CAN_TSTAT_TME0 | CAN_TSTAT_TME1 | CAN_TSTAT_TME2;
if((CAN_TSTAT(CANX) & available) == 0)
{
return;
}
FIFO_Index_t idx = util_FIFO_Out(&tx_fifo);
CAN_DataPacket_t *thisMsg = &tx_queue[idx];
txMsg.tx_efid = thisMsg->ExtID;
txMsg.tx_sfid = thisMsg->ExtID;
txMsg.tx_dlen = thisMsg->Len;
memcpy(txMsg.tx_data, &thisMsg->Data, thisMsg->Len);
can_message_transmit(CANX,&txMsg);
}
GPIO时钟再进行CAN初始化之前已经全部打开,中断分组也配置了 |