1 /* 2 * Percepio 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 9 /** 10 * @file 11 * 12 * @brief Public trace timestamp APIs. 13 */ 14 15 #ifndef TRC_TIMESTAMP_H 16 #define TRC_TIMESTAMP_H 17 18 #if (TRC_USE_TRACEALYZER_RECORDER == 1) 19 20 #if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) 21 22 #include <trcTypes.h> 23 24 #ifdef __cplusplus 25 extern "C" { 26 #endif 27 28 /** 29 * @defgroup trace_timestamp_apis Trace Timestamp APIs 30 * @ingroup trace_recorder_apis 31 * @{ 32 */ 33 34 /** 35 * @brief Trace Timestamp Structure 36 */ 37 typedef struct TraceTimestampData /* Aligned */ 38 { 39 uint32_t type; /**< Timer type (direction) */ 40 uint32_t period; /**< Timer Period */ 41 TraceUnsignedBaseType_t frequency; /**< Timer Frequency */ 42 uint32_t wraparounds; /**< Nr of timer wraparounds */ 43 uint32_t osTickHz; /**< RTOS tick frequency */ 44 uint32_t latestTimestamp; /**< Latest timestamp */ 45 uint32_t osTickCount; /**< RTOS tick count */ 46 } TraceTimestampData_t; 47 48 extern TraceTimestampData_t* pxTraceTimestamp; 49 50 /** 51 * @internal Initialize trace timestamp system. 52 * 53 * @param[in] pxBuffer Pointer to memory that will be used by the 54 * trace timestamp system. 55 * 56 * @retval TRC_FAIL Failure 57 * @retval TRC_SUCCESS Success 58 */ 59 traceResult xTraceTimestampInitialize(TraceTimestampData_t *pxBuffer); 60 61 #if ((TRC_CFG_USE_TRACE_ASSERT) == 1) 62 63 /** 64 * @brief Gets current trace timestamp. 65 * 66 * @param[out] puiTimestamp Timestamp. 67 * 68 * @retval TRC_FAIL Failure 69 * @retval TRC_SUCCESS Success 70 */ 71 traceResult xTraceTimestampGet(uint32_t* puiTimestamp); 72 73 /** 74 * @brief Gets trace timestamp wraparounds. 75 * 76 * @param[out] puiTimerWraparounds Timer wraparounds. 77 * 78 * @retval TRC_FAIL Failure 79 * @retval TRC_SUCCESS Success 80 */ 81 traceResult xTraceTimestampGetWraparounds(uint32_t* puiTimerWraparounds); 82 83 /** 84 * @brief Sets trace timestamp frequency. 85 * 86 * @param[in] uxFrequency Frequency. 87 * 88 * @retval TRC_FAIL Failure 89 * @retval TRC_SUCCESS Success 90 */ 91 traceResult xTraceTimestampSetFrequency(TraceUnsignedBaseType_t uxFrequency); 92 93 /** 94 * @brief Gets trace timestamp frequency. 95 * 96 * @param[out] puxFrequency Frequency. 97 * 98 * @retval TRC_FAIL Failure 99 * @retval TRC_SUCCESS Success 100 */ 101 traceResult xTraceTimestampGetFrequency(TraceUnsignedBaseType_t* puxFrequency); 102 103 /** 104 * @brief Sets trace timestamp period. 105 * 106 * @param[in] uiPeriod Period. 107 * 108 * @retval TRC_FAIL Failure 109 * @retval TRC_SUCCESS Success 110 */ 111 traceResult xTraceTimestampSetPeriod(uint32_t uiPeriod); 112 113 /** 114 * @brief Gets trace timestamp period. 115 * 116 * @param[out] puiPeriod Period. 117 * 118 * @retval TRC_FAIL Failure 119 * @retval TRC_SUCCESS Success 120 */ 121 traceResult xTraceTimestampGetPeriod(uint32_t* puiPeriod); 122 123 /** 124 * @brief Sets trace timestamp OS tick count. 125 * 126 * @param[in] uiOsTickCount OS tick count. 127 * 128 * @retval TRC_FAIL Failure 129 * @retval TRC_SUCCESS Success 130 */ 131 traceResult xTraceTimestampSetOsTickCount(uint32_t uiOsTickCount); 132 133 /** 134 * @brief Gets trace timestamp OS tick count. 135 * 136 * @param[in] puiOsTickCount 137 * 138 * @retval TRC_FAIL Failure 139 * @retval TRC_SUCCESS Success 140 */ 141 traceResult xTraceTimestampGetOsTickCount(uint32_t *puiOsTickCount); 142 143 #else /* ((TRC_CFG_USE_TRACE_ASSERT) == 1) */ 144 145 /** 146 * @brief Gets current trace timestamp. 147 * 148 * @param[out] puiTimestamp Timestamp. 149 * 150 * @retval TRC_FAIL Failure 151 * @retval TRC_SUCCESS Success 152 */ 153 #if ((TRC_HWTC_TYPE == TRC_FREE_RUNNING_32BIT_INCR) || (TRC_HWTC_TYPE == TRC_CUSTOM_TIMER_INCR)) 154 #define xTraceTimestampGet(puiTimestamp) TRC_COMMA_EXPR_TO_STATEMENT_EXPR_4(*(puiTimestamp) = TRC_HWTC_COUNT, (*(puiTimestamp) < pxTraceTimestamp->latestTimestamp) ? pxTraceTimestamp->wraparounds++ : 0, pxTraceTimestamp->latestTimestamp = *(puiTimestamp), TRC_SUCCESS) 155 #elif ((TRC_HWTC_TYPE == TRC_FREE_RUNNING_32BIT_DECR) || (TRC_HWTC_TYPE == TRC_CUSTOM_TIMER_DECR)) 156 #define xTraceTimestampGet(puiTimestamp) TRC_COMMA_EXPR_TO_STATEMENT_EXPR_4(*(puiTimestamp) = TRC_HWTC_COUNT, (*(puiTimestamp) > pxTraceTimestamp->latestTimestamp) ? pxTraceTimestamp->wraparounds++ : 0, pxTraceTimestamp->latestTimestamp = *(puiTimestamp), TRC_SUCCESS) 157 #elif ((TRC_HWTC_TYPE == TRC_OS_TIMER_INCR) || (TRC_HWTC_TYPE == TRC_OS_TIMER_DECR)) 158 #define xTraceTimestampGet(puiTimestamp) TRC_COMMA_EXPR_TO_STATEMENT_EXPR_4(*(puiTimestamp) = ((TRC_HWTC_COUNT) & 0x00FFFFFFU) + ((pxTraceTimestamp->osTickCount & 0x000000FFU) << 24), pxTraceTimestamp->wraparounds = pxTraceTimestamp->osTickCount, pxTraceTimestamp->latestTimestamp = *(puiTimestamp), TRC_SUCCESS) 159 #endif 160 161 /** 162 * @brief Gets trace timestamp wraparounds. 163 * 164 * @param[out] puiTimerWraparounds Timer wraparounds. 165 * 166 * @retval TRC_FAIL Failure 167 * @retval TRC_SUCCESS Success 168 */ 169 #define xTraceTimestampGetWraparounds(puiTimerWraparounds) TRC_COMMA_EXPR_TO_STATEMENT_EXPR_2(*(puiTimerWraparounds) = pxTraceTimestamp->wraparounds, TRC_SUCCESS) 170 171 /** 172 * @brief Sets trace timestamp frequency. 173 * 174 * @param[in] uxFrequency Frequency. 175 * 176 * @retval TRC_FAIL Failure 177 * @retval TRC_SUCCESS Success 178 */ 179 #define xTraceTimestampSetFrequency(uxFrequency) TRC_COMMA_EXPR_TO_STATEMENT_EXPR_2(pxTraceTimestamp->frequency = uxFrequency, TRC_SUCCESS) 180 181 /** 182 * @brief Sets trace timestamp period. 183 * 184 * @param[in] uiPeriod Period. 185 * 186 * @retval TRC_FAIL Failure 187 * @retval TRC_SUCCESS Success 188 */ 189 #define xTraceTimestampSetPeriod(uiPeriod) TRC_COMMA_EXPR_TO_STATEMENT_EXPR_2(pxTraceTimestamp->period = uiPeriod, TRC_SUCCESS) 190 191 /** 192 * @brief Sets trace timestamp OS tick count. 193 * 194 * @param[in] uiOsTickCount OS tick count. 195 * 196 * @retval TRC_FAIL Failure 197 * @retval TRC_SUCCESS Success 198 */ 199 #define xTraceTimestampSetOsTickCount(uiOsTickCount) TRC_COMMA_EXPR_TO_STATEMENT_EXPR_2(pxTraceTimestamp->osTickCount = uiOsTickCount, TRC_SUCCESS) 200 201 /** 202 * @brief Gets trace timestamp frequency. 203 * 204 * @param[out] puxFrequency Frequency. 205 * 206 * @retval TRC_FAIL Failure 207 * @retval TRC_SUCCESS Success 208 */ 209 #define xTraceTimestampGetFrequency(puxFrequency) TRC_COMMA_EXPR_TO_STATEMENT_EXPR_2(*(puxFrequency) = pxTraceTimestamp->frequency, TRC_SUCCESS) 210 211 /** 212 * @brief Gets trace timestamp period. 213 * 214 * @param[out] puiPeriod Period. 215 * 216 * @retval TRC_FAIL Failure 217 * @retval TRC_SUCCESS Success 218 */ 219 #define xTraceTimestampGetPeriod(puiPeriod) TRC_COMMA_EXPR_TO_STATEMENT_EXPR_2(*(puiPeriod) = pxTraceTimestamp->period, TRC_SUCCESS) 220 221 /** 222 * @brief Gets trace timestamp OS tick count. 223 * 224 * @param[in] puiOsTickCount 225 * 226 * @retval TRC_FAIL Failure 227 * @retval TRC_SUCCESS Success 228 */ 229 #define xTraceTimestampGetOsTickCount(puiOsTickCount) TRC_COMMA_EXPR_TO_STATEMENT_EXPR_2(*(puiOsTickCount) = pxTraceTimestamp->osTickCount, TRC_SUCCESS) 230 231 #endif /* ((TRC_CFG_USE_TRACE_ASSERT) == 1) */ 232 233 /** @} */ 234 235 #ifdef __cplusplus 236 } 237 #endif 238 239 #endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */ 240 241 #endif /* (TRC_USE_TRACEALYZER_RECORDER == 1) */ 242 243 #endif /* TRC_TIMESTAMP_H */ 244