cancel
Showing results for 
Search instead for 
Did you mean: 

Do not operate the printf() after reboot

Ekim.1
Associate II

Hello, everyone.

I am using STM32H753 and STM32CubeIDE.

I tried to use the printf () function, but nothing was displayed at the terminal.

After seeing an Internet example, I added the following code to use printf():

int _write(int file,char *ptr, int len)
{
	HAL_UART_Transmit(&huart1, (uint8_t*) ptr, len ,10);
	return len;
}

When I downloaded the code, my code normally performed a printf() function.

But when I turned it off and on (reboot) the terminal was not marked with anything.

If the code is downloaded again after reboot, the printf() operates normally.

Why won't the printf() work after the reboot?

5 REPLIES 5
TDK
Guru

Are you initializing the UART correctly?

Is the return value of HAL_UART_Transmit HAL_OK?

What happens when you debug and step through this part of the code?

If you feel a post has answered your question, please click "Accept as Solution".
Ekim.1
Associate II

I think I'm initializing UART correctly.

My UART initialization code is as follows.

void MX_USART1_UART_Init(void) {
	huart1.Instance = USART1;
	huart1.Init.BaudRate = 115200;
	huart1.Init.WordLength = UART_WORDLENGTH_8B;
	huart1.Init.StopBits = UART_STOPBITS_1;
	huart1.Init.Parity = UART_PARITY_NONE;
	huart1.Init.Mode = UART_MODE_TX_RX;
	huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
	huart1.Init.OverSampling = UART_OVERSAMPLING_16;
	huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
	huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
	huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
	if (HAL_UART_Init(&huart1) != HAL_OK) {
		Error_Handler();
	}
	if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8)
			!= HAL_OK) {
		Error_Handler();
	}
	if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8)
			!= HAL_OK) {
		Error_Handler();
	}
	if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK) {
		Error_Handler();
	}
}

I changed and executed the _write() function as follows: The results were the same as the first. After downloading, the printf() operated normally, but after reboot, the print() did not work.

int _write(int file,char *ptr, int len)
{
	if(HAL_UART_Transmit(&huart1, (uint8_t*) ptr, len ,10) != HAL_OK)
	{
		Error_Handler();
	}
	return len;
}

I debugged and step through this part of the code, but the code only goes to the next line, not showing the inside of the printf(). (I used Steo Into).

TDK
Guru

Is your Error_Handler actually defined? The default implementation is empty.

if you put a breakpoint at the statement, and it isnt hit, then execution isnt making it there.

I use putchar to implement stdout. Works fine. Not sure of the details on using _write instead.

If you feel a post has answered your question, please click "Accept as Solution".
berendi
Principal

printf() uses malloc() internally, which depends on _sbrk(). Verify that _sbrk() works correctly.

Is there any way to make sure that _sbrk works correctly?