32 #ifndef ADC_SETTINGS_H
33 #define ADC_SETTINGS_H
44 #if defined(__MK20DX256__) // Teensy 3.1/3.2
45 #define ADC_TEENSY_3_1
46 #elif defined(__MK20DX128__) // Teensy 3.0
47 #define ADC_TEENSY_3_0
48 #elif defined(__MKL26Z64__) // Teensy LC
50 #elif defined(__MK64FX512__) // Teensy 3.5
51 #define ADC_TEENSY_3_5
52 #elif defined(__MK66FX1M0__) // Teensy 3.6
53 #define ADC_TEENSY_3_6
54 #elif defined(__IMXRT1062__) // Teensy 4.0/4.1
57 #ifdef ARDUINO_TEENSY41
58 #define ADC_TEENSY_4_1
60 #define ADC_TEENSY_4_0
63 #error "Board not supported!"
67 #if defined(ADC_TEENSY_3_1) // Teensy 3.1
68 #define ADC_NUM_ADCS (2)
70 #elif defined(ADC_TEENSY_3_0) // Teensy 3.0
71 #define ADC_NUM_ADCS (1)
72 #define ADC_SINGLE_ADC
73 #elif defined(ADC_TEENSY_LC) // Teensy LC
74 #define ADC_NUM_ADCS (1)
75 #define ADC_SINGLE_ADC
76 #elif defined(ADC_TEENSY_3_5) // Teensy 3.5
77 #define ADC_NUM_ADCS (2)
79 #elif defined(ADC_TEENSY_3_6) // Teensy 3.6
80 #define ADC_NUM_ADCS (2)
82 #elif defined(ADC_TEENSY_4) // Teensy 4, 4.1
83 #define ADC_NUM_ADCS (2)
88 #if defined(ADC_TEENSY_3_1) // Teensy 3.1
90 #elif defined(ADC_TEENSY_3_0) // Teensy 3.0
92 #elif defined(ADC_TEENSY_LC) // Teensy LC
94 #elif defined(ADC_TEENSY_3_5) // Teensy 3.5
96 #elif defined(ADC_TEENSY_3_6) // Teensy 3.6
98 #elif defined(ADC_TEENSY_4) // Teensy 4, 4.1
103 #if defined(ADC_TEENSY_3_1) // Teensy 3.1
105 #elif defined(ADC_TEENSY_3_0) // Teensy 3.0
106 #elif defined(ADC_TEENSY_LC) // Teensy LC
107 #elif defined(ADC_TEENSY_3_5) // Teensy 3.5
108 #elif defined(ADC_TEENSY_3_6) // Teensy 3.6
109 #elif defined(ADC_TEENSY_4) // Teensy 4, 4.1
113 #if defined(ADC_TEENSY_3_1) // Teensy 3.1
115 #elif defined(ADC_TEENSY_3_0) // Teensy 3.0
117 #elif defined(ADC_TEENSY_LC) // Teensy LC
118 #elif defined(ADC_TEENSY_3_5) // Teensy 3.5
120 #elif defined(ADC_TEENSY_3_6) // Teensy 3.6
122 #elif defined(ADC_TEENSY_4) // Teensy 4, 4.1
126 #if defined(ADC_TEENSY_3_1) // Teensy 3.1
127 #define ADC_USE_QUAD_TIMER // TODO: Not implemented
128 #elif defined(ADC_TEENSY_3_0) // Teensy 3.0
129 #define ADC_USE_QUAD_TIMER // TODO: Not implemented
130 #elif defined(ADC_TEENSY_LC) // Teensy LC
131 #elif defined(ADC_TEENSY_3_5) // Teensy 3.5
132 #define ADC_USE_QUAD_TIMER // TODO: Not implemented
133 #elif defined(ADC_TEENSY_3_6) // Teensy 3.6
134 #define ADC_USE_QUAD_TIMER // TODO: Not implemented
135 #elif defined(ADC_TEENSY_4) // Teensy 4, 4.1
136 #define ADC_USE_QUAD_TIMER
140 #if defined(ADC_USE_PDB) || defined(ADC_USE_QUAD_TIMER)
141 #define ADC_USE_TIMER
145 #if defined(ADC_TEENSY_3_1) // Teensy 3.1
146 #define ADC_USE_INTERNAL_VREF
147 #elif defined(ADC_TEENSY_3_0) // Teensy 3.0
148 #define ADC_USE_INTERNAL_VREF
149 #elif defined(ADC_TEENSY_LC) // Teensy LC
150 #elif defined(ADC_TEENSY_3_5) // Teensy 3.5
151 #define ADC_USE_INTERNAL_VREF
152 #elif defined(ADC_TEENSY_3_6) // Teensy 3.6
153 #define ADC_USE_INTERNAL_VREF
154 #elif defined(ADC_TEENSY_4) // Teensy 4, 4.1
162 enum class ADC_REF_SOURCE : uint8_t {
169 #if defined(ADC_TEENSY_3_0) || defined(ADC_TEENSY_3_1) || \
170 defined(ADC_TEENSY_3_5) || defined(ADC_TEENSY_3_6)
179 REF_3V3 =
static_cast<uint8_t
>(ADC_REF_SOURCE::REF_DEFAULT),
180 REF_1V2 =
static_cast<uint8_t
>(ADC_REF_SOURCE::REF_ALT),
182 static_cast<uint8_t
>(ADC_REF_SOURCE::REF_DEFAULT),
183 NONE =
static_cast<uint8_t
>(
184 ADC_REF_SOURCE::REF_NONE)
186 #elif defined(ADC_TEENSY_LC)
194 REF_3V3 =
static_cast<uint8_t
>(ADC_REF_SOURCE::REF_ALT),
196 static_cast<uint8_t
>(ADC_REF_SOURCE::REF_DEFAULT),
197 NONE =
static_cast<uint8_t
>(
198 ADC_REF_SOURCE::REF_NONE)
200 #elif defined(ADC_TEENSY_4)
207 REF_3V3 =
static_cast<uint8_t
>(ADC_REF_SOURCE::REF_DEFAULT),
208 NONE =
static_cast<uint8_t
>(
209 ADC_REF_SOURCE::REF_NONE)
214 #if defined(ADC_TEENSY_3_1) // Teensy 3.1
215 #define ADC_MAX_PIN (43)
216 #elif defined(ADC_TEENSY_3_0) // Teensy 3.0
217 #define ADC_MAX_PIN (43)
218 #elif defined(ADC_TEENSY_LC) // Teensy LC
219 #define ADC_MAX_PIN (43)
220 #elif defined(ADC_TEENSY_3_5) // Teensy 3.5
221 #define ADC_MAX_PIN (69)
222 #elif defined(ADC_TEENSY_3_6) // Teensy 3.6
223 #define ADC_MAX_PIN (67)
224 #elif defined(ADC_TEENSY_4_0) // Teensy 4
225 #define ADC_MAX_PIN (27)
226 #elif defined(ADC_TEENSY_4_1) // Teensy 4
227 #define ADC_MAX_PIN (41)
231 #if defined(ADC_TEENSY_3_1) // Teensy 3.1
232 #define ADC_DIFF_PAIRS (2) // normal and with PGA
233 #elif defined(ADC_TEENSY_3_0) // Teensy 3.0
234 #define ADC_DIFF_PAIRS (2)
235 #elif defined(ADC_TEENSY_LC) // Teensy LC
236 #define ADC_DIFF_PAIRS (1)
237 #elif defined(ADC_TEENSY_3_5) // Teensy 3.5
238 #define ADC_DIFF_PAIRS (1)
239 #elif defined(ADC_TEENSY_3_6) // Teensy 3.6
240 #define ADC_DIFF_PAIRS (1)
241 #elif defined(ADC_TEENSY_4) // Teensy 4, 4.1
242 #define ADC_DIFF_PAIRS (0)
249 #if defined(ADC_TEENSY_LC)
264 #elif defined(ADC_TEENSY_3_1) || defined(ADC_TEENSY_3_0)
280 #elif defined(ADC_TEENSY_3_5) || defined(ADC_TEENSY_3_6)
297 #elif defined(ADC_TEENSY_4)
312 #if defined(ADC_TEENSY_4)
314 volatile uint32_t HC0;
315 volatile uint32_t HC1;
316 volatile uint32_t HC2;
317 volatile uint32_t HC3;
318 volatile uint32_t HC4;
319 volatile uint32_t HC5;
320 volatile uint32_t HC6;
321 volatile uint32_t HC7;
322 volatile uint32_t HS;
323 volatile uint32_t R0;
324 volatile uint32_t R1;
325 volatile uint32_t R2;
326 volatile uint32_t R3;
327 volatile uint32_t R4;
328 volatile uint32_t R5;
329 volatile uint32_t R6;
330 volatile uint32_t R7;
331 volatile uint32_t CFG;
332 volatile uint32_t GC;
333 volatile uint32_t GS;
334 volatile uint32_t CV;
335 volatile uint32_t OFS;
336 volatile uint32_t CAL;
338 #define ADC0_START (*(ADC_REGS_t *)0x400C4000)
339 #define ADC1_START (*(ADC_REGS_t *)0x400C8000)
342 volatile uint32_t SC1A;
343 volatile uint32_t SC1B;
344 volatile uint32_t CFG1;
345 volatile uint32_t CFG2;
346 volatile uint32_t RA;
347 volatile uint32_t RB;
348 volatile uint32_t CV1;
349 volatile uint32_t CV2;
350 volatile uint32_t SC2;
351 volatile uint32_t SC3;
352 volatile uint32_t OFS;
353 volatile uint32_t PG;
354 volatile uint32_t MG;
355 volatile uint32_t CLPD;
356 volatile uint32_t CLPS;
357 volatile uint32_t CLP4;
358 volatile uint32_t CLP3;
359 volatile uint32_t CLP2;
360 volatile uint32_t CLP1;
361 volatile uint32_t CLP0;
362 volatile uint32_t PGA;
363 volatile uint32_t CLMD;
364 volatile uint32_t CLMS;
365 volatile uint32_t CLM4;
366 volatile uint32_t CLM3;
367 volatile uint32_t CLM2;
368 volatile uint32_t CLM1;
369 volatile uint32_t CLM0;
371 #define ADC0_START (*(ADC_REGS_t *)0x4003B000)
372 #define ADC1_START (*(ADC_REGS_t *)0x400BB000)
404 #define ADC_MHz (1000000) // not so many zeros
406 #if defined(ADC_TEENSY_4)
407 #define ADC_MIN_FREQ (4 * ADC_MHz)
409 #define ADC_MIN_FREQ (1 * ADC_MHz)
413 #if defined(ADC_TEENSY_3_6)
414 #define ADC_MAX_FREQ (24 * ADC_MHz)
415 #elif defined(ADC_TEENSY_4)
416 #define ADC_MAX_FREQ (40 * ADC_MHz)
418 #define ADC_MAX_FREQ (18 * ADC_MHz)
422 #if defined(ADC_TEENSY_4)
423 #define ADC_MIN_FREQ_16BITS ADC_MIN_FREQ
424 #define ADC_MAX_FREQ_16BITS ADC_MAX_FREQ
427 #define ADC_MIN_FREQ_16BITS (2 * ADC_MHz)
429 #define ADC_MAX_FREQ_16BITS (12 * ADC_MHz)
446 #define ADC_LIB_CFG1_ADIV(n) (((n)&0x03) << 5)
447 #define ADC_LIB_CFG1_ADICLK(n) (((n)&0x03) << 0)
449 #if defined(ADC_TEENSY_4)
450 #define ADC_F_BUS F_BUS_ACTUAL // (150*ADC_MHz)
452 #define ADC_F_BUS F_BUS
457 constexpr uint32_t get_CFG_VERY_LOW_SPEED(uint32_t f_adc_clock) {
458 if (f_adc_clock / 16 >= ADC_MIN_FREQ) {
459 return (ADC_LIB_CFG1_ADIV(3) + ADC_LIB_CFG1_ADICLK(1));
460 }
else if (f_adc_clock / 8 >= ADC_MIN_FREQ) {
461 return (ADC_LIB_CFG1_ADIV(3) + ADC_LIB_CFG1_ADICLK(0));
462 }
else if (f_adc_clock / 4 >= ADC_MIN_FREQ) {
463 return (ADC_LIB_CFG1_ADIV(2) + ADC_LIB_CFG1_ADICLK(0));
464 }
else if (f_adc_clock / 2 >= ADC_MIN_FREQ) {
465 return (ADC_LIB_CFG1_ADIV(1) + ADC_LIB_CFG1_ADICLK(0));
467 return (ADC_LIB_CFG1_ADIV(0) + ADC_LIB_CFG1_ADICLK(0));
472 constexpr uint32_t get_CFG_LOW_SPEED(uint32_t f_adc_clock) {
473 if (f_adc_clock / 16 >= ADC_MIN_FREQ_16BITS) {
474 return (ADC_LIB_CFG1_ADIV(3) + ADC_LIB_CFG1_ADICLK(1));
475 }
else if (f_adc_clock / 8 >= ADC_MIN_FREQ_16BITS) {
476 return (ADC_LIB_CFG1_ADIV(3) + ADC_LIB_CFG1_ADICLK(0));
477 }
else if (f_adc_clock / 4 >= ADC_MIN_FREQ_16BITS) {
478 return (ADC_LIB_CFG1_ADIV(2) + ADC_LIB_CFG1_ADICLK(0));
479 }
else if (f_adc_clock / 2 >= ADC_MIN_FREQ_16BITS) {
480 return (ADC_LIB_CFG1_ADIV(1) + ADC_LIB_CFG1_ADICLK(0));
482 return (ADC_LIB_CFG1_ADIV(0) + ADC_LIB_CFG1_ADICLK(0));
487 constexpr uint32_t get_CFG_HI_SPEED_16_BITS(uint32_t f_adc_clock) {
488 if (f_adc_clock <= ADC_MAX_FREQ_16BITS) {
489 return (ADC_LIB_CFG1_ADIV(0) + ADC_LIB_CFG1_ADICLK(0));
490 }
else if (f_adc_clock / 2 <= ADC_MAX_FREQ_16BITS) {
491 return (ADC_LIB_CFG1_ADIV(1) + ADC_LIB_CFG1_ADICLK(0));
492 }
else if (f_adc_clock / 4 <= ADC_MAX_FREQ_16BITS) {
493 return (ADC_LIB_CFG1_ADIV(2) + ADC_LIB_CFG1_ADICLK(0));
494 }
else if (f_adc_clock / 8 <= ADC_MAX_FREQ_16BITS) {
495 return (ADC_LIB_CFG1_ADIV(3) + ADC_LIB_CFG1_ADICLK(0));
497 return (ADC_LIB_CFG1_ADIV(3) + ADC_LIB_CFG1_ADICLK(1));
504 constexpr uint32_t get_CFG_MEDIUM_SPEED(uint32_t f_adc_clock) {
505 uint32_t ADC_CFG1_LOW_SPEED = get_CFG_LOW_SPEED(f_adc_clock);
506 uint32_t ADC_CFG1_HI_SPEED_16_BITS = get_CFG_HI_SPEED_16_BITS(f_adc_clock);
507 if (ADC_CFG1_LOW_SPEED - ADC_CFG1_HI_SPEED_16_BITS > 0x20) {
508 return ADC_CFG1_HI_SPEED_16_BITS + 0x20;
510 return ADC_CFG1_HI_SPEED_16_BITS;
515 constexpr uint32_t get_CFG_HIGH_SPEED(uint32_t f_adc_clock) {
516 if (f_adc_clock <= ADC_MAX_FREQ) {
517 return (ADC_LIB_CFG1_ADIV(0) + ADC_LIB_CFG1_ADICLK(0));
518 }
else if (f_adc_clock / 2 <= ADC_MAX_FREQ) {
519 return (ADC_LIB_CFG1_ADIV(1) + ADC_LIB_CFG1_ADICLK(0));
520 }
else if (f_adc_clock / 4 <= ADC_MAX_FREQ) {
521 return (ADC_LIB_CFG1_ADIV(2) + ADC_LIB_CFG1_ADICLK(0));
522 }
else if (f_adc_clock / 8 <= ADC_MAX_FREQ) {
523 return (ADC_LIB_CFG1_ADIV(3) + ADC_LIB_CFG1_ADICLK(0));
525 return (ADC_LIB_CFG1_ADIV(3) + ADC_LIB_CFG1_ADICLK(1));
532 constexpr uint32_t get_CFG_VERY_HIGH_SPEED(uint32_t f_adc_clock) {
533 const uint8_t speed_factor = 2;
534 if (f_adc_clock <= speed_factor * ADC_MAX_FREQ) {
535 return (ADC_LIB_CFG1_ADIV(0) + ADC_LIB_CFG1_ADICLK(0));
536 }
else if (f_adc_clock / 2 <= speed_factor * ADC_MAX_FREQ) {
537 return (ADC_LIB_CFG1_ADIV(1) + ADC_LIB_CFG1_ADICLK(0));
538 }
else if (f_adc_clock / 4 <= speed_factor * ADC_MAX_FREQ) {
539 return (ADC_LIB_CFG1_ADIV(2) + ADC_LIB_CFG1_ADICLK(0));
540 }
else if (f_adc_clock / 8 <= speed_factor * ADC_MAX_FREQ) {
541 return (ADC_LIB_CFG1_ADIV(3) + ADC_LIB_CFG1_ADICLK(0));
543 return (ADC_LIB_CFG1_ADIV(3) + ADC_LIB_CFG1_ADICLK(1));
563 #if defined(ADC_TEENSY_4)
604 #if defined(ADC_TEENSY_4)
627 #define ADC_SC1A_CHANNELS (0x1F)
630 #define ADC_SC1A_PIN_INVALID (0x1F)
632 #define ADC_SC1A_PIN_MUX (0x80)
635 #define ADC_SC1A_PIN_DIFF (0x40)
637 #define ADC_SC1A_PIN_PGA (0x80)
640 #define ADC_ERROR_DIFF_VALUE (-70000)
641 #define ADC_ERROR_VALUE ADC_ERROR_DIFF_VALUE
680 return static_cast<ADC_ERROR>(
static_cast<uint16_t
>(lhs) |
681 static_cast<uint16_t
>(rhs));
685 return static_cast<ADC_ERROR>(
static_cast<uint16_t
>(lhs) &
686 static_cast<uint16_t
>(rhs));
691 return lhs =
static_cast<ADC_ERROR>(
static_cast<uint16_t
>(lhs) |
692 static_cast<uint16_t
>(rhs));
697 return lhs =
static_cast<ADC_ERROR>(
static_cast<uint16_t
>(lhs) &
698 static_cast<uint16_t
>(rhs));
706 inline void resetError(
volatile ADC_ERROR &fail_flag) {
713 #endif // ADC_SETTINGS_H