[rtems commit] Convert CPU counter ticks to/from sbintime_t

Sebastian Huber sebh at rtems.org
Mon Dec 3 08:45:54 UTC 2018


Module:    rtems
Branch:    master
Commit:    b9ffb62467daaf4b8f99c05a8c7c84c2dcd3e37f
Changeset: http://git.rtems.org/rtems/commit/?id=b9ffb62467daaf4b8f99c05a8c7c84c2dcd3e37f

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Fri Nov 30 13:57:04 2018 +0100

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.

---

 cpukit/include/rtems/counter.h           | 36 +++++++++++++++++++++++++-------
 cpukit/sapi/src/cpucounterconverter.c    | 22 +++++++++++++++++--
 testsuites/sptests/spcpucounter01/init.c | 15 +++++++++++--
 3 files changed, 61 insertions(+), 12 deletions(-)

diff --git a/cpukit/include/rtems/counter.h b/cpukit/include/rtems/counter.h
index e770c96..f43b7f1 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 12d5536..7387fe4 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 )
diff --git a/testsuites/sptests/spcpucounter01/init.c b/testsuites/sptests/spcpucounter01/init.c
index 7b2684a..fefa742 100644
--- a/testsuites/sptests/spcpucounter01/init.c
+++ b/testsuites/sptests/spcpucounter01/init.c
@@ -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)



More information about the vc mailing list