µ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++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2016-2025 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software
6 * for any purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can
9 * be obtained from https://opensource.org/licenses/mit.
10 */
11
12#ifndef CMSIS_PLUS_RTOS_OS_CLOCKS_H_
13#define CMSIS_PLUS_RTOS_OS_CLOCKS_H_
14
15// ----------------------------------------------------------------------------
16
17#if defined(__cplusplus)
18
19// ----------------------------------------------------------------------------
20
21#if defined(OS_USE_OS_APP_CONFIG_H)
22#include <cmsis-plus/os-app-config.h>
23#endif
24
26
27// ----------------------------------------------------------------------------
28
29#pragma GCC diagnostic push
30#if defined(__clang__)
31#pragma clang diagnostic ignored "-Wc++98-compat"
32#pragma clang diagnostic ignored "-Wdocumentation-unknown-command"
33#endif
34
35// ----------------------------------------------------------------------------
36
37namespace os
38{
39 namespace rtos
40 {
41
42 // ========================================================================
43
52#pragma GCC diagnostic push
53#if defined(__clang__)
54#pragma clang diagnostic ignored "-Wpadded"
55#elif defined(__GNUC__)
56#pragma GCC diagnostic ignored "-Wpadded"
57#pragma GCC diagnostic ignored "-Wsuggest-final-methods"
58#pragma GCC diagnostic ignored "-Wsuggest-final-types"
59#endif
61 {
62 public:
63 // ----------------------------------------------------------------------
64
79
89
100
105 // ----------------------------------------------------------------------
111 protected:
121 clock (const char* name);
122
127 public:
132 // The rule of five.
133 clock (const clock&) = delete;
134 clock (clock&&) = delete;
135 clock&
136 operator= (const clock&)
137 = delete;
138 clock&
139 operator= (clock&&)
140 = delete;
141
149 virtual ~clock ();
150
155 // ----------------------------------------------------------------------
156 public:
169 virtual void
170 start (void)
171 = 0;
172
180 virtual timestamp_t
181 now (void);
182
190 steady_now (void);
191
201 sleep_for (duration_t duration);
202
210 virtual result_t
211 sleep_until (timestamp_t timestamp);
212
222 wait_for (duration_t timeout);
223
231
238 virtual offset_t
239 offset (void);
240
246 virtual offset_t
248
254 steady_list (void);
255
256 void
257 internal_increment_count (void);
258
259 void
260 internal_check_timestamps (void);
261
270 protected:
282 virtual result_t
283 internal_wait_until_ (timestamp_t timestamp,
285
290 // ----------------------------------------------------------------------
301 duration_t volatile sleep_count_ = 0;
302
306 timestamp_t volatile steady_count_ = 0;
307
315 };
316#pragma GCC diagnostic pop
317
318 // ========================================================================
319
325 class adjustable_clock : public clock
326 {
327 // ----------------------------------------------------------------------
333 protected:
343 adjustable_clock (const char* name);
344
349 public:
354 // The rule of five.
355 adjustable_clock (const adjustable_clock&) = delete;
358 operator= (const adjustable_clock&)
359 = delete;
361 operator= (adjustable_clock&&)
362 = delete;
363
371 virtual ~adjustable_clock () override;
372
377 public:
390 virtual timestamp_t
391 now (void) override;
392
400 virtual result_t
401 sleep_until (timestamp_t timestamp) override;
402
409 virtual offset_t
410 offset (void) override;
411
417 virtual offset_t
418 offset (offset_t value) override;
419
420 void
422
427 protected:
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:
472
477 // ----------------------------------------------------------------------
487
492 // The rule of five.
493 clock_systick (const clock_systick&) = delete;
494 clock_systick (clock_systick&&) = delete;
496 operator= (const clock_systick&)
497 = delete;
499 operator= (clock_systick&&)
500 = delete;
501
509 virtual ~clock_systick () override;
510
515 // ----------------------------------------------------------------------
521 virtual void
522 start (void) override;
523
531 template <typename Rep_T>
532 static constexpr clock::duration_t
533 ticks_cast (Rep_T microsec);
534
539 // ----------------------------------------------------------------------
540 protected:
550#if defined(OS_USE_RTOS_PORT_CLOCK_SYSTICK_WAIT_FOR)
551
561 virtual result_t
562 internal_wait_until_ (timestamp_t timestamp,
564
565#endif /* defined(OS_USE_RTOS_PORT_CLOCK_SYSTICK_WAIT_FOR) */
566
574 };
575
581 extern clock_systick sysclock;
582
583 // ========================================================================
584
591 {
592 public:
601 static constexpr uint32_t frequency_hz = 1;
602
607 // ----------------------------------------------------------------------
617
622 // The rule of five.
623 clock_rtc (const clock_rtc&) = delete;
624 clock_rtc (clock_rtc&&) = delete;
625 clock_rtc&
626 operator= (const clock_rtc&)
627 = delete;
628 clock_rtc&
629 operator= (clock_rtc&&)
630 = delete;
631
639 virtual ~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,
682 clock_timestamps_list& list);
683
684#endif
685
689 };
690
696 extern clock_rtc rtclock;
697
698 // ========================================================================
699
705 class clock_highres : public clock
706 {
707 public:
717
722 // The rule of five.
723 clock_highres (const clock_highres&) = delete;
724 clock_highres (clock_highres&&) = delete;
726 operator= (const clock_highres&)
727 = delete;
729 operator= (clock_highres&&)
730 = delete;
731
739 virtual ~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:
783 };
784
790 extern clock_highres hrclock;
791
792 } /* namespace rtos */
793} /* namespace os */
794
795// ===== Inline & template implementations ====================================
796
797namespace os
798{
799 namespace rtos
800 {
801
802 // ========================================================================
803
808 inline clock::clock (const char* name) : internal::object_named{ name }
809 {
810 }
811
812 inline internal::clock_timestamps_list& __attribute__ ((always_inline))
813 clock::steady_list (void)
814 {
815 return steady_list_;
816 }
817
818 inline void __attribute__ ((always_inline))
819 clock::internal_increment_count (void)
820 {
821 // One more tick count passed.
822
823#pragma GCC diagnostic push
824#if defined(__clang__)
825#pragma clang diagnostic ignored "-Wdeprecated-volatile"
826#elif defined(__GNUC__)
827#pragma GCC diagnostic ignored "-Wvolatile"
828#endif
829 ++steady_count_;
830#pragma GCC diagnostic pop
831 }
832
833 inline void __attribute__ ((always_inline))
834 clock::internal_check_timestamps (void)
835 {
836 steady_list_.check_timestamp (steady_count_);
837 }
838
843 // ========================================================================
844
849 inline adjustable_clock::adjustable_clock (const char* name)
850 : clock{ name }
851 {
852 }
853
854 inline void __attribute__ ((always_inline))
856 {
857 clock::internal_check_timestamps ();
858
859#pragma GCC diagnostic push
860#if defined(__clang__)
861#pragma clang diagnostic ignored "-Wsign-conversion"
862#elif defined(__GNUC__)
863#pragma GCC diagnostic ignored "-Wsign-conversion"
864#endif
865 adjusted_list_.check_timestamp (steady_count_ + offset_);
866#pragma GCC diagnostic pop
867 }
868
873 // ========================================================================
883 template <typename Rep_T>
884 constexpr clock::duration_t
885 clock_systick::ticks_cast (Rep_T microsec)
886 {
887 // TODO: add some restrictions to match only numeric types
888 return static_cast<clock::duration_t> (
889 (((microsec) * (static_cast<Rep_T> (frequency_hz)))
890 + static_cast<Rep_T> (1000000ul) - 1)
891 / static_cast<Rep_T> (1000000ul));
892 }
893
894#pragma GCC diagnostic push
895#if defined(__clang__)
896#pragma clang diagnostic ignored "-Wc++98-compat"
897#endif
901#pragma GCC diagnostic pop
902
903 // ========================================================================
904 inline void __attribute__ ((always_inline))
905 clock_highres::internal_increment_count (void)
906 {
907 // Increment the highres count by SysTick divisor.
908#pragma GCC diagnostic push
909#if defined(__clang__)
910#pragma clang diagnostic ignored "-Wdeprecated-volatile"
911#elif defined(__GNUC__)
912#pragma GCC diagnostic ignored "-Wvolatile"
913#endif
914 steady_count_ += port::clock_highres::cycles_per_tick ();
915#pragma GCC diagnostic pop
916 }
917
918 inline uint32_t __attribute__ ((always_inline))
919 clock_highres::input_clock_frequency_hz (void)
920 {
922 }
923
924 // ========================================================================
925
926 } /* namespace rtos */
927} /* namespace os */
928
929#pragma GCC diagnostic pop
930
931// ----------------------------------------------------------------------------
932
933#endif /* __cplusplus */
934
935// ----------------------------------------------------------------------------
936
937#endif /* CMSIS_PLUS_RTOS_OS_CLOCKS_H_ */
Adjustable (non-steady) clock.
Definition os-clocks.h:326
virtual timestamp_t now(void) override
Tell the current time adjusted for epoch.
virtual offset_t offset(void) override
Get adjustment offset.
virtual result_t sleep_until(timestamp_t timestamp) override
Sleep until an absolute timestamp.
virtual ~adjustable_clock() override
Destruct the clock object instance.
virtual offset_t offset(offset_t value) override
Set 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:905
virtual ~clock_highres() override
Destruct the SysTick clock object instance.
virtual void start(void) override
Start the clock.
uint32_t input_clock_frequency_hz(void)
Definition os-clocks.h:919
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:601
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:471
virtual void start(void) override
Start the clock.
clock_systick()
Construct a SysTick clock object instance.
static constexpr clock::duration_t ticks_cast(Rep_T microsec)
Convert microseconds to ticks.
virtual ~clock_systick() override
Destruct the SysTick clock object instance.
Generic clock.
Definition os-clocks.h:61
virtual offset_t offset(void)
Get adjustment offset (placeholder).
virtual offset_t offset(offset_t value)
Set adjustment offset (placeholder)
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:666
Base class for named objects.
Definition os-decls.h:352
const char * name(void) const
Get object name.
Definition os-decls.h:753
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:78
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:88
clock_systick sysclock
The system clock object instance.
port::clock::offset_t offset_t
Type of variables holding clock offsets.
Definition os-clocks.h:99
uint32_t duration_t
Type of variables holding timer durations.
Definition os-decls.h:812
uint64_t timestamp_t
Type of variables holding time stamps.
Definition os-decls.h:821
uint32_t result_t
Type of values returned by RTOS functions.
Definition os-decls.h:95
System namespace.