KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
Timer Unit

SH4 CPU peripheral providing timers and counters More...

Topics

 Channels
 TMU channel constants
 
 Direct-Access
 Low-level timer driver
 
 Primary Timer
 Primary timer used by the kernel.
 
 Uptime
 Maintaining time since system boot.
 

Files

file  timer.h
 Timer functionality.
 
file  timer.h
 Low-level timer functionality.
 

Functions

static timespec_t timer_gettime (void)
 Get the current uptime of the system.
 
static uint64_t timer_ms_gettime64 (void)
 Get the current uptime of the system (in milliseconds).
 
static uint64_t timer_us_gettime64 (void)
 Get the current uptime of the system (in microseconds).
 
static uint64_t timer_ns_gettime64 (void)
 Get the current uptime of the system (in nanoseconds).
 
static void timer_ms_gettime (uint32_t *secs, uint32_t *msecs)
 Get the current uptime of the system (in secs and millisecs).
 
static void timer_us_gettime (uint32_t *secs, uint32_t *usecs)
 Get the current uptime of the system (in secs and microsecs).
 
static void timer_ns_gettime (uint32_t *secs, uint32_t *nsecs)
 Get the current uptime of the system (in secs and nanosecs).
 
static void timer_spin_delay_us (unsigned short us)
 Spin-loop delay function with microsecond granularity.
 
static void timer_spin_delay_ns (unsigned short ns)
 Spin-loop delay function with nanosecond granularity.
 
Do not use timer_spin_sleep ()
 Spin-loop delay function with millisecond granularity.
 

Detailed Description

SH4 CPU peripheral providing timers and counters

The Dreamcast's SH4 includes an on-chip Timer Unit (TMU) containing 3 independent 32-bit channels (TMU0-TMU2). Each channel provides a down-counter with automatic reload and can be configured to use 1 of 7 divider circuits for its input clock. By default, KOS uses the fastest input clock for each TMU channel, providing a maximum internal resolution of 80ns ticks.

Warning
Under normal circumstances, all 3 TMU channels are reserved by KOS for various OS-related purposes. If you need a free general-purpose interval timer, consider using the Watchdog Timer.
Note
90% of the time, you will never have a need to directly interact with this API, as it's mostly used as a kernel-level driver which backs other APIs. For example, querying for ticks, fetching the current timestamp, or putting a thread to sleep is typically done via the standard C, C++, or POSIX APIs.

Function Documentation

◆ timer_gettime()

static timespec_t timer_gettime ( void )
inlinestatic

Get the current uptime of the system.

This function retrieves the number of seconds and nanoseconds since KOS was started.

Returns
The current uptime of the system as a timespec struct.

References arch_timer_gettime().

Referenced by timer_ms_gettime(), timer_ms_gettime64(), timer_ns_gettime(), timer_ns_gettime64(), timer_us_gettime(), and timer_us_gettime64().

◆ timer_ms_gettime()

static void timer_ms_gettime ( uint32_t * secs,
uint32_t * msecs )
inlinestatic

Get the current uptime of the system (in secs and millisecs).

This function retrieves the number of seconds and milliseconds since KOS was started.

Parameters
secsA pointer to store the number of seconds since boot into.
msecsA pointer to store the number of milliseconds past a second since boot.
Note
To get the total number of milliseconds since boot, calculate (*secs * 1000) + *msecs, or use the timer_ms_gettime64() function.

References timer_gettime(), and uint32_t().

◆ timer_ms_gettime64()

static uint64_t timer_ms_gettime64 ( void )
inlinestatic

Get the current uptime of the system (in milliseconds).

This function retrieves the number of milliseconds since KOS was started. It is equivalent to calling timer_ms_gettime() and combining the number of seconds and milliseconds into one 64-bit value.

Returns
The number of milliseconds since KOS started.

References timer_gettime().

Referenced by thd_sleep().

◆ timer_ns_gettime()

static void timer_ns_gettime ( uint32_t * secs,
uint32_t * nsecs )
inlinestatic

Get the current uptime of the system (in secs and nanosecs).

This function retrieves the number of seconds and nanoseconds since KOS was started.

Note
To get the total number of nanoseconds since boot, calculate (*secs * 1000000000) + *nsecs, or use the timer_ns_gettime64() function.
Parameters
secsA pointer to store the number of seconds since boot into.
nsecsA pointer to store the number of nanoseconds past a second since boot.

References timer_gettime(), and uint32_t().

◆ timer_ns_gettime64()

static uint64_t timer_ns_gettime64 ( void )
inlinestatic

Get the current uptime of the system (in nanoseconds).

This function retrieves the number of nanoseconds since KOS was started.

Returns
The uptime in nanoseconds.

References timer_gettime().

Referenced by timer_spin_delay_ns().

◆ timer_spin_delay_ns()

static void timer_spin_delay_ns ( unsigned short ns)
inlinestatic

Spin-loop delay function with nanosecond granularity.

This function is meant as a very accurate delay function, even if threading and interrupts are disabled. It is a delay and not a sleep, which means that the CPU will be busy-looping during that time frame.

Note that the parameter is 16-bit, which means that the maximum acceptable value is 65535 nanoseconds.

Parameters
nsThe number of nanoseconds to wait for.
See also
timer_spin_delay_us, thd_sleep

References timer_ns_gettime64().

◆ timer_spin_delay_us()

static void timer_spin_delay_us ( unsigned short us)
inlinestatic

Spin-loop delay function with microsecond granularity.

This function is meant as a very accurate delay function, even if threading and interrupts are disabled. It is a delay and not a sleep, which means that the CPU will be busy-looping during that time frame. For any time frame bigger than a few hundred microseconds, it is recommended to sleep instead.

Note that the parameter is 16-bit, which means that the maximum acceptable value is 65535 microseconds.

Parameters
usThe number of microseconds to wait for.
See also
timer_spin_delay_ns, thd_sleep

References timer_us_gettime64().

◆ timer_spin_sleep()

Do not use timer_spin_sleep ( )

Spin-loop delay function with millisecond granularity.

This function should never be used, and is only used for compatibility with older code. It makes no sense to busy-wait for that long.

Parameters
msThe number of microseconds to wait for.
See also
thd_sleep

◆ timer_us_gettime()

static void timer_us_gettime ( uint32_t * secs,
uint32_t * usecs )
inlinestatic

Get the current uptime of the system (in secs and microsecs).

This function retrieves the number of seconds and microseconds since KOS was started.

Note
To get the total number of microseconds since boot, calculate (*secs * 1000000) + *usecs, or use the timer_us_gettime64() function.
Parameters
secsA pointer to store the number of seconds since boot into.
usecsA pointer to store the number of microseconds past a second since boot.

References timer_gettime(), and uint32_t().

◆ timer_us_gettime64()

static uint64_t timer_us_gettime64 ( void )
inlinestatic

Get the current uptime of the system (in microseconds).

This function retrieves the number of microseconds since KOS was started.

Returns
The uptime in microseconds.

References timer_gettime().

Referenced by timer_spin_delay_us().