# Finding configTOTAL\_HEAP\_SIZE Maximum Value

**URL:** <https://forums.freertos.org/t/finding-configtotal-heap-size-maximum-value/5797>\
**Category:** Kernel\
**Created:** [March 9, 2016, 10:19pm UTC](https://forums.freertos.org/t/finding-configtotal-heap-size-maximum-value/5797 "2016-03-09T22:19:11Z")\
**Posts on this page:** 9\
**Page:** 2

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 15, 2016, 9:46pm UTC](https://forums.freertos.org/t/finding-configtotal-heap-size-maximum-value/5797/21 "2016-03-15T21:46:19Z")

</div>

**[spflanze](https://sourceforge.net/u/spflanze/)** wrote on [Tuesday, March 15, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/da34d0b6/#ec3d):

The -O1 optimization produces this assembly code:

```auto
                    StartThreadUART_Rx:
08002098: StartThreadUART_Rx+0 push {r4, r5, r6, lr}
0800209a: StartThreadUART_Rx+2 sub sp, #16
0800209c: StartThreadUART_Rx+4 sub.w r3, sp, #12224 ; 0x2fc0
080020a0: StartThreadUART_Rx+8 movs r2, #0
080020a2: ...tThreadUART_Rx+10 str.w r2, [r3, #-60]
159 { xQueueReceiveResult = xQueueReceive( QueueUART_RxHandle, (void *)&rxchar, portMAX_DELAY );
080020a6: ...tThreadUART_Rx+14 ldr r5, [pc, #128] ; (0x8002128 <StartThreadUART_Rx+144>)
161 HAL_UART_DMAResumeStat = HAL_UART_DMAResume( &huart2 );
080020a8: ...tThreadUART_Rx+16 ldr r6, [pc, #128] ; (0x800212c <StartThreadUART_Rx+148>)
159 { xQueueReceiveResult = xQueueReceive( QueueUART_RxHandle, (void *)&rxchar, portMAX_DELAY );
080020aa: ...tThreadUART_Rx+18 movs r3, #0
080020ac: ...tThreadUART_Rx+20 mov.w r2, #4294967295
080020b0: ...tThreadUART_Rx+24 add.w r1, sp, #15
080020b4: ...tThreadUART_Rx+28 ldr r0, [r5, #0]
080020b6: ...tThreadUART_Rx+30 bl 0x8006984 <xQueueGenericReceive>

```

The Bus Fault happens just as before at …Rx+10.

Is it possible this is a GCC bug?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 15, 2016, 11:55pm UTC](https://forums.freertos.org/t/finding-configtotal-heap-size-maximum-value/5797/22 "2016-03-15T23:55:54Z")

</div>

**[spflanze](https://sourceforge.net/u/spflanze/)** wrote on [Tuesday, March 15, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/da34d0b6/#65e4):

I commented out code in void StartThreadUART\_Rx() so that this was all that was left in effect:

```auto
void StartThreadUART_Rx(void const * argument)
{ for(;;){}
}

```

The instructions that caused the Bus Fault were not produced, and the thread ran normally.

I then brought back only the call to xQueueReceive() such that the code in effect was:

```auto
void StartThreadUART_Rx(void const * argument)
{ uint8_t rxchar;
    BaseType_t xQueueReceiveResult;
    for(;;)
    { xQueueReceiveResult = xQueueReceive( QueueUART_RxHandle, (void *)&rxchar, 10000 ), portMAX_DELAY );
    }
}

```

The assembly code that caused the Bus Fault returned.

Next I tried:

```auto
void StartThreadUART_Rx(void const * argument)
{ size_t len 
   for(;;)
   { len = strlen("test");
   }
}

```

The assembly code that caused the Bus Fault was not present. This ran normally.

Next I tried:

```auto
void StartThreadUART_Rx(void const * argument)
{ HAL_StatusTypeDef HAL_UART_DMAResumeStat; 
   for(;;)
   { HAL_UART_DMAResumeStat = HAL_UART_DMAResume( &huart2 );
   }
}

```

The Bus Fault causing assembly code returned.

There are certain function calls the cause the Bus Fault assembly code to be created.

---

<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:** [March 16, 2016, 12:13am UTC](https://forums.freertos.org/t/finding-configtotal-heap-size-maximum-value/5797/23 "2016-03-16T00:13:37Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Wednesday, March 16, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/da34d0b6/#65e4/7568):

> 0800209c: StartThreadUART\_Rx+4 sub.w r3, sp, #12224 ;

So this line is still weird. Then there are these lines:

> 080020a0: StartThreadUART\_Rx+8 movs r2, #0  
> 080020a2: …tThreadUART\_Rx+10 str.w r2, [r3, #-60]

So it looks like it is loading a base address from into r3, maybe the  
base of a structure or array, but using an address which is off memory.  
Then it is indexing into an offset from that base address.

> I commented out code in void StartThreadUART\_Rx() so that this was all  
> that was left in effect:
> 
> void StartThreadUART\_Rx(void const \* argument)  
> { for(;;){}  
> }
> 
> The instructions that caused the Bus Fault were not produced, and the  
> thread ran normally.
> 
> I then brought back only the call to xQueueReceive() such that the code  
> in effect was:
> 
> void StartThreadUART\_Rx(void const \* argument)  
> { uint8\_t rxchar;  
> BaseType\_t xQueueReceiveResult;  
> for(;😉  
> { xQueueReceiveResult = xQueueReceive( QueueUART\_RxHandle, (void \*)&rxchar, 10000 ), portMAX\_DELAY );  
> }  
> }
> 
> The assembly code that caused the Bus Fault returned.

Even if QueueUART\_RxHandle was not a valid handle it would not explain  
the asm code.

I think you have one too many ‘)’ brackets in your line to  
xQueueReceive, but assume that is a typeo in adding the code to the  
forum post.

> Next I tried:
> 
> void StartThreadUART\_Rx(void const \* argument)  
> { size\_t len  
> for(;😉  
> { len = strlen(“test”);  
> }  
> }
> 
> The assembly code that caused the Bus Fault was not present. This ran  
> normally.

Ok.

> Next I tried:
> 
> void StartThreadUART\_Rx(void const \* argument)  
> { HAL\_StatusTypeDef HAL\_UART\_DMAResumeStat;  
> for(;😉  
> { HAL\_UART\_DMAResumeStat = HAL\_UART\_DMAResume( &huart2 );  
> }  
> }
> 
> The Bus Fault causing assembly code returned.
> 
> There are certain function calls the cause the Bus Fault assembly code  
> to be created.

So this shows it is not related to the xQueueReceive() function, or how  
it is used. Very very strange!

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 16, 2016, 6:40pm UTC](https://forums.freertos.org/t/finding-configtotal-heap-size-maximum-value/5797/24 "2016-03-16T18:40:56Z")

</div>

**[spflanze](https://sourceforge.net/u/spflanze/)** wrote on [Wednesday, March 16, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/da34d0b6/#2131):

This appears to me to be a compiler bug. As a work around I am going to try to do the function calls in assembly code. I am studying this document:  
[http://infocenter.arm.com/help/topic/com.arm.doc.ihi0042f/IHI0042F\_aapcs.pdf](http://infocenter.arm.com/help/topic/com.arm.doc.ihi0042f/IHI0042F_aapcs.pdf)  
to learn how to do this. If you can recommend any other I would appreciate your doing so.

The above xQueueReceive() call arguments were in error. It was a remnant of an experiment to find out if less than the maximum delay would make a difference here. It should have been:  
`xQueueReceive( QueueUART_RxHandle, (void *)&rxchar, portMAX_DELAY );`

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 17, 2016, 3:02am UTC](https://forums.freertos.org/t/finding-configtotal-heap-size-maximum-value/5797/25 "2016-03-17T03:02:52Z")

</div>

**[spflanze](https://sourceforge.net/u/spflanze/)** wrote on [Thursday, March 17, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/da34d0b6/#65e4/7568/4443):

An examination of the disassembly code showed that strlen() is not actually called. It was instead placed inline. And the compiler was smart enough to count the number of characters in the string, so not even counting characters is done in the instructions it produced. Instead it simply set len to 4.

So I tried the function malloc() in its place, and the instructions that produce the Bus Fault appeared. It appears that any function call in the body of StartThreadUART\_Rx() causes those Bus Fault instructions to be created, not just FREERTOS ones.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [March 17, 2016, 9:28pm UTC](https://forums.freertos.org/t/finding-configtotal-heap-size-maximum-value/5797/26 "2016-03-17T21:28:19Z")

</div>

**[spflanze](https://sourceforge.net/u/spflanze/)** wrote on [Thursday, March 17, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/da34d0b6/#8341):

This problem is solved. It became apparant this is a compiler issue and not a FREERTOS issue. So I created this thread at GCC where I got the answer:  
[http://gcc.1065356.n5.nabble.com/Bus-Fault-td1246632.html#a1246958](http://gcc.1065356.n5.nabble.com/Bus-Fault-td1246632.html#a1246958)

The problem was the -fstack-check compiler option. When this was removed the code that caused the Bus Fault was no longer created.

I thank you for your help 🙂

---

<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:** [March 18, 2016, 10:41am UTC](https://forums.freertos.org/t/finding-configtotal-heap-size-maximum-value/5797/27 "2016-03-18T10:41:53Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, March 18, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/da34d0b6/#8341/66d9):

> I thank you for your help 🙂

…and thanks for taking the time to report back.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 30, 2016, 9:23pm UTC](https://forums.freertos.org/t/finding-configtotal-heap-size-maximum-value/5797/28 "2016-05-30T21:23:57Z")

</div>

**[fedeandrades](https://sourceforge.net/u/fedeandrades/)** wrote on [Monday, May 30, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/da34d0b6/#a029):

where the option to disable the “-fstack-check” in the STM32 workbenck of AC6, I have the same problem and if this solves it would be wonderful to know how.

regreted, written in English by web translator

thanks.

edit:  
MCF GCC Compiler All options:  
-mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -D\_\_weak=“ **attribute** ((weak))” -D\_\_packed=“ **attribute** (( **packed** ))” -DUSE\_HAL\_DRIVER -DSTM32F429xx -I…/Inc -I"L:\DESIGN\CPUARM\WORKSPACE.SOFT\RTOS2016\_V1\Src\minini\_12b" -I…/Drivers/STM32F4xx\_HAL\_Driver/Inc -I…/Drivers/STM32F4xx\_HAL\_Driver/Inc/Legacy -I…/Middlewares/Third\_Party/FreeRTOS/Source/portable/GCC/ARM\_CM4F -I…/Middlewares/ST/STM32\_USB\_Device\_Library/Core/Inc -I…/Middlewares/ST/STM32\_USB\_Device\_Library/Class/MSC/Inc -I…/Middlewares/Third\_Party/FatFs/src -I…/Middlewares/Third\_Party/FreeRTOS/Source/include -I…/Middlewares/Third\_Party/FreeRTOS/Source/CMSIS\_RTOS -I…/Drivers/CMSIS/Include -I…/Drivers/CMSIS/Device/ST/STM32F4xx/Include -O0 -g3 -Wall -fmessage-length=0 -ffunction-sections -c -fmessage-length=0

i not have -fstack-check !!! but the problem still occurs

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [May 31, 2016, 6:17am UTC](https://forums.freertos.org/t/finding-configtotal-heap-size-maximum-value/5797/29 "2016-05-31T06:17:42Z")

</div>

**[heinbali01](https://sourceforge.net/u/heinbali01/)** wrote on [Tuesday, May 31, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/da34d0b6/#cc7c):

Frederico,

Lo siento, aunque el Español es mucho mas bonito que el Ingles :-), tu tendrás que aprender mucho Ingles si tu quieres volverte en un buen programador de embedded software. No es suficiente utilizar el Google Translate.

Porque piensas que tu tienes el mismo problema que la persona “Artist”? Qué estas observando? Que hace tu programa?

I’m sorry, although Spanish is much more beautiful than English :-), you will have to master English if you want to become a good embedded software programmer. It is not enough to use Google Translate.

Why do you think that you have a similar problem as the poster “Artist”, here above? What do you observe? What does the program do?

[Previous page](https://forums.freertos.org/t/finding-configtotal-heap-size-maximum-value/5797.md?page=1)
