Timing.cpp 4.0 KB

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