其实我是定义了两个数组
,就是
//unsigned char Uart1_Send_Buffer[150];
//unsigned char Uart1_RX_buffer[150];
DMA直接把接受到的数据存在Uart1_RX_buffer里面。空闲中断直接告诉我有数据接受完成,我就在大循环里处理数据,我以前做过这个东西,但是现在与时俱进用HAL库就不熟悉了,看看大家有什么好的建议,下面的程序可以用,但是有点小问题,我要发两个50字节的数据他才能收到一组,而且还不自动进入中断回调函数,我还是把回调函数放在中断中的,肯定有问题,这个是第二天 了,求大家看看,DMA接受数据不限定个数的!!!
//重定义fputc函数
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);//循环发送,直到发送完毕
USART1->DR = (u8) ch;
return ch;
}
UART_HandleTypeDef UartHandle;
static DMA_HandleTypeDef hdma_tx;
static DMA_HandleTypeDef hdma_rx;
void init_Uart(u32 BaudRate)
{
UartHandle.Instance = USART1;
UartHandle.Init.BaudRate = BaudRate;
UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
UartHandle.Init.StopBits = UART_STOPBITS_1;
UartHandle.Init.Parity = UART_PARITY_NONE;
UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
UartHandle.Init.Mode = UART_MODE_TX_RX;
UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
if(HAL_UART_Init(&UartHandle) != HAL_OK)
{
// printf("error with USART Init");
}
}
void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
GPIO_InitTypeDef GPIO_InitStruct;
/******Enable peripherals and GPIO Clocks ***/
/* Enable GPIO TX/RX clock */
__HAL_RCC_GPIOA_CLK_ENABLE();
/* Enable USART1 clock */
__HAL_RCC_USART1_CLK_ENABLE();
/* Enable DMA2 Clock*/
__HAL_RCC_DMA2_CLK_ENABLE();
/**** Configure peripheral GPIO *****/
/* UART TX GPIO pin configuration */
GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* UART RX GPIO pin configuration */
GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
HAL_GPIO_Init(GPIOA , &GPIO_InitStruct);
////////////////////////configure the DMA///////////////
hdma_tx.Instance = DMA2_Stream7;
hdma_tx.Init.Channel = DMA_CHANNEL_4;
hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_tx.Init.MemInc = DMA_MINC_ENABLE;
hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_tx.Init.Mode = DMA_NORMAL;
hdma_tx.Init.Priority = DMA_PRIORITY_LOW;
hdma_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma_tx.Init.MemBurst = DMA_MBURST_INC4;
hdma_tx.Init.PeriphBurst = DMA_PBURST_INC4;
HAL_DMA_Init(&hdma_tx);
/* Associate the initialized DMA handle to the the UART handle */
__HAL_LINKDMA(huart, hdmatx, hdma_tx);
/* Configure the DMA handler for Transmission process */
hdma_rx.Instance = DMA2_Stream5;
hdma_rx.Init.Channel = DMA_CHANNEL_4;
hdma_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_rx.Init.MemInc = DMA_MINC_ENABLE;
hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_rx.Init.Mode = DMA_NORMAL;
hdma_rx.Init.Priority = DMA_PRIORITY_HIGH;
hdma_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma_rx.Init.MemBurst = DMA_MBURST_INC4;
hdma_rx.Init.PeriphBurst = DMA_PBURST_INC4;
HAL_DMA_Init(&hdma_rx);
/* Associate the initialized DMA handle to the the UART handle */
__HAL_LINKDMA(huart, hdmarx, hdma_rx);
/*Configure the NVIC for DMA*/
/* NVIC configuration for DMA transfer complete interrupt (USARTx_TX) */
HAL_NVIC_SetPriority(DMA2_Stream7_IRQn, 0, 1);
HAL_NVIC_EnableIRQ(DMA2_Stream7_IRQn);
/* NVIC configuration for DMA transfer complete interrupt (USARTx_RX) */
HAL_NVIC_SetPriority(DMA2_Stream5_IRQn, 0, 2);
HAL_NVIC_EnableIRQ(DMA2_Stream5_IRQn);
/* NVIC configuration for USART TC interrupt */
HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USART1_IRQn);
__HAL_UART_ENABLE_IT(&UartHandle, UART_IT_IDLE);
HAL_UART_Receive_DMA(&UartHandle, (uint8_t *)Uart1_RX_buffer, 150);
}
void USART1_DMA_int(u32 BaudRate)
{
init_Uart(BaudRate);
}
void DMA2_Stream5_IRQHandler(void)
{
HAL_DMA_IRQHandler(UartHandle.hdmarx);
}
void DMA2_Stream7_IRQHandler(void)
{
HAL_DMA_IRQHandler(UartHandle.hdmatx);
}
void USART1_IRQHandler(void)
{
HAL_UART_IRQHandler(&UartHandle);
HAL_UART_RxCpltCallback(&UartHandle);
}
void USART1_DMA_send(uint16_t Size)
{
while(UartData.UART1_Send_Flag== 0);//等待上次发送完成
UartData.UART1_Send_Flag= 0;//清零标记 表示这次次发送未完成
HAL_UART_Transmit_DMA(&UartHandle, Uart1_Send_Buffer,Size);
}
/*DMA发送完成中断回调函数*/
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
__HAL_DMA_DISABLE(huart->hdmatx);
//发送完成
UartData.UART1_Send_Flag=1;
}
/*串口接收中断回调函数*/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
uint32_t temp,rx_len;
if((__HAL_UART_GET_FLAG(&UartHandle,UART_FLAG_IDLE) != RESET))
{
__HAL_UART_CLEAR_IDLEFLAG(&UartHandle);
temp = UartHandle.Instance->SR;
temp = UartHandle.Instance->DR;
HAL_UART_DMAStop(&UartHandle);
temp = hdma_rx.Instance->NDTR;
rx_len = 150 - temp;
// receive_flag=1;
UartData.UART1_RXD_Flag=1;
__HAL_UART_ENABLE_IT(&UartHandle, UART_IT_IDLE);
huart->hdmarx->State = HAL_DMA_STATE_READY;
// HAL_UART_Receive_DMA(&UartHandle,usartDMA_rxBuf,RECEIVELEN);
HAL_UART_Receive_DMA(&UartHandle, Uart1_RX_buffer,150);
}
}
void USART1_DMA_Receive(uint16_t Size)
{
HAL_UART_Receive_DMA(&UartHandle, Uart1_RX_buffer,Size);
} |