it looks like the code is excessive and not necessarily doing things in the right order.
based on the comments, waiting for a conversion to complete instead of using that time to write to SD ram is not efficient.
ADC->INTFLAG.bit.RESRDY = 1;
// Data ready flag cleared
while (ADC->STATUS.bit.SYNCBUSY == 1)
;
ADC->SWTRIG.bit.START = 1;
// Start ADC conversion
while ( ADC->INTFLAG.bit.RESRDY == 0 )
;
// Wait till conversion done
while (ADC->STATUS.bit.SYNCBUSY == 1)
;
ADCvalue = ADC->RESULT.reg;
I assume you're triggering the interrupt on a timer that should provide more than enough time for a conversion. Unless you think there is a problem the ISR should assume the conversion is complete, immediately start the next conversion and write the data to the ping/pong buffer. (are those wait loops really necessary)?
if the system is designed correctly, there no need to check if the conversion is ready and there's no need to break the adc value into bytes.
tempByte = (ADCvalue >> 8) & 0xFF;
if the adc value is 16 bits the ping/pong buffers should be uint16_t and write the adc value directly from the register to the buffer and handle wrap. after reading/storing the adv value, start the next conversion -- i think that's all the ISR should do.
pingPong [ind++] = ADC->RESULT.reg;
ind = BUF_SIZE == ind ? 0 : ind;
the ping/pong buffers should be the size in bytes of the block to be written to SD. if the SD block size is 512 bytes, each ping/pong buffer should be 256 uint16_t. but you should really just have a single buffer (see below)
#define PINGPONG_SIZE 256
#define BUF_SIZE 2*PINGPONG_SIZE
uint16_t pingPong [BUF_SIZE];
there's no need for the ISR to indicate when a ping/pong is full and there's no need to check if there's space. (if there isn't, something is wrong)
//signal to main loop,buffer is full
if (ind >= 512) {
state = !state;
captureActive = 0;
ind = 1;
}
//If buffer isnt full start next ADC
if (ind < 512) {
if the system is designed properly, there's enough time to write an SD block before a ping/pong buffer is filled. loop(), not the ISR should do any admin work. loop() can monitor the ind to determine when a ping/pong buffer is full. of course loop() will take longer when writing to SD
if (ping && PINGPONG_SIZE <= ind) {
ping = !ping;
writeSdBuf (& pingPong [0], SD_BLK_SIZE);
}
else if (! ping && PINGPING_SIZE > ind) {
ping = !ping;
writeSdBuf (& pingPong [PINGPONG_SIZE], SD_BLK_SIZE);
}
of course none of the above is tested. i hope it makes sense. and it needs to be that simple to get done with as few MIPS as possible
can't tell you how many times i felt what I needed to do was basically as succinct as above, only to get lost and add tons of code to figure things out and then finally sort out my misunderstandings and (basically) confirm what I originally thought but ended up with a lot of debug code.