# DMA for UART ring buffer

**URL:** <https://forum.arduino.cc/t/dma-for-uart-ring-buffer/936993>\
**Category:** Due\
**Created:** [December 17, 2021, 10:59pm UTC](https://forum.arduino.cc/t/dma-for-uart-ring-buffer/936993 "2021-12-17T22:59:54Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![gunky](https://avatars.discourse-cdn.com/v4/letter/g/a698b9/32.png) [@gunky](https://forum.arduino.cc/u/gunky)\
**Post date:** [December 17, 2021, 10:59pm UTC](https://forum.arduino.cc/t/dma-for-uart-ring-buffer/936993/1 "2021-12-17T22:59:55Z")

</div>

I have implemented a ring buffer using UART interrupt. Basically my code continually fills the ring buffer and when an endline character is read, it sets a flag to indicate a new message has been received.

I am wondering if I can achieve the same thing using the DMA controller?

---

<div class="post-metadata">

**Author:** ![gilshultz](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gilshultz](https://forum.arduino.cc/u/gilshultz)\
**Post date:** [December 18, 2021, 3:20am UTC](https://forum.arduino.cc/t/dma-for-uart-ring-buffer/936993/2 "2021-12-18T03:20:37Z")

</div>

How are your hardware skills, it can be done.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [December 18, 2021, 3:33am UTC](https://forum.arduino.cc/t/dma-for-uart-ring-buffer/936993/3 "2021-12-18T03:33:26Z")

</div>

What is your reason?

---

<div class="post-metadata">

**Author:** ![gunky](https://avatars.discourse-cdn.com/v4/letter/g/a698b9/32.png) [@gunky](https://forum.arduino.cc/u/gunky)\
**Post date:** [December 18, 2021, 3:58am UTC](https://forum.arduino.cc/t/dma-for-uart-ring-buffer/936993/4 "2021-12-18T03:58:09Z")

</div>

To remove computational burden on processor. I don't see a way to have the DMA trigger an interrupt on a particular character - am I wrong?

---

<div class="post-metadata">

**Author:** ![hzrnbgy](https://avatars.discourse-cdn.com/v4/letter/h/90db22/32.png) [@hzrnbgy](https://forum.arduino.cc/u/hzrnbgy)\
**Post date:** [December 18, 2021, 4:05am UTC](https://forum.arduino.cc/t/dma-for-uart-ring-buffer/936993/5 "2021-12-18T04:05:28Z")

</div>

DMA interrupts are usually based on the number of bytes transferred, normally half-complete and complete. There's no mechanism to check the content of each received byte during a DMA transaction.

You might be able to do it with a combination of "double buffer mode" and a timer...

---

<div class="post-metadata">

**Author:** ![gunky](https://avatars.discourse-cdn.com/v4/letter/g/a698b9/32.png) [@gunky](https://forum.arduino.cc/u/gunky)\
**Post date:** [December 18, 2021, 4:14am UTC](https://forum.arduino.cc/t/dma-for-uart-ring-buffer/936993/6 "2021-12-18T04:14:40Z")

</div>

The problem is I would still need to scan through the buffers with the processor if there's not a way to get the DMA controller to trigger it. For my application it becomes kind of pointless I guess.

As a secondary question(and I can make a new thread), do you know how to get sprintf to work with decimals? I'm using the arm GCC C library with atmel studio 7 and I can't print floating point numbers, some type of error gets triggered and the program crashes to a default handler.

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [December 18, 2021, 5:16am UTC](https://forum.arduino.cc/t/dma-for-uart-ring-buffer/936993/7 "2021-12-18T05:16:22Z")

</div>

> [@gunky](#):
>
> To remove computational burden on processor. I don't see a way to have the DMA trigger an interrupt on a particular character - am I wrong?

It is true, DMA is much faster than an interrupt driven routine. Your post makes me wonder two things. Is this application close to the limit of available processing time? The other thing, understand that in some systems, DMA and CPU share memory transparently, and others not. Some have a crossbar matrix such that both DMA and CPU can actually complete access cycles simultaneously, or nearly so. But some have contention for memory, in which case DMA will still impact CPU time, just not to such a large extent. By its nature, it takes priority.

Your other question will have to be sorted out some way, I suspect you need to inspect your DMA stream much less than you think. Try to remove whatever impediment created that need.

Secondary question, it seems to me a separate thread would be most logical. Include the code and error listing verbatim.

---

<div class="post-metadata">

**Author:** ![ard\_newbie](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@ard\_newbie](https://forum.arduino.cc/u/ard_newbie)\
**Post date:** [December 18, 2021, 6:19am UTC](https://forum.arduino.cc/t/dma-for-uart-ring-buffer/936993/8 "2021-12-18T06:19:04Z")

</div>

I provided some example sketches a while ago using PDC DMA either for UART or USART transfers, and AHB DMA for USART (from reply #5):

[https://forum.arduino.cc/t/dma-uart-serial/451697/4](https://forum.arduino.cc/t/dma-uart-serial/451697/4)

---

<div class="post-metadata">

**Author:** ![gunky](https://avatars.discourse-cdn.com/v4/letter/g/a698b9/32.png) [@gunky](https://forum.arduino.cc/u/gunky)\
**Post date:** [December 18, 2021, 6:48am UTC](https://forum.arduino.cc/t/dma-for-uart-ring-buffer/936993/9 "2021-12-18T06:48:18Z")

</div>

I think I figured out a way to do it, the USART has an RX time out interrupt, you can probably do something where your end-message signal can be triggered via that. The DMA can't be configured to use a ring buffer(STM parts can but they are way newer - idk why the due still on M3), probably the only option is having two buffers and switching the DMA between them when one is full. This will mess up your max message length though.

---

<div class="post-metadata">

**Author:** ![westfw](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/westfw/32/213443_2.png) [@westfw](https://forum.arduino.cc/u/westfw)\
**Post date:** [December 18, 2021, 11:03am UTC](https://forum.arduino.cc/t/dma-for-uart-ring-buffer/936993/10 "2021-12-18T11:03:27Z")

</div>

It’s usually really tricky to do uart reception using dma, for exactly the reasons you are noticing…

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [June 16, 2022, 11:03am UTC](https://forum.arduino.cc/t/dma-for-uart-ring-buffer/936993/11 "2022-06-16T11:03:38Z")

</div>

Closed on June 16, 2022, 11:03am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
