1 问题描述
GD32的CAN1无法进入接收中断,收不到数据。
注:MCU使用的是GD32E50x,其他型号不确定是否一样,本文只以GD32E50x举例说明。
2 原因分析
GD32的CAN过滤器总共有28个,通过过滤器控制寄存器(CAN_FCTL)打开或关闭,具体如下:
其中问题就出现在上图的HBC1F上面,CAN0使用的是过滤器编号0到编号(HBC1F-1),CAN1使用的是过滤器编号(HBC1F)到27,而这个默认值是0xE(14),也就是说CAN0默认使用的是编号为0-13的过滤器,CAN1默认使用的是编号为0-27的过滤器,因此,在初始化CAN1的时候,过滤器编号要在这个范围内才能被正确使用,否则是接收不到CAN数据的。
3 解决办法
1、使用编号为15-28的过滤器
2、通过修改CAN_FCTL寄存器的HBC1F,调整过滤器编号的分配
其中GD32固件库有封装函数可以修改HBC1F,函数原形如下:
/*!
\brief set CAN1 fliter start bank number
\param[in] start_bank: CAN1 start bank number
only one parameter can be selected which is shown as below:
\arg (1..27)
\param[out] none
\retval none
*/
void can1_filter_start_bank(uint8_t start_bank)
{
/* filter lock disable */
CAN_FCTL(CAN0) |= CAN_FCTL_FLD;
/* set CAN1 filter start number */
CAN_FCTL(CAN0) &= ~(uint32_t)CAN_FCTL_HBC1F;
CAN_FCTL(CAN0) |= FCTL_HBC1F(start_bank);
/* filter lock enable */
CAN_FCTL(CAN0) &= ~CAN_FCTL_FLD;
}
4 CAN配置参考代码
#include "main.h"
#include "stdio.h"
can_trasnmit_message_struct can0_tx_message;
can_trasnmit_message_struct can1_tx_message;
can_receive_message_struct can0_rx_message;
can_receive_message_struct can1_rx_message;
void can0_gpio_config(void)
{
/* enable CAN0 clock */
rcu_periph_clock_enable(RCU_CAN0);
rcu_periph_clock_enable(RCU_GPIOB);
rcu_periph_clock_enable(RCU_AF);
/* configure CAN0 GPIO */
gpio_init(GPIOB, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_8);
gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
gpio_pin_remap_config(GPIO_CAN0_PARTIAL_REMAP, ENABLE);
}
void can0_config(void)
{
can_parameter_struct can_parameter;
can_fdframe_struct can_fd_parameter;
can_fd_tdc_struct can_fd_tdc_parameter;
can_filter_parameter_struct can_filter;
can_struct_para_init(CAN_INIT_STRUCT, &can_parameter);
/* initialize CAN register */
can_deinit(CAN0);
#if 1 // CAN配置
/* 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 = DISABLE;
can_parameter.trans_fifo_order = DISABLE;
can_parameter.working_mode = CAN_NORMAL_MODE;
/* CAN波特率 = 时钟频率 / 分频系数 / (1 + TSG1 + TSG2) */
/* 仲裁段波特率 = 90M / 9 / (1 + 7 + 2) = 1M */
/* configure CAN baud rate 1MBps, sample point at 80% */
can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
can_parameter.time_segment_1 = CAN_BT_BS1_7TQ; // TSG1
can_parameter.time_segment_2 = CAN_BT_BS2_2TQ; // TSG2
can_parameter.prescaler = 9U; // 分频系数
/* initialize CAN */
can_init(CAN0, &can_parameter);
#else // CANFD配置
/* 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 = ENABLE;
can_parameter.rec_fifo_overwrite = ENABLE;
can_parameter.trans_fifo_order = ENABLE;
can_parameter.working_mode = CAN_NORMAL_MODE;
/* baudrate 1Mbps, sample piont at 80% */
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 = 9U;
/* initialize CAN */
can_init(CAN0, &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 = 0x04U;
can_fd_tdc_parameter.tdc_mode = CAN_TDCMOD_CALC_AND_OFFSET;
can_fd_tdc_parameter.tdc_offset = 0x04U;
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;
/* CAN波特率 = 时钟频率 / 分频系数 / (1 + TSG1 + TSG2) */
/* 数据段波特率 = 90M / 3 / (1 + 4 + 1) = 5M */
can_fd_parameter.data_resync_jump_width = CAN_BT_SJW_1TQ;
can_fd_parameter.data_time_segment_1 = CAN_BT_BS1_4TQ; // TSG1
can_fd_parameter.data_time_segment_2 = CAN_BT_BS2_1TQ; // TSG2
/* CAN-FD data segement prescaler should be the same as non-data segement prescaler */
can_fd_parameter.data_prescaler = 3; // 分频系数
/* initialize CAN-FD */
can_fd_init(CAN0, &can_fd_parameter);
#endif
/* initialize filter */
/* configure filter mode */
can_filter.filter_mode = CAN_FILTERMODE_MASK;
can_filter.filter_bits = CAN_FILTERBITS_32BIT;
/* configure filter ID */
can_filter.filter_list_high = 0x0000U;
can_filter.filter_list_low = 0x0000U;
/* configure filter mask */
can_filter.filter_mask_high = 0x0000U;
can_filter.filter_mask_low = 0x0000U;
/* select receiver fifo */
can_filter.filter_fifo_number = CAN_FIFO0;
can_filter.filter_number = 0U;
can_filter.filter_enable = ENABLE;
can_filter_init(CAN0, &can_filter);
/* configure CAN0 NVIC */
nvic_irq_enable(CAN0_RX0_IRQn, 0U, 0U);
/* enable can receive FIFO0 not empty interrupt */
can_interrupt_enable(CAN0, CAN_INTEN_RFNEIE0);
}
int can0_send_message(void)
{
uint8_t i;
uint16_t id = 1;
can0_tx_message.tx_sfid = id;
can0_tx_message.fd_flag = 0;
can0_tx_message.fd_brs = 0;
can0_tx_message.fd_esi = 0;
can0_tx_message.tx_dlen = 8;
for(i = 0; i < 8; i++)
{
can0_tx_message.tx_data = i;
}
#if 0
printf("\r\n can0 transmit data(id: 0x%x): ", can0_tx_message.tx_sfid);
for(i = 0U; i < can0_tx_message.tx_dlen; i++)
{
printf(" %02x", can0_tx_message.tx_data);
}
#endif
/* transmit message */
if(can_message_transmit(CAN0, &can0_tx_message) != CAN_NOMAILBOX)
{
return 1;
}
return 0;
}
void CAN0_RX0_IRQHandler(void)
{
/* check the receive message */
can_message_receive(CAN0, CAN_FIFO0, &can0_rx_message);
LOG("\r\n can0 receive(id=0x%X) data: ", can0_rx_message.rx_sfid);
for(int i = 0U; i < can0_rx_message.rx_dlen; i++)
{
LOG(" %02x", can0_rx_message.rx_data);
}
}
void can1_gpio_config(void)
{
/* enable CAN1 clock */
rcu_periph_clock_enable(RCU_CAN1);
rcu_periph_clock_enable(RCU_GPIOB);
rcu_periph_clock_enable(RCU_AF);
/* 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);
}
void can1_config(void)
{
can_parameter_struct can_parameter;
can_fdframe_struct can_fd_parameter;
can_fd_tdc_struct can_fd_tdc_parameter;
can_filter_parameter_struct can_filter;
can_struct_para_init(CAN_INIT_STRUCT, &can_parameter);
/* initialize CAN register */
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 = ENABLE; // TODO 自动重传是否需要使能
can_parameter.rec_fifo_overwrite = ENABLE;
can_parameter.trans_fifo_order = ENABLE;
can_parameter.working_mode = CAN_NORMAL_MODE;
/* CAN波特率 = 时钟频率 / 分频系数 / (1 + TSG1 + TSG2) */
/* 仲裁段波特率 = 90M / 9 / (1 + 7 + 2) = 1M */
/* baudrate 1Mbps, sample piont at 80% */
can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
can_parameter.time_segment_1 = CAN_BT_BS1_7TQ; // TSG1
can_parameter.time_segment_2 = CAN_BT_BS2_2TQ; // TSG2
can_parameter.prescaler = 9U; // 分频系数
/* initialize CAN */
can_init(CAN1, &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 = 0x04U;
can_fd_tdc_parameter.tdc_mode = CAN_TDCMOD_CALC_AND_OFFSET;
can_fd_tdc_parameter.tdc_offset = 0x04U;
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;
/* CAN波特率 = 时钟频率 / 分频系数 / (1 + TSG1 + TSG2) */
/* 数据段波特率 = 90M / 3 / (1 + 4 + 1) = 5M */
can_fd_parameter.data_resync_jump_width = CAN_BT_SJW_1TQ;
can_fd_parameter.data_time_segment_1 = CAN_BT_BS1_4TQ; // TSG1
can_fd_parameter.data_time_segment_2 = CAN_BT_BS2_1TQ; // TSG2
/* CAN-FD data segement prescaler should be the same as non-data segement prescaler */
can_fd_parameter.data_prescaler = 3; // 分频系数
/* initialize CAN-FD */
can_fd_init(CAN1, &can_fd_parameter);
/* initialize filter */
/* configure filter mode */
can_filter.filter_mode = CAN_FILTERMODE_MASK;
can_filter.filter_bits = CAN_FILTERBITS_32BIT;
/* configure filter ID */
can_filter.filter_list_high = 0x0000U;
can_filter.filter_list_low = 0x0000U;
/* configure filter mask */
can_filter.filter_mask_high = 0x0000U;
can_filter.filter_mask_low = 0x0000U;
/* select receiver fifo */
can_filter.filter_fifo_number = CAN_FIFO1;
can_filter.filter_number = 15U; // CAN_FCTL默认定义了CAN0和CAN1过滤器序号的分配数量,CAN0使用0-13序号,CAN1使用14-27,可以通过can1_filter_start_bank()修改
can_filter.filter_enable = ENABLE;
can_filter_init(CAN1, &can_filter);
/* configure CAN1 NVIC */
nvic_irq_enable(CAN1_RX1_IRQn, 1U, 1U);
/* enable can receive FIFO0 not empty interrupt */
can_interrupt_enable(CAN1, CAN_INTEN_RFNEIE1);
}
int can1_send_message(void)
{
uint8_t i;
uint16_t id = 2;
can1_tx_message.tx_sfid = id;
can1_tx_message.fd_flag = 1;
can1_tx_message.fd_brs = 1;
can1_tx_message.fd_esi = 0;
can1_tx_message.tx_dlen = 8;
for(i = 0; i < 8; i++)
{
can1_tx_message.tx_data = i;
}
#if 0
printf("\r\n can1 transmit data(id: 0x%x): ", can1_tx_message.tx_sfid);
for(i = 0U; i < can1_tx_message.tx_dlen; i++)
{
printf(" %02x", can1_tx_message.tx_data);
}
#endif
/* transmit message */
if(can_message_transmit(CAN1, &can1_tx_message) != CAN_NOMAILBOX)
{
return 1;
}
return 0;
}
void CAN1_RX1_IRQHandler(void)
{
/* check the receive message */
can_message_receive(CAN1, CAN_FIFO1, &can1_rx_message);
printf("\r\n can1 receive(id=0x%X) data: ", can1_rx_message.rx_sfid);
for(int i = 0U; i < can1_rx_message.rx_dlen; i++)
{
printf(" %02x", can1_rx_message.rx_data);
}
}
void can_user_init(void)
{
// CAN0 init
can0_gpio_config();
can0_config();
/* initialize can0 transmit message */
can_struct_para_init(CAN_TX_MESSAGE_STRUCT, &can0_tx_message);
/* initialize can0 receive message */
can_struct_para_init(CAN_RX_MESSAGE_STRUCT, &can0_rx_message);
// CAN1 init
can1_gpio_config();
can1_config();
/* initialize can1 transmit message */
can_struct_para_init(CAN_TX_MESSAGE_STRUCT, &can1_tx_message);
/* initialize can1 receive message */
can_struct_para_init(CAN_RX_MESSAGE_STRUCT, &can1_rx_message);
printf("can init success\r\n");
}
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/ShenZhen_zixian/article/details/141052725
|
|