Skip to content

Commit

Permalink
Bug Fix
Browse files Browse the repository at this point in the history
 From  ElTangas/jtag2updi:
The response generated by the JTAG2::enter_progmode() function was malformed in certain situations. This bug was silently ignored by avrdude, thus not detected until now. Thanks to @kosilin for detecting this.
  • Loading branch information
Dlloydev committed Mar 27, 2022
1 parent 0b219a7 commit 23e03d7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
24 changes: 11 additions & 13 deletions JTAG2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,15 @@ void JTAG2::delay_exec() {
}

// *** Set status function ***
void JTAG2::set_status(uint8_t status_code) {
void JTAG2::set_status(response status_code) {
packet.size_word[0] = 1;
packet.body[0] = status_code;
}
void JTAG2::set_status(response status_code, uint8_t param) {
packet.size_word[0] = 2;
packet.body[0] = status_code;
packet.body[1] = param;
}

// *** General command functions ***

Expand Down Expand Up @@ -194,9 +199,9 @@ void JTAG2::enter_progmode() {
break;
}
// At this point we need to check if the chip is locked, if so don't attempt to enter program mode
// This is because the previous chip reset may enable a lock bit that was just written (it only becomes active after reset).
if (UPDI::CPU_mode<0x01>()) {
packet.body[0] = RSP_ILLEGAL_MCU_STATE;
packet.body[1] = system_status; // return system status (bit 0 will be set to indicate the MCU is locked)
set_status(RSP_ILLEGAL_MCU_STATE, system_status | 0x01); // return system status (bit 0 will be set to indicate the MCU is locked)
break;
}
// Now we have time to enter program mode (this mode also disables the WDT)
Expand Down Expand Up @@ -243,8 +248,7 @@ void JTAG2::enter_progmode() {
break;
default:
// If we're somehow NOT in programming mode now, that's no good - inform host of this unfortunate state of affairs
packet.body[0] = RSP_ILLEGAL_MCU_STATE;
packet.body[1] = system_status; // return whatever system status caused this error
set_status(RSP_ILLEGAL_MCU_STATE, system_status); // return whatever system status caused this error
}
}

Expand Down Expand Up @@ -276,9 +280,7 @@ void JTAG2::leave_progmode() {
break;
// in other modes fail and inform host of wrong mode
default:
packet.size_word[0] = 2;
packet.body[0] = RSP_ILLEGAL_MCU_STATE;
packet.body[1] = system_status; // 0x01;
set_status(RSP_ILLEGAL_MCU_STATE, 0x01);
}
}

Expand All @@ -294,9 +296,7 @@ void JTAG2::go() {
void JTAG2::read_mem() {
if (UPDI::CPU_mode() != 0x08){
// fail if not in program mode
packet.size_word[0] = 2;
packet.body[0] = RSP_ILLEGAL_MCU_STATE;
packet.body[1] = 0x01;
set_status(RSP_ILLEGAL_MCU_STATE, 0x01);
}
else {
// in program mode
Expand Down Expand Up @@ -642,5 +642,3 @@ namespace {
#endif
}
}


6 changes: 3 additions & 3 deletions JTAG2.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace JTAG2 {
BAUD_256000,
BAUD_512000,
BAUD_1024000,
// Extension to jtagmkII protocol: extra baud rates, decimal series.
// Extension to jtagmkII protocol: extra baud rates, binary series.
BAUD_150000,
BAUD_200000,
BAUD_250000,
Expand All @@ -55,7 +55,6 @@ namespace JTAG2 {
BAUD_1500000,
BAUD_2000000,
BAUD_3000000,

BAUD_LOWER = BAUD_2400,
BAUD_UPPER = BAUD_3000000
};
Expand Down Expand Up @@ -172,7 +171,8 @@ namespace JTAG2 {
void delay_exec();

// *** Set status function ***
void set_status(uint8_t) __attribute__ ((noinline));
void set_status(response) __attribute__ ((noinline));
void set_status(response, uint8_t) __attribute__ ((noinline));

// *** General command functions ***
void sign_on();
Expand Down

0 comments on commit 23e03d7

Please sign in to comment.