OSCBoards.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 <Arduino.h>
  22. #include "OSCBoards.h"
  23. #ifndef analogInputToDigitalPin
  24. int analogInputToDigitalPin(int i)
  25. {
  26. switch(i)
  27. {
  28. case 0: return A0;
  29. case 1: return A1;
  30. case 2: return A2;
  31. case 3: return A3;
  32. case 4: return A4;
  33. case 5: return A5;
  34. #ifdef A6
  35. case 6: return A6;
  36. #endif
  37. #ifdef A7
  38. case 7: return A7;
  39. #endif
  40. #ifdef A8
  41. case 8: return A8;
  42. #endif
  43. #ifdef A9
  44. case 9: return A9;
  45. #endif
  46. #ifdef A10
  47. case 10: return A10;
  48. #endif
  49. #ifdef A11
  50. case 11: return A11;
  51. #endif
  52. #ifdef A12
  53. case 12: return A12;
  54. #endif
  55. #ifdef A13
  56. case 13: return A13;
  57. #endif
  58. #ifdef A14
  59. case 14: return A14;
  60. #endif
  61. #ifdef A15
  62. case 15: return A15;
  63. #endif
  64. #ifdef A16
  65. case 16: return A16;
  66. #endif
  67. }
  68. return -1;
  69. }
  70. #endif
  71. #ifdef BOARD_HAS_DIE_POWER_SUPPLY_MEASUREMENT
  72. #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MKL26Z64__) || defined(__MK64FX512V__) || defined(__MK66FX1M0__)
  73. float getSupplyVoltage()
  74. {
  75. analogReference(DEFAULT);
  76. analogReadResolution(12);
  77. analogReadAveraging(32);
  78. PMC_REGSC |= PMC_REGSC_BGBE; // 39=bandgap ref (PMC_REGSC |= PMC_REGSC_BGBE);
  79. delay(1);
  80. #if defined(__MKL26Z64__)
  81. // Teensy 3 LC
  82. int val = analogRead(39);
  83. return val>0? (1.0f*4095/val):0.0f;
  84. #elif defined(__MK64FX512V__) || defined(__MK66FX1M0__)
  85. int val = analogRead(71);
  86. return val>0? (1.195f*4095/val):0.0f;
  87. #else
  88. int val = analogRead(39);
  89. return val>0? (1.195f*4095/val):0.0f;
  90. #endif
  91. }
  92. #else
  93. // power supply measurement on some Arduinos
  94. float getSupplyVoltage(){
  95. // powersupply
  96. int result;
  97. // Read 1.1V reference against AVcc
  98. #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
  99. ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  100. #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
  101. ADMUX = _BV(MUX5) | _BV(MUX0);
  102. #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
  103. ADMUX = _BV(MUX3) | _BV(MUX2);
  104. #elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__) || defined(__AVR_ATmega1280__)
  105. ADMUX = 0x40| _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1) ;
  106. ADCSRB = 0;
  107. #else
  108. ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  109. #endif
  110. delayMicroseconds(300); // wait for Vref to settle
  111. ADCSRA |= _BV(ADSC); // Convert
  112. while (bit_is_set(ADCSRA,ADSC));
  113. result = ADCL;
  114. result |= ADCH<<8;
  115. float supplyvoltage = 1.1264f *1023 / result;
  116. return supplyvoltage;
  117. }
  118. #endif
  119. #endif
  120. #ifdef BOARD_HAS_DIE_TEMPERATURE_SENSOR
  121. #if defined(__MK20DX128__) || defined(__MK20DX256__)|| defined(__MKL26Z64__) || defined(__MK66FX1M0__) || defined(__MK64FX512V__)
  122. float getTemperature()
  123. {
  124. #if defined(__MK64FX512V__) || defined(__MK66FX1M0__)
  125. const int temppin = 70 ;
  126. #else
  127. const int temppin = 38;
  128. #endif
  129. // untested on all teensy 3.x
  130. analogReference(INTERNAL);
  131. analogReadResolution(12);
  132. analogReadAveraging(32);
  133. delay(2);
  134. float val = 25.0 + 0.17083 * (2454.19 - analogRead(temppin));
  135. analogReference(DEFAULT);
  136. return val;
  137. }
  138. #else
  139. // temperature
  140. float getTemperature(){
  141. int result;
  142. #if defined(__AVR_ATmega32U4__)
  143. ADMUX = _BV(REFS1) | _BV(REFS0) | _BV(MUX2) | _BV(MUX1) | _BV(MUX0);
  144. ADCSRB = _BV(MUX5);
  145. #else
  146. ADMUX = _BV(REFS1) | _BV(REFS0) | _BV(MUX3);
  147. #endif
  148. delayMicroseconds(200); // wait for Vref to settle
  149. ADCSRA |= _BV(ADSC); // Convert
  150. while (bit_is_set(ADCSRA,ADSC));
  151. result = ADCL;
  152. result |= ADCH<<8;
  153. analogReference(DEFAULT);
  154. return result/1023.0f;
  155. }
  156. #endif
  157. #endif