RTX笔记6 - 软件定时器 Timer

  软件定时器可以配置为一次性定时器one-shot、或者周期性定时器periodic。

  RTX在线程osRtxTimerThread 中管理定时器,回调函数在此线程的控制下运行。

  回调可以在专用计时器线程中执行,也可以在中断上下文中执行。因此,建议在回调函数中只使用ISR可调用函数。回调函数的优先级继承线程osRtxTimerThread,并使用该线程的栈空间,因此线程osRtxTimerThread的栈大小必须满足所有回调函数的需求。

  不能在中断中调用软件定时器相关函数。

Behavior of a Periodic Timer

   osTimerId_t osTimerNew (osTimerFunc_t func, osTimerType_t type, void *argument, const osTimerAttr_t *attr)

    创建软件定时器。

    

func 回调函数
type 定时器类型,osTimerOnceosTimerPeriodic
argument 回调函数的参数
attr 定时器属性; NULL: 默认值
osTimerAttr_t  Data Fields
const char * name name of the timer

Pointer to a constant string with a human readable name (displayed during debugging) of the timer object.

Default: NULL no name specified.

uint32_t attr_bits attribute bits

Reserved for future use (must be set to '0' for future compatibility).

void * cb_mem memory for control block

Pointer to a memory for the timer control block object. Refer to Static Object Memory for more information.

Default: NULL to use Automatic Dynamic Allocation for the timer control block.

uint32_t cb_size size of provided memory for control block

The size (in bytes) of memory block passed with cb_mem. For RTX, the minimum value is defined with osRtxTimerCbSize (higher values are permitted).

Default: 0 as the default is no memory provided with cb_mem.

  osStatus_t osTimerStart (osTimerId_t timer_id, uint32_t ticks)

    启动或者重新启动定时器。

    入参:

      timer_id:软件定时器ID

      ticks:软件定时器的定时周期

    返回值:

      osOK: 定时器启动成功

      osErrorISR:中断中调用错误

      osErrorParameter 参数错误

      osErrorResource 定时器处于失效状态

  osStatus_t osTimerStop (osTimerId_t timer_id)

    终止定时器,返回值同osTimerStart

 1 static void
 2 _thread1(void *argument)
 3 {
 4     osTimerId_t one_shot = (osTimerId_t)argument;
 5     osThreadId_t thread;
 6     
 7     osTimerStart(one_shot, 1000);
 8     
 9     thread = osThreadGetId();
10     osThreadTerminate(thread);
11 }
12 
13 static void
14 _oneShotTimerCallback(void *argument)
15 {
16     osTimerId_t periodic = (osTimerId_t)argument;
17     osTimerStart(periodic, 1000);
18 }
19 
20 static void
21 _periodicTimerCallback(void *argument)
22 {
23     Tick *tick = (Tick *)argument;
24     
25     tick->val ++;
26     menuShow(&seg_led, tick->val, 0);
27 }

 

posted @ 2021-10-03 22:52  Ivan0512  阅读(1034)  评论(0编辑  收藏  举报