move examples in here (#12)

wch-ch32v003
Matt Knight 2 years ago committed by GitHub
parent 9fa748ff13
commit d4a74cb4f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

3
.gitmodules vendored

@ -0,0 +1,3 @@
[submodule "deps/microzig"]
path = deps/microzig
url = https://github.com/ZigEmbeddedGroup/microzig.git

@ -1,9 +1,10 @@
const std = @import("std");
const Builder = std.build.Builder;
const Pkg = std.build.Pkg;
const comptimePrint = std.fmt.comptimePrint;
const microzig = @import("deps/microzig/src/main.zig");
const chip_path = comptimePrint("{s}/src/rp2040.zig", .{root()});
const board_path = comptimePrint("{s}/src/raspberry_pi_pico.zig", .{root()});
const hal_path = comptimePrint("{s}/src/hal.zig", .{root()});
@ -12,7 +13,6 @@ const linkerscript_path = comptimePrint("{s}/rp2040.ld", .{root()});
pub const BuildOptions = struct {};
pub fn addPiPicoExecutable(
comptime microzig: type,
builder: *Builder,
name: []const u8,
source: []const u8,
@ -49,6 +49,44 @@ pub fn addPiPicoExecutable(
return ret;
}
// this build script is mostly for testing and verification of this
// package. In an attempt to modularize -- designing for a case where a
// project requires multiple HALs, it accepts microzig as a param
pub fn build(b: *Builder) !void {
const mode = b.standardReleaseOptions();
var examples = Examples.init(b, mode);
examples.install();
}
fn root() []const u8 {
return std.fs.path.dirname(@src().file) orelse ".";
}
pub const Examples = struct {
blinky: microzig.EmbeddedExecutable,
blinky_core1: microzig.EmbeddedExecutable,
gpio_clk: microzig.EmbeddedExecutable,
pwm: microzig.EmbeddedExecutable,
uart: microzig.EmbeddedExecutable,
//uart_pins: microzig.EmbeddedExecutable,
pub fn init(b: *Builder, mode: std.builtin.Mode) Examples {
var ret: Examples = undefined;
inline for (@typeInfo(Examples).Struct.fields) |field| {
@field(ret, field.name) = addPiPicoExecutable(
b,
field.name,
comptime root() ++ "/examples/" ++ field.name ++ ".zig",
.{},
);
@field(ret, field.name).setBuildMode(mode);
}
return ret;
}
pub fn install(examples: *Examples) void {
inline for (@typeInfo(Examples).Struct.fields) |field|
@field(examples, field.name).install();
}
};

1
deps/microzig vendored

@ -0,0 +1 @@
Subproject commit 4159581b4848bfb8bbdf91dabdebd15ecd503427

@ -0,0 +1,20 @@
const std = @import("std");
const microzig = @import("microzig");
const rp2040 = microzig.hal;
const time = rp2040.time;
const pin_config = rp2040.pins.GlobalConfiguration{
.GPIO25 = .{
.name = "led",
.direction = .out,
},
};
pub fn main() !void {
const pins = pin_config.apply();
while (true) {
pins.led.toggle();
time.sleepMs(250);
}
}

@ -0,0 +1,29 @@
const std = @import("std");
const microzig = @import("microzig");
const rp2040 = microzig.hal;
const gpio = rp2040.gpio;
const time = rp2040.time;
const multicore = rp2040.multicore;
const led = 25;
fn core1() void {
while (true) {
gpio.put(led, 1);
time.sleepMs(250);
gpio.put(led, 0);
time.sleepMs(250);
}
}
pub fn main() !void {
gpio.init(led);
gpio.setDir(led, .out);
multicore.launchCore1(core1);
while (true) {
microzig.cpu.wfi();
}
}

@ -0,0 +1,21 @@
const std = @import("std");
const microzig = @import("microzig");
const rp2040 = microzig.hal;
const gpio = rp2040.gpio;
const clocks = rp2040.clocks;
const gpout0_pin = 21;
const clock_config = clocks.GlobalConfiguration.init(.{
.sys = .{ .source = .src_xosc },
.gpout0 = .{ .source = .clk_sys },
});
pub fn init() void {
clock_config.apply();
gpio.reset();
}
pub fn main() !void {
gpio.setFunction(gpout0_pin, .gpck);
while (true) {}
}

@ -0,0 +1,23 @@
const std = @import("std");
const microzig = @import("microzig");
const rp2040 = microzig.hal;
const gpio = rp2040.gpio;
const clocks = rp2040.clocks;
const time = rp2040.time;
const regs = microzig.chip.registers;
const multicore = rp2040.multicore;
const pin_config = rp2040.pins.GlobalConfiguration{
.GPIO25 = .{ .name = "led", .function = .PWM4_B },
};
pub fn main() !void {
const pins = pin_config.apply();
pins.led.slice().setWrap(100);
pins.led.setLevel(10);
pins.led.slice().enable();
while (true) {
time.sleepMs(250);
}
}

@ -0,0 +1,48 @@
const std = @import("std");
const microzig = @import("microzig");
const rp2040 = microzig.hal;
const time = rp2040.time;
const gpio = rp2040.gpio;
const clocks = rp2040.clocks;
const led = 25;
const uart_id = 0;
const baud_rate = 115200;
const uart_tx_pin = 0;
const uart_rx_pin = 1;
pub fn panic(message: []const u8, _: ?*std.builtin.StackTrace) noreturn {
std.log.err("panic: {s}", .{message});
@breakpoint();
while (true) {}
}
pub const log_level = .debug;
pub const log = rp2040.uart.log;
pub fn main() !void {
gpio.reset();
gpio.init(led);
gpio.setDir(led, .out);
gpio.put(led, 1);
const uart = rp2040.uart.UART.init(uart_id, .{
.baud_rate = baud_rate,
.tx_pin = uart_tx_pin,
.rx_pin = uart_rx_pin,
.clock_config = rp2040.clock_config,
});
rp2040.uart.initLogger(uart);
var i: u32 = 0;
while (true) : (i += 1) {
gpio.put(led, 1);
std.log.info("what {}", .{i});
time.sleepMs(500);
gpio.put(led, 0);
time.sleepMs(500);
}
}

@ -8,6 +8,8 @@ pub const multicore = @import("hal/multicore.zig");
pub const time = @import("hal/time.zig");
pub const uart = @import("hal/uart.zig");
pub const pwm = @import("hal/pwm.zig");
pub const resets = @import("hal/resets.zig");
pub const irq = @import("hal/irq.zig");
pub const clock_config = clocks.GlobalConfiguration.init(.{
.ref = .{ .source = .src_xosc },

@ -4,6 +4,8 @@ const resets = @import("resets.zig");
const regs = microzig.chip.registers;
const assert = std.debug.assert;
const log = std.log.scoped(.gpio);
pub const Function = enum(u5) {
xip,
spi,
@ -83,6 +85,7 @@ pub inline fn setDir(comptime gpio: u32, direction: Direction) void {
/// Drive a single GPIO high/low
pub inline fn put(comptime gpio: u32, value: u1) void {
std.log.debug("GPIO{} put: {}", .{ gpio, value });
const mask = 1 << gpio;
switch (value) {
0 => regs.SIO.GPIO_OUT_CLR.raw = mask,

@ -0,0 +1,20 @@
const microzig = @import("microzig");
const regs = microzig.chip.registers;
// TODO: the register definitions are improved now, use them instead of raw
// writes/reads
fn getInterruptMask(comptime interrupt_name: []const u8) u32 {
const offset = @offsetOf(microzig.chip.VectorTable, interrupt_name);
return (1 << ((offset / 4) - 16));
}
pub fn enable(comptime interrupt_name: []const u8) void {
const mask = comptime getInterruptMask(interrupt_name);
regs.SCS.NVIC.ICPR.raw = mask;
regs.SCS.NVIC.ISER.raw = mask;
}
pub fn disable(comptime interrupt_name: []const u8) void {
const mask = comptime getInterruptMask(interrupt_name);
regs.SCS.NVIC.ICER.raw = mask;
}

@ -88,7 +88,7 @@ pub fn launchCore1WithStack(entrypoint: fn () void, stack: []u32) void {
0,
0,
1,
regs.PPB.VTOR.raw,
regs.SCS.SCB.VTOR.raw,
stack_ptr,
@ptrToInt(wrapper),
};

@ -1,6 +1,9 @@
const std = @import("std");
const microzig = @import("microzig");
const regs = microzig.chip.registers;
const log = std.log.scoped(.pwm);
pub const Config = struct {};
fn getRegs(comptime slice: u32) *volatile Regs {
@ -67,12 +70,14 @@ const Regs = extern struct {
};
pub inline fn setSlicePhaseCorrect(comptime slice: u32, phase_correct: bool) void {
log.debug("PWM{} set phase correct: {}", .{ slice, phase_correct });
getRegs(slice).csr.modify(.{
.PH_CORRECT = if (phase_correct) 1 else 0,
});
}
pub inline fn setSliceClkDiv(comptime slice: u32, integer: u8, fraction: u4) void {
log.debug("PWM{} set clk div: {}.{}", .{ slice, integer, fraction });
getRegs(slice).div.modify(.{
.INT = integer,
.FRAC = fraction,
@ -80,6 +85,7 @@ pub inline fn setSliceClkDiv(comptime slice: u32, integer: u8, fraction: u4) voi
}
pub inline fn setSliceClkDivMode(comptime slice: u32, mode: ClkDivMode) void {
log.debug("PWM{} set clk div mode: {}", .{ slice, mode });
getRegs(slice).csr.modify(.{
.DIVMODE = @enumToInt(mode),
});
@ -101,6 +107,7 @@ pub inline fn setChannelInversion(
}
pub inline fn setSliceWrap(comptime slice: u32, wrap: u16) void {
log.debug("PWM{} set wrap: {}", .{ slice, wrap });
getRegs(slice).top.raw = wrap;
}
@ -109,6 +116,7 @@ pub inline fn setChannelLevel(
comptime channel: Channel,
level: u16,
) void {
log.debug("PWM{} {} set level: {}", .{ slice, channel, level });
switch (channel) {
.a => getRegs(slice).cc.modify(.{ .A = level }),
.b => getRegs(slice).cc.modify(.{ .B = level }),

@ -2,6 +2,8 @@ const std = @import("std");
const microzig = @import("microzig");
const gpio = @import("gpio.zig");
const clocks = @import("clocks.zig");
const resets = @import("resets.zig");
const time = @import("time.zig");
const assert = std.debug.assert;
const regs = microzig.chip.registers;
@ -39,7 +41,7 @@ pub const UartRegs = extern struct {
rsr: u32,
reserved0: [4]u32,
fr: @typeInfo(@TypeOf(regs.UART0.UARTFR)).Pointer.child,
resertev1: [1]u32,
reserved1: [1]u32,
ilpr: u32,
ibrd: u32,
fbrd: u32,
@ -149,16 +151,8 @@ pub const UART = enum {
pub fn reset(uart: UART) void {
switch (uart) {
.uart0 => {
regs.RESETS.RESET.modify(.{ .uart0 = 1 });
regs.RESETS.RESET.modify(.{ .uart0 = 0 });
while (regs.RESETS.RESET_DONE.read().uart0 != 1) {}
},
.uart1 => {
regs.RESETS.RESET.modify(.{ .uart1 = 1 });
regs.RESETS.RESET.modify(.{ .uart1 = 0 });
while (regs.RESETS.RESET_DONE.read().uart1 != 1) {}
},
.uart0 => resets.reset(&.{.uart0}),
.uart1 => resets.reset(&.{.uart1}),
}
}
@ -180,11 +174,13 @@ pub const UART = enum {
.one => @as(u1, 0),
.two => @as(u1, 1),
},
.PEN = if (parity != .none) @as(u1, 1) else @as(u1, 0),
.PEN = switch (parity) {
.none => @as(u1, 0),
.even, .odd => @as(u1, 1),
},
.EPS = switch (parity) {
.even => @as(u1, 1),
.odd => @as(u1, 0),
else => @as(u1, 0),
.odd, .none => @as(u1, 0),
},
});
}
@ -210,3 +206,30 @@ pub const UART = enum {
uart_regs.lcr_h.modify(.{});
}
};
var uart_logger: ?UART.Writer = null;
pub fn initLogger(uart: UART) void {
uart_logger = uart.writer();
}
pub fn log(
comptime level: std.log.Level,
comptime scope: @TypeOf(.EnumLiteral),
comptime format: []const u8,
args: anytype,
) void {
const level_prefix = comptime "[{}.{:0>6}] " ++ level.asText();
const prefix = comptime level_prefix ++ switch (scope) {
.default => ": ",
else => " (" ++ @tagName(scope) ++ "): ",
};
if (uart_logger) |uart| {
const current_time = time.getTimeSinceBoot();
const seconds = current_time.us_since_boot / std.time.us_per_s;
const microseconds = current_time.us_since_boot % std.time.us_per_s;
uart.print(prefix ++ format ++ "\r\n", .{ seconds, microseconds } ++ args) catch {};
}
}

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save