source: rtems/cpukit/sapi/src/cpucounterconverter.c

Last change on this file was b9ffb624, checked in by Sebastian Huber <sebastian.huber@…>, on Nov 30, 2018 at 12:57:04 PM

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.

  • Property mode set to 100644
File size: 1.8 KB
Line 
1/*
2 * Copyright (c) 2014, 2018 embedded brains GmbH.  All rights reserved.
3 *
4 *  embedded brains GmbH
5 *  Dornierstr. 4
6 *  82178 Puchheim
7 *  Germany
8 *  <rtems@embedded-brains.de>
9 *
10 * The license and distribution terms for this file may be
11 * found in the file LICENSE in this distribution or at
12 * http://www.rtems.org/license/LICENSE.
13 */
14
15#include <rtems/counter.h>
16#include <rtems/sysinit.h>
17
18RTEMS_STATIC_ASSERT(sizeof(rtems_counter_ticks) <= sizeof(uint32_t), type);
19
20static uint64_t to_ns_scaler;
21
22static uint64_t from_ns_scaler;
23
24static uint64_t to_sbt_scaler;
25
26static uint64_t from_sbt_scaler;
27
28uint64_t rtems_counter_ticks_to_nanoseconds( rtems_counter_ticks ticks )
29{
30  return (uint32_t) ((ticks * to_ns_scaler) >> 32);
31}
32
33rtems_counter_ticks rtems_counter_nanoseconds_to_ticks( uint32_t nanoseconds )
34{
35  return (rtems_counter_ticks) ((nanoseconds * from_ns_scaler) >> 32);
36}
37
38int64_t rtems_counter_ticks_to_sbintime( rtems_counter_ticks ticks )
39{
40  return (int64_t) ((ticks * to_sbt_scaler) >> 31);
41}
42
43rtems_counter_ticks rtems_counter_sbintime_to_ticks( int64_t sbt )
44{
45  return (rtems_counter_ticks) (((uint64_t) sbt * from_sbt_scaler) >> 31);
46}
47
48void rtems_counter_initialize_converter( uint32_t frequency )
49{
50  uint64_t ns_per_s = UINT64_C(1000000000);
51  uint64_t bin_per_s = UINT64_C(1) << 32;
52
53  to_ns_scaler = ((ns_per_s << 32) + frequency - 1) / frequency;
54  from_ns_scaler = ((UINT64_C(1) << 32) * frequency + ns_per_s - 1) / ns_per_s;
55
56  to_sbt_scaler = ((bin_per_s << 31) + frequency - 1) / frequency;
57  from_sbt_scaler = ((UINT64_C(1) << 31) * frequency + bin_per_s - 1) / bin_per_s;
58}
59
60static void rtems_counter_sysinit( void )
61{
62  rtems_counter_initialize_converter( rtems_counter_frequency() );
63}
64
65RTEMS_SYSINIT_ITEM(
66  rtems_counter_sysinit,
67  RTEMS_SYSINIT_CPU_COUNTER,
68  RTEMS_SYSINIT_ORDER_LAST
69);
Note: See TracBrowser for help on using the repository browser.