SdFat for Due posted

There's an issue when SdFat and the VGA library are used together (see VGA library - now with TV output - #21 by system - Arduino Due - Arduino Forum)

I've been looking into this and the problem seems to be down to an SPI overrun. The VGA library is quite a DMA hog, and what appears to happen is that in spiRec() the TX DMA sends data faster than the RX DMA can write it to memory due to the bus contention.

Looking at the data sheet, the SPI_MR register has a flag SPI_MR_WDRBT which should hold up the TX DMA until the RX DMA has read the SPI_RDR register. I'm experimenting with the following fix:

in Sd2Card.cpp, in spiRec() around line 234
after this line: #if USE_SAM3X_DMAC
add this line: pSpi->SPI_MR |= SPI_MR_WDRBT;

in spiSend() around line 268
after this line: #if USE_SAM3X_DMAC
add this line: pSpi->SPI_MR &=~ SPI_MR_WDRBT;

The idea is that in spiRec() the SPI_MR_WDRBT flag helps keep the two DMA channels in sync, but spiSend needs the flag turned off again otherwise it will hang.

I have not given this fix much testing other than running bench and QuickStart, and a modified QuickStart which outputs to VGA, which all work correctly.