trcTimestamp.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 timestamps.
  9. */
  10. #include <trcRecorder.h>
  11. #if (TRC_USE_TRACEALYZER_RECORDER == 1) && (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
  12. TraceTimestampData_t *pxTraceTimestamp TRC_CFG_RECORDER_DATA_ATTRIBUTE;
  13. traceResult xTraceTimestampInitialize(TraceTimestampData_t *pxBuffer)
  14. {
  15. /* This should never fail */
  16. TRC_ASSERT(pxBuffer != (void*)0);
  17. pxTraceTimestamp = pxBuffer;
  18. /* These will be set when tracing is enabled */
  19. pxTraceTimestamp->frequency = 0u;
  20. pxTraceTimestamp->period = 0u;
  21. pxTraceTimestamp->osTickHz = TRC_TICK_RATE_HZ;
  22. pxTraceTimestamp->osTickCount = 0u;
  23. pxTraceTimestamp->wraparounds = 0u;
  24. pxTraceTimestamp->type = TRC_HWTC_TYPE;
  25. #if (TRC_HWTC_TYPE == TRC_FREE_RUNNING_32BIT_INCR || TRC_HWTC_TYPE == TRC_CUSTOM_TIMER_INCR || TRC_HWTC_TYPE == TRC_OS_TIMER_INCR)
  26. pxTraceTimestamp->latestTimestamp = 0u;
  27. #elif (TRC_HWTC_TYPE == TRC_FREE_RUNNING_32BIT_DECR || TRC_HWTC_TYPE == TRC_CUSTOM_TIMER_DECR || TRC_HWTC_TYPE == TRC_OS_TIMER_DECR)
  28. pxTraceTimestamp->latestTimestamp = pxTraceTimestamp->period - 1u;
  29. #endif
  30. xTraceSetComponentInitialized(TRC_RECORDER_COMPONENT_TIMESTAMP);
  31. return TRC_SUCCESS;
  32. }
  33. #if ((TRC_CFG_USE_TRACE_ASSERT) == 1)
  34. traceResult xTraceTimestampGet(uint32_t *puiTimestamp)
  35. {
  36. /* This should never fail */
  37. TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_TIMESTAMP));
  38. /* This should never fail */
  39. TRC_ASSERT(puiTimestamp != (void*)0);
  40. switch (pxTraceTimestamp->type)
  41. {
  42. case TRC_FREE_RUNNING_32BIT_INCR:
  43. case TRC_CUSTOM_TIMER_INCR:
  44. *puiTimestamp = (uint32_t)(TRC_HWTC_COUNT);
  45. if (*puiTimestamp < pxTraceTimestamp->latestTimestamp)
  46. {
  47. pxTraceTimestamp->wraparounds++;
  48. }
  49. break;
  50. case TRC_FREE_RUNNING_32BIT_DECR:
  51. case TRC_CUSTOM_TIMER_DECR:
  52. *puiTimestamp = (uint32_t)(TRC_HWTC_COUNT);
  53. if (*puiTimestamp > pxTraceTimestamp->latestTimestamp)
  54. {
  55. pxTraceTimestamp->wraparounds++;
  56. }
  57. break;
  58. case TRC_OS_TIMER_INCR:
  59. case TRC_OS_TIMER_DECR:
  60. *puiTimestamp = (((uint32_t)(TRC_HWTC_COUNT)) & 0x00FFFFFFUL) + ((pxTraceTimestamp->osTickCount & 0x000000FFUL) << 24);
  61. pxTraceTimestamp->wraparounds = pxTraceTimestamp->osTickCount;
  62. break;
  63. default:
  64. return TRC_FAIL;
  65. }
  66. pxTraceTimestamp->latestTimestamp = *puiTimestamp;
  67. return TRC_SUCCESS;
  68. }
  69. traceResult xTraceTimestampGetWraparounds(uint32_t* puiTimerWraparounds)
  70. {
  71. /* This should never fail */
  72. TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_TIMESTAMP));
  73. /* This should never fail */
  74. TRC_ASSERT(puiTimerWraparounds != (void*)0);
  75. *puiTimerWraparounds = pxTraceTimestamp->wraparounds;
  76. return TRC_SUCCESS;
  77. }
  78. traceResult xTraceTimestampSetFrequency(TraceUnsignedBaseType_t uxFrequency)
  79. {
  80. /* This should never fail */
  81. TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_TIMESTAMP));
  82. pxTraceTimestamp->frequency = uxFrequency;
  83. return TRC_SUCCESS;
  84. }
  85. traceResult xTraceTimestampSetPeriod(uint32_t uiPeriod)
  86. {
  87. /* This should never fail */
  88. TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_TIMESTAMP));
  89. pxTraceTimestamp->period = uiPeriod;
  90. return TRC_SUCCESS;
  91. }
  92. traceResult xTraceTimestampSetOsTickCount(uint32_t uiOsTickCount)
  93. {
  94. /* This should never fail */
  95. TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_TIMESTAMP));
  96. pxTraceTimestamp->osTickCount = uiOsTickCount;
  97. return TRC_SUCCESS;
  98. }
  99. traceResult xTraceTimestampGetFrequency(TraceUnsignedBaseType_t *puxFrequency)
  100. {
  101. /* This should never fail */
  102. TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_TIMESTAMP));
  103. /* This should never fail */
  104. TRC_ASSERT(puxFrequency != (void*)0);
  105. *puxFrequency = pxTraceTimestamp->frequency;
  106. return TRC_SUCCESS;
  107. }
  108. traceResult xTraceTimestampGetPeriod(uint32_t *puiPeriod)
  109. {
  110. /* This should never fail */
  111. TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_TIMESTAMP));
  112. /* This should never fail */
  113. TRC_ASSERT(puiPeriod != (void*)0);
  114. *puiPeriod = pxTraceTimestamp->period;
  115. return TRC_SUCCESS;
  116. }
  117. traceResult xTraceTimestampGetOsTickCount(uint32_t* puiOsTickCount)
  118. {
  119. /* This should never fail */
  120. TRC_ASSERT(xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_TIMESTAMP));
  121. /* This should never fail */
  122. TRC_ASSERT(puiOsTickCount != (void*)0);
  123. *puiOsTickCount = pxTraceTimestamp->osTickCount;
  124. return TRC_SUCCESS;
  125. }
  126. #endif
  127. #endif