Updating fwupdate script it's officially updating the STM32 firmware all

on it's own!!!

It flashes pretty quickly too.  The STlink takes about 8sec to erase and
write.  I'm guessing it's erasing the whole 128KByte though.  My own
fwupdater takes ~3sec to flash, ~1.5sec to erase.  So there might not
even be that much room for speed up.  3sec is hard to beat signficantly
anyway and it comes at the cost of bytes.  Perhaps even complexity and
risk of OUT packet errors/loss on the device side.  So kinda like
leaving it as is.
This commit is contained in:
Paul XPS 2018-11-30 00:37:52 -06:00
parent e3efe04836
commit ff8495455c
1 changed files with 129 additions and 82 deletions

View File

@ -24,12 +24,14 @@ local function erase_main()
print("flash addr:", string.format("%X", rv) ) print("flash addr:", string.format("%X", rv) )
while (curpage<32) do while (curpage<32) do
-- while (curpage<128) do -- while (curpage<128) do --RB has 128KB but last 96KB isn't used (yet)
print("erasing page:", curpage) if(curpage%4 ==0) then
print("erasing page:", curpage)
end
dict.fwupdate("ERASE_1KB_PAGE", curpage) dict.fwupdate("ERASE_1KB_PAGE", curpage)
rv = dict.fwupdate("GET_FLASH_ADDR") --rv = dict.fwupdate("GET_FLASH_ADDR")
print("flash addr:", string.format("%X", rv) ) --print("flash addr:", string.format("%X", rv) )
curpage = curpage+1 curpage = curpage+1
end end
@ -40,107 +42,152 @@ local function update_firmware(newbuild)
print("updating") print("updating")
--open new file first, don't bother continuing if can't find it.
file = assert(io.open(newbuild, "rb"))
--TODO verify the first 2KByte match, don't continue if not..
--enter fwupdate mode --enter fwupdate mode
dict.bootload("PREP_FWUPDATE") dict.bootload("PREP_FWUPDATE")
--now the device will only respond to FWUPDATE dictionary commands --now the device will only respond to FWUPDATE dictionary commands
--open new file
file = assert(io.open(newbuild, "rb"))
--TODO verify first 2KByte matches build
--erase 30KByte of application code --erase 30KByte of application code
-- erase_main() erase_main()
rv = dict.fwupdate("GET_FLASH_ADDR") --Set FLASH->AR to beginging of application section
print("flash addr:", string.format("%X", rv) ) --this can be done be re-erasing it..
print("\n"); --maybe we could have skipped page 2 in erase_main
--or have erase_main count down..
--advance past the first 2KB of build dict.fwupdate("ERASE_1KB_PAGE", 2)
dict.fwupdate("ERASE_1KB_PAGE", 30)
rv = dict.fwupdate("GET_FLASH_ADDR")
print("flash addr:", string.format("%X", rv) )
rv = dict.fwupdate("GET_FLASH_ADDR")
print("flash addr:", string.format("%X", rv) )
rv = dict.fwupdate("GET_FLASH_ADDR")
print("flash addr:", string.format("%X", rv) )
rv = dict.fwupdate("GET_FLASH_ADDR") rv = dict.fwupdate("GET_FLASH_ADDR")
print("flash addr:", string.format("%X", rv) ) print("flash addr:", string.format("%X", rv) )
print("\n"); print("\n");
dict.fwupdate("SET_FLASH_ADDR", 0x7912, 0x01) --advance the file past first 2KByte
rv = dict.fwupdate("GET_FLASH_ADDR") local buffsize = 1
print("flash addr:", string.format("%X", rv) ) local byte, data
local byte_num = 0
for byte in file:lines(buffsize) do
data = string.unpack("B", byte, 1)
--print(string.format("%X", data))
byte_num = byte_num + 1
if byte_num == 2048 then break end
end
-- dict.fwupdate("UNLOCK_FLASH") local offset = 0
local readdata
local data_l
buffsize = 1 --2 bytes at a time
print("Writting half word at at time, but it's not much data..")
while byte_num < (32*1024) do
dict.fwupdate("WR_HWORD", 0xCC33, 0x00) --read next byte from the file and convert to binary
rv = dict.fwupdate("GET_FLASH_ADDR") --gotta be a better way to read a half word (16bits) at a time but don't care right now...
print("flash addr:", string.format("%X", rv) ) byte_str = file:read(buffsize)
rv = dict.fwupdate("GET_FLASH_DATA") if byte_str then
print("flash data:", string.format("%X", rv) ) data_l = string.unpack("B", byte_str, 1)
print("\n"); else
print("\n"); --should only have to make this check for lower byte
--binary file should be even
print("end of file")
break
end
byte_str = file:read(buffsize)
data = string.unpack("B", byte_str, 1)
data = (data<<8)+data_l
-- print("writting:", string.format("%X", data), "addr:", string.format("%X", byte_num))
if( (byte_num % (4*1024)) == 0 ) then
print("flashing KB", byte_num/1024)
end
dict.fwupdate("WR_HWORD", 0x1111, 0x01) --write the data
rv = dict.fwupdate("GET_FLASH_ADDR") dict.fwupdate("WR_HWORD", data, offset)
print("flash addr:", string.format("%X", rv) )
rv = dict.fwupdate("GET_FLASH_DATA")
print("flash data:", string.format("%X", rv) )
print("\n");
dict.fwupdate("WR_HWORD", 0x2222, 0x01) -- if (verify) then
rv = dict.fwupdate("GET_FLASH_ADDR") -- readdata = dict.fwupdate("READ_FLASH", byte_num, 0x00)
print("flash addr:", string.format("%X", rv) ) -- print("read data:", string.format("%X", rv) )
rv = dict.fwupdate("GET_FLASH_DATA") -- if readdata ~= data then
print("flash data:", string.format("%X", rv) ) -- print("ERROR flashing byte number", byte_num, " to flash ", data, readdata)
print("\n"); -- end
-- end
dict.fwupdate("WR_HWORD", 0x4444, 0x02) offset = 1 --this is zero for first byte, but one for all others..
rv = dict.fwupdate("GET_FLASH_ADDR") byte_num = byte_num + 2
print("flash addr:", string.format("%X", rv) ) end
rv = dict.fwupdate("GET_FLASH_DATA") --]]
print("flash data:", string.format("%X", rv) )
print("\n");
dict.fwupdate("WR_HWORD", 0x7777, 0x03) -- dict.fwupdate("WR_HWORD", 0xCC33, 0x00)
rv = dict.fwupdate("GET_FLASH_ADDR") -- rv = dict.fwupdate("GET_FLASH_ADDR")
print("flash addr:", string.format("%X", rv) ) -- print("flash addr:", string.format("%X", rv) )
rv = dict.fwupdate("GET_FLASH_DATA") -- rv = dict.fwupdate("GET_FLASH_DATA")
print("flash data:", string.format("%X", rv) ) -- print("flash data:", string.format("%X", rv) )
print("\n"); -- print("\n");
-- print("\n");
--
-- dict.fwupdate("WR_HWORD", 0x1111, 0x01)
-- rv = dict.fwupdate("GET_FLASH_ADDR")
-- print("flash addr:", string.format("%X", rv) )
-- rv = dict.fwupdate("GET_FLASH_DATA")
-- print("flash data:", string.format("%X", rv) )
-- print("\n");
--
-- dict.fwupdate("WR_HWORD", 0x2222, 0x01)
-- rv = dict.fwupdate("GET_FLASH_ADDR")
-- print("flash addr:", string.format("%X", rv) )
-- rv = dict.fwupdate("GET_FLASH_DATA")
-- print("flash data:", string.format("%X", rv) )
-- print("\n");
--
-- dict.fwupdate("WR_HWORD", 0x4444, 0x02)
-- rv = dict.fwupdate("GET_FLASH_ADDR")
-- print("flash addr:", string.format("%X", rv) )
-- rv = dict.fwupdate("GET_FLASH_DATA")
-- print("flash data:", string.format("%X", rv) )
-- print("\n");
--
-- dict.fwupdate("WR_HWORD", 0x7777, 0x03)
-- rv = dict.fwupdate("GET_FLASH_ADDR")
-- print("flash addr:", string.format("%X", rv) )
-- rv = dict.fwupdate("GET_FLASH_DATA")
-- print("flash data:", string.format("%X", rv) )
-- print("\n");
--
-- dict.fwupdate("WR_HWORD", 0xAAAA, 0x10)
-- rv = dict.fwupdate("GET_FLASH_ADDR")
-- print("flash addr:", string.format("%X", rv) )
-- rv = dict.fwupdate("GET_FLASH_DATA")
-- print("flash data:", string.format("%X", rv) )
-- print("\n");
--
-- dict.fwupdate("WR_HWORD", 0xBBBB, 0x20)
-- rv = dict.fwupdate("GET_FLASH_ADDR")
-- print("flash addr:", string.format("%X", rv) )
-- rv = dict.fwupdate("GET_FLASH_DATA")
-- print("flash data:", string.format("%X", rv) )
-- print("\n");
--
---- dict.fwupdate("LOCK_FLASH")
--
-- rv = dict.fwupdate("READ_FLASH", 0x0000, 0x00)
-- print("read data:", string.format("%X", rv) )
--
-- rv = dict.fwupdate("READ_FLASH", 0x053e, 0x00)
-- print("read data:", string.format("%X", rv) )
--
-- rv = dict.fwupdate("READ_FLASH", 0x791a, 0x00)
-- print("read data:", string.format("%X", rv) )
dict.fwupdate("WR_HWORD", 0xAAAA, 0x10) --close file
rv = dict.fwupdate("GET_FLASH_ADDR") assert(file:close())
print("flash addr:", string.format("%X", rv) ) print("\n\n DONE updating flash. \n\n Reseting device \n\n IGNORE the error that comes next.. \n\n")
rv = dict.fwupdate("GET_FLASH_DATA")
print("flash data:", string.format("%X", rv) )
print("\n");
dict.fwupdate("WR_HWORD", 0xBBBB, 0x20)
rv = dict.fwupdate("GET_FLASH_ADDR")
print("flash addr:", string.format("%X", rv) )
rv = dict.fwupdate("GET_FLASH_DATA")
print("flash data:", string.format("%X", rv) )
print("\n");
-- dict.fwupdate("LOCK_FLASH")
rv = dict.fwupdate("READ_FLASH", 0x0000, 0x00)
print("read data:", string.format("%X", rv) )
rv = dict.fwupdate("READ_FLASH", 0x053e, 0x00)
print("read data:", string.format("%X", rv) )
rv = dict.fwupdate("READ_FLASH", 0x791a, 0x00)
print("read data:", string.format("%X", rv) )
dict.fwupdate("RESET_DEVICE") dict.fwupdate("RESET_DEVICE")
--write build to flash --write build to flash
print("updated") print("updated") --this doesn't print because reset errored us out..
end end
-- global variables so other modules can use them -- global variables so other modules can use them