UART communication between two microcontrollers using DMA.

Post here first, or if you can't find a relevant section!
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: UART communication between two microcontrollers using DMA.

Post by mrburnette »

https://stm32f4-discovery.net/2017/07/s ... using-dma/

USART in Ardhino land is interrupt driven and reasonably efficient, but some efficiency gain may come with DMA; I have seen that on some SPI I/O previously. As they say, "Your mileage may vary." If implemented successfully, do please post the gain.

In coding an engine monitor on Mega2560 for a Rotax used in an Europa Experimental, my full-loop time was 750mS using two MAX31855. Measurements of readings such as voltage only become alive on the OLED displays after the 5th reading (5th loop) as the routine has a private [4] array for averaging but once active, display output is at 750mS. I did not look into DMA for serial as my initial thinking was there would be little benefit.

Code: Select all

void loop() {
    analog_13();          /* A13 Gasoline fuel remaining */
    analog_1();           /* A1  Oil temperature */
    analog_3();           /* A3  CylHead Front */
    analog_0();           /* A0  Water temperature */
    analog_15();          /* A15 Gasoline fuel pressure */
    analog_2();           /* A2  Engine Oil Pressure */
    analog_4();           /* A4  CylHead Rear temperature */
    analog_12();          /* A12 Battery voltage measured */
    analog_10();          /* A10 Amps measured */
    thermo_0();           /*     Exhaust Gas left */
    thermo_1();           /*     Exhaust Gas right */
 }
mustaq_ahm
Posts: 31
Joined: Tue Sep 14, 2021 11:08 am
Answers: 2

Re: UART communication between two microcontrollers using DMA.

Post by mustaq_ahm »

mrburnette wrote: Tue Nov 30, 2021 7:45 pm https://stm32f4-discovery.net/2017/07/s ... using-dma/
I have read this already. What I understand from this: If the receiving data is larger than receive buffer, then this DMA will be used to avoid the loss of data. {but as mentioned by @ag123 with ring buffer (I do not lose any data) [also I do not send more than the maximum receive buffer size]}.

I think I do not understand what you are trying to explain to me.
mrburnette wrote: Tue Nov 30, 2021 7:45 pm but some efficiency gain may come with DMA; I have seen that on some SPI I/O previously. As they say, "Your mileage may vary."
I am sorry but seriously, I did not get the point of what you are trying to say here.
mrburnette wrote: Tue Nov 30, 2021 7:45 pm In coding an engine monitor on Mega2560 for a Rotax used in an Europa Experimental, my full-loop time was 750mS using two MAX31855. Measurements of readings such as voltage only become alive on the OLED displays after the 5th reading (5th loop) as the routine has a private [4] array for averaging but once active, display output is at 750mS. I did not look into DMA for serial as my initial thinking was there would be little benefit.
So you used DMA in the Arduino Mega2560 board?
mrburnette
Posts: 633
Joined: Thu Dec 19, 2019 1:23 am
Answers: 7

Re: UART communication between two microcontrollers using DMA.

Post by mrburnette »

mustaq_ahm wrote: Tue Nov 30, 2021 8:26 pm ...
So you used DMA in the Arduino Mega2560 board?
No.
My point was that the mechanics taking the majority of 750mS would be unchanged; therefore DMA would have only marginally affected the full loop{} time. Generally for non-graphic applications, the complexity of DMA is not justified, IMO. Graphics over SPI or I/O is an entirely different matter as display response can be enhanced markedly.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: UART communication between two microcontrollers using DMA.

Post by ag123 »

mustaq_ahm wrote: Tue Nov 30, 2021 4:27 pm :arrow: My problem is with the total Time of the void loop() to complete one cycle.

normally in main(), it calls loop looks like this

Code: Select all

void main() {
  setup();

  while(1)
    loop();
}
Practically it is as fast as it can get ;)
dma is mainly to offload data moves to hardware, that alone won't make loop() run faster.
if is useful if you have *bulk data* like megabytes in a go, as dma requires you to setup buffers to receive them and normally u'd setup the interrupts to fire when the dma buffer is full. u'd still need to move that data out of that buffer or do something on it, that moving and processing will be 'slow'.

oh try something like the stm32f407xx or stm32f405xx, those runs at 168 mhz, and has that ART accelerator and fpu, they are *much* faster than stm32f103.

For multitasking, there is the 1 line scheduler

Code: Select all

loop() {
void *tasks[] = { &task1(), &task2(), &task3(), NULL };
void *task;
int i=0;
while(task = tasks[i++] != NULL) *(task)();
}
JimEli
Posts: 20
Joined: Sat Apr 17, 2021 11:33 pm

Re: UART communication between two microcontrollers using DMA.

Post by JimEli »

maybe selectively call functions respecting the rate at which they change.
mustaq_ahm
Posts: 31
Joined: Tue Sep 14, 2021 11:08 am
Answers: 2

Re: UART communication between two microcontrollers using DMA.

Post by mustaq_ahm »

Okay, got it. Got a major idea of what and how is DMA working. Will look into it in more detail. Thanks all for your patience and guidance for me to succeed further. Thanks to everyone who replied. :D
mustaq_ahm
Posts: 31
Joined: Tue Sep 14, 2021 11:08 am
Answers: 2

Re: UART communication between two microcontrollers using DMA.

Post by mustaq_ahm »

ag123 wrote: Thu Dec 02, 2021 7:50 am For multitasking, there is the 1 line scheduler

Code: Select all

loop() {
void *tasks[] = { &task1(), &task2(), &task3(), NULL };
void *task;
int i=0;
while(task = tasks[i++] != NULL) *(task)();
}
I will also try this. Lets see.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: UART communication between two microcontrollers using DMA.

Post by ag123 »

actually the simplier way is simply this

Code: Select all

void loop() {
	task1();
	task2();
	task3();
}
as simple as it gets, the magic is in how you code your tasks
this thread probably comes up in many google/msn searches
https://forum.arduino.cc/t/demonstratio ... ime/217158
another one from adafruit/digikey here
https://www.digikey.com/en/maker/projec ... d542c46a28
a trouble with 'libraries' is most of them assume your cpu/mcu is a infinite bandwidth supercomputer, i.e. time and latency isn't their concern.
and many codes simply spinlock to wait for something, let alone if they timeout if at all.
e.g. while(!Serial.available());
that'd wait indefinitely if you never send anything across Serial(). But that this is actually a classic code for a command processor, i.e. wait for the next byte of data/command
mustaq_ahm
Posts: 31
Joined: Tue Sep 14, 2021 11:08 am
Answers: 2

Re: UART communication between two microcontrollers using DMA.

Post by mustaq_ahm »

Sure. Thank you so much. I will look into all of it.
Post Reply

Return to “General discussion”