diff --git a/src/stk500v2.c b/src/stk500v2.c index f5bf5499..eb25dc67 100644 --- a/src/stk500v2.c +++ b/src/stk500v2.c @@ -2124,14 +2124,9 @@ static int stk500v2_open(PROGRAMMER *pgm, const char *port) { if (serdev && serdev->usbsn) pgm->usbsn = serdev->usbsn; - /* - * drain any extraneous input - */ - stk500v2_drain(pgm, 0); - - stk500v2_getsync(pgm); - - stk500v2_drain(pgm, 0); + // Drain any extraneous input, synchronise and drain again + if(stk500v2_drain(pgm, 0) < 0 || stk500v2_getsync(pgm) < 0 || stk500v2_drain(pgm, 0) < 0) + return -1; if (pgm->bitclock != 0.0) { if (pgm->set_sck_period(pgm, pgm->bitclock) != 0) @@ -2181,14 +2176,9 @@ static int stk600_open(PROGRAMMER *pgm, const char *port) { return -1; } - /* - * drain any extraneous input - */ - stk500v2_drain(pgm, 0); - - stk500v2_getsync(pgm); - - stk500v2_drain(pgm, 0); + // Drain any extraneous input, synchronise and drain again + if(stk500v2_drain(pgm, 0) < 0 || stk500v2_getsync(pgm) < 0 || stk500v2_drain(pgm, 0) < 0) + return -1; if (pgm->bitclock != 0.0) { if (pgm->set_sck_period(pgm, pgm->bitclock) != 0)