问答

汇集网友智慧,解决技术难题

21ic问答首页 - GD32C103CBT6的CAN1工作不正常

TI 脉冲 直接 MM GD32C103 CAN

GD32C103CBT6的CAN1工作不正常

hInstance2024-06-02
参考官方例程communication_FDmode,CAN1_TX只会出现一两个低脉冲。直接用例程去掉按键、加个50ms延时周期发送也是一样的波形.


回答 +关注 9
5613人浏览 4人回答问题 分享 举报
4 个回答
  • 和另一个帖子好像对应起来了
    https://bbs.21ic.com/icview-3381510-1-1.html
  • 好像,之前好几个坛友反应了GD32的can有问题,可以看下之前的兄弟怎么是怎么解决的。
  • 没细看, 不过remap不要注释掉, 写好DISABLE
    gpio_pin_remap_config(GPIO_CAN1_REMAP, DISABLE);
  • 用于发送数据缓冲的发送结构体
    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初始化之前已经全部打开,中断分组也配置了

您需要登录后才可以回复 登录 | 注册