PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

181 lines
4.2KB

  1. /*
  2. Written by Adrian Freed, The Center for New Music and Audio Technologies,
  3. University of California, Berkeley. Copyright (c) 2013, The Regents of
  4. the University of California (Regents).
  5. Permission to use, copy, modify, distribute, and distribute modified versions
  6. of this software and its documentation without fee and without a signed
  7. licensing agreement, is hereby granted, provided that the above copyright
  8. notice, this paragraph and the following two paragraphs appear in all copies,
  9. modifications, and distributions.
  10. IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
  11. SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
  12. OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF REGENTS HAS
  13. BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
  17. HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
  18. MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  19. For bug reports and feature requests please email me at yotam@cnmat.berkeley.edu
  20. */
  21. #include "OSCTiming.h"
  22. #include "OSCBoards.h"
  23. #if defined(TEENSYDUINO) && defined(__arm__)
  24. extern volatile uint32_t systick_millis_count;
  25. static uint32_t savedcount, savedcurrent;
  26. static void latchOscTime()
  27. {
  28. uint32_t istatus;
  29. uint32_t count, current;
  30. __disable_irq();
  31. current = SYST_CVR;
  32. count = systick_millis_count;
  33. istatus = SCB_ICSR; // bit 26 indicates if systick exception pending
  34. __enable_irq();
  35. //systick_current = current;
  36. //systick_count = count;
  37. //systick_istatus = istatus & SCB_ICSR_PENDSTSET ? 1 : 0;
  38. if ((istatus & SCB_ICSR_PENDSTSET) && current > 50) count++;
  39. current = ((F_CPU / 1000) - 1) - current;
  40. savedcount=count; savedcurrent=current;
  41. }
  42. static osctime_t computeOscTime()
  43. { //4,294,967,296
  44. osctime_t t;
  45. t.seconds = (( uint64_t)(savedcount/1000)) ;
  46. t.fractionofseconds = ( (uint64_t)(4294967295) * ( (savedcount * 1000 + (uint64_t)savedcurrent / (F_CPU / 1000000UL)) % 1000000) ) /1000000;
  47. return t;
  48. }
  49. osctime_t oscTime()
  50. {
  51. latchOscTime();
  52. return computeOscTime();
  53. }
  54. #elif defined(CORE_TEENSY)
  55. extern volatile uint32_t timer0_millis_count;
  56. static uint32_t savedcount, savedmicros;
  57. static void latchOscTime()
  58. {
  59. noInterrupts();
  60. savedcount = timer0_millis_count;
  61. savedmicros = micros();
  62. interrupts();
  63. }
  64. static osctime_t computeOscTime()
  65. { //4,294,967,296
  66. osctime_t t;
  67. savedmicros %= 1000000;
  68. t.fractionofseconds= (67108864ULL * savedmicros) / 15625 ; // 2^32/1000000
  69. t.seconds = savedcount/1000;
  70. return t;
  71. #ifdef ddfgsdfgsdfgsdfg
  72. return ((savedcount/1000)<<32) + ( (4294967295ULL) * ( (savedcount * 1000ULL + savedmicros) % 1000000ULL) ) /1000000ULL
  73. ;
  74. #endif
  75. }
  76. osctime_t oscTime()
  77. {
  78. latchOscTime();
  79. return computeOscTime();
  80. }
  81. #elif defined(AVR) || defined(__AVR_ATmega32U4__) || defined(__SAM3X8E__)
  82. static uint32_t savedcount, savedmicros;
  83. static void latchOscTime()
  84. {
  85. noInterrupts();
  86. //cli();
  87. savedcount = millis();
  88. savedmicros = micros();
  89. interrupts();
  90. //sei();
  91. }
  92. osctime_t computeOscTime()
  93. { //4,294,967,296
  94. osctime_t t;
  95. savedmicros %= 1000000UL;
  96. // t.fractionofseconds = (67108864ULL * (uint64_t)savedmicros) / 15625ULL ; // 2^32/1000000
  97. t.fractionofseconds= (67108864UL * savedmicros)/ 15625ULL ; // 2^32/1000000
  98. t.seconds = savedcount/1000;
  99. return t;
  100. }
  101. osctime_t oscTime()
  102. {
  103. latchOscTime();
  104. return computeOscTime();
  105. }
  106. #else
  107. static void latchOscTime()
  108. {
  109. }
  110. osctime_t oscTime()
  111. {
  112. osctime_t t;
  113. t.fractionofseconds = 1;
  114. return t;
  115. }
  116. #endif
  117. int adcRead(int pin, osctime_t *t)
  118. {
  119. latchOscTime();
  120. int v =analogRead(pin);
  121. *t = oscTime();
  122. return v;
  123. }
  124. #ifdef BOARD_HAS_CAPACITANCE_SENSING
  125. int capacitanceRead(int pin, osctime_t *t)
  126. {
  127. latchOscTime();
  128. int v = touchRead(pin);
  129. *t = oscTime();
  130. return v;
  131. }
  132. #endif
  133. int inputRead(int pin, osctime_t *t)
  134. {
  135. int v =digitalRead(pin);
  136. *t = oscTime();
  137. return v;
  138. }