Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

před 8 roky
před 8 roky
před 8 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #include "core_pins.h"
  31. //#include "HardwareSerial.h"
  32. static uint8_t calibrating;
  33. static uint8_t analog_right_shift = 0;
  34. static uint8_t analog_config_bits = 10;
  35. static uint8_t analog_num_average = 4;
  36. static uint8_t analog_reference_internal = 0;
  37. // the alternate clock is connected to OSCERCLK (16 MHz).
  38. // datasheet says ADC clock should be 2 to 12 MHz for 16 bit mode
  39. // datasheet says ADC clock should be 1 to 18 MHz for 8-12 bit mode
  40. #if F_BUS == 120000000
  41. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(3) + ADC_CFG1_ADICLK(1) // 7.5 MHz
  42. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1) // 15 MHz
  43. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1) // 15 MHz
  44. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1) // 15 MHz
  45. #elif F_BUS == 108000000
  46. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(3) + ADC_CFG1_ADICLK(1) // 7 MHz
  47. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1) // 14 MHz
  48. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1) // 14 MHz
  49. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1) // 14 MHz
  50. #elif F_BUS == 96000000
  51. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1) // 12 MHz
  52. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1) // 12 MHz
  53. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1) // 12 MHz
  54. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 24 MHz
  55. #elif F_BUS == 90000000
  56. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1) // 11.25 MHz
  57. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1) // 11.25 MHz
  58. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1) // 11.25 MHz
  59. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 22.5 MHz
  60. #elif F_BUS == 80000000
  61. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1) // 10 MHz
  62. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1) // 10 MHz
  63. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1) // 10 MHz
  64. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 20 MHz
  65. #elif F_BUS == 72000000
  66. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1) // 9 MHz
  67. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 18 MHz
  68. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 18 MHz
  69. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 18 MHz
  70. #elif F_BUS == 64000000
  71. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1) // 8 MHz
  72. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 16 MHz
  73. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 16 MHz
  74. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 16 MHz
  75. #elif F_BUS == 60000000
  76. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1) // 7.5 MHz
  77. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 15 MHz
  78. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 15 MHz
  79. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 15 MHz
  80. #elif F_BUS == 56000000 || F_BUS == 54000000
  81. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1) // 7 MHz
  82. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 14 MHz
  83. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 14 MHz
  84. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 14 MHz
  85. #elif F_BUS == 48000000
  86. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 12 MHz
  87. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 12 MHz
  88. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 12 MHz
  89. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(1) // 24 MHz
  90. #elif F_BUS == 40000000
  91. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 10 MHz
  92. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 10 MHz
  93. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 10 MHz
  94. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(1) // 20 MHz
  95. #elif F_BUS == 36000000
  96. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 9 MHz
  97. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(1) // 18 MHz
  98. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(1) // 18 MHz
  99. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(1) // 18 MHz
  100. #elif F_BUS == 24000000
  101. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(0) // 12 MHz
  102. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(0) // 12 MHz
  103. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(0) // 12 MHz
  104. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 24 MHz
  105. #elif F_BUS == 16000000
  106. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 8 MHz
  107. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 8 MHz
  108. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 8 MHz
  109. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 16 MHz
  110. #elif F_BUS == 8000000
  111. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 8 MHz
  112. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 8 MHz
  113. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 8 MHz
  114. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 8 MHz
  115. #elif F_BUS == 4000000
  116. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 4 MHz
  117. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 4 MHz
  118. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 4 MHz
  119. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 4 MHz
  120. #elif F_BUS == 2000000
  121. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 2 MHz
  122. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 2 MHz
  123. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 2 MHz
  124. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 2 MHz
  125. #else
  126. #error "F_BUS must be 120, 108, 96, 90, 80, 72, 64, 60, 56, 54, 48, 40, 36, 24, 4 or 2 MHz"
  127. #endif
  128. void analog_init(void)
  129. {
  130. uint32_t num;
  131. #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  132. VREF_TRM = 0x60;
  133. VREF_SC = 0xE1; // enable 1.2 volt ref
  134. #endif
  135. if (analog_config_bits == 8) {
  136. ADC0_CFG1 = ADC_CFG1_8BIT + ADC_CFG1_MODE(0);
  137. ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(3);
  138. #ifdef HAS_KINETIS_ADC1
  139. ADC1_CFG1 = ADC_CFG1_8BIT + ADC_CFG1_MODE(0);
  140. ADC1_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(3);
  141. #endif
  142. } else if (analog_config_bits == 10) {
  143. ADC0_CFG1 = ADC_CFG1_10BIT + ADC_CFG1_MODE(2) + ADC_CFG1_ADLSMP;
  144. ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(3);
  145. #ifdef HAS_KINETIS_ADC1
  146. ADC1_CFG1 = ADC_CFG1_10BIT + ADC_CFG1_MODE(2) + ADC_CFG1_ADLSMP;
  147. ADC1_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(3);
  148. #endif
  149. } else if (analog_config_bits == 12) {
  150. ADC0_CFG1 = ADC_CFG1_12BIT + ADC_CFG1_MODE(1) + ADC_CFG1_ADLSMP;
  151. ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(2);
  152. #ifdef HAS_KINETIS_ADC1
  153. ADC1_CFG1 = ADC_CFG1_12BIT + ADC_CFG1_MODE(1) + ADC_CFG1_ADLSMP;
  154. ADC1_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(2);
  155. #endif
  156. } else {
  157. ADC0_CFG1 = ADC_CFG1_16BIT + ADC_CFG1_MODE(3) + ADC_CFG1_ADLSMP;
  158. ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(2);
  159. #ifdef HAS_KINETIS_ADC1
  160. ADC1_CFG1 = ADC_CFG1_16BIT + ADC_CFG1_MODE(3) + ADC_CFG1_ADLSMP;
  161. ADC1_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(2);
  162. #endif
  163. }
  164. #if defined(__MK20DX128__)
  165. if (analog_reference_internal) {
  166. ADC0_SC2 = ADC_SC2_REFSEL(1); // 1.2V ref
  167. } else {
  168. ADC0_SC2 = ADC_SC2_REFSEL(0); // vcc/ext ref
  169. }
  170. #elif defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  171. if (analog_reference_internal) {
  172. ADC0_SC2 = ADC_SC2_REFSEL(1); // 1.2V ref
  173. ADC1_SC2 = ADC_SC2_REFSEL(1); // 1.2V ref
  174. } else {
  175. ADC0_SC2 = ADC_SC2_REFSEL(0); // vcc/ext ref
  176. ADC1_SC2 = ADC_SC2_REFSEL(0); // vcc/ext ref
  177. }
  178. #elif defined(__MKL26Z64__)
  179. if (analog_reference_internal) {
  180. ADC0_SC2 = ADC_SC2_REFSEL(0); // external AREF
  181. } else {
  182. ADC0_SC2 = ADC_SC2_REFSEL(1); // vcc
  183. }
  184. #endif
  185. num = analog_num_average;
  186. if (num <= 1) {
  187. ADC0_SC3 = ADC_SC3_CAL; // begin cal
  188. #ifdef HAS_KINETIS_ADC1
  189. ADC1_SC3 = ADC_SC3_CAL; // begin cal
  190. #endif
  191. } else if (num <= 4) {
  192. ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(0);
  193. #ifdef HAS_KINETIS_ADC1
  194. ADC1_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(0);
  195. #endif
  196. } else if (num <= 8) {
  197. ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(1);
  198. #ifdef HAS_KINETIS_ADC1
  199. ADC1_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(1);
  200. #endif
  201. } else if (num <= 16) {
  202. ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(2);
  203. #ifdef HAS_KINETIS_ADC1
  204. ADC1_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(2);
  205. #endif
  206. } else {
  207. ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(3);
  208. #ifdef HAS_KINETIS_ADC1
  209. ADC1_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(3);
  210. #endif
  211. }
  212. calibrating = 1;
  213. }
  214. static void wait_for_cal(void)
  215. {
  216. uint16_t sum;
  217. //serial_print("wait_for_cal\n");
  218. #if defined(HAS_KINETIS_ADC0) && defined(HAS_KINETIS_ADC1)
  219. while ((ADC0_SC3 & ADC_SC3_CAL) || (ADC1_SC3 & ADC_SC3_CAL)) {
  220. // wait
  221. }
  222. #elif defined(HAS_KINETIS_ADC0)
  223. while (ADC0_SC3 & ADC_SC3_CAL) {
  224. // wait
  225. }
  226. #endif
  227. __disable_irq();
  228. if (calibrating) {
  229. //serial_print("\n");
  230. sum = ADC0_CLPS + ADC0_CLP4 + ADC0_CLP3 + ADC0_CLP2 + ADC0_CLP1 + ADC0_CLP0;
  231. sum = (sum / 2) | 0x8000;
  232. ADC0_PG = sum;
  233. //serial_print("ADC0_PG = ");
  234. //serial_phex16(sum);
  235. //serial_print("\n");
  236. sum = ADC0_CLMS + ADC0_CLM4 + ADC0_CLM3 + ADC0_CLM2 + ADC0_CLM1 + ADC0_CLM0;
  237. sum = (sum / 2) | 0x8000;
  238. ADC0_MG = sum;
  239. //serial_print("ADC0_MG = ");
  240. //serial_phex16(sum);
  241. //serial_print("\n");
  242. #ifdef HAS_KINETIS_ADC1
  243. sum = ADC1_CLPS + ADC1_CLP4 + ADC1_CLP3 + ADC1_CLP2 + ADC1_CLP1 + ADC1_CLP0;
  244. sum = (sum / 2) | 0x8000;
  245. ADC1_PG = sum;
  246. sum = ADC1_CLMS + ADC1_CLM4 + ADC1_CLM3 + ADC1_CLM2 + ADC1_CLM1 + ADC1_CLM0;
  247. sum = (sum / 2) | 0x8000;
  248. ADC1_MG = sum;
  249. #endif
  250. calibrating = 0;
  251. }
  252. __enable_irq();
  253. }
  254. // ADCx_SC2[REFSEL] bit selects the voltage reference sources for ADC.
  255. // VREFH/VREFL - connected as the primary reference option
  256. // 1.2 V VREF_OUT - connected as the VALT reference option
  257. #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  258. #define DEFAULT 0
  259. #define INTERNAL 2
  260. #define INTERNAL1V2 2
  261. #define INTERNAL1V1 2
  262. #define EXTERNAL 0
  263. #elif defined(__MKL26Z64__)
  264. #define DEFAULT 0
  265. #define INTERNAL 0
  266. #define EXTERNAL 1
  267. #endif
  268. void analogReference(uint8_t type)
  269. {
  270. if (type) {
  271. // internal reference requested
  272. if (!analog_reference_internal) {
  273. analog_reference_internal = 1;
  274. if (calibrating) {
  275. ADC0_SC3 = 0; // cancel cal
  276. #ifdef HAS_KINETIS_ADC1
  277. ADC1_SC3 = 0; // cancel cal
  278. #endif
  279. }
  280. analog_init();
  281. }
  282. } else {
  283. // vcc or external reference requested
  284. if (analog_reference_internal) {
  285. analog_reference_internal = 0;
  286. if (calibrating) {
  287. ADC0_SC3 = 0; // cancel cal
  288. #ifdef HAS_KINETIS_ADC1
  289. ADC1_SC3 = 0; // cancel cal
  290. #endif
  291. }
  292. analog_init();
  293. }
  294. }
  295. }
  296. void analogReadRes(unsigned int bits)
  297. {
  298. unsigned int config;
  299. if (bits >= 13) {
  300. if (bits > 16) bits = 16;
  301. config = 16;
  302. } else if (bits >= 11) {
  303. config = 12;
  304. } else if (bits >= 9) {
  305. config = 10;
  306. } else {
  307. config = 8;
  308. }
  309. analog_right_shift = config - bits;
  310. if (config != analog_config_bits) {
  311. analog_config_bits = config;
  312. if (calibrating) ADC0_SC3 = 0; // cancel cal
  313. analog_init();
  314. }
  315. }
  316. void analogReadAveraging(unsigned int num)
  317. {
  318. if (calibrating) wait_for_cal();
  319. if (num <= 1) {
  320. num = 0;
  321. ADC0_SC3 = 0;
  322. } else if (num <= 4) {
  323. num = 4;
  324. ADC0_SC3 = ADC_SC3_AVGE + ADC_SC3_AVGS(0);
  325. } else if (num <= 8) {
  326. num = 8;
  327. ADC0_SC3 = ADC_SC3_AVGE + ADC_SC3_AVGS(1);
  328. } else if (num <= 16) {
  329. num = 16;
  330. ADC0_SC3 = ADC_SC3_AVGE + ADC_SC3_AVGS(2);
  331. } else {
  332. num = 32;
  333. ADC0_SC3 = ADC_SC3_AVGE + ADC_SC3_AVGS(3);
  334. }
  335. analog_num_average = num;
  336. }
  337. // The SC1A register is used for both software and hardware trigger modes of operation.
  338. #if defined(__MK20DX128__)
  339. static const uint8_t pin2sc1a[] = {
  340. 5, 14, 8, 9, 13, 12, 6, 7, 15, 4, 0, 19, 3, 21, // 0-13 -> A0-A13
  341. 5, 14, 8, 9, 13, 12, 6, 7, 15, 4, // 14-23 are A0-A9
  342. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // 24-33 are digital only
  343. 0, 19, 3, 21, // 34-37 are A10-A13
  344. 26, // 38 is temp sensor
  345. 22, // 39 is vref
  346. 23 // 40 is unused analog pin
  347. };
  348. #elif defined(__MK20DX256__)
  349. static const uint8_t pin2sc1a[] = {
  350. 5, 14, 8, 9, 13, 12, 6, 7, 15, 4, 0, 19, 3, 19+128, // 0-13 -> A0-A13
  351. 5, 14, 8, 9, 13, 12, 6, 7, 15, 4, // 14-23 are A0-A9
  352. 255, 255, // 24-25 are digital only
  353. 5+192, 5+128, 4+128, 6+128, 7+128, 4+192, // 26-31 are A15-A20
  354. 255, 255, // 32-33 are digital only
  355. 0, 19, 3, 19+128, // 34-37 are A10-A13
  356. 26, // 38 is temp sensor,
  357. 18+128, // 39 is vref
  358. 23 // 40 is A14
  359. };
  360. #elif defined(__MKL26Z64__)
  361. static const uint8_t pin2sc1a[] = {
  362. 5, 14, 8, 9, 13, 12, 6, 7, 15, 11, 0, 4+64, 23, // 0-12 -> A0-A12
  363. 255, // 13 is digital only (no A13 alias)
  364. 5, 14, 8, 9, 13, 12, 6, 7, 15, 11, 0, 4+64, 23, // 14-26 are A0-A12
  365. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // 27-37 unused
  366. 26, // 38=temperature
  367. 27 // 39=bandgap ref (PMC_REGSC |= PMC_REGSC_BGBE)
  368. };
  369. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  370. static const uint8_t pin2sc1a[] = {
  371. 5, 14, 8, 9, 13, 12, 6, 7, 15, 4, 3, 19+128, 14+128, 15+128, // 0-13 -> A0-A13
  372. 5, 14, 8, 9, 13, 12, 6, 7, 15, 4, // 14-23 are A0-A9
  373. 255, 255, 255, 255, 255, 255, 255, // 24-30 are digital only
  374. 14+128, 15+128, 17, 18, 4+128, 5+128, 6+128, 7+128, 17+128, // 31-39 are A12-A20
  375. 255, 255, 255, 255, 255, 255, 255, 255, 255, // 40-48 are digital only
  376. 10+128, 11+128, // 49-50 are A23-A24
  377. 255, 255, 255, 255, 255, 255, 255, // 51-57 are digital only
  378. 255, 255, 255, 255, 255, 255, // 58-63 (sd card pins) are digital only
  379. 3, 19+128, // 64-65 are A10-A11
  380. 23, 23+128,// 66-67 are A21-A22 (DAC pins)
  381. 1, 1+128, // 68-69 are A25-A26 (unused USB host port on Teensy 3.5)
  382. 26, // 70 is Temperature Sensor
  383. 18+128 // 71 is Vref
  384. };
  385. #endif
  386. // TODO: perhaps this should store the NVIC priority, so it works recursively?
  387. static volatile uint8_t analogReadBusyADC0 = 0;
  388. #ifdef HAS_KINETIS_ADC1
  389. static volatile uint8_t analogReadBusyADC1 = 0;
  390. #endif
  391. int analogRead(uint8_t pin)
  392. {
  393. int result;
  394. uint8_t channel;
  395. //serial_phex(pin);
  396. //serial_print(" ");
  397. if (pin >= sizeof(pin2sc1a)) return 0;
  398. channel = pin2sc1a[pin];
  399. if (channel == 255) return 0;
  400. if (calibrating) wait_for_cal();
  401. #ifdef HAS_KINETIS_ADC1
  402. if (channel & 0x80) goto beginADC1;
  403. #endif
  404. __disable_irq();
  405. startADC0:
  406. //serial_print("startADC0\n");
  407. #if defined(__MKL26Z64__)
  408. if (channel & 0x40) {
  409. ADC0_CFG2 &= ~ADC_CFG2_MUXSEL;
  410. channel &= 0x3F;
  411. } else {
  412. ADC0_CFG2 |= ADC_CFG2_MUXSEL;
  413. }
  414. #endif
  415. ADC0_SC1A = channel;
  416. analogReadBusyADC0 = 1;
  417. __enable_irq();
  418. while (1) {
  419. __disable_irq();
  420. if ((ADC0_SC1A & ADC_SC1_COCO)) {
  421. result = ADC0_RA;
  422. analogReadBusyADC0 = 0;
  423. __enable_irq();
  424. result >>= analog_right_shift;
  425. return result;
  426. }
  427. // detect if analogRead was used from an interrupt
  428. // if so, our analogRead got canceled, so it must
  429. // be restarted.
  430. if (!analogReadBusyADC0) goto startADC0;
  431. __enable_irq();
  432. yield();
  433. }
  434. #ifdef HAS_KINETIS_ADC1
  435. beginADC1:
  436. __disable_irq();
  437. startADC1:
  438. //serial_print("startADC1\n");
  439. // ADC1_CFG2[MUXSEL] bit selects between ADCx_SEn channels a and b.
  440. if (channel & 0x40) {
  441. ADC1_CFG2 &= ~ADC_CFG2_MUXSEL;
  442. } else {
  443. ADC1_CFG2 |= ADC_CFG2_MUXSEL;
  444. }
  445. ADC1_SC1A = channel & 0x3F;
  446. analogReadBusyADC1 = 1;
  447. __enable_irq();
  448. while (1) {
  449. __disable_irq();
  450. if ((ADC1_SC1A & ADC_SC1_COCO)) {
  451. result = ADC1_RA;
  452. analogReadBusyADC1 = 0;
  453. __enable_irq();
  454. result >>= analog_right_shift;
  455. return result;
  456. }
  457. // detect if analogRead was used from an interrupt
  458. // if so, our analogRead got canceled, so it must
  459. // be restarted.
  460. if (!analogReadBusyADC1) goto startADC1;
  461. __enable_irq();
  462. yield();
  463. }
  464. #endif
  465. }
  466. void analogWriteDAC0(int val)
  467. {
  468. #if defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  469. SIM_SCGC2 |= SIM_SCGC2_DAC0;
  470. if (analog_reference_internal) {
  471. DAC0_C0 = DAC_C0_DACEN; // 1.2V ref is DACREF_1
  472. } else {
  473. DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS; // 3.3V VDDA is DACREF_2
  474. }
  475. if (val < 0) val = 0; // TODO: saturate instruction?
  476. else if (val > 4095) val = 4095;
  477. *(int16_t *)&(DAC0_DAT0L) = val;
  478. #elif defined(__MKL26Z64__)
  479. SIM_SCGC6 |= SIM_SCGC6_DAC0;
  480. if (analog_reference_internal == 0) {
  481. // use 3.3V VDDA power as the reference (this is the default)
  482. DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS | DAC_C0_DACSWTRG; // 3.3V VDDA
  483. } else {
  484. // use whatever voltage is on the AREF pin
  485. DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACSWTRG; // 3.3V VDDA
  486. }
  487. if (val < 0) val = 0;
  488. else if (val > 4095) val = 4095;
  489. *(int16_t *)&(DAC0_DAT0L) = val;
  490. #endif
  491. }
  492. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  493. void analogWriteDAC1(int val)
  494. {
  495. SIM_SCGC2 |= SIM_SCGC2_DAC1;
  496. if (analog_reference_internal) {
  497. DAC1_C0 = DAC_C0_DACEN; // 1.2V ref is DACREF_1
  498. } else {
  499. DAC1_C0 = DAC_C0_DACEN | DAC_C0_DACRFS; // 3.3V VDDA is DACREF_2
  500. }
  501. if (val < 0) val = 0; // TODO: saturate instruction?
  502. else if (val > 4095) val = 4095;
  503. *(int16_t *)&(DAC1_DAT0L) = val;
  504. }
  505. #endif