GD32F303RET6 串口空闲中断+DMA数据发送接收+环形缓冲区方式保存数据

本文介绍了GD32F303RET6芯片如何利用串口的空闲中断结合DMA进行数据的发送和接收,并详细阐述了环形缓冲区在该过程中的实现,包括DMA通道的映射关系,以及相关的源文件和头文件配置。
摘要由CSDN通过智能技术生成

GD32F303RET6 DMA 通道映射关系在这里插入图片描述在这里插入图片描述

串口

源文件

#include "uart.h"
#include
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是使用DMA收发数据的示例程序,包括了初始化、发送接收的函数实现。 首先,需要初始化USART和DMA: ```c void USART_DMA_Init(void) { DMA_HandleTypeDef hdma_usart_tx; DMA_HandleTypeDef hdma_usart_rx; // Enable DMA2 clock __HAL_RCC_DMA2_CLK_ENABLE(); // Enable USART1 clock __HAL_RCC_USART1_CLK_ENABLE(); // Configure DMA for USART1 TX hdma_usart_tx.Instance = DMA2_Stream7; hdma_usart_tx.Init.Channel = DMA_CHANNEL_4; hdma_usart_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; hdma_usart_tx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_usart_tx.Init.MemInc = DMA_MINC_ENABLE; hdma_usart_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_usart_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_usart_tx.Init.Mode = DMA_NORMAL; hdma_usart_tx.Init.Priority = DMA_PRIORITY_LOW; hdma_usart_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; HAL_DMA_Init(&hdma_usart_tx); // Associate the initialized DMA handle to the USART handle __HAL_LINKDMA(&huart1, hdmatx, hdma_usart_tx); // Configure DMA for USART1 RX hdma_usart_rx.Instance = DMA2_Stream2; hdma_usart_rx.Init.Channel = DMA_CHANNEL_4; hdma_usart_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; hdma_usart_rx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_usart_rx.Init.MemInc = DMA_MINC_ENABLE; hdma_usart_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_usart_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_usart_rx.Init.Mode = DMA_CIRCULAR; hdma_usart_rx.Init.Priority = DMA_PRIORITY_HIGH; hdma_usart_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; HAL_DMA_Init(&hdma_usart_rx); // Associate the initialized DMA handle to the USART handle __HAL_LINKDMA(&huart1, hdmarx, hdma_usart_rx); // Enable DMA transfer complete interrupt __HAL_DMA_ENABLE_IT(&hdma_usart_rx, DMA_IT_TC); // Enable USART1 global interrupt HAL_NVIC_SetPriority(USART1_IRQn, 1, 1); HAL_NVIC_EnableIRQ(USART1_IRQn); } ``` 然后,需要实现发送函数: ```c void USART_DMA_Send(uint8_t *pData, uint16_t Size) { // Wait for DMA TX buffer to be ready while (HAL_DMA_GetState(huart1.hdmatx) != HAL_DMA_STATE_READY); // Disable DMA TX stream transfer __HAL_DMA_DISABLE(huart1.hdmatx); // Configure DMA TX stream transfer HAL_DMA_Start_IT(huart1.hdmatx, (uint32_t)pData, (uint32_t)&huart1.Instance->DR, Size); // Enable DMA TX stream transfer __HAL_DMA_ENABLE(huart1.hdmatx); } ``` 最后,需要实现接收函数: ```c void USART_DMA_Receive(uint8_t *pData, uint16_t Size) { // Wait for DMA RX buffer to be ready while (HAL_DMA_GetState(huart1.hdmarx) != HAL_DMA_STATE_READY); // Disable DMA RX stream transfer __HAL_DMA_DISABLE(huart1.hdmarx); // Configure DMA RX stream transfer HAL_DMA_Start_IT(huart1.hdmarx, (uint32_t)&huart1.Instance->DR, (uint32_t)pData, Size); // Enable DMA RX stream transfer __HAL_DMA_ENABLE(huart1.hdmarx); // Enable USART1 HAL_UART_Receive_IT(&huart1, pData, Size); } ``` 以上就是使用DMA收发数据的示例程序。需要注意的是,我们使用了循环DMA模式来接收数据,并启用了DMA传输完成中断。这样可以保证在接收到指定长度的数据后,及时通知CPU进行处理。同时,在发送数据时,我们需要等待DMA传输完成后再进行下一次传输。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Car12

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值