ChibiOS  21.6.0
chtm.c
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006,2007,2008,2009,2010,2011,2012,2013,2014,
3  2015,2016,2017,2018,2019,2020,2021 Giovanni Di Sirio.
4 
5  This file is part of ChibiOS.
6 
7  ChibiOS is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation version 3 of the License.
10 
11  ChibiOS is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 /**
21  * @file rt/src/chtm.c
22  * @brief Time Measurement module code.
23  *
24  * @addtogroup time_measurement
25  * @details Time Measurement APIs and services.
26  * @{
27  */
28 
29 #include "ch.h"
30 
31 #if (CH_CFG_USE_TM == TRUE) || defined(__DOXYGEN__)
32 
33 /*===========================================================================*/
34 /* Module local definitions. */
35 /*===========================================================================*/
36 
37 /*===========================================================================*/
38 /* Module exported variables. */
39 /*===========================================================================*/
40 
41 /*===========================================================================*/
42 /* Module local types. */
43 /*===========================================================================*/
44 
45 /*===========================================================================*/
46 /* Module local variables. */
47 /*===========================================================================*/
48 
49 /*===========================================================================*/
50 /* Module local functions. */
51 /*===========================================================================*/
52 
53 static inline void tm_stop(time_measurement_t *tmp,
54  rtcnt_t now,
55  rtcnt_t offset) {
56 
57  tmp->n++;
58  tmp->last = (now - tmp->last) - offset;
59  tmp->cumulative += (rttime_t)tmp->last;
60  if (tmp->last > tmp->worst) {
61  tmp->worst = tmp->last;
62  }
63  if (tmp->last < tmp->best) {
64  tmp->best = tmp->last;
65  }
66 }
67 
68 /*===========================================================================*/
69 /* Module exported functions. */
70 /*===========================================================================*/
71 
72 /**
73  * @brief Initializes a @p TimeMeasurement object.
74  *
75  * @param[out] tmp pointer to a @p TimeMeasurement structure
76  *
77  * @init
78  */
80 
81  tmp->best = (rtcnt_t)-1;
82  tmp->worst = (rtcnt_t)0;
83  tmp->last = (rtcnt_t)0;
84  tmp->n = (ucnt_t)0;
85  tmp->cumulative = (rttime_t)0;
86 }
87 
88 /**
89  * @brief Starts a measurement.
90  * @pre The @p time_measurement_t structure must be initialized.
91  *
92  * @param[in,out] tmp pointer to a @p TimeMeasurement structure
93  *
94  * @xclass
95  */
97 
99 }
100 
101 /**
102  * @brief Stops a measurement.
103  * @pre The @p time_measurement_t structure must be initialized.
104  *
105  * @param[in,out] tmp pointer to a @p time_measurement_t structure
106  *
107  * @xclass
108  */
110 
111  tm_stop(tmp, chSysGetRealtimeCounterX(), ch_system.tmc.offset);
112 }
113 
114 /**
115  * @brief Stops a measurement and chains to the next one using the same time
116  * stamp.
117  *
118  * @param[in,out] tmp1 pointer to the @p time_measurement_t structure to be
119  * stopped
120  * @param[in,out] tmp2 pointer to the @p time_measurement_t structure to be
121  * started
122  *
123  *
124  * @xclass
125  */
127  time_measurement_t *tmp2) {
128 
129  /* Starts new measurement.*/
130  tmp2->last = chSysGetRealtimeCounterX();
131 
132  /* Stops previous measurement using the same time stamp.*/
133  tm_stop(tmp1, tmp2->last, (rtcnt_t)0);
134 }
135 
136 #endif /* CH_CFG_USE_TM == TRUE */
137 
138 /** @} */
time_measurement_t::cumulative
rttime_t cumulative
Cumulative measurement.
Definition: chtm.h:84
rttime_t
port_rttime_t rttime_t
Definition: chearly.h:78
time_measurement_t::last
rtcnt_t last
Last measurement.
Definition: chtm.h:82
NOINLINE
#define NOINLINE
Makes functions not inlineable.
Definition: chtypes.h:91
chSysGetRealtimeCounterX
#define chSysGetRealtimeCounterX()
Returns the current value of the system real time counter.
Definition: chsys.h:292
ch_system::tmc
tm_calibration_t tmc
Time measurement calibration data.
Definition: chobjects.h:470
ucnt_t
uint32_t ucnt_t
Definition: chearly.h:93
tm_calibration_t::offset
rtcnt_t offset
Measurement calibration value.
Definition: chtm.h:67
time_measurement_t::worst
rtcnt_t worst
Worst measurement.
Definition: chtm.h:81
ch_system
Type of system data structure.
Definition: chobjects.h:457
time_measurement_t::n
ucnt_t n
Number of measurements.
Definition: chtm.h:83
time_measurement_t
Type of a Time Measurement object.
Definition: chtm.h:79
rtcnt_t
port_rtcnt_t rtcnt_t
Definition: chearly.h:77
chTMObjectInit
void chTMObjectInit(time_measurement_t *tmp)
Initializes a TimeMeasurement object.
Definition: chtm.c:79
chTMStopMeasurementX
NOINLINE void chTMStopMeasurementX(time_measurement_t *tmp)
Stops a measurement.
Definition: chtm.c:109
time_measurement_t::best
rtcnt_t best
Best measurement.
Definition: chtm.h:80
chTMChainMeasurementToX
NOINLINE void chTMChainMeasurementToX(time_measurement_t *tmp1, time_measurement_t *tmp2)
Stops a measurement and chains to the next one using the same time stamp.
Definition: chtm.c:126
chTMStartMeasurementX
NOINLINE void chTMStartMeasurementX(time_measurement_t *tmp)
Starts a measurement.
Definition: chtm.c:96