AnsweredAssumed Answered

Uart1 sometimes receive character gets missed STM32F205

Question asked by A2 on Sep 24, 2013
Latest reply on Sep 24, 2013 by Clive One
I have to tx byte continuously in main loop & if any character is received it will be received in a cyclcib buffer in uart interrupt. From other side a send a stream of data, problem is sometimes a missed bytes in b/w. I don't get what the problem is, I checked on CRO bytes comes in a stream. I am attaching my code if someone can check it.


void USART1_IRQHandler(void)
{
    uint32_t sr_value;
    uint32_t dummy;
    
/* read the status register */    
    sr_value = USART1->SR & 0x000003FF;      


/* check if overrun error */
    if( sr_value & 0x08 )
    {
        if( sr_value & 0x20 )   /* if rxne is set then it indicates that a valid data is stored in rx register but new data is read which is discarded */
        {
            if( uart_rx_cnt < c_uart_rx_buf_size )         /* only store if in valid range of arr size */
            {    
                uart_rx_buffer[uart_rx_cnt++] = (uint8_t)READ_UART_DATA_REG();
            }
            if( uart_rx_cnt >= c_uart_rx_buf_size )     /* if boundary of arrayt crossed then clear it so that it can go in cyclic fashion */
            {
                uart_rx_cnt = 0;
                uart_rx_buf_full = 1;
            }   
        }
        else
        {
            dummy = USART1->DR & 0x000000FF;  /* discard tha data */
        }   
    }
    
/* noise is deteted */    
    else if( sr_value & 0x04 )
    {
        dummy = USART1->DR & 0x000000FF;  /* discard tha data */
    }     
        
/* framing error or break error is detected */    
    else if( sr_value & 0x02 )
    {
        dummy = USART1->DR & 0x000000FF;  /* discard tha data */    
    }        
    
/* if receive data is ready & free from error */      
    else if( sr_value & 0x20 )   /* if rxne is set then it indicates that a valid data is stored but new data is read which is discarded */
    {
        if( uart_rx_cnt < c_uart_rx_buf_size )         /* only store if in valid range of arr size */
        {    
            uart_rx_buffer[uart_rx_cnt++] = (uint8_t)USART1->DR & 0x000000FF;
        }
        if( uart_rx_cnt >= c_uart_rx_buf_size )     /* if boundary of arrayt crossed then clear it so that it can go in cyclic fashion */
        {
            uart_rx_cnt = 0;
            uart_rx_buf_full = 1;
        }   
    }    
/* no other if-else condition */    
    else
    {
    }    
    
/* to avoid warning */    
    if(dummy)
    {
    }        
    
} /* ISR ends here */






void start_transfer(void)
{
     uint32_t cnt = 0;
     
     configure_uart();
     
     while(1)
     {
          enable_uart_and_interrupt();
     
          for( cnt = 0 ; cnt < 65535 ; cnt++ )
          {
               while( !(USART1->SR & 0x40)  );   /* while previous tx not complete */
               USART1->DR = cnt & oxff; 
          }
          
          disable_uart_and_interrupt();
          
          delay_1_sec();
     
     }


}








void enable_uart_and_interrupt(void) 
{
    uint32_t dummy;
    
/* clear cnt */    
    uart_rx_cnt = 0;    
    
/* enable uart */    
    USART1->CR1 |= USART_CR1_UE; 


/* check clean TX */
    while(!(USART1->SR & 0x40));


/* reading buffer until clear */ 
    while( (USART1->SR & 0x20)  )
    {
        dummy = (USART1->DR & 0x000000FF);
    }       
        
/* enable interrupts */    
    USART1->CR1 |= GPIO_Pin_5; ;    
    
/* to avoid warnibg */    
    if(dummy)
    {
    }    
    
} /* function ends here */  






void disable_uart_and_interrupt(void) 
{
/* check clean TX */
    while(!(USART1->SR & 0x40));    


/* disable interrupts */    
    USART1->CR1 &= (~GPIO_Pin_5); 
    
/* disable uart */    
    USART1->CR1 &= (~USART_CR1_UE);;    
    
} /* function ends here */   








void configure_uart(void) 
{
    GPIO_InitTypeDef  GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef  NVIC_InitStructure;


/** USART1 GPIO Configuration     
           PB6      ------> USART1_TX
           PB7      ------> USART1_RX
**/      
    
/* Enable the USARTx Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel                   = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    
/* Enable clock to usart 1 */    
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
    
/* configure uart tx */
    /* Enable clock */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);     
    /* configure the GPIO */
    GPIO_InitStructure.GPIO_Pin     = GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Mode    = GPIO_Mode_AF;    
    GPIO_InitStructure.GPIO_OType   = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd    = GPIO_PuPd_UP;
    GPIO_InitStructure.GPIO_Speed   = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
    /* configure alternate Tx function */
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);


/* configure uart rx */
    /* Enable clock */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);     
    /* configure the GPIO */
    GPIO_InitStructure.GPIO_Pin     = GPIO_Pin_7;
    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_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
    /* configure alternate Tx function */
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);


/* configure usart */
    /*
        - BaudRate = 115200 baud  
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
    */
    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(USART1 , &USART_InitStructure);


/* diable uart for now */
    USART1->CR1 &= (uint16_t)~((uint16_t)USART_CR1_UE);


} /* function ends here */



















Outcomes