Dma memory access

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
ezbe_290006
Level 2
Level 2
10 replies posted 10 sign-ins 5 replies posted

hi,

i want to use the DMA of psoc 5 to read from the uart to an array .

my question is :

the UART is asynchronous to my ISR, what will happen if at the exact same time the DMA writes to the array the isr will read from the same address will i receive a corrupted data?

 

0 Likes
1 Solution
Len_CONSULTRON
Level 9
Level 9
Beta tester 500 solutions authored 1000 replies posted

ezbe,

Corruption as you described is basically impossible.

Setting up the DMA in essence synchronizes the data transfer to the UART. 

If you were performing CPU-only transfers from the UART FIFO to RAM, it is possible to not service the UART fast enough and receive a FIFO overrun error.

Reason #1 - FIFO

There is a 4-byte FIFO on the UART component.  As long as you are removing the newly Rx'd byte immediately after it comes in, the FIFO will not get full. You can reliably pull data out of the FIFO's front-end as the newly serialized Rx data in being loaded into the back-end.

Reason #2 - DMA

Create a DMA event to move data from the UART's FIFO to RAM based on "RX FIFO not empty".   In most cases it takes at least 10-bit frames to acquire UART data.  Therefore, setting up the DMA to transfer the latest acquired Rx Data will only take a few CPU cycles from when it was first available to the FIFO.  Whereas, the next Rx data will take 10*<bit_rate> before it is available to load into the FIFO.

Len
"Engineering is an Art. The Art of Compromise."

View solution in original post

0 Likes
5 Replies
Len_CONSULTRON
Level 9
Level 9
Beta tester 500 solutions authored 1000 replies posted

ezbe,

Corruption as you described is basically impossible.

Setting up the DMA in essence synchronizes the data transfer to the UART. 

If you were performing CPU-only transfers from the UART FIFO to RAM, it is possible to not service the UART fast enough and receive a FIFO overrun error.

Reason #1 - FIFO

There is a 4-byte FIFO on the UART component.  As long as you are removing the newly Rx'd byte immediately after it comes in, the FIFO will not get full. You can reliably pull data out of the FIFO's front-end as the newly serialized Rx data in being loaded into the back-end.

Reason #2 - DMA

Create a DMA event to move data from the UART's FIFO to RAM based on "RX FIFO not empty".   In most cases it takes at least 10-bit frames to acquire UART data.  Therefore, setting up the DMA to transfer the latest acquired Rx Data will only take a few CPU cycles from when it was first available to the FIFO.  Whereas, the next Rx data will take 10*<bit_rate> before it is available to load into the FIFO.

Len
"Engineering is an Art. The Art of Compromise."
0 Likes

hi,

thanks for you replay.

i am not talking about fifo overrun,  i am talking about memmory access, if i read from ram address which the DMA is writing to at the same time, what will i read?

(the DMA is not synchronize to the isr)

thanks

0 Likes

ezbe,

I think I understand your description of the CPU reading from the same RAM being written to by the DMA at the same time.  This could be a problem if the UART data rate is fast and the CPU has latency is long.

I'm also assuming that you have only one RAM memory location to DMA the UART Rx data into.

Here's a method to address this issue.

Double-Buffering

Allocate more than one byte for DMA.  For example: Allocate two bytes (data[2]) in RAM for DMA'd UART Rx data.  On the first byte of UART Rx, DMA it to data[0].  On the second byte DMA it to data[1].  On the third byte DMA it to data[0].  An so on...

Connect the ISR for the CPU to process the UART Rx data to the 'nrq' output of the DMA.

Len_CONSULTRON_0-1617884614294.png

The ISR then should then synchronize which of the two bytes to process.

If you'd like to share your project with the thread, we can take a look at it with better suggestions.

 

Len
"Engineering is an Art. The Art of Compromise."
0 Likes

Hi,

I have one isr and it's much slower then the dma, one isr will be in 3 dma cycles, so it cannot be sync... How can I read the array without getting it compremise by dma writing?

Thanks

0 Likes

ezbe,

I mentioned in the other posting about Double-buffering.

Another method is to disable the DMA just before reading the buffer and then afterwards re-enabling the DMA to continue.

Len
"Engineering is an Art. The Art of Compromise."
0 Likes