Problems with FreeRTOS

Working libraries, libraries being ported and related hardware
Post Reply
Posts: 66
Joined: Thu Mar 19, 2020 9:32 am

Problems with FreeRTOS

Post by Phono »

The FreeRTOS library allows to add a config file at the sketch level. This is very convenient. However, there are settings that pose problems, and I would be pleased if someone could explain me the reason.
I am using a generic STM32F103 board, with a STM32F103RBT6 chip.
I have set the options in the config file at the bottom of this post.
I have a problem with malloc.
With Roger's core and the FreeRTOS821 library, I used to put in the file heap_1.c in the utility directory. This is the minimal implementation, including a malloc but no free.
In version 9 for STM32Duino, there is only one line about the heap options, which is :

Code: Select all

/* Begin custom definitions for STM32 */
/* Define memory allocation implementations to use:
 * 1 to 5 for heap_[1-5].c
 * -1 for heap_useNewlib.c
 * Default -1 see heap.c
/*#define configMEMMANG_HEAP_NB             3*/
/* End custom definitions for STM32 */
However, if i uncomment the #define and define it as 1, I get the following compile error:

Code: Select all

In file included from D:\STM32DuinoPortable\SketchesSTM32Duino\libraries\STM32duino_FreeRTOS\src\heap.c:21:
d:\stm32duinoportable\sketchesstm32duino\libraries\stm32duino_freertos\portable\memmang\heap_1.c:104:17: error: variably modified 'ucHeap' at file scope
  104 |  static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
      |                 ^~~~~~
So, how can I select the minimal option for the heap handling?

The config file is the following:

Code: Select all

    FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
    All rights reserved


    This file is part of the FreeRTOS distribution.

    FreeRTOS is free software; you can redistribute it and/or modify it under
    the terms of the GNU General Public License (version 2) as published by the
    Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.

    >>!   NOTE: The modification to the GPL is included to allow you to     !<<
    >>!   distribute a combined work that includes FreeRTOS without being   !<<
    >>!   obliged to provide the source code for proprietary components     !<<
    >>!   outside of the FreeRTOS kernel.                                   !<<

    FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    FOR A PARTICULAR PURPOSE.  Full license text is available on the following

     *                                                                       *
     *    FreeRTOS provides completely free yet professionally developed,    *
     *    robust, strictly quality controlled, supported, and cross          *
     *    platform software that is more than just the market leader, it     *
     *    is the industry's de facto standard.                               *
     *                                                                       *
     *    Help yourself get started quickly while simultaneously helping     *
     *    to support the FreeRTOS project by purchasing a FreeRTOS           *
     *    tutorial book, reference manual, or both:                          *
     *                              *
     *                                                                       *
    *************************************************************************** - Having a problem?  Start by reading
    the FAQ page "My application does not run, what could be wrong?".  Have you
    defined configASSERT()? - In return for receiving this top quality
    embedded software for free we request you assist our global community by
    participating in the support forum. - Investing in training allows your team to
    be as productive as possible as early as possible.  Now you can receive
    FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
    Ltd, and the world's leading authority on the world's leading RTOS. - A selection of FreeRTOS ecosystem products,
    including FreeRTOS+Trace - an indispensable productivity tool, a DOS
    compatible FAT file system, and our tiny thread aware UDP/IP stack. - Where new FreeRTOS products go to incubate.
    Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS. - Real Time Engineers ltd. license FreeRTOS to High
    Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS
    licenses offer ticketed support, indemnification and commercial middleware. - High Integrity Systems also provide a safety
    engineered and independently SIL3 certified version for use in safety and
    mission critical applications that require provable dependability.

    1 tab == 4 spaces!


 * Application specific definitions.
 * These definitions should be adjusted for your particular hardware and
 * application requirements.
 * See

/* Begin custom definitions for STM32 */
/* Define memory allocation implementations to use:
 * 1 to 5 for heap_[1-5].c
 * -1 for heap_useNewlib.c
 * Default -1 see heap.c
/*#define configMEMMANG_HEAP_NB             3*/
/* End custom definitions for STM32 */
#define /*#define configMEMMANG_HEAP_NB             3*/
             1   // !!!!!!!!!!!!!

/* Ensure stdint is only used by the compiler, and not the assembler. */
#if defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__)
 #include <stdint.h>
 extern uint32_t SystemCoreClock;
extern char _end; /* Defined in the linker script */
extern char _estack; /* Defined in the linker script */
extern char _Min_Stack_Size; /* Defined in the linker script */

#define configUSE_PREEMPTION              0
#define configUSE_IDLE_HOOK               0
#define configUSE_TICK_HOOK               0
#define configCPU_CLOCK_HZ                (SystemCoreClock)
#define configTICK_RATE_HZ                ((TickType_t)1000)
#define configMAX_PRIORITIES              (7)
 * _Min_Stack_Size is often set to 0x400 in the linker script
 * Use it divided by 8 to set minmimal stack size of a task to 128 by default.
 * End user will have to properly configure those value depending to their needs.
#define configMINIMAL_STACK_SIZE          ((uint16_t)((uint32_t)&_Min_Stack_Size/8))
#define configTOTAL_HEAP_SIZE             ((size_t)(&_estack - _Min_Stack_Size - &_end))
#define configMAX_TASK_NAME_LEN           (16)
#define configUSE_TRACE_FACILITY          1
#define configUSE_16_BIT_TICKS            0
#define configIDLE_SHOULD_YIELD           1
#define configUSE_MUTEXES                 1
#define configQUEUE_REGISTRY_SIZE         8
#define configCHECK_FOR_STACK_OVERFLOW    0
#define configUSE_RECURSIVE_MUTEXES       1
#define configUSE_MALLOC_FAILED_HOOK      0
#define configUSE_APPLICATION_TASK_TAG    0
#define configUSE_COUNTING_SEMAPHORES     1
#define configGENERATE_RUN_TIME_STATS     0
 * If configUSE_NEWLIB_REENTRANT is set to 1 then a newlib reent structure
 * will be allocated for each created task.
 * Note Newlib support has been included by popular demand, but is not used
 * by the FreeRTOS maintainers themselves. FreeRTOS is not responsible for
 * resulting newlib operation. User must be familiar with newlib and must
 * provide system-wide implementations of the necessary stubs.
 * Be warned that (at the time of writing) the current newlib design implements
 * a system-wide malloc() that must be provided with locks.
#define configUSE_NEWLIB_REENTRANT        0

/* Co-routine definitions. */
#define configUSE_CO_ROUTINES           0

/* Software timer definitions. */
#define configUSE_TIMERS             1
#define configTIMER_TASK_PRIORITY    (2)
#define configTIMER_QUEUE_LENGTH     10

/* Set the following definitions to 1 to include the API function, or zero
to exclude the API function. */
#define INCLUDE_vTaskPrioritySet       1
#define INCLUDE_uxTaskPriorityGet      1
#define INCLUDE_vTaskDelete            1
#define INCLUDE_vTaskCleanUpResources  1
#define INCLUDE_vTaskSuspend           1
#define INCLUDE_vTaskDelayUntil        1
#define INCLUDE_vTaskDelay             1
#define INCLUDE_xTaskGetSchedulerState 1
#define INCLUDE_uxTaskGetStackHighWaterMark 1
#define INCLUDE_xTaskGetIdleTaskHandle 1

/* Cortex-M specific definitions. */
 /* __BVIC_PRIO_BITS will be specified when CMSIS is being used. */
 #define configPRIO_BITS         __NVIC_PRIO_BITS
 #define configPRIO_BITS         4        /* 15 priority levels */

/* The lowest interrupt priority that can be used in a call to a "set priority"
function. */

/* The highest interrupt priority that can be used by any interrupt service
routine that makes calls to interrupt safe FreeRTOS API functions.  DO NOT CALL
PRIORITY THAN THIS! (higher priorities are lower numeric values. */

/* Interrupt priorities used by the kernel port layer itself.  These are generic
to all Cortex-M ports, and do not rely on any particular library functions. */
/* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!
See */

/* Normal assert() semantics without relying on the provision of an assert.h
header file. */
#define configASSERT( x ) if( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); for( ;; ); } 

/* Definitions that map the FreeRTOS port interrupt handlers to their CMSIS
   standard names. */
#define vPortSVCHandler    SVC_Handler
#define xPortPendSVHandler PendSV_Handler

/* IMPORTANT: This define MUST be commented when used with STM32Cube firmware,
              to prevent overwriting SysTick_Handler defined within STM32Cube HAL */
/* #define xPortSysTickHandler SysTick_Handler */

#endif /* FREERTOS_CONFIG_H */
User avatar
Posts: 1227
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 60
Location: Le Mans

Re: Problems with FreeRTOS

Post by fpiSTM »

Hi phono,

sorry for the delay, I've forgot to answer this :?
In fact, the configTOTAL_HEAP_SIZE is not, by default, a constant and relies on some variable:

Code: Select all

#define configTOTAL_HEAP_SIZE             ((size_t)(&_estack - _Min_Stack_Size - &_end))
but with config heap 1, this definition is used to allocate an array:

Code: Select all

static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
but it is not permits to use a variable to allocate statically an array, that's why you get this error:
error: variably modified 'ucHeap' at file scope
in your custom config you have to use a constant value:

Code: Select all

-#define configTOTAL_HEAP_SIZE             ((size_t)(&_estack - _Min_Stack_Size - &_end))
+#define configTOTAL_HEAP_SIZE             ((size_t)(15 * 1024))
I did not check if this is a good value for your board, you have to properly compute it, anyway with this, I'm able to build and run one of the example using memmang 1.
Post Reply

Return to “Libraries & Hardware”