1 /*
2 * Trace Recorder for Tracealyzer v4.9.2
3 * Copyright 2023 Percepio AB
4 * www.percepio.com
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 *
8 * The implementation of timestamps.
9 */
10 
11 #include <trcRecorder.h>
12 
13 #if (TRC_USE_TRACEALYZER_RECORDER == 1) && (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
14 
15 TraceTimestampData_t *pxTraceTimestamp TRC_CFG_RECORDER_DATA_ATTRIBUTE;
16 
xTraceTimestampInitialize(TraceTimestampData_t * pxBuffer)17 traceResult xTraceTimestampInitialize(TraceTimestampData_t *pxBuffer)
18 {
19 	/* This should never fail */
20 	TRC_ASSERT(pxBuffer != (void*)0);
21 
22 	pxTraceTimestamp = pxBuffer;
23 
24 	/* These will be set when tracing is enabled */
25 	pxTraceTimestamp->frequency = 0u;
26 	pxTraceTimestamp->period = 0u;
27 
28 	pxTraceTimestamp->osTickHz = TRC_TICK_RATE_HZ;
29 	pxTraceTimestamp->osTickCount = 0u;
30 	pxTraceTimestamp->wraparounds = 0u;
31 	pxTraceTimestamp->type = TRC_HWTC_TYPE;
32 
33 #if (TRC_HWTC_TYPE == TRC_FREE_RUNNING_32BIT_INCR || TRC_HWTC_TYPE == TRC_CUSTOM_TIMER_INCR || TRC_HWTC_TYPE == TRC_OS_TIMER_INCR)
34 	pxTraceTimestamp->latestTimestamp = 0u;
35 #elif (TRC_HWTC_TYPE == TRC_FREE_RUNNING_32BIT_DECR || TRC_HWTC_TYPE == TRC_CUSTOM_TIMER_DECR || TRC_HWTC_TYPE == TRC_OS_TIMER_DECR)
36 	pxTraceTimestamp->latestTimestamp = pxTraceTimestamp->period - 1u;
37 #endif
38 
39 	xTraceSetComponentInitialized(TRC_RECORDER_COMPONENT_TIMESTAMP);
40 
41 	return TRC_SUCCESS;
42 }
43 
44 #if ((TRC_CFG_USE_TRACE_ASSERT) == 1)
45 
xTraceTimestampGet(uint32_t * puiTimestamp)46 traceResult xTraceTimestampGet(uint32_t *puiTimestamp)
47 {
48 	/* This should never fail */
49 	TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_TIMESTAMP));
50 
51 	/* This should never fail */
52 	TRC_ASSERT(puiTimestamp != (void*)0);
53 
54 	switch (pxTraceTimestamp->type)
55 	{
56 	case TRC_FREE_RUNNING_32BIT_INCR:
57 	case TRC_CUSTOM_TIMER_INCR:
58 		*puiTimestamp = (uint32_t)(TRC_HWTC_COUNT);
59 		if (*puiTimestamp < pxTraceTimestamp->latestTimestamp)
60 		{
61 			pxTraceTimestamp->wraparounds++;
62 		}
63 		break;
64 	case TRC_FREE_RUNNING_32BIT_DECR:
65 	case TRC_CUSTOM_TIMER_DECR:
66 		*puiTimestamp = (uint32_t)(TRC_HWTC_COUNT);
67 		if (*puiTimestamp > pxTraceTimestamp->latestTimestamp)
68 		{
69 			pxTraceTimestamp->wraparounds++;
70 		}
71 		break;
72 	case TRC_OS_TIMER_INCR:
73 	case TRC_OS_TIMER_DECR:
74 		*puiTimestamp = (((uint32_t)(TRC_HWTC_COUNT)) & 0x00FFFFFFUL) + ((pxTraceTimestamp->osTickCount & 0x000000FFUL) << 24);
75 		pxTraceTimestamp->wraparounds = pxTraceTimestamp->osTickCount;
76 		break;
77 	default:
78 		return TRC_FAIL;
79 	}
80 
81 	pxTraceTimestamp->latestTimestamp = *puiTimestamp;
82 
83 	return TRC_SUCCESS;
84 }
85 
xTraceTimestampGetWraparounds(uint32_t * puiTimerWraparounds)86 traceResult xTraceTimestampGetWraparounds(uint32_t* puiTimerWraparounds)
87 {
88 	/* This should never fail */
89 	TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_TIMESTAMP));
90 
91 	/* This should never fail */
92 	TRC_ASSERT(puiTimerWraparounds != (void*)0);
93 
94 	*puiTimerWraparounds = pxTraceTimestamp->wraparounds;
95 
96 	return TRC_SUCCESS;
97 }
98 
xTraceTimestampSetFrequency(TraceUnsignedBaseType_t uxFrequency)99 traceResult xTraceTimestampSetFrequency(TraceUnsignedBaseType_t uxFrequency)
100 {
101 	/* This should never fail */
102 	TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_TIMESTAMP));
103 
104 	pxTraceTimestamp->frequency = uxFrequency;
105 
106 	return TRC_SUCCESS;
107 }
108 
xTraceTimestampSetPeriod(uint32_t uiPeriod)109 traceResult xTraceTimestampSetPeriod(uint32_t uiPeriod)
110 {
111 	/* This should never fail */
112 	TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_TIMESTAMP));
113 
114 	pxTraceTimestamp->period = uiPeriod;
115 
116 	return TRC_SUCCESS;
117 }
118 
xTraceTimestampSetOsTickCount(uint32_t uiOsTickCount)119 traceResult xTraceTimestampSetOsTickCount(uint32_t uiOsTickCount)
120 {
121 	/* This should never fail */
122 	TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_TIMESTAMP));
123 
124 	pxTraceTimestamp->osTickCount = uiOsTickCount;
125 
126 	return TRC_SUCCESS;
127 }
128 
xTraceTimestampGetFrequency(TraceUnsignedBaseType_t * puxFrequency)129 traceResult xTraceTimestampGetFrequency(TraceUnsignedBaseType_t *puxFrequency)
130 {
131 	/* This should never fail */
132 	TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_TIMESTAMP));
133 
134 	/* This should never fail */
135 	TRC_ASSERT(puxFrequency != (void*)0);
136 
137 	*puxFrequency = pxTraceTimestamp->frequency;
138 
139 	return TRC_SUCCESS;
140 }
141 
xTraceTimestampGetPeriod(uint32_t * puiPeriod)142 traceResult xTraceTimestampGetPeriod(uint32_t *puiPeriod)
143 {
144 	/* This should never fail */
145 	TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_TIMESTAMP));
146 
147 	/* This should never fail */
148 	TRC_ASSERT(puiPeriod != (void*)0);
149 
150 	*puiPeriod = pxTraceTimestamp->period;
151 
152 	return TRC_SUCCESS;
153 }
154 
xTraceTimestampGetOsTickCount(uint32_t * puiOsTickCount)155 traceResult xTraceTimestampGetOsTickCount(uint32_t* puiOsTickCount)
156 {
157 	/* This should never fail */
158 	TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_TIMESTAMP));
159 
160 	/* This should never fail */
161 	TRC_ASSERT(puiOsTickCount != (void*)0);
162 
163 	*puiOsTickCount = pxTraceTimestamp->osTickCount;
164 
165 	return TRC_SUCCESS;
166 }
167 
168 #endif
169 
170 #endif
171