You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

delay.c 3.8KB

5 jaren geleden
5 jaren geleden
5 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include "core_pins.h"
  2. #include "arm_math.h" // micros() synchronization
  3. //volatile uint32_t F_CPU = 396000000;
  4. //volatile uint32_t F_BUS = 132000000;
  5. volatile uint32_t systick_millis_count = 0;
  6. volatile uint32_t systick_cycle_count = 0;
  7. volatile uint32_t scale_cpu_cycles_to_microseconds = 0;
  8. uint32_t systick_safe_read; // micros() synchronization
  9. //The 24 MHz XTALOSC can be the external clock source of SYSTICK.
  10. //Hardware devides this down to 100KHz. (RM Rev2, 13.3.21 PG 986)
  11. #define SYSTICK_EXT_FREQ 100000
  12. #if 0
  13. // moved to EventResponder.cpp
  14. void systick_isr(void)
  15. {
  16. systick_millis_count++;
  17. // MillisTimer::runFromTimer();
  18. //digitalWriteFast(12, HIGH);
  19. //delayMicroseconds(1);
  20. //digitalWriteFast(12, LOW);
  21. }
  22. #endif
  23. #if 0
  24. void millis_init(void)
  25. {
  26. //printf("millis_init %08lX\n", SYST_CALIB);
  27. _VectorsRam[15] = systick_isr;
  28. #ifdef SYSTICK_EXT_FREQ
  29. SYST_RVR = (SYSTICK_EXT_FREQ / 1000) - 1;
  30. SYST_CVR = 0;
  31. SYST_CSR = SYST_CSR_TICKINT | SYST_CSR_ENABLE;
  32. #else
  33. SYST_RVR = (F_CPU / 1000) - 1;
  34. SYST_CVR = 0;
  35. SYST_CSR = SYST_CSR_CLKSOURCE | SYST_CSR_TICKINT | SYST_CSR_ENABLE;
  36. #endif
  37. //SCB_SHPR3 = 0x20200000; // Systick = priority 32
  38. //printf("RVR=%lu\r\n", SYST_RVR);
  39. }
  40. #endif
  41. /*void yield(void)
  42. {
  43. }*/
  44. void delay(uint32_t msec)
  45. {
  46. uint32_t start;
  47. if (msec == 0) return;
  48. start = micros();
  49. while (1) {
  50. while ((micros() - start) >= 1000) {
  51. if (--msec == 0) return;
  52. start += 1000;
  53. }
  54. yield();
  55. }
  56. // TODO...
  57. }
  58. uint32_t micros(void)
  59. {
  60. uint32_t smc, scc;
  61. do {
  62. __LDREXW(&systick_safe_read);
  63. smc = systick_millis_count;
  64. scc = systick_cycle_count;
  65. } while ( __STREXW(1, &systick_safe_read));
  66. uint32_t cyccnt = ARM_DWT_CYCCNT;
  67. asm volatile("" : : : "memory");
  68. uint32_t ccdelta = cyccnt - scc;
  69. uint32_t frac = ((uint64_t)ccdelta * scale_cpu_cycles_to_microseconds) >> 32;
  70. if (frac > 1000) frac = 1000;
  71. uint32_t usec = 1000*smc + frac;
  72. return usec;
  73. }
  74. #if 0 // kept to compare test to cycle count micro()
  75. uint32_t micros(void)
  76. {
  77. uint32_t msec, tick, elapsed, istatus, usec;
  78. //static uint32_t prev_msec=0;
  79. //static uint32_t prev_istatus=0;
  80. //static uint32_t prev_tick=0;
  81. //static uint32_t prev_elapsed=0;
  82. static uint32_t prev_usec=0;
  83. static int doprint=180;
  84. __disable_irq();
  85. tick = SYST_CVR;
  86. msec = systick_millis_count;
  87. istatus = SCB_ICSR; // bit 26 indicates if systick exception pending
  88. #ifndef SYSTICK_EXT_FREQ
  89. const uint32_t fcpu = F_CPU;
  90. #endif
  91. __enable_irq();
  92. istatus &= SCB_ICSR_PENDSTSET;
  93. #ifdef SYSTICK_EXT_FREQ
  94. if ((istatus & SCB_ICSR_PENDSTSET) && (tick == 0 || tick > (SYSTICK_EXT_FREQ / 2000))) {
  95. #else
  96. if ((istatus & SCB_ICSR_PENDSTSET) && (tick == 0 || tick > (fcpu / 2000))) {
  97. #endif
  98. // systick generated an interrupt at the 1 -> 0 transition, and
  99. // we read it before an ISR could increment systick_millis_count
  100. msec++;
  101. }
  102. #if defined(SYSTICK_EXT_FREQ) && SYSTICK_EXT_FREQ <= 1000000
  103. elapsed = (SYSTICK_EXT_FREQ / 1000) - tick;
  104. if (tick == 0) elapsed = 0;
  105. usec = msec * 1000 + elapsed * (1000000 / SYSTICK_EXT_FREQ);
  106. #elif defined(SYSTICK_EXT_FREQ) && SYSTICK_EXT_FREQ > 1000000
  107. elapsed = (SYSTICK_EXT_FREQ / 1000) - tick;
  108. if (tick == 0) elapsed = 0;
  109. usec = msec * 1000 + elapsed / (SYSTICK_EXT_FREQ / 1000000);
  110. #else
  111. elapsed = (fcpu / 1000) - tick;
  112. if (tick == 0) elapsed = 0;
  113. usec = msec * 1000 + elapsed / (fcpu / 1000000);
  114. #endif
  115. //if (doprint) printf("%lu %lu\r\n", msec, systick);
  116. if (usec < prev_usec && doprint) {
  117. //print("opps\r\n");
  118. //printf("opps then: msec=%lu, systick=%lu, elapsed=%lu, usec=%lu, i=%lx\n",
  119. //prev_msec, prev_tick, prev_elapsed, prev_usec, prev_istatus);
  120. //printf(" now: msec=%lu, systick=%lu, elapsed=%lu, usec=%lu, i=%lx\n",
  121. //msec, tick, elapsed, usec, istatus);
  122. if (doprint > 0) doprint--;
  123. }
  124. //prev_msec = msec;
  125. //prev_elapsed = elapsed;
  126. //prev_tick = tick;
  127. //prev_istatus = istatus;
  128. prev_usec = usec;
  129. return usec;
  130. }
  131. #endif