OSCTiming.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MKL26Z64__)
  23. extern volatile uint32_t systick_millis_count;
  24. static uint32_t savedcount, savedcurrent;
  25. static void latchOscTime()
  26. {
  27. uint32_t istatus;
  28. uint32_t count, current;
  29. __disable_irq();
  30. current = SYST_CVR;
  31. count = systick_millis_count;
  32. istatus = SCB_ICSR; // bit 26 indicates if systick exception pending
  33. __enable_irq();
  34. //systick_current = current;
  35. //systick_count = count;
  36. //systick_istatus = istatus & SCB_ICSR_PENDSTSET ? 1 : 0;
  37. if ((istatus & SCB_ICSR_PENDSTSET) && current > 50) count++;
  38. current = ((F_CPU / 1000) - 1) - current;
  39. savedcount=count; savedcurrent=current;
  40. }
  41. static osctime_t computeOscTime()
  42. { //4,294,967,296
  43. osctime_t t;
  44. t.seconds = (( uint64_t)(savedcount/1000)) ;
  45. t.fractionofseconds = ( (uint64_t)(4294967295) * ( (savedcount * 1000 + (uint64_t)savedcurrent / (F_CPU / 1000000UL)) % 1000000) ) /1000000;
  46. return t;
  47. }
  48. osctime_t oscTime()
  49. {
  50. latchOscTime();
  51. return computeOscTime();
  52. }
  53. #elif defined(CORE_TEENSY)
  54. extern volatile uint32_t timer0_millis_count;
  55. static uint32_t savedcount, savedmicros;
  56. static void latchOscTime()
  57. {
  58. noInterrupts();
  59. savedcount = timer0_millis_count;
  60. savedmicros = micros();
  61. interrupts();
  62. }
  63. static osctime_t computeOscTime()
  64. { //4,294,967,296
  65. osctime_t t;
  66. savedmicros %= 1000000;
  67. t.fractionofseconds= (67108864ULL * savedmicros) / 15625 ; // 2^32/1000000
  68. t.seconds = savedcount/1000;
  69. return t;
  70. #ifdef ddfgsdfgsdfgsdfg
  71. return ((savedcount/1000)<<32) + ( (4294967295ULL) * ( (savedcount * 1000ULL + savedmicros) % 1000000ULL) ) /1000000ULL
  72. ;
  73. #endif
  74. }
  75. osctime_t oscTime()
  76. {
  77. latchOscTime();
  78. return computeOscTime();
  79. }
  80. #elif defined(AVR) || defined(__AVR_ATmega32U4__) || defined(__SAM3X8E__) || defined(_SAMD21_) || defined(__ARM__)
  81. static uint32_t savedcount, savedmicros;
  82. static void latchOscTime()
  83. {
  84. noInterrupts();
  85. //cli();
  86. savedcount = millis();
  87. savedmicros = micros();
  88. interrupts();
  89. //sei();
  90. }
  91. osctime_t computeOscTime()
  92. { //4,294,967,296
  93. osctime_t t;
  94. savedmicros %= 1000000UL;
  95. // t.fractionofseconds = (67108864ULL * (uint64_t)savedmicros) / 15625ULL ; // 2^32/1000000
  96. t.fractionofseconds= (67108864UL * savedmicros)/ 15625ULL ; // 2^32/1000000
  97. t.seconds = savedcount/1000;
  98. return t;
  99. }
  100. osctime_t oscTime()
  101. {
  102. latchOscTime();
  103. return computeOscTime();
  104. }
  105. #else
  106. static void latchOscTime()
  107. {
  108. }
  109. osctime_t oscTime()
  110. {
  111. osctime_t t;
  112. t.fractionofseconds = 1;
  113. return t;
  114. }
  115. #endif
  116. int adcRead(int pin, osctime_t *t)
  117. {
  118. latchOscTime();
  119. int v =analogRead(pin);
  120. *t = oscTime();
  121. return v;
  122. }
  123. #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MKL26Z64__) || defined(__MK66FX1M0__)
  124. int capacitanceRead(int pin, osctime_t *t)
  125. {
  126. latchOscTime();
  127. int v = touchRead(pin);
  128. *t = oscTime();
  129. return v;
  130. }
  131. #endif
  132. int inputRead(int pin, osctime_t *t)
  133. {
  134. int v =digitalRead(pin);
  135. *t = oscTime();
  136. return v;
  137. }