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