diff options
author | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2018-11-30 13:57:04 +0100 |
---|---|---|
committer | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2018-12-03 09:45:37 +0100 |
commit | b9ffb62467daaf4b8f99c05a8c7c84c2dcd3e37f (patch) | |
tree | 05e10da0295c2916a1358079c427eb95446a7916 /cpukit | |
parent | 5 spelling errors for a Google Code-In task. (diff) | |
download | rtems-b9ffb62467daaf4b8f99c05a8c7c84c2dcd3e37f.tar.bz2 |
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.
Diffstat (limited to 'cpukit')
-rw-r--r-- | cpukit/include/rtems/counter.h | 36 | ||||
-rw-r--r-- | cpukit/sapi/src/cpucounterconverter.c | 22 |
2 files changed, 48 insertions, 10 deletions
diff --git a/cpukit/include/rtems/counter.h b/cpukit/include/rtems/counter.h index e770c96eb0..f43b7f110f 100644 --- a/cpukit/include/rtems/counter.h +++ b/cpukit/include/rtems/counter.h @@ -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,16 +116,36 @@ 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. * * This function must be used to initialize the diff --git a/cpukit/sapi/src/cpucounterconverter.c b/cpukit/sapi/src/cpucounterconverter.c index 12d55362df..7387fe4410 100644 --- a/cpukit/sapi/src/cpucounterconverter.c +++ b/cpukit/sapi/src/cpucounterconverter.c @@ -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 ) |