source: rtems-docs/c-user/clock_manager.rst @ e52906b

5
Last change on this file since e52906b was e52906b, checked in by Sebastian Huber <sebastian.huber@…>, on 01/09/19 at 15:14:06

Simplify SPDX-License-Identifier comment

  • Property mode set to 100644
File size: 19.4 KB
Line 
1.. SPDX-License-Identifier: CC-BY-SA-4.0
2
3.. Copyright (C) 1988, 2008 On-Line Applications Research Corporation (OAR)
4
5.. index:: clock
6
7Clock Manager
8*************
9
10Introduction
11============
12
13The clock manager provides support for time of day
14and other time related capabilities.  The directives provided by
15the clock manager are:
16
17- rtems_clock_set_ - Set date and time
18
19- rtems_clock_get_tod_ - Get date and time in TOD format
20
21- rtems_clock_get_tod_timeval_ - Get date and time in timeval format
22
23- rtems_clock_get_seconds_since_epoch_ - Get seconds since epoch
24
25- rtems_clock_get_ticks_per_second_ - Get ticks per second
26
27- rtems_clock_get_ticks_since_boot_ - Get current ticks counter value
28
29- rtems_clock_tick_later_ - Get tick value in the future
30
31- rtems_clock_tick_later_usec_ - Get tick value in the future in microseconds
32
33- rtems_clock_tick_before_ - Is tick value is before a point in time
34
35- rtems_clock_get_uptime_ - Get time since boot
36
37- rtems_clock_get_uptime_timeval_ - Get time since boot in timeval format
38
39- rtems_clock_get_uptime_seconds_ - Get seconds since boot
40
41- rtems_clock_get_uptime_nanoseconds_ - Get nanoseconds since boot
42
43Background
44==========
45
46Required Support
47----------------
48
49For the features provided by the clock manager to be utilized, periodic timer
50interrupts are required.  Therefore, a real-time clock or hardware timer is
51necessary to create the timer interrupts.  The clock tick directive
52is normally called by the timer ISR to announce to RTEMS that a system clock
53tick has occurred.  Elapsed time is measured in ticks.  A tick is defined to be
54an integral number of microseconds which is specified by the user in the
55Configuration Table.
56
57.. _Time and Date Data Structures:
58
59Time and Date Data Structures
60-----------------------------
61
62The clock facilities of the clock manager operate upon calendar time.  These
63directives utilize the following date and time structure for the native time
64and date format:
65
66.. index:: rtems_time_of_day
67
68.. code-block:: c
69
70    struct rtems_tod_control {
71        uint32_t year;   /* greater than 1987 */
72        uint32_t month;  /* 1 - 12 */
73        uint32_t day;    /* 1 - 31 */
74        uint32_t hour;   /* 0 - 23 */
75        uint32_t minute; /* 0 - 59 */
76        uint32_t second; /* 0 - 59 */
77        uint32_t ticks;  /* elapsed between seconds */
78    };
79    typedef struct rtems_tod_control rtems_time_of_day;
80
81The native date and time format is the only format supported when setting the
82system date and time using the ``rtems_clock_set`` directive.  Some
83applications expect to operate on a *UNIX-style* date and time data structure.
84The ``rtems_clock_get_tod_timeval`` always returns the date and time in
85``struct timeval`` format.
86
87The ``struct timeval`` data structure has two fields: ``tv_sec`` and
88``tv_usec`` which are seconds and microseconds, respectively.  The ``tv_sec``
89field in this data structure is the number of seconds since the POSIX epoch of
90*January 1, 1970* but will never be prior to the RTEMS epoch of *January 1,
911988*.
92
93.. index:: timeslicing
94
95Clock Tick and Timeslicing
96--------------------------
97
98Timeslicing is a task scheduling discipline in which tasks of equal priority
99are executed for a specific period of time before control of the CPU is passed
100to another task.  It is also sometimes referred to as the automatic round-robin
101scheduling algorithm.  The length of time allocated to each task is known as
102the quantum or timeslice.
103
104The system's timeslice is defined as an integral number of ticks, and is
105specified in the Configuration Table.  The timeslice is defined for the entire
106system of tasks, but timeslicing is enabled and disabled on a per task basis.
107
108The clock tick directives implement timeslicing by decrementing the
109running task's time-remaining counter when both timeslicing and preemption are
110enabled.  If the task's timeslice has expired, then that task will be preempted
111if there exists a ready task of equal priority.
112
113.. index:: delays
114
115Delays
116------
117
118A sleep timer allows a task to delay for a given interval or up until a given
119time, and then wake and continue execution.  This type of timer is created
120automatically by the ``rtems_task_wake_after`` and ``rtems_task_wake_when``
121directives and, as a result, does not have an RTEMS ID.  Once activated, a
122sleep timer cannot be explicitly deleted.  Each task may activate one and only
123one sleep timer at a time.
124
125.. index:: timeouts
126
127Timeouts
128--------
129
130Timeouts are a special type of timer automatically created when the timeout
131option is used on the ``rtems_message_queue_receive``, ``rtems_event_receive``,
132``rtems_semaphore_obtain`` and ``rtems_region_get_segment`` directives.  Each
133task may have one and only one timeout active at a time.  When a timeout
134expires, it unblocks the task with a timeout status code.
135
136Operations
137==========
138
139Announcing a Tick
140-----------------
141
142RTEMS provides the several clock tick directives which are called from the
143user's real-time clock ISR to inform RTEMS that a tick has elapsed.  Depending
144on the timer hardware capabilities the clock driver must choose the most
145appropriate clock tick directive.  The tick frequency value, defined in
146microseconds, is a configuration parameter found in the Configuration Table.
147RTEMS divides one million microseconds (one second) by the number of
148microseconds per tick to determine the number of calls to the clock tick
149directive per second.  The frequency of clock tick calls determines the
150resolution (granularity) for all time dependent RTEMS actions.  For example,
151calling the clock tick directive ten times per second yields a higher
152resolution than calling the clock tick two times per second.  The clock tick
153directives are responsible for maintaining both calendar time and the dynamic
154set of timers.
155
156Setting the Time
157----------------
158
159The ``rtems_clock_set`` directive allows a task or an ISR to set the date and
160time maintained by RTEMS.  If setting the date and time causes any outstanding
161timers to pass their deadline, then the expired timers will be fired during the
162invocation of the ``rtems_clock_set`` directive.
163
164Obtaining the Time
165------------------
166
167RTEMS provides multiple directives which can be used by an application to obtain the current date and time or date and time related information.  These directives allow a task or an ISR to obtain the current date and time or date and time related information.  The current date and time can be returned in either native or *UNIX-style* format.  Additionally, the application can obtain date and time related information such as the number of seconds since the RTEMS epoch, the number of ticks since the executive was initialized, and the number of ticks per second.  The following directives are available:
168
169``rtems_clock_get_tod``
170  obtain native style date and time
171
172``rtems_clock_get_time_value``
173  obtain *UNIX-style* date and time
174
175``rtems_clock_get_ticks_since_boot``
176  obtain number of ticks since RTEMS was initialized
177
178``rtems_clock_get_seconds_since_epoch``
179  obtain number of seconds since RTEMS epoch
180
181``rtems_clock_get_ticks_per_second``
182  obtain number of clock ticks per second
183
184Calendar time operations will return an error code if invoked before the date
185and time have been set.
186
187.. index:: rtems_clock_get
188
189Transition Advice for the Obsolete rtems_clock_get
190--------------------------------------------------
191
192The method ``rtems_clock_get`` took an untyped pointer with an
193options argument to indicate the time information desired. This has
194been replaced with a set of typed directives whose name is of the form
195``rtems_clock_get_INFORMATION`` where INFORMATION indicates the type of
196information and possibly the format.  These methods directly correspond to
197what were previously referred to ask "clock options." These strongly typed
198were available for multiple releases in parallel with ``rtems_clock_get``
199until that method was removed.
200
201
202Directives
203==========
204
205This section details the clock manager's directives.  A subsection is dedicated
206to each of this manager's directives and describes the calling sequence,
207related constants, usage, and status codes.
208
209.. raw:: latex
210
211   \clearpage
212
213.. _rtems_clock_set:
214
215.. index:: set the time of day
216.. index:: rtems_clock_set
217
218CLOCK_SET - Set date and time
219-----------------------------
220
221CALLING SEQUENCE:
222    .. code-block:: c
223
224        rtems_status_code rtems_clock_set(
225            rtems_time_of_day *time_buffer
226        );
227
228DIRECTIVE STATUS CODES:
229    .. list-table::
230      :class: rtems-table
231
232      * - ``RTEMS_SUCCESSFUL``
233        - date and time set successfully
234      * - ``RTEMS_INVALID_ADDRESS``
235        - ``time_buffer`` is NULL
236      * - ``RTEMS_INVALID_CLOCK``
237        - invalid time of day
238
239DESCRIPTION:
240    This directive sets the system date and time.  The date, time, and ticks in
241    the time_buffer structure are all range-checked, and an error is returned
242    if any one is out of its valid range.
243
244NOTES:
245    Years before 1988 are invalid.
246
247    The system date and time are based on the configured tick rate (number of
248    microseconds in a tick).
249
250    Setting the time forward may cause a higher priority task, blocked waiting
251    on a specific time, to be made ready.  In this case, the calling task will
252    be preempted after the next clock tick.
253
254    Re-initializing RTEMS causes the system date and time to be reset to an
255    uninitialized state.  Another call to ``rtems_clock_set`` is required to
256    re-initialize the system date and time to application specific
257    specifications.
258
259.. raw:: latex
260
261   \clearpage
262
263.. _rtems_clock_get_tod:
264
265.. index:: obtain the time of day
266.. index:: rtems_clock_get_tod
267
268CLOCK_GET_TOD - Get date and time in TOD format
269-----------------------------------------------
270
271CALLING SEQUENCE:
272    .. code-block:: c
273
274        rtems_status_code rtems_clock_get_tod(
275            rtems_time_of_day *time_buffer
276        );
277
278DIRECTIVE STATUS CODES:
279    .. list-table::
280      :class: rtems-table
281
282      * - ``RTEMS_SUCCESSFUL``
283        - current time obtained successfully
284      * - ``RTEMS_NOT_DEFINED``
285        - system date and time is not set
286      * - ``RTEMS_INVALID_ADDRESS``
287        - ``time_buffer`` is NULL
288
289DESCRIPTION:
290    This directive obtains the system date and time.  If the date and time has
291    not been set with a previous call to ``rtems_clock_set``, then the
292    ``RTEMS_NOT_DEFINED`` status code is returned.
293
294NOTES:
295    This directive is callable from an ISR.
296
297    This directive will not cause the running task to be preempted.
298    Re-initializing RTEMS causes the system date and time to be reset to an
299    uninitialized state.  Another call to ``rtems_clock_set`` is required to
300    re-initialize the system date and time to application specific
301    specifications.
302
303.. raw:: latex
304
305   \clearpage
306
307.. _rtems_clock_get_tod_timeval:
308
309.. index:: obtain the time of day
310.. index:: rtems_clock_get_tod_timeval
311
312CLOCK_GET_TOD_TIMEVAL - Get date and time in timeval format
313-----------------------------------------------------------
314
315CALLING SEQUENCE:
316    .. code-block:: c
317
318        rtems_status_code rtems_clock_get_tod_interval(
319            struct timeval  *time
320        );
321
322DIRECTIVE STATUS CODES:
323    .. list-table::
324      :class: rtems-table
325      * - ``RTEMS_SUCCESSFUL``
326        - current time obtained successfully
327      * - ``RTEMS_NOT_DEFINED``
328        - system date and time is not set
329      * - ``RTEMS_INVALID_ADDRESS``
330        - ``time`` is NULL
331
332DESCRIPTION:
333    This directive obtains the system date and time in POSIX ``struct timeval``
334    format.  If the date and time has not been set with a previous call to
335    ``rtems_clock_set``, then the ``RTEMS_NOT_DEFINED`` status code is
336    returned.
337
338NOTES:
339    This directive is callable from an ISR.
340
341    This directive will not cause the running task to be preempted.
342    Re-initializing RTEMS causes the system date and time to be reset to an
343    uninitialized state.  Another call to ``rtems_clock_set`` is required to
344    re-initialize the system date and time to application specific
345    specifications.
346
347.. raw:: latex
348
349   \clearpage
350
351.. _rtems_clock_get_seconds_since_epoch:
352
353.. index:: obtain seconds since epoch
354.. index:: rtems_clock_get_seconds_since_epoch
355
356CLOCK_GET_SECONDS_SINCE_EPOCH - Get seconds since epoch
357-------------------------------------------------------
358
359CALLING SEQUENCE:
360    .. code-block:: c
361
362        rtems_status_code rtems_clock_get_seconds_since_epoch(
363            rtems_interval *the_interval
364        );
365
366DIRECTIVE STATUS CODES:
367    .. list-table::
368      :class: rtems-table
369      * - ``RTEMS_SUCCESSFUL``
370        - current time obtained successfully
371      * - ``RTEMS_NOT_DEFINED``
372        - system date and time is not set
373      * - ``RTEMS_INVALID_ADDRESS``
374        - ``the_interval`` is NULL
375
376DESCRIPTION:
377    This directive returns the number of seconds since the RTEMS epoch and the
378    current system date and time.  If the date and time has not been set with a
379    previous call to ``rtems_clock_set``, then the ``RTEMS_NOT_DEFINED`` status
380    code is returned.
381
382NOTES:
383    This directive is callable from an ISR.
384
385    This directive will not cause the running task to be preempted.
386    Re-initializing RTEMS causes the system date and time to be reset to an
387    uninitialized state.  Another call to ``rtems_clock_set`` is required to
388    re-initialize the system date and time to application specific
389    specifications.
390
391.. raw:: latex
392
393   \clearpage
394
395.. _rtems_clock_get_ticks_per_second:
396
397.. index:: obtain seconds since epoch
398.. index:: rtems_clock_get_ticks_per_second
399
400CLOCK_GET_TICKS_PER_SECOND - Get ticks per second
401-------------------------------------------------
402
403CALLING SEQUENCE:
404    .. code-block:: c
405
406        rtems_interval rtems_clock_get_ticks_per_second(void);
407
408DIRECTIVE STATUS CODES:
409    NONE
410
411DESCRIPTION:
412    This directive returns the number of clock ticks per second.  This is
413    strictly based upon the microseconds per clock tick that the application
414    has configured.
415
416NOTES:
417    This directive is callable from an ISR.
418
419    This directive will not cause the running task to be preempted.
420
421.. raw:: latex
422
423   \clearpage
424
425.. _rtems_clock_get_ticks_since_boot:
426
427.. index:: obtain ticks since boot
428.. index:: get current ticks counter value
429.. index:: rtems_clock_get_ticks_since_boot
430
431CLOCK_GET_TICKS_SINCE_BOOT - Get current ticks counter value
432------------------------------------------------------------
433
434CALLING SEQUENCE:
435    .. code-block:: c
436
437        rtems_interval rtems_clock_get_ticks_since_boot(void);
438
439DIRECTIVE STATUS CODES:
440    NONE
441
442DESCRIPTION:
443
444    This directive returns the current tick counter value.  With a 1ms clock
445    tick, this counter overflows after 50 days since boot.  This is the
446    historical measure of uptime in an RTEMS system.  The newer service
447    ``rtems_clock_get_uptime`` is another and potentially more accurate way of
448    obtaining similar information.
449
450NOTES:
451
452    This directive is callable from an ISR.
453
454    This directive will not cause the running task to be preempted.
455
456.. raw:: latex
457
458   \clearpage
459
460.. _rtems_clock_tick_later:
461
462.. index:: rtems_clock_tick_later
463
464CLOCK_TICK_LATER - Get tick value in the future
465-----------------------------------------------
466
467CALLING SEQUENCE:
468    .. code-block:: c
469
470        rtems_interval rtems_clock_tick_later(
471            rtems_interval delta
472        );
473
474DESCRIPTION:
475    Returns the ticks counter value delta ticks in the future.
476
477NOTES:
478    This directive is callable from an ISR.
479
480    This directive will not cause the running task to be preempted.
481
482.. raw:: latex
483
484   \clearpage
485
486.. _rtems_clock_tick_later_usec:
487
488.. index:: rtems_clock_tick_later_usec
489
490CLOCK_TICK_LATER_USEC - Get tick value in the future in microseconds
491--------------------------------------------------------------------
492
493CALLING SEQUENCE:
494    .. code-block:: c
495
496        rtems_interval rtems_clock_tick_later_usec(
497            rtems_interval delta_in_usec
498        );
499
500DESCRIPTION:
501    Returns the ticks counter value at least delta microseconds in the future.
502
503NOTES:
504    This directive is callable from an ISR.
505
506    This directive will not cause the running task to be preempted.
507
508.. raw:: latex
509
510   \clearpage
511
512.. _rtems_clock_tick_before:
513
514.. index:: rtems_clock_tick_before
515
516CLOCK_TICK_BEFORE - Is tick value is before a point in time
517-----------------------------------------------------------
518
519CALLING SEQUENCE:
520    .. code-block:: c
521
522        rtems_interval rtems_clock_tick_before(
523            rtems_interval tick
524        );
525
526DESCRIPTION:
527    Returns true if the current ticks counter value indicates a time before the
528    time specified by the tick value and false otherwise.
529
530NOTES:
531    This directive is callable from an ISR.
532
533    This directive will not cause the running task to be preempted.
534
535EXAMPLE:
536    .. code-block:: c
537
538        status busy( void )
539        {
540            rtems_interval timeout = rtems_clock_tick_later_usec( 10000 );
541            do {
542                if ( ok() ) {
543                    return success;
544                }
545            } while ( rtems_clock_tick_before( timeout ) );
546            return timeout;
547        }
548
549.. raw:: latex
550
551   \clearpage
552
553.. _rtems_clock_get_uptime:
554
555.. index:: clock get uptime
556.. index:: uptime
557.. index:: rtems_clock_get_uptime
558
559CLOCK_GET_UPTIME - Get the time since boot
560------------------------------------------
561
562CALLING SEQUENCE:
563    .. code-block:: c
564
565        rtems_status_code rtems_clock_get_uptime(
566            struct timespec *uptime
567        );
568
569DIRECTIVE STATUS CODES:
570    .. list-table::
571      :class: rtems-table
572      * - ``RTEMS_SUCCESSFUL``
573        - clock tick processed successfully
574      * - ``RTEMS_INVALID_ADDRESS``
575        - ``time_buffer`` is ``NULL``
576
577DESCRIPTION:
578    This directive returns the seconds and nanoseconds since the system was
579    booted.  If the BSP supports nanosecond clock accuracy, the time reported
580    will probably be different on every call.
581
582NOTES:
583    This directive may be called from an ISR.
584
585.. raw:: latex
586
587   \clearpage
588
589.. _rtems_clock_get_uptime_timeval:
590
591.. index:: clock get uptime interval
592.. index:: uptime
593.. index:: rtems_clock_get_uptime_timeval
594
595CLOCK_GET_UPTIME_TIMEVAL - Get the time since boot in timeval format
596--------------------------------------------------------------------
597
598CALLING SEQUENCE:
599    .. code-block:: c
600
601        void rtems_clock_get_uptime_timeval(
602            struct timeval *uptime
603        );
604
605DIRECTIVE STATUS CODES:
606    NONE
607
608DESCRIPTION:
609    This directive returns the seconds and microseconds since the system was
610    booted.  If the BSP supports nanosecond clock accuracy, the time reported
611    will probably be different on every call.
612
613NOTES:
614    This directive may be called from an ISR.
615
616.. raw:: latex
617
618   \clearpage
619
620.. _rtems_clock_get_uptime_seconds:
621
622.. index:: clock get uptime seconds
623.. index:: uptime
624.. index:: rtems_clock_get_uptime_seconds
625
626CLOCK_GET_UPTIME_SECONDS - Get the seconds since boot
627-----------------------------------------------------
628
629CALLING SEQUENCE:
630    .. code-block:: c
631
632        time_t rtems_clock_get_uptime_seconds(void);
633
634DIRECTIVE STATUS CODES:
635    The system uptime in seconds.
636
637DESCRIPTION:
638    This directive returns the seconds since the system was booted.
639
640NOTES:
641    This directive may be called from an ISR.
642
643.. raw:: latex
644
645   \clearpage
646
647.. _rtems_clock_get_uptime_nanoseconds:
648
649.. index:: clock get nanoseconds uptime
650.. index:: uptime
651.. index:: rtems_clock_get_uptime_nanoseconds
652
653CLOCK_GET_UPTIME_NANOSECONDS - Get the nanoseconds since boot
654-------------------------------------------------------------
655
656CALLING SEQUENCE:
657    .. code-block:: c
658
659        uint64_t rtems_clock_get_uptime_nanoseconds(void);
660
661DIRECTIVE STATUS CODES:
662    The system uptime in nanoseconds.
663
664DESCRIPTION:
665    This directive returns the nanoseconds since the system was booted.
666
667NOTES:
668    This directive may be called from an ISR.
Note: See TracBrowser for help on using the repository browser.