DMA, or Dynamic Memory Access, is a feature on some microcontrollers whereby data can be moved around independent of the CPU, leaving the CPU free to do other work and therefore increasing the total amount of work which the system can do simultaneously.
DMA can move data from one memory region to another, from memory to a peripheral, or from a peripheral to memory.
Memory to Peripheral DMA
An example of memory-to-peripheral data copying is the case where a large amount of data should be transmitted via a communication peripheral (such as UART); without DMA the CPU would need to either ‘block’ whilst transmitting the data (which will usually be transmitted relatively slowly depending on the speed of the communication protocol) or use interrupts to manage the transmission (which would add additional processing overhead due to interrupt context switching).
This example also only considers the case where there is only one data transmission which needs to occur simultaneously - DMA can usually process multiple data streams at the same time (depending on the particular microcontroller implementation).
Peripheral to Memory DMA
Peripheral-to-memory DMA is similar - data can be read from a communications peripheral (such as UART) and copied into a memory buffer whilst the CPU is busy doing other things. Besides for leaving the CPU free to do other work, this also ensures that communications data is not lost because the CPU was busy with another task and not in a section of code which was ready to handle the received data.
Again, this is especially useful when there are multiple devices which may communicate simultaneously (think 5 or 10!); the DMA may be used to buffer each of their communications, perfectly and without the possibility of data loss, until the CPU is running a section of code which is ready to handle the received data. Whilst some microcontrollers include a small receiving buffer in the communications peripheral, with DMA the receiving buffer can be made arbitrarily large – limited only by the available RAM.
Another example of peripheral-to-memory DMA may be sampling analogue signals via ADC. In this example a timer is setup to trigger ADC sampling on one or more channels at set intervals. As the sampling occurs the readings are automatically transferred to memory buffers via DMA. A separate DMA stream may then copy these buffers to another memory region for the CPU to run DSP (Digital-Signal-Processing) algorithms on. Finally another DMA stream may be used to transfer the processed signal to the microcontroller DAC for output (memory-to-peripheral). (If you are not familiar with these terms then we you can find an overview in our ADC or DAC blog articles). The CPU is then free to spend all it’s time running intensive DSP algorithms, without wasting any time managing the ADC, memory copying, or the DAC.
For receiving data, some microcontrollers support circular DMA buffers whilst others support ping-pong buffers where data reception alternates between two sections of memory (the author of this article prefers circular buffers, considering them more efficient and easier to code around).
To the inexperienced coder DMA may seem daunting, but once you have invested the time to use it once then you have a code-template which can be easily copied to other projects and which can greatly increase the quality of your project and/or the complexity of project which your microcontroller can handle.
Proteus VSM CPU models model DMA operations when present in the real device. This allows you to write and test DMA firmware directly in the running simulation. Find out more about Proteus VSM MCU Simulation.All content Copyright Labcenter Electronics Ltd. 2023. Please acknowledge Labcenter copyright on any translation and provide a link to the source content on www.labcenter.com with any usage.
Get our articles in your inbox
Never miss a blog article with our mailchimp emails
Learn more about our built in graphing and advanced simulation features. Harness the mixed-mode simulation engine in Proteus to quickly test your analogue or digital circuitry directly on the schematic.
Ask An Expert
Have a Question? Ask one of Labcenters' expert technical team directly.