Microchip® Advanced Software Framework

freertos/freertos-7.3.0/source/timers.c File Reference
#include "include/FreeRTOS.h"
#include "include/task.h"
#include "include/queue.h"
#include "include/timers.h"

Data Structures

struct  tmrTimerControl
 
struct  tmrTimerQueueMessage
 

Macros

#define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
 
#define tmrNO_DELAY   ( portTickType ) 0U
 

Typedefs

typedef struct tmrTimerControl xTIMER
 
typedef struct tmrTimerQueueMessage xTIMER_MESSAGE
 

Functions

static void prvCheckForValidListAndQueue (static void prvTimerTask void)
 
static void prvCheckForValidListAndQueue (void)
 
static portTickType prvGetNextExpireTime (portBASE_TYPE *pxListWasEmpty)
 
static portBASE_TYPE prvInsertTimerInActiveList (xTIMER *pxTimer, portTickType xNextExpiryTime, portTickType xTimeNow, portTickType xCommandTime)
 
static void prvProcessExpiredTimer (portTickType xNextExpireTime, portTickType xTimeNow)
 
static void prvProcessReceivedCommands (void)
 
static void prvProcessTimerOrBlockTask (portTickType xNextExpireTime, portBASE_TYPE xListWasEmpty)
 
static portTickType prvSampleTimeNow (portBASE_TYPE *pxTimerListsWereSwitched)
 
static void prvSwitchTimerLists (portTickType xLastTime)
 
static void prvTimerTask (void *pvParameters)
 
voidpvTimerGetTimerID (xTimerHandle xTimer)
 void *pvTimerGetTimerID( xTimerHandle xTimer ); More...
 
xTimerHandle xTimerCreate (const signed char *pcTimerName, portTickType xTimerPeriodInTicks, unsigned portBASE_TYPE uxAutoReload, void *pvTimerID, tmrTIMER_CALLBACK pxCallbackFunction)
 xTimerHandle xTimerCreate( const signed char *pcTimerName, portTickType xTimerPeriodInTicks, unsigned portBASE_TYPE uxAutoReload, void * pvTimerID, tmrTIMER_CALLBACK pxCallbackFunction ); More...
 
portBASE_TYPE xTimerGenericCommand (xTimerHandle xTimer, portBASE_TYPE xCommandID, portTickType xOptionalValue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portTickType xBlockTime)
 
portBASE_TYPE xTimerIsTimerActive (xTimerHandle xTimer)
 portBASE_TYPE xTimerIsTimerActive( xTimerHandle xTimer ); More...
 

Variables

static PRIVILEGED_DATA xListpxCurrentTimerList
 
static PRIVILEGED_DATA xListpxOverflowTimerList
 
static PRIVILEGED_DATA xList xActiveTimerList1
 
static PRIVILEGED_DATA xList xActiveTimerList2
 
static PRIVILEGED_DATA xQueueHandle xTimerQueue = NULL
 

#define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
#define tmrNO_DELAY   ( portTickType ) 0U

typedef struct tmrTimerControl xTIMER

static void prvCheckForValidListAndQueue ( static void prvTimerTask  void)
static
static portTickType prvGetNextExpireTime ( portBASE_TYPE *  pxListWasEmpty)
static
static portBASE_TYPE prvInsertTimerInActiveList ( xTIMER pxTimer,
portTickType  xNextExpiryTime,
portTickType  xTimeNow,
portTickType  xCommandTime 
)
static
static void prvProcessTimerOrBlockTask ( portTickType  xNextExpireTime,
portBASE_TYPE  xListWasEmpty 
)
static
static portTickType prvSampleTimeNow ( portBASE_TYPE *  pxTimerListsWereSwitched)
static
static void prvTimerTask ( void pvParameters)
static
void* pvTimerGetTimerID ( xTimerHandle  xTimer)

void *pvTimerGetTimerID( xTimerHandle xTimer );

Returns the ID assigned to the timer.

IDs are assigned to timers using the pvTimerID parameter of the call to xTimerCreated() that was used to create the timer.

If the same callback function is assigned to multiple timers then the timer ID can be used within the callback function to identify which timer actually expired.

Parameters
xTimerThe timer being queried.
Returns
The ID assigned to the timer being queried.

Example usage:

See the xTimerCreate() API function example usage scenario.

References tmrTimerControl::pvTimerID.

xTimerHandle xTimerCreate ( const signed char *  pcTimerName,
portTickType  xTimerPeriodInTicks,
unsigned portBASE_TYPE  uxAutoReload,
void pvTimerID,
tmrTIMER_CALLBACK  pxCallbackFunction 
)

xTimerHandle xTimerCreate( const signed char *pcTimerName, portTickType xTimerPeriodInTicks, unsigned portBASE_TYPE uxAutoReload, void * pvTimerID, tmrTIMER_CALLBACK pxCallbackFunction );

Creates a new software timer instance. This allocates the storage required by the new timer, initialises the new timers internal state, and returns a handle by which the new timer can be referenced.

Timers are created in the dormant state. The xTimerStart(), xTimerReset(), xTimerStartFromISR(), xTimerResetFromISR(), xTimerChangePeriod() and xTimerChangePeriodFromISR() API functions can all be used to transition a timer into the active state.

Parameters
pcTimerNameA text name that is assigned to the timer. This is done purely to assist debugging. The kernel itself only ever references a timer by its handle, and never by its name.
xTimerPeriodInTicksThe timer period. The time is defined in tick periods so the constant portTICK_RATE_MS can be used to convert a time that has been specified in milliseconds. For example, if the timer must expire after 100 ticks, then xTimerPeriodInTicks should be set to 100. Alternatively, if the timer must expire after 500ms, then xPeriod can be set to ( 500 / portTICK_RATE_MS ) provided configTICK_RATE_HZ is less than or equal to 1000.
uxAutoReloadIf uxAutoReload is set to pdTRUE then the timer will expire repeatedly with a frequency set by the xTimerPeriodInTicks parameter. If uxAutoReload is set to pdFALSE then the timer will be a one-shot timer and enter the dormant state after it expires.
pvTimerIDAn identifier that is assigned to the timer being created. Typically this would be used in the timer callback function to identify which timer expired when the same callback function is assigned to more than one timer.
pxCallbackFunctionThe function to call when the timer expires. Callback functions must have the prototype defined by tmrTIMER_CALLBACK, which is "void vCallbackFunction( xTimerHandle xTimer );".
Returns
If the timer is successfully create then a handle to the newly created timer is returned. If the timer cannot be created (because either there is insufficient FreeRTOS heap remaining to allocate the timer structures, or the timer period was set to 0) then 0 is returned.

Example usage:

#define NUM_TIMERS 5

// An array to hold handles to the created timers. xTimerHandle xTimers[ NUM_TIMERS ];

// An array to hold a count of the number of times each timer expires. long lExpireCounters[ NUM_TIMERS ] = { 0 };

// Define a callback function that will be used by multiple timer instances. // The callback function does nothing but count the number of times the // associated timer expires, and stop the timer once the timer has expired // 10 times. void vTimerCallback( xTimerHandle pxTimer ) { long lArrayIndex; const long xMaxExpiryCountBeforeStopping = 10;

   // Optionally do something if the pxTimer parameter is NULL.
   configASSERT( pxTimer );

// Which timer expired?
lArrayIndex = ( long ) pvTimerGetTimerID( pxTimer );

// Increment the number of times that pxTimer has expired.
lExpireCounters[ lArrayIndex ] += 1;

// If the timer has expired 10 times then stop it from running.
if( lExpireCounters[ lArrayIndex ] == xMaxExpiryCountBeforeStopping )
{
    // Do not use a block time if calling a timer API function from a
    // timer callback function, as doing so could cause a deadlock!
    xTimerStop( pxTimer, 0 );
}

}

void main( void ) { long x;

// Create then start some timers.  Starting the timers before the scheduler
// has been started means the timers will start running immediately that
// the scheduler starts.
for( x = 0; x < NUM_TIMERS; x++ )
{
    xTimers[ x ] = xTimerCreate(     "Timer",         // Just a text name, not used by the kernel.
                                    ( 100 * x ),     // The timer period in ticks.
                                    pdTRUE,         // The timers will auto-reload themselves when they expire.
                                    ( void * ) x,     // Assign each timer a unique id equal to its array index.
                                    vTimerCallback     // Each timer calls the same callback when it expires.
                                );

    if( xTimers[ x ] == NULL )
    {
        // The timer was not created.
    }
    else
    {
        // Start the timer.  No block time is specified, and even if one was
        // it would be ignored because the scheduler has not yet been
        // started.
        if( xTimerStart( xTimers[ x ], 0 ) != pdPASS )
        {
            // The timer could not be set into the Active state.
        }
    }
}

// ...
// Create tasks here.
// ...

// Starting the scheduler will start the timers running as they have already
// been set into the active state.
xTaskStartScheduler();

// Should not reach here.
for( ;; );

References configASSERT, NULL, tmrTimerControl::pcTimerName, prvCheckForValidListAndQueue(), pvPortMalloc(), tmrTimerControl::pvTimerID, tmrTimerControl::pxCallbackFunction, traceTIMER_CREATE, traceTIMER_CREATE_FAILED, tmrTimerControl::uxAutoReload, vListInitialiseItem(), tmrTimerControl::xTimerListItem, and tmrTimerControl::xTimerPeriodInTicks.

portBASE_TYPE xTimerGenericCommand ( xTimerHandle  xTimer,
portBASE_TYPE  xCommandID,
portTickType  xOptionalValue,
signed portBASE_TYPE *  pxHigherPriorityTaskWoken,
portTickType  xBlockTime 
)
portBASE_TYPE xTimerIsTimerActive ( xTimerHandle  xTimer)

portBASE_TYPE xTimerIsTimerActive( xTimerHandle xTimer );

Queries a timer to see if it is active or dormant.

A timer will be dormant if: 1) It has been created but not started, or 2) It is an expired on-shot timer that has not been restarted.

Timers are created in the dormant state. The xTimerStart(), xTimerReset(), xTimerStartFromISR(), xTimerResetFromISR(), xTimerChangePeriod() and xTimerChangePeriodFromISR() API functions can all be used to transition a timer into the active state.

Parameters
xTimerThe timer being queried.
Returns
pdFALSE will be returned if the timer is dormant. A value other than pdFALSE will be returned if the timer is active.

Example usage:

// This function assumes xTimer has already been created. void vAFunction( xTimerHandle xTimer ) { if( xTimerIsTimerActive( xTimer ) != pdFALSE ) // or more simply and equivalently "if( xTimerIsTimerActive( xTimer ) )" { // xTimer is active, do something. } else { // xTimer is not active, do something else. } }

References listIS_CONTAINED_WITHIN, NULL, taskENTER_CRITICAL, taskEXIT_CRITICAL, and tmrTimerControl::xTimerListItem.

PRIVILEGED_DATA xList xActiveTimerList1
static
PRIVILEGED_DATA xList xActiveTimerList2
static