Teensy 4.1 core updated for C++20
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

147 lines
3.9KB

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