cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f446 Enabling USART send unwanted byte

Paul Florence
Associate II
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.
14 REPLIES 14
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

Pavel A.
Evangelist III
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

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.

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...

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 ...

Posted on July 11, 2018 at 13:44

Here is my init routine in pseudo code.

I am using USART3 clocked through APB1.

  1. APB1, ENR register enable usart3en bit to clock the peripheral
  2. APB1, ENR register set usart3rst bit
  3. APB1, ENR register clear usart3rst bit
  4. USART3, CR3 register, clear rtse and ctse bit to disable hardware transmission
  5. Compute the baud rate mantissa and fraction and write it to the BRR register of USART3
  6. USART3, CR2 register set the stop bits to 00 (for 1 stop bit)
  7. USART3, CR1 register set the te bit, the re bit and the ue bit to enable the peripheral

I don't use any interrupt for the moment.

Posted on July 11, 2018 at 13:58

Just as an example, my initialization of UART3 on a F407 looks like this (SPL-based):

void  initUART3 (void)

{

    GPIO_InitTypeDef        GPIO_InitStructure;

    USART_InitTypeDef       USART_InitStructure;

    USART_ClockInitTypeDef  USART_ClockInitStructure;

    NVIC_InitTypeDef        NVIC_InitStructure;

    RCC_APB1PeriphClockCmd (RCC_APB1Periph_USART3, ENABLE);     // USART clock

    RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOD, ENABLE);      // GPIO clock

    GPIO_PinAFConfig (GPIOD, GPIO_PinSource8, GPIO_AF_USART3);  // configure AF

    GPIO_PinAFConfig (GPIOD, GPIO_PinSource9, GPIO_AF_USART3);

    GPIO_InitStructure.GPIO_OType =  ...   <et cetera >

    GPIO_Init(GPIOD, &GPIO_InitStructure);

    USART_InitStructure.USART_BaudRate =   ...   <et cetera >

    USART_Init (USART3, &USART_InitStructure);

    USART_ClockInitStructure.USART_Clock  =   ...   <et cetera >

    USART_ClockInit (USART3, &USART_ClockInitStructure);

    USART_Cmd (USART3, ENABLE);

    NVIC_InitStructure.NVIC_IRQChannel   =   ...   <et cetera >

    NVIC_Init (&NVIC_InitStructure);

    /* Interrupt at reception and receive errors */

    USART_ITConfig (USART3, USART_IT_RXNE, ENABLE);

    USART_ITConfig (USART3, USART_IT_ERR, ENABLE);

}

Actual init values left out, they are of no interest here, supposedly.

Posted on July 11, 2018 at 14:02

Show code.

JW

Posted on July 11, 2018 at 14:02

I don't do any kind of GPIO initialization except for enabling them and setting up the alternate function.

Is there anything else that I should do ?