]> Zhao Yanbai Git Server - acecode.git/commitdiff
usart1 usart2 dma 转发
authorAceVest <zhaoyanbai@126.com>
Mon, 17 Jun 2019 05:32:07 +0000 (13:32 +0800)
committerAceVest <zhaoyanbai@126.com>
Mon, 17 Jun 2019 05:32:07 +0000 (13:32 +0800)
learn/stm32/F103RE/Src/gpio.c
learn/stm32/USART.md

index 27bf9810b68e9ecac7f0911c58dcafbab402ae76..5cd04121a3b3b4ff5fd2339baa855bc1181cb179 100644 (file)
@@ -71,7 +71,7 @@ void MX_GPIO_Init(void)
 \r
 /* USER CODE BEGIN 2 */\r
 void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {\r
-       HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, SET);\r
+       HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);\r
 }\r
 /* USER CODE END 2 */\r
 \r
index 52a8bd89514ef8fac3ed8b40be38dd6add9af7df..094d22bfc4df3219d241df3608b4becdc30a0dbd 100644 (file)
@@ -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