GD32F103C8,测试CAN通信,用CAN总线分析仪测试,发现收发异常。用回环模式测试,发现发送一直处于pending。
void RCC_Configuration(void)
{
SystemInit();
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_GPIOB);
rcu_periph_clock_enable(RCU_AF);
rcu_periph_clock_enable(RCU_CAN0);
rcu_periph_clock_enable(RCU_TIMER0);
rcu_periph_clock_enable(RCU_TIMER1);
rcu_periph_clock_enable(RCU_TIMER2);
rcu_periph_clock_enable(RCU_TIMER3);
}
void GPIO_Configuration(void)
{
//LED
gpio_init(LED_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, LED_PIN);
//CAN
gpio_init(GPIOA,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_11);
gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_12);
}
void CAN_Configuration(void)
{
can_parameter_struct can_parameter={0};
can_filter_parameter_struct can_filter={0};
can_struct_para_init(CAN_INIT_STRUCT, &can_parameter);
can_struct_para_init(CAN_INIT_STRUCT, &can_filter);
can_deinit(CAN0);
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_LOOPBACK_MODE;//CAN_NORMAL_MODE;
can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
can_parameter.time_segment_1 = CAN_BT_BS1_5TQ;
can_parameter.time_segment_2 = CAN_BT_BS2_3TQ;
can_parameter.prescaler = 24;//250k
/* initialize CAN */
can_init(CAN0, &can_parameter);
/* initialize filter */
can_filter.filter_number=0;
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);
can_interrupt_enable(CAN0, CAN_INT_RFNE0);
}
int main(void)
{
can_trasnmit_message_struct transmit_message;
uint32_t timeout = 0xFFFF;
uint8_t transmit_mailbox = 0;
char *buffer="Hello!!!";
unsigned char flag=0;
unsigned char error=0;
uint32_t clk=0;
RCC_Configuration();
GPIO_Configuration();
Timer_Configuration();
NVIC_Configuration();
transmit_message.tx_sfid = 0;
transmit_message.tx_efid = 0x00;
transmit_message.tx_ft = CAN_FT_DATA;
transmit_message.tx_ff = CAN_FF_STANDARD;
transmit_message.tx_dlen = 2;
transmit_message.tx_data[0]=0x5A;
transmit_message.tx_data[1]=0xA5;
while(1)
{
fwdgt_counter_reload();
if(SYS_TICK & g_cCMDFlag)
{
g_cCMDFlag&=~SYS_TICK;
flag=1-flag;
if(flag)
gpio_bit_write(LED_PORT,LED_PIN,SET);
else
gpio_bit_write(LED_PORT,LED_PIN,RESET);
timeout = 0xFFFF;
transmit_mailbox = can_message_transmit(CAN0, &transmit_message);
while((CAN_TRANSMIT_OK != can_transmit_states(CAN0, transmit_mailbox)) && (0 != timeout))
{
timeout--;
}
error=can_error_get(CAN0);
error=can_transmit_error_number_get(CAN0);
}
}
return 0;
}
用can_error_get(CAN0)和can_transmit_error_number_get(CAN0),返回值都是0,判断不出原因。
在另外一块GD32F190C8板子上,回环模式能正常收发,切换到正常工作模式,收发有问题,发送会报错。 |