Timing.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "Timing.h"
  2. #ifdef CORTEXM
  3. #define SYSTICK_INITIAL_VALUE 0xFFFFFF
  4. static uint32_t startCycles=0;
  5. #if defined ARMCM0
  6. #include "ARMCM0.h"
  7. #elif defined ARMCM0P
  8. #include "ARMCM0plus.h"
  9. #elif defined ARMCM0P_MPU
  10. #include "ARMCM0plus_MPU.h"
  11. #elif defined ARMCM3
  12. #include "ARMCM3.h"
  13. #elif defined ARMCM4
  14. #include "ARMCM4.h"
  15. #elif defined ARMCM4_FP
  16. #include "ARMCM4_FP.h"
  17. #elif defined ARMCM7
  18. #include "ARMCM7.h"
  19. #elif defined ARMCM7_SP
  20. #include "ARMCM7_SP.h"
  21. #elif defined ARMCM7_DP
  22. #include "ARMCM7_DP.h"
  23. #elif defined (ARMCM33_DSP_FP)
  24. #include "ARMCM33_DSP_FP.h"
  25. #elif defined (ARMCM33_DSP_FP_TZ)
  26. #include "ARMCM33_DSP_FP_TZ.h"
  27. #elif defined ARMSC000
  28. #include "ARMSC000.h"
  29. #elif defined ARMSC300
  30. #include "ARMSC300.h"
  31. #elif defined ARMv8MBL
  32. #include "ARMv8MBL.h"
  33. #elif defined ARMv8MML
  34. #include "ARMv8MML.h"
  35. #elif defined ARMv8MML_DSP
  36. #include "ARMv8MML_DSP.h"
  37. #elif defined ARMv8MML_SP
  38. #include "ARMv8MML_SP.h"
  39. #elif defined ARMv8MML_DSP_SP
  40. #include "ARMv8MML_DSP_SP.h"
  41. #elif defined ARMv8MML_DP
  42. #include "ARMv8MML_DP.h"
  43. #elif defined ARMv8MML_DSP_DP
  44. #include "ARMv8MML_DSP_DP.h"
  45. #elif defined ARMv81MML_DSP_DP_MVE_FP
  46. #include "ARMv81MML_DSP_DP_MVE_FP.h"
  47. #elif defined ARMv7A
  48. /* TODO */
  49. #else
  50. #warning "no appropriate header file found!"
  51. #endif
  52. #endif /* CORTEXM*/
  53. #if defined(CORTEXA) || defined(CORTEXR)
  54. #include "cmsis_cp15.h"
  55. unsigned int startCycles;
  56. #define DO_RESET 1
  57. #define ENABLE_DIVIDER 0
  58. #endif
  59. #if defined(EXTBENCH) || defined(CACHEANALYSIS)
  60. unsigned long sectionCounter=0;
  61. #endif
  62. void initCycleMeasurement()
  63. {
  64. #ifdef CORTEXM
  65. SysTick->LOAD = SYSTICK_INITIAL_VALUE;
  66. SysTick->VAL = 0;
  67. SysTick->CTRL = 0;
  68. #endif
  69. #if defined(CORTEXA) || defined(CORTEXR)
  70. // in general enable all counters (including cycle counter)
  71. int32_t value = 1;
  72. // peform reset:
  73. if (DO_RESET)
  74. {
  75. value |= 2; // reset all counters to zero.
  76. value |= 4; // reset cycle counter to zero.
  77. }
  78. if (ENABLE_DIVIDER)
  79. value |= 8; // enable "by 64" divider for CCNT.
  80. //value |= 16;
  81. // program the performance-counter control-register:
  82. __set_CP(15, 0, value, 9, 12, 0);
  83. // enable all counters:
  84. __set_CP(15, 0, 0x8000000f, 9, 12, 1);
  85. // clear overflows:
  86. __set_CP(15, 0, 0x8000000f, 9, 12, 3);
  87. #if defined(ARMCR52)
  88. __get_CP(15, 0, value, 14, 15, 7);
  89. value = value | (0x8000 << 12);
  90. __set_CP(15, 0, value, 14, 15, 7);
  91. #endif
  92. #endif
  93. }
  94. void cycleMeasurementStart()
  95. {
  96. #ifndef EXTBENCH
  97. #ifdef CORTEXM
  98. SysTick->CTRL = 0;
  99. SysTick->LOAD = SYSTICK_INITIAL_VALUE;
  100. SysTick->VAL = 0;
  101. SysTick->CTRL = SysTick_CTRL_ENABLE_Msk | SysTick_CTRL_CLKSOURCE_Msk;
  102. while(SysTick->VAL == 0);
  103. startCycles = SysTick->VAL;
  104. #endif
  105. #if defined(CORTEXA) || defined(CORTEXR)
  106. unsigned int value;
  107. // Read CCNT Register
  108. __get_CP(15, 0, value, 9, 13, 0);
  109. startCycles = value;
  110. #endif
  111. #endif
  112. }
  113. void cycleMeasurementStop()
  114. {
  115. #ifndef EXTBENCH
  116. #ifdef CORTEXM
  117. SysTick->CTRL = 0;
  118. SysTick->LOAD = SYSTICK_INITIAL_VALUE;
  119. #endif
  120. #endif
  121. }
  122. Testing::cycles_t getCycles()
  123. {
  124. #ifdef CORTEXM
  125. uint32_t v = SysTick->VAL;
  126. Testing::cycles_t result;
  127. if (v < startCycles)
  128. {
  129. result = startCycles - v;
  130. }
  131. else
  132. {
  133. result = SYSTICK_INITIAL_VALUE - (v - startCycles);
  134. }
  135. /* SysTick tested and tuned on IPSS.
  136. On other FVP, the value is forced to 0
  137. because measurement is wrong.
  138. */
  139. #if defined(NORMALFVP)
  140. return(0);
  141. #else
  142. return(result);
  143. #endif
  144. #endif
  145. #if defined(CORTEXA) || defined(CORTEXR)
  146. unsigned int value;
  147. // Read CCNT Register
  148. __get_CP(15, 0, value, 9, 13, 0);
  149. return(value - startCycles);
  150. #endif
  151. }