学习CAN通信,从GD32官网示例程序改过来了这段代码,使用回环模式正常收发,正常模式不行,求助各位大佬指点一下。
#include "CAN.h"
FlagStatus receive_flag;
ErrStatus CAN_Init_flag;
uint8_t transmit_number = 0x0;
can_trasnmit_message_struct transmit_message;
can_receive_message_struct receive_message;
void can_networking_init(void)
{
can_parameter_struct can_parameter;
can_filter_parameter_struct can_filter;
can_struct_para_init(CAN_INIT_STRUCT, &can_parameter);
can_struct_para_init(CAN_FILTER_STRUCT, &can_filter);
/* initialize CAN register */
can_deinit(CAN0);
/* initialize CAN */
can_parameter.time_triggered = DISABLE;
can_parameter.auto_bus_off_recovery = ENABLE;
can_parameter.auto_wake_up = DISABLE;
can_parameter.auto_retrans = DISABLE;
can_parameter.rec_fifo_overwrite = DISABLE;
can_parameter.trans_fifo_order = DISABLE;
can_parameter.working_mode = CAN_NORMAL_MODE; //CAN_NORMAL_MODE, CAN_LOOPBACK_MODE, CAN_SILENT_MODE, CAN_SILENT_LOOPBACK_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;
/* baudrate 250Mbps */
can_parameter.prescaler = 12;
CAN_Init_flag = can_init(CAN0, &can_parameter);
if(CAN_Init_flag == SUCCESS)
printf("can_init successful\r\n");
else
printf("can_init error\r\n");
/* 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 = 0x0000U;
can_filter.filter_list_low = 0x0000U;
can_filter.filter_mask_high = 0x0000U;
can_filter.filter_mask_low = 0x0000U;
can_filter.filter_fifo_number = CAN_FIFO1;
can_filter.filter_enable = ENABLE;
can_filter_init(&can_filter);
}
/*!
\brief configure GPIO
\param[in] none
\param[out] none
\retval none
*/
void can_gpio_config(void)
{
/* enable CAN clock */
rcu_periph_clock_enable(RCU_CAN0);
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_AF);
/* configure CAN0 GPIO */
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_nvic_config(void)
{
/* configure CAN0 NVIC */
nvic_irq_enable(CAN0_RX1_IRQn,1,1);
}
void CAN_Init(void)
{
can_gpio_config();
can_nvic_config();
can_networking_init();
/* enable CAN receive FIFO1 not empty interrupt */
can_interrupt_enable(CAN0, CAN_INT_RFNE1);
}
|