21 from eric7.EricWidgets.EricPathPicker import EricPathPickerModes |
21 from eric7.EricWidgets.EricPathPicker import EricPathPickerModes |
22 from eric7.SystemUtilities import FileSystemUtilities, OSUtilities |
22 from eric7.SystemUtilities import FileSystemUtilities, OSUtilities |
23 |
23 |
24 from . import Devices |
24 from . import Devices |
25 from .Ui_UF2FlashDialog import Ui_UF2FlashDialog |
25 from .Ui_UF2FlashDialog import Ui_UF2FlashDialog |
|
26 |
|
27 with contextlib.suppress(ImportError): |
|
28 import usb.core |
26 |
29 |
27 SupportedUF2Boards = { |
30 SupportedUF2Boards = { |
28 "circuitpython": { |
31 "circuitpython": { |
29 "volumes": { |
32 "volumes": { |
30 (0x03EB, 0x2402): [ |
33 (0x03EB, 0x2402): [ |
795 @rtype list of tuple of (str, str, (int, int)) |
798 @rtype list of tuple of (str, str, (int, int)) |
796 """ |
799 """ |
797 foundDevices = set() |
800 foundDevices = set() |
798 |
801 |
799 # step 1: determine all known UF2 volumes that are mounted |
802 # step 1: determine all known UF2 volumes that are mounted |
800 if not OSUtilities.isWindowsPlatform(): |
803 userMounts = ( |
801 userMounts = FileSystemUtilities.getUserMounts() |
804 [] if OSUtilities.isWindowsPlatform() else FileSystemUtilities.getUserMounts() |
|
805 ) |
802 |
806 |
803 boardTypes = [boardType] if boardType else list(SupportedUF2Boards) |
807 boardTypes = [boardType] if boardType else list(SupportedUF2Boards) |
804 for board in boardTypes: |
808 for board in boardTypes: |
805 for vidpid, volumes in SupportedUF2Boards[board]["volumes"].items(): |
809 for vidpid, volumes in SupportedUF2Boards[board]["volumes"].items(): |
806 for volume, description in volumes: |
810 for volume, description in volumes: |
811 # check only user mounted devices on non-Windows systems |
815 # check only user mounted devices on non-Windows systems |
812 for userMount in userMounts: |
816 for userMount in userMounts: |
813 if os.path.basename(userMount).startswith(volume): |
817 if os.path.basename(userMount).startswith(volume): |
814 foundDevices.add((board, description, vidpid)) |
818 foundDevices.add((board, description, vidpid)) |
815 break |
819 break |
|
820 if len(foundDevices) > 1: |
|
821 # There are potentially different devices with same mount path/volume. |
|
822 # Check the USB bus for the VID/PID of found devices and remove all |
|
823 # those not found on the bus. |
|
824 with contextlib.suppress(NameError, IOError): |
|
825 for board, description, vidpid in foundDevices.copy(): |
|
826 if not usb.core.find(idVendor=vidpid[0], idProduct=vidpid[1]): |
|
827 foundDevices.discard((board, description, vidpid)) |
816 |
828 |
817 # step 2: determine all devices that have their UF2 volume not mounted |
829 # step 2: determine all devices that have their UF2 volume not mounted |
818 availablePorts = QSerialPortInfo.availablePorts() |
830 availablePorts = QSerialPortInfo.availablePorts() |
819 for port in availablePorts: |
831 for port in availablePorts: |
820 vid = port.vendorIdentifier() |
832 vid = port.vendorIdentifier() |