hell yeah it works now!
This commit is contained in:
parent
678709eb1f
commit
19c317f305
|
@ -178,13 +178,14 @@ def jtag_scan(dev: DmjDevice, typ: str, start_pin: int, end_pin: int) -> int:
|
||||||
else:
|
else:
|
||||||
assert False, "wut"
|
assert False, "wut"
|
||||||
|
|
||||||
for i in range(nmatches): print("%02d\t%s" % (i, str(matches[i])))
|
for i in range(nmatches): print("% 2d\t%s" % (i+1, str(matches[i])))
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
else:
|
else:
|
||||||
print("Huh, device replied weird status %d?" % stat)
|
print("Huh, device replied weird status %d?" % stat)
|
||||||
return 1
|
return 1
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
traceback.print_exc()
|
||||||
print("Could not perform JTAG scan: %s" % str(e))
|
print("Could not perform JTAG scan: %s" % str(e))
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
|
|
@ -43,21 +43,20 @@ class JtagMatch(NamedTuple):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "TCK=%d TMS=%d TDI=%d TDO=%d nTRST=%d %s=%d%s" % \
|
return "TCK=%d TMS=%d TDI=%d TDO=%d nTRST=%d %s=%d%s" % \
|
||||||
(self.tck, self.tms, self.tdi, self.tdo, self.ntrst,
|
(self.tck, self.tms, self.tdi, self.tdo, self.ntrst,
|
||||||
"IRLEN" if irlen > 0 else "#toggle", self.irlen if self.irlen > 0 else self.toggle,
|
"IRLEN" if self.irlen > 0 else "#toggle", self.irlen if self.irlen > 0 else self.ntoggle,
|
||||||
(" (W: may be short-circuit: %d)" % self.short_warn) if self.short_warn else '')
|
(" (W: may be short-circuit: %d)" % self.short_warn) if self.short_warn else '')
|
||||||
|
|
||||||
|
|
||||||
class SwdMatch(NamedTuple):
|
class SwdMatch(NamedTuple):
|
||||||
swclk: int
|
swclk: int
|
||||||
swdio: int
|
swdio: int
|
||||||
manuf: int
|
idcode: int
|
||||||
part: int
|
|
||||||
|
|
||||||
def from_bytes(b: bytes) -> SwdMatch:
|
def from_bytes(b: bytes) -> SwdMatch:
|
||||||
assert len(b) == 6
|
assert len(b) == 6
|
||||||
|
|
||||||
clk, dio, m, p = struct.unpack('<BBHH', b)
|
clk, dio, id = struct.unpack('<BBI', b)
|
||||||
return SwdMatch(clk, dio, m, p)
|
return SwdMatch(clk, dio, id)
|
||||||
|
|
||||||
def list_from_bytes(b: bytes) -> List[SwdMatch]:
|
def list_from_bytes(b: bytes) -> List[SwdMatch]:
|
||||||
nmatches = len(b) // 6
|
nmatches = len(b) // 6
|
||||||
|
@ -68,8 +67,8 @@ class SwdMatch(NamedTuple):
|
||||||
return r
|
return r
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "SWCLK=%d SWDIO=%d manufacturer=%03x partno=%04x" % \
|
return "SWCLK=%d SWDIO=%d idcode=%08x" % \
|
||||||
(self.swclk, self.swdio, self.manuf, self.part)
|
(self.swclk, self.swdio, self.idcode)
|
||||||
|
|
||||||
|
|
||||||
def check_statpl(stat, pl, defmsg, minl=None, maxl=None):
|
def check_statpl(stat, pl, defmsg, minl=None, maxl=None):
|
||||||
|
|
|
@ -14,7 +14,7 @@ struct match_rec_jtag {
|
||||||
};
|
};
|
||||||
struct match_rec_swd {
|
struct match_rec_swd {
|
||||||
uint8_t swclk, swdio;
|
uint8_t swclk, swdio;
|
||||||
uint16_t manuf, part;
|
uint16_t idlo, idhi;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define N_MATCHES_JTAG (JSCAN_MAX_RESULT_BYTES / (sizeof(struct match_rec_jtag)))
|
#define N_MATCHES_JTAG (JSCAN_MAX_RESULT_BYTES / (sizeof(struct match_rec_jtag)))
|
||||||
|
@ -283,34 +283,41 @@ static void pulse_clk(uint8_t swclk) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static void reset_line(uint8_t swclk, uint8_t swdio) {
|
static void reset_line(uint8_t swclk, uint8_t swdio) {
|
||||||
|
//printf("rstl\n");
|
||||||
jscan_pin_set(swdio, 1);
|
jscan_pin_set(swdio, 1);
|
||||||
for (int i = 0; i < RESET_SEQUENCE_LENGTH; ++i) {
|
for (int i = 0; i < RESET_SEQUENCE_LENGTH; ++i) {
|
||||||
pulse_clk(swclk);
|
pulse_clk(swclk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void write_bits(uint8_t swclk, uint8_t swdio, uint64_t val, int len) {
|
static void write_bits(uint8_t swclk, uint8_t swdio, uint32_t val, int len) {
|
||||||
for (int i = 0; i < len; ++i) {
|
for (int i = 0; i < len; ++i, val >>= 1) {
|
||||||
jscan_pin_set(swdio, (val >> i) & 1);
|
jscan_pin_set(swdio, (val /*>> i*/) & 1);
|
||||||
|
//printf("wb#%d %lu\n", i, (val /*>> i*/) & 1);
|
||||||
pulse_clk(swclk);
|
pulse_clk(swclk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void read_bits(uint8_t swclk, uint8_t swdio, uint64_t* val, int len) {
|
static uint32_t read_bits(uint8_t swclk, uint8_t swdio, int len) {
|
||||||
|
uint32_t val = 0;
|
||||||
|
|
||||||
jscan_pin_mode(swdio, 0); //setup_m_read();
|
jscan_pin_mode(swdio, 0); //setup_m_read();
|
||||||
|
|
||||||
for (int i = 0; i < len; ++i) {
|
for (int i = 0; i < len; ++i) {
|
||||||
int bit = jscan_pin_get(swdio) ? 1 : 0;
|
uint32_t bit = jscan_pin_get(swdio) ? 1 : 0;
|
||||||
*val = (*val & ~(1 << i)) | (bit << i);
|
//printf("rb#%d %lu\n", i, bit);
|
||||||
|
val |= bit << i;
|
||||||
pulse_clk(swclk);
|
pulse_clk(swclk);
|
||||||
}
|
}
|
||||||
|
|
||||||
jscan_pin_mode(swdio, 1); //setup_m_write();
|
jscan_pin_mode(swdio, 1); //setup_m_write();
|
||||||
|
|
||||||
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline static uint32_t get_ack(uint32_t val) { return val & 0x7; }
|
inline static uint32_t get_ack(uint32_t val) { return val & 0x7; }
|
||||||
inline static uint32_t get_manuf(uint32_t val) { return (val >> 4) & 0x7ff; }
|
/*inline static uint32_t get_manuf(uint32_t val) { return (val >> 1) & 0x7ff; }
|
||||||
inline static uint32_t get_partno(uint32_t val) { return (val >> 15) & 0xffff; }
|
inline static uint32_t get_partno(uint32_t val) { return (val >> 12) & 0xffff; }*/
|
||||||
|
|
||||||
static void turn_around(uint8_t swclk, uint8_t swdio) {
|
static void turn_around(uint8_t swclk, uint8_t swdio) {
|
||||||
jscan_pin_mode(swdio, 0); //setup_m_read();
|
jscan_pin_mode(swdio, 0); //setup_m_read();
|
||||||
|
@ -324,34 +331,38 @@ static void switch_jtag_to_swd(uint8_t swclk, uint8_t swdio) {
|
||||||
write_bits(swclk, swdio, 0x00, 4);
|
write_bits(swclk, swdio, 0x00, 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void read_id_code(uint8_t swclk, uint8_t swdio, uint64_t* buf) {
|
static uint32_t read_id_code(uint8_t swclk, uint8_t swdio, uint32_t* retv) {
|
||||||
write_bits(swclk, swdio, 0xa5, 8);
|
write_bits(swclk, swdio, 0xa5, 8);
|
||||||
turn_around(swclk, swdio);
|
turn_around(swclk, swdio);
|
||||||
read_bits(swclk, swdio, buf, 36);
|
uint32_t ret = read_bits(swclk, swdio, 4); // ack stuff, + 1bit of ID code
|
||||||
|
*retv = read_bits(swclk, swdio, 32);
|
||||||
turn_around(swclk, swdio);
|
turn_around(swclk, swdio);
|
||||||
jscan_pin_mode(swdio, 1); //setup_m_write();
|
jscan_pin_mode(swdio, 1); //setup_m_write();
|
||||||
write_bits(swclk, swdio, 0x00, 8);
|
write_bits(swclk, swdio, 0x00, 8);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool test_swd_lines(uint8_t swclk, uint8_t swdio, uint16_t* manuf, uint16_t* part) {
|
static bool test_swd_lines(uint8_t swclk, uint8_t swdio, uint32_t* idcode) {
|
||||||
uint64_t readbuf = 0;
|
|
||||||
//set_pins(swclk, swdio); // saves pins to globals
|
//set_pins(swclk, swdio); // saves pins to globals
|
||||||
init_pins(swclk, swdio, 0xff, 0xff);
|
init_pins(swclk, swdio, 0xff, 0xff);
|
||||||
switch_jtag_to_swd(swclk, swdio);
|
switch_jtag_to_swd(swclk, swdio);
|
||||||
read_id_code(swclk, swdio, &readbuf);
|
uint32_t readbuf2;
|
||||||
bool result = get_ack(readbuf) == 1;
|
uint32_t readbuf = read_id_code(swclk, swdio, &readbuf2);
|
||||||
init_pins(0xff, 0xff, 0xff, 0xff);
|
init_pins(0xff, 0xff, 0xff, 0xff);
|
||||||
printf("swclk=%hhu swdio=%hhu -> %08llx\n", swclk, swdio, readbuf);
|
bool result = get_ack(readbuf) == 1;
|
||||||
if (result) {
|
uint32_t swd_idcode = ((readbuf >> 3) & 1) | (readbuf2 << 1);
|
||||||
*manuf = get_manuf(readbuf);
|
printf("swclk=%hhu swdio=%hhu -> %08lx\n", swclk, swdio, swd_idcode);
|
||||||
*part = get_partno(readbuf);
|
if (result) *idcode = swd_idcode;
|
||||||
} else { *manuf = 0; *part = 0; }
|
else *idcode = 0;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void scan_swd(void) {
|
static void scan_swd(void) {
|
||||||
init_pins(0xff, 0xff, 0xff, 0xff);
|
init_pins(0xff, 0xff, 0xff, 0xff);
|
||||||
|
|
||||||
|
/*uint16_t manuf, part;
|
||||||
|
test_swd_lines(startpin, startpin+1, &manuf, &part);*/
|
||||||
|
|
||||||
for (uint8_t swclk = startpin; swclk <= endpin; ++swclk) {
|
for (uint8_t swclk = startpin; swclk <= endpin; ++swclk) {
|
||||||
if (!jscan_pin_get(swclk)) continue;
|
if (!jscan_pin_get(swclk)) continue;
|
||||||
for (uint8_t swdio = startpin; swdio <= endpin; ++swdio) {
|
for (uint8_t swdio = startpin; swdio <= endpin; ++swdio) {
|
||||||
|
@ -359,14 +370,14 @@ static void scan_swd(void) {
|
||||||
|
|
||||||
if (!jscan_pin_get(swdio)) continue;
|
if (!jscan_pin_get(swdio)) continue;
|
||||||
|
|
||||||
uint16_t manuf = 0, part = 0;
|
uint32_t idcode = 0;
|
||||||
if (test_swd_lines(swclk, swdio, &manuf, &part)) {
|
if (test_swd_lines(swclk, swdio, &idcode)) {
|
||||||
if (nmatches < N_MATCHES_SWD) {
|
if (nmatches < N_MATCHES_SWD) {
|
||||||
//memset(&matches.s[nmatches], 0, sizeof(struct match_rec));
|
//memset(&matches.s[nmatches], 0, sizeof(struct match_rec));
|
||||||
matches.s[nmatches].swclk = swclk;
|
matches.s[nmatches].swclk = swclk;
|
||||||
matches.s[nmatches].swdio = swdio;
|
matches.s[nmatches].swdio = swdio;
|
||||||
matches.s[nmatches].manuf = manuf;
|
matches.s[nmatches].idlo = idcode & 0xffff;
|
||||||
matches.s[nmatches].part = part ;
|
matches.s[nmatches].idhi = (idcode >> 16);
|
||||||
|
|
||||||
++nmatches;
|
++nmatches;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue