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