AnsweredAssumed Answered

USART with DMA in receiving mode

Question asked by mohamadi.parisa on Sep 8, 2016
Latest reply on Sep 9, 2016 by mohamadi.parisa
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?

Outcomes