-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
uartbone: put linux specific code out of uartbone.c
To allow compiling uartbone.c with a bare-metal RISC-V toolchain. Signed-off-by: Yann Sionneau <[email protected]>
- Loading branch information
Showing
3 changed files
with
117 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |