Skip to content

Commit

Permalink
rp2xxx: Update PIO for rp2350
Browse files Browse the repository at this point in the history
  • Loading branch information
Grazfather committed Dec 19, 2024
1 parent cca83d5 commit 74b91f9
Show file tree
Hide file tree
Showing 11 changed files with 1,589 additions and 1,336 deletions.
46 changes: 23 additions & 23 deletions examples/raspberrypi/rp2xxx/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,39 @@ pub fn build(b: *std.Build) void {

const rp2040_only_examples: []const Example = &.{
// RaspberryPi Boards:
.{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "pico_adc", .file = "src/rp2040_only/adc.zig" },
.{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "pico_flash-program", .file = "src/rp2040_only/flash_program.zig" },
.{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "pico_flash-id", .file = "src/rp2040_only/flash_id.zig" },
.{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "pico_i2c-bus-scan", .file = "src/rp2040_only/i2c_bus_scan.zig" },
.{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "pico_pwm", .file = "src/rp2040_only/pwm.zig" },
.{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "pico_random", .file = "src/rp2040_only/random.zig" },
.{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "pico_spi-host", .file = "src/rp2040_only/spi_host.zig" },
.{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "pico_uart-echo", .file = "src/rp2040_only/uart_echo.zig" },
.{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "pico_uart-log", .file = "src/rp2040_only/uart_log.zig" },
.{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "pico_usb-hid", .file = "src/rp2040_only/usb_hid.zig" },
.{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "pico_usb-cdc", .file = "src/rp2040_only/usb_cdc.zig" },
.{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "pico_multicore", .file = "src/rp2040_only/blinky_core1.zig" },
.{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "pico_hd44780", .file = "src/rp2040_only/hd44780.zig" },
.{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "pico_pcf8574", .file = "src/rp2040_only/pcf8574.zig" },

// WaveShare Boards:
.{ .target = mb.ports.rp2xxx.boards.waveshare.rp2040_matrix, .name = "rp2040-matrix_tiles", .file = "src/rp2040_only/tiles.zig" },
// .{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "pico_adc", .file = "src/rp2040_only/adc.zig" },
// .{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "pico_flash-program", .file = "src/rp2040_only/flash_program.zig" },
// .{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "pico_flash-id", .file = "src/rp2040_only/flash_id.zig" },
// .{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "pico_i2c-bus-scan", .file = "src/rp2040_only/i2c_bus_scan.zig" },
// .{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "pico_pwm", .file = "src/rp2040_only/pwm.zig" },
// .{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "pico_random", .file = "src/rp2040_only/random.zig" },
// .{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "pico_spi-host", .file = "src/rp2040_only/spi_host.zig" },
// .{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "pico_uart-echo", .file = "src/rp2040_only/uart_echo.zig" },
// .{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "pico_uart-log", .file = "src/rp2040_only/uart_log.zig" },
// .{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "pico_usb-hid", .file = "src/rp2040_only/usb_hid.zig" },
// .{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "pico_usb-cdc", .file = "src/rp2040_only/usb_cdc.zig" },
// .{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "pico_multicore", .file = "src/rp2040_only/blinky_core1.zig" },
// .{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "pico_hd44780", .file = "src/rp2040_only/hd44780.zig" },
// .{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico, .name = "pico_pcf8574", .file = "src/rp2040_only/pcf8574.zig" },
//
// // WaveShare Boards:
// .{ .target = mb.ports.rp2xxx.boards.waveshare.rp2040_matrix, .name = "rp2040-matrix_tiles", .file = "src/rp2040_only/tiles.zig" },
// .{ .target = "board:waveshare/rp2040_eth", .name = "rp2040-eth" },
// .{ .target = "board:waveshare/rp2040_plus_4m", .name = "rp2040-plus-4m" },
// .{ .target = "board:waveshare/rp2040_plus_16m", .name = "rp2040-plus-16m" },
};

const rp2350_only_examples: []const Example = &.{
// TODO: No RP2350 feature specific examples to show off yet
.{ .target = mb.ports.rp2xxx.boards.raspberrypi.pico2_arm, .name = "ws2812", .file = "src/ws2812.zig" },
};

const chip_agnostic_examples: []const ChipAgnosticExample = &.{
.{ .name = "squarewave", .file = "src/squarewave.zig" },
.{ .name = "ws2812", .file = "src/ws2812.zig" },
.{ .name = "blinky", .file = "src/blinky.zig" },
.{ .name = "gpio-clock-output", .file = "src/gpio_clock_output.zig" },
.{ .name = "changing-system-clocks", .file = "src/changing_system_clocks.zig" },
.{ .name = "custom-clock-config", .file = "src/custom_clock_config.zig" },
// .{ .name = "squarewave", .file = "src/squarewave.zig" },
// .{ .name = "blinky", .file = "src/blinky.zig" },
// .{ .name = "gpio-clock-output", .file = "src/gpio_clock_output.zig" },
// .{ .name = "changing-system-clocks", .file = "src/changing_system_clocks.zig" },
// .{ .name = "custom-clock-config", .file = "src/custom_clock_config.zig" },
};

var available_examples = std.ArrayList(Example).init(b.allocator);
Expand Down
6 changes: 1 addition & 5 deletions port/raspberrypi/rp2xxx/src/hal/compatibility.zig
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
const std = @import("std");
const microzig = @import("microzig");

pub const CPU = enum {
RP2040,
RP2350,
};
const CPU = @import("cpu.zig").CPU;

pub const cpu: CPU = blk: {
if (std.mem.eql(u8, microzig.config.chip_name, "RP2040")) {
Expand Down
4 changes: 4 additions & 0 deletions port/raspberrypi/rp2xxx/src/hal/cpu.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub const CPU = enum {
RP2040,
RP2350,
};
26 changes: 22 additions & 4 deletions port/raspberrypi/rp2xxx/src/hal/pio/assembler.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const std = @import("std");
const assert = std.debug.assert;

const CPU = @import("../cpu.zig").CPU;
// TODO: Isn't this circular?
const tokenizer = @import("assembler/tokenizer.zig");
const encoder = @import("assembler/encoder.zig");

Expand Down Expand Up @@ -71,9 +73,9 @@ pub const Diagnostics = struct {
}
};

pub fn assemble_impl(comptime source: []const u8, diags: *?Diagnostics, options: AssembleOptions) !Output {
const tokens = try tokenizer.tokenize(source, diags, options.tokenize);
const encoder_output = try encoder.encode(tokens.slice(), diags, options.encode);
pub fn assemble_impl(comptime format: Format, comptime source: []const u8, diags: *?Diagnostics, options: AssembleOptions) !Output {
const tokens = try tokenizer.tokenize(format, source, diags, options.tokenize);
const encoder_output = try encoder.encode(format, tokens.slice(), diags, options.encode);
var programs = std.BoundedArray(Program, options.encode.max_programs).init(0) catch unreachable;
for (encoder_output.programs.slice()) |bounded|
try programs.append(bounded.to_exported_program());
Expand Down Expand Up @@ -121,9 +123,25 @@ fn format_compile_error(comptime message: []const u8, comptime source: []const u
});
}

// TODO: Place. Who should own this?
pub const Format = enum {
RP2040,
RP2350,
};

pub fn cpuToFormat(cpu: CPU) Format {
return switch (cpu) {
.RP2040 => .RP2040,
.RP2350 => .RP2350,
};
}

pub fn assemble(comptime source: []const u8, comptime options: AssembleOptions) Output {
var diags: ?Diagnostics = null;
return assemble_impl(source, &diags, options) catch |err| if (diags) |d|
// TODO: We can't import compatibility & zig build test since it depends on microzig
// cpuToFormat(cpu) instead of hardcoding
// const cpu = @import("../compatibility.zig").cpu;
return assemble_impl(.RP2040, source, &diags, options) catch |err| if (diags) |d|
@compileError(format_compile_error(d.message.slice(), source, d.index))
else
@compileError(err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const c = @cImport({
@cInclude("comparison_tests/hello.pio.h");
@cInclude("comparison_tests/hub75.pio.h");
@cInclude("comparison_tests/i2c.pio.h");
@cInclude("comparison_tests/irq.pio.h");
@cInclude("comparison_tests/manchester_encoding.pio.h");
@cInclude("comparison_tests/nec_carrier_burst.pio.h");
@cInclude("comparison_tests/nec_carrier_control.pio.h");
Expand All @@ -31,7 +32,8 @@ const c = @cImport({
});

fn pio_comparison(comptime source: []const u8) !void {
const output = comptime assembler.assemble(source, .{});
comptime var diags: ?assembler.Diagnostics = null;
const output = try comptime assembler.assemble_impl(.RP2040, source, &diags, .{});
try std.testing.expect(output.programs.len > 0);

inline for (output.programs) |program| {
Expand Down Expand Up @@ -87,6 +89,11 @@ test "pio.comparison.i2c" {
try pio_comparison(@embedFile("comparison_tests/i2c.pio"));
}

test "pio.comparison.irq" {
@setEvalBranchQuota(22000);
try pio_comparison(@embedFile("comparison_tests/irq.pio"));
}

test "pio.comparison.manchester_encoding" {
@setEvalBranchQuota(11000);
try pio_comparison(@embedFile("comparison_tests/manchester_encoding.pio"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# PIO example programs for testing

These were all taken from [the official pico examples repo](https://github.com/raspberrypi/pico-examples).
These were all taken from [the official pico examples
repo](https://github.com/raspberrypi/pico-examples).

The headers are generated using `pioasm`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.program irq
.side_set 1

.wrap_target
irq set 1 prev side 0 ; DELETEME
irq set 1 rel side 0 ; DELETEME
irq set 1 next side 0 ; DELETEME
irq wait 1 prev side 0 ; DELETEME
irq wait 1 rel side 0 ; DELETEME
irq wait 1 next side 0 ; DELETEME
irq clear 1 prev side 0 ; DELETEME
irq clear 1 rel side 0 ; DELETEME
irq clear 1 next side 0 ; DELETEME
.wrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

// TODO: Exercise more? delays, optional sideset, etc?
static const uint16_t irq_program_instructions[] = {
0xc009, // irq set 1 prev side 0
0xc011, // irq set 1 rel side 0
0xc019, // irq set 1 next side 0
0xc029, // irq wait 1 prev side 0
0xc031, // irq wait 1 rel side 0
0xc039, // irq wait 1 next side 0
0xc049, // irq clear 1 prev side 0
0xc051, // irq clear 1 rel side 0
0xc059, // irq clear 1 next side 0
};
Loading

0 comments on commit 74b91f9

Please sign in to comment.