add stm32f103 registers and reorganize so that microzig can be used as a package (#9)

wch-ch32v003
Matt Knight 3 years ago committed by GitHub
parent 5a1e72f380
commit 46a924234e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,13 +3,11 @@
//! This means we need to use addExecutable() instead of using //! This means we need to use addExecutable() instead of using
const std = @import("std"); const std = @import("std");
const boards = @import("src/modules/boards.zig"); const microzig = @import("src/main.zig");
const chips = @import("src/modules/chips.zig");
const LinkerscriptStep = @import("src/modules/LinkerScriptStep.zig");
const Board = boards.Board; const boards = microzig.boards;
const Chip = chips.Chip; const chips = microzig.chips;
const Cpu = chips.Cpu; const Backing = microzig.Backing;
pub fn build(b: *std.build.Builder) !void { pub fn build(b: *std.build.Builder) !void {
const mode = b.standardReleaseOptions(); const mode = b.standardReleaseOptions();
@ -22,6 +20,7 @@ pub fn build(b: *std.build.Builder) !void {
BuildConfig{ .name = "boards.mbed_lpc1768", .backing = Backing{ .board = boards.mbed_lpc1768 } }, BuildConfig{ .name = "boards.mbed_lpc1768", .backing = Backing{ .board = boards.mbed_lpc1768 } },
//BuildConfig{ .name = "chips.atmega328p", .backing = Backing{ .chip = pkgs.chips.atmega328p } }, //BuildConfig{ .name = "chips.atmega328p", .backing = Backing{ .chip = pkgs.chips.atmega328p } },
BuildConfig{ .name = "chips.lpc1768", .backing = Backing{ .chip = chips.lpc1768 } }, BuildConfig{ .name = "chips.lpc1768", .backing = Backing{ .chip = chips.lpc1768 } },
//BuildConfig{ .name = "chips.stm32f103x8", .backing = Backing{ .chip = chips.stm32f103x8 } },
}; };
const Test = struct { name: []const u8, source: []const u8 }; const Test = struct { name: []const u8, source: []const u8 };
@ -35,7 +34,7 @@ pub fn build(b: *std.build.Builder) !void {
inline for (all_backings) |cfg| { inline for (all_backings) |cfg| {
inline for (all_tests) |tst| { inline for (all_tests) |tst| {
const exe = try addEmbeddedExecutable( const exe = try microzig.addEmbeddedExecutable(
b, b,
"test-" ++ tst.name ++ "-" ++ cfg.name, "test-" ++ tst.name ++ "-" ++ cfg.name,
tst.source, tst.source,
@ -51,144 +50,3 @@ pub fn build(b: *std.build.Builder) !void {
} }
} }
} }
fn addEmbeddedExecutable(
builder: *std.build.Builder,
name: []const u8,
source: []const u8,
backing: Backing,
) !*std.build.LibExeObjStep {
const Pkg = std.build.Pkg;
const microzig_base = Pkg{
.name = "microzig",
.path = .{ .path = "src/core/microzig.zig" },
};
const chip = switch (backing) {
.chip => |c| c,
.board => |b| b.chip,
};
const has_board = (backing == .board);
const chip_package = Pkg{
.name = "chip",
.path = .{ .path = chip.path },
.dependencies = &[_]Pkg{
microzig_base,
pkgs.mmio,
Pkg{
.name = "cpu",
.path = .{ .path = chip.cpu.path },
.dependencies = &[_]Pkg{ microzig_base, pkgs.mmio },
},
},
};
const config_file_name = blk: {
const hash = hash_blk: {
var hasher = std.hash.SipHash128(1, 2).init("abcdefhijklmnopq");
hasher.update(chip.name);
hasher.update(chip.path);
hasher.update(chip.cpu.name);
hasher.update(chip.cpu.path);
if (backing == .board) {
hasher.update(backing.board.name);
hasher.update(backing.board.path);
}
var mac: [16]u8 = undefined;
hasher.final(&mac);
break :hash_blk mac;
};
const file_prefix = "zig-cache/microzig/config-";
const file_suffix = ".zig";
var ld_file_name: [file_prefix.len + 2 * hash.len + file_suffix.len]u8 = undefined;
const filename = try std.fmt.bufPrint(&ld_file_name, "{s}{}{s}", .{
file_prefix,
std.fmt.fmtSliceHexLower(&hash),
file_suffix,
});
break :blk builder.dupe(filename);
};
{
std.fs.cwd().makeDir(std.fs.path.dirname(config_file_name).?) catch {};
var config_file = try std.fs.cwd().createFile(config_file_name, .{});
defer config_file.close();
var writer = config_file.writer();
try writer.print("pub const has_board = {};\n", .{has_board});
if (has_board)
try writer.print("pub const board_name = .@\"{}\";\n", .{std.fmt.fmtSliceEscapeUpper(backing.board.name)});
try writer.print("pub const chip_name = .@\"{}\";\n", .{std.fmt.fmtSliceEscapeUpper(chip.name)});
try writer.print("pub const cpu_name = .@\"{}\";\n", .{std.fmt.fmtSliceEscapeUpper(chip.cpu.name)});
}
const config_pkg = Pkg{
.name = "microzig-config",
.path = .{ .path = config_file_name },
};
const linkerscript = try LinkerscriptStep.create(builder, chip);
const exe = builder.addExecutable(name, source);
// might not be true for all machines (Pi Pico), but
// for the HAL it's true (it doesn't know the concept of threading)
exe.single_threaded = true;
exe.setTarget(chip.cpu.target);
exe.setLinkerScriptPath(.{ .generated = &linkerscript.generated_file });
// TODO:
// - Generate the linker scripts from the "chip" or "board" package instead of using hardcoded ones.
// - This requires building another tool that runs on the host that compiles those files and emits the linker script.
// - src/tools/linkerscript-gen.zig is the source file for this
exe.bundle_compiler_rt = false;
switch (backing) {
.chip => {
exe.addPackage(Pkg{
.name = microzig_base.name,
.path = microzig_base.path,
.dependencies = &[_]Pkg{ config_pkg, chip_package },
});
},
.board => |board| {
exe.addPackage(Pkg{
.name = microzig_base.name,
.path = microzig_base.path,
.dependencies = &[_]Pkg{
config_pkg,
chip_package,
Pkg{
.name = "board",
.path = .{ .path = board.path },
.dependencies = &[_]Pkg{ microzig_base, chip_package, pkgs.mmio },
},
},
});
},
}
return exe;
}
pub const Backing = union(enum) {
board: Board,
chip: Chip,
};
const pkgs = struct {
const mmio = std.build.Pkg{
.name = "microzig-mmio",
.path = .{ .path = "src/core/mmio.zig" },
};
};

@ -0,0 +1,154 @@
const std = @import("std");
pub const LinkerScriptStep = @import("modules/LinkerScriptStep.zig");
pub const boards = @import("modules/boards.zig");
pub const chips = @import("modules/chips.zig");
pub const cpus = @import("modules/cpus.zig");
pub const Board = @import("modules/Board.zig");
pub const Chip = @import("modules/Chip.zig");
pub const Cpu = @import("modules/Cpu.zig");
pub const Backing = union(enum) {
board: Board,
chip: Chip,
};
const root_path = root() ++ "/";
fn root() []const u8 {
return std.fs.path.dirname(@src().file) orelse unreachable;
}
pub fn addEmbeddedExecutable(
builder: *std.build.Builder,
name: []const u8,
source: []const u8,
backing: Backing,
) !*std.build.LibExeObjStep {
const Pkg = std.build.Pkg;
const microzig_base = Pkg{
.name = "microzig",
.path = .{ .path = root_path ++ "core/microzig.zig" },
};
const chip = switch (backing) {
.chip => |c| c,
.board => |b| b.chip,
};
const has_board = (backing == .board);
const chip_package = Pkg{
.name = "chip",
.path = .{ .path = chip.path },
.dependencies = &[_]Pkg{
microzig_base,
pkgs.mmio,
Pkg{
.name = "cpu",
.path = .{ .path = chip.cpu.path },
.dependencies = &[_]Pkg{ microzig_base, pkgs.mmio },
},
},
};
const config_file_name = blk: {
const hash = hash_blk: {
var hasher = std.hash.SipHash128(1, 2).init("abcdefhijklmnopq");
hasher.update(chip.name);
hasher.update(chip.path);
hasher.update(chip.cpu.name);
hasher.update(chip.cpu.path);
if (backing == .board) {
hasher.update(backing.board.name);
hasher.update(backing.board.path);
}
var mac: [16]u8 = undefined;
hasher.final(&mac);
break :hash_blk mac;
};
const file_prefix = "zig-cache/microzig/config-";
const file_suffix = ".zig";
var ld_file_name: [file_prefix.len + 2 * hash.len + file_suffix.len]u8 = undefined;
const filename = try std.fmt.bufPrint(&ld_file_name, "{s}{}{s}", .{
file_prefix,
std.fmt.fmtSliceHexLower(&hash),
file_suffix,
});
break :blk builder.dupe(filename);
};
{
std.fs.cwd().makeDir(std.fs.path.dirname(config_file_name).?) catch {};
var config_file = try std.fs.cwd().createFile(config_file_name, .{});
defer config_file.close();
var writer = config_file.writer();
try writer.print("pub const has_board = {};\n", .{has_board});
if (has_board)
try writer.print("pub const board_name = .@\"{}\";\n", .{std.fmt.fmtSliceEscapeUpper(backing.board.name)});
try writer.print("pub const chip_name = .@\"{}\";\n", .{std.fmt.fmtSliceEscapeUpper(chip.name)});
try writer.print("pub const cpu_name = .@\"{}\";\n", .{std.fmt.fmtSliceEscapeUpper(chip.cpu.name)});
}
const config_pkg = Pkg{
.name = "microzig-config",
.path = .{ .path = config_file_name },
};
const linkerscript = try LinkerScriptStep.create(builder, chip);
const exe = builder.addExecutable(name, source);
// might not be true for all machines (Pi Pico), but
// for the HAL it's true (it doesn't know the concept of threading)
exe.single_threaded = true;
exe.setTarget(chip.cpu.target);
exe.setLinkerScriptPath(.{ .generated = &linkerscript.generated_file });
// TODO:
// - Generate the linker scripts from the "chip" or "board" package instead of using hardcoded ones.
// - This requires building another tool that runs on the host that compiles those files and emits the linker script.
// - src/tools/linkerscript-gen.zig is the source file for this
exe.bundle_compiler_rt = false;
switch (backing) {
.chip => {
exe.addPackage(Pkg{
.name = microzig_base.name,
.path = microzig_base.path,
.dependencies = &[_]Pkg{ config_pkg, chip_package },
});
},
.board => |board| {
exe.addPackage(Pkg{
.name = microzig_base.name,
.path = microzig_base.path,
.dependencies = &[_]Pkg{
config_pkg,
chip_package,
Pkg{
.name = "board",
.path = .{ .path = board.path },
.dependencies = &[_]Pkg{ microzig_base, chip_package, pkgs.mmio },
},
},
});
},
}
return exe;
}
const pkgs = struct {
const mmio = std.build.Pkg{
.name = "microzig-mmio",
.path = .{ .path = root_path ++ "core/mmio.zig" },
};
};

@ -0,0 +1,5 @@
const Chip = @import("Chip.zig");
name: []const u8,
path: []const u8,
chip: Chip,

@ -0,0 +1,7 @@
const MemoryRegion = @import("MemoryRegion.zig");
const Cpu = @import("Cpu.zig");
name: []const u8,
path: []const u8,
cpu: Cpu,
memory_regions: []const MemoryRegion,

@ -0,0 +1,5 @@
const std = @import("std");
name: []const u8,
path: []const u8,
target: std.zig.CrossTarget,

@ -1,6 +1,6 @@
const std = @import("std"); const std = @import("std");
const MemoryRegion = @import("MemoryRegion.zig"); const MemoryRegion = @import("MemoryRegion.zig");
const Chip = @import("chips.zig").Chip; const Chip = @import("Chip.zig");
const Step = std.build.Step; const Step = std.build.Step;
const Builder = std.build.Builder; const Builder = std.build.Builder;
const GeneratedFile = std.build.GeneratedFile; const GeneratedFile = std.build.GeneratedFile;

@ -1,20 +1,21 @@
const std = @import("std");
const chips = @import("chips.zig"); const chips = @import("chips.zig");
const Board = @import("Board.zig");
const Chip = chips.Chip; fn root() []const u8 {
pub const Board = struct { return std.fs.path.dirname(@src().file) orelse unreachable;
name: []const u8, }
path: []const u8,
chip: Chip, const root_path = root() ++ "/";
};
pub const arduino_nano = Board{ pub const arduino_nano = Board{
.name = "Arduino Nano", .name = "Arduino Nano",
.path = "src/modules/boards/arduino-nano/arduino-nano.zig", .path = root_path ++ "boards/arduino-nano/arduino-nano.zig",
.chip = chips.atmega328p, .chip = chips.atmega328p,
}; };
pub const mbed_lpc1768 = Board{ pub const mbed_lpc1768 = Board{
.name = "mbed LPC1768", .name = "mbed LPC1768",
.path = "src/modules/boards/mbed-lpc1768/mbed-lpc1768.zig", .path = root_path ++ "boards/mbed-lpc1768/mbed-lpc1768.zig",
.chip = chips.lpc1768, .chip = chips.lpc1768,
}; };

@ -1,5 +1,6 @@
const std = @import("std"); const std = @import("std");
const cpus = @import("cpus.zig"); const cpus = @import("cpus.zig");
const Chip = @import("Chip.zig");
const MemoryRegion = @import("MemoryRegion.zig"); const MemoryRegion = @import("MemoryRegion.zig");
fn root() []const u8 { fn root() []const u8 {
@ -8,18 +9,11 @@ fn root() []const u8 {
const root_path = root() ++ "/"; const root_path = root() ++ "/";
pub const Chip = struct {
name: []const u8,
path: []const u8,
cpu: cpus.Cpu,
memory_regions: []const MemoryRegion,
};
pub const atmega328p = Chip{ pub const atmega328p = Chip{
.name = "ATmega328p", .name = "ATmega328p",
.path = root_path ++ "chips/atmega328p/atmega328p.zig", .path = root_path ++ "chips/atmega328p/atmega328p.zig",
.cpu = cpus.avr5, .cpu = cpus.avr5,
.memory_regions = &[_]MemoryRegion{ .memory_regions = &.{
MemoryRegion{ .offset = 0x000000, .length = 32 * 1024, .kind = .flash }, MemoryRegion{ .offset = 0x000000, .length = 32 * 1024, .kind = .flash },
MemoryRegion{ .offset = 0x800100, .length = 2048, .kind = .ram }, MemoryRegion{ .offset = 0x800100, .length = 2048, .kind = .ram },
}, },
@ -29,9 +23,19 @@ pub const lpc1768 = Chip{
.name = "NXP LPC1768", .name = "NXP LPC1768",
.path = root_path ++ "chips/lpc1768/lpc1768.zig", .path = root_path ++ "chips/lpc1768/lpc1768.zig",
.cpu = cpus.cortex_m3, .cpu = cpus.cortex_m3,
.memory_regions = &[_]MemoryRegion{ .memory_regions = &.{
MemoryRegion{ .offset = 0x00000000, .length = 512 * 1024, .kind = .flash }, MemoryRegion{ .offset = 0x00000000, .length = 512 * 1024, .kind = .flash },
MemoryRegion{ .offset = 0x10000000, .length = 32 * 1024, .kind = .ram }, MemoryRegion{ .offset = 0x10000000, .length = 32 * 1024, .kind = .ram },
MemoryRegion{ .offset = 0x2007C000, .length = 32 * 1024, .kind = .ram }, MemoryRegion{ .offset = 0x2007C000, .length = 32 * 1024, .kind = .ram },
}, },
}; };
pub const stm32f103x8 = Chip{
.name = "STM32F103x8",
.path = root_path ++ "chips/stm32f103/stm32f103.zig",
.cpu = cpus.cortex_m3,
.memory_regions = &.{
MemoryRegion{ .offset = 0x00000000, .length = 64 * 1024, .kind = .flash },
MemoryRegion{ .offset = 0x10000000, .length = 20 * 1024, .kind = .ram },
},
};

File diff suppressed because it is too large Load Diff

@ -0,0 +1,11 @@
const std = @import("std");
const micro = @import("microzig");
const micro_linker = @import("microzig-linker");
pub const cpu = @import("cpu");
pub const registers = @import("registers.zig");
pub const memory_regions = [_]micro_linker.MemoryRegion{
micro_linker.MemoryRegion{ .offset = 0x00000000, .length = 64 * 1024, .kind = .flash },
micro_linker.MemoryRegion{ .offset = 0x10000000, .length = 20 * 1024, .kind = .ram },
};

@ -1,4 +1,5 @@
const std = @import("std"); const std = @import("std");
const Cpu = @import("Cpu.zig");
fn root() []const u8 { fn root() []const u8 {
return std.fs.path.dirname(@src().file) orelse unreachable; return std.fs.path.dirname(@src().file) orelse unreachable;
@ -6,12 +7,6 @@ fn root() []const u8 {
const root_path = root() ++ "/"; const root_path = root() ++ "/";
pub const Cpu = struct {
name: []const u8,
path: []const u8,
target: std.zig.CrossTarget,
};
pub const avr5 = Cpu{ pub const avr5 = Cpu{
.name = "AVR5", .name = "AVR5",
.path = root_path ++ "cpus/avr/avr5.zig", .path = root_path ++ "cpus/avr/avr5.zig",

@ -13,6 +13,7 @@ const led_pin = if (micro.config.has_board)
else switch (micro.config.chip_name) { else switch (micro.config.chip_name) {
.@"ATmega328p" => micro.Pin("PB5"), .@"ATmega328p" => micro.Pin("PB5"),
.@"NXP LPC1768" => micro.Pin("P1.18"), .@"NXP LPC1768" => micro.Pin("P1.18"),
.@"STM32F103x8" => micro.Pin("PC13"),
else => @compileError("unknown chip"), else => @compileError("unknown chip"),
}; };

Loading…
Cancel
Save