µOS++ IIIe Reference 7.0.0
The third edition of µOS++, a POSIX inspired open source framework, written in C++
Loading...
Searching...
No Matches

C API clocks definitions. More...

Classes

struct  os_clock_s
 Clock object storage. More...
 

Typedefs

typedef struct os_clock_s os_clock_t
 Clock object storage.
 

Clock Functions

const char * os_clock_get_name (os_clock_t *clock)
 Get the clock name.
 
os_clock_timestamp_t os_clock_now (os_clock_t *clock)
 Tell the current time, possibly adjusted for epoch.
 
os_clock_timestamp_t os_clock_steady_now (os_clock_t *clock)
 Tell the current time since startup.
 
os_result_t os_clock_sleep_for (os_clock_t *clock, os_clock_duration_t duration)
 Sleep for a relative duration.
 
os_result_t os_clock_sleep_until (os_clock_t *clock, os_clock_timestamp_t timestamp)
 Sleep until an absolute timestamp.
 
os_result_t os_clock_wait_for (os_clock_t *clock, os_clock_duration_t timeout)
 Timed wait for an event.
 
os_clock_offset_t os_clock_get_offset (os_clock_t *clock)
 Get adjustment offset.
 
os_clock_offset_t os_clock_set_offset (os_clock_t *clock, os_clock_offset_t offset)
 Set adjustment offset.
 
os_clock_tos_clock_get_sysclock (void)
 Get sysclock (the system clock).
 
os_clock_tos_clock_get_rtclock (void)
 Get rtclock (the real-time clock).
 
os_clock_tos_clock_get_hrclock (void)
 Get hrclock (the high resolution clock).
 
os_clock_timestamp_t os_sysclock_now (void)
 Tell the current time since startup.
 
os_result_t os_sysclock_sleep_for (os_clock_duration_t duration)
 Sleep for a relative duration.
 
os_result_t os_sysclock_sleep_until (os_clock_timestamp_t timestamp)
 Sleep until an absolute timestamp.
 
os_result_t os_sysclock_wait_for (os_clock_duration_t timeout)
 Timed wait for an event.
 
os_clock_duration_t os_sysclock_ticks_cast (uint32_t microsec)
 Convert microseconds to ticks.
 
os_clock_duration_t os_sysclock_ticks_cast_long (uint64_t microsec)
 Convert microseconds to ticks.
 

Clock handlers

void os_systick_handler (void)
 SysTick interrupt handler.
 
void os_rtc_handler (void)
 RTC interrupt handler.
 

Detailed Description

For the complete definition, see
RTOS C++ API
Examples
int
os_main (int argc, char* argv[])
{
{
// Return number of ticks since epoch, or, if epoch not set, from startup.
ts = os_sysclock_now ();
// Return the number of ticks since startup.
ts = os_sysclock_steady_now ();
// An event may resume the thread before the timeout expire.
}
}
os_result_t os_sysclock_wait_for(os_clock_duration_t timeout)
Timed wait for an event.
os_clock_timestamp_t os_sysclock_now(void)
Tell the current time since startup.
os_result_t os_sysclock_sleep_until(os_clock_timestamp_t timestamp)
Sleep until an absolute timestamp.
os_result_t os_sysclock_sleep_for(os_clock_duration_t duration)
Sleep for a relative duration.
os_port_clock_timestamp_t os_clock_timestamp_t
Type of variables holding clock time stamps.
Definition os-c-decls.h:185
int os_main(int argc, char *argv[])
Application entry point, running on the main thread context.

Typedef Documentation

◆ os_clock_t

typedef struct os_clock_s os_clock_t

This C structure has the same size as the C++ os::rtos::clock object.

The members of this structure are hidden and should not be used directly, but only through specific functions.

See also
os::rtos::clock

Function Documentation

◆ os_clock_get_hrclock()

os_clock_t * os_clock_get_hrclock ( void  )
Returns
The address of the clock_highres instance.
Note
Can be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::hrclock

Definition at line 1396 of file os-c-wrapper.cpp.

1397{
1398 return (os_clock_t*)&hrclock;
1399}
clock_highres hrclock
The high resolution clock object instance.
Clock object storage.
Definition os-c-decls.h:644

References os::rtos::hrclock.

◆ os_clock_get_name()

const char * os_clock_get_name ( os_clock_t clock)
Parameters
[in]clockPointer to clock object instance.
Returns
Null terminated string.
Note
Can be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::clock::name()

Definition at line 1257 of file os-c-wrapper.cpp.

1258{
1259 assert (clock != nullptr);
1260 return (reinterpret_cast<rtos::clock&> (*clock)).name ();
1261}
Generic clock.
Definition os-clocks.h:61

◆ os_clock_get_offset()

os_clock_offset_t os_clock_get_offset ( os_clock_t clock)
Parameters
[in]clockPointer to clock object instance.
Returns
Integer value representing the offset to epoch.
Note
Can be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::adjustable_clock::offset()

Definition at line 1340 of file os-c-wrapper.cpp.

1341{
1342 assert (clock != nullptr);
1343 return (os_clock_offset_t)(reinterpret_cast<rtos::clock&> (*clock))
1344 .offset ();
1345}
os_port_clock_offset_t os_clock_offset_t
Type of variables holding clock offsets.
Definition os-c-decls.h:208

◆ os_clock_get_rtclock()

os_clock_t * os_clock_get_rtclock ( void  )
Returns
The address of the clock_rtc instance.
Note
Can be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::rtclock

Definition at line 1384 of file os-c-wrapper.cpp.

1385{
1386 return (os_clock_t*)&rtclock;
1387}
clock_rtc rtclock
The real time clock object instance.

References os::rtos::rtclock.

◆ os_clock_get_sysclock()

os_clock_t * os_clock_get_sysclock ( void  )
Returns
The address of the clock_systick instance.
Note
Can be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::sysclock

Definition at line 1372 of file os-c-wrapper.cpp.

1373{
1374 return (os_clock_t*)&sysclock;
1375}
clock_systick sysclock
The system clock object instance.

References os::rtos::sysclock.

◆ os_clock_now()

os_clock_timestamp_t os_clock_now ( os_clock_t clock)
Parameters
[in]clockPointer to clock object instance.
Returns
The clock current timestamp (time units from startup plus the epoch offset).
Note
Can be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::clock::now()

Definition at line 1270 of file os-c-wrapper.cpp.

1271{
1272 assert (clock != nullptr);
1273 return (os_clock_timestamp_t)(reinterpret_cast<rtos::clock&> (*clock))
1274 .now ();
1275}

◆ os_clock_set_offset()

os_clock_offset_t os_clock_set_offset ( os_clock_t clock,
os_clock_offset_t  offset 
)
Parameters
[in]clockPointer to clock object instance.
[in]offsetInteger representing the offset to epoch (positive).
Returns
Integer value representing the previous offset to epoch.
Warning
Cannot be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::adjustable_clock::offset(os::rtos::clock::offset_t)

Definition at line 1357 of file os-c-wrapper.cpp.

1358{
1359 assert (clock != nullptr);
1360 assert (clock != nullptr);
1361 return (os_clock_offset_t)(reinterpret_cast<rtos::clock&> (*clock))
1362 .offset ((clock::offset_t)offset);
1363}

◆ os_clock_sleep_for()

os_result_t os_clock_sleep_for ( os_clock_t clock,
os_clock_duration_t  duration 
)
Parameters
[in]clockPointer to clock object instance.
[in]durationThe number of clock units (ticks or seconds) to sleep.
Return values
ETIMEDOUTThe sleep lasted the entire duration.
EPERMCannot be invoked from an Interrupt Service Routines.
EINTRThe sleep was interrupted.
Warning
Cannot be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::clock::sleep_for()

Definition at line 1298 of file os-c-wrapper.cpp.

1299{
1300 assert (clock != nullptr);
1301 return (os_result_t)(reinterpret_cast<rtos::clock&> (*clock))
1302 .sleep_for (duration);
1303}
uint32_t os_result_t
Type of values returned by RTOS functions.
Definition os-c-decls.h:94

◆ os_clock_sleep_until()

os_result_t os_clock_sleep_until ( os_clock_t clock,
os_clock_timestamp_t  timestamp 
)
Parameters
[in]clockPointer to clock object instance.
[in]timestampThe absolute moment in time, in clock units.
Return values
ETIMEDOUTThe sleep lasted the entire duration.
EPERMCannot be invoked from an Interrupt Service Routines.
EINTRThe sleep was interrupted.
Warning
Cannot be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::clock::sleep_until()

Definition at line 1312 of file os-c-wrapper.cpp.

1313{
1314 assert (clock != nullptr);
1315 return (os_result_t)(reinterpret_cast<rtos::clock&> (*clock))
1316 .sleep_until (timestamp);
1317}

◆ os_clock_steady_now()

os_clock_timestamp_t os_clock_steady_now ( os_clock_t clock)
Parameters
[in]clockPointer to clock object instance.
Returns
The clock current timestamp (time units from startup).
Note
Can be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::clock::steady_now()

Definition at line 1284 of file os-c-wrapper.cpp.

1285{
1286 assert (clock != nullptr);
1287 return (os_clock_timestamp_t)(reinterpret_cast<rtos::clock&> (*clock))
1288 .steady_now ();
1289}

◆ os_clock_wait_for()

os_result_t os_clock_wait_for ( os_clock_t clock,
os_clock_duration_t  timeout 
)
Parameters
[in]clockPointer to clock object instance.
[in]timeoutThe timeout in clock units.
Return values
os_okAn event occurred before the timeout.
ETIMEDOUTThe wait lasted the entire duration.
EPERMCannot be invoked from an Interrupt Service Routines.
EINTRThe sleep was interrupted.
Warning
Cannot be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::clock::wait_for()

Definition at line 1326 of file os-c-wrapper.cpp.

1327{
1328 assert (clock != nullptr);
1329 return (os_result_t)(reinterpret_cast<rtos::clock&> (*clock))
1330 .wait_for (timeout);
1331}

◆ os_rtc_handler()

void os_rtc_handler ( void  )

Must be called from the physical RTC interrupt handler.

Definition at line 95 of file os-clocks.cpp.

96{
97
98#if defined(OS_USE_RTOS_PORT_SCHEDULER)
99 // Prevent scheduler actions before starting it.
100 if (scheduler::started ())
101 {
103 }
104#endif
105
106#if defined(OS_TRACE_RTOS_RTC_TICK)
107 trace_putchar ('!');
108#endif
109
110 {
111 // ----- Enter critical section -------------------------------------------
113
114 rtclock.internal_increment_count ();
115 // ----- Exit critical section --------------------------------------------
116 }
117
119}
void internal_check_timestamps(void)
Interrupts critical section RAII helper.
Definition os-sched.h:502
static void internal_interrupt_service_routine(void)
RTC implementation hook.
bool started(void)
Check if the scheduler was started.
Definition os-sched.h:829
int trace_putchar(int c)

References os::rtos::adjustable_clock::internal_check_timestamps(), os::rtos::port::clock_rtc::internal_interrupt_service_routine(), os::rtos::rtclock, os::rtos::scheduler::started(), and trace_putchar().

Referenced by os_systick_handler().

◆ os_sysclock_now()

os_clock_timestamp_t os_sysclock_now ( void  )
Parameters
None.
Returns
The number of SysTick input clocks since startup.
Note
Can be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::clock::now()

Definition at line 1410 of file os-c-wrapper.cpp.

1411{
1413}
virtual timestamp_t now(void)
Tell the current time, possibly adjusted for epoch.

References os::rtos::clock::now(), and os::rtos::sysclock.

◆ os_sysclock_sleep_for()

os_result_t os_sysclock_sleep_for ( os_clock_duration_t  duration)
Parameters
[in]durationThe number of clock units (ticks or seconds) to sleep.
Return values
ETIMEDOUTThe sleep lasted the entire duration.
EPERMCannot be invoked from an Interrupt Service Routines.
EINTRThe sleep was interrupted.
Warning
Cannot be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::clock::sleep_for()

Definition at line 1422 of file os-c-wrapper.cpp.

1423{
1424 return (os_result_t)sysclock.sleep_for (duration);
1425}
result_t sleep_for(duration_t duration)
Sleep for a relative duration.

References os::rtos::clock::sleep_for(), and os::rtos::sysclock.

◆ os_sysclock_sleep_until()

os_result_t os_sysclock_sleep_until ( os_clock_timestamp_t  timestamp)
Parameters
[in]timestampThe absolute moment in time, in clock units.
Return values
ETIMEDOUTThe sleep lasted the entire duration.
EPERMCannot be invoked from an Interrupt Service Routines.
EINTRThe sleep was interrupted.
Warning
Cannot be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::clock::sleep_until()

Definition at line 1434 of file os-c-wrapper.cpp.

1435{
1436 return (os_result_t)sysclock.sleep_until (timestamp);
1437}
virtual result_t sleep_until(timestamp_t timestamp)
Sleep until an absolute timestamp.

References os::rtos::clock::sleep_until(), and os::rtos::sysclock.

◆ os_sysclock_ticks_cast()

os_clock_duration_t os_sysclock_ticks_cast ( uint32_t  microsec)
inline
Parameters
[in]microsecThe number of microseconds.
Returns
The number of ticks.

Definition at line 969 of file os-c-api.h.

970 {
971#pragma GCC diagnostic push
972#if defined(__clang__)
973#elif defined(__GNUC__)
974#if defined(__cplusplus)
975#pragma GCC diagnostic ignored "-Wuseless-cast"
976#endif
977#endif
978 return (
979 os_clock_duration_t)((((microsec)
981 + (uint32_t)1000000ul - 1)
982 / (uint32_t)1000000ul);
983#pragma GCC diagnostic pop
984 }
#define OS_INTEGER_SYSTICK_FREQUENCY_HZ
Define the scheduler frequency, in Hz.
os_port_clock_duration_t os_clock_duration_t
Type of variables holding clock durations.
Definition os-c-decls.h:196

References OS_INTEGER_SYSTICK_FREQUENCY_HZ.

◆ os_sysclock_ticks_cast_long()

os_clock_duration_t os_sysclock_ticks_cast_long ( uint64_t  microsec)
inline
Parameters
[in]microsecThe number of microseconds.
Returns
The number of ticks.

Definition at line 992 of file os-c-api.h.

993 {
994#pragma GCC diagnostic push
995#if defined(__clang__)
996#elif defined(__GNUC__)
997#if defined(__cplusplus)
998#pragma GCC diagnostic ignored "-Wuseless-cast"
999#endif
1000#endif
1001 return (
1002 os_clock_duration_t)((((microsec)
1003 * ((uint64_t)OS_INTEGER_SYSTICK_FREQUENCY_HZ))
1004 + (uint64_t)1000000ul - 1)
1005 / (uint64_t)1000000ul);
1006#pragma GCC diagnostic pop
1007 }

References OS_INTEGER_SYSTICK_FREQUENCY_HZ.

◆ os_sysclock_wait_for()

os_result_t os_sysclock_wait_for ( os_clock_duration_t  timeout)
Parameters
[in]timeoutThe timeout in clock units.
Return values
os_okAn event occurred before the timeout.
ETIMEDOUTThe wait lasted the entire duration.
EPERMCannot be invoked from an Interrupt Service Routines.
EINTRThe sleep was interrupted.
Warning
Cannot be invoked from Interrupt Service Routines.
For the complete definition, see
os::rtos::clock::wait_for()

Definition at line 1446 of file os-c-wrapper.cpp.

1447{
1448 return (os_result_t)sysclock.wait_for (timeout);
1449}
result_t wait_for(duration_t timeout)
Timed wait for an event.

References os::rtos::sysclock, and os::rtos::clock::wait_for().

◆ os_systick_handler()

void os_systick_handler ( void  )

Must be called from the physical interrupt handler.

Definition at line 36 of file os-clocks.cpp.

37{
38 using namespace os::rtos;
39
40#if defined(OS_USE_RTOS_PORT_SCHEDULER)
41 // Prevent scheduler actions before starting it.
42 if (scheduler::started ())
43 {
45 }
46#endif
47
48#if defined(OS_TRACE_RTOS_SYSCLOCK_TICK)
49 trace::putchar ('.');
50#elif defined(OS_TRACE_RTOS_SYSCLOCK_TICK_BRACES)
51 trace::printf ("{t ");
52#endif
53
54 {
55 // ----- Enter critical section -------------------------------------------
57
58 sysclock.internal_increment_count ();
60 // ----- Exit critical section --------------------------------------------
61 }
62 sysclock.internal_check_timestamps ();
63 hrclock.internal_check_timestamps ();
64
65#if !defined(OS_INCLUDE_RTOS_REALTIME_CLOCK_DRIVER)
66
67 // Simulate an RTC driver.
68 static uint32_t ticks = clock_systick::frequency_hz;
69
70 if (--ticks == 0)
71 {
73
75 }
76
77#endif /* !defined(OS_INCLUDE_RTOS_REALTIME_CLOCK_DRIVER) */
78
79#if !defined(OS_USE_RTOS_PORT_SCHEDULER)
80
82
83#endif /* !defined(OS_USE_RTOS_PORT_SCHEDULER) */
84
85#if defined(OS_TRACE_RTOS_SYSCLOCK_TICK_BRACES)
86 trace::printf (" t}");
87#endif
88}
void internal_increment_count(void)
Definition os-clocks.h:905
static constexpr uint32_t frequency_hz
SysTick frequency in Hz.
Definition os-clocks.h:471
static void internal_interrupt_service_routine(void)
SysTick implementation hook.
int printf(const char *format,...)
Write a formatted string to the trace device.
Definition trace.cpp:59
int putchar(int c)
Write the single character to the trace device.
Definition trace.cpp:120
void os_rtc_handler(void)
RTC interrupt handler.
Definition os-clocks.cpp:95
RTOS namespace.
Definition os-flags.h:37

References os::rtos::clock_systick::frequency_hz, os::rtos::hrclock, os::rtos::clock_highres::internal_increment_count(), os::rtos::port::clock_systick::internal_interrupt_service_routine(), os_rtc_handler(), os::trace::printf(), os::trace::putchar(), os::rtos::port::scheduler::reschedule(), os::rtos::scheduler::started(), and os::rtos::sysclock.