From ef9e4bb58926711f416de1350a61fd67f1fd95ad Mon Sep 17 00:00:00 2001 From: "CSSC-WORK\\murmur" Date: Tue, 17 Dec 2024 11:08:28 +0800 Subject: [PATCH] update DMA --- Core/Inc/main.h | 8 ++++++-- Core/Inc/stm32f1xx_it.h | 2 ++ Core/Src/dma.c | 6 ++++++ Core/Src/main.c | 15 +++++++++++++-- Core/Src/stm32f1xx_it.c | 40 +++++++++++++++++++++++++++++++++++++++- Core/Src/usart.c | 39 +++++++++++++++++++++++++++++++++++++++ f1CTL.ioc | 26 ++++++++++++++++++++++++-- 7 files changed, 129 insertions(+), 7 deletions(-) diff --git a/Core/Inc/main.h b/Core/Inc/main.h index e50431d..cb81e8d 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -36,12 +36,16 @@ extern "C" { /* Exported types ------------------------------------------------------------*/ /* USER CODE BEGIN ET */ - +extern volatile uint8_t hostRxLen; +extern volatile uint8_t hostRxEnd; +extern volatile uint8_t motorRxLen; +extern volatile uint8_t motorRxEnd; /* USER CODE END ET */ /* Exported constants --------------------------------------------------------*/ /* USER CODE BEGIN EC */ - +#define HOST_BUFFER_SIZE 64 +#define MOTOR_BUFFER_SIZE 64 /* USER CODE END EC */ /* Exported macro ------------------------------------------------------------*/ diff --git a/Core/Inc/stm32f1xx_it.h b/Core/Inc/stm32f1xx_it.h index e602ea5..1c8bb2d 100644 --- a/Core/Inc/stm32f1xx_it.h +++ b/Core/Inc/stm32f1xx_it.h @@ -55,6 +55,8 @@ void SVC_Handler(void); void DebugMon_Handler(void); void PendSV_Handler(void); void SysTick_Handler(void); +void DMA1_Channel2_IRQHandler(void); +void DMA1_Channel3_IRQHandler(void); void DMA1_Channel4_IRQHandler(void); void DMA1_Channel5_IRQHandler(void); void USART1_IRQHandler(void); diff --git a/Core/Src/dma.c b/Core/Src/dma.c index e01e5d3..36438b5 100644 --- a/Core/Src/dma.c +++ b/Core/Src/dma.c @@ -43,6 +43,12 @@ void MX_DMA_Init(void) __HAL_RCC_DMA1_CLK_ENABLE(); /* DMA interrupt init */ + /* DMA1_Channel2_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(DMA1_Channel2_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(DMA1_Channel2_IRQn); + /* DMA1_Channel3_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(DMA1_Channel3_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(DMA1_Channel3_IRQn); /* DMA1_Channel4_IRQn interrupt configuration */ HAL_NVIC_SetPriority(DMA1_Channel4_IRQn, 0, 0); HAL_NVIC_EnableIRQ(DMA1_Channel4_IRQn); diff --git a/Core/Src/main.c b/Core/Src/main.c index 654ae72..51f3db7 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -49,7 +49,12 @@ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ - +volatile uint8_t hostRxLen = 0; //接收数据长度 +volatile uint8_t hostRxEnd = 0; //接收完成标记 +uint8_t hostRxBuf[HOST_BUFFER_SIZE]; //接收缓存 +volatile uint8_t motorRxLen = 0; //接收数据长度 +volatile uint8_t motorRxEnd = 0; //接收完成标记 +uint8_t motorRxBuf[MOTOR_BUFFER_SIZE]; //接收缓存 /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ @@ -120,7 +125,7 @@ int main(void) HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET); printf("Init success!\r\n"); // transDataToHost("hello", 5); - runPumpDemo(); + // runPumpDemo(); /* USER CODE END 2 */ /* Infinite loop */ @@ -131,6 +136,12 @@ int main(void) /* USER CODE BEGIN 3 */ http_server_task(); + + if (hostRxEnd == 1) { + hostRxEnd = 0; + transDataToHost(hostRxBuf, hostRxLen); + ProcessHostCommand(hostRxBuf, hostRxLen); + } // transDataToHost("hello\r\n", 7); // HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin); // HAL_Delay(100); diff --git a/Core/Src/stm32f1xx_it.c b/Core/Src/stm32f1xx_it.c index eb20716..da21029 100644 --- a/Core/Src/stm32f1xx_it.c +++ b/Core/Src/stm32f1xx_it.c @@ -57,6 +57,8 @@ /* External variables --------------------------------------------------------*/ extern DMA_HandleTypeDef hdma_usart1_rx; extern DMA_HandleTypeDef hdma_usart1_tx; +extern DMA_HandleTypeDef hdma_usart3_rx; +extern DMA_HandleTypeDef hdma_usart3_tx; extern UART_HandleTypeDef huart1; extern UART_HandleTypeDef huart2; extern UART_HandleTypeDef huart3; @@ -202,6 +204,34 @@ void SysTick_Handler(void) /* please refer to the startup file (startup_stm32f1xx.s). */ /******************************************************************************/ +/** + * @brief This function handles DMA1 channel2 global interrupt. + */ +void DMA1_Channel2_IRQHandler(void) +{ + /* USER CODE BEGIN DMA1_Channel2_IRQn 0 */ + + /* USER CODE END DMA1_Channel2_IRQn 0 */ + HAL_DMA_IRQHandler(&hdma_usart3_tx); + /* USER CODE BEGIN DMA1_Channel2_IRQn 1 */ + + /* USER CODE END DMA1_Channel2_IRQn 1 */ +} + +/** + * @brief This function handles DMA1 channel3 global interrupt. + */ +void DMA1_Channel3_IRQHandler(void) +{ + /* USER CODE BEGIN DMA1_Channel3_IRQn 0 */ + + /* USER CODE END DMA1_Channel3_IRQn 0 */ + HAL_DMA_IRQHandler(&hdma_usart3_rx); + /* USER CODE BEGIN DMA1_Channel3_IRQn 1 */ + + /* USER CODE END DMA1_Channel3_IRQn 1 */ +} + /** * @brief This function handles DMA1 channel4 global interrupt. */ @@ -264,7 +294,15 @@ void USART2_IRQHandler(void) void USART3_IRQHandler(void) { /* USER CODE BEGIN USART3_IRQn 0 */ - + if (__HAL_UART_GET_FLAG(&huart3, UART_FLAG_IDLE) != RESET) {// idle标志被置 + __HAL_UART_CLEAR_IDLEFLAG(&huart3); //清除标志 +// HAL_UART_DMAStop(&huart1); + hostRxLen = HOST_BUFFER_SIZE - __HAL_DMA_GET_COUNTER(&hdma_usart3_rx);//得到已经接收的数据个数 + if(hostRxLen >10){//避免部分主机一次不能发送完整的帧致使的误触发空闲中断 + hostRxEnd = 1; //接受完成标志位置1 + HAL_UART_DMAStop(&huart3); + } + } /* USER CODE END USART3_IRQn 0 */ HAL_UART_IRQHandler(&huart3); /* USER CODE BEGIN USART3_IRQn 1 */ diff --git a/Core/Src/usart.c b/Core/Src/usart.c index 5d217b1..64faba8 100644 --- a/Core/Src/usart.c +++ b/Core/Src/usart.c @@ -29,6 +29,8 @@ UART_HandleTypeDef huart2; UART_HandleTypeDef huart3; DMA_HandleTypeDef hdma_usart1_rx; DMA_HandleTypeDef hdma_usart1_tx; +DMA_HandleTypeDef hdma_usart3_rx; +DMA_HandleTypeDef hdma_usart3_tx; /* USART1 init function */ @@ -238,6 +240,39 @@ void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + /* USART3 DMA Init */ + /* USART3_RX Init */ + hdma_usart3_rx.Instance = DMA1_Channel3; + hdma_usart3_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; + hdma_usart3_rx.Init.PeriphInc = DMA_PINC_DISABLE; + hdma_usart3_rx.Init.MemInc = DMA_MINC_ENABLE; + hdma_usart3_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; + hdma_usart3_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; + hdma_usart3_rx.Init.Mode = DMA_NORMAL; + hdma_usart3_rx.Init.Priority = DMA_PRIORITY_LOW; + if (HAL_DMA_Init(&hdma_usart3_rx) != HAL_OK) + { + Error_Handler(); + } + + __HAL_LINKDMA(uartHandle,hdmarx,hdma_usart3_rx); + + /* USART3_TX Init */ + hdma_usart3_tx.Instance = DMA1_Channel2; + hdma_usart3_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; + hdma_usart3_tx.Init.PeriphInc = DMA_PINC_DISABLE; + hdma_usart3_tx.Init.MemInc = DMA_MINC_ENABLE; + hdma_usart3_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; + hdma_usart3_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; + hdma_usart3_tx.Init.Mode = DMA_NORMAL; + hdma_usart3_tx.Init.Priority = DMA_PRIORITY_LOW; + if (HAL_DMA_Init(&hdma_usart3_tx) != HAL_OK) + { + Error_Handler(); + } + + __HAL_LINKDMA(uartHandle,hdmatx,hdma_usart3_tx); + /* USART3 interrupt Init */ HAL_NVIC_SetPriority(USART3_IRQn, 0, 0); HAL_NVIC_EnableIRQ(USART3_IRQn); @@ -308,6 +343,10 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) */ HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10|GPIO_PIN_11); + /* USART3 DMA DeInit */ + HAL_DMA_DeInit(uartHandle->hdmarx); + HAL_DMA_DeInit(uartHandle->hdmatx); + /* USART3 interrupt Deinit */ HAL_NVIC_DisableIRQ(USART3_IRQn); /* USER CODE BEGIN USART3_MspDeInit 1 */ diff --git a/f1CTL.ioc b/f1CTL.ioc index 6e4da79..efe3d2b 100644 --- a/f1CTL.ioc +++ b/f1CTL.ioc @@ -4,7 +4,9 @@ CAD.pinconfig= CAD.provider= Dma.Request0=USART1_RX Dma.Request1=USART1_TX -Dma.RequestsNb=2 +Dma.Request2=USART3_RX +Dma.Request3=USART3_TX +Dma.RequestsNb=4 Dma.USART1_RX.0.Direction=DMA_PERIPH_TO_MEMORY Dma.USART1_RX.0.Instance=DMA1_Channel5 Dma.USART1_RX.0.MemDataAlignment=DMA_MDATAALIGN_BYTE @@ -23,6 +25,24 @@ Dma.USART1_TX.1.PeriphDataAlignment=DMA_PDATAALIGN_BYTE Dma.USART1_TX.1.PeriphInc=DMA_PINC_DISABLE Dma.USART1_TX.1.Priority=DMA_PRIORITY_LOW Dma.USART1_TX.1.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority +Dma.USART3_RX.2.Direction=DMA_PERIPH_TO_MEMORY +Dma.USART3_RX.2.Instance=DMA1_Channel3 +Dma.USART3_RX.2.MemDataAlignment=DMA_MDATAALIGN_BYTE +Dma.USART3_RX.2.MemInc=DMA_MINC_ENABLE +Dma.USART3_RX.2.Mode=DMA_NORMAL +Dma.USART3_RX.2.PeriphDataAlignment=DMA_PDATAALIGN_BYTE +Dma.USART3_RX.2.PeriphInc=DMA_PINC_DISABLE +Dma.USART3_RX.2.Priority=DMA_PRIORITY_LOW +Dma.USART3_RX.2.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority +Dma.USART3_TX.3.Direction=DMA_MEMORY_TO_PERIPH +Dma.USART3_TX.3.Instance=DMA1_Channel2 +Dma.USART3_TX.3.MemDataAlignment=DMA_MDATAALIGN_BYTE +Dma.USART3_TX.3.MemInc=DMA_MINC_ENABLE +Dma.USART3_TX.3.Mode=DMA_NORMAL +Dma.USART3_TX.3.PeriphDataAlignment=DMA_PDATAALIGN_BYTE +Dma.USART3_TX.3.PeriphInc=DMA_PINC_DISABLE +Dma.USART3_TX.3.Priority=DMA_PRIORITY_LOW +Dma.USART3_TX.3.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority File.Version=6 GPIO.groupedBy=Group By Peripherals KeepUserPlacement=false @@ -66,6 +86,8 @@ Mcu.UserName=STM32F103VCTx MxCube.Version=6.13.0 MxDb.Version=DB.6.0.130 NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false +NVIC.DMA1_Channel2_IRQn=true\:0\:0\:false\:false\:true\:false\:true\:true +NVIC.DMA1_Channel3_IRQn=true\:0\:0\:false\:false\:true\:false\:true\:true NVIC.DMA1_Channel4_IRQn=true\:0\:0\:false\:false\:true\:false\:true\:true NVIC.DMA1_Channel5_IRQn=true\:0\:0\:false\:false\:true\:false\:true\:true NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false @@ -164,7 +186,7 @@ ProjectManager.ToolChainLocation= ProjectManager.UAScriptAfterPath= ProjectManager.UAScriptBeforePath= ProjectManager.UnderRoot=true -ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_DMA_Init-DMA-false-HAL-true,4-MX_USART1_UART_Init-USART1-false-HAL-true,5-MX_USART2_UART_Init-USART2-false-HAL-true,6-MX_SPI2_Init-SPI2-false-HAL-true +ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_DMA_Init-DMA-false-HAL-true,4-MX_USART1_UART_Init-USART1-false-HAL-true,5-MX_USART2_UART_Init-USART2-false-HAL-true,6-MX_SPI2_Init-SPI2-false-HAL-true,7-MX_USART3_UART_Init-USART3-false-HAL-true RCC.ADCFreqValue=36000000 RCC.AHBFreq_Value=72000000 RCC.APB1CLKDivider=RCC_HCLK_DIV2