Author Topic: How do I use the serial port (UART) support?  (Read 8781 times)

Offline aaronlawrence

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
How do I use the serial port (UART) support?
« on: March 03, 2010, 05:28:06 AM »
Hello,

What do I need to do to use serial ports? (For output only at this stage).
I want to use UART0 on LPC2366.
I tried looking through code, docs and forums but there doesn't seem to be any general guide.

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3234
    • View Profile
    • uTasker
Re: How do I use the serial port (UART) support?
« Reply #1 on: March 03, 2010, 12:16:12 PM »
Hi Aaron

The UART document: http://www.utasker.com/docs/uTasker/uTaskerUART.PDF should be suitable as a general guide.

- To use the first UART the channel number should be 0.
- To use it for tx only you don't need to enable the Rx:
fnDriver( SerialPortID, ( TX_ON | RX_ON ), 0 );                  // enable rx and tx

However, note that in the present LPC2xxx code the open automatically selects the function pins for Tx and Rx:

        PINSEL0 |= (PINSEL0_P0_2_TXD0 | PINSEL0_P0_3_RXD0);              // TXD0 and RXD0 on P0.2 and P0.3

If you don't want the Rx line being configured you can modify that in LPC23xx.c in fnConfigSCI(). In some processor projects the configuration of the pins has been moved from the open to the RX/TX enable code (fnRxOn(), fnTxOn()) so that only the actually used pin is configured. It is however rare that onyl one direction is used and the other pin has a different function.

Specifically for the LPC2xxx there is a further details that may be relevant. This concerns the clock speed and Baud accuracy. The UARTs are clocked by default from PCLK/4; in some cases this doesn't allow an accurate 115k Baud to be achieved (depends on crystal and PLL setting). In such a case the UART clock can be configured to be equal to PCLK (increasing the resolution by a factor of 4, but also requiring a bit more current) - there are details about how to do this just before the routine extern void fnConfigSCI(QUEUE_HANDLE Channel, TTYTABLE *pars) in LPC23XX.c

Regards

Mark

Offline aaronlawrence

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Re: How do I use the serial port (UART) support?
« Reply #2 on: March 03, 2010, 10:20:15 PM »
Thanks Mark. I didn't notice that doc because it was in the preliminary section.
Explains everything pretty well. In fact it's one of the best and most practical discussions of real UART operation I have ever seen! Kudos again.

Only thing I didn't notice is whether it uses a task of it's own, or is it all interrupt driven?

The API seems fairly complicated, probably more so than writing to the UART directly :) which I am used to ... except of course for the buffering.


Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3234
    • View Profile
    • uTasker
Re: How do I use the serial port (UART) support?
« Reply #3 on: March 04, 2010, 12:41:30 AM »
Hi Aaron

The UART is buffered and fully interrupt driven (some chips support DMA but the LPC2XXX doesn't support DMA to the UARTs...)

Once configured you should find sending ASCII data (as an example) very simple (typical debug output stuff). The standard technique is to set the debug handle to the UART that you would like to use to send debug messages to:
DebugHandle = SerialPortID; // assign the UART handle to the debug output handle.

Then things like:
fnDebugMsg("Hello, World!!\r\n");
will automatically be sent there (buffered so that the code doesn't have to wait for transmission to complete - just ensure that the buffer is large enough to queue what you send to it until it has been sent).

One advantage of this is that the debug output can be easily moved to others. By default the debug output (when not pointed to a UART) is the network output
extern QUEUE_TRANSFER fnNetworkTx(unsigned char *output_buffer, QUEUE_TRANSFER nr_of_bytes);
which will usually be TELNET - so if a TELNET connection is made all debug output shows up there. It can also be pointed to a USB CDC handle (if available - which will unfortunately not be the case in the LPC project at the moment since it doesn't support USB as some other processor packages do - but this will change soon...). By exiting fnNetworkTx() debug output can be sent to multiple outputs (eg. TELNET and 2 x UARTs, etc.) too.

Reading uses the fnRead() call and the read is generally performed in a task which is woken by UART receptions so that it doesn't have to poll the input buffer.

There are also several modes which means that the driver will automatically perform SW or HW flow control or can also control an RS485 transceiver, plus it supports character space timing as used by the MODBUS extension module.

If you have any questions just ask - the UART interfaces are very well used in the project so you should find a suitable set up for your requirement and become quite comfortable with it after a short time. The uTasker simulator allows all UART operation to be tested quite accurately in the simulator, which also makes life (and project development) easier.

Regards

Mark



Offline aaronlawrence

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
Re: How do I use the serial port (UART) support?
« Reply #4 on: March 06, 2010, 04:23:31 AM »
Using it now without drama. Much faster than my output over JTAG :)