In stm32w_flasher replaced hal-* calls with usage of pyudev.

This commit is contained in:
Christian Taedcke 2013-10-19 14:09:58 +02:00
parent 6dd9f2c37a
commit 108692bb31

View File

@ -4,6 +4,7 @@
import ftdi import ftdi
import os import os
import pyudev
import serial import serial
import struct import struct
import subprocess import subprocess
@ -62,10 +63,8 @@ class FTDI_Interface(object):
return returnValue return returnValue
def getFTDIIdFromDevPort(self, port): 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 ') device = pyudev.Device.from_device_file(pyudev.Context(), port)
p = subprocess.Popen([command], shell=True, stdout=subprocess.PIPE) return device['ID_SERIAL']
out = p.communicate()
return (out[0])
def guessResetDirection(self, h): def guessResetDirection(self, h):
if (self.resetValue is None or self.nBootValue is None): if (self.resetValue is None or self.nBootValue is None):
@ -1020,16 +1019,14 @@ def getAvailableSerialPorts(refreshList=True):
global rs232PortsList global rs232PortsList
if (refreshList or rs232PortsList is None): if (refreshList or rs232PortsList is None):
returnValue = {} 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' }] 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)): for device in context.list_devices(subsystem='tty', ID_BUS='usb'):
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') for serialId in id:
p = subprocess.Popen([command], shell=True, stdout=subprocess.PIPE) #device['ID_VENDOR_ID'] (VID) and device['ID_MODEL_ID'] (PID) are in hex, but without the 0x prefix
out = p.communicate() if (int(device['ID_VENDOR_ID'], 16) == serialId['vendor']) and (int(device['ID_MODEL_ID'], 16) == serialId['product']):
for line in (out[0]).splitlines(): #e.g. returnValue[/dev/ttyACM0] = [STM32, serial number]
device_serial = line.split(' ') returnValue[(device.device_node)] = [(serialId['type']), (device['ID_SERIAL'])]
returnValue[(device_serial[0])] = [((id[i])['type']), (device_serial[1])]
continue
continue
rs232PortsList = returnValue rs232PortsList = returnValue
return rs232PortsList return rs232PortsList