Merge pull request #386 from chrta/stm32w_flasher_udev

In stm32w_flasher replaced hal-* calls with usage of pyudev.
This commit is contained in:
Mariano Alvira 2013-12-02 09:21:39 -08:00
commit ab8fb6c396
2 changed files with 13 additions and 15 deletions

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

View File

@ -12,10 +12,11 @@ import optparse, os, sys, time
try: try:
import serial import serial
import ftdi import ftdi
import pyudev
except: 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 '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) sys.exit(-1)
from messages import infoMessage, errorMessage from messages import infoMessage, errorMessage