• AVR Freaks

Hot!PIC32 ADC with DMA problem

Author
nick23
New Member
  • Total Posts : 27
  • Reward points : 0
  • Joined: 2020/12/05 13:19:44
  • Location: 0
  • Status: offline
2021/01/21 10:53:27 (permalink)
0

PIC32 ADC with DMA problem

Hi,

I'm trying to load ADC samples to a buffer using DMA. So I set Timer 3 to trigger ADC convertions and once the convertion is complete it will trigger a DMA transfer. My objective is to load a buffer with all the ADC samples and once the buffer is complete I have to send it. My buffer is defined as "uint16_t buffer[20]".
So I set the DMA as follows:

    void *srcAddr  = (void *) &ADC1BUF0;
    void *destAddr = (void *) buffer

    DMAC_ChannelTransfer(DMAC_CHANNEL_1, srcAddr, 2, destAddr, sizeof(buffer), 2);

But all I get is the same sample being loaded in all of the buffers positions.


#1

13 Replies Related Threads

    nick23
    New Member
    • Total Posts : 27
    • Reward points : 0
    • Joined: 2020/12/05 13:19:44
    • Location: 0
    • Status: offline
    Re: PIC32 ADC with DMA problem 2021/01/21 16:21:53 (permalink)
    0
    I found out that Harmony is not setting the CHAEN bit. I have to set if manually with "DCH1CONSET = _DCH1CON_CHAEN_MASK;" Now I can load the vector but the problem I'm facing now is that the DMA transfer never stops. How can I stop DMA once the buffer is loaded?
    #2
    Murton Pike Systems
    Super Member
    • Total Posts : 223
    • Reward points : 0
    • Joined: 2020/09/10 02:13:01
    • Location: 0
    • Status: offline
    Re: PIC32 ADC with DMA problem 2021/01/21 17:20:41 (permalink)
    0
    There is a register to set for the amount of data to send.
     
     
    #3
    nick23
    New Member
    • Total Posts : 27
    • Reward points : 0
    • Joined: 2020/12/05 13:19:44
    • Location: 0
    • Status: offline
    Re: PIC32 ADC with DMA problem 2021/01/21 18:28:31 (permalink)
    0
    ok, setting the CHAEN bit makes the DMA start automatically which overwrites the samples in the buffer. I need the buffer to be loaded with samples, and once it's loaded I need an interrupt to tell me that it's been loaded. So I can send the samples and then start the process all over again.
    #4
    Murton Pike Systems
    Super Member
    • Total Posts : 223
    • Reward points : 0
    • Joined: 2020/09/10 02:13:01
    • Location: 0
    • Status: offline
    Re: PIC32 ADC with DMA problem 2021/01/21 18:32:35 (permalink)
    0
    There is a register to set for the amount of data to send.
    #5
    nick23
    New Member
    • Total Posts : 27
    • Reward points : 0
    • Joined: 2020/12/05 13:19:44
    • Location: 0
    • Status: offline
    Re: PIC32 ADC with DMA problem 2021/01/21 19:15:27 (permalink)
    0
    nigelwright7558
    There is a register to set for the amount of data to send.
    Could you be more specific? What register?
    #6
    Murton Pike Systems
    Super Member
    • Total Posts : 223
    • Reward points : 0
    • Joined: 2020/09/10 02:13:01
    • Location: 0
    • Status: offline
    Re: PIC32 ADC with DMA problem 2021/01/21 19:25:34 (permalink)
    4 (1)



    //set up dma
    DCH0SSA = KVA_TO_PA(&PORTB); // Move the data from PORTB
    DCH0DSA = KVA_TO_PA(&scopebuffercoherent);// Move the data to scopebuffer
    DCH0SSIZ = 2; // Source size is 2 bytes
    DCH0CSIZ = 2; // Move 2 bytes at a time
    DCH0DSIZ = scopebufferlength*2; // Move num_bytes bytes of data in total
    DCH0ECON=0; // Clear the DMA configuration settings
    DCH0ECONbits.CHSIRQ = _TIMER_2_VECTOR; // Timer 2
    DCH0ECONbits.SIRQEN = 1; // Enable Start IRQ
    DCH0ECONbits.AIRQEN = 0; // Disable Abort IRQ

    DCH0CON = 0x83; // channel enabled, priority 3






    // DCH0ECONbits.CFORCE=1;


    //wait until dma finished
    if (!DCH0INTbits.CHDDIF)
    ;

    //copy to cached buffer
    unsigned int zx;
    for (zx=0;zx<scopebufferlength-1;zx++)
    {
    scopebuffer[zx]=scopebuffercoherent[zx];
    }
     

    #7
    Murton Pike Systems
    Super Member
    • Total Posts : 223
    • Reward points : 0
    • Joined: 2020/09/10 02:13:01
    • Location: 0
    • Status: offline
    Re: PIC32 ADC with DMA problem 2021/01/21 19:26:53 (permalink)
    4 (1)
    Also be careful with DMA'ed data as it is not cached so make buffer it goes into coherent.
     
    unsigned short __attribute__((coherent)) scopebuffercoherent[scopebufferlength];
     
     
    #8
    nick23
    New Member
    • Total Posts : 27
    • Reward points : 0
    • Joined: 2020/12/05 13:19:44
    • Location: 0
    • Status: offline
    Re: PIC32 ADC with DMA problem 2021/01/21 19:48:17 (permalink)
    0
    nigelwright7558
    Also be careful with DMA'ed data as it is not cached so make buffer it goes into coherent.
     
    unsigned short __attribute__((coherent)) scopebuffercoherent[scopebufferlength];
     
     


    My PIC is a PIC32MX I thought that coherent attibute was for the MZ family only
    #9
    nick23
    New Member
    • Total Posts : 27
    • Reward points : 0
    • Joined: 2020/12/05 13:19:44
    • Location: 0
    • Status: offline
    Re: PIC32 ADC with DMA problem 2021/01/21 19:49:23 (permalink)
    0
    nigelwright7558

     


    //set up dma
    DCH0SSA = KVA_TO_PA(&PORTB); // Move the data from PORTB
    DCH0DSA = KVA_TO_PA(&scopebuffercoherent);// Move the data to scopebuffer
    DCH0SSIZ = 2; // Source size is 2 bytes
    DCH0CSIZ = 2; // Move 2 bytes at a time
    DCH0DSIZ = scopebufferlength*2; // Move num_bytes bytes of data in total
    DCH0ECON=0; // Clear the DMA configuration settings
    DCH0ECONbits.CHSIRQ = _TIMER_2_VECTOR; // Timer 2
    DCH0ECONbits.SIRQEN = 1; // Enable Start IRQ
    DCH0ECONbits.AIRQEN = 0; // Disable Abort IRQ

    DCH0CON = 0x83; // channel enabled, priority 3






    // DCH0ECONbits.CFORCE=1;


    //wait until dma finished
    if (!DCH0INTbits.CHDDIF)
    ;

    //copy to cached buffer
    unsigned int zx;
    for (zx=0;zx<scopebufferlength-1;zx++)
    {
    scopebuffer[zx]=scopebuffercoherent[zx];
    }
     
     
     



    The registers are set by the DMAC_ChannelTransfer function.
     
    #10
    Murton Pike Systems
    Super Member
    • Total Posts : 223
    • Reward points : 0
    • Joined: 2020/09/10 02:13:01
    • Location: 0
    • Status: offline
    Re: PIC32 ADC with DMA problem 2021/01/21 19:55:14 (permalink)
    0
    nick23
     
    My PIC is a PIC32MX I thought that coherent attibute was for the MZ family only


    Or use volatile ?
     
    #11
    nick23
    New Member
    • Total Posts : 27
    • Reward points : 0
    • Joined: 2020/12/05 13:19:44
    • Location: 0
    • Status: offline
    Re: PIC32 ADC with DMA problem 2021/01/22 06:07:21 (permalink)
    0
    I discovered that the issue doesn't seem to be the DMA transfer but something about the ADC. I changed the source address of the transfer to the TMR3 register and the buffer is loaded correctly. But not when I use the source address ADC1BUF0, there is only one value repeated in all the positions of the vector. It seems like only one convertion takes place so the ADC1BUF0 never changes and that would explain why I have the same value in the vector.
    #12
    Murton Pike Systems
    Super Member
    • Total Posts : 223
    • Reward points : 0
    • Joined: 2020/09/10 02:13:01
    • Location: 0
    • Status: offline
    Re: PIC32 ADC with DMA problem 2021/01/22 06:44:35 (permalink)
    0
    You will probably have to set adc to continuous mode so it keeps getting a2d values.
     
    #13
    nick23
    New Member
    • Total Posts : 27
    • Reward points : 0
    • Joined: 2020/12/05 13:19:44
    • Location: 0
    • Status: offline
    Re: PIC32 ADC with DMA problem 2021/01/22 07:00:17 (permalink)
    3 (1)
    I found the problem! it was the ADC config. The register AD1CON2 had the bits SMPI<3:0> set to interrupt after 16 samples. I think I might have changed it at some point and then I set it back to interrupt after one sample but for same reason Harmony did not change it back. So I set SMPI to "0000 = Interrupts at the completion of conversion for each sample/convert sequence" and now it works. Thanks!
    #14
    Jump to:
    © 2021 APG vNext Commercial Version 4.5