ChibiOS/RT  6.1.4
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  * @brief Number of iterations in the calibration loop.
39  * @note This is required in order to assess the best result in
40  * architectures with instruction cache.
41  */
42 #define TM_CALIBRATION_LOOP 4U
43 
44 /*===========================================================================*/
45 /* Module exported variables. */
46 /*===========================================================================*/
47 
48 /*===========================================================================*/
49 /* Module local types. */
50 /*===========================================================================*/
51 
52 /*===========================================================================*/
53 /* Module local variables. */
54 /*===========================================================================*/
55 
56 /*===========================================================================*/
57 /* Module local functions. */
58 /*===========================================================================*/
59 
60 static inline void tm_stop(time_measurement_t *tmp,
61  rtcnt_t now,
62  rtcnt_t offset) {
63 
64  tmp->n++;
65  tmp->last = (now - tmp->last) - offset;
66  tmp->cumulative += (rttime_t)tmp->last;
67  if (tmp->last > tmp->worst) {
68  tmp->worst = tmp->last;
69  }
70  if (tmp->last < tmp->best) {
71  tmp->best = tmp->last;
72  }
73 }
74 
75 /*===========================================================================*/
76 /* Module exported functions. */
77 /*===========================================================================*/
78 
79 /**
80  * @brief Initializes the time measurement unit.
81  *
82  * @init
83  */
84 void _tm_init(void) {
86  unsigned i;
87 
88  /* Time Measurement subsystem calibration, it does a null measurement
89  and calculates the call overhead which is subtracted to real
90  measurements.*/
91  ch.tm.offset = (rtcnt_t)0;
92  chTMObjectInit(&tm);
94  do {
97  i--;
98  } while (i > 0U);
99  ch.tm.offset = tm.best;
100 }
101 
102 /**
103  * @brief Initializes a @p TimeMeasurement object.
104  *
105  * @param[out] tmp pointer to a @p TimeMeasurement structure
106  *
107  * @init
108  */
110 
111  tmp->best = (rtcnt_t)-1;
112  tmp->worst = (rtcnt_t)0;
113  tmp->last = (rtcnt_t)0;
114  tmp->n = (ucnt_t)0;
115  tmp->cumulative = (rttime_t)0;
116 }
117 
118 /**
119  * @brief Starts a measurement.
120  * @pre The @p time_measurement_t structure must be initialized.
121  *
122  * @param[in,out] tmp pointer to a @p TimeMeasurement structure
123  *
124  * @xclass
125  */
127 
129 }
130 
131 /**
132  * @brief Stops a measurement.
133  * @pre The @p time_measurement_t structure must be initialized.
134  *
135  * @param[in,out] tmp pointer to a @p time_measurement_t structure
136  *
137  * @xclass
138  */
140 
141  tm_stop(tmp, chSysGetRealtimeCounterX(), ch.tm.offset);
142 }
143 
144 /**
145  * @brief Stops a measurement and chains to the next one using the same time
146  * stamp.
147  *
148  * @param[in,out] tmp1 pointer to the @p time_measurement_t structure to be
149  * stopped
150  * @param[in,out] tmp2 pointer to the @p time_measurement_t structure to be
151  * started
152  *
153  *
154  * @xclass
155  */
157  time_measurement_t *tmp2) {
158 
159  /* Starts new measurement.*/
160  tmp2->last = chSysGetRealtimeCounterX();
161 
162  /* Stops previous measurement using the same time stamp.*/
163  tm_stop(tmp1, tmp2->last, (rtcnt_t)0);
164 }
165 
166 #endif /* CH_CFG_USE_TM == TRUE */
167 
168 /** @} */
time_measurement_t::cumulative
rttime_t cumulative
Cumulative measurement.
Definition: chtm.h:77
time_measurement_t::last
rtcnt_t last
Last measurement.
Definition: chtm.h:75
TM_CALIBRATION_LOOP
#define TM_CALIBRATION_LOOP
Number of iterations in the calibration loop.
Definition: chtm.c:42
chSysGetRealtimeCounterX
#define chSysGetRealtimeCounterX()
Returns the current value of the system real time counter.
Definition: chsys.h:255
tm_calibration_t::offset
rtcnt_t offset
Measurement calibration value.
Definition: chtm.h:60
time_measurement_t::worst
rtcnt_t worst
Worst measurement.
Definition: chtm.h:74
time_measurement_t::n
ucnt_t n
Number of measurements.
Definition: chtm.h:76
time_measurement_t
Type of a Time Measurement object.
Definition: chtm.h:72
_tm_init
void _tm_init(void)
Initializes the time measurement unit.
Definition: chtm.c:84
chTMObjectInit
void chTMObjectInit(time_measurement_t *tmp)
Initializes a TimeMeasurement object.
Definition: chtm.c:109
ch
ch_system_t ch
System data structures.
Definition: chschd.c:42
ch.h
ChibiOS/RT main include file.
chTMStopMeasurementX
NOINLINE void chTMStopMeasurementX(time_measurement_t *tmp)
Stops a measurement.
Definition: chtm.c:139
time_measurement_t::best
rtcnt_t best
Best measurement.
Definition: chtm.h:73
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:156
ch_system::tm
tm_calibration_t tm
Time measurement calibration data.
Definition: chschd.h:435
chTMStartMeasurementX
NOINLINE void chTMStartMeasurementX(time_measurement_t *tmp)
Starts a measurement.
Definition: chtm.c:126