1 /*
2 * Percepio Trace Recorder for Tracealyzer v4.6.6
3 * Copyright 2021 Percepio AB
4 * www.percepio.com
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8 
9 /**
10  * @file
11  *
12  * @brief Public trace timestamp APIs.
13  */
14 
15 #ifndef TRC_TIMESTAMP_H
16 #define TRC_TIMESTAMP_H
17 
18 #if (TRC_USE_TRACEALYZER_RECORDER == 1)
19 
20 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
21 
22 #include <trcTypes.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 /**
29  * @defgroup trace_timestamp_apis Trace Timestamp APIs
30  * @ingroup trace_recorder_apis
31  * @{
32  */
33 
34 /**
35  * @brief Trace Timestamp Structure
36  */
37 typedef struct TraceTimestamp
38 {
39 	uint32_t type;						/**< Timer type (direction) */
40 	TraceUnsignedBaseType_t frequency;	/**< Timer Frequency */
41 	uint32_t period;					/**< Timer Period */
42 	uint32_t wraparounds;				/**< Nr of timer wraparounds */
43 	uint32_t osTickHz;					/**< RTOS tick frequency */
44 	uint32_t latestTimestamp;			/**< Latest timestamp */
45 	uint32_t osTickCount;				/**< RTOS tick count */
46 } TraceTimestamp_t;
47 
48 extern TraceTimestamp_t* pxTraceTimestamp;
49 
50 #define TRC_TIMESTAMP_RECORD_SIZE (sizeof(TraceTimestamp_t))
51 
52 /**
53  * @internal Trace Timestamp Buffer Structure
54  */
55 typedef struct TraceTimestampBuffer
56 {
57 	uint32_t buffer[(TRC_TIMESTAMP_RECORD_SIZE) / sizeof(uint32_t)];
58 } TraceTimestampBuffer_t;
59 
60 /**
61  * @internal Initialize trace timestamp system.
62  *
63  * @param[in] pxBuffer Pointer to memory that will be used by the
64  * trace timestamp system.
65  *
66  * @retval TRC_FAIL Failure
67  * @retval TRC_SUCCESS Success
68  */
69 traceResult xTraceTimestampInitialize(TraceTimestampBuffer_t *pxBuffer);
70 
71 #if ((TRC_CFG_USE_TRACE_ASSERT) == 1)
72 
73 /**
74  * @brief Gets current trace timestamp.
75  *
76  * @param[out] puiTimestamp Timestamp.
77  *
78  * @retval TRC_FAIL Failure
79  * @retval TRC_SUCCESS Success
80  */
81 traceResult xTraceTimestampGet(uint32_t* puiTimestamp);
82 
83 /**
84  * @brief Gets trace timestamp wraparounds.
85  *
86  * @param[out] puiTimerWraparounds Timer wraparounds.
87  *
88  * @retval TRC_FAIL Failure
89  * @retval TRC_SUCCESS Success
90  */
91 traceResult xTraceTimestampGetWraparounds(uint32_t* puiTimerWraparounds);
92 
93 /**
94  * @brief Sets trace timestamp frequency.
95  *
96  * @param[in] uxFrequency Frequency.
97  *
98  * @retval TRC_FAIL Failure
99  * @retval TRC_SUCCESS Success
100  */
101 traceResult xTraceTimestampSetFrequency(TraceUnsignedBaseType_t uxFrequency);
102 
103 /**
104  * @brief Gets trace timestamp frequency.
105  *
106  * @param[out] puxFrequency Frequency.
107  *
108  * @retval TRC_FAIL Failure
109  * @retval TRC_SUCCESS Success
110  */
111 traceResult xTraceTimestampGetFrequency(TraceUnsignedBaseType_t* puxFrequency);
112 
113 /**
114  * @brief Sets trace timestamp period.
115  *
116  * @param[in] uiPeriod Period.
117  *
118  * @retval TRC_FAIL Failure
119  * @retval TRC_SUCCESS Success
120  */
121 traceResult xTraceTimestampSetPeriod(uint32_t uiPeriod);
122 
123 /**
124  * @brief Gets trace timestamp period.
125  *
126  * @param[out] puiPeriod Period.
127  *
128  * @retval TRC_FAIL Failure
129  * @retval TRC_SUCCESS Success
130  */
131 traceResult xTraceTimestampGetPeriod(uint32_t* puiPeriod);
132 
133 /**
134  * @brief Sets trace timestamp OS tick count.
135  *
136  * @param[in] uiOsTickCount OS tick count.
137  *
138  * @retval TRC_FAIL Failure
139  * @retval TRC_SUCCESS Success
140  */
141 traceResult xTraceTimestampSetOsTickCount(uint32_t uiOsTickCount);
142 
143 /**
144  * @brief Gets trace timestamp OS tick count.
145  *
146  * @param[in] puiOsTickCount
147  *
148  * @retval TRC_FAIL Failure
149  * @retval TRC_SUCCESS Success
150  */
151 traceResult xTraceTimestampGetOsTickCount(uint32_t *puiOsTickCount);
152 
153 #else /* ((TRC_CFG_USE_TRACE_ASSERT) == 1) */
154 
155 /**
156  * @brief Gets current trace timestamp.
157  *
158  * @param[out] puiTimestamp Timestamp.
159  *
160  * @retval TRC_FAIL Failure
161  * @retval TRC_SUCCESS Success
162  */
163 #if ((TRC_HWTC_TYPE == TRC_FREE_RUNNING_32BIT_INCR) || (TRC_HWTC_TYPE == TRC_CUSTOM_TIMER_INCR))
164 #define xTraceTimestampGet(puiTimestamp) TRC_COMMA_EXPR_TO_STATEMENT_EXPR_4(*(puiTimestamp) = TRC_HWTC_COUNT, (*(puiTimestamp) < pxTraceTimestamp->latestTimestamp) ? pxTraceTimestamp->wraparounds++ : 0, pxTraceTimestamp->latestTimestamp = *(puiTimestamp), TRC_SUCCESS)
165 #elif ((TRC_HWTC_TYPE == TRC_FREE_RUNNING_32BIT_DECR) || (TRC_HWTC_TYPE == TRC_CUSTOM_TIMER_DECR))
166 #define xTraceTimestampGet(puiTimestamp) TRC_COMMA_EXPR_TO_STATEMENT_EXPR_4(*(puiTimestamp) = TRC_HWTC_COUNT, (*(puiTimestamp) > pxTraceTimestamp->latestTimestamp) ? pxTraceTimestamp->wraparounds++ : 0, pxTraceTimestamp->latestTimestamp = *(puiTimestamp), TRC_SUCCESS)
167 #elif ((TRC_HWTC_TYPE == TRC_OS_TIMER_INCR) || (TRC_HWTC_TYPE == TRC_OS_TIMER_DECR))
168 #define xTraceTimestampGet(puiTimestamp) TRC_COMMA_EXPR_TO_STATEMENT_EXPR_4(*(puiTimestamp) = ((TRC_HWTC_COUNT) & 0x00FFFFFFU) + ((pxTraceTimestamp->osTickCount & 0x000000FFU) << 24), pxTraceTimestamp->wraparounds = pxTraceTimestamp->osTickCount, pxTraceTimestamp->latestTimestamp = *(puiTimestamp), TRC_SUCCESS)
169 #endif
170 
171 /**
172  * @brief Gets trace timestamp wraparounds.
173  *
174  * @param[out] puiTimerWraparounds Timer wraparounds.
175  *
176  * @retval TRC_FAIL Failure
177  * @retval TRC_SUCCESS Success
178  */
179 #define xTraceTimestampGetWraparounds(puiTimerWraparounds) TRC_COMMA_EXPR_TO_STATEMENT_EXPR_2(*(puiTimerWraparounds) = pxTraceTimestamp->wraparounds, TRC_SUCCESS)
180 
181 /**
182  * @brief Sets trace timestamp frequency.
183  *
184  * @param[in] uxFrequency Frequency.
185  *
186  * @retval TRC_FAIL Failure
187  * @retval TRC_SUCCESS Success
188  */
189 #define xTraceTimestampSetFrequency(uxFrequency) TRC_COMMA_EXPR_TO_STATEMENT_EXPR_2(pxTraceTimestamp->frequency = uxFrequency, TRC_SUCCESS)
190 
191 /**
192  * @brief Sets trace timestamp period.
193  *
194  * @param[in] uiPeriod Period.
195  *
196  * @retval TRC_FAIL Failure
197  * @retval TRC_SUCCESS Success
198  */
199 #define xTraceTimestampSetPeriod(uiPeriod) TRC_COMMA_EXPR_TO_STATEMENT_EXPR_2(pxTraceTimestamp->period = uiPeriod, TRC_SUCCESS)
200 
201 /**
202  * @brief Sets trace timestamp OS tick count.
203  *
204  * @param[in] uiOsTickCount OS tick count.
205  *
206  * @retval TRC_FAIL Failure
207  * @retval TRC_SUCCESS Success
208  */
209 #define xTraceTimestampSetOsTickCount(uiOsTickCount) TRC_COMMA_EXPR_TO_STATEMENT_EXPR_2(pxTraceTimestamp->osTickCount = uiOsTickCount, TRC_SUCCESS)
210 
211 /**
212  * @brief Gets trace timestamp frequency.
213  *
214  * @param[out] puxFrequency Frequency.
215  *
216  * @retval TRC_FAIL Failure
217  * @retval TRC_SUCCESS Success
218  */
219 #define xTraceTimestampGetFrequency(puxFrequency) TRC_COMMA_EXPR_TO_STATEMENT_EXPR_2(*(puxFrequency) = pxTraceTimestamp->frequency, TRC_SUCCESS)
220 
221 /**
222  * @brief Gets trace timestamp period.
223  *
224  * @param[out] puiPeriod Period.
225  *
226  * @retval TRC_FAIL Failure
227  * @retval TRC_SUCCESS Success
228  */
229 #define xTraceTimestampGetPeriod(puiPeriod) TRC_COMMA_EXPR_TO_STATEMENT_EXPR_2(*(puiPeriod) = pxTraceTimestamp->period, TRC_SUCCESS)
230 
231 /**
232  * @brief Gets trace timestamp OS tick count.
233  *
234  * @param[in] puiOsTickCount
235  *
236  * @retval TRC_FAIL Failure
237  * @retval TRC_SUCCESS Success
238  */
239 #define xTraceTimestampGetOsTickCount(puiOsTickCount) TRC_COMMA_EXPR_TO_STATEMENT_EXPR_2(*(puiOsTickCount) = pxTraceTimestamp->osTickCount, TRC_SUCCESS)
240 
241 #endif /* ((TRC_CFG_USE_TRACE_ASSERT) == 1) */
242 
243 /** @} */
244 
245 #ifdef __cplusplus
246 }
247 #endif
248 
249 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
250 
251 #endif /* (TRC_USE_TRACEALYZER_RECORDER == 1) */
252 
253 #endif /* TRC_TIMESTAMP_H */
254