mirror of
https://gitlab.rtems.org/rtems/rtos/rtems.git
synced 2025-12-05 23:23:13 +00:00
Convert CPU counter ticks to/from sbintime_t
The sbintime_t is an efficient time format. Add the ability to convert CPU counter ticks to/from sbintime_t.
This commit is contained in:
@@ -72,9 +72,9 @@ static inline uint32_t rtems_counter_frequency( void )
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Reads the current counter values.
|
||||
* @brief Reads the current counter value.
|
||||
*
|
||||
* @return The current counter values.
|
||||
* @return The current counter value.
|
||||
*/
|
||||
static inline rtems_counter_ticks rtems_counter_read( void )
|
||||
{
|
||||
@@ -104,10 +104,10 @@ static inline rtems_counter_ticks rtems_counter_difference(
|
||||
/**
|
||||
* @brief Converts counter ticks into nanoseconds.
|
||||
*
|
||||
* @param[in] ticks Some counter ticks.
|
||||
* @param[in] ticks The counter ticks value to convert.
|
||||
*
|
||||
* @return The nanoseconds corresponding to the counter ticks. The value is
|
||||
* rounded up.
|
||||
* @return The nanoseconds value corresponding to the counter ticks. The value
|
||||
* is rounded up.
|
||||
*/
|
||||
uint64_t rtems_counter_ticks_to_nanoseconds(
|
||||
rtems_counter_ticks ticks
|
||||
@@ -116,15 +116,35 @@ uint64_t rtems_counter_ticks_to_nanoseconds(
|
||||
/**
|
||||
* @brief Converts nanoseconds into counter ticks.
|
||||
*
|
||||
* @param[in] nanoseconds Some nanoseconds.
|
||||
* @param[in] nanoseconds The nanoseconds value to convert.
|
||||
*
|
||||
* @return The counter ticks corresponding to the nanoseconds. The value is
|
||||
* rounded up.
|
||||
* @return The counter ticks value corresponding to the nanoseconds value. The
|
||||
* value is rounded up.
|
||||
*/
|
||||
rtems_counter_ticks rtems_counter_nanoseconds_to_ticks(
|
||||
uint32_t nanoseconds
|
||||
);
|
||||
|
||||
/**
|
||||
* @brief Converts counter ticks into signed binary time (sbintime_t).
|
||||
*
|
||||
* @param[in] ticks The counter ticks value to convert.
|
||||
*
|
||||
* @return The signed binary time value corresponding to the counter ticks
|
||||
* value. The value is rounded up.
|
||||
*/
|
||||
int64_t rtems_counter_ticks_to_sbintime( rtems_counter_ticks ticks );
|
||||
|
||||
/**
|
||||
* @brief Converts signed binary time (sbintime_t) into counter ticks.
|
||||
*
|
||||
* @param[in] sbt The signed binary time value to convert.
|
||||
*
|
||||
* @return The counter ticks value corresponding to the nanoseconds value. The
|
||||
* value is rounded up.
|
||||
*/
|
||||
rtems_counter_ticks rtems_counter_sbintime_to_ticks( int64_t sbt );
|
||||
|
||||
/**
|
||||
* @brief Initializes the counter ticks to/from nanoseconds converter functions.
|
||||
*
|
||||
|
||||
@@ -21,9 +21,13 @@ static uint64_t to_ns_scaler;
|
||||
|
||||
static uint64_t from_ns_scaler;
|
||||
|
||||
uint64_t rtems_counter_ticks_to_nanoseconds( rtems_counter_ticks counter )
|
||||
static uint64_t to_sbt_scaler;
|
||||
|
||||
static uint64_t from_sbt_scaler;
|
||||
|
||||
uint64_t rtems_counter_ticks_to_nanoseconds( rtems_counter_ticks ticks )
|
||||
{
|
||||
return (uint32_t) ((counter * to_ns_scaler) >> 32);
|
||||
return (uint32_t) ((ticks * to_ns_scaler) >> 32);
|
||||
}
|
||||
|
||||
rtems_counter_ticks rtems_counter_nanoseconds_to_ticks( uint32_t nanoseconds )
|
||||
@@ -31,12 +35,26 @@ rtems_counter_ticks rtems_counter_nanoseconds_to_ticks( uint32_t nanoseconds )
|
||||
return (rtems_counter_ticks) ((nanoseconds * from_ns_scaler) >> 32);
|
||||
}
|
||||
|
||||
int64_t rtems_counter_ticks_to_sbintime( rtems_counter_ticks ticks )
|
||||
{
|
||||
return (int64_t) ((ticks * to_sbt_scaler) >> 31);
|
||||
}
|
||||
|
||||
rtems_counter_ticks rtems_counter_sbintime_to_ticks( int64_t sbt )
|
||||
{
|
||||
return (rtems_counter_ticks) (((uint64_t) sbt * from_sbt_scaler) >> 31);
|
||||
}
|
||||
|
||||
void rtems_counter_initialize_converter( uint32_t frequency )
|
||||
{
|
||||
uint64_t ns_per_s = UINT64_C(1000000000);
|
||||
uint64_t bin_per_s = UINT64_C(1) << 32;
|
||||
|
||||
to_ns_scaler = ((ns_per_s << 32) + frequency - 1) / frequency;
|
||||
from_ns_scaler = ((UINT64_C(1) << 32) * frequency + ns_per_s - 1) / ns_per_s;
|
||||
|
||||
to_sbt_scaler = ((bin_per_s << 31) + frequency - 1) / frequency;
|
||||
from_sbt_scaler = ((UINT64_C(1) << 31) * frequency + bin_per_s - 1) / bin_per_s;
|
||||
}
|
||||
|
||||
static void rtems_counter_sysinit( void )
|
||||
|
||||
@@ -53,13 +53,24 @@ static rtems_interval sync_with_clock_tick(void)
|
||||
|
||||
static void test_converter(void)
|
||||
{
|
||||
CPU_Counter_ticks frequency = rtems_counter_nanoseconds_to_ticks(1000000000);
|
||||
uint64_t ns = rtems_counter_ticks_to_nanoseconds(frequency);
|
||||
CPU_Counter_ticks frequency;
|
||||
CPU_Counter_ticks frequency2;
|
||||
uint64_t ns;
|
||||
int64_t sbt;
|
||||
|
||||
frequency = rtems_counter_nanoseconds_to_ticks(1000000000);
|
||||
ns = rtems_counter_ticks_to_nanoseconds(frequency);
|
||||
|
||||
printf("CPU counter frequency: %" PRIu32 "Hz\n", frequency);
|
||||
printf("nanoseconds for frequency count ticks: %" PRIu64 "\n", ns);
|
||||
|
||||
rtems_test_assert(ns == 1000000000);
|
||||
|
||||
sbt = rtems_counter_ticks_to_sbintime(frequency);
|
||||
rtems_test_assert(sbt == (INT64_C(1) << 32));
|
||||
|
||||
frequency2 = rtems_counter_sbintime_to_ticks(sbt);
|
||||
rtems_test_assert(frequency == frequency2);
|
||||
}
|
||||
|
||||
static void test_delay_nanoseconds(test_context *ctx)
|
||||
|
||||
Reference in New Issue
Block a user