cancel
Showing results for 
Search instead for 
Did you mean: 

USART with DMA in receiving mode

parisa
Senior
Posted on September 08, 2016 at 11:49

Hello

Here is my code as shown below

#include <
stm32f10x_gpio.h
>
#include <
stm32f10x_rcc.h
>
#include <
stm32f10x_usart.h
>
#include <
stdio.h
>
#include <
stm32f10x_dma.h
>
struct __FILE { int handle;} ;
FILE __stdout;
FILE __stdin;
FILE __stderr;
int buffer[10];
DMA_InitTypeDef DMAStruc;
int fputc(int ch, FILE *f) 
{
while(!USART_GetFlagStatus(USART1,USART_FLAG_TXE)); 
USART_SendData(USART1,ch); 
return ch; 
}
void delay()
{
int i,j;
for(i=0;i<
10000
;i++)
for(
j
=
0
;j<250;j++);
}
void config(void){
GPIO_InitTypeDef GPIOStruc;
USART_InitTypeDef USAR;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOB |RCC_APB2Periph_AFIO,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_USART1,ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
//PD5
GPIOStruc.GPIO_Mode
=
GPIO_Mode_AF_PP
;
GPIOStruc.GPIO_Speed
=
GPIO_Speed_50MHz
;
GPIOStruc.GPIO_Pin
=
GPIO_Pin_6
;
GPIO_Init(GPIOB,&GPIOStruc);
//PD6
GPIOStruc.GPIO_Mode
=
GPIO_Mode_IN_FLOATING
;
//
GPIOStruc.GPIO_Speed
=
GPIO_Speed_50MHz
;
GPIOStruc.GPIO_Pin
=
GPIO_Pin_7
;
GPIO_Init(GPIOB,&GPIOStruc);
USAR.USART_BaudRate
=
9600
;
USAR.USART_StopBits
=
USART_StopBits_1
;
USAR.USART_WordLength
=
USART_WordLength_8b
;
//
USAR.USART_Parity
=
USART_Parity_No
;
USAR.USART_Parity
=
USART_Parity_Even
;
USAR.USART_HardwareFlowControl
=
USART_HardwareFlowControl_None
;
USAR.USART_Mode
=
USART_Mode_Rx
| USART_Mode_Tx;
USART_Init(USART1,&USAR);
USART_Cmd(USART1,ENABLE);
GPIOC->CRL=(2<< 
8
);
GPIOC->BSRR=(1<<
2
);
}
void DMA1_Channel4_IRQnHandler(void)
{
/* Test on DMA Stream Transfer Complete interrupt */
// if (DMA_GetITStatus(DMA1_IT_TE4))
//{
/* Clear DMA Stream Transfer Complete interrupt pending bit */
DMA_ClearITPendingBit(DMA1_IT_TE4); 
USART_SendData(USART1, 'C');
// }
}
/**************************************************************************************/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel
= 
DMA1_Channel4_IRQn
;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority
= 
0
;
NVIC_InitStructure.NVIC_IRQChannelSubPriority
= 
0
;
NVIC_InitStructure.NVIC_IRQChannelCmd
= 
ENABLE
;
NVIC_Init(&NVIC_InitStructure);
}
int main(){
config();
NVIC_Configuration();
DMA_DeInit(DMA1_Channel4);
DMAStruc.DMA_PeripheralBaseAddr=(uint32_t) &USART1->DR;
DMAStruc.DMA_BufferSize=10;
DMAStruc.DMA_MemoryBaseAddr=(uint32_t) buffer; 
DMAStruc.DMA_DIR=DMA_DIR_PeripheralSRC;
DMAStruc.DMA_M2M = DMA_M2M_Disable;
DMAStruc.DMA_Priority=DMA_Priority_High;
DMAStruc.DMA_Mode=DMA_Mode_Circular;
DMAStruc.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMAStruc.DMA_PeripheralInc=DMA_PeripheralInc_Disable;
DMA_Init(DMA1_Channel4,&DMAStruc);
USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);
DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE);
DMA_Cmd(DMA1_Channel4, ENABLE);
while(1)
{
// GPIOC->BSRR=(1<<2);
//printf(''Hello the world\n\r'');
//USART_SendData(USART1, 'T');
delay();
// 
//delay();
}
}

I configure USART1 and enable DMA interrupts but it doesn't do any reaction to receiving data. what is my mistake?
5 REPLIES 5
jpeacock23
Associate II
Posted on September 08, 2016 at 15:08

Some basic debugging:

Does it work if you don't use DMA?  How do you know your receiver is actually working?  Do you know if data actually arrives at the USART?

Look at status registers for USART and DMA.  Do they show errors?

Embedded is like any other type of programming.   Check to make sure your assumptions are correct.  Then look for obvious errors.  Never ignore the status registers; they are there for a reason.

  Jack Peacock

Walid FTITI_O
Senior II
Posted on September 08, 2016 at 15:58

Hi mohamadi.parisa,

You are only configuring the USAR1 Tx DMA channel (DMA1_Channel4). You should configure the RX channel for receiving data (DMA1_Channel5).

As a help, See code in this user [DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/UART%20with%20DMA%20mode&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=21897]thread

 

-Hannibal-

parisa
Senior
Posted on September 08, 2016 at 20:44

0690X00000605RpQAI.png

Thanks dear peacock.jack

I correct my DMA channel to right states.After I send 10(size of buffer) chars it hangs in above line and do nothing.

before sending the 10 chars it shows ''Hello the(0) world'' correctly

Posted on September 09, 2016 at 09:28

DMA1_Channel5_IRQHandler() 
 i.e. remove ''n''
 JW

parisa
Senior
Posted on September 09, 2016 at 19:36

Thank you waclawek.jan

It works fine.

How can change the address pointer to the first address of input buffer variable?before it causes an interrupt or getting full