2025-01-04 06:47 PM
When I use the UART_Printf example to output an array of size 1024 (or larger) via printf, the program seems to freeze and doesn't output any information after being downloaded to the STM32WB55 Nucleo board. However, when I reduce the array size to 600, it can successfully print via the serial port. How can I optimize the code to output a large array, or what might be causing this issue?
/* USER CODE BEGIN 2 */
/* Initialize BSP Led for LED3 */
BSP_LED_Init(LED3);
/* Output a message on Hyperterminal using printf function */
printf("\n\r UART Printf Example: retarget the C library printf function to the UART\n\r");
// Generate a test array with data
#define ARRAY_SIZE 1024
uint8_t tran_test[ARRAY_SIZE];
for (uint16_t i = 0; i < ARRAY_SIZE; i++)
{
tran_test[i] = (uint8_t)(i % 256); // cycling from 0x00 to 0xFF
}
printf("** Test finished successfully. ** \n\r");
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
printf("**ARRAY_SIZE = %d\n\r", ARRAY_SIZE);
for (int i = 0; i < ARRAY_SIZE; i++)
{
printf("0x%02X", tran_test[i]);
// adjust the data format
if ((i + 1) % 16 == 0)
{
printf("\r\n");
}
else
{
printf(" "); // 数据之间加空格
}
// 延时和提示信息,每 512 个数据
if ((i + 1) % 512 == 0)
{
HAL_Delay(90);
printf("\r\n-- Reached %d bytes of data --\n\r\n", i + 1);
}
}
printf("** Test finished successfully11111. ** \n\r");
HAL_Delay(2000);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
Solved! Go to Solution.
2025-01-04 07:36 PM
Probably a stack overflow. Large arrays should generally be placed in global memory (global variable) or in the heap.
2025-01-04 06:48 PM
2025-01-04 07:36 PM
Probably a stack overflow. Large arrays should generally be placed in global memory (global variable) or in the heap.