/***************************************************************************
- * Copyright (C) 2011 by Mathias Kuester *
+ * Copyright (C) 2011-2012 by Mathias Kuester *
* Mathias Kuester <kesmtp@freenet.de> *
* *
* This code is based on https://github.com/texane/stlink *
#define STLINK_TX_SIZE (4*128)
#define STLINK_RX_SIZE (4*128)
+enum stlink_jtag_api_version {
+ STLINK_JTAG_API_V1 = 0,
+ STLINK_JTAG_API_V2,
+};
+
/** */
struct stlink_usb_version {
/** */
int jtag;
/** */
int swim;
+ /** highest supported jtag api version */
+ enum stlink_jtag_api_version jtag_api_max;
};
/** */
uint16_t pid;
/** */
uint32_t sg_tag;
+ /** this is the currently used jtag api */
+ enum stlink_jtag_api_version jtag_api;
};
-#define STLINK_OK 0x80
-#define STLINK_FALSE 0x81
-#define STLINK_CORE_RUNNING 0x80
-#define STLINK_CORE_HALTED 0x81
-#define STLINK_CORE_STAT_UNKNOWN -1
-
-#define STLINK_GET_VERSION 0xF1
-#define STLINK_DEBUG_COMMAND 0xF2
-#define STLINK_DFU_COMMAND 0xF3
-#define STLINK_SWIM_COMMAND 0xF4
-#define STLINK_GET_CURRENT_MODE 0xF5
-
-#define STLINK_DEV_DFU_MODE 0x00
-#define STLINK_DEV_MASS_MODE 0x01
-#define STLINK_DEV_DEBUG_MODE 0x02
-#define STLINK_DEV_SWIM_MODE 0x03
-#define STLINK_DEV_UNKNOWN_MODE -1
-
-#define STLINK_DFU_EXIT 0x07
-
-#define STLINK_SWIM_ENTER 0x00
-#define STLINK_SWIM_EXIT 0x01
-
-#define STLINK_DEBUG_ENTER_JTAG 0x00
-#define STLINK_DEBUG_GETSTATUS 0x01
-#define STLINK_DEBUG_FORCEDEBUG 0x02
-#define STLINK_DEBUG_RESETSYS 0x03
-#define STLINK_DEBUG_READALLREGS 0x04
-#define STLINK_DEBUG_READREG 0x05
-#define STLINK_DEBUG_WRITEREG 0x06
-#define STLINK_DEBUG_READMEM_32BIT 0x07
-#define STLINK_DEBUG_WRITEMEM_32BIT 0x08
-#define STLINK_DEBUG_RUNCORE 0x09
-#define STLINK_DEBUG_STEPCORE 0x0a
-#define STLINK_DEBUG_SETFP 0x0b
-#define STLINK_DEBUG_READMEM_8BIT 0x0c
-#define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
-#define STLINK_DEBUG_CLEARFP 0x0e
-#define STLINK_DEBUG_WRITEDEBUGREG 0x0f
-
-#define STLINK_DEBUG_ENTER_JTAG 0x00
-#define STLINK_DEBUG_ENTER_SWD 0xa3
-
-#define STLINK_DEBUG_ENTER 0x20
-#define STLINK_DEBUG_EXIT 0x21
-#define STLINK_DEBUG_READCOREID 0x22
+#define STLINK_DEBUG_ERR_OK 0x80
+#define STLINK_DEBUG_ERR_FAULT 0x81
+#define STLINK_CORE_RUNNING 0x80
+#define STLINK_CORE_HALTED 0x81
+#define STLINK_CORE_STAT_UNKNOWN -1
+
+#define STLINK_GET_VERSION 0xF1
+#define STLINK_DEBUG_COMMAND 0xF2
+#define STLINK_DFU_COMMAND 0xF3
+#define STLINK_SWIM_COMMAND 0xF4
+#define STLINK_GET_CURRENT_MODE 0xF5
+
+#define STLINK_DEV_DFU_MODE 0x00
+#define STLINK_DEV_MASS_MODE 0x01
+#define STLINK_DEV_DEBUG_MODE 0x02
+#define STLINK_DEV_SWIM_MODE 0x03
+#define STLINK_DEV_BOOTLOADER_MODE 0x04
+#define STLINK_DEV_UNKNOWN_MODE -1
+
+#define STLINK_DFU_EXIT 0x07
+
+#define STLINK_SWIM_ENTER 0x00
+#define STLINK_SWIM_EXIT 0x01
+
+#define STLINK_DEBUG_ENTER_JTAG 0x00
+#define STLINK_DEBUG_GETSTATUS 0x01
+#define STLINK_DEBUG_FORCEDEBUG 0x02
+#define STLINK_DEBUG_APIV1_RESETSYS 0x03
+#define STLINK_DEBUG_APIV1_READALLREGS 0x04
+#define STLINK_DEBUG_APIV1_READREG 0x05
+#define STLINK_DEBUG_APIV1_WRITEREG 0x06
+#define STLINK_DEBUG_READMEM_32BIT 0x07
+#define STLINK_DEBUG_WRITEMEM_32BIT 0x08
+#define STLINK_DEBUG_RUNCORE 0x09
+#define STLINK_DEBUG_STEPCORE 0x0a
+#define STLINK_DEBUG_APIV1_SETFP 0x0b
+#define STLINK_DEBUG_READMEM_8BIT 0x0c
+#define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
+#define STLINK_DEBUG_APIV1_CLEARFP 0x0e
+#define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
+#define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
+
+#define STLINK_DEBUG_ENTER_JTAG 0x00
+#define STLINK_DEBUG_ENTER_SWD 0xa3
+
+#define STLINK_DEBUG_APIV1_ENTER 0x20
+#define STLINK_DEBUG_EXIT 0x21
+#define STLINK_DEBUG_READCOREID 0x22
+
+#define STLINK_DEBUG_APIV2_ENTER 0x30
+#define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
+#define STLINK_DEBUG_APIV2_RESETSYS 0x32
+#define STLINK_DEBUG_APIV2_READREG 0x33
+#define STLINK_DEBUG_APIV2_WRITEREG 0x34
+
+#define STLINK_DEBUG_APIV2_READALLREGS 0x3A
+
+#define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
+
+#define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
+#define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
+#define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
/** */
enum stlink_mode {
};
/** */
-static void stlink_usb_recv_v1_create_cmd(char *b, int s, uint32_t tag, uint32_t rxsize,
- uint8_t flag, uint8_t lun, uint8_t length)
-{
- int i = 0;
-
- memset(b, 0x00, s);
-
- /* fill the send buffer */
- strcpy(b, "USBC");
- i += 4;
-
- buf_set_u32(b+i, 0, 32, tag);
- i += 4;
- buf_set_u32(b+i, 0, 32, rxsize);
- i += 4;
- b[i++] = flag;
- b[i++] = lun;
- b[i++] = length;
-}
-
-/** */
-static int stlink_usb_recv_v1_mass_storage_cmd(void *handle, const uint8_t *txbuf, int txsize, uint8_t *rxbuf,
- int rxsize)
+static int stlink_usb_xfer_v1_send_cmd(void *handle, const uint8_t *cmd, int cmdsize, int ep, int size)
{
- char sg_buffer[31];
+ uint8_t sg_buffer[31];
struct stlink_usb_handle_s *h;
assert(handle != NULL);
+ assert(cmdsize <= 16);
h = (struct stlink_usb_handle_s *)handle;
- h->sg_tag = (h->sg_tag + 1) & 1;
+ h->sg_tag = (h->sg_tag + 1) & 1; /* seriously? */
+
+ memset(sg_buffer, 0, sizeof(sg_buffer));
+
+ h_u32_to_le(sg_buffer, 0x43425355); /* USBC */
+ h_u32_to_le(&sg_buffer[4], h->sg_tag);
+ h_u32_to_le(&sg_buffer[8], size);
- stlink_usb_recv_v1_create_cmd(sg_buffer, 31, h->sg_tag, rxsize, STLINK_TX_EP, 0x00, txsize);
+ sg_buffer[12] = (ep == STLINK_RX_EP ? ENDPOINT_IN : ENDPOINT_OUT);
+ /* sg_buffer[13] = 0; */
+ sg_buffer[14] = (uint8_t)cmdsize;
- memcpy(sg_buffer+15, txbuf, 10);
+ memcpy(&sg_buffer[15], cmd, cmdsize);
- if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)sg_buffer, 31,
- 1000) != 31) {
- printf("send failed\n");
+ if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)sg_buffer, sizeof(sg_buffer),
+ 1000) != sizeof(sg_buffer)) {
+ LOG_DEBUG("send failed\n");
return ERROR_FAIL;
}
return ERROR_OK;
}
-#define REQUEST_SENSE 0x03
-#define REQUEST_SENSE_LENGTH 18
-
/** */
-static int stlink_usb_recv_v1_get_status(void *handle, char *sg_buffer, int len)
+static int stlink_usb_xfer_v1_get_status(void *handle, uint8_t *sg_buffer, int len)
{
struct stlink_usb_handle_s *h;
h = (struct stlink_usb_handle_s *)handle;
/* read status */
- memset(sg_buffer, 0x00, len);
+ memset(sg_buffer, 0, len);
if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)sg_buffer,
len, 1000) != len)
return ERROR_FAIL;
- uint32_t t1, t2;
-
- t1 = buf_get_u32(sg_buffer+0, 0, 32);
- t2 = buf_get_u32(sg_buffer+4, 0, 32);
+ uint32_t t1 = le_to_h_u32(sg_buffer+0);
+ /* uint32_t t2 = le_to_h_u32(sg_buffer+4); */
/* check for USBS */
if (t1 != 0x53425355)
}
/** */
-static int stlink_usb_recv_v1_get_sense(void *handle)
+static int stlink_usb_xfer_rw(void *handle, int ep, uint8_t *buf, int size)
{
struct stlink_usb_handle_s *h;
- char cdb[16];
- char sg_buffer[31];
+
+ h = (struct stlink_usb_handle_s *)handle;
+
+ if (!size)
+ return ERROR_OK;
+
+ if (ep == STLINK_RX_EP) {
+ if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)buf,
+ size, 1000) != size) {
+ return ERROR_FAIL;
+ }
+ } else {
+ if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)buf,
+ size, 1000) != size) {
+ return ERROR_FAIL;
+ }
+ }
+
+ return ERROR_OK;
+}
+
+/**
+ * http://en.wikipedia.org/wiki/SCSI_Request_Sense_Command
+ */
+static int stlink_usb_xfer_v1_get_sense(void *handle)
+{
+ int err;
+
+ uint8_t cdb[STLINK_CMD_SIZE];
+ uint8_t sense[18];
+ uint8_t status[13];
assert(handle != NULL);
- h = (struct stlink_usb_handle_s *)handle;
- h->sg_tag = (h->sg_tag + 1) & 1;
+ memset(cdb, 0, sizeof(cdb));
- cdb[0] = REQUEST_SENSE;
- cdb[4] = REQUEST_SENSE_LENGTH;
+ cdb[0] = 0x03;
+ cdb[4] = sizeof(sense);
- stlink_usb_recv_v1_create_cmd(sg_buffer, 31, h->sg_tag, REQUEST_SENSE_LENGTH, STLINK_TX_EP,
- 0x00, 16);
+ err = stlink_usb_xfer_v1_send_cmd(handle, cdb, sizeof(cdb), STLINK_RX_EP, sizeof(sense));
- memcpy(sg_buffer+15, cdb, 16);
+ if (err != ERROR_OK)
+ return err;
- if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)sg_buffer, 16,
- 1000) != 16)
- return ERROR_FAIL;
+ err = stlink_usb_xfer_rw(handle, STLINK_RX_EP, sense, sizeof(sense));
- if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)cdb,
- 16, 1000) != 16)
- return ERROR_FAIL;
+ if (err != ERROR_OK)
+ return err;
+
+ err = stlink_usb_xfer_v1_get_status(handle, status, sizeof(status));
+
+ if (err != ERROR_OK)
+ return err;
- if (stlink_usb_recv_v1_get_status(handle, sg_buffer, 13) != ERROR_OK)
- return ERROR_FAIL;
/* check for sense */
- if (sg_buffer[12] != 0)
+ if (status[12] != 0)
return ERROR_FAIL;
/* if (sense[0] != 0x70 && sense[0] != 0x71) */
- return ERROR_OK;
+ return err;
}
/** */
-static int stlink_usb_recv_v1(void *handle, const uint8_t *txbuf, int txsize, uint8_t *rxbuf,
- int rxsize)
+static int stlink_usb_xfer_v1_check_status(void *handle)
{
int err;
- char sg_buffer[31];
- struct stlink_usb_handle_s *h;
-
- assert(handle != NULL);
-
- h = (struct stlink_usb_handle_s *)handle;
+ uint8_t sg_buffer[13];
- err = stlink_usb_recv_v1_mass_storage_cmd(handle, txbuf, txsize, rxbuf, rxsize);
+ err = stlink_usb_xfer_v1_get_status(handle, sg_buffer, sizeof(sg_buffer));
if (err != ERROR_OK)
return err;
- if (rxsize && rxbuf) {
- if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)rxbuf,
- rxsize, 1000) != rxsize) {
- LOG_DEBUG("jtag_libusb_bulk_read");
- return ERROR_FAIL;
- }
- }
-
- if (stlink_usb_recv_v1_get_status(handle, sg_buffer, 13) != ERROR_OK)
- return ERROR_FAIL;
/* check for sense */
if (sg_buffer[12] == 1) {
LOG_DEBUG("get sense");
- err = stlink_usb_recv_v1_get_sense(handle);
+
+ err = stlink_usb_xfer_v1_get_sense(handle);
}
+
return err;
}
/** */
-static int stlink_usb_recv_v2(void *handle, const uint8_t *txbuf, int txsize, uint8_t *rxbuf,
- int rxsize)
+static int stlink_usb_xfer_v1(void *handle, const uint8_t *cmd, int cmdsize, int ep,
+ uint8_t *buf, int size)
+{
+ int err;
+
+ assert(handle != NULL);
+
+ err = stlink_usb_xfer_v1_send_cmd(handle, cmd, cmdsize, ep, size);
+
+ if (err != ERROR_OK)
+ return err;
+
+ err = stlink_usb_xfer_rw(handle, ep, buf, size);
+
+ if (err != ERROR_OK)
+ return err;
+
+ return stlink_usb_xfer_v1_check_status(handle);
+}
+
+/** */
+static int stlink_usb_xfer_v2(void *handle, const uint8_t *cmd, int cmdsize, int ep,
+ uint8_t *buf, int size)
{
struct stlink_usb_handle_s *h;
h = (struct stlink_usb_handle_s *)handle;
- if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)txbuf, txsize,
- 1000) != txsize) {
+ if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)cmd, cmdsize,
+ 1000) != cmdsize) {
return ERROR_FAIL;
}
- if (rxsize && rxbuf) {
- if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)rxbuf,
- rxsize, 1000) != rxsize) {
- return ERROR_FAIL;
- }
- }
- return ERROR_OK;
+
+ return stlink_usb_xfer_rw(handle, ep, buf, size);
}
/** */
-static int stlink_usb_recv(void *handle, const uint8_t *txbuf, int txsize, uint8_t *rxbuf,
- int rxsize)
+static int stlink_usb_xfer(void *handle, const uint8_t *cmd, int cmdsize, int ep,
+ uint8_t *buf, int size)
{
struct stlink_usb_handle_s *h;
assert(handle != NULL);
+ assert(cmdsize == STLINK_CMD_SIZE);
h = (struct stlink_usb_handle_s *)handle;
if (h->version.stlink == 1) {
- return stlink_usb_recv_v1(handle, txbuf, txsize, rxbuf, rxsize);
+ return stlink_usb_xfer_v1(handle, cmd, cmdsize, ep, buf, size);
} else {
- if (txsize < STLINK_CMD_SIZE)
- txsize = STLINK_CMD_SIZE;
- return stlink_usb_recv_v2(handle, txbuf, txsize, rxbuf, rxsize);
+ return stlink_usb_xfer_v2(handle, cmd, cmdsize, ep, buf, size);
}
}
+/** */
+static int stlink_usb_recv(void *handle, const uint8_t *cmd, int cmdsize, uint8_t *rxbuf,
+ int rxsize)
+{
+ return stlink_usb_xfer(handle, cmd, cmdsize, STLINK_RX_EP, rxbuf, rxsize);
+}
+
+/** */
+static int stlink_usb_send(void *handle, const uint8_t *cmd, int cmdsize, uint8_t *txbuf,
+ int txsize)
+{
+ return stlink_usb_xfer(handle, cmd, cmdsize, STLINK_TX_EP, txbuf, txsize);
+}
+
/** */
static void stlink_usb_init_buffer(void *handle)
{
h = (struct stlink_usb_handle_s *)handle;
- memset(h->txbuf, 0, STLINK_CMD_SIZE);
+ memset(h->txbuf, 0, STLINK_TX_SIZE);
+ memset(h->rxbuf, 0, STLINK_RX_SIZE);
+}
+
+static const char * const stlink_usb_error_msg[] = {
+ "unknown"
+};
+
+/** */
+static int stlink_usb_error_check(void *handle)
+{
+ int res;
+ const char *err_msg = 0;
+ struct stlink_usb_handle_s *h;
+
+ assert(handle != NULL);
+
+ h = (struct stlink_usb_handle_s *)handle;
+
+ /* TODO: no error checking yet on api V1 */
+ if (h->jtag_api == STLINK_JTAG_API_V1)
+ h->rxbuf[0] = STLINK_DEBUG_ERR_OK;
+
+ switch (h->rxbuf[0]) {
+ case STLINK_DEBUG_ERR_OK:
+ res = ERROR_OK;
+ break;
+ case STLINK_DEBUG_ERR_FAULT:
+ default:
+ err_msg = stlink_usb_error_msg[0];
+ res = ERROR_FAIL;
+ break;
+ }
+
+ if (res != ERROR_OK)
+ LOG_DEBUG("status error: %d ('%s')", h->rxbuf[0], err_msg);
+
+ return res;
}
/** */
h->vid = buf_get_u32(h->rxbuf, 16, 16);
h->pid = buf_get_u32(h->rxbuf, 32, 16);
- LOG_DEBUG("STLINK v%d JTAG v%d SWIM v%d VID %04X PID %04X",
+ /* set the supported jtag api version
+ * V1 doesn't support API V2 at all
+ * V2 support API V2 since JTAG V13
+ */
+ if ((h->version.stlink == 2) && (h->version.jtag > 12))
+ h->version.jtag_api_max = STLINK_JTAG_API_V2;
+ else
+ h->version.jtag_api_max = STLINK_JTAG_API_V1;
+
+ LOG_DEBUG("STLINK v%d JTAG v%d API v%d SWIM v%d VID %04X PID %04X",
h->version.stlink,
h->version.jtag,
+ (h->version.jtag_api_max == STLINK_JTAG_API_V1) ? 1 : 2,
h->version.swim,
h->vid,
h->pid);
static int stlink_usb_mode_enter(void *handle, enum stlink_mode type)
{
int res;
+ int rx_size = 0;
struct stlink_usb_handle_s *h;
assert(handle != NULL);
switch (type) {
case STLINK_MODE_DEBUG_JTAG:
h->txbuf[0] = STLINK_DEBUG_COMMAND;
- h->txbuf[1] = STLINK_DEBUG_ENTER;
+ if (h->jtag_api == STLINK_JTAG_API_V1)
+ h->txbuf[1] = STLINK_DEBUG_APIV1_ENTER;
+ else
+ h->txbuf[1] = STLINK_DEBUG_APIV2_ENTER;
h->txbuf[2] = STLINK_DEBUG_ENTER_JTAG;
break;
case STLINK_MODE_DEBUG_SWD:
h->txbuf[0] = STLINK_DEBUG_COMMAND;
- h->txbuf[1] = STLINK_DEBUG_ENTER;
+ if (h->jtag_api == STLINK_JTAG_API_V1)
+ h->txbuf[1] = STLINK_DEBUG_APIV1_ENTER;
+ else
+ h->txbuf[1] = STLINK_DEBUG_APIV2_ENTER;
h->txbuf[2] = STLINK_DEBUG_ENTER_SWD;
break;
case STLINK_MODE_DEBUG_SWIM:
return ERROR_FAIL;
}
- res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
+ /* on api V2 we are able the read the latest command
+ * status
+ * TODO: we need the test on api V1 too
+ */
+ if (h->jtag_api == STLINK_JTAG_API_V2)
+ rx_size = 2;
+
+ res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, rx_size);
+
if (res != ERROR_OK)
return res;
- return ERROR_OK;
+ res = stlink_usb_error_check(h);
+
+ return res;
}
/** */
}
res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
+
if (res != ERROR_OK)
return res;
case STLINK_DEV_SWIM_MODE:
emode = STLINK_MODE_DEBUG_SWIM;
break;
+ case STLINK_DEV_BOOTLOADER_MODE:
+ case STLINK_DEV_MASS_MODE:
default:
emode = STLINK_MODE_UNKNOWN;
break;
h = (struct stlink_usb_handle_s *)handle;
+ if (h->jtag_api == STLINK_JTAG_API_V2)
+ return TARGET_UNKNOWN;
+
stlink_usb_init_buffer(handle);
h->txbuf[0] = STLINK_DEBUG_COMMAND;
stlink_usb_init_buffer(handle);
h->txbuf[0] = STLINK_DEBUG_COMMAND;
- h->txbuf[1] = STLINK_DEBUG_RESETSYS;
+
+ if (h->jtag_api == STLINK_JTAG_API_V1)
+ h->txbuf[1] = STLINK_DEBUG_APIV1_RESETSYS;
+ else
+ h->txbuf[1] = STLINK_DEBUG_APIV2_RESETSYS;
res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
h = (struct stlink_usb_handle_s *)handle;
+ if (h->jtag_api == STLINK_JTAG_API_V2)
+ return ERROR_FAIL;
+
stlink_usb_init_buffer(handle);
h->txbuf[0] = STLINK_DEBUG_COMMAND;
h = (struct stlink_usb_handle_s *)handle;
+ if (h->jtag_api == STLINK_JTAG_API_V2)
+ return ERROR_FAIL;
+
stlink_usb_init_buffer(handle);
h->txbuf[0] = STLINK_DEBUG_COMMAND;
h = (struct stlink_usb_handle_s *)handle;
+ if (h->jtag_api == STLINK_JTAG_API_V2)
+ return ERROR_FAIL;
+
stlink_usb_init_buffer(handle);
h->txbuf[0] = STLINK_DEBUG_COMMAND;
stlink_usb_init_buffer(handle);
h->txbuf[0] = STLINK_DEBUG_COMMAND;
- h->txbuf[1] = STLINK_DEBUG_READALLREGS;
+ if (h->jtag_api == STLINK_JTAG_API_V1)
+ h->txbuf[1] = STLINK_DEBUG_APIV1_READALLREGS;
+ else
+ h->txbuf[1] = STLINK_DEBUG_APIV2_READALLREGS;
res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 84);
stlink_usb_init_buffer(handle);
h->txbuf[0] = STLINK_DEBUG_COMMAND;
- h->txbuf[1] = STLINK_DEBUG_READREG;
+ if (h->jtag_api == STLINK_JTAG_API_V1)
+ h->txbuf[1] = STLINK_DEBUG_APIV1_READREG;
+ else
+ h->txbuf[1] = STLINK_DEBUG_APIV2_READREG;
h->txbuf[2] = num;
res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 4);
stlink_usb_init_buffer(handle);
h->txbuf[0] = STLINK_DEBUG_COMMAND;
- h->txbuf[1] = STLINK_DEBUG_WRITEREG;
+ if (h->jtag_api == STLINK_JTAG_API_V1)
+ h->txbuf[1] = STLINK_DEBUG_APIV1_WRITEREG;
+ else
+ h->txbuf[1] = STLINK_DEBUG_APIV2_WRITEREG;
h->txbuf[2] = num;
h_u32_to_le(h->txbuf + 3, val);
h_u32_to_le(h->txbuf + 2, addr);
h_u16_to_le(h->txbuf + 2 + 4, len);
- res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
-
- if (res != ERROR_OK)
- return res;
-
- res = stlink_usb_recv(handle, (uint8_t *) buffer, len, 0, 0);
+ res = stlink_usb_send(handle, h->txbuf, STLINK_CMD_SIZE, (uint8_t *) buffer, len);
if (res != ERROR_OK)
return res;
h_u32_to_le(h->txbuf + 2, addr);
h_u16_to_le(h->txbuf + 2 + 4, len);
- res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
-
- if (res != ERROR_OK)
- return res;
-
- res = stlink_usb_recv(handle, (uint8_t *) buffer, len, 0, 0);
+ res = stlink_usb_send(handle, h->txbuf, STLINK_CMD_SIZE, (uint8_t *) buffer, len);
if (res != ERROR_OK)
return res;
return err;
}
+ /* set the used jtag api */
+ h->jtag_api = STLINK_JTAG_API_V1;
+
+ /* initialize the debug hardware */
err = stlink_usb_init_mode(h);
if (err != ERROR_OK) {