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

Last change on this file since bd2e898 was bd2e898, checked in by Joel Sherrill <joel@…>, on 02/16/22 at 22:48:55

sapi/src/*.c: Change license to BSD-2

Updates #3053.

  • Property mode set to 100644
File size: 3.0 KB
Line 
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/*
4 * Copyright (c) 2014, 2018 embedded brains GmbH.  All rights reserved.
5 *
6 *  embedded brains GmbH
7 *  Dornierstr. 4
8 *  82178 Puchheim
9 *  Germany
10 *  <rtems@embedded-brains.de>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include <rtems/counter.h>
35#include <rtems/sysinit.h>
36
37RTEMS_STATIC_ASSERT(sizeof(rtems_counter_ticks) <= sizeof(uint32_t), type);
38
39static uint64_t to_ns_scaler;
40
41static uint64_t from_ns_scaler;
42
43static uint64_t to_sbt_scaler;
44
45static uint64_t from_sbt_scaler;
46
47uint64_t rtems_counter_ticks_to_nanoseconds( rtems_counter_ticks ticks )
48{
49  return (uint32_t) ((ticks * to_ns_scaler) >> 32);
50}
51
52rtems_counter_ticks rtems_counter_nanoseconds_to_ticks( uint32_t nanoseconds )
53{
54  return (rtems_counter_ticks) ((nanoseconds * from_ns_scaler) >> 32);
55}
56
57int64_t rtems_counter_ticks_to_sbintime( rtems_counter_ticks ticks )
58{
59  return (int64_t) ((ticks * to_sbt_scaler) >> 31);
60}
61
62rtems_counter_ticks rtems_counter_sbintime_to_ticks( int64_t sbt )
63{
64  return (rtems_counter_ticks) (((uint64_t) sbt * from_sbt_scaler) >> 32);
65}
66
67void rtems_counter_initialize_converter( uint32_t frequency )
68{
69  uint64_t ns_per_s = UINT64_C(1000000000);
70  uint64_t bin_per_s = UINT64_C(1) << 32;
71  uint64_t bin_freq = (uint64_t) frequency << 32;
72
73  to_ns_scaler = ((ns_per_s << 32) + frequency - 1) / frequency;
74  from_ns_scaler = (bin_freq + ns_per_s - 1) / ns_per_s;
75
76  to_sbt_scaler = ((bin_per_s << 31) + frequency - 1) / frequency;
77  from_sbt_scaler = (bin_freq + bin_per_s - 1) / bin_per_s;
78}
79
80static void rtems_counter_sysinit( void )
81{
82  rtems_counter_initialize_converter( rtems_counter_frequency() );
83}
84
85RTEMS_SYSINIT_ITEM(
86  rtems_counter_sysinit,
87  RTEMS_SYSINIT_CPU_COUNTER,
88  RTEMS_SYSINIT_ORDER_LAST_BUT_5
89);
Note: See TracBrowser for help on using the repository browser.