本文代码可以实现:上位机通过串口向单片机传输数据,通过DMA将数据缓存到存储器,然后DMA将数据通过串口传回到上位机。
前言
使用的硬件:
STM32F103ZET6 串口1:PA9、PA10 借鉴了网上的一些优秀文章: 一个严谨的STM32串口DMA发送&接收 STM32 | 串口DMA很难?其实就是如此简单!(超详细、附代码) stm32 利用DMA+串口空闲中断接受任意长数据 但网上的文章或多或少有一些错误,踩了一些坑,尤其是串口空闲中断的清零,不少文章写错了。
一、为什么使用DMA
使用DMA在ROM和IO设备间传输数据,不需要CPU控制,也不需要频繁产生中断,减轻CPU的负担。
二、代码
1.串口初始化
void uart1_init(u32 bound){
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_IDLE , ENABLE);
USART_Cmd(USART1, ENABLE);
}
2.DMA接收初始化
void DMA_Use_USART1_Rx_Init(void)
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
DMA_DeInit(DMA1_Channel5);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)u1rxbuf;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = USART1_MAX_RX_LEN;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);
DMA_Cmd(DMA1_Channel5, ENABLE);
}
3.DMA发送初始化
void DMA_Use_USART1_Tx_Init(void)
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
DMA_DeInit(DMA1_Channel4);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USART1->DR;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)USART1_TX_BUF;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = USART1_MAX_RX_LEN;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,ENABLE);
USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
DMA_Cmd(DMA1_Channel4, DISABLE);
}
4.DMA发送数据函数
void DMA_USART1_Tx_Data(u8 *buffer, u32 size)
{
while(USART1_TX_FLAG);
USART1_TX_FLAG=1;
DMA1_Channel4->CMAR = (uint32_t)buffer;
DMA1_Channel4->CNDTR = size;
DMA_Cmd(DMA1_Channel4, ENABLE);
}
void DMA1_Channel4_IRQHandler(void)
{
if(DMA_GetITStatus(DMA1_IT_TC4)!= RESET)
{
DMA_ClearITPendingBit(DMA1_IT_TC4);
USART_ClearFlag(USART1,USART_FLAG_TC);
DMA_Cmd(DMA1_Channel4, DISABLE );
USART1_TX_FLAG=0;
}
}
5.DMA接收不定长数据
void USART1_IRQHandler(void)
{
u8 rc_temp;
u8 *p;
u8 i;
u8 USART1_RX_LEN = 0;
if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
{
rc_temp=USART1->SR;
rc_temp=USART1->DR;
DMA_Cmd(DMA1_Channel5, DISABLE );
USART1_RX_LEN = USART1_MAX_RX_LEN - DMA1_Channel5->CNDTR;
if(witchbuf)
{
p=u2rxbuf;
DMA1_Channel5->CMAR=(u32)u1rxbuf;
witchbuf=0;
}else
{
p=u1rxbuf;
DMA1_Channel5->CMAR=(u32)u2rxbuf;
witchbuf=1;
}
DMA1_Channel5->CNDTR = USART1_MAX_RX_LEN;
DMA_Cmd(DMA1_Channel5, ENABLE);
DMA_USART1_Tx_Data(p,USART1_RX_LEN);
if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
{
rc_temp=USART1->SR;
rc_temp=USART1->DR;
for(i=0;i<USART1_RX_LEN;i++)
{
p[i]=0x00;
}
}
}
}
总结
本文提供了串口+DMA传输数据的实现代码,借鉴了网上的不少代码,在此基础上进行修改。接收缓存使用了双缓存,后续将介绍使用FIFO的代码。
|