µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches
os-clocks.h
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ distribution.
3 * (https://github.com/micro-os-plus)
4 * Copyright (c) 2016-2023 Liviu Ionescu. All rights reserved.
5 *
6 * Permission to use, copy, modify, and/or distribute this software
7 * for any purpose is hereby granted, under the terms of the MIT license.
8 *
9 * If a copy of the license was not distributed with this file, it can
10 * be obtained from https://opensource.org/licenses/mit/.
11 */
12
13#ifndef CMSIS_PLUS_RTOS_OS_CLOCKS_H_
14#define CMSIS_PLUS_RTOS_OS_CLOCKS_H_
15
16// ----------------------------------------------------------------------------
17
18#if defined(__cplusplus)
19
20// ----------------------------------------------------------------------------
21
22#if defined(OS_USE_OS_APP_CONFIG_H)
23#include <cmsis-plus/os-app-config.h>
24#endif
25
27
28// ----------------------------------------------------------------------------
29
30#pragma GCC diagnostic push
31#if defined(__clang__)
32#pragma clang diagnostic ignored "-Wc++98-compat"
33#pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
34#endif
35
36// ----------------------------------------------------------------------------
37
38namespace os
39{
40 namespace rtos
41 {
42
43 // ========================================================================
44
50#pragma GCC diagnostic push
51#if defined(__clang__)
52#pragma clang diagnostic ignored "-Wpadded"
53#elif defined(__GNUC__)
54#pragma GCC diagnostic ignored "-Wpadded"
55#pragma GCC diagnostic ignored "-Wsuggest-final-methods"
56#pragma GCC diagnostic ignored "-Wsuggest-final-types"
57#endif
59 {
60 public:
61
62 // ----------------------------------------------------------------------
63
77
86
96
101 // ----------------------------------------------------------------------
107 protected:
108
117 clock (const char* name);
118
123 public:
124
129 // The rule of five.
130 clock (const clock&) = delete;
131 clock (clock&&) = delete;
132 clock&
133 operator= (const clock&) = delete;
134 clock&
135 operator= (clock&&) = delete;
136
144 virtual
145 ~clock ();
146
151 // ----------------------------------------------------------------------
152 public:
153
166 virtual void
167 start (void) = 0;
168
176 virtual timestamp_t
177 now (void);
178
186 steady_now (void);
187
197 sleep_for (duration_t duration);
198
206 virtual result_t
207 sleep_until (timestamp_t timestamp);
208
218 wait_for (duration_t timeout);
219
227
238 virtual offset_t
239 offset (void);
240
246 virtual offset_t
247 offset (offset_t value);
248
250 steady_list (void);
251
252 void
253 internal_increment_count (void);
254
255 void
256 internal_check_timestamps (void);
257
266 protected:
267
279 virtual result_t
280 internal_wait_until_ (timestamp_t timestamp,
282
287 // ----------------------------------------------------------------------
298 duration_t volatile sleep_count_ = 0;
299
303 timestamp_t volatile steady_count_ = 0;
304
312 };
313#pragma GCC diagnostic pop
314
315 // ========================================================================
316
322 class adjustable_clock : public clock
323 {
324 // ----------------------------------------------------------------------
330 protected:
331
341 adjustable_clock (const char* name);
342
347 public:
348
353 // The rule of five.
354 adjustable_clock (const adjustable_clock&) = delete;
357 operator= (const adjustable_clock&) = delete;
359 operator= (adjustable_clock&&) = delete;
360
368 virtual
369 ~adjustable_clock () override;
370
375 public:
376
389 virtual timestamp_t
390 now (void) override;
391
399 virtual result_t
400 sleep_until (timestamp_t timestamp) override;
401
408 virtual offset_t
409 offset (void) override;
410
416 virtual offset_t
417 offset (offset_t value) override;
418
419 void
421
426 protected:
427
440 offset_t volatile offset_ = 0;
441
442 internal::clock_timestamps_list adjusted_list_;
443
451 };
452
453 // ========================================================================
454
460 class clock_systick : public clock
461 {
462 public:
463
473
478 // ----------------------------------------------------------------------
487 clock_systick ();
488
493 // The rule of five.
494 clock_systick (const clock_systick&) = delete;
495 clock_systick (clock_systick&&) = delete;
497 operator= (const clock_systick&) = delete;
499 operator= (clock_systick&&) = delete;
500
508 virtual
509 ~clock_systick () override;
510
515 // ----------------------------------------------------------------------
521 virtual void
522 start (void) override;
523
530 template<typename Rep_T>
531 static constexpr clock::duration_t
532 ticks_cast (Rep_T microsec);
533
538 // ----------------------------------------------------------------------
539 protected:
540
550#if defined(OS_USE_RTOS_PORT_CLOCK_SYSTICK_WAIT_FOR)
551
561 virtual result_t
562 internal_wait_until_ (timestamp_t timestamp, internal::clock_timestamps_list& list);
563
564#endif /* defined(OS_USE_RTOS_PORT_CLOCK_SYSTICK_WAIT_FOR) */
565
574 };
575
581 extern clock_systick sysclock;
582
583 // ========================================================================
584
591 {
592 public:
593
602 static constexpr uint32_t frequency_hz = 1;
603
608 // ----------------------------------------------------------------------
617 clock_rtc ();
618
623 // The rule of five.
624 clock_rtc (const clock_rtc&) = delete;
625 clock_rtc (clock_rtc&&) = delete;
626 clock_rtc&
627 operator= (const clock_rtc&) = delete;
628 clock_rtc&
629 operator= (clock_rtc&&) = delete;
630
638 virtual
639 ~clock_rtc () override;
640
645 // ----------------------------------------------------------------------
658 virtual void
659 start (void) override;
660
669#if defined(OS_USE_RTOS_PORT_CLOCK_REALTIME_WAIT_FOR)
670
680 virtual result_t
681 internal_wait_until_ (timestamp_t timestamp, clock_timestamps_list& list);
682
683#endif
684
689 };
690
696 extern clock_rtc rtclock;
697
698 // ========================================================================
699
705 class clock_highres : public clock
706 {
707 public:
708
717 clock_highres ();
718
723 // The rule of five.
724 clock_highres (const clock_highres&) = delete;
725 clock_highres (clock_highres&&) = delete;
727 operator= (const clock_highres&) = delete;
729 operator= (clock_highres&&) = delete;
730
738 virtual
739 ~clock_highres () override;
740
745 // ----------------------------------------------------------------------
751 virtual void
752 start (void) override;
753
760 virtual timestamp_t
761 now (void) override;
762
763 uint32_t
765
766 void
768
773 // ----------------------------------------------------------------------
774 protected:
775
785 };
786
792 extern clock_highres hrclock;
793
794 } /* namespace rtos */
795} /* namespace os */
796
797// ===== Inline & template implementations ====================================
798
799namespace os
800{
801 namespace rtos
802 {
803
804 // ========================================================================
805
810 inline
811 clock::clock (const char* name) :
812 internal::object_named
813 { name }
814 {
815 }
816
817 inline internal::clock_timestamps_list&
818 __attribute__((always_inline))
819 clock::steady_list (void)
820 {
821 return steady_list_;
822 }
823
824 inline void
825 __attribute__((always_inline))
826 clock::internal_increment_count (void)
827 {
828 // One more tick count passed.
829
830#pragma GCC diagnostic push
831#if defined(__clang__)
832#pragma clang diagnostic ignored "-Wdeprecated-volatile"
833#elif defined(__GNUC__)
834#pragma GCC diagnostic ignored "-Wvolatile"
835#endif
836 ++steady_count_;
837#pragma GCC diagnostic pop
838 }
839
840 inline void
841 __attribute__((always_inline))
842 clock::internal_check_timestamps (void)
843 {
844 steady_list_.check_timestamp (steady_count_);
845 }
846
851 // ========================================================================
852
857 inline
858 adjustable_clock::adjustable_clock (const char* name) :
859 clock
860 { name }
861 {
862 }
863
864 inline void
865 __attribute__((always_inline))
867 {
868 clock::internal_check_timestamps ();
869
870#pragma GCC diagnostic push
871#if defined(__clang__)
872#pragma clang diagnostic ignored "-Wsign-conversion"
873#elif defined(__GNUC__)
874#pragma GCC diagnostic ignored "-Wsign-conversion"
875#endif
876 adjusted_list_.check_timestamp (steady_count_ + offset_);
877#pragma GCC diagnostic pop
878 }
879
884 // ========================================================================
894 template<typename Rep_T>
895 constexpr clock::duration_t
896 clock_systick::ticks_cast (Rep_T microsec)
897 {
898 // TODO: add some restrictions to match only numeric types
899 return static_cast<clock::duration_t> ((((microsec)
900 * (static_cast<Rep_T> (frequency_hz)))
901 + static_cast<Rep_T> (1000000ul) - 1)
902 / static_cast<Rep_T> (1000000ul));
903 }
904
905#pragma GCC diagnostic push
906#if defined(__clang__)
907#pragma clang diagnostic ignored "-Wc++98-compat"
908#endif
912#pragma GCC diagnostic pop
913
914 // ========================================================================
915 inline void
916 __attribute__((always_inline))
917 clock_highres::internal_increment_count (void)
918 {
919 // Increment the highres count by SysTick divisor.
920#pragma GCC diagnostic push
921#if defined(__clang__)
922#pragma clang diagnostic ignored "-Wdeprecated-volatile"
923#elif defined(__GNUC__)
924#pragma GCC diagnostic ignored "-Wvolatile"
925#endif
926 steady_count_ += port::clock_highres::cycles_per_tick ();
927#pragma GCC diagnostic pop
928 }
929
930 inline uint32_t
931 __attribute__((always_inline))
932 clock_highres::input_clock_frequency_hz (void)
933 {
935 }
936
937 // ========================================================================
938
939 } /* namespace rtos */
940} /* namespace os */
941
942#pragma GCC diagnostic pop
943
944// ----------------------------------------------------------------------------
945
946#endif /* __cplusplus */
947
948// ----------------------------------------------------------------------------
949
950#endif /* CMSIS_PLUS_RTOS_OS_CLOCKS_H_ */
Adjustable (non-steady) clock.
Definition os-clocks.h:323
virtual ~adjustable_clock() override
Destruct the clock object instance.
virtual timestamp_t now(void) override
Tell the current time adjusted for epoch.
virtual result_t sleep_until(timestamp_t timestamp) override
Sleep until an absolute timestamp.
virtual offset_t offset(void) override
Get adjustment offset.
void internal_check_timestamps(void)
High Resolution derived clock.
Definition os-clocks.h:706
void internal_increment_count(void)
Definition os-clocks.h:917
virtual ~clock_highres() override
Destruct the SysTick clock object instance.
virtual void start(void) override
uint32_t input_clock_frequency_hz(void)
Definition os-clocks.h:932
clock_highres()
Construct a SysTick clock object instance.
virtual timestamp_t now(void) override
Tell the current time.
Real time clock.
Definition os-clocks.h:591
static constexpr uint32_t frequency_hz
Real time clock frequency in Hz.
Definition os-clocks.h:602
virtual void start(void) override
Initialise and make the RTC tick.
virtual ~clock_rtc() override
Destruct the real time clock object instance.
clock_rtc()
Construct a real time clock object instance.
SysTick derived clock.
Definition os-clocks.h:461
static constexpr uint32_t frequency_hz
SysTick frequency in Hz.
Definition os-clocks.h:472
virtual ~clock_systick() override
Destruct the SysTick clock object instance.
clock_systick()
Construct a SysTick clock object instance.
static constexpr clock::duration_t ticks_cast(Rep_T microsec)
Convert microseconds to ticks.
virtual void start(void) override
Generic clock.
Definition os-clocks.h:59
result_t wait_for(duration_t timeout)
Timed wait for an event.
virtual result_t sleep_until(timestamp_t timestamp)
Sleep until an absolute timestamp.
timestamp_t update_for_slept_time(duration_t duration)
Increase the internal count after a deep sleep.
timestamp_t steady_now(void)
Tell the current time since startup.
virtual timestamp_t now(void)
Tell the current time, possibly adjusted for epoch.
virtual void start(void)=0
Start the clock.
virtual ~clock()
Destruct the clock object instance.
result_t sleep_for(duration_t duration)
Sleep for a relative duration.
Ordered list of time stamp nodes.
Definition os-lists.h:671
Base class for named objects.
Definition os-decls.h:351
const char * name(void) const
Get object name.
Definition os-decls.h:759
static uint32_t input_clock_frequency_hz(void)
static uint32_t cycles_per_tick(void)
#define OS_INTEGER_SYSTICK_FREQUENCY_HZ
Define the scheduler frequency, in Hz.
clock_t clock(void)
port::clock::duration_t duration_t
Type of variables holding clock durations.
Definition os-clocks.h:76
clock_highres hrclock
The high resolution clock object instance.
clock_rtc rtclock
The real time clock object instance.
port::clock::timestamp_t timestamp_t
Type of variables holding clock time stamps.
Definition os-clocks.h:85
clock_systick sysclock
The system clock object instance.
port::clock::offset_t offset_t
Type of variables holding clock offsets.
Definition os-clocks.h:95
uint32_t duration_t
Type of variables holding timer durations.
Definition os-decls.h:820
uint64_t timestamp_t
Type of variables holding time stamps.
Definition os-decls.h:828
uint32_t result_t
Type of values returned by RTOS functions.
Definition os-decls.h:96
System namespace.