Skip to content

Commit 9385613

Browse files
committed
try to improve error handling on a failed data transmission plus some minor adjustments and fixes
1 parent 29438a2 commit 9385613

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

src/pickit5.c

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -449,10 +449,29 @@ static int pickit5_download_data(const PROGRAMMER *pgm, const unsigned char *scr
449449
}
450450
if(usbdev_bulk_send(&pgm->fd, send_buf, send_len) < 0) {
451451
pmsg_error("Transmission failed on the data channel\n");
452+
if(pickit5_send_script_done(pgm) < 0) {
453+
pmsg_error("Failed to abort download mode, please power-cycle the programmer and part\n");
454+
return -3;
455+
}
456+
pmsg_notice("Attemting to recover from transmission error\n");
457+
if(pickit5_program_disable(pgm, NULL) < 0) {
458+
pmsg_error("Failed to disable programming mode, please power-cycle the programmer and part\n");
459+
return -3;
460+
}
461+
if(pickit5_program_enable(pgm, NULL) < 0) {
462+
pmsg_error("Failed to reenable programming mode, please power-cycle the programmer and part\n");
463+
return -3;
464+
}
465+
pmsg_notice("Successfully recovered from transmission error, please retry the previous operation\n");
452466
return -3;
453467
}
454468
if(pickit5_get_status(pgm, CHECK_ERROR) < 0) {
455-
pmsg_error("error check not 'NONE'\n");
469+
pmsg_error("error check not 'NONE' on download\n");
470+
471+
if(pickit5_send_script_done(pgm) < 0) {
472+
pmsg_error("Failed to abort download mode, please power-cycle the programmer and part\n");
473+
return -4;
474+
}
456475
return -4;
457476
}
458477
if(pickit5_send_script_done(pgm) < 0) {
@@ -473,11 +492,23 @@ static int pickit5_upload_data(const PROGRAMMER *pgm, const unsigned char *scr,
473492
return -1;
474493
}
475494
if(pickit5_read_response(pgm) < 0) {
495+
if(pickit5_send_script_done(pgm) < 0) {
496+
pmsg_error("Failed to abort upload mode, please power-cycle the programmer and part\n");
497+
return -2;
498+
}
499+
if(pickit5_program_disable(pgm, NULL) < 0) {
500+
pmsg_error("Failed to disable programming mode, please power-cycle the programmer and part\n");
501+
return -2;
502+
}
503+
if(pickit5_program_enable(pgm, NULL) < 0) {
504+
pmsg_error("Failed to reenable programming mode, please power-cycle the programmer and part\n");
505+
return -2;
506+
}
476507
return -2;
477508
}
478509
if(usbdev_bulk_recv(&pgm->fd, recv_buf, recv_len) < 0) {
479510
pmsg_error("reading data memory failed\n");
480-
return -3;
511+
//return -3; // Do not abort here, try to send script done
481512
}
482513
if(pickit5_send_script_done(pgm) < 0) {
483514
pmsg_error("sending script done message failed\n");
@@ -735,13 +766,14 @@ static int pickit5_updi_init(const PROGRAMMER *pgm, const AVRPART *p, double v_t
735766
}
736767
pickit5_set_sck_period(pgm, 1.0 / 100000); // Start with 100 kHz
737768
pickit5_updi_write_cs_reg(pgm, UPDI_ASI_CTRLA, 0x01); // Change UPDI clock to 16 MHz
738-
unsigned char ret_val = 0;
739769

770+
unsigned char ret_val = 0;
740771
pickit5_updi_read_cs_reg(pgm, UPDI_ASI_CTRLA, &ret_val);
741772
if(ret_val != 0x01) {
742773
pmsg_warning("failed to change UPDI clock, falling back to 225 kHz\n");
743774
baud = 225000;
744775
}
776+
// Possible speed optimization: Reduce Guard Time Value and maybe response signature?
745777
}
746778
}
747779

@@ -776,7 +808,7 @@ static int pickit5_initialize(const PROGRAMMER *pgm, const AVRPART *p) {
776808
default_baud = 125000;
777809
} else if(both_jtag(pgm, p) || both_xmegajtag(pgm, p)) {
778810
rc = get_pickit_jtag_script(&(my.scripts), p->desc);
779-
default_baud = 200000;
811+
default_baud = 500000;
780812
} else if(both_updi(pgm, p)) {
781813
rc = get_pickit_updi_script(&(my.scripts), p->desc);
782814
default_baud = 200000;
@@ -785,7 +817,7 @@ static int pickit5_initialize(const PROGRAMMER *pgm, const AVRPART *p) {
785817
default_baud = 125000;
786818
} else if(both_pdi(pgm, p)) {
787819
rc = get_pickit_pdi_script(&(my.scripts), p->desc);
788-
default_baud = 400000;
820+
default_baud = 500000;
789821
}
790822

791823
if(rc == -1) {
@@ -900,6 +932,7 @@ static int pickit5_cmd(const PROGRAMMER *pgm, const unsigned char *cmd, unsigned
900932
}
901933

902934
static int pickit5_program_enable(const PROGRAMMER *pgm, const AVRPART *p) {
935+
(void) p; // Warning! this file is passing NULL at some point
903936
pmsg_debug("%s()\n", __func__);
904937
const unsigned char *enter_prog = my.scripts.EnterProgMode;
905938
unsigned int enter_prog_len = my.scripts.EnterProgMode_len;
@@ -922,6 +955,7 @@ static int pickit5_program_enable(const PROGRAMMER *pgm, const AVRPART *p) {
922955
}
923956

924957
static int pickit5_program_disable(const PROGRAMMER *pgm, const AVRPART *p) {
958+
(void) p; // Warning! this file is passing NULL at some point
925959
pmsg_debug("%s()\n", __func__);
926960
const unsigned char *exit_prog = my.scripts.ExitProgMode;
927961
unsigned int exit_prog_len = my.scripts.ExitProgMode_len;
@@ -1168,7 +1202,7 @@ static int pickit5_write_array(const PROGRAMMER *pgm, const AVRPART *p,
11681202

11691203
int rc = pickit5_download_data(pgm, write_bytes, write_bytes_len, param, 8, value, len);
11701204
if(rc < 0) {
1171-
return -1;
1205+
return LIBAVRDUDE_EXIT; // Any error here means that a write fail occured, so restart
11721206
} else {
11731207
return len;
11741208
}
@@ -1268,7 +1302,7 @@ static int pickit5_read_array(const PROGRAMMER *pgm, const AVRPART *p,
12681302
int rc = pickit5_upload_data(pgm, read_bytes, read_bytes_len, param, 8, value, len);
12691303

12701304
if(rc < 0) {
1271-
return -1;
1305+
return LIBAVRDUDE_EXIT; // Any error here means that a read fail occured, better restart
12721306
} else {
12731307
return len;
12741308
}
@@ -1971,7 +2005,7 @@ static int usbdev_bulk_recv(const union filedescriptor *fd, unsigned char *buf,
19712005
static int usbdev_bulk_send(const union filedescriptor *fd, const unsigned char *bp, size_t mlen) {
19722006
int rv;
19732007
const unsigned char *p = bp;
1974-
int tx_size, i = 0;
2008+
int tx_size, i = mlen;
19752009

19762010
if(fd->usb.handle == NULL)
19772011
return -1;

tools/scripts_decoder.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#
2929
# The idea behind this program is to extract the sub-programs defined in the
3030
# script.xml. The original file has a size of about 300MB, containing a lot of
31-
# redundand information (like the XML tags) as well as sub-programs for chips
31+
# redundant information (like the XML tags) as well as sub-programs for chips
3232
# avrdude doesn't support, like ARM MCUs.
3333
# This python script goes through all functions, removing identical ones,
3434
# and indexes those. The index is then used to connect the MCUs with those functions
@@ -42,7 +42,6 @@
4242

4343

4444
# The list of functions, as a Python Dictionary, that will be used by avr-dude
45-
# The complete list of available functions can be found below
4645
c_func_list = [
4746
# Started with UPDI
4847
"EnterProgMode",
@@ -201,7 +200,7 @@ def find_xml():
201200
print("List of scripts.xml files:")
202201
print(result)
203202
time, path = 0, ""
204-
for t, p in result:
203+
for t, p in result: # find the most recent scripts file in out list
205204
if t > time:
206205
time, path = t, p
207206
return path
@@ -224,8 +223,6 @@ def find_xml():
224223

225224

226225

227-
# A sum of the previous functions, generating the c/h files
228-
# without needing any intermediate files
229226
def convert_xml(xml_path, c_funcs):
230227
if xml_path == None:
231228
print("No Path to XML file provided")

0 commit comments

Comments
 (0)