mirror of
https://gitee.com/dlmu-cone/bf_original_balance_chassis
synced 2026-07-24 11:37:45 +08:00
重构bsp层,bsp层将和HAL的配置一致,修改CubeMX之后不需要修改bsp。重构bmi088。
This commit is contained in:
@@ -1,4 +0,0 @@
|
||||
#include "bsp_iic.h"
|
||||
#include "memory.h"
|
||||
#include "stdlib.h"
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
#include "i2c.h"
|
||||
#include "stdint.h"
|
||||
|
||||
#define IIC_DEVICE_CNT ()
|
||||
|
||||
typedef struct
|
||||
{
|
||||
I2C_HandleTypeDef* handle;
|
||||
|
||||
};
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
/* 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()-------------------- */
|
||||
|
||||
@@ -62,7 +63,6 @@ static void CANServiceInit()
|
||||
|
||||
CANInstance *CANRegister(CAN_Init_Config_s *config)
|
||||
{
|
||||
static uint8_t idx; // 全局CAN实例索引,每次有新的模块注册会自增
|
||||
if (!idx)
|
||||
{
|
||||
CANServiceInit(); // 第一次注册,先进行硬件初始化
|
||||
@@ -79,7 +79,7 @@ CANInstance *CANRegister(CAN_Init_Config_s *config)
|
||||
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;
|
||||
instance[idx]->id = config->id;
|
||||
|
||||
CANAddFilter(instance[idx]); // 添加CAN过滤器规则
|
||||
return instance[idx++]; // 返回指针
|
||||
@@ -96,9 +96,9 @@ void CANTransmit(CANInstance *_instance)
|
||||
|
||||
void CANSetDLC(CANInstance *_instance, uint8_t length)
|
||||
{
|
||||
if (length > 8) // 安全检查
|
||||
if (length > 8 || length < 0) // 安全检查
|
||||
while (1)
|
||||
;
|
||||
; // 发送长度错误!检查调用参数是否出错,或出现野指针/越界访问
|
||||
_instance->txconf.DLC = length;
|
||||
}
|
||||
|
||||
@@ -113,23 +113,22 @@ void CANSetDLC(CANInstance *_instance, uint8_t length)
|
||||
*/
|
||||
static void CANFIFOxCallback(CAN_HandleTypeDef *_hcan, uint32_t fifox)
|
||||
{
|
||||
uint8_t can_rx_buff[8];
|
||||
CAN_RxHeaderTypeDef rxconf;
|
||||
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 < MX_REGISTER_DEVICE_CNT; ++i)
|
||||
for (size_t i = 0; i < idx; ++i)
|
||||
{
|
||||
if (instance[i] != NULL) // 碰到NULL说明已经遍历完所有实例
|
||||
{ // 两者相等说明这是要找的实例
|
||||
if (_hcan == instance[i]->can_handle && rxconf.StdId == instance[i]->rx_id)
|
||||
// 两者相等说明这是要找的实例
|
||||
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]->rx_len = rxconf.DLC;
|
||||
memcpy(instance[i]->rx_buff, can_rx_buff, rxconf.DLC); // 消息拷贝到对应实例
|
||||
instance[i]->can_module_callback(instance[i]); // 触发回调进行数据解析和处理
|
||||
break;
|
||||
instance[i]->can_module_callback(instance[i]); // 触发回调进行数据解析和处理
|
||||
}
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,13 +17,13 @@ typedef struct _
|
||||
CAN_TxHeaderTypeDef txconf; // CAN报文发送配置
|
||||
uint32_t tx_id; // 发送id
|
||||
uint32_t tx_mailbox; // CAN消息填入的邮箱号
|
||||
uint8_t tx_buff[8]; // 发送缓存,最大为8
|
||||
uint8_t rx_buff[8]; // 接收缓存
|
||||
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;
|
||||
void* id; // 使用can外设的
|
||||
} CANInstance;
|
||||
#pragma pack()
|
||||
|
||||
0
bsp/dac/bsp_dac.c
Normal file
0
bsp/dac/bsp_dac.c
Normal file
0
bsp/dac/bsp_dac.h
Normal file
0
bsp/dac/bsp_dac.h
Normal file
0
bsp/dac/bsp_dac.md
Normal file
0
bsp/dac/bsp_dac.md
Normal file
0
bsp/dwt/bsp_dwt.md
Normal file
0
bsp/dwt/bsp_dwt.md
Normal file
0
bsp/gpio/bsp_gpio.c
Normal file
0
bsp/gpio/bsp_gpio.c
Normal file
0
bsp/gpio/bsp_gpio.h
Normal file
0
bsp/gpio/bsp_gpio.h
Normal file
0
bsp/gpio/bsp_gpio.md
Normal file
0
bsp/gpio/bsp_gpio.md
Normal file
145
bsp/iic/bsp_iic.c
Normal file
145
bsp/iic/bsp_iic.c
Normal file
@@ -0,0 +1,145 @@
|
||||
#include "bsp_iic.h"
|
||||
#include "memory.h"
|
||||
#include "stdlib.h"
|
||||
|
||||
static uint8_t idx = 0; // 配合中断以及初始化
|
||||
static IICInstance *iic_instance[IIC_DEVICE_CNT] = {NULL};
|
||||
|
||||
/**
|
||||
* @brief 接收完成回调函数
|
||||
*
|
||||
* @param hi2c handle
|
||||
*/
|
||||
void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
|
||||
{
|
||||
// 如果是当前i2c硬件发出的complete,且dev_address和之前发起接收的地址相同,同时回到函数不为空, 则调用回调函数
|
||||
for (uint8_t i = 0; i < idx; i++)
|
||||
{
|
||||
if (iic_instance[i]->handle == hi2c && hi2c->Devaddress == iic_instance[i]->dev_address)
|
||||
{
|
||||
if (iic_instance[i]->callback != NULL)
|
||||
iic_instance[i]->callback(iic_instance[i]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 仅做形式上的封装,仍然使用HAL_I2C_MasterRxCpltCallback
|
||||
*
|
||||
* @param hi2c handle
|
||||
*/
|
||||
void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c)
|
||||
{
|
||||
HAL_I2C_MasterRxCpltCallback(hi2c);
|
||||
}
|
||||
|
||||
IICInstance *IICRegister(IIC_Init_Config_s *conf)
|
||||
{
|
||||
// 申请到的空间未必是0, 所以需要手动初始化
|
||||
iic_instance[idx] = (IICInstance *)malloc(sizeof(IICInstance));
|
||||
memset(iic_instance[idx], 0, sizeof(IICInstance));
|
||||
|
||||
iic_instance[idx]->dev_address = conf->dev_address;
|
||||
iic_instance[idx]->callback = conf->callback;
|
||||
iic_instance[idx]->work_mode = conf->work_mode;
|
||||
iic_instance[idx]->handle = conf->handle;
|
||||
iic_instance[idx]->id = conf->id;
|
||||
|
||||
return iic_instance[idx++];
|
||||
}
|
||||
|
||||
void IICSetMode(IICInstance *iic, IIC_Work_Mode_e mode)
|
||||
{ // HAL自带重入保护,不需要手动终止或等待传输完成
|
||||
if (iic->work_mode != mode) // 如果不同才需要修改
|
||||
{
|
||||
iic->work_mode = mode;
|
||||
}
|
||||
}
|
||||
|
||||
void IICTransmit(IICInstance *iic, uint8_t *data, uint16_t size, IIC_Seq_Mode_e seq_mode)
|
||||
{
|
||||
if (seq_mode != IIC_RELEASE && seq_mode != IIC_HOLD_ON)
|
||||
while (1)
|
||||
; // 未知传输模式, 程序停止
|
||||
|
||||
switch (iic->work_mode)
|
||||
{
|
||||
case IIC_BLOCK_MODE:
|
||||
if (seq_mode != IIC_RELEASE)
|
||||
while (1)
|
||||
; // 阻塞模式下不支持HOLD ON模式!!!
|
||||
HAL_I2C_Master_Transmit(iic->handle, iic->dev_address, data, size, 100);
|
||||
break;
|
||||
case IIC_IT_MODE:
|
||||
if (seq_mode == IIC_RELEASE)
|
||||
HAL_I2C_Master_Seq_Transmit_IT(iic->handle, iic->dev_address, data, size, I2C_OTHER_AND_LAST_FRAME);
|
||||
else if (seq_mode == IIC_HOLD_ON)
|
||||
HAL_I2C_Master_Seq_Transmit_IT(iic->handle, iic->dev_address, data, size, I2C_OTHER_FRAME);
|
||||
break;
|
||||
case IIC_DMA_MODE:
|
||||
if (seq_mode == IIC_RELEASE)
|
||||
HAL_I2C_Master_Seq_Transmit_DMA(iic->handle, iic->dev_address, data, size, I2C_OTHER_AND_LAST_FRAME);
|
||||
else if (seq_mode == IIC_HOLD_ON)
|
||||
HAL_I2C_Master_Seq_Transmit_DMA(iic->handle, iic->dev_address, data, size, I2C_OTHER_FRAME);
|
||||
break;
|
||||
default:
|
||||
while (1)
|
||||
; // 未知传输模式, 程序停止
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void IICReceive(IICInstance *iic, uint8_t *data, uint16_t size, IIC_Seq_Mode_e seq_mode)
|
||||
{
|
||||
if (seq_mode != IIC_RELEASE && seq_mode != IIC_HOLD_ON)
|
||||
while (1)
|
||||
; // 未知传输模式, 程序停止
|
||||
|
||||
// 初始化接收缓冲区地址以及接受长度, 用于中断回调函数
|
||||
iic->rx_buffer = data;
|
||||
iic->rx_len = size;
|
||||
|
||||
switch (iic->work_mode)
|
||||
{
|
||||
case IIC_BLOCK_MODE:
|
||||
if (seq_mode != IIC_RELEASE)
|
||||
while (1)
|
||||
; // 阻塞模式下不支持HOLD ON模式!!!
|
||||
HAL_I2C_Master_Receive(iic->handle, iic->dev_address, data, size, 100);
|
||||
break;
|
||||
case IIC_IT_MODE:
|
||||
if (seq_mode == IIC_RELEASE)
|
||||
HAL_I2C_Master_Seq_Receive_IT(iic->handle, iic->dev_address, data, size, I2C_OTHER_AND_LAST_FRAME);
|
||||
else if (seq_mode == IIC_HOLD_ON)
|
||||
HAL_I2C_Master_Seq_Receive_IT(iic->handle, iic->dev_address, data, size, I2C_OTHER_FRAME);
|
||||
break;
|
||||
case IIC_DMA_MODE:
|
||||
if (seq_mode == IIC_RELEASE)
|
||||
HAL_I2C_Master_Seq_Receive_DMA(iic->handle, iic->dev_address, data, size, I2C_OTHER_AND_LAST_FRAME);
|
||||
else if (seq_mode == IIC_HOLD_ON)
|
||||
HAL_I2C_Master_Seq_Receive_DMA(iic->handle, iic->dev_address, data, size, I2C_OTHER_FRAME);
|
||||
break;
|
||||
default:
|
||||
while (1)
|
||||
; // 未知传输模式, 程序停止
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void IICAcessMem(IICInstance *iic, uint8_t mem_addr, uint8_t *data, uint16_t size, IIC_Mem_Mode_e mem_mode)
|
||||
{
|
||||
if (mem_mode == IIC_WRITE_MEM)
|
||||
{
|
||||
HAL_I2C_Mem_Write(iic->handle, iic->dev_address, mem_addr, I2C_MEMADD_SIZE_8BIT, data, size, 1000);
|
||||
}
|
||||
else if (mem_mode == IIC_READ_MEM)
|
||||
{
|
||||
HAL_I2C_Mem_Read(iic->handle, iic->dev_address, mem_addr, I2C_MEMADD_SIZE_8BIT, data, size, 1000);
|
||||
}
|
||||
else
|
||||
{
|
||||
while (1)
|
||||
; // 未知模式, 程序停止
|
||||
}
|
||||
}
|
||||
105
bsp/iic/bsp_iic.h
Normal file
105
bsp/iic/bsp_iic.h
Normal file
@@ -0,0 +1,105 @@
|
||||
#include "i2c.h"
|
||||
#include "stdint.h"
|
||||
|
||||
#define IIC_DEVICE_CNT 2 // C板引出了I2C2和I2C3
|
||||
#define MX_IIC_SLAVE_CNT 8 // 最大从机数目,根据需要修改
|
||||
|
||||
/* i2c 工作模式枚举 */
|
||||
typedef enum
|
||||
{
|
||||
// 基本工作模式
|
||||
IIC_BLOCK_MODE = 0, // 阻塞模式
|
||||
IIC_IT_MODE, // 中断模式
|
||||
IIC_DMA_MODE, // DMA模式
|
||||
|
||||
} IIC_Work_Mode_e;
|
||||
|
||||
/* I2C MEM工作模式枚举,这两种方法都是阻塞 */
|
||||
typedef enum
|
||||
{
|
||||
IIC_READ_MEM = 0, // 读取从机内部的寄存器or内存
|
||||
IIC_WRITE_MEM, // 写入从机内部的寄存器or内存
|
||||
} IIC_Mem_Mode_e;
|
||||
|
||||
/* Seq传输工作模式枚举,注意HOLD_ON要在IT或DMA下使用 */
|
||||
// 必须以IIC_RELEASE为最后一次传输,否则会导致总线占有权无法释放
|
||||
typedef enum
|
||||
{
|
||||
IIC_RELEASE, // 完成传输后释放总线占有权,这是默认的传输方式
|
||||
IIC_HOLD_ON = 0, // 保持总线占有权不释放,只支持IT和DMA模式
|
||||
} IIC_Seq_Mode_e;
|
||||
|
||||
/* i2c实例 */
|
||||
typedef struct iic_temp_s
|
||||
{
|
||||
I2C_HandleTypeDef *handle; // i2c handle
|
||||
uint8_t dev_address; // 暂时只支持7位地址(还有一位是读写位)
|
||||
|
||||
IIC_Work_Mode_e work_mode; // 工作模式
|
||||
uint8_t *rx_buffer; // 接收缓冲区指针
|
||||
uint8_t rx_len; // 接收长度
|
||||
void (*callback)(struct iic_temp_s *); // 接收完成后的回调函数
|
||||
|
||||
void *id; // 用于标识i2c instance
|
||||
} IICInstance;
|
||||
|
||||
/* I2C 初始化结构体配置 */
|
||||
typedef struct
|
||||
{
|
||||
I2C_HandleTypeDef *handle; // i2c handle
|
||||
uint8_t dev_address; // 暂时只支持7位地址(还有一位是读写位),注意不需要左移
|
||||
IIC_Work_Mode_e work_mode; // 工作模式
|
||||
void (*callback)(IICInstance *); // 接收完成后的回调函数
|
||||
void *id; // 用于标识i2c instance
|
||||
} IIC_Init_Config_s;
|
||||
|
||||
/**
|
||||
* @brief IIC初始化
|
||||
*
|
||||
* @param conf 初始化配置
|
||||
* @return IICInstance*
|
||||
*/
|
||||
IICInstance *IICRegister(IIC_Init_Config_s *conf);
|
||||
|
||||
/**
|
||||
* @brief IIC设置工作模式
|
||||
*
|
||||
* @param iic 要设置的iic实例
|
||||
* @param mode 工作模式
|
||||
*/
|
||||
void IICSetMode(IICInstance *iic, IIC_Work_Mode_e mode);
|
||||
|
||||
/**
|
||||
* @brief IIC发送数据
|
||||
*
|
||||
* @param iic iic实例
|
||||
* @param data 待发送的数据首地址指针
|
||||
* @param size 发送长度
|
||||
* @param mode 序列传输模式
|
||||
* @note 注意,如果发送结构体,那么该结构体在声明时务必使用#pragma pack(1)进行对齐,并在声明结束后使用#pragma pack()恢复对齐
|
||||
*
|
||||
*/
|
||||
void IICTransmit(IICInstance *iic, uint8_t *data, uint16_t size, IIC_Seq_Mode_e mode);
|
||||
|
||||
/**
|
||||
* @brief IIC接收数据
|
||||
*
|
||||
* @param iic iic实例
|
||||
* @param data 接收数据的首地址指针
|
||||
* @param size 接收长度
|
||||
* @param mode 序列传输模式
|
||||
* @note 注意,如果直接将接收数据memcpy到目标结构体或通过强制类型转换进行逐字节写入,
|
||||
* 那么该结构体在声明时务必使用#pragma pack(1)进行对齐,并在声明结束后使用#pragma pack()恢复对齐
|
||||
*/
|
||||
void IICReceive(IICInstance *iic, uint8_t *data, uint16_t size,IIC_Seq_Mode_e mode);
|
||||
|
||||
/**
|
||||
* @brief IIC读取从机寄存器(内存),只支持阻塞模式,超时默认为1ms
|
||||
*
|
||||
* @param iic iic实例
|
||||
* @param mem_addr 要读取的从机内存地址,目前只支持8位地址
|
||||
* @param data 要读取或写入的数据首地址指针
|
||||
* @param size 要读取或写入的数据长度
|
||||
* @param mode 写入或读取模式: IIC_READ_MEM or IIC_WRITE_MEM
|
||||
*/
|
||||
void IICAcessMem(IICInstance *iic, uint8_t mem_addr, uint8_t *data, uint16_t size, IIC_Mem_Mode_e mode);
|
||||
0
bsp/iic/bsp_iic.md
Normal file
0
bsp/iic/bsp_iic.md
Normal file
0
bsp/pwm/bsp_pwm.c
Normal file
0
bsp/pwm/bsp_pwm.c
Normal file
0
bsp/pwm/bsp_pwm.h
Normal file
0
bsp/pwm/bsp_pwm.h
Normal file
@@ -15,11 +15,16 @@ void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
|
||||
{
|
||||
for (size_t i = 0; i < idx; i++)
|
||||
{
|
||||
if (spi_instance[i]->spi_handle == hspi && spi_instance[i]->callback)
|
||||
// 如果是当前spi硬件发出的complete,且cs_pin为低电平(说明正在传输),则尝试调用回调函数
|
||||
if (spi_instance[i]->spi_handle == hspi &&
|
||||
HAL_GPIO_ReadPin(spi_instance[i]->GPIO_cs, spi_instance[i]->cs_pin) == GPIO_PIN_RESET)
|
||||
{
|
||||
// 拉高片选(关闭传输),调用解析回调函数
|
||||
HAL_GPIO_WritePin(spi_instance[i]->GPIO_cs, spi_instance[i]->cs_pin, GPIO_PIN_SET);
|
||||
spi_instance[i]->callback(spi_instance[i]);
|
||||
if (spi_instance[i]->callback) // 回调函数不为空, 则调用回调函数
|
||||
{
|
||||
// 先拉高片选,结束传输
|
||||
HAL_GPIO_WritePin(spi_instance[i]->GPIO_cs, spi_instance[i]->cs_pin, GPIO_PIN_SET);
|
||||
spi_instance[i]->callback(spi_instance[i]);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -41,6 +46,9 @@ SPIInstance *SPIRegister(SPI_Init_Config_s *conf)
|
||||
spi_instance[idx]->callback = conf->callback;
|
||||
spi_instance[idx]->spi_work_mode = conf->spi_work_mode;
|
||||
spi_instance[idx]->spi_handle = conf->spi_handle;
|
||||
spi_instance[idx]->GPIO_cs = conf->GPIO_cs;
|
||||
spi_instance[idx]->cs_pin = conf->cs_pin;
|
||||
spi_instance[idx]->id = conf->id;
|
||||
return spi_instance[idx++];
|
||||
}
|
||||
|
||||
@@ -70,6 +78,9 @@ void SPITransmit(SPIInstance *spi_ins, uint8_t *ptr_data, uint8_t len)
|
||||
|
||||
void SPIRecv(SPIInstance *spi_ins, uint8_t *ptr_data, uint8_t len)
|
||||
{
|
||||
// 用于稍后回调使用
|
||||
spi_ins->rx_size = len;
|
||||
spi_ins->rx_buffer = ptr_data;
|
||||
// 拉低片选,开始传输
|
||||
HAL_GPIO_WritePin(spi_ins->GPIO_cs, spi_ins->cs_pin, GPIO_PIN_RESET);
|
||||
switch (spi_ins->spi_work_mode)
|
||||
@@ -94,6 +105,9 @@ void SPIRecv(SPIInstance *spi_ins, uint8_t *ptr_data, uint8_t len)
|
||||
|
||||
void SPITransRecv(SPIInstance *spi_ins, uint8_t *ptr_data_rx, uint8_t *ptr_data_tx, uint8_t len)
|
||||
{
|
||||
// 用于稍后回调使用
|
||||
spi_ins->rx_size = len;
|
||||
spi_ins->rx_buffer = ptr_data_rx;
|
||||
// 拉低片选,开始传输
|
||||
HAL_GPIO_WritePin(spi_ins->GPIO_cs, spi_ins->cs_pin, GPIO_PIN_RESET);
|
||||
switch (spi_ins->spi_work_mode)
|
||||
@@ -118,26 +132,12 @@ void SPITransRecv(SPIInstance *spi_ins, uint8_t *ptr_data_rx, uint8_t *ptr_data_
|
||||
|
||||
void SPISetMode(SPIInstance *spi_ins, SPI_TXRX_MODE_e spi_mode)
|
||||
{
|
||||
if (spi_mode != SPI_DMA_MODE && spi_mode != SPI_IT_MODE && spi_mode != SPI_BLOCK_MODE)
|
||||
while (1)
|
||||
; // error mode! 请查看是否正确设置模式,或出现指针越界导致模式被异常修改的情况
|
||||
|
||||
if (spi_ins->spi_work_mode != spi_mode)
|
||||
{
|
||||
switch (spi_ins->spi_work_mode)
|
||||
{
|
||||
case SPI_IT_MODE:
|
||||
case SPI_DMA_MODE:
|
||||
// IT和DMA处理相同,都是先终止传输,防止传输未完成直接切换导致spi死机
|
||||
HAL_SPI_Abort_IT(spi_ins->spi_handle);
|
||||
HAL_GPIO_WritePin(spi_ins->GPIO_cs, spi_ins->cs_pin, GPIO_PIN_SET); // 关闭后拉高片选
|
||||
break;
|
||||
case SPI_BLOCK_MODE:
|
||||
// 阻塞模式仍然有可能在多线程的情况下出现传输到一半切换,因此先终止
|
||||
HAL_SPI_Abort(spi_ins->spi_handle);
|
||||
HAL_GPIO_WritePin(spi_ins->GPIO_cs, spi_ins->cs_pin, GPIO_PIN_SET); // 关闭后拉高片选
|
||||
break;
|
||||
default:
|
||||
while (1)
|
||||
; // error mode! 请查看是否正确设置模式,或出现指针越界导致模式被异常修改的情况
|
||||
break;
|
||||
}
|
||||
spi_ins->spi_work_mode = spi_mode;
|
||||
}
|
||||
}
|
||||
@@ -17,24 +17,32 @@ typedef enum
|
||||
/* SPI实例结构体定义 */
|
||||
typedef struct spi_ins_temp
|
||||
{
|
||||
SPI_HandleTypeDef *spi_handle; // SPI外设handle
|
||||
GPIO_TypeDef *GPIO_cs; // 片选信号对应的GPIO,如GPIOA,GPIOB等等
|
||||
uint16_t cs_pin; // 片选信号对应的引脚号,GPIO_PIN_1,GPIO_PIN_2等等
|
||||
SPI_HandleTypeDef *spi_handle; // SPI外设handle
|
||||
GPIO_TypeDef *GPIO_cs; // 片选信号对应的GPIO,如GPIOA,GPIOB等等
|
||||
uint16_t cs_pin; // 片选信号对应的引脚号,GPIO_PIN_1,GPIO_PIN_2等等
|
||||
|
||||
SPI_TXRX_MODE_e spi_work_mode; // 传输工作模式
|
||||
uint8_t rx_size; // 本次接收的数据长度
|
||||
uint8_t *rx_buffer; // 本次接收的数据缓冲区
|
||||
void (*callback)(struct spi_ins_temp *); // 接收回调函数
|
||||
|
||||
void *id; // 模块指针
|
||||
} SPIInstance;
|
||||
|
||||
/* rx data resolve callback*/
|
||||
typedef void (*spi_rx_callback)(SPIInstance *);
|
||||
|
||||
/* SPI初始化配置,其实和SPIIstance一模一样,为了代码风格统一因此再次定义 */
|
||||
/* SPI初始化配置,其实基本和SPIIstance一模一样,为了代码风格统一因此再次定义 */
|
||||
typedef struct
|
||||
{
|
||||
SPI_HandleTypeDef *spi_handle; // SPI外设handle
|
||||
GPIO_TypeDef *GPIO_cs; // 片选信号对应的GPIO,如GPIOA,GPIOB等等
|
||||
uint16_t cs_pin; // 片选信号对应的引脚号,GPIO_PIN_1,GPIO_PIN_2等等
|
||||
|
||||
SPI_TXRX_MODE_e spi_work_mode; // 传输工作模式
|
||||
spi_rx_callback callback; // 接收回调函数
|
||||
|
||||
spi_rx_callback callback; // 接收回调函数
|
||||
void *id; // 模块指针
|
||||
} SPI_Init_Config_s;
|
||||
|
||||
/**
|
||||
@@ -78,5 +86,8 @@ void SPITransRecv(SPIInstance *spi_ins, uint8_t *ptr_data_rx, uint8_t *ptr_data_
|
||||
*
|
||||
* @param spi_ins spi实例指针
|
||||
* @param spi_mode 工作模式,包括阻塞模式(block),中断模式(IT),DMA模式.详见SPI_TXRX_MODE_e的定义
|
||||
* @param force_set_flag 强制设置标志,当该标志为1时,强制停止当前spi的收发,并切换到新的工作模式;
|
||||
* 当该标志为0时,如果当前spi正在收发,则不会切换工作模式,等待传输完成后切换.
|
||||
* @todo HAL已经提供了防止重入的机制,因此强制设置标志可以去掉,也不需要再判断spi是否正在收发
|
||||
*/
|
||||
void SPISetMode(SPIInstance *spi_ins, SPI_TXRX_MODE_e spi_mode);
|
||||
Reference in New Issue
Block a user