Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:22

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup RTEMSImpl
0007  *
0008  * @brief This source file contains a implementation of the counter value
0009  *   conversion functions.
0010  */
0011 
0012 /*
0013  * Copyright (C) 2014, 2018 embedded brains GmbH & Co. KG
0014  *
0015  * Redistribution and use in source and binary forms, with or without
0016  * modification, are permitted provided that the following conditions
0017  * are met:
0018  * 1. Redistributions of source code must retain the above copyright
0019  *    notice, this list of conditions and the following disclaimer.
0020  * 2. Redistributions in binary form must reproduce the above copyright
0021  *    notice, this list of conditions and the following disclaimer in the
0022  *    documentation and/or other materials provided with the distribution.
0023  *
0024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0027  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0028  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0029  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0030  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0031  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0032  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0033  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0034  * POSSIBILITY OF SUCH DAMAGE.
0035  */
0036 
0037 #include <rtems/counter.h>
0038 #include <rtems/sysinit.h>
0039 
0040 RTEMS_STATIC_ASSERT(sizeof(rtems_counter_ticks) <= sizeof(uint32_t), type);
0041 
0042 static uint64_t to_ns_scaler;
0043 
0044 static uint64_t from_ns_scaler;
0045 
0046 static uint64_t to_sbt_scaler;
0047 
0048 static uint64_t from_sbt_scaler;
0049 
0050 uint64_t rtems_counter_ticks_to_nanoseconds( rtems_counter_ticks ticks )
0051 {
0052   return (uint32_t) ((ticks * to_ns_scaler) >> 32);
0053 }
0054 
0055 rtems_counter_ticks rtems_counter_nanoseconds_to_ticks( uint32_t nanoseconds )
0056 {
0057   return (rtems_counter_ticks) ((nanoseconds * from_ns_scaler) >> 32);
0058 }
0059 
0060 int64_t rtems_counter_ticks_to_sbintime( rtems_counter_ticks ticks )
0061 {
0062   return (int64_t) ((ticks * to_sbt_scaler) >> 31);
0063 }
0064 
0065 rtems_counter_ticks rtems_counter_sbintime_to_ticks( int64_t sbt )
0066 {
0067   return (rtems_counter_ticks) (((uint64_t) sbt * from_sbt_scaler) >> 32);
0068 }
0069 
0070 void rtems_counter_initialize_converter( uint32_t frequency )
0071 {
0072   uint64_t ns_per_s = UINT64_C(1000000000);
0073   uint64_t bin_per_s = UINT64_C(1) << 32;
0074   uint64_t bin_freq = (uint64_t) frequency << 32;
0075 
0076   to_ns_scaler = ((ns_per_s << 32) + frequency - 1) / frequency;
0077   from_ns_scaler = (bin_freq + ns_per_s - 1) / ns_per_s;
0078 
0079   to_sbt_scaler = ((bin_per_s << 31) + frequency - 1) / frequency;
0080   from_sbt_scaler = (bin_freq + bin_per_s - 1) / bin_per_s;
0081 }
0082 
0083 static void rtems_counter_sysinit( void )
0084 {
0085   rtems_counter_initialize_converter( rtems_counter_frequency() );
0086 }
0087 
0088 RTEMS_SYSINIT_ITEM(
0089   rtems_counter_sysinit,
0090   RTEMS_SYSINIT_CPU_COUNTER,
0091   RTEMS_SYSINIT_ORDER_LAST_BUT_5
0092 );