Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

578 lines
17KB

  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 == 60000000
  41. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1) // 7.5 MHz
  42. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 15 MHz
  43. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 15 MHz
  44. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 15 MHz
  45. #elif F_BUS == 56000000 || F_BUS == 54000000
  46. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1) // 7 MHz
  47. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 14 MHz
  48. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 14 MHz
  49. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 14 MHz
  50. #elif F_BUS == 48000000
  51. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 12 MHz
  52. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 12 MHz
  53. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 12 MHz
  54. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(1) // 24 MHz
  55. #elif F_BUS == 40000000
  56. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 10 MHz
  57. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 10 MHz
  58. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 10 MHz
  59. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(1) // 20 MHz
  60. #elif F_BUS == 36000000
  61. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1) // 9 MHz
  62. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(1) // 18 MHz
  63. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(1) // 18 MHz
  64. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(1) // 18 MHz
  65. #elif F_BUS == 24000000
  66. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(0) // 12 MHz
  67. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(0) // 12 MHz
  68. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(0) // 12 MHz
  69. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 24 MHz
  70. #elif F_BUS == 16000000
  71. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 8 MHz
  72. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 8 MHz
  73. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 8 MHz
  74. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 16 MHz
  75. #elif F_BUS == 8000000
  76. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 8 MHz
  77. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 8 MHz
  78. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 8 MHz
  79. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 8 MHz
  80. #elif F_BUS == 4000000
  81. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 4 MHz
  82. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 4 MHz
  83. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 4 MHz
  84. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 4 MHz
  85. #elif F_BUS == 2000000
  86. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 2 MHz
  87. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 2 MHz
  88. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 2 MHz
  89. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 2 MHz
  90. #else
  91. #error "F_BUS must be 60, 56, 54, 48, 40, 36, 24, 4 or 2 MHz"
  92. #endif
  93. void analog_init(void)
  94. {
  95. uint32_t num;
  96. #if defined(__MK20DX128__) || defined(__MK20DX256__)
  97. VREF_TRM = 0x60;
  98. VREF_SC = 0xE1; // enable 1.2 volt ref
  99. #endif
  100. if (analog_config_bits == 8) {
  101. ADC0_CFG1 = ADC_CFG1_8BIT + ADC_CFG1_MODE(0);
  102. ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(3);
  103. #ifdef HAS_KINETIS_ADC1
  104. ADC1_CFG1 = ADC_CFG1_8BIT + ADC_CFG1_MODE(0);
  105. ADC1_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(3);
  106. #endif
  107. } else if (analog_config_bits == 10) {
  108. ADC0_CFG1 = ADC_CFG1_10BIT + ADC_CFG1_MODE(2) + ADC_CFG1_ADLSMP;
  109. ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(3);
  110. #ifdef HAS_KINETIS_ADC1
  111. ADC1_CFG1 = ADC_CFG1_10BIT + ADC_CFG1_MODE(2) + ADC_CFG1_ADLSMP;
  112. ADC1_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(3);
  113. #endif
  114. } else if (analog_config_bits == 12) {
  115. ADC0_CFG1 = ADC_CFG1_12BIT + ADC_CFG1_MODE(1) + ADC_CFG1_ADLSMP;
  116. ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(2);
  117. #ifdef HAS_KINETIS_ADC1
  118. ADC1_CFG1 = ADC_CFG1_12BIT + ADC_CFG1_MODE(1) + ADC_CFG1_ADLSMP;
  119. ADC1_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(2);
  120. #endif
  121. } else {
  122. ADC0_CFG1 = ADC_CFG1_16BIT + ADC_CFG1_MODE(3) + ADC_CFG1_ADLSMP;
  123. ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(2);
  124. #ifdef HAS_KINETIS_ADC1
  125. ADC1_CFG1 = ADC_CFG1_16BIT + ADC_CFG1_MODE(3) + ADC_CFG1_ADLSMP;
  126. ADC1_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(2);
  127. #endif
  128. }
  129. #if defined(__MK20DX128__)
  130. if (analog_reference_internal) {
  131. ADC0_SC2 = ADC_SC2_REFSEL(1); // 1.2V ref
  132. } else {
  133. ADC0_SC2 = ADC_SC2_REFSEL(0); // vcc/ext ref
  134. }
  135. #elif defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  136. if (analog_reference_internal) {
  137. ADC0_SC2 = ADC_SC2_REFSEL(1); // 1.2V ref
  138. ADC1_SC2 = ADC_SC2_REFSEL(1); // 1.2V ref
  139. } else {
  140. ADC0_SC2 = ADC_SC2_REFSEL(0); // vcc/ext ref
  141. ADC1_SC2 = ADC_SC2_REFSEL(0); // vcc/ext ref
  142. }
  143. #elif defined(__MKL26Z64__)
  144. if (analog_reference_internal) {
  145. ADC0_SC2 = ADC_SC2_REFSEL(0); // external AREF
  146. } else {
  147. ADC0_SC2 = ADC_SC2_REFSEL(1); // vcc
  148. }
  149. #endif
  150. num = analog_num_average;
  151. if (num <= 1) {
  152. ADC0_SC3 = ADC_SC3_CAL; // begin cal
  153. #ifdef HAS_KINETIS_ADC1
  154. ADC1_SC3 = ADC_SC3_CAL; // begin cal
  155. #endif
  156. } else if (num <= 4) {
  157. ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(0);
  158. #ifdef HAS_KINETIS_ADC1
  159. ADC1_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(0);
  160. #endif
  161. } else if (num <= 8) {
  162. ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(1);
  163. #ifdef HAS_KINETIS_ADC1
  164. ADC1_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(1);
  165. #endif
  166. } else if (num <= 16) {
  167. ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(2);
  168. #ifdef HAS_KINETIS_ADC1
  169. ADC1_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(2);
  170. #endif
  171. } else {
  172. ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(3);
  173. #ifdef HAS_KINETIS_ADC1
  174. ADC1_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(3);
  175. #endif
  176. }
  177. calibrating = 1;
  178. }
  179. static void wait_for_cal(void)
  180. {
  181. uint16_t sum;
  182. //serial_print("wait_for_cal\n");
  183. #if defined(HAS_KINETIS_ADC0) && defined(HAS_KINETIS_ADC1)
  184. while ((ADC0_SC3 & ADC_SC3_CAL) || (ADC1_SC3 & ADC_SC3_CAL)) {
  185. // wait
  186. }
  187. #elif defined(HAS_KINETIS_ADC0)
  188. while (ADC0_SC3 & ADC_SC3_CAL) {
  189. // wait
  190. }
  191. #endif
  192. __disable_irq();
  193. if (calibrating) {
  194. //serial_print("\n");
  195. sum = ADC0_CLPS + ADC0_CLP4 + ADC0_CLP3 + ADC0_CLP2 + ADC0_CLP1 + ADC0_CLP0;
  196. sum = (sum / 2) | 0x8000;
  197. ADC0_PG = sum;
  198. //serial_print("ADC0_PG = ");
  199. //serial_phex16(sum);
  200. //serial_print("\n");
  201. sum = ADC0_CLMS + ADC0_CLM4 + ADC0_CLM3 + ADC0_CLM2 + ADC0_CLM1 + ADC0_CLM0;
  202. sum = (sum / 2) | 0x8000;
  203. ADC0_MG = sum;
  204. //serial_print("ADC0_MG = ");
  205. //serial_phex16(sum);
  206. //serial_print("\n");
  207. #ifdef HAS_KINETIS_ADC1
  208. sum = ADC1_CLPS + ADC1_CLP4 + ADC1_CLP3 + ADC1_CLP2 + ADC1_CLP1 + ADC1_CLP0;
  209. sum = (sum / 2) | 0x8000;
  210. ADC1_PG = sum;
  211. sum = ADC1_CLMS + ADC1_CLM4 + ADC1_CLM3 + ADC1_CLM2 + ADC1_CLM1 + ADC1_CLM0;
  212. sum = (sum / 2) | 0x8000;
  213. ADC1_MG = sum;
  214. #endif
  215. calibrating = 0;
  216. }
  217. __enable_irq();
  218. }
  219. // ADCx_SC2[REFSEL] bit selects the voltage reference sources for ADC.
  220. // VREFH/VREFL - connected as the primary reference option
  221. // 1.2 V VREF_OUT - connected as the VALT reference option
  222. #if defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  223. #define DEFAULT 0
  224. #define INTERNAL 2
  225. #define INTERNAL1V2 2
  226. #define INTERNAL1V1 2
  227. #define EXTERNAL 0
  228. #elif defined(__MKL26Z64__)
  229. #define DEFAULT 0
  230. #define INTERNAL 0
  231. #define EXTERNAL 1
  232. #endif
  233. void analogReference(uint8_t type)
  234. {
  235. if (type) {
  236. // internal reference requested
  237. if (!analog_reference_internal) {
  238. analog_reference_internal = 1;
  239. if (calibrating) {
  240. ADC0_SC3 = 0; // cancel cal
  241. #ifdef HAS_KINETIS_ADC1
  242. ADC1_SC3 = 0; // cancel cal
  243. #endif
  244. }
  245. analog_init();
  246. }
  247. } else {
  248. // vcc or external reference requested
  249. if (analog_reference_internal) {
  250. analog_reference_internal = 0;
  251. if (calibrating) {
  252. ADC0_SC3 = 0; // cancel cal
  253. #ifdef HAS_KINETIS_ADC1
  254. ADC1_SC3 = 0; // cancel cal
  255. #endif
  256. }
  257. analog_init();
  258. }
  259. }
  260. }
  261. void analogReadRes(unsigned int bits)
  262. {
  263. unsigned int config;
  264. if (bits >= 13) {
  265. if (bits > 16) bits = 16;
  266. config = 16;
  267. } else if (bits >= 11) {
  268. config = 12;
  269. } else if (bits >= 9) {
  270. config = 10;
  271. } else {
  272. config = 8;
  273. }
  274. analog_right_shift = config - bits;
  275. if (config != analog_config_bits) {
  276. analog_config_bits = config;
  277. if (calibrating) ADC0_SC3 = 0; // cancel cal
  278. analog_init();
  279. }
  280. }
  281. void analogReadAveraging(unsigned int num)
  282. {
  283. if (calibrating) wait_for_cal();
  284. if (num <= 1) {
  285. num = 0;
  286. ADC0_SC3 = 0;
  287. } else if (num <= 4) {
  288. num = 4;
  289. ADC0_SC3 = ADC_SC3_AVGE + ADC_SC3_AVGS(0);
  290. } else if (num <= 8) {
  291. num = 8;
  292. ADC0_SC3 = ADC_SC3_AVGE + ADC_SC3_AVGS(1);
  293. } else if (num <= 16) {
  294. num = 16;
  295. ADC0_SC3 = ADC_SC3_AVGE + ADC_SC3_AVGS(2);
  296. } else {
  297. num = 32;
  298. ADC0_SC3 = ADC_SC3_AVGE + ADC_SC3_AVGS(3);
  299. }
  300. analog_num_average = num;
  301. }
  302. // The SC1A register is used for both software and hardware trigger modes of operation.
  303. #if defined(__MK20DX128__)
  304. static const uint8_t channel2sc1a[] = {
  305. 5, 14, 8, 9, 13, 12, 6, 7, 15, 4,
  306. 0, 19, 3, 21, 26, 22, 23
  307. };
  308. #elif defined(__MK20DX256__)
  309. static const uint8_t channel2sc1a[] = {
  310. 5, 14, 8, 9, 13, 12, 6, 7, 15, 4,
  311. 0, 19, 3, 19+128, 26, 18+128, 23,
  312. 5+192, 5+128, 4+128, 6+128, 7+128, 4+192
  313. // +64 -> use muxA
  314. // +128 -> use ADC1
  315. // A15 26 E1 ADC1_SE5a 5+64
  316. // A16 27 C9 ADC1_SE5b 5
  317. // A17 28 C8 ADC1_SE4b 4
  318. // A18 29 C10 ADC1_SE6b 6
  319. // A19 30 C11 ADC1_SE7b 7
  320. // A20 31 E0 ADC1_SE4a 4+64
  321. };
  322. #elif defined(__MKL26Z64__)
  323. static const uint8_t channel2sc1a[] = {
  324. 5, 14, 8, 9, 13, 12, 6, 7, 15, 11,
  325. 0, 4+64, 23, 26, 27
  326. };
  327. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  328. static const uint8_t channel2sc1a[] = {
  329. 5, 14, 8, 9, 13, 12, 6, 7, 15, 4, // A0-A9
  330. 3, 19+128, // A10-A11
  331. // A10 ADC1_DP0/ADC0_DP3
  332. // A11 ADC1_DM0/ADC0_DM3
  333. 14+128, 15+128, 17, 18, 4+128, 5+128, 6+128, 7+128, 17+128, // A12-A20
  334. // A12 PTB10 ADC1_SE14
  335. // A13 PTB11 ADC1_SE15
  336. // A14 PTE24 ADC0_SE17
  337. // A15 PTE25 ADC0_SE18
  338. // A16 PTC8 ADC1_SE4b
  339. // A17 PTC9 ADC1_SE5b
  340. // A18 PTC10 ADC1_SE6b
  341. // A19 PTC11 ADC1_SE7b
  342. // A20 PTA17 ADC1_SE17
  343. 23, 23+128, 26, 18+128 // A21-A22, temp sensor, vref
  344. // A21 DAC0 ADC0_SE23
  345. // A22 DAC1 ADC1_SE23
  346. };
  347. #endif
  348. // TODO: perhaps this should store the NVIC priority, so it works recursively?
  349. static volatile uint8_t analogReadBusyADC0 = 0;
  350. #ifdef HAS_KINETIS_ADC1
  351. static volatile uint8_t analogReadBusyADC1 = 0;
  352. #endif
  353. int analogRead(uint8_t pin)
  354. {
  355. int result;
  356. uint8_t index, channel;
  357. //serial_phex(pin);
  358. //serial_print(" ");
  359. #if defined(__MK20DX128__)
  360. if (pin <= 13) {
  361. index = pin; // 0-13 refer to A0-A13
  362. } else if (pin <= 23) {
  363. index = pin - 14; // 14-23 are A0-A9
  364. } else if (pin >= 34 && pin <= 40) {
  365. index = pin - 24; // 34-37 are A10-A13, 38 is temp sensor,
  366. // 39 is vref, 40 is unused analog pin
  367. } else {
  368. return 0;
  369. }
  370. #elif defined(__MK20DX256__)
  371. if (pin <= 13) {
  372. index = pin; // 0-13 refer to A0-A13
  373. } else if (pin <= 23) {
  374. index = pin - 14; // 14-23 are A0-A9
  375. } else if (pin >= 26 && pin <= 31) {
  376. index = pin - 9; // 26-31 are A15-A20
  377. } else if (pin >= 34 && pin <= 40) {
  378. index = pin - 24; // 34-37 are A10-A13, 38 is temp sensor,
  379. // 39 is vref, 40 is A14
  380. } else {
  381. return 0;
  382. }
  383. #elif defined(__MK64FX512__) || defined(__MK66FX1M0__)
  384. if (pin <= 13) {
  385. index = pin; // 0-13 refer to A0-A13
  386. } else if (pin <= 23) {
  387. index = pin - 14; // 14-23 are A0-A9
  388. } else if (pin >= 31 && pin <= 39) {
  389. index = pin - 19; // 31-39 are A12-A20
  390. } else if (pin >= 40 && pin <= 41) {
  391. index = pin - 30; // 40-41 are A10-A11
  392. } else if (pin >= 42 && pin <= 45) {
  393. index = pin - 21; // 42-43 are A21-A22, 44 is temp sensor, 45 is vref
  394. } else {
  395. return 0;
  396. }
  397. #elif defined(__MKL26Z64__)
  398. if (pin <= 12) {
  399. index = pin; // 0-12 refer to A0-A12
  400. } else if (pin >= 14 && pin <= 26) {
  401. index = pin - 14; // 14-26 are A0-A12
  402. } else if (pin >= 38 && pin <= 39) {
  403. index = pin - 25; // 38=temperature
  404. // 39=bandgap ref (PMC_REGSC |= PMC_REGSC_BGBE)
  405. } else {
  406. return 0;
  407. }
  408. #endif
  409. //serial_phex(index);
  410. //serial_print(" ");
  411. channel = channel2sc1a[index];
  412. //serial_phex(channel);
  413. //serial_print(" ");
  414. //serial_print("analogRead");
  415. //return 0;
  416. if (calibrating) wait_for_cal();
  417. //pin = 5; // PTD1/SE5b, pin 14, analog 0
  418. #ifdef HAS_KINETIS_ADC1
  419. if (channel & 0x80) goto beginADC1;
  420. #endif
  421. __disable_irq();
  422. startADC0:
  423. //serial_print("startADC0\n");
  424. #if defined(__MKL26Z64__)
  425. if (channel & 0x40) {
  426. ADC0_CFG2 &= ~ADC_CFG2_MUXSEL;
  427. channel &= 0x3F;
  428. } else {
  429. ADC0_CFG2 |= ADC_CFG2_MUXSEL;
  430. }
  431. #endif
  432. ADC0_SC1A = channel;
  433. analogReadBusyADC0 = 1;
  434. __enable_irq();
  435. while (1) {
  436. __disable_irq();
  437. if ((ADC0_SC1A & ADC_SC1_COCO)) {
  438. result = ADC0_RA;
  439. analogReadBusyADC0 = 0;
  440. __enable_irq();
  441. result >>= analog_right_shift;
  442. return result;
  443. }
  444. // detect if analogRead was used from an interrupt
  445. // if so, our analogRead got canceled, so it must
  446. // be restarted.
  447. if (!analogReadBusyADC0) goto startADC0;
  448. __enable_irq();
  449. yield();
  450. }
  451. #ifdef HAS_KINETIS_ADC1
  452. beginADC1:
  453. __disable_irq();
  454. startADC1:
  455. //serial_print("startADC1\n");
  456. // ADC1_CFG2[MUXSEL] bit selects between ADCx_SEn channels a and b.
  457. if (channel & 0x40) {
  458. ADC1_CFG2 &= ~ADC_CFG2_MUXSEL;
  459. } else {
  460. ADC1_CFG2 |= ADC_CFG2_MUXSEL;
  461. }
  462. ADC1_SC1A = channel & 0x3F;
  463. analogReadBusyADC1 = 1;
  464. __enable_irq();
  465. while (1) {
  466. __disable_irq();
  467. if ((ADC1_SC1A & ADC_SC1_COCO)) {
  468. result = ADC1_RA;
  469. analogReadBusyADC1 = 0;
  470. __enable_irq();
  471. result >>= analog_right_shift;
  472. return result;
  473. }
  474. // detect if analogRead was used from an interrupt
  475. // if so, our analogRead got canceled, so it must
  476. // be restarted.
  477. if (!analogReadBusyADC1) goto startADC1;
  478. __enable_irq();
  479. yield();
  480. }
  481. #endif
  482. }
  483. void analogWriteDAC0(int val)
  484. {
  485. #if defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
  486. SIM_SCGC2 |= SIM_SCGC2_DAC0;
  487. if (analog_reference_internal) {
  488. DAC0_C0 = DAC_C0_DACEN; // 1.2V ref is DACREF_1
  489. } else {
  490. DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS; // 3.3V VDDA is DACREF_2
  491. }
  492. if (val < 0) val = 0; // TODO: saturate instruction?
  493. else if (val > 4095) val = 4095;
  494. *(int16_t *)&(DAC0_DAT0L) = val;
  495. #elif defined(__MKL26Z64__)
  496. SIM_SCGC6 |= SIM_SCGC6_DAC0;
  497. if (analog_reference_internal == 0) {
  498. // use 3.3V VDDA power as the reference (this is the default)
  499. DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS | DAC_C0_DACSWTRG; // 3.3V VDDA
  500. } else {
  501. // use whatever voltage is on the AREF pin
  502. DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACSWTRG; // 3.3V VDDA
  503. }
  504. if (val < 0) val = 0;
  505. else if (val > 4095) val = 4095;
  506. *(int16_t *)&(DAC0_DAT0L) = val;
  507. #endif
  508. }
  509. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  510. void analogWriteDAC1(int val)
  511. {
  512. SIM_SCGC2 |= SIM_SCGC2_DAC1;
  513. if (analog_reference_internal) {
  514. DAC1_C0 = DAC_C0_DACEN; // 1.2V ref is DACREF_1
  515. } else {
  516. DAC1_C0 = DAC_C0_DACEN | DAC_C0_DACRFS; // 3.3V VDDA is DACREF_2
  517. }
  518. if (val < 0) val = 0; // TODO: saturate instruction?
  519. else if (val > 4095) val = 4095;
  520. *(int16_t *)&(DAC1_DAT0L) = val;
  521. }
  522. #endif