Skip to content
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

Replace internal knowledge in jtag3.c by a public API #996

Merged
merged 2 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 6 additions & 6 deletions src/avr.c
Original file line number Diff line number Diff line change
Expand Up @@ -439,16 +439,16 @@ int avr_read(PROGRAMMER * pgm, AVRPART * p, char * memtype,
(vmem->tags[i] & TAG_ALLOCATED) != 0)
{
rc = pgm->read_byte(pgm, p, mem, i, mem->buf + i);
if (rc != 0) {
if (rc != LIBAVRDUDE_SUCCESS) {
avrdude_message(MSG_INFO, "avr_read(): error reading address 0x%04lx\n", i);
if (rc == -1) {
if (rc == LIBAVRDUDE_GENERAL_FAILURE) {
avrdude_message(MSG_INFO, " read operation not supported for memory \"%s\"\n",
memtype);
return -2;
return LIBAVRDUDE_NOTSUPPORTED;
}
avrdude_message(MSG_INFO, " read operation failed for memory \"%s\"\n",
memtype);
return rc;
return LIBAVRDUDE_SOFTFAIL;
}
}
report_progress(i, mem->size, NULL);
Expand Down Expand Up @@ -1035,14 +1035,14 @@ int avr_signature(PROGRAMMER * pgm, AVRPART * p)

report_progress (0,1,"Reading");
rc = avr_read(pgm, p, "signature", 0);
if (rc < 0) {
if (rc < LIBAVRDUDE_SUCCESS) {
avrdude_message(MSG_INFO, "%s: error reading signature data for part \"%s\", rc=%d\n",
progname, p->desc, rc);
return rc;
}
report_progress (1,1,NULL);

return 0;
return LIBAVRDUDE_SUCCESS;
}

static uint8_t get_fuse_bitmask(AVRMEM * m) {
Expand Down
23 changes: 16 additions & 7 deletions src/jtag3.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,14 @@ static void jtag3_prmsg(PROGRAMMER * pgm, unsigned char * data, size_t len)
}
}

static int jtag3_errcode(int reason)
{
if (reason == RSP3_FAIL_OCD_LOCKED ||
reason == RSP3_FAIL_CRC_FAILURE)
return LIBAVRDUDE_SOFTFAIL;
return LIBAVRDUDE_GENERAL_FAILURE;
}

static void jtag3_prevent(PROGRAMMER * pgm, unsigned char * data, size_t len)
{
int i;
Expand Down Expand Up @@ -834,7 +842,7 @@ int jtag3_recv(PROGRAMMER * pgm, unsigned char **msg) {
}
}

int jtag3_command(PROGRAMMER *pgm, unsigned char *cmd, unsigned int cmdlen,
int jtag3_command(PROGRAMMER *pgm, unsigned char *cmd, unsigned int cmdlen,
unsigned char **resp, const char *descr)
{
int status;
Expand All @@ -850,17 +858,18 @@ int jtag3_recv(PROGRAMMER * pgm, unsigned char **msg) {
putc('\n', stderr);
avrdude_message(MSG_NOTICE2, "%s: %s command: timeout/error communicating with programmer (status %d)\n",
progname, descr, status);
return -1;
return LIBAVRDUDE_GENERAL_FAILURE;
} else if (verbose >= 3) {
putc('\n', stderr);
jtag3_prmsg(pgm, *resp, status);
} else {
avrdude_message(MSG_NOTICE2, "0x%02x (%d bytes msg)\n", (*resp)[1], status);
}

c = (*resp)[1];
if ((c & RSP3_STATUS_MASK) != RSP3_OK) {
if ((c == RSP3_FAILED) && ((*resp)[3] == RSP3_FAIL_OCD_LOCKED)) {
c = (*resp)[1] & RSP3_STATUS_MASK;
if (c != RSP3_OK) {
if ((c == RSP3_FAILED) && ((*resp)[3] == RSP3_FAIL_OCD_LOCKED ||
(*resp)[3] == RSP3_FAIL_CRC_FAILURE)) {
avrdude_message(MSG_INFO,
"%s: Device is locked! Chip erase required to unlock.\n",
progname);
Expand All @@ -871,7 +880,7 @@ int jtag3_recv(PROGRAMMER * pgm, unsigned char **msg) {
status = (*resp)[3];
free(*resp);
resp = 0;
return -status;
return jtag3_errcode(status);
}

return status;
Expand Down Expand Up @@ -987,7 +996,7 @@ static int jtag3_program_enable(PROGRAMMER * pgm)
free(resp);
PDATA(pgm)->prog_enabled = 1;

return 0;
return LIBAVRDUDE_SUCCESS;
}

return status;
Expand Down
1 change: 1 addition & 0 deletions src/jtag3_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
# define RSP3_FAIL_WRONG_MODE 0x32 /* progmode vs. non-prog */
# define RSP3_FAIL_UNSUPP_MEMORY 0x34 /* unsupported memory type */
# define RSP3_FAIL_WRONG_LENGTH 0x35 /* wrong lenth for mem access */
# define RSP3_FAIL_CRC_FAILURE 0x43 /* CRC failure in device */
# define RSP3_FAIL_OCD_LOCKED 0x44 /* device is locked */
# define RSP3_FAIL_NOT_UNDERSTOOD 0x91

Expand Down
10 changes: 10 additions & 0 deletions src/libavrdude.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
#include <stdint.h>

typedef uint32_t pinmask_t;
/*
* Values returned by library functions.
* Some library functions also return a count, i.e. a positive
* number greater than 0.
*/
#define LIBAVRDUDE_SUCCESS 0
#define LIBAVRDUDE_GENERAL_FAILURE (-1)
#define LIBAVRDUDE_NOTSUPPORTED (-2) // operation not supported
#define LIBAVRDUDE_SOFTFAIL (-3) // returned by avr_signature() if caller
// might proceed with chip erase

/* formerly lists.h */

Expand Down
5 changes: 2 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1116,9 +1116,8 @@ int main(int argc, char * argv [])
usleep(waittime);
if (init_ok) {
rc = avr_signature(pgm, p);
if (rc != 0) {
// -68 == -(0x44) == -(RSP3_FAIL_OCD_LOCKED)
if ((rc == -68) && (p->flags & AVRPART_HAS_UPDI) && (attempt < 1)) {
if (rc != LIBAVRDUDE_SUCCESS) {
if ((rc == LIBAVRDUDE_SOFTFAIL) && (p->flags & AVRPART_HAS_UPDI) && (attempt < 1)) {
attempt++;
if (pgm->read_sib) {
// Read SIB and compare FamilyID
Expand Down