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