Teensy 4.1 core updated for C++20
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

119 行
3.1KB

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