本帖最后由 紫宸 于 2014-7-28 13:25 编辑
我的问题,我想使用F4的DMA进行串口的DMA接收,配置过程:GPIO配置、UART配置、DMA优先级配置、DMA参数配置,就是这几个函数。
测试中,我用电脑串口发送数据给F4,并使用LED的翻转来表示进入中断。可问题出现了,只有我发送成功第一次的数据(设定的是6个字节)的时候,LED翻转,说明进入了DMA发送完成中断,但是以后不会再进入。更奇怪的是,我把存放DMA接收数据打印出来的 时候,发现里面的数据是实时更新的,也就是说DMA依然在正常工作,想不明白,那为什么不进入中断了呢?
请高手帮忙看看,谢谢!
谁有更好的串口DMA例程,能拿出来交流交流吗?
/*---------------------- COM 端口及引脚选择 --------------------------*/
/*COM*/
#define COM_USART USART2
#define COM_GPIO_AF GPIO_AF_USART2
#define COM_RCC_USART_CLK RCC_APB1Periph_USART2
#define COM_IRQ_Channel USART2_IRQn
#define COM_IRQHandler USART2_IRQHandler
/*TX*/
#define COM_TX_PIN GPIO_Pin_2
#define COM_TX_GPIO_PORT GPIOA
#define COM_TX_GPIO_CLK RCC_AHB1Periph_GPIOA
#define COM_TX_GPIO_PinSource GPIO_PinSource2
/*RX*/
#define COM_RX_PIN GPIO_Pin_3
#define COM_RX_GPIO_PORT GPIOA
#define COM_RX_GPIO_CLK RCC_AHB1Periph_GPIOA
#define COM_RX_GPIO_PinSource GPIO_PinSource3
/*--------------------------------------------------------------------*/
/**************************实现函数********************************************
*函数原型: void COM_Init(u32 baudrate)
*功 能: 初始化串口1
输入参数: u32 baudrate 串口的波特率
输出参数:没有
*******************************************************************************/
void COM_Init(u32 baudrate)
{
USART_InitTypeDef USART_InitStructure; //定义USART初始化结构体
COM_GPIO_Config(); //配置UART1的相关引脚
RCC_APB1PeriphClockCmd(COM_RCC_USART_CLK,ENABLE);//使能USART1外设时钟
/* Enable the USART OverSampling by 8 */
USART_OverSampling8Cmd(COM_USART, ENABLE);
/* USARTx configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = baudrate;
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(COM_USART , &USART_InitStructure);
COM_DMA_NVIC_Config(); //配置串口DMA中断的优先级
COM_DAM_Config(); //配置串口DMA
USART_ClearFlag(COM_USART,USART_FLAG_TC); //清除标志位,防止第一个数据发送失败
USART_Cmd(COM_USART,ENABLE); //使能串口
}
void COM_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //定义GPIO初始化结构体
RCC_AHB1PeriphClockCmd(COM_TX_GPIO_CLK|COM_RX_GPIO_CLK,ENABLE); //使能USART1对应的引脚端口TX RX
/* Configure USART Tx as alternate function */
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //使用复用功能
GPIO_InitStructure.GPIO_Pin = COM_TX_PIN; //TX pin
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //输出频率 50M
GPIO_Init(COM_TX_GPIO_PORT, &GPIO_InitStructure); //TX PIN 初始化
/* Configure USART Rx as alternate function */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //使用复用功能
GPIO_InitStructure.GPIO_Pin = COM_RX_PIN ; //RX pin
GPIO_Init(COM_RX_GPIO_PORT, &GPIO_InitStructure); //RX PIN初始化
/* Connect PXx to USARTx_Tx*/
GPIO_PinAFConfig(COM_TX_GPIO_PORT , COM_TX_GPIO_PinSource , COM_GPIO_AF);
/* Connect PXx to USARTx_Rx*/
GPIO_PinAFConfig(COM_RX_GPIO_PORT , COM_RX_GPIO_PinSource , COM_GPIO_AF);
}
/**********************************************************************************************
*函数名:void COM_DMA_NVIC_Config(void)
* 参 数
* 返回值:
* 功能:配置串口DMA的中断优先级
**********************************************************************************************/
void COM_DMA_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/**********************************************************************************************
*函数名:void COM_DAM_Config(void)
* 参 数
* 返回值:
* 功能: 配置串口的DMA
**********************************************************************************************/
void COM_DAM_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
/* Enable the DMA clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
/* Configure DMA controller to manage USART TX and RX DMA request ----------*/
DMA_DeInit(DMA1_Stream5);
/* Configure DMA Initialization Structure */
DMA_InitStructure.DMA_BufferSize = 6 ;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable ;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull ;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single ;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_PeripheralBaseAddr =(uint32_t) (&(COM_USART->DR)) ;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_Channel = DMA_Channel_4 ;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory ;
DMA_InitStructure.DMA_Memory0BaseAddr =(uint32_t)COM_Rx_Buf;
DMA_Init(DMA1_Stream5,&DMA_InitStructure);
/* Enable DMA Stream Transfer Complete interrupt */
DMA_ITConfig(DMA1_Stream5, DMA_IT_TC, ENABLE); //开启DMA数据发送完成中断
DMA_ClearITPendingBit(DMA1_Stream5, DMA_IT_TCIF5); //清标志
/* Enable USART RX DMA */
USART_DMACmd(COM_USART, USART_DMAReq_Rx, ENABLE); //开启USART接收DMA请求
DMA_Cmd(DMA1_Stream5,ENABLE);
}
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] handles DMA Stream interrupt request.
* @param None
* @retval None
*/
void DMA1_Stream5_IRQHandler(void)
{
if(DMA_GetITStatus(DMA1_Stream5, DMA_IT_TCIF5))
{
/* Clear DMA Stream Transfer Complete interrupt pending bit */
DMA_ClearITPendingBit(DMA1_Stream5, DMA_IT_TCIF5);
STM_EVAL_LEDOn(LED2);
}
}
|