FreeRTOS软件定时器xTimerCreate
(2017-03-10 18:03:50)分类: OS |
头文件:timers.h
FreeRTOS软件定时器xTimerCreate函数通过计时、计时清零、函数调用的方式,建立一个需要不断运行的任务。该任务不被调度器打断,保证运行稳定。常用在LED灯闪烁等功能的开发上面。
函数原型:
TimerHandle_t xTimerCreate
( const char * const pcTimerName,
const TickType_t xTimerPeriod,
const UBaseType_t uxAutoReload,
void * const pvTimerID,
TimerCallbackFunction_t pxCallbackFunction );
Creates a new
For this RTOS API function to be available:
- configUSE_TIMERS and
configSUPPORT_DYNAMIC_ALLOCATION must both be set to 1 in FreeRTOSConfig.h (configSUPPORT_DYNAMIC_ALLOCATION can also be left undefined, in which case it will default to 1). - The FreeRTOS/Source/timers.c C source file must be included in the build.
Each software timer requires a small amount of RAM that is used to
hold the timer's state. If a timer is created using xTimerCreate()
then this RAM is automatically allocated from
the
Timers are created in the dormant state.
The
- Parameters:
-
pcTimerName A human readable text name that is assigned to the timer. This is done purely to assist debugging. The RTOS kernel itself only ever references a timer by its handle, and never by its name. xTimerPeriod The period of the timer. The period is specified in ticks, and the macro pdMS_TO_TICKS() can be used to convert a time specified in milliseconds to a time specified in ticks. For example, if the timer must expire after 100 ticks, then simply set xTimerPeriod to 100. Alternatively, if the timer must expire after 500ms, then set xTimerPeriod to pdMS_TO_TICKS( 500 ). pdMS_TO_TICKS() can only be used if configTICK_RATE_HZ is less than or equal to 1000. uxAutoReload If uxAutoReload is set to pdTRUE, then the timer will expire repeatedly with a frequency set by the xTimerPeriod parameter. If uxAutoReload is set to pdFALSE, then the timer will be a one-shot and enter the dormant state after it expires. pvTimerID An 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, or together with the vTimerSetTimerID() and pvTimerGetTimerID() API functions to save a value between calls to the timer's callback function. pxCallbackFunction The function to call when the timer expires. Callback functions must have the prototype defined by TimerCallbackFunction_t, which is: void vCallbackFunction( TimerHandle_t xTimer );
.
- Returns:
- If the timer is created successfully 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 NULL is returned.
Example usage:
#define NUM_TIMERS 5
TimerHandle_t xTimers[ NUM_TIMERS ];
void vTimerCallback( TimerHandle_t xTimer )
{
const uint32_t ulMaxExpiryCountBeforeStopping = 10;
uint32_t ulCount;
configASSERT( pxTimer );
ulCount = ( uint32_t ) pvTimerGetTimerID( xTimer );
ulCount++;
if( ulCount >= ulMaxExpiryCountBeforeStopping )
{
xTimerStop( pxTimer, 0 );
}
else
{
vTimerSetTimerID( xTimer, ( void * ) ulCount );
}
}
void main( void )
{
long x;
for( x = 0; x < NUM_TIMERS; x++ )
{
xTimers[ x ] = xTimerCreate
(
"Timer",
( 100 * x ) + 100,
pdTRUE,
( void * ) 0,
vTimerCallback
);
if( xTimers[ x ] == NULL )
{
}
else
{
if( xTimerStart( xTimers[ x ], 0 ) != pdPASS )
{
}
}
}
vTaskStartScheduler();
for( ;; );
}