# STM32F429Discovery with STM32Cube, USB, fatfs and FreeRTOS

**URL:** <https://forums.freertos.org/t/stm32f429discovery-with-stm32cube-usb-fatfs-and-freertos/671>\
**Category:** Kernel\
**Created:** [September 9, 2015, 8:57am UTC](https://forums.freertos.org/t/stm32f429discovery-with-stm32cube-usb-fatfs-and-freertos/671 "2015-09-09T08:57:04Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 9, 2015, 8:57am UTC](https://forums.freertos.org/t/stm32f429discovery-with-stm32cube-usb-fatfs-and-freertos/671/1 "2015-09-09T08:57:04Z")

</div>

**[thyanger](https://sourceforge.net/u/thyanger/)** wrote on [Wednesday, September 09, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/14fb7b28/#524b):

Hi,

I am developping an application to write something in a file over an USB key connected to the discovery board. A very basic example without FreeRTOS works well. This example is one of those provided by ST within Cube (STM32Cube\_FW\_F4\_V1.5.0\Projects\STM32F429I-Discovery\Applications\FatFs\FatFs\_USBDisk), which I rearranged to make it work with eclipse mars. This example, and also my application, uses the USB middleware provided within Cube. The USB is configured in HS as a host mass storage class (MSC) device.

Now, I have rearranged the code to use FreeRTOS but in this case it does not work. I am really confused by the USB middleware layer which I tried to investigate for debug purposes, so I know that also my help request is unclear. The code is organized as follow: there are one task and one queue. The task, namely userTask, is that which is in charge to write the file. The drivers are adapted to the use with an RTOS, being enough to check the flag USBH\_USE\_OS == 1 in a configuration file. By means of the driver initialization procedure, another task is created by the driver at startup, namely driverTask, which is in charge to detect the events occurring about the USB peripheral (i.e. connection, disconnection and so on). The userTask and the dirverTask communicates via the queue I have mentioned before. The driverTask communicates with the low level driver with other queues (by code inspection you can see this).

As an example I also gave a look at the code written for the evaluation board and provided within Cube (for the discovery there is no example with freertos+usbkey+fatfs).

So, the microcontroller starts with the USB key attached. During debug I noticed that both tasks are in blocking state, like they are waiting for something to happen. So I put a breakpoint to the IRQHanlder call and I have noticed that by repeatedly pressing the “play” button in eclipse, it always enters that piece of code as the interrupt is never served. However I checked the interrupt priority and everything looks ok.

At this point I really don’t know what to check since my lack of knowledge of this middleware (which I deem awful). Any help will be really appreciated.

Thank you and regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 9, 2015, 5:00pm UTC](https://forums.freertos.org/t/stm32f429discovery-with-stm32cube-usb-fatfs-and-freertos/671/2 "2015-09-09T17:00:57Z")

</div>

**[edwards3](https://sourceforge.net/u/edwards3/)** wrote on [Wednesday, September 09, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/14fb7b28/#6d74):

Sounds like the problem is not directly a freeRTOS one, but the interrupt being executed repeatedly might be the right behavior for USB. How often does the interrupt run in the working code?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 10, 2015, 8:07am UTC](https://forums.freertos.org/t/stm32f429discovery-with-stm32cube-usb-fatfs-and-freertos/671/3 "2015-09-10T08:07:04Z")

</div>

**[thyanger](https://sourceforge.net/u/thyanger/)** wrote on [Thursday, September 10, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/14fb7b28/#6d74/edbc):

Dear MEdwards,

thank you for the reply!

However I just found the problem. It seems that with heap\_4.c my code does not work. I changed heap\_4.c with heap\_3.c, clean and build and it works indeed. For doublecheck I tried again with heap\_4.c (clean and build) and it does not work.

Basicly I took a previous code I wrote which I knew it was working and then I started to compare with the non working one. Going further I copied all the code from the working one to the non working one and I finally end up with two folders with only one difference: the memory management scheme.

I am not an expert, really, so I don’t know if this is a bug or if some memory management schemes are not suited this particular purpose. What I know is that this worked for me.

I hope that this can help somebody else.

Regards.

---

<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:** [September 10, 2015, 8:56am UTC](https://forums.freertos.org/t/stm32f429discovery-with-stm32cube-usb-fatfs-and-freertos/671/4 "2015-09-10T08:56:30Z")

</div>

**[heinbali01](https://sourceforge.net/u/heinbali01/)** wrote on [Thursday, September 10, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/14fb7b28/#b406):

Hi Valerio,

As you probably know:

heap\_3 uses the standard `malloc()`/`free()` couple.

heap\_4 uses a fixed block of heap space, statically declared as:

```
uint8_t ucHeap[configTOTAL_HEAP_SIZE];

```

heap\_5 uses a series of non-contiguous memory blocks. It allows e.g. to use both internal SRAM as well as external SDRAM.

When using heap\_4 or heap\_5, make sure that `malloc()` and `free()` are not being called. Some library calls (`sprintf()`, `localtime()`) may use them.

Sometimes I declare them as follows, so that the linker will issue an error whenever they are called:

```auto
    /* Define a non-existent function. */
    extern void malloc_is_called( void );
    extern void free_is_called( void );
    void* malloc( size_t xWantedSize )
    {
        /* Call a non-existent function. */
        malloc_is_called();
        return NULL;
    }
    void free( void* pv )
    {
        /* Call a non-existent function. */
        free_is_called();
    }

```

Other developer redefine `malloc()`/`free()` as follows:

```auto
    /* Make a wrapper for malloc() and free().
    You can put breaks in a debugger here.
    Do not use this in combination with heap_3 :-) */
    void* malloc( size_t xWantedSize )
    {
        return pvPortMalloc( xWantedSize );
    }
    void free( void* pv )
    {
        vPortFree( pv );
    }

```

For C++ users, make sure that the above declarations get an `extern "C"` linking.

( I’m not sure if I like that mix of `malloc()` and `pvPortMalloc()`. \*/

Keep on using heap\_3 that calls `malloc()`/`free()` ? That is OK.

I tend to prefer heap\_4 and heap\_5 because they are:

- very fast
- they are easy to inspect while debugging
- easy to monitor ( `xPortGetMinimumEverFreeHeapSize()` / `xPortGetFreeHeapSize()` )
- easy to configure ( `configTOTAL_HEAP_SIZE` or `vPortDefineHeapRegions()` )

I once wrote a test for these two modules: allocating and freeing random amounts of memory. They both passed the test more than well.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 10, 2015, 9:02am UTC](https://forums.freertos.org/t/stm32f429discovery-with-stm32cube-usb-fatfs-and-freertos/671/5 "2015-09-10T09:02:10Z")

</div>

**[thyanger](https://sourceforge.net/u/thyanger/)** wrote on [Thursday, September 10, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/14fb7b28/#e257):

Thank you!

---

<div class="post-metadata">

**Author:** ![JacobApium](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jacobapium/32/2172_2.png) [@JacobApium](https://forums.freertos.org/u/JacobApium)\
**Post date:** [October 30, 2020, 2:31am UTC](https://forums.freertos.org/t/stm32f429discovery-with-stm32cube-usb-fatfs-and-freertos/671/6 "2020-10-30T02:31:00Z")

</div>

> [@htibosch](#):
>
> When using heap\_4 or heap\_5, make sure that `malloc()` and `free()` are not being called. Some library calls (`sprintf()`, `localtime()`) may use them.
> 
> Sometimes I declare them as follows, so that the linker will issue an error whenever they are called:
> 
> ```auto
> /* Define a non-existent function. */
> extern void malloc_is_called( void );
> extern void free_is_called( void );
> void* malloc( size_t xWantedSize )
> {
> /* Call a non-existent function. */
> malloc_is_called();
> return NULL;
> }
> void free( void* pv )
> {
> /* Call a non-existent function. */
> free_is_called();
> }
> 
> ```

Hein,

If you redefine malloc() and free() is it okay to use snprintf() with FreeRTOS? We have done this + also have a realloc() for FreeRTOS.

Jacob

---

<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:** [October 30, 2020, 2:57am UTC](https://forums.freertos.org/t/stm32f429discovery-with-stm32cube-usb-fatfs-and-freertos/671/7 "2020-10-30T02:57:06Z")

</div>

In some projects, I defined free()/malloc() directly.

There is another way, using ` __wrap_malloc` and `__ wrap_free`, see [this post](https://forum.atollic.com/viewtopic.php?t=1759).

I don’t think that `[vsn]printf` functions use the heap, but I have noticed that some implementations are very stack-hungry. I would never use `snprintf()` in an ISR context.

I personally like to use [`printf-stdarg.c`](https://github.com/htibosch/freertos_plus_projects/blob/master/plus/Framework/Utilities/printf-stdarg.c)  
That implementation is ISR-safe, and it has a small use of the stack. However, it does not implement floating point notations.

---

<div class="post-metadata">

**Author:** ![JacobApium](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jacobapium/32/2172_2.png) [@JacobApium](https://forums.freertos.org/u/JacobApium)\
**Post date:** [October 30, 2020, 3:38pm UTC](https://forums.freertos.org/t/stm32f429discovery-with-stm32cube-usb-fatfs-and-freertos/671/8 "2020-10-30T15:38:52Z")

</div>

Hein,

Thanks your your feedback. We have wrapped malloc and free as described in the link you provided. And indeed the printf that comes out of the box with STM32Cube does call malloc. We are using floating point with printf (though not in a lot of places). My question is if we have wrapped malloc around FreeRTOS vpPortMalloc do you think its safe to use printf that calls malloc with FreeRTOS?

Thanks,

Jacob

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [October 30, 2020, 3:59pm UTC](https://forums.freertos.org/t/stm32f429discovery-with-stm32cube-usb-fatfs-and-freertos/671/9 "2020-10-30T15:59:26Z")

</div>

With thread-safe malloc/free it’s ok to use the default C-library [vsn]printf functions in multiple FreeRTOS tasks 👍

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [October 30, 2020, 5:16pm UTC](https://forums.freertos.org/t/stm32f429discovery-with-stm32cube-usb-fatfs-and-freertos/671/10 "2020-10-30T17:16:19Z")

</div>

BTW You might also have a look at [Dave Nadler’s work](http://www.nadler.com/embedded/newlibAndFreeRTOS.html) for some further, interesting details regarding malloc, newlib and FreeRTOS.

---

<div class="post-metadata">

**Author:** ![JacobApium](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/jacobapium/32/2172_2.png) [@JacobApium](https://forums.freertos.org/u/JacobApium)\
**Post date:** [October 30, 2020, 5:26pm UTC](https://forums.freertos.org/t/stm32f429discovery-with-stm32cube-usb-fatfs-and-freertos/671/11 "2020-10-30T17:26:10Z")

</div>

Thanks, I have looked at Dave’s worked and tested out his solution but choose to remap malloc and alloc because you loose the use of xPortGetMinimumEverFreeHeapSize()

> <https://github.com/DRNadler/FreeRTOS_helpers/issues/4>

Jacob
