cancel
Showing results for 
Search instead for 
Did you mean: 

my TX pin doesnt seem to work when using UART (STM32F446RE)

seb5767
Visitor

These are my registers addresses for stm32446re i have checked these with the datasheet so im pretty sure their correct

#define GPIOA_BASE 0x40020000U
#define RCC_BASE 0x40023800U // this is the power block
#define USART2_BASE 0x40004400U
// This macro handles the pointer casting and the 0x30 offset automatically
#define RCC_APB1ENR *(volatile uint32_t *) (RCC_BASE + 0x40U)
#define RCC_AHB1ENR *(volatile uint32_t *)(RCC_BASE + 0x30U)

//Turns internal Pull-up or Pull-down resistors on/off
#define GPIOA_PUPDR *(volatile uint32_t *)(GPIOA_BASE + 0x0CU)
//Input Data Register (Reads voltage coming into pins)
#define GPIOA_IDR *(volatile uint32_t *) (GPIOA_BASE + 0x10U)
//Sets pin modes (Input, Output, Alternate Function)
#define GPIOA_MODER *(volatile uint32_t *) (GPIOA_BASE + 0x00U)
//Output Data Register (Sends voltage out to pins)
#define GPIOA_ODR *(volatile uint32_t *) (GPIOA_BASE + 0x14U)
//Alternate Function Low (Routes internal engines to Pins 0–7
#define GPIOA_AFRL *(volatile uint32_t *) (GPIOA_BASE + 0x20U)
//Alternate Function High (Routes internal engines to Pins 8–15)
#define GPIOA_AFRH *(volatile uint32_t *) (GPIOA_BASE + 0x24U)
#define GPIOA_OTYPER *(volatile uint32_t *) (GPIOA_BASE + 0x04U)
#define GPIOA_OSPEEDR *(volatile uint32_t *) (GPIOA_BASE +0x08)
//Uart offsets

//Status Register (Checks if the hardware is busy or empty)
#define USART2_SR *(volatile uint32_t *) (USART2_BASE + 0x00U)
//Data Register (The mailbox slot where you load characters)
#define USART2_DR *(volatile uint32_t *) (USART2_BASE + 0x04U)
//Baud Rate Register (Controls transmission speed)
#define USART2_BRR *(volatile uint32_t *) (USART2_BASE + 0x08U)
//Control Register 1 (The main power and pin enable dashboard
#define USART2_CR1 *(volatile uint32_t *) (USART2_BASE + 0x0CU)
//Control Register 2 (Advanced layout options, like stop bits)
#define USART2_CR2 *(volatile uint32_t *) (USART2_BASE + 0x10U)
//Control Register 3 (Advanced system features, like DMA)
#define USART2_CR3 *(volatile uint32_t *) (USART2_BASE + 0x14U)

* UART.C

*

* Created on: 3/06/2026

* Author: libak

*/

#include "register.h"

void uart_write(int ch)

{

while (!(USART2_SR & (1U << 7)));

USART2_DR = (uint8_t)ch;

}

 

void uart_write_string(char *str)

{

while (*str)

{

uart_write(*str++);

}

}

 

int uart_read(void)

{

while (!(USART2_SR & (1U << 5)));

return (int)(USART2_DR & 0xFF);

}

 

1 REPLY 1
Ozone
Principal III

Sorry, but the code presented is hardly readable, for two reasons.
First the format - there is an "Insert/Edit code sample" option of the forum editor, use it :

Ozone_0-1780466667277.png


Second, you almost exclusively use "magic numbers".
I'm not willing to wade through the reference manual just to check your code.
There are device header files with definitions of speaking names and macros, it doesn't hurt to use those.
And you immediately understand the intention of you own code even months or years later ...

> my TX pin doesnt seem to work when using UART

Have you checked the schematics, that no other circuitry is connected to this pin ?

Use a debugger and the register view to check that all your settings actually take effect.

Have you checked that RS232 input is visible on the Rx pin, e.g. with a scope ?