UART Buffer Clearing

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

cross mob
ScEn_283436
Level 5
Level 5
5 sign-ins First solution authored 100 replies posted

Hi,

   

I'm having a slight problem with the UART. Using the recommended method of a while loop UART_GetBufferSize() then doing UART_GetChar(), I get the received data and act on it. My code uses the CR-LF to indicate that a command has been received. The very next time I check back, there is still a CR-LF (or another one) as seen by the UART_GetBufferSize(). I do not see a CR-LF coming in with a scope on the receive line and have to believe it is a buffer issue. The work around is to UART_Stop and UART_Start. Any suggestions?

   

Nick

0 Likes
1 Solution
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

I am quite sure that _ClearRxBuffer() does work as expected, the code is quite clear and there are no other complais from other users. Nonetheless I am not able to find a bug in your code, I would suggest you to write a test-project that only accesses your cr-lf problem to verify. When expanding that project you'll find that bug. Candidates are exceeding array bounds, clobbered pointers,,,

   

 

   

Bob

View solution in original post

0 Likes
9 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

while(UART_GetBufferSize()) UART_GetChar();

   

But I frequently program UART projects like yours, so can you please post your complete project, so that we all can have a look at all of your settings? To do so, use
Creator->File->Create Workspace Bundle (minimal)
and attach the resulting file.

   

 

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
ScEn_283436
Level 5
Level 5
5 sign-ins First solution authored 100 replies posted

Sure, here it is.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Quite complicated structure, not easy to check. I presume you are checking for remaining cr-lf before they were received by UART.

   

I would suggest to remove cr-lf and indicate to have found a command to process when your buffer is not empty.

   

 

   

Bob

0 Likes
ScEn_283436
Level 5
Level 5
5 sign-ins First solution authored 100 replies posted

Bob,

   

Not sure what you're saying. How would I check for something that hasn't been received by UART?

   

The buffer IS empty based on the value returned by UART_GetRxBufferSize(). I have both the transmit and receive lines captured on the oscilloscope and there is no extra CR-LF being sent. I wrote the code for the sending device and it only sends one CR-LF. What I did was make a local variable called "num_bytes" and put the GetRxBufferSize return value in it. I set a breakpoint and watch each byte read from the buffer and number of bytes reduce accordingly. The expected return value string is '0' '\r' '\n' . If I watch each byte come in with a breakpoint, then everything works fine but if I let it fly at full speed, that's when I get the extra '\r' '\n'. That might be a clue.

   

Is there a possible memory overlap between the UART and the USBUART? Can I examine the actual buffer?

   

Nick

0 Likes
ScEn_283436
Level 5
Level 5
5 sign-ins First solution authored 100 replies posted

Bob,

   

Ok I think I know what you're saying now. Since this routine has been around for years I was reluctant to change it but what I found that it's just as easy to filter a CR-LF where there is no data and carry on as if nothing happened. 

   

It's just that our systems are designed to use either USB or RS232, and in this case, I'm using both. When I use one or the other exclusively, no problem. Something to consider.

   

Thanks

   

Nick

0 Likes
ScEn_283436
Level 5
Level 5
5 sign-ins First solution authored 100 replies posted

The UART_ClearRxBuffer doesn't work.

   

Our traditional protocol is to send the host a CR-LF as an acknowledgement that we have received a command. Then there is either data returned or not. A prompt character is sent '> ' to signal that the command has been completed. There is no way I can distinguish an erroneous CR-LF in the receive buffer, from one sent to me.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

I am quite sure that _ClearRxBuffer() does work as expected, the code is quite clear and there are no other complais from other users. Nonetheless I am not able to find a bug in your code, I would suggest you to write a test-project that only accesses your cr-lf problem to verify. When expanding that project you'll find that bug. Candidates are exceeding array bounds, clobbered pointers,,,

   

 

   

Bob

0 Likes
ScEn_283436
Level 5
Level 5
5 sign-ins First solution authored 100 replies posted

Is there a way I can identify and monitor the memory space where the UART does it's business?

   

If I put a break point where the CR-LF is captured and transferred to my buffer trying to catch it in the act, the erroneous CR-LF doesn't happen.

   

No two developers use the device in the exact same way. It's a programmable device and I'm following the documentation. Cypress continually upgrades the devices, so that you haven't heard of any other complaints doesn't mean anything.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Look into UART_1_INT.c

   

 

   

Bob

0 Likes