cancel
Showing results for 
Search instead for 
Did you mean: 

printf() working on one project but not another

JRM_02
Associate II

I have 2 projects for a nucleo-H536ZI. the first project the printf()s work and are tied to the _write() function which I have debugged and proven. The second project the printfs() do not work using the same printf() & _write() function code again and I have verified it gets to the correct  _write(), code shown below. Both projects have the same pins (verified) and the same MX_USART2_UART_Init() as shown below. I did copy and drop the BSP folder from the repository incase it maters on the project that doesn't work. 

Both have the same debugger configurations set up. I have shown the code that both projects use in main() to get to the first printf() statement.  I am out of ideas why one works and the other does not, any help is most appreciated.

PA0 CTS

PA3 Rx

PD4 RTS

PD5 Tx

 

 ====================================

 

int main(void)

{

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/* MCU Configuration--------------------------------------------------------*/

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */

HAL_Init();

/* USER CODE BEGIN Init */

/* USER CODE END Init */

/* Configure the system clock */

SystemClock_Config();

/* USER CODE BEGIN SysInit */

/* USER CODE END SysInit */

/* Initialize all configured peripherals */

MX_GPIO_Init();

MX_I3C1_Init();

MX_ICACHE_Init();

MX_USART2_UART_Init();

/* USER CODE BEGIN 2 */

printf("Hello comm setup.\n"); //This works on project 1 not project 2

...

 ====================================

 

static void MX_USART2_UART_Init(void)

{

/* USER CODE BEGIN USART2_Init 0 */

/* USER CODE END USART2_Init 0 */

/* USER CODE BEGIN USART2_Init 1 */

/* USER CODE END USART2_Init 1 */

huart2.Instance = USART2;

huart2.Init.BaudRate = 115200;

huart2.Init.WordLength = UART_WORDLENGTH_8B;

huart2.Init.StopBits = UART_STOPBITS_1;

huart2.Init.Parity = UART_PARITY_NONE;

huart2.Init.Mode = UART_MODE_TX_RX;

huart2.Init.HwFlowCtl = UART_HWCONTROL_RTS_CTS;

huart2.Init.OverSampling = UART_OVERSAMPLING_16;

huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;

huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;

huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

if (HAL_UART_Init(&huart2) != HAL_OK)

{

Error_Handler();

}

if (HAL_UARTEx_SetTxFifoThreshold(&huart2, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)

{

Error_Handler();

}

if (HAL_UARTEx_SetRxFifoThreshold(&huart2, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)

{

Error_Handler();

}

if (HAL_UARTEx_DisableFifoMode(&huart2) != HAL_OK)

{

Error_Handler();

}

/* USER CODE BEGIN USART2_Init 2 */

 ====================================

int _write(int file, char *ptr, int len)

{

for(int DataIdx=0; DataIdx < len; DataIdx++)

{

//please note, I have proven I get to this line for both projects, I

//just have not seen output in the SWV ITM Data Console port0 with trace on.

ITM_SendChar(*ptr++); //Send/Display string char by char.

}

return len;

}

 

 

 

/* USER CODE END USART2_Init 2 */

 

}

1 ACCEPTED SOLUTION

Accepted Solutions
JRM_02
Associate II

Solved!

The SWV core clock was by default set to 16.0 and needed to match the SYSCLCK value of 250 MHz. This is not the case with the project that had functional printf() (that one works with the default 16.0), but it solved the problem nonetheless.

View solution in original post

2 REPLIES 2
JRM_02
Associate II

Solved!

The SWV core clock was by default set to 16.0 and needed to match the SYSCLCK value of 250 MHz. This is not the case with the project that had functional printf() (that one works with the default 16.0), but it solved the problem nonetheless.

+1 Yes clock settings expectations must be consistent/coherent.

The debug pod side doesn't know the internal details for HSE, PLL settings, etc. it knows the SWCLK rate and the baud rate dividers in the SWO/SWV path need that to relate to the core's clocking speed.

Similarly watch HSE clock vs define for HSE_VALUE used for UART baud rate computation. Old ST-LINK/V2 were 8 MHz, where as ST-LINK/V3 is often 8.333 MHz unless it's 5 MHz off the ST-LINK/V3 25 MHz reference, or HSI

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