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

Updated code to regz rewrite and added fix for i2c on ATmega328P #125

Merged
merged 4 commits into from
Apr 25, 2023
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: 1 addition & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub fn build(b: *std.build.Builder) !void {
},
.optimize = optimize,
});
exe.install();
exe.installArtifact(b);
}
----

Expand Down
35 changes: 19 additions & 16 deletions src/core/experimental/clock.zig
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
const std = @import("std");
const micro = @import("microzig");
const chip = @import("chip");
const hal = @import("hal");
const app = struct {}; // workaround for error: no package named 'app' available within package 'root.microzig'
const board = @import("board");
const cpu = @import("cpu");
const config = @import("config");

/// An enumeration of clock sources.
pub const Source = enum {
none,
application,
board,
chip,
hal,
cpu,
};

/// A struct containing the frequency in hertz for each clock domain
pub const Clocks = std.enums.EnumFieldStruct(chip.clock.Domain, u32, null);
pub const Clocks = std.enums.EnumFieldStruct(hal.clock.Domain, u32, null);

/// Is `true` when microzig has a clock frequency available.
/// Clock can be provided by several clock sources
Expand All @@ -23,10 +26,10 @@ pub const is_dynamic = has_clock and !@typeInfo(@TypeOf(&@field(clock_source_typ

/// Contains the source which provides microzig with clock information.
pub const source: Source = switch (clock_source_type) {
micro.app => .application,
micro.board => .board,
micro.chip => .chip,
micro.cpu => .cpu,
app => .application,
board => .board,
hal => .hal,
cpu => .cpu,
no_clock_source_type => .none,
else => unreachable,
};
Expand All @@ -46,14 +49,14 @@ pub inline fn get() Clocks {
const freq_decl_name = "clock_frequencies";

const no_clock_source_type = opaque {};
const clock_source_type = if (@hasDecl(micro.app, freq_decl_name))
micro.app
else if (micro.config.has_board and @hasDecl(micro.board, freq_decl_name))
micro.board
else if (@hasDecl(micro.chip, freq_decl_name))
micro.chip
else if (@hasDecl(micro.cpu, freq_decl_name))
micro.cpu
const clock_source_type = if (@hasDecl(app, freq_decl_name))
app
else if (config.has_board and @hasDecl(board, freq_decl_name))
board
else if (@hasDecl(hal, freq_decl_name))
hal
else if (@hasDecl(cpu, freq_decl_name))
cpu
else
no_clock_source_type;

Expand Down
9 changes: 5 additions & 4 deletions src/core/experimental/debug.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const std = @import("std");
const micro = @import("microzig");
const config = @import("config");
const board = @import("board");

pub fn busy_sleep(comptime limit: comptime_int) void {
if (limit <= 0) @compileError("limit must be non-negative!");
Expand Down Expand Up @@ -27,12 +28,12 @@ fn writer_write(ctx: void, string: []const u8) DebugErr!usize {
const DebugWriter = std.io.Writer(void, DebugErr, writer_write);

pub fn write(string: []const u8) void {
if (!micro.config.has_board)
if (!config.has_board)
return;
if (!@hasDecl(micro.board, "debugWrite"))
if (!@hasDecl(board, "debugWrite"))
return;

micro.board.debug_write(string);
board.debug_write(string);
}

pub fn writer() DebugWriter {
Expand Down
7 changes: 4 additions & 3 deletions src/core/experimental/i2c.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const std = @import("std");
const micro = @import("microzig");
const chip = @import("chip");
const hal = @import("hal");
const cfg = @import("config");

pub fn I2CController(comptime index: usize, comptime pins: Pins) type {
const SystemI2CController = chip.I2CController(index, pins);
const SystemI2CController = hal.I2CController(index, pins);

const I2CDevice = struct {
const Device = @This();
Expand Down Expand Up @@ -128,6 +128,7 @@ pub fn I2CController(comptime index: usize, comptime pins: Pins) type {
const Self = @This();

internal: SystemI2CController,
variable_so_struct_is_not_of_size_zero_and_mcu_hangs_when_returning_struct_on_avr: if (std.mem.eql(u8, cfg.chip_name, "ATmega328P")) u8 else void = undefined,

pub fn init(config: Config) InitError!Self {
return Self{
Expand Down
14 changes: 7 additions & 7 deletions src/core/experimental/uart.zig
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
const std = @import("std");
const micro = @import("microzig");
const chip = @import("chip");
const hal = @import("hal");
const clock = @import("clock.zig");

pub fn Uart(comptime index: usize, comptime pins: Pins) type {
const SystemUart = chip.Uart(index, pins);
const SystemUart = hal.Uart(index, pins);
return struct {
const Self = @This();

internal: SystemUart,

/// Initializes the UART with the given config and returns a handle to the uart.
pub fn init(config: Config) InitError!Self {
micro.clock.ensure();
clock.ensure();
return Self{
.internal = try SystemUart.init(config),
};
Expand Down Expand Up @@ -81,9 +81,9 @@ pub const Config = struct {
};

// TODO: comptime verify that the enums are valid
pub const DataBits = chip.uart.DataBits;
pub const StopBits = chip.uart.StopBits;
pub const Parity = chip.uart.Parity;
pub const DataBits = hal.uart.DataBits;
pub const StopBits = hal.uart.StopBits;
pub const Parity = hal.uart.Parity;

pub const InitError = error{
UnsupportedWordSize,
Expand Down