Can't make UART work in Bare Metal using STM32

61 Views Asked by At

I'm trying to transmit a message using UART in bare metal between an STM32F407 Discovery(bare metal) and a XIAO ESP32C3(ArduinoIDE) but I can't make it work, I don't receive anything.

I tried programming it between 2 STM32, It seems that I receive something in DR but it doesnt make much sense, I tried transmitting a 0 and got a 252 and when I sent a 7 I got a 128

Here is my init:

#define TX 9
#define RX 10

uart_init()
{
    RCC_AHB1ENR |= RCC_GPIOAEN;
    RCC_APB2ENR |= RCC_USART1EN;
    
    // configure usart pins
    GPIOA_MODER = REP_BITS(GPIOA_MODER, TX*2, 2, GPIO_MODER_ALT);
    GPIOA_AFRH = REP_BITS(GPIOA_AFRH, 1*4, 4, 7);
    GPIOA_OSPEEDR = REP_BITS(GPIOA_OSPEEDR, TX*2, 2, GPIO_OSPEEDR_HI);


    GPIOA_MODER = REP_BITS(GPIOA_MODER, RX*2, 2, GPIO_MODER_ALT);
    GPIOA_AFRH = REP_BITS(GPIOA_AFRH, 2*4, 4, 7);
    GPIOA_OSPEEDR = REP_BITS(GPIOA_OSPEEDR, RX*2, 2, GPIO_OSPEEDR_HI);

    
    USART1_CR1 = 0;
    USART1_CR1 |= USART_CR1_TE | USART_CR1_RE;

    //9600 baud
    USART1_BRR |= (273<<4);
    USART1_BRR |= 7; 

    //115200 baud
    // USART1_BRR |= (22<<4);
    // USART1_BRR |= 13; 

    USART1_CR1 |= USART_CR1_UE;
}

My write function:

void write(){
    while (!(USART1_SR & USART_SR_TXE)) {};
    USART1_DR = 1; 
    while((USART1_SR & USART_SR_TC) == 0);
}

My read function:

void read(){
    while((USART1_SR & USART_SR_RXNE) == 0); 
    int c =  USART1_DR;
    printf("%d\n",c);
}

With the BRR I made(9600 bauds), the receiver gets incoherent messages but if I try a BRR for 115200 bauds it doesnt even work. I don't really know what else I could try.

0

There are 0 best solutions below