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