]> granicus.if.org Git - esp-idf/commitdiff
Backport the static allocation feature from FreeRTOS V9.0.0
authordaniel <daniel@pycom.io>
Thu, 22 Sep 2016 04:20:56 +0000 (12:20 +0800)
committerdaniel <daniel@pycom.io>
Thu, 22 Sep 2016 05:41:21 +0000 (13:41 +0800)
This feature allows to use static buffers (or from a pool of memory which is not
controlled by FreeRTOS).
In order to reduce the impact of the changes, the static feature has only been added
to the queus (and in consequence to the semaphores and the mutexes) and the tasks.
The Timer task is always dynamically allocated and also the idle task(s), which in the
case of the ESP-IDF is ok, since we always need to have dynamic allocation enabled.

components/freertos/include/freertos/FreeRTOS.h
components/freertos/include/freertos/FreeRTOSConfig.h
components/freertos/include/freertos/queue.h
components/freertos/include/freertos/semphr.h
components/freertos/include/freertos/task.h
components/freertos/queue.c
components/freertos/tasks.c

index 04b39b65e9298016481b1dc5f5cce50bc038f69c..f6c9aa497d9fea259e9b0caf7c332554afc5a832 100644 (file)
@@ -74,6 +74,7 @@
  * Include the generic headers required for the FreeRTOS port being used.
  */
 #include <stddef.h>
+#include "sys/reent.h"
 
 /*
  * If stdint.h cannot be located then:
@@ -739,6 +740,20 @@ extern "C" {
        #define portTICK_TYPE_IS_ATOMIC 0
 #endif
 
+#ifndef configSUPPORT_STATIC_ALLOCATION
+       /* Defaults to 0 for backward compatibility. */
+       #define configSUPPORT_STATIC_ALLOCATION 0
+#endif
+
+#ifndef configSUPPORT_DYNAMIC_ALLOCATION
+       /* Defaults to 1 for backward compatibility. */
+       #define configSUPPORT_DYNAMIC_ALLOCATION 1
+#endif
+
+#if( ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 0 ) )
+       #error configSUPPORT_STATIC_ALLOCATION and configSUPPORT_DYNAMIC_ALLOCATION cannot both be 0, but can both be 1.
+#endif
+
 #if( portTICK_TYPE_IS_ATOMIC == 0 )
        /* Either variables of tick type cannot be read atomically, or
        portTICK_TYPE_IS_ATOMIC was not set - map the critical sections used when
@@ -791,6 +806,153 @@ V8 if desired. */
        #define configESP32_PER_TASK_DATA 1
 #endif
 
+/*
+ * In line with software engineering best practice, FreeRTOS implements a strict
+ * data hiding policy, so the real structures used by FreeRTOS to maintain the
+ * state of tasks, queues, semaphores, etc. are not accessible to the application
+ * code.  However, if the application writer wants to statically allocate such
+ * an object then the size of the object needs to be know.  Dummy structures
+ * that are guaranteed to have the same size and alignment requirements of the
+ * real objects are used for this purpose.  The dummy list and list item
+ * structures below are used for inclusion in such a dummy structure.
+ */
+struct xSTATIC_LIST_ITEM
+{
+       TickType_t xDummy1;
+       void *pvDummy2[ 4 ];
+};
+typedef struct xSTATIC_LIST_ITEM StaticListItem_t;
+
+/* See the comments above the struct xSTATIC_LIST_ITEM definition. */
+struct xSTATIC_MINI_LIST_ITEM
+{
+       TickType_t xDummy1;
+       void *pvDummy2[ 2 ];
+};
+typedef struct xSTATIC_MINI_LIST_ITEM StaticMiniListItem_t;
+
+/* See the comments above the struct xSTATIC_LIST_ITEM definition. */
+typedef struct xSTATIC_LIST
+{
+       UBaseType_t uxDummy1;
+       void *pvDummy2;
+       StaticMiniListItem_t xDummy3;
+} StaticList_t;
+
+/*
+ * In line with software engineering best practice, especially when supplying a
+ * library that is likely to change in future versions, FreeRTOS implements a
+ * strict data hiding policy.  This means the Task structure used internally by
+ * FreeRTOS is not accessible to application code.  However, if the application
+ * writer wants to statically allocate the memory required to create a task then
+ * the size of the task object needs to be know.  The StaticTask_t structure
+ * below is provided for this purpose.  Its sizes and alignment requirements are
+ * guaranteed to match those of the genuine structure, no matter which
+ * architecture is being used, and no matter how the values in FreeRTOSConfig.h
+ * are set.  Its contents are somewhat obfuscated in the hope users will
+ * recognise that it would be unwise to make direct use of the structure members.
+ */
+typedef struct xSTATIC_TCB
+{
+       void                            *pxDummy1;
+       #if ( portUSING_MPU_WRAPPERS == 1 )
+               xMPU_SETTINGS   xDummy2;
+       #endif
+       StaticListItem_t        xDummy3[ 2 ];
+       UBaseType_t                     uxDummy5;
+       void                            *pxDummy6;
+       uint8_t                         ucDummy7[ configMAX_TASK_NAME_LEN ];
+    UBaseType_t                        uxDummyCoreId;
+       #if ( portSTACK_GROWTH > 0 )
+               void                    *pxDummy8;
+       #endif
+       #if ( portCRITICAL_NESTING_IN_TCB == 1 )
+               UBaseType_t             uxDummy9;
+        uint32_t        OldInterruptState;
+       #endif
+       #if ( configUSE_TRACE_FACILITY == 1 )
+               UBaseType_t             uxDummy10[ 2 ];
+       #endif
+       #if ( configUSE_MUTEXES == 1 )
+               UBaseType_t             uxDummy12[ 2 ];
+       #endif
+       #if ( configUSE_APPLICATION_TASK_TAG == 1 )
+               void                    *pxDummy14;
+       #endif
+       #if( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
+               void                    *pvDummy15[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
+    #if ( configTHREAD_LOCAL_STORAGE_DELETE_CALLBACKS )
+               void                    *pvDummyLocalStorageCallBack[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
+       #endif
+       #endif
+       #if ( configGENERATE_RUN_TIME_STATS == 1 )
+               uint32_t                ulDummy16;
+       #endif
+       #if ( configUSE_NEWLIB_REENTRANT == 1 )
+               struct  _reent  xDummy17;
+       #endif
+       #if ( configUSE_TASK_NOTIFICATIONS == 1 )
+               uint32_t                ulDummy18;
+               uint32_t                ucDummy19;
+       #endif
+       #if( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
+               uint8_t                 uxDummy20;
+       #endif
+
+} StaticTask_t;
+
+/*
+ * In line with software engineering best practice, especially when supplying a
+ * library that is likely to change in future versions, FreeRTOS implements a
+ * strict data hiding policy.  This means the Queue structure used internally by
+ * FreeRTOS is not accessible to application code.  However, if the application
+ * writer wants to statically allocate the memory required to create a queue
+ * then the size of the queue object needs to be know.  The StaticQueue_t
+ * structure below is provided for this purpose.  Its sizes and alignment
+ * requirements are guaranteed to match those of the genuine structure, no
+ * matter which architecture is being used, and no matter how the values in
+ * FreeRTOSConfig.h are set.  Its contents are somewhat obfuscated in the hope
+ * users will recognise that it would be unwise to make direct use of the
+ * structure members.
+ */
+typedef struct xSTATIC_QUEUE
+{
+       void *pvDummy1[ 3 ];
+
+       union
+       {
+               void *pvDummy2;
+               UBaseType_t uxDummy2;
+       } u;
+
+       StaticList_t xDummy3[ 2 ];
+       UBaseType_t uxDummy4[ 3 ];
+       BaseType_t ucDummy5[ 2 ];
+
+       #if( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
+               uint8_t ucDummy6;
+       #endif
+
+       #if ( configUSE_QUEUE_SETS == 1 )
+               void *pvDummy7;
+       #endif
+
+       #if ( configUSE_TRACE_FACILITY == 1 )
+               UBaseType_t uxDummy8;
+               uint8_t ucDummy9;
+       #endif
+
+    struct {
+           volatile uint32_t mux;
+    #ifdef CONFIG_FREERTOS_PORTMUX_DEBUG
+           const char *lastLockedFn;
+           int lastLockedLine;
+    #endif
+    } mux;
+
+} StaticQueue_t;
+typedef StaticQueue_t StaticSemaphore_t;
+
 #ifdef __cplusplus
 }
 #endif
index 089d799ddafb24586778c7ea1d8b4a50dd6c9dad..f3f2df73cf2185986ea3fb5164769d25475ce3c4 100644 (file)
 
 #define configUSE_NEWLIB_REENTRANT             1
 
+#define configSUPPORT_DYNAMIC_ALLOCATION    1
+
 /* Test FreeRTOS timers (with timer task) and more. */
 /* Some files don't compile if this flag is disabled */
 #define configUSE_TIMERS                    1
index 2095c59b0cb99b95ed6a81b6afdd45bfb116f2bc..876f1a1b30b6fdbb8477f88aaf657e86b3519516 100644 (file)
@@ -170,7 +170,95 @@ typedef void * QueueSetMemberHandle_t;
  * \defgroup xQueueCreate xQueueCreate
  * \ingroup QueueManagement
  */
-#define xQueueCreate( uxQueueLength, uxItemSize ) xQueueGenericCreate( uxQueueLength, uxItemSize, queueQUEUE_TYPE_BASE )
+#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
+       #define xQueueCreate( uxQueueLength, uxItemSize ) xQueueGenericCreate( ( uxQueueLength ), ( uxItemSize ), ( queueQUEUE_TYPE_BASE ) )
+#endif
+
+/**
+ * queue. h
+ * <pre>
+ QueueHandle_t xQueueCreateStatic(
+                                                         UBaseType_t uxQueueLength,
+                                                         UBaseType_t uxItemSize,
+                                                         uint8_t *pucQueueStorageBuffer,
+                                                         StaticQueue_t *pxQueueBuffer
+                                                 );
+ * </pre>
+ *
+ * Creates a new queue instance, and returns a handle by which the new queue
+ * can be referenced.
+ *
+ * Internally, within the FreeRTOS implementation, queues use two blocks of
+ * memory.  The first block is used to hold the queue's data structures.  The
+ * second block is used to hold items placed into the queue.  If a queue is
+ * created using xQueueCreate() then both blocks of memory are automatically
+ * dynamically allocated inside the xQueueCreate() function.  (see
+ * http://www.freertos.org/a00111.html).  If a queue is created using
+ * xQueueCreateStatic() then the application writer must provide the memory that
+ * will get used by the queue.  xQueueCreateStatic() therefore allows a queue to
+ * be created without using any dynamic memory allocation.
+ *
+ * http://www.FreeRTOS.org/Embedded-RTOS-Queues.html
+ *
+ * @param uxQueueLength The maximum number of items that the queue can contain.
+ *
+ * @param uxItemSize The number of bytes each item in the queue will require.
+ * Items are queued by copy, not by reference, so this is the number of bytes
+ * that will be copied for each posted item.  Each item on the queue must be
+ * the same size.
+ *
+ * @param pucQueueStorageBuffer If uxItemSize is not zero then
+ * pucQueueStorageBuffer must point to a uint8_t array that is at least large
+ * enough to hold the maximum number of items that can be in the queue at any
+ * one time - which is ( uxQueueLength * uxItemsSize ) bytes.  If uxItemSize is
+ * zero then pucQueueStorageBuffer can be NULL.
+ *
+ * @param pxQueueBuffer Must point to a variable of type StaticQueue_t, which
+ * will be used to hold the queue's data structure.
+ *
+ * @return If the queue is created then a handle to the created queue is
+ * returned.  If pxQueueBuffer is NULL then NULL is returned.
+ *
+ * Example usage:
+   <pre>
+ struct AMessage
+ {
+       char ucMessageID;
+       char ucData[ 20 ];
+ };
+
+ #define QUEUE_LENGTH 10
+ #define ITEM_SIZE sizeof( uint32_t )
+
+ // xQueueBuffer will hold the queue structure.
+ StaticQueue_t xQueueBuffer;
+
+ // ucQueueStorage will hold the items posted to the queue.  Must be at least
+ // [(queue length) * ( queue item size)] bytes long.
+ uint8_t ucQueueStorage[ QUEUE_LENGTH * ITEM_SIZE ];
+
+ void vATask( void *pvParameters )
+ {
+ QueueHandle_t xQueue1;
+
+       // Create a queue capable of containing 10 uint32_t values.
+       xQueue1 = xQueueCreate( QUEUE_LENGTH, // The number of items the queue can hold.
+                                                       ITEM_SIZE         // The size of each item in the queue
+                                                       &( ucQueueStorage[ 0 ] ), // The buffer that will hold the items in the queue.
+                                                       &xQueueBuffer ); // The buffer that will hold the queue structure.
+
+       // The queue is guaranteed to be created successfully as no dynamic memory
+       // allocation is used.  Therefore xQueue1 is now a handle to a valid queue.
+
+       // ... Rest of task code.
+ }
+ </pre>
+ * \defgroup xQueueCreateStatic xQueueCreateStatic
+ * \ingroup QueueManagement
+ */
+#if( configSUPPORT_STATIC_ALLOCATION == 1 )
+       #define xQueueCreateStatic( uxQueueLength, uxItemSize, pucQueueStorage, pxQueueBuffer ) xQueueGenericCreateStatic( ( uxQueueLength ), ( uxItemSize ), ( pucQueueStorage ), ( pxQueueBuffer ), ( queueQUEUE_TYPE_BASE ) )
+#endif /* configSUPPORT_STATIC_ALLOCATION */
 
 /**
  * queue. h
@@ -1479,7 +1567,9 @@ BaseType_t xQueueCRReceive( QueueHandle_t xQueue, void *pvBuffer, TickType_t xTi
  * these functions directly.
  */
 QueueHandle_t xQueueCreateMutex( const uint8_t ucQueueType ) PRIVILEGED_FUNCTION;
+QueueHandle_t xQueueCreateMutexStatic( const uint8_t ucQueueType, StaticQueue_t *pxStaticQueue ) PRIVILEGED_FUNCTION;
 QueueHandle_t xQueueCreateCountingSemaphore( const UBaseType_t uxMaxCount, const UBaseType_t uxInitialCount ) PRIVILEGED_FUNCTION;
+QueueHandle_t xQueueCreateCountingSemaphoreStatic( const UBaseType_t uxMaxCount, const UBaseType_t uxInitialCount, StaticQueue_t *pxStaticQueue ) PRIVILEGED_FUNCTION;
 void* xQueueGetMutexHolder( QueueHandle_t xSemaphore ) PRIVILEGED_FUNCTION;
 
 /*
@@ -1538,10 +1628,22 @@ BaseType_t xQueueGiveMutexRecursive( QueueHandle_t pxMutex ) PRIVILEGED_FUNCTION
 #endif
 
 /*
- * Generic version of the queue creation function, which is in turn called by
- * any queue, semaphore or mutex creation function or macro.
+ * Generic version of the function used to creaet a queue using dynamic memory
+ * allocation.  This is called by other functions and macros that create other
+ * RTOS objects that use the queue structure as their base.
+ */
+#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
+       QueueHandle_t xQueueGenericCreate( const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize, const uint8_t ucQueueType ) PRIVILEGED_FUNCTION;
+#endif
+
+/*
+ * Generic version of the function used to creaet a queue using dynamic memory
+ * allocation.  This is called by other functions and macros that create other
+ * RTOS objects that use the queue structure as their base.
  */
-QueueHandle_t xQueueGenericCreate( const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize, const uint8_t ucQueueType ) PRIVILEGED_FUNCTION;
+#if( configSUPPORT_STATIC_ALLOCATION == 1 )
+       QueueHandle_t xQueueGenericCreateStatic( const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize, uint8_t *pucQueueStorage, StaticQueue_t *pxStaticQueue, const uint8_t ucQueueType ) PRIVILEGED_FUNCTION;
+#endif
 
 /*
  * Queue sets provide a mechanism to allow a task to block (pend) on a read
index 5866ab1ec54d884f43a64edcd9de521420063e7a..6343d0190a0ea264cbfb25d7fc5f7a0ff604de24 100644 (file)
@@ -128,19 +128,37 @@ typedef QueueHandle_t SemaphoreHandle_t;
  * \defgroup vSemaphoreCreateBinary vSemaphoreCreateBinary
  * \ingroup Semaphores
  */
-#define vSemaphoreCreateBinary( xSemaphore )                                                                                                                                                                                   \
-       {                                                                                                                                                                                                                                                                       \
-               ( xSemaphore ) = xQueueGenericCreate( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_BINARY_SEMAPHORE );    \
-               if( ( xSemaphore ) != NULL )                                                                                                                                                                                                    \
-               {                                                                                                                                                                                                                                                               \
-                       ( void ) xSemaphoreGive( ( xSemaphore ) );                                                                                                                                                                      \
-               }                                                                                                                                                                                                                                                               \
-       }
+#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
+       #define vSemaphoreCreateBinary( xSemaphore )                                                                                                                                                                                    \
+               {                                                                                                                                                                                                                                                                       \
+                       ( xSemaphore ) = xQueueGenericCreate( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_BINARY_SEMAPHORE );    \
+                       if( ( xSemaphore ) != NULL )                                                                                                                                                                                                    \
+                       {                                                                                                                                                                                                                                                               \
+                               ( void ) xSemaphoreGive( ( xSemaphore ) );                                                                                                                                                                      \
+                       }                                                                                                                                                                                                                                                               \
+               }
+#endif
 
 /**
  * semphr. h
  * <pre>SemaphoreHandle_t xSemaphoreCreateBinary( void )</pre>
  *
+ * Creates a new binary semaphore instance, and returns a handle by which the
+ * new semaphore can be referenced.
+ *
+ * In many usage scenarios it is faster and more memory efficient to use a
+ * direct to task notification in place of a binary semaphore!
+ * http://www.freertos.org/RTOS-task-notifications.html
+ *
+ * Internally, within the FreeRTOS implementation, binary semaphores use a block
+ * of memory, in which the semaphore structure is stored.  If a binary semaphore
+ * is created using xSemaphoreCreateBinary() then the required memory is
+ * automatically dynamically allocated inside the xSemaphoreCreateBinary()
+ * function.  (see http://www.freertos.org/a00111.html).  If a binary semaphore
+ * is created using xSemaphoreCreateBinaryStatic() then the application writer
+ * must provide the memory.  xSemaphoreCreateBinaryStatic() therefore allows a
+ * binary semaphore to be created without using any dynamic memory allocation.
+ *
  * The old vSemaphoreCreateBinary() macro is now deprecated in favour of this
  * xSemaphoreCreateBinary() function.  Note that binary semaphores created using
  * the vSemaphoreCreateBinary() macro are created in a state such that the
@@ -182,7 +200,68 @@ typedef QueueHandle_t SemaphoreHandle_t;
  * \defgroup vSemaphoreCreateBinary vSemaphoreCreateBinary
  * \ingroup Semaphores
  */
-#define xSemaphoreCreateBinary() xQueueGenericCreate( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_BINARY_SEMAPHORE )
+#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
+       #define xSemaphoreCreateBinary() xQueueGenericCreate( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_BINARY_SEMAPHORE )
+#endif
+
+/**
+ * semphr. h
+ * <pre>SemaphoreHandle_t xSemaphoreCreateBinaryStatic( StaticSemaphore_t *pxSemaphoreBuffer )</pre>
+ *
+ * Creates a new binary semaphore instance, and returns a handle by which the
+ * new semaphore can be referenced.
+ *
+ * NOTE: In many usage scenarios it is faster and more memory efficient to use a
+ * direct to task notification in place of a binary semaphore!
+ * http://www.freertos.org/RTOS-task-notifications.html
+ *
+ * Internally, within the FreeRTOS implementation, binary semaphores use a block
+ * of memory, in which the semaphore structure is stored.  If a binary semaphore
+ * is created using xSemaphoreCreateBinary() then the required memory is
+ * automatically dynamically allocated inside the xSemaphoreCreateBinary()
+ * function.  (see http://www.freertos.org/a00111.html).  If a binary semaphore
+ * is created using xSemaphoreCreateBinaryStatic() then the application writer
+ * must provide the memory.  xSemaphoreCreateBinaryStatic() therefore allows a
+ * binary semaphore to be created without using any dynamic memory allocation.
+ *
+ * This type of semaphore can be used for pure synchronisation between tasks or
+ * between an interrupt and a task.  The semaphore need not be given back once
+ * obtained, so one task/interrupt can continuously 'give' the semaphore while
+ * another continuously 'takes' the semaphore.  For this reason this type of
+ * semaphore does not use a priority inheritance mechanism.  For an alternative
+ * that does use priority inheritance see xSemaphoreCreateMutex().
+ *
+ * @param pxSemaphoreBuffer Must point to a variable of type StaticSemaphore_t,
+ * which will then be used to hold the semaphore's data structure, removing the
+ * need for the memory to be allocated dynamically.
+ *
+ * @return If the semaphore is created then a handle to the created semaphore is
+ * returned.  If pxSemaphoreBuffer is NULL then NULL is returned.
+ *
+ * Example usage:
+ <pre>
+ SemaphoreHandle_t xSemaphore = NULL;
+ StaticSemaphore_t xSemaphoreBuffer;
+
+ void vATask( void * pvParameters )
+ {
+    // Semaphore cannot be used before a call to xSemaphoreCreateBinary().
+    // The semaphore's data structures will be placed in the xSemaphoreBuffer
+    // variable, the address of which is passed into the function.  The
+    // function's parameter is not NULL, so the function will not attempt any
+    // dynamic memory allocation, and therefore the function will not return
+    // return NULL.
+    xSemaphore = xSemaphoreCreateBinary( &xSemaphoreBuffer );
+
+    // Rest of task code goes here.
+ }
+ </pre>
+ * \defgroup xSemaphoreCreateBinaryStatic xSemaphoreCreateBinaryStatic
+ * \ingroup Semaphores
+ */
+#if( configSUPPORT_STATIC_ALLOCATION == 1 )
+       #define xSemaphoreCreateBinaryStatic( pxStaticSemaphore ) xQueueGenericCreateStatic( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, NULL, pxStaticSemaphore, queueQUEUE_TYPE_BINARY_SEMAPHORE )
+#endif /* configSUPPORT_STATIC_ALLOCATION */
 
 /**
  * semphr. h
@@ -652,9 +731,18 @@ typedef QueueHandle_t SemaphoreHandle_t;
  * <i>Macro</i> that implements a mutex semaphore by using the existing queue
  * mechanism.
  *
- * Mutexes created using this macro can be accessed using the xSemaphoreTake()
+ * Internally, within the FreeRTOS implementation, mutex semaphores use a block
+ * of memory, in which the mutex structure is stored.  If a mutex is created
+ * using xSemaphoreCreateMutex() then the required memory is automatically
+ * dynamically allocated inside the xSemaphoreCreateMutex() function.  (see
+ * http://www.freertos.org/a00111.html).  If a mutex is created using
+ * xSemaphoreCreateMutexStatic() then the application writer must provided the
+ * memory.  xSemaphoreCreateMutexStatic() therefore allows a mutex to be created
+ * without using any dynamic memory allocation.
+ *
+ * Mutexes created using this function can be accessed using the xSemaphoreTake()
  * and xSemaphoreGive() macros.  The xSemaphoreTakeRecursive() and
- * xSemaphoreGiveRecursive() macros should not be used.
+ * xSemaphoreGiveRecursive() macros must not be used.
  *
  * This type of semaphore uses a priority inheritance mechanism so a task
  * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
@@ -667,8 +755,9 @@ typedef QueueHandle_t SemaphoreHandle_t;
  * semaphore and another always 'takes' the semaphore) and from within interrupt
  * service routines.
  *
- * @return xSemaphore Handle to the created mutex semaphore.  Should be of type
- *             SemaphoreHandle_t.
+ * @return If the mutex was successfully created then a handle to the created
+ * semaphore is returned.  If there was not enough heap to allocate the mutex
+ * data structures then NULL is returned.
  *
  * Example usage:
  <pre>
@@ -690,19 +779,93 @@ typedef QueueHandle_t SemaphoreHandle_t;
  * \defgroup vSemaphoreCreateMutex vSemaphoreCreateMutex
  * \ingroup Semaphores
  */
-#define xSemaphoreCreateMutex() xQueueCreateMutex( queueQUEUE_TYPE_MUTEX )
+#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
+       #define xSemaphoreCreateMutex() xQueueCreateMutex( queueQUEUE_TYPE_MUTEX )
+#endif
+
+/**
+ * semphr. h
+ * <pre>SemaphoreHandle_t xSemaphoreCreateMutexStatic( StaticSemaphore_t *pxMutexBuffer )</pre>
+ *
+ * Creates a new mutex type semaphore instance, and returns a handle by which
+ * the new mutex can be referenced.
+ *
+ * Internally, within the FreeRTOS implementation, mutex semaphores use a block
+ * of memory, in which the mutex structure is stored.  If a mutex is created
+ * using xSemaphoreCreateMutex() then the required memory is automatically
+ * dynamically allocated inside the xSemaphoreCreateMutex() function.  (see
+ * http://www.freertos.org/a00111.html).  If a mutex is created using
+ * xSemaphoreCreateMutexStatic() then the application writer must provided the
+ * memory.  xSemaphoreCreateMutexStatic() therefore allows a mutex to be created
+ * without using any dynamic memory allocation.
+ *
+ * Mutexes created using this function can be accessed using the xSemaphoreTake()
+ * and xSemaphoreGive() macros.  The xSemaphoreTakeRecursive() and
+ * xSemaphoreGiveRecursive() macros must not be used.
+ *
+ * This type of semaphore uses a priority inheritance mechanism so a task
+ * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
+ * semaphore it is no longer required.
+ *
+ * Mutex type semaphores cannot be used from within interrupt service routines.
+ *
+ * See xSemaphoreCreateBinary() for an alternative implementation that can be
+ * used for pure synchronisation (where one task or interrupt always 'gives' the
+ * semaphore and another always 'takes' the semaphore) and from within interrupt
+ * service routines.
+ *
+ * @param pxMutexBuffer Must point to a variable of type StaticSemaphore_t,
+ * which will be used to hold the mutex's data structure, removing the need for
+ * the memory to be allocated dynamically.
+ *
+ * @return If the mutex was successfully created then a handle to the created
+ * mutex is returned.  If pxMutexBuffer was NULL then NULL is returned.
+ *
+ * Example usage:
+ <pre>
+ SemaphoreHandle_t xSemaphore;
+ StaticSemaphore_t xMutexBuffer;
+
+ void vATask( void * pvParameters )
+ {
+    // A mutex cannot be used before it has been created.  xMutexBuffer is
+    // into xSemaphoreCreateMutexStatic() so no dynamic memory allocation is
+    // attempted.
+    xSemaphore = xSemaphoreCreateMutexStatic( &xMutexBuffer );
+
+    // As no dynamic memory allocation was performed, xSemaphore cannot be NULL,
+    // so there is no need to check it.
+ }
+ </pre>
+ * \defgroup xSemaphoreCreateMutexStatic xSemaphoreCreateMutexStatic
+ * \ingroup Semaphores
+ */
+ #if( configSUPPORT_STATIC_ALLOCATION == 1 )
+       #define xSemaphoreCreateMutexStatic( pxMutexBuffer ) xQueueCreateMutexStatic( queueQUEUE_TYPE_MUTEX, ( pxMutexBuffer ) )
+#endif /* configSUPPORT_STATIC_ALLOCATION */
 
 
 /**
  * semphr. h
  * <pre>SemaphoreHandle_t xSemaphoreCreateRecursiveMutex( void )</pre>
  *
- * <i>Macro</i> that implements a recursive mutex by using the existing queue
- * mechanism.
+ * Creates a new recursive mutex type semaphore instance, and returns a handle
+ * by which the new recursive mutex can be referenced.
+ *
+ * Internally, within the FreeRTOS implementation, recursive mutexs use a block
+ * of memory, in which the mutex structure is stored.  If a recursive mutex is
+ * created using xSemaphoreCreateRecursiveMutex() then the required memory is
+ * automatically dynamically allocated inside the
+ * xSemaphoreCreateRecursiveMutex() function.  (see
+ * http://www.freertos.org/a00111.html).  If a recursive mutex is created using
+ * xSemaphoreCreateRecursiveMutexStatic() then the application writer must
+ * provide the memory that will get used by the mutex.
+ * xSemaphoreCreateRecursiveMutexStatic() therefore allows a recursive mutex to
+ * be created without using any dynamic memory allocation.
  *
  * Mutexes created using this macro can be accessed using the
  * xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() macros.  The
- * xSemaphoreTake() and xSemaphoreGive() macros should not be used.
+ * xSemaphoreTake() and xSemaphoreGive() macros must not be used.
  *
  * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
  * doesn't become available again until the owner has called
@@ -745,14 +908,104 @@ typedef QueueHandle_t SemaphoreHandle_t;
  * \defgroup vSemaphoreCreateMutex vSemaphoreCreateMutex
  * \ingroup Semaphores
  */
-#define xSemaphoreCreateRecursiveMutex() xQueueCreateMutex( queueQUEUE_TYPE_RECURSIVE_MUTEX )
+#if( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configUSE_RECURSIVE_MUTEXES == 1 ) )
+       #define xSemaphoreCreateRecursiveMutex() xQueueCreateMutex( queueQUEUE_TYPE_RECURSIVE_MUTEX )
+#endif
+
+/**
+ * semphr. h
+ * <pre>SemaphoreHandle_t xSemaphoreCreateRecursiveMutexStatic( StaticSemaphore_t *pxMutexBuffer )</pre>
+ *
+ * Creates a new recursive mutex type semaphore instance, and returns a handle
+ * by which the new recursive mutex can be referenced.
+ *
+ * Internally, within the FreeRTOS implementation, recursive mutexs use a block
+ * of memory, in which the mutex structure is stored.  If a recursive mutex is
+ * created using xSemaphoreCreateRecursiveMutex() then the required memory is
+ * automatically dynamically allocated inside the
+ * xSemaphoreCreateRecursiveMutex() function.  (see
+ * http://www.freertos.org/a00111.html).  If a recursive mutex is created using
+ * xSemaphoreCreateRecursiveMutexStatic() then the application writer must
+ * provide the memory that will get used by the mutex.
+ * xSemaphoreCreateRecursiveMutexStatic() therefore allows a recursive mutex to
+ * be created without using any dynamic memory allocation.
+ *
+ * Mutexes created using this macro can be accessed using the
+ * xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() macros.  The
+ * xSemaphoreTake() and xSemaphoreGive() macros must not be used.
+ *
+ * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
+ * doesn't become available again until the owner has called
+ * xSemaphoreGiveRecursive() for each successful 'take' request.  For example,
+ * if a task successfully 'takes' the same mutex 5 times then the mutex will
+ * not be available to any other task until it has also  'given' the mutex back
+ * exactly five times.
+ *
+ * This type of semaphore uses a priority inheritance mechanism so a task
+ * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
+ * semaphore it is no longer required.
+ *
+ * Mutex type semaphores cannot be used from within interrupt service routines.
+ *
+ * See xSemaphoreCreateBinary() for an alternative implementation that can be
+ * used for pure synchronisation (where one task or interrupt always 'gives' the
+ * semaphore and another always 'takes' the semaphore) and from within interrupt
+ * service routines.
+ *
+ * @param pxMutexBuffer Must point to a variable of type StaticSemaphore_t,
+ * which will then be used to hold the recursive mutex's data structure,
+ * removing the need for the memory to be allocated dynamically.
+ *
+ * @return If the recursive mutex was successfully created then a handle to the
+ * created recursive mutex is returned.  If pxMutexBuffer was NULL then NULL is
+ * returned.
+ *
+ * Example usage:
+ <pre>
+ SemaphoreHandle_t xSemaphore;
+ StaticSemaphore_t xMutexBuffer;
+
+ void vATask( void * pvParameters )
+ {
+    // A recursive semaphore cannot be used before it is created.  Here a
+    // recursive mutex is created using xSemaphoreCreateRecursiveMutexStatic().
+    // The address of xMutexBuffer is passed into the function, and will hold
+    // the mutexes data structures - so no dynamic memory allocation will be
+    // attempted.
+    xSemaphore = xSemaphoreCreateRecursiveMutexStatic( &xMutexBuffer );
+
+    // As no dynamic memory allocation was performed, xSemaphore cannot be NULL,
+    // so there is no need to check it.
+ }
+ </pre>
+ * \defgroup xSemaphoreCreateRecursiveMutexStatic xSemaphoreCreateRecursiveMutexStatic
+ * \ingroup Semaphores
+ */
+#if( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configUSE_RECURSIVE_MUTEXES == 1 ) )
+       #define xSemaphoreCreateRecursiveMutexStatic( pxStaticSemaphore ) xQueueCreateMutexStatic( queueQUEUE_TYPE_RECURSIVE_MUTEX, pxStaticSemaphore )
+#endif /* configSUPPORT_STATIC_ALLOCATION */
 
 /**
  * semphr. h
  * <pre>SemaphoreHandle_t xSemaphoreCreateCounting( UBaseType_t uxMaxCount, UBaseType_t uxInitialCount )</pre>
  *
- * <i>Macro</i> that creates a counting semaphore by using the existing
- * queue mechanism.
+ * Creates a new counting semaphore instance, and returns a handle by which the
+ * new counting semaphore can be referenced.
+ *
+ * In many usage scenarios it is faster and more memory efficient to use a
+ * direct to task notification in place of a counting semaphore!
+ * http://www.freertos.org/RTOS-task-notifications.html
+ *
+ * Internally, within the FreeRTOS implementation, counting semaphores use a
+ * block of memory, in which the counting semaphore structure is stored.  If a
+ * counting semaphore is created using xSemaphoreCreateCounting() then the
+ * required memory is automatically dynamically allocated inside the
+ * xSemaphoreCreateCounting() function.  (see
+ * http://www.freertos.org/a00111.html).  If a counting semaphore is created
+ * using xSemaphoreCreateCountingStatic() then the application writer can
+ * instead optionally provide the memory that will get used by the counting
+ * semaphore.  xSemaphoreCreateCountingStatic() therefore allows a counting
+ * semaphore to be created without using any dynamic memory allocation.
  *
  * Counting semaphores are typically used for two things:
  *
@@ -808,7 +1061,94 @@ typedef QueueHandle_t SemaphoreHandle_t;
  * \defgroup xSemaphoreCreateCounting xSemaphoreCreateCounting
  * \ingroup Semaphores
  */
-#define xSemaphoreCreateCounting( uxMaxCount, uxInitialCount ) xQueueCreateCountingSemaphore( ( uxMaxCount ), ( uxInitialCount ) )
+#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
+       #define xSemaphoreCreateCounting( uxMaxCount, uxInitialCount ) xQueueCreateCountingSemaphore( ( uxMaxCount ), ( uxInitialCount ) )
+#endif
+
+/**
+ * semphr. h
+ * <pre>SemaphoreHandle_t xSemaphoreCreateCountingStatic( UBaseType_t uxMaxCount, UBaseType_t uxInitialCount, StaticSemaphore_t *pxSemaphoreBuffer )</pre>
+ *
+ * Creates a new counting semaphore instance, and returns a handle by which the
+ * new counting semaphore can be referenced.
+ *
+ * In many usage scenarios it is faster and more memory efficient to use a
+ * direct to task notification in place of a counting semaphore!
+ * http://www.freertos.org/RTOS-task-notifications.html
+ *
+ * Internally, within the FreeRTOS implementation, counting semaphores use a
+ * block of memory, in which the counting semaphore structure is stored.  If a
+ * counting semaphore is created using xSemaphoreCreateCounting() then the
+ * required memory is automatically dynamically allocated inside the
+ * xSemaphoreCreateCounting() function.  (see
+ * http://www.freertos.org/a00111.html).  If a counting semaphore is created
+ * using xSemaphoreCreateCountingStatic() then the application writer must
+ * provide the memory.  xSemaphoreCreateCountingStatic() therefore allows a
+ * counting semaphore to be created without using any dynamic memory allocation.
+ *
+ * Counting semaphores are typically used for two things:
+ *
+ * 1) Counting events.
+ *
+ *    In this usage scenario an event handler will 'give' a semaphore each time
+ *    an event occurs (incrementing the semaphore count value), and a handler
+ *    task will 'take' a semaphore each time it processes an event
+ *    (decrementing the semaphore count value).  The count value is therefore
+ *    the difference between the number of events that have occurred and the
+ *    number that have been processed.  In this case it is desirable for the
+ *    initial count value to be zero.
+ *
+ * 2) Resource management.
+ *
+ *    In this usage scenario the count value indicates the number of resources
+ *    available.  To obtain control of a resource a task must first obtain a
+ *    semaphore - decrementing the semaphore count value.  When the count value
+ *    reaches zero there are no free resources.  When a task finishes with the
+ *    resource it 'gives' the semaphore back - incrementing the semaphore count
+ *    value.  In this case it is desirable for the initial count value to be
+ *    equal to the maximum count value, indicating that all resources are free.
+ *
+ * @param uxMaxCount The maximum count value that can be reached.  When the
+ *        semaphore reaches this value it can no longer be 'given'.
+ *
+ * @param uxInitialCount The count value assigned to the semaphore when it is
+ *        created.
+ *
+ * @param pxSemaphoreBuffer Must point to a variable of type StaticSemaphore_t,
+ * which will then be used to hold the semaphore's data structure, removing the
+ * need for the memory to be allocated dynamically.
+ *
+ * @return If the counting semaphore was successfully created then a handle to
+ * the created counting semaphore is returned.  If pxSemaphoreBuffer was NULL
+ * then NULL is returned.
+ *
+ * Example usage:
+ <pre>
+ SemaphoreHandle_t xSemaphore;
+ StaticSemaphore_t xSemaphoreBuffer;
+
+ void vATask( void * pvParameters )
+ {
+ SemaphoreHandle_t xSemaphore = NULL;
+
+    // Counting semaphore cannot be used before they have been created.  Create
+    // a counting semaphore using xSemaphoreCreateCountingStatic().  The max
+    // value to which the semaphore can count is 10, and the initial value
+    // assigned to the count will be 0.  The address of xSemaphoreBuffer is
+    // passed in and will be used to hold the semaphore structure, so no dynamic
+    // memory allocation will be used.
+    xSemaphore = xSemaphoreCreateCounting( 10, 0, &xSemaphoreBuffer );
+
+    // No memory allocation was attempted so xSemaphore cannot be NULL, so there
+    // is no need to check its value.
+ }
+ </pre>
+ * \defgroup xSemaphoreCreateCountingStatic xSemaphoreCreateCountingStatic
+ * \ingroup Semaphores
+ */
+#if( configSUPPORT_STATIC_ALLOCATION == 1 )
+       #define xSemaphoreCreateCountingStatic( uxMaxCount, uxInitialCount, pxSemaphoreBuffer ) xQueueCreateCountingSemaphoreStatic( ( uxMaxCount ), ( uxInitialCount ), ( pxSemaphoreBuffer ) )
+#endif /* configSUPPORT_STATIC_ALLOCATION */
 
 /**
  * semphr. h
index ddf7a7589dec5ce7ffa5dd4996968c576e391225..6a781dc8c215780d0b85f60bb6ae1104263e68a9 100644 (file)
@@ -177,6 +177,7 @@ typedef struct xTASK_STATUS
        UBaseType_t uxCurrentPriority;  /* The priority at which the task was running (may be inherited) when the structure was populated. */
        UBaseType_t uxBasePriority;             /* The priority to which the task will return if the task's current priority has been inherited to avoid unbounded priority inversion when obtaining a mutex.  Only valid if configUSE_MUTEXES is defined as 1 in FreeRTOSConfig.h. */
        uint32_t ulRunTimeCounter;              /* The total run time allocated to the task so far, as defined by the run time stats clock.  See http://www.freertos.org/rtos-run-time-stats.html.  Only valid when configGENERATE_RUN_TIME_STATS is defined as 1 in FreeRTOSConfig.h. */
+       StackType_t *pxStackBase;               /* Points to the lowest address of the task's stack area. */
        uint16_t usStackHighWaterMark;  /* The minimum amount of stack space that has remained for the task since the task was created.  The closer this value is to zero the closer the task has come to overflowing its stack. */
 } TaskStatus_t;
 
@@ -281,8 +282,19 @@ is used in assert() statements. */
                                                  );</pre>
  *
  * Create a new task and add it to the list of tasks that are ready to run.
- * On multicore environments, this will give no specific affinity to the task.
- * Use xTaskCreatePinnedToCore to give affinity.
+ *
+ * Internally, within the FreeRTOS implementation, tasks use two blocks of
+ * memory.  The first block is used to hold the task's data structures.  The
+ * second block is used by the task as its stack.  If a task is created using
+ * xTaskCreate() then both blocks of memory are automatically dynamically
+ * allocated inside the xTaskCreate() function.  (see
+ * http://www.freertos.org/a00111.html).  If a task is created using
+ * xTaskCreateStatic() then the application writer must provide the required
+ * memory.  xTaskCreateStatic() therefore allows a task to be created without
+ * using any dynamic memory allocation.
+ *
+ * See xTaskCreateStatic() for a version that does not use any dynamic memory
+ * allocation.
  *
  * xTaskCreate() can only be used to create a task that has unrestricted
  * access to the entire microcontroller memory map.  Systems that include MPU
@@ -350,8 +362,139 @@ is used in assert() statements. */
  * \defgroup xTaskCreate xTaskCreate
  * \ingroup Tasks
  */
-#define xTaskCreate( pvTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask ) xTaskGenericCreate( ( pvTaskCode ), ( pcName ), ( usStackDepth ), ( pvParameters ), ( uxPriority ), ( pxCreatedTask ), ( NULL ), ( NULL ), tskNO_AFFINITY )
-#define xTaskCreatePinnedToCore( pvTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask, xCoreID ) xTaskGenericCreate( ( pvTaskCode ), ( pcName ), ( usStackDepth ), ( pvParameters ), ( uxPriority ), ( pxCreatedTask ), ( NULL ), ( NULL ), xCoreID )
+#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
+    BaseType_t xTaskCreatePinnedToCore(        TaskFunction_t pxTaskCode,
+                                                                   const char * const pcName,
+                                                                   const uint16_t usStackDepth,
+                                                                   void * const pvParameters,
+                                                                   UBaseType_t uxPriority,
+                                                                   TaskHandle_t * const pxCreatedTask,
+                                                                   const BaseType_t xCoreID);
+
+#define xTaskCreate( pvTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask ) xTaskCreatePinnedToCore( ( pvTaskCode ), ( pcName ), ( usStackDepth ), ( pvParameters ), ( uxPriority ), ( pxCreatedTask ), tskNO_AFFINITY )
+#endif
+
+/**
+ * task. h
+ *<pre>
+ TaskHandle_t xTaskCreateStatic( TaskFunction_t pvTaskCode,
+                                                                const char * const pcName,
+                                                                uint32_t ulStackDepth,
+                                                                void *pvParameters,
+                                                                UBaseType_t uxPriority,
+                                                                StackType_t *pxStackBuffer,
+                                                                StaticTask_t *pxTaskBuffer,
+                                 const BaseType_t xCoreID );</pre>
+
+ *
+ * Create a new task and add it to the list of tasks that are ready to run.
+ *
+ * Internally, within the FreeRTOS implementation, tasks use two blocks of
+ * memory.  The first block is used to hold the task's data structures.  The
+ * second block is used by the task as its stack.  If a task is created using
+ * xTaskCreate() then both blocks of memory are automatically dynamically
+ * allocated inside the xTaskCreate() function.  (see
+ * http://www.freertos.org/a00111.html).  If a task is created using
+ * xTaskCreateStatic() then the application writer must provide the required
+ * memory.  xTaskCreateStatic() therefore allows a task to be created without
+ * using any dynamic memory allocation.
+ *
+ * @param pvTaskCode Pointer to the task entry function.  Tasks
+ * must be implemented to never return (i.e. continuous loop).
+ *
+ * @param pcName A descriptive name for the task.  This is mainly used to
+ * facilitate debugging.  The maximum length of the string is defined by
+ * configMAX_TASK_NAME_LEN in FreeRTOSConfig.h.
+ *
+ * @param ulStackDepth The size of the task stack specified as the number of
+ * variables the stack can hold - not the number of bytes.  For example, if
+ * the stack is 32-bits wide and ulStackDepth is defined as 100 then 400 bytes
+ * will be allocated for stack storage.
+ *
+ * @param pvParameters Pointer that will be used as the parameter for the task
+ * being created.
+ *
+ * @param uxPriority The priority at which the task will run.
+ *
+ * @param pxStackBuffer Must point to a StackType_t array that has at least
+ * ulStackDepth indexes - the array will then be used as the task's stack,
+ * removing the need for the stack to be allocated dynamically.
+ *
+ * @param pxTaskBuffer Must point to a variable of type StaticTask_t, which will
+ * then be used to hold the task's data structures, removing the need for the
+ * memory to be allocated dynamically.
+ *
+ * @return If neither pxStackBuffer or pxTaskBuffer are NULL, then the task will
+ * be created and pdPASS is returned.  If either pxStackBuffer or pxTaskBuffer
+ * are NULL then the task will not be created and
+ * errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY is returned.
+ *
+ * Example usage:
+   <pre>
+
+    // Dimensions the buffer that the task being created will use as its stack.
+    // NOTE:  This is the number of words the stack will hold, not the number of
+    // bytes.  For example, if each stack item is 32-bits, and this is set to 100,
+    // then 400 bytes (100 * 32-bits) will be allocated.
+    #define STACK_SIZE 200
+
+    // Structure that will hold the TCB of the task being created.
+    StaticTask_t xTaskBuffer;
+
+    // Buffer that the task being created will use as its stack.  Note this is
+    // an array of StackType_t variables.  The size of StackType_t is dependent on
+    // the RTOS port.
+    StackType_t xStack[ STACK_SIZE ];
+
+    // Function that implements the task being created.
+    void vTaskCode( void * pvParameters )
+    {
+        // The parameter value is expected to be 1 as 1 is passed in the
+        // pvParameters value in the call to xTaskCreateStatic().
+        configASSERT( ( uint32_t ) pvParameters == 1UL );
+
+        for( ;; )
+        {
+            // Task code goes here.
+        }
+    }
+
+    // Function that creates a task.
+    void vOtherFunction( void )
+    {
+        TaskHandle_t xHandle = NULL;
+
+        // Create the task without using any dynamic memory allocation.
+        xHandle = xTaskCreateStatic(
+                      vTaskCode,       // Function that implements the task.
+                      "NAME",          // Text name for the task.
+                      STACK_SIZE,      // Stack size in words, not bytes.
+                      ( void * ) 1,    // Parameter passed into the task.
+                      tskIDLE_PRIORITY,// Priority at which the task is created.
+                      xStack,          // Array to use as the task's stack.
+                      &xTaskBuffer );  // Variable to hold the task's data structure.
+
+        // puxStackBuffer and pxTaskBuffer were not NULL, so the task will have
+        // been created, and xHandle will be the task's handle.  Use the handle
+        // to suspend the task.
+        vTaskSuspend( xHandle );
+    }
+   </pre>
+ * \defgroup xTaskCreateStatic xTaskCreateStatic
+ * \ingroup Tasks
+ */
+#if( configSUPPORT_STATIC_ALLOCATION == 1 )
+       TaskHandle_t xTaskCreateStaticPinnedToCore(     TaskFunction_t pxTaskCode,
+                                                                                   const char * const pcName,
+                                                                                   const uint32_t ulStackDepth,
+                                                                                   void * const pvParameters,
+                                                                                   UBaseType_t uxPriority,
+                                                                                   StackType_t * const puxStackBuffer,
+                                                                                   StaticTask_t * const pxTaskBuffer,
+                                                const BaseType_t xCoreID );
+
+#define xTaskCreateStatic( pvTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxStackBuffer, pxTaskBuffer ) xTaskCreateStaticPinnedToCore( ( pvTaskCode ), ( pcName ), ( usStackDepth ), ( pvParameters ), ( uxPriority ), ( pxStackBuffer ), ( pxTaskBuffer ), tskNO_AFFINITY )
+#endif /* configSUPPORT_STATIC_ALLOCATION */
 
 /**
  * task. h
@@ -420,7 +563,9 @@ TaskHandle_t xHandle;
  * \defgroup xTaskCreateRestricted xTaskCreateRestricted
  * \ingroup Tasks
  */
-#define xTaskCreateRestricted( x, pxCreatedTask ) xTaskGenericCreate( ((x)->pvTaskCode), ((x)->pcName), ((x)->usStackDepth), ((x)->pvParameters), ((x)->uxPriority), (pxCreatedTask), ((x)->puxStackBuffer), ((x)->xRegions) )
+#if( portUSING_MPU_WRAPPERS == 1 )
+       BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition, TaskHandle_t *pxCreatedTask ) PRIVILEGED_FUNCTION;
+#endif
 
 /**
  * task. h
@@ -1968,12 +2113,6 @@ void vTaskPriorityInherit( TaskHandle_t const pxMutexHolder ) PRIVILEGED_FUNCTIO
  */
 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder ) PRIVILEGED_FUNCTION;
 
-/*
- * Generic version of the task creation function which is in turn called by the
- * xTaskCreate() and xTaskCreateRestricted() macros.
- */
-BaseType_t xTaskGenericCreate( TaskFunction_t pxTaskCode, const char * const pcName, const uint16_t usStackDepth, void * const pvParameters, UBaseType_t uxPriority, TaskHandle_t * const pxCreatedTask, StackType_t * const puxStackBuffer, const MemoryRegion_t * const xRegions, const BaseType_t xCoreID) PRIVILEGED_FUNCTION; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
-
 /*
  * Get the uxTCBNumber assigned to the task referenced by the xTask parameter.
  */
index 248ae7c00a08e75a143c812f4e9924194793f762..445a9e2e91e58abde8ddae77c5f3d7b9ec94d4c6 100644 (file)
@@ -166,15 +166,19 @@ typedef struct QueueDefinition
        volatile BaseType_t xRxLock;    /*< Stores the number of items received from the queue (removed from the queue) while the queue was locked.  Set to queueUNLOCKED when the queue is not locked. */
        volatile BaseType_t xTxLock;    /*< Stores the number of items transmitted to the queue (added to the queue) while the queue was locked.  Set to queueUNLOCKED when the queue is not locked. */
 
-       #if ( configUSE_TRACE_FACILITY == 1 )
-               UBaseType_t uxQueueNumber;
-               uint8_t ucQueueType;
+       #if( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
+               uint8_t ucStaticallyAllocated;  /*< Set to pdTRUE if the memory used by the queue was statically allocated to ensure no attempt is made to free the memory. */
        #endif
 
        #if ( configUSE_QUEUE_SETS == 1 )
                struct QueueDefinition *pxQueueSetContainer;
        #endif
 
+    #if ( configUSE_TRACE_FACILITY == 1 )
+               UBaseType_t uxQueueNumber;
+               uint8_t ucQueueType;
+       #endif
+
        portMUX_TYPE mux;
 
 } xQUEUE;
@@ -255,6 +259,21 @@ static void prvCopyDataFromQueue( Queue_t * const pxQueue, void * const pvBuffer
        static BaseType_t prvNotifyQueueSetContainer( const Queue_t * const pxQueue, const BaseType_t xCopyPosition ) PRIVILEGED_FUNCTION;
 #endif
 
+/*
+ * Called after a Queue_t structure has been allocated either statically or
+ * dynamically to fill in the structure's members.
+ */
+static void prvInitialiseNewQueue( const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize, uint8_t *pucQueueStorage, const uint8_t ucQueueType, Queue_t *pxNewQueue ) PRIVILEGED_FUNCTION;
+
+/*
+ * Mutexes are a special type of queue.  When a mutex is created, first the
+ * queue is created, then prvInitialiseMutex() is called to configure the queue
+ * as a mutex.
+ */
+#if( configUSE_MUTEXES == 1 )
+       static void prvInitialiseMutex( Queue_t *pxNewQueue ) PRIVILEGED_FUNCTION;
+#endif
+
 /*-----------------------------------------------------------*/
 
 /*
@@ -333,134 +352,165 @@ Queue_t * const pxQueue = ( Queue_t * ) xQueue;
 }
 /*-----------------------------------------------------------*/
 
-QueueHandle_t xQueueGenericCreate( const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize, const uint8_t ucQueueType )
-{
-Queue_t *pxNewQueue;
-size_t xQueueSizeInBytes;
-QueueHandle_t xReturn = NULL;
-int8_t *pcAllocatedBuffer;
+#if( configSUPPORT_STATIC_ALLOCATION == 1 )
 
-       /* Remove compiler warnings about unused parameters should
-       configUSE_TRACE_FACILITY not be set to 1. */
-       ( void ) ucQueueType;
+       QueueHandle_t xQueueGenericCreateStatic( const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize, uint8_t *pucQueueStorage, StaticQueue_t *pxStaticQueue, const uint8_t ucQueueType )
+       {
+       Queue_t *pxNewQueue;
 
-       configASSERT( uxQueueLength > ( UBaseType_t ) 0 );
+               configASSERT( uxQueueLength > ( UBaseType_t ) 0 );
 
-       if( uxItemSize == ( UBaseType_t ) 0 )
-       {
-               /* There is not going to be a queue storage area. */
-               xQueueSizeInBytes = ( size_t ) 0;
-       }
-       else
-       {
-               /* The queue is one byte longer than asked for to make wrap checking
-               easier/faster. */
-               xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
+               /* The StaticQueue_t structure and the queue storage area must be
+               supplied. */
+               configASSERT( pxStaticQueue != NULL );
+
+               /* A queue storage area should be provided if the item size is not 0, and
+               should not be provided if the item size is 0. */
+               configASSERT( !( ( pucQueueStorage != NULL ) && ( uxItemSize == 0 ) ) );
+               configASSERT( !( ( pucQueueStorage == NULL ) && ( uxItemSize != 0 ) ) );
+
+               #if( configASSERT_DEFINED == 1 )
+               {
+                       /* Sanity check that the size of the structure used to declare a
+                       variable of type StaticQueue_t or StaticSemaphore_t equals the size of
+                       the real queue and semaphore structures. */
+                       volatile size_t xSize = sizeof( StaticQueue_t );
+                       configASSERT( xSize == sizeof( Queue_t ) );
+               }
+               #endif /* configASSERT_DEFINED */
+
+               /* The address of a statically allocated queue was passed in, use it.
+               The address of a statically allocated storage area was also passed in
+               but is already set. */
+               pxNewQueue = ( Queue_t * ) pxStaticQueue; /*lint !e740 Unusual cast is ok as the structures are designed to have the same alignment, and the size is checked by an assert. */
+
+               if( pxNewQueue != NULL )
+               {
+                       #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
+                       {
+                               /* Queues can be allocated wither statically or dynamically, so
+                               note this queue was allocated statically in case the queue is
+                               later deleted. */
+                               pxNewQueue->ucStaticallyAllocated = pdTRUE;
+                       }
+                       #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
+
+                       prvInitialiseNewQueue( uxQueueLength, uxItemSize, pucQueueStorage, ucQueueType, pxNewQueue );
+               }
+
+               return pxNewQueue;
        }
 
-       /* Allocate the new queue structure and storage area. */
-       pcAllocatedBuffer = ( int8_t * ) pvPortMalloc( sizeof( Queue_t ) + xQueueSizeInBytes );
+#endif /* configSUPPORT_STATIC_ALLOCATION */
+/*-----------------------------------------------------------*/
 
-       if( pcAllocatedBuffer != NULL )
+#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
+
+       QueueHandle_t xQueueGenericCreate( const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize, const uint8_t ucQueueType )
        {
-               pxNewQueue = ( Queue_t * ) pcAllocatedBuffer; /*lint !e826 MISRA The buffer cannot be to small because it was dimensioned by sizeof( Queue_t ) + xQueueSizeInBytes. */
+       Queue_t *pxNewQueue;
+       size_t xQueueSizeInBytes;
+       uint8_t *pucQueueStorage;
+
+               configASSERT( uxQueueLength > ( UBaseType_t ) 0 );
 
                if( uxItemSize == ( UBaseType_t ) 0 )
                {
-                       /* No RAM was allocated for the queue storage area, but PC head
-                       cannot be set to NULL because NULL is used as a key to say the queue
-                       is used as a mutex.  Therefore just set pcHead to point to the queue
-                       as a benign value that is known to be within the memory map. */
-                       pxNewQueue->pcHead = ( int8_t * ) pxNewQueue;
+                       /* There is not going to be a queue storage area. */
+                       xQueueSizeInBytes = ( size_t ) 0;
                }
                else
                {
-                       /* Jump past the queue structure to find the location of the queue
-                       storage area - adding the padding bytes to get a better alignment. */
-                       pxNewQueue->pcHead = pcAllocatedBuffer + sizeof( Queue_t );
+                       /* Allocate enough space to hold the maximum number of items that
+                       can be in the queue at any time. */
+                       xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
                }
 
-               /* Initialise the queue members as described above where the queue type
-               is defined. */
-               pxNewQueue->uxLength = uxQueueLength;
-               pxNewQueue->uxItemSize = uxItemSize;
-               ( void ) xQueueGenericReset( pxNewQueue, pdTRUE );
+               pxNewQueue = ( Queue_t * ) pvPortMalloc( sizeof( Queue_t ) + xQueueSizeInBytes );
 
-               #if ( configUSE_TRACE_FACILITY == 1 )
+               if( pxNewQueue != NULL )
                {
-                       pxNewQueue->ucQueueType = ucQueueType;
-               }
-               #endif /* configUSE_TRACE_FACILITY */
+                       /* Jump past the queue structure to find the location of the queue
+                       storage area. */
+                       pucQueueStorage = ( ( uint8_t * ) pxNewQueue ) + sizeof( Queue_t );
 
-               #if( configUSE_QUEUE_SETS == 1 )
-               {
-                       pxNewQueue->pxQueueSetContainer = NULL;
+                       #if( configSUPPORT_STATIC_ALLOCATION == 1 )
+                       {
+                               /* Queues can be created either statically or dynamically, so
+                               note this task was created dynamically in case it is later
+                               deleted. */
+                               pxNewQueue->ucStaticallyAllocated = pdFALSE;
+                       }
+                       #endif /* configSUPPORT_STATIC_ALLOCATION */
+
+                       prvInitialiseNewQueue( uxQueueLength, uxItemSize, pucQueueStorage, ucQueueType, pxNewQueue );
                }
-               #endif /* configUSE_QUEUE_SETS */
 
-               traceQUEUE_CREATE( pxNewQueue );
-               xReturn = pxNewQueue;
+               return pxNewQueue;
+       }
+
+#endif /* configSUPPORT_STATIC_ALLOCATION */
+/*-----------------------------------------------------------*/
+
+static void prvInitialiseNewQueue( const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize, uint8_t *pucQueueStorage, const uint8_t ucQueueType, Queue_t *pxNewQueue )
+{
+       /* Remove compiler warnings about unused parameters should
+       configUSE_TRACE_FACILITY not be set to 1. */
+       ( void ) ucQueueType;
+
+       if( uxItemSize == ( UBaseType_t ) 0 )
+       {
+               /* No RAM was allocated for the queue storage area, but PC head cannot
+               be set to NULL because NULL is used as a key to say the queue is used as
+               a mutex.  Therefore just set pcHead to point to the queue as a benign
+               value that is known to be within the memory map. */
+               pxNewQueue->pcHead = ( int8_t * ) pxNewQueue;
        }
        else
        {
-               mtCOVERAGE_TEST_MARKER();
+               /* Set the head to the start of the queue storage area. */
+               pxNewQueue->pcHead = ( int8_t * ) pucQueueStorage;
        }
 
-       configASSERT( xReturn );
+       /* Initialise the queue members as described where the queue type is
+       defined. */
+       pxNewQueue->uxLength = uxQueueLength;
+       pxNewQueue->uxItemSize = uxItemSize;
+       ( void ) xQueueGenericReset( pxNewQueue, pdTRUE );
 
-       return xReturn;
+       #if ( configUSE_TRACE_FACILITY == 1 )
+       {
+               pxNewQueue->ucQueueType = ucQueueType;
+       }
+       #endif /* configUSE_TRACE_FACILITY */
+
+       #if( configUSE_QUEUE_SETS == 1 )
+       {
+               pxNewQueue->pxQueueSetContainer = NULL;
+       }
+       #endif /* configUSE_QUEUE_SETS */
+
+       traceQUEUE_CREATE( pxNewQueue );
 }
 /*-----------------------------------------------------------*/
 
-#if ( configUSE_MUTEXES == 1 )
+#if( configUSE_MUTEXES == 1 )
 
-       QueueHandle_t xQueueCreateMutex( const uint8_t ucQueueType )
+       static void prvInitialiseMutex( Queue_t *pxNewQueue )
        {
-       Queue_t *pxNewQueue;
-
-               /* Prevent compiler warnings about unused parameters if
-               configUSE_TRACE_FACILITY does not equal 1. */
-               ( void ) ucQueueType;
-
-               /* Allocate the new queue structure. */
-               pxNewQueue = ( Queue_t * ) pvPortMalloc( sizeof( Queue_t ) );
                if( pxNewQueue != NULL )
                {
-                       /* Information required for priority inheritance. */
+                       /* The queue create function will set all the queue structure members
+                       correctly for a generic queue, but this function is creating a
+                       mutex.  Overwrite those members that need to be set differently -
+                       in particular the information required for priority inheritance. */
                        pxNewQueue->pxMutexHolder = NULL;
                        pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
 
-                       /* Queues used as a mutex no data is actually copied into or out
-                       of the queue. */
-                       pxNewQueue->pcWriteTo = NULL;
-                       pxNewQueue->u.pcReadFrom = NULL;
-
-                       /* Each mutex has a length of 1 (like a binary semaphore) and
-                       an item size of 0 as nothing is actually copied into or out
-                       of the mutex. */
-                       pxNewQueue->uxMessagesWaiting = ( UBaseType_t ) 0U;
-                       pxNewQueue->uxLength = ( UBaseType_t ) 1U;
-                       pxNewQueue->uxItemSize = ( UBaseType_t ) 0U;
-                       pxNewQueue->xRxLock = queueUNLOCKED;
-                       pxNewQueue->xTxLock = queueUNLOCKED;
-
-                       #if ( configUSE_TRACE_FACILITY == 1 )
-                       {
-                               pxNewQueue->ucQueueType = ucQueueType;
-                       }
-                       #endif
-
-                       #if ( configUSE_QUEUE_SETS == 1 )
-                       {
-                               pxNewQueue->pxQueueSetContainer = NULL;
-                       }
-                       #endif
+                       /* In case this is a recursive mutex. */
+                       pxNewQueue->u.uxRecursiveCallCount = 0;
 
-                       /* Ensure the event queues start with the correct state. */
-                       vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
-                       vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
-
-                       vPortCPUInitializeMutex(&pxNewQueue->mux);
+            vPortCPUInitializeMutex(&pxNewQueue->mux);
 
                        traceCREATE_MUTEX( pxNewQueue );
 
@@ -471,8 +521,41 @@ int8_t *pcAllocatedBuffer;
                {
                        traceCREATE_MUTEX_FAILED();
                }
+       }
+
+#endif /* configUSE_MUTEXES */
+/*-----------------------------------------------------------*/
+
+#if( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
+
+       QueueHandle_t xQueueCreateMutex( const uint8_t ucQueueType )
+       {
+       Queue_t *pxNewQueue;
+       const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;
+
+               pxNewQueue = ( Queue_t * ) xQueueGenericCreate( uxMutexLength, uxMutexSize, ucQueueType );
+               prvInitialiseMutex( pxNewQueue );
+
+               return pxNewQueue;
+       }
+
+#endif /* configUSE_MUTEXES */
+/*-----------------------------------------------------------*/
+
+#if( ( configUSE_MUTEXES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
+
+       QueueHandle_t xQueueCreateMutexStatic( const uint8_t ucQueueType, StaticQueue_t *pxStaticQueue )
+       {
+       Queue_t *pxNewQueue;
+       const UBaseType_t uxMutexLength = ( UBaseType_t ) 1, uxMutexSize = ( UBaseType_t ) 0;
+
+               /* Prevent compiler warnings about unused parameters if
+               configUSE_TRACE_FACILITY does not equal 1. */
+               ( void ) ucQueueType;
+
+               pxNewQueue = ( Queue_t * ) xQueueGenericCreateStatic( uxMutexLength, uxMutexSize, NULL, pxStaticQueue, ucQueueType );
+               prvInitialiseMutex( pxNewQueue );
 
-               configASSERT( pxNewQueue );
                return pxNewQueue;
        }
 
@@ -607,7 +690,35 @@ int8_t *pcAllocatedBuffer;
 #endif /* configUSE_RECURSIVE_MUTEXES */
 /*-----------------------------------------------------------*/
 
-#if ( configUSE_COUNTING_SEMAPHORES == 1 )
+#if( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
+
+       QueueHandle_t xQueueCreateCountingSemaphoreStatic( const UBaseType_t uxMaxCount, const UBaseType_t uxInitialCount, StaticQueue_t *pxStaticQueue )
+       {
+       QueueHandle_t xHandle;
+
+               configASSERT( uxMaxCount != 0 );
+               configASSERT( uxInitialCount <= uxMaxCount );
+
+               xHandle = xQueueGenericCreateStatic( uxMaxCount, queueSEMAPHORE_QUEUE_ITEM_LENGTH, NULL, pxStaticQueue, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
+
+               if( xHandle != NULL )
+               {
+                       ( ( Queue_t * ) xHandle )->uxMessagesWaiting = uxInitialCount;
+
+                       traceCREATE_COUNTING_SEMAPHORE();
+               }
+               else
+               {
+                       traceCREATE_COUNTING_SEMAPHORE_FAILED();
+               }
+
+               return xHandle;
+       }
+
+#endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
+/*-----------------------------------------------------------*/
+
+#if( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
 
        QueueHandle_t xQueueCreateCountingSemaphore( const UBaseType_t uxMaxCount, const UBaseType_t uxInitialCount )
        {
@@ -633,7 +744,7 @@ int8_t *pcAllocatedBuffer;
                return xHandle;
        }
 
-#endif /* configUSE_COUNTING_SEMAPHORES */
+#endif /* ( ( configUSE_COUNTING_SEMAPHORES == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
 /*-----------------------------------------------------------*/
 
 BaseType_t xQueueGenericSend( QueueHandle_t xQueue, const void * const pvItemToQueue, TickType_t xTicksToWait, const BaseType_t xCopyPosition )
@@ -1777,7 +1888,33 @@ Queue_t * const pxQueue = ( Queue_t * ) xQueue;
                vQueueUnregisterQueue( pxQueue );
        }
        #endif
-       vPortFree( pxQueue );
+
+       #if( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) )
+       {
+               /* The queue can only have been allocated dynamically - free it
+               again. */
+               vPortFree( pxQueue );
+       }
+       #elif( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
+       {
+               /* The queue could have been allocated statically or dynamically, so
+               check before attempting to free the memory. */
+               if( pxQueue->ucStaticallyAllocated == ( uint8_t ) pdFALSE )
+               {
+                       vPortFree( pxQueue );
+               }
+               else
+               {
+                       mtCOVERAGE_TEST_MARKER();
+               }
+       }
+       #else
+       {
+               /* The queue must have been statically allocated, so is not going to be
+               deleted.  Avoid compiler warnings about the unused parameter. */
+               ( void ) pxQueue;
+       }
+       #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
 }
 /*-----------------------------------------------------------*/
 
@@ -2477,7 +2614,7 @@ Queue_t * const pxQueue = ( Queue_t * ) xQueue;
 #endif /* configUSE_TIMERS */
 /*-----------------------------------------------------------*/
 
-#if ( configUSE_QUEUE_SETS == 1 )
+#if( ( configUSE_QUEUE_SETS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
 
        QueueSetHandle_t xQueueCreateSet( const UBaseType_t uxEventQueueLength )
        {
index ff3a0530d65c3514528970487af06069489f69b7..034ffc08ed8c0e243c8fd0c9779a22d44ccbef14 100644 (file)
@@ -85,7 +85,6 @@ task.h is included from an application file. */
 #include "StackMacros.h"
 #include "portmacro.h"
 #include "semphr.h"
-#include "sys/reent.h"
 
 /* Lint e961 and e750 are suppressed as a MISRA exception justified because the
 MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined for the
@@ -129,6 +128,8 @@ functions but without including stdio.h here. */
                                        } while(0)
 #endif
 
+
+
 /* Value that can be assigned to the eNotifyState member of the TCB. */
 typedef enum
 {
@@ -137,6 +138,26 @@ typedef enum
        eNotified
 } eNotifyValue;
 
+/* Sometimes the FreeRTOSConfig.h settings only allow a task to be created using
+dynamically allocated RAM, in which case when any task is deleted it is known
+that both the task's stack and TCB need to be freed.  Sometimes the
+FreeRTOSConfig.h settings only allow a task to be created using statically
+allocated RAM, in which case when any task is deleted it is known that neither
+the task's stack or TCB should be freed.  Sometimes the FreeRTOSConfig.h
+settings allow a task to be created using either statically or dynamically
+allocated RAM, in which case a member of the TCB is used to record whether the
+stack and/or TCB were allocated statically or dynamically, so when a task is
+deleted the RAM that was allocated dynamically is freed again and no attempt is
+made to free the RAM that was allocated statically.
+tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE is only true if it is possible for a
+task to be created using either statically or dynamically allocated RAM.  Note
+that if portUSING_MPU_WRAPPERS is 1 then a protected task can be created with
+a statically allocated stack and a dynamically allocated TCB. */
+#define tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE ( ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) || ( portUSING_MPU_WRAPPERS == 1 ) )
+#define tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB                 ( ( uint8_t ) 0 )
+#define tskSTATICALLY_ALLOCATED_STACK_ONLY                     ( ( uint8_t ) 1 )
+#define tskSTATICALLY_ALLOCATED_STACK_AND_TCB          ( ( uint8_t ) 2 )
+
 /*
  * Task control block.  A task control block (TCB) is allocated for each task,
  * and stores task state information, including a pointer to the task's context
@@ -148,7 +169,6 @@ typedef struct tskTaskControlBlock
 
        #if ( portUSING_MPU_WRAPPERS == 1 )
                xMPU_SETTINGS   xMPUSettings;           /*< The MPU settings are defined as part of the port layer.  THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
-               BaseType_t              xUsingStaticallyAllocatedStack; /* Set to pdTRUE if the stack is a statically allocated array, and pdFALSE if the stack is dynamically allocated. */
        #endif
 
        ListItem_t                      xGenericListItem;       /*< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */
@@ -208,6 +228,12 @@ typedef struct tskTaskControlBlock
                volatile eNotifyValue eNotifyState;
        #endif
 
+       /* See the comments above the definition of
+       tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
+       #if( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
+               uint8_t ucStaticallyAllocated;          /*< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
+       #endif
+
 } tskTCB;
 
 /* The old tskTCB name is maintained above then typedefed to the new TCB_t name
@@ -455,12 +481,6 @@ to its original value when it is released. */
 
 /* File private functions. --------------------------------*/
 
-/*
- * Utility to ready a TCB for a given task.  Mainly just copies the parameters
- * into the TCB structure.
- */
-static void prvInitialiseTCBVariables( TCB_t * const pxTCB, const char * const pcName, UBaseType_t uxPriority, const MemoryRegion_t * const xRegions, const uint16_t usStackDepth, const BaseType_t xCoreID ) PRIVILEGED_FUNCTION; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
-
 /**
  * Utility task that simply returns pdTRUE if the task referenced by xTask is
  * currently in the Suspended state, or pdFALSE if the task referenced by xTask
@@ -515,12 +535,6 @@ static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
  */
 static void prvAddCurrentTaskToDelayedList( const portBASE_TYPE xCoreID, const TickType_t xTimeToWake ) PRIVILEGED_FUNCTION;
 
-/*
- * Allocates memory from the heap for a TCB and associated stack.  Checks the
- * allocation was successful.
- */
-static TCB_t *prvAllocateTCBAndStack( const uint16_t usStackDepth, StackType_t * const puxStackBuffer ) PRIVILEGED_FUNCTION;
-
 /*
  * Fills an TaskStatus_t structure with information on each task that is
  * referenced from the pxList list (which may be a ready list, a delayed list,
@@ -577,6 +591,26 @@ static void prvResetNextTaskUnblockTime( void );
 
 #endif
 
+/*
+ * Called after a Task_t structure has been allocated either statically or
+ * dynamically to fill in the structure's members.
+ */
+static void prvInitialiseNewTask(      TaskFunction_t pxTaskCode,
+                                                                       const char * const pcName,
+                                                                       const uint32_t ulStackDepth,
+                                                                       void * const pvParameters,
+                                                                       UBaseType_t uxPriority,
+                                                                       TaskHandle_t * const pxCreatedTask,
+                                                                       TCB_t *pxNewTCB,
+                                                                       const MemoryRegion_t * const xRegions ) PRIVILEGED_FUNCTION; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
+
+/*
+ * Called after a new task has been created and initialised to place the task
+ * under the control of the scheduler.
+ */
+static void prvAddNewTaskToReadyList( TCB_t *pxNewTCB, TaskFunction_t pxTaskCode, const BaseType_t xCoreID ) PRIVILEGED_FUNCTION;
+
+
 /*-----------------------------------------------------------*/
 
 
@@ -589,114 +623,403 @@ static void vTaskInitializeLocalMuxes( void )
 
 /*-----------------------------------------------------------*/
 
-BaseType_t xTaskGenericCreate( TaskFunction_t pxTaskCode, const char * const pcName, const uint16_t usStackDepth, void * const pvParameters, UBaseType_t uxPriority, TaskHandle_t * const pxCreatedTask, StackType_t * const puxStackBuffer, const MemoryRegion_t * const xRegions, const BaseType_t xCoreID) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
-{
-BaseType_t xReturn;
-TCB_t * pxNewTCB;
-StackType_t *pxTopOfStack;
-BaseType_t i;
-       
-       /* Initialize mutexes, if they're not already initialized. */
-       if (xMutexesInitialised == pdFALSE) vTaskInitializeLocalMuxes();
-       
-       configASSERT( pxTaskCode );
-       configASSERT( ( ( uxPriority & ( ~portPRIVILEGE_BIT ) ) < configMAX_PRIORITIES ) );
-       configASSERT( (xCoreID>=0 && xCoreID<portNUM_PROCESSORS) || (xCoreID==tskNO_AFFINITY) );
+#if( configSUPPORT_STATIC_ALLOCATION == 1 )
+
+       TaskHandle_t xTaskCreateStaticPinnedToCore(     TaskFunction_t pxTaskCode,
+                                                                       const char * const pcName,
+                                                                       const uint32_t ulStackDepth,
+                                                                       void * const pvParameters,
+                                                                       UBaseType_t uxPriority,
+                                                                       StackType_t * const puxStackBuffer,
+                                                                       StaticTask_t * const pxTaskBuffer,
+                                    const BaseType_t xCoreID )
+       {
+       TCB_t *pxNewTCB;
+       TaskHandle_t xReturn;
 
-       /* Allocate the memory required by the TCB and stack for the new task,
-       checking that the allocation was successful. */
-       pxNewTCB = prvAllocateTCBAndStack( usStackDepth, puxStackBuffer );
+               configASSERT( puxStackBuffer != NULL );
+               configASSERT( pxTaskBuffer != NULL );
+        configASSERT( (xCoreID>=0 && xCoreID<portNUM_PROCESSORS) || (xCoreID==tskNO_AFFINITY) );
 
-       if( pxNewTCB != NULL )
+               if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
+               {
+                       /* The memory used for the task's TCB and stack are passed into this
+                       function - use them. */
+                       pxNewTCB = ( TCB_t * ) pxTaskBuffer; /*lint !e740 Unusual cast is ok as the structures are designed to have the same alignment, and the size is checked by an assert. */
+                       pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
+
+                       #if( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
+                       {
+                               /* Tasks can be created statically or dynamically, so note this
+                               task was created statically in case the task is later deleted. */
+                               pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
+                       }
+                       #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
+
+                       prvInitialiseNewTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, &xReturn, pxNewTCB, NULL );
+                       prvAddNewTaskToReadyList( pxNewTCB, pxTaskCode, xCoreID );
+               }
+               else
+               {
+                       xReturn = NULL;
+               }
+
+               return xReturn;
+       }
+
+#endif /* SUPPORT_STATIC_ALLOCATION */
+/*-----------------------------------------------------------*/
+
+#if( portUSING_MPU_WRAPPERS == 1 )
+
+       BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition, TaskHandle_t *pxCreatedTask )
        {
-               #if( portUSING_MPU_WRAPPERS == 1 )
-                       /* Should the task be created in privileged mode? */
-                       BaseType_t xRunPrivileged;
-                       if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
+       TCB_t *pxNewTCB;
+       BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
+
+               configASSERT( pxTaskDefinition->puxStackBuffer );
+
+               if( pxTaskDefinition->puxStackBuffer != NULL )
+               {
+                       /* Allocate space for the TCB.  Where the memory comes from depends
+                       on the implementation of the port malloc function and whether or
+                       not static allocation is being used. */
+                       pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
+
+                       if( pxNewTCB != NULL )
                        {
-                               xRunPrivileged = pdTRUE;
+                               /* Store the stack location in the TCB. */
+                               pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
+
+                               /* Tasks can be created statically or dynamically, so note
+                               this task had a statically allocated stack in case it is
+                               later deleted.  The TCB was allocated dynamically. */
+                               pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
+
+                               prvInitialiseNewTask(   pxTaskDefinition->pvTaskCode,
+                                                                               pxTaskDefinition->pcName,
+                                                                               ( uint32_t ) pxTaskDefinition->usStackDepth,
+                                                                               pxTaskDefinition->pvParameters,
+                                                                               pxTaskDefinition->uxPriority,
+                                                                               pxCreatedTask, pxNewTCB,
+                                                                               pxTaskDefinition->xRegions );
+
+                               prvAddNewTaskToReadyList( pxNewTCB, pxTaskDefinition->pvTaskCode, tskNO_AFFINITY );
+                               xReturn = pdPASS;
                        }
-                       else
+               }
+
+               return xReturn;
+       }
+
+#endif /* portUSING_MPU_WRAPPERS */
+/*-----------------------------------------------------------*/
+
+#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
+
+       BaseType_t xTaskCreatePinnedToCore(     TaskFunction_t pxTaskCode,
+                                                       const char * const pcName,
+                                                       const uint16_t usStackDepth,
+                                                       void * const pvParameters,
+                                                       UBaseType_t uxPriority,
+                                                       TaskHandle_t * const pxCreatedTask,
+                            const BaseType_t xCoreID )
+       {
+       TCB_t *pxNewTCB;
+       BaseType_t xReturn;
+
+               /* If the stack grows down then allocate the stack then the TCB so the stack
+               does not grow into the TCB.  Likewise if the stack grows up then allocate
+               the TCB then the stack. */
+               #if( portSTACK_GROWTH > 0 )
+               {
+                       /* Allocate space for the TCB.  Where the memory comes from depends on
+                       the implementation of the port malloc function and whether or not static
+                       allocation is being used. */
+                       pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
+
+                       if( pxNewTCB != NULL )
                        {
-                               xRunPrivileged = pdFALSE;
+                               /* Allocate space for the stack used by the task being created.
+                               The base of the stack memory stored in the TCB so the task can
+                               be deleted later if required. */
+                               pxNewTCB->pxStack = ( StackType_t * ) pvPortMalloc( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
+
+                               if( pxNewTCB->pxStack == NULL )
+                               {
+                                       /* Could not allocate the stack.  Delete the allocated TCB. */
+                                       vPortFree( pxNewTCB );
+                                       pxNewTCB = NULL;
+                               }
                        }
-                       uxPriority &= ~portPRIVILEGE_BIT;
+               }
+               #else /* portSTACK_GROWTH */
+               {
+               StackType_t *pxStack;
+
+                       /* Allocate space for the stack used by the task being created. */
+                       pxStack = ( StackType_t * ) pvPortMalloc( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
 
-                       if( puxStackBuffer != NULL )
+                       if( pxStack != NULL )
                        {
-                               /* The application provided its own stack.  Note this so no
-                               attempt is made to delete the stack should that task be
-                               deleted. */
-                               pxNewTCB->xUsingStaticallyAllocatedStack = pdTRUE;
+                               /* Allocate space for the TCB. */
+                               pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) ); /*lint !e961 MISRA exception as the casts are only redundant for some paths. */
+
+                               if( pxNewTCB != NULL )
+                               {
+                                       /* Store the stack location in the TCB. */
+                                       pxNewTCB->pxStack = pxStack;
+                               }
+                               else
+                               {
+                                       /* The stack cannot be used as the TCB was not created.  Free
+                                       it again. */
+                                       vPortFree( pxStack );
+                               }
                        }
                        else
                        {
-                               /* The stack was allocated dynamically.  Note this so it can be
-                               deleted again if the task is deleted. */
-                               pxNewTCB->xUsingStaticallyAllocatedStack = pdFALSE;
+                               pxNewTCB = NULL;
                        }
-               #endif /* portUSING_MPU_WRAPPERS == 1 */
+               }
+               #endif /* portSTACK_GROWTH */
 
-               /* Calculate the top of stack address.  This depends on whether the
-               stack grows from high memory to low (as per the 80x86) or vice versa.
-               portSTACK_GROWTH is used to make the result positive or negative as
-               required by the port. */
-               #if( portSTACK_GROWTH < 0 )
+               if( pxNewTCB != NULL )
                {
-                       pxTopOfStack = pxNewTCB->pxStack + ( usStackDepth - ( uint16_t ) 1 );
-                       pxTopOfStack = ( StackType_t * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) & ( ( portPOINTER_SIZE_TYPE ) ~portBYTE_ALIGNMENT_MASK  ) ); /*lint !e923 MISRA exception.  Avoiding casts between pointers and integers is not practical.  Size differences accounted for using portPOINTER_SIZE_TYPE type. */
+                       #if( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
+                       {
+                               /* Tasks can be created statically or dynamically, so note this
+                               task was created dynamically in case it is later deleted. */
+                               pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
+                       }
+                       #endif /* configSUPPORT_STATIC_ALLOCATION */
 
-                       /* Check the alignment of the calculated top of stack is correct. */
-                       configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
+                       prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
+                       prvAddNewTaskToReadyList( pxNewTCB, pxTaskCode, xCoreID );
+                       xReturn = pdPASS;
                }
-               #else /* portSTACK_GROWTH */
+               else
                {
-                       pxTopOfStack = pxNewTCB->pxStack;
+                       xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
+               }
 
-                       /* Check the alignment of the stack buffer is correct. */
-                       configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxNewTCB->pxStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
+               return xReturn;
+       }
 
-                       /* If we want to use stack checking on architectures that use
-                       a positive stack growth direction then we also need to store the
-                       other extreme of the stack space. */
-                       pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( usStackDepth - 1 );
-               }
-               #endif /* portSTACK_GROWTH */
+#endif /* configSUPPORT_DYNAMIC_ALLOCATION */
+/*-----------------------------------------------------------*/
 
-               /* Setup the newly allocated TCB with the initial state of the task. */
-               prvInitialiseTCBVariables( pxNewTCB, pcName, uxPriority, xRegions, usStackDepth, xCoreID );
+static void prvInitialiseNewTask(      TaskFunction_t pxTaskCode,
+                                                                       const char * const pcName,
+                                                                       const uint32_t ulStackDepth,
+                                                                       void * const pvParameters,
+                                                                       UBaseType_t uxPriority,
+                                                                       TaskHandle_t * const pxCreatedTask,
+                                                                       TCB_t *pxNewTCB,
+                                                                       const MemoryRegion_t * const xRegions ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
+{
+StackType_t *pxTopOfStack;
+UBaseType_t x;
 
-               /* Initialize the TCB stack to look as if the task was already running,
-               but had been interrupted by the scheduler.  The return address is set
-               to the start of the task function. Once the stack has been initialised
-               the     top of stack variable is updated. */
-               #if( portUSING_MPU_WRAPPERS == 1 )
+       #if( portUSING_MPU_WRAPPERS == 1 )
+               /* Should the task be created in privileged mode? */
+               BaseType_t xRunPrivileged;
+               if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
                {
-                       pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );
+                       xRunPrivileged = pdTRUE;
                }
-               #else /* portUSING_MPU_WRAPPERS */
+               else
                {
-                       pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
+                       xRunPrivileged = pdFALSE;
                }
-               #endif /* portUSING_MPU_WRAPPERS */
+               uxPriority &= ~portPRIVILEGE_BIT;
+       #endif /* portUSING_MPU_WRAPPERS == 1 */
+
+       /* Avoid dependency on memset() if it is not required. */
+       #if( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )
+       {
+               /* Fill the stack with a known value to assist debugging. */
+               ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) ulStackDepth * sizeof( StackType_t ) );
+       }
+       #endif /* ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) ) ) */
+
+       /* Calculate the top of stack address.  This depends on whether the stack
+       grows from high memory to low (as per the 80x86) or vice versa.
+       portSTACK_GROWTH is used to make the result positive or negative as required
+       by the port. */
+       #if( portSTACK_GROWTH < 0 )
+       {
+               pxTopOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
+               pxTopOfStack = ( StackType_t * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) ); /*lint !e923 MISRA exception.  Avoiding casts between pointers and integers is not practical.  Size differences accounted for using portPOINTER_SIZE_TYPE type. */
 
-               if( ( void * ) pxCreatedTask != NULL )
+               /* Check the alignment of the calculated top of stack is correct. */
+               configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
+       }
+       #else /* portSTACK_GROWTH */
+       {
+               pxTopOfStack = pxNewTCB->pxStack;
+
+               /* Check the alignment of the stack buffer is correct. */
+               configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxNewTCB->pxStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
+
+               /* The other extreme of the stack space is required if stack checking is
+               performed. */
+               pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
+       }
+       #endif /* portSTACK_GROWTH */
+
+       /* Store the task name in the TCB. */
+       for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
+       {
+               pxNewTCB->pcTaskName[ x ] = pcName[ x ];
+
+               /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
+               configMAX_TASK_NAME_LEN characters just in case the memory after the
+               string is not accessible (extremely unlikely). */
+               if( pcName[ x ] == 0x00 )
                {
-                       /* Pass the TCB out - in an anonymous way.  The calling function/
-                       task can use this as a handle to delete the task later if
-                       required.*/
-                       *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
+                       break;
                }
                else
                {
                        mtCOVERAGE_TEST_MARKER();
                }
+       }
 
-               /* Ensure interrupts don't access the task lists while they are being
-               updated. */
-               taskENTER_CRITICAL(&xTaskQueueMutex);
+       /* Ensure the name string is terminated in the case that the string length
+       was greater or equal to configMAX_TASK_NAME_LEN. */
+       pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
+
+       /* This is used as an array index so must ensure it's not too large.  First
+       remove the privilege bit if one is present. */
+       if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
+       {
+               uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
+       }
+       else
+       {
+               mtCOVERAGE_TEST_MARKER();
+       }
+
+       pxNewTCB->uxPriority = uxPriority;
+       #if ( configUSE_MUTEXES == 1 )
+       {
+               pxNewTCB->uxBasePriority = uxPriority;
+               pxNewTCB->uxMutexesHeld = 0;
+       }
+       #endif /* configUSE_MUTEXES */
+
+       vListInitialiseItem( &( pxNewTCB->xGenericListItem ) );
+       vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
+
+       /* Set the pxNewTCB as a link back from the ListItem_t.  This is so we can get
+       back to the containing TCB from a generic item in a list. */
+       listSET_LIST_ITEM_OWNER( &( pxNewTCB->xGenericListItem ), pxNewTCB );
+
+       /* Event lists are always in priority order. */
+       listSET_LIST_ITEM_VALUE( &( pxNewTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
+       listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
+
+       #if ( portCRITICAL_NESTING_IN_TCB == 1 )
+       {
+               pxNewTCB->uxCriticalNesting = ( UBaseType_t ) 0U;
+       }
+       #endif /* portCRITICAL_NESTING_IN_TCB */
+
+       #if ( configUSE_APPLICATION_TASK_TAG == 1 )
+       {
+               pxNewTCB->pxTaskTag = NULL;
+       }
+       #endif /* configUSE_APPLICATION_TASK_TAG */
+
+       #if ( configGENERATE_RUN_TIME_STATS == 1 )
+       {
+               pxNewTCB->ulRunTimeCounter = 0UL;
+       }
+       #endif /* configGENERATE_RUN_TIME_STATS */
+
+       #if ( portUSING_MPU_WRAPPERS == 1 )
+       {
+               vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
+       }
+       #else
+       {
+               /* Avoid compiler warning about unreferenced parameter. */
+               ( void ) xRegions;
+       }
+       #endif
+
+       #if( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
+       {
+               for( x = 0; x < ( UBaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS; x++ )
                {
-                       uxCurrentNumberOfTasks++;
+                       pxNewTCB->pvThreadLocalStoragePointers[ x ] = NULL;
+               }
+       }
+       #endif
+
+       #if ( configUSE_TASK_NOTIFICATIONS == 1 )
+       {
+               pxNewTCB->ulNotifiedValue = 0;
+               pxNewTCB->eNotifyState = eNotWaitingNotification;
+       }
+       #endif
+
+       #if ( configUSE_NEWLIB_REENTRANT == 1 )
+       {
+               /* Initialise this task's Newlib reent structure. */
+               _REENT_INIT_PTR( ( &( pxNewTCB->xNewLib_reent ) ) );
+       }
+       #endif
+
+       #if( INCLUDE_xTaskAbortDelay == 1 )
+       {
+               pxNewTCB->ucDelayAborted = pdFALSE;
+       }
+       #endif
+
+       /* Initialize the TCB stack to look as if the task was already running,
+       but had been interrupted by the scheduler.  The return address is set
+       to the start of the task function. Once the stack has been initialised
+       the     top of stack variable is updated. */
+       #if( portUSING_MPU_WRAPPERS == 1 )
+       {
+               pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );
+       }
+       #else /* portUSING_MPU_WRAPPERS */
+       {
+               pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
+       }
+       #endif /* portUSING_MPU_WRAPPERS */
+
+       if( ( void * ) pxCreatedTask != NULL )
+       {
+               /* Pass the handle out in an anonymous way.  The handle can be used to
+               change the created task's priority, delete the created task, etc.*/
+               *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
+       }
+       else
+       {
+               mtCOVERAGE_TEST_MARKER();
+       }
+}
+/*-----------------------------------------------------------*/
+
+static void prvAddNewTaskToReadyList( TCB_t *pxNewTCB, TaskFunction_t pxTaskCode, const BaseType_t xCoreID )
+{
+       BaseType_t i;
+
+    /* Ensure interrupts don't access the task lists while the lists are being
+       updated. */
+       taskENTER_CRITICAL(&xTaskQueueMutex);
+       {
+               uxCurrentNumberOfTasks++;
+               if( pxCurrentTCB[ xPortGetCoreID() ] == NULL )
+               {
+                       /* There are no other tasks, or all the other tasks are in
+                       the suspended state - make this the current task. */
+                       pxCurrentTCB[ xPortGetCoreID() ] = pxNewTCB;
+
                        if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
                        {
                                /* This is the first task to be created so do the preliminary
@@ -704,6 +1027,16 @@ BaseType_t i;
                                fails, but we will report the failure. */
                                prvInitialiseTaskLists();
                        }
+                       else
+                       {
+                               mtCOVERAGE_TEST_MARKER();
+                       }
+               }
+               else
+               {
+                       /* If the scheduler is not already running, make this task the
+                       current task if it is the highest priority task to be created
+                       so far. */
                        if( xSchedulerRunning == pdFALSE )
                        {
                                /* Scheduler isn't running yet. We need to determine on which CPU to run this task. */
@@ -713,7 +1046,7 @@ BaseType_t i;
                                        if (xCoreID == tskNO_AFFINITY || xCoreID == i) 
                                        {
                                                /* Schedule if nothing is scheduled yet, or overwrite a task of lower prio. */
-                                               if ( pxCurrentTCB[i] == NULL || pxCurrentTCB[i]->uxPriority <= uxPriority )
+                                               if ( pxCurrentTCB[i] == NULL || pxCurrentTCB[i]->uxPriority <= pxNewTCB->uxPriority )
                                                {
 #if portFIRST_TASK_HOOK
                                                        if ( i == 0) {
@@ -731,56 +1064,45 @@ BaseType_t i;
                        {
                                mtCOVERAGE_TEST_MARKER();
                        }
+               }
 
-                       uxTaskNumber++;
+               uxTaskNumber++;
 
-                       #if ( configUSE_TRACE_FACILITY == 1 )
-                       {
-                               /* Add a counter into the TCB for tracing only. */
-                               pxNewTCB->uxTCBNumber = uxTaskNumber;
-                       }
-                       #endif /* configUSE_TRACE_FACILITY */
-                       traceTASK_CREATE( pxNewTCB );
+               #if ( configUSE_TRACE_FACILITY == 1 )
+               {
+                       /* Add a counter into the TCB for tracing only. */
+                       pxNewTCB->uxTCBNumber = uxTaskNumber;
+               }
+               #endif /* configUSE_TRACE_FACILITY */
+               traceTASK_CREATE( pxNewTCB );
 
-                       prvAddTaskToReadyList( pxNewTCB );
+               prvAddTaskToReadyList( pxNewTCB );
 
-                       xReturn = pdPASS;
-                       portSETUP_TCB( pxNewTCB );
-               }
-               taskEXIT_CRITICAL(&xTaskQueueMutex);
-       }
-       else
-       {
-               xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
-               traceTASK_CREATE_FAILED();
+               portSETUP_TCB( pxNewTCB );
        }
+       taskEXIT_CRITICAL(&xTaskQueueMutex);
 
-       if( xReturn == pdPASS )
+       if( xSchedulerRunning != pdFALSE )
        {
-               if( xSchedulerRunning != pdFALSE )
+               /* Scheduler is running. If the created task is of a higher priority than an executing task
+              then it should run now.
+                  ToDo: This only works for the current core. If a task is scheduled on an other processor,
+                  the other processor will keep running the task it's working on, and only switch to the newer 
+                  task on a timer interrupt. */
+               //No mux here, uxPriority is mostly atomic and there's not really any harm if this check misfires.
+               if( pxCurrentTCB[ xPortGetCoreID() ]->uxPriority < pxNewTCB->uxPriority )
                {
-                       /* Scheduler is running. If the created task is of a higher priority than an executing task
-                         then it should run now.
-                         ToDo: This only works for the current core. If a task is scheduled on an other processor,
-                         the other processor will keep running the task it's working on, and only switch to the newer 
-                         task on a timer interrupt. */
-                         //No mux here, uxPriority is mostly atomic and there's not really any harm if this check misfires.
-                       if( pxCurrentTCB[ xPortGetCoreID() ]->uxPriority < uxPriority )
-                       {
-                               taskYIELD_IF_USING_PREEMPTION();
-                       }
-                       else
-                       {
-                               mtCOVERAGE_TEST_MARKER();
-                       }
+                       taskYIELD_IF_USING_PREEMPTION();
                }
                else
                {
                        mtCOVERAGE_TEST_MARKER();
                }
        }
-
-       return xReturn;
+       else
+       {
+               mtCOVERAGE_TEST_MARKER();
+       }
 }
 /*-----------------------------------------------------------*/
 
@@ -2971,120 +3293,6 @@ static portTASK_FUNCTION( prvIdleTask, pvParameters )
 #endif /* configUSE_TICKLESS_IDLE */
 /*-----------------------------------------------------------*/
 
-static void prvInitialiseTCBVariables( TCB_t * const pxTCB, const char * const pcName, UBaseType_t uxPriority, const MemoryRegion_t * const xRegions, const uint16_t usStackDepth, const BaseType_t xCoreID ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
-{
-UBaseType_t x;
-
-       /* Store the task name in the TCB. */
-       for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
-       {
-               pxTCB->pcTaskName[ x ] = pcName[ x ];
-
-               /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
-               configMAX_TASK_NAME_LEN characters just in case the memory after the
-               string is not accessible (extremely unlikely). */
-               if( pcName[ x ] == 0x00 )
-               {
-                       break;
-               }
-               else
-               {
-                       mtCOVERAGE_TEST_MARKER();
-               }
-       }
-
-       /* Ensure the name string is terminated in the case that the string length
-       was greater or equal to configMAX_TASK_NAME_LEN. */
-       pxTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
-
-       /* This is used as an array index so must ensure it's not too large.  First
-       remove the privilege bit if one is present. */
-       if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
-       {
-               uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
-       }
-       else
-       {
-               mtCOVERAGE_TEST_MARKER();
-       }
-
-       pxTCB->uxPriority = uxPriority;
-       pxTCB->xCoreID = xCoreID;
-       #if ( configUSE_MUTEXES == 1 )
-       {
-               pxTCB->uxBasePriority = uxPriority;
-               pxTCB->uxMutexesHeld = 0;
-       }
-       #endif /* configUSE_MUTEXES */
-
-       vListInitialiseItem( &( pxTCB->xGenericListItem ) );
-       vListInitialiseItem( &( pxTCB->xEventListItem ) );
-
-       /* Set the pxTCB as a link back from the ListItem_t.  This is so we can get
-       back to the containing TCB from a generic item in a list. */
-       listSET_LIST_ITEM_OWNER( &( pxTCB->xGenericListItem ), pxTCB );
-
-       /* Event lists are always in priority order. */
-       listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
-       listSET_LIST_ITEM_OWNER( &( pxTCB->xEventListItem ), pxTCB );
-
-       #if ( portCRITICAL_NESTING_IN_TCB == 1 )
-       {
-               pxTCB->uxCriticalNesting = ( UBaseType_t ) 0U;
-       }
-       #endif /* portCRITICAL_NESTING_IN_TCB */
-
-       #if ( configUSE_APPLICATION_TASK_TAG == 1 )
-       {
-               pxTCB->pxTaskTag = NULL;
-       }
-       #endif /* configUSE_APPLICATION_TASK_TAG */
-
-       #if ( configGENERATE_RUN_TIME_STATS == 1 )
-       {
-               pxTCB->ulRunTimeCounter = 0UL;
-       }
-       #endif /* configGENERATE_RUN_TIME_STATS */
-
-       #if ( portUSING_MPU_WRAPPERS == 1 )
-       {
-               vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, pxTCB->pxStack, usStackDepth );
-       }
-       #else /* portUSING_MPU_WRAPPERS */
-       {
-               ( void ) xRegions;
-               ( void ) usStackDepth;
-       }
-       #endif /* portUSING_MPU_WRAPPERS */
-
-       #if( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
-       {
-               for( x = 0; x < ( UBaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS; x++ )
-               {
-                       pxTCB->pvThreadLocalStoragePointers[ x ] = NULL;
-                       #if ( configTHREAD_LOCAL_STORAGE_DELETE_CALLBACKS )
-                       pxTCB->pvThreadLocalStoragePointersDelCallback[ x ] = (TlsDeleteCallbackFunction_t)NULL;
-                       #endif
-               }
-       }
-       #endif
-
-
-       #if ( configUSE_TASK_NOTIFICATIONS == 1 )
-       {
-               pxTCB->ulNotifiedValue = 0;
-               pxTCB->eNotifyState = eNotWaitingNotification;
-       }
-       #endif
-
-       #if ( configUSE_NEWLIB_REENTRANT == 1 )
-       {
-               /* Initialise this task's Newlib reent structure. */
-               _REENT_INIT_PTR( ( &( pxTCB->xNewLib_reent ) ) );
-       }
-       #endif /* configUSE_NEWLIB_REENTRANT */
-}
-/*-----------------------------------------------------------*/
 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
 
 #if ( configTHREAD_LOCAL_STORAGE_DELETE_CALLBACKS )
@@ -3280,81 +3488,6 @@ static void prvAddCurrentTaskToDelayedList( const BaseType_t xCoreID, const Tick
 }
 /*-----------------------------------------------------------*/
 
-static TCB_t *prvAllocateTCBAndStack( const uint16_t usStackDepth, StackType_t * const puxStackBuffer )
-{
-TCB_t *pxNewTCB;
-
-       /* If the stack grows down then allocate the stack then the TCB so the stack
-       does not grow into the TCB.  Likewise if the stack grows up then allocate
-       the TCB then the stack. */
-       #if( portSTACK_GROWTH > 0 )
-       {
-               /* Allocate space for the TCB.  Where the memory comes from depends on
-               the implementation of the port malloc function. */
-               pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
-
-               if( pxNewTCB != NULL )
-               {
-                       /* Allocate space for the stack used by the task being created.
-                       The base of the stack memory stored in the TCB so the task can
-                       be deleted later if required. */
-                       pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocAligned( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ), puxStackBuffer ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
-
-                       if( pxNewTCB->pxStack == NULL )
-                       {
-                               /* Could not allocate the stack.  Delete the allocated TCB. */
-                               vPortFree( pxNewTCB );
-                               pxNewTCB = NULL;
-                       }
-               }
-       }
-       #else /* portSTACK_GROWTH */
-       {
-       StackType_t *pxStack;
-
-               /* Allocate space for the stack used by the task being created. */
-               pxStack = ( StackType_t * ) pvPortMallocAligned( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ), puxStackBuffer ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
-
-               if( pxStack != NULL )
-               {
-                       /* Allocate space for the TCB.  Where the memory comes from depends
-                       on the implementation of the port malloc function. */
-                       pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
-
-                       if( pxNewTCB != NULL )
-                       {
-                               /* Store the stack location in the TCB. */
-                               pxNewTCB->pxStack = pxStack;
-                       }
-                       else
-                       {
-                               /* The stack cannot be used as the TCB was not created.  Free it
-                               again. */
-                               vPortFree( pxStack );
-                       }
-               }
-               else
-               {
-                       pxNewTCB = NULL;
-               }
-       }
-       #endif /* portSTACK_GROWTH */
-
-       if( pxNewTCB != NULL )
-       {
-               /* Avoid dependency on memset() if it is not required. */
-               #if( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )
-               {
-                       /* Just to help debugging. */
-       ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) usStackDepth * sizeof( StackType_t ) );
-               }
-               #endif /* ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) ) ) */
-       }
-
-       return pxNewTCB;
-}
-/*-----------------------------------------------------------*/
-
 #if ( configUSE_TRACE_FACILITY == 1 )
 
        static UBaseType_t prvListTaskWithinSingleList( TaskStatus_t *pxTaskStatusArray, List_t *pxList, eTaskState eState )
@@ -3509,22 +3642,40 @@ TCB_t *pxNewTCB;
                }
                #endif /* configUSE_NEWLIB_REENTRANT */
 
-               #if( portUSING_MPU_WRAPPERS == 1 )
+               #if( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
+               {
+                       /* The task can only have been allocated dynamically - free both
+                       the stack and TCB. */
+                       vPortFreeAligned( pxTCB->pxStack );
+                       vPortFree( pxTCB );
+               }
+               #elif( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 )
                {
-                       /* Only free the stack if it was allocated dynamically in the first
-                       place. */
-                       if( pxTCB->xUsingStaticallyAllocatedStack == pdFALSE )
+                       /* The task could have been allocated statically or dynamically, so
+                       check what was statically allocated before trying to free the
+                       memory. */
+                       if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
                        {
+                               /* Both the stack and TCB were allocated dynamically, so both
+                               must be freed. */
                                vPortFreeAligned( pxTCB->pxStack );
+                               vPortFree( pxTCB );
+                       }
+                       else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
+                       {
+                               /* Only the stack was statically allocated, so the TCB is the
+                               only memory that must be freed. */
+                               vPortFree( pxTCB );
+                       }
+                       else
+                       {
+                               /* Neither the stack nor the TCB were allocated dynamically, so
+                               nothing needs to be freed. */
+                               configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB     )
+                               mtCOVERAGE_TEST_MARKER();
                        }
                }
-               #else
-               {
-                       vPortFreeAligned( pxTCB->pxStack );
-               }
-               #endif
-
-               vPortFree( pxTCB );
+               #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
        }
 
 #endif /* INCLUDE_vTaskDelete */
@@ -3932,7 +4083,9 @@ scheduler will re-enable the interrupts instead. */
                function is executing. */
                uxArraySize = uxCurrentNumberOfTasks;
 
-               /* Allocate an array index for each task. */
+               /* Allocate an array index for each task.  NOTE!  if
+               configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
+               equate to NULL. */
                pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );
 
                if( pxTaskStatusArray != NULL )
@@ -3972,7 +4125,8 @@ scheduler will re-enable the interrupts instead. */
                                pcWriteBuffer += strlen( pcWriteBuffer );
                        }
 
-                       /* Free the array again. */
+                       /* Free the array again.  NOTE!  If configSUPPORT_DYNAMIC_ALLOCATION
+                       is 0 then vPortFree() will be #defined to nothing. */
                        vPortFree( pxTaskStatusArray );
                }
                else
@@ -4030,7 +4184,9 @@ scheduler will re-enable the interrupts instead. */
                function is executing. */
                uxArraySize = uxCurrentNumberOfTasks;
 
-               /* Allocate an array index for each task. */
+               /* Allocate an array index for each task.  NOTE!  If
+               configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
+               equate to NULL. */
                pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );
 
                if( pxTaskStatusArray != NULL )
@@ -4096,7 +4252,8 @@ scheduler will re-enable the interrupts instead. */
                                mtCOVERAGE_TEST_MARKER();
                        }
 
-                       /* Free the array again. */
+                       /* Free the array again.  NOTE!  If configSUPPORT_DYNAMIC_ALLOCATION
+                       is 0 then vPortFree() will be #defined to nothing. */
                        vPortFree( pxTaskStatusArray );
                }
                else