feat: added build pristine, build configuration, erase and recover

This commit is contained in:
unai committed 2026-10-12 11:51:45 +02:00
1 parent 80a2c4d56f
commit 5b1d640d4b
11 files changed
+636 -80

No files matched your search

+121 -14
View File
@@ -1,41 +1,148 @@
local M = {}
--- nrfutil emite JSONL (un objeto por línea), no un array.
local function extract_devices(obj)
if type(obj) ~= "table" or type(obj.data) ~= "table" then
return nil
end
if obj.data.devices then
return obj.data.devices
end
if type(obj.data.data) == "table" and obj.data.data.devices then
return obj.data.data.devices
end
return nil
end
function M.devices()
local out = vim.fn.system({ "nrfutil", "device", "list", "--json" })
local ok, data = pcall(vim.json.decode, out)
if not ok or type(data) ~= "table" then
local raw = vim.fn.system({ "nrfutil", "device", "list", "--json" })
if vim.v.shell_error ~= 0 then
return {}
end
local devs = {}
for _, d in ipairs(data) do
if d.serial_number then
table.insert(devs, {
sn = d.serial_number,
name = (d.traits and d.traits.product) or "DK",
})
for _, line in ipairs(vim.split(raw, "\n", { plain = true })) do
if line ~= "" then
local ok, obj = pcall(vim.json.decode, line)
if ok and obj then
local list = extract_devices(obj)
if list then
for _, d in ipairs(list) do
if d.serialNumber then
local name = (d.devkit and d.devkit.deviceFamily) or (d.usb and d.usb.product) or "DK"
table.insert(devs, { sn = d.serialNumber, name = name })
end
end
if #devs > 0 then
break
end
end
end
end
end
return devs
end
function M.run(opts)
-- Modos soportados y el flag que añaden a `west flash`.
local MODES = {
flash = { flag = nil, title = "hego://flash" },
erase = { flag = "--erase", title = "hego://flash-erase" },
recover = { flag = "--recover", title = "hego://flash-recover" },
}
local CONFIRM = {
erase = "Se borra TODA la flash: particiones, factory data y estado de comisión.\n¿Seguir?",
recover = "Se borra toda la flash Y se desbloquea APPROTECT.\n"
.. "Es lo que hay que usar cuando el flash normal falla con "
.. "AP-Protect / readback protection.\n¿Seguir?",
}
local function argv(opts, sn, mode)
local a = { "flash", "-d", opts.build_dir, "--dev-id", sn }
if MODES[mode].flag then
table.insert(a, MODES[mode].flag)
end
return a
end
--- mode: "flash" (por defecto) | "erase" | "recover"
function M.run(opts, mode)
mode = mode or "flash"
if not MODES[mode] then
vim.notify("[hego] modo de flash desconocido: " .. tostring(mode), vim.log.levels.ERROR)
return
end
if CONFIRM[mode] then
if vim.fn.confirm(CONFIRM[mode], "&Sí\n&No", 2) ~= 1 then
return
end
end
local devs = M.devices()
if #devs == 0 then
vim.notify("[hego] no hay ninguna placa conectada", vim.log.levels.WARN)
return
end
local exec = require("hego.exec")
local function go(sn)
local exec = require("hego.exec")
local cmd = exec.ncs(opts, { "west", "flash", "-d", opts.build_dir, "--dev-id", sn })
exec.run_async(cmd, { title = "hego://flash" })
exec.run_async(exec.ncs(opts, argv(opts, sn, mode)), {
title = MODES[mode].title,
cwd = vim.fn.getcwd(),
env = exec.zephyr_env(opts),
})
end
if #devs == 1 then
return go(devs[1].sn)
end
vim.ui.select(devs, {
prompt = "¿A qué placa flasheo?",
prompt = "¿A qué placa?",
format_item = function(d)
return d.name .. " (" .. d.sn .. ")"
end,
}, function(c)
if c then
go(c.sn)
end
end)
end
--- Plan B: recuperar la placa sin pasar por west ni por sysbuild.
--- Útil si `west flash --recover` se atraganta con el multi-dominio.
function M.device_recover(opts)
local devs = M.devices()
if #devs == 0 then
vim.notify("[hego] no hay ninguna placa conectada", vim.log.levels.WARN)
return
end
if
vim.fn.confirm("Se borra toda la flash de la placa y se desbloquea APPROTECT.\n¿Seguir?", "&Sí\n&No", 2) ~= 1
then
return
end
local exec = require("hego.exec")
local function go(sn)
-- Ojo: esto NO va envuelto en el toolchain, es nrfutil a pelo.
exec.run_async({ "nrfutil", "device", "recover", "--serial-number", sn }, {
title = "hego://device-recover",
cwd = vim.fn.getcwd(),
})
end
if #devs == 1 then
return go(devs[1].sn)
end
vim.ui.select(devs, {
prompt = "¿Qué placa recupero?",
format_item = function(d)
return d.name .. " (" .. d.sn .. ")"
end,