9-FreeRTOS之静态内存分配与动态内存分配
admin
2024-03-22 16:17:20
0次

1-简介

从9.0版本之后,FreeRTOS允许用户自己定义内存的大小,允许在以下对象创建而不用创建内存的分配:
任务

  • 软件定时器
  • 队列
  • 事件组
  • 二进制信号量
  • 计数信号量
  • 递归信号量
  • 互斥体
    使用静态内存分配还是动态内存分配取决于应用程序,以及应用程序编写人员的偏好。这两种方法各有优缺点,可以在同一个RTOS应用程序中应用。在FreeRTOS/Source/WIN32-MSVC-Static-Allocation-Only 目录中目录下有对应的堆演示实验。

2- 动态创建FreeRTOS对象

  • 创建对象时需要的函数参数较少。
  • 内存分配在RTOS API函数中自动进行。
  • 应用程序编写人员不需要关心分配内存的问题。
  • 如果删除RTOS对象,则可以重新使用该对象所使用的RAM,从而可能减少应用程序的最大RAM占用。
  • RTOS API函数提供了返回堆使用信息的功能,允许优化堆大小。
  • 若
    configSUPPORT_DYNAMIC_ALLOCATION设置为1,那么下列API函数,可以使用动态内存分配创建:
xTaskCreate()
xQueueCreate()
xTimerCreate()
xEventGroupCreate()
xSemaphoreCreateBinary()
xSemaphoreCreateCounting()
xSemaphoreCreateMutex()
xSemaphoreCreateRecursiveMutex()

静态内存分配的创建

静态内存分配的好处是,可以更好的对应用程序的内存的利用,具有更好的控制代码的编写:

  • RTOS 对象可以放置在特定的内存位置。
  • 最大 RAM 占用空间可以在链路时确定,而不是 运行时。
  • 应用程序编写者不需要关心如何 处理内存分配失败的问题。
  • 它允许在不允许任何动态内存分配的应用程序中使用RTOS(尽管FreeRTOS包括可以克服大多数异议的分配方案)。
    如果configSUPPORT_STATIC_ALLOCATION设置为1,则下列API可用:
xTaskCreateStatic()
xQueueCreateStatic()
xTimerCreateStatic()
xEventGroupCreateStatic()
xSemaphoreCreateBinaryStatic()
xSemaphoreCreateCountingStatic()
xSemaphoreCreateMutexStatic()
xSemaphoreCreateRecursiveMutexStatic()

以上这些函数后面都会讲解。
下面是官方提供的有关上面这些函数的静态应用,自己可以看一下,我看了一下,基本上没有理解难度:

/** FreeRTOS V202011.00* Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.** Permission is hereby granted, free of charge, to any person obtaining a copy of* this software and associated documentation files (the "Software"), to deal in* the Software without restriction, including without limitation the rights to* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of* the Software, and to permit persons to whom the Software is furnished to do so,* subject to the following conditions:** The above copyright notice and this permission notice shall be included in all* copies or substantial portions of the Software.** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.** http://www.FreeRTOS.org* http://aws.amazon.com/freertos** 1 tab == 4 spaces!*//** Demonstrates how to create FreeRTOS objects using pre-allocated memory,* rather than the normal dynamically allocated memory, and tests objects being* created and deleted with both statically allocated memory and dynamically* allocated memory.** See http://www.FreeRTOS.org/Static_Vs_Dynamic_Memory_Allocation.html*//* Scheduler include files. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include "event_groups.h"
#include "timers.h"/* Demo program include files. */
#include "StaticAllocation.h"/* Exclude the entire file if configSUPPORT_STATIC_ALLOCATION is 0. */
#if( configSUPPORT_STATIC_ALLOCATION == 1 )/* The priority at which the task that performs the tests is created. */
#define staticTASK_PRIORITY					( tskIDLE_PRIORITY + 2 )/* The length of the queue, in items, not bytes, used in the queue static
allocation tests. */
#define staticQUEUE_LENGTH_IN_ITEMS			( 5 )/* A block time of 0 simply means "don't block". */
#define staticDONT_BLOCK					( ( TickType_t ) 0 )/* Binary semaphores have a maximum count of 1. */
#define staticBINARY_SEMAPHORE_MAX_COUNT	( 1 )/* The size of the stack used by the task that runs the tests. */
#define staticCREATOR_TASK_STACK_SIZE		( configMINIMAL_STACK_SIZE * 2 )/* The number of times the software timer will execute before stopping itself. */
#define staticMAX_TIMER_CALLBACK_EXECUTIONS	( 5 )/*-----------------------------------------------------------*//** The task that repeatedly creates and deletes statically allocated tasks, and* other RTOS objects.*/
static void prvStaticallyAllocatedCreator( void *pvParameters );/** The callback function used by the software timer that is repeatedly created* and deleted using both static and dynamically allocated memory.*/
static void prvTimerCallback( TimerHandle_t xExpiredTimer );/** A task that is created and deleted multiple times, using both statically and* dynamically allocated stack and TCB.*/
static void prvStaticallyAllocatedTask( void *pvParameters );/** A function that demonstrates and tests the API functions that create and* delete tasks using both statically and dynamically allocated TCBs and stacks.*/
static void prvCreateAndDeleteStaticallyAllocatedTasks( void );/** A function that demonstrates and tests the API functions that create and* delete event groups using both statically and dynamically allocated RAM.*/
static void prvCreateAndDeleteStaticallyAllocatedEventGroups( void );/** A function that demonstrates and tests the API functions that create and* delete queues using both statically and dynamically allocated RAM.*/
static void prvCreateAndDeleteStaticallyAllocatedQueues( void );/** A function that demonstrates and tests the API functions that create and* delete binary semaphores using both statically and dynamically allocated RAM.*/
static void prvCreateAndDeleteStaticallyAllocatedBinarySemaphores( void );/** A function that demonstrates and tests the API functions that create and* delete software timers using both statically and dynamically allocated RAM.*/
static void prvCreateAndDeleteStaticallyAllocatedTimers( void );/** A function that demonstrates and tests the API functions that create and* delete mutexes using both statically and dynamically allocated RAM.*/
static void prvCreateAndDeleteStaticallyAllocatedMutexes( void );/** A function that demonstrates and tests the API functions that create and* delete counting semaphores using both statically and dynamically allocated* RAM.*/
static void prvCreateAndDeleteStaticallyAllocatedCountingSemaphores( void );/** A function that demonstrates and tests the API functions that create and* delete recursive mutexes using both statically and dynamically allocated RAM.*/
static void prvCreateAndDeleteStaticallyAllocatedRecursiveMutexes( void );/** Utility function to create pseudo random numbers.*/
static UBaseType_t prvRand( void );/** The task that creates and deletes other tasks has to delay occasionally to* ensure lower priority tasks are not starved of processing time.  A pseudo* random delay time is used just to add a little bit of randomisation into the* execution pattern.  prvGetNextDelayTime() generates the pseudo random delay.*/
static TickType_t prvGetNextDelayTime( void );/** Checks the basic operation of a queue after it has been created.*/
static void prvSanityCheckCreatedQueue( QueueHandle_t xQueue );/** Checks the basic operation of a recursive mutex after it has been created.*/
static void prvSanityCheckCreatedRecursiveMutex( SemaphoreHandle_t xSemaphore );/** Checks the basic operation of a binary semaphore after it has been created.*/
static void prvSanityCheckCreatedSemaphore( SemaphoreHandle_t xSemaphore, UBaseType_t uxMaxCount );/** Checks the basic operation of an event group after it has been created.*/
static void prvSanityCheckCreatedEventGroup( EventGroupHandle_t xEventGroup );/*-----------------------------------------------------------*//* StaticTask_t is a publicly accessible structure that has the same size and
alignment requirements as the real TCB structure.  It is provided as a mechanism
for applications to know the size of the TCB (which is dependent on the
architecture and configuration file settings) without breaking the strict data
hiding policy by exposing the real TCB.  This StaticTask_t variable is passed
into the xTaskCreateStatic() function that creates the
prvStaticallyAllocatedCreator() task, and will hold the TCB of the created
tasks. */
static StaticTask_t xCreatorTaskTCBBuffer;/* This is the stack that will be used by the prvStaticallyAllocatedCreator()
task, which is itself created using statically allocated buffers (so without any
dynamic memory allocation). */
static StackType_t uxCreatorTaskStackBuffer[ staticCREATOR_TASK_STACK_SIZE ];/* Used by the pseudo random number generating function. */
static uint32_t ulNextRand = 0;/* Used so a check task can ensure this test is still executing, and not
stalled. */
static volatile UBaseType_t uxCycleCounter = 0;/* A variable that gets set to pdTRUE if an error is detected. */
static volatile BaseType_t xErrorOccurred = pdFALSE;/*-----------------------------------------------------------*/void vStartStaticallyAllocatedTasks( void  )
{/* Create a single task, which then repeatedly creates and deletes the otherRTOS objects using both statically and dynamically allocated RAM. */xTaskCreateStatic( prvStaticallyAllocatedCreator,		/* The function that implements the task being created. */"StatCreate",						/* Text name for the task - not used by the RTOS, its just to assist debugging. */staticCREATOR_TASK_STACK_SIZE,		/* Size of the buffer passed in as the stack - in words, not bytes! */NULL,								/* Parameter passed into the task - not used in this case. */staticTASK_PRIORITY,					/* Priority of the task. */&( uxCreatorTaskStackBuffer[ 0 ] ),  /* The buffer to use as the task's stack. */&xCreatorTaskTCBBuffer );			/* The variable that will hold the task's TCB. */
}
/*-----------------------------------------------------------*/static void prvStaticallyAllocatedCreator( void *pvParameters )
{/* Avoid compiler warnings. */( void ) pvParameters;for( ;; ){/* Loop, running functions that create and delete the various RTOSobjects that can be optionally created using either static or dynamicmemory allocation. */prvCreateAndDeleteStaticallyAllocatedTasks();prvCreateAndDeleteStaticallyAllocatedQueues();/* Delay to ensure lower priority tasks get CPU time, and increment thecycle counter so a 'check' task can determine that this task is stillexecuting. */vTaskDelay( prvGetNextDelayTime() );uxCycleCounter++;prvCreateAndDeleteStaticallyAllocatedBinarySemaphores();prvCreateAndDeleteStaticallyAllocatedCountingSemaphores();vTaskDelay( prvGetNextDelayTime() );uxCycleCounter++;prvCreateAndDeleteStaticallyAllocatedMutexes();prvCreateAndDeleteStaticallyAllocatedRecursiveMutexes();vTaskDelay( prvGetNextDelayTime() );uxCycleCounter++;prvCreateAndDeleteStaticallyAllocatedEventGroups();prvCreateAndDeleteStaticallyAllocatedTimers();}
}
/*-----------------------------------------------------------*/static void prvCreateAndDeleteStaticallyAllocatedCountingSemaphores( void )
{
SemaphoreHandle_t xSemaphore;
const UBaseType_t uxMaxCount = ( UBaseType_t ) 10;/* StaticSemaphore_t is a publicly accessible structure that has the same size
and alignment requirements as the real semaphore structure.  It is provided as a
mechanism for applications to know the size of the semaphore (which is dependent
on the architecture and configuration file settings) without breaking the strict
data hiding policy by exposing the real semaphore internals.  This
StaticSemaphore_t variable is passed into the xSemaphoreCreateCountingStatic()
function calls within this function.  NOTE: In most usage scenarios now it is
faster and more memory efficient to use a direct to task notification instead of
a counting semaphore.  http://www.freertos.org/RTOS-task-notifications.html */
StaticSemaphore_t xSemaphoreBuffer;/* Create the semaphore.  xSemaphoreCreateCountingStatic() has one moreparameter than the usual xSemaphoreCreateCounting() function.  The parameteris a pointer to the pre-allocated StaticSemaphore_t structure, which willhold information on the semaphore in an anonymous way.  If the pointer ispassed as NULL then the structure will be allocated dynamically, just aswhen xSemaphoreCreateCounting() is called. */xSemaphore = xSemaphoreCreateCountingStatic( uxMaxCount, 0, &xSemaphoreBuffer );/* The semaphore handle should equal the static semaphore structure passedinto the xSemaphoreCreateBinaryStatic() function. */configASSERT( xSemaphore == ( SemaphoreHandle_t ) &xSemaphoreBuffer );/* Ensure the semaphore passes a few sanity checks as a valid semaphore. */prvSanityCheckCreatedSemaphore( xSemaphore, uxMaxCount );/* Delete the semaphore again so the buffers can be reused. */vSemaphoreDelete( xSemaphore );#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 ){/* Now do the same but using dynamically allocated buffers to ensure thedelete functions are working correctly in both the static and dynamicallocation cases. */xSemaphore = xSemaphoreCreateCounting( uxMaxCount, 0 );configASSERT( xSemaphore != NULL );prvSanityCheckCreatedSemaphore( xSemaphore, uxMaxCount );vSemaphoreDelete( xSemaphore );}#endif
}
/*-----------------------------------------------------------*/static void prvCreateAndDeleteStaticallyAllocatedRecursiveMutexes( void )
{
SemaphoreHandle_t xSemaphore;/* StaticSemaphore_t is a publicly accessible structure that has the same size
and alignment requirements as the real semaphore structure.  It is provided as a
mechanism for applications to know the size of the semaphore (which is dependent
on the architecture and configuration file settings) without breaking the strict
data hiding policy by exposing the real semaphore internals.  This
StaticSemaphore_t variable is passed into the
xSemaphoreCreateRecursiveMutexStatic() function calls within this function. */
StaticSemaphore_t xSemaphoreBuffer;/* Create the semaphore.  xSemaphoreCreateRecursiveMutexStatic() has onemore parameter than the usual xSemaphoreCreateRecursiveMutex() function.The parameter is a pointer to the pre-allocated StaticSemaphore_t structure,which will hold information on the semaphore in an anonymous way.  If thepointer is passed as NULL then the structure will be allocated dynamically,just as	when xSemaphoreCreateRecursiveMutex() is called. */xSemaphore = xSemaphoreCreateRecursiveMutexStatic( &xSemaphoreBuffer );/* The semaphore handle should equal the static semaphore structure passedinto the xSemaphoreCreateBinaryStatic() function. */configASSERT( xSemaphore == ( SemaphoreHandle_t ) &xSemaphoreBuffer );/* Ensure the semaphore passes a few sanity checks as a validrecursive semaphore. */prvSanityCheckCreatedRecursiveMutex( xSemaphore );/* Delete the semaphore again so the buffers can be reused. */vSemaphoreDelete( xSemaphore );/* Now do the same using dynamically allocated buffers to ensure the deletefunctions are working correctly in both the static and dynamic memoryallocation cases. */#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 ){xSemaphore = xSemaphoreCreateRecursiveMutex();configASSERT( xSemaphore != NULL );prvSanityCheckCreatedRecursiveMutex( xSemaphore );vSemaphoreDelete( xSemaphore );}#endif
}
/*-----------------------------------------------------------*/static void prvCreateAndDeleteStaticallyAllocatedQueues( void )
{
QueueHandle_t xQueue;/* StaticQueue_t is a publicly accessible structure that has the same size and
alignment requirements as the real queue structure.  It is provided as a
mechanism for applications to know the size of the queue (which is dependent on
the architecture and configuration file settings) without breaking the strict
data hiding policy by exposing the real queue internals.  This StaticQueue_t
variable is passed into the xQueueCreateStatic() function calls within this
function. */
static StaticQueue_t xStaticQueue;/* The queue storage area must be large enough to hold the maximum number of
items it is possible for the queue to hold at any one time, which equals the
queue length (in items, not bytes) multiplied by the size of each item.  In this
case the queue will hold staticQUEUE_LENGTH_IN_ITEMS 64-bit items.  See
http://www.freertos.org/Embedded-RTOS-Queues.html */
static uint8_t ucQueueStorageArea[ staticQUEUE_LENGTH_IN_ITEMS * sizeof( uint64_t ) ];/* Create the queue.  xQueueCreateStatic() has two more parameters than theusual xQueueCreate() function.  The first new parameter is a pointer to thepre-allocated queue storage area.  The second new parameter is a pointer tothe StaticQueue_t structure that will hold the queue state information inan anonymous way.  If the two pointers are passed as NULL then the datawill be allocated dynamically as if xQueueCreate() had been called. */xQueue = xQueueCreateStatic( staticQUEUE_LENGTH_IN_ITEMS, /* The maximum number of items the queue can hold. */sizeof( uint64_t ), /* The size of each item. */ucQueueStorageArea, /* The buffer used to hold items within the queue. */&xStaticQueue );	 /* The static queue structure that will hold the state of the queue. *//* The queue handle should equal the static queue structure passed into thexQueueCreateStatic() function. */configASSERT( xQueue == ( QueueHandle_t ) &xStaticQueue );/* Ensure the queue passes a few sanity checks as a valid queue. */prvSanityCheckCreatedQueue( xQueue );/* Delete the queue again so the buffers can be reused. */vQueueDelete( xQueue );/* Now do the same using a dynamically allocated queue to ensure the deletefunction is working correctly in both the static and dynamic memoryallocation cases. */#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 ){xQueue = xQueueCreate( staticQUEUE_LENGTH_IN_ITEMS, /* The maximum number of items the queue can hold. */sizeof( uint64_t ) ); 		/* The size of each item. *//* The queue handle should equal the static queue structure passed into thexQueueCreateStatic() function. */configASSERT( xQueue != NULL );/* Ensure the queue passes a few sanity checks as a valid queue. */prvSanityCheckCreatedQueue( xQueue );/* Delete the queue again so the buffers can be reused. */vQueueDelete( xQueue );}#endif
}
/*-----------------------------------------------------------*/static void prvCreateAndDeleteStaticallyAllocatedMutexes( void )
{
SemaphoreHandle_t xSemaphore;
BaseType_t xReturned;/* StaticSemaphore_t is a publicly accessible structure that has the same size
and alignment requirements as the real semaphore structure.  It is provided as a
mechanism for applications to know the size of the semaphore (which is dependent
on the architecture and configuration file settings) without breaking the strict
data hiding policy by exposing the real semaphore internals.  This
StaticSemaphore_t variable is passed into the xSemaphoreCreateMutexStatic()
function calls within this function. */
StaticSemaphore_t xSemaphoreBuffer;/* Create the semaphore.  xSemaphoreCreateMutexStatic() has one moreparameter than the usual xSemaphoreCreateMutex() function.  The parameteris a pointer to the pre-allocated StaticSemaphore_t structure, which willhold information on the semaphore in an anonymous way.  If the pointer ispassed as NULL then the structure will be allocated dynamically, just aswhen xSemaphoreCreateMutex() is called. */xSemaphore = xSemaphoreCreateMutexStatic( &xSemaphoreBuffer );/* The semaphore handle should equal the static semaphore structure passedinto the xSemaphoreCreateMutexStatic() function. */configASSERT( xSemaphore == ( SemaphoreHandle_t ) &xSemaphoreBuffer );/* Take the mutex so the mutex is in the state expected by theprvSanityCheckCreatedSemaphore() function. */xReturned = xSemaphoreTake( xSemaphore, staticDONT_BLOCK );if( xReturned != pdPASS ){xErrorOccurred = pdTRUE;}/* Ensure the semaphore passes a few sanity checks as a valid semaphore. */prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT );/* Delete the semaphore again so the buffers can be reused. */vSemaphoreDelete( xSemaphore );/* Now do the same using a dynamically allocated mutex to ensure the deletefunction is working correctly in both the static and dynamic allocationcases. */#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 ){xSemaphore = xSemaphoreCreateMutex();/* The semaphore handle should equal the static semaphore structurepassed into the xSemaphoreCreateMutexStatic() function. */configASSERT( xSemaphore != NULL );/* Take the mutex so the mutex is in the state expected by theprvSanityCheckCreatedSemaphore() function. */xReturned = xSemaphoreTake( xSemaphore, staticDONT_BLOCK );if( xReturned != pdPASS ){xErrorOccurred = pdTRUE;}/* Ensure the semaphore passes a few sanity checks as a valid semaphore. */prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT );/* Delete the semaphore again so the buffers can be reused. */vSemaphoreDelete( xSemaphore );}#endif
}
/*-----------------------------------------------------------*/static void prvCreateAndDeleteStaticallyAllocatedBinarySemaphores( void )
{
SemaphoreHandle_t xSemaphore;/* StaticSemaphore_t is a publicly accessible structure that has the same size
and alignment requirements as the real semaphore structure.  It is provided as a
mechanism for applications to know the size of the semaphore (which is dependent
on the architecture and configuration file settings) without breaking the strict
data hiding policy by exposing the real semaphore internals.  This
StaticSemaphore_t variable is passed into the xSemaphoreCreateBinaryStatic()
function calls within this function.  NOTE: In most usage scenarios now it is
faster and more memory efficient to use a direct to task notification instead of
a binary semaphore.  http://www.freertos.org/RTOS-task-notifications.html */
StaticSemaphore_t xSemaphoreBuffer;/* Create the semaphore.  xSemaphoreCreateBinaryStatic() has one moreparameter than the usual xSemaphoreCreateBinary() function.  The parameteris a pointer to the pre-allocated StaticSemaphore_t structure, which willhold information on the semaphore in an anonymous way.  If the pointer ispassed as NULL then the structure will be allocated dynamically, just aswhen xSemaphoreCreateBinary() is called. */xSemaphore = xSemaphoreCreateBinaryStatic( &xSemaphoreBuffer );/* The semaphore handle should equal the static semaphore structure passedinto the xSemaphoreCreateBinaryStatic() function. */configASSERT( xSemaphore == ( SemaphoreHandle_t ) &xSemaphoreBuffer );/* Ensure the semaphore passes a few sanity checks as a valid semaphore. */prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT );/* Delete the semaphore again so the buffers can be reused. */vSemaphoreDelete( xSemaphore );/* Now do the same using a dynamically allocated semaphore to check thedelete function is working correctly in both the static and dynamicallocation cases. */#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 ){xSemaphore = xSemaphoreCreateBinary();configASSERT( xSemaphore != NULL );prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT );vSemaphoreDelete( xSemaphore );}#endif/* There isn't a static version of the old and deprecatedvSemaphoreCreateBinary() macro (because its deprecated!), but check it isstill functioning correctly. */#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 ){vSemaphoreCreateBinary( xSemaphore );/* The macro starts with the binary semaphore available, but the testfunction expects it to be unavailable. */if( xSemaphoreTake( xSemaphore, staticDONT_BLOCK ) == pdFAIL ){xErrorOccurred = pdTRUE;}prvSanityCheckCreatedSemaphore( xSemaphore, staticBINARY_SEMAPHORE_MAX_COUNT );vSemaphoreDelete( xSemaphore );}#endif
}
/*-----------------------------------------------------------*/static void prvTimerCallback( TimerHandle_t xExpiredTimer )
{
UBaseType_t *puxVariableToIncrement;
BaseType_t xReturned;/* The timer callback just demonstrates it is executing by incrementing avariable - the address of which is passed into the timer as its ID.  Obtainthe address of the variable to increment. */puxVariableToIncrement = ( UBaseType_t * ) pvTimerGetTimerID( xExpiredTimer );/* Increment the variable to show the timer callback has executed. */( *puxVariableToIncrement )++;/* If this callback has executed the required number of times, stop thetimer. */if( *puxVariableToIncrement == staticMAX_TIMER_CALLBACK_EXECUTIONS ){/* This is called from a timer callback so must not block.  Seehttp://www.FreeRTOS.org/FreeRTOS-timers-xTimerStop.html */xReturned = xTimerStop( xExpiredTimer, staticDONT_BLOCK );if( xReturned != pdPASS ){xErrorOccurred = pdTRUE;}}
}
/*-----------------------------------------------------------*/static void prvCreateAndDeleteStaticallyAllocatedTimers( void )
{
TimerHandle_t xTimer;
UBaseType_t uxVariableToIncrement;
const TickType_t xTimerPeriod = pdMS_TO_TICKS( 20 );
BaseType_t xReturned;/* StaticTimer_t is a publicly accessible structure that has the same size
and alignment requirements as the real timer structure.  It is provided as a
mechanism for applications to know the size of the timer structure (which is
dependent on the architecture and configuration file settings) without breaking
the strict data hiding policy by exposing the real timer internals.  This
StaticTimer_t variable is passed into the xTimerCreateStatic() function calls
within this function. */
StaticTimer_t xTimerBuffer;/* Create the software time.  xTimerCreateStatic() has an extra parameterthan the normal xTimerCreate() API function.  The parameter is a pointer tothe StaticTimer_t structure that will hold the software timer structure.  Ifthe parameter is passed as NULL then the structure will be allocateddynamically, just as if xTimerCreate() had been called. */xTimer = xTimerCreateStatic( "T1",					/* Text name for the task.  Helps debugging only.  Not used by FreeRTOS. */xTimerPeriod,			/* The period of the timer in ticks. */pdTRUE,				/* This is an auto-reload timer. */( void * ) &uxVariableToIncrement,	/* The variable incremented by the test is passed into the timer callback using the timer ID. */prvTimerCallback,		/* The function to execute when the timer expires. */&xTimerBuffer );		/* The buffer that will hold the software timer structure. *//* The timer handle should equal the static timer structure passed into thexTimerCreateStatic() function. */configASSERT( xTimer == ( TimerHandle_t ) &xTimerBuffer );/* Set the variable to 0, wait for a few timer periods to expire, then checkthe timer callback has incremented the variable to the expected value. */uxVariableToIncrement = 0;/* This is a low priority so a block time should not be needed. */xReturned = xTimerStart( xTimer, staticDONT_BLOCK );if( xReturned != pdPASS ){xErrorOccurred = pdTRUE;}vTaskDelay( xTimerPeriod * staticMAX_TIMER_CALLBACK_EXECUTIONS );/* By now the timer should have expired staticMAX_TIMER_CALLBACK_EXECUTIONStimes, and then stopped itself. */if( uxVariableToIncrement != staticMAX_TIMER_CALLBACK_EXECUTIONS ){xErrorOccurred = pdTRUE;}/* Finished with the timer, delete it. */xReturned = xTimerDelete( xTimer, staticDONT_BLOCK );/* Again, as this is a low priority task it is expected that the timercommand will have been sent even without a block time being used. */if( xReturned != pdPASS ){xErrorOccurred = pdTRUE;}/* Just to show the check task that this task is still executing. */uxCycleCounter++;/* Now do the same using a dynamically allocated software timer to ensurethe delete function is working correctly in both the static and dynamicallocation cases. */#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 ){xTimer = xTimerCreate( "T1",								/* Text name for the task.  Helps debugging only.  Not used by FreeRTOS. */xTimerPeriod,						/* The period of the timer in ticks. */pdTRUE,								/* This is an auto-reload timer. */( void * ) &uxVariableToIncrement,	/* The variable incremented by the test is passed into the timer callback using the timer ID. */prvTimerCallback );					/* The function to execute when the timer expires. */configASSERT( xTimer != NULL );uxVariableToIncrement = 0;xReturned = xTimerStart( xTimer, staticDONT_BLOCK );if( xReturned != pdPASS ){xErrorOccurred = pdTRUE;}vTaskDelay( xTimerPeriod * staticMAX_TIMER_CALLBACK_EXECUTIONS );if( uxVariableToIncrement != staticMAX_TIMER_CALLBACK_EXECUTIONS ){xErrorOccurred = pdTRUE;}xReturned = xTimerDelete( xTimer, staticDONT_BLOCK );if( xReturned != pdPASS ){xErrorOccurred = pdTRUE;}}#endif
}
/*-----------------------------------------------------------*/static void prvCreateAndDeleteStaticallyAllocatedEventGroups( void )
{
EventGroupHandle_t xEventGroup;/* StaticEventGroup_t is a publicly accessible structure that has the same size
and alignment requirements as the real event group structure.  It is provided as
a mechanism for applications to know the size of the event group (which is
dependent on the architecture and configuration file settings) without breaking
the strict data hiding policy by exposing the real event group internals.  This
StaticEventGroup_t variable is passed into the xSemaphoreCreateEventGroupStatic()
function calls within this function. */
StaticEventGroup_t xEventGroupBuffer;/* Create the event group.  xEventGroupCreateStatic() has an extra parameterthan the normal xEventGroupCreate() API function.  The parameter is apointer to the StaticEventGroup_t structure that will hold the event groupstructure. */xEventGroup = xEventGroupCreateStatic( &xEventGroupBuffer );/* The event group handle should equal the static event group structurepassed into the xEventGroupCreateStatic() function. */configASSERT( xEventGroup == ( EventGroupHandle_t ) &xEventGroupBuffer );/* Ensure the event group passes a few sanity checks as a valid eventgroup. */prvSanityCheckCreatedEventGroup( xEventGroup );/* Delete the event group again so the buffers can be reused. */vEventGroupDelete( xEventGroup );/* Now do the same using a dynamically allocated event group to ensure thedelete function is working correctly in both the static and dynamicallocation cases. */#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 ){xEventGroup = xEventGroupCreate();configASSERT( xEventGroup != NULL );prvSanityCheckCreatedEventGroup( xEventGroup );vEventGroupDelete( xEventGroup );}#endif
}
/*-----------------------------------------------------------*/static void prvCreateAndDeleteStaticallyAllocatedTasks( void )
{
TaskHandle_t xCreatedTask;/* The variable that will hold the TCB of tasks created by this function.  See
the comments above the declaration of the xCreatorTaskTCBBuffer variable for
more information.  NOTE:  This is not static so relies on the tasks that use it
being deleted before this function returns and deallocates its stack.  That will
only be the case if configUSE_PREEMPTION is set to 1. */
StaticTask_t xTCBBuffer;/* This buffer that will be used as the stack of tasks created by this function.
See the comments above the declaration of the uxCreatorTaskStackBuffer[] array
above for more information. */
static StackType_t uxStackBuffer[ configMINIMAL_STACK_SIZE ];/* Create the task.  xTaskCreateStatic() has two more parameters thanthe usual xTaskCreate() function.  The first new parameter is a pointer tothe pre-allocated stack.  The second new parameter is a pointer to theStaticTask_t structure that will hold the task's TCB.  If both pointers arepassed as NULL then the respective object will be allocated dynamically asif xTaskCreate() had been called. */xCreatedTask = xTaskCreateStatic(prvStaticallyAllocatedTask, 	/* Function that implements the task. */"Static",						/* Human readable name for the task. */configMINIMAL_STACK_SIZE,		/* Task's stack size, in words (not bytes!). */NULL,							/* Parameter to pass into the task. */uxTaskPriorityGet( NULL ) + 1,	/* The priority of the task. */&( uxStackBuffer[ 0 ] ),		/* The buffer to use as the task's stack. */&xTCBBuffer );					/* The variable that will hold that task's TCB. *//* Check the task was created correctly, then delete the task. */if( xCreatedTask == NULL ){xErrorOccurred = pdTRUE;}else if( eTaskGetState( xCreatedTask ) != eSuspended ){/* The created task had a higher priority so should have executed andsuspended itself by now. */xErrorOccurred = pdTRUE;}else{vTaskDelete( xCreatedTask );}/* Now do the same using a dynamically allocated task to ensure the deletefunction is working correctly in both the static and dynamic allocationcases. */#if( configSUPPORT_DYNAMIC_ALLOCATION == 1 ){BaseType_t xReturned;xReturned = xTaskCreate(prvStaticallyAllocatedTask,		/* Function that implements the task - the same function is used but is actually dynamically allocated this time. */"Static",						/* Human readable name for the task. */configMINIMAL_STACK_SIZE,		/* Task's stack size, in words (not bytes!). */NULL,							/* Parameter to pass into the task. */uxTaskPriorityGet( NULL ) + 1,	/* The priority of the task. */&xCreatedTask );				/* Handle of the task being created. */if( eTaskGetState( xCreatedTask ) != eSuspended ){xErrorOccurred = pdTRUE;}configASSERT( xReturned == pdPASS );if( xReturned != pdPASS ){xErrorOccurred = pdTRUE;}vTaskDelete( xCreatedTask );}#endif
}
/*-----------------------------------------------------------*/static void prvStaticallyAllocatedTask( void *pvParameters )
{( void ) pvParameters;/* The created task just suspends itself to wait to get deleted.  The taskthat creates this task checks this task is in the expected Suspended statebefore deleting it. */vTaskSuspend( NULL );
}
/*-----------------------------------------------------------*/static UBaseType_t prvRand( void )
{
const uint32_t ulMultiplier = 0x015a4e35UL, ulIncrement = 1UL;/* Utility function to generate a pseudo random number. */ulNextRand = ( ulMultiplier * ulNextRand ) + ulIncrement;return( ( ulNextRand >> 16UL ) & 0x7fffUL );
}
/*-----------------------------------------------------------*/static TickType_t prvGetNextDelayTime( void )
{
TickType_t xNextDelay;
const TickType_t xMaxDelay = pdMS_TO_TICKS( ( TickType_t ) 150 );
const TickType_t xMinDelay = pdMS_TO_TICKS( ( TickType_t ) 75 );
const TickType_t xTinyDelay = pdMS_TO_TICKS( ( TickType_t ) 2 );/* Generate the next delay time.  This is kept within a narrow band so asnot to disturb the timing of other tests - but does add in some pseudorandomisation into the tests. */do{xNextDelay = prvRand() % xMaxDelay;/* Just in case this loop is executed lots of times. */vTaskDelay( xTinyDelay );} while ( xNextDelay < xMinDelay );return xNextDelay;
}
/*-----------------------------------------------------------*/static void prvSanityCheckCreatedEventGroup( EventGroupHandle_t xEventGroup )
{
EventBits_t xEventBits;
const EventBits_t xFirstTestBits = ( EventBits_t ) 0xaa, xSecondTestBits = ( EventBits_t ) 0x55;/* The event group should not have any bits set yet. */xEventBits = xEventGroupGetBits( xEventGroup );if( xEventBits != ( EventBits_t ) 0 ){xErrorOccurred = pdTRUE;}/* Some some bits, then read them back to check they are as expected. */xEventGroupSetBits( xEventGroup, xFirstTestBits );xEventBits = xEventGroupGetBits( xEventGroup );if( xEventBits != xFirstTestBits ){xErrorOccurred = pdTRUE;}xEventGroupSetBits( xEventGroup, xSecondTestBits );xEventBits = xEventGroupGetBits( xEventGroup );if( xEventBits != ( xFirstTestBits | xSecondTestBits ) ){xErrorOccurred = pdTRUE;}/* Finally try clearing some bits too and check that operation proceeds asexpected. */xEventGroupClearBits( xEventGroup, xFirstTestBits );xEventBits = xEventGroupGetBits( xEventGroup );if( xEventBits != xSecondTestBits ){xErrorOccurred = pdTRUE;}
}
/*-----------------------------------------------------------*/static void prvSanityCheckCreatedSemaphore( SemaphoreHandle_t xSemaphore, UBaseType_t uxMaxCount )
{
BaseType_t xReturned;
UBaseType_t x;
const TickType_t xShortBlockTime = pdMS_TO_TICKS( 10 );
TickType_t xTickCount;/* The binary semaphore should start 'empty', so a call to xSemaphoreTake()should fail. */xTickCount = xTaskGetTickCount();xReturned = xSemaphoreTake( xSemaphore, xShortBlockTime );if( ( ( TickType_t ) ( xTaskGetTickCount() - xTickCount ) ) < xShortBlockTime ){/* Did not block on the semaphore as long as expected. */xErrorOccurred = pdTRUE;}if( xReturned != pdFAIL ){xErrorOccurred = pdTRUE;}/* Should be possible to 'give' the semaphore up to a maximum of uxMaxCounttimes. */for( x = 0; x < uxMaxCount; x++ ){xReturned = xSemaphoreGive( xSemaphore );if( xReturned == pdFAIL ){xErrorOccurred = pdTRUE;}}/* Giving the semaphore again should fail, as it is 'full'. */xReturned = xSemaphoreGive( xSemaphore );if( xReturned != pdFAIL ){xErrorOccurred = pdTRUE;}configASSERT( uxSemaphoreGetCount( xSemaphore ) == uxMaxCount );/* Should now be possible to 'take' the semaphore up to a maximum ofuxMaxCount times without blocking. */for( x = 0; x < uxMaxCount; x++ ){xReturned = xSemaphoreTake( xSemaphore, staticDONT_BLOCK );if( xReturned == pdFAIL ){xErrorOccurred = pdTRUE;}}/* Back to the starting condition, where the semaphore should not beavailable. */xTickCount = xTaskGetTickCount();xReturned = xSemaphoreTake( xSemaphore, xShortBlockTime );if( ( ( TickType_t ) ( xTaskGetTickCount() - xTickCount ) ) < xShortBlockTime ){/* Did not block on the semaphore as long as expected. */xErrorOccurred = pdTRUE;}if( xReturned != pdFAIL ){xErrorOccurred = pdTRUE;}configASSERT( uxSemaphoreGetCount( xSemaphore ) == 0 );
}
/*-----------------------------------------------------------*/static void prvSanityCheckCreatedQueue( QueueHandle_t xQueue )
{
uint64_t ull, ullRead;
BaseType_t xReturned, xLoop;/* This test is done twice to ensure the queue storage area wraps. */for( xLoop = 0; xLoop < 2; xLoop++ ){/* A very basic test that the queue can be written to and read from asexpected.  First the queue should be empty. */xReturned = xQueueReceive( xQueue, &ull, staticDONT_BLOCK );if( xReturned != errQUEUE_EMPTY ){xErrorOccurred = pdTRUE;}/* Now it should be possible to write to the queue staticQUEUE_LENGTH_IN_ITEMStimes. */for( ull = 0; ull < staticQUEUE_LENGTH_IN_ITEMS; ull++ ){xReturned = xQueueSend( xQueue, &ull, staticDONT_BLOCK );if( xReturned != pdPASS ){xErrorOccurred = pdTRUE;}}/* Should not now be possible to write to the queue again. */xReturned = xQueueSend( xQueue, &ull, staticDONT_BLOCK );if( xReturned != errQUEUE_FULL ){xErrorOccurred = pdTRUE;}/* Now read back from the queue to ensure the data read back matches thatwritten. */for( ull = 0; ull < staticQUEUE_LENGTH_IN_ITEMS; ull++ ){xReturned = xQueueReceive( xQueue, &ullRead, staticDONT_BLOCK );if( xReturned != pdPASS ){xErrorOccurred = pdTRUE;}if( ullRead != ull ){xErrorOccurred = pdTRUE;}}/* The queue should be empty again. */xReturned = xQueueReceive( xQueue, &ull, staticDONT_BLOCK );if( xReturned != errQUEUE_EMPTY ){xErrorOccurred = pdTRUE;}}
}
/*-----------------------------------------------------------*/static void prvSanityCheckCreatedRecursiveMutex( SemaphoreHandle_t xSemaphore )
{
const BaseType_t xLoops = 5;
BaseType_t x, xReturned;/* A very basic test that the recursive semaphore behaved like a recursivesemaphore. First the semaphore should not be able to be given, as it has notyet been taken. */xReturned = xSemaphoreGiveRecursive( xSemaphore );if( xReturned != pdFAIL ){xErrorOccurred = pdTRUE;}/* Now it should be possible to take the mutex a number of times. */for( x = 0; x < xLoops; x++ ){xReturned = xSemaphoreTakeRecursive( xSemaphore, staticDONT_BLOCK );if( xReturned != pdPASS ){xErrorOccurred = pdTRUE;}}/* Should be possible to give the semaphore the same number of times as itwas given in the loop above. */for( x = 0; x < xLoops; x++ ){xReturned = xSemaphoreGiveRecursive( xSemaphore );if( xReturned != pdPASS ){xErrorOccurred = pdTRUE;}}/* No more gives should be possible though. */xReturned = xSemaphoreGiveRecursive( xSemaphore );if( xReturned != pdFAIL ){xErrorOccurred = pdTRUE;}
}
/*-----------------------------------------------------------*/BaseType_t xAreStaticAllocationTasksStillRunning( void )
{
static UBaseType_t uxLastCycleCounter = 0;
BaseType_t xReturn;if( uxCycleCounter == uxLastCycleCounter ){xErrorOccurred = pdTRUE;}else{uxLastCycleCounter = uxCycleCounter;}if( xErrorOccurred != pdFALSE ){xReturn = pdFAIL;}else{xReturn = pdPASS;}return xReturn;
}
/*-----------------------------------------------------------*//* Exclude the entire file if configSUPPORT_STATIC_ALLOCATION is 0. */
#endif /* configSUPPORT_STATIC_ALLOCATION == 1 */

相关内容

热门资讯

AI做攻略、“双向奔赴”……国... 本文转自:人民网人民网记者 许维娜今年国庆假期,AI(人工智能)成了不少年轻人的“旅行搭子”。行前做...
合肥的这个“梗”, 翻篇了 社交平台的评论区,曾长期流传一句专属于合肥的黑色幽默。有人问合肥哪里好玩,本地网友半开玩笑地回复:合...
7万+旅游中高层都在的群,今天... 做旅游这行,资源卡位、渠道互通、项目落地——往往就差一个“对的机会”。我们的行业社群已聚集70,00...
一场帐篷音乐节,如何让井研从“... 帐篷音乐节现场封面新闻记者 周洪攀10月的研溪湿地,晚风里裹着柑橘的清甜与民谣的回响。夜幕降临,10...
白鹤翩翩落星愿,上海迪士尼度假... 10月9日,世界候鸟日前夕,上海迪士尼度假区星愿公园的湖风里多了一分白鹤的灵动。三座白鹤雕塑正式揭幕...
成都锦江边赏初秋枫叶飘飘 目前,成都锦江边国华街枫叶树红了。近年来,国华街附近的几条街都栽种了红枫树,是城区较集中看枫叶的点位...
GBTA:商务旅行正在复苏,但... 亚太正在成为全球商务旅行增长的重要引擎。在经历疫情后的快速反弹之后,全球商务旅行正在进入一个新的增长...
从景区焕新到节赛引流 邹城双节... 齐鲁网·闪电新闻10月9日讯 中秋、国庆“双节”假期,邹城市文化和旅游局提前谋划、周密部署,立足自身...
江山如此多娇 且看石楼今朝 金秋十月,黄河岸畔秋意盎然,丰收盛会喜迎国庆。本次石楼国庆文旅活动以“江山如此多娇,且看石楼今朝”为...
海南国庆假期接待游客475.4... 2026年国庆假期,海南接待游客475.40万人次,较2025年增长13.1%,较2024年增长15...