ChibiOS/RT  5.1.0
chtm.c
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio.
3 
4  This file is part of ChibiOS.
5 
6  ChibiOS is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
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 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 the time measurement unit.
74  *
75  * @init
76  */
77 void _tm_init(void) {
79 
80  /* Time Measurement subsystem calibration, it does a null measurement
81  and calculates the call overhead which is subtracted to real
82  measurements.*/
83  ch.tm.offset = (rtcnt_t)0;
84  chTMObjectInit(&tm);
87  ch.tm.offset = tm.last;
88 }
89 
90 /**
91  * @brief Initializes a @p TimeMeasurement object.
92  *
93  * @param[out] tmp pointer to a @p TimeMeasurement structure
94  *
95  * @init
96  */
98 
99  tmp->best = (rtcnt_t)-1;
100  tmp->worst = (rtcnt_t)0;
101  tmp->last = (rtcnt_t)0;
102  tmp->n = (ucnt_t)0;
103  tmp->cumulative = (rttime_t)0;
104 }
105 
106 /**
107  * @brief Starts a measurement.
108  * @pre The @p time_measurement_t structure must be initialized.
109  *
110  * @param[in,out] tmp pointer to a @p TimeMeasurement structure
111  *
112  * @xclass
113  */
115 
117 }
118 
119 /**
120  * @brief Stops a measurement.
121  * @pre The @p time_measurement_t structure must be initialized.
122  *
123  * @param[in,out] tmp pointer to a @p time_measurement_t structure
124  *
125  * @xclass
126  */
128 
129  tm_stop(tmp, chSysGetRealtimeCounterX(), ch.tm.offset);
130 }
131 
132 /**
133  * @brief Stops a measurement and chains to the next one using the same time
134  * stamp.
135  *
136  * @param[in,out] tmp1 pointer to the @p time_measurement_t structure to be
137  * stopped
138  * @param[in,out] tmp2 pointer to the @p time_measurement_t structure to be
139  * started
140  *
141  *
142  * @xclass
143  */
145  time_measurement_t *tmp2) {
146 
147  /* Starts new measurement.*/
148  tmp2->last = chSysGetRealtimeCounterX();
149 
150  /* Stops previous measurement using the same time stamp.*/
151  tm_stop(tmp1, tmp2->last, (rtcnt_t)0);
152 }
153 
154 #endif /* CH_CFG_USE_TM == TRUE */
155 
156 /** @} */
rtcnt_t worst
Worst measurement.
Definition: chtm.h:74
#define chSysGetRealtimeCounterX()
Returns the current value of the system real time counter.
Definition: chsys.h:253
rtcnt_t best
Best measurement.
Definition: chtm.h:73
void _tm_init(void)
Initializes the time measurement unit.
Definition: chtm.c:77
Type of a Time Measurement object.
Definition: chtm.h:72
rtcnt_t offset
Measurement calibration value.
Definition: chtm.h:60
rtcnt_t last
Last measurement.
Definition: chtm.h:75
rttime_t cumulative
Cumulative measurement.
Definition: chtm.h:77
ch_system_t ch
System data structures.
Definition: chschd.c:42
NOINLINE void chTMStartMeasurementX(time_measurement_t *tmp)
Starts a measurement.
Definition: chtm.c:114
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:144
ucnt_t n
Number of measurements.
Definition: chtm.h:76
void chTMObjectInit(time_measurement_t *tmp)
Initializes a TimeMeasurement object.
Definition: chtm.c:97
ChibiOS/RT main include file.
tm_calibration_t tm
Time measurement calibration data.
Definition: chschd.h:432
NOINLINE void chTMStopMeasurementX(time_measurement_t *tmp)
Stops a measurement.
Definition: chtm.c:127