ConclusionThis can be a good solution provided the RAM and processing capacity is available. The partitioning of the application into tasks and the priority assigned to each task requires careful consideration.
ExampleThis example is a partial implementation of the hypothetical application introduced previously. The FreeRTOS API is used.Plant Control TaskThis task implements all the control functionality. It has critical timing requirements and is therefore given the highest priority within the system:
#define CYCLE_RATE_MS 10
#define MAX_COMMS_DELAY 2
void PlantControlTask( void *pvParameters )
{
TickType_t xLastWakeTime;
DataType Data1, Data2;
InitialiseTheQueue();
// A
xLastWakeTime = xTaskGetTickCount();
// B
for( ;; )
{
// C
vTaskDelayUntil( &xLastWakeTime, CYCLE_RATE_MS );
// Request data from the sensors.
TransmitRequest();
// D
if( xQueueReceive( xFieldBusQueue, &Data1, MAX_COMMS_DELAY ) )
{
// E
if( xQueueReceive( xFieldBusQueue, &Data2, MAX_COMMS_DELAY ) )
{
PerformControlAlgorithm();
TransmitResults();
}
}
}
// Will never get here!
}
Referring to the labels within the code fragment above:
Embedded Web Server TaskThe embedded web server task can be represented by the following pseudo code. This only utilises processor time when data is available but will take a variable and relatively long time to complete. It is therefore given a low priority to prevent it adversely effecting the timing of the plant control, RS232 or keypad scanning tasks.
void WebServerTask( void *pvParameters )
{
DataTypeA Data;
for( ;; )
{
// Block until data arrives. xEthernetQueue is filled by the
// Ethernet interrupt service routine.
if( xQueueReceive( xEthernetQueue, &Data, MAX_DELAY ) )
{
ProcessHTTPData( Data );
}
}
}
RS232 InterfaceThis is very similar in structure to the embedded web server task. It is given a medium priority to ensure it does not adversely effect the timing of the plant control task.
void RS232Task( void *pvParameters )
{
DataTypeB Data;
for( ;; )
{
// Block until data arrives. xRS232Queue is filled by the
// RS232 interrupt service routine.
if( xQueueReceive( xRS232Queue, &Data, MAX_DELAY ) )
{
ProcessSerialCharacters( Data );
}
}
}
Keypad Scanning TaskThis is a simple cyclical task. It is given a medium priority as its timing requirements are similar to the RS232 task.The cycle time is set much faster than the specified limit. This is to account for the fact that it may not get processor time immediately upon request - and once executing may get pre-empted by the plant control task.
LED TaskThis is the simplest of all the tasks.
#define DELAY_PERIOD 1000
void LEDTask( void *pvParmeters )
{
TickType_t xLastWakeTime;
xLastWakeTime = xTaskGetTickCount();
for( ;; )
{
// Wait for the next cycle.
vTaskDelayUntil( &xLastWakeTime, DELAY_PERIOD );
// Flash the appropriate LED.
if( SystemIsHealthy() )
{
FlashLED( GREEN );
}
else
{
FlashLED( RED );
}
}
}
NEXT >>> Solution #3: Reducing RAM utilisation
Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.
|
Latest News
NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS. Meet Richard Barry and learn about running FreeRTOS on RISC-V at FOSDEM 2019 Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed. View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS. Careers
FreeRTOS and other embedded software careers at AWS. FreeRTOS Partners
|
|||||||||||||||||||||||||||||||||