刚接触GD32,芯片是GD32F303RBT6。回环通讯可以,但两块板子之间无法通讯。现改为一块板子发送,通过CAN分析仪发送到PC端,这种情况比较简化一些,但PC端就是收不到数据。CAN控制器型号:NSi1050。用示波器看CANH和CANL之间的波形,波形就不对。折腾好几天没个结果,想到论坛里一定有很多熟悉GD32CAN通讯的朋友,就到这里来请教了。附上代码和图片。
int main(void)
{
receive_flag = RESET;
systick_config();
/* configure GPIO */
gpio_config();
/* configure NVIC */
nvic_config();
/* initialize CAN */
can_networking_init();
/* enable CAN receive FIFO1 not empty interrupt */
can_interrupt_enable(CAN0, CAN_INT_RFNE1);
/* initialize transmit message */
can_struct_para_init(CAN_TX_MESSAGE_STRUCT, &transmit_message);
transmit_message.tx_sfid = 0x321;
transmit_message.tx_efid = 0x01;
transmit_message.tx_ft = CAN_FT_DATA;
transmit_message.tx_ff = CAN_FF_STANDARD;
transmit_message.tx_dlen = 1;
/* initialize receive message */
can_struct_para_init(CAN_RX_MESSAGE_STRUCT, &receive_message);
while(1)
{
delay_1ms(500);
/* if transmit_number is 0x10, set it to 0x00 */
if(transmit_number == 0x10)
{
transmit_number = 0x00;
}else
{
transmit_message.tx_data[0] = transmit_number++;
/* transmit message */
can_message_transmit(CAN0, &transmit_message);
}
if(SET == receive_flag)
{
receive_flag = RESET;
}
}
}
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(CANX);
/* initialize CAN */
can_parameter.time_triggered = DISABLE;
can_parameter.auto_bus_off_recovery = DISABLE;
can_parameter.auto_wake_up = DISABLE;
can_parameter.auto_retrans = 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.time_segment_1 = CAN_BT_BS1_6TQ; //5
can_parameter.time_segment_2 = CAN_BT_BS2_3TQ; //4
/* baudrate 1Mbps 125k*/
can_parameter.prescaler = 48;//6
can_init(CANX, &can_parameter);//CANX=CAN0 #define CANX CAN0
/* initialize filter */
#ifdef CAN0_USED
/* CAN0 filter number */
can_filter.filter_number = 0;
#else
/* CAN1 filter number */
can_filter.filter_number = 15;
#endif
/* initialize filter */
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_FIFO1;
can_filter.filter_enable = ENABLE;
can_filter_init(&can_filter);
}
void gpio_config(void)
{
/* enable CAN clock */
rcu_periph_clock_enable(RCU_CAN0);
#ifdef GD32F30X_CL /*GD32F30X_CL 未定义*/
rcu_periph_clock_enable(RCU_CAN1);
rcu_periph_clock_enable(RCU_GPIOB);
#endif
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_AF);
/* configure CAN0 GPIO */
gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_12); //CAN_TX
gpio_init(GPIOA,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_11); //CAN_RX
// gpio_pin_remap_config(GPIO_CAN0_FULL_REMAP,ENABLE);
gpio_pin_remap_config(GPIO_CAN_FULL_REMAP,ENABLE); //debug
#ifdef GD32F30X_CL
/* configure CAN1 GPIO */
gpio_init(GPIOB,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_5);
gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_6);
gpio_pin_remap_config(GPIO_CAN1_REMAP,ENABLE);
#endif
}
void nvic_config(void)
{
#ifdef CAN0_USED /*CAN0_USED 定义了的*/
/* configure CAN0 NVIC */
nvic_irq_enable(CAN0_RX1_IRQn,0,0);
#else
/* configure CAN1 NVIC */
nvic_irq_enable(CAN1_RX1_IRQn,0,0);
#endif
}
void can_interrupt_enable(uint32_t can_periph, uint32_t interrupt)
{
CAN_INTEN(can_periph) |= interrupt;
}
void can_struct_para_init(can_struct_type_enum type, void* p_struct)
{
uint8_t i;
if(NULL == p_struct){
CAN_ERROR_HANDLE("struct parameter can not be NULL \r\n");
}
/* get type of the struct */
switch(type){
/* used for can_init() */
case CAN_INIT_STRUCT:
((can_parameter_struct*)p_struct)->auto_bus_off_recovery = DISABLE;
((can_parameter_struct*)p_struct)->auto_retrans = DISABLE;
((can_parameter_struct*)p_struct)->auto_wake_up = DISABLE;
((can_parameter_struct*)p_struct)->prescaler = 0x03FFU;
((can_parameter_struct*)p_struct)->rec_fifo_overwrite = DISABLE;
((can_parameter_struct*)p_struct)->resync_jump_width = CAN_BT_SJW_1TQ;
((can_parameter_struct*)p_struct)->time_segment_1 = CAN_BT_BS1_3TQ;
((can_parameter_struct*)p_struct)->time_segment_2 = CAN_BT_BS2_1TQ;
((can_parameter_struct*)p_struct)->time_triggered = DISABLE;
((can_parameter_struct*)p_struct)->trans_fifo_order = DISABLE;
((can_parameter_struct*)p_struct)->working_mode = CAN_NORMAL_MODE;
break;
/* used for can_filter_init() */
case CAN_FILTER_STRUCT:
((can_filter_parameter_struct*)p_struct)->filter_bits = CAN_FILTERBITS_32BIT;
((can_filter_parameter_struct*)p_struct)->filter_enable = DISABLE;
((can_filter_parameter_struct*)p_struct)->filter_fifo_number = CAN_FIFO0;
((can_filter_parameter_struct*)p_struct)->filter_list_high = 0x0000U;
((can_filter_parameter_struct*)p_struct)->filter_list_low = 0x0000U;
((can_filter_parameter_struct*)p_struct)->filter_mask_high = 0x0000U;
((can_filter_parameter_struct*)p_struct)->filter_mask_low = 0x0000U;
((can_filter_parameter_struct*)p_struct)->filter_mode = CAN_FILTERMODE_MASK;
((can_filter_parameter_struct*)p_struct)->filter_number = 0U;
break;
/* used for can_message_transmit() */
case CAN_TX_MESSAGE_STRUCT:
for(i = 0U; i < 8U; i++){
((can_trasnmit_message_struct*)p_struct)->tx_data[i] = 0U;
}
((can_trasnmit_message_struct*)p_struct)->tx_dlen = 0u;
((can_trasnmit_message_struct*)p_struct)->tx_efid = 0U;
((can_trasnmit_message_struct*)p_struct)->tx_ff = (uint8_t)CAN_FF_STANDARD;
((can_trasnmit_message_struct*)p_struct)->tx_ft = (uint8_t)CAN_FT_DATA;
((can_trasnmit_message_struct*)p_struct)->tx_sfid = 0U;
break;
/* used for can_message_receive() */
case CAN_RX_MESSAGE_STRUCT:
for(i = 0U; i < 8U; i++){
((can_receive_message_struct*)p_struct)->rx_data[i] = 0U;
}
((can_receive_message_struct*)p_struct)->rx_dlen = 0U;
((can_receive_message_struct*)p_struct)->rx_efid = 0U;
((can_receive_message_struct*)p_struct)->rx_ff = (uint8_t)CAN_FF_STANDARD;
((can_receive_message_struct*)p_struct)->rx_fi = 0U;
((can_receive_message_struct*)p_struct)->rx_ft = (uint8_t)CAN_FT_DATA;
((can_receive_message_struct*)p_struct)->rx_sfid = 0U;
break;
default:
CAN_ERROR_HANDLE("parameter is invalid \r\n");
}
}
E:\controller\杂项\*.* |