ChibiOS/RT 7.0.6
chtm.h
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/include/chtm.h
21 * @brief Time Measurement module macros and structures.
22 *
23 * @addtogroup time_measurement
24 * @{
25 */
26
27#ifndef CHTM_H
28#define CHTM_H
29
30#if (CH_CFG_USE_TM == TRUE) || defined(__DOXYGEN__)
31
32/*===========================================================================*/
33/* Module constants. */
34/*===========================================================================*/
35
36/**
37 * @brief Number of iterations in the calibration loop.
38 * @note This is required in order to assess the best result in
39 * architectures with instruction cache.
40 */
41#define TM_CALIBRATION_LOOP 4U
42
43/*===========================================================================*/
44/* Module pre-compile time settings. */
45/*===========================================================================*/
46
47/*===========================================================================*/
48/* Derived constants and error checks. */
49/*===========================================================================*/
50
51#if PORT_SUPPORTS_RT == FALSE
52#error "CH_CFG_USE_TM requires PORT_SUPPORTS_RT"
53#endif
54
55/*===========================================================================*/
56/* Module data structures and types. */
57/*===========================================================================*/
58
59/**
60 * @brief Type of a time measurement calibration data.
61 */
62typedef struct {
63 /**
64 * @brief Measurement calibration value.
65 */
68
69/**
70 * @brief Type of a Time Measurement object.
71 * @note The maximum measurable time period depends on the implementation
72 * of the realtime counter and its clock frequency.
73 * @note The measurement is not 100% cycle-accurate, it can be in excess
74 * of few cycles depending on the compiler and target architecture.
75 * @note Interrupts can affect measurement if the measurement is performed
76 * with interrupts enabled.
77 */
78typedef struct {
79 rtcnt_t best; /**< @brief Best measurement. */
80 rtcnt_t worst; /**< @brief Worst measurement. */
81 rtcnt_t last; /**< @brief Last measurement. */
82 ucnt_t n; /**< @brief Number of measurements. */
83 rttime_t cumulative; /**< @brief Cumulative measurement. */
85
86/*===========================================================================*/
87/* Module macros. */
88/*===========================================================================*/
89
90/*===========================================================================*/
91/* External declarations. */
92/*===========================================================================*/
93
94#ifdef __cplusplus
95extern "C" {
96#endif
98 NOINLINE void chTMStartMeasurementX(time_measurement_t *tmp);
99 NOINLINE void chTMStopMeasurementX(time_measurement_t *tmp);
101 time_measurement_t *tmp2);
102#ifdef __cplusplus
103}
104#endif
105
106/*===========================================================================*/
107/* Module inline functions. */
108/*===========================================================================*/
109
110/**
111 * @brief Time measurement initialization.
112 * @note Internal use only.
113 *
114 * @param[out] tcp pointer to the @p tm_calibration_t structure
115 *
116 * @notapi
117 */
119 unsigned i;
121
122 /* Time Measurement subsystem calibration, it does a null measurement
123 and calculates the call overhead which is subtracted to real
124 measurements.*/
125 tcp->offset = (rtcnt_t)0;
126 chTMObjectInit(&tm);
128 do {
131 i--;
132 } while (i > 0U);
133 tcp->offset = tm.best;
134}
135
136#endif /* CH_CFG_USE_TM == TRUE */
137
138#endif /* CHTM_H */
139
140/** @} */
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
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_calibration_object_init(tm_calibration_t *tcp)
Time measurement initialization.
Definition chtm.h:118
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
#define TM_CALIBRATION_LOOP
Number of iterations in the calibration loop.
Definition chtm.h:41
NOINLINE void chTMStartMeasurementX(time_measurement_t *tmp)
Starts a measurement.
Definition chtm.c:95
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
Type of a time measurement calibration data.
Definition chtm.h:62
rtcnt_t offset
Measurement calibration value.
Definition chtm.h:66