打印

GD32C103CBT6的CAN1工作不正常

[复制链接]
5907|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
hInstance|  楼主 | 2024-6-2 21:22 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
沙发
hInstance|  楼主 | 2024-6-2 21:44 | 只看该作者
用于发送数据缓冲的发送结构体
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初始化之前已经全部打开,中断分组也配置了

使用特权

评论回复
板凳
imsapp| | 2024-6-2 23:11 | 只看该作者
没细看, 不过remap不要注释掉, 写好DISABLE
gpio_pin_remap_config(GPIO_CAN1_REMAP, DISABLE);

使用特权

评论回复
地板
cooldog123pp| | 2024-6-3 09:24 | 只看该作者
好像,之前好几个坛友反应了GD32的can有问题,可以看下之前的兄弟怎么是怎么解决的。

使用特权

评论回复
5
dirtwillfly| | 2024-7-5 16:13 | 只看该作者
和另一个帖子好像对应起来了
https://bbs.21ic.com/icview-3381510-1-1.html

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

4

帖子

0

粉丝