From 108692bb3196a1274c6be538165432fc9a64216a Mon Sep 17 00:00:00 2001 From: Christian Taedcke Date: Sat, 19 Oct 2013 14:09:58 +0200 Subject: [PATCH 1/3] In stm32w_flasher replaced hal-* calls with usage of pyudev. --- .../py_files/rs232_interface.py | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/tools/stm32w/stm32w_flasher/py_files/rs232_interface.py b/tools/stm32w/stm32w_flasher/py_files/rs232_interface.py index ae9c9d3b0..a5b2f9f94 100644 --- a/tools/stm32w/stm32w_flasher/py_files/rs232_interface.py +++ b/tools/stm32w/stm32w_flasher/py_files/rs232_interface.py @@ -4,6 +4,7 @@ import ftdi import os +import pyudev import serial import struct import subprocess @@ -62,10 +63,8 @@ class FTDI_Interface(object): return returnValue def getFTDIIdFromDevPort(self, port): - command = (('for udi in `hal-find-by-capability --capability serial`\n do\n parent=`hal-get-property --udi ${udi} --key "info.parent"`\n device=`hal-get-property --udi ${udi} --key "linux.device_file"`\n grandpa=`hal-get-property --udi ${parent} --key "info.parent"`\n serial=`hal-get-property --udi ${grandpa} --key "usb_device.serial" 2>/dev/null`\n if [ "${device}" = "' + port) + '" ]\n then\n printf "%s" "${serial}"\n break\n fi\n done ') - p = subprocess.Popen([command], shell=True, stdout=subprocess.PIPE) - out = p.communicate() - return (out[0]) + device = pyudev.Device.from_device_file(pyudev.Context(), port) + return device['ID_SERIAL'] def guessResetDirection(self, h): if (self.resetValue is None or self.nBootValue is None): @@ -1020,16 +1019,14 @@ def getAvailableSerialPorts(refreshList=True): global rs232PortsList if (refreshList or rs232PortsList is None): returnValue = {} + context = pyudev.Context() id = [{ 'vendor': FT232R_VENDOR , 'product': FT232R_PRODUCT , 'type': 'FTDI' }, { 'vendor': STM32F103_VENDOR , 'product': STM32F103_PRODUCT , 'type': 'STM32' }, { 'vendor': STM32F103_VENDOR , 'product': STM32F103_PRODUCT_OLD , 'type': 'STM32' }] - for i in range(len(id)): - command = (((('for udi in `hal-find-by-capability --capability serial`\n do\n parent=`hal-get-property --udi ${udi} --key "info.parent"`\n device=`hal-get-property --udi ${udi} --key "linux.device_file"`\n serial=`hal-get-property --udi ${parent} --key "usb.serial" 2>/dev/null`\n vendor=`hal-get-property --udi ${parent} --key "usb.vendor_id" 2>/dev/null`\n product=`hal-get-property --udi ${parent} --key "usb.product_id" 2>/dev/null`\n if [ "${vendor}" = "' + str(((id[i])['vendor']))) + '" ] && [ "${product}" = "') + str(((id[i])['product']))) + '" ]\n then\n printf "%s %s\n" "${device}" "${serial}"\n fi\n done') - p = subprocess.Popen([command], shell=True, stdout=subprocess.PIPE) - out = p.communicate() - for line in (out[0]).splitlines(): - device_serial = line.split(' ') - returnValue[(device_serial[0])] = [((id[i])['type']), (device_serial[1])] - continue - continue + for device in context.list_devices(subsystem='tty', ID_BUS='usb'): + for serialId in id: + #device['ID_VENDOR_ID'] (VID) and device['ID_MODEL_ID'] (PID) are in hex, but without the 0x prefix + if (int(device['ID_VENDOR_ID'], 16) == serialId['vendor']) and (int(device['ID_MODEL_ID'], 16) == serialId['product']): + #e.g. returnValue[/dev/ttyACM0] = [STM32, serial number] + returnValue[(device.device_node)] = [(serialId['type']), (device['ID_SERIAL'])] rs232PortsList = returnValue return rs232PortsList From 6a30e7839046df0cfee597456acc60d53ffeb433 Mon Sep 17 00:00:00 2001 From: Christian Taedcke Date: Tue, 19 Nov 2013 21:47:07 +0100 Subject: [PATCH 2/3] In stm32w_flasher added check for pyudev and updated ubuntu package instructions. --- tools/stm32w/stm32w_flasher/py_files/stm32w_flasher.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/stm32w/stm32w_flasher/py_files/stm32w_flasher.py b/tools/stm32w/stm32w_flasher/py_files/stm32w_flasher.py index 4f486408a..d83c095e7 100755 --- a/tools/stm32w/stm32w_flasher/py_files/stm32w_flasher.py +++ b/tools/stm32w/stm32w_flasher/py_files/stm32w_flasher.py @@ -12,10 +12,11 @@ import optparse, os, sys, time try: import serial import ftdi + import pyudev except: print 'Python modules serial and ftdi could not be loaded.' print 'Please install these dependencies:' - print '(On Ubuntu) $ sudo apt-get install python-serial python-ftdi' + print '(On Ubuntu) $ sudo apt-get install python-serial python-ftdi python-pyudev' sys.exit(-1) from messages import infoMessage, errorMessage From 0de2e6ddedb96ed253b7a598f6368e64f1f02f45 Mon Sep 17 00:00:00 2001 From: Christian Taedcke Date: Thu, 28 Nov 2013 16:49:43 +0100 Subject: [PATCH 3/3] In stm32w_flasher adapted error message for missing pyudev. --- tools/stm32w/stm32w_flasher/py_files/stm32w_flasher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/stm32w/stm32w_flasher/py_files/stm32w_flasher.py b/tools/stm32w/stm32w_flasher/py_files/stm32w_flasher.py index d83c095e7..e3d2f3fa5 100755 --- a/tools/stm32w/stm32w_flasher/py_files/stm32w_flasher.py +++ b/tools/stm32w/stm32w_flasher/py_files/stm32w_flasher.py @@ -14,7 +14,7 @@ try: import ftdi import pyudev except: - print 'Python modules serial and ftdi could not be loaded.' + print 'Python modules serial, ftdi and pyudev could not be loaded.' print 'Please install these dependencies:' print '(On Ubuntu) $ sudo apt-get install python-serial python-ftdi python-pyudev' sys.exit(-1)