From: AceVest Date: Mon, 17 Jun 2019 05:32:07 +0000 (+0800) Subject: usart1 usart2 dma 转发 X-Git-Url: http://zhaoyanbai.com/repos/html/index.html?a=commitdiff_plain;h=3f63d1227ab2587575020ef61f59f848332d6efd;p=acecode.git usart1 usart2 dma 转发 --- diff --git a/learn/stm32/F103RE/Src/gpio.c b/learn/stm32/F103RE/Src/gpio.c index 27bf981..5cd0412 100644 --- a/learn/stm32/F103RE/Src/gpio.c +++ b/learn/stm32/F103RE/Src/gpio.c @@ -71,7 +71,7 @@ void MX_GPIO_Init(void) /* USER CODE BEGIN 2 */ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { - HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, SET); + HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin); } /* USER CODE END 2 */ diff --git a/learn/stm32/USART.md b/learn/stm32/USART.md index 52a8bd8..094d22b 100644 --- a/learn/stm32/USART.md +++ b/learn/stm32/USART.md @@ -109,4 +109,117 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { { TransferUsartData(); } +``` + + +# USART1、USART2 DMA转发 +``` +#define USART_RX_BUFSZ 64 +typedef struct { + uint32_t len : 24; + uint32_t end : 8; + DMA_HandleTypeDef *dmarx; + DMA_HandleTypeDef *dmatx; + uint8_t buf[USART_RX_BUFSZ]; +}UsartDmaData_t; + +UsartDmaData_t usart1_dma_data; +UsartDmaData_t usart2_dma_data; + +void InitUsartDmaTransfer(){ + __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE); + __HAL_UART_ENABLE_IT(&huart2, UART_IT_IDLE); + + memset(&usart1_dma_data, 0, sizeof(usart1_dma_data)); + memset(&usart2_dma_data, 0, sizeof(usart2_dma_data)); + + usart1_dma_data.dmarx = &hdma_usart1_rx; + usart2_dma_data.dmarx = &hdma_usart2_rx; + usart1_dma_data.dmatx = &hdma_usart1_tx; + usart2_dma_data.dmatx = &hdma_usart2_tx; + + HAL_UART_Receive_DMA(&huart1, usart1_dma_data.buf, USART_RX_BUFSZ); + HAL_UART_Receive_DMA(&huart2, usart2_dma_data.buf, USART_RX_BUFSZ); +} + + +void TransferToUsart(UsartDmaData_t *data, UART_HandleTypeDef *from_huart, UART_HandleTypeDef *to_huart){ + if(data->end != 1) { + return; + } + + HAL_UART_Transmit_DMA(to_huart, data->buf, data->len); + //HAL_UART_Transmit(to_huart, data->buf, data->len, 0xFFFF); + data->len = 0; + data->end = 0; + HAL_UART_Receive_DMA(from_huart, data->buf, USART_RX_BUFSZ); +} + +void TransferUsartData() { + TransferToUsart(&usart1_dma_data, &huart1, &huart2); + TransferToUsart(&usart2_dma_data, &huart2, &huart1); +} + +void ReceiveUsartDmaData(UsartDmaData_t *data, UART_HandleTypeDef *huart) { + uint32_t flag = __HAL_UART_GET_FLAG(huart,UART_FLAG_IDLE); + if(flag == RESET) { + return; + } + + __HAL_UART_CLEAR_IDLEFLAG(huart); + + HAL_UART_DMAStop(huart); + + data->len = USART_RX_BUFSZ - data->dmarx->Instance->CNDTR; + data->end = 1; +} + + +void ReceiveUsartData(UART_HandleTypeDef *huart) { + if(huart->Instance == USART1) { + return ReceiveUsartDmaData(&usart1_dma_data, huart); + } + + if(huart->Instance == USART2) { + return ReceiveUsartDmaData(&usart2_dma_data, huart); + } +} +``` + +``` +// stm32f1xx_it.c + + +void USART1_IRQHandler(void) +{ + /* USER CODE BEGIN USART1_IRQn 0 */ + + /* USER CODE END USART1_IRQn 0 */ + HAL_UART_IRQHandler(&huart1); + /* USER CODE BEGIN USART1_IRQn 1 */ + ReceiveUsartData(&huart1); + /* USER CODE END USART1_IRQn 1 */ +} + + +void USART2_IRQHandler(void) +{ + /* USER CODE BEGIN USART2_IRQn 0 */ + + /* USER CODE END USART2_IRQn 0 */ + HAL_UART_IRQHandler(&huart2); + /* USER CODE BEGIN USART2_IRQn 1 */ + ReceiveUsartData(&huart2); + /* USER CODE END USART2_IRQn 1 */ +} + +``` + +``` + // main.c + InitUsartDmaTransfer(); + while (1) + { + TransferUsartData(); + } ``` \ No newline at end of file