Teensy 4.1 core updated for C++20
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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