This repository has been archived by the owner on Feb 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbuild.zig
305 lines (263 loc) · 10.5 KB
/
build.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
const std = @import("std");
const microzig = @import("root").dependencies.imports.microzig; // HACK: Please import MicroZig always under the name `microzig`. Otherwise the RP2040 module will fail to be properly imported.
fn root() []const u8 {
return comptime (std.fs.path.dirname(@src().file) orelse ".");
}
const build_root = root();
////////////////////////////////////////
// MicroZig Gen 2 Interface //
////////////////////////////////////////
pub fn build(b: *std.Build) !void {
// Dummy func to make package manager happy
_ = b;
}
pub const chips = struct {
// Note: This chip has no flash support defined and requires additional configuration!
pub const rp2040 = .{
.preferred_format = .{ .uf2 = .RP2040 },
.chip = chip,
.hal = hal,
.board = null,
.linker_script = linker_script,
};
};
pub const boards = struct {
pub const raspberry_pi = struct {
pub const pico = .{
.preferred_format = .{ .uf2 = .RP2040 },
.chip = chip,
.hal = hal,
.linker_script = linker_script,
.board = .{
.name = "RaspberryPi Pico",
.source_file = .{ .cwd_relative = build_root ++ "/src/boards/raspberry_pi_pico.zig" },
.url = "https://www.raspberrypi.com/products/raspberry-pi-pico/",
},
.configure = rp2040_configure(.w25q080),
};
};
pub const waveshare = struct {
pub const rp2040_plus_4m = .{
.preferred_format = .{ .uf2 = .RP2040 },
.chip = chip,
.hal = hal,
.linker_script = linker_script,
.board = .{
.name = "Waveshare RP2040-Plus (4M Flash)",
.source_file = .{ .cwd_relative = build_root ++ "/src/boards/waveshare_rp2040_plus_4m.zig" },
.url = "https://www.waveshare.com/rp2040-plus.htm",
},
.configure = rp2040_configure(.w25q080),
};
pub const rp2040_plus_16m = .{
.preferred_format = .{ .uf2 = .RP2040 },
.chip = chip,
.hal = hal,
.linker_script = linker_script,
.board = .{
.name = "Waveshare RP2040-Plus (16M Flash)",
.source_file = .{ .cwd_relative = build_root ++ "/src/boards/waveshare_rp2040_plus_16m.zig" },
.url = "https://www.waveshare.com/rp2040-plus.htm",
},
.configure = rp2040_configure(.w25q080),
};
pub const rp2040_eth = .{
.preferred_format = .{ .uf2 = .RP2040 },
.chip = chip,
.hal = hal,
.linker_script = linker_script,
.board = .{
.name = "Waveshare RP2040-ETH Mini",
.source_file = .{ .cwd_relative = build_root ++ "/src/boards/waveshare_rp2040_eth.zig" },
.url = "https://www.waveshare.com/rp2040-eth.htm",
},
.configure = rp2040_configure(.w25q080),
};
pub const rp2040_matrix = .{
.preferred_format = .{ .uf2 = .RP2040 },
.chip = chip,
.hal = hal,
.linker_script = linker_script,
.board = .{
.name = "Waveshare RP2040-Matrix",
.source_file = .{ .cwd_relative = build_root ++ "/src/boards/waveshare_rp2040_matrix.zig" },
.url = "https://www.waveshare.com/rp2040-matrix.htm",
},
.configure = rp2040_configure(.w25q080),
};
};
};
pub const BootROM = union(enum) {
artifact: *std.build.CompileStep, // provide a custom startup code
blob: std.build.LazyPath, // just include a binary blob
// Pre-shipped ones:
at25sf128a,
generic_03h,
is25lp080,
w25q080,
w25x10cl,
// Use the old stage2 bootloader vendored with MicroZig till 2023-09-13
legacy,
};
const linker_script = .{
.source_file = .{ .cwd_relative = build_root ++ "/rp2040.ld" },
};
const hal = .{
.source_file = .{ .cwd_relative = build_root ++ "/src/hal.zig" },
};
const chip = .{
.name = "RP2040",
.url = "https://www.raspberrypi.com/products/rp2040/",
.cpu = .cortex_m0plus,
.register_definition = .{
.json = .{ .cwd_relative = build_root ++ "/src/chips/RP2040.json" },
},
.memory_regions = &.{
.{ .kind = .flash, .offset = 0x10000100, .length = (2048 * 1024) - 256 },
.{ .kind = .flash, .offset = 0x10000000, .length = 256 },
.{ .kind = .ram, .offset = 0x20000000, .length = 256 * 1024 },
},
};
/// Returns a configuration function that will add the provided `BootROM` to the firmware.
pub fn rp2040_configure(comptime bootrom: BootROM) *const fn (host_build: *std.Build, *microzig.Firmware) void {
const T = struct {
fn configure(host_build: *std.Build, fw: *microzig.Firmware) void {
const bootrom_file = getBootrom(host_build, bootrom);
// HACK: Inject the file as a dependency to MicroZig.board
fw.modules.board.?.dependencies.put(
"bootloader",
host_build.createModule(.{
.source_file = bootrom_file.bin,
}),
) catch @panic("oom");
bootrom_file.bin.addStepDependencies(&fw.artifact.step);
}
};
return T.configure;
}
pub const Stage2Bootloader = struct {
bin: std.Build.LazyPath,
elf: ?std.Build.LazyPath,
};
pub fn getBootrom(b: *std.Build, rom: BootROM) Stage2Bootloader {
const rom_exe = switch (rom) {
.artifact => |artifact| artifact,
.blob => |blob| return Stage2Bootloader{
.bin = blob,
.elf = null,
},
else => blk: {
var target = @as(microzig.CpuModel, chip.cpu).getDescriptor().target;
target.abi = .eabi;
const rom_path = b.pathFromRoot(b.fmt("{s}/src/bootroms/{s}.S", .{ build_root, @tagName(rom) }));
const rom_exe = b.addExecutable(.{
.name = b.fmt("stage2-{s}", .{@tagName(rom)}),
.optimize = .ReleaseSmall,
.target = target,
.root_source_file = null,
});
rom_exe.linkage = .static;
// rom_exe.pie = false;
// rom_exe.force_pic = false;
rom_exe.setLinkerScript(.{ .path = build_root ++ "/src/bootroms/shared/stage2.ld" });
rom_exe.addAssemblyFile(.{ .path = rom_path });
break :blk rom_exe;
},
};
const rom_objcopy = b.addObjCopy(rom_exe.getEmittedBin(), .{
.basename = b.fmt("{s}.bin", .{@tagName(rom)}),
.format = .bin,
});
return Stage2Bootloader{
.bin = rom_objcopy.getOutput(),
.elf = rom_exe.getEmittedBin(),
};
}
/////////////////////////////////////////
// MicroZig Legacy Interface //
/////////////////////////////////////////
// // this build script is mostly for testing and verification of this
// // package. In an attempt to modularize -- designing for a case where a
// // project requires multiple HALs, it accepts microzig as a param
// pub fn build(b: *Build) !void {
// const optimize = b.standardOptimizeOption(.{});
// const args_dep = b.dependency("args", .{});
// const args_mod = args_dep.module("args");
// var examples = Examples.init(b, optimize);
// examples.install(b);
// const pio_tests = b.addTest(.{
// .root_source_file = .{
// .path = "src/hal.zig",
// },
// .optimize = optimize,
// });
// pio_tests.addIncludePath(.{ .path = "src/hal/pio/assembler" });
// const test_step = b.step("test", "run unit tests");
// test_step.dependOn(&b.addRunArtifact(pio_tests).step);
// {
// const flash_tool = b.addExecutable(.{
// .name = "rp2040-flash",
// .optimize = .Debug,
// .target = .{},
// .root_source_file = .{ .path = "tools/rp2040-flash.zig" },
// });
// flash_tool.addModule("args", args_mod);
// b.installArtifact(flash_tool);
// }
// // Install all bootroms for debugging and CI
// inline for (comptime std.enums.values(std.meta.Tag(BootROM))) |rom| {
// if (rom == .artifact or rom == .blob) {
// continue;
// }
// if (rom == .is25lp080) {
// // TODO: https://github.com/ZigEmbeddedGroup/raspberrypi-rp2040/issues/79
// // is25lp080.o:(text+0x16): has non-ABS relocation R_ARM_THM_CALL against symbol 'read_flash_sreg'
// continue;
// }
// const files = getBootrom(b, rom);
// if (files.elf) |elf| {
// b.getInstallStep().dependOn(
// &b.addInstallFileWithDir(elf, .{ .custom = "stage2" }, b.fmt("{s}.elf", .{@tagName(rom)})).step,
// );
// }
// b.getInstallStep().dependOn(
// &b.addInstallFileWithDir(files.bin, .{ .custom = "stage2" }, b.fmt("{s}.bin", .{@tagName(rom)})).step,
// );
// }
// }
// pub const Examples = struct {
// adc: *microzig.EmbeddedExecutable,
// blinky: *microzig.EmbeddedExecutable,
// blinky_core1: *microzig.EmbeddedExecutable,
// gpio_clk: *microzig.EmbeddedExecutable,
// i2c_bus_scan: *microzig.EmbeddedExecutable,
// pwm: *microzig.EmbeddedExecutable,
// spi_master: *microzig.EmbeddedExecutable,
// uart: *microzig.EmbeddedExecutable,
// squarewave: *microzig.EmbeddedExecutable,
// //uart_pins: microzig.EmbeddedExecutable,
// flash_program: *microzig.EmbeddedExecutable,
// usb_device: *microzig.EmbeddedExecutable,
// usb_hid: *microzig.EmbeddedExecutable,
// ws2812: *microzig.EmbeddedExecutable,
// random: *microzig.EmbeddedExecutable,
// pub fn init(b: *Build, optimize: std.builtin.OptimizeMode) Examples {
// var ret: Examples = undefined;
// inline for (@typeInfo(Examples).Struct.fields) |field| {
// const path = comptime root() ++ "examples/" ++ field.name ++ ".zig";
// @field(ret, field.name) = addExecutable(b, .{
// .name = field.name,
// .source_file = .{ .path = path },
// .optimize = optimize,
// });
// }
// return ret;
// }
// pub fn install(examples: *Examples, b: *Build) void {
// inline for (@typeInfo(Examples).Struct.fields) |field| {
// b.getInstallStep().dependOn(
// &b.addInstallFileWithDir(@field(examples, field.name).inner.getEmittedBin(), .{ .custom = "firmware" }, field.name ++ ".elf").step,
// );
// }
// }
// };