Teensy 4.1 core updated for C++20
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

137 linhas
3.6KB

  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. extern uint32_t ccmicros(void);
  57. uint32_t ccmicros(void)
  58. {
  59. uint32_t ccdelta, usec, smc, scc;
  60. do {
  61. smc = systick_millis_count;
  62. scc = systick_cycle_count;
  63. } while ( smc != systick_millis_count || scc != systick_cycle_count ); // repeat if systick_isr
  64. ccdelta = ARM_DWT_CYCCNT - scc;
  65. usec = 1000*smc + (ccdelta/(F_CPU_ACTUAL/1000000));
  66. return usec;
  67. }
  68. //#if 0 // kept to compare test to cycle count micro()
  69. uint32_t micros(void)
  70. {
  71. uint32_t msec, tick, elapsed, istatus, usec;
  72. //static uint32_t prev_msec=0;
  73. //static uint32_t prev_istatus=0;
  74. //static uint32_t prev_tick=0;
  75. //static uint32_t prev_elapsed=0;
  76. static uint32_t prev_usec=0;
  77. static int doprint=180;
  78. __disable_irq();
  79. tick = SYST_CVR;
  80. msec = systick_millis_count;
  81. istatus = SCB_ICSR; // bit 26 indicates if systick exception pending
  82. #ifndef SYSTICK_EXT_FREQ
  83. const uint32_t fcpu = F_CPU;
  84. #endif
  85. __enable_irq();
  86. istatus &= SCB_ICSR_PENDSTSET;
  87. #ifdef SYSTICK_EXT_FREQ
  88. if ((istatus & SCB_ICSR_PENDSTSET) && (tick == 0 || tick > (SYSTICK_EXT_FREQ / 2000))) {
  89. #else
  90. if ((istatus & SCB_ICSR_PENDSTSET) && (tick == 0 || tick > (fcpu / 2000))) {
  91. #endif
  92. // systick generated an interrupt at the 1 -> 0 transition, and
  93. // we read it before an ISR could increment systick_millis_count
  94. msec++;
  95. }
  96. #if defined(SYSTICK_EXT_FREQ) && SYSTICK_EXT_FREQ <= 1000000
  97. elapsed = (SYSTICK_EXT_FREQ / 1000) - tick;
  98. if (tick == 0) elapsed = 0;
  99. usec = msec * 1000 + elapsed * (1000000 / SYSTICK_EXT_FREQ);
  100. #elif defined(SYSTICK_EXT_FREQ) && SYSTICK_EXT_FREQ > 1000000
  101. elapsed = (SYSTICK_EXT_FREQ / 1000) - tick;
  102. if (tick == 0) elapsed = 0;
  103. usec = msec * 1000 + elapsed / (SYSTICK_EXT_FREQ / 1000000);
  104. #else
  105. elapsed = (fcpu / 1000) - tick;
  106. if (tick == 0) elapsed = 0;
  107. usec = msec * 1000 + elapsed / (fcpu / 1000000);
  108. #endif
  109. //if (doprint) printf("%lu %lu\r\n", msec, systick);
  110. if (usec < prev_usec && doprint) {
  111. //print("opps\r\n");
  112. //printf("opps then: msec=%lu, systick=%lu, elapsed=%lu, usec=%lu, i=%lx\n",
  113. //prev_msec, prev_tick, prev_elapsed, prev_usec, prev_istatus);
  114. //printf(" now: msec=%lu, systick=%lu, elapsed=%lu, usec=%lu, i=%lx\n",
  115. //msec, tick, elapsed, usec, istatus);
  116. if (doprint > 0) doprint--;
  117. }
  118. //prev_msec = msec;
  119. //prev_elapsed = elapsed;
  120. //prev_tick = tick;
  121. //prev_istatus = istatus;
  122. prev_usec = usec;
  123. return usec;
  124. }
  125. //#endif