加载中…
个人资料
电子世界的小蜗牛
电子世界的小蜗牛
  • 博客等级:
  • 博客积分:0
  • 博客访问:6,795
  • 关注人气:7
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

FreeRTOS软件定时器xTimerCreate

(2017-03-10 18:03:50)
分类: OS
http://blog.csdn.net/xikangsoon/article/details/53925388

头文件: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 software timer instance and returns a handle by which the timer can be referenced.

For this RTOS API function to be available:

  1. 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).
  2. 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 FreeRTOS heap. If a software timer is created using xTimerCreateStatic() then the RAM is provided by the application writer, which requires an additional parameter, but allows the RAM to be statically allocated at compile time. See the Static Vs Dynamic allocation page for more information.

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:
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( ;; );
 }

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有