DMA, part 2 of 4 Part 1: Working on DMAPart 2: Direct Memory Access on the PIC32MZPart 3: DMA SD card reads on the PIC32MZPart 4: Updated SPI SD DMA code and DMA Pattern Matching

Direct Memory Access on the PIC32MZ

All 4 posts in DMA
  1. Working on DMA
  2. Direct Memory Access on the PIC32MZ
  3. DMA SD card reads on the PIC32MZ
  4. Updated SPI SD DMA code and DMA Pattern Matching

What is Direct Memory Access (DMA)?

Direct Memory Access is a way for the CPU to offload the work of data transfers either to or from a peripheral to an external module that can take care of the transfer in the background and let the CPU know when it's done.
DMA is, in my opinion, one of the most powerful things found on microcontrollers and a big differentiator between them. But how does it work and why would I want to use it?

In my LCD example I was sending an image of Tux to the LCD. What if I now want to expand that to read frames from an SD card and blit them to the LCD? As per my example, I'd do this:

while (!F_EOF)
{       
    // Read frame from disk
    f_read(&file, frame, FRAME_SIZE, &bytes_read); 

    // Set LCD window position and size
    LCD_set_address(0,0,FRAME_WIDTH - 1,FRAME_HEIGHT - 1);
    LCD_write_command(0x2C);
    PMADDR = 1;         

    // Send the pixel data to the LCD
    for (cnt = 0; cnt < FRAME_SIZE; cnt++)
    {
        PMP_wait();
        PMDIN = Tux[cnt];
    }
}

OK, that works fine but it also means the CPU is occupied 100% of the time in that for loop, just for sending pixels to the LCD. The Tux image was 210 x 248 pixels big, which is fairly big. What kind of frame rate could we expect from such an approach?
210 x 248 x 2 = 104,160 bytes per frame. If I want to do 30 frames per second, that translates to 3,124,800 bytes per second that I need to read from the SD card and send to the LCD. That might be possible, but just barely.

Let's expand this example to an actual LCD sized frame, 320 x 240 at 30 fps. This is 320 x 240 x 2 x 30 = 4,608,000 bytes per second to read and send to the LCD. Currently, even running the SD card at 50MHz I only get about 3.3MB/s reading, so this would be impossible. How could we speed this up? Well, the slow part is the SD code, the writing to the LCD is actually quite fast. So what I want is some way to spend less CPU time sending data to the LCD and devote more CPU time to reading from the SD card. In effect, I want some way to send the data to the LCD that doesn't involve me waiting around in a for loop. Well, that's what the DMA module can do for us. It can read and write from peripherals or ports in the background without using any CPU time, which means we are free to do other tasks, like reading from an SD card, while it is busy.

Let's take a look at the official block diagram of the DMA controller (click on it for an enlarged view):

PIC32MZ - I2C - PIC32MZ - DMA module

From the diagram you can see that the CPU and the DMA module are separate. The CPU can give the DMA module an instruction, like "Send the data in the frame array to the PMP module" and then DMA module will start doing that immediately. This instruction to the DMA module only takes a few lines of code to set up, and therefore is much, much faster than having to run through an entire for loop. Again, it also happens entirely in the background, without the CPU's involvement, which leaves us free to do whatever we want while it's busy.

To summarise: One of the biggest advantages of DMA is it frees up the CPU to do other work while large data transfers are happening.

Using DMA on the PIC32MZ

The PIC32MZ has eight of these DMA channels, and each of these can transfer up to 64kB at a time. It runs directly off of the System Clock (SYSCLK). There are also advanced features like chaining channels together, pattern matching and CRC generation. Today we're going to look at how to set up DMA transfers and use pattern matching too.

For starters, let's see what information we need to give the DMA controller:

  • The address of the source of the data
  • The size of the source of the data, in bytes
  • The cell size (how much data to transfer each time), in bytes
  • The address of the destination of the data
  • The size of the destination of the data, in bytes
  • The source of the "clock signal" or interrupt to tell it to move the data (covered later)

In theory, it's very simple but this is the PIC32MZ. It takes your cute "theory" and laughs at it before ripping out your heart and laughing at you. There are many things the documentation either doesn't mention or describes very poorly. The most important of them is this:

Any buffers you use **MUST be declared coherent or nothing will work**

Note

Coherent? The memory on the PIC32 is a bit slow and peripherals use various tricks, like caching or making their own copies of data, to get better speed. The problem with this is that two devices accessing the same area of memory can end up reading different values from the same memory location due to this. In DMA, this would lead to disaster. The coherent memory space is one in which no caching or tricks are allowed and everything accesses the memory directly. This means it's slower but more reliable.

If you look in any Harmony example that use DMA, they declare their buffers like this:

unsigned short APP_MAKE_BUFFER_DMA_READY buffer[1024];

It turns out that APP_MAKE_BUFFER_DMA_READY is a friendly way of saying:

unsigned short __attribute__ ((coherent, aligned(16)))

Which tells the compiler to assign the array in coherent memory. So, where before you had to declare your buffer like this:

unsigned short read_buffer[1024]

You now need to declare it like this:

unsigned short __attribute__ ((coherent, aligned(16))) read_buffer[1024]

It looks confusing but it's not a huge change. Please remember that the 16 is the number of bits, so for an unsigned char you'd need to change that to 8.

Note

If you prefer using heap memory and malloc() and free() the coherent memory version of those are __pic32_alloc_coherent() and __pic32_free_coherent().
Remember though, if you use heap memory that you need to specify a heap size under XC32 compiler options or it will not work.

OK, enough theory for now, let's take a look at some code to send a 16-bit buffer to the PMP:


volatile DMA_DONE_FLAG = 0; void LCD_blit(unsigned short *buffer, int num_bytes) { DCH0CONbits.CHEN = 0; // Turn off this channel DCH0SSA = virt_to_phys(buffer); // Move the data from the [buffer] array DCH0DSA = virt_to_phys((const void*)&PMDIN);// Move the data to the PMDIN register DCH0SSIZ = DMA_TRANSFER_SIZE; // Move num_bytes bytes of data in total DCH0CSIZ = 2; // Move 2 bytes at a time DCH0DSIZ = 2; // Destination size is 2 bytes DCH0ECON=0; // Clear the DMA configuration settings DCH0ECONbits.CHSIRQ = _PMP_VECTOR; // Move data on PMP interrupt DCH0ECONbits.CHAIRQ = _PMP_ERROR_VECTOR; // Abort on PMP error DCH0ECONbits.SIRQEN = 1; // Enable Start IRQ DCH0ECONbits.AIRQEN = 1; // Enable Abort IRQ DCH0CONbits.CHPRI = 3; // The priority of this channel is 3 (highest) DCH0CONbits.CHEN = 1; // Turn this channel on now IPC33bits.DMA0IP = 3; // Set DMA 0 interrupt priority to 3 IPC33bits.DMA0IS = 1; // Set DMA 0 interrupt sub-priority to 1 IFS4bits.PMPIF = 0; // Clear the PMP interrupt flag IFS4bits.DMA0IF = 0; // Clear the DMA channel 0 interrupt flag IEC4bits.DMA0IE = 1; // Enable the DMA 0 interrupt DCH0INTbits.CHBCIE = 1; // Enable the Channel Block Transer Complete (CHBC) Interrupt DCH0ECONbits.CFORCE = 1; // Force the start of the transfer now DMACONSET=0x8000; // Turn the DMA module on } // Interrupt handler void __attribute__((vector(_DMA0_VECTOR), interrupt(IPL3SRS), nomips16)) DMA0_handler() { IFS4bits.DMA0IF=0; // Clear the DMA channel 0 interrupt flag IEC4bits.DMA0IE=0; // Disable the DMA 0 interrupt DMA_DONE_FLAG = 1; // DMA transfer is done }

Important: Before continuing, I want to mention again that this can transfer a maximum of 65,536 bytes. This means is cannot transfer an entire 320x240x2 bytes frame of data at one time. That can be accomplished by DMA chaining or interrupt handling, neither of which I am going into today.

There are a few new things here. First of all, what is virt_to_phys? Then what's this IRQ-related stuff? Well, virt_to_phys is the name I copied from the datasheet. Let's take a look at what it does:

extern __inline__ unsigned int __attribute__((always_inline)) virt_to_phys(const void* p) 
{ 
 return (int)p<0?((int)p&0x1fffffffL):(unsigned int)((unsigned char*)p+0x40000000L); 
}

Easy, right? Seriously though, what it's doing is converting the virtual memory address of something to a physical memory address because the DMA module works with physical addresses.

Note

Virtual vs Physical memory. To put it very simply, the PIC32 takes the physical memory and maps it into segments (like KSEG0, KSEG1, etc) some of which are cachable and some of which are not. It uses something called Fixed Mapping Translation (FMT) to translate these addresses to the actual physical memory location when they are used. The DMA module requires the actual physical address of the memory used, so we need to translate the pic32's virtual memory address into a physical address, which is what virt_to_phys() does.

The next thing you'll notice is we have to supply a "source" interrupt for the DMA transfers. If you remember from the LCD example, in PMP_init() I had this line:

    PMMODEbits.IRQM = 1;    // IRQ at the end of the Read/Write cycle

This means that after any PMP transfers is completed an interrupt will be generated. We do not need to write the Interrupt Service Routine (ISR) for this, it's all handled internally and the DMA module will intercept the interrupt and clear the interrupt flag for us each time.
There is also the option to abort the DMA transfer if the PMP error interrupt is generated, that's what _PMP_ERROR_VECTOR is doing.
Next, we can see that each DMA channel has a priority, just like interrupts did. This priority is also important in DMA chaining.

A word on interrupts. First, why have I changed to using the Shadow Register Set instead of the software interrupts? Simply put, it's faster because it means the PIC32 doesn't have to save the contents of all the many registers to memory before it calls the interrupt service routine (ISR). Before using this feature, it needs to be enabled, usually somewhere after set_performance_mode() in your main() function like this:

PRISS = 0x76543210; // Assign shadow register sets to interrupt priorities 1 through 7

When the DMA transfer is done, it can generate an interrupt to let us know it's done. Knowing what we know about ISRs and how they take valuable time, you may be tempted to do this:

while (DCH0INTbits.CHSDIF == 0);    // Wait for DMA transfer to finish

However, this would be a big mistake. In the DMA datasheet in a code example they say:
" continuously polling the DMA controller in a tight loop would affect the performance of the DMA transfer "
You could check the flag, wait a few microseconds and check again but I prefer to use the interrupt approach as, in theory, it could result in better turn-around times. There are 8 different kinds of interrupts that can be generated which makes the DMA module very flexible.

Note

Can't we do something to shorten that horrendous ISR declaration? Turns out yes, we can. Somewhere in your code, you can define:
#pragma interrupt DMA0_handler IPL3SRS vector _DMA0_VECTOR
and then later in your code, for the actual ISR function, you can just say:
void DMA0_handler(void)
Which is quite a nice change from that mess up above. All a matter of personal preference, really.

The last thing I want to take a look at today is a really cool ability of the DMA module called "Pattern Matching". This is a way to abort a DMA transfer upon receiving a certain byte / word. This pattern can be either 8 or 16 bits.
This is very useful in reading from SD cards, because before we read a block we have to output 0xFF until the SD card returns the 0xFE token to tell us it's ready to give us the data. You can set up a pattern match like this:

DCH0ECONbits.PATEN = 1;     // Enable abort on pattern match
DCH0CONbits.CHPATLEN = 0;   // 8-bit pattern
DCH0DAT = 'รพ';              // Character 0xFE

Right, that's long enough for one day. Next time I'll write about how you can use two DMA channels to read from an SD card.

24 comments
  1. Richard Watson

    Great write-up.

    We are currently running into an issue with data over-running during DMA to PMP transfers on the PIC32MZ. Driving to an LCD TFT which requires a clock rate of ~6Mhz. With the CPU rate running at 24Mhz, the DMA is spamming the PMP at that rate. With the PMP running at 6Mhz, 75% of the bytes are being dropped.

    Is there a way to slow down the DMA block transfer or enable hand-shaking between the PMP and DMA to prevent the data loss?

    1. Aidan Author

      Hi there Richard,

      Apologies for the late reply. You may have solved this already but here goes!

      It sounds like the LCD just can't keep up. The DMA peripheral itself runs at a fixed speed but each transfer is triggered by an interrupt, in the case of the PMP it's the PMP IRQ.

      So what you need to do is slow down the PMP itself, which will cause the PMP IRQ to be generated slower and thus slow the DMA transfers down too.

      Have you played with these settings?


      PMMODEbits.WAITB = 3; // Maximum wait / slowest speed
      PMMODEbits.WAITM = 15; // Maximum wait / slowest speed
      PMMODEbits.WAITE = 3; // Maximum wait / slowest speed

      If you find that it's still too fast, you may need to either slow down your system clock or slow down the peripheral bus block that controls the PMP peripheral, which is PBCLK2. To do that, look in my code example and you'll see there's a set_performance_mode() function. Change this line:
      PB2DIVbits.PBDIV = 1; // Peripheral Bus 2 Clock Divisor Control (PBCLK2 is SYSCLK divided by 2)

      from 1 to 2 (or whatever) but be aware that this will slow down all the other peripherals on PBCLK2 as well.

      1. Richard Watson

        Hi Aidan,

        Attempting to reduce the peripheral clock results in a white screen (no pixels painted). Any attempt to use a timer alarm function to trigger the DMA transfers results in a white screen. Only by running the Peripheral clock 1:1 to the SYSCLK and the LCC force starting the DMA transfer on each state machine update does the screen paint work as expected, though there is an artifact resulting in a single pixel shift to the left that appears constantly and sporadically on the screen. So I suspect we are still running too fact but have not yet found a way to slow it down there the system still works.

        1. Aidan Author

          Hi Richard,

          If it's possible could you send me the code or at least paste the DMA setup and send, PMP setup and peripheral clock setup parts here so I can take a look at them?

          1. Richard Watson

            Hi Aidan,

            Just confirming you received my e-mail? Was sent last Wednesday. I understand if you're busy, but just wanted to make sure it went through.

            Cheers,
            -Rich

            1. Aidan Author

              Hi Richard,

              Sorry for not getting back to you sooner. I did receive the email and looked at the code a few days ago.

              The Harmony code is unfortunately really hard to read. The purpose of my site was to avoid using Harmony after all, specifically due to these cases where it doesn't work and then you're stuck debugging the nightmare that is Harmony code.

              From what I can see of the code it looks like it's doing the right things. I know I suggested it before but have you tried changing the delays here:

              PLIB_PMP_WaitStatesDataHoldSelect(0, PMP_DATA_HOLD_1); //1
              PLIB_PMP_WaitStatesDataSetUpSelect(0, PMP_DATA_WAIT_ONE); //PMP_DATA_WAIT_ONE
              PLIB_PMP_WaitStatesStrobeSelect(0, PMP_STROBE_WAIT_1); //1

              to something higher than 1, like even 3 to slow down the PMP transfers? The white screen sounds like it just isn't sending anything at all.

              Also, have you tried slowing the whole system down by running at something like 20MHz, just to see if the LCD works then?

              1. Richard Watson

                Yes, adjusting the Setup or Hold timing has no effect. Lengthening the strobe results in dropped pixels (the image gets compressed horizontally and only takes up 1/2 the screen).

                Yes, slowing SYSCLK down results in a refresh rate that is too slow and the screen flickers.

                1. Aidan Author

                  So apart from the flickering and occasional error it sounds like the DMA part of the code is displaying the data right, but too fast and you're dropping pixels. Hmmm.

                  It looks like you're using a 320x240 LCD but is it 8bit or 16bit? I'm seeing the frame buffer is RGB332. Finally, which chipset does the LCD use? Is it SSD1963 based?

                  1. Richard Watson

                    The display is 24-bit but we're using 8-bit, 332 mode due to the width of the PMP. New Haven NHC-3.5-320240MF-ATXL#-T-1

                    The chipset is an NV3035C:
                    http://www.newhavendisplay....

                    1. Aidan Author

                      Hi there Rich,

                      I had a look at the chipset and board. I've never used it before but it seems fairly standard.

                      Are you using the 24.75MHz reference clock to drive the display? If so, when you changed the system clock speed to make it slower did you adjust the reference clock to ensure it remained at 24.75MHz?

                      Running out of ideas I'm afraid :/

                      1. Richard Watson

                        Not sure what you mean by the 24.75MHz ref clock? I don't see anything in the data sheets about a 24.75MHz clock.

                        We're using the PMP Write Strobe to drive the display CLK_IN

                        1. Aidan Author

                          Yeah looking again I somehow read the RGB max clock as 24.75MHz, not sure how I did that. I'll blame it on late night reading of datasheets.

                          Just looking at your email again I see your System clock is 99MHz and from the comments you're running PBCLK2 with a divider of 0, so 1:1 with system clock and only that seems to work? That's really odd because usually running it like that causes weird timing issues in my code. I've run PMP and DMA at various PBCLK2 divisors in the past with an LCD with no issues so I'm wondering if the issue isn't somewhere within the Harmony code that's causing issues.

                          Have you tried running it without DMA, just via the PMP?

                          Also, these pointers, are they all allocated coherent?

                          cntxt->layer.active->buffers[cntxt->layer.active->buffer_read_idx].pb.pixels

                          1. Richard Watson

                            This is where I am confused. I was under the impression the DMA was linked to the SYSCLK and when the PMP Peripheral clock was slowed down there creates a data rate mis-match and pixels are lost (the display supports this hypothesis when I do this: compressed image on the horizontal that only occupies half the screen). I couldn't find any documentation or mention of how to disconnect the DMA from SYSCLK and slow it down to match the PMP clock rate.

                            Yes, I have tried removing the DMA and writing the PMP port directly, I don't recall the result of that experiment but it didn't work.

                            Yes, the pointers are declared coherent.

                            1. Aidan Author

                              Hi Richard,

                              Apologies for a late reply again. We had a covid-19 spike and work became really hectic for a while as we adjusted.

                              The DMA peripheral is linked to the SYS_CLK but each transfer is triggered by the PMP so it shouldn't make a difference what rate it runs at. That is what I mean about "slowing it down", sorry if that was explained badly. In other words, slowing down PBCLK2 will slow down the PMP which should slow down the rate at which the PMP generates an interrupt and triggers a DMA transfer. And because the interrupt sets a flag when it's triggered, there shouldn't be a case where a clock rate mismatch matters at all. It also therefore shouldn't matter what frequency the system clock is running at, in my opinion.

                              As you say the documentation is missing in places and this is all based on what I've been doing with DMA myself. For example, I have no problem running SPI and DMA at various dividers and frequencies to read from an SD card, where such problems become immediately noticeable.

                              If the LCD stuff doesn't work with just the PMP then surely there's an issue in the code somewhere? DMA doesn't change anything it just offloads it, after all.

                              Sorry I can't be of more help, I'm not a PIC32MZ wizard or anything :) I just started this blog because I was frustrated with the lack of information online and wanted to share what I'd found.

                              1. Richard Watson

                                No worries Aidan,

                                I found another bug in the LCC where the DMA cell size was set incorrectly to the number of pixels, so it was spamming the full block of data instead of 1 pixel at a time before waiting for the interrupt.

                                So success on that front, now have the PMP pushing data at ~5Mhz (6Mhz is ideal) and the refresh rate is now too slow, running at 100 uSec right now, needs to be at least 70 uSec. Not sure how to get more speed out of the LCC->DMA-> PMP, so working on that.

                                The PMP Wait states are set for the display timing settings right now and even with the PMP hold = 0, there is a lot of hold time on the scope, which may just be part of the PMP design.

                                Both the DMA and PMP interrupts are set to the highest priorities/subpriorities while all the other interrupts are lower.

                                The Peripheral2 clock is running at 99Mhz and the system tells me I cannot run it any faster. SYSCLK is also 99Mhz.

  2. colle

    hi Aidan, hope you're all right.
    i'm trying to improve my TFT speed by adding the DMA transfer. i followed this thread and all works fine if i have to write the buffer only one time on the display. but if i try to write the same buffer several times it doesn't work.

    i try this:
    basically your DMA routines


    extern __inline__ unsigned int __attribute__((always_inline)) pmp_virt_to_phys(const void* p) {
    return (int)p<0?((int)p&0x1fffffffL):(unsigned int)((unsigned char*)p+0x40000000L);
    }


    void LCD_blit(unsigned short *buffer){
    IEC4bits.DMA2IE = 0; // Disable the DMA 0 interrupt
    IFS4bits.DMA2IF = 0; // Clear the DMA channel 0 interrupt flag

    DMACONCLR=0x8000; // Turn the DMA module off
    DCH2ECON=0; // Clear the DMA configuration settings
    DCH2CON=0; // Clear the DMA configuration settings

    DCH2CONbits.CHEN = 0; // Turn off this channel
    DCH2ECONbits.CABORT = 1;
    DCH2CONbits.CHPRI = 3; // The priority of this channel is 3 (highest)

    DCH2SSA = pmp_virt_to_phys(buffer); // Move the data from the [buffer] array
    DCH2DSA = pmp_virt_to_phys((const void*)&PMDIN);// Move the data to the PMDIN register

    DCH2SSIZ = 38400; // Move num_bytes bytes of data in total
    DCH2CSIZ = 2; // Move 2 bytes at a time
    DCH2DSIZ = 2; // Destination size is 2 bytes

    DCH2ECONbits.CHSIRQ = _PMP_VECTOR; // Move data on PMP interrupt
    DCH2ECONbits.CHAIRQ = _PMP_ERROR_VECTOR; // Abort on PMP error
    DCH2ECONbits.SIRQEN = 1; // Enable Start IRQ
    DCH2ECONbits.AIRQEN = 1; // Enable Abort IRQ

    IPC34bits.DMA2IP = 3; // Set DMA 2 interrupt priority to 3
    IPC34bits.DMA2IS = 1; // Set DMA 2 interrupt sub-priority to 1
    IFS4bits.PMPIF = 0; // Clear the PMP interrupt flag
    IFS4bits.DMA2IF = 0; // Clear the DMA channel 2 interrupt flag
    IEC4bits.DMA2IE = 1; // Enable the DMA 0 interrupt
    DCH2INTbits.CHBCIE = 1; // Enable the Channel Block Transer Complete (CHBC) Interrupt

    DMACONSET=0x8000; // Turn the DMA module on
    DCH2CONbits.CHEN = 1; // Turn this channel on now
    DCH2ECONbits.CFORCE = 1; // Force the start of the transfer now

    }

    void __attribute__((vector(_DMA2_VECTOR), interrupt(IPL3SRS), nomips16)) DMA2_handler()
    {

    dma_done_flag = 1; // DMA transfer is done
    LED_3_ON();
    IFS4bits.DMA2IF=0; // Clear the DMA channel 0 interrupt flag
    IEC4bits.DMA2IE=0; // Disable the DMA 0 interrupt
    //
    }

    then i create an array with 19200 pixels RGB

    for (i=0; i<40;i++){
    for(b=0; b<480; b++){
    if (b <160)
    buffer[b+(480*i)]=RED;
    else if (b >= 160 && b<320)
    buffer[b+(480*i)]=GREEN;
    else
    buffer[b+(480*i)]=BLUE;
    }
    }

    an then I send the array to the display. if i do this one time all works fine

    LCD_blit(buffer);

    but if i want to send the same buffer 20 times for fill the display the program is stuck on the while (!dma_done_flag) line

    for(_i = 0; _i<20; _i++){
    LCD_blit(buffer);
    while (!dma_done_flag);

    dma_done_flag = 0;
    }

    the isr is executed at the end of the DMA transfer, I've inserted the line LED_3_ON(); to verify that and the led turning on.

    any ideas?

    thanks

    1. Aidan Author

      Hi colle, long time no see. I'm fine thanks, and you?

      I can't see any problems in the code except for one potential one. With the dma_done_flag, did you declare it volatile?
      i.e. volatile char dma_done_flag? If you don't it won't update the value in the ISR. Apart from that I can't see anything wrong, but I can't test anything this week all my PIC32 stuff is at work :)

      1. colle

        i'm good thanks!!
        yes, i declare it volatile. during the night i rewrite part of the main program and now this code works:


        for(_i = 0; _i<20; _i++){
        LCD_blit(buffer);
        while (!dma_done_flag);

        dma_done_flag = 0;
        }

        but obviusly the CPU is stuck in the for loop. so the time divverence between DMA and pixel to pixel write is 7ms.

        i'm trying now to retrigger the buffer transfer via DMA in the ISR loop, something like this:


        volatile char dma_done_flag = 0;
        volatile unsigned int dma_byte_to_transfer = 768000;
        volatile unsigned int dma_block_to_transfer = 38400;

        void __attribute__((vector(_DMA2_VECTOR), interrupt(IPL3SRS), nomips16)) DMA2_handler()
        {
        dma_byte_to_transfer -= dma_block_to_transfer;

        if (dma_byte_to_transfer > 0){
        DCH2SPTR = 0;
        DCH2DPTR = 0;
        DCH2CONbits.CHEN = 1; // Turn this channel on now
        DCH2ECONbits.CFORCE = 1; // Force the start of the transfer now
        }

        else {
        dma_done_flag = 1; // DMA transfer is done
        IEC4bits.DMA2IE=0; // Disable the DMA 0 interrupt
        }
        IFS4CLR = _IFS4_DMA2IF_MASK;
        }

        and in the main program:

        TFT_set_window (1,1,800,480);
        TFT_write_command(SSD1963_WRITE_MEMORY_START);
        LCD_blit(buffer);

        but for now it's not workimg

        1. Aidan Author

          Hi colle,

          With that code, are you trying to send the same buffer (position 0 to 38400) to the LCD multiple times?

          I'll try and have a look at that tomorrow, Im almost sure something is missing there but I can't see what it is :) If the code isn't a secret maybe you could share it with me, I might be able to help better then.

          And good luck!

          1. colle

            how can i send to you the file?

            1. Aidan Author

              You can email it to me via email :)

              1. Richard Watson

                Where is your e-mail? I would also like to send you the code you requested.

                Thanks!

  3. Ankur Agarwal

    Hi Aidan, you are doing a wonderful work here. I have been learning to program PIC32MZ by reading your blog. I am trying to trigger DMA transfers to LATE to send digital data out to a R2R ladder. I want fast transfers. I am using TIMER2 module with PR2 set at 10 and Presclaer set to 1:1, to trigger the DMA transfers. What I have noticed is when I reduce the PR2 to any value less than 10, say 8, the DMA triggers only after 16 cycles. I am using the set_performance_mode() function you have made.
    My question is, where you able to achieve DMA triggering with TIMER set to say 20 ns period? The fastest I get is 100 ns period. If u are able to trigger at 20 ns period, can u share the code?


    #include "config_MZ_EF.h"

    void TIMER4_init(int frequency)
    {
    T4CONbits.ON = 0; // disable Timer4
    delay_us(1);
    TMR4 = 0x00; // reset Timer4 counter
    T4CONbits.TCKPS = 0b000; // set TIMER4 pre-scaler
    PR4 = (SYS_FREQ / 2 / frequency / 1) - 1; // set the time period for TIMER4
    T4CONbits.ON = 1; // enable TIMER4
    delay_us(1);
    }

    uint8_t __attribute__((coherent)) sineBuffer[100];

    void DMA0_init(void)
    {
    IEC4bits.DMA0IE = 0; // disable DMA CH0 interrupt
    IFS4bits.DMA0IF = 0; // clear DMA CH0 interrupt flag

    DMACONbits.ON = 1; // enable DMA controller
    DCH0CONbits.CHEN = 0; // disable DMA CH0
    DCH0CONbits.CHAEN = 1; //enable Channel Automatic Enable
    DCH0CONbits.CHPRI = 0b11; // set priority as 3 (highest)
    DCH0CONbits.CHAED = 1; // allow events when channel is disabled
    DCH0ECONbits.CHSIRQ = _TIMER_4_VECTOR; // set the Channel Transfer Start IRQ
    DCH0ECONbits.SIRQEN = 1; // enable start IRQ
    //DCH0INTbits.CHCCIE = 1; //enable DMA CH0 Cell Transfer complete bit

    //program the transfer
    DCH0SSA = KVA_TO_PA((uint32_t *)sineBuffer); // source address
    DCH0DSA = KVA_TO_PA((uint32_t)&LATE); // destination address
    DCH0SSIZ = 10; // source size in bytes
    DCH0DSIZ = 1; // destination size in bytes
    DCH0CSIZ = 1; // No. bytes to be transfered per event
    DCH0INT = 0; // diable all DMA interrupts and flags
    DCH0CONbits.CHEN = 1; // enable DMA CH0

    //DCH0ECONbits.CFORCE = 1; // initiate conversion
    }

    void main()
    {
    set_performance_mode(); // Set max performance mode
    INTCONbits.MVEC = 1; // Enable Multi-vectored Interrupt
    PRISS = 0x76543210; // Assign shadow register sets to interrupt priorities 1 through 7

    TRISC = 0x0000;
    LATCbits.LATC13 = 1; // keep DigiPOT inactive
    LATCbits.LATC14 = 1; // keep DigiPOT inactive

    TRISE = 0x00;
    LATE = 0x00;

    int i;

    for(i=0;i<10;i++)
    {
    sineBuffer[i] = (sin(2*PI*i/10)+1)*127;
    }

    TIMER4_init(10000000);
    DMA0_init();

    while(1)
    {

    }

    }

  4. Aravind TS

    hai Aidan your blogs are very helpfull.You are doing a great job.You helped me alot.I want to ask you on DMA chain mode transfer (ping pong mode) .Do you happend to have respective driver for that.i wriiten a code but its not working

    This my code

    DMACON = 0;
    DCRCCON = 0;

    IEC4bits.DMA5IE = 0; // disable DMA channel 5 interrupts
    IFS4bits.DMA5IF = 0; // clear existing DMA channel 5 interrupt flag

    IEC4bits.DMA6IE = 0; // disable DMA channel 6 interrupts
    IFS4bits.DMA6IF = 0; // clear existing DMA channel 6 interrupt flag

    IPC34bits.DMA5IP = 5; // DMA Channel 5 Interrupt Priority 2.
    IEC4bits.DMA5IE = 1; // DMA Channel 5 Interrupt is enabled.

    IPC35bits.DMA6IP = 2; // DMA Channel 6 Interrupt Priority 5.
    IEC4bits.DMA6IE = 1; // DMA Channel 6 Interrupt is enabled.

    DMACONbits.ON = 1; // DMA Module is enabled.

    DCH5CONbits.CHPRI = 2; // priority 3, no chaining
    DCH5CONbits.CHEN = 1; // turn channel on
    //DCH5CONbits.CHAED = 1; // Allow channel to be chained.

    DCH6CONbits.CHPRI = 3; // priority 3, no chaining
    DCH6CONbits.CHCHN = 0; // priority 3, chaining
    DCH6CONbits.CHEN = 1; // turn channel on
    // DCH6CONbits.CHAED = 1; // Allow channel to be chained.

    DCH5ECONbits.CHSIRQ = _ADC_DATA9_VECTOR; // // ADC Channel Start IRQ
    DCH5ECONbits.SIRQEN = 1; // start IRQs, no pattern match

    DCH6ECONbits.CHSIRQ = _ADC_DATA9_VECTOR; // ADC Channel Start IRQ
    DCH6ECONbits.SIRQEN = 1; // start IRQs, no pattern match

    DCH5SSA = sourceAddr; // transfer source physical address
    DCH5DSA = destinationAddr1;// transfer destination physical address

    DCH5SSIZ = 2; // source size 200 bytes
    DCH5DSIZ = 200; // destination size 200 bytes
    DCH5CSIZ = 2; // 200 bytes transferred per event

    DCH6SSA = sourceAddr; // transfer source physical address
    DCH6DSA = destinationAddr2;// transfer destination physical address

    DCH6SSIZ = 2; // source size 200 bytes
    DCH6DSIZ = 200; // destination size 200 bytes
    DCH6CSIZ = 2; // 200 bytes transferred per event

    DCH5INT = 0; // All DMA Interrupts are disabled.
    //DCH5INTbits.CHDDIE = 1; // DMA Destination Done interrupt enabled.
    // DCH5INTbits.CHERIE = 1; // Address Error Interrupt
    // DCH5INTbits.CHBCIE = 1; // Block Transfer Complete Interrupt

    DCH6INT = 0; // All DMA Interrupts are disabled.
    // DCH6INTbits.CHDDIE = 1; // DMA Destination Done interrupt enabled.
    // DCH6INTbits.CHERIE = 1; // Address Error Interrupt
    // DCH6INTbits.CHBCIE = 1; // Block Transfer Complete Interrupt

    DCH6INT = 0xff0000; // Block Transfer Complete Interrupt
    DCH5INT = 0xff0000; // Block Transfer Complete Interrupt

    DCH5CONbits.CHCHNS = 0; // Chain to lower priority.
    // DCH5CONbits.CHCHN = 1; // Allow channel to be chained.
    DCH5CONbits.CHAED = 1; // Allow channel to be chained.

    //DCH5CONbits.CHEDET = 1; // Allow channel to be chained.
    // printf("Chanel 5 event detect %d\n\r",DCH5CONbits.CHEDET);

    DCH6CONbits.CHCHNS = 1; // Chain to higher priority.
    DCH6CONbits.CHCHN = 1; // Allow channel to be chained.
    DCH6CONbits.CHAED = 1; // Allow channel to be chained.

    // DCH6CONbits.CHEDET = 1; // Allow channel to be chained.
    // printf("Chanel 6 event detect %d\n\r",DCH6CONbits.CHEDET);

    // DCH5ECONbits.CFORCE = 1; // set CFORCE to 1
    // DCH6ECONbits.CFORCE = 1; // set CFORCE to 1
    DCH5CONbits.CHEN = 1; // turn channel on
    DCH6CONbits.CHEN = 1; // turn channel on

Comments are hosted by Disqus, which loads only when you ask for it.