HumbCa
HumbCa

Reputation: 39

STM32 Usart receive interrupt works only once

I am trying to get my STM32F103 to receive 4 bytes via USART and store them in a value.

So far, if I reset the microcontroller, the first time I send the 4 bytes they're received normally and the microcontroller stores them in USART_RX_BUF, however if I send another 4 bytes, USART_RX_BUF stays unchanged, furthermore, the contents of USART_RX_BUF don't seem to be copied into dato as I'd like

The code for the USART interrupt routine is:

void USART1_IRQHandler(void
{
    u8 Res;
#if SYSTEM_SUPPORT_OS       
    OSIntEnter();    
#endif
    if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)  
    {
        Res =USART_ReceiveData(USART1);

        if((USART_RX_STA&0x8000)==0)
        {
            if(USART_RX_STA&0x4000)
            {
                if(Res!=0x0a)USART_RX_STA=0;
                else{
                    USART_RX_STA|=0x8000;   
                    dato[0]= USART_RX_BUF[0];
                    dato[1]= USART_RX_BUF[1];
                    dato[2]= USART_RX_BUF[2];
                    dato[3]= USART_RX_BUF[3];
                    //USART_RX_STA= 0;
                    //memset(USART_RX_BUF,0,5);
                } 


            }
            else //No se recibio 0x0d todavia
            {   
                if(Res==0x0d)USART_RX_STA|=0x4000;
                else
                {
                    USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
                    USART_RX_STA++;
                    if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;

                }        
            }
        }            
    } 

As described, this code receives only 4 bytes and ignores anything I send after those 4 bytes (The received data buffer can't be overwritten) and for the data from the buffer to be written into the array called dato.

Thanks for helping

Upvotes: 2

Views: 4258

Answers (2)

Tadeáš Pilař
Tadeáš Pilař

Reputation: 21

I encountered this issue when porting my code from STM32G0 to STM32F3. Apparently, it has been fixed in G0 HAL libraries, but not in F3. After googling around for an hour, I did not find any easy solution, so I had to came up with my own. Although this is more of a workaround, then solution. I just call HAL_UART_Abort_IT(&huart) and HAL_UART_Recieve_IT(&huart) after processing the received data. It is probably not the cleanest way, but it is the simplest.

Upvotes: 1

Jonas Kibildis
Jonas Kibildis

Reputation: 181

most likely, you just need to clear USART_IT_RXNE flag after you read data

USART_ClearITPendingBit(USART1, USART_IT_RXNE);

Upvotes: 2

Related Questions