void CAN_Init(void)
{
can_parameter_struct can_parameter;
can_filter_parameter_struct can_filter;
/* enable CAN clock */
rcu_periph_clock_enable(RCU_CAN0);
#if CONFIG_CAN1_EN
rcu_periph_clock_enable(RCU_CAN1);
#endif
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_GPIOB);
/* configure CAN1 GPIO */
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_11);
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_11);
gpio_af_set(GPIOA, GPIO_AF_9, GPIO_PIN_11);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_12);
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_12);
gpio_af_set(GPIOA, GPIO_AF_9, GPIO_PIN_12);
/* initialize CAN register */
can_deinit(CAN0);
#if CONFIG_CAN1_EN
/* configure CAN1 GPIO */
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_5);
gpio_af_set(GPIOB, GPIO_AF_9, GPIO_PIN_5);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_6);
gpio_af_set(GPIOB, GPIO_AF_9, GPIO_PIN_6);
/* initialize CAN register */
can_deinit(CAN1);
#endif
can_struct_para_init(CAN_INIT_STRUCT, &can_parameter);
can_struct_para_init(CAN_INIT_STRUCT, &can_filter);
/* initialize CAN parameters */
can_parameter.time_triggered = DISABLE;
can_parameter.auto_bus_off_recovery = DISABLE;
can_parameter.auto_wake_up = DISABLE;
can_parameter.no_auto_retrans = DISABLE;
can_parameter.rec_fifo_overwrite = DISABLE;
can_parameter.trans_fifo_order = DISABLE;
can_parameter.working_mode = CAN_NORMAL_MODE;
can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
can_parameter.time_segment_1 = CAN_BT_BS1_3TQ;
can_parameter.time_segment_2 = CAN_BT_BS2_3TQ;
/* 1MBps */
#if CAN_BAUDRATE == 1000
can_parameter.prescaler = 6;
/* 500KBps */
#elif CAN_BAUDRATE == 500
can_parameter.prescaler = 10;
/* 250KBps */
#elif CAN_BAUDRATE == 250
can_parameter.prescaler = 20;
/* 125KBps */
#elif CAN_BAUDRATE == 125
can_parameter.prescaler = 40;
/* 100KBps */
#elif CAN_BAUDRATE == 100
can_parameter.prescaler = 50;
/* 50KBps */
#elif CAN_BAUDRATE == 50
can_parameter.prescaler = 100;
/* 20KBps */
#elif CAN_BAUDRATE == 20
can_parameter.prescaler = 250;
#else
#error "please select list can baudrate in private defines in main.c "
#endif
can_init(CAN0, &can_parameter);
#if CONFIG_CAN1_EN
/* initialize CAN */
can_init(CAN1, &can_parameter);
#endif
//can1_filter_start_bank(14);
/* initialize filter */
can_filter.filter_number = 0; //CAN0:0->13;CAN1:14->27
can_filter.filter_mode = CAN_FILTERMODE_MASK;
can_filter.filter_bits = CAN_FILTERBITS_32BIT;
can_filter.filter_list_high = 0x0000;//0x3000;
can_filter.filter_list_low = 0x0000;
can_filter.filter_mask_high = 0x0000;//0x3000;
can_filter.filter_mask_low = 0x0000;
can_filter.filter_fifo_number = CAN_FIFO0;
can_filter.filter_enable = ENABLE;
can_filter_init(&can_filter);
#if CONFIG_CAN1_EN
can_filter.filter_fifo_number = CAN_FIFO1;
can_filter.filter_number = 15;
can_filter_init(&can_filter);
#endif
}
开中断是写在别的地方
nvic_irq_enable(CAN1_RX1_IRQn,0,0);
can_interrupt_enable(CAN1, CAN_INT_RFNE1);然后是中断服务函数
void CAN1_RX1_IRQHandler(void)
{
can_receive_message_struct receive_message;
can_message_receive(CAN1, CAN_FIFO1, &receive_message);
}
|
在用回环模式进行调试,一直收不到数据,can0接受用的FIFO0,过滤器0,can0中断可以正常接收。can1用的FIFO1,过滤器16,收不到数据