UART

The UART submodule uses interrupts to send and receive data on the UART port through a circular buffer. Once the receive buffer is full, incoming bytes are dropped. More information about accessing peripheral IO is in the Device Interface section.

The following is an example of how to read/write the UART in an OS environment:

#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <dev/uart.h>

int read_write_uart(){
     int fd;
     uart_attr_t attr;
     char buffer[16];

     fd = open("/dev/uart0", O_RDWR); //Here O_RDWR could be OR'd with O_NONBLOCK for non blocking operation
     if ( fd < 0 ){
          printf("Error opening peripheral (%d)\n", errno);
     } else {
          //initializes using default configuration
          if( ioctl(fd, I_SETATTR, 0) < 0 ){
               perror("failed to set attr");
               return -1;
     }

     //Now read or write the UART
     strcpy(buffer, "UART Test\n");
     if ( write(fd, buffer, strlen(buffer) < 0 ){ //returns after all bytes have been written
          perror("failed to write UART");
     }

     if ( read(fd, buffer, 16) < 0 ){ //doesn't return until at least 1 bytes arrives (use O_NONBLOCK to return with whatever is available)
          perror("failed to read UART");
     }

     }
     close(fd);
     return 0;
}

Details

define UART_VERSION

define UART_IOC_IDENT_CHAR

define UART_PIN_ASSIGNMENT_COUNT

define I_UART_GETVERSION

define I_UART_GETINFO

define I_UART_SETATTR

define I_UART_SETACTION

define I_UART_GET

This gets a single byte from the UART. The request returns -1 if no bytes are available to get. Otherwise, 0 is returned and the character is written to the ctl argument. Example:

#include <dev/uart.h>

char c;
if ( ioctl(uart_fd, I_UART_GET, &c) == 0 ){
     //one byte from the UART was written to c
}

define I_UART_PUT

This gets a single byte from the UART. The request returns -1 if no bytes are available to get. Otherwise, 0 is returned and the character is written to the ctl argument. Example:

#include <dev/uart.h>

char c = 'j';
if ( ioctl(uart_fd, I_UART_PUT, c) == 0 ){
     //one byte from the UART was written to c
}

define I_UART_FLUSH

This flushes all incoming bytes from the buffer.

#include <dev/uart.h>
ioctl(uart_fd, I_UART_FLUSH);

define I_UART_TOTAL

enum uart_flag_t
  • UART_FLAG_SET_LINE_CODING Set the control line state (baudrate, stop bits, etc)
  • UART_FLAG_IS_STOP1 One stop bit (default behavior)
  • UART_FLAG_IS_STOP2 Two stop bits
  • UART_FLAG_IS_STOP0_5 0.5 stop bits
  • UART_FLAG_IS_STOP1_5 1.5 stop bits
  • UART_FLAG_IS_PARITY_NONE Indicates no parity (default behavior)
  • UART_FLAG_IS_PARITY_ODD Indicates odd parity
  • UART_FLAG_IS_PARITY_EVEN Indicates even parity
  • UART_FLAG_SET_LINE_CODING_DEFAULT Sets line coding to no parity and one stop bit
  • UART_FLAG_IS_RX_FIFO The UART supports an receive buffer FIFO (read only using I_UART_GETINFO)

struct::uart_info_t

u32 o_flags

A bitmask for the supported features

u32 o_events

Events supported by this driver

u32 size_ready

Number of bytes ready if a FIFO is built-in

u32 size

Number of bytes available to the FIFO

u32 resd

struct::uart_pin_assignment_t

mcu_pin_t tx

mcu_pin_t rx

mcu_pin_t rts

mcu_pin_t cts

struct::uart_attr_t

This structure defines how the control structure for opening or reconfiguring the UART port.

u32 o_flags

u32 freq

uart_pin_assignment_t pin_assignment

u8 width

u32 resd

X

Thanks for Coming!

Subscribe to news and updates