增加了spi的bsp支持

This commit is contained in:
NeoZng 2022-12-28 23:12:25 +08:00
parent 25a0e24529
commit ab1a9726b1
10 changed files with 320 additions and 4 deletions

View File

@ -102,8 +102,11 @@ HAL_N_Middlewares/Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM4F/
HAL_N_Middlewares/Middlewares/Third_Party/SEGGER/RTT/SEGGER_RTT_printf.c \
HAL_N_Middlewares/Middlewares/Third_Party/SEGGER/RTT/SEGGER_RTT.c \
bsp/bsp_dwt.c \
bsp/bsp_pwm.c \
bsp/bsp_temperature.c \
bsp/bsp_led.c \
bsp/bsp_spi.c \
bsp/bsp_iic.c \
bsp/bsp_can.c \
bsp/bsp_buzzer.c \
bsp/bsp_usart.c \

View File

@ -21,12 +21,82 @@ CAN2.CalculateBaudRate=1000000
CAN2.CalculateTimeQuantum=71.42857142857143
CAN2.IPParameters=CalculateTimeQuantum,BS1,BS2,Prescaler,CalculateBaudRate
CAN2.Prescaler=3
Dma.MEMTOMEM.5.Direction=DMA_MEMORY_TO_MEMORY
Dma.MEMTOMEM.5.FIFOMode=DMA_FIFOMODE_ENABLE
Dma.MEMTOMEM.5.FIFOThreshold=DMA_FIFO_THRESHOLD_FULL
Dma.MEMTOMEM.5.Instance=DMA2_Stream3
Dma.MEMTOMEM.5.MemBurst=DMA_MBURST_SINGLE
Dma.MEMTOMEM.5.MemDataAlignment=DMA_MDATAALIGN_BYTE
Dma.MEMTOMEM.5.MemInc=DMA_MINC_ENABLE
Dma.MEMTOMEM.5.Mode=DMA_NORMAL
Dma.MEMTOMEM.5.PeriphBurst=DMA_PBURST_SINGLE
Dma.MEMTOMEM.5.PeriphDataAlignment=DMA_PDATAALIGN_BYTE
Dma.MEMTOMEM.5.PeriphInc=DMA_PINC_ENABLE
Dma.MEMTOMEM.5.Priority=DMA_PRIORITY_HIGH
Dma.MEMTOMEM.5.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode,FIFOThreshold,MemBurst,PeriphBurst
Dma.Request0=USART3_RX
Dma.Request1=USART6_RX
Dma.Request2=USART6_TX
Dma.Request3=USART1_TX
Dma.Request4=USART1_RX
Dma.RequestsNb=5
Dma.Request5=MEMTOMEM
Dma.Request6=SPI1_RX
Dma.Request7=SPI1_TX
Dma.Request8=SPI2_RX
Dma.Request9=SPI2_TX
Dma.RequestsNb=10
Dma.SPI1_RX.6.Direction=DMA_PERIPH_TO_MEMORY
Dma.SPI1_RX.6.FIFOMode=DMA_FIFOMODE_ENABLE
Dma.SPI1_RX.6.FIFOThreshold=DMA_FIFO_THRESHOLD_FULL
Dma.SPI1_RX.6.Instance=DMA2_Stream0
Dma.SPI1_RX.6.MemBurst=DMA_MBURST_SINGLE
Dma.SPI1_RX.6.MemDataAlignment=DMA_MDATAALIGN_BYTE
Dma.SPI1_RX.6.MemInc=DMA_MINC_ENABLE
Dma.SPI1_RX.6.Mode=DMA_NORMAL
Dma.SPI1_RX.6.PeriphBurst=DMA_PBURST_SINGLE
Dma.SPI1_RX.6.PeriphDataAlignment=DMA_PDATAALIGN_BYTE
Dma.SPI1_RX.6.PeriphInc=DMA_PINC_DISABLE
Dma.SPI1_RX.6.Priority=DMA_PRIORITY_LOW
Dma.SPI1_RX.6.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode,FIFOThreshold,MemBurst,PeriphBurst
Dma.SPI1_TX.7.Direction=DMA_MEMORY_TO_PERIPH
Dma.SPI1_TX.7.FIFOMode=DMA_FIFOMODE_ENABLE
Dma.SPI1_TX.7.FIFOThreshold=DMA_FIFO_THRESHOLD_FULL
Dma.SPI1_TX.7.Instance=DMA2_Stream5
Dma.SPI1_TX.7.MemBurst=DMA_MBURST_SINGLE
Dma.SPI1_TX.7.MemDataAlignment=DMA_MDATAALIGN_BYTE
Dma.SPI1_TX.7.MemInc=DMA_MINC_ENABLE
Dma.SPI1_TX.7.Mode=DMA_NORMAL
Dma.SPI1_TX.7.PeriphBurst=DMA_PBURST_SINGLE
Dma.SPI1_TX.7.PeriphDataAlignment=DMA_PDATAALIGN_BYTE
Dma.SPI1_TX.7.PeriphInc=DMA_PINC_DISABLE
Dma.SPI1_TX.7.Priority=DMA_PRIORITY_LOW
Dma.SPI1_TX.7.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode,FIFOThreshold,MemBurst,PeriphBurst
Dma.SPI2_RX.8.Direction=DMA_PERIPH_TO_MEMORY
Dma.SPI2_RX.8.FIFOMode=DMA_FIFOMODE_ENABLE
Dma.SPI2_RX.8.FIFOThreshold=DMA_FIFO_THRESHOLD_FULL
Dma.SPI2_RX.8.Instance=DMA1_Stream3
Dma.SPI2_RX.8.MemBurst=DMA_MBURST_SINGLE
Dma.SPI2_RX.8.MemDataAlignment=DMA_MDATAALIGN_BYTE
Dma.SPI2_RX.8.MemInc=DMA_MINC_ENABLE
Dma.SPI2_RX.8.Mode=DMA_NORMAL
Dma.SPI2_RX.8.PeriphBurst=DMA_PBURST_SINGLE
Dma.SPI2_RX.8.PeriphDataAlignment=DMA_PDATAALIGN_BYTE
Dma.SPI2_RX.8.PeriphInc=DMA_PINC_DISABLE
Dma.SPI2_RX.8.Priority=DMA_PRIORITY_LOW
Dma.SPI2_RX.8.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode,FIFOThreshold,MemBurst,PeriphBurst
Dma.SPI2_TX.9.Direction=DMA_MEMORY_TO_PERIPH
Dma.SPI2_TX.9.FIFOMode=DMA_FIFOMODE_ENABLE
Dma.SPI2_TX.9.FIFOThreshold=DMA_FIFO_THRESHOLD_FULL
Dma.SPI2_TX.9.Instance=DMA1_Stream4
Dma.SPI2_TX.9.MemBurst=DMA_MBURST_SINGLE
Dma.SPI2_TX.9.MemDataAlignment=DMA_MDATAALIGN_BYTE
Dma.SPI2_TX.9.MemInc=DMA_MINC_ENABLE
Dma.SPI2_TX.9.Mode=DMA_NORMAL
Dma.SPI2_TX.9.PeriphBurst=DMA_PBURST_SINGLE
Dma.SPI2_TX.9.PeriphDataAlignment=DMA_PDATAALIGN_BYTE
Dma.SPI2_TX.9.PeriphInc=DMA_PINC_DISABLE
Dma.SPI2_TX.9.Priority=DMA_PRIORITY_LOW
Dma.SPI2_TX.9.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode,FIFOThreshold,MemBurst,PeriphBurst
Dma.USART1_RX.4.Direction=DMA_PERIPH_TO_MEMORY
Dma.USART1_RX.4.FIFOMode=DMA_FIFOMODE_DISABLE
Dma.USART1_RX.4.Instance=DMA2_Stream2
@ -185,8 +255,12 @@ NVIC.CAN1_RX1_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true
NVIC.CAN2_RX0_IRQn=true\:6\:0\:true\:false\:true\:true\:true\:true\:true
NVIC.CAN2_RX1_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true
NVIC.DMA1_Stream1_IRQn=true\:5\:0\:true\:false\:true\:true\:false\:true\:true
NVIC.DMA1_Stream3_IRQn=true\:5\:0\:false\:false\:true\:true\:false\:true\:true
NVIC.DMA1_Stream4_IRQn=true\:5\:0\:false\:false\:true\:true\:false\:true\:true
NVIC.DMA2_Stream0_IRQn=true\:5\:0\:false\:false\:true\:true\:false\:true\:true
NVIC.DMA2_Stream1_IRQn=true\:5\:0\:true\:false\:true\:true\:false\:true\:true
NVIC.DMA2_Stream2_IRQn=true\:5\:0\:false\:false\:true\:true\:false\:true\:true
NVIC.DMA2_Stream5_IRQn=true\:5\:0\:false\:false\:true\:true\:false\:true\:true
NVIC.DMA2_Stream6_IRQn=true\:5\:0\:true\:false\:true\:true\:false\:true\:true
NVIC.DMA2_Stream7_IRQn=true\:5\:0\:true\:false\:true\:true\:false\:true\:true
NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:false
@ -197,6 +271,8 @@ NVIC.NonMaskableInt_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false\:fa
NVIC.OTG_FS_IRQn=true\:5\:0\:false\:false\:true\:true\:false\:true\:true
NVIC.PendSV_IRQn=true\:15\:0\:false\:false\:false\:true\:false\:false\:false
NVIC.PriorityGroup=NVIC_PRIORITYGROUP_4
NVIC.SPI1_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true
NVIC.SPI2_IRQn=true\:5\:0\:false\:false\:true\:true\:true\:true\:true
NVIC.SVCall_IRQn=true\:0\:0\:false\:false\:false\:false\:false\:false\:false
NVIC.SavedPendsvIrqHandlerGenerated=true
NVIC.SavedSvcallIrqHandlerGenerated=true

View File

@ -1,6 +1,7 @@
#include "bsp_can.h"
#include "main.h"
#include "memory.h"
#include "stdlib.h"
/* can instance ptrs storage, used for recv callback */
// 在CAN产生接收中断会遍历数组,选出hcan和rxid与发生中断的实例相同的那个,调用其回调函数

View File

@ -0,0 +1,4 @@
#include "bsp_iic.h"
#include "memory.h"
#include "stdlib.h"

View File

@ -0,0 +1,11 @@
#include "i2c.h"
#include "stdint.h"
#define IIC_DEVICE_CNT ()
typedef struct
{
I2C_HandleTypeDef* handle;
};

0
bsp/bsp_pwm.c Normal file
View File

0
bsp/bsp_pwm.h Normal file
View File

View File

@ -0,0 +1,143 @@
#include "bsp_spi.h"
#include "memory.h"
#include "stdlib.h"
/* 所有的spi instance保存于此,用于callback时判断中断来源*/
static SPIInstance *spi_instance[SPI_DEVICE_CNT] = {NULL};
static uint8_t idx = 0; // 配合中断以及初始化
/**
* @brief SPI接收完成,,
*
* @param hspi spi handle
*/
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)
{
// 拉高片选(关闭传输),调用解析回调函数
HAL_GPIO_WritePin(spi_instance[i]->GPIO_cs, spi_instance[i]->cs_pin, GPIO_PIN_SET);
spi_instance[i]->callback(spi_instance[i]);
break;
}
}
}
/**
* @brief RxCpltCallback共用解析即可,,
*
* @param hspi spi handle
*/
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
{
HAL_SPI_RxCpltCallback(hspi);
}
SPIInstance *SPIRegister(SPI_Init_Config_s *conf)
{
spi_instance[idx] = (SPIInstance *)malloc(sizeof(SPIInstance));
spi_instance[idx]->callback = conf->callback;
spi_instance[idx]->spi_work_mode = conf->spi_work_mode;
spi_instance[idx]->spi_handle = conf->spi_handle;
return spi_instance[idx++];
}
void SPITransmit(SPIInstance *spi_ins, uint8_t *ptr_data, uint8_t len)
{
// 拉低片选,开始传输
HAL_GPIO_WritePin(spi_ins->GPIO_cs, spi_ins->cs_pin, GPIO_PIN_RESET);
switch (spi_ins->spi_work_mode)
{
case SPI_DMA_MODE:
HAL_SPI_Transmit_DMA(spi_ins->spi_handle, ptr_data, len);
break;
case SPI_IT_MODE:
HAL_SPI_Transmit_IT(spi_ins->spi_handle, ptr_data, len);
break;
case SPI_BLOCK_MODE:
HAL_SPI_Transmit(spi_ins->spi_handle, ptr_data, len, 10);
// 阻塞模式不会调用回调函数,传输完成后直接拉高片选结束
HAL_GPIO_WritePin(spi_ins->GPIO_cs, spi_ins->cs_pin, GPIO_PIN_SET);
break;
default:
while (1)
; // error mode! 请查看是否正确设置模式,或出现指针越界导致模式被异常修改的情况
break;
}
}
void SPIRecv(SPIInstance *spi_ins, uint8_t *ptr_data, uint8_t len)
{
// 拉低片选,开始传输
HAL_GPIO_WritePin(spi_ins->GPIO_cs, spi_ins->cs_pin, GPIO_PIN_RESET);
switch (spi_ins->spi_work_mode)
{
case SPI_DMA_MODE:
HAL_SPI_Receive_DMA(spi_ins->spi_handle, ptr_data, len);
break;
case SPI_IT_MODE:
HAL_SPI_Receive_IT(spi_ins->spi_handle, ptr_data, len);
break;
case SPI_BLOCK_MODE:
HAL_SPI_Receive(spi_ins->spi_handle, ptr_data, len, 10);
// 阻塞模式不会调用回调函数,传输完成后直接拉高片选结束
HAL_GPIO_WritePin(spi_ins->GPIO_cs, spi_ins->cs_pin, GPIO_PIN_SET);
break;
default:
while (1)
; // error mode! 请查看是否正确设置模式,或出现指针越界导致模式被异常修改的情况
break;
}
}
void SPITransRecv(SPIInstance *spi_ins, uint8_t *ptr_data_rx, uint8_t *ptr_data_tx, uint8_t len)
{
// 拉低片选,开始传输
HAL_GPIO_WritePin(spi_ins->GPIO_cs, spi_ins->cs_pin, GPIO_PIN_RESET);
switch (spi_ins->spi_work_mode)
{
case SPI_DMA_MODE:
HAL_SPI_TransmitReceive_DMA(spi_ins->spi_handle, ptr_data_tx, ptr_data_rx, len);
break;
case SPI_IT_MODE:
HAL_SPI_TransmitReceive_IT(spi_ins->spi_handle, ptr_data_tx, ptr_data_rx, len);
break;
case SPI_BLOCK_MODE:
HAL_SPI_TransmitReceive(spi_ins->spi_handle, ptr_data_tx, ptr_data_rx, len, 10);
// 阻塞模式不会调用回调函数,传输完成后直接拉高片选结束
HAL_GPIO_WritePin(spi_ins->GPIO_cs, spi_ins->cs_pin, GPIO_PIN_SET);
break;
default:
while (1)
; // error mode! 请查看是否正确设置模式,或出现指针越界导致模式被异常修改的情况
break;
}
}
void SPISetMode(SPIInstance *spi_ins, SPI_TXRX_MODE_e spi_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;
}
}

View File

@ -1,4 +1,82 @@
#include "stdint.h"
#include "spi.h"
#include "stdint.h"
#include "gpio.h"
/* 根据开发板引出的spi引脚以及CubeMX中的初始化配置设定 */
#define SPI_DEVICE_CNT 2 // C型开发板引出两路spi,分别连接BMI088/作为扩展IO在8pin牛角座引出
#define MX_SPI_BUS_SLAVE_CNT 4 // 单个spi总线上挂载的从机数目
/* spi transmit recv mode enumerate*/
typedef enum
{
SPI_BLOCK_MODE = 0, // 默认使用阻塞模式
SPI_IT_MODE,
SPI_DMA_MODE,
} SPI_TXRX_MODE_e;
/* 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_TXRX_MODE_e spi_work_mode; // 传输工作模式
void (*callback)(struct spi_ins_temp *); // 接收回调函数
} SPIInstance;
/* rx data resolve callback*/
typedef void (*spi_rx_callback)(SPIInstance *);
/* 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_Init_Config_s;
/**
* @brief spi instance
*
* @param conf spi配置
* @return SPIInstance* spi实例指针,spi外设
*/
SPIInstance *SPIRegister(SPI_Init_Config_s *conf);
/**
* @brief spi向对应从机发送数据
*
* @param spi_ins spi实例指针
* @param ptr_data
* @param len
*/
void SPITransmit(SPIInstance *spi_ins, uint8_t *ptr_data, uint8_t len);
/**
* @brief spi从从机获取数据
*
* @param spi_ins spi实例指针
* @param ptr_data buffer的首地址
* @param len
*/
void SPIRecv(SPIInstance *spi_ins, uint8_t *ptr_data, uint8_t len);
/**
* @brief spi利用移位寄存器同时收发数据
*
* @param spi_ins spi实例指针
* @param ptr_data_rx
* @param ptr_data_tx
* @param len &
*/
void SPITransRecv(SPIInstance *spi_ins, uint8_t *ptr_data_rx, uint8_t *ptr_data_tx, uint8_t len);
/**
* @brief spi收发的工作模式
*
* @param spi_ins spi实例指针
* @param spi_mode ,(block),(IT),DMA模式.SPI_TXRX_MODE_e的定义
*/
void SPISetMode(SPIInstance *spi_ins, SPI_TXRX_MODE_e spi_mode);

View File

@ -317,7 +317,7 @@ uint8_t bmi088_gyro_init(void)
void BMI088_Read(IMU_Data_t *bmi088)
{
static uint8_t buf[8] = {0, 0, 0, 0, 0, 0};
static uint8_t buf[8] = {0};
static int16_t bmi088_raw_temp;
static float dt = 1.0;
static uint8_t first_read_flag = 0;