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

import microzig as microzig #413

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1 change: 0 additions & 1 deletion core/src/core/experimental/gpio.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const std = @import("std");
const micro = @import("microzig");
const hal = @import("hal");

pub const Mode = enum {
Expand Down
32 changes: 16 additions & 16 deletions core/test/programs/blinky.zig
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
const micro = @import("microzig");
const microzig = @import("microzig");

// Configures the led_pin to a hardware pin
const led_pin = if (micro.config.has_board)
switch (micro.config.board_name) {
.@"Arduino Nano" => micro.Pin("D13"),
.@"Arduino Uno" => micro.Pin("D13"),
.@"mbed LPC1768" => micro.Pin("LED-1"),
.STM32F3DISCOVERY => micro.Pin("LD3"),
.STM32F4DISCOVERY => micro.Pin("LD5"),
.STM32F429IDISCOVERY => micro.Pin("LD4"),
.@"Longan Nano" => micro.Pin("PA2"),
const led_pin = if (microzig.config.has_board)
switch (microzig.config.board_name) {
.@"Arduino Nano" => microzig.Pin("D13"),
.@"Arduino Uno" => microzig.Pin("D13"),
.@"mbed LPC1768" => microzig.Pin("LED-1"),
.STM32F3DISCOVERY => microzig.Pin("LD3"),
.STM32F4DISCOVERY => microzig.Pin("LD5"),
.STM32F429IDISCOVERY => microzig.Pin("LD4"),
.@"Longan Nano" => microzig.Pin("PA2"),
else => @compileError("unknown board"),
}
else switch (micro.config.chip_name) {
.ATmega328p => micro.Pin("PB5"),
.@"NXP LPC1768" => micro.Pin("P1.18"),
.STM32F103x8 => micro.Pin("PC13"),
.GD32VF103x8 => micro.Pin("PA2"),
else switch (microzig.config.chip_name) {
.ATmega328p => microzig.Pin("PB5"),
.@"NXP LPC1768" => microzig.Pin("P1.18"),
.STM32F103x8 => microzig.Pin("PC13"),
.GD32VF103x8 => microzig.Pin("PA2"),
else => @compileError("unknown chip"),
};

pub fn main() void {
const led = micro.Gpio(led_pin, .{
const led = microzig.Gpio(led_pin, .{
.mode = .output,
.initial_state = .low,
});
Expand Down
1 change: 0 additions & 1 deletion core/test/programs/interrupt.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const micro = @import("microzig");
const builtin = @import("builtin");

pub const interrupts = switch (builtin.cpu.arch) {
Expand Down
38 changes: 19 additions & 19 deletions core/test/programs/uart-sync.zig
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
const micro = @import("microzig");
const microzig = @import("microzig");

// Configures the led_pin and uart index
const cfg = if (micro.config.has_board)
switch (micro.config.board_name) {
const cfg = if (microzig.config.has_board)
switch (microzig.config.board_name) {
.@"mbed LPC1768" => .{
.led_pin = micro.Pin("LED-1"),
.led_pin = microzig.Pin("LED-1"),
.uart_idx = 1,
.pins = .{},
},
.STM32F3DISCOVERY => .{
.led_pin = micro.Pin("LD3"),
.led_pin = microzig.Pin("LD3"),
.uart_idx = 1,
.pins = .{},
},
.STM32F4DISCOVERY => .{
.led_pin = micro.Pin("LD5"),
.led_pin = microzig.Pin("LD5"),
.uart_idx = 2,
.pins = .{ .tx = micro.Pin("PA2"), .rx = micro.Pin("PA3") },
.pins = .{ .tx = microzig.Pin("PA2"), .rx = microzig.Pin("PA3") },
},
.@"Longan Nano" => .{
.led_pin = micro.Pin("PA2"),
.led_pin = microzig.Pin("PA2"),
.uart_idx = 1,
.pins = .{ .tx = null, .rx = null },
},
.@"Arduino Uno" => .{
.led_pin = micro.Pin("D13"),
.led_pin = microzig.Pin("D13"),
.uart_idx = 0,
.pins = .{},
},
else => @compileError("unknown board"),
}
else switch (micro.config.chip_name) {
.@"NXP LPC1768" => .{ .led_pin = micro.Pin("P1.18"), .uart_idx = 1, .pins = .{} },
.GD32VF103x8 => .{ .led_pin = micro.Pin("PA2"), .uart_idx = 1, .pins = .{} },
else switch (microzig.config.chip_name) {
.@"NXP LPC1768" => .{ .led_pin = microzig.Pin("P1.18"), .uart_idx = 1, .pins = .{} },
.GD32VF103x8 => .{ .led_pin = microzig.Pin("PA2"), .uart_idx = 1, .pins = .{} },
else => @compileError("unknown chip"),
};

pub fn main() !void {
const led = micro.Gpio(cfg.led_pin, .{
const led = microzig.Gpio(cfg.led_pin, .{
.mode = .output,
.initial_state = .low,
});
led.init();

var uart = micro.Uart(cfg.uart_idx, cfg.pins).init(.{
var uart = microzig.Uart(cfg.uart_idx, cfg.pins).init(.{
.baud_rate = 9600,
.stop_bits = .one,
.parity = null,
.data_bits = .eight,
}) catch |err| {
blinkError(led, err);

micro.hang();
microzig.hang();
};

var out = uart.writer();
Expand All @@ -60,11 +60,11 @@ pub fn main() !void {
led.setToHigh();
try out.writeAll("Hello microzig!\r\n");
led.setToLow();
micro.debug.busySleep(100_000);
microzig.debug.busySleep(100_000);
}
}

fn blinkError(led: anytype, err: micro.uart.InitError) void {
fn blinkError(led: anytype, err: microzig.uart.InitError) void {
var blinks: u3 =
switch (err) {
error.UnsupportedBaudRate => 1,
Expand All @@ -75,8 +75,8 @@ fn blinkError(led: anytype, err: micro.uart.InitError) void {

while (blinks > 0) : (blinks -= 1) {
led.setToHigh();
micro.debug.busySleep(1_000_000);
microzig.debug.busySleep(1_000_000);
led.setToLow();
micro.debug.busySleep(1_000_000);
microzig.debug.busySleep(1_000_000);
}
}
56 changes: 28 additions & 28 deletions core/thoughts.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ root
Declaring `panic` in the root file will cause `builtin` to reference the proper package which will then instantiate microzig which will reference `mcu` package which will instantiate `reset` (or similar) which will invoke `microzig.main()` which will invoke `root.main()`. Oh boy :laughing:

```zig
const micro = @import("micro");
const microzig = @import("microzig");

pub const panic = micro.panic; // this will instantiate microzig
pub const panic = microzig.panic; // this will instantiate microzig

comptime { _ = micro }; // this should not be necessary
comptime { _ = microzig }; // this should not be necessary

pub fn main() void {

Expand Down Expand Up @@ -59,18 +59,18 @@ A symbol can be a function `fn() void`, or a anonymous enum literal `.hang` or `

```zig
pub fn main() void {
micro.interrupts.enable(.WDT); // enables the WDT interrupt
micro.interrupts.disable(.WDT); // enables the WDT interrupt
micro.interrupts.enable(.WTF); // yields compile error "WTF is not a valid interrupt"
micro.interrupts.enable(.PCINT0); // yields compile error "PCINT0 has no defined interrupt handler"
micro.interrupts.enable(.NMI); // yields compile error "NMI cannot be masked"
micro.interrupts.enableAll();
micro.interrupts.batchEnable(.{ .NMI, .WDT });
microzig.interrupts.enable(.WDT); // enables the WDT interrupt
microzig.interrupts.disable(.WDT); // enables the WDT interrupt
microzig.interrupts.enable(.WTF); // yields compile error "WTF is not a valid interrupt"
microzig.interrupts.enable(.PCINT0); // yields compile error "PCINT0 has no defined interrupt handler"
microzig.interrupts.enable(.NMI); // yields compile error "NMI cannot be masked"
microzig.interrupts.enableAll();
microzig.interrupts.batchEnable(.{ .NMI, .WDT });

micro.interrupts.cli(); // set interrupt enabled (global enable)
microzig.interrupts.cli(); // set interrupt enabled (global enable)

{ // critical section
var crit = micro.interrupts.enterCriticalSection();
var crit = microzig.interrupts.enterCriticalSection();
defer crit.leave();
}
}
Expand Down Expand Up @@ -100,7 +100,7 @@ pub const interrupt_handlers = struct {
microzig should allow having a general purpose timer mechanism

```zig
pub var cpu_frequency = 16.0 * micro.clock.mega_hertz;
pub var cpu_frequency = 16.0 * microzig.clock.mega_hertz;

pub const sleep_mode = .timer; // timer, busyloop, whatever

Expand All @@ -109,7 +109,7 @@ pub fn main() !void {

while(true) {
led.toggle();
micro.sleep(100_000); // sleep 100ms
microzig.sleep(100_000); // sleep 100ms
}
}
```
Expand All @@ -121,18 +121,18 @@ pub fn main() !void {

```zig

// micro.Pin returns a type containing all relevant pin information
const status_led_pin = micro.Pin("PA3");
// microzig.Pin returns a type containing all relevant pin information
const status_led_pin = microzig.Pin("PA3");

// generate a runtime possible pin that cannot be used in all APIs
var generic_pic: micro.RuntimePin.init(status_led_pin);
var generic_pic: microzig.RuntimePin.init(status_led_pin);

// 4 Bit IEEE-488 bit banging register
const serial_out = micro.GpioOutputRegister(.{
micro.Pin("PA0"),
micro.Pin("PA1"),
micro.Pin("PA3"), // whoopsies, i miswired, let the software fix that
micro.Pin("PA2"),
const serial_out = microzig.GpioOutputRegister(.{
microzig.Pin("PA0"),
microzig.Pin("PA1"),
microzig.Pin("PA3"), // whoopsies, i miswired, let the software fix that
microzig.Pin("PA2"),
});

pub fn bitBang(nibble: u4) void {
Expand All @@ -150,9 +150,9 @@ pub fn main() !void {
// route that pin to UART.RXD
status_led_pin.route(.uart0_rxd);

//var uart_read_dma_channel = micro.Dma.init(.{.channel = 1});
//var uart_read_dma_channel = microzig.Dma.init(.{.channel = 1});

const status_led = micro.Gpio(status_led_pin, .{
const status_led = microzig.Gpio(status_led_pin, .{
.mode = .output, // { input, output, input_output, open_drain, generic }
.initial_state = .unspecificed, // { unspecified, low, high, floating, driven }
});
Expand Down Expand Up @@ -206,17 +206,17 @@ pub fn main() !void {

```zig
const std = @import("std");
const micro = @import("µzig");
const microzig = @import("µzig");

// if const it can be comptime-optimized
pub var cpu_frequency = 100.0 * micro.clock.mega_hertz;
pub var cpu_frequency = 100.0 * microzig.clock.mega_hertz;

// if this is enabled, a event loop will run
// in microzig.main() that allows using `async`/`await` "just like that" *grin*
pub const io_mode = .evented;

pub fn main() !void {
var debug_port = micro.Uart.init(0, .{
var debug_port = microzig.Uart.init(0, .{
.baud_rate = 9600,
.stop_bits = .@"2",
.parity = .none, // { none, even, odd, mark, space }
Expand All @@ -239,7 +239,7 @@ pub fn main() !void {

## Initialization

somewhere inside micro.zig
somewhere inside microzig.zig
```zig
extern fn reset() noreturn {
if(@hasDecl(mcu, "mcu_reset")) {
Expand Down
1 change: 0 additions & 1 deletion port/gigadevice/gd32/src/boards/longan_nano.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub const chip = @import("chip");
pub const micro = @import("microzig");

pub const cpu_frequency = 8_000_000; // 8 MHz

Expand Down
12 changes: 6 additions & 6 deletions port/gigadevice/gd32/src/hals/GD32VF103.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const micro = @import("microzig");
const peripherals = micro.chip.peripherals;
const microzig = @import("microzig");
const peripherals = microzig.chip.peripherals;
const UART3 = peripherals.UART3;
const UART4 = peripherals.UART4;

Expand Down Expand Up @@ -40,13 +40,13 @@ pub const gpio = struct {
// TODO: check if pin is already configured as input
}

pub fn read(comptime pin: type) micro.gpio.State {
pub fn read(comptime pin: type) microzig.gpio.State {
_ = pin;
// TODO: check if pin is configured as input
return .low;
}

pub fn write(comptime pin: type, state: micro.gpio.State) void {
pub fn write(comptime pin: type, state: microzig.gpio.State) void {
_ = pin;
_ = state;
// TODO: check if pin is configured as output
Expand Down Expand Up @@ -74,7 +74,7 @@ pub const uart = struct {
};
};

pub fn Uart(comptime index: usize, comptime pins: micro.uart.Pins) type {
pub fn Uart(comptime index: usize, comptime pins: microzig.uart.Pins) type {
if (pins.tx != null or pins.rx != null)
@compileError("TODO: custom pins are not currently supported");

Expand All @@ -86,7 +86,7 @@ pub fn Uart(comptime index: usize, comptime pins: micro.uart.Pins) type {
};
const Self = @This();

pub fn init(config: micro.uart.Config) !Self {
pub fn init(config: microzig.uart.Config) !Self {
_ = config;
return Self{};
}
Expand Down
8 changes: 4 additions & 4 deletions port/gigadevice/gd32/src/hals/GD32VF103/uart.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const micro = @import("microzig");
const peripherals = micro.chip.peripherals;
const microzig = @import("microzig");
const peripherals = microzig.chip.peripherals;
const UART3 = peripherals.UART3;
const UART4 = peripherals.UART4;

Expand All @@ -24,7 +24,7 @@ pub const uart = struct {
};
};

pub fn Uart(comptime index: usize, comptime pins: micro.uart.Pins) type {
pub fn Uart(comptime index: usize, comptime pins: microzig.uart.Pins) type {
if (pins.tx != null or pins.rx != null)
@compileError("TODO: custom pins are not currently supported");

Expand All @@ -36,7 +36,7 @@ pub fn Uart(comptime index: usize, comptime pins: micro.uart.Pins) type {
};
const Self = @This();

pub fn init(config: micro.uart.Config) !Self {
pub fn init(config: microzig.uart.Config) !Self {
_ = config;
return Self{};
}
Expand Down
2 changes: 1 addition & 1 deletion port/gigadevice/gd32/test/programs/minimal.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const micro = @import("microzig");
const microzig = @import("microzig");

pub fn main() void {
// This function will contain the application logic.
Expand Down
1 change: 0 additions & 1 deletion port/microchip/avr/src/boards.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const std = @import("std");
const micro = @import("microzig");
const chips = @import("chips.zig");

fn root_dir() []const u8 {
Expand Down
6 changes: 3 additions & 3 deletions port/microchip/avr/src/chips.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const std = @import("std");
const micro = @import("microzig");
const Chip = micro.Chip;
const MemoryRegion = micro.MemoryRegion;
const microzig = @import("microzig");
const Chip = microzig.Chip;
const MemoryRegion = microzig.MemoryRegion;

fn root_dir() []const u8 {
return std.fs.path.dirname(@src().file) orelse ".";
Expand Down
Loading
Loading