使用DMA1时,会发现串口2,3,4,5,6,8都在DMA1上,有时候要这其中的两个都用DMA1不方便,因此试着配了一下。
下面是串口5和串口7同时使用DMA1的程序,已验证。
#include "main.h"
#define SEND_BUF_SIZE 200
u8 SendBuff[SEND_BUF_SIZE];
u8 SendBuff2[SEND_BUF_SIZE];
u8 FLAG;
int main(void)
{
u16 i;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
Initial_UART7();
Initial_UART5();
DMA1_UART5_Config(DMA_Channel_4,(u32)&UART5->DR,(u32)SendBuff2,SEND_BUF_SIZE);
UART5_DMA_NVIC_Config();
SendBuff[0]=0x01;
for(i=1;i<SEND_BUF_SIZE;i++)
{
SendBuff=0xaa;
}
for(i=0;i<SEND_BUF_SIZE;i++)
{
SendBuff2=i;
}
FLAG=1;
while(1)
{
if(FLAG==1)
{
FLAG=0;
DMA1_UART5_Enable((u32)SendBuff2,100);
}
else if(FLAG==2)
{
FLAG=0;
DMA1_Enable((u32)SendBuff,SEND_BUF_SIZE);
}
}
}
void DMA1_Stream1_IRQHandler(void)//uart7???ê
{
if(DMA_GetITStatus(DMA1_Stream1,DMA_IT_TCIF1))
{
FLAG=1;//uart5????
DMA_ClearITPendingBit(DMA1_Stream1,DMA_IT_TCIF1);
DMA1_UART5_Config(DMA_Channel_4,(u32)&UART5->DR,(u32)SendBuff1,SEND_BUF_SIZE);
UART5_DMA_NVIC_Config();
}
}
void DMA1_Stream7_IRQHandler(void)
{
if(DMA_GetITStatus(DMA1_Stream7,DMA_IT_TCIF7))
{
FLAG=2;
DMA_ClearITPendingBit(DMA1_Stream7,DMA_IT_TCIF7);
DMA1_Config(DMA_Channel_5,(u32)&UART7->DR,(u32)SendBuff,SEND_BUF_SIZE);
UART_DMA_NVIC_Config();
}
}
#include "dma.h"
#include "stm32f4xx.h"
void DMA1_Config(u32 chx,u32 par,u32 mar,u16 ndtr)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE);
DMA_DeInit(DMA1_Stream1);
while (DMA_GetCmdStatus(DMA1_Stream1) != DISABLE){}
/* ???? DMA Stream */
DMA_InitStructure.DMA_Channel = chx;
DMA_InitStructure.DMA_PeripheralBaseAddr = par;
DMA_InitStructure.DMA_Memory0BaseAddr = mar;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_BufferSize = ndtr;
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_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream1, &DMA_InitStructure);
DMA_ITConfig(DMA1_Stream1,DMA_IT_TC,ENABLE);
}
void DMA1_UART5_Config(u32 chx,u32 par,u32 mar,u16 ndtr)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE);
DMA_DeInit(DMA1_Stream7);
while (DMA_GetCmdStatus(DMA1_Stream7) != DISABLE){}
/* ???? DMA Stream */
DMA_InitStructure.DMA_Channel = chx;
DMA_InitStructure.DMA_PeripheralBaseAddr = par;
DMA_InitStructure.DMA_Memory0BaseAddr = mar;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_BufferSize = ndtr;
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_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_Low;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream7, &DMA_InitStructure);
DMA_ITConfig(DMA1_Stream7,DMA_IT_TC,ENABLE);
}
void DMA1_Enable(u32 mar,u16 ndtr)
{
DMA_Cmd(DMA1_Stream1, DISABLE); //??±?DMA????
while (DMA_GetCmdStatus(DMA1_Stream1) != DISABLE){} //?·±?DMA????±??è??
DMA_SetCurrDataCounter(DMA1_Stream1,ndtr); //??????????
DMA1_Stream1->M0AR=mar;
DMA_Cmd(DMA1_Stream1, ENABLE); //????DMA????
}
void DMA1_UART5_Enable(u32 mar,u16 ndtr)
{
DMA_Cmd(DMA1_Stream7, DISABLE); //??±?DMA????
while (DMA_GetCmdStatus(DMA1_Stream7) != DISABLE){} //?·±?DMA????±??è??
DMA_SetCurrDataCounter(DMA1_Stream7,ndtr);
DMA1_Stream7->M0AR= mar;
DMA_Cmd(DMA1_Stream7, ENABLE); //????DMA????
}
void UART_DMA_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_DMACmd(UART7, USART_DMAReq_Tx, ENABLE);
}
void UART5_DMA_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream7_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_DMACmd(UART5, USART_DMAReq_Tx, ENABLE);
} |