Package icaro-bloques :: Module docker
[hide private]
[frames] | no frames]

Source Code for Module icaro-bloques.docker

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  #  docker.py 
  5  #   
  6  #  Copyright 2012 Valentin Basel <valentinbasel@gmail.com> 
  7  #   
  8  #  This program is free software; you can redistribute it and/or modify 
  9  #  it under the terms of the GNU General Public License as published by 
 10  #  the Free Software Foundation; either version 2 of the License, or 
 11  #  (at your option) any later version. 
 12  #   
 13  #  This program is distributed in the hope that it will be useful, 
 14  #  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16  #  GNU General Public License for more details. 
 17  #   
 18  #  You should have received a copy of the GNU General Public License 
 19  #  along with this program; if not, write to the Free Software 
 20  #  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 21  #  MA 02110-1301, USA. 
 22  #   
 23  #   
 24  # tomado del proyecto de Jean-Pierre MANDON <jp.mandon@gmail.com>  para un docker en python 
 25  #proyecto original en : 
 26  # http://code.google.com/p/vascodownloader/ 
 27  # based on the DOCKER software licensed by Pierre Gaufillet <pierre.gaufillet@magic.fr>  
 28  # original project hosted on gforge 
 29  # http://gforge.enseeiht.fr/projects/vasco/ 
 30   
 31   
 32  import sys 
 33  import os 
 34  import usb 
 35  import time 
 36   
 37  # USB endpoint 
 38   
 39  BULK_IN_EP = 0x82 
 40  BULK_OUT_EP = 0x01 
 41   
 42  # comandos para el Bootloader  
 43  ERASE_FLASH_CMD=0 
 44  WRITE_FLASH_CMD=1 
 45  LEER_FLASH_CMD=2 
 46  VALID_APPLICATION_CMD=3 
 47  RESET_CMD=4 
 48  SECTION_DESCRIPTOR_CMD=5 
 49   
 50  vendor=int("0x04D8",16) 
 51  product=int("0xFEAA",16) 
52 -def borrar(adresse,manejador):
53 leerbyte0=ERASE_FLASH_CMD 54 adresse="%06X"%adresse 55 leerbyte1=int(adresse[4:6],16) 56 leerbyte2=int(adresse[2:4],16) 57 leerbyte3=int(adresse[0:2],16) 58 manejador.bulkWrite(BULK_OUT_EP,chr(leerbyte0)+chr(leerbyte1)+chr(leerbyte2)+chr(leerbyte3),200)
59
60 -def CargaArchivo(file,manejador):
61 data=[] 62 oldadd=0 63 maxadd=0 64 fichero=open(file,'r') 65 lines=fichero.readlines() 66 fichero.close() 67 for line in lines: 68 nb=int(line[1:3],16) 69 add=int(line[3:7],16) 70 if (add>oldadd) and (add<0x8000): 71 maxadd=add+nb 72 oldadd=add 73 maxadd=maxadd+64-(maxadd%64) 74 for i in range(maxadd): 75 data.append(0xFF) 76 for line in lines: 77 nb=int(line[1:3],16) 78 add=int(line[3:7],16) 79 if add>=0x2000 and add<0x8000: 80 for i in range(0,nb): 81 data[add+i]=int(line[9+(2*i):11+(2*i)],16) 82 index=0 83 frame=[] 84 for i in range(0x2000,maxadd): 85 if i%64==0: 86 borrar(i,manejador) 87 if i%32==0: 88 index=0 89 if frame!=[]: 90 EscribeUSB(i-32,frame,manejador) 91 frame=[] 92 if data[i]!=[]: 93 frame.append(data[i]) 94 index+=1 95 print "el archivo fue cargado con exito"
96 -def EscribeUSB(adresse,bloc,manejador):
97 leerbyte0=WRITE_FLASH_CMD 98 adresse="%06X"%adresse 99 leerbyte1=int(adresse[4:6],16) 100 leerbyte2=int(adresse[2:4],16) 101 leerbyte3=int(adresse[0:2],16) 102 frame=chr(leerbyte0)+chr(leerbyte1)+chr(leerbyte2)+chr(leerbyte3) 103 for i in range(32): 104 frame=frame+chr(bloc[i]) 105 manejador.bulkWrite(BULK_OUT_EP,frame,200)
106
107 -def docker(archivo):
108 np05=None 109 buses=usb.busses() 110 for bus in buses: 111 for device in bus.devices: 112 if device.idVendor==vendor and device.idProduct==product: 113 np05= device 114 if np05==None: 115 print "no esta el pic" 116 return 1 117 try: 118 manejador=np05.open() 119 manejador.setConfiguration(2) 120 manejador.claimInterface(0) 121 except Exception, ex: 122 print "error al leer el pic" 123 return 2 124 try: 125 126 CargaArchivo(archivo,manejador) 127 manejador.releaseInterface() 128 except Exception, ex: 129 return 3 130 131 return 0
132