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

Merge DMA code into main #62

Merged
merged 3 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 5 additions & 7 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ const Build = std.Build;
const MicroZig = @import("microzig/build");
const atsam = @import("microzig/bsp/microchip/atsam");

pub const py_badge: MicroZig.Target = .{
.preferred_format = .elf,
.chip = atsam.chips.atsamd51j19,
.hal = null,
};

fn sycl_badge_microzig_target(d: *Build.Dependency) MicroZig.Target {
var atsamd51j19_chip_with_fpu = atsam.chips.atsamd51j19.chip;
atsamd51j19_chip_with_fpu.cpu.target.cpu_features_add = std.Target.arm.featureSet(&.{.vfp4d16sp});
atsamd51j19_chip_with_fpu.cpu.target.abi = .eabihf;
return .{
.preferred_format = .elf,
.chip = atsam.chips.atsamd51j19.chip,
.chip = atsamd51j19_chip_with_fpu,
.hal = .{
.root_source_file = d.builder.path("src/hal.zig"),
},
Expand Down Expand Up @@ -106,6 +103,7 @@ pub fn build(b: *Build) void {
}

inline for (.{
"draw",
"neopixels",
"song",
}) |name| {
Expand Down
155 changes: 4 additions & 151 deletions src/badge.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const cart = @import("badge/cart.zig");

const led_pin = board.D13;

const Lcd = board.Lcd;
const lcd = board.lcd;
const ButtonPoller = board.ButtonPoller;
const light_sensor_pin = microzig.board.A7_LIGHT;
const audio = board.audio;
Expand All @@ -52,6 +52,7 @@ pub const microzig_options = .{
};

pub fn main() !void {
// Enable safety traps
SystemControl.CCR.modify(.{
.NONBASETHRDENA = 0,
.USERSETMPEND = 0,
Expand All @@ -60,11 +61,7 @@ pub fn main() !void {
.BFHFNMIGN = 0,
.STKALIGN = .{ .value = .VALUE_1 },
});
SystemControl.SHCSR.modify(.{
.MEMFAULTENA = 1,
.BUSFAULTENA = 1,
.USGFAULTENA = 1,
});
// Enable FPU access.
SystemControl.CPACR.write(.{
.reserved20 = 0,
.CP10 = .{ .value = .FULL },
Expand Down Expand Up @@ -220,7 +217,6 @@ pub fn main() !void {

timer.init();
audio.init();
init_frame_sync();

// Light sensor adc
light_sensor_pin.set_mux(.B);
Expand All @@ -229,29 +225,7 @@ pub fn main() !void {
const freqs = clocks.Frequencies.get(state);
_ = freqs;

const lcd = Lcd.init(.{
.spi = sercom.spi.Master.init(.SERCOM4, .{
.cpha = .LEADING_EDGE,
.cpol = .IDLE_LOW,
.dord = .MSB,
.dopo = .PAD2,
.ref_freq_hz = 120_000_000,
.baud_freq_hz = 12_000_000,
}),
.pins = .{
.rst = board.TFT_RST,
.lite = board.TFT_LITE,
.dc = board.TFT_DC,
.cs = board.TFT_CS,
.sck = board.TFT_SCK,
.mosi = board.TFT_MOSI,
},
.fb = .{
.bpp16 = @ptrCast(cart.api.framebuffer),
},
});

lcd.clear_screen(.{ .r = 0, .g = 0, .b = 0 });
lcd.init(.bpp16, @ptrCast(cart.api.framebuffer));

const neopixels = board.Neopixels.init(board.D8_NEOPIX);
adc.init();
Expand All @@ -260,9 +234,6 @@ pub fn main() !void {

cart.start();
while (true) {
//if (!frame_is_ready())
// continue;

const light_reading = adc.single_shot_blocking(.AIN6);
cart.api.light_level.* = @intCast(light_reading);

Expand All @@ -289,123 +260,5 @@ pub fn main() !void {
};

neopixels.write(&pixels);
led_pin.write(if (cart.api.red_led.*) .high else .low);
lcd.set_window(0, 0, 160, 128);
lcd.send_colors(@ptrCast(cart.api.framebuffer));
}
}

pub fn init_frame_sync() void {
TC4.COUNT16.CTRLA.write(.{
.SWRST = 0,
.ENABLE = 0,
.MODE = .{ .raw = 0 },
.PRESCSYNC = .{ .raw = 0 },
.RUNSTDBY = 0,
.ONDEMAND = 0,
.PRESCALER = .{ .raw = 0 },
.ALOCK = 0,
.reserved16 = 0,
.CAPTEN0 = 0,
.CAPTEN1 = 0,
.reserved20 = 0,
.COPEN0 = 0,
.COPEN1 = 0,
.reserved24 = 0,
.CAPTMODE0 = .{ .raw = 0 },
.reserved27 = 0,
.CAPTMODE1 = .{ .raw = 0 },
.padding = 0,
});
while (TC4.COUNT16.SYNCBUSY.read().ENABLE != 0) {}

TC4.COUNT16.CTRLA.write(.{
.SWRST = 1,
.ENABLE = 0,
.MODE = .{ .raw = 0 },
.PRESCSYNC = .{ .raw = 0 },
.RUNSTDBY = 0,
.ONDEMAND = 0,
.PRESCALER = .{ .raw = 0 },
.ALOCK = 0,
.reserved16 = 0,
.CAPTEN0 = 0,
.CAPTEN1 = 0,
.reserved20 = 0,
.COPEN0 = 0,
.COPEN1 = 0,
.reserved24 = 0,
.CAPTMODE0 = .{ .raw = 0 },
.reserved27 = 0,
.CAPTMODE1 = .{ .raw = 0 },
.padding = 0,
});
while (TC4.COUNT16.SYNCBUSY.read().SWRST != 0) {}
TC4.COUNT16.CTRLA.write(.{
.SWRST = 0,
.ENABLE = 0,
.MODE = .{ .value = .COUNT16 },
.PRESCSYNC = .{ .value = .PRESC },
.RUNSTDBY = 0,
.ONDEMAND = 0,
.PRESCALER = .{ .value = .DIV64 },
.ALOCK = 0,
.reserved16 = 0,
.CAPTEN0 = 0,
.CAPTEN1 = 0,
.reserved20 = 0,
.COPEN0 = 0,
.COPEN1 = 0,
.reserved24 = 0,
.CAPTMODE0 = .{ .raw = 0 },
.reserved27 = 0,
.CAPTMODE1 = .{ .raw = 0 },
.padding = 0,
});
TC4.COUNT16.WAVE.write(.{ .WAVEGEN = .{ .value = .MFRQ }, .padding = 0 });
TC4.COUNT16.CC[0].write(.{ .CC = @divExact(8_467_200, 64 * 60) - 1 });
while (TC4.COUNT16.SYNCBUSY.read().CC0 != 0) {}
TC4.COUNT16.CTRLA.write(.{
.SWRST = 0,
.ENABLE = 1,
.MODE = .{ .value = .COUNT16 },
.PRESCSYNC = .{ .value = .PRESC },
.RUNSTDBY = 0,
.ONDEMAND = 0,
.PRESCALER = .{ .value = .DIV64 },
.ALOCK = 0,
.reserved16 = 0,
.CAPTEN0 = 0,
.CAPTEN1 = 0,
.reserved20 = 0,
.COPEN0 = 0,
.COPEN1 = 0,
.reserved24 = 0,
.CAPTMODE0 = .{ .raw = 0 },
.reserved27 = 0,
.CAPTMODE1 = .{ .raw = 0 },
.padding = 0,
});
while (TC4.COUNT16.SYNCBUSY.read().ENABLE != 0) {}
TC4.COUNT16.CTRLBSET.write(.{
.DIR = 0,
.LUPD = 0,
.ONESHOT = 0,
.reserved5 = 0,
.CMD = .{ .value = .RETRIGGER },
});
while (TC4.COUNT16.SYNCBUSY.read().CTRLB != 0) {}
}

pub fn frame_is_ready() bool {
if (TC4.COUNT16.INTFLAG.read().OVF != 1) return false;
TC4.COUNT16.INTFLAG.write(.{
.OVF = 1,
.ERR = 0,
.reserved4 = 0,
.MC0 = 0,
.MC1 = 0,
.padding = 0,
});
return true;
}
25 changes: 14 additions & 11 deletions src/badge/cart.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const std = @import("std");
const microzig = @import("microzig");
const board = microzig.board;
const audio = board.audio;
const lcd = board.lcd;
const timer = microzig.hal.timer;
pub const api = @import("../cart/api.zig");

Expand All @@ -14,7 +15,10 @@ const libcart = struct {

extern fn start() void;
extern fn update() void;
extern fn __return_thunk__() noreturn;
export fn __return_thunk__() linksection(".text.cart") noreturn {
asm volatile (" svc #11");
unreachable;
}
};

pub fn svcall_handler() callconv(.Naked) void {
Expand Down Expand Up @@ -118,7 +122,7 @@ pub const HSRAM = struct {
};

pub fn start() void {
@memset(@as(*[0x19A0]u8, @ptrFromInt(0x20000000)), 0);
@memset(@as(*[0xA01E]u8, @ptrFromInt(0x20000000)), 0);
api.neopixels.* = .{
.{ .r = 0, .g = 0, .b = 0 },
.{ .r = 0, .g = 0, .b = 0 },
Expand Down Expand Up @@ -149,11 +153,10 @@ pub fn start() void {
call(&libcart.start);
}
pub fn tick() void {
// TODO: check if frame is ready

// read gamepad
//if (SYSTEM_FLAGS.* & SYSTEM_PRESERVE_FRAMEBUFFER == 0) @memset(FRAMEBUFFER, 0b00_00_00_00);
// non-rendering logic could go here
lcd.vsync();
call(&libcart.update);
lcd.update();
}

fn call(func: *const fn () callconv(.C) void) void {
Expand All @@ -178,11 +181,11 @@ fn call(func: *const fn () callconv(.C) void) void {
fn User(comptime T: type) type {
return extern struct {
const Self = @This();
const suffix = switch (@sizeOf(T)) {
1 => "b",
2 => "h",
4 => "",
else => @compileError("loadUser doesn't support " ++ @typeName(T)),
const suffix = switch (@bitSizeOf(T)) {
8 => "b",
16 => "h",
32 => "",
else => @compileError("User doesn't support " ++ @typeName(T)),
};

unsafe: T,
Expand Down
13 changes: 13 additions & 0 deletions src/badge/demos/draw.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const cart = @import("cart-api");

pub export fn start() void {}

var tick: u32 = 0;

pub export fn update() void {
@memset(cart.framebuffer, @bitCast(@as(u16, @bitCast(switch (@as(u1, @truncate(tick))) {
0 => cart.DisplayColor{ .r = 0x1f, .g = 0x00, .b = 0x00 },
1 => cart.DisplayColor{ .r = 0x00, .g = 0x3f, .b = 0x00 },
}))));
tick +%= 1;
}
34 changes: 3 additions & 31 deletions src/badge/demos/lcd.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const tft_dc_pin = board.TFT_DC;
const tft_cs_pin = board.TFT_CS;
const tft_sck_pin = board.TFT_SCK;
const tft_mosi_pin = board.TFT_MOSI;
const Lcd = board.Lcd;
const lcd = board.lcd;

var fb: [Lcd.width][Lcd.height]Lcd.Color16 = undefined;
var fb: [lcd.width][lcd.height]lcd.Color16 = undefined;

pub fn main() !void {
tft_rst_pin.set_dir(.out);
Expand Down Expand Up @@ -46,36 +46,8 @@ pub fn main() !void {
});

timer.init();
var lcd = Lcd.init(.{
.spi = sercom.spi.Master.init(.SERCOM4, .{
.cpha = .LEADING_EDGE,
.cpol = .IDLE_LOW,
.dord = .MSB,
.dopo = .PAD2,
.ref_freq_hz = 48_000_000,
.baud_freq_hz = 4_000_000,
}),
.pins = .{
.rst = tft_rst_pin,
.lite = tft_lite_pin,
.dc = tft_dc_pin,
.cs = tft_cs_pin,
.sck = tft_sck_pin,
.mosi = tft_mosi_pin,
},
.fb = .{
.bpp16 = &fb,
},
});

lcd.clear_screen(.{
.r = 31,
.g = 0,
.b = 0,
});
lcd.set_window(0, 0, 10, 10);
lcd.init(.bpp16, @ptrCast(&fb));

//Lcd.fill16(red16);
timer.delay_us(5 * std.time.us_per_s);
lcd.invert();
while (true) {}
Expand Down
2 changes: 1 addition & 1 deletion src/badge/demos/song.zig
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ var channels_note_start = [1]f32{0.0} ** song.len;
pub export fn start() void {}

pub export fn update() void {
time += 1.0 / 10.0; // TODO: should be higher fps once lcd is dma'd
time += 1.0 / 11.5; // TODO: should be higher fps once lcd is dma'd
for (
0..,
&channels_note_index,
Expand Down
2 changes: 1 addition & 1 deletion src/board.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const port = hal.port;

pub const NeopixelColor = @import("board/neopixel.zig").Color;
pub const Neopixels = @import("board/neopixel.zig").Group(5);
pub const Lcd = @import("board/lcd.zig").Lcd;
pub const lcd = @import("board/lcd.zig");
pub const audio = @import("board/audio.zig");

pub const TFT_RST = port.pin(.a, 0);
Expand Down
Loading
Loading