Continuing with this data logger application - I have a serious bug which I cannot figure out.
As I said before, the device uses an ADC on a SPI port and
writes data to a SD card. The ADC runs at a 1KHz sampling rate and
samples 8 channels. An interrupt procedure is called by timer tick
on TCE0. Time in interrupt procedure is 75us, or 7.5% of CPU time.
The interrupt does an ADC scan and stores the data to a double-
buffer; each buffer holds 2048 samples. The two buffers together use
8KB RAM, leaving 8KB for other stuff. The main program watches for
buffer full, then writes the filled buffer to SD card using
F16_blockwrite.
Some specs:
4-layer PCB with separate analog and digital ground planes.
CPU = Xmega256A3U, 32MHz
SD card on SPI_C. Have tried both FAT16 and FAT16_32 file systems.
ADC = Linear Technology LTC2372, 8-ch, 16-bit, on SPI_D
RTC = NXP PCF2129 on SPI_E
Serport = SerPortF0, 115200 baud, TX buf = 64, RX buf = 20.
My problem is that sometimes the data written to SD is corrupted.
A typical situation is that data is written OK for 20 minutes then
there will be a 5-minute stretch where the data on all channels is
near zero, but not exactly zero - samples are in the range +/-10.
Then after 5 minutes or so, the data recovers and is correct again.
I use a Serout port going to a terminal program on my computer. I
monitor stack and frame usage (with CheckStackValid and CheckFrameValid)
as well as sending the data periodically. The data is always correct,
even during the 5 minutes that it's near zero on the SD.
*SO* - How is it that I can monitor the buffer data on Serout and it
is correct, but when it is written to SD it is not correct?
Is there any way I can debug the lower-level workings of the SD
file system? I admit that a bug in the SD procedures is not likely,
but I can't think of anything else.