sentry_chassis_hzz/bsp/can/bsp_can.c

162 lines
7.1 KiB
C
Raw Normal View History

2022-10-20 17:13:02 +08:00
#include "bsp_can.h"
#include "main.h"
#include "memory.h"
2022-12-28 23:12:25 +08:00
#include "stdlib.h"
2022-10-20 17:13:02 +08:00
2022-10-30 22:19:13 +08:00
/* can instance ptrs storage, used for recv callback */
2022-12-02 23:10:36 +08:00
// 在CAN产生接收中断会遍历数组,选出hcan和rxid与发生中断的实例相同的那个,调用其回调函数
2023-01-01 17:32:22 +08:00
static CANInstance *can_instance[CAN_MX_REGISTER_CNT] = {NULL};
static uint8_t idx; // 全局CAN实例索引,每次有新的模块注册会自增
2022-10-31 12:14:47 +08:00
/* ----------------two static function called by CANRegister()-------------------- */
2022-10-20 17:13:02 +08:00
/**
2023-01-01 17:32:22 +08:00
* @brief id的报文的接收,CANRegister()
2022-12-02 23:10:36 +08:00
* CAN添加过滤器后,BxCAN会根据接收到的报文的id进行消息过滤,id会被填入FIFO触发中断
2022-10-31 12:14:47 +08:00
*
2023-01-01 17:32:22 +08:00
* @note f407的bxCAN有28个过滤器,14CAN1使用,14CAN2使用
* ,id的模块会被分配到FIFO0,id的模块会被分配到FIFO1
* CAN1的模块使用过滤器0-13,CAN2使用过滤器14-27
2022-10-31 12:14:47 +08:00
*
2023-01-01 17:32:22 +08:00
* @attention ,,
* !,(reference manual)
2022-10-31 12:14:47 +08:00
*
2022-10-20 17:13:02 +08:00
* @param _instance can instance owned by specific module
*/
static void CANAddFilter(CANInstance *_instance)
2022-10-20 17:13:02 +08:00
{
CAN_FilterTypeDef can_filter_conf;
2022-10-31 12:14:47 +08:00
static uint8_t can1_filter_idx = 0, can2_filter_idx = 14;
2022-10-20 17:13:02 +08:00
can_filter_conf.FilterMode = CAN_FILTERMODE_IDLIST;
can_filter_conf.FilterScale = CAN_FILTERSCALE_16BIT;
2022-11-23 22:23:58 +08:00
can_filter_conf.FilterFIFOAssignment = (_instance->tx_id & 1) ? CAN_RX_FIFO0 : CAN_RX_FIFO1;
2022-10-20 17:13:02 +08:00
can_filter_conf.SlaveStartFilterBank = 14;
can_filter_conf.FilterIdLow = _instance->rx_id << 5;
2022-10-31 12:14:47 +08:00
can_filter_conf.FilterBank = _instance->can_handle == &hcan1 ? (can1_filter_idx++) : (can2_filter_idx++);
2022-10-20 17:13:02 +08:00
can_filter_conf.FilterActivation = CAN_FILTER_ENABLE;
HAL_CAN_ConfigFilter(_instance->can_handle, &can_filter_conf);
}
/**
2023-01-01 17:32:22 +08:00
* @brief CAN实例初始化的时候会自动调用此函数,CAN服务
2022-10-31 12:14:47 +08:00
*
2023-01-01 17:32:22 +08:00
* @note CAN1和CAN2,CAN1和CAN2的FIFO0 & FIFO1溢出通知
2022-10-31 12:14:47 +08:00
*
2022-10-20 17:13:02 +08:00
*/
static void CANServiceInit()
{
HAL_CAN_Start(&hcan1);
HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING);
HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO1_MSG_PENDING);
HAL_CAN_Start(&hcan2);
HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO0_MSG_PENDING);
HAL_CAN_ActivateNotification(&hcan2, CAN_IT_RX_FIFO1_MSG_PENDING);
}
/* ----------------------- two extern callable function -----------------------*/
2022-10-30 22:19:13 +08:00
CANInstance *CANRegister(CAN_Init_Config_s *config)
2022-10-20 17:13:02 +08:00
{
2022-10-31 12:14:47 +08:00
if (!idx)
2022-10-20 17:13:02 +08:00
{
2022-12-02 23:10:36 +08:00
CANServiceInit(); // 第一次注册,先进行硬件初始化
2022-10-20 17:13:02 +08:00
}
2023-01-01 17:32:22 +08:00
if (idx >= CAN_MX_REGISTER_CNT) // 超过最大实例数
while (1)
;
CANInstance *instance = (CANInstance *)malloc(sizeof(CANInstance)); // 分配空间
2023-01-01 17:32:22 +08:00
memset(instance, 0, sizeof(CANInstance)); // 分配的空间未必是0,所以要先清空
2022-12-02 23:10:36 +08:00
// 进行发送报文的配置
2023-01-01 17:32:22 +08:00
instance->txconf.StdId = config->tx_id; // 发送id
instance->txconf.IDE = CAN_ID_STD; // 使用标准id,扩展id则使用CAN_ID_EXT(目前没有需求)
instance->txconf.RTR = CAN_RTR_DATA; // 发送数据帧
instance->txconf.DLC = 0x08; // 默认发送长度为8
2022-12-02 23:10:36 +08:00
// 设置回调函数和接收发送id
instance->can_handle = config->can_handle;
instance->tx_id = config->tx_id; // 好像没用,可以删掉
instance->rx_id = config->rx_id;
instance->can_module_callback = config->can_module_callback;
instance->id = config->id;
CANAddFilter(instance); // 添加CAN过滤器规则
can_instance[idx++] = instance; // 将实例保存到can_instance中
return instance; // 返回can实例指针
2022-10-20 17:13:02 +08:00
}
2023-01-01 17:32:22 +08:00
/* @todo 目前似乎封装过度,应该添加一个指向tx_buff的指针,tx_buff不应该由CAN instance保存 */
/* 如果让CANinstance保存txbuff,会增加一次复制的开销 */
void CANTransmit(CANInstance *_instance)
2022-10-20 17:13:02 +08:00
{
2023-01-01 17:32:22 +08:00
while (HAL_CAN_GetTxMailboxesFreeLevel(_instance->can_handle) == 0) // 等待邮箱空闲
2022-10-31 12:14:47 +08:00
;
2022-12-02 23:10:36 +08:00
// tx_mailbox会保存实际填入了这一帧消息的邮箱,但是知道是哪个邮箱发的似乎也没啥用
2022-10-30 22:19:13 +08:00
HAL_CAN_AddTxMessage(_instance->can_handle, &_instance->txconf, _instance->tx_buff, &_instance->tx_mailbox);
2022-10-20 17:13:02 +08:00
}
void CANSetDLC(CANInstance *_instance, uint8_t length)
{
if (length > 8 || length < 0) // 安全检查
while (1)
; // 发送长度错误!检查调用参数是否出错,或出现野指针/越界访问
_instance->txconf.DLC = length;
}
2022-10-30 22:19:13 +08:00
/* -----------------------belows are callback definitions--------------------------*/
2022-10-20 17:13:02 +08:00
/**
2023-01-01 17:32:22 +08:00
* @brief ,FIFO0和FIFO1溢出中断()
* ,can_handle和rx_id相等的实例时,
2022-10-31 12:14:47 +08:00
*
* @param _hcan
2022-10-20 17:13:02 +08:00
* @param fifox passed to HAL_CAN_GetRxMessage() to get mesg from a specific fifo
*/
2022-10-31 12:14:47 +08:00
static void CANFIFOxCallback(CAN_HandleTypeDef *_hcan, uint32_t fifox)
2022-10-20 17:13:02 +08:00
{
2023-01-01 17:32:22 +08:00
static uint8_t can_rx_buff[8]; // 用于保存接收到的数据,static是为了减少栈空间占用,避免重复分配
static CAN_RxHeaderTypeDef rxconf; // 同上
HAL_CAN_GetRxMessage(_hcan, fifox, &rxconf, can_rx_buff); // 从FIFO中获取数据
for (size_t i = 0; i < idx; ++i)
{ // 两者相等说明这是要找的实例
if (_hcan == can_instance[i]->can_handle && rxconf.StdId == can_instance[i]->rx_id)
{
2023-01-01 17:32:22 +08:00
if (can_instance[i]->can_module_callback != NULL) // 回调函数不为空就调用
{
can_instance[i]->rx_len = rxconf.DLC; // 保存接收到的数据长度
memcpy(can_instance[i]->rx_buff, can_rx_buff, rxconf.DLC); // 消息拷贝到对应实例
can_instance[i]->can_module_callback(can_instance[i]); // 触发回调进行数据解析和处理
}
return;
}
2022-10-31 12:14:47 +08:00
}
2022-10-20 17:13:02 +08:00
}
2023-01-01 17:32:22 +08:00
/**
* @brief ,STM32的两个CAN设备共享两个FIFO
* HAL库中的回调函数,__weak,()
* FIFO0或FIFO1溢出时会调用这两个函数
*/
// 下面的函数会调用CANFIFOxCallback()来进一步处理来自特定CAN设备的消息
2022-10-20 17:13:02 +08:00
/**
* @brief rx fifo callback. Once FIFO_0 is full,this func would be called
2022-10-31 12:14:47 +08:00
*
2022-10-20 17:13:02 +08:00
* @param hcan CAN handle indicate which device the oddest mesg in FIFO_0 comes from
*/
2022-10-31 12:14:47 +08:00
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
2022-10-20 17:13:02 +08:00
{
2023-01-01 17:32:22 +08:00
CANFIFOxCallback(hcan, CAN_RX_FIFO0); // 调用我们自己写的函数来处理消息
2022-10-20 17:13:02 +08:00
}
/**
* @brief rx fifo callback. Once FIFO_1 is full,this func would be called
2022-10-31 12:14:47 +08:00
*
2022-10-20 17:13:02 +08:00
* @param hcan CAN handle indicate which device the oddest mesg in FIFO_1 comes from
*/
2022-10-31 12:14:47 +08:00
void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan)
2022-10-20 17:13:02 +08:00
{
2023-01-01 17:32:22 +08:00
CANFIFOxCallback(hcan, CAN_RX_FIFO1); // 调用我们自己写的函数来处理消息
2022-10-20 17:13:02 +08:00
}