Teensy 4.1 core updated for C++20
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
11 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 == 4000000
  71. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 4 MHz
  72. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 4 MHz
  73. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 4 MHz
  74. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 4 MHz
  75. #elif F_BUS == 2000000
  76. #define ADC_CFG1_16BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 2 MHz
  77. #define ADC_CFG1_12BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 2 MHz
  78. #define ADC_CFG1_10BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 2 MHz
  79. #define ADC_CFG1_8BIT ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0) // 2 MHz
  80. #else
  81. #error "F_BUS must be 60, 56, 48, 40, 36, 24, 4 or 2 MHz"
  82. #endif
  83. void analog_init(void)
  84. {
  85. uint32_t num;
  86. VREF_TRM = 0x60;
  87. VREF_SC = 0xE1; // enable 1.2 volt ref
  88. if (analog_config_bits == 8) {
  89. ADC0_CFG1 = ADC_CFG1_8BIT + ADC_CFG1_MODE(0);
  90. ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(3);
  91. #if defined(__MK20DX256__)
  92. ADC1_CFG1 = ADC_CFG1_8BIT + ADC_CFG1_MODE(0);
  93. ADC1_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(3);
  94. #endif
  95. } else if (analog_config_bits == 10) {
  96. ADC0_CFG1 = ADC_CFG1_10BIT + ADC_CFG1_MODE(2) + ADC_CFG1_ADLSMP;
  97. ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(3);
  98. #if defined(__MK20DX256__)
  99. ADC1_CFG1 = ADC_CFG1_10BIT + ADC_CFG1_MODE(2) + ADC_CFG1_ADLSMP;
  100. ADC1_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(3);
  101. #endif
  102. } else if (analog_config_bits == 12) {
  103. ADC0_CFG1 = ADC_CFG1_12BIT + ADC_CFG1_MODE(1) + ADC_CFG1_ADLSMP;
  104. ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(2);
  105. #if defined(__MK20DX256__)
  106. ADC1_CFG1 = ADC_CFG1_12BIT + ADC_CFG1_MODE(1) + ADC_CFG1_ADLSMP;
  107. ADC1_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(2);
  108. #endif
  109. } else {
  110. ADC0_CFG1 = ADC_CFG1_16BIT + ADC_CFG1_MODE(3) + ADC_CFG1_ADLSMP;
  111. ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(2);
  112. #if defined(__MK20DX256__)
  113. ADC1_CFG1 = ADC_CFG1_16BIT + ADC_CFG1_MODE(3) + ADC_CFG1_ADLSMP;
  114. ADC1_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(2);
  115. #endif
  116. }
  117. if (analog_reference_internal) {
  118. ADC0_SC2 = ADC_SC2_REFSEL(1); // 1.2V ref
  119. #if defined(__MK20DX256__)
  120. ADC1_SC2 = ADC_SC2_REFSEL(1); // 1.2V ref
  121. #endif
  122. } else {
  123. ADC0_SC2 = ADC_SC2_REFSEL(0); // vcc/ext ref
  124. #if defined(__MK20DX256__)
  125. ADC1_SC2 = ADC_SC2_REFSEL(0); // vcc/ext ref
  126. #endif
  127. }
  128. num = analog_num_average;
  129. if (num <= 1) {
  130. ADC0_SC3 = ADC_SC3_CAL; // begin cal
  131. #if defined(__MK20DX256__)
  132. ADC1_SC3 = ADC_SC3_CAL; // begin cal
  133. #endif
  134. } else if (num <= 4) {
  135. ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(0);
  136. #if defined(__MK20DX256__)
  137. ADC1_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(0);
  138. #endif
  139. } else if (num <= 8) {
  140. ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(1);
  141. #if defined(__MK20DX256__)
  142. ADC1_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(1);
  143. #endif
  144. } else if (num <= 16) {
  145. ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(2);
  146. #if defined(__MK20DX256__)
  147. ADC1_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(2);
  148. #endif
  149. } else {
  150. ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(3);
  151. #if defined(__MK20DX256__)
  152. ADC1_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(3);
  153. #endif
  154. }
  155. calibrating = 1;
  156. }
  157. static void wait_for_cal(void)
  158. {
  159. uint16_t sum;
  160. //serial_print("wait_for_cal\n");
  161. #if defined(__MK20DX128__)
  162. while (ADC0_SC3 & ADC_SC3_CAL) {
  163. // wait
  164. }
  165. #elif defined(__MK20DX256__)
  166. while ((ADC0_SC3 & ADC_SC3_CAL) || (ADC1_SC3 & ADC_SC3_CAL)) {
  167. // wait
  168. }
  169. #endif
  170. __disable_irq();
  171. if (calibrating) {
  172. //serial_print("\n");
  173. sum = ADC0_CLPS + ADC0_CLP4 + ADC0_CLP3 + ADC0_CLP2 + ADC0_CLP1 + ADC0_CLP0;
  174. sum = (sum / 2) | 0x8000;
  175. ADC0_PG = sum;
  176. //serial_print("ADC0_PG = ");
  177. //serial_phex16(sum);
  178. //serial_print("\n");
  179. sum = ADC0_CLMS + ADC0_CLM4 + ADC0_CLM3 + ADC0_CLM2 + ADC0_CLM1 + ADC0_CLM0;
  180. sum = (sum / 2) | 0x8000;
  181. ADC0_MG = sum;
  182. //serial_print("ADC0_MG = ");
  183. //serial_phex16(sum);
  184. //serial_print("\n");
  185. #if defined(__MK20DX256__)
  186. sum = ADC1_CLPS + ADC1_CLP4 + ADC1_CLP3 + ADC1_CLP2 + ADC1_CLP1 + ADC1_CLP0;
  187. sum = (sum / 2) | 0x8000;
  188. ADC1_PG = sum;
  189. sum = ADC1_CLMS + ADC1_CLM4 + ADC1_CLM3 + ADC1_CLM2 + ADC1_CLM1 + ADC1_CLM0;
  190. sum = (sum / 2) | 0x8000;
  191. ADC1_MG = sum;
  192. #endif
  193. calibrating = 0;
  194. }
  195. __enable_irq();
  196. }
  197. // ADCx_SC2[REFSEL] bit selects the voltage reference sources for ADC.
  198. // VREFH/VREFL - connected as the primary reference option
  199. // 1.2 V VREF_OUT - connected as the VALT reference option
  200. #define DEFAULT 0
  201. #define INTERNAL 2
  202. #define INTERNAL1V2 2
  203. #define INTERNAL1V1 2
  204. #define EXTERNAL 0
  205. void analogReference(uint8_t type)
  206. {
  207. if (type) {
  208. // internal reference requested
  209. if (!analog_reference_internal) {
  210. analog_reference_internal = 1;
  211. if (calibrating) {
  212. ADC0_SC3 = 0; // cancel cal
  213. #if defined(__MK20DX256__)
  214. ADC1_SC3 = 0; // cancel cal
  215. #endif
  216. }
  217. analog_init();
  218. }
  219. } else {
  220. // vcc or external reference requested
  221. if (analog_reference_internal) {
  222. analog_reference_internal = 0;
  223. if (calibrating) {
  224. ADC0_SC3 = 0; // cancel cal
  225. #if defined(__MK20DX256__)
  226. ADC1_SC3 = 0; // cancel cal
  227. #endif
  228. }
  229. analog_init();
  230. }
  231. }
  232. }
  233. void analogReadRes(unsigned int bits)
  234. {
  235. unsigned int config;
  236. if (bits >= 13) {
  237. if (bits > 16) bits = 16;
  238. config = 16;
  239. } else if (bits >= 11) {
  240. config = 12;
  241. } else if (bits >= 9) {
  242. config = 10;
  243. } else {
  244. config = 8;
  245. }
  246. analog_right_shift = config - bits;
  247. if (config != analog_config_bits) {
  248. analog_config_bits = config;
  249. if (calibrating) ADC0_SC3 = 0; // cancel cal
  250. analog_init();
  251. }
  252. }
  253. void analogReadAveraging(unsigned int num)
  254. {
  255. if (calibrating) wait_for_cal();
  256. if (num <= 1) {
  257. num = 0;
  258. ADC0_SC3 = 0;
  259. } else if (num <= 4) {
  260. num = 4;
  261. ADC0_SC3 = ADC_SC3_AVGE + ADC_SC3_AVGS(0);
  262. } else if (num <= 8) {
  263. num = 8;
  264. ADC0_SC3 = ADC_SC3_AVGE + ADC_SC3_AVGS(1);
  265. } else if (num <= 16) {
  266. num = 16;
  267. ADC0_SC3 = ADC_SC3_AVGE + ADC_SC3_AVGS(2);
  268. } else {
  269. num = 32;
  270. ADC0_SC3 = ADC_SC3_AVGE + ADC_SC3_AVGS(3);
  271. }
  272. analog_num_average = num;
  273. }
  274. // The SC1A register is used for both software and hardware trigger modes of operation.
  275. #if defined(__MK20DX128__)
  276. static const uint8_t channel2sc1a[] = {
  277. 5, 14, 8, 9, 13, 12, 6, 7, 15, 4,
  278. 0, 19, 3, 21, 26, 22, 23
  279. };
  280. #elif defined(__MK20DX256__)
  281. static const uint8_t channel2sc1a[] = {
  282. 5, 14, 8, 9, 13, 12, 6, 7, 15, 4,
  283. 0, 19, 3, 19+128, 26, 18+128, 23,
  284. 5+192, 5+128, 4+128, 6+128, 7+128, 4+192
  285. // A15 26 E1 ADC1_SE5a 5+64
  286. // A16 27 C9 ADC1_SE5b 5
  287. // A17 28 C8 ADC1_SE4b 4
  288. // A18 29 C10 ADC1_SE6b 6
  289. // A19 30 C11 ADC1_SE7b 7
  290. // A20 31 E0 ADC1_SE4a 4+64
  291. };
  292. #endif
  293. // TODO: perhaps this should store the NVIC priority, so it works recursively?
  294. static volatile uint8_t analogReadBusyADC0 = 0;
  295. #if defined(__MK20DX256__)
  296. static volatile uint8_t analogReadBusyADC1 = 0;
  297. #endif
  298. int analogRead(uint8_t pin)
  299. {
  300. int result;
  301. uint8_t index, channel;
  302. //serial_phex(pin);
  303. //serial_print(" ");
  304. if (pin <= 13) {
  305. index = pin; // 0-13 refer to A0-A13
  306. } else if (pin <= 23) {
  307. index = pin - 14; // 14-23 are A0-A9
  308. #if defined(__MK20DX256__)
  309. } else if (pin >= 26 && pin <= 31) {
  310. index = pin - 9; // 26-31 are A15-A20
  311. #endif
  312. } else if (pin >= 34 && pin <= 40) {
  313. index = pin - 24; // 34-37 are A10-A13, 38 is temp sensor,
  314. // 39 is vref, 40 is unused (A14 on Teensy 3.1)
  315. } else {
  316. return 0; // all others are invalid
  317. }
  318. //serial_phex(index);
  319. //serial_print(" ");
  320. channel = channel2sc1a[index];
  321. //serial_phex(channel);
  322. //serial_print(" ");
  323. //serial_print("analogRead");
  324. //return 0;
  325. if (calibrating) wait_for_cal();
  326. //pin = 5; // PTD1/SE5b, pin 14, analog 0
  327. #if defined(__MK20DX256__)
  328. if (channel & 0x80) goto beginADC1;
  329. #endif
  330. __disable_irq();
  331. startADC0:
  332. //serial_print("startADC0\n");
  333. ADC0_SC1A = channel;
  334. analogReadBusyADC0 = 1;
  335. __enable_irq();
  336. while (1) {
  337. __disable_irq();
  338. if ((ADC0_SC1A & ADC_SC1_COCO)) {
  339. result = ADC0_RA;
  340. analogReadBusyADC0 = 0;
  341. __enable_irq();
  342. result >>= analog_right_shift;
  343. return result;
  344. }
  345. // detect if analogRead was used from an interrupt
  346. // if so, our analogRead got canceled, so it must
  347. // be restarted.
  348. if (!analogReadBusyADC0) goto startADC0;
  349. __enable_irq();
  350. yield();
  351. }
  352. #if defined(__MK20DX256__)
  353. beginADC1:
  354. __disable_irq();
  355. startADC1:
  356. //serial_print("startADC0\n");
  357. // ADC1_CFG2[MUXSEL] bit selects between ADCx_SEn channels a and b.
  358. if (channel & 0x40) {
  359. ADC1_CFG2 &= ~ADC_CFG2_MUXSEL;
  360. } else {
  361. ADC1_CFG2 |= ADC_CFG2_MUXSEL;
  362. }
  363. ADC1_SC1A = channel & 0x3F;
  364. analogReadBusyADC1 = 1;
  365. __enable_irq();
  366. while (1) {
  367. __disable_irq();
  368. if ((ADC1_SC1A & ADC_SC1_COCO)) {
  369. result = ADC1_RA;
  370. analogReadBusyADC1 = 0;
  371. __enable_irq();
  372. result >>= analog_right_shift;
  373. return result;
  374. }
  375. // detect if analogRead was used from an interrupt
  376. // if so, our analogRead got canceled, so it must
  377. // be restarted.
  378. if (!analogReadBusyADC1) goto startADC1;
  379. __enable_irq();
  380. yield();
  381. }
  382. #endif
  383. }
  384. void analogWriteDAC0(int val)
  385. {
  386. #if defined(__MK20DX256__)
  387. SIM_SCGC2 |= SIM_SCGC2_DAC0;
  388. if (analog_reference_internal) {
  389. DAC0_C0 = DAC_C0_DACEN; // 1.2V ref is DACREF_1
  390. } else {
  391. DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS; // 3.3V VDDA is DACREF_2
  392. }
  393. if (val < 0) val = 0; // TODO: saturate instruction?
  394. else if (val > 4095) val = 4095;
  395. *(int16_t *)&(DAC0_DAT0L) = val;
  396. #endif
  397. }