Skip to content
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
2 changes: 0 additions & 2 deletions .github/workflows/ports.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,10 @@ jobs:
working-directory: examples/gigadevice/gd32

- name: microchip/atmega
if: 0 # https://codeberg.org/ziglang/zig/issues/32108
run: zig build -Doptimize=ReleaseSmall
working-directory: examples/microchip/atmega

- name: microchip/attiny
if: 0 # https://codeberg.org/ziglang/zig/issues/32108
run: zig build -Doptimize=ReleaseSmall
working-directory: examples/microchip/attiny

Expand Down
39 changes: 16 additions & 23 deletions core/src/cpus/avr_common.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@ pub const interrupt = struct {
};

/// AVR interrupt handler function type.
pub const HandlerFn = extern union {
pub const HandlerFn = union(enum) {
/// Standard AVR interrupt handler. Disables global interrupts while it is
/// executing. Compiler generates prologue for pushing clobbered registers to
/// the stack, and corresponding epilogue with `reti` intsruction for
/// returning from an interrupt.
signal: *const fn () callconv(.avr_signal) void,
/// Similar to the signal calling convention, but global interrupts are
/// enabled in the prologue, allowing for nested interrupts.
interrupt: *const fn () callconv(.avr_interrupt) void,
/// No prologue, no epilogue, best for handwritten assembly.
naked: *const fn () callconv(.naked) noreturn,
};

Expand Down Expand Up @@ -78,8 +85,8 @@ pub fn generate_vector_table_asm(comptime jump_insn: JumpInstruction) []const u8
for (field_names[1..]) |field_name| {
const handler = @field(interrupt_options, field_name);
if (handler) |func| {
const isr = make_isr_handler(field_name, func);
asm_str = asm_str ++ jump_insn.to_string() ++ isr.exported_name ++ "\n";
const isr_symbol = export_isr_handler(field_name, func);
asm_str = asm_str ++ jump_insn.to_string() ++ " " ++ isr_symbol ++ "\n";
} else {
asm_str = asm_str ++ jump_insn.to_string() ++ " microzig_unhandled_vector\n";
}
Expand All @@ -88,28 +95,14 @@ pub fn generate_vector_table_asm(comptime jump_insn: JumpInstruction) []const u8
return asm_str;
}

fn make_isr_handler(comptime name: []const u8, comptime func: anytype) type {
const calling_convention = switch (@typeInfo(@TypeOf(func))) {
.@"fn" => |info| info.calling_convention,
.pointer => |info| switch (@typeInfo(info.child)) {
.@"fn" => |fn_info| fn_info.calling_convention,
else => @compileError("Declarations in 'interrupts' namespace must all be functions. '" ++ name ++ "' is not a function"),
},
else => @compileError("Declarations in 'interrupts' namespace must all be functions. '" ++ name ++ "' is not a function"),
};

switch (calling_convention) {
.auto, .avr_signal, .avr_interrupt => {},
else => @compileError("Calling conventions for interrupts must be 'avr_interrupt', 'avr_signal', or unspecified. The avr_signal calling convention leaves global interrupts disabled during the ISR, where avr_interrupt enables global interrupts for nested ISRs."),
}
fn export_isr_handler(comptime name: []const u8, comptime handler: HandlerFn) []const u8 {
const exported_name = "microzig_isr_" ++ name;

return struct {
pub const exported_name = "microzig_isr_" ++ name;
switch (handler) {
inline else => |func| @export(func, .{ .name = exported_name }),
}

comptime {
@export(func, .{ .name = exported_name });
}
};
return exported_name;
}

pub const startup_logic = struct {
Expand Down
1 change: 0 additions & 1 deletion examples/microchip/attiny/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pub fn build(b: *std.Build) void {
.{ .target = mb.ports.attiny.boards.digispark, .name = "digispark_blinky", .file = "src/blinky.zig" },
.{ .target = mb.ports.attiny.boards.adafruit.trinket, .name = "trinket_blinky", .file = "src/blinky.zig" },
.{ .target = mb.ports.attiny.boards.adafruit.gemma, .name = "gemma_blinky", .file = "src/blinky.zig" },
.{ .target = mb.ports.attiny.chips.attiny85, .name = "attiny85_blinky", .file = "src/blinky.zig" },
.{ .target = mb.ports.attiny.chips.attiny85, .name = "attiny85_blinky_interrupt", .file = "src/blinky_interrupt.zig" },
.{ .target = mb.ports.attiny.chips.attiny84, .name = "attiny84_blinky", .file = "src/blinky84.zig" },
.{ .target = mb.ports.attiny.chips.attiny1634, .name = "attiny1634_pwm_adc", .file = "src/attiny1634_pwm_adc.zig" },
Expand Down
4 changes: 4 additions & 0 deletions examples/microchip/attiny/src/attiny1616_tca_rtc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const aux_led = hal.gpio.pin(.b, 5);
const switch_pin = hal.gpio.pin(.a, 5);
const Ramp = hal.progmem.Table(u8, 4, .{ 1, 4, 16, 64 });

comptime {
_ = microzig.export_startup();
}

pub fn main() void {
hal.clock.use_default20_m_hz_div2();

Expand Down
4 changes: 4 additions & 0 deletions examples/microchip/attiny/src/attiny1634_pwm_adc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const fet_pwm = hal.gpio.pin(.c, 0);
const voltage = hal.gpio.pin(.b, 1);
const Gamma = hal.progmem.Table(u8, 4, .{ 0, 8, 32, 255 });

comptime {
_ = microzig.export_startup();
}

pub fn main() void {
ch1_pwm.set_direction(.output);
ch2_pwm.set_direction(.output);
Expand Down
4 changes: 4 additions & 0 deletions examples/microchip/attiny/src/blinky.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ const microzig = @import("microzig");

const led_pin = microzig.board.led_pin;

comptime {
_ = microzig.export_startup();
}

pub fn main() void {
led_pin.set_direction(.output);

Expand Down
6 changes: 5 additions & 1 deletion examples/microchip/attiny/src/blinky84.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ const std = @import("std");
const microzig = @import("microzig");
const gpio = microzig.hal.gpio;

comptime {
_ = microzig.export_startup();
}

// ATtiny84: use PA0 as the LED pin
const led_pin = gpio.pin(.a, 0);

Expand All @@ -22,7 +26,7 @@ pub fn busy_sleep(comptime limit: comptime_int) void {
bits += 1;
}

const I = std.meta.Int(.unsigned, bits);
const I = @Int(.unsigned, bits);

var i: I = 0;
while (i < limit) : (i += 1) {
Expand Down
6 changes: 5 additions & 1 deletion examples/microchip/attiny/src/blinky_interrupt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ const std = @import("std");
const microzig = @import("microzig");
const gpio = microzig.hal.gpio;

comptime {
_ = microzig.export_startup();
}

const led_pin = gpio.pin(.b, 1);

pub const microzig_options: microzig.Options = .{
.interrupts = .{
.INT0 = &my_int0_handler,
.INT0 = .{ .signal = &my_int0_handler },
},
};

Expand Down
11 changes: 3 additions & 8 deletions port/microchip/attiny/src/boards/adafruit_gemma.zig
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
pub const chip = @import("chip");
const microzig = @import("microzig");
const gpio = microzig.hal.gpio;

pub const clock_frequencies = .{
.cpu = 8_000_000,
};

pub const pin_map = .{
.D0 = "PB0",
.D1 = "PB1",
.D2 = "PB2",
// Built-in LED on D1 (PB1)
.LED = "PB1",
};
pub const led_pin = gpio.pin(.b, 1);
15 changes: 3 additions & 12 deletions port/microchip/attiny/src/boards/digispark.zig
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
pub const chip = @import("chip");
const microzig = @import("microzig");
const gpio = microzig.hal.gpio;

pub const clock_frequencies = .{
.cpu = 16_500_000,
};

pub const pin_map = .{
// Digispark pin numbering maps to PORTB
.P0 = "PB0",
.P1 = "PB1",
.P2 = "PB2",
.P3 = "PB3",
.P4 = "PB4",
.P5 = "PB5",
// Built-in LED on P1 (PB1)
.LED = "PB1",
};
pub const led_pin = gpio.pin(.b, 1);
Loading