Author Topic: DMA with SPI for SD Cards  (Read 13651 times)

Offline justin@uc

  • Newbie
  • *
  • Posts: 5
  • Inspector Butters
    • View Profile
DMA with SPI for SD Cards
« on: July 11, 2012, 03:26:27 AM »
Hi Mark,

Have you attempted or given some thought into using DMA for SPI? I'm *trying* to implement DMA in the SD write routine for my M52259 board, but I'm having no luck.

Specifically, I'm trying to replace the WRITE_SPI_CMD(BYTE) function. Here is what I have so far:

Code: [Select]
void utWriteSPICmdWithDMA(unsigned char *ptrBuf, signed long ulLength)
        // Allow access to SPI for DMA

// DMA
    // Config SPI registers
    QWR = ((0 << QSPI_START_SHIFT) | (0 << QSPI_END_SHIFT));
    // Setup DMA
    DMA_SR_BCR2 = ulLength;
    DMA_SAR2  = (unsigned long)ptrBuf;                          // address of first byte to be transferred  (SARn)
    DMA_DAR2  = (unsigned long)(QSPI_ADD + 0x14);                                // address of first destination byte       (DARn) = QDR
    DMA_DCR2  = (DCR_BWC_16K | DCR_SINC | /*DCR_DINC |*/ DCR_SSIZE_BYTE | DCR_DSIZE_BYTE | DCR_START); // set up DMA operation (DCRn) and start DMA transfer
    while (!(DMA_SR_BCR2 & DSR_DONE)) { };                 // wait until the transfer has terminated


// DMA transfer done.
    DMA_SR_BCR2 = DSR_DONE;                                          // clear all status bits ready for future transfer

After running this code I probed the clock, DataIn, and ChipSelect lines, all look OK. But no writes ever take place...the QSPI data register is always empty and the DMA status register reports no error either.

There are almost NO examples or anything online relating to using DMA with QSPI on my board (or any other boards for that matter!). Your DMA+UART code helps, but there are differences (I'm guessing) that prevent it from working. Shouldn't DMA automatically take care of handling the SPI though?

Does anyone have any ideas as well? Insights? Anything is appreciated!


Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3238
    • View Profile
    • uTasker
Re: DMA with SPI for SD Cards
« Reply #1 on: July 11, 2012, 01:47:39 PM »
Hi Justin

I don't think that there are any real advantages in using DMA with the QSPI in the Kinetis for the data transfer.
Specifically to this chip, its DMA doesn't have peripheral support for the QSPI - this means that the QSPI itself will not generate DMA transfer requests.
It is still possible to use memeory to QSPI DMA transfers but this will need to be limited to the depth of the QSPI FIFO (16 bytes if I remember correctly) - I think that in your case you will be transfering larger amounts of data very quickly to the FIFO and this will then overrun. If DMA were to be used like this it would be necessary to send just DMA bursts of 16 bytes at a time and wait for each bust to be completed before continuing with the next. The result is therefore no real improvement on simply writing each byte and waiting, and the SW overhead is then also lower.

Also reading using DMA is not really possible due to the fact that the QSPI can not genate DMA requests. Any burst reading would result in QSPI underruns and so no data.

A possibility would be use 2 DMA timers set up to the SPI byte rate to control reading and writing (the speed would need to be set to slightly less that the byte rate to ensure no under/overruns). One would control data write transfers and one data read transfers from/to buffers. But again I don't think that there is any real speed advantages since the utFAT code still has to wait for the transfers to complete before it can continue. DMA transfers will only be as fast as the QSPI clock rate and so the transfer time will effectively be the same for both SW and DMA. The only advantage with DMA would be when there are SW interrupts which will stop the SW operation for a short time but the DMA operation could continue, but I don't expect this difference to be noticeable in general cases.



Offline justin@uc

  • Newbie
  • *
  • Posts: 5
  • Inspector Butters
    • View Profile
Re: DMA with SPI for SD Cards
« Reply #2 on: July 14, 2012, 04:47:35 AM »
Hi Mark,

Thank you for the information! I never realized that SPI didn't have support to generate DMA timers. The Reference Manual made it seem like it did, but I guess when they say that "the source/destination addresses can include peripheral device registers", then it would only work on a per-byte basis. How dissapointing!

So does this mean the only peripheral device that can use DMA effectively is UART?  :( How crazy...

Hmmm...I'm still trying to find ways to speed up SD writing. Byte-at-a-time seems so...slow!

Thanks again Mark. This info helps!