Table 30. pvTimerGetTimerID() parameters and return value
Parameter Name/
Returned Value
Description
xTimer The handle of the software timer being queried. The handle will have been returned from the call to xTimerCreate() used to create the software timer.
Returned value The ID of the software timer being queried.
Example 14. Using the callback function parameter and the software timer ID
The same callback function can be assigned to more than one software timer. When that is done, the callback function parameter is used to determine which software timer expired.
Example 13 used two separate callback functions; one callback function was used by the one-
shot timer, and the other callback function was used by the auto-reload timer. Example 14
creates similar functionality to that created by Example 13, but assigns a single callback
function to both software timers.
The main() function used by Example 14 is almost identical to the main() function used in
Example 13. The only difference is where the software timers are created. This difference is
shown in Listing 80, where prvTimerCallback() is used as the callback function for both timers.
/* Create the one shot timer software timer, storing the handle in xOneShotTimer. */ xOneShotTimer = xTimerCreate( "OneShot",
mainONE_SHOT_TIMER_PERIOD,
pdFALSE,
/* The timer’s ID is initialized to 0. */
0,
/* prvTimerCallback() is used by both timers. */
prvTimerCallback );
/* Create the auto-reload software timer, storing the handle in xAutoReloadTimer */ xAutoReloadTimer = xTimerCreate( "AutoReload",
mainAUTO_RELOAD_TIMER_PERIOD,
pdTRUE,
/* The timer’s ID is initialized to 0. */
0,
/* prvTimerCallback() is used by both timers. */
prvTimerCallback );
Listing 80. Creating the timers used in Example 14
167
161204 Pre-release for FreeRTOS V8.x.x. See http://www.FreeRTOS.org/FreeRTOS-V9.html
for information about FreeRTOS
V9.x.x. Use http://www.FreeRTOS.org/contact to provide feedback, corrections, and check for updates. prvTimerCallback() will execute when either timer expires. The implementation of prvTimerCallback() uses the function’s parameter to determine if it was called because the one-shot timer expired, or because the auto-reload timer expired. prvTimerCallback() also demonstrates how to use the software timer ID as timer specific storage; each software timer keeps a count of the number of times it has expired in its own ID, and the auto-reload timer uses the count to stop itself the fifth time it executes.
The implementation of prvTimerCallback() is shown in Listing 79.
static void prvTimerCallback( TimerHandle_t xTimer )
{
TickType_t xTimeNow; uint32_t ulExecutionCount;
/* A count of the number of times this software timer has expired is stored in the timer's
ID. Obtain the ID, increment it, then save it as the new ID value. The ID is a void
pointer, so is cast to a uint32_t. */
ulExecutionCount = ( uint32_t ) pvTimerGetTimerID( xTimer );
ulExecutionCount++;
vTimerSetTimerID( xTimer, ( void * ) ulExecutionCount );
/* Obtain the current tick count. */
xTimeNow = xTaskGetTickCount();
/* The handle of the one-shot timer was stored in xOneShotTimer when the timer was created.
Compare the handle passed into this function with xOneShotTimer to determine if it was the
one-shot or auto-reload timer that expired, then output a string to show the time at which
the callback was executed. */
if( xTimer == xOneShotTimer )
{
vPrintStringAndNumber( "One-shot timer callback executing", xTimeNow );
}
else
{
/* xTimer did not equal xOneShotTimer, so it must have been the auto-reload timer that
expired. */
vPrintStringAndNumber( "Auto-reload timer callback executing", xTimeNow );
if( ulExecutionCount == 5 )
{
/* Stop the auto-reload timer after it has executed 5 times. This callback function
executes in the context of the RTOS daemon task so must not call any functions that
might place the daemon task into the Blocked state. Therefore a block time of 0 is
used. */
xTimerStop( xTimer, 0 );
}
}
}
Listing 81. The timer callback function used in Example 14
The output produced by Example 14 is shown in Figure 45. It can be seen that the auto-
reload timer only executes five times.
168
Figure 45 The output produced when Example 14 is executed
169