I can see how to move the console between UART0 and UART1, but how do I get it on UART2?
Thanks.
Console on UART2
-
- Posts: 513
- Joined: Sat Apr 26, 2008 7:14 am
Re: Console on UART2
Most of the NB development boards only have rs232 drivers for com0 and 1. You can use Com2 if the 52xx pins are properly jumpered (ie no other hardware conflicts) and if configured (usuallly via OpenSerial).
You may also need to add the appropriate rs232/422/485 drivers if you need more than logic levels. For prototyping '232, I use these little boards all the time: http://www.acroname.com/robotics/parts/ ... -CONN.html if the handshake lines are not required, and this one if they are: http://www.softbaugh.com/ProductPage.cf ... tNo=BRS232
You may also need to add the appropriate rs232/422/485 drivers if you need more than logic levels. For prototyping '232, I use these little boards all the time: http://www.acroname.com/robotics/parts/ ... -CONN.html if the handshake lines are not required, and this one if they are: http://www.softbaugh.com/ProductPage.cf ... tNo=BRS232
-
- Posts: 38
- Joined: Tue Jun 03, 2008 10:26 am
Re: Console on UART2
I am sorry I did not explain myself sufficiently. I have UART2 functioning perfectly physically. I just want to know if "printf" etc. can be routed there instead of UART0 or UART1.
Thanks again.
Thanks again.
-
- Posts: 38
- Joined: Tue Jun 03, 2008 10:26 am
Re: Console on UART2
I am surprised that no one knows this. Anyhow, even if the setup tools only permit UART0 and UART1 for stdio redirection, it is possible to use c code in the application to do this.
http://www.netburner.com/support/techni ... stdout.htm
http://www.netburner.com/support/techni ... stdout.htm
Re: Console on UART2
I have not tried it, but i think it works.
SerialClose( 0 );
int fd1 = OpenSerial( 2, 115200, 1, 8, eParityNone );
// ReRoute Uart0 to fd1
ReplaceStdio( 0, fd1);
SerialClose( 0 );
int fd1 = OpenSerial( 2, 115200, 1, 8, eParityNone );
// ReRoute Uart0 to fd1
ReplaceStdio( 0, fd1);
-
- Posts: 513
- Joined: Sat Apr 26, 2008 7:14 am
Re: Console on UART2
OK... You can mess around with ReplaceStdio. However a few cautionary notes: stdio defaults to polled mode, so if you use OpenSerial to get an fd that you would like to associate with stdio do something like this (assuming for a moment that we are just talking about com0):
SerialClose( 0 );
//==============================================================================
// Get an fd for the COM0 UART. This will invoke interrput-based
// serial coms. We'll use Com0 for the STDIO (default). The parameters below were
// previously #defined.
fdCOM0 = OpenSerial( Com0Port,
COM0BaudRate,
TwoStopBits,
EightDataBits,
eParityNone );
StdIOCom_fd = fdCOM0; // create an alias to be clear about your intent
// Now that the fdCOM0 is ready for use, make sure stdio uses the
// same fd IRQ based routines and does not try to use the default polled
// routines at the same time.
ReplaceStdio( STDIN_FD, StdIOCom_fd );
ReplaceStdio( STDOUT_FD, StdIOCom_fd );
ReplaceStdio( STDERR_FD, StdIOCom_fd );
Now stdio will use the interrupt based com0 drivers and you will have an fd that you can use for things like read(), write(), writestring() and the other character IO functions in the IO system part of the runtime library.
Now for printf()... printf() wants to work w/ stdio. As seulater mentioned, you can redirect stdio to other ports as long as you have an fd. However, this is often not such a great idea because one task often thinks it has stdio and that it is directed out a given com port. If another task redirects it somewhere else to a different port, unexpected result are nearly guaranteed.
A much simpler approach is to use the printf() variants: sprintf, snprintf() sniprintf() to format a string for output.These functions store the result into a predefined string buffer which you can easily writestring to any desired fd. There is also a great utility that does this in one step: fdprintf() that will do printf() formating and send it to any fd instead of to a string. Here is some example code:
int Ticks = 0; // A variable to foo with....
// Create a buffer to store chars we'll send out the "GPS" or COM_n port
#define MAX_CHARS_IN_GPSBUF (256)
char GPS_buf[MAX_CHARS_IN_GPSBUF];
//1) Send chars out stdout...
iprintf("This is MyTaskask #%d\r\n", Ticks++ );
//----------------------------------------------------------------------
// Send chars out the serial port associated with the GPS_fd (COM_n, created w/OpenSerial)...
//2A) First format and store a string in a buffer...
sniprintf( GPS_buf,
MAX_CHARS_IN_GPSBUF,
"MyTask is talking over the GPS_fd, Ticks = %d\r\n",
Ticks);
// 2B) Then write the string to the desired fd. This could be a serial port opened via OpenSerial()
// or any oher type of fd:
writestring( GPS_fd, GPS_buf); // now
//----------------------------------------------------------------------
// 3) or skip the two step sniprintf/writestring approach and use fdprintf() in one step. fdprintf() is defined in
// C:\nburn\examples\fdprintf\main.cpp. It is very handy for this kind of thing....
fdprintf(GPS_fd, " --MyTask: Ticks = %d = 0x%04x\r\n", Ticks, Ticks);
SerialClose( 0 );
//==============================================================================
// Get an fd for the COM0 UART. This will invoke interrput-based
// serial coms. We'll use Com0 for the STDIO (default). The parameters below were
// previously #defined.
fdCOM0 = OpenSerial( Com0Port,
COM0BaudRate,
TwoStopBits,
EightDataBits,
eParityNone );
StdIOCom_fd = fdCOM0; // create an alias to be clear about your intent
// Now that the fdCOM0 is ready for use, make sure stdio uses the
// same fd IRQ based routines and does not try to use the default polled
// routines at the same time.
ReplaceStdio( STDIN_FD, StdIOCom_fd );
ReplaceStdio( STDOUT_FD, StdIOCom_fd );
ReplaceStdio( STDERR_FD, StdIOCom_fd );
Now stdio will use the interrupt based com0 drivers and you will have an fd that you can use for things like read(), write(), writestring() and the other character IO functions in the IO system part of the runtime library.
Now for printf()... printf() wants to work w/ stdio. As seulater mentioned, you can redirect stdio to other ports as long as you have an fd. However, this is often not such a great idea because one task often thinks it has stdio and that it is directed out a given com port. If another task redirects it somewhere else to a different port, unexpected result are nearly guaranteed.
A much simpler approach is to use the printf() variants: sprintf, snprintf() sniprintf() to format a string for output.These functions store the result into a predefined string buffer which you can easily writestring to any desired fd. There is also a great utility that does this in one step: fdprintf() that will do printf() formating and send it to any fd instead of to a string. Here is some example code:
int Ticks = 0; // A variable to foo with....
// Create a buffer to store chars we'll send out the "GPS" or COM_n port
#define MAX_CHARS_IN_GPSBUF (256)
char GPS_buf[MAX_CHARS_IN_GPSBUF];
//1) Send chars out stdout...
iprintf("This is MyTaskask #%d\r\n", Ticks++ );
//----------------------------------------------------------------------
// Send chars out the serial port associated with the GPS_fd (COM_n, created w/OpenSerial)...
//2A) First format and store a string in a buffer...
sniprintf( GPS_buf,
MAX_CHARS_IN_GPSBUF,
"MyTask is talking over the GPS_fd, Ticks = %d\r\n",
Ticks);
// 2B) Then write the string to the desired fd. This could be a serial port opened via OpenSerial()
// or any oher type of fd:
writestring( GPS_fd, GPS_buf); // now
//----------------------------------------------------------------------
// 3) or skip the two step sniprintf/writestring approach and use fdprintf() in one step. fdprintf() is defined in
// C:\nburn\examples\fdprintf\main.cpp. It is very handy for this kind of thing....
fdprintf(GPS_fd, " --MyTask: Ticks = %d = 0x%04x\r\n", Ticks, Ticks);