Skip to main content
Paul Florence
Associate
July 10, 2018
Question

stm32f446 Enabling USART send unwanted byte

  • July 10, 2018
  • 14 replies
  • 3008 views
Posted on July 10, 2018 at 22:37

Hi,

When I enable my USART peripheral it sends an unwanted byte interpreted as 0xFE.

To enable the USART peripheral, I first enable it in the APBENR register then I reset it through APBRSTR.

I then disable flow control, and program the baudrate.

I then configure the number of stop bits (0, this is quasi default communication) and then I enable USART through CR1 register. After that all what is left is to enable the TE and RE bit in CR1.

After sending this unwanted byte, it all works very well because I can send data without any problem at the correct baudrate. There is juste this annoying byte that get sent first...

Do you have any idea where I should look ?

I think it may be related to this note in the datasheet :

Note: 1: During transmission, a “0�? pulse on the TE bit (“0�? followed by “1�?) sends a preamble

(idle line) after the current word, except in smartcard mode.

2: When TE is set there is a 1 bit-time delay before the transmission starts.

Thanks a lot & have a nice day !

Note: this post was migrated and contained many threaded conversations, some content may be missing.
    This topic has been closed for replies.

    14 replies

    waclawek.jan
    Super User
    July 11, 2018
    Posted on July 11, 2018 at 10:46

    Yes, that remark is probably related. I usually set all 3 bits (TE, RE, UE) at once, by one writing into the CR1, and haven't observed any problem (but honestly, did not look very carefully so far).

    JW

    Paul Florence
    Associate
    July 11, 2018
    Posted on July 11, 2018 at 12:26

    I also set all of them at once...

    Here is a capture of what is happening : 0690X00000602NzQAI.png

    The unwanted byte is the first 0xFF isolated from the rest.

    waclawek.jan
    Super User
    July 11, 2018
    Posted on July 11, 2018 at 14:02

    Show code.

    JW

    Pavel A.
    July 11, 2018
    Posted on July 11, 2018 at 12:07

    Read the data register immediately after initializing the USART, but before enabling interrupts. This will clear any junk sitting there.

    -- pa

    Paul Florence
    Associate
    July 11, 2018
    Posted on July 11, 2018 at 12:35

    I tried multiple ways of reading to DR. Reading it with gdb should be working right ? I also tried to read it from my program but it changed nothing.

    Moreover the reference specify that the registers is initialized with 0.

    I really don't know what to do...

    AvaTar
    Senior III
    July 11, 2018
    Posted on July 11, 2018 at 13:31

    I tried multiple ways of reading to DR. Reading it with gdb should be working right ? I also tried to read it from my program but it changed nothing.

    This is expected, because a read accesses the RX register, and not TX. In other words, this shouldn't make a difference.

    Pretty sure you do some init accesses in a wrong order. I used to base my UART code on SPL examples, and never had such an issue. And thus I can't remember the detailed order ...

    turboscrew
    Senior III
    July 12, 2018
    Posted on July 12, 2018 at 06:50

    I set up the USART first and turn the TX pin as output later. The USART ticks out the byte, but it's not getting out of the chip. And that's why I also use TXEIE to start/stop transmission, not TCIE.

    Dan Mackie
    Associate III
    July 12, 2018
    Posted on July 12, 2018 at 14:31

    I agree with Juha.  For standard serial I/O, I would try only enabling the transmitter as part of the output service routine, and disable it when no data was ready to go.  When I had data ready to transmit, and in the transmit output register, then I would enable the transmitter (really enable the transmit register empty interrupt).