hal::Uart Class Reference
Inheritance diagram for hal::Uart:

Documentation

This class implements a serial UART port.

Here is an example of how to use the UART using default parameters provide by the board support package.

#include <sapi/hal.hpp>
int main(int argc, char * argv[]){
Uart uart(0); //use UART0
char buffer[256];
uart.init(); //initializes using default parameters
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 <sapi/hal.hpp>
int main(int argc, char * argv[]){
Uart uart(0);
char buffer[256];
UartPinAssignment pin_assignment;
//now set the attributes
pin_assignment->tx = mcu_pin(0,0);
pin_assignment->rx = mcu_pin(0,1);
uart.set_attr(Uart::FLAG_IS_STOP1|Uart::FLAG_IS_PARITY_NONE,
115200, //115200 baud rate
8,
pin_assignment); //this value can be null to use the BSP's default pin assignment values
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
}

The UART can also be configured directly using the POSIX API.

#include <sos/dev/uart.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char * argv[]){
int fd;
char str[] = "Hello!\n";
fd = open("/dev/uart0", O_RDWR);
if( fd < 0 ){
perror("Failed to open uart");
} else {
attr.width = 8;
attr.freq = 115200;
attr.pin_assignment.tx = mcu_pin(0,0);
attr.pin_assignment.rx = mcu_pin(0,1);
attr.pin_assignment.cts = mcu_pin(0xff,0xff); //don't use CTS
attr.pin_assignment.rts = mcu_pin(0xff,0xff); //don't use RTS
if( ioctl(fd, I_UART_SETATTR, &attr) < 0 ){
perror("Failed to set uart attr");
} else {
write(fd, str, strlen(str));
}
close(fd);
}
return 0;
}
See also
hal::UartPinAssignment
hal::UartAttr

Public Member Functions

 Uart (port_t port)
 
int flush ()
 
int get (char &c)
 
int init (u32 o_flags, u32 freq, u32 width=8, const uart_pin_assignment_t *pin_assignment=0)
 
int put (char c)
 
int set_attr (u32 o_flags, u32 freq, u32 width=8, const uart_pin_assignment_t *pin_assignment=0) const
 
- Public Member Functions inherited from hal::Periph< uart_info_t, uart_attr_t, UART_IOC_IDENT_CHAR >
 Periph (core_periph_t periph, port_t port)
 
int get_info (uart_info_t &info) const
 
int get_version () const
 
int init ()
 
int set_action (u32 channel, u32 o_events, s8 prio=0, mcu_callback_t callback=0, void *context=0) const
 
int set_attr () const
 
int set_priority (s8 priority, u32 o_events, int channel=0)
 
- Public Member Functions inherited from hal::PeriphObject
int close ()
 
int ioctl (int req, void *arg) const
 
int open (int o_mode=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::Device
 Device ()
 
int set_signal_action (const DeviceSignal &signal, u32 o_events, u32 channel)
 
- 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 (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_msec, char terminator= '\n') const
 
void set_fileno (int fd)
 
u32 size () const
 
int write (int loc, const void *buf, int nbyte) const
 
int write (const var::String &str) const
 
- Public Member Functions inherited from api::WorkObject
void clear_error_number () const
 
int error_number () const
 

Additional Inherited Members

- Public Types inherited from hal::PeriphObject
typedef unsigned int port_t
 
- Public Types inherited from sys::File
- Static Public Member Functions inherited from sys::File
static int access (const char *path, int o_access)
 
static int copy (var::String &source_path, var::String &dest_path)
 
static bool exists (const char *path)
 
static const char * name (const char *path)
 
static int remove (const char *path)
 
static int rename (var::String &old_path, var::String &new_path)
 
static u32 size (const char *path)
 
static int stat (const char *path, struct stat *st)
 
static const char * suffix (const char *path)
 
- Static Public Member Functions inherited from api::ApiObject
static const char * api_version ()
 
- Protected Member Functions inherited from hal::PeriphObject
int open (const char *name, int flags)
 

Member Enumeration Documentation

anonymous enum
Enumerator
SET_LINE_CODING 

See UART_FLAG_SET_LINE_CODING

IS_STOP1 

See UART_FLAG_IS_STOP1

IS_STOP2 

See UART_FLAG_IS_STOP2

IS_STOP0_5 

See UART_FLAG_IS_STOP0_5

IS_STOP1_5 

See UART_FLAG_IS_STOP1_5

IS_PARITY_NONE 

See UART_FLAG_IS_PARITY_NONE

IS_PARITY_ODD 

See UART_FLAG_IS_PARITY_ODD

IS_PARITY_EVEN 

See UART_FLAG_IS_PARITY_EVEN

Constructor & Destructor Documentation

hal::Uart::Uart ( port_t  port)

Constructs a new Uart object.

Parameters
portThe port to use (Zero is always the first port)

Member Function Documentation

int hal::Uart::flush ( )

Flushes the TX/RX buffers.

int hal::Uart::get ( char &  c)

Reads 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 ( u32  o_flags,
u32  freq,
u32  width = 8,
const uart_pin_assignment_t *  pin_assignment = 0 
)
inline

Initializes the uart with the specified attributes.

Parameters
o_flagsBitmask of attribute flags
freqBaudrate
widthNumber of bits (usually 8)
pin_assignmentA pointer to the pin assignment (null to use the default)
Returns
Zero on success

This method calls open() and set_attr().

int hal::Uart::put ( char  c)

Writes a single byte on the UART.

Parameters
cThe character to write
Returns
Zero on success
int hal::Uart::set_attr ( u32  o_flags,
u32  freq,
u32  width = 8,
const uart_pin_assignment_t *  pin_assignment = 0 
) const
inline

Sets the attributes of the Uart.

Parameters
o_flagsBitmask of attribute flags
freqBaudrate
widthNumber of bits (usually 8)
pin_assignmentA pointer to the pin assignment (null to use the default)
Returns
Zero on success