X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2Fstlink-sg.c;h=b8220de133bf2c26a5f6b92338c87b288bb8e09c;hb=105e266a05bdc2ffefacedc7ffe9e23201a417f9;hp=891d16e1b7c08cbeef6d62100774f0f8fd7e88ef;hpb=d12da44c747ef42e0b4d6c5944103fec3e3681d3;p=fw%2Fstlink diff --git a/src/stlink-sg.c b/src/stlink-sg.c index 891d16e..b8220de 100644 --- a/src/stlink-sg.c +++ b/src/stlink-sg.c @@ -63,6 +63,9 @@ to your /etc/modprobe.conf or /etc/modprobe.d/local.conf (or add the "quirks=..." part to an existing options line for usb-storage). + + https://wiki.kubuntu.org/Kernel/Debugging/USB explains the protocoll and + would allow to replace the sg access to pure libusb access */ @@ -77,12 +80,14 @@ #include #include +#include "stlink-common.h" + +#if CONFIG_USE_LIBSG // sgutils2 (apt-get install libsgutils2-dev) #include #include - -#include "stlink-common.h" #include "stlink-sg.h" +#endif // Suspends execution of the calling process for @@ -116,9 +121,7 @@ void _stlink_sg_close(stlink_t *sl) { if (sl) { struct stlink_libsg *slsg = sl->backend_data; scsi_pt_close_device(slsg->sg_fd); - // CAUTION!? s this right? free(slsg); - free(sl); } } @@ -264,6 +267,36 @@ void stlink_stat(stlink_t *stl, char *txt) { } } + +static void parse_version(stlink_t *stl) { + struct stlink_libsg *sl = stl->backend_data; + + sl->st_vid = 0; + sl->stlink_pid = 0; + + if (stl->q_len <= 0) { + fprintf(stderr, "Error: could not parse the stlink version"); + return; + } + + uint32_t b0 = stl->q_buf[0]; //lsb + uint32_t b1 = stl->q_buf[1]; + uint32_t b2 = stl->q_buf[2]; + uint32_t b3 = stl->q_buf[3]; + uint32_t b4 = stl->q_buf[4]; + uint32_t b5 = stl->q_buf[5]; //msb + + // b0 b1 || b2 b3 | b4 b5 + // 4b | 6b | 6b || 2B | 2B + // stlink_v | jtag_v | swim_v || st_vid | stlink_pid + + sl->stlink_v = (b0 & 0xf0) >> 4; + sl->jtag_v = ((b0 & 0x0f) << 2) | ((b1 & 0xc0) >> 6); + sl->swim_v = b1 & 0x3f; + sl->st_vid = (b3 << 8) | b2; + sl->stlink_pid = (b5 << 8) | b4; +} + void _stlink_sg_version(stlink_t *stl) { struct stlink_libsg *sl = stl->backend_data; D(stl, "\n*** stlink_version ***\n"); @@ -272,6 +305,7 @@ void _stlink_sg_version(stlink_t *stl) { stl->q_len = 6; sl->q_addr = 0; stlink_q(stl); + parse_version(stl); } // Get stlink mode: @@ -403,7 +437,7 @@ void _stlink_sg_status(stlink_t *sl) { // Force the core into the debug mode -> halted state. -void stlink_force_debug(stlink_t *sl) { +void _stlink_sg_force_debug(stlink_t *sl) { struct stlink_libsg *sg = sl->backend_data; D(sl, "\n*** stlink_force_debug ***\n"); clear_cdb(sg); @@ -418,6 +452,10 @@ void stlink_force_debug(stlink_t *sl) { void _stlink_sg_read_all_regs(stlink_t *sl, reg *regp) { struct stlink_libsg *sg = sl->backend_data; + + /* unused */ + regp = regp; + clear_cdb(sg); sg->cdb_cmd_blk[1] = STLINK_DEBUG_READALLREGS; sl->q_len = 84; @@ -718,7 +756,8 @@ stlink_backend_t _stlink_sg_backend = { _stlink_sg_read_reg, _stlink_sg_write_reg, _stlink_sg_step, - _stlink_sg_current_mode + _stlink_sg_current_mode, + _stlink_sg_force_debug }; stlink_t* stlink_open(const char *dev_name, const int verbose) { @@ -775,7 +814,7 @@ stlink_t* stlink_quirk_open(const char *dev_name, const int verbose) { stlink_version(sl); struct stlink_libsg *sg = sl->backend_data; - if (sg->st_vid != USB_ST_VID || sg->stlink_pid != USB_STLINK_PID) { + if ((sg->st_vid != USB_ST_VID) || (sg->stlink_pid != USB_STLINK_PID)) { fprintf(stderr, "Error: the device %s is not a stlink\n", dev_name); fprintf(stderr, " VID: got %04x expect %04x \n",