/*******************************************************************************\r
- * FreeRTOS+Trace v2.2.3 Recorder Library\r
- * Percepio AB, www.percepio.se\r
+ * FreeRTOS+Trace v2.3.0 Recorder Library\r
+ * Percepio AB, www.percepio.com\r
*\r
* trcPort.c\r
*\r
* , hobbyists or early-phase startups) we have an attractive offer: \r
* Provide a hardware timer port and get a FREE single-user licence for\r
* FreeRTOS+Trace Professional Edition. Read more about this offer at \r
- * www.percepio.se or contact us directly at support@percepio.se.\r
+ * www.percepio.com or contact us directly at support@percepio.com.\r
*\r
* FreeRTOS+Trace is available as Free Edition and in two premium editions.\r
* You may use the premium features during 30 days for evaluation.\r
- * Download FreeRTOS+Trace at http://www.percepio.se/index.php?page=downloads\r
+ * Download FreeRTOS+Trace at http://www.percepio.com/products/downloads/\r
*\r
* Copyright Percepio AB, 2012.\r
- * www.percepio.se\r
+ * www.percepio.com\r
******************************************************************************/\r
-#include "FreeRTOS.h"\r
-#include "trcPort.h"\r
+\r
#include "trcUser.h"\r
\r
#if (configUSE_TRACE_FACILITY == 1)\r
static char* prvFileName = NULL;\r
#endif\r
\r
+\r
/*******************************************************************************\r
* uiTraceTickCount\r
*\r
* OFFER FROM PERCEPIO:\r
* For silicon companies and non-corporate FreeRTOS users (researchers, students\r
* , hobbyists or early-phase startups) we have an attractive offer: \r
- * Provide a hardware timer port and get a FREE single-user licence for\r
+ * Provide a hardware timer port and get a FREE single-user license for\r
* FreeRTOS+Trace Professional Edition. Read more about this offer at \r
- * www.percepio.se or contact us directly at support@percepio.se.\r
+ * www.percepio.com or contact us directly at support@percepio.com.\r
******************************************************************************/\r
-uint32_t uiTracePortGetTimeStamp()\r
+void uiTracePortGetTimeStamp(uint32_t *pTimestamp)\r
{\r
- /* Keep these static to avoid using more stack than necessary */\r
- static uint32_t last_timestamp = 0;\r
- static uint32_t timestamp;\r
-\r
+ static uint32_t last_traceTickCount = 0;\r
+ static uint32_t last_hwtc_count = 0;\r
+ uint32_t traceTickCount = 0;\r
+ uint32_t hwtc_count = 0;\r
+ \r
+ /* Retrieve HWTC_COUNT only once since the same value should be used all throughout this function. */\r
#if (HWTC_COUNT_DIRECTION == DIRECTION_INCREMENTING)\r
- timestamp = ((uiTraceTickCount * HWTC_PERIOD) + HWTC_COUNT) / HWTC_DIVISOR;\r
-#else\r
-#if (HWTC_COUNT_DIRECTION == DIRECTION_DECREMENTING)\r
- timestamp = ((uiTraceTickCount * HWTC_PERIOD) + (HWTC_PERIOD - HWTC_COUNT)) / HWTC_DIVISOR;\r
+ hwtc_count = HWTC_COUNT;\r
+#elif (HWTC_COUNT_DIRECTION == DIRECTION_DECREMENTING)\r
+ hwtc_count = HWTC_PERIOD - HWTC_COUNT;\r
#else\r
Junk text to cause compiler error - HWTC_COUNT_DIRECTION is not set correctly!\r
Should be DIRECTION_INCREMENTING or DIRECTION_DECREMENTING\r
#endif\r
-#endif\r
+ \r
+ if (last_traceTickCount - uiTraceTickCount - 1 < 0x80000000)\r
+ {\r
+ /* This means last_traceTickCount is higher than uiTraceTickCount,\r
+ so we have previously compensated for a missed tick.\r
+ Therefore we use the last stored value because that is more accurate. */\r
+ traceTickCount = last_traceTickCount;\r
+ }\r
+ else\r
+ {\r
+ /* Business as usual */\r
+ traceTickCount = uiTraceTickCount;\r
+ }\r
\r
- /* May occur due to overflow, if the update of uiTraceTickCount has been \r
+ /* Check for overflow. May occur if the update of uiTraceTickCount has been \r
delayed due to disabled interrupts. */\r
- if (timestamp < last_timestamp)\r
+ if (traceTickCount == last_traceTickCount && hwtc_count < last_hwtc_count)\r
{\r
- timestamp += (HWTC_PERIOD / HWTC_DIVISOR);\r
+ /* A trace tick has occurred but not been executed by the kernel, so we compensate manually. */\r
+ traceTickCount++;\r
}\r
-\r
- last_timestamp = timestamp;\r
-\r
- return timestamp;\r
+ \r
+ /* Check if the return address is OK, then we perform the calculation. */\r
+ if (pTimestamp)\r
+ {\r
+ /* Get timestamp from trace ticks. Scale down the period to avoid unwanted overflows. */\r
+ *pTimestamp = traceTickCount * (HWTC_PERIOD / HWTC_DIVISOR);\r
+ /* Increase timestamp by (hwtc_count + "lost hardware ticks from scaling down period") / HWTC_DIVISOR. */\r
+ *pTimestamp += (hwtc_count + traceTickCount * (HWTC_PERIOD % HWTC_DIVISOR)) / HWTC_DIVISOR;\r
+ }\r
+ \r
+ /* Store the previous values. */\r
+ last_traceTickCount = traceTickCount;\r
+ last_hwtc_count = hwtc_count;\r
}\r
\r
/*******************************************************************************\r