瑞萨的RA4E2和RA6E2是有CANFD接口的,软件stack归类为CANFD-lite,要使用瑞萨片上的CANFD接口,必须外接CAN收发器,这里我用在淘宝上能买到的常用收发器模块TJA1050,RX接RA4E2和RA6E2的P402引脚,TX接RA6E2的P401引脚:
设置20MHz的外部晶振2分频,16倍频,获得160MHz系统主频,再设置CANFD时钟4分频,获得CANFD时钟40MHz,RA4E2和RA6E2都这么做:
CANFD-lite stack配置,只有通道0可用:
波特率使用默认的配置,向下兼容模式500KHz, FD模式2MHz,采样率75%:
启用MailBox 0发送中断:
启用一个buffer,payload size 8个字节:
两块板的AFL参数都设置相同,ID号示例为0x50:
- const canfd_afl_entry_t p_canfd0_afl[CANFD_CFG_AFL_CH0_RULE_NUM] =
- {
- {
- .id =
- {
- .id = 0x50,
- .frame_type = CAN_FRAME_TYPE_DATA,
- .id_mode = CAN_ID_MODE_STANDARD
- },
- .mask =
- {
- .mask_id = 0x7fe,
- .mask_frame_type = 0,
- .mask_id_mode = 1,
- },
- .destination =
- {
- .minimum_dlc = CANFD_MINIMUM_DLC_0,
- .rx_buffer = CANFD_RX_MB_0,
- },
- },
- };
发送ID号也要用0x50,不然是收不到数据的:
- can_tx_frame.id = 0x50;
- can_tx_frame.id_mode = CAN_ID_MODE_STANDARD;
- can_tx_frame.type = CAN_FRAME_TYPE_DATA;
- can_tx_frame.data_length_code = 1;
- memcpy((uint8_t*)&can_tx_frame.data[0], (uint8_t*)&can_rx_msg1[0], 1);
接收端用的函数为:
- R_CANFD_InfoGet(&g_canfd0_ctrl , &can_rx_info);
- if((can_rx_info.rx_mb_status & (1<<0)) == (1<<0))
- {
- R_CANFD_Read(&g_canfd0_ctrl , 0 , &g_can_rx_frame);
- printf("R_CANFD_Read %s\n" , g_can_rx_frame.data);
- }
运行效果,一切正常,一收一发,没有任何问题: