GD32 can发送报no mailbox 故障

1、问题描述

在使用GD32can通信时,发现如果can线断开了,再把can线连上,用can分析仪监测不到总线数据,调试后发现can发送没有成功,通过串口打印出错误,显示是没有邮箱。

2、问题解决

这个问题的原因是没有没有使能重新上线自动连接的功能。

can_parameter.auto_bus_off_recovery = ENABLE;

打开这个功能后,就正常。

另外,要使用can1,必须要初始化can0才行。

GD307的can初始化代码如下:

void init_can0(void)
{
    can_parameter_struct can_parameter;
    can_filter_parameter_struct can_filter;

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

    /* initialize CAN */
    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_6TQ;
    can_parameter.time_segment_2 = CAN_BT_BS2_3TQ;
    /* baudrate 1Mbps */
    can_parameter.prescaler = 6;
    can_init(CAN0, &can_parameter);

    /* initialize filter */
    /* CAN0 filter number */
    can_filter.filter_number = 0;

    /* initialize filter */    
    can_filter.filter_mode = CAN_FILTERMODE_MASK;
    can_filter.filter_bits = CAN_FILTERBITS_32BIT;
    can_filter.filter_list_high = 0x0000;
    can_filter.filter_list_low = 0x0000;
    can_filter.filter_mask_high = 0x0000;
    can_filter.filter_mask_low = 0x0000;  
    can_filter.filter_fifo_number = CAN_FIFO0;
    can_filter.filter_enable = ENABLE;
    can_filter_init(&can_filter);

    nvic_irq_enable(CAN0_RX0_IRQn, 10, 0);
    /* enable CAN receive FIFO0 not empty interrupt */
    can_interrupt_enable(CAN0, CAN_INTEN_RFNEIE0);
}

/**
 * @description: ӫƁĻǽ¶¯°卨Ѕ
 * @param {*}
 * @return {*}
 */
void init_can1(void)
{
    can_parameter_struct can_parameter;
    can_filter_parameter_struct can_filter;

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

    /* initialize CAN */
    can_parameter.time_triggered = DISABLE;
    can_parameter.auto_bus_off_recovery = ENABLE;
    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_6TQ;
    can_parameter.time_segment_2 = CAN_BT_BS2_3TQ;
    /* baudrate 1Mbps */
    can_parameter.prescaler = 6;
    can_init(CAN1, &can_parameter);

    /* initialize filter */
    /* CAN0 filter number */
    can_filter.filter_number = 14;

    /* initialize filter */    
    can_filter.filter_mode = CAN_FILTERMODE_MASK;
    can_filter.filter_bits = CAN_FILTERBITS_32BIT;
    can_filter.filter_list_high = 0x0000;
    can_filter.filter_list_low = 0x0000;
    can_filter.filter_mask_high = 0x0000;
    can_filter.filter_mask_low = 0x0000;  
    can_filter.filter_fifo_number = CAN_FIFO1;
    can_filter.filter_enable = ENABLE;
    can_filter_init(&can_filter);

    nvic_irq_enable(CAN1_RX1_IRQn, 5, 0);
    /* enable CAN receive FIFO0 not empty interrupt */
    can_interrupt_enable(CAN1, CAN_INTEN_RFNEIE1);
}

你可能感兴趣的:(单片机,stm32,嵌入式硬件,GD32,can)