cancel
Showing results for 
Search instead for 
Did you mean: 

How reset USART receiver counter???

l90mehdi
Associate II
Posted on May 25, 2017 at 13:43

Hi 

I use DMA to send and receive characters with USART in STM32F405.

The characters that will be sent have the unknown length.

so

when i receive characters i should know How many characters have been received and

should reset USART receiver counter.

I check NDTR and i know  

How many characters have been received

But i can not reset the usart receiver counter 

so 

what should i do ?????

thank you

#character-counter #usart-receiver-counter #usart #dma-usart #usart-counter
7 REPLIES 7
Posted on May 25, 2017 at 13:48

Remember where you left it?

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Danish1
Lead II
Posted on May 25, 2017 at 14:25

To expand on what Clive said, what you seem to want to do is start each lot of character reception with a fresh DMA transfer so each new set of characters is at the start of the DMA buffer.

You cannot modify the appropriate DMA registers while the stream is enabled, so one approach would be to disable the stream before resetting all the registers.

I much prefer to have my USART reception by DMA into a circular buffer.

You already know how to calculate where in the buffer the last-character-that-arrived went, using NDTR.

You just need to have another index / pointer into the buffer showing how far you've read to so far.

And how many fresh characters there are is the difference between the two. There's a little book-keeping / arithmetic to cope with when the DMA has wrapped round but your read pointer has not yet done so.

Hope this helps,

Danish

Posted on May 25, 2017 at 14:25

HI Clive One

Please explain more 

thank you

Posted on May 25, 2017 at 14:59

Hi Danish Ali 

yes 

i want start each lot of character reception with a fresh DMA transfer so each new set of characters is at the start of the DMA buffer.

this way that you offer is very good

but 

i want to reset receiver counter because it is important that 

start each lot of character reception with a fresh DMA transfer so each new set of characters is at the start of the DMA buffer

for me.

but in my registers i can not find counter register.

so if i 

cannot modify the appropriate DMA registers while the stream is enabled 

how to do this ??????????????

thank you 

Posted on May 25, 2017 at 15:17

You cannot write to DMA_SxNDTR while DMA_SxCR.EN is 1.

So write 0 to DMA_SxCR.EN

Then write to 

DMA_SxNDTR.

then, to re-enable DMA, write 1 to DMA_SxCR.

A caveat is that while EN is 0, you might miss incoming characters.

Posted on May 25, 2017 at 15:44

thank you so much 

This is exactly what I want

Posted on May 25, 2017 at 16:36

In systems where you can't reset something, or doing so creates a race condition, one would remember where one left off and then come back to that position the next time around.

ie

startptr = &buffer[x]; // When x doesn't have to be zero

Depending on the size of the buffers, and the desire to copy or use-in-place, you'd then manage the data as you need it. Now clearly that's a little more complex than assuming everything starts at the beginning of the buffer, but not significantly so, and can be quite robust.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..