main.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /****************************************************************************
  2. * Copyright 2021 Gorgon Meducer (Email:embedded_zhuoran@hotmail.com) *
  3. * *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); *
  5. * you may not use this file except in compliance with the License. *
  6. * You may obtain a copy of the License at *
  7. * *
  8. * http://www.apache.org/licenses/LICENSE-2.0 *
  9. * *
  10. * Unless required by applicable law or agreed to in writing, software *
  11. * distributed under the License is distributed on an "AS IS" BASIS, *
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
  13. * See the License for the specific language governing permissions and *
  14. * limitations under the License. *
  15. * *
  16. ****************************************************************************/
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. #include <cmsis_compiler.h>
  20. #include "perf_counter.h"
  21. #ifndef __PERF_CNT_USE_LONG_CLOCK__
  22. #include <time.h>
  23. #else
  24. typedef int64_t clock_t ;
  25. #endif
  26. extern void systimer_1ms_handler(void);
  27. void systimer_1ms_handler(void)
  28. {
  29. //printf("Running original Systick_Handler...\r\n");
  30. }
  31. typedef struct example_lv1_t {
  32. uint32_t wLV1A;
  33. uint16_t hwLV1B;
  34. uint8_t chLV1C;
  35. uint8_t chLV1D;
  36. }example_lv1_t;
  37. typedef struct example_lv0_t {
  38. uint32_t wA;
  39. uint16_t hwB;
  40. uint8_t chC;
  41. uint8_t chID;
  42. example_lv1_t tLV1;
  43. } example_lv0_t;
  44. static example_lv0_t s_tItem[8] = {
  45. {.chID = 0},
  46. {.chID = 1},
  47. {.chID = 2},
  48. {.chID = 3},
  49. {.chID = 4},
  50. {.chID = 5},
  51. {.chID = 6},
  52. {.chID = 7},
  53. };
  54. #if __IS_COMPILER_ARM_COMPILER__
  55. #if defined(__clang__)
  56. # pragma clang diagnostic push
  57. # pragma clang diagnostic ignored "-Wdollar-in-identifier-extension"
  58. # pragma clang diagnostic ignored "-Wdouble-promotion"
  59. #endif
  60. uint32_t calculate_stack_usage_topdown(void)
  61. {
  62. extern uint32_t Image$$ARM_LIB_STACK$$Limit[];
  63. extern uint32_t Image$$ARM_LIB_STACK$$Length;
  64. uint32_t *pwStack = Image$$ARM_LIB_STACK$$Limit;
  65. uint32_t wStackSize = (uintptr_t)&Image$$ARM_LIB_STACK$$Length / 4;
  66. uint32_t wStackUsed = 0;
  67. do {
  68. if (*--pwStack == 0xDEADBEEF) {
  69. break;
  70. }
  71. wStackUsed++;
  72. } while(--wStackSize);
  73. printf("\r\nStack Usage: [%d/%d] %2.2f%%\r\n",
  74. wStackUsed * 4,
  75. (uintptr_t)&Image$$ARM_LIB_STACK$$Length,
  76. ( (float)wStackUsed * 400.0f
  77. / (float)(uintptr_t)&Image$$ARM_LIB_STACK$$Length));
  78. return wStackUsed * 4;
  79. }
  80. uint32_t calculate_stack_usage_bottomup(void)
  81. {
  82. extern uint32_t Image$$ARM_LIB_STACK$$Base[];
  83. extern uint32_t Image$$ARM_LIB_STACK$$Length;
  84. uint32_t *pwStack = Image$$ARM_LIB_STACK$$Base;
  85. uint32_t wStackSize = (uintptr_t)&Image$$ARM_LIB_STACK$$Length;
  86. uint32_t wStackUsed = wStackSize / 4;
  87. do {
  88. if (*pwStack++ != 0xDEADBEEF) {
  89. break;
  90. }
  91. } while(--wStackUsed);
  92. printf("\r\nStack Usage: [%d/%d] %2.2f%%\r\n",
  93. wStackUsed * 4,
  94. wStackSize,
  95. ( (float)wStackUsed * 400.0f / (float)wStackSize));
  96. return wStackUsed * 4;
  97. }
  98. #if defined(__clang__)
  99. # pragma clang diagnostic pop
  100. #endif
  101. #endif
  102. /*----------------------------------------------------------------------------
  103. Main function
  104. *----------------------------------------------------------------------------*/
  105. int main (void)
  106. {
  107. int32_t iCycleResult = 0;
  108. /*! demo of using() block */
  109. using(int a = 0,printf("========= On Enter =======\r\n"),
  110. printf("========= On Leave =======\r\n")) {
  111. printf("\t In Body a=%d \r\n", ++a);
  112. }
  113. __cycleof__("Calibration") {}
  114. printf("\r\n\r\n\r\n\r\n");
  115. /*! demo of __cycleof__() operation */
  116. __cycleof__() {
  117. foreach(example_lv0_t, s_tItem, ptItem) {
  118. printf("Processing item with ID = %d\r\n", _->chID);
  119. }
  120. }
  121. /* measure cycles and store it in a dedicated variable without printf */
  122. __cycleof__("delay_us(1000ul)",
  123. /* insert code to __cycleof__ body, "{}" can be omitted */
  124. {
  125. iCycleResult = __cycle_count__; /*< "__cycle_count__" stores the result */
  126. }) {
  127. delay_us(1000ul);
  128. }
  129. printf("\r\n delay_us(1000ul) takes %d cycles\r\n", (int)iCycleResult);
  130. /*! demo of with block */
  131. with(example_lv0_t, &s_tItem[0], pitem) {
  132. _->wA = 1;
  133. _->hwB = 2;
  134. _->chC = 3;
  135. with(example_lv1_t, &pitem->tLV1) {
  136. _->wLV1A = 4;
  137. _->hwLV1B = 5;
  138. _->chLV1C = 6;
  139. }
  140. }
  141. //! demo of using clock() in timer.h
  142. do {
  143. int64_t tStart = get_system_ticks();
  144. __IRQ_SAFE {
  145. printf("no interrupt \r\n");
  146. }
  147. printf("used clock cycle: %d", (int32_t)(get_system_ticks() - tStart));
  148. } while(0);
  149. #if __IS_COMPILER_ARM_COMPILER__
  150. calculate_stack_usage_topdown();
  151. calculate_stack_usage_bottomup();
  152. #endif
  153. #ifdef __PERF_COUNTER_COREMARK__
  154. coremark_main();
  155. #endif
  156. while (1) {
  157. if (perfc_is_time_out_ms(1000)) {
  158. printf("\r[%010d]", get_system_ms());
  159. }
  160. __cpu_time__(10) {
  161. delay_us(30000);
  162. }
  163. delay_us(70000);
  164. }
  165. }