source: rtems-docs/c-user/timer_manager.rst @ c65aeed

5
Last change on this file since c65aeed was 464d541, checked in by Sebastian Huber <sebastian.huber@…>, on 03/07/18 at 13:18:10

c-user: Use uniprocessor throughout

  • Property mode set to 100644
File size: 19.7 KB
Line 
1.. comment SPDX-License-Identifier: CC-BY-SA-4.0
2
3.. COMMENT: COPYRIGHT (c) 1988-2008.
4.. COMMENT: On-Line Applications Research Corporation (OAR).
5.. COMMENT: All rights reserved.
6
7.. index:: timers
8
9Timer Manager
10*************
11
12Introduction
13============
14
15The timer manager provides support for timer
16facilities.  The directives provided by the timer manager are:
17
18- rtems_timer_create_ - Create a timer
19
20- rtems_timer_ident_ - Get ID of a timer
21
22- rtems_timer_cancel_ - Cancel a timer
23
24- rtems_timer_delete_ - Delete a timer
25
26- rtems_timer_fire_after_ - Fire timer after interval
27
28- rtems_timer_fire_when_ - Fire timer when specified
29
30- rtems_timer_initiate_server_ - Initiate server for task-based timers
31
32- rtems_timer_server_fire_after_ - Fire task-based timer after interval
33
34- rtems_timer_server_fire_when_ - Fire task-based timer when specified
35
36- rtems_timer_reset_ - Reset an interval timer
37
38Background
39==========
40
41Required Support
42----------------
43
44A clock tick is required to support the functionality provided by this manager.
45
46Timers
47------
48
49A timer is an RTEMS object which allows the application to schedule operations
50to occur at specific times in the future.  User supplied timer service routines
51are invoked by either a clock tick directive or a special Timer
52Server task when the timer fires.  Timer service routines may perform any
53operations or directives which normally would be performed by the application
54code which invoked a clock tick directive.
55
56The timer can be used to implement watchdog routines which only fire to denote
57that an application error has occurred.  The timer is reset at specific points
58in the application to ensure that the watchdog does not fire.  Thus, if the
59application does not reset the watchdog timer, then the timer service routine
60will fire to indicate that the application has failed to reach a reset point.
61This use of a timer is sometimes referred to as a "keep alive" or a "deadman"
62timer.
63
64Timer Server
65------------
66
67The Timer Server task is responsible for executing the timer service routines
68associated with all task-based timers.  This task executes at a priority
69specified by :ref:`rtems_timer_initiate_server() <rtems_timer_initiate_server>`
70and it may have a priority of zero (the highest priority).  In uniprocessor
71configurations, it is created non-preemptible.
72
73By providing a mechanism where timer service routines execute in task rather
74than interrupt space, the application is allowed a bit more flexibility in what
75operations a timer service routine can perform.  For example, the Timer Server
76can be configured to have a floating point context in which case it would be
77safe to perform floating point operations from a task-based timer.  Most of the
78time, executing floating point instructions from an interrupt service routine
79is not considered safe. The timer service routines invoked by the Timer Server
80may block, however, since this blocks the Timer Server itself, other timer
81service routines that are already pending do not run until the blocked timer
82service routine finished its work.
83
84The Timer Server is designed to remain blocked until a task-based timer fires.
85This reduces the execution overhead of the Timer Server.
86
87.. index:: rtems_timer_service_routine
88
89Timer Service Routines
90----------------------
91
92The timer service routine should adhere to C calling conventions and have a
93prototype similar to the following:
94
95.. code-block:: c
96
97    rtems_timer_service_routine user_routine(
98        rtems_id   timer_id,
99        void      *user_data
100    );
101
102Where the timer_id parameter is the RTEMS object ID of the timer which is being
103fired and user_data is a pointer to user-defined information which may be
104utilized by the timer service routine.  The argument user_data may be NULL.
105
106Operations
107==========
108
109Creating a Timer
110----------------
111
112The ``rtems_timer_create`` directive creates a timer by allocating a Timer
113Control Block (TMCB), assigning the timer a user-specified name, and assigning
114it a timer ID.  Newly created timers do not have a timer service routine
115associated with them and are not active.
116
117Obtaining Timer IDs
118-------------------
119
120When a timer is created, RTEMS generates a unique timer ID and assigns it to
121the created timer until it is deleted.  The timer ID may be obtained by either
122of two methods.  First, as the result of an invocation of the
123``rtems_timer_create`` directive, the timer ID is stored in a user provided
124location.  Second, the timer ID may be obtained later using the
125``rtems_timer_ident`` directive.  The timer ID is used by other directives to
126manipulate this timer.
127
128Initiating an Interval Timer
129----------------------------
130
131The ``rtems_timer_fire_after`` and ``rtems_timer_server_fire_after`` directives
132initiate a timer to fire a user provided timer service routine after the
133specified number of clock ticks have elapsed.  When the interval has elapsed,
134the timer service routine will be invoked from a clock tick
135directive if it was initiated by the ``rtems_timer_fire_after`` directive and
136from the Timer Server task if initiated by the
137``rtems_timer_server_fire_after`` directive.
138
139Initiating a Time of Day Timer
140------------------------------
141
142The ``rtems_timer_fire_when`` and ``rtems_timer_server_fire_when`` directive
143initiate a timer to fire a user provided timer service routine when the
144specified time of day has been reached.  When the interval has elapsed, the
145timer service routine will be invoked from a clock tick directive
146by the ``rtems_timer_fire_when`` directive and from the Timer Server task if
147initiated by the ``rtems_timer_server_fire_when`` directive.
148
149Canceling a Timer
150-----------------
151
152The ``rtems_timer_cancel`` directive is used to halt the specified timer.  Once
153canceled, the timer service routine will not fire unless the timer is
154reinitiated.  The timer can be reinitiated using the ``rtems_timer_reset``,
155``rtems_timer_fire_after``, and ``rtems_timer_fire_when`` directives.
156
157Resetting a Timer
158-----------------
159
160The ``rtems_timer_reset`` directive is used to restore an interval timer
161initiated by a previous invocation of ``rtems_timer_fire_after`` or
162``rtems_timer_server_fire_after`` to its original interval length.  If the
163timer has not been used or the last usage of this timer was by the
164``rtems_timer_fire_when`` or ``rtems_timer_server_fire_when`` directive, then
165an error is returned.  The timer service routine is not changed or fired by
166this directive.
167
168Initiating the Timer Server
169---------------------------
170
171The ``rtems_timer_initiate_server`` directive is used to allocate and start the
172execution of the Timer Server task.  The application can specify both the stack
173size and attributes of the Timer Server.  The Timer Server executes at a
174priority higher than any application task and thus the user can expect to be
175preempted as the result of executing the ``rtems_timer_initiate_server``
176directive.
177
178Deleting a Timer
179----------------
180
181The ``rtems_timer_delete`` directive is used to delete a timer.  If the timer
182is running and has not expired, the timer is automatically canceled.  The
183timer's control block is returned to the TMCB free list when it is deleted.  A
184timer can be deleted by a task other than the task which created the timer.
185Any subsequent references to the timer's name and ID are invalid.
186
187Directives
188==========
189
190This section details the timer manager's directives.  A subsection is dedicated
191to each of this manager's directives and describes the calling sequence,
192related constants, usage, and status codes.
193
194.. raw:: latex
195
196   \clearpage
197.. index:: create a timer
198.. index:: rtems_timer_create
199
200.. _rtems_timer_create:
201
202TIMER_CREATE - Create a timer
203-----------------------------
204
205CALLING SEQUENCE:
206    .. code-block:: c
207
208        rtems_status_code rtems_timer_create(
209            rtems_name  name,
210            rtems_id   *id
211        );
212
213DIRECTIVE STATUS CODES:
214    .. list-table::
215     :class: rtems-table
216
217     * - ``RTEMS_SUCCESSFUL``
218       - timer created successfully
219     * - ``RTEMS_INVALID_ADDRESS``
220       - ``id`` is NULL
221     * - ``RTEMS_INVALID_NAME``
222       - invalid timer name
223     * - ``RTEMS_TOO_MANY``
224       - too many timers created
225
226DESCRIPTION:
227    This directive creates a timer.  The assigned timer id is returned in id.
228    This id is used to access the timer with other timer manager directives.
229    For control and maintenance of the timer, RTEMS allocates a TMCB from the
230    local TMCB free pool and initializes it.
231
232NOTES:
233    This directive will obtain the allocator mutex and may cause the calling
234    task to be preempted.
235
236    In SMP configurations, the processor of the currently executing thread
237    determines the processor used for the created timer.  During the life-time
238    of the timer this processor is used to manage the timer internally.
239
240.. raw:: latex
241
242   \clearpage
243
244.. index:: obtain the ID of a timer
245.. index:: rtems_timer_ident
246
247.. _rtems_timer_ident:
248
249TIMER_IDENT - Get ID of a timer
250-------------------------------
251
252CALLING SEQUENCE:
253    .. code-block:: c
254
255        rtems_status_code rtems_timer_ident(
256            rtems_name  name,
257            rtems_id   *id
258        );
259
260DIRECTIVE STATUS CODES:
261    .. list-table::
262     :class: rtems-table
263
264     * - ``RTEMS_SUCCESSFUL``
265       - timer identified successfully
266     * - ``RTEMS_INVALID_ADDRESS``
267       - ``id`` is NULL
268     * - ``RTEMS_INVALID_NAME``
269       - timer name not found
270
271DESCRIPTION:
272    This directive obtains the timer id associated with the timer name to be
273    acquired.  If the timer name is not unique, then the timer id will match
274    one of the timers with that name.  However, this timer id is not guaranteed
275    to correspond to the desired timer.  The timer id is used to access this
276    timer in other timer related directives.
277
278NOTES:
279    This directive will not cause the running task to be preempted.
280
281.. raw:: latex
282
283   \clearpage
284
285.. index:: cancel a timer
286.. index:: rtems_timer_cancel
287
288.. _rtems_timer_cancel:
289
290TIMER_CANCEL - Cancel a timer
291-----------------------------
292
293CALLING SEQUENCE:
294    .. code-block:: c
295
296        rtems_status_code rtems_timer_cancel(
297            rtems_id id
298        );
299
300DIRECTIVE STATUS CODES:
301    .. list-table::
302     :class: rtems-table
303
304     * - ``RTEMS_SUCCESSFUL``
305       - timer canceled successfully
306     * - ``RTEMS_INVALID_ID``
307       - invalid timer id
308
309DESCRIPTION:
310    This directive cancels the timer id.  This timer will be reinitiated by the
311    next invocation of ``rtems_timer_reset``, ``rtems_timer_fire_after``, or
312    ``rtems_timer_fire_when`` with this id.
313
314NOTES:
315    This directive will not cause the running task to be preempted.
316
317.. raw:: latex
318
319   \clearpage
320
321.. index:: delete a timer
322.. index:: rtems_timer_delete
323
324.. _rtems_timer_delete:
325
326TIMER_DELETE - Delete a timer
327-----------------------------
328
329CALLING SEQUENCE:
330    .. code-block:: c
331
332        rtems_status_code rtems_timer_delete(
333            rtems_id id
334        );
335
336DIRECTIVE STATUS CODES:
337    .. list-table::
338     :class: rtems-table
339
340     * - ``RTEMS_SUCCESSFUL``
341       - timer deleted successfully
342     * - ``RTEMS_INVALID_ID``
343       - invalid timer id
344
345DESCRIPTION:
346    This directive deletes the timer specified by id.  If the timer is running,
347    it is automatically canceled.  The TMCB for the deleted timer is reclaimed
348    by RTEMS.
349
350NOTES:
351    This directive will obtain the allocator mutex and may cause the calling
352    task to be preempted.
353
354    A timer can be deleted by a task other than the task which created the
355    timer.
356
357.. raw:: latex
358
359   \clearpage
360
361.. index:: fire a timer after an interval
362.. index:: rtems_timer_fire_after
363
364.. _rtems_timer_fire_after:
365
366TIMER_FIRE_AFTER - Fire timer after interval
367--------------------------------------------
368
369CALLING SEQUENCE:
370    .. code-block:: c
371
372        rtems_status_code rtems_timer_fire_after(
373            rtems_id                           id,
374            rtems_interval                     ticks,
375            rtems_timer_service_routine_entry  routine,
376            void                              *user_data
377        );
378
379DIRECTIVE STATUS CODES:
380    .. list-table::
381     :class: rtems-table
382
383     * - ``RTEMS_SUCCESSFUL``
384       - timer initiated successfully
385     * - ``RTEMS_INVALID_ADDRESS``
386       - ``routine`` is NULL
387     * - ``RTEMS_INVALID_ID``
388       - invalid timer id
389     * - ``RTEMS_INVALID_NUMBER``
390       - invalid interval
391
392DESCRIPTION:
393    This directive initiates the timer specified by id.  If the timer is
394    running, it is automatically canceled before being initiated.  The timer is
395    scheduled to fire after an interval ticks clock ticks has passed.  When the
396    timer fires, the timer service routine routine will be invoked with the
397    argument user_data.
398
399NOTES:
400    This directive will not cause the running task to be preempted.
401
402.. raw:: latex
403
404   \clearpage
405
406.. index:: fire a timer at wall time
407.. index:: rtems_timer_fire_when
408
409.. _rtems_timer_fire_when:
410
411TIMER_FIRE_WHEN - Fire timer when specified
412-------------------------------------------
413
414CALLING SEQUENCE:
415    .. code-block:: c
416
417        rtems_status_code rtems_timer_fire_when(
418            rtems_id                           id,
419            rtems_time_of_day                 *wall_time,
420            rtems_timer_service_routine_entry  routine,
421            void                              *user_data
422        );
423
424DIRECTIVE STATUS CODES:
425    .. list-table::
426     :class: rtems-table
427
428     * - ``RTEMS_SUCCESSFUL``
429       - timer initiated successfully
430     * - ``RTEMS_INVALID_ADDRESS``
431       - ``routine`` is NULL
432     * - ``RTEMS_INVALID_ADDRESS``
433       - ``wall_time`` is NULL
434     * - ``RTEMS_INVALID_ID``
435       - invalid timer id
436     * - ``RTEMS_NOT_DEFINED``
437       - system date and time is not set
438     * - ``RTEMS_INVALID_CLOCK``
439       - invalid time of day
440
441DESCRIPTION:
442    This directive initiates the timer specified by id.  If the timer is
443    running, it is automatically canceled before being initiated.  The timer is
444    scheduled to fire at the time of day specified by wall_time.  When the
445    timer fires, the timer service routine routine will be invoked with the
446    argument user_data.
447
448NOTES:
449    This directive will not cause the running task to be preempted.
450
451.. raw:: latex
452
453   \clearpage
454
455.. index:: initiate the Timer Server
456.. index:: rtems_timer_initiate_server
457
458.. _rtems_timer_initiate_server:
459
460TIMER_INITIATE_SERVER - Initiate server for task-based timers
461-------------------------------------------------------------
462
463CALLING SEQUENCE:
464    .. code-block:: c
465
466        rtems_status_code rtems_timer_initiate_server(
467            uint32_t         priority,
468            uint32_t         stack_size,
469            rtems_attribute  attribute_set
470        );
471
472DIRECTIVE STATUS CODES:
473    .. list-table::
474     :class: rtems-table
475
476     * - ``RTEMS_SUCCESSFUL``
477       - Timer Server initiated successfully
478     * - ``RTEMS_TOO_MANY``
479       - too many tasks created
480
481DESCRIPTION:
482    This directive initiates the Timer Server task.  This task is responsible
483    for executing all timers initiated via the
484    ``rtems_timer_server_fire_after`` or ``rtems_timer_server_fire_when``
485    directives.
486
487NOTES:
488    This directive could cause the calling task to be preempted.
489
490    The Timer Server task is created using the ``rtems_task_create`` service
491    and must be accounted for when configuring the system.
492
493    Even through this directive invokes the ``rtems_task_create`` and
494    ``rtems_task_start`` directives, it should only fail due to resource
495    allocation problems.
496
497.. raw:: latex
498
499   \clearpage
500
501.. index:: fire task-based a timer after an interval
502.. index:: rtems_timer_server_fire_after
503
504.. _rtems_timer_server_fire_after:
505
506TIMER_SERVER_FIRE_AFTER - Fire task-based timer after interval
507--------------------------------------------------------------
508
509CALLING SEQUENCE:
510    .. code-block:: c
511
512        rtems_status_code rtems_timer_server_fire_after(
513            rtems_id                           id,
514            rtems_interval                     ticks,
515            rtems_timer_service_routine_entry  routine,
516            void                              *user_data
517        );
518
519DIRECTIVE STATUS CODES:
520    .. list-table::
521     :class: rtems-table
522
523     * - ``RTEMS_SUCCESSFUL``
524       - timer initiated successfully
525     * - ``RTEMS_INVALID_ADDRESS``
526       - ``routine`` is NULL
527     * - ``RTEMS_INVALID_ID``
528       - invalid timer id
529     * - ``RTEMS_INVALID_NUMBER``
530       - invalid interval
531     * - ``RTEMS_INCORRECT_STATE``
532       - Timer Server not initiated
533
534DESCRIPTION:
535    This directive initiates the timer specified by id and specifies that when
536    it fires it will be executed by the Timer Server.
537
538    If the timer is running, it is automatically canceled before being
539    initiated.  The timer is scheduled to fire after an interval ticks clock
540    ticks has passed.  When the timer fires, the timer service routine routine
541    will be invoked with the argument user_data.
542
543NOTES:
544    This directive will not cause the running task to be preempted.
545
546.. raw:: latex
547
548   \clearpage
549
550.. index:: fire a task-based timer at wall time
551.. index:: rtems_timer_server_fire_when
552
553.. _rtems_timer_server_fire_when:
554
555TIMER_SERVER_FIRE_WHEN - Fire task-based timer when specified
556-------------------------------------------------------------
557
558CALLING SEQUENCE:
559    .. code-block:: c
560
561        rtems_status_code rtems_timer_server_fire_when(
562            rtems_id                           id,
563            rtems_time_of_day                 *wall_time,
564            rtems_timer_service_routine_entry  routine,
565            void                              *user_data
566        );
567
568DIRECTIVE STATUS CODES:
569    .. list-table::
570     :class: rtems-table
571
572     * - ``RTEMS_SUCCESSFUL``
573       - timer initiated successfully
574     * - ``RTEMS_INVALID_ADDRESS``
575       - ``routine`` is NULL
576     * - ``RTEMS_INVALID_ADDRESS``
577       - ``wall_time`` is NULL
578     * - ``RTEMS_INVALID_ID``
579       - invalid timer id
580     * - ``RTEMS_NOT_DEFINED``
581       - system date and time is not set
582     * - ``RTEMS_INVALID_CLOCK``
583       - invalid time of day
584     * - ``RTEMS_INCORRECT_STATE``
585       - Timer Server not initiated
586
587DESCRIPTION:
588    This directive initiates the timer specified by id and specifies that when
589    it fires it will be executed by the Timer Server.
590
591    If the timer is running, it is automatically canceled before being
592    initiated.  The timer is scheduled to fire at the time of day specified by
593    wall_time.  When the timer fires, the timer service routine routine will be
594    invoked with the argument user_data.
595
596NOTES:
597    This directive will not cause the running task to be preempted.
598
599.. raw:: latex
600
601   \clearpage
602
603.. index:: reset a timer
604.. index:: rtems_timer_reset
605
606.. _rtems_timer_reset:
607
608TIMER_RESET - Reset an interval timer
609-------------------------------------
610
611CALLING SEQUENCE:
612    .. code-block:: c
613
614        rtems_status_code rtems_timer_reset(
615            rtems_id   id
616        );
617
618DIRECTIVE STATUS CODES:
619    .. list-table::
620     :class: rtems-table
621
622     * - ``RTEMS_SUCCESSFUL``
623       - timer reset successfully
624     * - ``RTEMS_INVALID_ID``
625       - invalid timer id
626     * - ``RTEMS_NOT_DEFINED``
627       - attempted to reset a when or newly created timer
628
629DESCRIPTION:
630    This directive resets the timer associated with id.  This timer must have
631    been previously initiated with either the ``rtems_timer_fire_after`` or
632    ``rtems_timer_server_fire_after`` directive.  If active the timer is
633    canceled, after which the timer is reinitiated using the same interval and
634    timer service routine which the original ``rtems_timer_fire_after`` or
635    ``rtems_timer_server_fire_after`` directive used.
636
637NOTES:
638    If the timer has not been used or the last usage of this timer was by a
639    ``rtems_timer_fire_when`` or ``rtems_timer_server_fire_when`` directive,
640    then the ``RTEMS_NOT_DEFINED`` error is returned.
641
642    Restarting a cancelled after timer results in the timer being reinitiated
643    with its previous timer service routine and interval.
644
645    This directive will not cause the running task to be preempted.
Note: See TracBrowser for help on using the repository browser.