#!/usr/bin/env python # coding: utf-8 # copyright Michael Weber 2008 avr at xmw.de # released with GNU GPL # build upon the docs at http://www.myavr.de/download/techb_mysmartusb.pdf # tested with mySmartUSB hardware version 2 firmware 2.5 import sys, tty import serial, os, os.path CP2101_DEVICES = filter(lambda fn: fn.startswith('ttyUSB'), os.listdir('/sys/bus/usb-serial/drivers/cp210x/')) if len(CP2101_DEVICES) > 1: CP2101_DEVICES.sort() print "interface auto detection got devices", CP2101_DEVICES print "using", CP2101_DEVICES[0] LINKNAME = os.path.join('/dev', CP2101_DEVICES[0]) class mySmartUsb: MAGIC = ''.join(map(chr, (0xE6, 0xB5, 0xBA, 0xB9, 0xB2, 0xB3, 0xA9))) def __init__(self, linkname): self.link = serial.serial(linkname, tty.B19200) def write(self, cmd): """write MAGIC with given cmd to the unit""" self.link.write(self.MAGIC + cmd) def read(self): """read up to 100 bytes from the unit, timeout 10ms""" return self.link.read(100, timeout=0.01) def action(self, cmd): """combined write and read action""" self.write(cmd) return self.read() def resetBoard(self): """reset the board""" return self.action('r') def resetProgrammer(self): """reset the programmer""" return self.action('R') def setBoardPowerOn(self): """set board power to on""" return self.action('+') def setBoardPowerOff(self): """set board power to off""" return self.action('-') def setModeProgram(self): """set mode to program""" return self.action('p') def setModeData(self): """set mode to data""" return self.action('d') def setModeQuiet(self): """set mode to quiet""" return self.action('q') def getStatus(self): """get the programmer status""" return self.action('i') if __name__ == '__main__': import sys, os.path modes = [] modes.append(('-mp', mySmartUsb.setModeProgram)) modes.append(('-md', mySmartUsb.setModeData)) modes.append(('-mq', mySmartUsb.setModeQuiet)) modes.append(('-pon', mySmartUsb.setBoardPowerOn)) modes.append(('-poff', mySmartUsb.setBoardPowerOff)) modes.append(('-r', mySmartUsb.resetBoard)) modes.append(('-R', mySmartUsb.resetProgrammer)) modes.append(('-i', mySmartUsb.getStatus)) def usage(): #print 'usage:', sys.argv[0], '', '