# Getting a hardfault with two threads

**URL:** <https://forums.freertos.org/t/getting-a-hardfault-with-two-threads/10343>\
**Category:** Kernel\
**Created:** [July 24, 2020, 2:36am UTC](https://forums.freertos.org/t/getting-a-hardfault-with-two-threads/10343 "2020-07-24T02:36:20Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![MasterSil](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MasterSil](https://forums.freertos.org/u/MasterSil)\
**Post date:** [July 24, 2020, 2:36am UTC](https://forums.freertos.org/t/getting-a-hardfault-with-two-threads/10343/1 "2020-07-24T02:36:21Z")

</div>

So i’m learning FreeRTOS on STM32 and currently following a tutorial to get things started. Following are my concerns with the first one being the most:

- I have two threads, but upon running a scheduler, I see a hardfault. But when I run a single thread, I see no issues. I initially thought it’d be related to the stack size but each thread is taking up `130 * 4 = 520 bytes`.

- `TaskHandle_t` is a pointer to `struct tskTaskControlBlock`. Then why is the address of the `xTaskBlinky` passed to the last parameter of `xTaskCreate()`, which takes `TaskHandle_t * const pxCreatedTask )`?

---

<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:** [July 24, 2020, 3:59am UTC](https://forums.freertos.org/t/getting-a-hardfault-with-two-threads/10343/2 "2020-07-24T03:59:11Z")

</div>

> but each thread is taking up `130 * 4 = 520 bytes` .

How do you determine that? Is that the value of `configMINIMAL_STACK_SIZE`?

> `TaskHandle_t` is a pointer to `struct tskTaskControlBlock` . Then why is the address of the `xTaskBlinky` passed to the last parameter of `xTaskCreate()` , which takes `TaskHandle_t * const pxCreatedTask )` ?

The last parameter to `xTaskCreate()` is an output parameter to return the handle of the created task. You can find the detailed documentation of `xTaskCreate()` here: [This page describes the RTOS xTaskCreate() FreeRTOS API function which is part of the RTOS task control API. FreeRTOS is a professional grade, small footprint, open source RTOS for microcontrollers.](https://www.freertos.org/a00125.html)

Where did you get the project from? The line you show is a default handler - how do you determine that it is hard fault? Are SVC\_Handler, PendSV\_Handler and Systick\_Handler installed correctly as mentioned on this page: [FreeRTOS - Open Source RTOS Kernel for small embedded systems](https://www.freertos.org/FAQHelp.html). You should also try the other debugging options (like defining malloc failed hook, defining configASERT) described on the same page.

See this page for specifically debugging hard fault: [Debugging and diagnosing hard faults on ARM Cortex-M CPUs](https://www.freertos.org/Debugging-Hard-Faults-On-Cortex-M-Microcontrollers.html)

Thanks.

---

<div class="post-metadata">

**Author:** ![MasterSil](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MasterSil](https://forums.freertos.org/u/MasterSil)\
**Post date:** [July 24, 2020, 4:42pm UTC](https://forums.freertos.org/t/getting-a-hardfault-with-two-threads/10343/3 "2020-07-24T16:42:50Z")

</div>

> [@aggarg](#):
>
> How do you determine that? Is that the value of `configMINIMAL_STACK_SIZE` ?

Well, 130 is the value of `configMINIMAL_STACK_SIZE`

> [@aggarg](#):
>
> how do you determine that it is hard fault?

`SCB->ICSR->VECTACTIVE` is 0x3 indicating it’s a hardfault

And I have the following defined inside `stm32f4xx_it.h` , but wondering if i really need this file if i’m using freertos.

```auto
void PendSV_Handler(void);
void SysTick_Handler(void);
void SVC_Handler(void);

```

Looks like the interrupts are defined in `port.h` but even excluding `stm32f4xx_it.h` from the project didn’t help

---

<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:** [July 24, 2020, 5:45pm UTC](https://forums.freertos.org/t/getting-a-hardfault-with-two-threads/10343/4 "2020-07-24T17:45:30Z")

</div>

Where did you get this project from? Do you have the following in your `FreeRTOSConfig.h`:

```auto
#define vPortSVCHandler SVC_Handler
#define xPortPendSVHandler PendSV_Handler
#define xPortSysTickHandler SysTick_Handler

```

Thanks.

---

<div class="post-metadata">

**Author:** ![MasterSil](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MasterSil](https://forums.freertos.org/u/MasterSil)\
**Post date:** [July 24, 2020, 6:13pm UTC](https://forums.freertos.org/t/getting-a-hardfault-with-two-threads/10343/5 "2020-07-24T18:13:23Z")

</div>

Yes, I have these #defines in `FreeRTOSConfig.h`. And I created an STM32 Project in eclipse, and copied over the kernel files

---

<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:** [July 24, 2020, 6:37pm UTC](https://forums.freertos.org/t/getting-a-hardfault-with-two-threads/10343/6 "2020-07-24T18:37:13Z")

</div>

Please follow the instructions here to determine what is causing hard fault: [https://www.freertos.org/Debugging-Hard-Faults-On-Cortex-M-Microcontrollers.html](https://www.freertos.org/Debugging-Hard-Faults-On-Cortex-M-Microcontrollers.html)

If you share your project, I can take a look too.

Thanks.

---

<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:** [July 24, 2020, 6:37pm UTC](https://forums.freertos.org/t/getting-a-hardfault-with-two-threads/10343/7 "2020-07-24T18:37:16Z")

</div>

Recommend making sure you replace the default handler on interrupt vectors with a handler that lets you know which interrupt it was that occurred. Alternatively you can add some assembly code to the handler to determine with interrupt is being processed - more info on that here [https://www.freertos.org/Debugging-Hard-Faults-On-Cortex-M-Microcontrollers.html](https://www.freertos.org/Debugging-Hard-Faults-On-Cortex-M-Microcontrollers.html)

Also suggest you have configASSERT() defined, stack overflow protection set to 2, are using a very recent version of FreeRTOS to benefit from additional programmatic checks that your configuration matches the hardware, a malloc failed hook defined, etc. This is the best starting place to learn about these things: [https://www.freertos.org/FAQHelp.html](https://www.freertos.org/FAQHelp.html)

---

<div class="post-metadata">

**Author:** ![MasterSil](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MasterSil](https://forums.freertos.org/u/MasterSil)\
**Post date:** [July 24, 2020, 7:07pm UTC](https://forums.freertos.org/t/getting-a-hardfault-with-two-threads/10343/8 "2020-07-24T19:07:18Z")

</div>

any way I could attach a project?

---

<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:** [July 24, 2020, 7:32pm UTC](https://forums.freertos.org/t/getting-a-hardfault-with-two-threads/10343/9 "2020-07-24T19:32:16Z")

</div>

You can use any means to share - Google Drive, GitHub, or Anything else etc.

---

<div class="post-metadata">

**Author:** ![MasterSil](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MasterSil](https://forums.freertos.org/u/MasterSil)\
**Post date:** [July 24, 2020, 8:39pm UTC](https://forums.freertos.org/t/getting-a-hardfault-with-two-threads/10343/10 "2020-07-24T20:39:48Z")

</div>

[https://drive.google.com/drive/folders/1S8ODYQOFWbV67VkJmMrhxWgr3L9yYS7r](https://drive.google.com/drive/folders/1S8ODYQOFWbV67VkJmMrhxWgr3L9yYS7r)

---

<div class="post-metadata">

**Author:** ![MasterSil](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MasterSil](https://forums.freertos.org/u/MasterSil)\
**Post date:** [July 24, 2020, 9:46pm UTC](https://forums.freertos.org/t/getting-a-hardfault-with-two-threads/10343/11 "2020-07-24T21:46:01Z")

</div>

> replace the default handler on interrupt vectors with a handler that lets you know which interrupt it was that occurred.

thing is all NVIC registers are 0. which handler would let me know about the interrupt that occurred?

> you have configASSERT() defined, stack overflow protection set to 2

are you referring to `configCHECK_FOR_STACK_OVERFLOW`?

---

<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:** [July 24, 2020, 9:53pm UTC](https://forums.freertos.org/t/getting-a-hardfault-with-two-threads/10343/12 "2020-07-24T21:53:40Z")

</div>

You are excluding the file `stm32f4xx_it.c` from compilation which excludes the following default SysTick\_Handler:

```auto
/**
  * @brief This function handles System tick timer.
  */
void SysTick_Handler(void)
{
  /* USER CODE BEGIN SysTick_IRQn 0 */

  /* USER CODE END SysTick_IRQn 0 */
  HAL_IncTick();
  /* USER CODE BEGIN SysTick_IRQn 1 */

  /* USER CODE END SysTick_IRQn 1 */
}

```

The result is that the ` HAL_IncTick();` is never called and the ST HAL won’t work. The correct way to do is the following:

1. Change the HAL to use a timer other than the SysTick as shown in the image below:  
 ![Capture](https://d2v6vdsk2p900z.cloudfront.net/original/1X/275a7accd8644296ff7b4420c672e129e09b43ca.jpeg)
2. Remove the definition of SysTick\_Handler, PendSV\_Handler and SVC\_Handler from both `stm32f4xx_it.h` and `stm32f4xx_it.c` but do not exclude these files.

Another issue is that the include paths are configured to full paths on your machine which do not exist on other machines. Better to use relative paths there.

Thanks.

---

<div class="post-metadata">

**Author:** ![MasterSil](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MasterSil](https://forums.freertos.org/u/MasterSil)\
**Post date:** [July 24, 2020, 11:05pm UTC](https://forums.freertos.org/t/getting-a-hardfault-with-two-threads/10343/13 "2020-07-24T23:05:58Z")

</div>

Thanks for taking your time out.

Regards to your comments, it would complain if I didn’t have SysTick\_Handler() commented out cause it’s it’s defined inside `port.c` and declared in `FreeRTOSConfig.h `  
`#define xPortSysTickHandler SysTick_Handler`

Is the main reason behind not using SysTick is to avoid the conflict with the definition in `port.c`?

---

<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:** [July 24, 2020, 11:14pm UTC](https://forums.freertos.org/t/getting-a-hardfault-with-two-threads/10343/14 "2020-07-24T23:14:19Z")

</div>

Yes, we do not want to use the default SysTick\_Handler, PendSV\_Handler and SVC\_Handler in `stm32f4xx_it.c` and therefore I asked you to comment those out. Do not exclude the file from project because other handlers in the file `stm32f4xx_it.c` are still needed.

> Is the main reason behind not using SysTick is to avoid the conflict with the definition in `port.c` ?

Yes, we want to use SysTick\_Handler, PendSV\_Handler and SVC\_Handler in port.c.

Thanks.
