Page 1 of 1

FSMC, DMA and interrupts

Posted: Thu Aug 18, 2011 7:41 pm
by kgysmits
I'm using the STM32 FSMC to talk to a QVGA LCD with onboard framebuffer. I want to use DMA to do 2^16 transfers at a time, freeing the CPU for other things.
I already have DMA2Channel5 up and running (manually setting up the hardware through it's registers). Up until now i just waited for the transfer complete flag:

Code: Select all

DMA2_Channel5->CCR |= DMA_CCR5_EN;      //enable DMA
while (!(DMA2->ISR & DMA_ISR_TCIF5));   //wait for completion
DMA2->IFCR |= DMA_IFCR_CGIF5;           //clear TC flag
DMA2_Channel5->CCR &= ~DMA_CCR5_EN;     //disable DMA
etc.

To give other threads a chance while using DMA, things need to become interrupt-based. This is what i came up with:

in the LCD thread, when ready to write (DMA now has DMA_CCR5_TCIE enabled for IRQ generation):

Code: Select all

chBSemWait(&bsemFSMC_IRQ);               //wait for DMA completion
DMA2_Channel5->CCR &= ~DMA_CCR5_EN;      //disable DMA channel
// <... some code ...>
DMA2_Channel5->CCR |= DMA_CCR5_EN; //enable DMA


main:

Code: Select all

chBSemInit(&bsemFSMC_IRQ, FALSE); //Init as taken == false
NVIC_EnableIRQ(DMA2_Channel4_5_IRQn);   //enable IRQ vector in NVIC


Interrupt handler:

Code: Select all

CH_IRQ_HANDLER(DMA2_Ch4_5_IRQHandler) {
   CH_IRQ_PROLOGUE();
   chSysLockFromIsr();
      // DMA2Ch5 global interrupt clear
      DMA2->IFCR = DMA_IFCR_CGIF4;
      DMA2->IFCR = DMA_IFCR_CGIF5;
      /* Wakes up the thread.*/
      chBSemSignalI(&bsemFSMC_IRQ);
   chSysUnlockFromIsr();
   CH_IRQ_EPILOGUE();
}


This looked like it should work to me, until I found out that the DMA helper driver already has interrupt handlers defined for all DMA channels. I took a shot at the dma helper driver, but i don't have a clue as to how to use and configure it honestly. Could someone please explain how to make this work?

Also, is a semaphore faster than events for this case? Only one thread will be communicating with FSMC/DMA, so events would be an option also.

I like this OS a lot, but the source sometimes is hard to read. Maybe some clear examples of events, semaphores, mutexes and mailboxes should be documented on the site.

Re: FSMC, DMA and interrupts

Posted: Thu Aug 18, 2011 8:26 pm
by Giovanni
The DMA helper driver allows to allocate a DMA channel, use it, and finally release it when no more needed. It enables the sharing of a DMA channel among multiple drivers.

The various device drivers use this mechanism so you can fin examples into the SPI, UART and ADC drivers for STM32. Look into the various xxxStart()/xxxStop() driver functions. It is not really that complicated.

Basically there are two functions: dmaAllocate() and dmaRelease(). One of the parameters of dmaAlloc() is a callback pointer that is invoked when the DMA channel triggers an interrupt.

About the documentation, the system APIs are well documented, what you are searching for are use cases. I recommend to look at the test code, there are examples of API usage for all kernel mechanisms. Undeniably, there is never something like "sufficient documentation".

The fun thing is that I also received complains about too much documentation to read :-)

Giovanni

Re: FSMC, DMA and interrupts

Posted: Thu Aug 18, 2011 8:28 pm
by Giovanni
BTW, of course if there are questions about specific mechanisms the best course of action is to ask in the forum.

Giovanni

Re: FSMC, DMA and interrupts

Posted: Thu Aug 18, 2011 10:45 pm
by kgysmits
Have been coding all day, and it got the best of me. It's fun and rewarding figuring out how this OS works, but it's nice to just get things working once in a while :)
Giovanni wrote:... It is not really that complicated.

It never is, once you get the hang of it :oops:.

I'll take a look at the test code this weekend.

Thanks.

Re: FSMC, DMA and interrupts

Posted: Sun Aug 28, 2011 10:00 am
by Giovanni
Hi,

The version in the repository now uses a new DMA helper driver that is both easier to user and better documented.

Thanks for feedback.

Giovanni