Kiibohd Controller
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

analog.c 11KB

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