cancel
Showing results for 
Search instead for 
Did you mean: 

ThreadX, printf() and hard fault problem

TDJ
Lead

I have a very simple ThreadX app build with CubeMX, all settings default, the only relevant change is this code:

 

void tx_app_thread_entry(ULONG thread_input)
{
  static uint32_t c = 1;
  /* USER CODE BEGIN tx_app_thread_entry */
  printf("Starting ThreadX App\n\n");
  
  while (1) {
    tx_thread_sleep(10);
    // printf("1 this %li is a 0x%02X test 0x%02X\n", c, 0xaa, 0xbb);
    // printf("2 this %li is a 0x%02X test 0x%02X\n", c, 0xaa, 0xbb);
    // printf("3 this %li is a 0x%02X test 0x%02X\n", c, 0xaa, 0xbb);
    printf("1 this %li is %s a 0x%02X test 0x%02X \n", c, "inner string", 0xaa, 0xbb);
    printf("2 this %li is %s a 0x%02X test 0x%02X \n", c, "inner string", 0xaa, 0xbb);
    printf("3 this %li is %s a 0x%02X test 0x%02X \n", c, "inner string", 0xaa, 0xbb);
    c++;
  }
  /* USER CODE END tx_app_thread_entry */
}

 

 

/* USER CODE BEGIN 0 */
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif

PUTCHAR_PROTOTYPE {
	HAL_StatusTypeDef status = HAL_UART_Transmit(&huart3, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
  if (status != HAL_OK) {
    printf("\n");
  }
	return ch;
}
/* USER CODE END 0 */

 

When the loop executes for the 10th time hard fault occurs. UFSR=2 (INVSTATE flag set), HFSR=0x4000000 (FORCED flag set), BFSR, MMFSR, ABFSR=0.
Hard fault does not occur if commented out lines are called instead. I am clueless.
Using gcc compiler "Arm GNU Toolchain 12.3.Rel1 (Build arm-12.35)", tested with Nucleo-H7A3ZI board, but the problem is not limited to this particular MCU, e.g. occurs on U5A9 as well.

TDJ_0-1697634993487.png

TDJ_0-1697638673905.png

1 ACCEPTED SOLUTION

Accepted Solutions
TDJ
Lead

I found the problem. TX_APP_STACK_SIZE was simply too small. Increasing it 4x seems to solve the issue.

View solution in original post

1 REPLY 1
TDJ
Lead

I found the problem. TX_APP_STACK_SIZE was simply too small. Increasing it 4x seems to solve the issue.