cancel
Showing results for 
Search instead for 
Did you mean: 

I can't use the printf() function to transmit data via USART3

icaro_mendes00
Associate II

I am developing a program to enable the USART3 (initially as a UART) of the STM32H755 (NUCLEO-H755ZI-Q) and use it to transmit information through the functions "__io_putchar" and "printf" but it does not work. At first I thought it was an error in the baud rate calculation, I searched the reference manual and understood that the default clock after the reset is 64Mhz, through the HSI, I made the change, but it still does not work. Here is the file with the settings and functions:

#include <stdint.h>
#include "stm32h755xx.h"

#define SYS_FREQ_64M    64000000 // sys_ck -> 64MHz
#define APB1L_CK        SYS_FREQ_64M

#define UART_BAUDRATE   115200

void UART3_tx_Init(void)
{
	RCC->AHB4ENR |= RCC_AHB4ENR_GPIODEN;    // habilita o clock no GPIOD (PD9 - RX)
	GPIOD->MODER &= ~(0b11 << 18);          // limpa os campos do PD9
	GPIOD->MODER |= (0b10 << 18);           // seta o PD9 no modo alternativo
	GPIOD->AFR[1] &= ~(0xF << 4);           // limpa os campos do PD9
	GPIOD->AFR[1] |= (0b0111 << 4);         // seta o PD9 no modo alternativo 7

	// configura a usart 3
	RCC->APB1LENR |= RCC_APB1LENR_USART3EN;      // habilita o clock pra usart 3
	uart3_set_baudrate(APB1L_CK, UART_BAUDRATE); // configura o baudrate

	USART3->CR1 = ~CR1_FIFO_EN;
	USART3->CR1 = CR1_TE_EN;                     // configura a direcao de transferencia TX
	USART3->CR1 = CR1_UE_EN;                     //habilita o modulo uart
}

uint16_t calc_uart_baud_rate(uint32_t clock_periferico, uint32_t baudrate)
{
	return ((clock_periferico + (baudrate/2U))/baudrate);
}

void uart3_set_baudrate(uint32_t clock_periferico, uint32_t baudrate)
{
	USART3->BRR = calc_uart_baud_rate(clock_periferico, baudrate);
}

void uart3_write(int ch)
{
	while(!(USART3->ISR & ISR_TXE)); // garante que o registrador de transmissao esta vazio
	USART3->TDR = (ch &0xFF);         // grava no registrador de transmissao
}

int __io_putchar(int ch)
{
	uart3_write(ch);
	return ch;
}

 

1 ACCEPTED SOLUTION

Accepted Solutions

Watch the clock enable hazard when subsequently writing to the peripheral

USART3_TX is PD8, not PD9

Test uart3_write() independently of printf(), putchar(), etc

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

View solution in original post

1 REPLY 1

Watch the clock enable hazard when subsequently writing to the peripheral

USART3_TX is PD8, not PD9

Test uart3_write() independently of printf(), putchar(), etc

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