Setting up ISR for ADC on Zero

Actually I might’ve spoke to soon. After letting it run for a few hours I noticed some of my files were bigger then what they were supposed to be.It doesn’t happen often,but it does happen.The past week ive been trying to fix(current code below).

So DMAC alternates between 2 channels ADC set to 35.7kHz samplerate each.
And fills 1/2 of a buffer[0->512],and then fills other 1/2 of the buffer[512->1024] using a interrupt
If 1 of the halves is full, a SUSPEND is turned on
Interrupt looks for a SUSPEND,and toggles between which buffer half is full
In main loop, if a buffer half is full ,process it(write to SD card),and then if other buffer half is full,do the same. This alternates constantly…
WHAT I tried to do is set up a simple variable that increments every time a buffer half is processed and after a certain # of times,pause DMA & ISR, close SD file,and create a new one:
example: after going through/processing 250 buffer halves,pause DMA & ISR,close SD file,and create a new one.
This works and data is correct,but sometimes it adds extra data to SD card instead of being constant 256,000 bytes, it’ll sometimes be 257,024 bytes…

I have exhausted all my debugging skills.
I don’t think its my buffer size[1024], since SD take 512 bytes per blocks I thought this would be fastest way to send data.
It takes 13.6mS to fill half of buffer (512*(1/37500hZ)) = 13.6mS
After testing using millis() it takes time to write 512 bytes to SD = 5-8mS
So I know I can process data(IE write to SD) faster then I can obtain it, which is good…This should mean it wont get interrupted(IE write to SD) while the next buffer half is filled.Right?

SAMD51_ALT_CH_SD.ino (12 KB)

This is an interesting topic. I've just upgraded to a SAM21 (Zero) board from a AtMega2560 specifically to gain access to a faster ADC. I read it as the ADC could reach 300KHz but from the comments earlier even 50KHz will overwhelm the ISR. If that is the case then I'm not sure where the 300KHz factors in, other than perhaps some type of burst/averaging mode.

For my application 20KHz will be fine, it's still massively faster than what the 2560 could do. I'm measuring two AC channels (power factor) at a frequency of 1-200Hz. The line has lots of harmonics so the more reads the better. Software will determine the zero cross for voltage and current then work out the phase delta.