2025-02-07 11:08 AM
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))
{
}
}
Solved! Go to Solution.
2025-02-08 11:21 PM
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.
2025-02-07 11:44 AM
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:
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.
2025-02-07 01:56 PM
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?
2025-02-08 01:59 AM
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?
2025-02-08 02:26 AM - edited 2025-02-08 02:30 AM
@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:
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.
2025-02-08 06:16 AM
Hello @Imen.D I have checked in HAL receive and transmit UART works fine.
2025-02-08 07:16 AM
@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!
2025-02-08 07:37 AM
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.
2025-02-08 07:44 AM
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.
2025-02-08 07:50 AM
@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.