两个串口基本设置相同 串口1使用DMA方式发送不正常,而不用DMA能正常发送,而串口2工作完全正常,不知道是什么原因。
程序如下:
#include "uart.h"
#include <stdarg.h>
#include <stdio.h>
/* Private function prototypes -----------------------------------------------*/
#define USART1_TDR_ADDRESS 0x40013828
#define USART1_RDR_ADDRESS 0x40013824
#define USART2_TDR_ADDRESS 0x40004428
#define USART2_RDR_ADDRESS 0x40004424
uint8_t Txd1_Fifo[FIFO_NUM],Rxd1_Fifo[FIFO_NUM];
uint8_t Txd2_Fifo[FIFO_NUM],Rxd2_Fifo[FIFO_NUM];
uint8_t DmaTxd1_Flag,DmaTxd2_Flag; //正在发送置1 发送完成清0 在启动发送时需要判断该值是否为0 为0才能启动发送,在发射完成中断中清0
/* Private functions ---------------------------------------------------------*/
DMA_InitTypeDef DMA_InitStructure;
void USART1_Configuration(void)//串口初始化函数
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_1);
/*
* USART1_TX -> PA9 , USART1_RX -> PA10
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;//设置串口波特率
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); //配置入结构体
//这里设置串口超时中断 由于USART2没有超时中断,为了使两个串口一致性处理,这里暂时不用
// USART_ReceiverTimeOutCmd(USART1,ENABLE);
// USART_SetReceiverTimeOut(USART1,35);
// USART_ITConfig(USART1,USART_IT_RTO,ENABLE);
//
// NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
// NVIC_InitStructure.NVIC_IRQChannelPriority = 3;
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
// NVIC_Init(&NVIC_InitStructure);
USART_Cmd(USART1, ENABLE);//使能串口1
/* DMA Configuration -------------------------------------------------------*/
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_DeInit(DMA1_Channel2);
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //发送时normal模式
DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_TDR_ADDRESS;
DMA_InitStructure.DMA_BufferSize = (uint16_t)FIFO_NUM;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Txd1_Fifo;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_Init(DMA1_Channel2, &DMA_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel2_3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_DeInit(DMA1_Channel3);
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //接收使用循环模式
DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_RDR_ADDRESS;
DMA_InitStructure.DMA_BufferSize = (uint16_t)FIFO_NUM;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Rxd1_Fifo;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_Priority = DMA_Priority_Low;
DMA_Init(DMA1_Channel2, &DMA_InitStructure);
USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);
USART_ClearFlag(USART1,USART_FLAG_RXNE);
DMA_Cmd(DMA1_Channel3,ENABLE);
}
void USART1_Txd(uint8_t num,uint8_t *txbuff)
{
DmaTxd1_Flag = 1;
DMA_ClearFlag(DMA1_FLAG_GL2);
DMA_Cmd(DMA1_Channel2,DISABLE);
USART_DMACmd(USART1,USART_DMAReq_Tx,DISABLE);
DMA1_Channel2->CNDTR = (uint16_t)num;
DMA1_Channel2->CMAR = (uint32_t)txbuff;
USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);
USART_ClearFlag(USART1,USART_FLAG_TC);
DMA_ITConfig(DMA1_Channel2,DMA_IT_TC,ENABLE);
DMA_Cmd(DMA1_Channel2,ENABLE);
}
void USART2_Configuration(void)//串口初始化函数
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE );
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_1);
/*
* USART2_TX -> PA2 , USART2_RX -> PA3
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;//设置串口波特率
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(USART2, &USART_InitStructure); //配置入结构体
USART_Cmd(USART2, ENABLE);//使能串口2
/* DMA Configuration -------------------------------------------------------*/
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_DeInit(DMA1_Channel4);
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //发送时normal模式
DMA_InitStructure.DMA_PeripheralBaseAddr = USART2_TDR_ADDRESS;
DMA_InitStructure.DMA_BufferSize = (uint16_t)FIFO_NUM;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Txd2_Fifo;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
DMA_DeInit(DMA1_Channel5);
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //接收使用循环模式
DMA_InitStructure.DMA_PeripheralBaseAddr = USART2_RDR_ADDRESS;
DMA_InitStructure.DMA_BufferSize = (uint16_t)FIFO_NUM;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)Rxd2_Fifo;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_Priority = DMA_Priority_Low;
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
USART_DMACmd(USART2,USART_DMAReq_Rx,ENABLE);
USART_ClearFlag(USART2,USART_FLAG_RXNE);
DMA_Cmd(DMA1_Channel5,ENABLE);
}
void USART2_Txd(uint8_t num,uint8_t *txbuff)
{
DmaTxd1_Flag = 1;
DMA_ClearFlag(DMA1_FLAG_GL4);
DMA_Cmd(DMA1_Channel4,DISABLE);
USART_DMACmd(USART2,USART_DMAReq_Tx,DISABLE);
DMA1_Channel4->CNDTR = (uint16_t)num;
DMA1_Channel4->CMAR = (uint32_t)txbuff;
USART_DMACmd(USART2,USART_DMAReq_Tx,ENABLE);
USART_ClearFlag(USART2,USART_FLAG_TC);
DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,ENABLE);
DMA_Cmd(DMA1_Channel4,ENABLE);
}
|
|