Skip to content

Fix bugs in external flash flashing #2826

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
15 changes: 13 additions & 2 deletions ExtLibs/px4uploader/Firmware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public static Firmware ProcessFirmware(string path)
Console.WriteLine("extf_image_size {0} size {1}", fw.extf_image_size, size);

// pad image to 4-byte length
while ((fw.imagebyte.Length % 4) != 0)
while ((fw.extf_imagebyte.Length % 4) != 0)
{
Array.Resize(ref fw.extf_imagebyte, fw.extf_imagebyte.Length + (4 - (fw.extf_imagebyte.Length % 4)));
}
Expand All @@ -170,7 +170,18 @@ public int crc(int padlen)
{
uint state = __crc32(imagebyte, 0);

for (int i = imagebyte.Length; i < (padlen -1); i += 4)
for (int i = imagebyte.Length; i < (padlen - 1); i += 4)
{
state = __crc32(crcpad, state);
}
return (int)state;
}

public int extf_crc(int padlen)
{
uint state = __crc32(extf_imagebyte, 0);

for (int i = extf_imagebyte.Length; i < (padlen - 1); i += 4)
{
state = __crc32(crcpad, state);
}
Expand Down
149 changes: 114 additions & 35 deletions ExtLibs/px4uploader/Uploader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ public void __getSync()
while(port.BytesToRead == 0)
{
if (DateTime.Now > deadline)
throw new TimeoutException("timeout waiting for responce");
throw new TimeoutException("timeout waiting for response");
Thread.Yield();
}

Expand Down Expand Up @@ -500,6 +500,8 @@ public bool __trySync()
byte c = __recv()[0];
if (c != (byte)Code.INSYNC)
return false;

c = __recv()[0];
if (c != (byte)Code.OK)
return false;

Expand All @@ -522,38 +524,48 @@ public void __erase()
__getInfo(Info.BL_REV);

__send(new byte[] { (byte)Code.CHIP_ERASE, (byte)Code.EOC });
DateTime deadline = DateTime.Now.AddSeconds(20);
while (DateTime.Now < deadline)
{
System.Threading.Thread.Sleep(100);
if (port.BytesToRead > 0)
{
Console.WriteLine("__erase btr "+ port.BytesToRead);
break;
}
}
__wait_for_bytes(1, 20);

__getSync();
}

public void __erase_extf()
public void __erase_extf(Firmware fw)
{
__sync();

// fix for bootloader bug - must see a sync and a get_device
__getInfo(Info.BL_REV);

__send(new byte[] { (byte)Code.EXTF_ERASE, (byte)Code.EOC });
DateTime deadline = DateTime.Now.AddSeconds(20);
while (DateTime.Now < deadline)
byte[] size_bytes = BitConverter.GetBytes(fw.extf_image_size);

__send(new byte[] {(byte)Code.EXTF_ERASE,
size_bytes[0], size_bytes[1], size_bytes[2], size_bytes[3],
(byte) Code.EOC});

__getSync();

int last_pct = 0;
while (true)
{
System.Threading.Thread.Sleep(100);
if (port.BytesToRead > 0)
if (last_pct < 95)
{
int pct = __recv()[0];
if (last_pct != pct)
{
last_pct = pct;

if (ProgressEvent != null)
ProgressEvent(pct);
}
}
else if (__trySync())
{
Console.WriteLine("__erase_extf btr " + port.BytesToRead);
if (ProgressEvent != null)
ProgressEvent(100);

break;
}
}
__getSync();
}

public void __program_multi_extf(byte[] data)
Expand Down Expand Up @@ -589,6 +601,21 @@ public bool __verify_multi(byte[] data)
return true;
}

bool __wait_for_bytes(int num_bytes, int timeout_secs)
{
DateTime deadline = DateTime.Now.AddSeconds(timeout_secs);
while (DateTime.Now < deadline)
{
port.BaseStream.Flush();
if (port.BytesToRead >= num_bytes)
{
return true;
}
Thread.Yield();
}
return false;
}

bool isMatch(byte[] d1, byte[] d2)
{
if (d1.Length != d2.Length)
Expand Down Expand Up @@ -727,9 +754,34 @@ public void __verify_v3(Firmware fw)

public void __verify_extf(Firmware fw)
{
int expect_crc = fw.crc(self.extf_maxsize);
int expect_crc = fw.extf_crc(fw.extf_image_size);

byte[] size_bytes = BitConverter.GetBytes(fw.extf_image_size);

__send(new byte[] {(byte)Code.EXTF_GET_CRC,
(byte) Code.EOC});
size_bytes[0], size_bytes[1], size_bytes[2], size_bytes[3],
(byte) Code.EOC});

DateTime deadline = DateTime.Now.AddSeconds(10);
while (DateTime.Now < deadline)
{
port.BaseStream.Flush();
if (port.BytesToRead >= 4)
{
if (ProgressEvent != null)
ProgressEvent(100);
break;
}
if (ProgressEvent != null)
{
TimeSpan ts = new TimeSpan(deadline.Ticks - DateTime.Now.Ticks);
ProgressEvent(((10.0 - ts.TotalSeconds) / 10.0) * 100.0);
}
Thread.Yield();
}

__wait_for_bytes(4, 20);

int report_crc = __recv_int();
__getSync();

Expand All @@ -742,25 +794,52 @@ public void __verify_extf(Firmware fw)
}



public void currentChecksum(Firmware fw)
{
if (self.bl_rev < 3)
return;

__sync();

this.port.ReadTimeout = 1000; // 1 sec

int expect_crc = fw.crc(self.fw_maxsize);
__send(new byte[] {(byte)Code.GET_CRC,
(byte) Code.EOC});
int report_crc = __recv_int();
self.__getSync();
if (self.fw_maxsize > 0 && false)
{
int expect_crc = fw.crc(self.fw_maxsize);
__send(new byte[] {(byte)Code.GET_CRC,
(byte) Code.EOC});
int report_crc = __recv_int();
self.__getSync();

print("FW File 0x" + hexlify(BitConverter.GetBytes(expect_crc)) + " " + expect_crc);
print("Current 0x" + hexlify(BitConverter.GetBytes(report_crc)) + " " + report_crc);
print("FW File 0x" + hexlify(BitConverter.GetBytes(expect_crc)) + " " + expect_crc);
print("Current 0x" + hexlify(BitConverter.GetBytes(report_crc)) + " " + report_crc);

if (expect_crc == report_crc)
throw new Exception("Same Firmware. Not uploading");
}

if (self.extf_maxsize > 0)
{
int expect_crc = fw.extf_crc(fw.extf_image_size);

if (expect_crc == report_crc)
throw new Exception("Same Firmware. Not uploading");
byte[] size_bytes = BitConverter.GetBytes(fw.extf_image_size);

__send(new byte[] {(byte)Code.EXTF_GET_CRC,
size_bytes[0], size_bytes[1], size_bytes[2], size_bytes[3],
(byte) Code.EOC});

// crc can be slow, give it 10s
__wait_for_bytes(4, 30);

int report_crc = __recv_int();
self.__getSync();

print("Ext FW File 0x" + hexlify(BitConverter.GetBytes(expect_crc)) + " " + expect_crc);
print("Current 0x" + hexlify(BitConverter.GetBytes(report_crc)) + " " + report_crc);

if (expect_crc == report_crc)
throw new Exception("Same Firmware. Not uploading");
}
}

public void identify()
Expand Down Expand Up @@ -847,11 +926,11 @@ public void upload(Firmware fw)
}
if(fw.extf_image_size > 0)
{
print("erase...");
self.__erase_extf();
print("program...");
print("erase extf...");
self.__erase_extf(fw);
print("program extf...");
self.__program_extf(fw);
print("verify...");
print("verify extf...");
self.__verify_extf(fw);
}

Expand Down
12 changes: 10 additions & 2 deletions Utilities/Firmware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -705,9 +705,9 @@ public bool UploadPX4(string filename, BoardDetect.boards board)

updateProgress(-1, port + " Identify");
log.InfoFormat(
"Found board type {0} brdrev {1} blrev {2} fwmax {3} chip {5:X} chipdes {6} on {4}",
"Found board type {0} brdrev {1} blrev {2} fwmax {3} chip {5:X} chipdes {6} on {4} extmax {7}",
up.board_type,
up.board_rev, up.bl_rev, up.fw_maxsize, port, up.chip, up.chip_desc);
up.board_rev, up.bl_rev, up.fw_maxsize, port, up.chip, up.chip_desc, up.extf_maxsize);

up.ProgressEvent += new Uploader.ProgressEventHandler(up_ProgressEvent);
up.LogEvent += new Uploader.LogEventHandler(up_LogEvent);
Expand Down Expand Up @@ -746,6 +746,14 @@ public bool UploadPX4(string filename, BoardDetect.boards board)
result = false;
return false;
}
catch (TimeoutException ex)
{
log.Error(ex);
CustomMessageBox.Show("lost communication with the board.", "comms timeout");
uploader.close();
result = false;
return false;
}
catch
{
uploader.__reboot();
Expand Down