const std = @import("std"); const ptg = @import("ptg.zig"); const zli = @import("cli"); const packs = @import("packs.zig"); const hex = @import("hex.zig"); const Session = @import("Session.zig"); const PtgSession = @import("PtgSession.zig"); const program = @import("program.zig"); const tools_mod = @import("tools.zig"); const style = @import("style.zig"); const File = std.Io.File; const Dir = std.Io.Dir; const Init = std.process.Init; const blush = @import("blush"); pub fn main(init: Init) !void { try zli.run(init, "Manage MPLAB X packs and program target devices", &.{ .list("packs", "Manage tool and device packs for MPLAB X", &.{ .cmd(packs.details, "Get details for a pack"), .cmd(packs.install, "Install a tool or device pack"), .cmd(packs.list, "List tool and device packs"), .cmd(packs.uninstall, "Uninstall a tool or device pack"), .cmd(packs.update, "Update all installed packs"), }), .list("program", "Program target with tool", &.{ .cmd(program.run, "Program/erase/verify device via the MPLAB X engine"), .cmd(tools.pk5, "PICkit 5 (native, experimental)"), .cmd(tools.pm3, "PM3"), }), .cmd(tools.tools, "List attached tools"), .list("ptg", "Programmer-to-go", &.{ .cmd(ptg.list, "List images in tool"), .cmd(ptg.active, "Get active image on tool"), .cmd(ptg.set, "Sets active image on tool"), .cmd(ptg.reset, "Reset statistics for image on tool"), .cmd(ptg.add, "Send image to tool"), .cmd(ptg.remove, "Delete image from tool"), }), }); } const tools = struct { pub fn tools(init: Init) !void { var out_buf: [4096]u8 = undefined; var out_writer = File.stdout().writer(init.io, &out_buf); const w = &out_writer.interface; const attached = try tools_mod.listAttached(init.io, init.gpa); defer { for (attached) |t| t.deinit(init.gpa); init.gpa.free(attached); } if (attached.len == 0) { try w.print("No supported tools found.\n", .{}); } for (attached) |t| { try w.print( "{s} serial={s} usb={x:0>4}:{x:0>4} ({s})\n", .{ t.name, t.serial, t.vid, t.pid, t.node }, ); } try w.flush(); } pub fn pm3( init: Init, restricted: zli.Param( "restricted", bool, .optional(false), "Safe/Restricted mode", ), vpp: zli.Param( "vpp", ?f128, .optional(null), "VPP", ), ) !void { _ = init; _ = vpp; _ = restricted; } pub fn pk5( init: Init, device: zli.Param( "device", []const u8, .required, "Target device", ), file: zli.Param( "file", []const u8, .required, "file to load", ), power: zli.Param( "power", bool, .optional(false), "Power target from tool", ), sqtp: zli.Param( "sqtp", ?[]const u8, .optional(null), "SQTP file selection", ), release: zli.Param( "release", bool, .optional(false), "Release from reset", ), regions: zli.Param( "regions", ?[]enum { program, eeprom, id, configuration, boot, auxiliary, all }, .optional(null), "Program regions", ), verify: zli.Param( "verify", ?[]enum { program, eeprom, id, configuration, boot, auxiliary, all }, .optional(null), "Verify memory regions", ), serial: zli.Param( "serial", ?[]const u8, .optional(null), "Serial number of tool to use", ), ) !void { _ = sqtp; _ = release; _ = device; _ = verify; _ = power; _ = regions; var out_buf: [4096]u8 = undefined; var out_writer = File.stdout().writer(init.io, &out_buf); const w = &out_writer.interface; var cwd: Dir = .cwd(); const text = try cwd.readFileAlloc(init.io, file.value, init.gpa, .limited(64 << 20)); defer init.gpa.free(text); var image = try hex.parse(init.gpa, text); defer image.deinit(); const info = try tools_mod.findPickit5(init, serial.value); var sess: Session = try .init(init.io, init.gpa, info.node); defer sess.deinit(); try w.print("Connecting to {s} ({s}, serial {s})\n", .{ info.name, info.node, info.serial }); try w.print( "Image {s}: {d} bytes in {d} segments\n", .{ file.value, image.totalBytes(), image.segments.len }, ); try w.print("Programming not implemented yet\n", .{}); // try sess.endSession(false); try w.flush(); } };