1 /*
2 * Percepio 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 
9 /**
10  * @file
11  *
12  * @brief Public trace counter APIs.
13  */
14 
15 #ifndef TRC_COUNTER_H
16 #define TRC_COUNTER_H
17 
18 #if (TRC_USE_TRACEALYZER_RECORDER == 1)
19 
20 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
21 
22 #define TRC_COUNTER_VALUE_INDEX 0
23 #define TRC_COUNTER_LOWER_LIMIT_INDEX 1
24 #define TRC_COUNTER_UPPER_LIMIT_INDEX 2
25 
26 #include <trcTypes.h>
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 /**
33  * @defgroup trace_counter_apis Trace Counter APIs
34  * @ingroup trace_recorder_apis
35  * @{
36  */
37 
38 /**
39  * @brief Sets trace counter callback.
40  *
41  * @param[in] xCallback Callback
42  *
43  * @retval TRC_FAIL Failure
44  * @retval TRC_SUCCESS Success
45  */
46 traceResult xTraceCounterSetCallback(TraceCounterCallback_t xCallback);
47 
48 /**
49  * @brief Creates trace counter.
50  *
51  * @param[in] szName Name.
52  * @param[in] xInitialValue Initial value.
53  * @param[in] xLowerLimit Lower limit.
54  * @param[in] xUpperLimit Upper limit.
55  * @param[out] pxCounterHandle Uninitialized trace counter handle.
56  *
57  * @retval TRC_FAIL Failure
58  * @retval TRC_SUCCESS Success
59  */
60 traceResult xTraceCounterCreate(const char* szName, TraceBaseType_t xInitialValue, TraceBaseType_t xLowerLimit, TraceBaseType_t xUpperLimit, TraceCounterHandle_t* pxCounterHandle);
61 
62 /**
63  * @brief Adds value to trace counter.
64  *
65  * @param[in] xCounterHandle Initialized trace counter handle.
66  * @param[in] xValue Value.
67  *
68  * @retval TRC_FAIL Failure
69  * @retval TRC_SUCCESS Success
70  */
71 #define xTraceCounterAdd(xCounterHandle, xValue) xTraceCounterSet(xCounterHandle, (TraceBaseType_t)(xTraceEntryGetStateReturn((TraceEntryHandle_t)(xCounterHandle), TRC_COUNTER_VALUE_INDEX)) + (xValue))
72 
73 /**
74  * @brief Sets trace counter value.
75  *
76  * @param[in] xCounterHandle Initialized trace counter handle.
77  * @param[in] xValue Value.
78  *
79  * @retval TRC_FAIL Failure
80  * @retval TRC_SUCCESS Success
81  */
82 traceResult xTraceCounterSet(TraceCounterHandle_t xCounterHandle, TraceBaseType_t xValue);
83 
84 /**
85  * @brief Gets trace counter value.
86  *
87  * @param[in] xCounterHandle Initialized trace counter handle.
88  * @param[out] pxValue Returned value.
89  *
90  * @retval TRC_FAIL Failure
91  * @retval TRC_SUCCESS Success
92  */
93 #define xTraceCounterGet(xCounterHandle, pxValue) xTraceEntryGetState((TraceEntryHandle_t)(xCounterHandle), TRC_COUNTER_VALUE_INDEX, (TraceUnsignedBaseType_t*)(pxValue))
94 
95 /**
96  * @brief Increases trace counter value.
97  *
98  * @param[in] xCounterHandle Initialized trace counter handle
99  *
100  * @retval TRC_FAIL Failure
101  * @retval TRC_SUCCESS Success
102  */
103 #define xTraceCounterIncrease(xCounterHandle) xTraceCounterAdd(xCounterHandle, 1)
104 
105 /**
106  * @brief Decreases trace counter value.
107  *
108  * @param[in] xCounterHandle Initialized trace counter handle
109  *
110  * @retval TRC_FAIL Failure
111  * @retval TRC_SUCCESS Success
112  */
113 #define xTraceCounterDecrease(xCounterHandle) xTraceCounterAdd(xCounterHandle, -1)
114 
115 /**
116  * @brief Gets trace counter upper limit.
117  *
118  * @param[in] xCounterHandle Initialized trace counter handle
119  * @param[out] pxValue Returned value
120  *
121  * @retval TRC_FAIL Failure
122  * @retval TRC_SUCCESS Success
123  */
124 #define xTraceCounterGetUpperLimit(xCounterHandle, pxValue) xTraceEntryGetState((TraceEntryHandle_t)(xCounterHandle), TRC_COUNTER_UPPER_LIMIT_INDEX, (TraceUnsignedBaseType_t*)(pxValue))
125 
126 /**
127  * @brief Gets trace counter lower limit.
128  *
129  * @param[in] xCounterHandle Initialized trace counter handle
130  * @param[out] pxValue Returned value
131  *
132  * @retval TRC_FAIL Failure
133  * @retval TRC_SUCCESS Success
134  */
135 #define xTraceCounterGetLowerLimit(xCounterHandle, pxValue) xTraceEntryGetState((TraceEntryHandle_t)(xCounterHandle), TRC_COUNTER_LOWER_LIMIT_INDEX, (TraceUnsignedBaseType_t*)(pxValue))
136 
137 /**
138  * @brief Gets trace counter name.
139  *
140  * @param[in] xCounterHandle Initialized trace counter handle.
141  * @param[out] pszName Returned name.
142  *
143  * @retval TRC_FAIL Failure
144  * @retval TRC_SUCCESS Success
145  */
146 #define xTraceCounterGetName(xCounterHandle, pszName) xTraceEntryGetSymbol((TraceEntryHandle_t)(xCounterHandle), pszName)
147 
148 /** @} */
149 
150 #ifdef __cplusplus
151 }
152 #endif
153 
154 #else
155 
156 /**
157  * @brief Disabled by TRC_CFG_RECORDER_MODE
158  */
159 #define xTraceCounterSetCallback(__xCallback) ((void)(__xCallback), TRC_SUCCESS)
160 
161 /**
162  * @brief Disabled by TRC_CFG_RECORDER_MODE
163  */
164 #define xTraceCounterCreate(__szName, __xInitialValue, __xLowerLimit, __xUpperLimit, __pxCounterHandle) ((void)(__szName), (void)(__xInitialValue), (void)(__xLowerLimit), (void)(__xUpperLimit), *(__pxCounterHandle) = 0, TRC_SUCCESS)
165 
166 /**
167  * @brief Disabled by TRC_CFG_RECORDER_MODE
168  */
169 #define xTraceCounterAdd(__xCounterHandle, __xValue) ((void)(__xCounterHandle), (void)(__xValue), TRC_SUCCESS)
170 
171 /**
172  * @brief Disabled by TRC_CFG_RECORDER_MODE
173  */
174 #define xTraceCounterSet(__xCounterHandle, __xValue) ((void)(__xCounterHandle), (void)(__xValue), TRC_SUCCESS)
175 
176 /**
177  * @brief Disabled by TRC_CFG_RECORDER_MODE
178  */
179 #define xTraceCounterGet(__xCounterHandle, __pxValue) ((void)(__xCounterHandle), *(__pxValue) = 0, TRC_SUCCESS)
180 
181 /**
182  * @brief Disabled by TRC_CFG_RECORDER_MODE
183  */
184 #define xTraceCounterIncrease(__xCounterHandle) ((void)(__xCounterHandle), TRC_SUCCESS)
185 
186 /**
187  * @brief Disabled by TRC_CFG_RECORDER_MODE
188  */
189 #define xTraceCounterDecrease(__xCounterHandle) ((void)(__xCounterHandle), TRC_SUCCESS)
190 
191 /**
192  * @brief Disabled by TRC_CFG_RECORDER_MODE
193  */
194 #define xTraceCounterGetUpperLimit(__xCounterHandle, __pxValue) ((void)(__xCounterHandle), *(__pxValue) = 0, TRC_SUCCESS)
195 
196 /**
197  * @brief Disabled by TRC_CFG_RECORDER_MODE
198  */
199 #define xTraceCounterGetLowerLimit(__xCounterHandle, __pxValue) ((void)(__xCounterHandle), *(__pxValue) = 0, TRC_SUCCESS)
200 
201 /**
202  * @brief Disabled by TRC_CFG_RECORDER_MODE
203  */
204 #define xTraceCounterGetName(__xCounterHandle, __pszName) ((void)(__xCounterHandle), *(__pszName) = "N/A", TRC_SUCCESS)
205 
206 #endif
207 
208 #endif
209 
210 #endif
211