Teensy 4.1 core updated for C++20
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

520 líneas
15KB

  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
  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, 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. #if defined(__MK20DX256__)
  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. #if defined(__MK20DX256__)
  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. #if defined(__MK20DX256__)
  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. #if defined(__MK20DX256__)
  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__)
  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. #if defined(__MK20DX256__)
  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. #if defined(__MK20DX256__)
  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. #if defined(__MK20DX256__)
  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. #if defined(__MK20DX256__)
  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. #if defined(__MK20DX256__)
  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(__MK20DX128__)
  184. while (ADC0_SC3 & ADC_SC3_CAL) {
  185. // wait
  186. }
  187. #elif defined(__MK20DX256__)
  188. while ((ADC0_SC3 & ADC_SC3_CAL) || (ADC1_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. #if defined(__MK20DX256__)
  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__)
  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. #if defined(__MK20DX256__)
  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. #if defined(__MK20DX256__)
  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. // A15 26 E1 ADC1_SE5a 5+64
  314. // A16 27 C9 ADC1_SE5b 5
  315. // A17 28 C8 ADC1_SE4b 4
  316. // A18 29 C10 ADC1_SE6b 6
  317. // A19 30 C11 ADC1_SE7b 7
  318. // A20 31 E0 ADC1_SE4a 4+64
  319. };
  320. #elif defined(__MKL26Z64__)
  321. static const uint8_t channel2sc1a[] = {
  322. 5, 14, 8, 9, 13, 12, 6, 7, 15, 11,
  323. 0, 4+64, 23
  324. };
  325. #endif
  326. // TODO: perhaps this should store the NVIC priority, so it works recursively?
  327. static volatile uint8_t analogReadBusyADC0 = 0;
  328. #if defined(__MK20DX256__)
  329. static volatile uint8_t analogReadBusyADC1 = 0;
  330. #endif
  331. int analogRead(uint8_t pin)
  332. {
  333. int result;
  334. uint8_t index, channel;
  335. //serial_phex(pin);
  336. //serial_print(" ");
  337. #if defined(__MK20DX128__)
  338. if (pin <= 13) {
  339. index = pin; // 0-13 refer to A0-A13
  340. } else if (pin <= 23) {
  341. index = pin - 14; // 14-23 are A0-A9
  342. } else if (pin >= 34 && pin <= 40) {
  343. index = pin - 24; // 34-37 are A10-A13, 38 is temp sensor,
  344. // 39 is vref, 40 is unused analog pin
  345. } else {
  346. return 0;
  347. }
  348. #elif defined(__MK20DX256__)
  349. if (pin <= 13) {
  350. index = pin; // 0-13 refer to A0-A13
  351. } else if (pin <= 23) {
  352. index = pin - 14; // 14-23 are A0-A9
  353. } else if (pin >= 26 && pin <= 31) {
  354. index = pin - 9; // 26-31 are A15-A20
  355. } else if (pin >= 34 && pin <= 40) {
  356. index = pin - 24; // 34-37 are A10-A13, 38 is temp sensor,
  357. // 39 is vref, 40 is A14
  358. } else {
  359. return 0;
  360. }
  361. #elif defined(__MKL26Z64__)
  362. if (pin <= 12) {
  363. index = pin; // 0-12 refer to A0-A12
  364. } else if (pin >= 14 && pin <= 26) {
  365. index = pin - 14; // 14-26 are A0-A12
  366. } else {
  367. return 0;
  368. }
  369. #endif
  370. //serial_phex(index);
  371. //serial_print(" ");
  372. channel = channel2sc1a[index];
  373. //serial_phex(channel);
  374. //serial_print(" ");
  375. //serial_print("analogRead");
  376. //return 0;
  377. if (calibrating) wait_for_cal();
  378. //pin = 5; // PTD1/SE5b, pin 14, analog 0
  379. #if defined(__MK20DX256__)
  380. if (channel & 0x80) goto beginADC1;
  381. #endif
  382. __disable_irq();
  383. startADC0:
  384. //serial_print("startADC0\n");
  385. #if defined(__MKL26Z64__)
  386. if (channel & 0x40) {
  387. ADC0_CFG2 &= ~ADC_CFG2_MUXSEL;
  388. channel &= 0x3F;
  389. } else {
  390. ADC0_CFG2 |= ADC_CFG2_MUXSEL;
  391. }
  392. #endif
  393. ADC0_SC1A = channel;
  394. analogReadBusyADC0 = 1;
  395. __enable_irq();
  396. while (1) {
  397. __disable_irq();
  398. if ((ADC0_SC1A & ADC_SC1_COCO)) {
  399. result = ADC0_RA;
  400. analogReadBusyADC0 = 0;
  401. __enable_irq();
  402. result >>= analog_right_shift;
  403. return result;
  404. }
  405. // detect if analogRead was used from an interrupt
  406. // if so, our analogRead got canceled, so it must
  407. // be restarted.
  408. if (!analogReadBusyADC0) goto startADC0;
  409. __enable_irq();
  410. yield();
  411. }
  412. #if defined(__MK20DX256__)
  413. beginADC1:
  414. __disable_irq();
  415. startADC1:
  416. //serial_print("startADC0\n");
  417. // ADC1_CFG2[MUXSEL] bit selects between ADCx_SEn channels a and b.
  418. if (channel & 0x40) {
  419. ADC1_CFG2 &= ~ADC_CFG2_MUXSEL;
  420. } else {
  421. ADC1_CFG2 |= ADC_CFG2_MUXSEL;
  422. }
  423. ADC1_SC1A = channel & 0x3F;
  424. analogReadBusyADC1 = 1;
  425. __enable_irq();
  426. while (1) {
  427. __disable_irq();
  428. if ((ADC1_SC1A & ADC_SC1_COCO)) {
  429. result = ADC1_RA;
  430. analogReadBusyADC1 = 0;
  431. __enable_irq();
  432. result >>= analog_right_shift;
  433. return result;
  434. }
  435. // detect if analogRead was used from an interrupt
  436. // if so, our analogRead got canceled, so it must
  437. // be restarted.
  438. if (!analogReadBusyADC1) goto startADC1;
  439. __enable_irq();
  440. yield();
  441. }
  442. #endif
  443. }
  444. void analogWriteDAC0(int val)
  445. {
  446. #if defined(__MK20DX256__)
  447. SIM_SCGC2 |= SIM_SCGC2_DAC0;
  448. if (analog_reference_internal) {
  449. DAC0_C0 = DAC_C0_DACEN; // 1.2V ref is DACREF_1
  450. } else {
  451. DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS; // 3.3V VDDA is DACREF_2
  452. }
  453. if (val < 0) val = 0; // TODO: saturate instruction?
  454. else if (val > 4095) val = 4095;
  455. *(int16_t *)&(DAC0_DAT0L) = val;
  456. #endif
  457. }