中级会员
 
- 积分
- 264
- 金钱
- 264
- 注册时间
- 2012-10-26
- 在线时间
- 27 小时
|
7金钱
UART4和5使用DMA然后空闲中断里面接收,接收不到数据,中断可以产生,求解
[mw_shl_code=applescript,true]#include "UART5.h"
#include "misc.h"
#include "stdio.h"
#include "string.h"
#include "includes.h"
uint8_t Uart5_Rx1[UART5_RX1_LEN];//UART1接收数组
//uint8_t Uart4_Rx2[UART4_RX2_LEN];//UART1接收数组
void UART5_Config(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOD,ENABLE); //开GPIOC时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE);//开UART4时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //GPIOC11与GPIOC10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO速度50M
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推免复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOC,&GPIO_InitStructure); //初始化IO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 ;
GPIO_Init(GPIOD,&GPIO_InitStructure);
//串口4对应引脚复用映射
GPIO_PinAFConfig(GPIOC,GPIO_PinSource12,GPIO_AF_UART5); //GPIOC12¸复用为USART5
GPIO_PinAFConfig(GPIOD,GPIO_PinSource2 ,GPIO_AF_UART5); //GPIOD2
//USART4 初始化
USART_InitStructure.USART_BaudRate = bound;//波特率设置
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//8bit
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(UART5, &USART_InitStructure); //初始化串口4
USART_DMACmd(UART5, USART_DMAReq_Rx, ENABLE);
//中断设置
USART_ITConfig(UART5, USART_IT_IDLE, ENABLE);//空闲中断
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;//串口1中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;//抢占优先级2
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0; //子优先级0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //初始化
//DMA使能
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); //打开DMA2的时钟
DMA_DeInit(DMA1_Stream0); //DeInit
DMA_InitStructure.DMA_Channel = DMA_Channel_4; //通道选择
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&(UART5->DR));//源地址
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)(Uart5_Rx1); //目的地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; //外设到内存
DMA_InitStructure.DMA_BufferSize = UART5_RX1_LEN; //数据传输量
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设非增量模式
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存增量模式
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;//数据长度8bit
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //存储器数据长度8bit
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //普通模式
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; // 优先级高
//DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable;
//DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;//存储器突发单次传输
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;//外设突发单次传输
DMA_Init(DMA1_Stream0, &DMA_InitStructure);//初始化
DMA_Cmd(DMA1_Stream0,ENABLE); //使能
USART_Cmd(UART5, ENABLE); //开启串口4
}
void UART5_IRQHandler(void)
{
uint32_t Length = 0;
u16 i;
if(USART_GetITStatus(UART5, USART_IT_IDLE) != RESET)
{
DMA_Cmd(DMA1_Stream0,DISABLE);
Length = UART5->SR;
Length = UART5->DR; //清除标志位
Length = UART5_RX1_LEN - DMA_GetCurrDataCounter(DMA1_Stream0);
printf("\r\n%d\r\n",Length);
for(i=0;i<Length;i++)
{
//Putc_UART4(Uart4_Rx1);
printf("%c ",Uart5_Rx1);
}
DMA_SetCurrDataCounter(DMA1_Stream0,UART5_RX1_LEN);//重装填,并让接收地址从0开始
DMA_Cmd(DMA1_Stream0, ENABLE);//处理完,重开DMA
}
__nop();
}
void Putc_UART5(u8 ch)
{
while((UART5->SR&0X40)==0);
USART_SendData(UART5, ch);
}
[/mw_shl_code]
|
|