Excellent points, rstofer.
It looks like Teensy 3.2, 3.5, and 3.6 (but not LC or 4.0) have sufficiently accurate SAR ADCs (two of them!) to get 200,000 samples/second. One does need to reduce the number of samples averaged (from the default 32) to 16 or less, but that should still leave at least 12 meaningful bits in the result, I think; I haven't verified this.
I don't think there is a way to DMA data from different analog input pins using a single ADC. I could be wrong, though.
I would try a periodic interrupt that triggers the next conversion on the current ADC, then reads the conversion result from the other ADC, and selects the input pin for the next-next sample (the current ADC). It has no waits or anything like that, so even a high-sounding rate like 200,000 interrupts per second should work okay.
I do not know if the overhead of such an interrupt is too much for SD card transfers. The interrupt can only occur between byte transfers, so it is a matter of if the interrupt causes stretches a SPI clock cycle between bytes too long.
20 channels is pushing Teensy 3.2 to its limits (and half of those analog input pins are on pads underneath, which are a bit tricky to connect to), but might work fine on a 3.5 or 3.6, as they have a much higher clock rate, and the analog pins are easier to access. I don't have a 3.5 or 3.6 myself to test, though.
I took a looksee at the
AD7606 datasheet.
It turns out AD7606 can do 200 ksamples/second
per channel, and can support both ±10V and ±5V analog voltage ranges when powered from 5V (AV
cc) while the logic voltage levels are 3.3V (V
drive). Darned interesting to my needs as well; I wonder why I haven't encountered it before! The modules at fleabay are suspiciously cheap compared to the chips themselves, although they look a lot like the circuit note.
It is completely controlled by logic-state pins (i.e., no command input, and only output is sample data; mode is set by tying control pins high or low), and is rather simple to operate. It has three main modes: serial output, byte output/8-bit parallel output, and 16-bit parallel output.
To trigger a new sample (all channels are sampled simultaneously), you pull CONVST(s) high. Within 45ns, the AD7606s pull BUSY high. It will stay high for up to 4.2us (longer if oversampling is enabled). When an AD7606 pulls BUSY back low, the samples are available.
To read the 8 samples in serial mode:
SCLK is high. The microcontroller pulls
CS low for the AD7606, and waits 35ns or longer. Then, it starts pulsing SCLK (down) 128 times at a maximum frequency of 15 MHz (and duty cycle between 40% and 60%, so this must be a single noninterrupted SPI transaction). On each rising edge of SCLK, the microcontroller receives the next bit on the D
OUTA pin. After the 128 bits, the microcontroller pulls
CS high.
The byte mode is very similar:
RD and
CS are high. The microcontroller pulls
CS low, and
RD low for at least 30ns, for the AD7606. On each rising edge of
RD, the next 8 bits are available on DB7:0.
RD must be low for at least 30ns, and high for at least 15ns, so the max
RD rate is about 20MHz at 3.3V V
DRIVE logic levels. After the 16 bytes (rising edges of
RD), the microcontroller pulls
CS high.
The 16-bit parallel mode supports a linked
CS and
RD mode:
RD and
CS are is high. The microcontroller pulls both low, and waits for at least 30ns (at 3.3V V
DRIVE), then reads the sample from DB15:0, and pulls both high again. After at least 22ns, it can repeat this for the second channel; and repeat for all eight channels.
In the byte mode, an inline assembly routine on Teensy 3.2/3.5/3.6 can read the eight samples in about 1us per AD7606.
With three AD7606s/24 channels, and using an interrupt (the AD7606s can share the data lines, as long as only one has its
CS low at any time), this is 30,000 interrupts per second, and 30ms or so of execution time, per second. Not bad at all. Whether the latencies caused by the 1us interrupts are too much jitter for the SD cards, I do not know. (It is equivalent to about 25 cycles of the SD card SPI clock running at 25MHz, but only occurs between byte transfers, not mid-byte.)
So, it seems to me, trying out the built-in ADCs on a Teensy 3.5 or a 3.6 would be the way to go.
If they don't give good enough results, I'd go for the AD7606s, unless something better is found.
If the code to handle both the sampling and the SD card storage on a single Teensy is too complicated, then I'd use two, connecting the two using UART for commands, and SPI for sample data. Then, the one storing the data to an SD card can definitely use DMA for the data (being SPI slave), and having enough RAM for buffering the data, can use the standard SdFat library to write the data to a standard FAT filesystem on the SD card, with a loop that just tries to keep up with the DMA. (The DMA is connected to a completion interrupt, which sets up the next chunks, with at least a dozen or so such chunks in your buffer.)
Teensy 4.0 should be able to control three AD7606s in byte mode. The data pins would be
16-23 (DB0 pin 19, 18, 17, 16, 22, 23, 20, DB7 pin 21); pins 0 and 1 for UART; pins 10-13 for SPI; CONVST on pin 14 and RD on pin 15; and the three CSs, three BUSYs and RESET somewhere on pins 2-9. There are an additional ten pins on the bottom of Teensy 4.0 on pads, but they're a bit tricky to solder to, but if used, would allow expanding to a fourth AD7606 for 32 channels. Teensy 4.0 would be the SPI master, although only providing CS, SCK, and MOSI/data out. The CONVST pin would be a PWM or timer output, for optimal sampling stability while still allowing precise sample rate control.
The UART is used to set the sample rate and select the channels sampled (well, ALL are sampled, but only some are forwarded), with say a single-byte "OK" or "Error" acknowledgement (for simplicity and ease of use).
The nice thing is that if connected to USB, it could switch to USB mode, and since Teensy 4.0 has a 480Mbit/s USB interface, could provide up to the maximum 200,000 samples/second per channel for all 24 connected channels, making the same thing useful as a AD7606 data acquisition controller.
If my Christmas budget allows (I'm broke
), I'll get a couple of those AD7606 modules off fleabay, and try them with my Teensy 4.0.