This class implements a serial UART port.

Here is an example of how to use the UART.

#include <stfy/hal.hpp>
int main(int argc, char * argv[]){
Uart uart(0); //use UART0
char buffer[256];
uart.init(115200); //initializes using 115200 and default parameters (see init()), 256); //this will block until at least one byte arrives
uart.close(); //free the file descriptors and power down the device
return 0;

The above example opens using the UART in blocking mode. If you want to be able to read the UART without blocking until a byte arrives, you can use non-blocking mode.

#include <stfy/hal.hpp>
int main(int argc, char * argv[]){
Uart uart(0);
char buffer[256];
//now set the attributes
uart.set_attr(115200, //115200 baud rate
0, //pin assignment 0 (P0.2 and P0.3 on LPC17xx)
Uart::NONE, // no parity
Uart::STOP1, //one stop bit
8 //8 bits per character
);, 256); //returns immediately even if no data is available (errno is set to EAGAIN if no data)
uart.close(); //free the resources

Public Types

- Public Types inherited from hal::Dev

Public Member Functions

int flush ()
int get_attr (uart_attr_t &attr)
int get_byte (char *c)
int init (int baudrate=19200, int pin_assign=0, uart_parity_t parity=UART_PARITY_NONE, int stop=UART_ATTR_STOP_BITS_2, int width=8)
int set_attr (const uart_attr_t &attr)
- Public Member Functions inherited from hal::Periph
int close ()
int ioctl (int req, void *arg) const
int open (int flags=READWRITE)
int read (void *buf, int nbyte) const
int read (sys::Aio &aio) const
int seek (int loc, int whence) const
int write (const void *buf, int nbyte) const
int write (sys::Aio &aio) const
- Public Member Functions inherited from hal::Dev
int fileno () const
int flags () const
char * gets (char *s, int n, char term= '\n') const
char * gets (var::String &s, char term= '\n') const
int ioctl (int req, const void *arg) const
int ioctl (int req) const
int ioctl (int req, int arg) const
int loc () const
int read (int loc, void *buf, int nbyte) const
int readline (char *buf, int nbyte, int timeout, char term) const
int write (int loc, const void *buf, int nbyte) const
int write (const char *s) const
int write (const var::String &str) const

Additional Inherited Members

- Static Public Member Functions inherited from hal::Dev
static const char * name (const char *path)
- Protected Member Functions inherited from hal::Periph
int open (const char *name, int flags)

Member Enumeration Documentation

anonymous enum

UART Parity values.

This is a list of the UART parity values


No Parity


Even Parity


Odd Parity

anonymous enum

UART Stop bits.

This is a list of the UART stop bit values


One stop bit


Two stop bits

Member Function Documentation

int hal::Uart::flush ( )

Flush the TX/RX buffers

int hal::Uart::get_attr ( uart_attr_t attr)

Get UART attributes in attr

int hal::Uart::get_byte ( char *  c)

This method gets a single byte (if available from the UART). Upon success, the byte is written to the value pointed to by c.

Zero on successfully reading a byte, -1 if no bytes are available.
int hal::Uart::init ( int  baudrate = 19200,
int  pin_assign = 0,
uart_parity_t  parity = UART_PARITY_NONE,
int  stop = UART_ATTR_STOP_BITS_2,
int  width = 8 

Initialize the UART to

  • 19200 baudrate
  • default pin assignment
  • No parity
  • 8 bit width
  • 2 stop bits
    Zero on success.
int hal::Uart::set_attr ( const uart_attr_t attr)

Set UART attributes to attr

int hal::Uart::set_attr ( int  baudrate = 19200,
int  pin_assign = 0,
int  parity = UART_PARITY_NONE,
int  stop = STOP1,
int  width = 8 

Set the attributes as specified.

pin_assignPin assignment
stopNumber of stop bits
widthbits per character