aboutsummaryrefslogtreecommitdiffstats
path: root/src/gpio.zig
blob: 9313c38ab8a7e13b9c373c7186f3e44689f9a3e9 (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (C) 2024 Clyne Sullivan <clyne@bitgloo.com>
//
// Distributed under the GNU GPL v3 or later. You should have received a copy of
// the GNU General Public License along with this program.
// If not, see <https://www.gnu.org/licenses/>.

const mode = enum(u32) {
    alternate_0 = 0,
    alternate_1,
    alternate_2,
    alternate_3,
    alternate_4,
    alternate_5,
    alternate_6,
    alternate_7,
    alternate_8,
    alternate_9,
    alternate_10,
    alternate_11,
    alternate_12,
    alternate_13,
    alternate_14,
    alternate_15,
    input,
    output,
    analog,
};

const driver_stm32 = packed struct {
    moder: u32,
    otyper: u32,
    ospeedr: u32,
    pupdr: u32,
    idr: u32,
    odr: u32,
    bsrr: u32,
    lckr: u32,
    afr: u64,
    brr: u32,
    ascr: u32,

    pub fn set_mode(self: *driver_stm32, pin: u4, m: mode) void {
        const offset = 2 * @as(u5, pin);
        const moder = self.moder & ~(@as(u32, 3) << offset);

        const mask: u32 = switch (m) {
            .input => 0,
            .output => 1,
            .analog => 3,
            else => 2,
        };

        self.moder = moder | (mask << offset);

        if (mask == 2) {
            const afr_offset = (4 * @as(u6, pin));
            const afr = self.afr & ~(@as(u64, 0xF) << afr_offset);
            self.afr = afr | (@as(u64, @intFromEnum(m)) << afr_offset);
        }
    }

    pub fn toggle(self: *driver_stm32, pin: u4) void {
        self.odr ^= @as(u32, 1) << pin;
    }

    pub fn set(self: *driver_stm32, pin: u4) void {
        self.odr |= @as(u32, 1) << pin;
    }

    pub fn clear(self: *driver_stm32, pin: u4) void {
        self.odr &= ~(@as(u32, 1) << pin);
    }

    pub fn write(self: *driver_stm32, pin: u4, val: bool) void {
        if (val) {
            self.set(pin);
        } else {
            self.clear(pin);
        }
    }

    pub fn read(self: driver_stm32, pin: u4) bool {
        return (self.idr & @as(u32, 1) << pin) != 0;
    }
};

const driver = struct {
    const lld_type = driver_stm32;

    lld: *lld_type,

    pub fn set_mode(self: driver, pin: u4, m: mode) void {
        self.lld.set_mode(pin, m);
    }

    pub fn toggle(self: driver, pin: u4) void {
        self.lld.toggle(pin);
    }

    pub fn set(self: driver, pin: u4) void {
        self.lld.set(pin);
    }

    pub fn clear(self: driver, pin: u4) void {
        self.lld.clear(pin);
    }

    pub fn write(self: driver, pin: u4, val: bool) void {
        self.lld.write(pin, val);
    }

    pub fn read(self: driver, pin: u4) bool {
        return self.lld.read(pin);
    }
};

pub const gpioa = driver { .lld = @ptrFromInt(0x48000000) };
pub const gpiob = driver { .lld = @ptrFromInt(0x48000400) };
pub const gpioc = driver { .lld = @ptrFromInt(0x48000800) };
pub const gpiod = driver { .lld = @ptrFromInt(0x48000C00) };
pub const gpioe = driver { .lld = @ptrFromInt(0x48001000) };
pub const gpiof = driver { .lld = @ptrFromInt(0x48001400) };
pub const gpiog = driver { .lld = @ptrFromInt(0x48001800) };
pub const gpioh = driver { .lld = @ptrFromInt(0x48001C00) };