-
Notifications
You must be signed in to change notification settings - Fork 190
Expand file tree
/
Copy pathgpio_v2.zig
More file actions
177 lines (152 loc) · 5.41 KB
/
Copy pathgpio_v2.zig
File metadata and controls
177 lines (152 loc) · 5.41 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const std = @import("std");
const microzig = @import("microzig");
const assert = std.debug.assert;
pub const peripherals = microzig.chip.peripherals;
const gpio_v2 = microzig.chip.types.peripherals.gpio_v2;
const GPIO = gpio_v2.GPIO;
const MODER = gpio_v2.MODER;
const PUPDR = gpio_v2.PUPDR;
const OSPEEDR = gpio_v2.OSPEEDR;
const OT = gpio_v2.OT;
const AFIO = microzig.chip.peripherals.AFIO;
pub const Port = enum {
A,
B,
C,
D,
E,
F,
G,
};
pub const Mode = union(enum) {
input: InputMode,
output: OutputMode,
analog: AnalogMode,
alternate_function: AlternateFunction,
digital_io: Digital_IO,
};
pub const Digital_IO = struct {};
pub const InputMode = struct {
resistor: PUPDR,
};
pub const OutputMode = struct {
resistor: PUPDR,
o_type: OT,
o_speed: OSPEEDR = .LowSpeed,
};
pub const AnalogMode = struct {
resistor: PUPDR = .Floating,
};
pub const AF = enum(u4) {
AF0,
AF1,
AF2,
AF3,
AF4,
AF5,
AF6,
AF7,
AF8,
AF9,
AF10,
AF11,
AF12,
AF13,
AF14,
AF15,
};
pub const AlternateFunction = struct {
afr: AF,
resistor: PUPDR = .Floating,
o_type: OT = .PushPull,
o_speed: OSPEEDR = .HighSpeed,
};
// This is mostly internal to hal for writing configuration.
// Public implementation is provided in the pins.zig file.
pub const Pin = enum(usize) {
_,
pub inline fn write_pin_config(gpio: Pin, mode: Mode) void {
switch (mode) {
.input => |imode| {
gpio.set_moder(MODER.Input);
gpio.set_bias(imode.resistor);
},
.output => |omode| {
gpio.set_moder(MODER.Output);
gpio.set_output_type(omode.o_type);
gpio.set_bias(omode.resistor);
gpio.set_speed(omode.o_speed);
},
.analog => |amode| {
gpio.set_moder(MODER.Analog);
gpio.set_bias(amode.resistor);
},
.alternate_function => |afmode| {
gpio.set_moder(MODER.Alternate);
gpio.set_bias(afmode.resistor);
gpio.set_speed(afmode.o_speed);
gpio.set_output_type(afmode.o_type);
gpio.set_alternate_function(afmode.afr);
},
.digital_io => {
// Nothing for now
},
}
}
pub fn mask_2bit(gpio: Pin) u32 {
const pin: u5 = @intCast(@intFromEnum(gpio) % 16);
return @as(u32, 0b11) << (pin << 1);
}
pub fn mask(gpio: Pin) u32 {
const pin: u4 = @intCast(@intFromEnum(gpio) % 16);
return @as(u32, 1) << pin;
}
//NOTE: should invalid pins panic or just be ignored?
pub fn get_port(gpio: Pin) *volatile GPIO {
const port: usize = @divFloor(@intFromEnum(gpio), 16);
switch (port) {
0 => return if (@hasDecl(peripherals, "GPIOA")) peripherals.GPIOA else @panic("Invalid Pin"),
1 => return if (@hasDecl(peripherals, "GPIOB")) peripherals.GPIOB else @panic("Invalid Pin"),
2 => return if (@hasDecl(peripherals, "GPIOC")) peripherals.GPIOC else @panic("Invalid Pin"),
3 => return if (@hasDecl(peripherals, "GPIOD")) peripherals.GPIOD else @panic("Invalid Pin"),
4 => return if (@hasDecl(peripherals, "GPIOE")) peripherals.GPIOE else @panic("Invalid Pin"),
5 => return if (@hasDecl(peripherals, "GPIOF")) peripherals.GPIOF else @panic("Invalid Pin"),
6 => return if (@hasDecl(peripherals, "GPIOG")) peripherals.GPIOG else @panic("Invalid Pin"),
else => @panic("The STM32 only has ports 0..6 (A..G)"),
}
}
pub inline fn set_bias(gpio: Pin, bias: PUPDR) void {
const port = gpio.get_port();
const pin: u5 = @intCast(@intFromEnum(gpio) % 16);
const modMask: u32 = gpio.mask_2bit();
port.PUPDR.write_raw((port.PUPDR.raw & ~modMask) | @as(u32, @intFromEnum(bias)) << (pin << 1));
}
pub inline fn set_speed(gpio: Pin, speed: OSPEEDR) void {
const port = gpio.get_port();
const pin: u5 = @intCast(@intFromEnum(gpio) % 16);
const modMask: u32 = gpio.mask_2bit();
port.OSPEEDR.write_raw((port.OSPEEDR.raw & ~modMask) | @as(u32, @intFromEnum(speed)) << (pin << 1));
}
pub inline fn set_moder(gpio: Pin, moder: MODER) void {
const port = gpio.get_port();
const pin: u5 = @intCast(@intFromEnum(gpio) % 16);
const modMask: u32 = gpio.mask_2bit();
port.MODER.write_raw((port.MODER.raw & ~modMask) | @as(u32, @intFromEnum(moder)) << (pin << 1));
}
pub inline fn set_output_type(gpio: Pin, otype: OT) void {
const port = gpio.get_port();
const pin: u5 = @intCast(@intFromEnum(gpio) % 16);
port.OTYPER.write_raw((port.OTYPER.raw & ~gpio.mask()) | @as(u32, @intFromEnum(otype)) << pin);
}
pub inline fn set_alternate_function(gpio: Pin, afr: AF) void {
const port = gpio.get_port();
const pin: u5 = @intCast(@intFromEnum(gpio) % 16);
const afrMask: u32 = @as(u32, 0b1111) << ((pin % 8) << 2);
const register = if (pin > 7) &port.AFR[1] else &port.AFR[0];
register.write_raw((register.raw & ~afrMask) | @as(u32, @intFromEnum(afr)) << ((pin % 8) << 2));
}
pub fn from_port(port: Port, pin: u4) Pin {
const value: usize = pin + (@as(usize, 16) * @intFromEnum(port));
return @enumFromInt(value);
}
};