重构bsp层,bsp层将和HAL的配置一致,修改CubeMX之后不需要修改bsp。重构bmi088。

This commit is contained in:
NeoZng
2022-12-30 23:39:04 +08:00
parent ab1a9726b1
commit a3631a5ca5
39 changed files with 842 additions and 256 deletions

155
bsp/can/bsp_can.c Normal file
View File

@@ -0,0 +1,155 @@
#include "bsp_can.h"
#include "main.h"
#include "memory.h"
#include "stdlib.h"
/* can instance ptrs storage, used for recv callback */
// 在CAN产生接收中断会遍历数组,选出hcan和rxid与发生中断的实例相同的那个,调用其回调函数
static CANInstance *instance[MX_REGISTER_DEVICE_CNT] = {NULL};
static uint8_t idx; // 全局CAN实例索引,每次有新的模块注册会自增
/* ----------------two static function called by CANRegister()-------------------- */
/**
* @brief add filter to receive mesg with specific ID,called by CANRegister()
* 给CAN添加过滤器后,BxCAN会根据接收到的报文的id进行消息过滤,符合规则的id会被填入FIFO触发中断
*
* @note there are total 28 filter and 2 FIFO in bxCAN of STM32F4 series product.
* here, we assign the former 14 to CAN1 and the rest for CAN2
* when initializing, module with odd ID will be assigned to FIFO0 while even one to FIFO1
* those modules which registered in CAN1 would use Filter0-13, while CAN2 use Filter14-27
*
* @attention you don't have to fully understand what this function done, cause it is basically
* for initialization.Enjoy developing without caring about the infrastructure!
* if you really want to know what is happeng, contact author.
*
* @param _instance can instance owned by specific module
*/
static void CANAddFilter(CANInstance *_instance)
{
CAN_FilterTypeDef can_filter_conf;
static uint8_t can1_filter_idx = 0, can2_filter_idx = 14;
can_filter_conf.FilterMode = CAN_FILTERMODE_IDLIST;
can_filter_conf.FilterScale = CAN_FILTERSCALE_16BIT;
can_filter_conf.FilterFIFOAssignment = (_instance->tx_id & 1) ? CAN_RX_FIFO0 : CAN_RX_FIFO1;
can_filter_conf.SlaveStartFilterBank = 14;
can_filter_conf.FilterIdLow = _instance->rx_id << 5;
can_filter_conf.FilterBank = _instance->can_handle == &hcan1 ? (can1_filter_idx++) : (can2_filter_idx++);
can_filter_conf.FilterActivation = CAN_FILTER_ENABLE;
HAL_CAN_ConfigFilter(_instance->can_handle, &can_filter_conf);
}
/**
* @brief called by CANRegister before the first module being registered
* 在第一个CAN实例初始化的时候会自动调用此函数,启动CAN服务
*
* @note this func will handle all these thing automatically
* there is no need to worry about hardware initialization, we do these for you!
*
*/
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 -----------------------*/
CANInstance *CANRegister(CAN_Init_Config_s *config)
{
if (!idx)
{
CANServiceInit(); // 第一次注册,先进行硬件初始化
}
instance[idx] = (CANInstance *)malloc(sizeof(CANInstance)); // 分配空间
memset(instance[idx], 0, sizeof(CANInstance));
// 进行发送报文的配置
instance[idx]->txconf.StdId = config->tx_id;
instance[idx]->txconf.IDE = CAN_ID_STD;
instance[idx]->txconf.RTR = CAN_RTR_DATA;
instance[idx]->txconf.DLC = 0x08; // 默认发送长度为8
// 设置回调函数和接收发送id
instance[idx]->can_handle = config->can_handle;
instance[idx]->tx_id = config->tx_id; // 好像没用,可以删掉
instance[idx]->rx_id = config->rx_id;
instance[idx]->can_module_callback = config->can_module_callback;
instance[idx]->id = config->id;
CANAddFilter(instance[idx]); // 添加CAN过滤器规则
return instance[idx++]; // 返回指针
}
/* TODO:目前似乎封装过度,应该添加一个指向tx_buff的指针,tx_buff不应该由CAN instance保存 */
void CANTransmit(CANInstance *_instance)
{
while (HAL_CAN_GetTxMailboxesFreeLevel(_instance->can_handle) == 0)
;
// tx_mailbox会保存实际填入了这一帧消息的邮箱,但是知道是哪个邮箱发的似乎也没啥用
HAL_CAN_AddTxMessage(_instance->can_handle, &_instance->txconf, _instance->tx_buff, &_instance->tx_mailbox);
}
void CANSetDLC(CANInstance *_instance, uint8_t length)
{
if (length > 8 || length < 0) // 安全检查
while (1)
; // 发送长度错误!检查调用参数是否出错,或出现野指针/越界访问
_instance->txconf.DLC = length;
}
/* -----------------------belows are callback definitions--------------------------*/
/**
* @brief this func will recv data from @param:fifox to a tmp can_rx_buff
* then, all the instances will be polling to check which should recv this pack of data
*
* @param _hcan
* @param fifox passed to HAL_CAN_GetRxMessage() to get mesg from a specific fifo
*/
static void CANFIFOxCallback(CAN_HandleTypeDef *_hcan, uint32_t fifox)
{
static uint8_t can_rx_buff[8];
static CAN_RxHeaderTypeDef rxconf;
HAL_CAN_GetRxMessage(_hcan, fifox, &rxconf, can_rx_buff);
for (size_t i = 0; i < idx; ++i)
{
// 两者相等说明这是要找的实例
if (_hcan == instance[i]->can_handle && rxconf.StdId == instance[i]->rx_id)
{
instance[i]->rx_len = rxconf.DLC;
memcpy(instance[i]->rx_buff, can_rx_buff, rxconf.DLC); // 消息拷贝到对应实例
if (instance[i]->can_module_callback != NULL)
{
instance[i]->can_module_callback(instance[i]); // 触发回调进行数据解析和处理
}
break;
}
}
}
/* ATTENTION: two CAN devices in STM32 share two FIFOs */
/* functions below will call CANFIFOxCallback() to further process message from a specific CAN device */
/**
* @brief rx fifo callback. Once FIFO_0 is full,this func would be called
*
* @param hcan CAN handle indicate which device the oddest mesg in FIFO_0 comes from
*/
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
CANFIFOxCallback(hcan, CAN_RX_FIFO0);
}
/**
* @brief rx fifo callback. Once FIFO_1 is full,this func would be called
*
* @param hcan CAN handle indicate which device the oddest mesg in FIFO_1 comes from
*/
void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
CANFIFOxCallback(hcan, CAN_RX_FIFO1);
}

64
bsp/can/bsp_can.h Normal file
View File

@@ -0,0 +1,64 @@
#ifndef BSP_CAN_H
#define BSP_CAN_H
#include <stdint.h>
#include "can.h"
#define MX_REGISTER_DEVICE_CNT 12 // maximum number of device can be registered to CAN service
// this number depends on the load of CAN bus.
#define MX_CAN_FILTER_CNT (2 * 14) // temporarily useless
#define DEVICE_CAN_CNT 2 // CAN1,CAN2
/* can instance typedef, every module registered to CAN should have this variable */
#pragma pack(1)
typedef struct _
{
CAN_HandleTypeDef *can_handle; // can句柄
CAN_TxHeaderTypeDef txconf; // CAN报文发送配置
uint32_t tx_id; // 发送id
uint32_t tx_mailbox; // CAN消息填入的邮箱号
uint8_t tx_buff[8]; // 发送缓存,发送消息长度可以通过CANSetDLC()设定,最大为8
uint8_t rx_buff[8]; // 接收缓存,最大消息长度为8
uint32_t rx_id; // 接收id
uint8_t rx_len; // 接收长度,可能为0-8
// 接收的回调函数,用于解析接收到的数据
void (*can_module_callback)(struct _ *); // callback needs an instance to tell among registered ones
void* id; // 使用can外设的
} CANInstance;
#pragma pack()
/* this structure is used for initialization */
typedef struct
{
CAN_HandleTypeDef *can_handle;
uint32_t tx_id;
uint32_t rx_id;
void (*can_module_callback)(CANInstance *);
void* id;
} CAN_Init_Config_s;
/**
* @brief 修改CAN发送报文的数据帧长度;注意最大长度为8,在没有进行修改的时候,默认长度为8
*
* @param _instance 要修改长度的can实例
* @param length 设定长度
*/
void CANSetDLC(CANInstance *_instance, uint8_t length);
/**
* @brief transmit mesg through CAN device,通过can实例发送消息
* 发送前需要向CAN实例的tx_buff写入发送数据
*
* @param _instance* can instance owned by module
*/
void CANTransmit(CANInstance *_instance);
/**
* @brief Register a module to CAN service,remember to call this before using a CAN device
* 注册(初始化)一个can实例,需要传入初始化配置的指针.
* @param config init config
* @return CANInstance* can instance owned by module
*/
CANInstance *CANRegister(CAN_Init_Config_s *config);
#endif

102
bsp/can/bsp_can.md Normal file
View File

@@ -0,0 +1,102 @@
# bsp_can
<p align='right'>neozng1@hnu.edu.cn</p>
> TODO:
>
> 1. 增加数据帧的长度定义使得收发更加灵活而不是固定的8 bytes
> 2. 增加自动检测ID冲突的log输出。
## 使用说明
若你希望新增一个基于CAN的module首先在该模块下应该有一个包含`can_instance`指针的module结构体或当功能简单的时候可以是单独存在的`can_instance`,但不推荐这样做)。
## 代码结构
.h文件内包括了外部接口和类型定义,以及模块对应的宏。c文件内为私有函数和外部接口的定义。
## 类型定义
```c
#define MX_REGISTER_DEVICE_CNT 12 // maximum number of device can be registered to CAN service, this number depends on the load of CAN bus.
#define MX_CAN_FILTER_CNT (4 * 14) // temporarily useless
#define DEVICE_CAN_CNT 2 // CAN1,CAN2
/* can instance typedef, every module registered to CAN should have this variable */
typedef struct _
{
CAN_HandleTypeDef* can_handle;
CAN_TxHeaderTypeDef txconf;
uint32_t tx_id;
uint32_t tx_mailbox;
uint8_t tx_buff[8];
uint8_t rx_buff[8];
uint32_t rx_id;
void (*can_module_callback)(struct _*);
} can_instance;
typedef struct
{
CAN_HandleTypeDef* can_handle;
uint32_t tx_id;
uint32_t rx_id;
void (*can_module_callback)(can_instance*);
} can_instance_config;
typedef void (*can_callback)(can_instance*);
```
- `MX_REGISTER_DEVICE_CNT`是最大的CAN设备注册数量当每个设备的发送频率都较高时设备过多会产生总线拥塞从而出现丢包和数据错误的情况。
- `MX_CAN_FILTER_CNT`是最大的CAN接收过滤器数量两个CAN共享标号0~27共28个过滤器。这部分内容比较繁杂暂时不用理解有兴趣自行参考MCU的数据手册。当前为简单起见每个过滤器只设置一组规则用于控制一个id的过滤。
- `DEVICE_CAN_CNT`是MCU拥有的CAN硬件数量。
- `can_instance`是一个CAN实例。注意CAN作为一个总线设备一条总线上可以挂载多个设备因此多个设备可以共享同一个CAN硬件。其成员变量包括发送id发送邮箱不需要管只是一个32位变量CAN收发器会自动设置其值发送buff以及接收buff还有接收id和接收协议解析回调函数。**由于目前使用的设备每个数据帧的长度都是8因此收发buff长度暂时固定为8**。定义该结构体的时候使用了一个技巧,使得在结构体内部可以用结构体自身的指针作为成员,即`can_module_callback`的定义。
- `can_instance_config`是用于初始化CAN实例的结构在调用CAN实例的初始化函数时传入下面介绍函数时详细介绍
- `can_module_callback()`是模块提供给CAN接收中断回调函数使用的协议解析函数指针。对于每个需要CAN的模块需要定义一个这样的函数用于解包数据。
- 每个使用CAN外设的module都需要在其内部定义一个`can_instance*`
## 外部接口
```c
void CANRegister(can_instance* instance, can_instance_config config);
void CANTransmit(can_instance* _instance);
```
`CANRegister`是用于初始化CAN实例的接口module层的模块对象也应当为一个结构体内要包含一个`usart_instance`。调用时传入实例指针以及用于初始化的config。`CANRegister`应当在module的初始化函数内被调用推荐config采用以下的方式定义更加直观明了
```c
can_instance_config config={.can_handle=&hcan1,
.tx_id=0x005,
.rx_id=0x200,
can_module_callback=MotorCallback}
```
`CANTransmit()`是通过模块通过其拥有的CAN实例发送数据的接口调用时传入对应的instance。在发送之前应当给instance内的`send_buff`赋值。
## 私有函数和变量
在.c文件内设为static的函数和变量
```c
static can_instance *instance[MX_REGISTER_DEVICE_CNT]={NULL};
```
这是bsp层管理所有CAN实例的入口。
```c
static void CANServiceInit()
static void CANAddFilter(can_instance *_instance)
static void CANFIFOxCallback(CAN_HandleTypeDef *_hcan, uint32_t fifox)
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
void HAL_CAN_RxFifo1MsgPendingCallback(CAN_HandleTypeDef *hcan)
```
- `CANServiceInit()`会被`CANRegister()`调用对CAN外设进行硬件初始化并开启接收中断和消息提醒。
- `CANAddFilter()`在每次使用`CANRegister()`的时候被调用,用于给当前注册的实例添加过滤器规则并设定处理对应`rx_id`的接收FIFO。过滤器的作用是减小CAN收发器的压力只接收符合过滤器规则的报文否则不会产生接收中断
- `HAL_CAN_RxFifo0MsgPendingCallback()``HAL_CAN_RxFifo1MsgPendingCallback()`都是对HAL的CAN回调函数的重定义原本的callback是`__week`修饰的弱定义当发生FIFO0或FIFO1有新消息到达的时候对应的callback会被调用。`CANFIFOxCallback()`随后被前两者调用并根据接收id和硬件中断来源哪一个CAN硬件CAN1还是CAN2调用对应的instance的回调函数进行协议解析。