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.

464 line
14KB

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