STM32——DMA接收和发送的实现

时间:2022-05-04 09:11:47

最近写程序,需要一段一段数据的接收,再通过其他串口发送出去。

老司机们建议用DMA通信,以节约CPU资源。然后,我听了,发现挺好用的。特此,把自己写的代码贴上了。

DMA发送接收的步骤如下:

1.初始化。

a.IO时钟+串口时钟+DMA时钟使能。

b.IO初始化

c.串口初始化

d.DMA初始化

e.中断向量设置

2.串口中断服务函数

3.DMA中断服务函数

具体实现如下:

一。初始化

 //串口1的初始化 //bound:波特率
/************************************************/
void uart1_init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;

1。IO时钟+串口时钟+DMA时钟使能。

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);    //串口1时钟初始化+GPIOA时钟初始化
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);   //DMA1初始化

2.IO初始化

     USART_DeInit(USART1);  //¸´Î»´®¿Ú1
//USART1_TX PA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃÍÆÍìÊä³ö
GPIO_Init(GPIOA, &GPIO_InitStructure); //³õʼ»¯PA9
//USART1_RX PA.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë
GPIO_Init(GPIOA, &GPIO_InitStructure); //³õʼ»¯PA10

3.串口初始化

     USART_InitStructure.USART_BaudRate = bound;//Ò»°ãÉèÖÃΪ9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö³¤Îª8λÊý¾Ý¸ñʽ
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_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);//串口发送DMA使能
USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);//串口接收DMA使能
USART_Cmd(USART1, ENABLE);//ʹÄÜ´®¿Ú
USART_ClearFlag(USART1,USART_FLAG_TC);//Çå³ý·¢ËÍÍê³É±êÖ¾

4。DMA初始化

 //DMA1_Channel5 -> USART1_Rx
DMA_Cmd(DMA1_Channel5,DISABLE);
DMA_DeInit(DMA1_Channel5);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);//ÍâÉèΪUSART2->DR
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)USART1_Receive_Data;//ÄÚ´æ»ùµØÖ·
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//ÍâÉè×÷ΪÊý¾Ý´«ÊäµÄÄ¿µÄµØ£¬½ÓÊÕ
DMA_InitStructure.DMA_BufferSize = ;//ÄÚ´æ´óС
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//ÍâÉèµØÖ·¼Ä´æÆ÷²»±ä
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//ÄÚ´æµØÖ·¼Ä´æÆ÷µÝÔö
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//Êý¾Ý¿í¶ÈΪ8λ
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;//Êý¾Ý¿í¶ÈΪ8λ
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//¹¤×÷ÔÚÕý³£»º´æģʽ
DMA_InitStructure.DMA_Priority = DMA_Priority_High;//DMAͨµÀ5¾ßÓиßÓÅÏȼ¶
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//DMAͨµÀ6ûÓÐÉèÖÃΪÄÚ´æµ½ÄÚ´æ´«Êä
DMA_Init(DMA1_Channel5,&DMA_InitStructure);
DMA_ITConfig(DMA1_Channel5,DMA_IT_TC,ENABLE);//ʹÄÜ´«ÊäÍê³ÉÖжÏ
DMA_Cmd(DMA1_Channel5,ENABLE);
//DMA1_Channel4 -> USART1_Tx
DMA_Cmd(DMA1_Channel4,DISABLE);
DMA_DeInit(DMA1_Channel4);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);//ÍâÉèΪUSART2->DR
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)USART_Send.USART1_Send_Data;//ÄÚ´æ»ùµØÖ·
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;//ÍâÉè×÷ΪÊý¾Ý´«ÊäµÄÀ´Ô´£¬·¢ËÍ
DMA_InitStructure.DMA_BufferSize = ;//ÄÚ´æ´óС
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//ÍâÉèµØÖ·¼Ä´æÆ÷²»±ä
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//ÄÚ´æµØÖ·¼Ä´æÆ÷µÝÔö
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//Êý¾Ý¿í¶ÈΪ8λ
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;//Êý¾Ý¿í¶ÈΪ8λ
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//¹¤×÷ÔÚÕý³£»º´æģʽ
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;//DMAͨµÀ5¾ßÓиßÓÅÏȼ¶
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//DMAͨµÀ6ûÓÐÉèÖÃΪÄÚ´æµ½ÄÚ´æ´«Êä
DMA_Init(DMA1_Channel4,&DMA_InitStructure);
DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,ENABLE);//ʹÄÜ´«ÊäÍê³ÉÖжÏ
DMA_Cmd(DMA1_Channel4,ENABLE);

5.中断向量设置

     //USART1_NVIC
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= ;//ÇÀÕ¼ÓÅÏȼ¶1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = ; //×ÓÓÅÏȼ¶0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀʹÄÜ
NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ
//DMA_Channel5 NVIC -> USART1_Rx
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = ;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//DMA_Channel4 NVIC -> USART1_Tx
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = ;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}

//以上的话,初始化就完成了~~~~~,下面是USART中断服务函数

二。串口中断函数

 #if EN_USART1_RX   //Èç¹ûʹÄÜÁ˽ÓÊÕ
void USART1_IRQHandler(void) //´®¿Ú1ÖжϷþÎñ³ÌÐò
{
u16 DATA_LEN;
u16 i = ;
u8 Res;
if(USART_GetITStatus(USART1, USART_IT_IDLE ) != RESET) //¿ÕÏÐÖжÏ
{
Res = USART1->SR;
Res = USART1->DR;//Çå³ý¿ÕÏÐÖжϱêÖ¾ DMA_Cmd(DMA1_Channel5,DISABLE);//¹Ø±ÕDMA,·ÀÖ¹´¦ÀíÆä¼äÓÐÊý¾Ý
DATA_LEN = - DMA_GetCurrDataCounter(DMA1_Channel5);//»ñÈ¡Êý¾Ý³¤¶È
//±£´æ½ÓÊÕµ½µÄÊý¾Ý,·ÅÖõ½USART2_SendData
USART_Send.USART1_Len_R = DATA_LEN; //±£´æÊý¾Ý³¤¶È
for(i=;i<DATA_LEN;i++)
{
USART_Send.USART1_Receive_Data[i] = USART1_Receive_Data[i];//±£´æÊý¾Ý
}
USART_Send.USART1_Rx_Finish = ;//±êÖ¾ÖÃ1£¬±íʾÊý¾Ý±£´æÍê±Ï DMA1_Channel5->CNDTR = ;//ÖØÐÂÉèÖÃDMA1_Channel5µÄÊý¾Ý´«ÊäÊýÁ¿
DMA_Cmd(DMA1_Channel5,ENABLE);//¿ªÆôDMA1_Channel5
} }

三。DMA通道中断服务函数

//DMA1_Channel5-RxÖжϷþÎñº¯Êý,usart1_Rx
void DMA1_Channel5_IRQHandler(void)
{
DMA_ClearITPendingBit(DMA1_IT_TE5);//Çå³ý´íÎóÖжÏ
if(DMA_GetITStatus(DMA1_IT_TC5))//·¢ÉúÁË´«ÊäÍê³ÉÖжÏ
{
u16 DATA_LEN;
u16 i = ;
u8 Res;
DMA_ClearITPendingBit(DMA1_IT_TC5);//Çå³ýÖжÏÍê³É±êÖ¾
DMA_Cmd(DMA1_Channel5,DISABLE);//¹Ø±ÕDMA,·ÀÖ¹´¦ÀíÆÚ¼äÓÐÊý¾Ý DATA_LEN = - DMA_GetCurrDataCounter(DMA1_Channel5);//»ñÈ¡Êý¾Ý³¤¶È
//±£´æ½ÓÊÕµ½µÄÊý¾Ý,·ÅÖõ½USART2_SendData
USART_Send.USART1_Len_R = DATA_LEN; //±£´æÊý¾Ý³¤¶È
for(i=;i<DATA_LEN;i++)
{
USART_Send.USART1_Receive_Data[i] = USART1_Receive_Data[i];//±£´æÊý¾Ý
}
USART_Send.USART1_Rx_Finish = ;//±êÖ¾ÖÃ1£¬±íʾÊý¾Ý±£´æÍê±Ï DMA1_Channel5->CNDTR = ;//ÖØÐÂÌî×°
DMA_Cmd(DMA1_Channel5,ENABLE);//´¦ÀíÍê³Éºó£¬ÖØ¿ªDMA
}
}
//DMA1_Channel4-TxÖжϷþÎñº¯Êý,usart1_Tx
void DMA1_Channel4_IRQHandler(void)
{
DMA_ClearITPendingBit(DMA1_IT_TE4);//Çå³ý´íÎóÖжϱêÖ¾
if(DMA_GetITStatus(DMA1_IT_TC4))//·¢Éú´«ÊäÍê³ÉÖжϱêÖ¾
{
DMA_ClearITPendingBit(DMA1_IT_TC4);//Çå³ýÖжϱêÖ¾
DMA_Cmd(DMA1_Channel4,DISABLE);//¹Ø±ÕÖжÏ
USART_Send.USART1_Tx_Finish = ;
}
}

很困,别的+注释这周补上。。。