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 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 TraceCounterData_t *pxCounterData TRC_CFG_RECORDER_DATA_ATTRIBUTE;
18 
xTraceCounterInitialize(TraceCounterData_t * pxBuffer)19 traceResult xTraceCounterInitialize(TraceCounterData_t *pxBuffer)
20 {
21 	TRC_ASSERT(pxBuffer != (void*)0);
22 
23 	pxCounterData = pxBuffer;
24 
25 	pxCounterData->xCallbackFunction = 0;
26 
27 	xTraceSetComponentInitialized(TRC_RECORDER_COMPONENT_COUNTER);
28 
29 	return TRC_SUCCESS;
30 }
31 
xTraceCounterSetCallback(const TraceCounterCallback_t xCallback)32 traceResult xTraceCounterSetCallback(const TraceCounterCallback_t xCallback)
33 {
34 	TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_COUNTER));
35 
36 	TRC_ASSERT(xCallback != 0);
37 
38 	pxCounterData->xCallbackFunction = xCallback;
39 
40 	return TRC_SUCCESS;
41 }
42 
43 /*cstat !MISRAC2004-6.3 !MISRAC2012-Dir-4.6_a Suppress basic char type usage*/
xTraceCounterCreate(const char * szName,TraceBaseType_t xInitialValue,TraceBaseType_t xLowerLimit,TraceBaseType_t xUpperLimit,TraceCounterHandle_t * pxCounterHandle)44 traceResult xTraceCounterCreate(const char *szName, TraceBaseType_t xInitialValue, TraceBaseType_t xLowerLimit, TraceBaseType_t xUpperLimit, TraceCounterHandle_t *pxCounterHandle)
45 {
46 	TraceObjectHandle_t xObjectHandle;
47 	TraceUnsignedBaseType_t uxStates[3] = { 0 };
48 
49 	TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_COUNTER));
50 
51 	/* This should never fail */
52 	TRC_ASSERT(pxCounterHandle != (void*)0);
53 
54 	TRC_ASSERT((xInitialValue >= xLowerLimit) && (xInitialValue <= xUpperLimit));
55 
56 	uxStates[TRC_COUNTER_VALUE_INDEX] = (TraceUnsignedBaseType_t)xInitialValue;
57 	uxStates[TRC_COUNTER_LOWER_LIMIT_INDEX] = (TraceUnsignedBaseType_t)xLowerLimit;
58 	uxStates[TRC_COUNTER_UPPER_LIMIT_INDEX] = (TraceUnsignedBaseType_t)xUpperLimit;
59 
60 	/* We need to check this */
61 	if (xTraceObjectRegisterInternal(PSF_EVENT_COUNTER_CREATE, (void*)0, szName, 3u, uxStates, TRC_ENTRY_OPTION_COUNTER, &xObjectHandle) == TRC_FAIL)
62 	{
63 		return TRC_FAIL;
64 	}
65 
66 	*pxCounterHandle = (TraceCounterHandle_t)xObjectHandle;
67 
68 	return TRC_SUCCESS;
69 }
70 
xTraceCounterSet(TraceCounterHandle_t xCounterHandle,TraceBaseType_t xValue)71 traceResult xTraceCounterSet(TraceCounterHandle_t xCounterHandle, TraceBaseType_t xValue)
72 {
73 	TraceBaseType_t xLowerLimit = 0;
74 	TraceBaseType_t xUpperLimit = 0;
75 
76 	TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_COUNTER));
77 
78 	/* This should never fail */
79 	TRC_ASSERT_ALWAYS_EVALUATE(xTraceObjectSetSpecificState((TraceEntryHandle_t)xCounterHandle, TRC_COUNTER_VALUE_INDEX, (TraceUnsignedBaseType_t)xValue) == TRC_SUCCESS);
80 
81 	(void)xTraceEventCreate2(PSF_EVENT_COUNTER_CHANGE, (TraceUnsignedBaseType_t)xCounterHandle, (TraceUnsignedBaseType_t)xValue); /*cstat !MISRAC2004-11.3 !MISRAC2012-Rule-11.4 Suppress conversion from pointer to integer check*/
82 
83 	/* These should never fail */
84 	TRC_ASSERT_ALWAYS_EVALUATE(xTraceCounterGetLowerLimit(xCounterHandle, &xLowerLimit) == TRC_SUCCESS);
85 	TRC_ASSERT_ALWAYS_EVALUATE(xTraceCounterGetUpperLimit(xCounterHandle, &xUpperLimit) == TRC_SUCCESS);
86 
87 	if ((xValue < xLowerLimit) || (xValue > xUpperLimit))
88 	{
89 		(void)xTraceEventCreate1(PSF_EVENT_COUNTER_LIMIT_EXCEEDED, (TraceUnsignedBaseType_t)xCounterHandle); /*cstat !MISRAC2004-11.3 !MISRAC2012-Rule-11.4 Suppress conversion from pointer to integer check*/
90 
91 		if (pxCounterData->xCallbackFunction != 0)
92 		{
93 			pxCounterData->xCallbackFunction(xCounterHandle);
94 		}
95 	}
96 
97 	return TRC_SUCCESS;
98 }
99 
100 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
101 
102 #endif /* (TRC_USE_TRACEALYZER_RECORDER == 1) */
103