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.

531 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, 26, 27
  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 if (pin >= 38 && pin <= 39) {
  367. index = pin - 25; // 38=temperature
  368. // 39=bandgap ref (PMC_REGSC |= PMC_REGSC_BGBE)
  369. } else {
  370. return 0;
  371. }
  372. #endif
  373. //serial_phex(index);
  374. //serial_print(" ");
  375. channel = channel2sc1a[index];
  376. //serial_phex(channel);
  377. //serial_print(" ");
  378. //serial_print("analogRead");
  379. //return 0;
  380. if (calibrating) wait_for_cal();
  381. //pin = 5; // PTD1/SE5b, pin 14, analog 0
  382. #if defined(__MK20DX256__)
  383. if (channel & 0x80) goto beginADC1;
  384. #endif
  385. __disable_irq();
  386. startADC0:
  387. //serial_print("startADC0\n");
  388. #if defined(__MKL26Z64__)
  389. if (channel & 0x40) {
  390. ADC0_CFG2 &= ~ADC_CFG2_MUXSEL;
  391. channel &= 0x3F;
  392. } else {
  393. ADC0_CFG2 |= ADC_CFG2_MUXSEL;
  394. }
  395. #endif
  396. ADC0_SC1A = channel;
  397. analogReadBusyADC0 = 1;
  398. __enable_irq();
  399. while (1) {
  400. __disable_irq();
  401. if ((ADC0_SC1A & ADC_SC1_COCO)) {
  402. result = ADC0_RA;
  403. analogReadBusyADC0 = 0;
  404. __enable_irq();
  405. result >>= analog_right_shift;
  406. return result;
  407. }
  408. // detect if analogRead was used from an interrupt
  409. // if so, our analogRead got canceled, so it must
  410. // be restarted.
  411. if (!analogReadBusyADC0) goto startADC0;
  412. __enable_irq();
  413. yield();
  414. }
  415. #if defined(__MK20DX256__)
  416. beginADC1:
  417. __disable_irq();
  418. startADC1:
  419. //serial_print("startADC0\n");
  420. // ADC1_CFG2[MUXSEL] bit selects between ADCx_SEn channels a and b.
  421. if (channel & 0x40) {
  422. ADC1_CFG2 &= ~ADC_CFG2_MUXSEL;
  423. } else {
  424. ADC1_CFG2 |= ADC_CFG2_MUXSEL;
  425. }
  426. ADC1_SC1A = channel & 0x3F;
  427. analogReadBusyADC1 = 1;
  428. __enable_irq();
  429. while (1) {
  430. __disable_irq();
  431. if ((ADC1_SC1A & ADC_SC1_COCO)) {
  432. result = ADC1_RA;
  433. analogReadBusyADC1 = 0;
  434. __enable_irq();
  435. result >>= analog_right_shift;
  436. return result;
  437. }
  438. // detect if analogRead was used from an interrupt
  439. // if so, our analogRead got canceled, so it must
  440. // be restarted.
  441. if (!analogReadBusyADC1) goto startADC1;
  442. __enable_irq();
  443. yield();
  444. }
  445. #endif
  446. }
  447. void analogWriteDAC0(int val)
  448. {
  449. #if defined(__MK20DX256__)
  450. SIM_SCGC2 |= SIM_SCGC2_DAC0;
  451. if (analog_reference_internal) {
  452. DAC0_C0 = DAC_C0_DACEN; // 1.2V ref is DACREF_1
  453. } else {
  454. DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS; // 3.3V VDDA is DACREF_2
  455. }
  456. if (val < 0) val = 0; // TODO: saturate instruction?
  457. else if (val > 4095) val = 4095;
  458. *(int16_t *)&(DAC0_DAT0L) = val;
  459. #elif defined(__MKL26Z64__)
  460. SIM_SCGC6 |= SIM_SCGC6_DAC0;
  461. if (analog_reference_internal == 0) {
  462. // use 3.3V VDDA power as the reference (this is the default)
  463. DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS | DAC_C0_DACSWTRG; // 3.3V VDDA
  464. } else {
  465. // use whatever voltage is on the AREF pin
  466. DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACSWTRG; // 3.3V VDDA
  467. }
  468. if (val < 0) val = 0;
  469. else if (val > 4095) val = 4095;
  470. *(int16_t *)&(DAC0_DAT0L) = val;
  471. #endif
  472. }