diff --git a/src/core/usb.zig b/src/core/usb.zig index 7c6d24b3..3d76c3ba 100644 --- a/src/core/usb.zig +++ b/src/core/usb.zig @@ -111,7 +111,7 @@ pub fn Usb(comptime f: anytype) type { if (reqty == Dir.Out and req != null and req.? == SetupRequest.SetAddress) { // The new address is in the bottom 8 bits of the setup // packet value field. Store it for use later. - S.new_address = @intCast(u8, setup.value & 0xff); + S.new_address = @as(u8, @intCast(setup.value & 0xff)); // The address will actually get set later, we have // to use address 0 to send a status response. f.usb_start_tx( @@ -224,7 +224,7 @@ pub fn Usb(comptime f: anytype) type { if (debug) std.log.info(" String", .{}); // String descriptor index is in bottom 8 bits of // `value`. - const i = @intCast(usize, setup.value & 0xff); + const i: usize = @intCast(setup.value & 0xff); const bytes = StringBlk: { if (i == 0) { // Special index 0 requests the language @@ -235,7 +235,7 @@ pub fn Usb(comptime f: anytype) type { const s = usb_config.?.descriptor_strings[i - 1]; const len = 2 + s.len; - S.tmp[0] = @intCast(u8, len); + S.tmp[0] = @intCast(len); S.tmp[1] = 0x03; @memcpy(S.tmp[2..len], s); @@ -356,7 +356,7 @@ pub fn Usb(comptime f: anytype) type { // SetAddress request, if there is one: if (S.new_address) |addr| { // Change our address: - f.set_address(@intCast(u7, addr)); + f.set_address(@intCast(addr)); } else { // Otherwise, we've just finished sending // something to the host. We expect an ensuing @@ -534,8 +534,8 @@ pub const EndpointDescriptor = packed struct { out[1] = @intFromEnum(self.descriptor_type); out[2] = self.endpoint_address; out[3] = self.attributes; - out[4] = @intCast(u8, self.max_packet_size & 0xff); - out[5] = @intCast(u8, (self.max_packet_size >> 8) & 0xff); + out[4] = @intCast(self.max_packet_size & 0xff); + out[5] = @intCast((self.max_packet_size >> 8) & 0xff); out[6] = self.interval; return out; } @@ -612,8 +612,8 @@ pub const ConfigurationDescriptor = packed struct { var out: [9]u8 = undefined; out[0] = 9; // length out[1] = @intFromEnum(self.descriptor_type); - out[2] = @intCast(u8, self.total_length & 0xff); - out[3] = @intCast(u8, (self.total_length >> 8) & 0xff); + out[2] = @intCast(self.total_length & 0xff); + out[3] = @intCast((self.total_length >> 8) & 0xff); out[4] = self.num_interfaces; out[5] = self.configuration_value; out[6] = self.configuration_s; @@ -660,18 +660,18 @@ pub const DeviceDescriptor = packed struct { var out: [18]u8 = undefined; out[0] = 18; // length out[1] = @intFromEnum(self.descriptor_type); - out[2] = @intCast(u8, self.bcd_usb & 0xff); - out[3] = @intCast(u8, (self.bcd_usb >> 8) & 0xff); + out[2] = @intCast(self.bcd_usb & 0xff); + out[3] = @intCast((self.bcd_usb >> 8) & 0xff); out[4] = self.device_class; out[5] = self.device_subclass; out[6] = self.device_protocol; out[7] = self.max_packet_size0; - out[8] = @intCast(u8, self.vendor & 0xff); - out[9] = @intCast(u8, (self.vendor >> 8) & 0xff); - out[10] = @intCast(u8, self.product & 0xff); - out[11] = @intCast(u8, (self.product >> 8) & 0xff); - out[12] = @intCast(u8, self.bcd_device & 0xff); - out[13] = @intCast(u8, (self.bcd_device >> 8) & 0xff); + out[8] = @intCast(self.vendor & 0xff); + out[9] = @intCast((self.vendor >> 8) & 0xff); + out[10] = @intCast(self.product & 0xff); + out[11] = @intCast((self.product >> 8) & 0xff); + out[12] = @intCast(self.bcd_device & 0xff); + out[13] = @intCast((self.bcd_device >> 8) & 0xff); out[14] = self.manufacturer_s; out[15] = self.product_s; out[16] = self.serial_s; @@ -707,8 +707,8 @@ pub const DeviceQualifierDescriptor = packed struct { var out: [10]u8 = undefined; out[0] = 10; // length out[1] = @intFromEnum(self.descriptor_type); - out[2] = @intCast(u8, self.bcd_usb & 0xff); - out[3] = @intCast(u8, (self.bcd_usb >> 8) & 0xff); + out[2] = @intCast(self.bcd_usb & 0xff); + out[3] = @intCast((self.bcd_usb >> 8) & 0xff); out[4] = self.device_class; out[5] = self.device_subclass; out[6] = self.device_protocol; diff --git a/src/core/usb/hid.zig b/src/core/usb/hid.zig index 758e1c18..e2c6d7f4 100644 --- a/src/core/usb/hid.zig +++ b/src/core/usb/hid.zig @@ -105,13 +105,13 @@ pub const HidDescriptor = packed struct { var out: [9]u8 = undefined; out[0] = 9; // length out[1] = @intFromEnum(self.descriptor_type); - out[2] = @intCast(u8, self.bcd_hid & 0xff); - out[3] = @intCast(u8, (self.bcd_hid >> 8) & 0xff); + out[2] = @intCast(self.bcd_hid & 0xff); + out[3] = @intCast((self.bcd_hid >> 8) & 0xff); out[4] = self.country_code; out[5] = self.num_descriptors; out[6] = @intFromEnum(self.report_type); - out[7] = @intCast(u8, self.report_length & 0xff); - out[8] = @intCast(u8, (self.report_length >> 8) & 0xff); + out[7] = @intCast(self.report_length & 0xff); + out[8] = @intCast((self.report_length >> 8) & 0xff); return out; } }; @@ -297,7 +297,7 @@ pub fn hid_report_item( ) [n + 1]u8 { var out: [n + 1]u8 = undefined; - out[0] = (@intCast(u8, tag) << 4) | (@intCast(u8, typ) << 2) | n; + out[0] = (@as(u8, @intCast(tag)) << 4) | (@as(u8, @intCast(typ)) << 2) | n; var i: usize = 0; while (i < n) : (i += 1) { @@ -438,19 +438,19 @@ test "create hid report item" { "\x22\x11".*, ); - try std.testing.expectEqual(@intCast(usize, 3), r.len); - try std.testing.expectEqual(@intCast(u8, 50), r[0]); - try std.testing.expectEqual(@intCast(u8, 0x22), r[1]); - try std.testing.expectEqual(@intCast(u8, 0x11), r[2]); + try std.testing.expectEqual(@as(usize, @intCast(3)), r.len); + try std.testing.expectEqual(@as(u8, @intCast(50)), r[0]); + try std.testing.expectEqual(@as(u8, @intCast(0x22)), r[1]); + try std.testing.expectEqual(@as(u8, @intCast(0x11)), r[2]); } test "create hid fido usage page" { const f = hid_usage_page(2, UsageTable.fido); - try std.testing.expectEqual(@intCast(usize, 3), f.len); - try std.testing.expectEqual(@intCast(u8, 6), f[0]); - try std.testing.expectEqual(@intCast(u8, 0xd0), f[1]); - try std.testing.expectEqual(@intCast(u8, 0xf1), f[2]); + try std.testing.expectEqual(@as(usize, @intCast(3)), f.len); + try std.testing.expectEqual(@as(u8, @intCast(6)), f[0]); + try std.testing.expectEqual(@as(u8, @intCast(0xd0)), f[1]); + try std.testing.expectEqual(@as(u8, @intCast(0xf1)), f[2]); } test "report descriptor fido" { diff --git a/src/mmio.zig b/src/mmio.zig index 2d6b7603..83c7699b 100644 --- a/src/mmio.zig +++ b/src/mmio.zig @@ -22,14 +22,14 @@ pub fn Mmio(comptime PackedT: type) type { pub const underlying_type = PackedT; pub inline fn read(addr: *volatile Self) PackedT { - return @bitCast(PackedT, addr.raw); + return @bitCast(addr.raw); } pub inline fn write(addr: *volatile Self, val: PackedT) void { comptime { assert(@bitSizeOf(PackedT) == @bitSizeOf(IntT)); } - addr.write_raw(@bitCast(IntT, val)); + addr.write_raw(@bitCast(val)); } pub fn write_raw(addr: *volatile Self, val: IntT) void { diff --git a/src/modules/cpus/cortex-m.zig b/src/modules/cpus/cortex-m.zig index a9ad3fa6..b37e12bf 100644 --- a/src/modules/cpus/cortex-m.zig +++ b/src/modules/cpus/cortex-m.zig @@ -5,7 +5,7 @@ const root = @import("root"); pub const regs = struct { // Interrupt Control and State Register - pub const ICSR = @ptrFromInt(*volatile mmio.Mmio(packed struct { + pub const ICSR: *volatile mmio.Mmio(packed struct { VECTACTIVE: u9, reserved0: u2, RETTOBASE: u1, @@ -20,7 +20,7 @@ pub const regs = struct { PENDSVSET: u1, reserved3: u2, NMIPENDSET: u1, - }), 0xE000ED04); + }) = @ptrFromInt(0xE000ED04); }; pub fn executing_isr() bool { @@ -80,8 +80,8 @@ pub const startup_logic = struct { // fill .bss with zeroes { - const bss_start = @ptrCast([*]u8, µzig_bss_start); - const bss_end = @ptrCast([*]u8, µzig_bss_end); + const bss_start: [*]u8 = @ptrCast(µzig_bss_start); + const bss_end: [*]u8 = @ptrCast(µzig_bss_end); const bss_len = @intFromPtr(bss_end) - @intFromPtr(bss_start); @memset(bss_start[0..bss_len], 0); @@ -89,10 +89,10 @@ pub const startup_logic = struct { // load .data from flash { - const data_start = @ptrCast([*]u8, µzig_data_start); - const data_end = @ptrCast([*]u8, µzig_data_end); + const data_start: [*]u8 = @ptrCast(µzig_data_start); + const data_end: [*]u8 = @ptrCast(µzig_data_end); const data_len = @intFromPtr(data_end) - @intFromPtr(data_start); - const data_src = @ptrCast([*]const u8, µzig_data_load_start); + const data_src: [*]const u8 = @ptrCast(µzig_data_load_start); @memcpy(data_start[0..data_len], data_src[0..data_len]); } @@ -175,3 +175,116 @@ fn create_interrupt_vector( }, }; } + +pub const peripherals = struct { + /// System Tick Timer + pub const SysTick = @as(*volatile types.peripherals.SysTick, @ptrFromInt(0xe000e010)); + + /// System Control Space + pub const NVIC = @compileError("TODO"); // @ptrFromInt(*volatile types.peripherals.NVIC, 0xe000e100); + + /// System Control Block + pub const SCB = @as(*volatile types.peripherals.SCB, @ptrFromInt(0xe000ed00)); +}; + +pub const types = struct { + pub const peripherals = struct { + /// System Tick Timer + pub const SysTick = extern struct { + /// SysTick Control and Status Register + CTRL: mmio.Mmio(packed struct(u32) { + ENABLE: u1, + TICKINT: u1, + CLKSOURCE: u1, + reserved16: u13, + COUNTFLAG: u1, + padding: u15, + }), + /// SysTick Reload Value Register + LOAD: mmio.Mmio(packed struct(u32) { + RELOAD: u24, + padding: u8, + }), + /// SysTick Current Value Register + VAL: mmio.Mmio(packed struct(u32) { + CURRENT: u24, + padding: u8, + }), + /// SysTick Calibration Register + CALIB: mmio.Mmio(packed struct(u32) { + TENMS: u24, + reserved30: u6, + SKEW: u1, + NOREF: u1, + }), + }; + + /// System Control Block + pub const SCB = extern struct { + CPUID: mmio.Mmio(packed struct(u32) { + REVISION: u4, + PARTNO: u12, + ARCHITECTURE: u4, + VARIANT: u4, + IMPLEMENTER: u8, + }), + /// Interrupt Control and State Register + ICSR: mmio.Mmio(packed struct(u32) { + VECTACTIVE: u9, + reserved12: u3, + VECTPENDING: u9, + reserved22: u1, + ISRPENDING: u1, + ISRPREEMPT: u1, + reserved25: u1, + PENDSTCLR: u1, + PENDSTSET: u1, + PENDSVCLR: u1, + PENDSVSET: u1, + reserved31: u2, + NMIPENDSET: u1, + }), + /// Vector Table Offset Register + VTOR: mmio.Mmio(packed struct(u32) { + reserved8: u8, + TBLOFF: u24, + }), + /// Application Interrupt and Reset Control Register + AIRCR: mmio.Mmio(packed struct(u32) { + reserved1: u1, + VECTCLRACTIVE: u1, + SYSRESETREQ: u1, + reserved15: u12, + ENDIANESS: u1, + VECTKEY: u16, + }), + /// System Control Register + SCR: mmio.Mmio(packed struct(u32) { + reserved1: u1, + SLEEPONEXIT: u1, + SLEEPDEEP: u1, + reserved4: u1, + SEVONPEND: u1, + padding: u27, + }), + /// Configuration Control Register + CCR: mmio.Mmio(packed struct(u32) { + reserved3: u3, + UNALIGN_TRP: u1, + reserved9: u5, + STKALIGN: u1, + padding: u22, + }), + reserved28: [4]u8, + /// System Handlers Priority Registers. [0] is RESERVED + SHP: u32, + reserved36: [4]u8, + /// System Handler Control and State Register + SHCSR: mmio.Mmio(packed struct(u32) { + reserved15: u15, + SVCALLPENDED: u1, + padding: u16, + }), + }; + }; +}; diff --git a/src/modules/cpus/riscv32.zig b/src/modules/cpus/riscv32.zig index e0059047..fea883b7 100644 --- a/src/modules/cpus/riscv32.zig +++ b/src/modules/cpus/riscv32.zig @@ -57,8 +57,8 @@ pub const startup_logic = struct { // fill .bss with zeroes { - const bss_start = @ptrCast([*]u8, µzig_bss_start); - const bss_end = @ptrCast([*]u8, µzig_bss_end); + const bss_start: [*]u8 = @ptrCast(µzig_bss_start); + const bss_end: [*]u8 = @ptrCast(µzig_bss_end); const bss_len = @intFromPtr(bss_end) - @intFromPtr(bss_start); @memset(bss_start[0..bss_len], 0); @@ -66,10 +66,10 @@ pub const startup_logic = struct { // load .data from flash { - const data_start = @ptrCast([*]u8, µzig_data_start); - const data_end = @ptrCast([*]u8, µzig_data_end); + const data_start: [*]u8 = @ptrCast(µzig_data_start); + const data_end: [*]u8 = @ptrCast(µzig_data_end); const data_len = @intFromPtr(data_end) - @intFromPtr(data_start); - const data_src = @ptrCast([*]const u8, µzig_data_load_start); + const data_src: [*]const u8 = @ptrCast(µzig_data_load_start); @memcpy(data_start[0..data_len], data_src[0..data_len]); } diff --git a/src/start.zig b/src/start.zig index 69cf81a4..59ec4cd4 100644 --- a/src/start.zig +++ b/src/start.zig @@ -139,8 +139,8 @@ pub fn initialize_system_memories() void { // fill .bss with zeroes { - const bss_start = @ptrCast([*]u8, §ions.microzig_bss_start); - const bss_end = @ptrCast([*]u8, §ions.microzig_bss_end); + const bss_start: [*]u8 = @ptrCast(§ions.microzig_bss_start); + const bss_end: [*]u8 = @ptrCast(§ions.microzig_bss_end); const bss_len = @intFromPtr(bss_end) - @intFromPtr(bss_start); @memset(bss_start[0..bss_len], 0); @@ -148,10 +148,10 @@ pub fn initialize_system_memories() void { // load .data from flash { - const data_start = @ptrCast([*]u8, §ions.microzig_data_start); - const data_end = @ptrCast([*]u8, §ions.microzig_data_end); + const data_start: [*]u8 = @ptrCast(§ions.microzig_data_start); + const data_end: [*]u8 = @ptrCast(§ions.microzig_data_end); const data_len = @intFromPtr(data_end) - @intFromPtr(data_start); - const data_src = @ptrCast([*]const u8, §ions.microzig_data_load_start); + const data_src: [*]const u8 = @ptrCast(§ions.microzig_data_load_start); @memcpy(data_start[0..data_len], data_src[0..data_len]); }