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

5
Last change on this file since c2ee227 was 3384994, checked in by Chris Johns <chrisj@…>, on 11/13/17 at 02:25:18

Clean up sphinx warnings.

  • Fix minor formatting issues.
  • Fix reference the gloassary TLS using ':term:'.
  • Make sure nothing is between an anchor and the heading where ':ref:' references the anchor. This meant moving all the recently added '.. index::' entries.

Update #3232.
Update #3229.

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