X-Git-Url: https://review.openocd.org/gitweb?p=openocd.git;a=blobdiff_plain;f=src%2Fjtag%2Fdrivers%2Fstlink_usb.c;h=1f3fbe2cdc7d7112b7e729084979505e95910902;hp=0a16f2c07691b6d1b903b1cb7be4771c2478011a;hb=refs%2Fchanges%2F60%2F1560%2F3;hpb=e0d4d46dbec3b729ea8ca51c247ad00c7c12edd7 diff --git a/src/jtag/drivers/stlink_usb.c b/src/jtag/drivers/stlink_usb.c index 0a16f2c076..1f3fbe2cdc 100644 --- a/src/jtag/drivers/stlink_usb.c +++ b/src/jtag/drivers/stlink_usb.c @@ -20,7 +20,7 @@ * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * ***************************************************************************/ #ifdef HAVE_CONFIG_H @@ -45,6 +45,7 @@ #define STLINK_NULL_EP 0 #define STLINK_RX_EP (1|ENDPOINT_IN) #define STLINK_TX_EP (2|ENDPOINT_OUT) +#define STLINK_TRACE_EP (3|ENDPOINT_IN) #define STLINK_SG_SIZE (31) #define STLINK_DATA_SIZE (4*128) #define STLINK_CMD_SIZE_V2 (16) @@ -91,6 +92,17 @@ struct stlink_usb_handle_s { uint16_t pid; /** this is the currently used jtag api */ enum stlink_jtag_api_version jtag_api; + /** */ + struct { + /** whether SWO tracing is enabled or not */ + bool enabled; + /** trace data destination file */ + FILE *output_f; + /** trace module source clock (for prescaler) */ + uint32_t source_hz; + /** trace module clock prescaler */ + uint32_t prescale; + } trace; }; #define STLINK_DEBUG_ERR_OK 0x80 @@ -107,6 +119,7 @@ struct stlink_usb_handle_s { #define STLINK_DFU_COMMAND 0xF3 #define STLINK_SWIM_COMMAND 0xF4 #define STLINK_GET_CURRENT_MODE 0xF5 +#define STLINK_GET_TARGET_VOLTAGE 0xF7 #define STLINK_DEV_DFU_MODE 0x00 #define STLINK_DEV_MASS_MODE 0x01 @@ -157,10 +170,18 @@ struct stlink_usb_handle_s { #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C +#define STLINK_DEBUG_APIV2_START_TRACE_RX 0x40 +#define STLINK_DEBUG_APIV2_STOP_TRACE_RX 0x41 +#define STLINK_DEBUG_APIV2_GET_TRACE_NB 0x42 + #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00 #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01 #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02 +#define STLINK_TRACE_SIZE 1024 +#define STLINK_TRACE_MAX_HZ 2000000 +#define STLINK_TRACE_MIN_VERSION 13 + /** */ enum stlink_mode { STLINK_MODE_UNKNOWN = 0, @@ -304,6 +325,26 @@ static int stlink_usb_xfer(void *handle, const uint8_t *buf, int size) return ERROR_OK; } +/** */ +static int stlink_usb_read_trace(void *handle, const uint8_t *buf, int size) +{ + struct stlink_usb_handle_s *h; + + assert(handle != NULL); + + h = (struct stlink_usb_handle_s *)handle; + + assert(h->version.stlink >= 2); + + if (jtag_libusb_bulk_read(h->fd, STLINK_TRACE_EP, (char *)buf, + size, 1000) != size) { + LOG_ERROR("bulk trace read failed"); + return ERROR_FAIL; + } + + return ERROR_OK; +} + /** */ static void stlink_usb_xfer_v1_create_cmd(void *handle, uint8_t direction, uint32_t size) { @@ -424,6 +465,40 @@ static int stlink_usb_version(void *handle) return ERROR_OK; } +static int stlink_usb_check_voltage(void *handle, float *target_voltage) +{ + struct stlink_usb_handle_s *h; + uint32_t adc_results[2]; + + h = (struct stlink_usb_handle_s *)handle; + + /* only supported by stlink/v2 and for firmware >= 13 */ + if (h->version.stlink == 1 || h->version.jtag < 13) + return ERROR_COMMAND_NOTFOUND; + + stlink_usb_init_buffer(handle, STLINK_RX_EP, 8); + + h->cmdbuf[h->cmdidx++] = STLINK_GET_TARGET_VOLTAGE; + + int result = stlink_usb_xfer(handle, h->databuf, 8); + + if (result != ERROR_OK) + return result; + + /* convert result */ + adc_results[0] = le_to_h_u32(h->databuf); + adc_results[1] = le_to_h_u32(h->databuf + 4); + + *target_voltage = 0; + + if (adc_results[0]) + *target_voltage = 2 * ((float)adc_results[1]) * (float)(1.2 / adc_results[0]); + + LOG_INFO("Target voltage: %f", (double)*target_voltage); + + return ERROR_OK; +} + /** */ static int stlink_usb_current_mode(void *handle, uint8_t *mode) { @@ -544,8 +619,10 @@ static int stlink_usb_mode_leave(void *handle, enum stlink_mode type) return ERROR_OK; } +static int stlink_usb_assert_srst(void *handle, int srst); + /** */ -static int stlink_usb_init_mode(void *handle) +static int stlink_usb_init_mode(void *handle, bool connect_under_reset) { int res; uint8_t mode; @@ -593,6 +670,29 @@ static int stlink_usb_init_mode(void *handle) if (res != ERROR_OK) return res; + /* we check the target voltage here as an aid to debugging connection problems. + * the stlink requires the target Vdd to be connected for reliable debugging. + * this cmd is supported in all modes except DFU + */ + if (mode != STLINK_DEV_DFU_MODE) { + + float target_voltage; + + /* check target voltage (if supported) */ + res = stlink_usb_check_voltage(h, &target_voltage); + + if (res != ERROR_OK) { + if (res != ERROR_COMMAND_NOTFOUND) + LOG_ERROR("voltage check failed"); + /* attempt to continue as it is not a catastrophic failure */ + } else { + /* check for a sensible target voltage, operating range is 1.65-5.5v + * according to datasheet */ + if (target_voltage < 1.5) + LOG_ERROR("target voltage may be too low for reliable debugging"); + } + } + LOG_DEBUG("MODE: 0x%02X", mode); /* set selected mode */ @@ -616,6 +716,12 @@ static int stlink_usb_init_mode(void *handle) return ERROR_FAIL; } + if (connect_under_reset) { + res = stlink_usb_assert_srst(handle, 0); + if (res != ERROR_OK) + return res; + } + res = stlink_usb_mode_enter(handle, emode); if (res != ERROR_OK) @@ -713,6 +819,42 @@ static int stlink_usb_write_debug_reg(void *handle, uint32_t addr, uint32_t val) return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL; } +/** */ +static void stlink_usb_trace_read(void *handle) +{ + struct stlink_usb_handle_s *h; + + assert(handle != NULL); + + h = (struct stlink_usb_handle_s *)handle; + + if (h->trace.enabled && h->version.jtag >= STLINK_TRACE_MIN_VERSION) { + int res; + + stlink_usb_init_buffer(handle, STLINK_RX_EP, 10); + + h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND; + h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GET_TRACE_NB; + + res = stlink_usb_xfer(handle, h->databuf, 2); + if (res == ERROR_OK) { + uint8_t buf[STLINK_TRACE_SIZE]; + size_t size = le_to_h_u16(h->databuf); + + if (size > 0) { + size = size < sizeof(buf) ? size : sizeof(buf) - 1; + + res = stlink_usb_read_trace(handle, buf, size); + if (res == ERROR_OK) { + /* Log retrieved trace output */ + if (fwrite(buf, 1, size, h->trace.output_f) > 0) + fflush(h->trace.output_f); + } + } + } + } +} + static enum target_state stlink_usb_v2_get_status(void *handle) { int result; @@ -727,6 +869,8 @@ static enum target_state stlink_usb_v2_get_status(void *handle) else if (status & S_RESET_ST) return TARGET_RESET; + stlink_usb_trace_read(handle); + return TARGET_RUNNING; } @@ -820,6 +964,132 @@ static int stlink_usb_assert_srst(void *handle, int srst) return h->databuf[0] == STLINK_DEBUG_ERR_OK ? ERROR_OK : ERROR_FAIL; } +/** */ +static int stlink_configure_target_trace_port(void *handle) +{ + int res; + uint32_t reg; + struct stlink_usb_handle_s *h; + + assert(handle != NULL); + + h = (struct stlink_usb_handle_s *)handle; + + /* configure the TPI */ + + /* enable the trace subsystem */ + res = stlink_usb_v2_read_debug_reg(handle, DCB_DEMCR, ®); + if (res != ERROR_OK) + goto out; + res = stlink_usb_write_debug_reg(handle, DCB_DEMCR, TRCENA|reg); + if (res != ERROR_OK) + goto out; + /* set the TPI clock prescaler */ + res = stlink_usb_write_debug_reg(handle, TPI_ACPR, h->trace.prescale); + if (res != ERROR_OK) + goto out; + /* select the pin protocol. The STLinkv2 only supports asynchronous + * UART emulation (NRZ) mode, so that's what we pick. */ + res = stlink_usb_write_debug_reg(handle, TPI_SPPR, 0x02); + if (res != ERROR_OK) + goto out; + /* disable continuous formatting */ + res = stlink_usb_write_debug_reg(handle, TPI_FFCR, (1<<8)); + if (res != ERROR_OK) + goto out; + + /* configure the ITM */ + + /* unlock access to the ITM registers */ + res = stlink_usb_write_debug_reg(handle, ITM_LAR, 0xC5ACCE55); + if (res != ERROR_OK) + goto out; + /* enable trace with ATB ID 1 */ + res = stlink_usb_write_debug_reg(handle, ITM_TCR, (1<<16)|(1<<0)|(1<<2)); + if (res != ERROR_OK) + goto out; + /* trace privilege */ + res = stlink_usb_write_debug_reg(handle, ITM_TPR, 1); + if (res != ERROR_OK) + goto out; + /* trace port enable (port 0) */ + res = stlink_usb_write_debug_reg(handle, ITM_TER, (1<<0)); + if (res != ERROR_OK) + goto out; + + res = ERROR_OK; +out: + return res; +} + +/** */ +static void stlink_usb_trace_disable(void *handle) +{ + int res = ERROR_OK; + struct stlink_usb_handle_s *h; + + assert(handle != NULL); + + h = (struct stlink_usb_handle_s *)handle; + + assert(h->version.jtag >= STLINK_TRACE_MIN_VERSION); + + LOG_DEBUG("Tracing: disable\n"); + + stlink_usb_init_buffer(handle, STLINK_RX_EP, 2); + h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND; + h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX; + res = stlink_usb_xfer(handle, h->databuf, 2); + + if (res == ERROR_OK) + h->trace.enabled = false; +} + + +/** */ +static int stlink_usb_trace_enable(void *handle) +{ + int res; + struct stlink_usb_handle_s *h; + + assert(handle != NULL); + + h = (struct stlink_usb_handle_s *)handle; + + if (h->version.jtag >= STLINK_TRACE_MIN_VERSION) { + uint32_t trace_hz; + + res = stlink_configure_target_trace_port(handle); + if (res != ERROR_OK) + LOG_ERROR("Unable to configure tracing on target\n"); + + trace_hz = h->trace.prescale > 0 ? + h->trace.source_hz / (h->trace.prescale + 1) : + h->trace.source_hz; + + stlink_usb_init_buffer(handle, STLINK_RX_EP, 10); + + h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND; + h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_START_TRACE_RX; + h_u16_to_le(h->cmdbuf+h->cmdidx, (uint16_t)STLINK_TRACE_SIZE); + h->cmdidx += 2; + h_u32_to_le(h->cmdbuf+h->cmdidx, trace_hz); + h->cmdidx += 4; + + res = stlink_usb_xfer(handle, h->databuf, 2); + + if (res == ERROR_OK) { + h->trace.enabled = true; + LOG_DEBUG("Tracing: recording at %uHz\n", trace_hz); + } + } else { + LOG_ERROR("Tracing is not supported by this version."); + res = ERROR_FAIL; + } + + return res; +} + /** */ static int stlink_usb_run(void *handle) { @@ -830,8 +1100,19 @@ static int stlink_usb_run(void *handle) h = (struct stlink_usb_handle_s *)handle; - if (h->jtag_api == STLINK_JTAG_API_V2) - return stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_DEBUGEN); + if (h->jtag_api == STLINK_JTAG_API_V2) { + res = stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_DEBUGEN); + + /* Try to start tracing, if requested */ + if (res == ERROR_OK && h->trace.output_f) { + if (stlink_usb_trace_enable(handle) == ERROR_OK) + LOG_DEBUG("Tracing: enabled\n"); + else + LOG_ERROR("Tracing: enable failed\n"); + } + + return res; + } stlink_usb_init_buffer(handle, STLINK_RX_EP, 2); @@ -856,8 +1137,14 @@ static int stlink_usb_halt(void *handle) h = (struct stlink_usb_handle_s *)handle; - if (h->jtag_api == STLINK_JTAG_API_V2) - return stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN); + if (h->jtag_api == STLINK_JTAG_API_V2) { + res = stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN); + + if (res == ERROR_OK && h->trace.enabled) + stlink_usb_trace_disable(handle); + + return res; + } stlink_usb_init_buffer(handle, STLINK_RX_EP, 2); @@ -1255,8 +1542,19 @@ static int stlink_usb_open(struct hl_interface_param_s *param, void **fd) /* set the used jtag api, this will default to the newest supported version */ h->jtag_api = api; + if (h->jtag_api >= 2 && param->trace_f && param->trace_source_hz > 0) { + uint32_t prescale; + + prescale = param->trace_source_hz > STLINK_TRACE_MAX_HZ ? + (param->trace_source_hz / STLINK_TRACE_MAX_HZ) - 1 : 0; + + h->trace.output_f = param->trace_f; + h->trace.source_hz = param->trace_source_hz; + h->trace.prescale = prescale; + } + /* initialize the debug hardware */ - err = stlink_usb_init_mode(h); + err = stlink_usb_init_mode(h, param->connect_under_reset); if (err != ERROR_OK) { LOG_ERROR("init mode failed");