Documentation

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())
uart.read(buffer, 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
);
uart.read(buffer, 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 sys::File

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, int parity=NONE, int stop=STOP1, 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 sys::File
int create (const char *path, bool overwrite=true, int perms=0666)
 
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 open (const char *name, int access, int perms)
 
int open_append (const char *path)
 
int open_readonly (const char *path)
 
int open_readwrite (const char *path)
 
int read (int loc, void *buf, int nbyte) const
 
int readline (char *buf, int nbyte, int timeout, char term) const
 
ssize_t size () 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 sys::File
static const char * name (const char *path)
 
static int remove (const char *path, link_transport_mdriver_t *driver=0)
 
static ssize_t size (const char *path, link_transport_mdriver_t *driver=0)
 
static int stat (const char *path, struct link_stat *st, link_transport_mdriver_t *driver=0)
 
- 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

Enumerator
NONE 

No Parity

EVEN 

Even Parity

ODD 

Odd Parity

anonymous enum

UART Stop bits.

This is a list of the UART stop bit values

Enumerator
STOP1 

One stop bit

STOP2 

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.

Returns
Zero on successfully reading a byte, -1 if no bytes are available.
int hal::Uart::init ( int  baudrate = 19200,
int  pin_assign = 0,
int  parity = NONE,
int  stop = STOP1,
int  width = 8 
)
inline

Initialize the UART to

  • 19200 baudrate
  • default pin assignment
  • No parity
  • 8 bit width
  • 1 stop bits
    Returns
    Zero on success.
Parameters
baudrateBaudrate
pin_assignPin assignment
parityParity
stopNumber of stop bits
widthbits per character
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 = NONE,
int  stop = STOP1,
int  width = 8 
)
inline

Set the attributes as specified.

Parameters
baudrateBaudrate
pin_assignPin assignment
parityParity
stopNumber of stop bits
widthbits per character