PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

580 lines
19KB

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