Skip to content

Commit

Permalink
Fixed building error for WCH CH32V (#298)
Browse files Browse the repository at this point in the history
* Fix build errors in example files.

Related to the commit 'Build system rewrite (#259)'

* use cpu specific module.

Without this cpu specific module, the startup_logic is skipped
because the cpu doen't start from _start label but start at the top of binary.

* Added LICENSE files for wch/ch32v.

---------

Co-authored-by: nosuz <[email protected]>
  • Loading branch information
nosuz and nosuz authored Dec 2, 2024
1 parent 50e684d commit 813b85c
Show file tree
Hide file tree
Showing 14 changed files with 205 additions and 134 deletions.
3 changes: 3 additions & 0 deletions build-internals/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ pub const Chip = struct {
/// The cpu target this controller uses.
cpu: std.Target.Query,

/// The cpu specific module file, which contains its startup_logic.
cpu_module_file: ?LazyPath = null,

/// The provider for register definitions.
register_definition: union(enum) {
/// Use `regz` to create a zig file from a JSON schema.
Expand Down
12 changes: 10 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub const MemoryRegion = internals.MemoryRegion;

const regz = @import("tools/regz");

// If more ports are available, the error "error: evaluation exceeded 1000 backwards branches" may occur.
// In such cases, consider increasing the argument value for @setEvalBranchQuota().
const port_list: []const struct {
name: [:0]const u8,
dep_name: [:0]const u8,
Expand All @@ -24,7 +26,7 @@ const port_list: []const struct {
.{ .name = "lpc", .dep_name = "port/nxp/lpc" },
.{ .name = "rp2xxx", .dep_name = "port/raspberrypi/rp2xxx" },
.{ .name = "stm32", .dep_name = "port/stmicro/stm32" },
//.{ .name = "ch32v", .dep_name = "port/wch/ch32v" },
.{ .name = "ch32v", .dep_name = "port/wch/ch32v" },
};

pub fn build(b: *Build) void {
Expand Down Expand Up @@ -315,7 +317,12 @@ pub fn MicroBuild(port_select: PortSelect) type {
},
});

const cpu_mod = cpu.create_module(b, mb.core_dep);
const cpu_mod = if (target.chip.cpu_module_file) |root_source_file|
b.createModule(.{
.root_source_file = root_source_file,
})
else
cpu.create_module(b, mb.core_dep);
cpu_mod.addImport("microzig", core_mod);
core_mod.addImport("cpu", cpu_mod);

Expand Down Expand Up @@ -660,6 +667,7 @@ pub inline fn custom_lazy_import(
}

inline fn custom_find_import_pkg_hash_or_fatal(comptime dep_name: []const u8) []const u8 {
@setEvalBranchQuota(2000);
const build_runner = @import("root");
const deps = build_runner.dependencies;

Expand Down
2 changes: 1 addition & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
.@"port/nxp/lpc" = .{ .path = "port/nxp/lpc", .lazy = true },
.@"port/raspberrypi/rp2xxx" = .{ .path = "port/raspberrypi/rp2xxx", .lazy = true },
.@"port/stmicro/stm32" = .{ .path = "port/stmicro/stm32", .lazy = true },
//.@"port/wch/ch32v" = .{ .path = "port/wch/ch32v", .lazy = true },
.@"port/wch/ch32v" = .{ .path = "port/wch/ch32v", .lazy = true },

// used for creating package tarballs
.boxzer = .{
Expand Down
2 changes: 1 addition & 1 deletion examples/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const example_dep_names: []const []const u8 = &.{
"nxp/lpc",
"raspberrypi/rp2xxx",
"stmicro/stm32",
// "wch/ch32",
"wch/ch32v",
};

pub fn build(b: *std.Build) void {
Expand Down
1 change: 1 addition & 0 deletions examples/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
.@"nxp/lpc" = .{ .path = "nxp/lpc" },
.@"raspberrypi/rp2xxx" = .{ .path = "raspberrypi/rp2xxx" },
.@"stmicro/stm32" = .{ .path = "stmicro/stm32" },
.@"wch/ch32v" = .{ .path = "wch/ch32v" },
},
.paths = .{
"build.zig",
Expand Down
19 changes: 19 additions & 0 deletions examples/wch/ch32v/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) Zig Embedded Group contributors

This software is provided 'as-is', without any express or implied warranty. In
no event will the authors be held liable for any damages arising from the use
of this software.

Permission is granted to anyone to use this software for any purpose, including
commercial applications, and to alter it and redistribute it freely, subject to
the following restrictions:

1. The origin of this software must not be misrepresented; you must not claim
that you wrote the original software. If you use this software in a product,
an acknowledgment in the product documentation would be appreciated but is
not required.

2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.

3. This notice may not be removed or altered from any source distribution.
59 changes: 31 additions & 28 deletions examples/wch/ch32v/build.zig
Original file line number Diff line number Diff line change
@@ -1,58 +1,61 @@
const std = @import("std");
const MicroZig = @import("microzig/build");
const wch = @import("microzig/port/wch/ch32v");

const available_examples = [_]Example{
// CH32V003
.{ .target = wch.chips.ch32v003x4, .name = "empty_ch32v003", .file = "src/empty.zig" },
.{ .target = wch.chips.ch32v003x4, .name = "blinky_ch32v003", .file = "src/blinky_ch32v003.zig" },
// .{ .target = wch.boards.ch32v003.ch32v003f4p6_r0_1v1, .name = "ch32v003f4p6_r0_1v1_empty", .file = "src/empty.zig" },
// .{ .target = wch.boards.ch32v003.ch32v003f4p6_r0_1v1, .name = "ch32v003f4p6_r0_1v1_blinky", .file = "src/blinky.zig" },

// CH32V103
.{ .target = wch.chips.ch32v103x8, .name = "empty_ch32v103", .file = "src/empty.zig" },
.{ .target = wch.chips.ch32v103x8, .name = "blinky_ch32v103", .file = "src/blinky.zig" },
// .{ .target = wch.boards.ch32v103.ch32v103r_r1_1v1, .name = "ch32v103r_r1_1v1_empty", .file = "src/empty.zig" },
// .{ .target = wch.boards.ch32v103.ch32v103r_r1_1v1, .name = "ch32v103r_r1_1v1_blinky", .file = "src/blinky.zig" },

// CH32V203
.{ .target = wch.chips.ch32v203x8, .name = "empty_ch32v203", .file = "src/empty.zig" },
.{ .target = wch.chips.ch32v203x8, .name = "blinky_ch32v203", .file = "src/blinky.zig" },
.{ .target = wch.boards.ch32v203.suzuduino_uno_v1b, .name = "suzuduino_blinky", .file = "src/board_blinky.zig" },
};
const microzig = @import("microzig");

const MicroBuild = microzig.MicroBuild(.{
.ch32v = true,
});

pub fn build(b: *std.Build) void {
const microzig = MicroZig.init(b, .{});
const optimize = b.standardOptimizeOption(.{});

const mz_dep = b.dependency("microzig", .{});
const mb = MicroBuild.init(b, mz_dep) orelse return;

const available_examples = [_]Example{
// CH32V003
.{ .target = mb.ports.ch32v.chips.ch32v003x4, .name = "empty_ch32v003", .file = "src/empty.zig" },
.{ .target = mb.ports.ch32v.chips.ch32v003x4, .name = "blinky_ch32v003", .file = "src/blinky_ch32v003.zig" },
.{ .target = mb.ports.ch32v.boards.ch32v003.ch32v003f4p6_r0_1v1, .name = "ch32v003f4p6_r0_1v1_empty", .file = "src/empty.zig" },
.{ .target = mb.ports.ch32v.boards.ch32v003.ch32v003f4p6_r0_1v1, .name = "ch32v003f4p6_r0_1v1_blinky", .file = "src/blinky.zig" },

// CH32V103
.{ .target = mb.ports.ch32v.chips.ch32v103x8, .name = "empty_ch32v103", .file = "src/empty.zig" },
.{ .target = mb.ports.ch32v.chips.ch32v103x8, .name = "blinky_ch32v103", .file = "src/blinky.zig" },
.{ .target = mb.ports.ch32v.boards.ch32v103.ch32v103r_r1_1v1, .name = "ch32v103r_r1_1v1_blinky", .file = "src/blinky.zig" },
.{ .target = mb.ports.ch32v.boards.ch32v103.ch32v103r_r1_1v1, .name = "ch32v103r_r1_1v1_empty", .file = "src/empty.zig" },

// CH32V203
.{ .target = mb.ports.ch32v.chips.ch32v203x8, .name = "empty_ch32v203", .file = "src/empty.zig" },
.{ .target = mb.ports.ch32v.chips.ch32v203x8, .name = "blinky_ch32v203", .file = "src/blinky.zig" },
.{ .target = mb.ports.ch32v.boards.ch32v203.suzuduino_uno_v1b, .name = "suzuduino_blinky", .file = "src/board_blinky.zig" },
};

for (available_examples) |example| {
// `add_firmware` basically works like addExecutable, but takes a
// `microzig.Target` for target instead of a `std.zig.CrossTarget`.
//
// The target will convey all necessary information on the chip,
// cpu and potentially the board as well.
const firmware = microzig.add_firmware(b, .{
const fw = mb.add_firmware(.{
.name = example.name,
.target = example.target,
.optimize = optimize,
// .optimize = .ReleaseSmall, // not work -Doptimize=ReleaseSmall
.root_source_file = b.path(example.file),
});

// `install_firmware()` is the MicroZig pendant to `Build.installArtifact()`
// and allows installing the firmware as a typical firmware file.
//
// This will also install into `$prefix/firmware` instead of `$prefix/bin`.
microzig.install_firmware(b, firmware, .{ .format = .bin });
mb.install_firmware(fw, .{});

// For debugging, we also always install the firmware as an ELF file
// microzig.install_firmware(b, firmware, .{}); // default format is ELF
microzig.install_firmware(b, firmware, .{ .format = .elf });
mb.install_firmware(fw, .{ .format = .elf });
}
}

const Example = struct {
target: MicroZig.Target,
target: *const microzig.Target,
name: []const u8,
file: []const u8,
};
3 changes: 1 addition & 2 deletions examples/wch/ch32v/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
.name = "examples/wch/ch32v",
.version = "0.0.0",
.dependencies = .{
.@"microzig/build" = .{ .path = "../../../build" },
.@"microzig/port/wch/ch32v" = .{ .path = "../../../port/wch/ch32v" },
.microzig = .{ .path = "../../.." },
},

.paths = .{
Expand Down
19 changes: 19 additions & 0 deletions port/wch/ch32v/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) Zig Embedded Group contributors

This software is provided 'as-is', without any express or implied warranty. In
no event will the authors be held liable for any damages arising from the use
of this software.

Permission is granted to anyone to use this software for any purpose, including
commercial applications, and to alter it and redistribute it freely, subject to
the following restrictions:

1. The origin of this software must not be misrepresented; you must not claim
that you wrote the original software. If you use this software in a product,
an acknowledgment in the product documentation would be appreciated but is
not required.

2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.

3. This notice may not be removed or altered from any source distribution.
Loading

0 comments on commit 813b85c

Please sign in to comment.