Skip to content

Commit

Permalink
update to master
Browse files Browse the repository at this point in the history
  • Loading branch information
mattnite committed Jan 20, 2025
1 parent c3e71e0 commit e08f948
Show file tree
Hide file tree
Showing 44 changed files with 287 additions and 300 deletions.
2 changes: 1 addition & 1 deletion build-internals/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const LazyPath = Build.LazyPath;
const Module = Build.Module;

const regz = @import("regz");
const Patch = regz.patch.Patch;
pub const Patch = regz.patch.Patch;
const uf2 = @import("uf2");
const FamilyId = uf2.FamilyId;

Expand Down
16 changes: 8 additions & 8 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ pub const PortSelect = blk: {
fields = fields ++ [_]std.builtin.Type.StructField{.{
.name = port.name,
.type = bool,
.default_value = @as(*const anyopaque, @ptrCast(&false)),
.default_value_ptr = @as(*const anyopaque, @ptrCast(&false)),
.is_comptime = false,
.alignment = @alignOf(bool),
}};
}
break :blk @Type(.{
.Struct = .{
.@"struct" = .{
.layout = .auto,
.fields = fields,
.decls = &.{},
Expand All @@ -140,13 +140,13 @@ pub const PortCache = blk: {
fields = fields ++ [_]std.builtin.Type.StructField{.{
.name = port.name,
.type = typ,
.default_value = @as(*const anyopaque, @ptrCast(&@as(typ, null))),
.default_value_ptr = @as(*const anyopaque, @ptrCast(&@as(typ, null))),
.is_comptime = false,
.alignment = @alignOf(typ),
}};
}
break :blk @Type(.{
.Struct = .{
.@"struct" = .{
.layout = .auto,
.fields = fields,
.decls = &.{},
Expand Down Expand Up @@ -194,15 +194,15 @@ pub fn MicroBuild(port_select: PortSelect) type {
fields = fields ++ [_]std.builtin.Type.StructField{.{
.name = port.name,
.type = typ,
.default_value = null,
.default_value_ptr = null,
.is_comptime = false,
.alignment = @alignOf(typ),
}};
}
}

break :blk @Type(.{
.Struct = .{
.@"struct" = .{
.layout = .auto,
.fields = fields,
.decls = &.{},
Expand Down Expand Up @@ -711,7 +711,7 @@ pub inline fn custom_lazy_import(
const deps = build_runner.dependencies;
const pkg_hash = custom_find_import_pkg_hash_or_fatal(dep_name);

inline for (@typeInfo(deps.packages).Struct.decls) |decl| {
inline for (@typeInfo(deps.packages).@"struct".decls) |decl| {
if (comptime std.mem.eql(u8, decl.name, pkg_hash)) {
const pkg = @field(deps.packages, decl.name);
const available = !@hasDecl(pkg, "available") or pkg.available;
Expand All @@ -733,7 +733,7 @@ inline fn custom_find_import_pkg_hash_or_fatal(comptime dep_name: []const u8) []
const build_runner = @import("root");
const deps = build_runner.dependencies;

const pkg_deps = comptime for (@typeInfo(deps.packages).Struct.decls) |decl| {
const pkg_deps = comptime for (@typeInfo(deps.packages).@"struct".decls) |decl| {
const pkg_hash = decl.name;
const pkg = @field(deps.packages, pkg_hash);
if (@hasDecl(pkg, "build_zig") and pkg.build_zig == @This()) break pkg.deps;
Expand Down
83 changes: 34 additions & 49 deletions core/src/core/usb.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pub const vendor = @import("usb/vendor.zig");
pub const utils = @import("usb/utils.zig");
pub const templates = @import("usb/templates.zig");


const DescType = types.DescType;
const Dir = types.Dir;
const Endpoint = types.Endpoint;
Expand Down Expand Up @@ -55,7 +54,7 @@ pub fn Usb(comptime f: anytype) type {
var clk_init: bool = false;
var itf_to_drv: [f.cfg_max_interfaces_count]u8 = .{0} ** f.cfg_max_interfaces_count;
var ep_to_drv: [f.cfg_max_endpoints_count][2]u8 = .{.{0} ** 2} ** f.cfg_max_endpoints_count;
pub const max_packet_size = if(f.high_speed) 512 else 64;
pub const max_packet_size = if (f.high_speed) 512 else 64;
const drvid_invalid = 0xff;

/// The callbacks passed provided by the caller
Expand All @@ -80,7 +79,7 @@ pub fn Usb(comptime f: anytype) type {
// descriptors for transmission.
var tmp: [128]u8 = .{0} ** 128;
// Keeps track of sent data from tmp buffer
var buffer_reader = BufferReader { .buffer = &.{} };
var buffer_reader = BufferReader{ .buffer = &.{} };
// Last setup packet request
var setup_packet: types.SetupPacket = undefined;
// Class driver associated with last setup request if any
Expand All @@ -89,10 +88,9 @@ pub fn Usb(comptime f: anytype) type {

// Command endpoint utilities
const CmdEndpoint = struct {

/// Command response utility function that can split long data in multiple packets
fn send_cmd_response(data: []const u8, expected_max_length: u16) void {
S.buffer_reader = BufferReader { .buffer = data[0..@min(data.len, expected_max_length)] };
S.buffer_reader = BufferReader{ .buffer = data[0..@min(data.len, expected_max_length)] };
const data_chunk = S.buffer_reader.try_peek(64);

if (data_chunk.len > 0) {
Expand Down Expand Up @@ -127,13 +125,7 @@ pub fn Usb(comptime f: anytype) type {
}

fn device() types.UsbDevice {
return .{
.fn_ready = device_ready,
.fn_control_transfer = device_control_transfer,
.fn_control_ack = device_control_ack,
.fn_endpoint_open = device_endpoint_open,
.fn_endpoint_transfer = device_endpoint_transfer
};
return .{ .fn_ready = device_ready, .fn_control_transfer = device_control_transfer, .fn_control_ack = device_control_ack, .fn_endpoint_open = device_endpoint_open, .fn_endpoint_transfer = device_endpoint_transfer };
}

fn device_ready() bool {
Expand Down Expand Up @@ -180,7 +172,7 @@ pub fn Usb(comptime f: anytype) type {

fn configuration_reset() void {
@memset(&itf_to_drv, drvid_invalid);
@memset(&ep_to_drv, .{drvid_invalid, drvid_invalid});
@memset(&ep_to_drv, .{ drvid_invalid, drvid_invalid });
}

/// Usb task function meant to be executed in regular intervals after
Expand All @@ -194,7 +186,6 @@ pub fn Usb(comptime f: anytype) type {

// Device Specific Request
const DeviceRequestProcessor = struct {

fn process_setup_request(setup: *const types.SetupPacket) !void {
switch (setup.request_type.type) {
.Class => {
Expand All @@ -221,7 +212,7 @@ pub fn Usb(comptime f: anytype) type {
if (cfg_num > 0) {
try process_set_config(cfg_num - 1);
// TODO: call mount callback if any
} else {
} else {
// TODO: call umount callback if any
}
}
Expand All @@ -234,10 +225,10 @@ pub fn Usb(comptime f: anytype) type {
if (descriptor_type) |dt| {
try process_get_descriptor(setup, dt);
}
}
},
}
},
else => {}
else => {},
}
}

Expand All @@ -246,15 +237,15 @@ pub fn Usb(comptime f: anytype) type {
.Device => {
if (S.debug_mode) std.log.info(" Device", .{});

var bw = BufferWriter { .buffer = &S.tmp };
var bw = BufferWriter{ .buffer = &S.tmp };
try bw.write(&usb_config.?.device_descriptor.serialize());

CmdEndpoint.send_cmd_response(bw.get_written_slice(), setup.length);
},
.Config => {
if (S.debug_mode) std.log.info(" Config", .{});
var bw = BufferWriter { .buffer = &S.tmp };

var bw = BufferWriter{ .buffer = &S.tmp };
try bw.write(usb_config.?.config_descriptor);

CmdEndpoint.send_cmd_response(bw.get_written_slice(), setup.length);
Expand All @@ -274,15 +265,15 @@ pub fn Usb(comptime f: anytype) type {
const s = usb_config.?.descriptor_strings[i - 1];
const len = 2 + s.len;

var wb = BufferWriter { .buffer = &S.tmp };
var wb = BufferWriter{ .buffer = &S.tmp };
try wb.write_int(u8, @intCast(len));
try wb.write_int(u8, 0x03);
try wb.write(s);

break :StringBlk wb.get_written_slice();
}
};

CmdEndpoint.send_cmd_response(bytes, setup.length);
},
.Interface => {
Expand All @@ -304,12 +295,12 @@ pub fn Usb(comptime f: anytype) type {
.num_configurations = usb_config.?.device_descriptor.num_configurations,
};

var bw = BufferWriter { .buffer = &S.tmp };
var bw = BufferWriter{ .buffer = &S.tmp };
try bw.write(&dqd.serialize());

CmdEndpoint.send_cmd_response(bw.get_written_slice(), setup.length);
},
else => {}
else => {},
}
}

Expand Down Expand Up @@ -359,7 +350,9 @@ pub fn Usb(comptime f: anytype) type {

fn bind_endpoints_to_driver(drv_bos_cfg: []const u8, drv_idx: u8) void {
var curr_bos_cfg = drv_bos_cfg;
while (curr_bos_cfg.len > 0) : ({curr_bos_cfg = BosConfig.get_desc_next(curr_bos_cfg);}) {
while (curr_bos_cfg.len > 0) : ({
curr_bos_cfg = BosConfig.get_desc_next(curr_bos_cfg);
}) {
if (BosConfig.try_get_desc_as(types.EndpointDescriptor, curr_bos_cfg)) |desc_ep| {
const ep_addr = desc_ep.endpoint_address;
ep_to_drv[Endpoint.num_from_address(ep_addr)][Endpoint.dir_from_address(ep_addr).as_number()] = drv_idx;
Expand All @@ -384,8 +377,7 @@ pub fn Usb(comptime f: anytype) type {

// Endpoint Specific Request
const EndpointRequestProcessor = struct {
fn process_setup_request(_: *const types.SetupPacket) !void {
}
fn process_setup_request(_: *const types.SetupPacket) !void {}
};

// Check which interrupt flags are set.
Expand All @@ -407,9 +399,8 @@ pub fn Usb(comptime f: anytype) type {
.Device => try DeviceRequestProcessor.process_setup_request(&setup),
.Interface => try InterfaceRequestProcessor.process_setup_request(&setup),
.Endpoint => try EndpointRequestProcessor.process_setup_request(&setup),
else => {}
else => {},
}

}

// Events on one or more buffers? (In practice, always one.)
Expand All @@ -426,24 +417,24 @@ pub fn Usb(comptime f: anytype) type {
switch (epb.endpoint_address) {
Endpoint.EP0_IN_ADDR => {
if (debug) std.log.info(" EP0_IN_ADDR", .{});

const buffer_reader = &S.buffer_reader;

// We use this opportunity to finish the delayed
// SetAddress request, if there is one:
if (S.new_address) |addr| {
// Change our address:
f.set_address(@intCast(addr));
}

if (epb.buffer.len > 0 and buffer_reader.get_remaining_bytes_count() > 0) {
_ = buffer_reader.try_advance(epb.buffer.len);
const next_data_chunk = buffer_reader.try_peek(64);
if (next_data_chunk.len > 0) {
f.usb_start_tx(
Endpoint.EP0_IN_ADDR,
next_data_chunk,
);
Endpoint.EP0_IN_ADDR,
next_data_chunk,
);
} else {
f.usb_start_rx(
Endpoint.EP0_OUT_ADDR,
Expand Down Expand Up @@ -497,7 +488,7 @@ pub fn Usb(comptime f: anytype) type {
S.new_address = null;
S.configured = false;
S.started = false;
S.buffer_reader = BufferReader { .buffer = &.{} };
S.buffer_reader = BufferReader{ .buffer = &.{} };
}

// If we have been configured but haven't reached this point yet, set up
Expand All @@ -513,13 +504,7 @@ pub fn Usb(comptime f: anytype) type {
// Driver support stuctures
// +++++++++++++++++++++++++++++++++++++++++++++++++

pub const DeviceConfiguration = struct {
device_descriptor: *const types.DeviceDescriptor,
config_descriptor: []const u8,
lang_descriptor: []const u8,
descriptor_strings: []const []const u8,
drivers: []types.UsbClassDriver
};
pub const DeviceConfiguration = struct { device_descriptor: *const types.DeviceDescriptor, config_descriptor: []const u8, lang_descriptor: []const u8, descriptor_strings: []const []const u8, drivers: []types.UsbClassDriver };

/// USB interrupt status
///
Expand Down Expand Up @@ -571,7 +556,7 @@ const BufferWriter = struct {
pos: usize = 0,
endian: std.builtin.Endian = builtin.cpu.arch.endian(),

pub const Error = error{ EndOfBuffer };
pub const Error = error{EndOfBuffer};

/// Moves forward write cursor by the provided number of bytes.
pub fn advance(self: *@This(), bytes: usize) Error!void {
Expand All @@ -583,12 +568,12 @@ const BufferWriter = struct {
pub fn write(self: *@This(), data: []const u8) Error!void {
try self.bound_check(data.len);
defer self.advance_unsafe(data.len);
@memcpy(self.buffer[self.pos..self.pos + data.len], data);
@memcpy(self.buffer[self.pos .. self.pos + data.len], data);
}

/// Writes an int with respect to the buffer's endianness and moves write cursor forward by int size.
pub fn write_int(self: *@This(), comptime T: type, value: T) Error!void {
const size = @divExact(@typeInfo(T).Int.bits, 8);
const size = @divExact(@typeInfo(T).int.bits, 8);
try self.bound_check(size);
defer self.advance_unsafe(size);
std.mem.writeInt(T, self.buffer[self.pos..][0..size], value, self.endian);
Expand All @@ -597,7 +582,7 @@ const BufferWriter = struct {
/// Writes an int with respect to the buffer's endianness but skip bound check.
/// Useful in cases where the bound can be checked once for batch of ints.
pub fn write_int_unsafe(self: *@This(), comptime T: type, value: T) void {
const size = @divExact(@typeInfo(T).Int.bits, 8);
const size = @divExact(@typeInfo(T).int.bits, 8);
defer self.advance_unsafe(size);
std.mem.writeInt(T, self.buffer[self.pos..][0..size], value, self.endian);
}
Expand Down Expand Up @@ -634,13 +619,13 @@ const BufferReader = struct {
pub fn try_read(self: *@This(), bytes: usize) []const u8 {
const size = @min(bytes, self.buffer.len - self.pos);
defer self.advance_unsafe(size);
return self.buffer[self.pos..self.pos + size];
return self.buffer[self.pos .. self.pos + size];
}

/// Attempts to read the given amount of bytes (or less if close to buffer end) without advancing the read cursor.
pub fn try_peek(self: *@This(), bytes: usize) []const u8 {
const size = @min(bytes, self.buffer.len - self.pos);
return self.buffer[self.pos..self.pos + size];
return self.buffer[self.pos .. self.pos + size];
}

/// Returns the number of bytes remaining from the current read cursor position to the end of the underlying buffer.
Expand Down
6 changes: 3 additions & 3 deletions core/src/cpus/avr5.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub const vector_table_asm = blk: {
const asm_str: []const u8 = "jmp microzig_start\n";

//const has_interrupts = @hasDecl(root, "microzig_options");
//for (@typeInfo(root.VectorTableOptions).Struct.fields) |field| {
//for (@typeInfo(root.VectorTableOptions).@"struct".fields) |field| {
// const new_insn = if (has_interrupts) overload: {
// const interrupts = root.microzig_options.interrupts;
// if (@hasDecl(interrupts, field.name)) {
Expand Down Expand Up @@ -62,7 +62,7 @@ export fn abort() noreturn {

pub fn export_startup_logic() void {
_ = startup_logic;
@export(vector_table, .{
@export(&vector_table, .{
.name = "_start",
});
}
Expand All @@ -87,7 +87,7 @@ fn make_isr_handler(comptime name: []const u8, comptime func: anytype) type {

comptime {
const options = .{ .name = exported_name, .linkage = .Strong };
@export(isr_vector, options);
@export(&isr_vector, options);
}
};
}
Expand Down
Loading

0 comments on commit e08f948

Please sign in to comment.