cancel
Showing results for 
Search instead for 
Did you mean: 

Issue with UART on STM32F303RE – Incorrect HEX Data Reception (F8F8 instead of Expected Characters)

FilipF303RE
Associate III

Hi everyone,

I'm working on UART communication with an STM32F303RE, and I'm experiencing an issue with data reception. I'm sending the character 'A', but I'm receiving incorrect values (F8F8 instead of 0x41).I tried changing the APB1 prescaler, but the received data remains incorrect. Its sets exactly, how the IOC in IDE cube shows the prescaler with frequency. I dont have osciloscope.

 

#include "stm32f303xe.h"
#include "stdint.h"
#include "stm32f3xx.h"
#include <stdio.h>
#define UART2_TIMER 500
#define LD4_ON GPIOA -> BSRR |= GPIO_BSRR_BS_5;
#define LD4_OF GPIOA -> BSRR |= GPIO_BSRR_BR_5;
volatile uint32_t Tick=0;

#include "stm32f303xe.h"
#include "stdint.h"
#include "stm32f3xx.h"
#include <stdio.h>
#define UART2_TIMER 500
volatile uint32_t Tick=0;




void config_UART(void)
{
	RCC -> AHBENR |= RCC_AHBENR_GPIOAEN;
	RCC -> CFGR |= RCC_CFGR_PPRE1_2;

	//PA2_TXAF7
	GPIOA -> MODER &= ~(GPIO_MODER_MODER2_0);
    GPIOA->MODER |= GPIO_MODER_MODER2_1;
    GPIOA->AFR[0] &= ~((0xF << GPIO_AFRL_AFRL2_Pos));
	GPIOA->AFR[0] |= (0x7 << GPIO_AFRL_AFRL2_Pos);

	//PA3_RXAF7
	GPIOA -> MODER &= ~(GPIO_MODER_MODER3_0);
	GPIOA->MODER |= GPIO_MODER_MODER3_1;
	GPIOA->AFR[0] &= ~((0xF << GPIO_AFRL_AFRL3_Pos));
	GPIOA->AFR[0] |= (0x7 << GPIO_AFRL_AFRL3_Pos);




	//GPIOA -> OTYPER |= GPIO_OTYPER_OT_3;
	//GPIOA->PUPDR |= GPIO_PUPDR_PUPDR3_0;
	RCC -> APB1ENR |= RCC_APB1ENR_USART2EN;

	//RCC -> CFGR |= RCC_CFGR_PPRE1_2;

	USART2->BRR = 36000000/115200;

	USART2-> CR1 |= USART_CR1_UE | USART_CR1_TE | USART_CR1_RE;

}
void UART2_Config(void);
uint32_t GetSystemTick(void);
uint32_t Timer_UART2;
void SysTick_Handler(void);
int main(void)
{
	SysTick_Config((72000000/ 1000));
	config_UART();

	//UART2 transmit

	//Timer_UART2 = GetSystemTick();
	//1s = 72 000 000
	// 0,001 = x
    /* Loop forever */


	while (1)
{


		if ((GetSystemTick() - Timer_UART2) > 500)
		{
						USART2->TDR = 'A';
						while (!(USART2->ISR & USART_ISR_TXE)) { } // Czekaj na opróżnienie bufora
							while (!(USART2->ISR & USART_ISR_TC)) { }  // Czekaj na zakończenie transmisji
						//	Timer_UART2 = GetSystemTick();
				                // Czekanie
		}


}
uint32_t GetSystemTick()
{
	return Tick;
}
void SysTick_Handler(void)
{
	Tick++;
}
void Delay(uint32_t Delay_ms)
{
	uint32_t StartTime = Tick;
	while(Tick < (StartTime + Delay_ms))
	{

	}
}

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

To repeat: Your MCU is running at 8 MHz and your code assumes it is running at 72 MHz. For a start, change the frequency definition in your code to 8 MHz. After you get it running, setup the clock for 72 MHz and change the code accordingly.

There are few other problems with your code but this is the main one.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice

View solution in original post

17 REPLIES 17
Imen.D
ST Employee

Hello @FilipF303RE ,

This could be related to a known limitation where data corruption can occur due to a noisy receive line.

This limitation is described as follows:

ImenD_0-1738957189591.png

As mentioned in the limitation description, the issue happens when the glitch to zero has a duration less than a half bit duration and arrives during the second half of the stop bit.

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen

Where's the code bringing up the HSE and PLL? Is this based on the SPL code? Is the configuration in SystemInit() ?

 

>>I don't have oscilloscope.

If you have a terminal application, and USB-to-CMOS Serial type adapter, you could check with the F303 starting off it's 8 MHz HSI, rather than the PLL

Is this a NUCLEO board?

Perhaps output 'U' characters continuously, and check different baud rates?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
gbm
Principal

Where is the clock setting part of your code. The symptoms suggest incorrect baud rate, resulting from the clock frequency different from the assumed one. You assume 72 MHz. The default frequency after reset is 8 MHz. Have you set the clock to 72 MHz?

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice

@FilipF303RE wrote:

I'm sending the character 'A', but I'm receiving incorrect values (F8F8 instead of 0x41).


So you're receiving junk characters.

As @gbm suggested, and @Tesla DeLorean hinted, receiving junk characters is generally an indication of wrong baud rate:

https://learn.sparkfun.com/tutorials/serial-communication/all#:~:text=If%20all%20the%20receiving%20device%20sees%20on%20its%20receive%20line%20is%20garbage%2C%20check%20to%20make%20sure%20the%20baud%20rates%20match%20up.

 

PS:


@FilipF303RE wrote:

I'm sending the character 'A',


How, and where from?

What is your target hardware, and how is it connected to the source of the serial data?

Please see: How to write your question to maximize your chances to find a solution.

Hello @Imen.D  I have checked in HAL receive and transmit UART works fine.


@FilipF303RE wrote:

 I have checked in HAL receive and transmit UART works fine.


What do you mean by that?

What, exactly, did you check? How, exactly, did you check it?

Clearly something  doesn't "work fine" if you're receiving corrupted data!

Hello @Tesla DeLorean,  The project is built from scratch—the only thing implemented is the CMSIS register libraries, so apart from that, I have nothing else. According to the reference manual, the default clock frequency for usart is from APB1. Would it be possible to check the baud rate via SWO? i left u here photos thats showsh from which bus frequency is coming from.

FilipF303RE_0-1739028690809.png

FilipF303RE_1-1739028977245.png

FilipF303RE_2-1739029036791.png

 

 

Hello @gbm Yes i have tried fo72Mgz it shows  few zeros in hex. What u mean by clock setting part of code. The project is built from scratch—the only thing implemented is the CMSIS register libraries, so apart from that, I have nothing else. According to the reference manual, the default clock frequency for usart is from APB1. Meaby there is some registers that should be set and i dont knowledge about it. 

FilipF303RE_3-1739029478747.png

 

FilipF303RE
Associate III

@Andrew Neil  Hi, my nucleo for now is just connected to the my PC with usb wire directly. In the future there will be probably used the external USART. I need to know how to run it and use interupts, becasue in the future i will need to implement bitmap by usart to control motor. I have allready checked working transmit and receive in HAL and it worked fine.