static void CAN1_Config(uint16_t Bps)
{
/* enable CAN clock */
rcu_can_clock_config(CAN1, RCU_CANSRC_PCLK2_DIV_2);
rcu_periph_clock_enable(RCU_CAN1);
can_parameter_struct can_parameter;
/* initialize CAN register */
can_deinit(CAN1);
/* initialize CAN */
can_struct_para_init(CAN_INIT_STRUCT, &can_parameter);
/* initialize CAN parameters */
can_parameter.internal_counter_source = CAN_TIMER_SOURCE_BIT_CLOCK;
can_parameter.self_reception = DISABLE;
can_parameter.mb_tx_order = CAN_TX_HIGH_PRIORITY_MB_FIRST;
can_parameter.mb_tx_abort_enable = ENABLE;
can_parameter.local_priority_enable = DISABLE;
can_parameter.mb_rx_ide_rtr_type = CAN_IDE_RTR_FILTERED;
can_parameter.mb_remote_frame = CAN_STORE_REMOTE_REQUEST_FRAME;
can_parameter.rx_private_filter_queue_enable = DISABLE;
can_parameter.edge_filter_enable = DISABLE;
can_parameter.protocol_exception_enable = DISABLE;
can_parameter.rx_filter_order = CAN_RX_FILTER_ORDER_MAILBOX_FIRST;
can_parameter.memory_size = CAN_MEMSIZE_32_UNIT;
/* filter configuration */
can_parameter.mb_public_filter = 0x0;
can_parameter.resync_jump_width = 1;
can_parameter.prop_time_segment = 2;
can_parameter.time_segment_1 = 4;
can_parameter.time_segment_2 = 3;
/*baudrate =fCANCLK/prescaler/(1+PTS+PBS1+PBS2)
=50M/40/(1+2+4+3)=125K
*/
can_parameter.prescaler = 50000/Bps/(1+2+4+3); //分频系数
/* initialize CAN */
can_init(CAN1, &can_parameter);
/* configure CAN0 NVIC */
nvic_irq_enable(CAN1_Message_IRQn,6, 0);
/* enable CAN MB0 interrupt */
// can_interrupt_enable(CAN1, CAN_INT_MB0); //邮箱0 接收、发送中断
can_interrupt_enable(CAN1, CAN_INT_FIFO_AVAILABLE); //FIFO 非空中断
// can_interrupt_enable(CAN1, CAN_INT_RX_WARNING); //接收错误中断
// can_interrupt_enable(CAN1, CAN_INT_TX_WARNING); //发送错误中断
// can_interrupt_enable(CAN1, CAN_INT_ERR_SUMMARY); //错误集合中断
CAN1_FIFO_Config();
can_operation_mode_enter(CAN1, CAN_NORMAL_MODE);
}
static void CAN1_FIFO_Config(void)
{
/*
00:格式A:每个标识符过滤表元素包含一个完整标识符(标准格式和扩展格式)
01:格式B:每个标识符过滤表元素包含两个完整标准格式标识符或者两个扩展格式标识符其中14位
10:格式C:每个标识符过滤表元素包含四个标准格式标识符其中8位或者四个扩展格式标识符其中8位
*/
// //3xx;
can_fifo_parameter_struct can_rx_fifo_config_struct;
can_rx_fifo_config_struct.dma_enable = DISABLE;
can_rx_fifo_config_struct.filter_format_and_number = CAN_RXFIFO_FILTER_A_NUM_8;// 8个过滤器
can_rx_fifo_config_struct.fifo_public_filter = FIFO_FILTER_ID_EXD_A(0xFFFFFF00);//0xFFFFF00; //bit1 的是完全匹配 0是不关心
can_rx_fifo_config(CAN1,&can_rx_fifo_config_struct);
can_rx_fifo_id_filter_struct rx_fifo_id_filter_config[8];
for(int i = 0 ; i < 8; i++)
can_struct_para_init(CAN_FIFO_ID_FILTER_STRUCT, &rx_fifo_id_filter_config[i]);
rx_fifo_id_filter_config[0].remote_frame = CAN_DATA_FRAME_ACCEPTED;//远程针不要
rx_fifo_id_filter_config[0].extended_frame = CAN_EXTENDED_FRAME_ACCEPTED; //只要扩展帧
rx_fifo_id_filter_config[0].id = 0xA101;
rx_fifo_id_filter_config[1].remote_frame = CAN_DATA_FRAME_ACCEPTED;//远程针不要
rx_fifo_id_filter_config[1].extended_frame = CAN_EXTENDED_FRAME_ACCEPTED; //只要扩展帧
rx_fifo_id_filter_config[1].id = 0xB101;
can_rx_fifo_filter_table_config(CAN1,rx_fifo_id_filter_config);
} |
@tyw :牛牛牛
去下了啃