下面是usart2的
//-----------------DMA1配置-------------------------------------
void uart2_init(int bps)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //修改
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//修改
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2); //GPIOA2¸修改
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2); //GPIOA3¸修改
//USART2¶Ë¿ÚÅäÖÃ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; //修改
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //¸´Óù¦ÄÜ
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //ËÙ¶È50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //ÍÆÍ츴ÓÃÊä³ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //ÉÏÀ
GPIO_Init(GPIOA,&GPIO_InitStructure); //修改
//USART2³õʼ»¯ÉèÖÃ
USART_InitStructure.USART_BaudRate =bps; //²¨ÌØÂÊÉèÖÃ
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_CTS; //ÎÞÓ²¼þÊý¾ÝÁ÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //ÊÕ·¢Ä£Ê½
USART_Init(USART2, &USART_InitStructure); //³õʼ»¯´®¿Ú2
USART_Cmd(USART2, ENABLE); //修改
USART_DMACmd(USART2,USART_DMAReq_Tx,ENABLE);
USART_ClearFlag(USART2, USART_FLAG_TC);
USART_ITConfig(USART2, USART_IT_RXNE, DISABLE); //修改
//Usart2 NVIC ÅäÖÃ
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//ÉèÖÃϵͳÖжÏÓÅÏȼ¶·Ö×é0
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;//´®¿Ú1ÖжÏͨµÀ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//ÇÀÕ¼ÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //×ÓÓÅÏȼ¶3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨµÀʹÄÜ
NVIC_Init(&NVIC_InitStructure); //¸ù¾ÝÖ¸¶¨µÄ²ÎÊý³õʼ»¯VIC¼Ä´æÆ÷¡¢
}
void usart2_dma_Config(uint32_t * usart2_dma_buffer, int byteLen)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE); //修改
DMA_DeInit(DMA1_Stream6);
while (DMA_GetCmdStatus(DMA1_Stream6) != DISABLE){} //µÈ´ýDMA¿ÉÅäÖÃ
/* ÅäÖÃ DMA Stream */
DMA_InitStructure.DMA_Channel = DMA_Channel_4; //ͨµÀÑ¡Ôñ
DMA_InitStructure.DMA_PeripheralBaseAddr =((u32)&USART2->DR); //DMAÍâÉèµØÖ·
DMA_InitStructure.DMA_Memory0BaseAddr = (u32)usart2_dma_buffer; //DMA ´æ´¢Æ÷0µØÖ·
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; //´æ´¢Æ÷µ½ÍâÉèģʽ
DMA_InitStructure.DMA_BufferSize =byteLen; //Êý¾Ý´«ÊäÁ¿
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_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_Stream6, &DMA_InitStructure); //修改
DMA_Cmd(DMA1_Stream6, ENABLE);
}
void usart2_dma_ConfigHalfWord(uint16_t * usart2_dma_buffer, int byteLen)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE); //修改
DMA_DeInit(DMA1_Stream6);
while (DMA_GetCmdStatus(DMA1_Stream6) != DISABLE){} //修改
/* ÅäÖÃ DMA Stream */
DMA_InitStructure.DMA_Channel = DMA_Channel_4; //ͨµÀÑ¡Ôñ
DMA_InitStructure.DMA_PeripheralBaseAddr =((u32)&USART2->DR); //DMAÍâÉèµØÖ·
DMA_InitStructure.DMA_Memory0BaseAddr = (u32 )usart2_dma_buffer; //DMA ´æ´¢Æ÷0µØÖ·
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; //´æ´¢Æ÷µ½ÍâÉèģʽ
DMA_InitStructure.DMA_BufferSize =byteLen; //Êý¾Ý´«ÊäÁ¿
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_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_Stream6, &DMA_InitStructure); //修改
DMA_Cmd(DMA1_Stream6, ENABLE);
}
void usart2_dma_ConfigByte(uint8_t * usart2_dma_buffer, int byteLen)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE); //修改
DMA_DeInit(DMA1_Stream6);
while (DMA_GetCmdStatus(DMA1_Stream6) != DISABLE){} //修改
/* ÅäÖÃ DMA Stream */
DMA_InitStructure.DMA_Channel = DMA_Channel_4; //ͨµÀÑ¡Ôñ
DMA_InitStructure.DMA_PeripheralBaseAddr =((u32)&USART2->DR); //修改
DMA_InitStructure.DMA_Memory0BaseAddr = (u32 )usart2_dma_buffer; //修改
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; //´æ´¢Æ÷µ½ÍâÉèģʽ
DMA_InitStructure.DMA_BufferSize =byteLen; //Êý¾Ý´«ÊäÁ¿
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_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_Stream6, &DMA_InitStructure); //修改
//DMA_SetCurrDataCounter(DMA1_Stream6, byteLen);
DMA_Cmd(DMA1_Stream6, ENABLE);
|