Skip to content

Commit

Permalink
some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Mar 16, 2023
1 parent a0b60eb commit 764a9a6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 45 deletions.
29 changes: 14 additions & 15 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
const std = @import("std");
const Builder = std.build.Builder;
const Builder = std.Build.Builder;
const CrossTarget = std.zig.CrossTarget;
const Target = std.Target;

pub fn build(b: *Builder) !void {
const uno = std.zig.CrossTarget{
const uno = CrossTarget{
.cpu_arch = .avr,
.cpu_model = .{ .explicit = &std.Target.avr.cpu.atmega328p },
.cpu_model = .{ .explicit = &Target.avr.cpu.atmega328p },
.os_tag = .freestanding,
.abi = .none,
};

const exe = b.addExecutable("avr-arduino-zig", "src/start.zig");
exe.setTarget(uno);
exe.setBuildMode(.ReleaseSafe);
const exe = b.addExecutable(.{
.name = "avr-arduino-zig",
.target = uno,
.optimize = .ReleaseSafe,
.root_source_file = .{ .path = "src/start.zig" },
});
exe.bundle_compiler_rt = false;
exe.setLinkerScriptPath(std.build.FileSource{ .path = "src/linker.ld" });
exe.setLinkerScriptPath(.{ .path = "src/linker.ld" });
exe.install();

const tty = b.option(
Expand All @@ -24,16 +29,10 @@ pub fn build(b: *Builder) !void {

const bin_path = b.getInstallPath(exe.install_step.?.dest_dir, exe.out_filename);

const flash_command = blk: {
var tmp = std.ArrayList(u8).init(b.allocator);
try tmp.appendSlice("-Uflash:w:");
try tmp.appendSlice(bin_path);
try tmp.appendSlice(":e");
break :blk tmp.toOwnedSlice();
};
const flash_command = b.fmt("-Uflash:w: {s} :e", .{bin_path});

const upload = b.step("upload", "Upload the code to an Arduino device using avrdude");
const avrdude = b.addSystemCommand(&.{
const avrdude = b.addSystemCommand(&[_][]const u8{
"avrdude",
"-carduino",
"-patmega328p",
Expand Down
22 changes: 2 additions & 20 deletions src/atmega328p.zig
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,8 @@ pub const VectorTable = extern struct {
};

pub const registers = struct {

/// Fuses
pub const FUSE = struct {

/// address: 0x2
pub const EXTENDED = @intToPtr(*volatile Mmio(8, packed struct {
/// Brown-out Detector trigger level
Expand Down Expand Up @@ -173,7 +171,6 @@ pub const registers = struct {

/// Lockbits
pub const LOCKBIT = struct {

/// address: 0x0
pub const LOCKBIT = @intToPtr(*volatile Mmio(8, packed struct {
/// Memory Lock
Expand Down Expand Up @@ -203,7 +200,6 @@ pub const registers = struct {

/// USART
pub const USART0 = struct {

/// address: 0xc6
/// USART I/O Data Register
pub const UDR0 = @intToPtr(*volatile u8, 0xc6);
Expand Down Expand Up @@ -284,7 +280,6 @@ pub const registers = struct {

/// Two Wire Serial Interface
pub const TWI = struct {

/// address: 0xbd
/// TWI (Slave) Address Mask Register
pub const TWAMR = @intToPtr(*volatile Mmio(8, packed struct {
Expand Down Expand Up @@ -347,7 +342,6 @@ pub const registers = struct {

/// Timer/Counter, 16-bit
pub const TC1 = struct {

/// address: 0x6f
/// Timer/Counter Interrupt Mask Register
pub const TIMSK1 = @intToPtr(*volatile Mmio(8, packed struct {
Expand Down Expand Up @@ -465,7 +459,6 @@ pub const registers = struct {

/// Timer/Counter, 8-bit Async
pub const TC2 = struct {

/// address: 0x70
/// Timer/Counter Interrupt Mask register
pub const TIMSK2 = @intToPtr(*volatile Mmio(8, packed struct {
Expand Down Expand Up @@ -585,7 +578,6 @@ pub const registers = struct {

/// Analog-to-Digital Converter
pub const ADC = struct {

/// address: 0x7c
/// The ADC multiplexer Selection Register
pub const ADMUX = @intToPtr(*volatile Mmio(8, packed struct {
Expand Down Expand Up @@ -682,7 +674,6 @@ pub const registers = struct {

/// Analog Comparator
pub const AC = struct {

/// address: 0x50
/// Analog Comparator Control And Status Register
pub const ACSR = @intToPtr(*volatile Mmio(8, packed struct {
Expand Down Expand Up @@ -725,7 +716,6 @@ pub const registers = struct {

/// I/O Port
pub const PORTB = struct {

/// address: 0x25
/// Port B Data Register
pub const PORTB = @intToPtr(*volatile u8, 0x25);
Expand All @@ -741,7 +731,6 @@ pub const registers = struct {

/// I/O Port
pub const PORTC = struct {

/// address: 0x28
/// Port C Data Register
pub const PORTC = @intToPtr(*volatile u7, 0x28);
Expand All @@ -757,7 +746,6 @@ pub const registers = struct {

/// I/O Port
pub const PORTD = struct {

/// address: 0x2b
/// Port D Data Register
pub const PORTD = @intToPtr(*volatile u8, 0x2b);
Expand All @@ -773,7 +761,6 @@ pub const registers = struct {

/// Timer/Counter, 8-bit
pub const TC0 = struct {

/// address: 0x48
/// Timer/Counter0 Output Compare Register
pub const OCR0B = @intToPtr(*volatile u8, 0x48);
Expand Down Expand Up @@ -872,7 +859,6 @@ pub const registers = struct {

/// External Interrupts
pub const EXINT = struct {

/// address: 0x69
/// External Interrupt Control Register
pub const EICRA = @intToPtr(*volatile Mmio(8, packed struct {
Expand Down Expand Up @@ -971,7 +957,6 @@ pub const registers = struct {

/// Serial Peripheral Interface
pub const SPI = struct {

/// address: 0x4e
/// SPI Data Register
pub const SPDR = @intToPtr(*volatile u8, 0x4e);
Expand Down Expand Up @@ -1019,7 +1004,6 @@ pub const registers = struct {

/// Watchdog Timer
pub const WDT = struct {

/// address: 0x60
/// Watchdog Timer Control Register
pub const WDTCSR = @intToPtr(*volatile Mmio(8, packed struct {
Expand All @@ -1040,7 +1024,6 @@ pub const registers = struct {

/// CPU Registers
pub const CPU = struct {

/// address: 0x64
/// Power Reduction Register
pub const PRR = @intToPtr(*volatile Mmio(8, packed struct {
Expand Down Expand Up @@ -1202,7 +1185,6 @@ pub const registers = struct {

/// EEPROM
pub const EEPROM = struct {

/// address: 0x41
/// EEPROM Address Register Bytes
pub const EEAR = @intToPtr(*volatile u10, 0x41);
Expand Down Expand Up @@ -1313,8 +1295,8 @@ pub fn mmioInt(addr: usize, comptime size: usize, comptime T: type) *volatile Mm
}

const InterruptVector = extern union {
C: fn () callconv(.C) void,
Naked: fn () callconv(.Naked) void,
C: *const fn () callconv(.C) void,
Naked: *const fn () callconv(.Naked) void,
// Interrupt is not supported on arm
};

Expand Down
20 changes: 10 additions & 10 deletions src/start.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const std = @import("std");
const builtin = std.builtin;

export const vector_table linksection(".vectors") = blk: {
std.debug.assert(std.mem.eql(u8, "RESET", std.meta.fields(atmega328p.VectorTable)[0].name));
std.debug.assert(std.mem.eql(u8, "RESET", std.meta.fieldNames(atmega328p.VectorTable)[0]));
var asm_str: []const u8 = "jmp _start\n";

const has_interrupts = @hasDecl(main, "interrupts");
Expand All @@ -16,9 +16,9 @@ export const vector_table linksection(".vectors") = blk: {
inline for (std.meta.declarations(main.interrupts)) |decl| {
if (!@hasField(atmega328p.VectorTable, decl.name)) {
var msg: []const u8 = "There is no such interrupt as '" ++ decl.name ++ "'. ISRs the 'interrupts' namespace must be one of:\n";
inline for (std.meta.fields(atmega328p.VectorTable)) |field| {
if (!std.mem.eql(u8, "RESET", field.name)) {
msg = msg ++ " " ++ field.name ++ "\n";
inline for (std.meta.fieldNames(atmega328p.VectorTable)) |field| {
if (!std.mem.eql(u8, "RESET", field)) {
msg = msg ++ " " ++ field ++ "\n";
}
}

Expand All @@ -27,13 +27,13 @@ export const vector_table linksection(".vectors") = blk: {
}
}

inline for (std.meta.fields(atmega328p.VectorTable)[1..]) |field| {
inline for (std.meta.fieldNames(atmega328p.VectorTable)) |field| {
const new_insn = if (has_interrupts) overload: {
if (@hasDecl(main.interrupts, field.name)) {
const handler = @field(main.interrupts, field.name);
const calling_convention = switch (@typeInfo(@TypeOf(@field(main.interrupts, field.name)))) {
if (@hasDecl(main.interrupts, field)) {
const handler = @field(main.interrupts, field);
const calling_convention = switch (@typeInfo(@TypeOf(@field(main.interrupts, field)))) {
.Fn => |info| info.calling_convention,
else => @compileError("Declarations in 'interrupts' namespace must all be functions. '" ++ field.name ++ "' is not a function"),
else => @compileError("Declarations in 'interrupts' namespace must all be functions. '" ++ field ++ "' is not a function"),
};

const exported_fn = switch (calling_convention) {
Expand Down Expand Up @@ -122,7 +122,7 @@ fn clear_bss() void {
// Probably a good idea to add clobbers here, but compiler doesn't seem to care
}

pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn {
pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace, _: ?usize) noreturn {
// Currently assumes that the uart is initialized in main().
uart.write("PANIC: ");
uart.write(msg);
Expand Down

0 comments on commit 764a9a6

Please sign in to comment.