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

Draft
wants to merge 24 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
50 changes: 24 additions & 26 deletions examples/raspberrypi/rp2xxx/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,38 @@ 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_rtc", .file = "src/rp2040_only/rtc.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_rtc", .file = "src/rp2040_only/rtc.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
};
const rp2350_only_examples: []const Example = &.{};

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,
};
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 (cpu) {
.RP2350 => @import("pio/rp2350.zig"),
};
pub const StateMachine = common.StateMachine;
pub const Instruction = common.Instruction;
pub const Instruction = common.Instruction(cpu);
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(cpu, source, options);
}

pub fn num(n: u2) Pio {
switch (cpu) {
Expand Down
34 changes: 22 additions & 12 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 CPU = @import("../cpu.zig").CPU;
const tokenizer = @import("assembler/tokenizer.zig");
const encoder = @import("assembler/encoder.zig");

Expand All @@ -9,7 +10,9 @@ pub const EncodeOptions = encoder.Options;

pub const Define = struct {
name: []const u8,
value: i64,
// NO idea why this has to change
value: i128,
// value: i64,
};

pub const Program = struct {
Expand Down Expand Up @@ -71,12 +74,16 @@ 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 cpu: CPU, source: []const u8, diags: *?Diagnostics, comptime options: AssembleOptions) !Output {
const tokens = try tokenizer.tokenize(cpu, source, diags, options.tokenize);
for (tokens.slice()) |t| // DELETEME
std.debug.print("Got tokens: {any}\n", .{t}); // DELETEME
const encoder_output = try encoder.encode(cpu, 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());
for (encoder_output.programs.slice()) |bounded| {
const prog = try bounded.to_exported_program();
try programs.append(prog);
}

return Output{
.defines = blk: {
Expand All @@ -99,7 +106,9 @@ fn format_compile_error(comptime message: []const u8, comptime source: []const u
var line_it = std.mem.tokenize(u8, source, "\n\r");
while (line_it.next()) |line| : (line_num += 1) {
line_str = line_str ++ "\n" ++ line;
if (line_it.index > index) {
// If the line iterator is overlapping the provided index, then we are on the correct line
if (line_it.index >= index) {
// Calculate the column
column = line.len - (line_it.index - index);
line_str = line;
break;
Expand All @@ -121,12 +130,13 @@ 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 cpu: CPU, comptime source: []const u8, comptime options: AssembleOptions) Output {
var diags: ?Diagnostics = null;
return assemble_impl(source, &diags, options) catch |err| if (diags) |d|
@compileError(format_compile_error(d.message.slice(), source, d.index))
else
@compileError(err);
return assemble_impl(cpu, source, &diags, options) catch |err| {
if (diags) |d|
@compileError(format_compile_error(d.message.slice(), source, d.index));
@compileError(@errorName(err));
};
}

test "tokenizer and encoder" {
Expand Down
Loading