Skip to content

Commit

Permalink
Update to master and add system registers for cortex-m (#134)
Browse files Browse the repository at this point in the history
* Update to zig 0.11.0-dev.3826+7a197f124

* Add cortex-m system registers
  • Loading branch information
vesim987 authored Jun 27, 2023
1 parent a49fad9 commit 9392fe0
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 50 deletions.
36 changes: 18 additions & 18 deletions src/core/usb.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
26 changes: 13 additions & 13 deletions src/core/usb/hid.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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" {
Expand Down
4 changes: 2 additions & 2 deletions src/mmio.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
127 changes: 120 additions & 7 deletions src/modules/cpus/cortex-m.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -20,7 +20,7 @@ pub const regs = struct {
PENDSVSET: u1,
reserved3: u2,
NMIPENDSET: u1,
}), 0xE000ED04);
}) = @ptrFromInt(0xE000ED04);
};

pub fn executing_isr() bool {
Expand Down Expand Up @@ -80,19 +80,19 @@ pub const startup_logic = struct {

// fill .bss with zeroes
{
const bss_start = @ptrCast([*]u8, &microzig_bss_start);
const bss_end = @ptrCast([*]u8, &microzig_bss_end);
const bss_start: [*]u8 = @ptrCast(&microzig_bss_start);
const bss_end: [*]u8 = @ptrCast(&microzig_bss_end);
const bss_len = @intFromPtr(bss_end) - @intFromPtr(bss_start);

@memset(bss_start[0..bss_len], 0);
}

// load .data from flash
{
const data_start = @ptrCast([*]u8, &microzig_data_start);
const data_end = @ptrCast([*]u8, &microzig_data_end);
const data_start: [*]u8 = @ptrCast(&microzig_data_start);
const data_end: [*]u8 = @ptrCast(&microzig_data_end);
const data_len = @intFromPtr(data_end) - @intFromPtr(data_start);
const data_src = @ptrCast([*]const u8, &microzig_data_load_start);
const data_src: [*]const u8 = @ptrCast(&microzig_data_load_start);

@memcpy(data_start[0..data_len], data_src[0..data_len]);
}
Expand Down Expand Up @@ -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,
}),
};
};
};
10 changes: 5 additions & 5 deletions src/modules/cpus/riscv32.zig
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,19 @@ pub const startup_logic = struct {

// fill .bss with zeroes
{
const bss_start = @ptrCast([*]u8, &microzig_bss_start);
const bss_end = @ptrCast([*]u8, &microzig_bss_end);
const bss_start: [*]u8 = @ptrCast(&microzig_bss_start);
const bss_end: [*]u8 = @ptrCast(&microzig_bss_end);
const bss_len = @intFromPtr(bss_end) - @intFromPtr(bss_start);

@memset(bss_start[0..bss_len], 0);
}

// load .data from flash
{
const data_start = @ptrCast([*]u8, &microzig_data_start);
const data_end = @ptrCast([*]u8, &microzig_data_end);
const data_start: [*]u8 = @ptrCast(&microzig_data_start);
const data_end: [*]u8 = @ptrCast(&microzig_data_end);
const data_len = @intFromPtr(data_end) - @intFromPtr(data_start);
const data_src = @ptrCast([*]const u8, &microzig_data_load_start);
const data_src: [*]const u8 = @ptrCast(&microzig_data_load_start);

@memcpy(data_start[0..data_len], data_src[0..data_len]);
}
Expand Down
10 changes: 5 additions & 5 deletions src/start.zig
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,19 @@ pub fn initialize_system_memories() void {

// fill .bss with zeroes
{
const bss_start = @ptrCast([*]u8, &sections.microzig_bss_start);
const bss_end = @ptrCast([*]u8, &sections.microzig_bss_end);
const bss_start: [*]u8 = @ptrCast(&sections.microzig_bss_start);
const bss_end: [*]u8 = @ptrCast(&sections.microzig_bss_end);
const bss_len = @intFromPtr(bss_end) - @intFromPtr(bss_start);

@memset(bss_start[0..bss_len], 0);
}

// load .data from flash
{
const data_start = @ptrCast([*]u8, &sections.microzig_data_start);
const data_end = @ptrCast([*]u8, &sections.microzig_data_end);
const data_start: [*]u8 = @ptrCast(&sections.microzig_data_start);
const data_end: [*]u8 = @ptrCast(&sections.microzig_data_end);
const data_len = @intFromPtr(data_end) - @intFromPtr(data_start);
const data_src = @ptrCast([*]const u8, &sections.microzig_data_load_start);
const data_src: [*]const u8 = @ptrCast(&sections.microzig_data_load_start);

@memcpy(data_start[0..data_len], data_src[0..data_len]);
}
Expand Down

0 comments on commit 9392fe0

Please sign in to comment.