cortexm / pyswd

Python SWD Debugging library for ST-Link/V2
MIT License
61 stars 21 forks source link

ST-Link not detected #18

Open MrMfromK opened 2 years ago

MrMfromK commented 2 years ago

Hi Pavel,

I just installed swd and wrote a small program to start with. But it does not find my ST-Link V3 nor a STM32-Nucleo board. I found issue #15 which looked for all devices and copied that to my program, so I hav this now:

#!/usr/bin/env python3 
# coding: utf-8 

import sys
import swd
import usb

def main ():
    devices = []
    # dev = usb.core.find(idVendor = 0x0483, idProduct = 0x374B, iProduct = 0x5, find_all = True)
    dev = usb.core.find(find_all = True)
    for device in dev:
        devices.append (device)
        print (hex(device.idVendor), hex(device.idProduct), device.serial_number)

    STLink = swd.Swd (swd_frequency=4000000, serial_no='')
    print (STLink)

if __name__ == '__main__':
    sys.exit(main())    

it produces this output:

0x4f2 0xb613 None
0x4f2 0xb604 0001
Traceback (most recent call last):
  File "E:\Projects\Software\Python\PySWD_Test\source\PySWD_Test.py", line 22, in <module>
    sys.exit(main())
  File "E:\Projects\Software\Python\PySWD_Test\source\PySWD_Test.py", line 17, in main
    STLink = swd.Swd (swd_frequency=4000000, serial_no='')
  File "C:\Users\Martin\AppData\Roaming\Python\Python310\site-packages\swd\swd.py", line 15, in __init__
    driver = _Stlink(
  File "C:\Users\Martin\AppData\Roaming\Python\Python310\site-packages\swd\stlink\__init__.py", line 114, in __init__
    usb = _usb.StlinkUsb(serial_no, debug=debug)
  File "C:\Users\Martin\AppData\Roaming\Python\Python310\site-packages\swd\stlink\usb.py", line 222, in __init__
    raise NoDeviceFoundException()
swd.stlink.usb.NoDeviceFoundException

With the upper dev = ... line nothing is found I changed idProduct to 0x374B (instead of 0x374F as in issue #15), because I found this value in Windows device manager. I'm running Python 3.10 under Win 10 (also swd.Swd () does not accept a keyword argument 'logger': error message is :TypeError: Swd.init() got an unexpected keyword argument 'logger')

Thanks for any help Martin

pavelrevak commented 2 years ago

what version do you use? is it latest? in main branch is supported 0x374b: https://github.com/cortexm/pyswd/blob/master/swd/stlink/usb.py#L115

MrMfromK commented 2 years ago

Yes, the latest, I downloaded today. Meanwhile I saw that windows device manager shows 0x374b for STM32 Nucleo board and 0x374F for ST-Link V3. But neither is found when I run the python script with the right value for the connected one. Also I checked out usb.py and I found both values (and more).

MrMfromK commented 2 years ago

Hi Pavel, what can I do to find the cause of this? Can I do some tests, output some data or so? I am really keen on using pyswd for my project, so would like to get it running. Thanks Martin

pavelrevak commented 2 years ago
0x4f2 0xb613 None
0x4f2 0xb604 0001

these devices are not supported, what st-link do you have ? and what version is there ? are these numbers really from st-link ?

MrMfromK commented 2 years ago

Hi Pavel,

yes, it’s true, these are not ST-Link. Meanwhile I know a little more and can say that the problem is not a PySWD issue but a PyUSB one. It is PyUSB that does not find the devices. I will try from a Raspi and see if that works.

Thanks for your help

Martin

pavelrevak commented 2 years ago

Try to install drivers for ST-link, this will make access to these drivers from libusb (hope)

MrMfromK commented 2 years ago

Hi Pavel, meanwhile I found time to install and test PySWD on a Raspberry Pi (Model 3B) and it works out of the box, STLink, a Nucleo in my case is found and I can read and set memory, thats what I tried so far. Also I did a lot of research to get this working under windows but my current conclusion ist that there is only one way and that is to install libusb as a driver for the target STLink rendering it unaccessible for CubeIDE, CubeProgrammer and so on. So this is not a solution in most cases and that means PySWD will not work under windows. I'll be happy to learn and experience something different, but I really spent hours... Anyway, thank you for providing PySWD I will use it on a Raspi.

Martin

pavelrevak commented 2 years ago

I have had also used it on windows.

drivers for ST-LINK add (enable) access for this USB device (it will install only .inf file) libusb is library used for access to USB ports

MrMfromK commented 2 years ago

Hi Pavel, please can you give more details about the process to setup the driver for an ST-Link. I looked into windows device manager, the entry of my ST-Link. It has a driver installed and also an .inf-file. I found this inf-file in C:\Windows\INF and it says it uses winusb.sys. So where do I get the driver you mentioned and how to install it? And most important: will I then still be able to use say Cube-IDE for program upload and debug and PySWD alternately? I'ld really love to get it running that way. Thanks for any help Martin

pavelrevak commented 2 years ago

I don;t have windows at home, so I can't test this, but if I remember, the process was to install:

MrMfromK commented 2 years ago

Hi Pavel, now I am a little further. I had to use Zadig to install the driver, I will report details later. Now PyUSB is running also under Windows and I see a lot of USB devices. Among them my STLinkV3 and Nucleo boards for G07 and H7. Unfortunatelly the lines `
STLink = swd.Swd ()

print (STLink)

print (STLink.get_version ().str) ` run fine only for the Nucleo G07 Nucleo H7 produces this output:

Traceback (most recent call last):
  File "E:\Projects\Software\Python\PySWD_Test\source\PySWD_Test.py", line 42, in <module>
    sys.exit(main())
  File "E:\Projects\Software\Python\PySWD_Test\source\PySWD_Test.py", line 35, in main
    STLink = swd.Swd ()
  File "C:\Program Files\Python310\lib\site-packages\swd\swd.py", line 15, in __init__
    driver = _Stlink(
  File "C:\Program Files\Python310\lib\site-packages\swd\stlink\__init__.py", line 123, in __init__
    _check_status(status)
  File "C:\Program Files\Python310\lib\site-packages\swd\stlink\__init__.py", line 56, in _check_status
    raise StlinkException(_com.StlinkCom.STATUS.MESSAGES[status])
swd.stlink.StlinkException: Get IDCODE error

and ST-Link V3 produces this output:
Traceback (most recent call last):
  File "E:\Projects\Software\Python\PySWD_Test\source\PySWD_Test.py", line 42, in <module>
    sys.exit(main())
  File "E:\Projects\Software\Python\PySWD_Test\source\PySWD_Test.py", line 35, in main
    STLink = swd.Swd ()
  File "C:\Program Files\Python310\lib\site-packages\swd\swd.py", line 15, in __init__
    driver = _Stlink(
  File "C:\Program Files\Python310\lib\site-packages\swd\stlink\__init__.py", line 114, in __init__
    usb = _usb.StlinkUsb(serial_no, debug=debug)
  File "C:\Program Files\Python310\lib\site-packages\swd\stlink\usb.py", line 210, in __init__
    raise NoDeviceFoundException()
swd.stlink.usb.NoDeviceFoundException

All three the same on windows and Raspberry Pi so I think it must be somewhere in the PySWD code. Updating them to the latest ST-Link FW version did not change the bahaviour.

Can you help please, it seems success is getting closer...

Thanks a lot

Martin

MrMfromK commented 2 years ago

I think I should give you some details because that might help you further investigate in detail: In some stackoverflow topic I read that one has to use Zadig to use libusb and so be able to talk to USB devices through PyUSB. So I did this with some Nucleo board Also I read that you have to specify the libusb-file to use with backendUsblib1 = libusb1.get_backend(find_library=lambda x: "C:\Windows\SysWOW64\libusb-1.0.dll") (I have 64-bit Python, it does not work giving the file C:\Windows\System32\libusb-1.0.dll, although both file are in these places and are identical) and then use PyUSB with dev = usb.core.find(backend=backendUsblib1, find_all=True)
With the driver installed by Zadig suddenly I could see a lot of USB devices. And I can see all three ST-Link. Also this was sufficient for the Nucleo G07 to run the line STLink = swd.Swd () But not for the others. Now I am not sure if swd.Swd () is using the same driver as given above, I guess not. How can I force swd.SWD() to use libusb1 and the driver in C:\Windows\SysWOW64\libusb-1.0.dll ?

Thanks for any help Martin

pavelrevak commented 2 years ago

okay, swd.stlink.usb.NoDeviceFoundException means that your stlinkv3 is not supported look here if your v3 is here (by ID_VENDOR and ID_PRODUCT): https://github.com/cortexm/pyswd/blob/master/swd/stlink/usb.py ..from line 106

MrMfromK commented 2 years ago

yes I looked at these lines before and my STlink is 0x483 and 0x374F, so it is in your list.

pavelrevak commented 2 years ago

please provide me debug info with parameter -ddd also with command line which you used

MrMfromK commented 2 years ago

Ah, now I took a closer look, my sources are different although pip-installed just 4 weeks back I have two entries StlinkUsbV3 one of them with PID 0x374F. I will try an update or do it manually

MrMfromK commented 2 years ago

step by step... now I copied the new usb.py to my PySWD installation and checked the other files are equal to the latest version and now for my ST-Link V3 I also get "Get IDCODE error" like for the Nucleo H7 board. Where do I have to set parameter -ddd?

MrMfromK commented 2 years ago

More details: I also have here a Nucleo G0B1, that works fine Then I have more Nucleo H7, looking all the same at first glance, four work fine, one does not, not one single time, it always comes up with Get IDCODE error Further I have two new Nucleo H7, both work fine and they show up to be a ST-Link/V3E Finally I checked a second Nucleo G07, it also runs fine. So if it was only for the Nucleo H7 I'ld kick that out, but the ST-Link V3 should work and also it seems there is some sporadic error about reading out the devices. Who knows if it is only with reading the IDCODE. I'll be happy to give you more information to find the error. Just tell me what I have to do.... PS : I can check more Nucleos and ST-Link V2 and V3 in a few days.

schneidersoft commented 2 years ago

just a though, You can try updating the STLINK fw. Also always try to make this work on linux first. That way you eliminate all the libusb not working on windows garbage.