Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rp2xxx] Add initial riscv support for the rp2350 chip #348

Merged
merged 18 commits into from
Jan 15, 2025
Merged
52 changes: 4 additions & 48 deletions build-internals/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const Module = Build.Module;

const regz = @import("regz");
const Patch = regz.patch.Patch;
const uf2 = @import("uf2");
const FamilyId = uf2.FamilyId;

pub fn build(b: *Build) void {
_ = b.addModule("build-internals", .{
Expand Down Expand Up @@ -110,6 +112,7 @@ pub const Chip = struct {
/// The memory regions that are present in this chip.
memory_regions: []const MemoryRegion,

/// Register patches for this chip.
patches: []const Patch = &.{},
};

Expand Down Expand Up @@ -172,54 +175,7 @@ pub const BinaryFormat = union(enum) {
dfu,

/// The [USB Flashing Format (UF2)](https://github.com/microsoft/uf2) designed by Microsoft.
uf2: enum {
ATMEGA32,
SAML21,
NRF52,
ESP32,
STM32L1,
STM32L0,
STM32WL,
LPC55,
STM32G0,
GD32F350,
STM32L5,
STM32G4,
MIMXRT10XX,
STM32F7,
SAMD51,
STM32F4,
FX2,
STM32F2,
STM32F1,
NRF52833,
STM32F0,
SAMD21,
STM32F3,
STM32F407,
STM32H7,
STM32WB,
ESP8266,
KL32L2,
STM32F407VG,
NRF52840,
ESP32S2,
ESP32S3,
ESP32C3,
ESP32C2,
ESP32H2,
RP2040,
RP2XXX_ABSOLUTE,
RP2XXX_DATA,
RP2350_ARM_S,
RP2350_RISC_V,
RP2350_ARM_NS,
STM32L4,
GD32VF103,
CSK4,
CSK6,
M0SENSE,
},
uf2: uf2.FamilyId,

/// The [firmware format](https://docs.espressif.com/projects/esptool/en/latest/esp32/advanced-topics/firmware-image-format.html) used by the [esptool](https://github.com/espressif/esptool) bootloader.
esp,
Expand Down
1 change: 1 addition & 0 deletions build-internals/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.version = "0.0.0",
.dependencies = .{
.regz = .{ .path = "../tools/regz" },
.uf2 = .{ .path = "../tools/uf2" },
},
.paths = .{
"LICENSE",
Expand Down
1 change: 1 addition & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ pub fn MicroBuild(port_select: PortSelect) type {
.imports = options.imports,
});
app_mod.addImport("microzig", core_mod);
core_mod.addImport("app", app_mod);

const fw = mb.builder.allocator.create(Firmware) catch @panic("out of memory");
fw.* = .{
Expand Down
4 changes: 2 additions & 2 deletions examples/raspberrypi/rp2xxx/src/changing_system_clocks.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ const Pin = rp2xxx.gpio.Pin;

/// The HAL provides a convenvience function for detecting which of the RP2XXX
/// family you're currently compiling for.
const cpu = rp2xxx.compatibility.cpu;
const chip = rp2xxx.compatibility.chip;

// Use the system() preset helper to change the SYS and REF clock frequencies from default
const system_clock_cfg = clocks.config.preset.system(
// Reduce the system clock by a factor of 4 (different default clock speeds for RP2350/RP2040)
switch (cpu) {
switch (chip) {
.RP2040 => 125_000_000 / 4,
.RP2350 => 150_000_000 / 4,
},
Expand Down
4 changes: 2 additions & 2 deletions examples/raspberrypi/rp2xxx/src/custom_clock_config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const Pin = gpio.Pin;

/// The HAL provides a convenvience function for detecting which of the RP2XXX
/// family you're currently compiling for.
const cpu = rp2xxx.compatibility.cpu;
const chip = rp2xxx.compatibility.chip;

const xosc_freq = microzig.board.xosc_freq;

Expand Down Expand Up @@ -84,7 +84,7 @@ const system_clock_cfg: GlobalConfig = val: {
};

// GlobalConfig has a slight difference between RP2350 (has HSTX) and RP2040 (has RTC)
switch (cpu) {
switch (chip) {
.RP2040 => {
// Drive RTC off XOSC / 3 for a change of pace!
temp.rtc = .{
Expand Down
4 changes: 2 additions & 2 deletions examples/raspberrypi/rp2xxx/src/rp2040_only/adc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const rp2xxx = microzig.hal;
const gpio = rp2xxx.gpio;
const adc = rp2xxx.adc;
const time = rp2xxx.time;
const cpu = rp2xxx.compatibility.cpu;
const chip = rp2xxx.compatibility.chip;

const uart = rp2xxx.uart.instance.num(0);
const baud_rate = 115200;
Expand All @@ -22,7 +22,7 @@ pub fn main() void {
.temp_sensor_enabled = true,
});

switch (cpu) {
switch (chip) {
.RP2040 => inline for (&.{ uart_tx_pin, uart_rx_pin }) |pin| {
pin.set_function(.uart);
},
Expand Down
4 changes: 2 additions & 2 deletions examples/raspberrypi/rp2xxx/src/rp2040_only/flash_id.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const rp2xxx = microzig.hal;
const time = rp2xxx.time;
const gpio = rp2xxx.gpio;
const flash = rp2xxx.flash;
const cpu = rp2xxx.compatibility.cpu;
const chip = rp2xxx.compatibility.chip;

const uart = rp2xxx.uart.instance.num(0);
const baud_rate = 115200;
Expand All @@ -24,7 +24,7 @@ pub const std_options = struct {
};

pub fn main() !void {
switch (cpu) {
switch (chip) {
.RP2040 => inline for (&.{ uart_tx_pin, uart_rx_pin }) |pin| {
pin.set_function(.uart);
},
Expand Down
4 changes: 2 additions & 2 deletions examples/raspberrypi/rp2xxx/src/rp2040_only/flash_program.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const flash = rp2xxx.flash;
const time = rp2xxx.time;
const gpio = rp2xxx.gpio;
const clocks = rp2xxx.clocks;
const cpu = rp2xxx.compatibility.cpu;
const chip = rp2xxx.compatibility.chip;

const led = gpio.num(25);
const uart = rp2xxx.uart.instance.num(0);
Expand All @@ -33,7 +33,7 @@ pub fn main() !void {
led.set_direction(.out);
led.put(1);

switch (cpu) {
switch (chip) {
.RP2040 => inline for (&.{ uart_tx_pin, uart_rx_pin }) |pin| {
pin.set_function(.uart);
},
Expand Down
4 changes: 2 additions & 2 deletions examples/raspberrypi/rp2xxx/src/rp2040_only/i2c_bus_scan.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const rp2xxx = microzig.hal;
const i2c = rp2xxx.i2c;
const gpio = rp2xxx.gpio;
const peripherals = microzig.chip.peripherals;
const cpu = rp2xxx.compatibility.cpu;
const chip = rp2xxx.compatibility.chip;

pub const microzig_options = .{
.log_level = .info,
Expand All @@ -20,7 +20,7 @@ const uart_rx_pin = gpio.num(1);
const i2c0 = i2c.instance.num(0);

pub fn main() !void {
switch (cpu) {
switch (chip) {
.RP2040 => inline for (&.{ uart_tx_pin, uart_rx_pin }) |pin| {
pin.set_function(.uart);
},
Expand Down
4 changes: 2 additions & 2 deletions examples/raspberrypi/rp2xxx/src/rp2040_only/random.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const time = rp2xxx.time;
const gpio = rp2xxx.gpio;
const clocks = rp2xxx.clocks;
const rand = rp2xxx.rand;
const cpu = rp2xxx.compatibility.cpu;
const chip = rp2xxx.compatibility.chip;

const led = gpio.num(25);
const uart = rp2xxx.uart.instance.num(0);
Expand All @@ -33,7 +33,7 @@ pub fn main() !void {
led.set_direction(.out);
led.put(1);

switch (cpu) {
switch (chip) {
.RP2040 => inline for (&.{ uart_tx_pin, uart_rx_pin }) |pin| {
pin.set_function(.uart);
},
Expand Down
4 changes: 2 additions & 2 deletions examples/raspberrypi/rp2xxx/src/rp2040_only/uart_log.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const rp2xxx = microzig.hal;
const time = rp2xxx.time;
const gpio = rp2xxx.gpio;
const clocks = rp2xxx.clocks;
const cpu = rp2xxx.compatibility.cpu;
const chip = rp2xxx.compatibility.chip;

const led = gpio.num(25);
const uart = rp2xxx.uart.instance.num(0);
Expand All @@ -29,7 +29,7 @@ pub fn main() !void {
led.set_direction(.out);
led.put(1);

switch (cpu) {
switch (chip) {
.RP2040 => inline for (&.{ uart_tx_pin, uart_rx_pin }) |pin| {
pin.set_function(.uart);
},
Expand Down
4 changes: 2 additions & 2 deletions examples/raspberrypi/rp2xxx/src/rp2040_only/usb_cdc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const time = rp2xxx.time;
const gpio = rp2xxx.gpio;
const clocks = rp2xxx.clocks;
const usb = rp2xxx.usb;
const cpu = rp2xxx.compatibility.cpu;
const chip = rp2xxx.compatibility.chip;

const led = gpio.num(25);
const uart = rp2xxx.uart.instance.num(0);
Expand Down Expand Up @@ -69,7 +69,7 @@ pub fn main() !void {
led.set_direction(.out);
led.put(1);

switch (cpu) {
switch (chip) {
.RP2040 => inline for (&.{ uart_tx_pin, uart_rx_pin }) |pin| {
pin.set_function(.uart);
},
Expand Down
4 changes: 2 additions & 2 deletions examples/raspberrypi/rp2xxx/src/rp2040_only/usb_hid.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const time = rp2xxx.time;
const gpio = rp2xxx.gpio;
const clocks = rp2xxx.clocks;
const usb = rp2xxx.usb;
const cpu = rp2xxx.compatibility.cpu;
const chip = rp2xxx.compatibility.chip;

const led = gpio.num(25);
const uart = rp2xxx.uart.instance.num(0);
Expand Down Expand Up @@ -71,7 +71,7 @@ pub fn main() !void {
led.set_direction(.out);
led.put(1);

switch (cpu) {
switch (chip) {
.RP2040 => inline for (&.{ uart_tx_pin, uart_rx_pin }) |pin| {
pin.set_function(.uart);
},
Expand Down
61 changes: 57 additions & 4 deletions port/raspberrypi/rp2xxx/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ const Self = @This();
chips: struct {
rp2040: *const microzig.Target,
rp2350_arm: *const microzig.Target,
rp2350_riscv: *const microzig.Target,
},

boards: struct {
raspberrypi: struct {
pico: *const microzig.Target,
pico2_arm: *const microzig.Target,
pico2_riscv: *const microzig.Target,
},
waveshare: struct {
rp2040_plus_4m: *const microzig.Target,
Expand Down Expand Up @@ -51,7 +53,7 @@ pub fn init(dep: *std.Build.Dependency) Self {
.linker_script = b.path("rp2040.ld"),
};

const chip_rp2350: microzig.Target = .{
const chip_rp2350_arm: microzig.Target = .{
.dep = dep,
.preferred_binary_format = .{ .uf2 = .RP2350_ARM_S },
.chip = .{
Expand All @@ -63,14 +65,57 @@ pub fn init(dep: *std.Build.Dependency) Self {
.abi = .eabi,
},
.register_definition = .{ .svd = b.path("src/chips/rp2350.svd") },
.memory_regions = &.{
.{ .kind = .flash, .offset = 0x10000000, .length = 2048 * 1024 },
.{ .kind = .ram, .offset = 0x20000000, .length = 256 * 1024 },
},
},
.hal = hal,
.linker_script = b.path("rp2350_arm.ld"),
};

const chip_rp2350_riscv: microzig.Target = .{
.dep = dep,
.preferred_binary_format = .{ .uf2 = .RP2350_RISC_V },
.chip = .{
.name = "RP2350",
.cpu = std.Target.Query{
.cpu_arch = .riscv32,
.cpu_model = .{ .explicit = &std.Target.riscv.cpu.generic_rv32 },
// the commented ones are in the datasheet but for some reason they break the code
.cpu_features_add = std.Target.riscv.featureSet(&.{
// std.Target.riscv.Feature.c,
std.Target.riscv.Feature.a,
std.Target.riscv.Feature.m,
std.Target.riscv.Feature.zba,
std.Target.riscv.Feature.zbb,
std.Target.riscv.Feature.zbs,
// std.Target.riscv.Feature.zcb,
// std.Target.riscv.Feature.zcmp,
std.Target.riscv.Feature.zbkb,
std.Target.riscv.Feature.zifencei,
}),
.os_tag = .freestanding,
.abi = .eabi,
},
.cpu_module_file = b.path("src/cpus/hazard3.zig"),
.register_definition = .{ .svd = b.path("src/chips/rp2350.svd") },
.memory_regions = &.{
.{ .kind = .flash, .offset = 0x10000100, .length = (2048 * 1024) - 256 },
.{ .kind = .flash, .offset = 0x10000000, .length = 256 },
.{ .kind = .ram, .offset = 0x20000000, .length = 256 * 1024 },
},
.patches = &.{
.{
.override_arch = .{
.device_name = "RP2350",
.arch = .hazard3,
},
},
},
},
.hal = hal,
.linker_script = b.path("rp2350.ld"),
.linker_script = b.path("rp2350_riscv.ld"),
};

const bootrom_rp2040 = get_bootrom(b, chip_rp2040.chip, .w25q080);
Expand All @@ -81,7 +126,8 @@ pub fn init(dep: *std.Build.Dependency) Self {
return .{
.chips = .{
.rp2040 = chip_rp2040.derive(.{}),
.rp2350_arm = chip_rp2350.derive(.{}),
.rp2350_arm = chip_rp2350_arm.derive(.{}),
.rp2350_riscv = chip_rp2350_riscv.derive(.{}),
},
.boards = .{
.raspberrypi = .{
Expand All @@ -93,7 +139,14 @@ pub fn init(dep: *std.Build.Dependency) Self {
.imports = rp2040_bootrom_imports,
},
}),
.pico2_arm = chip_rp2350.derive(.{
.pico2_arm = chip_rp2350_arm.derive(.{
.board = .{
.name = "RaspberryPi Pico 2",
.url = "https://www.raspberrypi.com/products/raspberry-pi-pico2/",
.root_source_file = b.path("src/boards/raspberry_pi_pico2.zig"),
},
}),
.pico2_riscv = chip_rp2350_riscv.derive(.{
.board = .{
.name = "RaspberryPi Pico 2",
.url = "https://www.raspberrypi.com/products/raspberry-pi-pico2/",
Expand Down
3 changes: 2 additions & 1 deletion port/raspberrypi/rp2xxx/build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"patches",
"tools",
"rp2040.ld",
"rp2350.ld",
"rp2350_arm.ld",
"rp2350_riscv.ld",
},
}
Loading
Loading