On Tue, Feb 21, 2012 at 8:13 AM, Mathias K. <[email protected]> wrote:
>
> You should look into texane sources. The function in question is named
> "get_sense".
I was actually looking at stlink-usb.c (it has v1 paths too), which
doesn't have any equivalent of that function.
I've added an implementation based on stlink-sg.c and the Wikipedia
entry in the attached patch, but the code is never executed during my
tests.
>
> Anyway. You should split your patch into the memory fix and the rewrite thing.
>
Ok, but it's not much of a split. There are some explanations in the
patch files.
The stlink_usb_recv function works more or less like before:
-send stlink command
-receive data
-check status
The new stlink_usb_send function (required by the fix) works like this:
-send stlink command
-send data
-check status
I've re-factored the code to facilitate the implementation of
stlink_usb_send without code duplication.
So the re-factoring is the bulk, and the fix itself is only a small
part of the patch.
Regards,
Szymon Modzelewski
From 70a6ff882b3182ea0aa7479e78ab8774bdcb7b21 Mon Sep 17 00:00:00 2001
From: szmodzelewski <[email protected]>
Date: Tue, 21 Feb 2012 13:43:11 +0100
Subject: [PATCH 1/2] stlink-v1: code cleanup
This patch moves the bulk of the stlink read/write code into the
stlink_usb_xfer set of functions and implements stlink_usb_recv and
stlink_usb_send in terms of the generic stlink_usb_xfer
stlink_usb_xfer:
-sends the stlink command
-performs a read or write (as requested)
-checks the status (v1 only)
---
src/jtag/drivers/stlink_usb.c | 237 +++++++++++++++++++++++------------------
1 files changed, 134 insertions(+), 103 deletions(-)
diff --git a/src/jtag/drivers/stlink_usb.c b/src/jtag/drivers/stlink_usb.c
index 3cb2434..8e90ac3 100644
--- a/src/jtag/drivers/stlink_usb.c
+++ b/src/jtag/drivers/stlink_usb.c
@@ -158,56 +158,40 @@ 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)
+static int stlink_usb_xfer_v1_send_cmd(void *handle, const uint8_t *cmd, int cmdsize, int ep, int size)
{
- 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)
-{
- 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;
-
- stlink_usb_recv_v1_create_cmd(sg_buffer, 31, h->sg_tag, rxsize, STLINK_TX_EP, 0x00, txsize);
-
- memcpy(sg_buffer+15, txbuf, 10);
-
- if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)sg_buffer, 31,
- 1000) != 31) {
- printf("send failed\n");
+ 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);
+
+ 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], cmd, cmdsize);
+
+ 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;
@@ -216,16 +200,14 @@ static int stlink_usb_recv_v1_get_status(void *handle, char *sg_buffer, int len)
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)
@@ -235,81 +217,134 @@ static int stlink_usb_recv_v1_get_status(void *handle, char *sg_buffer, int len)
}
/** */
-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];
-
- assert(handle != NULL);
-
+
h = (struct stlink_usb_handle_s *)handle;
- h->sg_tag = (h->sg_tag + 1) & 1;
-
- cdb[0] = REQUEST_SENSE;
- cdb[4] = REQUEST_SENSE_LENGTH;
+
+ 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;
+}
- stlink_usb_recv_v1_create_cmd(sg_buffer, 31, h->sg_tag, REQUEST_SENSE_LENGTH, STLINK_TX_EP,
- 0x00, 16);
+/**
+ * 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];
- memcpy(sg_buffer+15, cdb, 16);
+ assert(handle != NULL);
+
+ memset(cdb, 0, sizeof(cdb));
+
+ cdb[0] = 0x03;
+ cdb[4] = sizeof(sense);
- if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)sg_buffer, 16,
- 1000) != 16)
- return ERROR_FAIL;
+ err = stlink_usb_xfer_v1_send_cmd(handle, cdb, sizeof(cdb), STLINK_RX_EP, 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_rw(handle, STLINK_RX_EP, sense, sizeof(sense));
- if (stlink_usb_recv_v1_get_status(handle, sg_buffer, 13) != ERROR_OK)
- 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;
+
/* 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;
+ uint8_t sg_buffer[13];
+
+ err = stlink_usb_xfer_v1_get_status(handle, sg_buffer, sizeof(sg_buffer));
+
+ if (err != ERROR_OK)
+ return err;
+
+ /* check for sense */
+ if (sg_buffer[12] == 1) {
+ LOG_DEBUG("get sense");
+
+ err = stlink_usb_xfer_v1_get_sense(handle);
+ }
+
+ return err;
+}
+
+/** */
+static int stlink_usb_xfer_v1(void *handle, const uint8_t *cmd, int cmdsize, int ep,
+ uint8_t *buf, int size)
{
int err;
- char sg_buffer[31];
- struct stlink_usb_handle_s *h;
assert(handle != NULL);
- h = (struct stlink_usb_handle_s *)handle;
+ err = stlink_usb_xfer_v1_send_cmd(handle, cmd, cmdsize, ep, size);
- err = stlink_usb_recv_v1_mass_storage_cmd(handle, txbuf, txsize, rxbuf, rxsize);
+ if (err != ERROR_OK)
+ return err;
+
+ err = stlink_usb_xfer_rw(handle, ep, buf, size);
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;
- }
- }
+ return stlink_usb_xfer_v1_check_status(handle);
+}
- if (stlink_usb_recv_v1_get_status(handle, sg_buffer, 13) != ERROR_OK)
+/** */
+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;
+
+ assert(handle != NULL);
+
+ h = (struct stlink_usb_handle_s *)handle;
+
+ if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)cmd, cmdsize,
+ 1000) != cmdsize) {
return ERROR_FAIL;
- /* check for sense */
- if (sg_buffer[12] == 1) {
- LOG_DEBUG("get sense");
- err = stlink_usb_recv_v1_get_sense(handle);
}
- return err;
+
+ return stlink_usb_xfer_rw(handle, ep, buf, size);
}
/** */
-static int stlink_usb_recv_v2(void *handle, const uint8_t *txbuf, int txsize, uint8_t *rxbuf,
+static int stlink_usb_recv(void *handle, const uint8_t *cmd, int cmdsize, uint8_t *rxbuf,
int rxsize)
{
struct stlink_usb_handle_s *h;
@@ -317,36 +352,32 @@ static int stlink_usb_recv_v2(void *handle, const uint8_t *txbuf, int txsize, ui
assert(handle != NULL);
h = (struct stlink_usb_handle_s *)handle;
+
+ assert(cmdsize == STLINK_CMD_SIZE);
- if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)txbuf, txsize,
- 1000) != txsize) {
- return ERROR_FAIL;
- }
- if (rxsize && rxbuf) {
- if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)rxbuf,
- rxsize, 1000) != rxsize) {
- return ERROR_FAIL;
- }
+ if (h->version.stlink == 1) {
+ return stlink_usb_xfer_v1(handle, cmd, cmdsize, STLINK_RX_EP, rxbuf, rxsize);
+ } else {
+ return stlink_usb_xfer_v2(handle, cmd, cmdsize, STLINK_RX_EP, rxbuf, rxsize);
}
- return ERROR_OK;
}
/** */
-static int stlink_usb_recv(void *handle, const uint8_t *txbuf, int txsize, uint8_t *rxbuf,
- int rxsize)
+static int stlink_usb_send(void *handle, const uint8_t *cmd, int cmdsize, uint8_t *txbuf,
+ int txsize)
{
struct stlink_usb_handle_s *h;
assert(handle != NULL);
h = (struct stlink_usb_handle_s *)handle;
+
+ assert(cmdsize == STLINK_CMD_SIZE);
if (h->version.stlink == 1) {
- return stlink_usb_recv_v1(handle, txbuf, txsize, rxbuf, rxsize);
+ return stlink_usb_xfer_v1(handle, cmd, cmdsize, STLINK_TX_EP, txbuf, txsize);
} 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, STLINK_TX_EP, txbuf, txsize);
}
}
--
1.7.5.4
From 1f4695d6f76a250438913bf9981e719828454b77 Mon Sep 17 00:00:00 2001
From: szmodzelewski <[email protected]>
Date: Tue, 21 Feb 2012 13:44:00 +0100
Subject: [PATCH 2/2] stlink-v1: fix memory writes
change the stlink_usb_write_mem functions to use stlink_usb_send
using two calls to stlink_usb_recv is inappropriate since each
call issues a SG command on stlink-v1, resulting in errors
---
src/jtag/drivers/stlink_usb.c | 16 +++-------------
1 files changed, 3 insertions(+), 13 deletions(-)
diff --git a/src/jtag/drivers/stlink_usb.c b/src/jtag/drivers/stlink_usb.c
index 8e90ac3..ea27dc9 100644
--- a/src/jtag/drivers/stlink_usb.c
+++ b/src/jtag/drivers/stlink_usb.c
@@ -984,12 +984,7 @@ static int stlink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len,
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;
@@ -1046,13 +1041,8 @@ static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
h->txbuf[1] = STLINK_DEBUG_WRITEMEM_32BIT;
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;
--
1.7.5.4
------------------------------------------------------------------------------
Keep Your Developer Skills Current with LearnDevNow!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-d2d
_______________________________________________
OpenOCD-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openocd-devel