Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STLINK/V3-SET: If the device fails to enter SWD mode, the usb device is not released #1399

Open
5 tasks done
TheFissk opened this issue May 24, 2024 · 18 comments
Open
5 tasks done

Comments

@TheFissk
Copy link

TheFissk commented May 24, 2024

  • I made serious effort to avoid creating duplicate or nearly similar issue

  • Programmer/board type: STLINK/V3-SET
  • Operating system an version: Linux (Ubuntu 22.04 LTS + a really out of date version of Debian)
  • stlink tools version and/or git commit hash: v1.8.0
  • stlink commandline tool name: Tested against st-flash and st-info

When there is no board connected to the programmer causing it to fail to enter SWD mode subsequent invokations of the tool hang before failing.

Commandline output:
These commands are separated by several seconds, I'm not machinegunning the tool expecting it to work. The first command returns immediately, the second hangs for ~10s

$ st-flash reset
st-flash 1.8.0
Failed to enter SWD mode
Failed to connect to target
Failed to parse flash type or unrecognized flash type //This line went to stderr rather than stdout
$ st-flash reset
st-flash 1.8.0
2024-05-24T10:41:08 ERROR usb.c: GET_VERSION send request failed: LIBUSB_ERROR_TIMEOUT
2024-05-24T10:41:11 ERROR usb.c: GET_CURRENT_MODE send request failed: LIBUSB_ERROR_TIMEOUT
2024-05-24T10:41:14 ERROR usb.c: GET_COM_FREQ send request failed: LIBUSB_ERROR_TIMEOUT
2024-05-24T10:41:17 ERROR usb.c: GET_CURRENT_MODE send request failed: LIBUSB_ERROR_TIMEOUT
2024-05-24T10:41:20 ERROR usb.c: ENTER_SWD send request failed: LIBUSB_ERROR_TIMEOUT
Failed to parse flash type or unrecognized flash type
Failed to enter SWD mode
Failed to connect to target
2024-05-24T10:41:23 ERROR usb.c: DEBUG_EXIT send request failed: LIBUSB_ERROR_TIMEOUT

I also tested it against st-info --chipid and it works in much the same way, but it suppresses the stderr output, so that the program hangs for a ~10 seconds before outputting:

$ st-info --chipid
Failed to enter SWD mode
0x0000

Plugging a board into the programmer once its entered this degraded state does not resolve the issue, it will continue to output the same errors

st-info --probe works as expected returning instantly regardless of whether or not the device was able to enter SWD mode in previous invokations of st-info --probe

The V2 programmer works as expected, returning instantly even after failing to enter SWD mode.

Expected/description:

If the V3 programmer should return instantly after failing to enter SWD mode, regardless of whether or not it was able to enter SWD mode previously.

@Nightwalker-87 Nightwalker-87 added os/linux component/st-flash programmer/STLINK/V3 V3SET / V3MODS / V3MINI / V3MINIE / V3E / V3EC labels May 26, 2024
@Nightwalker-87
Copy link
Member

@Ant-ON: Can we be sure about that the STLINK-V3-SET is fully supported yet?
The only thing I found are the respective VID:PIDs in the udev-rules ...

@Nightwalker-87 Nightwalker-87 added this to the v1.8.1 milestone May 29, 2024
@Nightwalker-87 Nightwalker-87 moved this to Ready in Release v1.8.1 May 29, 2024
@Nightwalker-87 Nightwalker-87 moved this from Ready to In progress in Release v1.8.1 May 29, 2024
@Ant-ON
Copy link
Collaborator

Ant-ON commented Jun 4, 2024

@Nightwalker-87 STLINK-V3-SET has the same commands as STLINK-V3, therefore the STLINK-V3-SET should be supported too

@Nightwalker-87
Copy link
Member

So why shall there a problem then which does not seem to occur with other variants?
Users: Please test your devices and give feedback on any issues.

@ankurraji
Copy link
Contributor

ankurraji commented Jul 19, 2024

I have the same issue with my ST-LINK V3 SET.

sending flash reset command when no MCU is connected, leads to first this message

$ st-flash reset
st-flash 1.8.0
Failed to enter SWD mode
Failed to connect to target
Failed to parse flash type or unrecognized flash type

And when the command is triggered another time you will receive an error

$ st-flash reset
st-flash 1.8.0
2024-07-19T13:20:37 ERROR usb.c: GET_VERSION send request failed: LIBUSB_ERROR_TIMEOUT
2024-07-19T13:20:40 ERROR usb.c: GET_CURRENT_MODE send request failed: LIBUSB_ERROR_TIMEOUT
2024-07-19T13:20:43 ERROR usb.c: GET_COM_FREQ send request failed: LIBUSB_ERROR_TIMEOUT
2024-07-19T13:20:46 ERROR usb.c: GET_CURRENT_MODE send request failed: LIBUSB_ERROR_TIMEOUT
2024-07-19T13:20:49 ERROR usb.c: ENTER_SWD send request failed: LIBUSB_ERROR_TIMEOUT
Failed to enter SWD mode
Failed to connect to target
Failed to parse flash type or unrecognized flash type
2024-07-19T13:20:52 ERROR usb.c: DEBUG_EXIT send request failed: LIBUSB_ERROR_TIMEOUT

The debug flag does not give more information

$ st-flash reset --debug
st-flash 1.8.0
2024-07-19T13:20:59 ERROR usb.c: GET_VERSION send request failed: LIBUSB_ERROR_TIMEOUT
2024-07-19T13:21:02 ERROR usb.c: GET_CURRENT_MODE send request failed: LIBUSB_ERROR_TIMEOUT
2024-07-19T13:21:05 ERROR usb.c: GET_COM_FREQ send request failed: LIBUSB_ERROR_TIMEOUT
2024-07-19T13:21:08 ERROR usb.c: GET_CURRENT_MODE send request failed: LIBUSB_ERROR_TIMEOUT
2024-07-19T13:21:11 ERROR usb.c: ENTER_SWD send request failed: LIBUSB_ERROR_TIMEOUT
Failed to enter SWD mode
Failed to connect to target
Failed to parse flash type or unrecognized flash type
2024-07-19T13:21:14 ERROR usb.c: DEBUG_EXIT send request failed: LIBUSB_ERROR_TIMEOUT

Additionally, it looks like the ST-LINK V3 SET is stuck/hanging. This is something that I diagonalised with the LEDs and the response time delay when you execute the st-flash reset. Sometimes I have to wait for ~10 Secs to see any log message printed out in the terminal after the line st-flash 1.8.0 is printed.

ST-LINK-V3-SET Stuck

The issue remains the same even though I connect the MCU back again to the ST-LINK V3 SET; only a power reset of debugger i.e. unplugging the USB cable and connecting it back again solves the issue.

Finally, as @TheFissk mentioned, I too have not observed any issue with ST-LINK V2 it just comes out of the command with an error, and is able to connect to the MCU when the MCU is connected again to the debugger. The V2 does not produce LIBUSB_ERROR_TIMEOUT error.

Flash reset with ST-LINK V2.

$ st-flash reset
st-flash 1.8.0
Failed to enter SWD mode
Failed to connect to target
Failed to parse flash type or unrecognized flash type

Feel free to comment, if more information is required 😃

@Nightwalker-87 Nightwalker-87 moved this from In progress to Ready in Release v1.8.1 Aug 2, 2024
@Nightwalker-87 Nightwalker-87 pinned this issue Aug 11, 2024
@bfueldner
Copy link

bfueldner commented Sep 6, 2024

I can confirm the behaviour like @ankurraji mentioned with v1.8.0 and ST-LINK V3 MINIE. So it seems not to depend only on the combination command set and ST-LINK V3 SET.

@Nightwalker-87
Copy link
Member

Please investigate further. We need some volunteers with the appropriate hardware that are able to look into this.

@ypearson-bdai
Copy link

Got this on a stm32 G0 using a STlink-v3mini
st-flash 1.8.0
2024-10-24T13:09:06 ERROR usb.c: GET_VERSION send request failed: LIBUSB_ERROR_TIMEOUT
2024-10-24T13:09:09 ERROR usb.c: GET_CURRENT_MODE send request failed: LIBUSB_ERROR_TIMEOUT
2024-10-24T13:09:12 ERROR usb.c: GET_COM_FREQ send request failed: LIBUSB_ERROR_TIMEOUT
2024-10-24T13:09:15 ERROR usb.c: GET_CURRENT_MODE send request failed: LIBUSB_ERROR_TIMEOUT
2024-10-24T13:09:18 ERROR usb.c: ENTER_SWD send request failed: LIBUSB_ERROR_TIMEOUT
Failed to enter SWD mode
Failed to connect to target
Failed to parse flash type or unrecognized flash type
2024-10-24T13:09:21 ERROR usb.c: DEBUG_EXIT send request failed: LIBUSB_ERROR_TIMEOUT

@Nightwalker-87
Copy link
Member

We are still looking for further investigation and further ideas here. Please contribute.

@jmmunoz86
Copy link

jmmunoz86 commented Dec 19, 2024

I'm having the same issue but using the ST-LINK from the nucleo-L476RG (FW version V2J45M31):

PS C:\Users\Jonathan\Desktop\nucleo_l476rg_freeRTOS_VSCODE> st-flash --reset  write .\build\nucleo_l476rg_freeRTOS.bin 0x08000000  
st-flash 1.8.0
libusb: info [cache_config_descriptors] could not access configuration descriptor 1 (dummy) for 'USB\VID_0BDA&PID_8153\001000001': [31] A device attached to the system is not functioning.
Failed to enter SWD mode
Failed to connect to target
Failed to parse flash type or unrecognized flash type

I could flash my device just once. Following flashes failed. I can connect my nucleo board with the STM32CubeProgrammer and flash it.
What always have appeared is that libusb line despite the success/failure of the flashing process.

@Nightwalker-87
Copy link
Member

Please re-test this on a linux machine with a current libusb package from the default internal package source.

@TheFissk
Copy link
Author

TheFissk commented Jan 7, 2025

it still fails in the same way on my system, compiled against libusb 1.0.27

@Nightwalker-87
Copy link
Member

As far as I could investigate, this needs to be addressed within /src/stlink-lib/usb.c.

@Nightwalker-87
Copy link
Member

Nightwalker-87 commented Jan 11, 2025

I suppose the bug is somewhere here in usb.c:

int32_t _stlink_usb_enter_swd_mode(stlink_t * sl) {
    struct stlink_libusb * const slu = sl->backend_data;
    unsigned char* const cmd  = sl->c_buf;
    ssize_t size;
    unsigned char* const data = sl->q_buf;
    const uint32_t rep_len = sl->version.jtag_api == STLINK_JTAG_API_V1 ? 0 : 2;
    int32_t i = fill_command(sl, SG_DXFER_FROM_DEV, rep_len);

    cmd[i++] = STLINK_DEBUG_COMMAND;
    // select correct API-Version for entering SWD mode: V1 API (0x20) or V2 API (0x30).
    cmd[i++] = sl->version.jtag_api == STLINK_JTAG_API_V1 ? STLINK_DEBUG_APIV1_ENTER : STLINK_DEBUG_APIV2_ENTER;
    cmd[i++] = STLINK_DEBUG_ENTER_SWD;
    size = send_recv(slu, 1, cmd, slu->cmd_len, data, rep_len, CMD_CHECK_RETRY, "ENTER_SWD");

    return (size < 0 ? -1 : 0);
}

We find that STLINK_DEBUG_APIV3_ENTER is missing. The JTAG_API version is checked for v1 and if that turns out false, v2 is automatically selected. I could imagine this to be a relict from earlier times when STLINK/V3 programmers did not exist. That would also explain why this happens with different STLINK/V3 variants. (I expect that they all constantly fail to enter SWD mode.) As there are specific API V3 enums present, these would clearly require a STLINK_DEBUG_APIV3_ENTER.

@Nightwalker-87
Copy link
Member

Along with this topic, I propose some restructuring in the codebase regarding existing STLINK commands, defined in commands.h. If technically possible, I'd consider it a good idea to separate and resort specific commands for /v1 /v2 and /v3. The idea would also include dissolving the entanglement between the three different APIs in common.c and move the resorted code to a separate source file in order to ease maintenance. This is also in the context of common.c historically being a relatively unstructured hodgepodge of everything. How do you think about this?

@Nightwalker-87 Nightwalker-87 changed the title [STLINK-V3-SET]: If the device fails to enter SWD mode, the usb device is not released STLINK/V3-SET: If the device fails to enter SWD mode, the usb device is not released Jan 12, 2025
@Nightwalker-87
Copy link
Member

To highlight this renaming has taken place:

  • common.c --> common_legacy.c
  • sg.c --> sg_legacy.c
  • sg.h --> sg_legacy.h

I found that the whole STLINK/v3 implementation started here with commit 09ea99a.

@Nightwalker-87
Copy link
Member

Nightwalker-87 commented Jan 17, 2025

The STLINK-V3MINIE also failed to enter SWD mode here in a very similar way.
However, this was caused by a VCC wire connected to the wrong pin in a 20-pin JTAG header.
Note that the pin 2 VCC assignment is only optional here. There are other 20-pin pinouts where VCC is allocated to pin 2.

@TheFissk Can you rule out such a hardware-related cause?

@TheFissk
Copy link
Author

@Nightwalker-87 I got the same behaviour on both of my V3 SETS, on both the 1.8 release, the latest changes in the develop branch. both with the provided mezzanine board attached and without. Always without any 3rd party boards attached (no chance that anything other than the official ST hardware was the source of the problem) So unless ST messed up the hardware on both of my programmers, its unlikely that hardware is to blame.

I should note that if I expect to be able to enter SWD (there is a board attached to my programmer) everything works as expected. Its only when there isn't a board attached, and I shouldn't be able to enter SWD that I see this problem. (ie on the second running of the command the program hangs with libusb timeout errors, rather than failing instantly as in the first running).

@Nightwalker-87
Copy link
Member

@TheFissk: Thank you for the clarifications. We need to check further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: In progress
Development

No branches or pull requests

7 participants