Simple UART - Clear RX Buffer

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Simple UART - Clear RX Buffer

16,754 Views
philhale
Contributor IV

Hello again.

 

I am now receiving UART data in the rx callback buffer using the rx interrupt callback routine.

 

Now that I have "processed" the byte of data, how do I clear the buffer?  I have tried to set the first byte to '/0' or strcpy a null string and the buffer doesn't clear. I suspect that the buffer is a copy of the uartState->rxBuff when the rx callback routine is called. So, how do I clear the real rx buffer properly?

 

A little more detail:  When I type "a" then send out the resulting rx buffer I get "a", just as expected. But, after the subsequent character is entered, such as "b", printing out the rx buffer returns "ab"...and so on. The rx buffer is not getting cleared.

 

Incidentally, does the SDK implement a FIFO buffer by default?  Is this a FIFO clearing problem?  I didn't intentionally implement a FIFO is that is the case.

 

Thanks.

 

Phil

Labels (1)
0 Kudos
19 Replies

8,130 Views
kyizawwin
Contributor II

Hi Phil,

I am also facing the same problem with KL25Z freedom board platform. Since I set the buffer to 16 and my sting is variable between 5-11, the next data fill up till the array is filled up.

Hi Alice,

The driver file won't help as the re-initialization of data need to be done in user side.

Thanks,

Bryan

0 Kudos

8,130 Views
kyizawwin
Contributor II

Hi,

I had re-initialized call back function and able to re-set the buffer but I don't think this is the correct and efficient way.

UART_DRV_InstallRxCallback(FSL_BLE, BLE_RxCallback, recBuffCall, (void *)recCnt, true);

Best regards,

Bryan

0 Kudos

8,130 Views
philhale
Contributor IV

Kyi Zaw Win,

We are close. I tried what you suggested but I got a slightly different result.

Here is what I see when I type "12345" into PuTTY:

ScreenHunter_116 Dec. 29 09.22.jpg

It is like the first byte is kept in the buffer.

Here is my RxCallback():

void uartCom0_RxCallback(uint32_t instance, void * uartState)
{
  /* Write your code here ... */
  //GPIO_DRV_ClearPinOutput(LED_GREEN);
  GPIO_DRV_TogglePinOutput(LED_RED);
  UART_DRV_SendData(instance, rx_callback_buff, sizeof(rx_callback_buff));
  UART_DRV_InstallRxCallback(uartCom0_IDX, uartCom0_RxCallback, rx_callback_buff, NULL, true);
}
0 Kudos

8,130 Views
Alice_Yang
NXP TechSupport
NXP TechSupport

Hello ,

Please change the code here :

pastedImage_0.png

then it will work well .


Have a great day,
Alice Yang

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos

8,130 Views
philhale
Contributor IV

Sorry. Didn't work.

Typing an "a" returns an "a". Next, typing a "b" returns an "a". Next typing a "c" returns an "a".

The buffer is not getting cleared. If the buffer was getting cleared then your change would certainly work. Where and when is this buffer cleared?

Thank you, Alice.

Phil

0 Kudos

8,130 Views
kyizawwin
Contributor II

Btw, Phil. Somewhere in the forum, Alice mentioned that the following routine need to modify as in bold . Have u tried that?

UART_DRV_IRQHandler

{

.....

            if (uartState->rxCallback != NULL)

            {

                //uartState->rxCallbackParam++; // to keep track no of counter

                uartState->rxCallback(instance, uartState);

                    ++uartState->rxBuff; //fix according to https://community.freescale.com/thread/382455

                    --uartState->rxSize;

            }

            else

            {

                ++uartState->rxBuff;

                --uartState->rxSize;

......

}

Good luck!

0 Kudos

8,130 Views
philhale
Contributor IV

Kyi Zaw Win,

Regarding the ++uartState->rxBuff; routines. Yes. I implemented that modification. It wouldn't work without it.

Thanks.

Phil

0 Kudos

8,130 Views
kyizawwin
Contributor II

Hi Phil,

I also try to get string of data from UART using Rx Interrupt CallBack. My data is a string of character ended by "\n". I just copy data from callback buffer to my receive buffer upon receiving "\n". I could manage to grab whole string now.

Best regards

0 Kudos

8,130 Views
cabharufusbaska
Contributor I

Kyi Zaw Win,

          I too facing the same problem. I also need similar implementation where I have to grab a string which is ended by "\n".

I am always finding the first character of the string in my rx_callback buffer.

Even after making the ++uartState->rxBuff; routine modification also I am not able to clear the rx_callback buffer.

Do I need to copy the call back buffer to another buffer (strcpy(recData,recBuffCall) to clear rx_callback buff ?

I would appreciate suggestions from you

Thanks,

Cabha

0 Kudos

8,130 Views
kyizawwin
Contributor II

Hi Cabha,

Yes. I did strcpy data and then re-initialized call back buffer.

strcpy(recData,recBuffCall);

UART_DRV_ReceiveData(FSL_BLE, recBuffCall, sizeof(recBuffCall));

Best regards,

0 Kudos

8,130 Views
kyizawwin
Contributor II

Unfortunately, now my received data [1] is corrupted instead of [0]. looks like root cause need to be fixed. If not, the error will be running around everywhere

0 Kudos

8,130 Views
kyizawwin
Contributor II

And this function call (UART_DRV_ReceiveData) can be used instead of (UART_DRV_InstallRxCallback)

0 Kudos

8,130 Views
philhale
Contributor IV

Re: "And this function call (UART_DRV_ReceiveData) can be used instead of (UART_DRV_InstallRxCallback)"

This true but I want to get RS232 commands from the user and don't want to use polling. Interrupts are really the way to go.

0 Kudos

8,130 Views
kyizawwin
Contributor II

When I re-initialized the receive buffer for 2nd time, it seems like the pointer was incremented. So, the next receiving data start from incremented address. Thus, the first cell always keep the old data. i couldn't find a way yet

0 Kudos

8,130 Views
kyizawwin
Contributor II

Hi Phil,

When I copy the call back buffer to another buffer (strcpy(recData,recBuffCall);), the first bogus data was not there. Definitely some bugs are there but I guess I can ignore this problem for the time being.

Best regards,

0 Kudos

8,130 Views
philhale
Contributor IV

Re: "When I copy the call back buffer to another buffer..."

Glad you could find a work-around.

Thank you for being a part of this conversation. It was nice to know I was not the only one.

Best regards,

Phil

0 Kudos

8,129 Views
kyizawwin
Contributor II

Thanks, Alice!

But it didn't help to me.

Hi Phil,

The first data was stubbornly left in my case also. And my case I need to trigger two tx to get 1st tx which may be due to my code.

Let's keep on working! Hope I will get it on New Year Day

0 Kudos

8,130 Views
Alice_Yang
NXP TechSupport
NXP TechSupport

Hello Phil,

Which chip do you used ?

About the  function when receive one character , then send it , you can refer to the demo under the ksdk1.3 :

Freescale\KSDK_1.3.0\examples\frdmk64f\driver_examples\uart\uart_non_blocking  

If this demo could not meet your requirement ,  please send you project to me , i will help you check it on my side.

Hope it helps

ALice

0 Kudos

8,130 Views
philhale
Contributor IV

Alice,

I am using my own board utilizing the MK64FX512VLL12. In my code, I am utilizing the RxCallback interrupt routine to receive the incoming data from the UART. This allows me to accept UART data from an outside source (RS232 in my case) through non-polling methods. The routine works and I receive data in my rx buffer but after processing the data I do not know how to clear the buffer.

Here is what displays on my RS232 screen when I type "abc":

ScreenHunter_115 Dec. 29 08.49.jpg

Thanks.

Phil

0 Kudos