Home > other >  The DMA receiving STM32F103 usart1 and ADC1
The DMA receiving STM32F103 usart1 and ADC1

Time:05-01

This two days in the debugger, to realize usart1 serial port receiving and ADC1 data reception function, using the DMA, now is I separate debugging usart1 serial port receiving and ADC, can work together can't normal work, I set up a think, continue to receive and print ADC data, then, I send a serial port data, program should hang up, unable to receive serial data, also can't receive the ADC data, so want to please the great god, under the guidance of exactly what went wrong, is pin reuse? Or configuration problem?
Here is my code:
Uint8_t ReceiveBuff [RECEIVEBUFF_SIZE]={0};

/* *
* @ brief USART GPIO configuration, the working parameters configuration
No
* @ paramNo
* @ retval*/
Void USART_Config (void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;

//open the serial port of GPIO clock
DEBUG_USART_GPIO_APBxClkCmd (DEBUG_USART_GPIO_CLK, ENABLE);

//open the serial peripheral clock
DEBUG_USART_APBxClkCmd (DEBUG_USART_CLK, ENABLE);

//the USART Tx GPIO configuration for push-pull multiplexing mode
GPIO_InitStructure. GPIO_Pin=DEBUG_USART_TX_GPIO_PIN;
GPIO_InitStructure. GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init (DEBUG_USART_RX_GPIO_PORT, & amp; GPIO_InitStructure);

//the USART Rx GPIO configuration for floating empty input mode
GPIO_InitStructure. GPIO_Pin=DEBUG_USART_RX_GPIO_PIN;
GPIO_InitStructure. GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init (DEBUG_USART_RX_GPIO_PORT, & amp; GPIO_InitStructure);

//configuration parameters of the serial port
//configuration baud rate
USART_InitStructure. USART_BaudRate=DEBUG_USART_BAUDRATE;
//configuration data word long needle
USART_InitStructure. USART_WordLength=USART_WordLength_8b;
//configuration stop bit
USART_InitStructure. USART_StopBits=USART_StopBits_1;
//configuration check digit
USART_InitStructure. USART_Parity=USART_Parity_No;
//configure hardware flow control
USART_InitStructure. USART_HardwareFlowControl=USART_HardwareFlowControl_None;
//configuration mode, transceiver with
USART_InitStructure. USART_Mode=USART_Mode_Rx | USART_Mode_Tx;
//complete serial port initialization configuration
USART_Init (DEBUG_USARTx, & amp; USART_InitStructure);
//open free interrupt
USART_ITConfig (DEBUG_USARTx USART_IT_IDLE, ENABLE);

/* can make USART1 DMA receive */
USART_DMACmd (DEBUG_USARTx USART_DMAReq_Rx, ENABLE);
//can make a serial port
USART_Cmd (DEBUG_USARTx, ENABLE);
}

/* *
* @ brief USARTx RX NVIC configuration, peripheral to memory (DR - & gt; USART1)
No
* @ paramNo
* @ retval*/
Void USARTx_DMA_Config (void)
{
DMA_InitTypeDef DMA_InitStructure;

//open the DMA clock
RCC_AHBPeriphClockCmd (RCC_AHBPeriph_DMA1, ENABLE);
//set the DMA source address: serial port data register address */
DMA_InitStructure. DMA_PeripheralBaseAddr=USART_DR_ADDRESS;
//memory address (to a pointer to the variable transmission)
DMA_InitStructure. DMA_MemoryBaseAddr=(u32 ReceiveBuff);
//direction: from a peripheral to the memory
DMA_InitStructure. DMA_DIR=DMA_DIR_PeripheralSRC;
//transport size
DMA_InitStructure. DMA_BufferSize=RECEIVEBUFF_SIZE;
//increasing peripheral address
DMA_InitStructure. DMA_PeripheralInc=DMA_PeripheralInc_Enable;
//memory address on the
DMA_InitStructure. DMA_MemoryInc=DMA_MemoryInc_Enable;
//peripheral data unit
DMA_InitStructure. DMA_PeripheralDataSize=DMA_PeripheralDataSize_Byte;
//memory data unit
DMA_InitStructure. DMA_MemoryDataSize=DMA_MemoryDataSize_Byte;
//DMA mode, or circulation mode
DMA_InitStructure. DMA_Mode=DMA_Mode_Normal;
//DMA_InitStructure. DMA_Mode=DMA_Mode_Circular;
//priority:
DMA_InitStructure. DMA_Priority=DMA_Priority_High;
//prohibited from memory to memory transmission
DMA_InitStructure. DMA_M2M=DMA_M2M_Disable;
//DMA channels
DMA_Init (USART_RX_DMA_CHANNEL, & amp; DMA_InitStructure);
//enable the DMA
DMA_Cmd (USART_RX_DMA_CHANNEL, ENABLE);
}


//pointer to restore the DMA
Void MYDMA_Enable (DMA_Channel_TypeDef * DMA_CHx)
{
DMA_Cmd (DMA_CHx, DISABLE);//close USART1 RX indicated channel DMA1
DMA_SetCurrDataCounter (DMA_CHx RECEIVEBUFF_SIZE);//DMA channel of the DMA buffer size
DMA_Cmd (DMA_CHx, ENABLE);//open the USART1 RX indicated channel DMA1
}

/* *
* @ brief USARTx RX NVIC configuration, peripheral to memory (DR - & gt; USART1)
No
* @ paramNo
* @ retval*/
Void USARTx_NVIC_Config (void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */

NVIC_PriorityGroupConfig (NVIC_PriorityGroup_3);

/* the Enable the USART Interrupt */

NVIC_InitStructure. NVIC_IRQChannel=USART1_IRQn;

NVIC_InitStructure. NVIC_IRQChannelPreemptionPriority=2;

NVIC_InitStructure. NVIC_IRQChannelSubPriority=1;

NVIC_InitStructure. NVIC_IRQChannelCmd=ENABLE;

NVIC_Init (& amp; NVIC_InitStructure);

}

//a serial port interrupt function
Void USART1_IRQHandler (void)//serial port 1 interrupt service routines
{

Uint8_t Usart1_Rec_Cnt;

If (USART_GetITStatus (USART1, USART_IT_IDLE)!=RESET)//receiving interrupt (the received data must be 0 x0d 0 x0a end)
{
USART_ReceiveData (USART1);//read the data note: this must be, otherwise can't interrupt flag bit,
Usart1_Rec_Cnt=RECEIVEBUFF_SIZE - DMA_GetCurrDataCounter (DMA1_Channel5);//work out after the length of the frame data

//* * * * * * * * * * * frame data processing function * * * * * * * * * * * *//
Printf (" Thelenght: % d \ r \ n ", Usart1_Rec_Cnt);
Printf (" The data: \ r \ n ");

Usart_SendArray (DEBUG_USARTx ReceiveBuff, RECEIVEBUFF_SIZE);
Memset ();

Printf (" \ r \ nOver! \r\n");
//* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
USART_ClearITPendingBit (USART1, USART_IT_IDLE);//remove interrupt flag
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related