commit 18b065adf399b281dda30c3f86390c8426c188b6
parent f9d07df75fb361af7e6fb0c3734faac0c877fde1
Author: Sylvia Ivory <git@sivory.net>
Date: Wed, 4 Feb 2026 19:59:06 -0800
Remove C TTY code
Diffstat:
5 files changed, 10 insertions(+), 94 deletions(-)
diff --git a/build.zig b/build.zig
@@ -147,13 +147,14 @@ fn build_installer(b: *std.Build, target: std.Build.ResolvedTarget, optimize: st
.target = target,
});
- installer.addCSourceFile(.{ .file = b.path("include/setup-tty.c") });
- installer.addIncludePath(b.path("include/"));
installer.addImport("shared", shared);
const clap = b.dependency("clap", .{});
installer.addImport("clap", clap.module("clap"));
+ const serial = b.dependency("serial", .{});
+ installer.addImport("serial", serial.module("serial"));
+
const exe = b.addExecutable(.{
.name = "pi-install",
.root_module = installer,
diff --git a/build.zig.zon b/build.zig.zon
@@ -36,6 +36,10 @@
.url = "https://github.com/Hejsil/zig-clap/archive/refs/tags/0.11.0.tar.gz",
.hash = "clap-0.11.0-oBajB-HnAQDPCKYzwF7rO3qDFwRcD39Q0DALlTSz5H7e",
},
+ .serial = .{
+ .url = "https://github.com/ZigEmbeddedGroup/serial/archive/fbd7389ff8bbc9fa362aa74081588755b5d028a0.tar.gz",
+ .hash = "serial-0.0.1-PoeRzF60AAAN8Iu0yXTIX-t3DVzsnmN7vWHKM2HA2Zbq",
+ },
},
// Specifies the set of files and directories that are included in this package.
// Only files and directories listed here are included in the `hash` that
diff --git a/include/setup-tty.c b/include/setup-tty.c
@@ -1,83 +0,0 @@
-#include <fcntl.h>
-#include <string.h>
-#include <sys/stat.h>
-#include <termios.h>
-
-#include "setup-tty.h"
-
-unsigned parse_baud(unsigned rate) {
- switch(rate) {
- case 9600: return B9600;
- case 115200: return B115200;
- case 230400: return B230400;
- case 460800: return B460800;
- #ifdef B576000
- case 576000: return B576000;
- #endif
- #ifdef B921600
- case 921600: return B921600;
- #endif
- #ifdef B1000000
- case 1000000: return B1000000;
- #endif
- default:
- return B115200;
- }
-}
-
-// XXX: if anyone figures out a cleaner way to do this, lmk. I don't
-// have a mac, so stopped as soon as we had something that worked on
-// linux & macos.
-// params:
-// - <timeout> is in seconds (< 1 ok)
-// - <speed> is baud rate.
-int set_tty_to_8n1(int fd, unsigned speed, double timeout) {
- struct termios tty;
- memset(&tty, 0, sizeof tty);
- if (tcgetattr (fd, &tty) != 0)
- return -1;
- memset (&tty, 0, sizeof tty);
-
- // https://github.com/rlcheng/raspberry_pi_workshop
- cfsetspeed(&tty, speed);
-
- // disable IGNBRK for mismatched speed tests; otherwise receive break
- // as \000 chars
-
- // XXX: wait, does this disable break or ignore-ignore break??
- tty.c_iflag &= ~IGNBRK; // disable break processing
- tty.c_lflag = 0; // no signaling chars, no echo,
- // no canonical processing
- tty.c_oflag = 0; // no remapping, no delays
- tty.c_cc[VMIN] = 0; // read doesn't block
- // VTIME is in .1 seconds, so have to multiply by 10.
- tty.c_cc[VTIME] = (int)(timeout *10); // this seems to cause issues?
-
- /*
- * Setup TTY for 8n1 mode, used by the pi UART.
- */
-
- // Disables the Parity Enable bit(PARENB),So No Parity
- tty.c_cflag &= ~PARENB;
- // CSTOPB = 2 Stop bits,here it is cleared so 1 Stop bit
- tty.c_cflag &= ~CSTOPB;
- // Clears the mask for setting the data size
- tty.c_cflag &= ~CSIZE;
- // Set the data bits = 8
- tty.c_cflag |= CS8;
- // No Hardware flow Control
- tty.c_cflag &= ~CRTSCTS;
- // Enable receiver,Ignore Modem Control lines
- tty.c_cflag |= CREAD | CLOCAL;
-
- // Disable XON/XOFF flow control both i/p and o/p
- tty.c_iflag &= ~(IXON | IXOFF | IXANY);
- // Non Cannonical mode
- tty.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG);
- // No Output Processing
- tty.c_oflag &= ~OPOST;
-
- if(tcsetattr (fd, TCSANOW, &tty) != 0)
- return -1;
- return fd;
-}
diff --git a/include/setup-tty.h b/include/setup-tty.h
@@ -1,2 +0,0 @@
-unsigned parse_baud(unsigned rate);
-int set_tty_to_8n1(int fd, unsigned speed, double timeout);
diff --git a/installer/root.zig b/installer/root.zig
@@ -1,10 +1,8 @@
const std = @import("std");
const clap = @import("clap");
+const serial = @import("serial");
const bootloader = @import("./bootloader.zig");
-const c = @cImport({
- @cInclude("setup-tty.h");
-});
pub fn main() !void {
var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;
@@ -51,7 +49,7 @@ pub fn main() !void {
}
const tty_path = res.args.device orelse "/dev/ttyUSB0";
- const baud = c.parse_baud(res.args.baud orelse 921600);
+ const baud = res.args.baud orelse 921600;
const path = res.positionals[0] orelse return error.MissingFile;
const base_addr = res.args.@"arm-base" orelse 0x8000;
const timeout = res.args.timeout orelse 10.0;
@@ -60,9 +58,7 @@ pub fn main() !void {
const tty = try std.fs.openFileAbsolute(tty_path, .{ .allow_ctty = true, .mode = .read_write });
- if (c.set_tty_to_8n1(tty.handle, baud, timeout) == -1) {
- return error.SetTTY;
- }
+ try serial.configureSerialPort(tty, .{ .baud_rate = baud, .handshake = .none, .parity = .none, .stop_bits = .one, .word_size = .eight });
var code: []u8 = undefined;