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 intervals.
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 static TraceCounterCallback_t xCallbackFunction;
18 
xTraceCounterSetCallback(TraceCounterCallback_t xCallback)19 traceResult xTraceCounterSetCallback(TraceCounterCallback_t xCallback)
20 {
21 	TRC_ASSERT(xCallback != 0);
22 
23 	xCallbackFunction = xCallback;
24 
25 	/* We only set this component as initialized when the callback has been set */
26 	xTraceSetComponentInitialized(TRC_RECORDER_COMPONENT_COUNTER);
27 
28 	return TRC_SUCCESS;
29 }
30 
xTraceCounterCreate(const char * szName,TraceBaseType_t xInitialValue,TraceBaseType_t xLowerLimit,TraceBaseType_t xUpperLimit,TraceCounterHandle_t * pxCounterHandle)31 traceResult xTraceCounterCreate(const char *szName, TraceBaseType_t xInitialValue, TraceBaseType_t xLowerLimit, TraceBaseType_t xUpperLimit, TraceCounterHandle_t *pxCounterHandle)
32 {
33 	TraceObjectHandle_t xObjectHandle;
34 	TraceUnsignedBaseType_t uxStates[3];
35 
36 	TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_COUNTER));
37 
38 	/* This should never fail */
39 	TRC_ASSERT(pxCounterHandle != 0);
40 
41 	TRC_ASSERT(xInitialValue >= xLowerLimit && xInitialValue <= xUpperLimit);
42 
43 	uxStates[TRC_COUNTER_VALUE_INDEX] = (TraceUnsignedBaseType_t)xInitialValue;
44 	uxStates[TRC_COUNTER_LOWER_LIMIT_INDEX] = (TraceUnsignedBaseType_t)xLowerLimit;
45 	uxStates[TRC_COUNTER_UPPER_LIMIT_INDEX] = (TraceUnsignedBaseType_t)xUpperLimit;
46 
47 	/* We need to check this */
48 	if (xTraceObjectRegisterInternal(PSF_EVENT_COUNTER_CREATE, 0, szName, 3, uxStates, TRC_ENTRY_OPTION_COUNTER, &xObjectHandle) == TRC_FAIL)
49 	{
50 		return TRC_FAIL;
51 	}
52 
53 	*pxCounterHandle = (TraceCounterHandle_t)xObjectHandle;
54 
55 	return TRC_SUCCESS;
56 }
57 
xTraceCounterSet(TraceCounterHandle_t xCounterHandle,TraceBaseType_t xValue)58 traceResult xTraceCounterSet(TraceCounterHandle_t xCounterHandle, TraceBaseType_t xValue)
59 {
60 	TraceEventHandle_t xEventHandle = 0;
61 	TraceBaseType_t xLowerLimit;
62 	TraceBaseType_t xUpperLimit;
63 
64 	TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_COUNTER));
65 
66 	/* This should never fail */
67 	TRC_ASSERT_ALWAYS_EVALUATE(xTraceEntrySetState((TraceEntryHandle_t)xCounterHandle, TRC_COUNTER_VALUE_INDEX, (TraceUnsignedBaseType_t)xValue) == TRC_SUCCESS);
68 
69 	/* We need to check this */
70 	if (xTraceEventBegin(PSF_EVENT_COUNTER_CHANGE, sizeof(void*) + sizeof(uint32_t), &xEventHandle) == TRC_SUCCESS)
71 	{
72 		xTraceEventAddPointer(xEventHandle, (void*)xCounterHandle);
73 		xTraceEventAdd32(xEventHandle, (uint32_t)xValue);
74 		xTraceEventEnd(xEventHandle);
75 	}
76 
77 	/* These should never fail */
78 	TRC_ASSERT_ALWAYS_EVALUATE(xTraceCounterGetLowerLimit(xCounterHandle, &xLowerLimit) == TRC_SUCCESS);
79 	TRC_ASSERT_ALWAYS_EVALUATE(xTraceCounterGetUpperLimit(xCounterHandle, &xUpperLimit) == TRC_SUCCESS);
80 
81 	if (xValue < xLowerLimit || xValue > xUpperLimit)
82 	{
83 		if (xTraceEventBegin(PSF_EVENT_COUNTER_LIMIT_EXCEEDED, sizeof(void*), &xEventHandle) == TRC_SUCCESS)
84 		{
85 			xTraceEventAddPointer(xEventHandle, (void*)xCounterHandle);
86 			xTraceEventEnd(xEventHandle);
87 		}
88 
89 		xCallbackFunction(xCounterHandle);
90 	}
91 
92 	return TRC_SUCCESS;
93 }
94 
95 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
96 
97 #endif /* (TRC_USE_TRACEALYZER_RECORDER == 1) */
98