u8 gprsTxBuf[100];
u8 gprsRxBuf[1500];
//接收DMA初始化
void tx_dma_init(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_Cmd(DMA1_Channel7, DISABLE);
DMA_DeInit(DMA1_Channel7);
DMA_InitStructure.DMA_BufferSize=sizeof(gprsTxBuf); //ÏÈÉèÖÃΪ1K´óС
DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_M2M=DMA_M2M_Disable;
DMA_InitStructure.DMA_MemoryBaseAddr=(uint32_t)gprsTxBuf;
DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_Mode=DMA_Mode_Normal;
DMA_InitStructure.DMA_PeripheralBaseAddr=(u32)&USART2->DR;
DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_Priority=DMA_Priority_High;
DMA_Init(DMA1_Channel7,&DMA_InitStructure); //³õʼ»¯TXµÄDMA
}
//发送DMA初始化
void rx_dma_init(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_InitStructure.DMA_PeripheralBaseAddr = USART_DR_ADDRESS;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)gprsRxBuf;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = sizeof(gprsRxBuf);
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_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel6, &DMA_InitStructure);
DMA_Cmd (DMA1_Channel6,ENABLE);
}
//串口初始化
void uart_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_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_DMACmd(USART2,USART_DMAReq_Tx,ENABLE);
USART_DMACmd(USART2,USART_DMAReq_Rx,ENABLE);
USART_ClearFlag(USART2,USART_FLAG_RXNE);
USART_ClearFlag(USART2, USART_FLAG_TC);
USART_ITConfig(USART2,USART_IT_IDLE,ENABLE);
USART_Cmd(USART2, ENABLE);
}
//DMA发送数据
void send_commend(u8 *buf,u16 size)
{
memset(gprsTxBuf,0,100);
memcpy(gprsTxBuf,buf,size);
DMA_Cmd(DMA1_Channel7,DISABLE);
DMA_SetCurrDataCounter(DMA1_Channel7,size);
DMA_Cmd(DMA1_Channel7,ENABLE);
}
void gprs_dma_reset()
{
DMA_Cmd(DMA1_Channel6, DISABLE );
DMA_SetCurrDataCounter(DMA1_Channel6, sizeof(gprsRxBuf));
DMA_Cmd(DMA1_Channel6, ENABLE);
}
extern u8 gprsRecOverFlag;
extern u16 gprsRecCnt;
void USART2_IRQHandler(void)
{
u16 uartRecCnt=0;
if(USART_GetITStatus(USART2, USART_IT_IDLE) != RESET)
{
USART_ReceiveData(USART2);
uartRecCnt = sizeof(gprsRxBuf) - DMA_GetCurrDataCounter(DMA1_Channel6);
//**********************//
gprsRecOverFlag=1;
gprsRecCnt=uartRecCnt;
//*************************************//
gprs_dma_reset();
USART_ClearITPendingBit(USART2,USART_IT_IDLE);
}
}
|