AnsweredAssumed Answered

USART Recieve Problem

Question asked by Pou.M.Javad on Mar 6, 2016
Latest reply on Mar 8, 2016 by Clive One

Hello Everyone.

Transferring data via USART2; I have some problem. Hope to see a recommendation! 
I am doing as this : Sending data to PC on TX line normally without any Interrupt and receiving data from PC on RX line with use of Interrupt Handler.
My code is such below.
The problem occur-es  when I am starting the data transfer between stm32f407 and PC; the program stops at the USART interrupt handler function and then hangs.
It seems that something is happened after the program detect RXNE flag and tries to read USART RX buffer, but it can not read the receiving buffer completely.
so stops at the middle of  decoding code.
Any body have any Idea about?
Thanks.



volatile char c;
volatile char RF_Buffer_IN[50];

int main(void) {
     
    /* Initialize system */
    SystemInit();
   
    /* Initialize USART */
      MY_USART_initilize(19200)

while (1) {
     
       /* Transferring Some data via TX Line */
     USART_SendData(USART2, c);

    }
}
/*---------------------------------------------------------------------------------------------*/
void MY_USART_initilize(float Buadrate)
{
     
    GPIO_InitTypeDef     GPIO_InitStructure;
    USART_InitTypeDef    USART_InitStructure;
  NVIC_InitTypeDef     NVIC_InitStruct;
     
    /*-----------------------------USART2 For Sending data to PC----------------------------*/
    // clocks
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
   
    RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA , ENABLE);
         
    /* Configure USART2 Tx/RX (PA2/PA3) as alternate function push-pull */
     
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
   
    // Map USART2 to A2,A3
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
     
    // Initialize USART2
    USART_InitStructure.USART_BaudRate = Buadrate;
    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_Tx | USART_Mode_Rx;
    /* Configure USART */
    USART_Init(USART2, &USART_InitStructure);
    /* Enable the USART */
    USART_Cmd(USART2, ENABLE);
   
    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
   
    NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
    NVIC_Init(&NVIC_InitStruct);
}
 
 
/* USART IRQ handler */
void USART2_IRQHandler(void) {
   
  static int rx_index = 0;
 
  if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) // Received characters modify string
  {
    GPIO_SetBits(GPIOD, GPIO_Pin_15);


    RF_Buffer_IN[rx_index++] = USART_ReceiveData(USART2);
  
    if (rx_index >= (sizeof(RF_Buffer_IN) - 1))
        {
      rx_index = 0;
        }
    
     /******* Decoding is done here ********/
}


Outcomes