轮训模式下的数据发送接收测试函数
/*
轮训模式下CAN数据发送和接收测试,使用CAN的回环模式
*/
ErrStatus can_loopback(void)
{
can_trasnmit_message_struct transmit_message;
can_receive_message_struct receive_message;
uint32_t timeout = 0xFFFF;
uint8_t transmit_mailbox = 0;
/* 初始化 CAN */
can_loopback_init();
/* initialize transmit message 初始化发送消息结构 */
can_struct_para_init(CAN_TX_MESSAGE_STRUCT, &transmit_message);
transmit_message.tx_sfid = 0x11;//标准帧ID
transmit_message.tx_ft = CAN_FT_DATA;//数据帧
transmit_message.tx_ff = CAN_FF_STANDARD;//标准帧
transmit_message.tx_dlen = 2;//数据长度
transmit_message.tx_data[0] = 0xAB;//数据adcd
transmit_message.tx_data[1] = 0xCD;
/* initialize receive message初始化发送消息结构 */
can_struct_para_init(CAN_RX_MESSAGE_STRUCT, &receive_message);
/* transmit message 发送消息 返回邮箱编号*/
transmit_mailbox = can_message_transmit(CANX, &transmit_message);
/* waiting for transmit completed 等待消息发送完成*/
while((CAN_TRANSMIT_OK != can_transmit_states(CANX, transmit_mailbox)) && (0 != timeout))
{
timeout--;
}
timeout = 0xFFFF;
/* waiting for receive completed 等待接收完成*/
while((can_receive_message_length_get(CANX, CAN_FIFO1) < 1) && (0 != timeout))
{
timeout--;
}
/* initialize receive message 初始化接收结构体*/
receive_message.rx_sfid = 0x00;
receive_message.rx_ff = 0;
receive_message.rx_dlen = 0;
receive_message.rx_data[0] = 0x00;
receive_message.rx_data[1] = 0x00;
//接收数据
can_message_receive(CANX, CAN_FIFO1, &receive_message);
printf("recv:%02x %02x", (int)receive_message.rx_data[0], (int)receive_message.rx_data[1]);
/* check the receive message */
if((0x11 == receive_message.rx_sfid) && (CAN_FF_STANDARD == receive_message.rx_ff)
&& (2 == receive_message.rx_dlen) && (0xCDAB == (receive_message.rx_data[1] << 8 | receive_message.rx_data[0])))
{
printf("data send recv ok\r\n");
return SUCCESS;
}
else
{
printf("data send recv error\r\n");
return ERROR;
}
}
|
———————————————— 版权声明:本文为CSDN博主「Car12」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/u010261063/article/details/124251643