From ab1a9726b16bf56f42b30836288070a099c0d020 Mon Sep 17 00:00:00 2001 From: NeoZng Date: Wed, 28 Dec 2022 23:12:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86spi=E7=9A=84bsp?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 3 + basic_framework.ioc | 78 +++++++++++++++++++- bsp/bsp_can.c | 1 + bsp/bsp_iic.c | 4 ++ bsp/bsp_iic.h | 11 +++ bsp/bsp_pwm.c | 0 bsp/bsp_pwm.h | 0 bsp/bsp_spi.c | 143 +++++++++++++++++++++++++++++++++++++ bsp/bsp_spi.h | 82 ++++++++++++++++++++- modules/imu/BMI088driver.c | 2 +- 10 files changed, 320 insertions(+), 4 deletions(-) create mode 100644 bsp/bsp_pwm.c create mode 100644 bsp/bsp_pwm.h diff --git a/Makefile b/Makefile index 7271da0..4f243aa 100644 --- a/Makefile +++ b/Makefile @@ -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 \ diff --git a/basic_framework.ioc b/basic_framework.ioc index 38cc33d..1bd1e36 100644 --- a/basic_framework.ioc +++ b/basic_framework.ioc @@ -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 diff --git a/bsp/bsp_can.c b/bsp/bsp_can.c index 71a16c4..4549afe 100644 --- a/bsp/bsp_can.c +++ b/bsp/bsp_can.c @@ -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与发生中断的实例相同的那个,调用其回调函数 diff --git a/bsp/bsp_iic.c b/bsp/bsp_iic.c index e69de29..805ffed 100644 --- a/bsp/bsp_iic.c +++ b/bsp/bsp_iic.c @@ -0,0 +1,4 @@ +#include "bsp_iic.h" +#include "memory.h" +#include "stdlib.h" + diff --git a/bsp/bsp_iic.h b/bsp/bsp_iic.h index e69de29..aafc249 100644 --- a/bsp/bsp_iic.h +++ b/bsp/bsp_iic.h @@ -0,0 +1,11 @@ +#include "i2c.h" +#include "stdint.h" + +#define IIC_DEVICE_CNT () + +typedef struct +{ + I2C_HandleTypeDef* handle; + +}; + diff --git a/bsp/bsp_pwm.c b/bsp/bsp_pwm.c new file mode 100644 index 0000000..e69de29 diff --git a/bsp/bsp_pwm.h b/bsp/bsp_pwm.h new file mode 100644 index 0000000..e69de29 diff --git a/bsp/bsp_spi.c b/bsp/bsp_spi.c index e69de29..184a89c 100644 --- a/bsp/bsp_spi.c +++ b/bsp/bsp_spi.c @@ -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; + } +} diff --git a/bsp/bsp_spi.h b/bsp/bsp_spi.h index f289901..c428333 100644 --- a/bsp/bsp_spi.h +++ b/bsp/bsp_spi.h @@ -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); diff --git a/modules/imu/BMI088driver.c b/modules/imu/BMI088driver.c index 787a304..db2d2eb 100644 --- a/modules/imu/BMI088driver.c +++ b/modules/imu/BMI088driver.c @@ -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;