1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
const std = @import("std");
const board = @import("board.zig");
const AUX_BASE = board.AUX_BASE;
const AUX_ENABLE = board.AUX_ENABLE;
const gpio = @import("gpio.zig");
const mmio = @import("mmio.zig");
const IO = AUX_BASE + 0x40;
const IER = AUX_BASE + 0x44;
const IIR = AUX_BASE + 0x48;
const LCR = AUX_BASE + 0x4c;
const MCR = AUX_BASE + 0x50;
const LSR = AUX_BASE + 0x54;
const MSR = AUX_BASE + 0x58;
const SCRATCH = AUX_BASE + 0x5c;
const CNTL = AUX_BASE + 0x60;
const STAT = AUX_BASE + 0x64;
const BAUD = AUX_BASE + 0x68;
pub fn enable() void {
board.enableAux(.mini_uart);
gpio.setPull(14, .none);
gpio.setFunction(14, .alt5);
gpio.setPull(15, .none);
gpio.setFunction(15, .alt5);
mmio.write(IER, 0); // disable interrupts
mmio.write(IIR, 6); // clear FIFOs
mmio.write(LCR, 3); // 8 bit mode
mmio.write(BAUD, baudRegVal(115200));
mmio.write(CNTL, 0x3);
}
fn baudRegVal(baud: comptime_int) comptime_int {
return 500000000 / (baud * 8) - 1;
}
fn writeByte(b: u8) void {
while ((mmio.read(LSR) & 0x20) == 0) {}
mmio.write(IO, b);
}
fn writeString(str: []const u8) void {
for (str) |b| {
if (b == '\n') {
writeByte('\r');
}
writeByte(b);
}
}
const Writer = std.Io.Writer;
fn drain(
w: *Writer,
data: []const []const u8,
_: usize,
) Writer.Error!usize {
var written: usize = 0;
writeString(w.buffer[0..w.end]);
written += w.end;
w.end = 0;
for (data) |slice| {
writeString(slice);
written += slice.len;
}
return written;
}
var buf: [1024]u8 = undefined;
var writer: Writer = .{
.vtable = &.{ .drain = drain },
.buffer = &buf,
};
pub fn print(comptime fmt: []const u8, args: anytype) void {
writer.print(fmt, args) catch {};
writer.flush() catch {};
}
|