pvPortMalloc causing hard fault

adambujak wrote on Saturday, September 21, 2019:


I’m very new to RTOS, so I’m sorry if this is something really obvious.

I’m working on an Apollo3 EVB.

When I flash my board and debug using GDB everything works as expected. If I power cycle my board and let the program boot from ROM something goes wrong; if I then connect with JLink and send a reset command, and then a go command, everything works as expected.

I can debug the issue only by flashing the board, then power cycling, then debugging by connecting to a running target.

I only noticed this issue when I added a sleep on program start feature, so I’m not sure if that’s causing some sort of issue, or just unmasking an existing issue.

Below are the functions where I start seeing issues:

static void setup_task ( void *pvParameters )


  // Run setup functions.

  // Create the functional tasks
  xTaskCreate(CLI_Task_Main,    "CLI_Task",    512,  0, 4, &cliTaskHandle);
  xTaskCreate(App_Task_Main,    "AppTask",     512,  0, 0, &appTaskHandle);
  xTaskCreate(UI_Task_Main,     "UI_Task",     512,  0, 0, &uiTaskHandle);
  xTaskCreate(Sensor_Task_Main, "SensorTask",  512,  0, 0, &sensorTaskHandle);

  // The setup operations are complete, so suspend the setup task now.

  while (1);

// Initializes all tasks
void run_tasks(void)
  // Note: Timer priority is handled by the FreeRTOS kernel, so we won't
  // touch it here.

  // Create essential tasks.
  xTaskCreate(setup_task, "Setup", 512, 0, 0, &xSetupTask);

  // Start the scheduler.

When I comment out all but UI_Task_Setup() and the respective taskCreate everything works as expected.

Otherwise, I seem to be getting hard faults. I think I’ve narrowed it down to the pvPortMalloc function. Specifically the line:

while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ))

I did some debugging and found that this causes a hard fault becuase pxBlock->xBlockSize isn’t defined. Further investigation led me to find that the heap isn’t initialized using prvHeapInit, because xHeapHasBeenInitialised is some random value instead of 1 or 0.

I read somewhere that this may be due to priority issues, and I messed around with the priorities a little bit to no avail (like I said, I’m very new to RTOS).

Anyway, I’ve been scratching/banging my head for a few days, so I’d appreciate any help anyone can offer.

Thanks in advance.

rtel wrote on Saturday, September 21, 2019:

If it works fine when you start up using the debugger but not without the debugger then look closely at what the debug start up is doing. Sometimes a debugger will run a start up script that does some kind of hardware or software initialisation before it runs the program, or maybe manually sets the program counter to the start of the application rather than using the reset vector, etc.

adambujak wrote on Sunday, September 22, 2019:

Thanks! Any tips on how I can go about seeing what’s happening behind the scenes? Would I have to do research on the specific debugger I’m using? Or is there a general way to see what’s going on?