Skip to content

Commit

Permalink
uartbone: put linux specific code out of uartbone.c
Browse files Browse the repository at this point in the history
To allow compiling uartbone.c with a bare-metal RISC-V toolchain.

Signed-off-by: Yann Sionneau <[email protected]>
  • Loading branch information
fallen committed Mar 3, 2024
1 parent e758551 commit 776722c
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 111 deletions.
6 changes: 4 additions & 2 deletions software/libuartbone/Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
uartbone_cli: linux_cli.o uartbone.o
uartbone_cli: linux_cli.o uartbone.o uartbone_linux.o
$(CC) $(LDFLAGS) -o $@ $^

linux_cli.o: linux_cli.c

uartbone.o: uartbone.c

uartbone_linux.o: uartbone_linux.c

clean:
rm -f linux_cli.o uartbone.o uartbone_cli
rm -f linux_cli.o uartbone.o uartbone_linux.o uartbone_cli
109 changes: 0 additions & 109 deletions software/libuartbone/uartbone.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
#include <arpa/inet.h> // for byte order related functions
#include <stdbool.h>
#include <errno.h>
#include <fcntl.h> // Contains file controls like O_RDWR
#include <termios.h> // Contains POSIX terminal control definitions
#include <unistd.h> // write(), read(), close()
#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -116,107 +111,3 @@ void uartbone_write(struct uartbone_ctx *ctx, uint64_t addr, uint32_t val) {
uart_send(ctx, buffer, cmd_length);
uart_send(ctx, wbval.buff, 4);
}

static speed_t baudrate_to_speed(unsigned int baudrate) {
switch (baudrate) {
case 9600:
return B9600;
case 19200:
return B19200;
case 38400:
return B38400;
case 57600:
return B57600;
case 115200:
return B115200;
case 230400:
return B230400;
case 460800:
return B460800;
case 500000:
return B500000;
case 576000:
return B576000;
case 921600:
return B921600;
case 1000000:
return B1000000;
case 1152000:
return B1152000;
case 1500000:
return B1500000;
case 2000000:
return B2000000;
case 2500000:
return B2500000;
case 3000000:
return B3000000;
case 3500000:
return B3500000;
case 4000000:
return B4000000;
default:
return B0;
}
}

void uartbone_unix_init(struct uartbone_ctx *ctx, char *file, unsigned int baudrate, unsigned int addr_width) {
struct termios tty;
int fd;

ctx->error = 0;
ctx->open = false;
ctx->addr_width = addr_width;

fd = open(file, O_RDWR);

if (fd == -1) {
printf("Error %d could not open uart %s: %s\n", errno, file, strerror(errno));
ctx->error = errno;
return;
}

ctx->fd = fd;

if (tcgetattr(fd, &tty) != 0) {
printf("Error %d from tcgetattr: %s\n", errno, strerror(errno));
ctx->error = errno;
close(fd);
return;
}

tty.c_cflag &= ~PARENB; // Clear parity bit, disabling parity (most common)
tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication (most common)
tty.c_cflag &= ~CSIZE; // Clear all bits that set the data size
tty.c_cflag |= CS8; // 8 bits per byte (most common)
tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control (most common)
tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1)

tty.c_lflag &= ~ICANON;
tty.c_lflag &= ~ECHO; // Disable echo
tty.c_lflag &= ~ECHOE; // Disable erasure
tty.c_lflag &= ~ECHONL; // Disable new-line echo
tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL); // Disable any special handling of received bytes

tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed

tty.c_cc[VTIME] = 10; // Wait for up to 1s (10 deciseconds), returning as soon as any data is received.
tty.c_cc[VMIN] = 0;

// Set in/out baud rate
cfsetispeed(&tty, baudrate_to_speed(baudrate));
cfsetospeed(&tty, baudrate_to_speed(baudrate));

// Save tty settings, also checking for error
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
printf("Error %i from tcsetattr: %s\n", errno, strerror(errno));
ctx->error = errno;
close(fd);
return;
}

ctx->open = true;
}
113 changes: 113 additions & 0 deletions software/libuartbone/uartbone_linux.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#include <errno.h>
#include <fcntl.h> // Contains file controls like O_RDWR
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <termios.h> // Contains POSIX terminal control definitions
#include <unistd.h>

#include "uartbone.h"

static speed_t baudrate_to_speed(unsigned int baudrate) {
switch (baudrate) {
case 9600:
return B9600;
case 19200:
return B19200;
case 38400:
return B38400;
case 57600:
return B57600;
case 115200:
return B115200;
case 230400:
return B230400;
case 460800:
return B460800;
case 500000:
return B500000;
case 576000:
return B576000;
case 921600:
return B921600;
case 1000000:
return B1000000;
case 1152000:
return B1152000;
case 1500000:
return B1500000;
case 2000000:
return B2000000;
case 2500000:
return B2500000;
case 3000000:
return B3000000;
case 3500000:
return B3500000;
case 4000000:
return B4000000;
default:
return B0;
}
}

void uartbone_unix_init(struct uartbone_ctx *ctx, char *file, unsigned int baudrate, unsigned int addr_width) {
struct termios tty;
int fd;

ctx->error = 0;
ctx->open = false;
ctx->addr_width = addr_width;

fd = open(file, O_RDWR);

if (fd == -1) {
printf("Error %d could not open uart %s: %s\n", errno, file, strerror(errno));
ctx->error = errno;
return;
}

ctx->fd = fd;

if (tcgetattr(fd, &tty) != 0) {
printf("Error %d from tcgetattr: %s\n", errno, strerror(errno));
ctx->error = errno;
close(fd);
return;
}

tty.c_cflag &= ~PARENB; // Clear parity bit, disabling parity (most common)
tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication (most common)
tty.c_cflag &= ~CSIZE; // Clear all bits that set the data size
tty.c_cflag |= CS8; // 8 bits per byte (most common)
tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control (most common)
tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1)

tty.c_lflag &= ~ICANON;
tty.c_lflag &= ~ECHO; // Disable echo
tty.c_lflag &= ~ECHOE; // Disable erasure
tty.c_lflag &= ~ECHONL; // Disable new-line echo
tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL); // Disable any special handling of received bytes

tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed

tty.c_cc[VTIME] = 10; // Wait for up to 1s (10 deciseconds), returning as soon as any data is received.
tty.c_cc[VMIN] = 0;

// Set in/out baud rate
cfsetispeed(&tty, baudrate_to_speed(baudrate));
cfsetospeed(&tty, baudrate_to_speed(baudrate));

// Save tty settings, also checking for error
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
printf("Error %i from tcsetattr: %s\n", errno, strerror(errno));
ctx->error = errno;
close(fd);
return;
}

ctx->open = true;
}

0 comments on commit 776722c

Please sign in to comment.