在前一个shell终端测试的基础上,本次完成CAN总线接口的收发测试,采用e2studio基于fsp的架构,测试起来还是相当顺利的。
通过数据手册了解到,R7FA4M2AD3CFP具备一组CAN总线接口,其占用的控制器引脚分别为P102、P103和P402、P401两组。这里选择P102和P103这组引脚进行测试,其中P102为CRX0,P103为CTX0。
如下图所示,这两引脚在J4的1脚和J3的7脚。
在FSP的Pin Configuration选项卡中,选择Peripherals->Connectivity:CAN->CAN0,并如下图所示,配置为
Operation Mode: Enable
CRX0 : P102
CTX0 : P103
如果发现P102和P103被占用无法选择,需要将被占用的功能关闭。
切换到Stacks选项页,选择New Stack->Connectivity->CAN(r_can),添加一个CAN口g_can0 CAN(r_can)。
点击新增的CAN控件,编辑Properties,如下标所示。我这里分配了4 Mailboxes,其中
前三个为发送邮箱,第4个为接收邮箱,并且将Mailbox 0-3 Group Mask设置为0,取消ID过滤功能。
波特率选择 125000(125K);中断回调函数设置为can_callback 。
增加CAN中断回调函数,处理发送完成和接收完成两个事件。
void can_callback (can_callback_args_t * p_args)
{
if (p_args->event == *CAN_EVENT_TX_COMPLETE* )
{
can_send_complete_flag = true;
}
if (p_args->event == *CAN_EVENT_RX_COMPLETE* )
{
can_r = p_args->frame;
can_recv_complete_flag = true;
}
}
创建两个shell命令,分别为can_send和can_recv,观察执行效果。
can_send函数接收一个十进制的id输入,固定输出8个字节的标准数据帧。
int can_send ( int argc, char argv[])
{
if (argc < 2)
{
shellPrint(&shell, "please input string same as can_send 123\n\r");
return -1;
}
unsigned int id = ( unsigned int ) atoi (argv[1]);
fsp_err_t err = FSP_SUCCESS ;
can_frame_t
can_s;
can_s.id = id;
can_s.id_mode = CAN_ID_MODE_STANDARD ;
can_s.data_length_code = 8;
can_s.data[0] = 0x01;
can_s.data[1] = 0x02;
can_s.data[2] = 0x03;
can_s.data[3] = 0x04;
can_s.data[4] = 0x05;
can_s.data[5] = 0x06;
can_s.data[6] = 0x07;
can_s.data[7] = 0x08;
err = R_CAN_Write (&g_can0_ctrl, 0, &can_s);
if (FSP_SUCCESS != err)
__BKPT();
while (can_send_complete_flag == false){}
can_send_complete_flag = false;
return 0;
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE( SHELL_TYPE_CMD_MAIN ),
can_send, can_send, can_send id);
can_recv函数输入一个接收测试的数量,并将接收到的信息打印出来。
int can_recv ( int argc, char *argv[])
{
if (argc< 2)
{
shellPrint(&shell, "please input string same as can_recv 100\n\r");
return -1;
}
unsigned int c = ( unsigned int ) atoi (argv[1]);
unsigned int count = 0;
while (count < c)
{
if (can_recv_complete_flag == true)
{
can_recv_complete_flag = false;
count++;
shellPrint(&shell,"canfram %04x %d %02x %02x %02x %02x %02x %02x %02x %02x\n\r", can_r.id, can_r.data_length_code,
can_r.data[0],
can_r.data[1],
can_r.data[2],
can_r.data[3],
can_r.data[4],
can_r.data[5],
can_r.data[6],
can_r.data[7]
);
}
vTaskDelay(10);
}
return 0;
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE( SHELL_TYPE_CMD_MAIN ), can_recv, can_recv, can_recv count);
更多回帖