view python/stlink.py @ 113:1f40be088ee8

Added first start stlink
author Windel Bouwman
date Sat, 05 Jan 2013 00:06:27 +0100
parents
children f42268da614f
line wrap: on
line source

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