增加CAN Comm通信功能,接收中断处理尚未编写完毕

This commit is contained in:
NeoZeng 2022-11-27 20:58:49 +08:00
parent 1abd0d7e4b
commit 82ee444c00
5 changed files with 144 additions and 9 deletions

View File

@ -9,7 +9,9 @@
"ins_task.h": "c", "ins_task.h": "c",
"task.h": "c", "task.h": "c",
"user_lib.h": "c", "user_lib.h": "c",
"chassis.h": "c" "chassis.h": "c",
"can_comm.h": "c",
"string.h": "c"
}, },
"C_Cpp.default.configurationProvider": "ms-vscode.makefile-tools", "C_Cpp.default.configurationProvider": "ms-vscode.makefile-tools",
"C_Cpp.intelliSenseEngineFallback": "enabled", "C_Cpp.intelliSenseEngineFallback": "enabled",

View File

@ -102,12 +102,17 @@ static void CANFIFOxCallback(CAN_HandleTypeDef *_hcan, uint32_t fifox)
HAL_CAN_GetRxMessage(_hcan, fifox, &rxconf, can_rx_buff); HAL_CAN_GetRxMessage(_hcan, fifox, &rxconf, can_rx_buff);
for (size_t i = 0; i < DEVICE_CAN_CNT; i++) for (size_t i = 0; i < DEVICE_CAN_CNT; i++)
{ {
if (_hcan == instance[i]->can_handle && rxconf.StdId == instance[i]->rx_id) if(instance[i]!=NULL)
{ {
memcpy(instance[i]->rx_buff, can_rx_buff, 8); if (_hcan == instance[i]->can_handle && rxconf.StdId == instance[i]->rx_id)
instance[i]->can_module_callback(instance[i]); {
break; memcpy(instance[i]->rx_buff, can_rx_buff, 8);
instance[i]->can_module_callback(instance[i]);
break;
}
} }
else
break;
} }
} }

View File

@ -14,10 +14,10 @@
// PID优化环节函数声明 // PID优化环节函数声明
static void f_Trapezoid_Intergral(PID_t *pid); // 梯形积分 static void f_Trapezoid_Intergral(PID_t *pid); // 梯形积分
static void f_Integral_Limit(PID_t *pid); // 积分限幅 static void f_Integral_Limit(PID_t *pid); // 积分限幅
static void f_Derivative_On_Measurement(PID_t *pid); // 微分先行(仅使用反馈微分) static void f_Derivative_On_Measurement(PID_t *pid); // 微分先行(仅使用反馈值而不计参考输入的微分)
static void f_Changing_Integration_Rate(PID_t *pid); // 变速积分 static void f_Changing_Integration_Rate(PID_t *pid); // 变速积分(误差小时积分作用更强)
static void f_Output_Filter(PID_t *pid); // 输出滤波 static void f_Output_Filter(PID_t *pid); // 输出滤波(平滑输出)
static void f_Derivative_Filter(PID_t *pid); // 微分滤波(采集时) static void f_Derivative_Filter(PID_t *pid); // 微分滤波(采集时,滤除高频噪声)
static void f_Output_Limit(PID_t *pid); // 输出限幅 static void f_Output_Limit(PID_t *pid); // 输出限幅
static void f_PID_ErrorHandle(PID_t *pid); // 堵转保护 static void f_PID_ErrorHandle(PID_t *pid); // 堵转保护

View File

@ -0,0 +1,52 @@
#include "can_comm.h"
#include "memory.h"
#include "stdlib.h"
static CANCommInstance *can_comm_instance[MX_CAN_COMM_COUNT] = {NULL};
static uint8_t idx;
static void CANCommRxCallback(can_instance *_instance)
{
for (size_t i = 0; i < idx; i++)
{
if (&can_comm_instance[i]->can_ins == _instance)
{
}
}
}
CANCommInstance *CANCommInit(CANComm_Init_Config_s config)
{
can_comm_instance[idx] = (CANCommInstance *)malloc(sizeof(CANCommInstance));
memset(can_comm_instance[idx], 0, sizeof(CANCommInstance));
can_comm_instance[idx]->recv_data_len = config.recv_data_len;
can_comm_instance[idx]->send_data_len = config.send_data_len;
can_comm_instance[idx]->send_buf_len = config.send_data_len + CAN_COMM_OFFSET_BYTES;
can_comm_instance[idx]->raw_sendbuf[0] = CAN_COMM_HEADER;
can_comm_instance[idx]->raw_sendbuf[1] = config.send_data_len;
can_comm_instance[idx]->raw_sendbuf[config.send_data_len + CAN_COMM_OFFSET_BYTES - 1] = CAN_COMM_TAIL;
config.can_config.can_module_callback = CANCommRxCallback;
CANRegister(&can_comm_instance[idx]->can_ins, config.can_config);
return can_comm_instance[idx++];
}
void CANCommSend(CANCommInstance *instance, uint8_t *data)
{
static uint8_t crc8;
memcpy(instance->raw_sendbuf + 2, data, instance->send_data_len);
crc8 = crc_8(data, instance->send_data_len);
instance->raw_sendbuf[2 + instance->send_data_len] = crc8;
for (size_t i = 0; i < instance->send_buf_len; i += 8)
{
memcpy(instance->can_ins.tx_buff, instance->raw_sendbuf[i], 8);
CANTransmit(&instance->can_ins);
}
}
void *CANCommGet(CANCommInstance *instance)
{
instance->update_flag = 0;
return instance->unpacked_recv_data;
}

View File

@ -0,0 +1,76 @@
/**
* @file can_comm.h
* @author Neo neozng1@hnu.edu.cn
* @brief CAN通信的收发模块
* @version 0.1
* @date 2022-11-27
*
* @copyright Copyright (c) 2022 HNUYueLu EC all rights reserved
*
*/
#ifndef CAN_COMM_H
#define CAN_COMM_H
#include "bsp_can.h"
#include "crc8.h"
#define MX_CAN_COMM_COUNT 4 //注意均衡负载,一条总线上不要挂载过多的外设
#define CAN_COMM_MAX_BUFFSIZE 68 // 最大发送/接收字节数
#define CAN_COMM_HEADER 's'
#define CAN_COMM_TAIL 'e'
#define CAN_COMM_OFFSET_BYTES 4 // 's'+datalen+'e'+crc8
typedef struct
{
can_instance can_ins;
/* 发送部分 */
uint8_t send_data_len;
uint8_t send_buf_len;
uint8_t raw_sendbuf[CAN_COMM_MAX_BUFFSIZE+CAN_COMM_OFFSET_BYTES]; //额外4个bytes保存帧头帧尾和校验和
/* 接收部分 */
uint8_t recv_data_len;
uint8_t recv_buf_len;
uint8_t raw_recvbuf[CAN_COMM_MAX_BUFFSIZE+CAN_COMM_OFFSET_BYTES]; //额外4个bytes保存帧头帧尾和校验和
uint8_t unpacked_recv_data[CAN_COMM_MAX_BUFFSIZE];
/* 接收和更新标志位*/
uint8_t recv_state;
uint8_t cur_recv_len;
uint8_t update_flag;
} CANCommInstance;
typedef struct
{
uint8_t send_data_len;
uint8_t recv_data_len;
can_instance_config_s can_config;
}CANComm_Init_Config_s;
/**
* @brief
*
* @param config
* @return CANCommInstance*
*/
CANCommInstance* CANCommInit(CANComm_Init_Config_s config);
/**
* @brief
*
* @param instance can comm实例
* @param data datalen相同
*/
void CANCommSend(CANCommInstance* instance,uint8_t* data);
/**
* @brief CAN COMM接收的数据,使void指针转换成指定类型
*
* @return void*
* @attention 访,union或struct,使pack(n)
* CAN COMM接收到的数据可以看作是pack(1),
*/
void* CANCommGet(CANCommInstance* instance);
#endif // !CAN_COMM_H