再试试贴代码吧,方便你我他,要积分的都无耻。改天
uart.c文件就这么多代码,
#include "Uart.h"
#include "stm32h7xx_hal.h"
#define RxBufSize 1024
UART_HandleTypeDef hUart1 = {0};
DMA_HandleTypeDef hDmaUart1Tx = {0};
DMA_HandleTypeDef hDmaUart1Rx = {0};
//数组后边的那个限定跟你的内存分配有关 如果你的主RAM在512K的那个片内存就可以不加 这是AC6编译器用法
uint8_t RxBuf[2][RxBufSize] __attribute__((section (".RAM_D1")));
void (*Uart1RxCompleteCallback)(uint8_t *pData,uint16_t *Count);
void Uart1Init(uint32_t BaudRate,void (*RxCompleteCallback)(uint8_t *pData,uint16_t *Count))
{
hUart1.Instance = USART1;
hUart1.Init.BaudRate = BaudRate;
hUart1.Init.ClockPrescaler = UART_PRESCALER_DIV2;
hUart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
hUart1.Init.Mode = UART_MODE_TX_RX;
hUart1.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED;
hUart1.Init.OverSampling = UART_OVERSAMPLING_8;
hUart1.Init.Parity = UART_PARITY_NONE;
hUart1.Init.StopBits = UART_STOPBITS_1;
hUart1.Init.WordLength = UART_WORDLENGTH_8B;
hUart1.FifoMode = UART_FIFOMODE_DISABLE;
HAL_UART_Init(&hUart1);
__HAL_RCC_DMA1_CLK_ENABLE();
hDmaUart1Tx.Instance = DMA1_Stream0;
hDmaUart1Tx.Init.Request = DMA_REQUEST_USART1_TX;
hDmaUart1Tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hDmaUart1Tx.Init.PeriphInc = DMA_PINC_DISABLE;
hDmaUart1Tx.Init.MemInc = DMA_MINC_ENABLE;
hDmaUart1Tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hDmaUart1Tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hDmaUart1Tx.Init.Mode = DMA_NORMAL;
hDmaUart1Tx.Init.Priority = DMA_PRIORITY_MEDIUM;
HAL_DMA_Init(&hDmaUart1Tx);
__HAL_LINKDMA(&hUart1,hdmatx,hDmaUart1Tx);
hDmaUart1Rx.Instance = DMA1_Stream1;
hDmaUart1Rx.Init.Request = DMA_REQUEST_USART1_RX;
hDmaUart1Rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hDmaUart1Rx.Init.PeriphInc = DMA_PINC_DISABLE;
hDmaUart1Rx.Init.MemInc = DMA_MINC_ENABLE;
hDmaUart1Rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hDmaUart1Rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hDmaUart1Rx.Init.Mode = DMA_NORMAL;
hDmaUart1Rx.Init.Priority = DMA_PRIORITY_MEDIUM;
HAL_DMA_Init(&hDmaUart1Rx);
__HAL_LINKDMA(&hUart1,hdmarx,hDmaUart1Rx);
HAL_UART_Receive_DMA(&hUart1,RxBuf[0],RxBufSize);
__HAL_UART_ENABLE_IT(&hUart1,UART_IT_IDLE);
HAL_NVIC_EnableIRQ(USART1_IRQn);
HAL_NVIC_SetPriority(USART1_IRQn,14,0);
HAL_NVIC_EnableIRQ(DMA1_Stream0_IRQn);//Tx
HAL_NVIC_SetPriority(DMA1_Stream0_IRQn,14,0);
HAL_NVIC_EnableIRQ(DMA1_Stream1_IRQn);//Rx
HAL_NVIC_SetPriority(DMA1_Stream1_IRQn,14,0);
Uart1RxCompleteCallback = RxCompleteCallback;
}
void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
if(huart == &hUart1)//串口1
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_9;
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_USART1_CLK_ENABLE();
HAL_GPIO_Init(GPIOA,&GPIO_InitStruct);
}
}
void Uart1TxData(uint8_t *pData,uint16_t Count)
{
if(Count)
HAL_UART_Transmit_DMA(&hUart1,pData,Count);
}
void USART1_IRQHandler(void)
{
if(__HAL_UART_GET_FLAG(&hUart1,UART_FLAG_IDLE))
{
static uint16_t count;
__HAL_UART_CLEAR_IDLEFLAG(&hUart1);
if(Uart1RxCompleteCallback)
{
hUart1.RxState = HAL_UART_STATE_READY;
hDmaUart1Rx.State = HAL_DMA_STATE_READY;
HAL_UART_RxCpltCallback(&hUart1);
}
}
else
HAL_UART_IRQHandler(&hUart1);
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart == &hUart1)
{
static uint16_t count;
count = RxBufSize - __HAL_DMA_GET_COUNTER(&hDmaUart1Rx);
if(count == 0)return;
hDmaUart1Rx.Lock = HAL_UNLOCKED;
if(huart->pRxBuffPtr < RxBuf[1])
{
Uart1RxCompleteCallback(RxBuf[0],&count);
HAL_UART_Receive_DMA(&hUart1,RxBuf[1],RxBufSize);
}
else
{
Uart1RxCompleteCallback(RxBuf[1],&count);
HAL_UART_Receive_DMA(&hUart1,RxBuf[0],RxBufSize);
}
hDmaUart1Rx.Lock = HAL_LOCKED;
}
}
void DMA1_Stream0_IRQHandler(void)
{
HAL_DMA_IRQHandler(&hDmaUart1Tx);
}
void DMA1_Stream1_IRQHandler(void)
{
HAL_DMA_IRQHandler(&hDmaUart1Rx);
}
|
dma很占优势