Receiving characters from a UART under MQX

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

Receiving characters from a UART under MQX

714 Views
dcooper
Contributor II

I am trying to implement a serial (UART) interface on a K60 under MQX.  This seemed relatively straight forward, but am having no success.

I intend to have a task that that opens and receives characters on ittyx port. 

I do not know how many characters will be received.

I want to read 4 characters when available (a header) and then read additional characters based on a length value included in the first 4 bytes read.  It did not seem unreasonable to think the MQX serial drivers could support this kind of use case.  Can someone point me to example code or offer insights as to how the driver works?

Questions:

Should I be using read() or fread(), I am not clear on the difference?

If in a while(1) loop I make a call to fread() and no characters are available, does this task give up the cpu allowing other tasks to run.

If, as an example, the chip has received 8 bytes and I do an fread() to read the first 4 bytes, can I then do another fread() for the remaining bytes, will I get the next 4 bytes?

0 Kudos
1 Reply

297 Views
DavidS
NXP Employee
NXP Employee

Hi David,

Yes you can do this.

With UART in interrupt mode it will buffer the inputs and code can go extract as many bytes at a time as you want.  And then go back and get more.

If too many bytes are received before reading from the buffer, then the buffer will retain what it accumulated and drop the rest of the incoming characters.  The buffer is configurable in size in the BSP file init_sci.c.  It defaults to 64 bytes for the TX buffer and 64 bytes for the RX buffer.

UPDATE:

I created test code for the Tower TWR-K60D100M+TWR-SER using C:\Freescale\Freescale_MQX_4_2\mqx\examples\hello2 example.

The three key files are attached (user_config.h, twrk60d100m.h, hello.c).

Recompile the BSP/PSP and hello2 MQX application.

Run it and use terminal with 115200baud, 8-bit, 1-stop, no parity.

The default terminal output window will show below if you do not type any character as input:

Hello

- World

-------------------

No characters received in last 4 seconds.... --------------------

No characters received in last 4 seconds.... -------------------

No characters received in last 4 seconds.... --------------------

The code will check the UART Rx buffer every 4 seconds to see if any characters have been received.  If not then you see the above results.

If you type 1 or more characters (I typed "1234") during the 4 second interval, then you will see following:

No characters received in last 4 seconds.... ------------1234--------

Number of characters read was 5-------------------

No characters received in last 4 seconds.... --------------------

No characters received in last 4 seconds.... --------------------

If you type more then 64 characters during the 4 second interval you will see:

No characters received in last 4 seconds.... --------------------

No characters received in last 4 seconds.... --------abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ__abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ__-----------

RX_OVERRUNS Error...

-------------------

No characters received in last 4 seconds.... -------------------

No characters received in last 4 seconds.... --------------------

You should be able to create a state machine to read your first four characters to determine what the header is telling you to do (read more, restart, other).  Assuming the header indicates how many characters you need to read, they are still in the input buffer waiting for you get them.

Regards,

David

0 Kudos