2017-05-25 04:43 AM
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 andshould 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-counter2017-05-25 04:48 AM
Remember where you left it?
2017-05-25 05:25 AM
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
2017-05-25 07:25 AM
HI Clive One
Please explain morethank you
2017-05-25 07:59 AM
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 thatstart 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
2017-05-25 08:17 AM
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.
2017-05-25 08:44 AM
thank you so much
This is exactly what I want
2017-05-25 09:36 AM
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.