Skip to content
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
1 change: 1 addition & 0 deletions drivers/build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.{
.name = .mz_drivers,
.fingerprint = 0x576453eedc5af46e,
.minimum_zig_version = "0.15.1",
.version = "0.0.1",
.paths = .{
"build.zig",
Expand Down
2 changes: 2 additions & 0 deletions drivers/framework.zig
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ pub const wireless = struct {
pub const Cyw43_Bus = cyw43_bus.Cyw43_Bus;
pub const Cyw43_Runner = cyw43_runner.Cyw43_Runner;
// pub const sx1278 = @import("wireless/sx1278.zig");

pub const Cyw43439 = @import("wireless/cyw43439.zig");
};

pub const time = struct {
Expand Down
99 changes: 99 additions & 0 deletions drivers/wireless/cyw43439.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
const std = @import("std");
const mem = std.mem;
const assert = std.debug.assert;

const Bus = @import("cyw43439/bus.zig");
const WiFi = @import("cyw43439/wifi.zig");

const log = std.log.scoped(.cyw43);

const Self = @This();

bus: Bus = undefined,
wifi: WiFi = undefined,
mac: [6]u8 = @splat(0),

pub fn init(
self: *Self,
spi: Bus.Spi,
sleep_ms: *const fn (delay: u32) void,
) !void {
self.bus = .{ .spi = spi, .sleep_ms = sleep_ms };
try self.bus.init();

self.wifi = .{ .bus = &self.bus };
try self.wifi.init();

self.mac = try self.read_mac();
}

pub fn join(self: *Self, ssid: []const u8, pwd: []const u8, opt: WiFi.JoinOptions) !void {
try self.wifi.join(ssid, pwd, opt);
}

fn show_clm_ver(self: *Self) !void {
var data: [128]u8 = @splat(0);
const n = try self.wifi.get_var("clmver", &data);
var iter = mem.splitScalar(u8, data[0..n], 0x0a);
log.debug("clmver:", .{});
while (iter.next()) |line| {
if (line.len == 0 or line[0] == 0x00) continue;
log.debug(" {s}", .{line});
}
}

fn read_mac(self: *Self) ![6]u8 {
var mac: [6]u8 = @splat(0);
const n = try self.wifi.get_var("cur_etheraddr", &mac);
if (n != mac.len) {
log.err("read_mac unexpected read bytes: {}", .{n});
return error.ReadMacFailed;
}
return mac;
}

pub fn recv_zc(ptr: *anyopaque, bytes: []u8) anyerror!?struct { usize, usize } {
const self: *Self = @ptrCast(@alignCast(ptr));
return self.wifi.recv_zc(bytes);
}

pub fn send_zc(ptr: *anyopaque, bytes: []u8) anyerror!void {
const self: *Self = @ptrCast(@alignCast(ptr));
try self.wifi.send_zc(bytes);
}

pub fn ready(ptr: *anyopaque) bool {
const self: *Self = @ptrCast(@alignCast(ptr));
return self.wifi.has_credit();
}

pub fn gpio(self: *Self, pin: u2) Pin {
assert(pin < 3);
self.wifi.gpio_enable(pin);
return .{
.pin = pin,
.wifi = &self.wifi,
};
}

pub const Pin = struct {
pin: u2,
wifi: *WiFi,

pub fn get(self: *Pin) bool {
return self.wifi.gpio_get(self.pin);
}

pub fn put(self: *Pin, value: u1) void {
self.wifi.gpio_set(self.pin, value);
}

pub fn toggle(self: *Pin) void {
self.wifi.gpio_toggle(self.pin);
}
};

// References:
// https://github.com/embassy-rs/embassy/blob/abb1d8286e2415686150e2e315ca1c380659c3c3/cyw43/src/consts.rs
// https://github.com/jbentham/picowi/blob/main/lib/picowi_regs.h
// https://github.com/georgerobotics/cyw43-driver/blob/dd7568229f3bf7a37737b9e1ef250c26efe75b23/src/cyw43_ll.c#L126
Loading
Loading