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

Last change on this file was 58840ffb, checked in by Sebastian Huber <sebastian.huber@…>, on 07/25/23 at 06:48:55

rtems: Add files to Doxygen groups

Provide basic Doxygen comments.

Update #3706.
Update #3707.

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