SPI (master) reading using DMA?

Author
zoranj
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2009/04/08 14:48:43
  • Location: Požarevac
  • Status: offline
2009/04/08 15:12:42 (permalink)
0

SPI (master) reading using DMA?

Is there any example of SPI (master) reading using DMA?
Is it possible to do?

#1

8 Replies Related Threads

    zilym
    Super Member
    • Total Posts : 225
    • Reward points : 0
    • Joined: 2003/11/07 12:43:37
    • Location: Mesa, AZ
    • Status: offline
    RE: SPI (master) reading using DMA? 2009/04/08 15:36:00 (permalink)
    0
    Yes, it is possible. You have to use two DMA channels: one DMA channel writes bytes to the SPIxBUF register to initiate SPI transfers. The other DMA channel reads bytes from the SPIxBUF register and saves data to RAM.
    #2
    zoranj
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2009/04/08 14:48:43
    • Location: Požarevac
    • Status: offline
    RE: SPI (master) reading using DMA? 2009/04/08 17:10:23 (permalink)
    0
    Thanks to zilym

    I tried this on the PIC32MX360F512L and works well:

    // SYSCLK = 80 MHz
    // PBCLK = 40 MHz
    //...

    #define FL_SPI_CHANNEL     1
    #define SPI1_TX_DMA_CHN    1
    #define SPI1_RX_DMA_CHN    0

    unsigned char data[MAX_BUF];
    //...
       SpiChnOpen(spi_channel,
                  SPI_CON_ON | SPI_CON_MSTEN | SPI_CON_MODE8 | SPI_CON_SMP | SPI_CON_CKE,
                  fpbDiv);
    //...

       DmaChnOpen(SPI1_TX_DMA_CHN, DMA_CHN_PRI3, DMA_OPEN_DEFAULT);
       DmaChnOpen(SPI1_RX_DMA_CHN, DMA_CHN_PRI3, DMA_OPEN_DEFAULT);

    //...

       // DMA channels config:
       DmaChnSetEventControl(SPI1_TX_DMA_CHN, DMA_EV_START_IRQ_EN | DMA_EV_START_IRQ(_SPI1_RX_IRQ));
       DmaChnSetEventControl(SPI1_RX_DMA_CHN, DMA_EV_START_IRQ_EN | DMA_EV_START_IRQ(_SPI1_RX_IRQ));

       DmaChnClrEvFlags(SPI1_RX_DMA_CHN, DMA_EV_ALL_EVNTS);
       DmaChnClrEvFlags(SPI1_TX_DMA_CHN, DMA_EV_ALL_EVNTS);
       
       SpiChnClrTxIntFlag(FL_SPI_CHANNEL);
       SpiChnClrRxIntFlag(FL_SPI_CHANNEL);
      
       DmaChnSetTxfer(SPI1_TX_DMA_CHN, data, (void*)&SPI1BUF, bytes_for_reading, 1, 1);
       DmaChnSetTxfer(SPI1_RX_DMA_CHN, (void*)&SPI1BUF, data, 1, bytes_for_reading, 1);

       DmaChnEnable(SPI1_RX_DMA_CHN);
       DmaChnStartTxfer(SPI1_TX_DMA_CHN, DMA_WAIT_NOT, 0);

    //...
     
    #3
    yannsionneau
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2011/03/02 06:54:32
    • Location: Paris
    • Status: offline
    Re: RE: SPI (master) reading using DMA? 2011/03/02 08:57:56 (permalink)
    0
    This didn't work for me (on PIC32MX775F512L)

    I've put EvFlags to DMA_EV_BLOCK_DONE and set up interrupt handlers as in the spi-txfer.c example from microchip.

    The PIC32 enters in none of the two dma channel interrupt handlers.

    After some breakpoints it appears that the DMA only transfers 5 or 6 bytes and then stops doing anything even if the block size is 0x2B

    here is the state of some registers : 

    DMACON 0x0000C800
    DMASTAT 0x00000001

    I am using SPI1A port, so SPI1ABUF and _SPI1A_RX_IRQ

    DCH1SPTR 0x5
    DCH1SSIZ 0x2B

    DCH2DPTR 0x6
    DCH2DSIZ 0x2B

    DCH2CON 0x00008083
    DCH2ECON 0x1B10
    DCH2INT 0x00800C4
    DCH2SSA 0x1F805820
    DCH2DSA 0x00000620

    Channel 1 is used to transmit 
    Channel 2 is used to receive
    #4
    sborden
    Super Member
    • Total Posts : 1932
    • Reward points : 0
    • Joined: 2010/08/05 02:12:53
    • Location: 0
    • Status: offline
    Re: RE: SPI (master) reading using DMA? 2011/03/02 12:22:30 (permalink)
    0
    Did you use the library functions as above also?
    #5
    yannsionneau
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2011/03/02 06:54:32
    • Location: Paris
    • Status: offline
    Re: RE: SPI (master) reading using DMA? 2011/03/02 12:38:04 (permalink)
    0
    What do you mean ?
    #6
    sborden
    Super Member
    • Total Posts : 1932
    • Reward points : 0
    • Joined: 2010/08/05 02:12:53
    • Location: 0
    • Status: offline
    Re: RE: SPI (master) reading using DMA? 2011/03/02 12:56:40 (permalink)
    0
    I mean using the provided library functions and not home-grown code.
    #7
    yannsionneau
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2011/03/02 06:54:32
    • Location: Paris
    • Status: offline
    Re: RE: SPI (master) reading using DMA? 2011/03/03 04:22:10 (permalink)
    0
    Well yes I used the code in the post #3 of this thread.
    #8
    dmytro
    New Member
    • Total Posts : 14
    • Reward points : 0
    • Joined: 2012/11/24 09:08:03
    • Location: 0
    • Status: offline
    Re: RE: SPI (master) reading using DMA? 2013/03/24 14:02:07 (permalink)
    0
    Hi there,
    I noticed same kind of issue when was trying to apply DMA for SPI transactions(SPI master).
    I don't use xc32 nor its peripherals library. Both DMA and SPI drivers are self-written and work fine in all other cases.
     
    What I observe is that once DMA channel is configured to start the transaction with some event(SPI RX irq in my case) it does not take into account CFORCE bit in DMAxECON register. Thereby SPI transaction is never started.
    To workaround this I had to manually start SPI transaction by writing first byte into SPI TX/RX buffer. Of course now I have to cautious about the transaction size(-1) and start address of source buffer(+1) for DMA SPI TX channel. Applying all constraints I can make it work for any-sized transactions.
     
    Is there any errata about that? I'm using 32MX795F512L.
    #9
    Jump to:
    © 2018 APG vNext Commercial Version 4.5