Faster ADC sampling challenges (SPI, MCP3008)

I added photonAudio sample 4. I should stress that this is a proof-of-concept only and only sort of works.

The main thing is that the code uses two ADCs (ADC1 and ADC2). They’re run in dual-simultaneous mode, so both are sampled at the same time.

They’re also run in scan mode so multiple pins are sample sequentially, and very rapidly, 5 cycles of the ADC clock (30 MHz).

ADC1 is set to read A0, A2, and A4. ADC2 is set to read A1, A3, and A5.

44100 times per second, controlled by a hardware timer, the ADCs are set off do do their thing, and store the data in a DMA buffer, so the inter-frame spacing is rock-solid.

The DMA buffer is 1020 bytes, because something around 1024 bytes is ideal with 0.6.x and TCP, but the buffer size should be a multiple of 12 (the number of channels * 2 bytes per channel).

There are two DMA buffers, and the interrupt flag is set when the buffer is half-full so it can be sent out from loop while the other half is being filled.

From the main loop we poll the IRQ flag to see if there’s a buffer ready to be transmitted. The good part is that this is not particularly timing-sensitive.

Because all of the sampling is done via hardware, there’s no interrupt latency to worry about.

Still, this is right at the edge of what the Photon can do, so I don’t really recommend trying to send 517 Kbytes/sec. off the Photon, but it sort of works.

5 Likes