cancel
Showing results for 
Search instead for 
Did you mean: 

UART issue for reading 2 or more bytes length

GGhed.1
Associate II

I am having an issue with the UART1 communication between two devices (Computer and a STM8S208 board).

I implemented software in the computer that writes two bytes length of data to the STM8

• the first byte that acts as an identifier;

•the second byte was the data that I wanted to write at VLS memory,

However, at the STM8, only the first byte was read and twice, For instance, I sent the identifier 0x04 and the data 0x90, but only the byte 0x04 was read and stored in the buffer as: (0x04 , 0x04 ).

Now I am implementing it reading 1-byte lenght each time via a UART interruption, but I am afraid that it could let the firmware more prone to bugs, in case of the data, have the same value of the identifier byte - Some of my data are 2 bytes long + 1 byte for Identifier.

I believe that the best solution is to send it as one byte for identifier plus two bytes for data, altogether.

But how could I implement it or why only the first byte is being read repeatly?

Below are the functions for Receiving and Writing data in the STM8:

void UART1_ReceiveBytes(uint8_t * buf, uint8_t numberOfBytes)
{
    while (numberOfBytes > 0){
        *(buf++) = UART1_ReceiveData8();
        numberOfBytes--;
    }
}
 
 
void UART1_SendBytes(uint8_t *data, unsigned int numberOfBytes)
{
    while (numberOfBytes > 0) {
        UART1_SendData8(*data++);
        numberOfBytes--;
        while(UART1_GetFlagStatus(UART1_FLAG_TXE) == RESET);
    }
}

UART Setup:

void UART1_setup(void)
{
    UART1_DeInit();
    UART1_Init( 9600, 
                UART1_WORDLENGTH_8D,
                UART1_STOPBITS_1,
                UART1_PARITY_NO,
                UART1_SYNCMODE_CLOCK_DISABLE,
                UART1_MODE_TXRX_ENABLE);
    
    UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE);
    enableInterrupts();
    
    UART1_Cmd(ENABLE);
}

0693W000004GyvoQAC.png

3 REPLIES 3
Cristian Gyorgy
Senior III

C compilers are great! But, you would best know what they do, and the functions you use!

"UART1_ReceiveData8();" - is this a library function? What does it do? Just read the UART rx input register, or it waits for a byte to actually be available in the rx register and than it reads it?

GGhed.1
Associate II

Yes, it is from the STM8 UART1 library and this function just read and returns the most recent received byte by the UART peripheral

Cristian Gyorgy
Senior III

"this function just read and returns the most recent received byte" - right, so if you have 0x04 in the data input register, you can read the register 100 times and get 100 times the 0x04 value!

I suggest you check if there is new data available in the rx register and than read it. Or better yet: use interrupts!