changeset 113:1f40be088ee8

Added first start stlink
author Windel Bouwman
date Sat, 05 Jan 2013 00:06:27 +0100
parents 056face59ee7
children f42268da614f
files python/st-flash.py python/stlink.py python/usb.py
diffstat 3 files changed, 105 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/st-flash.py	Sat Jan 05 00:06:27 2013 +0100
@@ -0,0 +1,22 @@
+#!/usr/bin/python
+
+import argparse
+from stlink import STLink
+
+def hex2int(s):
+   return int(s, 16)
+
+parser = argparse.ArgumentParser(description='ST-link flash utility')
+#parser.add_argument('filename', type=str)
+#parser.add_argument('address', type=hex2int)
+
+args = parser.parse_args()
+print(args)
+
+stl = STLink()
+print(stl)
+stl.open()
+if stl.CurrentMode == STLink.DFU_MODE:
+   pass
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/stlink.py	Sat Jan 05 00:06:27 2013 +0100
@@ -0,0 +1,44 @@
+from usb import UsbContext
+
+class STLinkException(Exception):
+   pass
+
+def checkDevice(device):
+   ST_VID=0x0483
+   STLINK2_PID=0x3748
+   return device.VendorId == ST_VID and device.ProductId == STLINK2_PID
+
+XFER_TO_DEV=0
+XFER_FROM_DEV=0x80
+
+class STLink(object):
+   DFU_MODE = 222
+   def __init__(self):
+      self.context = UsbContext()
+   def open(self):
+      context = UsbContext()
+      stlink2s = list(filter(checkDevice, context.DeviceList))
+      if not stlink2s:
+         raise STLinkException('Could not find an ST link')
+      if len(stlink2s) > 1:
+         print('More then one stlink2 found, picking first one')
+      stlink2 = stlink2s[0]
+      dev = stlink2.open()
+      if dev.Configuration != 1:
+         dev.Configuration = 1
+      dev.claimInterface(0)
+   def getCurrentMode(self):
+      print('get cur mode')
+      rep_len = 2
+      self.fillCommand(self, XFER_FROM_DEV, rep_len)
+      size = self.send_recv(1, cmd, data)
+      return self.q_buf[0]
+   CurrentMode = property(getCurrentMode)
+   def fillCommand(self, di, rl):
+      bytes(b'USBC')
+      pass
+   def reset(self):
+      pass
+   def send_recv(self, txbuf, rxbuf):
+      pass
+
--- a/python/usb.py	Fri Jan 04 18:40:05 2013 +0100
+++ b/python/usb.py	Sat Jan 05 00:06:27 2013 +0100
@@ -10,6 +10,7 @@
    f = getattr(libusb, name)
    f.argtypes = argtypes
    f.restype = restype
+   globals()[name] = f
    return f
 def enum(**enums):
    reverse = dict((value, key) for key, value in enums.items())
@@ -44,6 +45,11 @@
 libusb_device_p_p = POINTER(libusb_device_p)
 libusb_device_p_p_p = POINTER(libusb_device_p_p)
 
+class libusb_device_handle(Structure):
+   pass
+libusb_device_handle_p = POINTER(libusb_device_handle)
+libusb_device_handle_p_p = POINTER(libusb_device_handle_p)
+
 class libusb_device_descriptor(Structure):
    _fields_ = [
                ('bLength', c_uint8),
@@ -64,25 +70,25 @@
 libusb_device_descriptor_p = POINTER(libusb_device_descriptor)
 
 # functions
-libusb_init = buildfunc('libusb_init', [libusb_context_p_p], c_int)
+buildfunc('libusb_init', [libusb_context_p_p], c_int)
 
-libusb_get_device_list = buildfunc('libusb_get_device_list', \
+buildfunc('libusb_get_device_list', \
    [libusb_context_p, libusb_device_p_p_p], c_ssize_t)
-libusb_free_device_list = buildfunc('libusb_free_device_list',\
-   [libusb_device_p_p, c_int], None)
-libusb_get_bus_number = buildfunc('libusb_get_bus_number', \
-   [libusb_device_p], c_uint8)
-libusb_get_device_address = buildfunc('libusb_get_device_address', \
-   [libusb_device_p], c_uint8)
-libusb_get_device_speed = buildfunc('libusb_get_device_speed', \
-   [libusb_device_p], c_int)
-libusb_unref_device = buildfunc('libusb_unref_device', \
-   [libusb_device_p], None)
+buildfunc('libusb_free_device_list', [libusb_device_p_p, c_int], None)
+buildfunc('libusb_get_bus_number', [libusb_device_p], c_uint8)
+buildfunc('libusb_get_device_address', [libusb_device_p], c_uint8)
+buildfunc('libusb_get_device_speed', [libusb_device_p], c_int)
+buildfunc('libusb_unref_device', [libusb_device_p], None)
+buildfunc('libusb_open', [libusb_device_p, libusb_device_handle_p_p], c_int)
+buildfunc('libusb_close', [libusb_device_handle_p], None)
+buildfunc('libusb_get_configuration', \
+   [libusb_device_handle_p, POINTER(c_int)], c_int)
+buildfunc('libusb_set_configuration', [libusb_device_handle_p, c_int], c_int)
+buildfunc('libusb_claim_interface', [libusb_device_handle_p, c_int], c_int)
 
-libusb_get_device_descriptor = buildfunc('libusb_get_device_descriptor', [libusb_device_p, libusb_device_descriptor_p], c_int)
 
-# Quick hack function:
-#libusb_open_device_with_vid_pid = buildfunc('libusb_open_device_with_vid_pid', [libusb_context_p, ctypes.c_uint16, ctypes.c_uint16], libusb_device_handle_p)
+buildfunc('libusb_get_device_descriptor',\
+   [libusb_device_p, libusb_device_descriptor_p], c_int)
 
 # pythonic API:
 
@@ -145,9 +151,12 @@
       r = libusb_open(self.dev_p, byref(handle_p))
       if r != 0:
          raise UsbError('error opening device', r)
-      return UseDeviceHandle(self, handle_p)
+      return UsbDeviceHandle(self, handle_p)
    def __repr__(self):
-      r2 = 'Usb device: bus {0} address {1} {2:04X}:{3:04X}'.format(self.BusNumber, self.DeviceAddress, self.VendorId, self.ProductId)
+      r2 = 'Usb device: bus {0} address {1} {2:04X}:{3:04X} speed {4}' \
+         .format( \
+         self.BusNumber, self.DeviceAddress, self.VendorId, \
+         self.ProductId, self.Speed)
       return r2
 
 class UsbDeviceHandle:
@@ -155,6 +164,19 @@
    def __init__(self, device, handle_p):
       self.device = device
       self.handle_p = handle_p
+   def getConfiguration(self):
+      config = c_int()
+      r = libusb_get_configuration(self.handle_p, byref(config))
+      if r != 0: raise UsbError('Error getting configuration', r)
+      return config.value
+   def setConfiguration(self, config):
+      r = libusb_set_configuration(self.handle_p, config)
+      if r != 0: raise UsbError('Error setting configuration', r)
+   Configuration = property(getConfiguration, setConfiguration)
+   def claimInterface(self, interface_number):
+      r = libusb_claim_interface(self.handle_p, interface_number)
+      if r != 0: raise UsbError('Error claiming interface', r)
+
 
 class UsbTransfer:
    def __init__(self):