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))
	{

	}
}

 

 

17 REPLIES 17

It's not running at 72 MHz

If you leave the RCC alone the chip will clock at 8 MHz via HSI,and busses at DIV1

SWO should work at 8 MHz

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

@Andrew Neil I have check it by using interrupt callback function, by sending these chars. The realterm showed from sprintf text and the led was showing corectly state, also on the wrong char it have showed "zlyznak".

/* USER CODE BEGIN 4 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if(huart->Instance == USART2)
	{
		if(znak =='e')
		{
			HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET);
			dl_kom = sprintf(komunikat,"DIODA ON\n");

		}
		else if(znak == 'd')
		{
			HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
			dl_kom = sprintf(komunikat,"DIODA OFF\n");

		}else
		{
			dl_kom = sprintf(komunikat,"zlyznak");

		}
		HAL_UART_Receive_IT(&huart2, &znak, 1);
		HAL_UART_Transmit_IT(&huart2, komunikat, dl_kom);
	}
}
/* USER CODE END 4 */

 

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

@gbm Ok, I didint know i have to set up everthing in clock configuration. I wrote this clock_cofing function and the char, is coming withouth any problem. I deleted lane code with sysclock config and everything is fine on 36mHz. The baude rate is okey now. Can you tell what other things, I need to know to properly configure my MCU?

void clock_config(void)
{
	RCC->CR |= RCC_CR_HSION;
	 while (!(RCC->CR & RCC_CR_HSIRDY));
	 RCC->APB1ENR |= RCC_APB1ENR_PWREN;

	 RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
	 RCC->CFGR|= RCC_CFGR_PPRE1_DIV2;
	 //PPL SOURCE MUX

	// RCC->CFGR |= RCC_CFGR_PLLMUL9;
	 RCC->CFGR |= (0x7 << RCC_CFGR_PLLMUL_Pos);
	 RCC->CR |= RCC_CR_PLLON;
	 while (!(RCC->CR & RCC_CR_PLLRDY));
	 RCC->CFGR |= RCC_CFGR_SW_PLL;
	 while(!(RCC->CFGR & RCC_CFGR_SWS));
}

 

I forgot to say thank you for your help.


@FilipF303RE wrote:

@gbm Ok, I didint know i have to set up everthing in clock configuration.


Well, anything you don't specifically set will always be left at its default!

This applies to all configurations - not just the clocks.

@Andrew Neil  Can you list me all basic config, I should have to just properly run MCU?

CubeMX will do that for you.