Replace all but one (very unlikely to trigger) exit(1)

by return -1.  exit(1) from a library function is not going to make
many friends upstream (in particular the day someone uses that as
part of a GUI frontend).


git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@800 81a1dc3b-b13d-400b-aceb-764788c761c2
This commit is contained in:
joerg_wunsch 2009-02-18 20:20:56 +00:00
parent 1c20a074e3
commit 81faade20a
2 changed files with 45 additions and 25 deletions

View File

@ -1,3 +1,8 @@
2009-02-18 Joerg Wunsch <j.gnu@uriah.heep.sax.de>
* usbtiny.c: Replace all but one (very unlikely to trigger) exit(1)
by return -1.
2009-02-18 Joerg Wunsch <j.gnu@uriah.heep.sax.de> 2009-02-18 Joerg Wunsch <j.gnu@uriah.heep.sax.de>
Submitted by Dick Streefland: Submitted by Dick Streefland:

View File

@ -91,7 +91,7 @@ static int usb_control (PROGRAMMER * pgm,
USB_TIMEOUT ); // default timeout USB_TIMEOUT ); // default timeout
if(nbytes < 0){ if(nbytes < 0){
fprintf(stderr, "\n%s: error: usbtiny_transmit: %s\n", progname, usb_strerror()); fprintf(stderr, "\n%s: error: usbtiny_transmit: %s\n", progname, usb_strerror());
exit(1); return -1;
} }
return nbytes; return nbytes;
@ -124,7 +124,7 @@ static int usb_in (PROGRAMMER * pgm,
} }
fprintf(stderr, "\n%s: error: usbtiny_receive: %s (expected %d, got %d)\n", fprintf(stderr, "\n%s: error: usbtiny_receive: %s (expected %d, got %d)\n",
progname, usb_strerror(), buflen, nbytes); progname, usb_strerror(), buflen, nbytes);
exit(1); return -1;
} }
// Report the number of retries, and reset the counter. // Report the number of retries, and reset the counter.
@ -158,7 +158,7 @@ static int usb_out (PROGRAMMER * pgm,
if (nbytes != buflen) { if (nbytes != buflen) {
fprintf(stderr, "\n%s: error: usbtiny_send: %s (expected %d, got %d)\n", fprintf(stderr, "\n%s: error: usbtiny_send: %s (expected %d, got %d)\n",
progname, usb_strerror(), buflen, nbytes); progname, usb_strerror(), buflen, nbytes);
exit(1); return -1;
} }
return nbytes; return nbytes;
@ -220,7 +220,7 @@ static int usbtiny_open(PROGRAMMER* pgm, char* name)
if (!PDATA(pgm)->usb_handle) { if (!PDATA(pgm)->usb_handle) {
fprintf( stderr, "%s: Error: Could not find USBtiny device (0x%x/0x%x)\n", fprintf( stderr, "%s: Error: Could not find USBtiny device (0x%x/0x%x)\n",
progname, USBTINY_VENDOR, USBTINY_PRODUCT ); progname, USBTINY_VENDOR, USBTINY_PRODUCT );
exit(1); return -1;
} }
return 0; // If we got here, we must have found a good USB device return 0; // If we got here, we must have found a good USB device
@ -269,7 +269,9 @@ static int usbtiny_set_sck_period (PROGRAMMER *pgm, double v)
} }
// send the command to the usbtiny device. // send the command to the usbtiny device.
usb_control(pgm, USBTINY_POWERUP, PDATA(pgm)->sck_period, RESET_LOW); // MEME: for at90's fix resetstate? // MEME: for at90's fix resetstate?
if (usb_control(pgm, USBTINY_POWERUP, PDATA(pgm)->sck_period, RESET_LOW) < 0)
return -1;
// with the new speed, we'll have to update how much data we send per usb transfer // with the new speed, we'll have to update how much data we send per usb transfer
usbtiny_set_chunk_size(pgm, PDATA(pgm)->sck_period); usbtiny_set_chunk_size(pgm, PDATA(pgm)->sck_period);
@ -292,7 +294,9 @@ static int usbtiny_initialize (PROGRAMMER *pgm, AVRPART *p )
printf( "%s: Using SCK period of %d usec\n", printf( "%s: Using SCK period of %d usec\n",
progname, PDATA(pgm)->sck_period ); progname, PDATA(pgm)->sck_period );
} }
usb_control(pgm, USBTINY_POWERUP, PDATA(pgm)->sck_period, RESET_LOW ); if (usb_control(pgm, USBTINY_POWERUP,
PDATA(pgm)->sck_period, RESET_LOW ) < 0)
return -1;
usbtiny_set_chunk_size(pgm, PDATA(pgm)->sck_period); usbtiny_set_chunk_size(pgm, PDATA(pgm)->sck_period);
} }
@ -302,8 +306,11 @@ static int usbtiny_initialize (PROGRAMMER *pgm, AVRPART *p )
// Attempt to use the underlying avrdude methods to connect (MEME: is this kosher?) // Attempt to use the underlying avrdude methods to connect (MEME: is this kosher?)
if (! usbtiny_avr_op(pgm, p, AVR_OP_PGM_ENABLE, res)) { if (! usbtiny_avr_op(pgm, p, AVR_OP_PGM_ENABLE, res)) {
// no response, RESET and try again // no response, RESET and try again
usb_control(pgm, USBTINY_POWERUP, PDATA(pgm)->sck_period, RESET_HIGH); if (usb_control(pgm, USBTINY_POWERUP,
usb_control(pgm, USBTINY_POWERUP, PDATA(pgm)->sck_period, RESET_LOW); PDATA(pgm)->sck_period, RESET_HIGH) < 0 ||
usb_control(pgm, USBTINY_POWERUP,
PDATA(pgm)->sck_period, RESET_LOW) < 0)
return -1;
usleep(50000); usleep(50000);
if ( ! usbtiny_avr_op( pgm, p, AVR_OP_PGM_ENABLE, res)) { if ( ! usbtiny_avr_op( pgm, p, AVR_OP_PGM_ENABLE, res)) {
// give up // give up
@ -335,6 +342,8 @@ static int usbtiny_cmd(PROGRAMMER * pgm, unsigned char cmd[4], unsigned char res
(cmd[1] << 8) | cmd[0], // convert to 16-bit words (cmd[1] << 8) | cmd[0], // convert to 16-bit words
(cmd[3] << 8) | cmd[2], // " (cmd[3] << 8) | cmd[2], // "
res, 4, 8 * PDATA(pgm)->sck_period ); res, 4, 8 * PDATA(pgm)->sck_period );
if (nbytes < 0)
return -1;
check_retries(pgm, "SPI command"); check_retries(pgm, "SPI command");
if (verbose > 1) { if (verbose > 1) {
// print out the data we sent and received // print out the data we sent and received
@ -404,14 +413,17 @@ static int usbtiny_paged_load (PROGRAMMER * pgm, AVRPART * p, AVRMEM* m,
// Send the chunk of data to the USBtiny with the function we want // Send the chunk of data to the USBtiny with the function we want
// to perform // to perform
usb_in(pgm, if (usb_in(pgm,
function, // EEPROM or flash function, // EEPROM or flash
0, // delay between SPI commands 0, // delay between SPI commands
i, // index i, // index
m->buf + i, // pointer to where we store data m->buf + i, // pointer to where we store data
chunk, // number of bytes chunk, // number of bytes
32 * PDATA(pgm)->sck_period); // each byte gets turned into a 4-byte SPI cmd 32 * PDATA(pgm)->sck_period) // each byte gets turned into a 4-byte SPI cmd
< 0) {
// usb_in() multiplies this per byte. // usb_in() multiplies this per byte.
return -1;
}
// Tell avrdude how we're doing to provide user feedback // Tell avrdude how we're doing to provide user feedback
report_progress(i + chunk, n_bytes, NULL ); report_progress(i + chunk, n_bytes, NULL );
@ -446,7 +458,8 @@ static int usbtiny_paged_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
if (! m->paged) { if (! m->paged) {
// Does this chip not support paged writes? // Does this chip not support paged writes?
i = (m->readback[1] << 8) | m->readback[0]; i = (m->readback[1] << 8) | m->readback[0];
usb_control(pgm, USBTINY_POLL_BYTES, i, 0 ); if (usb_control(pgm, USBTINY_POLL_BYTES, i, 0 ) < 0)
return -1;
delay = m->max_write_delay; delay = m->max_write_delay;
} }
@ -462,16 +475,18 @@ static int usbtiny_paged_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
if (chunk > n_bytes-i) if (chunk > n_bytes-i)
chunk = n_bytes - i; chunk = n_bytes - i;
usb_out(pgm, if (usb_out(pgm,
function, // Flash or EEPROM function, // Flash or EEPROM
delay, // How much to wait between each byte delay, // How much to wait between each byte
i, // Index of data i, // Index of data
m->buf + i, // Pointer to data m->buf + i, // Pointer to data
chunk, // Number of bytes to write chunk, // Number of bytes to write
32 * PDATA(pgm)->sck_period + delay // each byte gets turned into a 32 * PDATA(pgm)->sck_period + delay // each byte gets turned into a
// 4-byte SPI cmd usb_out() multiplies // 4-byte SPI cmd usb_out() multiplies
// this per byte. Then add the cmd-delay // this per byte. Then add the cmd-delay
); ) < 0) {
return -1;
}
next = i + chunk; // Calculate what address we're at now next = i + chunk; // Calculate what address we're at now
if (m->paged if (m->paged
@ -520,7 +535,7 @@ static int usbtiny_nousb_open(struct programmer_t *pgm, char * name)
fprintf(stderr, "%s: error: no usb support. Please compile again with libusb installed.\n", fprintf(stderr, "%s: error: no usb support. Please compile again with libusb installed.\n",
progname); progname);
exit(1); return -1;
} }
void usbtiny_initpgm(PROGRAMMER * pgm) void usbtiny_initpgm(PROGRAMMER * pgm)