# HardFault\_Handler after prvPortStartFirstTask

**URL:** <https://forums.freertos.org/t/hardfault-handler-after-prvportstartfirsttask/13455>\
**Category:** Kernel\
**Created:** [September 14, 2021, 11:05am UTC](https://forums.freertos.org/t/hardfault-handler-after-prvportstartfirsttask/13455 "2021-09-14T11:05:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![eeyc](https://avatars.discourse-cdn.com/v4/letter/e/f475e1/32.png) [@eeyc](https://forums.freertos.org/u/eeyc)\
**Post date:** [September 14, 2021, 11:05am UTC](https://forums.freertos.org/t/hardfault-handler-after-prvportstartfirsttask/13455/1 "2021-09-14T11:05:47Z")

</div>

Hi,  
I have HardFault\_Handler after prvPortStartFirstTask, I am tries to detect when or where it happens.  
Every time it happens from another location.  
My app is very big and it difficult to understand where it is – right know this is my approach  
I am frequently checks the heap available size:  
xFreeBytesRemaining size\_t 44824  
I have setup  
#define configCHECK\_FOR\_STACK\_OVERFLOW 1 //Stack Overflow Detection - Method 1  
#define configASSERT( x ) if ((x) == 0) {assert\_failed((uint8\_t \*) **FILE** , **LINE** ); taskDISABLE\_INTERRUPTS(); for ( ;; );}  
void vApplicationStackOverflowHook(void ){ assert\_failed ( **FILE** , **LINE** );}

but I cannot catch it. How can I debug it from the HardFault back to location of the error.

- this is not happen right a way after starting the kernel

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [September 14, 2021, 2:24pm UTC](https://forums.freertos.org/t/hardfault-handler-after-prvportstartfirsttask/13455/2 "2021-09-14T14:24:57Z")

</div>

Which port are you using?  
Which compiler are you using?  
Which version of FreeRTOS are you using?  
Why not set configCHECK\_FOR\_STACK\_OVERFLOW to 2?

---

<div class="post-metadata">

**Author:** ![eeyc](https://avatars.discourse-cdn.com/v4/letter/e/f475e1/32.png) [@eeyc](https://forums.freertos.org/u/eeyc)\
**Post date:** [September 14, 2021, 3:28pm UTC](https://forums.freertos.org/t/hardfault-handler-after-prvportstartfirsttask/13455/3 "2021-09-14T15:28:10Z")

</div>

port: not sure what is it.  
Compiler : GCC), STM32CubeIDE.  
FreeRTOS:V1,FreeRTOS Kernel V10.3.1  
configCHECK\_FOR\_STACK\_OVERFLOW - now I set it to 2.and i will monitor it  
now it stopped to go to HardFault, don’t know why - still under investigation

I think it crash due, task size what too low.  
How can I know that the size of the task is starting to get small?

This is the task definition I have used:  
osThreadDef(runnig\_main\_ThreadName, _runnig\_main_, osPriorityNormal, 0, 4048+1024);|  
osThreadCreate(osThread(runnig\_main\_ThreadName), NULL);|

Another thing,  
my code is in the original used RTX and it converted to FreeRTOS  
_runnig\_main_ is Initialize the all system it reads staff from SPI(interrupts) created and tasks, mutex’s, timers… Initializing staff

When I used RTX - _runnig\_main_ not in task, but here It have to do it like that because the freeRTOS disable interrupts until the kernel starting.

After _runnig\_main_ task creating all the tasks… it can finish, but the other tasks will stoped as well, so I hold it inside

I am not sure this is the best practice for that, what is your opinion on this?

Thanks alot,

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [September 14, 2021, 4:33pm UTC](https://forums.freertos.org/t/hardfault-handler-after-prvportstartfirsttask/13455/4 "2021-09-14T16:33:21Z")

</div>

When you say the task size is too small, do you mean the task’s stack size is too small?  
If it is not going to a hardfault now, what is the code doing? Is it in an assert?

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [September 15, 2021, 1:12am UTC](https://forums.freertos.org/t/hardfault-handler-after-prvportstartfirsttask/13455/5 "2021-09-15T01:12:03Z")

</div>

Which hardware are you using? If possible, it might be useful to comment out some tasks to narrow down the problem. This page provides some additional details about debugging hard faults - [Debugging and diagnosing hard faults on ARM Cortex-M CPUs](https://www.freertos.org/Debugging-Hard-Faults-On-Cortex-M-Microcontrollers.html)

---

<div class="post-metadata">

**Author:** ![eeyc](https://avatars.discourse-cdn.com/v4/letter/e/f475e1/32.png) [@eeyc](https://forums.freertos.org/u/eeyc)\
**Post date:** [September 19, 2021, 5:31am UTC](https://forums.freertos.org/t/hardfault-handler-after-prvportstartfirsttask/13455/6 "2021-09-19T05:31:10Z")

</div>

Thanks, I’ll do as you recommend it sounds like a good plan - part after part
