炯哈哈 2024-08-05 16:30 采纳率: 28.6%
浏览 25

GD32F470VE芯片,初始化两个CAN口之后,CAN0的中断可用,CAN1的中断不可以

在FreeRTOS系统下,GPIO口已正确初始化,下面这段代码是初始化CAN0和CAN1的程序。

can_parameter_struct can_init_parameter;
    can_filter_parameter_struct can_filter_parameter;

    can_struct_para_init(CAN_INIT_STRUCT, &can_init_parameter);
    can_struct_para_init(CAN_FILTER_STRUCT, &can_filter_parameter);

    /* initialize CAN register */
    can_deinit(CAN0);
    can_deinit(CAN1);

    /* initialize CAN parameters */
    can_init_parameter.time_triggered = DISABLE;
    can_init_parameter.auto_bus_off_recovery = DISABLE;
    can_init_parameter.auto_wake_up = DISABLE;
    can_init_parameter.auto_retrans = ENABLE;
    can_init_parameter.rec_fifo_overwrite = DISABLE;
    can_init_parameter.trans_fifo_order = DISABLE;
    can_init_parameter.working_mode = CAN_NORMAL_MODE;
    can_init_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
    can_init_parameter.time_segment_1 = CAN_BT_BS1_7TQ;
    can_init_parameter.time_segment_2 = CAN_BT_BS2_2TQ;

    /* 1MBps */
#if CAN_BAUDRATE == 1000
    can_init_parameter.prescaler = 5;
    /* 500KBps */
#elif CAN_BAUDRATE == 500
    can_init_parameter.prescaler = 10;
    /* 250KBps */
#elif CAN_BAUDRATE == 250
    can_init_parameter.prescaler = 20;
    /* 125KBps */
#elif CAN_BAUDRATE == 125
    can_init_parameter.prescaler = 40;
    /* 100KBps */
#elif  CAN_BAUDRATE == 100
    can_init_parameter.prescaler = 50;
    /* 50KBps */
#elif  CAN_BAUDRATE == 50
    can_init_parameter.prescaler = 100;
    /* 20KBps */
#elif  CAN_BAUDRATE == 20
    can_init_parameter.prescaler = 250;
#else
    //#error "please select list can baudrate in private defines in main.c "
#endif  
    /* initialize CAN */
    can_init(CAN0, &can_init_parameter);
    can_init(CAN1, &can_init_parameter);

    /* initialize filter */
    can_filter_parameter.filter_number = 0;
    can_filter_parameter.filter_mode = CAN_FILTERMODE_MASK;
    can_filter_parameter.filter_bits = CAN_FILTERBITS_32BIT;
    can_filter_parameter.filter_list_high = 0;
    can_filter_parameter.filter_list_low = 0;
    can_filter_parameter.filter_mask_high = 0;
    can_filter_parameter.filter_mask_low = 0;
    can_filter_parameter.filter_fifo_number = CAN_FIFO0;
    can_filter_parameter.filter_enable = ENABLE;

    can_filter_init(&can_filter_parameter);
    can_filter_parameter.filter_number = 15;
    can_filter_init(&can_filter_parameter);

    /* enable can receive FIFO0 not empty interrupt */
    can_interrupt_enable(CAN0, CAN_INT_RFNE0);
    can_interrupt_enable(CAN1, CAN_INT_RFNE0);

使能中断

    NVIC_SetPriority(CAN0_RX0_IRQn, 3);
    NVIC_EnableIRQ(CAN0_RX0_IRQn);

    NVIC_SetPriority(CAN1_RX0_IRQn, 4);
    NVIC_EnableIRQ(CAN1_RX0_IRQn);

问题:CAN0的中断可以正常进入,CAN1的中断无法执行

  • 写回答

1条回答 默认 最新

  • 码农阿豪@新空间代码工作室 Java领域优质创作者 2024-08-06 08:54
    关注
    让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
    在这段代码中,虽然已经使能了CAN1的中断并设置了中断优先级,但在初始化CAN1时,使用了相同的参数结构体`can_init_parameter`和`can_filter_parameter`。这样做会导致CAN0和CAN1的初始化参数和过滤器参数相同,可能会导致CAN0和CAN1之间的冲突,进而导致CAN1的中断无法执行。 解决方法是为CAN1使用独立的参数结构体和过滤器参数结构体,确保CAN0和CAN1之间的参数独立设置。以下是修改后的代码示例:
    can_parameter_struct can_init_parameter_0;    
    can_filter_parameter_struct can_filter_parameter_0;
    can_parameter_struct can_init_parameter_1;    
    can_filter_parameter_struct can_filter_parameter_1;
    can_struct_para_init(CAN_INIT_STRUCT, &can_init_parameter_0);    
    can_struct_para_init(CAN_FILTER_STRUCT, &can_filter_parameter_0);
    can_struct_para_init(CAN_INIT_STRUCT, &can_init_parameter_1);    
    can_struct_para_init(CAN_FILTER_STRUCT, &can_filter_parameter_1);
    /* initialize CAN register */    
    can_deinit(CAN0);    
    can_deinit(CAN1);
    /* initialize CAN0 parameters */    
    can_init_parameter_0.time_triggered = DISABLE;    
    // Set other CAN0 parameters
    /* initialize CAN1 parameters */    
    can_init_parameter_1.time_triggered = DISABLE;    
    // Set other CAN1 parameters
    /* Initialize CAN */
    can_init(CAN0, &can_init_parameter_0);    
    can_init(CAN1, &can_init_parameter_1);
    /* initialize filter for CAN0 */    
    can_filter_parameter_0.filter_number = 0;    
    // Set other CAN0 filter parameters
    can_filter_init(&can_filter_parameter_0);    
    /* initialize filter for CAN1 */    
    can_filter_parameter_1.filter_number = 0;    
    // Set other CAN1 filter parameters
    can_filter_init(&can_filter_parameter_1);
    /* enable can receive FIFO0 not empty interrupt */    
    can_interrupt_enable(CAN0, CAN_INT_RFNE0);    
    can_interrupt_enable(CAN1, CAN_INT_RFNE0);
    /* Enable NVIC interrupt for CAN0 and CAN1 */    
    NVIC_SetPriority(CAN0_RX0_IRQn, 3);    
    NVIC_EnableIRQ(CAN0_RX0_IRQn);    
    NVIC_SetPriority(CAN1_RX0_IRQn, 4);    
    NVIC_EnableIRQ(CAN1_RX0_IRQn);
    

    通过以上修改,两个CAN节点的初始化参数和过滤器参数分别独立设置,可以避免相互冲突,从而保证CAN1的中断能够正常执行。

    评论

报告相同问题?

问题事件

  • 创建了问题 8月5日

悬赏问题

  • ¥60 QQOP数据,什么是op数据号,怎么提取op数据!能不能大量提取(语言-c语言)
  • ¥15 matlab代码 关于微分方程和嵌套的分段函数。
  • ¥15 把VMware项目复制到另一台电脑
  • ¥15 onlyoffice编辑完后立即下载,下载的不是最新编辑的文档
  • ¥15 求caverdock使用教程
  • ¥15 Coze智能助手搭建过程中的问题请教
  • ¥15 12864只亮屏 不显示汉字
  • ¥20 三极管1000倍放大电路
  • ¥15 vscode报错如何解决
  • ¥15 前端vue CryptoJS Aes CBC加密后端java解密