Skip to content

Commit

Permalink
Further simplifications of FreeRTOS
Browse files Browse the repository at this point in the history
  • Loading branch information
ckormanyos committed Feb 6, 2025
1 parent e2c9fc6 commit 7c64e84
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 359 deletions.
4 changes: 3 additions & 1 deletion examples/chapter11_07/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,15 @@ The port for the ATmega328P target can be found in
This OS-portable part has been modified for this example
via changes including switching from a C to a C++ file,
cleaning up spaces, tabs and alignment of typing.
In addition, some configuration macros and config checks
have been removed simplification purposes.
The OS tick uses `compare-match-a` from `timer1` to generate
a tick interrupt at $100~\text{Hz}$ (i.e., approximately every $10~\text{ms}$)
using an undecorated ISR handle named `__vector_11`.
When using the simulated host (such as `_MSC_VER` or GCC on `x86_64`),
FreeRTOS is not used, but rather the C++ `<thread>` and `<chrono>` libraries
are used for simulated task scheduling. The `<thread>` library
are used for task timing and scheduling. The `<thread>` library
does not use adjustable priorities in this case, but the thread scheduler
supports preemptive scheduling and emulates the desired timing quite well.
Expand Down
41 changes: 2 additions & 39 deletions examples/chapter11_07/src/os/FreeRTOS/Source/event_groups.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,9 @@ static BaseType_t prvTestWaitCondition( const EventBits_t uxCurrentEventBits,
EventGroup_t * pxEventBits;

/* A StaticEventGroup_t object must be provided. */
configASSERT( pxEventGroupBuffer );

#if ( configASSERT_DEFINED == 1 )
{
/* Sanity check that the size of the structure used to declare a
* variable of type StaticEventGroup_t equals the size of the real
* event group structure. */
volatile size_t xSize = sizeof( StaticEventGroup_t );
configASSERT( xSize == sizeof( EventGroup_t ) );
} /*lint !e529 xSize is referenced if configASSERT() is defined. */
#endif /* configASSERT_DEFINED */

/* The user has provided a statically allocated event group - use it. */
pxEventBits = ( EventGroup_t * ) pxEventGroupBuffer; /*lint !e740 !e9087 EventGroup_t and StaticEventGroup_t are deliberately aliased for data hiding purposes and guaranteed to have the same size and alignment requirement - checked by configASSERT(). */
pxEventBits = ( EventGroup_t * ) pxEventGroupBuffer; /*lint !e740 !e9087 EventGroup_t and StaticEventGroup_t are deliberately aliased for data hiding purposes and guaranteed to have the same size and alignment requirement. */

if( pxEventBits != NULL )
{
Expand Down Expand Up @@ -199,14 +188,6 @@ EventBits_t xEventGroupSync( EventGroupHandle_t xEventGroup,
BaseType_t xAlreadyYielded;
BaseType_t xTimeoutOccurred = pdFALSE;

configASSERT( ( uxBitsToWaitFor & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
configASSERT( uxBitsToWaitFor != 0 );
#if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
{
configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
}
#endif

vTaskSuspendAll();
{
uxOriginalBitValue = pxEventBits->uxEventBits;
Expand Down Expand Up @@ -325,14 +306,6 @@ EventBits_t xEventGroupWaitBits( EventGroupHandle_t xEventGroup,

/* Check the user is not attempting to wait on the bits used by the kernel
* itself, and that at least one bit is being requested. */
configASSERT( xEventGroup );
configASSERT( ( uxBitsToWaitFor & eventEVENT_BITS_CONTROL_BYTES ) == 0 );
configASSERT( uxBitsToWaitFor != 0 );
#if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
{
configASSERT( !( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) && ( xTicksToWait != 0 ) ) );
}
#endif

vTaskSuspendAll();
{
Expand Down Expand Up @@ -474,11 +447,6 @@ EventBits_t xEventGroupClearBits( EventGroupHandle_t xEventGroup,
EventGroup_t * pxEventBits = xEventGroup;
EventBits_t uxReturn;

/* Check the user is not attempting to clear the bits used by the kernel
* itself. */
configASSERT( xEventGroup );
configASSERT( ( uxBitsToClear & eventEVENT_BITS_CONTROL_BYTES ) == 0 );

taskENTER_CRITICAL();
{
traceEVENT_GROUP_CLEAR_BITS( xEventGroup, uxBitsToClear );
Expand Down Expand Up @@ -538,11 +506,6 @@ EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup,
EventGroup_t * pxEventBits = xEventGroup;
BaseType_t xMatchFound = pdFALSE;

/* Check the user is not attempting to set the bits used by the kernel
* itself. */
configASSERT( xEventGroup );
configASSERT( ( uxBitsToSet & eventEVENT_BITS_CONTROL_BYTES ) == 0 );

pxList = &( pxEventBits->xTasksWaitingForBits );
pxListEnd = listGET_END_MARKER( pxList ); /*lint !e826 !e740 !e9087 The mini list structure is used as the list end to save RAM. This is checked and valid. */
vTaskSuspendAll();
Expand Down Expand Up @@ -636,7 +599,7 @@ void vEventGroupDelete( EventGroupHandle_t xEventGroup )
{
/* Unblock the task, returning 0 as the event list is being deleted
* and cannot therefore have any bits set. */
configASSERT( pxTasksWaitingForBits->xListEnd.pxNext != ( const ListItem_t * ) &( pxTasksWaitingForBits->xListEnd ) );

vTaskRemoveFromUnorderedEventList( pxTasksWaitingForBits->xListEnd.pxNext, eventUNBLOCKED_DUE_TO_BIT_SET );
}

Expand Down
19 changes: 0 additions & 19 deletions examples/chapter11_07/src/os/FreeRTOS/Source/include/FreeRTOS.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,25 +219,6 @@
#error configMAX_TASK_NAME_LEN must be set to a minimum of 1 in FreeRTOSConfig.h
#endif

#ifndef configASSERT
#define configASSERT( x )
#define configASSERT_DEFINED 0
#else
#define configASSERT_DEFINED 1
#endif

/* configPRECONDITION should be defined as configASSERT.
* The CBMC proofs need a way to track assumptions and assertions.
* A configPRECONDITION statement should express an implicit invariant or
* assumption made. A configASSERT statement should express an invariant that must
* hold explicit before calling the code. */
#ifndef configPRECONDITION
#define configPRECONDITION( X ) configASSERT( X )
#define configPRECONDITION_DEFINED 0
#else
#define configPRECONDITION_DEFINED 1
#endif

#ifndef portMEMORY_BARRIER
#define portMEMORY_BARRIER()
#endif
Expand Down
4 changes: 2 additions & 2 deletions examples/chapter11_07/src/os/FreeRTOS/Source/include/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@

/* Define macros that will assert if one of the structure members does not
* contain its expected value. */
#define listTEST_LIST_ITEM_INTEGRITY( pxItem ) configASSERT( ( ( pxItem )->xListItemIntegrityValue1 == pdINTEGRITY_CHECK_VALUE ) && ( ( pxItem )->xListItemIntegrityValue2 == pdINTEGRITY_CHECK_VALUE ) )
#define listTEST_LIST_INTEGRITY( pxList ) configASSERT( ( ( pxList )->xListIntegrityValue1 == pdINTEGRITY_CHECK_VALUE ) && ( ( pxList )->xListIntegrityValue2 == pdINTEGRITY_CHECK_VALUE ) )
#define listTEST_LIST_ITEM_INTEGRITY( pxItem )
#define listTEST_LIST_INTEGRITY( pxList )
#endif /* configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES */


Expand Down
10 changes: 2 additions & 8 deletions examples/chapter11_07/src/os/FreeRTOS/Source/include/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -520,10 +520,7 @@ typedef struct QueueDefinition * QueueSetMemberHandle_t;
* QueueHandle_t xQueue;
* uint32_t ulVarToSend, ulValReceived;
*
* // Create a queue to hold one uint32_t value. It is strongly
* // recommended *not* to use xQueueOverwrite() on queues that can
* // contain more than one value, and doing so will trigger an assertion
* // if configASSERT() is defined.
* // Create a queue to hold one uint32_t value.
* xQueue = xQueueCreate( 1, sizeof( uint32_t ) );
*
* // Write the value 10 to the queue using xQueueOverwrite().
Expand Down Expand Up @@ -1123,10 +1120,7 @@ void vQueueDelete( QueueHandle_t xQueue ) PRIVILEGED_FUNCTION;
*
* void vFunction( void *pvParameters )
* {
* // Create a queue to hold one uint32_t value. It is strongly
* // recommended *not* to use xQueueOverwriteFromISR() on queues that can
* // contain more than one value, and doing so will trigger an assertion
* // if configASSERT() is defined.
* // Create a queue to hold one uint32_t value.
* xQueue = xQueueCreate( 1, sizeof( uint32_t ) );
* }
*
Expand Down
10 changes: 2 additions & 8 deletions examples/chapter11_07/src/os/FreeRTOS/Source/include/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,8 @@ typedef enum
*/
#define taskENABLE_INTERRUPTS() portENABLE_INTERRUPTS()

/* Definitions returned by xTaskGetSchedulerState(). taskSCHEDULER_SUSPENDED is
* 0 to generate more optimal code when configASSERT() is defined as the constant
* is used in assert() statements. */
/* Definitions returned by xTaskGetSchedulerState().
*/
#define taskSCHEDULER_SUSPENDED ( ( BaseType_t ) 0 )
#define taskSCHEDULER_NOT_STARTED ( ( BaseType_t ) 1 )
#define taskSCHEDULER_RUNNING ( ( BaseType_t ) 2 )
Expand Down Expand Up @@ -321,7 +320,6 @@ typedef enum
* // an automatic stack variable it might no longer exist, or at least have been corrupted, by the time
* // the new task attempts to access it.
* xTaskCreate( vTaskCode, "NAME", STACK_SIZE, &ucParameterToPass, tskIDLE_PRIORITY, &xHandle );
* configASSERT( xHandle );
*
* // Use the handle to delete the task.
* if( xHandle != NULL )
Expand Down Expand Up @@ -418,7 +416,6 @@ typedef enum
* {
* // 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( ;; )
* {
Expand Down Expand Up @@ -823,9 +820,6 @@ eTaskState eTaskGetState( TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
* // Obtain the handle of a task from its name.
* xHandle = xTaskGetHandle( "Task_Name" );
*
* // Check the handle is not NULL.
* configASSERT( xHandle );
*
* // Use the handle to obtain further information about the task.
* vTaskGetInfo( xHandle,
* &xTaskDetails,
Expand Down
12 changes: 1 addition & 11 deletions examples/chapter11_07/src/os/FreeRTOS/Source/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,6 @@ void vListInsertEnd( List_t * const pxList,
{
ListItem_t * const pxIndex = pxList->pxIndex;

/* Only effective when configASSERT() is also defined, these tests may catch
* the list data structures being overwritten in memory. They will not catch
* data errors caused by incorrect configuration or use of FreeRTOS. */
listTEST_LIST_INTEGRITY( pxList );
listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );

Expand Down Expand Up @@ -118,12 +115,6 @@ void vListInsert( List_t * const pxList,
ListItem_t * pxIterator;
const TickType_t xValueOfInsertion = pxNewListItem->xItemValue;

/* Only effective when configASSERT() is also defined, these tests may catch
* the list data structures being overwritten in memory. They will not catch
* data errors caused by incorrect configuration or use of FreeRTOS. */
listTEST_LIST_INTEGRITY( pxList );
listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );

/* Insert the new list item into the list, sorted in xItemValue order.
*
* If the list already contains a list item with the same item value then the
Expand All @@ -141,8 +132,7 @@ void vListInsert( List_t * const pxList,
/* *** NOTE ***********************************************************
* If you find your application is crashing here then likely causes are
* listed below. In addition see https://www.FreeRTOS.org/FAQHelp.html for
* more tips, and ensure configASSERT() is defined!
* https://www.FreeRTOS.org/a00110.html#configASSERT
* more tips.
*
* 1) Stack overflow -
* see https://www.FreeRTOS.org/Stacks-and-stack-overflow-checking.html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,6 @@ static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter )
Sleep( portTICK_PERIOD_MS );
}

configASSERT( xPortRunning );

/* Can't proceed if in a critical section as pvInterruptEventMutex won't
be available. */
WaitForSingleObject( pvInterruptEventMutex, INFINITE );
Expand All @@ -190,7 +188,6 @@ static DWORD WINAPI prvSimulatedPeripheralTimer( LPVOID lpParameter )
handler thread. Must be outside of a critical section to get here so
the handler thread can execute immediately pvInterruptEventMutex is
released. */
configASSERT( ulCriticalNesting == 0UL );
SetEvent( pvInterruptEvent );

/* Give back the mutex so the simulated interrupt handler unblocks
Expand Down Expand Up @@ -247,7 +244,7 @@ StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t px

/* Create the thread itself. */
pxThreadState->pvThread = CreateThread( NULL, xStackSize, ( LPTHREAD_START_ROUTINE ) pxCode, pvParameters, CREATE_SUSPENDED | STACK_SIZE_PARAM_IS_A_RESERVATION, NULL );
configASSERT( pxThreadState->pvThread ); /* See comment where TerminateThread() is called. */

SetThreadAffinityMask( pxThreadState->pvThread, 0x01 );
SetThreadPriorityBoost( pxThreadState->pvThread, TRUE );
SetThreadPriority( pxThreadState->pvThread, portTASK_THREAD_PRIORITY );
Expand Down Expand Up @@ -363,7 +360,6 @@ static uint32_t prvProcessTickInterrupt( void )
uint32_t ulSwitchRequired;

/* Process the tick itself. */
configASSERT( xPortRunning );
ulSwitchRequired = ( uint32_t ) xTaskIncrementTick();

return ulSwitchRequired;
Expand Down Expand Up @@ -471,7 +467,6 @@ static void prvProcessSimulatedInterrupts( void )

/* pxThreadState->pvThread can be NULL if the task deleted
itself - but a deleted task should never be resumed here. */
configASSERT( pxThreadState->pvThread != NULL );
ResumeThread( pxThreadState->pvThread );
}
}
Expand All @@ -496,7 +491,6 @@ void vPortDeleteThread( void *pvTaskToDelete )
ThreadState_t *pxThreadState;
uint32_t ulErrorCode;

/* Remove compiler warnings if configASSERT() is not defined. */
( void ) ulErrorCode;

/* Find the handle of the thread being deleted. */
Expand All @@ -515,10 +509,8 @@ void vPortDeleteThread( void *pvTaskToDelete )
tasks (rather than deleting themselves) as the task stacks will not be
freed. */
ulErrorCode = TerminateThread( pxThreadState->pvThread, 0 );
configASSERT( ulErrorCode );

ulErrorCode = CloseHandle( pxThreadState->pvThread );
configASSERT( ulErrorCode );

ReleaseMutex( pvInterruptEventMutex );
}
Expand All @@ -531,7 +523,6 @@ void vPortCloseRunningThread( void *pvTaskToDelete, volatile BaseType_t *pxPendY
void *pvThread;
uint32_t ulErrorCode;

/* Remove compiler warnings if configASSERT() is not defined. */
( void ) ulErrorCode;

/* Find the handle of the thread being deleted. */
Expand All @@ -554,7 +545,6 @@ void vPortCloseRunningThread( void *pvTaskToDelete, volatile BaseType_t *pxPendY

/* Close the thread. */
ulErrorCode = CloseHandle( pvThread );
configASSERT( ulErrorCode );

/* This is called from a critical section, which must be exited before the
thread stops. */
Expand All @@ -575,8 +565,6 @@ void vPortGenerateSimulatedInterrupt( uint32_t ulInterruptNumber )
{
ThreadState_t *pxThreadState = ( ThreadState_t *) *( ( size_t * ) pxCurrentTCB );

configASSERT( xPortRunning );

if( ( ulInterruptNumber < portMAX_INTERRUPTS ) && ( pvInterruptEventMutex != NULL ) )
{
WaitForSingleObject( pvInterruptEventMutex, INFINITE );
Expand Down Expand Up @@ -663,8 +651,6 @@ void vPortExitCritical( void )
{
ThreadState_t *pxThreadState = ( ThreadState_t *) *( ( size_t * ) pxCurrentTCB );

configASSERT( xPortRunning );

/* The interrupt won't actually executed until
pvInterruptEventMutex is released as it waits on both
pvInterruptEventMutex and pvInterruptEvent.
Expand Down Expand Up @@ -692,7 +678,6 @@ void vPortExitCritical( void )
{
if( lMutexNeedsReleasing == pdTRUE )
{
configASSERT( xPortRunning );
ReleaseMutex( pvInterruptEventMutex );
}
}
Expand Down
Loading

0 comments on commit 7c64e84

Please sign in to comment.