0

I am having an issue with the UART1 communication between two devices (Computer and a STM8S2 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 0xb7 and the data 0x90, but only the byte 0xb7 was stored in the buffer as (0xb7, 0xb7).

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.

I believe that the best solution is to send it as two bytes of data. 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);
}

Sample of data that is being sent to the STM8

enter image description here

I am using ST Visual Develop with Cosmic compiler.

Thanks in advance

Ghedim
  • 1
  • 3

1 Answers1

0

the function UART1_ReceiveBytes is wrong. it doesn't wait for a new character to arrive. it should be something like this :

void UART1_ReceiveBytes(uint8_t * buf, uint8_t numberOfBytes)
    {
        while (numberOfBytes > 0)
        {
            while (!(UART1->SR & (UART1_SR_RXNE)))
            {
                //timeout considerations, you can just leave it empty
            }
            *(buf++) = UART1_ReceiveData8();
            numberOfBytes--;
        }
    }
  • It's getting stuck here - while (!(UART1->SR & (UART1_SR_RXNE)))- after the second loop. I was debugging line per line but I could not figure out the reason – Ghedim Sep 24 '20 at 14:25
  • ah yes because you only send 2 bytes, it waits for the third one. I'll correct it soon – Tirdad Sadri Nejad Sep 24 '20 at 16:20
  • @GustavodaSilvaGhedim but anyway, if you set numberOfBytes=2, why the outer while should get to third loop ? are you sure about setting numberOfBytes correctly ? – Tirdad Sadri Nejad Sep 24 '20 at 16:27
  • Do you mean about < while (numberOfBytes > 0) > but it get in the loop only twice, the third time the condition is not satisfied anymore – Ghedim Sep 25 '20 at 11:45