UART Code

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

UART Code

11,587 Views
saibhargavi
Contributor II

Hi,

 

I am working on UART code using MQX now. I went the user guide and the example code. I am still not clear on how should i initialise the uart function and transmit or received a set of data. I know the following steps are require for a UART to work

 

1) Initailise the UART port with required baud rate, parity and stop bits

3) Initialise the interrupt register to enable the UART ISR

4) Transmit the data to the UART TX register to transmit the data

5) Receive the data from UARTRX register to receive the data.

6) Change the baud rate if required.

 

But i am not clear on how sholud this be done using MQX :smileysad: . Please help me in explaining the steps i need to follow for writing code using MQX. I would be more happy if the steps are explained using example code. I am struck up with this module and looking forward for help at the earliest.

 

Thanks and Regards

 

Sai Bhargavi

24 Replies

5,062 Views
MrCleaver
Contributor I

Hello,

 

I'm trying to modify the Hello2 example in the mqx example folder to use interrupt driven IO and I'm running into some problems. 

 

I have added the following lines to the user_config.h(and remade all libraries just to be sure). 

 

#define BSPCFG_ENABLE_TTYA   0#define BSPCFG_ENABLE_ITTYA   1

and I uncommented the delay code in the hello2.c source. 

 

When I run the program however, nothing is ever printed to the screen.

 

Do I have to do an extra step in order to use the interrupt driven IO? 

 

 

0 Kudos
Reply

5,062 Views
MrCleaver
Contributor I

Ah, nevermind, I have figured it out.

 

I guess printf will always write to the driver defined by a macro in one of the files in BSP which is set to 'ttya'

 

so if I want to print to ittya I need to obtain a file handle to ittya (via fopen) and then use fprintf to specify printing to the interrupt driven serial driver. 

 

Would interrupt driven receive be somewhat similar to this procedure? 

 

How would I service the interrupt (by printing out the character entered for example) when receiving data from an interrupt driven IO channel? 

 

--If this post should go to a different thread my apologies, but I think the problems are somewhat related to the parent issue so I put it here.

 

Message Edited by MrCleaver on 2009-11-04 12:30 AM
0 Kudos
Reply

5,062 Views
JuroV
NXP Employee
NXP Employee

printf and scanf use task's IO_STDOUT, IO_STDIN handles.

Use BSP_DEFAULT_IO_CHANNEL in user_config.h to set default value for all tasks. It does not depend on if you use polled ("ttyX:") or interrupt ("ittyX:") driver.

0 Kudos
Reply

5,062 Views
niravj
Contributor I

I tried to find out the hello2.c file in the mqx installation folder, but didn't found it, was it available with some older vesion of mqx ?

 

Any example source code for UART using isr - receiving ?

 

Any inputs will be greatly helpful to me

 

Nirav

0 Kudos
Reply

5,062 Views
Lakshmi
Contributor II

Hi Nirav,

 

hello2.c is available in the following path,

 

C:\Program Files\Freescale\Freescale MQX 3.5\mqx\examples\hello2

 

Which version of MQX RTOS are you using? I am currently using version 3.5.

 

There is NO example source code for interrupt driven UART.

 

There is ONLY UART polling example source code available with MQX RTOS installation..

 

I also am struggling with the same. There is no proper documentation by MQX to explain about this.

 

I shall update you if I get a breakthrough.

 

 

 

 

0 Kudos
Reply

5,062 Views
niravj
Contributor I

Hi Lakshmi,

 

Thanks for the update, I was able to see the code and example - hello2.

 

Meanwhile I am agree with you the documentation of Freescale in this regards i.e. using UART with help of BSP in interrupt mode is - most probably no at all available - probably not for any bsp available.

 

I contacted freescale technical support local contacts and he also updated me the same.

 

Any freescaler reading this thread can update us if he/she have further information in this regards.

 

Beside this if you are finding any head way in the same let me know and I will also do the same....

 

Thanks for your inputs...

 

0 Kudos
Reply

5,057 Views
DavidS
NXP Employee
NXP Employee

To place the UART into interrupt mode I modify the user_config.h file to get UART1 into interrupt mode:

#define BSPCFG_ENABLE_ITTYB      1

Make sure to recompile the build_libs.mcp for you particular build.

 

The attached io_main9.c is an evolution of the mqx\examples\io\main.c code base.

The terminal/console is using UART0/ttya in polled mode and UART1/ittyb is setup for interrupt mode.

So hardware wise connect UART0 and UART1 to two terminals with 115200,8,1,N settings.

Set a breakpoint at one of these lines:

err = _io_ioctl(fh_ptr, IO_IOCTL_SERIAL_GET_BAUD, &Old_Baud_rate);      //Get Baud Rate from default to "Baud_rate"
   printf("\nCurrent Baud Rate for alternate UART port is %d\n", Old_Baud_rate); //print to uart0
   err = _io_ioctl(fh_ptr, IO_IOCTL_SERIAL_SET_BAUD, &New_Baud_rate);      //Change/set Baud Rate from default to "Baud_rate"
Once you break, then change the UART1 terminal to 9600 before continuing.  This was an example for customer to see how to use the IOCTL capability.

 

Note that when placing the UART into interrupt mode, the BSP initialization code is setting up a 64 byte fifo so that as the UART receives a character it shoves it into the fifo.  When code wants a character it can check to see if byte is available (my code doesn't do this yet) or just ask for it and if available from fifo it will get it.

Regards,

David

 

0 Kudos
Reply

5,055 Views
MWMinor
Contributor V

Hi David

There didn't seem to be an attachment to your post.

Did I miss something...?

Ken

0 Kudos
Reply

5,055 Views
niravj
Contributor I

Hi David,

 

Thanks for the code and the detail description for UART in interrupt mode.

 

I may be wrong in my following understanding but - still I am not clear how to use UART in interrupt mode for receiving any byte/character.

 

The steps I understood for using UART0 in interrupt mode ::

 

1. Modify user_config.h with flag .... BSPCFG_ENABLE_ITTYA  1 and recompile the bsp library

2. I am using FILE * uart_0 = fopen(BSP_DEFAULT_IO_CHANNEL, 0);

3. Use fprintf (uart_0,"%d",some_value); to  send the data to uart_0

 

Steps not understood:

When some character has been received by uart_0 how can i read it in ISR of UART 0.  Is there any way i can define the isr - getting called when UART receives characters or it's rx buffer is becoming full ? 

I don't want to call fread()/read() in a while loop - polling mode for reading the UART_0.

 

Can you please explain/guide how can I do this - this is not clear to me from the example you provided. Also I tried to see the hello2 example in mqx/examples/hello2 folder but - receiving part is still not clear to me.

 

I am using the UART bsp of mqx for the first time..

 

thanks for your help in advance.

 

 

 

 

0 Kudos
Reply

5,055 Views
Lakshmi
Contributor II

Hi Nirav,

 

Refer to the following thread for more information

 

https://community.freescale.com/thread/64147

 

 

0 Kudos
Reply

5,055 Views
Lakshmi
Contributor II

 

Hi

 

I thanks for your inputs.

 

I will try it out & get back to you

 

Thanks

0 Kudos
Reply

5,062 Views
saibhargavi
Contributor II

Hi,

 

Thankyou for your reply. I have one more doubt regarding the UART communication. When we write code in MCF52259 not using MQX, we check for MCF_UART_UISR_RXRDY_FU in MCF_UART_UISR register to see if the uart is in receive mode and MCF_UART_UISR_TXRDY_FU in MCF_UART_UISR to check if uart is in TX mode and then take neccessary action either to transmit or receive in the same routine.. how will we do the same in MQX??

 

Best Regards

 

Sai Bhargavi

0 Kudos
Reply

5,062 Views
PetrM
Senior Contributor I

Hello,

 

MQX UART driver uses USR register and checks its flags TXRDY and RXRDY, it's always open for both transmit and receiving.

You can still (partially) emulate the routine and its behaviour you described using fputc, fgetc and fstatus functions.

 

Regards,

PetrM

0 Kudos
Reply

5,062 Views
Jairo
Contributor III

The UART modules have two operation modes polled and interrupt driven, the default mode is polled, to enable the interrupt driven you need to modify the user_config.h inn the BSP and recompile, i can't give you example code but look on the hello2 example as far as i can remember in that example you can modify some commented lines to convert the program from polled to interrupt.

 

Best Regards

0 Kudos
Reply

5,062 Views
saibhargavi
Contributor II

Hi,

 

Can we use an ISR to receive the data from UART port instead of read??. Can anyone send me an example code for RX and TX using ISR??

 

Best Regards

 

Sai Bhargavi

0 Kudos
Reply

5,062 Views
Jairo
Contributor III

Hi, i tried to use the io example in the 52259DEMOKIT board, but replacing the fopen("ttyb:"...) with the the ttyc: port, which is the port in the accesible pins, I changed the value of the macro BSPCFG_ENABLE_TTYC to 1 in m52259.h, recompiled the bsp and psp but nothing is coming out by the port.Even with all connections copied from the schematics

 

 pointer fh_ptr;   printf("This is printed to the default device\n");   fflush(stdout);   if (!strcmp("ttya:", BSP_DEFAULT_IO_CHANNEL))     fh_ptr = (pointer)fopen("ttyc:", BSP_DEFAULT_IO_OPEN_MODE);   else     fh_ptr = (pointer)fopen("ttya:", BSP_DEFAULT_IO_OPEN_MODE);   if (fh_ptr == NULL) {      printf("cannot open the other IO device\n");   } else {      _io_set_handle(IO_STDOUT, fh_ptr);      printf("This is printed to the other device\n");   }      fflush(stdout);   _mqx_exit(0); 

 

 Anyone have an idea of what i'm missing?

 

Thanks in advance.

 

0 Kudos
Reply

5,062 Views
eGuy
Contributor IV

Hi,

 

Search for  BSP_DEFAULT_IO_OPEN_MODE in MQX code,you will find some thining like:

 

#define BSP_DEFAULT_IO_OPEN_MODE                      (pointer) (IO_SERIAL_XON_XOFF | IO_SERIAL_TRANSLATION | IO_SERIAL_ECHO)
 

 

remove the "IO_SERIAL_ECHO" part. then the "1"  you pressed will not echo back.

 

hope this helps.

 

 

Regards,

 

Jeff

0 Kudos
Reply

5,062 Views
eGuy
Contributor IV
you can also change BSP_DEFAULT_IO_OPEN_MODE define to IO_SERIAL_RAW_IO if you don't need XON/XOFF to be handled by MQX low level serial driver.
0 Kudos
Reply

5,063 Views
saibhargavi
Contributor II

Hi,

 

I have one more doubt. Say for example if i need to receive a response from the slave within 100 ms after i transmit a request

 

Case1:

In a non- RTOS code we will just run a timer and see if we have receievd a response before the timer expiry. If not received then we will reset the buffer..

 

Case2:

 

how will we chck for this timing in the uart0 task routine ???? ot in what way we should check for this timer expiry :smileyhappy:

 

Thanks and Regards

 

Sai Bhargavi

0 Kudos
Reply

5,063 Views
JuroV
NXP Employee
NXP Employee
You can for example test time expired (use  _time_get(...)) for some interval value between 2 reads. You can use lwevent and wait for an event for specified time (event will be set by next task when char was received), you can use MQX's timer component. There are many ways.
0 Kudos
Reply