Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup RTEMSScoreTimecounter
0007  *
0008  * @brief This header file provides interfaces of the
0009  *   @ref RTEMSScoreTimecounter which are used by the implementation and the API.
0010  */
0011 
0012 /*
0013  * Copyright (C) 2015, 2021 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 #ifndef _RTEMS_SCORE_TIMECOUNTER_H
0038 #define _RTEMS_SCORE_TIMECOUNTER_H
0039 
0040 #include <sys/time.h>
0041 #include <sys/timetc.h>
0042 #include <machine/_timecounter.h>
0043 
0044 #include <rtems/score/isrlock.h>
0045 
0046 #ifdef __cplusplus
0047 extern "C" {
0048 #endif /* __cplusplus */
0049 
0050 /**
0051  * @defgroup RTEMSScoreTimecounter Timecounter Handler
0052  *
0053  * @ingroup RTEMSScore
0054  *
0055  * @brief This group contains the Timecounter Handler implementation.
0056  *
0057  * @{
0058  */
0059 
0060 /**
0061  * @brief Returns the wall clock time in the bintime format.
0062  *
0063  * @param[out] bt Returns the wall clock time.
0064  */
0065 void _Timecounter_Bintime( struct bintime *bt );
0066 
0067 /**
0068  * @brief Returns the wall clock time in the timespec format.
0069  *
0070  * @param[out] ts Returns the wall clock time.
0071  */
0072 void _Timecounter_Nanotime( struct timespec *ts );
0073 
0074 /**
0075  * @brief Returns the wall clock time in the timeval format.
0076  *
0077  * @param[out] tv Returns the wall clock time.
0078  */
0079 void _Timecounter_Microtime( struct timeval *tv );
0080 
0081 /**
0082  * @brief Returns the uptime in the bintime format.
0083  *
0084  * @param[out] bt Returns the uptime.
0085  */
0086 void _Timecounter_Binuptime( struct bintime *bt );
0087 
0088 /**
0089  * @brief Returns the uptime in the sbintime_t format.
0090  *
0091  * @return Returns the uptime.
0092  */
0093 int64_t _Timecounter_Sbinuptime( void );
0094 
0095 /**
0096  * @brief Returns the uptime in the timespec format.
0097  *
0098  * @param[out] ts Returns the uptime.
0099  */
0100 void _Timecounter_Nanouptime( struct timespec *ts );
0101 
0102 /**
0103  * @brief Returns the uptime in the timeval format.
0104  *
0105  * @param[out] tv Returns the uptime.
0106  */
0107 void _Timecounter_Microuptime( struct timeval *tv );
0108 
0109 /**
0110  * @brief Returns the wall clock time in the bintime format.
0111  *
0112  * This function obtains the time with a lower overhead and lower accuracy
0113  * compared to the _Timecounter_Bintime() variant.
0114  *
0115  * @param[out] ts Returns the wall clock time.
0116  */
0117 void _Timecounter_Getbintime( struct bintime *bt );
0118 
0119 /**
0120  * @brief Returns the wall clock time in the timespec format.
0121  *
0122  * This function obtains the time with a lower overhead and lower accuracy
0123  * compared to the _Timecounter_Nanotime() variant.
0124  *
0125  * @param[out] ts Returns the wall clock time.
0126  *
0127  * @see _Timecounter_Getbintime().
0128  */
0129 void _Timecounter_Getnanotime( struct timespec *ts );
0130 
0131 /**
0132  * @brief Returns the wall clock time in the timeval format.
0133  *
0134  * This function obtains the time with a lower overhead and lower accuracy
0135  * compared to the _Timecounter_Microtime() variant.
0136  *
0137  * @param[out] tv Returns the wall clock time.
0138  *
0139  * @see _Timecounter_Getbintime().
0140  */
0141 void _Timecounter_Getmicrotime( struct timeval *tv );
0142 
0143 /**
0144  * @brief Returns the uptime in the bintime format.
0145  *
0146  * This function obtains the time with a lower overhead and lower accuracy
0147  * compared to the _Timecounter_Binuptime() variant.
0148  *
0149  * @param[out] ts Returns the uptime.
0150  */
0151 void _Timecounter_Getbinuptime( struct bintime *bt );
0152 
0153 /**
0154  * @brief Returns the uptime in the timespec format.
0155  *
0156  * This function obtains the time with a lower overhead and lower accuracy
0157  * compared to the _Timecounter_Nanouptime() variant.
0158  *
0159  * @param[out] ts Returns the uptime.
0160  */
0161 void _Timecounter_Getnanouptime( struct timespec *ts );
0162 
0163 /**
0164  * @brief Returns the uptime in the timeval format.
0165  *
0166  * This function obtains the time with a lower overhead and lower accuracy
0167  * compared to the _Timecounter_Microuptime() variant.
0168  *
0169  * @param[out] tv Returns the uptime.
0170  */
0171 void _Timecounter_Getmicrouptime( struct timeval *tv );
0172 
0173 /**
0174  * @brief Returns the boot time in the timeval format.
0175  *
0176  * @param[out] tv Returns the boot time.
0177  */
0178 void _Timecounter_Getboottime( struct timeval *tv );
0179 
0180 /**
0181  * @brief Returns the boot time in the bintime format.
0182  *
0183  * @param[out] tv Returns the boot time.
0184  */
0185 void _Timecounter_Getboottimebin( struct bintime *bt );
0186 
0187 /**
0188  * @brief Installs the timecounter.
0189  *
0190  * The timecounter structure must contain valid values in the fields
0191  * tc_get_timecount, tc_counter_mask, tc_frequency and tc_quality.  All other
0192  * fields must be zero initialized.
0193  *
0194  * @param tc The timecounter.
0195  */
0196 void _Timecounter_Install( struct timecounter *tc );
0197 
0198 /**
0199  * @brief Performs a timecounter tick.
0200  */
0201 void _Timecounter_Tick( void );
0202 
0203 #if ISR_LOCK_NEEDS_OBJECT
0204 /**
0205  * @brief Lock to protect the timecounter mechanic.
0206  */
0207 extern ISR_lock_Control _Timecounter_Lock;
0208 #endif
0209 
0210 /**
0211  * @brief Acquires the timecounter lock.
0212  *
0213  * @param lock_context The lock context.
0214  *
0215  * See _Timecounter_Tick_simple().
0216  */
0217 #define _Timecounter_Acquire( lock_context ) \
0218   _ISR_lock_ISR_disable_and_acquire( &_Timecounter_Lock, lock_context )
0219 
0220 /**
0221  * @brief Releases the timecounter lock.
0222  *
0223  * @param lock_context The lock context.
0224  *
0225  * See _Timecounter_Tick_simple().
0226  */
0227 #define _Timecounter_Release(lock_context) \
0228    _ISR_lock_Release_and_ISR_enable(&_Timecounter_Lock, lock_context)
0229 
0230 /**
0231  * @brief Performs a simple timecounter tick.
0232  *
0233  * This is a special purpose tick function for simple timecounter to support
0234  * legacy clock drivers.
0235  *
0236  * @param delta The time in timecounter ticks elapsed since the last call
0237  * to _Timecounter_Tick_simple().
0238  * @param offset The current value of the timecounter.
0239  * @param] lock_context The lock context of the corresponding
0240  * _Timecounter_Acquire().
0241  */
0242 void _Timecounter_Tick_simple(
0243   uint32_t delta,
0244   uint32_t offset,
0245   ISR_lock_Context *lock_context
0246 );
0247 
0248 /**
0249  * @brief The wall clock time in seconds.
0250  */
0251 extern volatile time_t _Timecounter_Time_second;
0252 
0253 /**
0254  * @brief The uptime in seconds.
0255  *
0256  * For compatibility with the FreeBSD network stack the initial value is one
0257  * second.
0258  */
0259 extern volatile int32_t _Timecounter_Time_uptime;
0260 
0261 /**
0262  * @brief The current timecounter.
0263  */
0264 extern struct timecounter *_Timecounter;
0265 
0266 /**
0267  * @brief Handler doing the NTP update second processing shall have this type.
0268  *
0269  * @param[in, out] adjustment is the NTP time adjustment.
0270  *
0271  * @param[in, out] newsec is the number of seconds since Unix epoch.
0272  */
0273 typedef void ( *Timecounter_NTP_update_second )(
0274   int64_t *adjustment,
0275   time_t  *newsec
0276 );
0277 
0278 /**
0279  * @brief Sets the NTP update second handler.
0280  *
0281  * @param handler is the new NTP update second handler used to carry out the
0282  *   NTP update second processing.
0283  */
0284 void _Timecounter_Set_NTP_update_second(
0285   Timecounter_NTP_update_second handler
0286 );
0287 
0288 /**
0289  * @brief Updates the time adjustment and seconds according to the NTP state.
0290  *
0291  * @param[in, out] adjustment is the NTP time adjustment.
0292  *
0293  * @param[in, out] newsec is the number of seconds since Unix epoch.
0294  */
0295 void _Timecounter_NTP_update_second(int64_t *adjustment, time_t *newsec);
0296 
0297 /**
0298  * @brief Gets the frequency in Hz of the current timecounter at some time
0299  *   point during the call.
0300  *
0301  * @return Returns the frequency in Hz.
0302  */
0303 uint64_t _Timecounter_Get_frequency(void);
0304 
0305 /**
0306  * @brief Updates the timecounter frequency adjustment used by
0307  * _Timecounter_Set_NTP_update_second().
0308  *
0309  * This function is part of the time synchronization using a PPS
0310  * (Pulse Per Second) signal.
0311  *
0312  * When an event (a rising or falling edge of the PPS signal) occurs, the
0313  * functions pps_capture() and pps_event() are executed. Only if the kernel
0314  * consumer is configured to be PPS_KC_HARDPPS, the timecounter is disciplined.
0315  *
0316  * @param[in] tsp is the time at PPS event
0317  *
0318  * @param[i] nsec is the time in nanoseconds from the last PPS event
0319  */
0320 void _Timecounter_Discipline(struct timespec *tsp, long nsec);
0321 
0322 /** @} */
0323 
0324 #ifdef __cplusplus
0325 }
0326 #endif /* __cplusplus */
0327 
0328 #endif /* _RTEMS_SCORE_TIMECOUNTER_H */