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 support for assembling RP2350-specific PIO #320

Merged
merged 31 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
0f42b3b
rp2xxx: Update PIO for rp2350
Grazfather Dec 16, 2024
59cdab0
- Reorganized some of the comptime behavior surrounding CPU
Dec 19, 2024
35f945a
Get pio comparison tests running on both cpu types
Grazfather Dec 19, 2024
fd8fbf2
Fix PIO sm_set_shift_options
Grazfather Dec 19, 2024
6926292
s/format/cpu
Grazfather Dec 19, 2024
dc2b012
better error in tokenizer tests
Grazfather Dec 19, 2024
15474cd
Test expected index in define expect_define
Grazfather Dec 19, 2024
0fc40b4
More tests for both cpus
Grazfather Dec 19, 2024
0a45bd6
Add jmppin as valid source for wait
Grazfather Dec 19, 2024
97feb30
Test mov to pindirs
Grazfather Dec 19, 2024
0742044
cleanup irq comp test
Grazfather Dec 19, 2024
7e8f487
wip movrx
Grazfather Dec 20, 2024
7a04501
mov to rx working
Grazfather Dec 20, 2024
7ca9cc2
Add movrx comparison tests
Grazfather Dec 20, 2024
4117ce3
put ws2812 in both chips
Grazfather Jan 6, 2025
ac53836
wip: print diag and error
Grazfather Jan 6, 2025
4852d4b
Fix error handling in rp2xxx pio assembler
Grazfather Jan 6, 2025
3f6d84c
Repro issue in diags
Grazfather Jan 7, 2025
b062108
Fix diag issue
Grazfather Jan 7, 2025
1a41680
Cleanup diags
Grazfather Jan 7, 2025
d12afdd
improve movtorx parsing and diag
Grazfather Jan 7, 2025
bb248a0
Get movfromrx encoding and most tokenization working
Grazfather Jan 7, 2025
d66e9b8
Get tokenizing mov from rx to work
Grazfather Jan 7, 2025
dc3b857
wip: Allow pio assemble to work at runtime
Grazfather Jan 7, 2025
eef3e79
Revert "wip: Allow pio assemble to work at runtime"
Grazfather Jan 16, 2025
e807673
Merge remote-tracking branch 'origin/main' into rp2350_pio_assembler
Grazfather Jan 16, 2025
cc38cd0
irq rel
Grazfather Jan 16, 2025
148ae16
Fix mov from idx
Grazfather Jan 16, 2025
9186848
cleanup
Grazfather Jan 16, 2025
52ed8a1
fix
Grazfather Jan 16, 2025
2757bff
Some cleanup
Grazfather Jan 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions port/raspberrypi/rp2xxx/src/hal/chip.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub const Chip = enum {
RP2040,
RP2350,
};
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 Chip = enum {
RP2040,
RP2350,
};
const Chip = @import("chip.zig").Chip;

pub const chip: Chip = blk: {
if (std.mem.eql(u8, microzig.config.chip_name, "RP2040")) {
Expand Down
6 changes: 4 additions & 2 deletions port/raspberrypi/rp2xxx/src/hal/pio.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const chip_specific = switch (chip) {
.RP2350 => @import("pio/rp2350.zig"),
};
pub const StateMachine = common.StateMachine;
pub const Instruction = common.Instruction;
pub const Instruction = common.Instruction(chip);
pub const PinMapping = common.PinMapping;
pub const PinMappingOptions = common.PinMappingOptions;
pub const StateMachineInitOptions = chip_specific.StateMachineInitOptions;
Expand All @@ -24,7 +24,9 @@ pub const assembler = @import("pio/assembler.zig");
const encoder = @import("pio/assembler/encoder.zig");

pub const Program = assembler.Program;
pub const assemble = assembler.assemble;
pub inline fn assemble(comptime source: []const u8, comptime options: assembler.AssembleOptions) assembler.Output {
return assembler.assemble(chip, source, options);
}

pub fn num(n: u2) Pio {
switch (chip) {
Expand Down
11 changes: 6 additions & 5 deletions port/raspberrypi/rp2xxx/src/hal/pio/assembler.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const std = @import("std");
const assert = std.debug.assert;

const Chip = @import("../chip.zig").Chip;
const tokenizer = @import("assembler/tokenizer.zig");
const encoder = @import("assembler/encoder.zig");

Expand Down Expand Up @@ -71,9 +72,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 chip: Chip, comptime source: []const u8, diags: *?Diagnostics, options: AssembleOptions) !Output {
const tokens = try tokenizer.tokenize(chip, source, diags, options.tokenize);
const encoder_output = try encoder.encode(chip, 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 +122,9 @@ fn format_compile_error(comptime message: []const u8, comptime source: []const u
});
}

pub fn assemble(comptime source: []const u8, comptime options: AssembleOptions) Output {
pub fn assemble(comptime chip: Chip, comptime source: []const u8, comptime options: AssembleOptions) Output {
var diags: ?Diagnostics = null;
return assemble_impl(source, &diags, options) catch |err| {
return assemble_impl(chip, source, &diags, options) catch |err| {
if (diags) |d|
@compileError(format_compile_error(d.message.slice(), source, d.index));
@compileError(@errorName(err));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const std = @import("std");
const assembler = @import("../assembler.zig");
const tokenizer = @import("tokenizer.zig");
const Chip = @import("../../chip.zig").Chip;

const c = @cImport({
@cDefine("PICO_NO_HARDWARE", "1");
@cInclude("stdint.h");
Expand All @@ -12,7 +14,9 @@ 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/movrx.pio.h");
@cInclude("comparison_tests/nec_carrier_burst.pio.h");
@cInclude("comparison_tests/nec_carrier_control.pio.h");
@cInclude("comparison_tests/nec_receive.pio.h");
Expand All @@ -31,7 +35,13 @@ const c = @cImport({
});

fn pio_comparison(comptime source: []const u8) !void {
const output = comptime assembler.assemble(source, .{});
inline for (comptime .{ Chip.RP2040, Chip.RP2350 }) |chip| {
try pio_comparison_chip(chip, source);
}
}

fn pio_comparison_chip(comptime chip: Chip, comptime source: []const u8) !void {
const output = comptime assembler.assemble(chip, source, .{});
try std.testing.expect(output.programs.len > 0);

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

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

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

test "pio.comparison.movrx" {
@setEvalBranchQuota(11000);
try pio_comparison_chip(.RP2350, @embedFile("comparison_tests/movrx.pio"));
}

test "pio.comparison.nec_carrier_burst" {
@setEvalBranchQuota(6000);
try pio_comparison(@embedFile("comparison_tests/nec_carrier_burst.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
irq set 1 rel side 0
irq set 1 next side 0
irq wait 1 prev side 0
irq wait 1 rel side 0
irq wait 1 next side 0
irq clear 1 prev side 0
irq clear 1 rel side 0
irq clear 1 next side 0
.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
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.program movrx

.wrap_target
mov rxfifoy, isr
mov rxfifo0, isr
mov rxfifo1, isr
mov rxfifo2, isr
mov rxfifo3, isr

mov osr, rxfifoy
mov osr, rxfifo0
mov osr, rxfifo1
mov osr, rxfifo2
mov osr, rxfifo3
.wrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

static const uint16_t movrx_program_instructions[] = {
// 0b1000_ssss_0001_yiii
0x8018, // mov rxfifoy, isr
0x8010, // mov rxfifo0, isr
0x8011, // mov rxfifo1, isr
0x8012, // mov rxfifo2, isr
0x8013, // mov rxfifo3, isr
// 0b1000_ssss_1001_yiii
0x8098, // mov osr, rxfifoy
0x8090, // mov osr, rxfifo0
0x8091, // mov osr, rxfifo1
0x8092, // mov osr, rxfifo2
0x8093, // mov osr, rxfifo3
};
Loading
Loading