diff --git a/src/platforms/hosted/bmp_remote.c b/src/platforms/hosted/bmp_remote.c index 817ec17..a6461a5 100644 --- a/src/platforms/hosted/bmp_remote.c +++ b/src/platforms/hosted/bmp_remote.c @@ -172,19 +172,16 @@ static uint32_t remote_adiv5_dp_read(ADIv5_DP_t *dp, uint16_t addr) } uint32_t dest[1]; unhexify(dest, (const char*)&construct[1], 4); - if (cl_debuglevel & BMP_DEBUG_PLATFORM) - printf("dp_read @ %04x: 0x%08" PRIx32 "\n", addr, dest[0]); return dest[0]; } -static uint32_t remote_adiv5_low_access(ADIv5_DP_t *dp, uint8_t RnW, uint16_t addr, uint32_t value) +static uint32_t remote_adiv5_low_access( + ADIv5_DP_t *dp, uint8_t RnW, uint16_t addr, uint32_t value) { (void)dp; uint8_t construct[REMOTE_MAX_MSG_SIZE]; int s = snprintf((char *)construct, REMOTE_MAX_MSG_SIZE, REMOTE_LOW_ACCESS_STR, RnW, addr, value); - if (cl_debuglevel & BMP_DEBUG_PLATFORM) - printf("Dp_write @ %04x: 0x%08" PRIx32 "\n", addr, value); platform_buffer_write(construct, s); s = platform_buffer_read(construct, REMOTE_MAX_MSG_SIZE); if ((!s) || (construct[0] == REMOTE_RESP_ERR)) { @@ -192,8 +189,6 @@ static uint32_t remote_adiv5_low_access(ADIv5_DP_t *dp, uint8_t RnW, uint16_t ad } uint32_t dest[1]; unhexify(dest, (const char*)&construct[1], 4); - if (cl_debuglevel & BMP_DEBUG_PLATFORM) - printf("dp_read @ %04x: 0x%08" PRIx32 "\n", addr, dest[0]); return dest[0]; } @@ -209,8 +204,6 @@ static uint32_t remote_adiv5_ap_read(ADIv5_AP_t *ap, uint16_t addr) } uint32_t dest[1]; unhexify(dest, (const char*)&construct[1], 4); - if (cl_debuglevel & BMP_DEBUG_PLATFORM) - printf("ap_read @ %04x: 0x%08" PRIx32 "\n", addr, dest[0]); return dest[0]; } @@ -219,8 +212,6 @@ static void remote_adiv5_ap_write(ADIv5_AP_t *ap, uint16_t addr, uint32_t value) uint8_t construct[REMOTE_MAX_MSG_SIZE]; int s = snprintf((char *)construct, REMOTE_MAX_MSG_SIZE,REMOTE_AP_WRITE_STR, ap->apsel, addr, value); - if (cl_debuglevel & BMP_DEBUG_PLATFORM) - printf("ap_write @ %04x: 0x%08" PRIx32 "\n", addr, value); platform_buffer_write(construct, s); s = platform_buffer_read(construct, REMOTE_MAX_MSG_SIZE); if ((!s) || (construct[0] == REMOTE_RESP_ERR)) { @@ -279,8 +270,6 @@ static void remote_ap_mem_read( (void)ap; if (len == 0) return; - if (cl_debuglevel & BMP_DEBUG_PLATFORM) - printf("ap_memread @ %" PRIx32 " len %ld\n", src, len); char construct[REMOTE_MAX_MSG_SIZE]; int batchsize = (REMOTE_MAX_MSG_SIZE - 0x20) / 2; while(len) { @@ -320,9 +309,6 @@ static void remote_ap_mem_write_sized( (void)ap; if (len == 0) return; - if (cl_debuglevel & BMP_DEBUG_PLATFORM) - printf("ap_mem_write_sized @ %" PRIx32 " len %ld, align %d\n", - dest, len, 1 << align); char construct[REMOTE_MAX_MSG_SIZE]; /* (5 * 1 (char)) + (2 * 2 (bytes)) + (3 * 8 (words)) */ int batchsize = (REMOTE_MAX_MSG_SIZE - 0x30) / 2; diff --git a/src/platforms/hosted/cmsis_dap.c b/src/platforms/hosted/cmsis_dap.c index 04c3905..395f308 100644 --- a/src/platforms/hosted/cmsis_dap.c +++ b/src/platforms/hosted/cmsis_dap.c @@ -41,8 +41,6 @@ #include "target.h" #include "target_internal.h" -#define MAX_DEBUGGERS 20 - uint8_t dap_caps; uint8_t mode; diff --git a/src/platforms/hosted/jlink_adiv5_swdp.c b/src/platforms/hosted/jlink_adiv5_swdp.c index 06da0a1..507f1db 100644 --- a/src/platforms/hosted/jlink_adiv5_swdp.c +++ b/src/platforms/hosted/jlink_adiv5_swdp.c @@ -104,8 +104,6 @@ static int line_reset(bmp_info_t *info) static int swdptap_init(bmp_info_t *info) { - if (cl_debuglevel) - printf("swdptap_init\n"); uint8_t cmd[2] = {CMD_GET_SELECT_IF, JLINK_IF_GET_AVAILABLE}; uint8_t res[4]; send_recv(info->usb_link, cmd, 2, res, sizeof(res)); @@ -190,46 +188,8 @@ int jlink_swdp_scan(bmp_info_t *info) return target_list?1:0; } -void ap_decode_access(uint16_t addr, uint8_t RnW) -{ - if (RnW) - printf("Read "); - else - printf("Write "); - switch(addr) { - case 0x00: - if (RnW) - printf("DP_DPIDR: "); - else - printf("DP_ABORT: "); - break; - case 0x004: printf("CTRL/STAT: "); - break; - case 0x008: - if (RnW) - printf("RESEND: "); - else - printf("DP_SELECT: "); - break; - case 0x00c: printf("DP_RDBUFF: "); - break; - case 0x100: printf("AP_CSW: "); - break; - case 0x104: printf("AP_TAR: "); - break; - case 0x10c: printf("AP_DRW: "); - break; - case 0x1f8: printf("AP_BASE: "); - break; - case 0x1fc: printf("AP_IDR: "); - break; - } -} - static uint32_t jlink_adiv5_swdp_read(ADIv5_DP_t *dp, uint16_t addr) { - if (cl_debuglevel) - printf("jlink_adiv5_swdp_read 0x%04x\n", addr); if (addr & ADIV5_APnDP) { adiv5_dp_low_access(dp, ADIV5_LOW_READ, addr, 0); return adiv5_dp_low_access(dp, ADIV5_LOW_READ, @@ -266,17 +226,6 @@ static uint32_t jlink_adiv5_swdp_error(ADIv5_DP_t *dp) static uint32_t jlink_adiv5_swdp_low_access(ADIv5_DP_t *dp, uint8_t RnW, uint16_t addr, uint32_t value) { - if (cl_debuglevel) { - if (RnW) { - if (cl_debuglevel > 0) { - ap_decode_access(addr, RnW); - printf(" start:\n"); - } - }else{ - ap_decode_access(addr, RnW); - printf(" %08" PRIx32 "\n", value); - } - } bool APnDP = addr & ADIV5_APnDP; uint8_t addr8 = addr & 0xff; uint8_t request = 0x81; @@ -316,14 +265,14 @@ static uint32_t jlink_adiv5_swdp_low_access(ADIv5_DP_t *dp, uint8_t RnW, raise_exception(EXCEPTION_TIMEOUT, "SWDP ACK timeout"); if(ack == SWDP_ACK_FAULT) { - if (cl_debuglevel) + if (cl_debuglevel & BMP_DEBUG_PLATFORM) fprintf(stderr, "Fault\n"); dp->fault = 1; return 0; } if(ack != SWDP_ACK_OK) { - if (cl_debuglevel) + if (cl_debuglevel & BMP_DEBUG_PLATFORM) fprintf(stderr, "Protocol\n"); line_reset(&info); return 0; @@ -342,10 +291,6 @@ static uint32_t jlink_adiv5_swdp_low_access(ADIv5_DP_t *dp, uint8_t RnW, response = res[0] | res[1] << 8 | res[2] << 16 | res[3] << 24; int parity = res[4] & 1; int bit_count = __builtin_popcount (response) + parity; - if (cl_debuglevel ) { - ap_decode_access(addr, RnW);; - printf("0x%08" PRIx32 "\n", response); - } if (bit_count & 1) /* Give up on parity error */ raise_exception(EXCEPTION_ERROR, "SWDP Parity error"); } else { diff --git a/src/platforms/hosted/jlink_jtagtap.c b/src/platforms/hosted/jlink_jtagtap.c index d69d904..6ea1cbd 100644 --- a/src/platforms/hosted/jlink_jtagtap.c +++ b/src/platforms/hosted/jlink_jtagtap.c @@ -40,7 +40,7 @@ static void jtagtap_reset(void) static void jtagtap_tms_seq(uint32_t MS, int ticks) { - if (cl_debuglevel) + if (cl_debuglevel & BMP_DEBUG_PLATFORM) printf("jtagtap_tms_seq 0x%08" PRIx32 ", ticks %d\n", MS, ticks); int len = (ticks + 7) / 8; uint8_t cmd[12]; @@ -68,7 +68,7 @@ static void jtagtap_tdi_tdo_seq(uint8_t *DO, const uint8_t final_tms, if (!ticks) return; int len = (ticks + 7) / 8; - if (cl_debuglevel) { + if (cl_debuglevel & BMP_DEBUG_PLATFORM) { printf("jtagtap_tdi_tdo %s, ticks %d, DI: ", (final_tms) ? "Final TMS" : "", ticks); for (int i = 0; i < len; i++) { @@ -103,12 +103,24 @@ static void jtagtap_tdi_tdo_seq(uint8_t *DO, const uint8_t final_tms, static void jtagtap_tdi_seq(const uint8_t final_tms, const uint8_t *DI, int ticks) { + if (cl_debuglevel & BMP_DEBUG_PLATFORM) { + printf("jtagtap_tdi_seq %s:", (final_tms)? "final_tms" : ""); + const uint8_t *p = DI; + unsigned int i = (ticks & 7) & ~7 ; + if (i > 16) + i = 16; + while (i--) + printf(" %02x", *p++); + if (ticks > (16 * 8)) + printf(" ..."); + printf("\n"); + } return jtagtap_tdi_tdo_seq(NULL, final_tms, DI, ticks); } static uint8_t jtagtap_next(uint8_t dTMS, uint8_t dTDI) { - if (cl_debuglevel) + if (cl_debuglevel & BMP_DEBUG_PLATFORM) printf("jtagtap_next TMS 0x%02x, TDI %02x\n", dTMS, dTDI); uint8_t cmd[6]; cmd[0] = CMD_HW_JTAG3; @@ -128,7 +140,7 @@ static uint8_t jtagtap_next(uint8_t dTMS, uint8_t dTDI) int jlink_jtagtap_init(bmp_info_t *info, jtag_proc_t *jtag_proc) { - if (cl_debuglevel) + if (cl_debuglevel & BMP_DEBUG_PLATFORM) printf("jtap_init\n"); uint8_t cmd_switch[2] = {CMD_GET_SELECT_IF, JLINK_IF_GET_AVAILABLE}; uint8_t res[4]; diff --git a/src/platforms/hosted/libftdi_jtagtap.c b/src/platforms/hosted/libftdi_jtagtap.c index 1724d3f..7a009ab 100644 --- a/src/platforms/hosted/libftdi_jtagtap.c +++ b/src/platforms/hosted/libftdi_jtagtap.c @@ -49,21 +49,21 @@ int libftdi_jtagtap_init(jtag_proc_t *jtag_proc) assert(ftdic != NULL); int err = ftdi_usb_purge_buffers(ftdic); if (err != 0) { - fprintf(stderr, "ftdi_usb_purge_buffer: %d: %s\n", + printf("ftdi_usb_purge_buffer: %d: %s\n", err, ftdi_get_error_string(ftdic)); abort(); } /* Reset MPSSE controller. */ err = ftdi_set_bitmode(ftdic, 0, BITMODE_RESET); if (err != 0) { - fprintf(stderr, "ftdi_set_bitmode: %d: %s\n", + printf("ftdi_set_bitmode: %d: %s\n", err, ftdi_get_error_string(ftdic)); return -1; } /* Enable MPSSE controller. Pin directions are set later.*/ err = ftdi_set_bitmode(ftdic, 0, BITMODE_MPSSE); if (err != 0) { - fprintf(stderr, "ftdi_set_bitmode: %d: %s\n", + printf("ftdi_set_bitmode: %d: %s\n", err, ftdi_get_error_string(ftdic)); return -1; } diff --git a/src/platforms/hosted/libftdi_swdptap.c b/src/platforms/hosted/libftdi_swdptap.c index 0798901..6feb70d 100644 --- a/src/platforms/hosted/libftdi_swdptap.c +++ b/src/platforms/hosted/libftdi_swdptap.c @@ -43,26 +43,26 @@ static void swdptap_seq_out_parity(uint32_t MS, int ticks); int libftdi_swdptap_init(swd_proc_t *swd_proc) { if (!active_cable->bitbang_tms_in_pin) { - DEBUG("SWD not possible or missing item in cable description.\n"); + printf("SWD not possible or missing item in cable description.\n"); return -1; } int err = ftdi_usb_purge_buffers(ftdic); if (err != 0) { - fprintf(stderr, "ftdi_usb_purge_buffer: %d: %s\n", + printf("ftdi_usb_purge_buffer: %d: %s\n", err, ftdi_get_error_string(ftdic)); return -1; } /* Reset MPSSE controller. */ err = ftdi_set_bitmode(ftdic, 0, BITMODE_RESET); if (err != 0) { - fprintf(stderr, "ftdi_set_bitmode: %d: %s\n", + printf("ftdi_set_bitmode: %d: %s\n", err, ftdi_get_error_string(ftdic)); return -1; } /* Enable MPSSE controller. Pin directions are set later.*/ err = ftdi_set_bitmode(ftdic, 0, BITMODE_MPSSE); if (err != 0) { - fprintf(stderr, "ftdi_set_bitmode: %d: %s\n", + printf("ftdi_set_bitmode: %d: %s\n", err, ftdi_get_error_string(ftdic)); return -1; } diff --git a/src/platforms/hosted/platform.c b/src/platforms/hosted/platform.c index 430ed72..45366e6 100644 --- a/src/platforms/hosted/platform.c +++ b/src/platforms/hosted/platform.c @@ -469,3 +469,140 @@ void platform_buffer_flush(void) break; } } + +static void ap_decode_access(uint16_t addr, uint8_t RnW) +{ + if (RnW) + printf("Read "); + else + printf("Write "); + switch(addr) { + case 0x00: + if (RnW) + printf("DP_DPIDR :"); + else + printf("DP_ABORT :"); + break; + case 0x004: printf("CTRL/STAT:"); + break; + case 0x008: + if (RnW) + printf("RESEND :"); + else + printf("DP_SELECT:"); + break; + case 0x00c: printf("DP_RDBUFF:"); + break; + case 0x100: printf("AP_CSW :"); + break; + case 0x104: printf("AP_TAR :"); + break; + case 0x10c: printf("AP_DRW :"); + break; + case 0x1f8: printf("AP_BASE :"); + break; + case 0x1fc: printf("AP_IDR :"); + break; + } +} + +void adiv5_dp_write(ADIv5_DP_t *dp, uint16_t addr, uint32_t value) +{ + if (cl_debuglevel & BMP_DEBUG_PLATFORM) { + ap_decode_access(addr, ADIV5_LOW_WRITE); + printf(" 0x%08" PRIx32 "\n", value); + } + dp->low_access(dp, ADIV5_LOW_WRITE, addr, value); +} + +uint32_t adiv5_dp_read(ADIv5_DP_t *dp, uint16_t addr) +{ + uint32_t ret = dp->dp_read(dp, addr); + if (cl_debuglevel & BMP_DEBUG_PLATFORM) { + ap_decode_access(addr, ADIV5_LOW_READ); + printf(" 0x%08" PRIx32 "\n", ret); + } + return ret; +} + +uint32_t adiv5_dp_error(ADIv5_DP_t *dp) +{ + uint32_t ret = dp->error(dp); + if (cl_debuglevel & BMP_DEBUG_PLATFORM) { + printf("DP Error 0x%08" PRIx32 "\n", ret); + } + return ret; +} + +uint32_t adiv5_dp_low_access(struct ADIv5_DP_s *dp, uint8_t RnW, + uint16_t addr, uint32_t value) +{ + uint32_t ret = dp->low_access(dp, RnW, addr, value); + if (cl_debuglevel & BMP_DEBUG_PLATFORM) { + ap_decode_access(addr, RnW); + printf(" 0x%08" PRIx32 "\n", (RnW)? ret : value); + } + return ret; +} + +uint32_t adiv5_ap_read(ADIv5_AP_t *ap, uint16_t addr) +{ + uint32_t ret = ap->dp->ap_read(ap, addr); + if (cl_debuglevel & BMP_DEBUG_PLATFORM) { + ap_decode_access(addr, ADIV5_LOW_READ); + printf(" 0x%08" PRIx32 "\n", ret); + } + return ret; +} + +void adiv5_ap_write(ADIv5_AP_t *ap, uint16_t addr, uint32_t value) +{ + if (cl_debuglevel & BMP_DEBUG_PLATFORM) { + ap_decode_access(addr, ADIV5_LOW_WRITE); + printf(" 0x%08" PRIx32 "\n", value); + } + return ap->dp->ap_write(ap, addr, value); +} + +void adiv5_mem_read(ADIv5_AP_t *ap, void *dest, uint32_t src, size_t len) +{ + ap->dp->mem_read(ap, dest, src, len); + if (cl_debuglevel & BMP_DEBUG_PLATFORM) { + printf("ap_memread @ %" PRIx32 " len %" PRIx32 ":", src, (uint32_t)len); + uint8_t *p = (uint8_t *) dest; + unsigned int i = len; + if (i > 16) + i = 16; + while (i--) + printf(" %02x", *p++); + if (len > 16) + printf(" ..."); + printf("\n"); + } + return; +} +void adiv5_mem_write_sized( ADIv5_AP_t *ap, uint32_t dest, const void *src, + size_t len, enum align align) +{ + if (cl_debuglevel & BMP_DEBUG_PLATFORM) { + printf("ap_mem_write_sized @ %" PRIx32 " len %" PRIx32 ", align %d:", + dest, (uint32_t)len, 1 << align); + uint8_t *p = (uint8_t *) src; + unsigned int i = len; + if (i > 16) + i = 16; + while (i--) + printf(" %02x", *p++); + if (len > 16) + printf(" ..."); + printf("\n"); + } + return ap->dp->mem_write_sized(ap, dest, src, len, align); +} + +void adiv5_dp_abort(struct ADIv5_DP_s *dp, uint32_t abort) +{ + if (cl_debuglevel & BMP_DEBUG_PLATFORM) + printf("Abort: %08" PRIx32 "\n", abort); + return dp->abort(dp, abort); +} diff --git a/src/platforms/hosted/stlinkv2.c b/src/platforms/hosted/stlinkv2.c index 2d50a72..b6ce189 100644 --- a/src/platforms/hosted/stlinkv2.c +++ b/src/platforms/hosted/stlinkv2.c @@ -232,43 +232,43 @@ static int stlink_usb_error_check(uint8_t *data, bool verbose) return STLINK_ERROR_OK; case STLINK_DEBUG_ERR_FAULT: if (verbose) - DEBUG("SWD fault response (0x%x)\n", STLINK_DEBUG_ERR_FAULT); + printf("SWD fault response (0x%x)\n", STLINK_DEBUG_ERR_FAULT); return STLINK_ERROR_FAIL; case STLINK_JTAG_UNKNOWN_JTAG_CHAIN: if (verbose) - DEBUG("Unknown JTAG chain\n"); + printf("Unknown JTAG chain\n"); return STLINK_ERROR_FAIL; case STLINK_NO_DEVICE_CONNECTED: if (verbose) - DEBUG("No device connected\n"); + printf("No device connected\n"); return STLINK_ERROR_FAIL; case STLINK_JTAG_COMMAND_ERROR: if (verbose) - DEBUG("Command error\n"); + printf("Command error\n"); return STLINK_ERROR_FAIL; case STLINK_JTAG_GET_IDCODE_ERROR: if (verbose) - DEBUG("Failure reading IDCODE\n"); + printf("Failure reading IDCODE\n"); return STLINK_ERROR_FAIL; case STLINK_JTAG_DBG_POWER_ERROR: if (verbose) - DEBUG("Failure powering DBG\n"); + printf("Failure powering DBG\n"); return STLINK_ERROR_WAIT; case STLINK_SWD_AP_WAIT: if (verbose) - DEBUG("wait status SWD_AP_WAIT (0x%x)\n", STLINK_SWD_AP_WAIT); + printf("wait status SWD_AP_WAIT (0x%x)\n", STLINK_SWD_AP_WAIT); return STLINK_ERROR_WAIT; case STLINK_SWD_DP_WAIT: if (verbose) - DEBUG("wait status SWD_DP_WAIT (0x%x)\n", STLINK_SWD_DP_WAIT); + printf("wait status SWD_DP_WAIT (0x%x)\n", STLINK_SWD_DP_WAIT); return STLINK_ERROR_WAIT; case STLINK_JTAG_WRITE_ERROR: if (verbose) - DEBUG("Write error\n"); + printf("Write error\n"); return STLINK_ERROR_FAIL; case STLINK_JTAG_WRITE_VERIF_ERROR: if (verbose) - DEBUG("Write verify error, ignoring\n"); + printf("Write verify error, ignoring\n"); return STLINK_ERROR_OK; case STLINK_SWD_AP_FAULT: /* git://git.ac6.fr/openocd commit 657e3e885b9ee10 @@ -278,40 +278,40 @@ static int stlink_usb_error_check(uint8_t *data, bool verbose) */ Stlink.ap_error = true; if (verbose) - DEBUG("STLINK_SWD_AP_FAULT\n"); + printf("STLINK_SWD_AP_FAULT\n"); return STLINK_ERROR_DP_FAULT; case STLINK_SWD_AP_ERROR: if (verbose) - DEBUG("STLINK_SWD_AP_ERROR\n"); + printf("STLINK_SWD_AP_ERROR\n"); return STLINK_ERROR_FAIL; case STLINK_SWD_AP_PARITY_ERROR: if (verbose) - DEBUG("STLINK_SWD_AP_PARITY_ERROR\n"); + printf("STLINK_SWD_AP_PARITY_ERROR\n"); return STLINK_ERROR_FAIL; case STLINK_SWD_DP_FAULT: if (verbose) - DEBUG("STLINK_SWD_DP_FAULT\n"); + printf("STLINK_SWD_DP_FAULT\n"); return STLINK_ERROR_FAIL; case STLINK_SWD_DP_ERROR: if (verbose) - DEBUG("STLINK_SWD_DP_ERROR\n"); + printf("STLINK_SWD_DP_ERROR\n"); raise_exception(EXCEPTION_ERROR, "STLINK_SWD_DP_ERROR"); return STLINK_ERROR_FAIL; case STLINK_SWD_DP_PARITY_ERROR: if (verbose) - DEBUG("STLINK_SWD_DP_PARITY_ERROR\n"); + printf("STLINK_SWD_DP_PARITY_ERROR\n"); return STLINK_ERROR_FAIL; case STLINK_SWD_AP_WDATA_ERROR: if (verbose) - DEBUG("STLINK_SWD_AP_WDATA_ERROR\n"); + printf("STLINK_SWD_AP_WDATA_ERROR\n"); return STLINK_ERROR_FAIL; case STLINK_SWD_AP_STICKY_ERROR: if (verbose) - DEBUG("STLINK_SWD_AP_STICKY_ERROR\n"); + printf("STLINK_SWD_AP_STICKY_ERROR\n"); return STLINK_ERROR_FAIL; case STLINK_SWD_AP_STICKYORUN_ERROR: if (verbose) - DEBUG("STLINK_SWD_AP_STICKYORUN_ERROR\n"); + printf("STLINK_SWD_AP_STICKYORUN_ERROR\n"); return STLINK_ERROR_FAIL; case STLINK_BAD_AP_ERROR: /* ADIV5 probe 256 APs, most of them are non exisitant.*/ @@ -319,15 +319,15 @@ static int stlink_usb_error_check(uint8_t *data, bool verbose) case STLINK_TOO_MANY_AP_ERROR: /* TI TM4C duplicates AP. Error happens at AP9.*/ if (verbose) - DEBUG("STLINK_TOO_MANY_AP_ERROR\n"); + printf("STLINK_TOO_MANY_AP_ERROR\n"); return STLINK_ERROR_FAIL; case STLINK_JTAG_UNKNOWN_CMD : if (verbose) - DEBUG("STLINK_JTAG_UNKNOWN_CMD\n"); + printf("STLINK_JTAG_UNKNOWN_CMD\n"); return STLINK_ERROR_FAIL; default: if (verbose) - DEBUG("unknown/unexpected STLINK status code 0x%x\n", data[0]); + printf("unknown/unexpected STLINK status code 0x%x\n", data[0]); return STLINK_ERROR_FAIL; } } @@ -345,7 +345,7 @@ static int stlink_send_recv_retry(uint8_t *txbuf, size_t txsize, return res; uint32_t now = platform_time_ms(); if (((now - start) > 1000) || (res != STLINK_ERROR_WAIT)) { - DEBUG("write_retry failed. "); + printf("write_retry failed. "); return res; } } @@ -364,7 +364,7 @@ static int read_retry(uint8_t *txbuf, size_t txsize, return res; uint32_t now = platform_time_ms(); if (((now -start) > 1000) || (res != STLINK_ERROR_WAIT)) { - DEBUG("read_retry failed. "); + printf("read_retry failed. "); stlink_usb_get_rw_status(true); return res; } @@ -430,15 +430,15 @@ static void stlink_version(bmp_info_t *info) Stlink.ver_swim = (version >> 0) & 0x3f; } } - DEBUG("V%dJ%d",Stlink.ver_stlink, Stlink.ver_jtag); + printf("V%dJ%d",Stlink.ver_stlink, Stlink.ver_jtag); if (Stlink.ver_hw == 30) { - DEBUG("M%dB%dS%d", Stlink.ver_mass, Stlink.ver_bridge, Stlink.ver_swim); + printf("M%dB%dS%d", Stlink.ver_mass, Stlink.ver_bridge, Stlink.ver_swim); } else if (Stlink.ver_hw == 20) { - DEBUG("S%d", Stlink.ver_swim); + printf("S%d", Stlink.ver_swim); } else if (Stlink.ver_hw == 21) { - DEBUG("M%d", Stlink.ver_mass); + printf("M%d", Stlink.ver_mass); } - DEBUG("\n"); + printf("\n"); } static bool stlink_leave_state(bmp_info_t *info) @@ -448,23 +448,23 @@ static bool stlink_leave_state(bmp_info_t *info) send_recv(info->usb_link,cmd, 16, data, 2); if (data[0] == STLINK_DEV_DFU_MODE) { uint8_t dfu_cmd[16] = {STLINK_DFU_COMMAND, STLINK_DFU_EXIT}; - DEBUG("Leaving DFU Mode\n"); + printf("Leaving DFU Mode\n"); send_recv(info->usb_link, dfu_cmd, 16, NULL, 0); return true; } else if (data[0] == STLINK_DEV_SWIM_MODE) { uint8_t swim_cmd[16] = {STLINK_SWIM_COMMAND, STLINK_SWIM_EXIT}; - DEBUG("Leaving SWIM Mode\n"); + printf("Leaving SWIM Mode\n"); send_recv(info->usb_link, swim_cmd, 16, NULL, 0); } else if (data[0] == STLINK_DEV_DEBUG_MODE) { uint8_t dbg_cmd[16] = {STLINK_DEBUG_COMMAND, STLINK_DEBUG_EXIT}; - DEBUG("Leaving DEBUG Mode\n"); + printf("Leaving DEBUG Mode\n"); send_recv(info->usb_link, dbg_cmd, 16, NULL, 0); } else if (data[0] == STLINK_DEV_BOOTLOADER_MODE) { - DEBUG("Leaving BOOTLOADER Mode\n"); + printf("Leaving BOOTLOADER Mode\n"); } else if (data[0] == STLINK_DEV_MASS_MODE) { - DEBUG("Leaving MASS Mode\n"); + printf("Leaving MASS Mode\n"); } else { - DEBUG("Unknown Mode %02x\n", data[0]); + printf("Unknown Mode %02x\n", data[0]); } return false; } @@ -593,7 +593,7 @@ int stlink_init(bmp_info_t *info) } if ((Stlink.ver_stlink < 3 && Stlink.ver_jtag < 32) || (Stlink.ver_stlink == 3 && Stlink.ver_jtag < 3)) { - DEBUG("Please update Firmware\n"); + printf("Please update Firmware\n"); return -1; } if (stlink_leave_state(info)) { @@ -646,7 +646,7 @@ static bool stlink3_set_freq_divisor(bmp_info_t *info, uint16_t divisor) divisor = size; uint8_t *p = data + 12 + divisor * sizeof(uint32_t); uint32_t freq = p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24; - DEBUG("Selected %" PRId32 " khz\n", freq); + printf("Selected %" PRId32 " khz\n", freq); cmd[1] = STLINK_APIV3_SET_COM_FREQ; cmd[2] = Stlink.transport_mode; cmd[3] = 0; @@ -676,7 +676,7 @@ static int stlink_enter_debug_jtag(bmp_info_t *info) STLINK_DEBUG_APIV2_ENTER, STLINK_DEBUG_ENTER_JTAG_NO_RESET}; uint8_t data[2]; - DEBUG("Enter JTAG\n"); + printf("Enter JTAG\n"); send_recv(info->usb_link, cmd, 16, data, 2); return stlink_usb_error_check(data, true); } @@ -688,7 +688,7 @@ static uint32_t stlink_read_coreid(void) uint8_t data[4]; send_recv(info.usb_link, cmd, 16, data, 4); uint32_t id = data[0] | data[1] << 8 | data[2] << 16 | data[3] << 24; - DEBUG("Read Core ID: 0x%08" PRIx32 "\n", id); + printf("Read Core ID: 0x%08" PRIx32 "\n", id); return id; } @@ -713,12 +713,10 @@ uint32_t stlink_dp_low_access(ADIv5_DP_t *dp, uint8_t RnW, static uint32_t stlink_dp_read(ADIv5_DP_t *dp, uint16_t addr) { if (addr & ADIV5_APnDP) { - DEBUG_STLINK("AP read addr 0x%04" PRIx16 "\n", addr); stlink_dp_low_access(dp, ADIV5_LOW_READ, addr, 0); return stlink_dp_low_access(dp, ADIV5_LOW_READ, ADIV5_DP_RDBUFF, 0); } else { - DEBUG_STLINK("DP read addr 0x%04" PRIx16 "\n", addr); return stlink_dp_low_access(dp, ADIV5_LOW_READ, addr, 0); } } @@ -743,7 +741,7 @@ uint32_t stlink_dp_error(ADIv5_DP_t *dp) adiv5_dp_write(dp, ADIV5_DP_ABORT, clr); dp->fault = 0; if (err) - DEBUG("stlink_dp_error %d\n", err); + printf("stlink_dp_error %d\n", err); err |= Stlink.ap_error; Stlink.ap_error = false; return err; @@ -764,15 +762,13 @@ static int stlink_read_dp_register(uint16_t port, uint16_t addr, uint32_t *reg) cmd[4] = ((Stlink.dap_select & 0xf) << 4) | (addr & 0xf); else cmd[4] = addr & 0xff; - DEBUG_STLINK("Read DP, Addr 0x%04" PRIx16 ": \n", addr); uint8_t data[8]; int res = stlink_send_recv_retry(cmd, 16, data, 8); if (res == STLINK_ERROR_OK) { uint32_t ret = data[4] | data[5] << 8 | data[6] << 16 | data[7] << 24; - DEBUG_STLINK("0x%08" PRIx32" \n", ret); *reg = ret; } else { - DEBUG_STLINK("failed, res %d\n", res); + printf("%s error %d\n", __func__, res); } return res; } @@ -781,7 +777,8 @@ static int stlink_write_dp_register(uint16_t port, uint16_t addr, uint32_t val) { if (port == STLINK_DEBUG_PORT_ACCESS && addr == 8) { Stlink.dap_select = val; - DEBUG_STLINK("Caching SELECT 0x%02" PRIx32 "\n", val); + if (cl_debuglevel & BMP_DEBUG_PLATFORM) + printf("Caching SELECT 0x%02" PRIx32 "\n", val); return STLINK_ERROR_OK; } else { uint8_t cmd[16] = { @@ -792,8 +789,6 @@ static int stlink_write_dp_register(uint16_t port, uint16_t addr, uint32_t val) (val >> 24) & 0xff}; uint8_t data[2]; stlink_send_recv_retry(cmd, 16, data, 2); - DEBUG_STLINK("Write DP, Addr 0x%04" PRIx16 ": 0x%08" PRIx32 - " \n", addr, val); return stlink_usb_error_check(data, true); } } @@ -806,11 +801,7 @@ uint32_t stlink_dp_low_access(ADIv5_DP_t *dp, uint8_t RnW, if (RnW) { res = stlink_read_dp_register( STLINK_DEBUG_PORT_ACCESS, addr, &response); - DEBUG_STLINK("SWD read addr %04" PRIx16 ": %08" PRIx32 "\n", - addr, response); } else { - DEBUG_STLINK("SWD write addr %04" PRIx16 ": %08" PRIx32 "\n", - addr, value); res = stlink_write_dp_register(STLINK_DEBUG_PORT_ACCESS, addr, value); } if (res == STLINK_ERROR_WAIT) @@ -837,11 +828,12 @@ static bool stlink_ap_setup(int ap) }; uint8_t data[2]; send_recv(info.usb_link, cmd, 16, data, 2); - DEBUG_STLINK("Open AP %d\n", ap); + if (cl_debuglevel & BMP_DEBUG_PLATFORM) + printf("Open AP %d\n", ap); int res = stlink_usb_error_check(data, true); if (res) { if (Stlink.ver_hw == 30) { - DEBUG("STLINKV3 only connects to STM8/32!\n"); + printf("STLINKV3 only connects to STM8/32!\n"); } return false; } @@ -857,7 +849,8 @@ static void stlink_ap_cleanup(int ap) }; uint8_t data[2]; send_recv(info.usb_link, cmd, 16, data, 2); - DEBUG_STLINK("Close AP %d\n", ap); + if (cl_debuglevel & BMP_DEBUG_PLATFORM) + printf("Close AP %d\n", ap); stlink_usb_error_check(data, true); } static int stlink_usb_get_rw_status(bool verbose) @@ -877,26 +870,19 @@ static void stlink_readmem(ADIv5_AP_t *ap, void *dest, uint32_t src, size_t len) return; size_t read_len = len; uint8_t type; - char *CMD; if (src & 1 || len & 1) { - CMD = "READMEM_8BIT"; type = STLINK_DEBUG_READMEM_8BIT; if (len > Stlink.block_size) { - DEBUG(" Too large!\n"); + printf(" Too large!\n"); return; } if (len == 1) read_len ++; /* Fix read length as in openocd*/ } else if (src & 3 || len & 3) { - CMD = "READMEM_16BIT"; type = STLINK_DEBUG_APIV2_READMEM_16BIT; } else { - CMD = "READMEM_32BIT"; type = STLINK_DEBUG_READMEM_32BIT; - } - DEBUG_STLINK("%s len %zu addr 0x%08" PRIx32 " AP %d : ", - CMD, len, src, ap->apsel); uint8_t cmd[16] = { STLINK_DEBUG_COMMAND, type, @@ -904,19 +890,14 @@ static void stlink_readmem(ADIv5_AP_t *ap, void *dest, uint32_t src, size_t len) (src >> 24) & 0xff, len & 0xff, len >> 8, ap->apsel}; int res = read_retry(cmd, 16, dest, read_len); - if (res == STLINK_ERROR_OK) { - uint8_t *p = (uint8_t*)dest; - for (size_t i = 0; i < len ; i++) { - DEBUG_STLINK("%02x", *p++); - } - } else { + if (res != STLINK_ERROR_OK) { /* FIXME: What is the right measure when failing? * * E.g. TM4C129 gets here when NRF probe reads 0x10000010 * Approach taken: * Fill the memory with some fixed pattern so hopefully * the caller notices the error*/ - DEBUG("stlink_readmem failed\n"); + printf("stlink_readmem failed\n"); memset(dest, 0xff, len); } DEBUG_STLINK("\n"); @@ -926,12 +907,6 @@ static void stlink_writemem8(usb_link_t *link, ADIv5_AP_t *ap, uint32_t addr, size_t len, uint8_t *buffer) { (void)link; - DEBUG_STLINK("Mem Write8 AP %d len %zu addr 0x%08" PRIx32 ": ", - ap->apsel, len, addr); - for (size_t t = 0; t < len; t++) { - DEBUG_STLINK("%02x", buffer[t]); - } - DEBUG_STLINK("\n"); while (len) { size_t length; /* OpenOCD has some note about writemem8*/ @@ -957,12 +932,6 @@ static void stlink_writemem16(usb_link_t *link, ADIv5_AP_t *ap, uint32_t addr, size_t len, uint16_t *buffer) { (void)link; - DEBUG_STLINK("Mem Write16 AP %d len %zu addr 0x%08" PRIx32 ": ", - ap->apsel, len, addr); - for (size_t t = 0; t < len; t+=2) { - DEBUG_STLINK("%04x", buffer[t]); - } - DEBUG_STLINK("\n"); uint8_t cmd[16] = { STLINK_DEBUG_COMMAND, STLINK_DEBUG_APIV2_WRITEMEM_16BIT, @@ -978,12 +947,6 @@ static void stlink_writemem32(usb_link_t * link, ADIv5_AP_t *ap, uint32_t addr, size_t len, uint32_t *buffer) { (void)link; - DEBUG_STLINK("Mem Write32 AP %d len %zu addr 0x%08" PRIx32 ": ", - ap->apsel, len, addr); - for (size_t t = 0; t < len; t+=4) { - DEBUG_STLINK("%04x", buffer[t]); - } - DEBUG_STLINK("\n"); uint8_t cmd[16] = { STLINK_DEBUG_COMMAND, STLINK_DEBUG_WRITEMEM_32BIT, @@ -998,7 +961,8 @@ static void stlink_regs_read(ADIv5_AP_t *ap, void *data) uint8_t cmd[16] = {STLINK_DEBUG_COMMAND, STLINK_DEBUG_APIV2_READALLREGS, ap->apsel}; uint8_t res[88]; - DEBUG_STLINK("AP %d: Read all core registers\n", ap->apsel); + if (cl_debuglevel & BMP_DEBUG_PLATFORM) + printf("AP %d: Read all core registers\n", ap->apsel); send_recv(info.usb_link, cmd, 16, res, 88); stlink_usb_error_check(res, true); memcpy(data, res + 4, 84); @@ -1012,8 +976,9 @@ static uint32_t stlink_reg_read(ADIv5_AP_t *ap, int num) send_recv(info.usb_link, cmd, 16, res, 8); stlink_usb_error_check(res, true); uint32_t ret = res[0] | res[1] << 8 | res[2] << 16 | res[3] << 24; - DEBUG_STLINK("AP %d: Read reg %02" PRId32 " val 0x%08" PRIx32 "\n", - ap->apsel, num, ret); + if (cl_debuglevel & BMP_DEBUG_PLATFORM) + printf("AP %d: Read reg %02" PRId32 " val 0x%08" PRIx32 "\n", + ap->apsel, num, ret); return ret; } @@ -1025,8 +990,9 @@ static void stlink_reg_write(ADIv5_AP_t *ap, int num, uint32_t val) (val >> 24) & 0xff, ap->apsel}; uint8_t res[2]; send_recv(info.usb_link, cmd, 16, res, 2); - DEBUG_STLINK("AP %d: Write reg %02" PRId32 " val 0x%08" PRIx32 "\n", - ap->apsel, num, val); + if (cl_debuglevel & BMP_DEBUG_PLATFORM) + printf("AP %d: Write reg %02" PRId32 " val 0x%08" PRIx32 "\n", + ap->apsel, num, val); stlink_usb_error_check(res, true); } @@ -1129,7 +1095,7 @@ int stlink_enter_debug_swd(bmp_info_t *info, ADIv5_DP_t *dp) STLINK_DEBUG_APIV2_ENTER, STLINK_DEBUG_ENTER_SWD_NO_RESET}; uint8_t data[2]; - DEBUG("Enter SWD\n"); + printf("Enter SWD\n"); send_recv(info->usb_link, cmd, 16, data, 2); if (stlink_usb_error_check(data, true)) return -1; diff --git a/src/platforms/pc/libusb_utils.c b/src/platforms/pc/libusb_utils.c index 2672490..1e090d5 100644 --- a/src/platforms/pc/libusb_utils.c +++ b/src/platforms/pc/libusb_utils.c @@ -93,7 +93,7 @@ int send_recv(usb_link_t *link, link->ep_tx | LIBUSB_ENDPOINT_OUT, txbuf, txlen, NULL, NULL, 0); - if (cl_debuglevel > 0) { + if (cl_debuglevel & BMP_DEBUG_WIRE) { int i = 0; printf(" Send (%3d): ", txlen); for (; i < txlen; i++) { @@ -127,7 +127,7 @@ int send_recv(usb_link_t *link, if (res >0) { int i; uint8_t *p = rxbuf; - if (cl_debuglevel > 0) { + if (cl_debuglevel & BMP_DEBUG_WIRE) { printf(" Rec (%zu/%d)", rxsize, res); for (i = 0; i < res && i < 32 ; i++) { if ( i && ((i & 7) == 0)) @@ -137,7 +137,7 @@ int send_recv(usb_link_t *link, } } } - if (cl_debuglevel > 0) + if (cl_debuglevel & BMP_DEBUG_WIRE) printf("\n"); return res; } diff --git a/src/platforms/pc/serial_unix.c b/src/platforms/pc/serial_unix.c index 2af20ba..0135956 100644 --- a/src/platforms/pc/serial_unix.c +++ b/src/platforms/pc/serial_unix.c @@ -139,8 +139,8 @@ int platform_buffer_write(const uint8_t *data, int size) { int s; - if (cl_debuglevel) - printf("%s\n",data); + if (cl_debuglevel & BMP_DEBUG_WIRE) + printf("%s\n", data); s = write(fd, data, size); if (s < 0) { printf("Failed to write\n"); @@ -197,7 +197,7 @@ int platform_buffer_read(uint8_t *data, int maxsize) s = read(fd, c, 1); if (*c==REMOTE_EOM) { *c = 0; - if (cl_debuglevel) + if (cl_debuglevel & BMP_DEBUG_WIRE) printf(" %s\n",data); return (c - data); } else { diff --git a/src/target/adiv5.c b/src/target/adiv5.c index d2f2142..2dc0a8d 100644 --- a/src/target/adiv5.c +++ b/src/target/adiv5.c @@ -241,11 +241,6 @@ void adiv5_ap_unref(ADIv5_AP_t *ap) } } -void adiv5_dp_write(ADIv5_DP_t *dp, uint16_t addr, uint32_t value) -{ - dp->low_access(dp, ADIV5_LOW_WRITE, addr, value); -} - static uint32_t adiv5_mem_read32(ADIv5_AP_t *ap, uint32_t addr) { uint32_t ret; @@ -483,29 +478,29 @@ void adiv5_dp_init(ADIv5_DP_t *dp) * correctly on STM32. CDBGRSTACK is never asserted, and we * just wait forever. This scenario is described in B2.4.1 * so we have a timeout mechanism in addition to the sensing one. - */ - - /* Write request for debug reset */ + * + * Write request for debug reset */ adiv5_dp_write(dp, ADIV5_DP_CTRLSTAT, ctrlstat |= ADIV5_DP_CTRLSTAT_CDBGRSTREQ); platform_timeout timeout; - platform_timeout_set(&timeout,200); - /* Wait for acknowledge */ - while ((!platform_timeout_is_expired(&timeout)) && - (!((ctrlstat = adiv5_dp_read(dp, ADIV5_DP_CTRLSTAT)) & ADIV5_DP_CTRLSTAT_CDBGRSTACK)) - ); - + platform_timeout_set(&timeout, 101); /* Write request for debug reset release */ adiv5_dp_write(dp, ADIV5_DP_CTRLSTAT, ctrlstat &= ~ADIV5_DP_CTRLSTAT_CDBGRSTREQ); - - platform_timeout_set(&timeout,200); /* Wait for acknowledge */ - while ((!platform_timeout_is_expired(&timeout)) && - (adiv5_dp_read(dp, ADIV5_DP_CTRLSTAT) & ADIV5_DP_CTRLSTAT_CDBGRSTACK) - ); - DEBUG("RESET_SEQ %s\n", (platform_timeout_is_expired(&timeout)) ? "failed": "succeeded"); + while(1) { + platform_delay(20); + ctrlstat = adiv5_dp_read(dp, ADIV5_DP_CTRLSTAT); + if (ctrlstat & ADIV5_DP_CTRLSTAT_CDBGRSTACK) { + DEBUG("RESET_SEQ succeeded.\n"); + break; + } + if (platform_timeout_is_expired(&timeout)) { + DEBUG("RESET_SEQ failed\n"); + break; + } + } uint32_t dp_idcode = adiv5_dp_read(dp, ADIV5_DP_IDCODE); if ((dp_idcode & ADIV5_DP_VERSION_MASK) == ADIV5_DPv2) { diff --git a/src/target/adiv5.h b/src/target/adiv5.h index 1a6132b..e337e3d 100644 --- a/src/target/adiv5.h +++ b/src/target/adiv5.h @@ -172,6 +172,18 @@ typedef struct ADIv5_DP_s { #endif } ADIv5_DP_t; +struct ADIv5_AP_s { + int refcnt; + + ADIv5_DP_t *dp; + uint8_t apsel; + + uint32_t idr; + uint32_t base; + uint32_t csw; +}; + +#if PC_HOSTED == 0 static inline uint32_t adiv5_dp_read(ADIv5_DP_t *dp, uint16_t addr) { return dp->dp_read(dp, addr); @@ -193,17 +205,6 @@ static inline void adiv5_dp_abort(struct ADIv5_DP_s *dp, uint32_t abort) return dp->abort(dp, abort); } -struct ADIv5_AP_s { - int refcnt; - - ADIv5_DP_t *dp; - uint8_t apsel; - - uint32_t idr; - uint32_t base; - uint32_t csw; -}; - static inline uint32_t adiv5_ap_read(ADIv5_AP_t *ap, uint16_t addr) { return ap->dp->ap_read(ap, addr); @@ -219,21 +220,39 @@ static inline void adiv5_mem_read(ADIv5_AP_t *ap, void *dest, uint32_t src, { return ap->dp->mem_read(ap, dest, src, len); } -static inline void adiv5_mem_write_sized(ADIv5_AP_t *ap, uint32_t dest, - const void *src, size_t len, enum align align) + +static inline void adiv5_mem_write_sized( + ADIv5_AP_t *ap, uint32_t dest, const void *src, size_t len, + enum align align) { return ap->dp->mem_write_sized(ap, dest, src, len, align); } +static inline void adiv5_dp_write(ADIv5_DP_t *dp, uint16_t addr, uint32_t value) +{ + dp->low_access(dp, ADIV5_LOW_WRITE, addr, value); +} + +#else +uint32_t adiv5_dp_read(ADIv5_DP_t *dp, uint16_t addr); +uint32_t adiv5_dp_error(ADIv5_DP_t *dp); +uint32_t adiv5_dp_low_access(struct ADIv5_DP_s *dp, uint8_t RnW, + uint16_t addr, uint32_t value); +void adiv5_dp_abort(struct ADIv5_DP_s *dp, uint32_t abort); +uint32_t adiv5_ap_read(ADIv5_AP_t *ap, uint16_t addr); +void adiv5_ap_write(ADIv5_AP_t *ap, uint16_t addr, uint32_t value); +void adiv5_mem_read(ADIv5_AP_t *ap, void *dest, uint32_t src, size_t len); +void adiv5_mem_write_sized(ADIv5_AP_t *ap, uint32_t dest, + const void *src, size_t len, enum align align); void adiv5_dp_write(ADIv5_DP_t *dp, uint16_t addr, uint32_t value); +#endif + void adiv5_dp_init(ADIv5_DP_t *dp); void platform_adiv5_dp_defaults(ADIv5_DP_t *dp); ADIv5_AP_t *adiv5_new_ap(ADIv5_DP_t *dp, uint8_t apsel); void adiv5_ap_ref(ADIv5_AP_t *ap); void adiv5_ap_unref(ADIv5_AP_t *ap); -void adiv5_ap_write(ADIv5_AP_t *ap, uint16_t addr, uint32_t value); - void adiv5_jtag_dp_handler(jtag_dev_t *dev); int platform_jtag_dp_init(ADIv5_DP_t *dp); @@ -257,5 +276,4 @@ uint32_t fw_adiv5_jtagdp_read(ADIv5_DP_t *dp, uint16_t addr); uint32_t firmware_swdp_error(ADIv5_DP_t *dp); void firmware_swdp_abort(ADIv5_DP_t *dp, uint32_t abort); - #endif