wires

summary refs log tree commit diff
path: root/src/mini_uart.zig
blob: e6145a5a08e896d1ed387bf66ea0bb2333f322a3 (plain) (blame)
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
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 Console = @import("console.zig").Console;

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 setup(_: *const Console) 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(_: *const Console, str: []const u8) void {
    for (str) |b| {
        if (b == '\n') {
            writeByte('\r');
        }
        writeByte(b);
    }
}

pub const console = Console{
    .write = writeString,
    .setup = setup,
};