class::hal::Uart

class hal::Uart
  : public hal::Periph< uart_info_t, uart_attr_t, UartAttributes, UART_IOC_IDENT_CHAR >

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;
   uart.open(Uart::NONBLOCK|Uart::RDWR);
   //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;
   uart_attr_t attr;
   char str[] = "Hello!\n";
   fd = open("/dev/uart0", O_RDWR);
   if( fd < 0 ){
      perror("Failed to open uart");
   } else {
      attr.o_flags = UART_FLAG_SET_LINE_CODING | UART_FLAG_IS_PARITY_NONE;
      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

See also: hal::UartAttr

Summary

Uart(

port_t port
)

int flush()

int get(

char & c
)

int get_info()

UartInfo get_info() const

int put(

char c
)

enum uart_flags

Members

Uart(

port_t port
)

Constructs a new Uart object.

Parameters

  • port The port to use (Zero is always the first port)

int flush()

Flushes the TX/RX buffers.


int 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 get_info()

Gets the peripheral info.

Parameters

  • info A reference to the info object that will be written.

Returns

Zero on success, less than one for an error


UartInfo get_info() const

int put(

char c
)

Writes a single byte on the UART.

Parameters

  • c The character to write

Returns

Zero on success


enum uart_flags

X

Thanks for Coming!

Subscribe to news and updates