From 7e36f6a0cf00466344f1988263659d364a08b2e2 Mon Sep 17 00:00:00 2001 From: NeoZng Date: Sun, 1 Jan 2023 12:45:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86BSP=20PWM=E7=9A=84?= =?UTF-8?q?=E6=94=AF=E6=8C=81,=E4=BF=AE=E6=94=B9=E4=BA=86bsp=E5=92=8Cmodul?= =?UTF-8?q?e=E5=B1=82=E7=9A=84=E5=88=9D=E5=A7=8B=E5=8C=96=E6=8F=90?= =?UTF-8?q?=E9=AB=98=E5=8F=AF=E8=AF=BB=E6=80=A7,=E6=96=B0=E5=B9=B4?= =?UTF-8?q?=E5=BF=AB=E4=B9=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 3 - basic_framework.ioc | 285 ++++++++------------------ bsp/can/bsp_can.c | 45 ++-- bsp/dac/bsp_dac.h | 0 bsp/dac/bsp_dac.md | 0 bsp/iic/bsp_iic.c | 18 +- bsp/pwm/bsp_pwm.c | 67 ++++++ bsp/pwm/bsp_pwm.h | 71 +++++++ bsp/{dac/bsp_dac.c => pwm/bsp_pwm.md} | 0 bsp/spi/bsp_spi.c | 21 +- bsp/usart/bsp_usart.c | 52 ++--- modules/can_comm/can_comm.c | 4 +- modules/can_comm/can_comm.h | 10 +- modules/daemon/daemon.c | 34 +-- modules/daemon/daemon.h | 18 +- modules/motor/HT04.c | 32 +-- modules/motor/LK9025.c | 27 +-- modules/motor/dji_motor.c | 172 ++++++++-------- modules/motor/servo_motor.c | 31 +-- modules/super_cap/super_cap.c | 1 + modules/super_cap/super_cap.h | 2 - 21 files changed, 469 insertions(+), 424 deletions(-) delete mode 100644 bsp/dac/bsp_dac.h delete mode 100644 bsp/dac/bsp_dac.md rename bsp/{dac/bsp_dac.c => pwm/bsp_pwm.md} (100%) diff --git a/Makefile b/Makefile index 0319ba7..de76c51 100644 --- a/Makefile +++ b/Makefile @@ -40,7 +40,6 @@ C_SOURCES = \ HAL_N_Middlewares/Src/main.c \ HAL_N_Middlewares/Src/gpio.c \ HAL_N_Middlewares/Src/i2c.c \ -HAL_N_Middlewares/Src/i2c.c \ HAL_N_Middlewares/Src/adc.c \ HAL_N_Middlewares/Src/can.c \ HAL_N_Middlewares/Src/dma.c \ @@ -68,8 +67,6 @@ HAL_N_Middlewares/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c HAL_N_Middlewares/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c \ HAL_N_Middlewares/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c_ex.c \ HAL_N_Middlewares/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c \ -HAL_N_Middlewares/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c_ex.c \ -HAL_N_Middlewares/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_i2c.c \ HAL_N_Middlewares/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c \ HAL_N_Middlewares/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c \ HAL_N_Middlewares/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c \ diff --git a/basic_framework.ioc b/basic_framework.ioc index 52eb390..fbe356b 100644 --- a/basic_framework.ioc +++ b/basic_framework.ioc @@ -9,9 +9,6 @@ ADC1.master=1 CAD.formats= CAD.pinconfig= CAD.provider= -CAD.formats= -CAD.pinconfig= -CAD.provider= CAN1.BS1=CAN_BS1_10TQ CAN1.BS2=CAN_BS2_3TQ CAN1.CalculateBaudRate=1000000 @@ -24,87 +21,55 @@ 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.ADC1.12.Direction=DMA_PERIPH_TO_MEMORY -Dma.ADC1.12.FIFOMode=DMA_FIFOMODE_DISABLE -Dma.ADC1.12.Instance=DMA2_Stream4 -Dma.ADC1.12.MemDataAlignment=DMA_MDATAALIGN_HALFWORD -Dma.ADC1.12.MemInc=DMA_MINC_ENABLE -Dma.ADC1.12.Mode=DMA_NORMAL -Dma.ADC1.12.PeriphDataAlignment=DMA_PDATAALIGN_HALFWORD -Dma.ADC1.12.PeriphInc=DMA_PINC_DISABLE -Dma.ADC1.12.Priority=DMA_PRIORITY_LOW -Dma.ADC1.12.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode -Dma.I2C2_RX.8.Direction=DMA_PERIPH_TO_MEMORY -Dma.I2C2_RX.8.FIFOMode=DMA_FIFOMODE_DISABLE -Dma.I2C2_RX.8.Instance=DMA1_Stream2 -Dma.I2C2_RX.8.MemDataAlignment=DMA_MDATAALIGN_BYTE -Dma.I2C2_RX.8.MemInc=DMA_MINC_ENABLE -Dma.I2C2_RX.8.Mode=DMA_NORMAL -Dma.I2C2_RX.8.PeriphDataAlignment=DMA_PDATAALIGN_BYTE -Dma.I2C2_RX.8.PeriphInc=DMA_PINC_DISABLE -Dma.I2C2_RX.8.Priority=DMA_PRIORITY_LOW -Dma.I2C2_RX.8.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode -Dma.I2C2_TX.9.Direction=DMA_MEMORY_TO_PERIPH -Dma.I2C2_TX.9.FIFOMode=DMA_FIFOMODE_DISABLE -Dma.I2C2_TX.9.Instance=DMA1_Stream7 -Dma.I2C2_TX.9.MemDataAlignment=DMA_MDATAALIGN_BYTE -Dma.I2C2_TX.9.MemInc=DMA_MINC_ENABLE -Dma.I2C2_TX.9.Mode=DMA_NORMAL -Dma.I2C2_TX.9.PeriphDataAlignment=DMA_PDATAALIGN_BYTE -Dma.I2C2_TX.9.PeriphInc=DMA_PINC_DISABLE -Dma.I2C2_TX.9.Priority=DMA_PRIORITY_LOW -Dma.I2C2_TX.9.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode -Dma.MEMTOMEM.11.Direction=DMA_MEMORY_TO_MEMORY -Dma.MEMTOMEM.11.FIFOMode=DMA_FIFOMODE_ENABLE -Dma.MEMTOMEM.11.FIFOThreshold=DMA_FIFO_THRESHOLD_FULL -Dma.MEMTOMEM.11.Instance=DMA2_Stream5 -Dma.MEMTOMEM.11.MemBurst=DMA_MBURST_SINGLE -Dma.MEMTOMEM.11.MemDataAlignment=DMA_MDATAALIGN_BYTE -Dma.MEMTOMEM.11.MemInc=DMA_MINC_ENABLE -Dma.MEMTOMEM.11.Mode=DMA_NORMAL -Dma.MEMTOMEM.11.PeriphBurst=DMA_PBURST_SINGLE -Dma.MEMTOMEM.11.PeriphDataAlignment=DMA_PDATAALIGN_BYTE -Dma.MEMTOMEM.11.PeriphInc=DMA_PINC_ENABLE -Dma.MEMTOMEM.11.Priority=DMA_PRIORITY_LOW -Dma.MEMTOMEM.11.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode,FIFOThreshold,MemBurst,PeriphBurst +Dma.ADC1.8.Direction=DMA_PERIPH_TO_MEMORY +Dma.ADC1.8.FIFOMode=DMA_FIFOMODE_DISABLE +Dma.ADC1.8.Instance=DMA2_Stream4 +Dma.ADC1.8.MemDataAlignment=DMA_MDATAALIGN_HALFWORD +Dma.ADC1.8.MemInc=DMA_MINC_ENABLE +Dma.ADC1.8.Mode=DMA_NORMAL +Dma.ADC1.8.PeriphDataAlignment=DMA_PDATAALIGN_HALFWORD +Dma.ADC1.8.PeriphInc=DMA_PINC_DISABLE +Dma.ADC1.8.Priority=DMA_PRIORITY_LOW +Dma.ADC1.8.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode +Dma.I2C2_RX.9.Direction=DMA_PERIPH_TO_MEMORY +Dma.I2C2_RX.9.FIFOMode=DMA_FIFOMODE_DISABLE +Dma.I2C2_RX.9.Instance=DMA1_Stream2 +Dma.I2C2_RX.9.MemDataAlignment=DMA_MDATAALIGN_BYTE +Dma.I2C2_RX.9.MemInc=DMA_MINC_ENABLE +Dma.I2C2_RX.9.Mode=DMA_NORMAL +Dma.I2C2_RX.9.PeriphDataAlignment=DMA_PDATAALIGN_BYTE +Dma.I2C2_RX.9.PeriphInc=DMA_PINC_DISABLE +Dma.I2C2_RX.9.Priority=DMA_PRIORITY_LOW +Dma.I2C2_RX.9.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode +Dma.I2C2_TX.10.Direction=DMA_MEMORY_TO_PERIPH +Dma.I2C2_TX.10.FIFOMode=DMA_FIFOMODE_DISABLE +Dma.I2C2_TX.10.Instance=DMA1_Stream7 +Dma.I2C2_TX.10.MemDataAlignment=DMA_MDATAALIGN_BYTE +Dma.I2C2_TX.10.MemInc=DMA_MINC_ENABLE +Dma.I2C2_TX.10.Mode=DMA_NORMAL +Dma.I2C2_TX.10.PeriphDataAlignment=DMA_PDATAALIGN_BYTE +Dma.I2C2_TX.10.PeriphInc=DMA_PINC_DISABLE +Dma.I2C2_TX.10.Priority=DMA_PRIORITY_LOW +Dma.I2C2_TX.10.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode Dma.Request0=USART6_RX Dma.Request1=USART6_TX -Dma.Request10=USART3_RX -Dma.Request11=MEMTOMEM -Dma.Request12=ADC1 +Dma.Request10=I2C2_TX +Dma.Request11=SPI1_TX Dma.Request2=USART1_TX Dma.Request3=USART1_RX Dma.Request4=SPI1_RX -Dma.Request5=SPI1_TX -Dma.Request6=SPI2_RX -Dma.Request7=SPI2_TX -Dma.Request8=I2C2_RX -Dma.Request9=I2C2_TX -Dma.RequestsNb=13 +Dma.Request5=SPI2_RX +Dma.Request6=SPI2_TX +Dma.Request7=USART3_RX +Dma.Request8=ADC1 +Dma.Request9=I2C2_RX +Dma.RequestsNb=12 Dma.SPI1_RX.4.Direction=DMA_PERIPH_TO_MEMORY Dma.SPI1_RX.4.FIFOMode=DMA_FIFOMODE_ENABLE Dma.SPI1_RX.4.FIFOThreshold=DMA_FIFO_THRESHOLD_FULL Dma.SPI1_RX.4.Instance=DMA2_Stream0 Dma.SPI1_RX.4.MemBurst=DMA_MBURST_SINGLE Dma.SPI1_RX.4.MemDataAlignment=DMA_MDATAALIGN_BYTE -Dma.Request5=MEMTOMEM -Dma.Request6=SPI1_RX -Dma.Request7=SPI1_TX -Dma.Request8=SPI2_RX -Dma.Request9=SPI2_TX Dma.SPI1_RX.4.MemInc=DMA_MINC_ENABLE Dma.SPI1_RX.4.Mode=DMA_NORMAL Dma.SPI1_RX.4.PeriphBurst=DMA_PBURST_SINGLE @@ -112,100 +77,45 @@ Dma.SPI1_RX.4.PeriphDataAlignment=DMA_PDATAALIGN_BYTE Dma.SPI1_RX.4.PeriphInc=DMA_PINC_DISABLE Dma.SPI1_RX.4.Priority=DMA_PRIORITY_LOW Dma.SPI1_RX.4.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode,FIFOThreshold,MemBurst,PeriphBurst -Dma.SPI1_TX.5.Direction=DMA_MEMORY_TO_PERIPH -Dma.SPI1_TX.5.FIFOMode=DMA_FIFOMODE_ENABLE -Dma.SPI1_TX.5.FIFOThreshold=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_StreamDMA_FIFO_THRESHOLD_FULL -Dma.SPI1_TX.5.Instance=DMA2_Stream3 -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.SPI1_TX.5.MemBurst=DMA_MBURST_SINGLE -Dma.SPI1_TX.5.MemDataAlignment=DMA_MDATAALIGN_BYTE -Dma.SPI1_TX.5.MemInc=DMA_MINC_ENABLE -Dma.SPI1_TX.5.Mode=DMA_NORMAL -Dma.SPI1_TX.5.PeriphBurst=DMA_PBURST_SINGLE -Dma.SPI1_TX.5.PeriphDataAlignment=DMA_PDATAALIGN_BYTE -Dma.SPI1_TX.5.PeriphInc=DMA_PINC_DISABLE -Dma.SPI1_TX.5.Priority=DMA_PRIORITY_LOW -Dma.SPI1_TX.5.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode,FIFOThreshold,MemBurst,PeriphBurst -Dma.SPI2_RX.6.Direction=DMA_PERIPH_TO_MEMORY -Dma.SPI2_RX.6.FIFOMode=DMA_FIFOMODE_ENABLE -Dma.SPI2_RX.6.FIFOThreshold=DMA_FIFO_THRESHOLD_FULL -Dma.SPI2_RX.6.Instance=DMA1_Stream3 -Dma.SPI2_RX.6.MemBurst=DMA_MBURST_SINGLE -Dma.SPI2_RX.6.MemDataAlignment=DMA_MDATAALIGN_BYTE -Dma.SPI2_RX.6.MemInc=DMA_MINC_ENABLE -Dma.SPI2_RX.6.Mode=DMA_NORMAL -Dma.SPI2_RX.6.PeriphBurst=DMA_PBURST_SINGLE -Dma.SPI2_RX.6.PeriphDataAlignment=DMA_PDATAALIGN_BYTE -Dma.SPI2_RX.6.PeriphInc=DMA_PINC_DISABLE -Dma.SPI2_RX.6.Priority=DMA_PRIORITY_LOW -Dma.SPI2_RX.6.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode,FIFOThreshold,MemBurst,PeriphBurst -Dma.SPI2_TX.7.Direction=DMA_MEMORY_TO_PERIPH -Dma.SPI2_TX.7.FIFOMode=DMA_FIFOMODE_ENABLE -Dma.SPI2_TX.7.FIFOThreshold=DMA_FIFO_THRESHOLD_FULL -Dma.SPI2_TX.7.Instance=DMA1_Stream4 -Dma.SPI2_TX.7.MemBurst=DMA_MBURST_SINGLE -Dma.SPI2_TX.7.MemDataAlignment=DMA_MDATAALIGN_BYTE -Dma.SPI2_TX.7.MemInc=DMA_MINC_ENABLE -Dma.SPI2_TX.7.Mode=DMA_NORMAL -Dma.SPI2_TX.7.PeriphBurst=DMA_PBURST_SINGLE -Dma.SPI2_TX.7.PeriphDataAlignment=DMA_PDATAALIGN_BYTE -Dma.SPI2_TX.7.PeriphInc=DMA_PINC_DISABLE -Dma.SPI2_TX.7.Priority=DMA_PRIORITY_LOW -Dma.SPI2_TX.7.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode,FIFOThreshold,MemBurst,PeriphBurst +Dma.SPI1_TX.11.Direction=DMA_MEMORY_TO_PERIPH +Dma.SPI1_TX.11.FIFOMode=DMA_FIFOMODE_DISABLE +Dma.SPI1_TX.11.Instance=DMA2_Stream3 +Dma.SPI1_TX.11.MemDataAlignment=DMA_MDATAALIGN_BYTE +Dma.SPI1_TX.11.MemInc=DMA_MINC_ENABLE +Dma.SPI1_TX.11.Mode=DMA_NORMAL +Dma.SPI1_TX.11.PeriphDataAlignment=DMA_PDATAALIGN_BYTE +Dma.SPI1_TX.11.PeriphInc=DMA_PINC_DISABLE +Dma.SPI1_TX.11.Priority=DMA_PRIORITY_LOW +Dma.SPI1_TX.11.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode +Dma.SPI2_RX.5.Direction=DMA_PERIPH_TO_MEMORY +Dma.SPI2_RX.5.FIFOMode=DMA_FIFOMODE_ENABLE +Dma.SPI2_RX.5.FIFOThreshold=DMA_FIFO_THRESHOLD_FULL +Dma.SPI2_RX.5.Instance=DMA1_Stream3 +Dma.SPI2_RX.5.MemBurst=DMA_MBURST_SINGLE +Dma.SPI2_RX.5.MemDataAlignment=DMA_MDATAALIGN_BYTE +Dma.SPI2_RX.5.MemInc=DMA_MINC_ENABLE +Dma.SPI2_RX.5.Mode=DMA_NORMAL +Dma.SPI2_RX.5.PeriphBurst=DMA_PBURST_SINGLE +Dma.SPI2_RX.5.PeriphDataAlignment=DMA_PDATAALIGN_BYTE +Dma.SPI2_RX.5.PeriphInc=DMA_PINC_DISABLE +Dma.SPI2_RX.5.Priority=DMA_PRIORITY_LOW +Dma.SPI2_RX.5.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode,FIFOThreshold,MemBurst,PeriphBurst +Dma.SPI2_TX.6.Direction=DMA_MEMORY_TO_PERIPH +Dma.SPI2_TX.6.FIFOMode=DMA_FIFOMODE_ENABLE +Dma.SPI2_TX.6.FIFOThreshold=DMA_FIFO_THRESHOLD_FULL +Dma.SPI2_TX.6.Instance=DMA1_Stream4 +Dma.SPI2_TX.6.MemBurst=DMA_MBURST_SINGLE +Dma.SPI2_TX.6.MemDataAlignment=DMA_MDATAALIGN_BYTE +Dma.SPI2_TX.6.MemInc=DMA_MINC_ENABLE +Dma.SPI2_TX.6.Mode=DMA_NORMAL +Dma.SPI2_TX.6.PeriphBurst=DMA_PBURST_SINGLE +Dma.SPI2_TX.6.PeriphDataAlignment=DMA_PDATAALIGN_BYTE +Dma.SPI2_TX.6.PeriphInc=DMA_PINC_DISABLE +Dma.SPI2_TX.6.Priority=DMA_PRIORITY_LOW +Dma.SPI2_TX.6.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode,FIFOThreshold,MemBurst,PeriphBurst Dma.USART1_RX.3.Direction=DMA_PERIPH_TO_MEMORY Dma.USART1_RX.3.FIFOMode=DMA_FIFOMODE_DISABLE -Dma.USART1_RX.3.Instance=DMA2_Stream2 +Dma.USART1_RX.3.Instance=DMA2_Stream5 Dma.USART1_RX.3.MemDataAlignment=DMA_MDATAALIGN_BYTE Dma.USART1_RX.3.MemInc=DMA_MINC_ENABLE Dma.USART1_RX.3.Mode=DMA_NORMAL @@ -223,19 +133,19 @@ Dma.USART1_TX.2.PeriphDataAlignment=DMA_PDATAALIGN_BYTE Dma.USART1_TX.2.PeriphInc=DMA_PINC_DISABLE Dma.USART1_TX.2.Priority=DMA_PRIORITY_VERY_HIGH Dma.USART1_TX.2.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode -Dma.USART3_RX.10.Direction=DMA_PERIPH_TO_MEMORY -Dma.USART3_RX.10.FIFOMode=DMA_FIFOMODE_DISABLE -Dma.USART3_RX.10.Instance=DMA1_Stream1 -Dma.USART3_RX.10.MemDataAlignment=DMA_MDATAALIGN_BYTE -Dma.USART3_RX.10.MemInc=DMA_MINC_ENABLE -Dma.USART3_RX.10.Mode=DMA_NORMAL -Dma.USART3_RX.10.PeriphDataAlignment=DMA_PDATAALIGN_BYTE -Dma.USART3_RX.10.PeriphInc=DMA_PINC_DISABLE -Dma.USART3_RX.10.Priority=DMA_PRIORITY_LOW -Dma.USART3_RX.10.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode +Dma.USART3_RX.7.Direction=DMA_PERIPH_TO_MEMORY +Dma.USART3_RX.7.FIFOMode=DMA_FIFOMODE_DISABLE +Dma.USART3_RX.7.Instance=DMA1_Stream1 +Dma.USART3_RX.7.MemDataAlignment=DMA_MDATAALIGN_BYTE +Dma.USART3_RX.7.MemInc=DMA_MINC_ENABLE +Dma.USART3_RX.7.Mode=DMA_NORMAL +Dma.USART3_RX.7.PeriphDataAlignment=DMA_PDATAALIGN_BYTE +Dma.USART3_RX.7.PeriphInc=DMA_PINC_DISABLE +Dma.USART3_RX.7.Priority=DMA_PRIORITY_LOW +Dma.USART3_RX.7.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority,FIFOMode Dma.USART6_RX.0.Direction=DMA_PERIPH_TO_MEMORY Dma.USART6_RX.0.FIFOMode=DMA_FIFOMODE_DISABLE -Dma.USART6_RX.0.Instance=DMA2_Stream1 +Dma.USART6_RX.0.Instance=DMA2_Stream2 Dma.USART6_RX.0.MemDataAlignment=DMA_MDATAALIGN_BYTE Dma.USART6_RX.0.MemInc=DMA_MINC_ENABLE Dma.USART6_RX.0.Mode=DMA_NORMAL @@ -265,10 +175,6 @@ I2C2.I2C_Mode=I2C_Fast I2C2.IPParameters=I2C_Mode I2C3.I2C_Mode=I2C_Fast I2C3.IPParameters=I2C_Mode -I2C2.I2C_Mode=I2C_Fast -I2C2.IPParameters=I2C_Mode -I2C3.I2C_Mode=I2C_Fast -I2C3.IPParameters=I2C_Mode KeepUserPlacement=false Mcu.CPN=STM32F407IGH6 Mcu.Family=STM32F4 @@ -313,8 +219,6 @@ Mcu.Pin16=PD1 Mcu.Pin17=PA11 Mcu.Pin18=PF0 Mcu.Pin19=PA9 -Mcu.Pin18=PF0 -Mcu.Pin19=PA9 Mcu.Pin2=PG14 Mcu.Pin20=PC9 Mcu.Pin21=PA8 @@ -326,16 +230,6 @@ Mcu.Pin26=PG6 Mcu.Pin27=PH12 Mcu.Pin28=PG3 Mcu.Pin29=PH11 -Mcu.Pin20=PC9 -Mcu.Pin21=PA8 -Mcu.Pin22=PH0-OSC_IN -Mcu.Pin23=PH1-OSC_OUT -Mcu.Pin24=PF1 -Mcu.Pin25=PC6 -Mcu.Pin26=PG6 -Mcu.Pin27=PH12 -Mcu.Pin28=PG3 -Mcu.Pin29=PH11 Mcu.Pin3=PB4 Mcu.Pin30=PH10 Mcu.Pin31=PD14 @@ -391,11 +285,10 @@ 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.DMA1_Stream7_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\:false\:false\:true\:false\:false\:true\:true NVIC.DMA2_Stream2_IRQn=true\:5\:0\:false\:false\:true\:true\:false\:true\:true -NVIC.DMA2_Stream3_IRQn=true\:5\:0\:false\:false\:true\:false\:false\:true\:true +NVIC.DMA2_Stream3_IRQn=true\:5\:0\:false\:false\:true\:true\:false\:true\:true NVIC.DMA2_Stream4_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_Stream5_IRQn=true\:5\:0\:false\:false\:true\:false\: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 @@ -412,8 +305,6 @@ 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.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/can/bsp_can.c b/bsp/can/bsp_can.c index 4bb482d..389a1a4 100644 --- a/bsp/can/bsp_can.c +++ b/bsp/can/bsp_can.c @@ -5,7 +5,7 @@ /* can instance ptrs storage, used for recv callback */ // 在CAN产生接收中断会遍历数组,选出hcan和rxid与发生中断的实例相同的那个,调用其回调函数 -static CANInstance *instance[MX_REGISTER_DEVICE_CNT] = {NULL}; +static CANInstance *can_instance[MX_REGISTER_DEVICE_CNT] = {NULL}; static uint8_t idx; // 全局CAN实例索引,每次有新的模块注册会自增 /* ----------------two static function called by CANRegister()-------------------- */ @@ -67,22 +67,24 @@ CANInstance *CANRegister(CAN_Init_Config_s *config) { CANServiceInit(); // 第一次注册,先进行硬件初始化 } - instance[idx] = (CANInstance *)malloc(sizeof(CANInstance)); // 分配空间 - memset(instance[idx], 0, sizeof(CANInstance)); + CANInstance *instance = (CANInstance *)malloc(sizeof(CANInstance)); // 分配空间 + memset(instance, 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 + instance->txconf.StdId = config->tx_id; + instance->txconf.IDE = CAN_ID_STD; + instance->txconf.RTR = CAN_RTR_DATA; + instance->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; + 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[idx]); // 添加CAN过滤器规则 - return instance[idx++]; // 返回指针 + CANAddFilter(instance); // 添加CAN过滤器规则 + can_instance[idx++] = instance; // 将实例保存到can_instance中 + + return instance; // 返回can实例指针 } /* TODO:目前似乎封装过度,应该添加一个指向tx_buff的指针,tx_buff不应该由CAN instance保存 */ @@ -117,17 +119,16 @@ static void CANFIFOxCallback(CAN_HandleTypeDef *_hcan, uint32_t fifox) 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) + { // 两者相等说明这是要找的实例 + if (_hcan == can_instance[i]->can_handle && rxconf.StdId == can_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) + if (can_instance[i]->can_module_callback != NULL) { - instance[i]->can_module_callback(instance[i]); // 触发回调进行数据解析和处理 + 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]); // 触发回调进行数据解析和处理 } - break; + return; } } } diff --git a/bsp/dac/bsp_dac.h b/bsp/dac/bsp_dac.h deleted file mode 100644 index e69de29..0000000 diff --git a/bsp/dac/bsp_dac.md b/bsp/dac/bsp_dac.md deleted file mode 100644 index e69de29..0000000 diff --git a/bsp/iic/bsp_iic.c b/bsp/iic/bsp_iic.c index 88420cf..55065d1 100644 --- a/bsp/iic/bsp_iic.c +++ b/bsp/iic/bsp_iic.c @@ -37,16 +37,18 @@ void HAL_I2C_MemRxCpltCallback(I2C_HandleTypeDef *hi2c) IICInstance *IICRegister(IIC_Init_Config_s *conf) { // 申请到的空间未必是0, 所以需要手动初始化 - iic_instance[idx] = (IICInstance *)malloc(sizeof(IICInstance)); - memset(iic_instance[idx], 0, sizeof(IICInstance)); + IICInstance *instance = (IICInstance *)malloc(sizeof(IICInstance)); + instance = (IICInstance *)malloc(sizeof(IICInstance)); + memset(instance, 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; + instance->dev_address = conf->dev_address; + instance->callback = conf->callback; + instance->work_mode = conf->work_mode; + instance->handle = conf->handle; + instance->id = conf->id; - return iic_instance[idx++]; + iic_instance[idx++] = instance; + return instance; } void IICSetMode(IICInstance *iic, IIC_Work_Mode_e mode) diff --git a/bsp/pwm/bsp_pwm.c b/bsp/pwm/bsp_pwm.c index e69de29..53d63eb 100644 --- a/bsp/pwm/bsp_pwm.c +++ b/bsp/pwm/bsp_pwm.c @@ -0,0 +1,67 @@ +#include "bsp_pwm.h" +#include "stdlib.h" +#include "memory.h" + +// 配合中断以及初始化 +static uint8_t idx; +static PWMInstance *pwm_instance[PWM_DEVICE_CNT] = {NULL}; // 所有的pwm instance保存于此,用于callback时判断中断来源 + +void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim) +{ + for (uint8_t i = 0; i < idx; i++) + { // 来自同一个定时器的中断且通道相同 + if (pwm_instance[i]->htim == htim && htim->Channel == pwm_instance[i]->channel) + { + if (pwm_instance[i]->callback) // 如果有回调函数 + { + pwm_instance[i]->callback(pwm_instance[i]); + } + return; // 一次只能有一个通道的中断,所以直接返回 + } + } +} + +PWMInstance *PWMRegister(PWM_Init_Config_s *config) +{ + PWMInstance *pwm = (PWMInstance *)malloc(sizeof(PWMInstance)); + memset(pwm, 0, sizeof(PWMInstance)); + + pwm->htim = config->htim; + pwm->channel = config->channel; + pwm->period = config->period; + pwm->pulse = config->pulse; + pwm->callback = config->callback; + pwm->id = config->id; + + HAL_TIM_PWM_Start(pwm->htim, pwm->channel); + __HAL_TIM_SetCompare(pwm->htim, pwm->channel, pwm->pulse); + + pwm_instance[idx++] = pwm; + return pwm; +} + +/* 只是对HAL的函数进行了形式上的封装 */ +void PWMStart(PWMInstance *pwm) +{ + HAL_TIM_PWM_Start(pwm->htim, pwm->channel); + __HAL_TIM_SetCompare(pwm->htim, pwm->channel, pwm->pulse); +} + +/* 只是对HAL的函数进行了形式上的封装 */ +void PWMStop(PWMInstance *pwm) +{ + HAL_TIM_PWM_Stop(pwm->htim, pwm->channel); +} + +/* 只是对HAL的函数进行了形式上的封装 */ +void PWMSetPulse(PWMInstance *pwm, uint32_t pulse) +{ + pwm->pulse = pulse; + __HAL_TIM_SetCompare(pwm->htim, pwm->channel, pwm->pulse); +} + +/* 只是对HAL的函数进行了形式上的封装 */ +void PWMStartDMA(PWMInstance *pwm, uint32_t *pData, uint32_t Size) +{ + HAL_TIM_PWM_Start_DMA(pwm->htim, pwm->channel, pData, Size); +} \ No newline at end of file diff --git a/bsp/pwm/bsp_pwm.h b/bsp/pwm/bsp_pwm.h index e69de29..9386876 100644 --- a/bsp/pwm/bsp_pwm.h +++ b/bsp/pwm/bsp_pwm.h @@ -0,0 +1,71 @@ +#ifndef BSP_PWM_H +#define BSP_PWM_H + +#include "tim.h" +#include "stdint.h" + +#define PWM_DEVICE_CNT 16 // PWM实例数量 + +/* pwm实例结构体 */ +typedef struct pwm_ins_temp +{ + TIM_HandleTypeDef *htim; // TIM句柄 + uint32_t channel; // 通道 + uint32_t period; // 周期 + uint32_t pulse; // 脉宽 + void (*callback)(struct pwm_ins_temp *); // DMA传输完成回调函数 + void *id; // 实例ID +} PWMInstance; + +typedef struct +{ + TIM_HandleTypeDef *htim; // TIM句柄 + uint32_t channel; // 通道 + uint32_t period; // 周期 + uint32_t pulse; // 脉宽 + void (*callback)(struct pwm_ins_temp *); // DMA传输完成回调函数 + void *id; // 实例ID +} PWM_Init_Config_s; + +/** + * @brief 注册一个pwm实例 + * + * @param config 初始化配置 + * @return PWMInstance* + */ +PWMInstance *PWMRegister(PWM_Init_Config_s *config); + +/** + * @brief 启动pwm + * + * @param pwm pwm实例 + */ +void PWMStart(PWMInstance *pwm); + +/** + * @brief 停止pwm + * + * @param pwm pwm实例 + */ +void PWMStop(PWMInstance *pwm); + +/** + * @brief 设置pwm脉宽 + * + * @param pwm pwm实例 + * @param pulse 脉宽 + */ +void PWMSetPulse(PWMInstance *pwm, uint32_t pulse); + +/** + * @brief 启动pwm dma传输 + * + * @param pwm pwm实例 + * @param pData 数据首地址指针,注意数据的位数必须和CubeMX配置的DMA传输位数(字长)一致 + * @param Size 数据长度 + * @note 如果使用此函数,则需要在CubeMX中配置DMA传输位数为对应位数 + * 例如:使用16位数据,则需要配置DMA传输位数为16位(half word),配置错误会导致指针越界或数据错误 + */ +void PWMStartDMA(PWMInstance *pwm, uint32_t *pData, uint32_t Size); + +#endif // BSP_PWM_H \ No newline at end of file diff --git a/bsp/dac/bsp_dac.c b/bsp/pwm/bsp_pwm.md similarity index 100% rename from bsp/dac/bsp_dac.c rename to bsp/pwm/bsp_pwm.md diff --git a/bsp/spi/bsp_spi.c b/bsp/spi/bsp_spi.c index 4e5ad15..74ab133 100644 --- a/bsp/spi/bsp_spi.c +++ b/bsp/spi/bsp_spi.c @@ -25,7 +25,7 @@ void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi) HAL_GPIO_WritePin(spi_instance[i]->GPIO_cs, spi_instance[i]->cs_pin, GPIO_PIN_SET); spi_instance[i]->callback(spi_instance[i]); } - break; + return; } } } @@ -42,14 +42,17 @@ void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *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; - 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++]; + SPIInstance *instance = (SPIInstance *)malloc(sizeof(SPIInstance)); + memset(instance, 0, sizeof(SPIInstance)); + instance->spi_handle = conf->spi_handle; + instance->GPIO_cs = conf->GPIO_cs; + instance->cs_pin = conf->cs_pin; + instance->spi_work_mode = conf->spi_work_mode; + instance->callback = conf->callback; + instance->id = conf->id; + + spi_instance[idx++] = instance; + return instance; } void SPITransmit(SPIInstance *spi_ins, uint8_t *ptr_data, uint8_t len) diff --git a/bsp/usart/bsp_usart.c b/bsp/usart/bsp_usart.c index c77e97a..9eb33a3 100644 --- a/bsp/usart/bsp_usart.c +++ b/bsp/usart/bsp_usart.c @@ -14,7 +14,8 @@ /* usart service instance, modules' info would be recoreded here using USARTRegister() */ /* usart服务实例,所有注册了usart的模块信息会被保存在这里 */ -static USARTInstance *instance[DEVICE_USART_CNT] = {NULL}; +static uint8_t idx; +static USARTInstance *usart_instance[DEVICE_USART_CNT] = {NULL}; /** * @brief usart service will start automatically, after each module registered @@ -27,23 +28,21 @@ static void USARTServiceInit(USARTInstance *_instance) HAL_UARTEx_ReceiveToIdle_DMA(_instance->usart_handle, _instance->recv_buff, _instance->recv_buff_size); // 关闭dma half transfer中断防止两次进入HAL_UARTEx_RxEventCallback() // 这是HAL库的一个设计失误,发生DMA传输完成/半完成以及串口IDLE中断都会触发HAL_UARTEx_RxEventCallback() - // 我们只希望处理,因此直接关闭DMA半传输中断第一种和第三种情况 + // 我们只希望处理第一种和第三种情况,因此直接关闭DMA半传输中断 __HAL_DMA_DISABLE_IT(_instance->usart_handle->hdmarx, DMA_IT_HT); } USARTInstance *USARTRegister(USART_Init_Config_s *init_config) { - static uint8_t idx; + USARTInstance *instance = (USARTInstance *)malloc(sizeof(USARTInstance)); + memset(instance, 0, sizeof(USARTInstance)); - instance[idx] = (USARTInstance *)malloc(sizeof(USARTInstance)); - memset(instance[idx], 0, sizeof(USARTInstance)); + instance->usart_handle = init_config->usart_handle; + instance->recv_buff_size = init_config->recv_buff_size; + instance->module_callback = init_config->module_callback; - instance[idx]->module_callback = init_config->module_callback; - instance[idx]->recv_buff_size = init_config->recv_buff_size; - instance[idx]->usart_handle = init_config->usart_handle; - USARTServiceInit(instance[idx]); - - return instance[idx++]; + usart_instance[idx++] = instance; + return instance; } /* @todo 当前仅进行了形式上的封装,后续要进一步考虑是否将module的行为与bsp完全分离 */ @@ -70,15 +69,18 @@ void USARTSend(USARTInstance *_instance, uint8_t *send_buf, uint16_t send_size) */ void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) { - for (uint8_t i = 0; i < 3; ++i) - { - if (huart == instance[i]->usart_handle) - { - instance[i]->module_callback(); - memset(instance[i]->recv_buff, 0, Size); // 接收结束后清空buffer,对于变长数据是必要的 - HAL_UARTEx_ReceiveToIdle_DMA(instance[i]->usart_handle, instance[i]->recv_buff, instance[i]->recv_buff_size); - __HAL_DMA_DISABLE_IT(instance[i]->usart_handle->hdmarx, DMA_IT_HT); - break; + for (uint8_t i = 0; i < idx; ++i) + { // find the instance which is being handled + if (huart == usart_instance[i]->usart_handle) + { // call the callback function if it is not NULL + if (usart_instance[i]->module_callback != NULL) + { + usart_instance[i]->module_callback(); + memset(usart_instance[i]->recv_buff, 0, Size); // 接收结束后清空buffer,对于变长数据是必要的 + } + HAL_UARTEx_ReceiveToIdle_DMA(usart_instance[i]->usart_handle, usart_instance[i]->recv_buff, usart_instance[i]->recv_buff_size); + __HAL_DMA_DISABLE_IT(usart_instance[i]->usart_handle->hdmarx, DMA_IT_HT); + return; // break the loop } } } @@ -93,13 +95,13 @@ void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) */ void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) { - for (uint8_t i = 0; i < 3; ++i) + for (uint8_t i = 0; i < idx; ++i) { - if (huart == instance[i]->usart_handle) + if (huart == usart_instance[i]->usart_handle) { - HAL_UARTEx_ReceiveToIdle_DMA(instance[i]->usart_handle, instance[i]->recv_buff, instance[i]->recv_buff_size); - __HAL_DMA_DISABLE_IT(instance[i]->usart_handle->hdmarx, DMA_IT_HT); - break; + HAL_UARTEx_ReceiveToIdle_DMA(usart_instance[i]->usart_handle, usart_instance[i]->recv_buff, usart_instance[i]->recv_buff_size); + __HAL_DMA_DISABLE_IT(usart_instance[i]->usart_handle->hdmarx, DMA_IT_HT); + return; } } } \ No newline at end of file diff --git a/modules/can_comm/can_comm.c b/modules/can_comm/can_comm.c index c13a916..39702e9 100644 --- a/modules/can_comm/can_comm.c +++ b/modules/can_comm/can_comm.c @@ -16,7 +16,7 @@ static void CANCommResetRx(CANCommInstance *ins) } /** - * @brief + * @brief cancomm的接收回调函数 * * @param _instance */ @@ -66,7 +66,7 @@ static void CANCommRxCallback(CANInstance *_instance) CANCommResetRx(comm); return; // 重置状态然后返回 } - return; // 访问完一个can comm直接退出,一次中断只会也只可能会处理一个实例的回调 + return; // 访问完一个can comm直接退出,一次中断只处理一个实例的回调 } } } diff --git a/modules/can_comm/can_comm.h b/modules/can_comm/can_comm.h index fb53822..c184f80 100644 --- a/modules/can_comm/can_comm.h +++ b/modules/can_comm/can_comm.h @@ -26,16 +26,16 @@ typedef struct { CANInstance *can_ins; /* 发送部分 */ - uint8_t send_data_len; - uint8_t send_buf_len; + uint8_t send_data_len; // 发送数据长度 + uint8_t send_buf_len; // 发送缓冲区长度,为发送数据长度+帧头单包数据长度帧尾以及校验和(4) uint8_t raw_sendbuf[CAN_COMM_MAX_BUFFSIZE + CAN_COMM_OFFSET_BYTES]; // 额外4个bytes保存帧头帧尾和校验和 /* 接收部分 */ - uint8_t recv_data_len; - uint8_t recv_buf_len; + uint8_t recv_data_len; // 接收数据长度 + uint8_t recv_buf_len; // 接收缓冲区长度,为接收数据长度+帧头单包数据长度帧尾以及校验和(4) uint8_t raw_recvbuf[CAN_COMM_MAX_BUFFSIZE + CAN_COMM_OFFSET_BYTES]; // 额外4个bytes保存帧头帧尾和校验和 uint8_t unpacked_recv_data[CAN_COMM_MAX_BUFFSIZE]; // 解包后的数据,调用CANCommGet()后cast成对应的类型通过指针读取即可 /* 接收和更新标志位*/ - uint8_t recv_state; + uint8_t recv_state; // 接收状态, uint8_t cur_recv_len; uint8_t update_flag; } CANCommInstance; diff --git a/modules/daemon/daemon.c b/modules/daemon/daemon.c index 49d6677..ac74caa 100644 --- a/modules/daemon/daemon.c +++ b/modules/daemon/daemon.c @@ -1,21 +1,23 @@ #include "daemon.h" #include "bsp_dwt.h" // 后续通过定时器来计时? #include "stdlib.h" +#include "memory.h" +// 用于保存所有的daemon instance static DaemonInstance *daemon_instances[DAEMON_MX_CNT] = {NULL}; -static uint8_t idx; +static uint8_t idx; // 用于记录当前的daemon instance数量,配合回调使用 DaemonInstance *DaemonRegister(Daemon_Init_Config_s *config) { - - daemon_instances[idx] = (DaemonInstance *)malloc(sizeof(DaemonInstance)); + DaemonInstance *instance = (DaemonInstance *)malloc(sizeof(DaemonInstance)); + memset(instance, 0, sizeof(DaemonInstance)); - daemon_instances[idx]->reload_count = config->reload_count; - daemon_instances[idx]->callback = config->callback; - daemon_instances[idx]->owner_id = config->id; - daemon_instances[idx]->temp_count = config->reload_count; + instance->owner_id = config->owner_id; + instance->reload_count = config->reload_count; + instance->callback = config->callback; - return daemon_instances[idx++]; + daemon_instances[idx++] = instance; + return instance; } void DaemonReload(DaemonInstance *instance) @@ -25,20 +27,20 @@ void DaemonReload(DaemonInstance *instance) uint8_t DaemonIsOnline(DaemonInstance *instance) { - return instance->temp_count>0; + return instance->temp_count > 0; } void DaemonTask() { - static DaemonInstance* pins; //提高可读性同时降低访存开销 + static DaemonInstance *dins; // 提高可读性同时降低访存开销 for (size_t i = 0; i < idx; ++i) { - pins=daemon_instances[i]; - if(pins->temp_count>0) - pins->temp_count--; - else if(pins->callback) - { // 每个module根据自身的offline callback进行调用 - pins->callback(pins->owner_id); // module将owner_id强制类型转换成自身类型 + dins = daemon_instances[i]; + if (dins->temp_count > 0) + dins->temp_count--; + else if (dins->callback) // 如果有callback + { + dins->callback(dins->owner_id); // module内可以将owner_id强制类型转换成自身类型从而调用自身的offline callback } } } diff --git a/modules/daemon/daemon.h b/modules/daemon/daemon.h index 7167885..f2e6a50 100644 --- a/modules/daemon/daemon.h +++ b/modules/daemon/daemon.h @@ -11,11 +11,11 @@ typedef void (*offline_callback)(void *); /* daemon结构体定义 */ typedef struct daemon_ins { - uint16_t reload_count; // 重载值 + uint16_t reload_count; // 重载值 offline_callback callback; // 异常处理函数,当模块发生异常时会被调用 - uint16_t temp_count; //当前值,减为零说明模块离线或异常 - void *owner_id; // daemon实例的地址,初始化的时候填入 + uint16_t temp_count; // 当前值,减为零说明模块离线或异常 + void *owner_id; // daemon实例的地址,初始化的时候填入 } DaemonInstance; @@ -23,13 +23,13 @@ typedef struct daemon_ins typedef struct { uint16_t reload_count; // 实际上这是app唯一需要设置的值? - offline_callback callback; - void *id; // id取拥有daemon的实例的地址,如DJIMotorInstance*,cast成void*类型 + offline_callback callback; + void *owner_id; // id取拥有daemon的实例的地址,如DJIMotorInstance*,cast成void*类型 } Daemon_Init_Config_s; /** * @brief 注册一个daemon实例 - * + * * @param config 初始化配置 * @return DaemonInstance* 返回实例指针 */ @@ -37,14 +37,14 @@ DaemonInstance *DaemonRegister(Daemon_Init_Config_s *config); /** * @brief 当模块收到新的数据或进行其他动作时,调用该函数重载temp_count,相当于"喂狗" - * + * * @param instance daemon实例指针 */ void DaemonReload(DaemonInstance *instance); /** * @brief 确认模块是否离线 - * + * * @param instance daemon实例指针 * @return uint8_t 若在线且工作正常,返回1;否则返回零. 后续根据异常类型和离线状态等进行优化. */ @@ -53,7 +53,7 @@ uint8_t DaemonIsOnline(DaemonInstance *instance); /** * @brief 放入rtos中,会给每个daemon实例的temp_count按频率进行递减操作. * 模块成功接受数据或成功操作则会重载temp_count的值为reload_count. - * + * */ void DaemonTask(); diff --git a/modules/motor/HT04.c b/modules/motor/HT04.c index 5172eff..51381b8 100644 --- a/modules/motor/HT04.c +++ b/modules/motor/HT04.c @@ -3,7 +3,7 @@ #include "general_def.h" static uint8_t idx; -HTMotorInstance *ht_motor_info[HT_MOTOR_CNT]; +HTMotorInstance *ht_motor_instance[HT_MOTOR_CNT]; /** * @brief @@ -45,7 +45,7 @@ static void HTMotorDecode(CANInstance *motor_can) static uint8_t *rxbuff; rxbuff = motor_can->rx_buff; - measure = &((HTMotorInstance *)motor_can->id)->motor_measure; + measure = &((HTMotorInstance *)motor_can->id)->motor_measure; // 将can实例中保存的id转换成电机实例的指针 measure->last_angle = measure->total_angle; @@ -63,23 +63,23 @@ static void HTMotorDecode(CANInstance *motor_can) HTMotorInstance *HTMotorInit(Motor_Init_Config_s *config) { + HTMotorInstance *motor = (HTMotorInstance *)malloc(sizeof(HTMotorInstance)); + memset(motor, 0, sizeof(HTMotorInstance)); - ht_motor_info[idx] = (HTMotorInstance *)malloc(sizeof(HTMotorInstance)); - memset(ht_motor_info[idx], 0, sizeof(HTMotorInstance)); - - ht_motor_info[idx]->motor_settings = config->controller_setting_init_config; - PID_Init(&ht_motor_info[idx]->current_PID, &config->controller_param_init_config.current_PID); - PID_Init(&ht_motor_info[idx]->speed_PID, &config->controller_param_init_config.speed_PID); - PID_Init(&ht_motor_info[idx]->angle_PID, &config->controller_param_init_config.angle_PID); - ht_motor_info[idx]->other_angle_feedback_ptr = config->controller_param_init_config.other_angle_feedback_ptr; - ht_motor_info[idx]->other_speed_feedback_ptr = config->controller_param_init_config.other_speed_feedback_ptr; + motor->motor_settings = config->controller_setting_init_config; + PID_Init(&motor->current_PID, &config->controller_param_init_config.current_PID); + PID_Init(&motor->speed_PID, &config->controller_param_init_config.speed_PID); + PID_Init(&motor->angle_PID, &config->controller_param_init_config.angle_PID); + motor->other_angle_feedback_ptr = config->controller_param_init_config.other_angle_feedback_ptr; + motor->other_speed_feedback_ptr = config->controller_param_init_config.other_speed_feedback_ptr; config->can_init_config.can_module_callback = HTMotorDecode; - config->can_init_config.id = ht_motor_info[idx]; - ht_motor_info[idx]->motor_can_instace = CANRegister(&config->can_init_config); + config->can_init_config.id = motor; + motor->motor_can_instace = CANRegister(&config->can_init_config); - HTMotorEnable(ht_motor_info[idx]); - return ht_motor_info[idx++]; + HTMotorEnable(motor); + ht_motor_instance[idx++] = motor; + return motor; } void HTMotorSetRef(HTMotorInstance *motor, float ref) @@ -99,7 +99,7 @@ void HTMotorControl() // 遍历所有电机实例,计算PID for (size_t i = 0; i < idx; i++) { // 先获取地址避免反复寻址 - motor = ht_motor_info[i]; + motor = ht_motor_instance[i]; measure = &motor->motor_measure; setting = &motor->motor_settings; motor_can = motor_can; diff --git a/modules/motor/LK9025.c b/modules/motor/LK9025.c index fd98fa3..decfda7 100644 --- a/modules/motor/LK9025.c +++ b/modules/motor/LK9025.c @@ -11,7 +11,7 @@ static void LKMotorDecode(CANInstance *_instance) static LKMotor_Measure_t *measure; static uint8_t *rx_buff; rx_buff = _instance->rx_buff; - measure = &((LKMotorInstance *)_instance)->measure; + measure = &((LKMotorInstance *)_instance)->measure; // 通过caninstance保存的id获取对应的motorinstance measure->last_ecd = measure->ecd; measure->ecd = (uint16_t)((rx_buff[7] << 8) | rx_buff[6]); @@ -36,26 +36,27 @@ static void LKMotorDecode(CANInstance *_instance) LKMotorInstance *LKMotroInit(Motor_Init_Config_s *config) { - lkmotor_instance[idx] = (LKMotorInstance *)malloc(sizeof(LKMotorInstance)); - memset(lkmotor_instance[idx], 0, sizeof(LKMotorInstance)); + LKMotorInstance *motor = (LKMotorInstance *)malloc(sizeof(LKMotorInstance)); + motor = (LKMotorInstance *)malloc(sizeof(LKMotorInstance)); + memset(motor, 0, sizeof(LKMotorInstance)); - lkmotor_instance[idx]->motor_settings = config->controller_setting_init_config; - PID_Init(&lkmotor_instance[idx]->current_PID, &config->controller_param_init_config.current_PID); - PID_Init(&lkmotor_instance[idx]->speed_PID, &config->controller_param_init_config.speed_PID); - PID_Init(&lkmotor_instance[idx]->angle_PID, &config->controller_param_init_config.angle_PID); - lkmotor_instance[idx]->other_angle_feedback_ptr = config->controller_param_init_config.other_angle_feedback_ptr; - lkmotor_instance[idx]->other_speed_feedback_ptr = config->controller_param_init_config.other_speed_feedback_ptr; + motor->motor_settings = config->controller_setting_init_config; + PID_Init(&motor->current_PID, &config->controller_param_init_config.current_PID); + PID_Init(&motor->speed_PID, &config->controller_param_init_config.speed_PID); + PID_Init(&motor->angle_PID, &config->controller_param_init_config.angle_PID); + motor->other_angle_feedback_ptr = config->controller_param_init_config.other_angle_feedback_ptr; + motor->other_speed_feedback_ptr = config->controller_param_init_config.other_speed_feedback_ptr; - config->can_init_config.id = lkmotor_instance[idx]; + config->can_init_config.id = motor; config->can_init_config.can_module_callback = LKMotorDecode; config->can_init_config.rx_id = 0x140 + config->can_init_config.tx_id; config->can_init_config.tx_id = config->can_init_config.tx_id + 0x280; - lkmotor_instance[idx]->motor_can_ins = CANRegister(&config->can_init_config); + motor->motor_can_ins = CANRegister(&config->can_init_config); if (idx == 0) - sender_instance = lkmotor_instance[idx]->motor_can_ins; + sender_instance = motor->motor_can_ins; - LKMotorEnable(lkmotor_instance[idx]); + LKMotorEnable(motor); return lkmotor_instance[idx++]; } diff --git a/modules/motor/dji_motor.c b/modules/motor/dji_motor.c index 38487ee..72ab446 100644 --- a/modules/motor/dji_motor.c +++ b/modules/motor/dji_motor.c @@ -4,7 +4,7 @@ static uint8_t idx = 0; // register idx,是该文件的全局电机索引,在注册时使用 /* DJI电机的实例,此处仅保存指针,内存的分配将通过电机实例初始化时通过malloc()进行 */ -static DJIMotorInstance *dji_motor_info[DJI_MOTOR_CNT] = {NULL}; +static DJIMotorInstance *dji_motor_instance[DJI_MOTOR_CNT] = {NULL}; /** * @brief 由于DJI电机发送以四个一组的形式进行,故对其进行特殊处理,用6个(2can*3group)can_instance专门负责发送 @@ -54,7 +54,7 @@ static void MotorSenderGrouping(CAN_Init_Config_s *config) uint8_t motor_send_num; uint8_t motor_grouping; - switch (dji_motor_info[idx]->motor_type) + switch (dji_motor_instance[idx]->motor_type) { case M2006: case M3508: @@ -72,13 +72,13 @@ static void MotorSenderGrouping(CAN_Init_Config_s *config) // 计算接收id并设置分组发送id config->rx_id = 0x200 + motor_id + 1; sender_enable_flag[motor_grouping] = 1; - dji_motor_info[idx]->message_num = motor_send_num; - dji_motor_info[idx]->sender_group = motor_grouping; + dji_motor_instance[idx]->message_num = motor_send_num; + dji_motor_instance[idx]->sender_group = motor_grouping; // 检查是否发生id冲突 for (size_t i = 0; i < idx; ++i) { - if (dji_motor_info[i]->motor_can_instance->can_handle == config->can_handle && dji_motor_info[i]->motor_can_instance->rx_id == config->rx_id) + if (dji_motor_instance[i]->motor_can_instance->can_handle == config->can_handle && dji_motor_instance[i]->motor_can_instance->rx_id == config->rx_id) IDcrash_Handler(i, idx); } break; @@ -97,12 +97,12 @@ static void MotorSenderGrouping(CAN_Init_Config_s *config) config->rx_id = 0x204 + motor_id + 1; sender_enable_flag[motor_grouping] = 1; - dji_motor_info[idx]->message_num = motor_send_num; - dji_motor_info[idx]->sender_group = motor_grouping; + dji_motor_instance[idx]->message_num = motor_send_num; + dji_motor_instance[idx]->sender_group = motor_grouping; for (size_t i = 0; i < idx; ++i) { - if (dji_motor_info[i]->motor_can_instance->can_handle == config->can_handle && dji_motor_info[i]->motor_can_instance->rx_id == config->rx_id) + if (dji_motor_instance[i]->motor_can_instance->can_handle == config->can_handle && dji_motor_instance[i]->motor_can_instance->rx_id == config->rx_id) IDcrash_Handler(i, idx); } break; @@ -130,8 +130,8 @@ static void DecodeDJIMotor(CANInstance *_instance) measure->last_ecd = measure->ecd; measure->ecd = ((uint16_t)rxbuff[0]) << 8 | rxbuff[1]; measure->angle_single_round = ECD_ANGLE_COEF_DJI * (float)measure->ecd; - measure->speed_aps = (1.0f - SPEED_SMOOTH_COEF) * measure->speed_aps + - RPM_2_ANGLE_PER_SEC * SPEED_SMOOTH_COEF * (float)((int16_t)(rxbuff[2] << 8 | rxbuff[3])); + measure->speed_aps = (1.0f - SPEED_SMOOTH_COEF) * measure->speed_aps + + RPM_2_ANGLE_PER_SEC * SPEED_SMOOTH_COEF * (float)((int16_t)(rxbuff[2] << 8 | rxbuff[3])); measure->real_current = (1.0f - CURRENT_SMOOTH_COEF) * measure->real_current + CURRENT_SMOOTH_COEF * (float)((int16_t)(rxbuff[4] << 8 | rxbuff[5])); measure->temperate = rxbuff[6]; @@ -147,42 +147,49 @@ static void DecodeDJIMotor(CANInstance *_instance) // 电机初始化,返回一个电机实例 DJIMotorInstance *DJIMotorInit(Motor_Init_Config_s *config) { - dji_motor_info[idx] = (DJIMotorInstance *)malloc(sizeof(DJIMotorInstance)); - memset(dji_motor_info[idx], 0, sizeof(DJIMotorInstance)); + DJIMotorInstance *instance = (DJIMotorInstance *)malloc(sizeof(DJIMotorInstance)); + memset(instance, 0, sizeof(DJIMotorInstance)); // motor basic setting - dji_motor_info[idx]->motor_type = config->motor_type; - dji_motor_info[idx]->motor_settings = config->controller_setting_init_config; + instance->motor_type = config->motor_type; + instance->motor_settings = config->controller_setting_init_config; // motor controller init - PID_Init(&dji_motor_info[idx]->motor_controller.current_PID, &config->controller_param_init_config.current_PID); - PID_Init(&dji_motor_info[idx]->motor_controller.speed_PID, &config->controller_param_init_config.speed_PID); - PID_Init(&dji_motor_info[idx]->motor_controller.angle_PID, &config->controller_param_init_config.angle_PID); - dji_motor_info[idx]->motor_controller.other_angle_feedback_ptr = config->controller_param_init_config.other_angle_feedback_ptr; - dji_motor_info[idx]->motor_controller.other_speed_feedback_ptr = config->controller_param_init_config.other_speed_feedback_ptr; + PID_Init(&instance->motor_controller.current_PID, &config->controller_param_init_config.current_PID); + PID_Init(&instance->motor_controller.speed_PID, &config->controller_param_init_config.speed_PID); + PID_Init(&instance->motor_controller.angle_PID, &config->controller_param_init_config.angle_PID); + instance->motor_controller.other_angle_feedback_ptr = config->controller_param_init_config.other_angle_feedback_ptr; + instance->motor_controller.other_speed_feedback_ptr = config->controller_param_init_config.other_speed_feedback_ptr; // group motors, because 4 motors share the same CAN control message MotorSenderGrouping(&config->can_init_config); // register motor to CAN bus config->can_init_config.can_module_callback = DecodeDJIMotor; // set callback - config->can_init_config.id = dji_motor_info[idx]; // set id,eq to address(it is identity) - dji_motor_info[idx]->motor_can_instance = CANRegister(&config->can_init_config); + config->can_init_config.id = instance; // set id,eq to address(it is identity) + instance->motor_can_instance = CANRegister(&config->can_init_config); - DJIMotorEnable(dji_motor_info[idx]); - return dji_motor_info[idx++]; + DJIMotorEnable(instance); + dji_motor_instance[idx++] = instance; + return instance; } +/* 电流只能通过电机自带传感器监测,后续考虑加入力矩传感器应变片等 */ void DJIMotorChangeFeed(DJIMotorInstance *motor, Closeloop_Type_e loop, Feedback_Source_e type) { if (loop == ANGLE_LOOP) { motor->motor_settings.angle_feedback_source = type; } - if (loop == SPEED_LOOP) + else if (loop == SPEED_LOOP) { motor->motor_settings.speed_feedback_source = type; } + else + { + while (1) + ; // LOOP TYPE ERROR!!!检查是否传入了正确的LOOP类型,或发生了指针越界 + } } void DJIMotorStop(DJIMotorInstance *motor) @@ -195,6 +202,7 @@ void DJIMotorEnable(DJIMotorInstance *motor) motor->stop_flag = MOTOR_ENALBED; } +/* 修改电机的实际闭环对象 */ void DJIMotorOuterLoop(DJIMotorInstance *motor, Closeloop_Type_e outer_loop) { motor->motor_settings.outer_loop_type = outer_loop; @@ -211,70 +219,68 @@ void DJIMotorControl() { // 预先通过静态变量定义避免反复释放分配栈空间,直接保存一次指针引用从而减小访存的开销 // 同样可以提高可读性 - static uint8_t group, num; - static int16_t set; + static uint8_t group, num; // 电机组号和组内编号 + static int16_t set; // 电机控制CAN发送设定值 static DJIMotorInstance *motor; - static Motor_Control_Setting_s *motor_setting; - static Motor_Controller_s *motor_controller; - static DJI_Motor_Measure_s *motor_measure; - static float pid_measure, pid_ref; + static Motor_Control_Setting_s *motor_setting; // 电机控制参数 + static Motor_Controller_s *motor_controller; // 电机控制器 + static DJI_Motor_Measure_s *motor_measure; // 电机测量值 + static float pid_measure, pid_ref; // 电机PID测量值和设定值 // 遍历所有电机实例,进行串级PID的计算并设置发送报文的值 for (size_t i = 0; i < idx; ++i) - { - if (dji_motor_info[i]) + { // 减小访存开销,先保存指针引用 + motor = dji_motor_instance[i]; + motor_setting = &motor->motor_settings; + motor_controller = &motor->motor_controller; + motor_measure = &motor->motor_measure; + pid_ref = motor_controller->pid_ref; // 保存设定值,防止motor_controller->pid_ref在计算过程中被修改 + + // pid_ref会顺次通过被启用的闭环充当数据的载体 + // 计算位置环,只有启用位置环且外层闭环为位置时会计算速度环输出 + if ((motor_setting->close_loop_type & ANGLE_LOOP) && motor_setting->outer_loop_type == ANGLE_LOOP) { - motor = dji_motor_info[i]; - motor_setting = &motor->motor_settings; - motor_controller = &motor->motor_controller; - motor_measure = &motor->motor_measure; - pid_ref = motor_controller->pid_ref; // 保存设定值,防止motor_controller->pid_ref在计算过程中被修改 - - // pid_ref会顺次通过被启用的闭环充当数据的载体 - // 计算位置环,只有启用位置环且外层闭环为位置时会计算速度环输出 - if ((motor_setting->close_loop_type & ANGLE_LOOP) && motor_setting->outer_loop_type == ANGLE_LOOP) - { - if (motor_setting->angle_feedback_source == OTHER_FEED) - pid_measure = *motor_controller->other_angle_feedback_ptr; - else // MOTOR_FEED - pid_measure = motor_measure->total_angle; // 对total angle闭环,防止在边界处出现突跃 - // 更新pid_ref进入下一个环 - pid_ref = PID_Calculate(&motor_controller->angle_PID, pid_measure, pid_ref); - } - - // 计算速度环,(外层闭环为速度或位置)且(启用速度环)时会计算速度环 - if ((motor_setting->close_loop_type & SPEED_LOOP) && (motor_setting->outer_loop_type & (ANGLE_LOOP | SPEED_LOOP))) - { - if (motor_setting->speed_feedback_source == OTHER_FEED) - pid_measure = *motor_controller->other_speed_feedback_ptr; - else // MOTOR_FEED - pid_measure = motor_measure->speed_aps; - // 更新pid_ref进入下一个环 - pid_ref = PID_Calculate(&motor_controller->speed_PID, pid_measure, pid_ref); - } - - // 计算电流环,只要启用了电流环就计算,不管外层闭环是什么,并且电流只有电机自身传感器的反馈 - if (motor_setting->close_loop_type & CURRENT_LOOP) - { - pid_ref = PID_Calculate(&motor_controller->current_PID, motor_measure->real_current, pid_ref); - } - - // 获取最终输出 - set = (int16_t)pid_ref; - if (motor_setting->reverse_flag == MOTOR_DIRECTION_REVERSE) - set *= -1; // 设置反转 - - // 分组填入发送数据 - group = motor->sender_group; - num = motor->message_num; - sender_assignment[group].tx_buff[2 * num] = (uint8_t)(set >> 8); - sender_assignment[group].tx_buff[2 * num + 1] = (uint8_t)(set & 0x00ff); - - // 电机是否停止运行 - if (motor->stop_flag == MOTOR_STOP) - { // 若该电机处于停止状态,直接将buff置零 - memset(sender_assignment[group].tx_buff + 2 * num, 0, 16u); - } + if (motor_setting->angle_feedback_source == OTHER_FEED) + pid_measure = *motor_controller->other_angle_feedback_ptr; + else + pid_measure = motor_measure->total_angle; // MOTOR_FEED,对total angle闭环,防止在边界处出现突跃 + // 更新pid_ref进入下一个环 + pid_ref = PID_Calculate(&motor_controller->angle_PID, pid_measure, pid_ref); } + + // 计算速度环,(外层闭环为速度或位置)且(启用速度环)时会计算速度环 + if ((motor_setting->close_loop_type & SPEED_LOOP) && (motor_setting->outer_loop_type & (ANGLE_LOOP | SPEED_LOOP))) + { + if (motor_setting->speed_feedback_source == OTHER_FEED) + pid_measure = *motor_controller->other_speed_feedback_ptr; + else // MOTOR_FEED + pid_measure = motor_measure->speed_aps; + // 更新pid_ref进入下一个环 + pid_ref = PID_Calculate(&motor_controller->speed_PID, pid_measure, pid_ref); + } + + // 计算电流环,目前只要启用了电流环就计算,不管外层闭环是什么,并且电流只有电机自身传感器的反馈 + if (motor_setting->close_loop_type & CURRENT_LOOP) + { + pid_ref = PID_Calculate(&motor_controller->current_PID, motor_measure->real_current, pid_ref); + } + + // 获取最终输出 + set = (int16_t)pid_ref; + if (motor_setting->reverse_flag == MOTOR_DIRECTION_REVERSE) + set *= -1; // 设置反转 + + // 分组填入发送数据 + group = motor->sender_group; + num = motor->message_num; + sender_assignment[group].tx_buff[2 * num] = (uint8_t)(set >> 8); + sender_assignment[group].tx_buff[2 * num + 1] = (uint8_t)(set & 0x00ff); + + // 电机是否停止运行 + if (motor->stop_flag == MOTOR_STOP) + { // 若该电机处于停止状态,直接将buff置零 + memset(sender_assignment[group].tx_buff + 2 * num, 0, 16u); + } + } // 遍历flag,检查是否要发送这一帧报文 diff --git a/modules/motor/servo_motor.c b/modules/motor/servo_motor.c index 80132cc..fb15863 100644 --- a/modules/motor/servo_motor.c +++ b/modules/motor/servo_motor.c @@ -1,22 +1,26 @@ #include "servo_motor.h" +#include "stdlib.h" +#include "memory.h" extern TIM_HandleTypeDef htim1; /*第二版*/ -static ServoInstance *servo_motor[SERVO_MOTOR_CNT] = {NULL}; -static int16_t compare_value[SERVO_MOTOR_CNT]={0}; +static ServoInstance *servo_motor_instance[SERVO_MOTOR_CNT] = {NULL}; +static int16_t compare_value[SERVO_MOTOR_CNT] = {0}; static uint8_t servo_idx = 0; // register servo_idx,是该文件的全局舵机索引,在注册时使用 // 通过此函数注册一个舵机 ServoInstance *ServoInit(Servo_Init_Config_s *Servo_Init_Config) { - servo_motor[servo_idx] = (ServoInstance *)malloc(sizeof(ServoInstance)); - memset(servo_motor[servo_idx], 0, sizeof(ServoInstance)); - servo_motor[servo_idx]->Servo_type = Servo_Init_Config->Servo_type; - servo_motor[servo_idx]->htim = Servo_Init_Config->htim; - servo_motor[servo_idx]->Channel = Servo_Init_Config->Channel; - HAL_TIM_Base_Start(Servo_Init_Config->htim); + ServoInstance *servo = (ServoInstance *)malloc(sizeof(ServoInstance)); + memset(servo, 0, sizeof(ServoInstance)); + + servo->Servo_type = Servo_Init_Config->Servo_type; + servo->htim = Servo_Init_Config->htim; + servo->Channel = Servo_Init_Config->Channel; + HAL_TIM_PWM_Start(Servo_Init_Config->htim, Servo_Init_Config->Channel); - return servo_motor[servo_idx++]; + servo_motor_instance[servo_idx++] = servo; + return servo; } /** @@ -63,16 +67,15 @@ void Servo_Motor_StartSTOP_Angle_Set(ServoInstance *Servo_Motor, int16_t Start_a } /** * @brief 舵机模式选择 - * + * * @param Servo_Motor 注册的舵机实例 * @param mode 需要选择的模式 */ -void Servo_Motor_Type_Select(ServoInstance *Servo_Motor,int16_t mode) +void Servo_Motor_Type_Select(ServoInstance *Servo_Motor, int16_t mode) { Servo_Motor->Servo_Angle_Type = mode; } - /** * @brief 舵机输出控制 * @@ -84,9 +87,9 @@ void Servo_Motor_Control() for (size_t i = 0; i < servo_idx; i++) { - if (servo_motor[i]) + if (servo_motor_instance[i]) { - Servo_Motor = servo_motor[i]; + Servo_Motor = servo_motor_instance[i]; Servo_type = Servo_Motor->Servo_type; switch (Servo_type) diff --git a/modules/super_cap/super_cap.c b/modules/super_cap/super_cap.c index 9c47809..fecbf84 100644 --- a/modules/super_cap/super_cap.c +++ b/modules/super_cap/super_cap.c @@ -26,6 +26,7 @@ SuperCapInstance *SuperCapInit(SuperCap_Init_Config_s *supercap_config) { super_cap_instance = (SuperCapInstance *)malloc(sizeof(SuperCapInstance)); memset(super_cap_instance, 0, sizeof(SuperCapInstance)); + supercap_config->can_config.can_module_callback = SuperCapRxCallback; super_cap_instance->can_ins = CANRegister(&supercap_config->can_config); return super_cap_instance; diff --git a/modules/super_cap/super_cap.h b/modules/super_cap/super_cap.h index 06d889e..0ac4754 100644 --- a/modules/super_cap/super_cap.h +++ b/modules/super_cap/super_cap.h @@ -28,8 +28,6 @@ typedef struct typedef struct { CAN_Init_Config_s can_config; - uint8_t send_data_len; - uint8_t recv_data_len; } SuperCap_Init_Config_s; SuperCapInstance *SuperCapInit(SuperCap_Init_Config_s *supercap_config);