完成第一版can comm多机通信,还未测验,可能有bug.

This commit is contained in:
NeoZeng 2022-11-27 22:38:59 +08:00
parent 82ee444c00
commit 36df442214
4 changed files with 87 additions and 6 deletions

View File

@ -11,7 +11,10 @@
"user_lib.h": "c",
"chassis.h": "c",
"can_comm.h": "c",
"string.h": "c"
"string.h": "c",
"stdlib.h": "c",
"crc8.h": "c",
"main.h": "c"
},
"C_Cpp.default.configurationProvider": "ms-vscode.makefile-tools",
"C_Cpp.intelliSenseEngineFallback": "enabled",

View File

@ -1,16 +1,62 @@
#include "can_comm.h"
#include "memory.h"
#include "stdlib.h"
#include "crc8.h"
/* can_comm用于保存每个实例的指针数组,用于回调函数区分实例 */
static CANCommInstance *can_comm_instance[MX_CAN_COMM_COUNT] = {NULL};
static uint8_t idx;
static uint8_t idx; //配合can_comm_instance的初始化使用,标识当前初始化的是哪一个实例
/**
* @brief
*
* @param _instance
*/
static void CANCommRxCallback(can_instance *_instance)
{
for (size_t i = 0; i < idx; i++)
{
if (&can_comm_instance[i]->can_ins == _instance)
if (&can_comm_instance[i]->can_ins == _instance) // 遍历,找到对应的接收CAN COMM实例
{
if(_instance->rx_buff[0]==CAN_COMM_HEADER && can_comm_instance[i]->recv_state==0) //尚未开始接收且新的一包里有帧头
{
if(_instance->rx_buff[1]==can_comm_instance[i]->recv_data_len) // 接收长度等于设定接收长度
{
can_comm_instance[i]->recv_state=1;
}
else
return ; //直接跳过即可
}
if(can_comm_instance[i]->recv_state) //已经开始接收
{ // 直接拷贝到当前的接收buffer后面
memcpy(can_comm_instance[i]->raw_recvbuf+can_comm_instance[i]->cur_recv_len,_instance->rx_buff,8);
can_comm_instance[i]->cur_recv_len+=8;
// 当前已经收满
if(can_comm_instance[i]->cur_recv_len>=can_comm_instance[i]->recv_buf_len)
{ // buff里本该是tail的位置不等于CAN_COMM_TAIL
if(can_comm_instance[i]->raw_recvbuf[can_comm_instance[i]->recv_buf_len-1]!=CAN_COMM_TAIL)
{
memset(can_comm_instance[i]->raw_recvbuf,0,can_comm_instance[i]->recv_buf_len);
can_comm_instance[i]->recv_state=0;
can_comm_instance[i]->cur_recv_len=0;
return ; // 重置状态然后返回
}
else // tail正确, 对数据进行crc8校验
{
if(can_comm_instance[i]->raw_recvbuf[can_comm_instance[i]->recv_buf_len-2] ==
crc_8(can_comm_instance[i]->raw_recvbuf+2,can_comm_instance[i]->recv_data_len))
{ // 通过校验,复制数据到unpack_data中
memcpy(can_comm_instance[i]->raw_recvbuf+2,can_comm_instance[i]->unpacked_recv_data,can_comm_instance[i]->recv_data_len);
can_comm_instance[i]->update_flag=1; //数据更新flag置为1
}
memset(can_comm_instance[i]->raw_recvbuf,0,can_comm_instance[i]->recv_buf_len); //整个buff置零
can_comm_instance[i]->recv_state=0;
can_comm_instance[i]->cur_recv_len=0;
return ; // 重置状态然后返回
}
}
}
return ;
}
}
}

View File

@ -13,7 +13,6 @@
#include "bsp_can.h"
#include "crc8.h"
#define MX_CAN_COMM_COUNT 4 //注意均衡负载,一条总线上不要挂载过多的外设

View File

@ -1,3 +1,36 @@
can_comm
# can_comm
<p align='right'>neozng1@hnu.edu.cn</p>
双板CAN通信模块
## 总览和封装说明
## 代码结构
## 外部接口
## 私有函数和变量
## 接收解析流程
双板CAN通信