本帖最后由 --hyk 于 2019-10-28 16:46 编辑
只使用了can0通道,在正常工作模式下,接收正常,发送一直发不出去,显示“显性位错误”,回环模式可以正常工作。下面是初始化程序:
void can_config(void)
{
can_parameter_struct can_parameter={0};
can_filter_parameter_struct can_filterInit={0};
/* initialize CAN register */
can_deinit(CAN0);
can_struct_para_init(CAN_INIT_STRUCT,&can_parameter);
can_debug_freeze_disable(CAN0);
can_parameter.auto_bus_off_recovery = ENABLE;
can_parameter.auto_retrans = DISABLE;
can_parameter.auto_wake_up = ENABLE;
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.prescaler = 20;
can_parameter.time_segment_1 = CAN_BT_BS1_2TQ;
can_parameter.time_segment_2 = CAN_BT_BS2_3TQ;
can_init(CAN0,&can_parameter);
can_filterInit.filter_number = 0;
can_filterInit.filter_mode = CAN_FILTERMODE_MASK;
can_filterInit.filter_bits = CAN_FILTERBITS_32BIT;
can_filterInit.filter_list_high =0x0000;
can_filterInit.filter_list_low = 0x0000;
can_filterInit.filter_mask_high = 0x0000;
can_filterInit.filter_mask_low = 0x0000;
can_filterInit.filter_fifo_number = CAN_FIFO0;
can_filterInit.filter_enable = ENABLE;
can_filter_init(&can_filterInit);
g_transmit_message.tx_efid = 0x01;
g_transmit_message.tx_sfid = CAN0_ID;
g_transmit_message.tx_dlen = 4;
g_transmit_message.tx_ff = CAN_FF_STANDARD;
g_transmit_message.tx_ft = CAN_FT_DATA;
}
/* 监测寄存器 */
uint32_t can_ctl,can_stat,can_tstat,tmi0,tmi1,tmi2,tmp0,tmp1,tmp2,data0,data1,can_err,mailbox_num,bt;
uint32_t num=0;
void can_test(void)
{
uint8_t transmit_mailbox=0;
uint32_t timeout = 0xffff;
num++;
g_transmit_message.tx_data[0] = 0x1;
g_transmit_message.tx_data[1] = 0x2;
g_transmit_message.tx_data[2] = 0x3;
g_transmit_message.tx_data[3] = 0x4;
/* transmit message */
transmit_mailbox=can_message_transmit(CAN0, &g_transmit_message);
while((CAN_TRANSMIT_OK != can_transmit_states(CAN0, transmit_mailbox)) && (0 != timeout)){
timeout--;
}
timeout = 0xFFFF;
while((can_receive_message_length_get(CAN0, CAN_FIFO0) < 1) && (0 != timeout)){
timeout--;
}
can_message_receive(CAN0, CAN_FIFO0, &g_receive_message);
printf("times:%d \n",num);
printf("theTXmsg:%x ",g_transmit_message.tx_data[0]);
printf("theTXmsg:%x ",g_transmit_message.tx_data[1]);
printf("theTXmsg:%x ",g_transmit_message.tx_data[2]);
printf("theTXmsg:%x ",g_transmit_message.tx_data[3]);
printf("\nCAN_ID:%x ",g_receive_message.rx_sfid);
printf("theRXmsg:%x ",g_receive_message.rx_data[0]);
printf("theRXmsg:%x ",g_receive_message.rx_data[1]);
printf("theRXmsg:%x ",g_receive_message.rx_data[2]);
printf("theRXmsg:%x ",g_receive_message.rx_data[3]);
printf("\n\n");
#ifdef ENABLE_MONITOR
mailbox_num=transmit_mailbox;
can_ctl=CAN_CTL(CAN0);
can_stat=CAN_STAT(CAN0);
can_tstat=CAN_TSTAT(CAN0);
can_err=CAN_ERR(CAN0);
tmi0=CAN_TMI0(CAN0);
tmi1=CAN_TMI1(CAN0);
tmi2=CAN_TMI2(CAN0);
tmp0=CAN_TMP0(CAN0);
tmp1=CAN_TMP1(CAN0);
tmp2=CAN_TMP2(CAN0);
bt= CAN_BT(CAN0);
#endif
}
|