hal::Uart Class Reference

Documentation

This class implements a serial UART port.

Here is an example of how to use the UART.

#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];
//now set the attributes
115200, //115200 baud rate
8);
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 Member Functions

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 >
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
 

Member Enumeration Documentation

anonymous enum
Enumerator
FLAG_SET_CONTROL_LINE_STATE 

See UART_FLAG_SET_CONTROL_LINE_STATE

FLAG_IS_STOP1 

See UART_FLAG_IS_STOP1

FLAG_IS_STOP2 

See UART_FLAG_IS_STOP2

FLAG_IS_STOP0_5 

See UART_FLAG_IS_STOP0_5

FLAG_IS_STOP1_5 

See UART_FLAG_IS_STOP1_5

FLAG_IS_PARITY_NONE 

See UART_FLAG_IS_PARITY_NONE

FLAG_IS_PARITY_ODD 

See UART_FLAG_IS_PARITY_ODD

FLAG_IS_PARITY_EVEN 

See UART_FLAG_IS_PARITY_EVEN

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