Skip to content

Commit d76885c

Browse files
tact1m4n3Copper280z
authored andcommitted
nrf5x: micro:bit support (#600)
1 parent 0d71ecb commit d76885c

File tree

17 files changed

+455
-82
lines changed

17 files changed

+455
-82
lines changed

examples/nordic/nrf5x/build.zig

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,35 @@ pub fn build(b: *std.Build) void {
1515
const nrf52840_dongle = mb.ports.nrf5x.boards.nordic.nrf52840_dongle;
1616
const nrf52840_mdk = mb.ports.nrf5x.boards.nordic.nrf52840_mdk;
1717
const pca10040 = mb.ports.nrf5x.boards.nordic.pca10040;
18+
const microbit_v1 = mb.ports.nrf5x.boards.bbc.microbit_v1;
19+
const microbit_v2 = mb.ports.nrf5x.boards.bbc.microbit_v2;
20+
21+
// TODO: better system for examples
1822

1923
const available_examples = [_]Example{
20-
.{ .target = pca10040, .name = "blinky", .file = "src/blinky.zig" },
21-
.{ .target = nrf52840_dongle, .name = "blinky", .file = "src/blinky.zig" },
22-
.{ .target = nrf52840_mdk, .name = "blinky", .file = "src/blinky.zig" },
23-
.{ .target = pca10040, .name = "uart", .file = "src/uart.zig" },
24-
.{ .target = nrf52840_mdk, .name = "uart", .file = "src/uart.zig" },
25-
.{ .target = pca10040, .name = "i2c_bus_scan", .file = "src/i2c_bus_scan.zig" },
26-
.{ .target = nrf52840_mdk, .name = "i2c_bus_scan", .file = "src/i2c_bus_scan.zig" },
27-
.{ .target = nrf52840_mdk, .name = "i2c_accel", .file = "src/i2c_accel.zig" },
28-
.{ .target = pca10040, .name = "i2c_temp", .file = "src/i2c_temp.zig" },
29-
.{ .target = nrf52840_mdk, .name = "i2c_temp", .file = "src/i2c_temp.zig" },
30-
.{ .target = nrf52840_mdk, .name = "spi_master", .file = "src/spi_master.zig" },
31-
.{ .target = pca10040, .name = "spi_master", .file = "src/spi_master.zig" },
24+
.{ .target = nrf52840_dongle, .name = "nrf52840_dongle_blinky", .file = "src/blinky.zig" },
25+
26+
.{ .target = nrf52840_mdk, .name = "nrf52840_mdk_blinky", .file = "src/blinky.zig" },
27+
.{ .target = nrf52840_mdk, .name = "nrf52840_mdk_uart", .file = "src/uart.zig" },
28+
.{ .target = nrf52840_mdk, .name = "nrf52840_mdk_i2c_bus_scan", .file = "src/i2c_bus_scan.zig" },
29+
.{ .target = nrf52840_mdk, .name = "nrf52840_mdk_i2c_accel", .file = "src/i2c_accel.zig" },
30+
.{ .target = nrf52840_mdk, .name = "nrf52840_mdk_i2c_temp", .file = "src/i2c_temp.zig" },
31+
.{ .target = nrf52840_mdk, .name = "nrf52840_mdk_spi_master", .file = "src/spi_master.zig" },
32+
33+
.{ .target = pca10040, .name = "pca10040_blinky", .file = "src/blinky.zig" },
34+
.{ .target = pca10040, .name = "pca10040_uart", .file = "src/uart.zig" },
35+
.{ .target = pca10040, .name = "pca10040_i2c_bus_scan", .file = "src/i2c_bus_scan.zig" },
36+
.{ .target = pca10040, .name = "pca10040_i2c_temp", .file = "src/i2c_temp.zig" },
37+
.{ .target = pca10040, .name = "pca10040_spi_master", .file = "src/spi_master.zig" },
38+
39+
.{ .target = microbit_v1, .name = "microbit_v1_display", .file = "src/microbit/display.zig" },
40+
.{ .target = microbit_v2, .name = "microbit_v2_display", .file = "src/microbit/display.zig" },
3241
};
3342

3443
for (available_examples) |example| {
35-
const target = example.target;
36-
const name = mb.builder.fmt("{s}_{s}", .{
37-
if (target.board) |board| board.name else target.chip.name,
38-
example.name,
39-
});
40-
4144
// If we specify example, only select the ones that match
4245
if (maybe_example) |selected_example|
43-
if (!std.mem.containsAtLeast(u8, name, 1, selected_example))
46+
if (!std.mem.containsAtLeast(u8, example.name, 1, selected_example))
4447
continue;
4548

4649
// `add_firmware` basically works like addExecutable, but takes a
@@ -49,7 +52,7 @@ pub fn build(b: *std.Build) void {
4952
// The target will convey all necessary information on the chip,
5053
// cpu and potentially the board as well.
5154
const fw = mb.add_firmware(.{
52-
.name = name,
55+
.name = example.name,
5356
.target = example.target,
5457
.optimize = optimize,
5558
.root_source_file = b.path(example.file),
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const std = @import("std");
2+
const microzig = @import("microzig");
3+
const nrf = microzig.hal;
4+
const time = nrf.time;
5+
const microbit = microzig.board;
6+
7+
pub const heart: [5][5]u1 = .{
8+
.{ 0, 1, 0, 1, 0 },
9+
.{ 1, 0, 1, 0, 1 },
10+
.{ 1, 0, 0, 0, 1 },
11+
.{ 0, 1, 0, 1, 0 },
12+
.{ 0, 0, 1, 0, 0 },
13+
};
14+
15+
pub fn main() !void {
16+
microbit.display.init();
17+
18+
while (true) {
19+
microbit.display.render(heart, .from_ms(1_000));
20+
time.sleep_ms(1_000);
21+
}
22+
}

port/nordic/nrf5x/build.zig

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ const microzig = @import("microzig/build-internals");
44
const Self = @This();
55

66
chips: struct {
7+
nrf51822: *const microzig.Target,
78
nrf52832: *const microzig.Target,
9+
nrf52833: *const microzig.Target,
810
nrf52840: *const microzig.Target,
911
},
1012

@@ -14,6 +16,10 @@ boards: struct {
1416
nrf52840_mdk: *const microzig.Target,
1517
pca10040: *const microzig.Target,
1618
},
19+
bbc: struct {
20+
microbit_v1: *const microzig.Target,
21+
microbit_v2: *const microzig.Target,
22+
},
1723
},
1824

1925
pub fn init(dep: *std.Build.Dependency) Self {
@@ -25,6 +31,30 @@ pub fn init(dep: *std.Build.Dependency) Self {
2531
.root_source_file = b.path("src/hal.zig"),
2632
};
2733

34+
const chip_nrf51822: microzig.Target = .{
35+
.dep = dep,
36+
.preferred_binary_format = .elf,
37+
.zig_target = .{
38+
.cpu_arch = .thumb,
39+
.cpu_model = .{ .explicit = &std.Target.arm.cpu.cortex_m0 },
40+
.os_tag = .freestanding,
41+
.abi = .eabi,
42+
},
43+
.chip = .{
44+
.name = "nrf51",
45+
.url = "https://www.nordicsemi.com/products/nrf51822",
46+
.register_definition = .{
47+
.svd = nrfx.path("mdk/nrf51.svd"),
48+
},
49+
.memory_regions = &.{
50+
.{ .tag = .flash, .offset = 0x00000000, .length = 128 * 1024, .access = .rx },
51+
.{ .tag = .ram, .offset = 0x20000000, .length = 16 * 1024, .access = .rwx },
52+
},
53+
.patches = @import("patches/nrf51.zig").patches,
54+
},
55+
.hal = .{ .root_source_file = b.path("src/hal.zig") },
56+
};
57+
2858
const chip_nrf52832: microzig.Target = .{
2959
.dep = dep,
3060
.preferred_binary_format = .elf,
@@ -38,14 +68,37 @@ pub fn init(dep: *std.Build.Dependency) Self {
3868
.name = "nrf52",
3969
.url = "https://www.nordicsemi.com/products/nrf52832",
4070
.register_definition = .{
41-
// TODO: does this determine the name of the chips/x.zig?
4271
.svd = nrfx.path("mdk/nrf52.svd"),
4372
},
4473
.memory_regions = &.{
4574
.{ .tag = .flash, .offset = 0x00000000, .length = 0x80000, .access = .rx },
46-
.{ .tag = .ram, .offset = 0x20000000, .length = 0x10000, .access = .rw },
75+
.{ .tag = .ram, .offset = 0x20000000, .length = 0x10000, .access = .rwx },
4776
},
48-
.patches = @import("patches/nrf52832.zig").patches,
77+
.patches = @import("patches/nrf528xx.zig").patches,
78+
},
79+
.hal = .{ .root_source_file = b.path("src/hal.zig") },
80+
};
81+
82+
const chip_nrf52833: microzig.Target = .{
83+
.dep = dep,
84+
.preferred_binary_format = .elf,
85+
.zig_target = .{
86+
.cpu_arch = .thumb,
87+
.cpu_model = .{ .explicit = &std.Target.arm.cpu.cortex_m4 },
88+
.os_tag = .freestanding,
89+
.abi = .eabi,
90+
},
91+
.chip = .{
92+
.name = "nrf52833",
93+
.url = "https://www.nordicsemi.com/products/nrf52833",
94+
.register_definition = .{
95+
.svd = nrfx.path("mdk/nrf52833.svd"),
96+
},
97+
.memory_regions = &.{
98+
.{ .tag = .flash, .offset = 0x00000000, .length = 512 * 1024, .access = .rx },
99+
.{ .tag = .ram, .offset = 0x20000000, .length = 128 * 1024, .access = .rwx },
100+
},
101+
.patches = @import("patches/nrf528xx.zig").patches,
49102
},
50103
.hal = .{ .root_source_file = b.path("src/hal.zig") },
51104
};
@@ -76,14 +129,16 @@ pub fn init(dep: *std.Build.Dependency) Self {
76129
// CODE_RAM
77130
.{ .name = "code_ram", .offset = 0x800000, .length = 0x40000, .access = .x },
78131
},
79-
.patches = @import("patches/nrf52840.zig").patches,
132+
.patches = @import("patches/nrf528xx.zig").patches,
80133
},
81134
.hal = hal,
82135
};
83136

84137
return .{
85138
.chips = .{
139+
.nrf51822 = chip_nrf51822.derive(.{}),
86140
.nrf52832 = chip_nrf52832.derive(.{}),
141+
.nrf52833 = chip_nrf52833.derive(.{}),
87142
.nrf52840 = chip_nrf52840.derive(.{}),
88143
},
89144
.boards = .{
@@ -110,6 +165,24 @@ pub fn init(dep: *std.Build.Dependency) Self {
110165
},
111166
}),
112167
},
168+
.bbc = .{
169+
.microbit_v1 = chip_nrf51822.derive(.{
170+
.preferred_binary_format = .hex,
171+
.board = .{
172+
.name = "micro:bit v1",
173+
.url = "https://tech.microbit.org/hardware/1-5-revision",
174+
.root_source_file = b.path("src/boards/microbit.zig"),
175+
},
176+
}),
177+
.microbit_v2 = chip_nrf52833.derive(.{
178+
.preferred_binary_format = .hex,
179+
.board = .{
180+
.name = "micro:bit v2",
181+
.url = "https://tech.microbit.org/hardware/2-2-revision",
182+
.root_source_file = b.path("src/boards/microbit.zig"),
183+
},
184+
}),
185+
},
113186
},
114187
};
115188
}

port/nordic/nrf5x/patches/nrf52840.zig renamed to port/nordic/nrf5x/patches/nrf51.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const Patch = @import("microzig/build-internals").Patch;
33
pub const patches: []const Patch = &.{
44
.{
55
.add_enum = .{
6-
.parent = "types.peripherals.P0",
6+
.parent = "types.peripherals.GPIO",
77
.@"enum" = .{
88
.name = "Pull",
99
.bitsize = 2,
@@ -15,10 +15,10 @@ pub const patches: []const Patch = &.{
1515
},
1616
},
1717
},
18-
.{ .set_enum_type = .{ .of = "types.peripherals.P0.PIN_CNF.PULL", .to = "types.peripherals.P0.Pull" } },
18+
.{ .set_enum_type = .{ .of = "types.peripherals.GPIO.PIN_CNF.PULL", .to = "types.peripherals.GPIO.Pull" } },
1919
.{
2020
.add_enum = .{
21-
.parent = "types.peripherals.P0",
21+
.parent = "types.peripherals.GPIO",
2222
.@"enum" = .{
2323
.name = "DriveStrength",
2424
.bitsize = 3,
@@ -35,5 +35,5 @@ pub const patches: []const Patch = &.{
3535
},
3636
},
3737
},
38-
.{ .set_enum_type = .{ .of = "types.peripherals.P0.PIN_CNF.DRIVE", .to = "types.peripherals.P0.DriveStrength" } },
38+
.{ .set_enum_type = .{ .of = "types.peripherals.GPIO.PIN_CNF.DRIVE", .to = "types.peripherals.GPIO.DriveStrength" } },
3939
};
File renamed without changes.

0 commit comments

Comments
 (0)