-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Based on some initial HAL code that was inspired by the current STM32F103 code, but with some variations that seemed to give a slightly simpler result. (Also made the old unused HAL code in STM32F303.zig compile, and marked it as 'to be moved to separate files'.)
- Loading branch information
Showing
4 changed files
with
270 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const std = @import("std"); | ||
|
||
const microzig = @import("microzig"); | ||
const peripherals = microzig.chip.peripherals; | ||
|
||
pub const Mode = union(enum) { | ||
input: InputMode, | ||
output: OutputMode, | ||
}; | ||
|
||
pub const InputMode = enum(u2) { | ||
analog, | ||
floating, | ||
pull_up, | ||
pull_down, | ||
}; | ||
|
||
pub const OutputMode = enum(u2) { | ||
push_pull, | ||
open_drain, | ||
}; | ||
|
||
// TODO: Add the following | ||
// pub const Speed = enum(u2) { | ||
// low, | ||
// medium, | ||
// high, | ||
// }; | ||
|
||
pub const Pin = struct { | ||
port_id: []const u8, | ||
number_str: []const u8, | ||
|
||
pub fn init(port_id: []const u8, number_str: []const u8) Pin { | ||
return Pin{ | ||
.port_id = port_id, | ||
.number_str = number_str, | ||
}; | ||
} | ||
|
||
pub fn configure(comptime self: @This()) void { | ||
const port_peripheral = @field(peripherals, "GPIO" ++ self.port_id); | ||
// TODO: Support input | ||
port_peripheral.MODER.modify_one("MODER[" ++ self.number_str ++ "]", .Output); | ||
// TODO: Support different modes, for input and for output | ||
port_peripheral.OTYPER.modify_one("OT[" ++ self.number_str ++ "]", .PushPull); | ||
// TODO: Support different speeds | ||
port_peripheral.OSPEEDR.modify_one("OSPEEDR[" ++ self.number_str ++ "]", .LowSpeed); | ||
// TODO: Support pull-up / pull-down | ||
port_peripheral.PUPDR.modify_one("PUPDR[" ++ self.number_str ++ "]", .Floating); | ||
} | ||
|
||
pub fn toggle(comptime self: @This()) void { | ||
@field(peripherals, "GPIO" ++ self.port_id).ODR.toggle_one("ODR[" ++ self.number_str ++ "]", .High); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
const std = @import("std"); | ||
const comptimePrint = std.fmt.comptimePrint; | ||
const StructField = std.builtin.Type.StructField; | ||
|
||
const microzig = @import("microzig"); | ||
const peripherals = microzig.chip.peripherals; | ||
|
||
const gpio = @import("gpio.zig"); | ||
|
||
/// For now this is always a GPIO pin configuration | ||
const PinConfiguration = struct { | ||
name: ?[:0]const u8 = null, | ||
mode: ?gpio.Mode = null, | ||
}; | ||
|
||
fn GPIO(comptime port: []const u8, comptime num: []const u8, comptime mode: gpio.Mode) type { | ||
if (mode == .input) @compileError("TODO: implement GPIO input mode"); | ||
return switch (mode) { | ||
.input => struct { | ||
const pin = gpio.Pin.init(port, num); | ||
|
||
pub inline fn read(_: @This()) u1 { | ||
return pin.read(); | ||
} | ||
}, | ||
.output => packed struct { | ||
const pin = gpio.Pin.init(port, num); | ||
|
||
pub inline fn put(_: @This(), value: u1) void { | ||
pin.put(value); | ||
} | ||
|
||
pub inline fn toggle(_: @This()) void { | ||
pin.toggle(); | ||
} | ||
|
||
fn configure(_: @This(), pin_config: PinConfiguration) void { | ||
_ = pin_config; // Later: use for GPIO pin speed etc. | ||
pin.configure(); | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
/// This is a helper empty struct with comptime constants for parsing an STM32 pin name. | ||
/// Example: PinDescription("PE9").gpio_port_id = "E" | ||
/// Example: PinDescription("PA12").gpio_port_number_str = "12" | ||
fn PinDescription(comptime spec: []const u8) type { | ||
const invalid_format_msg = "The given pin '" ++ spec ++ "' has an invalid format. Pins must follow the format \"P{Port}{Pin}\" scheme."; | ||
|
||
if (spec[0] != 'P') | ||
@compileError(invalid_format_msg); | ||
if (spec[1] < 'A' or spec[1] > 'F') | ||
@compileError(invalid_format_msg); | ||
|
||
const gpio_pin_number_int: comptime_int = std.fmt.parseInt(u4, spec[2..], 10) catch @compileError(invalid_format_msg); | ||
return struct { | ||
/// 'A'...'F' | ||
const gpio_port_id = spec[1..2]; | ||
const gpio_pin_number_str = std.fmt.comptimePrint("{d}", .{gpio_pin_number_int}); | ||
}; | ||
} | ||
|
||
/// Based on the fields in `config`, returns a struct { | ||
/// PE11: GPIO(...), | ||
/// PF7: GPIO(...), | ||
/// } | ||
/// Later: Also support non-GPIO pins? | ||
pub fn Pins(comptime config: GlobalConfiguration) type { | ||
comptime { | ||
var fields: []const StructField = &.{}; | ||
for (@typeInfo(GlobalConfiguration).Struct.fields) |port_field| { | ||
if (@field(config, port_field.name)) |port_config| { | ||
for (@typeInfo(PortConfiguration()).Struct.fields) |field| { | ||
if (@field(port_config, field.name)) |pin_config| { | ||
const D = PinDescription(field.name); | ||
fields = fields ++ &[_]StructField{.{ | ||
.is_comptime = false, | ||
.name = pin_config.name orelse field.name, | ||
.type = GPIO(D.gpio_port_id, D.gpio_pin_number_str, pin_config.mode orelse .{ .input = .floating }), | ||
.default_value = null, | ||
.alignment = @alignOf(field.type), | ||
}}; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return @Type(.{ | ||
.Struct = .{ | ||
.layout = .auto, | ||
.is_tuple = false, | ||
.fields = fields, | ||
.decls = &.{}, | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
/// Returns the struct { | ||
/// PA0: ?PinConfiguration = null, | ||
/// ... | ||
/// PF15: ?PinConfiguration = null, | ||
/// } | ||
fn PortConfiguration() type { | ||
var fields: []const StructField = &.{}; | ||
for ("ABCDEF") |gpio_port_id| { | ||
for (0..16) |gpio_pin_number_int| { | ||
fields = fields ++ &[_]StructField{.{ | ||
.is_comptime = false, | ||
.name = std.fmt.comptimePrint("P{c}{d}", .{ gpio_port_id, gpio_pin_number_int }), | ||
.type = ?PinConfiguration, | ||
.default_value = &@as(?PinConfiguration, null), | ||
.alignment = @alignOf(?PinConfiguration), | ||
}}; | ||
} | ||
} | ||
|
||
return @Type(.{ | ||
.Struct = .{ | ||
.layout = .auto, | ||
.is_tuple = false, | ||
.fields = fields, | ||
.decls = &.{}, | ||
}, | ||
}); | ||
} | ||
pub const GlobalConfiguration = struct { | ||
GPIOA: ?PortConfiguration() = null, | ||
GPIOB: ?PortConfiguration() = null, | ||
GPIOC: ?PortConfiguration() = null, | ||
GPIOD: ?PortConfiguration() = null, | ||
GPIOE: ?PortConfiguration() = null, | ||
GPIOF: ?PortConfiguration() = null, | ||
|
||
pub fn apply(comptime config: @This()) Pins(config) { | ||
const pins: Pins(config) = undefined; // Later: something seems incomplete here... | ||
|
||
inline for (@typeInfo(@This()).Struct.fields) |port_field| { | ||
const gpio_port_name = port_field.name; | ||
if (@field(config, gpio_port_name)) |port_config| { | ||
peripherals.RCC.AHBENR.modify_one(gpio_port_name ++ "EN", 1); | ||
|
||
inline for (@typeInfo(PortConfiguration()).Struct.fields) |pin_field| { | ||
if (@field(port_config, pin_field.name)) |pin_config| { | ||
@field(pins, pin_field.name).configure(pin_config); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return pins; | ||
} | ||
}; |