Skip to content

Commit 56518d3

Browse files
committed
Checksum calculator option for microcontroller.
1 parent c3a4736 commit 56518d3

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

lpc17prl.lua

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ local usage_str = string.format([=[
1111
Usage:
1212
%s
1313
[-vqi] [-O[no-]<option>[=<value>]] [-l <lua-file>]
14-
[-W|-R|-V <file-name>] [-P|-T|-E|-B|-I|-h]
14+
[-W|-R|-V|-X|-XW <file-name>] [-P|-T|-E|-B|-I|-h]
1515
]=], arg[0])
1616

1717
local help_str = [=[
@@ -26,6 +26,8 @@ Modes:
2626
-W Write flash (default)
2727
-R Read flash
2828
-V Verify
29+
-X Calculate and write checksum
30+
-XW Calculate checksum and write flash
2931
without <file-name>:
3032
-P Probe for chip ID (default without -i)
3133
-T Terminal (default for -i)
@@ -77,6 +79,7 @@ local opts = {
7779
baudrate = 115200,
7880
serial = "",
7981
product = "SEPACK-NXP",
82+
write_after = false,
8083
}
8184

8285
function parsekeyopt(s)
@@ -99,20 +102,23 @@ function parsekeyopt(s)
99102
end
100103

101104
do
102-
for o, a in os.getopt(arg, 'qvcihl:WRVPTEBIO:') do
105+
for o, a in os.getopt(arg, 'qvcihl:WRVPTEBIXO:') do
103106
if o == 'q' then opts.verbose = opts.verbose - 1
104107
elseif o == 'v' then opts.verbose = opts.verbose + 1
105108
elseif o == 'i' then opts.interactive = true
106109
elseif o == 'c' then opts.clean = true
107110
elseif o == 'l' then opts.customcode = a
108-
elseif o == 'W' then opts.mode = 'write'
111+
elseif o == 'W' then
112+
if opts.mode == 'calculate' then opts.write_after = true end
113+
opts.mode = 'write'
109114
elseif o == 'R' then opts.mode = 'read'
110115
elseif o == 'V' then opts.mode = 'verify'
111116
elseif o == 'P' then opts.mode = 'probe'
112117
elseif o == 'T' then opts.mode = 'terminal'
113118
elseif o == 'E' then opts.fullerase = true
114119
elseif o == 'B' then opts.mode = 'blank-check'
115120
elseif o == 'I' then opts.mode = 'isp'
121+
elseif o == 'X' then opts.mode = 'calculate'
116122
elseif o == 'h' then help()
117123
elseif o == 'O' then
118124
local a, v = parsekeyopt(a)
@@ -150,6 +156,33 @@ if opts.verbose > 3 then
150156
D.blue'opts:'(opts)
151157
end
152158

159+
if opts.mode == 'calculate' or opts.write_after then
160+
if not opts.fname then D.abort(2, "Binary file not provided") end
161+
162+
local f, err = io.open(opts.fname, "r+b")
163+
if not f then D.abort(2, "Cannot open input file: "..opts.fname..": "..err) end
164+
165+
local sum = 0
166+
167+
for byteno=1,7 do
168+
local bytes = f:read(4)
169+
local int32 = B.dec32LE(bytes)
170+
sum = sum + int32
171+
end
172+
173+
sum = -sum
174+
175+
f:seek("set", 28) -- ensure that we are writing checksum in correct place
176+
f:write(B.enc32LE(sum))
177+
D.green'Checksum stored'()
178+
f:close()
179+
180+
if opts.write_after then opts.mode = 'write'
181+
else os.exit()
182+
end
183+
184+
end
185+
153186
if opts.mode == 'write' or opts.mode == 'verify' then
154187
if not opts.fname then
155188
D.abort(1, "error: no file name given")

0 commit comments

Comments
 (0)