FreeRTOS Porting Description

Port

The current version of FreeRTOS can be downloaded from www.freertos.org. For porting, we need the folder <FreeRTOS/Source> and one of the portables ARM_CM3, ARM_CM3_MPU, ARM_CM4FP, ARM_CM4_MPU. The following table shows the supported features of each portable.

PortableFPUMPU
ARM_CM3(error)

(error)

ARM_CM3_MPU(error)(tick)
ARM_CM4FP(tick)(error)
ARM_CM4_MPU(tick)(tick)

Simply copy the required port.c/ portmacro.h file into the folders Source/ Include.

FreeRTOS Config

The FreeRTOSConfig file customizes your FreeRTOS port and must be defined according to your needs. FreeRTOS already provides a lot of information here.

Heap

FreeRTOS provides the mentioned five heap implementations. Since none of those support newlib c, we also included Dave Nadlers newlib heap implementation.

The required heap needs to be specified in the target wscript, e.g.:

PendSV and SVC Hanlder

To perform a task switch, FreeRTOS utilizes the CMSIS Pendable Service (PendSV) and the supervisor call (SVC) handlers.

They must not be implemented somewhere else!

FreeRTOSCommonHooks

The following Hook Functions are required:

FreeRTOSCommonHooks.h
/**
 * @brief Delay for the specified number of milliSeconds
 * @param ms: Delay in milliSeconds
 * @return Nothing
 * @note Delays the specified number of milliSeoconds using a task delay
 */
void FreeRTOSDelay(uint32_t ms);

/**
 * @brief FreeRTOS malloc fail hook
 * @return Nothing
 * @note This function is alled when a malloc fails to allocate data.
 */
void vApplicationMallocFailedHook(void);

/**
 * @brief FreeRTOS remalloc fail hook
 * @return  Nothing
 * @note This function is alled when a malloc fails to allocate data.
 */
void vApplicationReallocFailedHook(void);

/**
 * @brief FreeRTOS application idle hook
 * @return Nothing
 * @note Calls ARM Wait for Interrupt function to idle core
 */
void vApplicationIdleHook(void);
FreeRTOSCommonHooks.c
/* Delay for the specified number of milliSeconds */
void FreeRTOSDelay(uint32_t ms)
{
  portTickType xDelayTime;

  xDelayTime = xTaskGetTickCount();
  vTaskDelayUntil(&xDelayTime, ms);
}
/*-----------------------------------------------------------*/

/* FreeRTOS malloc fail hook */
void vApplicationMallocFailedHook(void)
{
  /* vApplicationMallocFailedHook() will only be called if
    configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook
    function that will get called if a call to pvPortMalloc() fails.
    pvPortMalloc() is called internally by the kernel whenever a task, queue,
    timer or semaphore is created.  It is also called by various parts of the
    demo application.  If heap_1.c or heap_2.c are used, then the size of the
    heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
    FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
    to query the size of free heap space that remains (although it does not
    provide information on how the remaining heap might be fragmented). */

  taskDISABLE_INTERRUPTS();
  for (;; ) {}
}
/*-----------------------------------------------------------*/

/* FreeRTOS malloc fail hook */
void vApplicationReallocFailedHook(void)
{
  /* vApplicationMallocFailedHook() will only be called if
    configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook
    function that will get called if a call to pvPortMalloc() fails.
    pvPortMalloc() is called internally by the kernel whenever a task, queue,
    timer or semaphore is created.  It is also called by various parts of the
    demo application.  If heap_1.c or heap_2.c are used, then the size of the
    heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
    FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
    to query the size of free heap space that remains (although it does not
    provide information on how the remaining heap might be fragmented). */

  taskDISABLE_INTERRUPTS();
  for (;; ) {}
}
/*-----------------------------------------------------------*/

/* FreeRTOS application idle hook */
void vApplicationIdleHook(void)
{
  /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
  to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle
  task.  It is essential that code added to this hook function never attempts
  to block in any way (for example, call xQueueReceive() with a block time
  specified, or call vTaskDelay()).  If the application makes use of the
  vTaskDelete() API function (as this demo application does) then it is also
  important that vApplicationIdleHook() is permitted to return to its calling
  function, because it is the responsibility of the idle task to clean up
  memory allocated by the kernel to any task that has since been deleted. */

  /* Best to sleep here until next systick */
  __WFI();
}

/* FreeRTOS stack overflow hook */
void vApplicationStackOverflowHook(xTaskHandle pxTask, char * pcTaskName)
{
  (void) pxTask;
  (void) pcTaskName;

  //DEBUGOUT("DIE:ERROR:FreeRTOS: Stack overflow in task %s\r\n", pcTaskName);

  /* Run time stack overflow checking is performed if
     configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook
     function is called if a stack overflow is detected. */
  taskDISABLE_INTERRUPTS();
  for (;; ) {}
}

/* FreeRTOS application tick hook */
void vApplicationTickHook(void)
{
  /* This function will be called by each tick interrupt if
  configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be
  added here, but the tick hook is called from an interrupt context, so
  code must not attempt to block, and only the interrupt safe FreeRTOS API
  functions can be used (those that end in FromISR()). */
#ifdef DRV_TIM_MODULE_ENABLED
  DRVSysTick_Handler();
#endif


/* configSUPPORT_STATIC_ALLOCATION is set to 1, so the application must provide an
implementation of vApplicationGetIdleTaskMemory() to provide the memory that is
used by the Idle task. */
void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer,
                                    StackType_t **ppxIdleTaskStackBuffer,
                                    uint32_t *pulIdleTaskStackSize )
{
/* If the buffers to be provided to the Idle task are declared inside this
function then they must be declared static - otherwise they will be allocated on
the stack and so not exists after this function exits. */
static StaticTask_t xIdleTaskTCB;
static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];

    /* Pass out a pointer to the StaticTask_t structure in which the Idle task's
    state will be stored. */
    *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;

    /* Pass out the array that will be used as the Idle task's stack. */
    *ppxIdleTaskStackBuffer = uxIdleTaskStack;

    /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.
    Note that, as the array is necessarily of type StackType_t,
    configMINIMAL_STACK_SIZE is specified in words, not bytes. */
    *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
}
/*-----------------------------------------------------------*/

/* configSUPPORT_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the
application must provide an implementation of vApplicationGetTimerTaskMemory()
to provide the memory that is used by the Timer service task. */
void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer,
                                     StackType_t **ppxTimerTaskStackBuffer,
                                     uint32_t *pulTimerTaskStackSize )
{
/* If the buffers to be provided to the Timer task are declared inside this
function then they must be declared static - otherwise they will be allocated on
the stack and so not exists after this function exits. */
static StaticTask_t xTimerTaskTCB;
static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];

    /* Pass out a pointer to the StaticTask_t structure in which the Timer
    task's state will be stored. */
    *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;

    /* Pass out the array that will be used as the Timer task's stack. */
    *ppxTimerTaskStackBuffer = uxTimerTaskStack;

    /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.
    Note that, as the array is necessarily of type StackType_t,
    configTIMER_TASK_STACK_DEPTH is specified in words, not bytes. */
    *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
}

Note that the vApplicationTickHook() calls the netX Driver Systick Handler in case the timer module is enabled!

This is required since the timer implementation of the netX driver might need the systick!