Skip to content

Add ATtiny port with avr25 CPU support and fix AVR vector tables#923

Open
mattnite wants to merge 9 commits into
mainfrom
attiny
Open

Add ATtiny port with avr25 CPU support and fix AVR vector tables#923
mattnite wants to merge 9 commits into
mainfrom
attiny

Conversation

@mattnite
Copy link
Copy Markdown
Contributor

Add a new port for Microchip ATtiny (ATtiny85, ATtiny84) with HAL, board definitions (Digispark, Adafruit Trinket, Adafruit Gemma), and blinky examples including an interrupt handler example.

Also fix the AVR startup logic for both avr25 and avr5:

  • Place vector table in microzig_flash_start section so it lands at address 0x0 where the hardware expects it
  • Emit full vector table with entries for all interrupt vectors, not just the reset vector
  • Add InterruptOptions so users can specify interrupt handlers via microzig_options, which get wired into the vector table
  • Update calling convention names for zig 0.15.2 compatibility (.avr_signal/.avr_interrupt/.auto, .@"fn")

const gpio = microzig.hal.gpio;

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't we put this in the board config so that we can use the same blinky.zig for multiple boards?

@mattnite mattnite force-pushed the attiny branch 2 times, most recently from d24d37b to 9ab36bb Compare May 19, 2026 05:01
Copy link
Copy Markdown
Contributor Author

@mattnite mattnite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels like there's a lot of duplication in the cpu files, will have to comb through and trim it down.

Comment thread core/src/cpus/avr5.zig
};

/// AVR interrupt handler function type.
pub const HandlerFn = *const fn () callconv(.avr_signal) void;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if this should actually be avr_signal

Comment thread core/src/cpus/avr25.zig
});
}

fn make_isr_handler(comptime name: []const u8, comptime func: anytype) type {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a duplicate?

Comment thread build.zig
.name = "avr5",
.root_source_file = mb.core_dep.namedLazyPath("cpu_avr5"),
};
} else if (std.mem.eql(u8, target.cpu.model.name, "avr25")) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's start looking at making a table instead of if elses.

mattnite and others added 6 commits May 19, 2026 14:33
Add a new port for Microchip ATtiny (ATtiny85, ATtiny84) with HAL,
board definitions (Digispark, Adafruit Trinket, Adafruit Gemma),
and blinky examples including an interrupt handler example.

Also fix the AVR startup logic for both avr25 and avr5:
- Place vector table in microzig_flash_start section so it lands at
  address 0x0 where the hardware expects it
- Emit full vector table with entries for all interrupt vectors, not
  just the reset vector
- Add InterruptOptions so users can specify interrupt handlers via
  microzig_options, which get wired into the vector table
- Update calling convention names for zig 0.15.2 compatibility
  (.avr_signal/.avr_interrupt/.auto, .@"fn")

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor Author

@mattnite mattnite left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More comments and verifications that need to be made before merging


pub const microzig_options: microzig.Options = .{
.interrupts = .{
.INT0 = &my_int0_handler,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any configuration in the main function for this, consider removing the example it if it's unverified.

@@ -0,0 +1,16 @@
const microzig = @import("microzig");
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Double check

@@ -0,0 +1,18 @@
const microzig = @import("microzig");
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: double check

@@ -0,0 +1,20 @@
const microzig = @import("microzig");
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: double check

const microzig = @import("microzig");
const cpu = microzig.cpu;

pub const gpio = struct {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: review ATtiny84 datasheet/family reference.


const atpack = b.dependency("atpack", .{});

const avr25_target: std.Target.Query = .{
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we be getting this from the cpu?

.atdf = atpack.path("atdf/ATtiny85.atdf"),
},
.memory_regions = &.{
.{ .tag = .flash, .offset = 0x000000, .length = 8 * 1024, .access = .rx },
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Double check memory

Comment thread port/microchip/attiny/build.zig Outdated
.atdf = atpack.path("atdf/ATtiny84.atdf"),
},
.memory_regions = &.{
.{ .tag = .flash, .offset = 0x000000, .length = 8 * 1024, .access = .rx },
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: double check memory


return .{
.chips = .{
.attiny85 = chip_attiny85.derive(.{}),
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why aren't we just assigning these? it should be safe to do that.

.attiny85 = chip_attiny85.derive(.{}),
.attiny84 = chip_attiny84.derive(.{}),
},
.boards = .{
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of these boards use chip_attiny84.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants