def cmd_write(self, address, value): al = (address & 0x00FF) >> 0 ah = (address & 0xFF00) >> 8 self.serial.tx('F' + chr(al) + chr(ah) + chr(value) + '\n') r = self.serial.rx(1) cout("Write 0x%02X at 0x%04X: %s (%s)\n" % (value, address, r, s2hex(r)))
def run(self): # prompt = self.connect( self.sync_5_1 ) try: # # Get all registers content # for r in nrf_registers: self.get_prompt() rn, stx, nrx = r ntx = len(stx) ra = ord(stx[0]) cout("Register %-10s: 0x%02X = " % (rn, ra)) flushout() self.serial.tx('=' + chr(ntx) + chr(nrx) + stx) r = self.serial.rx(nrx + 1) if r: if r.endswith(self.prompt): cout(s2hex(r[:-1]) + '\n') else: cerr(" received `%s`\n" % r) else: cerr("no response.\n") break except KeyboardInterrupt: cout("\n") finally: self.serial.close()
def run(self): # prompt = self.connect( self.sync_5_1 ) try: # # Get all registers content # for r in nrf_registers: self.get_prompt() rn, stx, nrx = r ntx = len(stx) ra = ord(stx[0]) cout("Register %-10s: 0x%02X = " % (rn,ra)) flushout() self.serial.tx('='+chr(ntx)+chr(nrx)+stx) r = self.serial.rx(nrx+1) if r: if r.endswith(self.prompt): cout( s2hex(r[:-1])+'\n' ) else: cerr(" received `%s`\n" % r) else: cerr("no response.\n") break except KeyboardInterrupt: cout("\n") finally: self.serial.close()
def identify(self): """ Identify the device. """ r = self.execute("i", 1000) #trace("i: %s\n" % s2hex(r)) self.protocol = ord(r[0]) if self.protocol != 4 and self.protocol != 5: die(_('protocol #%d not supported' % self.protocol)) signature = s2hex(r[1:4]).replace(' ', '') r = r[4:] try: device = devices[signature]() except KeyError: die(_("Unknown device with signature %s" % signature)) device.link = self.link device.protocol = self.protocol device.blpages = struct.unpack('B', r[0])[0] r = r[1:] device.bladdr = device.flashsize - device.blpages * device.pagesize device.appcrc = struct.unpack('>H', r[0:2])[0] r = r[2:] device.fuses = r[0:4] r = r[4:] device.eeappcrc = device.read_eeprom_appcrc() if device.eeappcrc is None: raise Exception(_('could not get EEPROM application CRC')) device.pgmcount = device.read_eeprom_pgmcount() if device.pgmcount is None: raise Exception(_('could not get EEPROM programmings count')) if device.pgmcount == 0xFFFFFFFF: device.pgmcount = 0 return device
def eeread(self, address, n): cout("Reading %d bytes of EEPROM starting at 0x%04X: " % (n,address)) mem = "" a = address while n: al = (a & 0x00FF) >> 0 ah = (a & 0xFF00) >> 8 l = min(n, 64) cout('.') flushout() r = self.serial.command('e'+chr(al)+chr(ah)+chr(l)+'\n', l+1, timeout=0.1) if len(r) == l+1 and r[-1]=='$': mem += r[:-1] n -= l a += l else: cout(" Error reading %d bytes of EEPROM starting at 0x%04X.\n" % (l,a)) cout(" Received %d bytes: %s\n" % (len(r),s2hex(r))) break cout('\n') cout(hexdump(address, mem)+'\n')
def identify(self): """ Identify the device. """ r = self.execute("i", 1000) #trace("i: %s\n" % s2hex(r)) self.protocol = ord(r[0]) if self.protocol != 4 and self.protocol != 5: die(_('protocol #%d not supported' % self.protocol)) signature=s2hex(r[1:4]).replace(' ','') ; r = r[4:] try: device = devices[signature]() except KeyError: die(_("Unknown device with signature %s" % signature)) device.link = self.link device.protocol = self.protocol device.blpages = struct.unpack('B', r[0])[0] ; r = r[1:] device.bladdr = device.flashsize-device.blpages*device.pagesize device.appcrc = struct.unpack('>H', r[0:2])[0] ; r = r[2:] device.fuses = r[0:4] ; r = r[4:] device.eeappcrc = device.read_eeprom_appcrc() if device.eeappcrc is None: raise Exception(_('could not get EEPROM application CRC')) device.pgmcount = device.read_eeprom_pgmcount() if device.pgmcount is None: raise Exception(_('could not get EEPROM programmings count')) if device.pgmcount == 0xFFFFFFFF: device.pgmcount = 0 return device
def eewrite(self, address, value): al = (address & 0x00FF) >> 0 ah = (address & 0xFF00) >> 8 r = self.serial.command('E'+chr(al)+chr(ah)+chr(value)+'\n', 1, timeout=0.1) cout("Write 0x%02X at 0x%04X: %s (%s)\n" % (address, value, r, s2hex(r)))
xserial.add_arguments(parser) options = parser.parse_args() if options.wires == 0: options.wires = 2 serial = xserial.get_serial(options) # resets the device prompt = serial.rx(1) if prompt == '$': # # Get all registers content # for r in nrf_registers: rn, stx, nrx = r ntx = len(stx) ra = ord(stx[0]) cout("Register %-10s: 0x%02X = " % (rn, ra)) flushout() serial.tx('=' + chr(ntx) + chr(nrx) + stx) r = serial.rx(nrx + 1) if r: if r.endswith(prompt): cout(s2hex(r[:-1]) + '\n') else: die(" received `%s`\n" % s2hex(r)) else: cout("no response.\n") else: cout("Unexpected prompt: %s\n" % s2hex(prompt)) serial.close()
def execute ( self, cmdstr, rlen, repeats=4, **kwargs ): """ Send a command, wait for rlen reply bytes. If rlen > 2, check the CRC of the reply. Return the reply without the CRC nor the prompt. A void reply for the 'write_flash_page' command can mean that the inter-char delay is not sufficient and the device has not enough time to compute the CRC. Repeated CRC errors can mean that the device estimated the baudrate not accurately enough. """ dbg("EXECUTE(%s,%d,%d,%s)\n" % (s2hex(cmdstr), rlen, repeats, repr(kwargs))) built = None crcerrors = 0 for repeat in range(repeats): # if repeat>0: # trace() self.resume() self.link.lastchar = "" dbg("EXECUTE: tx(%s) -> " % (s2hex(cmdstr))) self.link.tx(cmdstr) self.ncmds += 1 if rlen == 0: return None if kwargs.has_key('timeout'): # # Wait for a reply until timeout # r="" t=timer()+kwargs['timeout'] while len(r) < rlen and timer() < t: c=self.link.rx(1) if c: t=timer()+kwargs['timeout'] r += c # if r!= "": # break # if r=="": # for i in range(256): # self.com.tx('\n') # c = self.com.rx(1) # if c=='!': # dbg("Sent %d \\n\n" % (i+1)) # break # # raise Exception("\nCOMMAND FAIL: [%s] -> [%s]\n" % (s2hex(cmdstr), s2hex(r))) # else: # # r += self.com.rx(rlen-1) # prev=timer() # while len(r)<rlen and timer() < t: # r += self.com.rx(1) # last=timer() # if last-prev > 0.001: # trace() # prev=last else: r = self.link.rx(rlen) if r: dbg("%s\n" % s2hex(r)) else: dbg("no reply\n") if self.link.lastchar != '#': dbg(" COMMAND FAILED\n") self.ncmdfails += 1 continue r = r[:-1] if rlen <= 2: return r else: crc = CRC.check(cmdstr+r) if crc == 0: if built: dbg("\nBUILT REPLY: %s:" % s2hex(built)) for i in range(rlen-1): if built[i] != r[i]: dbg(" #%d" % i) dbg("\nGOOD REPLY: %s\n\n" % s2hex(r)) return r[:-2] dbg(" CRC ERROR: 0x%04X\n" % crc) # # CRC error, try to correct it. # # Bytes are transfered least significant bit # first. Inaccurate synchronization of the device can lead # to the last transmitted bit (msb) of bytes to be # defective: # # * If the device estimated the baudrate a little too # high, the host can sample the stop bit (1) instead of # the msb. This leads to an error only if the original # msb is 0, then it is corrected by setting the msb back # to 0. Only bytes whose received msb is high are # concerned. # # * If the device estimated the baudrate a little too low, # the host can sample bit b6 instead of the msb. This # leads to an error only if the original b6 and b7 (msb) # are different, then it is corrected by setting b7 to # the opposite of b6. # crcerrors += 1 self.ncrcerrors += 1 if len(r) != rlen-1: # dbg(" BAD REPLY #%d: %s\n" % (repeat,s2hex(r))) continue if not built: # # Display a rule to make finding bytes easier # dbg("\n ") for i in range(rlen-1): dbg(" %02d" % i) dbg(" BAD REPLY #%d: %s\n" % (repeat,s2hex(r))) updated = False if not built: built = r updated = True else: # # Try to build the correct frame with bytes whose b7,b6 # bits are different # positions=[] for p in range(rlen-1): if ( (ord(built[p]) & 0xC0) == 0xC0 or (ord(built[p]) & 0xC0) == 0x00 )\ and ord(r[p]) != ord(built[p]): positions.append(p) built = built[:p] + r[p] + built[p+1:] dbg(" #%d" % p) updated = True if updated: # dbg("\nUPDATED BYTES:") # for p in positions: # dbg(" #%d" % p) if CRC.check(cmdstr+built) == 0: dbg("\nFRAME FULLY CORRECTED\n\n") return built[:-2] dbg("\n") # Device a little too low? # Try setting b7 to the opposite of b6 # if updated: for p in range(rlen-1): old = ord(built[p]) if (old & 0xC0) == 0x00 or (old & 0xC0) == 0xC0: new = old ^ 0x80 x = built[:p] + chr(new) + built[p+1:] crc = CRC.check(cmdstr+x) if crc == 0: dbg("\nbyte #%d has been corrected " "from %02X to %02X\n" % (p,old,new)) return x[:-2] # s = "\nCOMMAND [%s] FAILED." % s2hex(cmdstr) if len(cmdstr) > 8: s = "\nCOMMAND [%s ... %s] (%d bytes) FAILED." \ % (s2hex(cmdstr[:4]), s2hex(cmdstr[-2:]), len(cmdstr)) else: s = "\nCOMMAND [%s] FAILED." % s2hex(cmdstr) if crcerrors > 3: s += _("\nMany CRC unrecoverable errors detected. Your baudrate setting (%d) "\ "may be too high for the device.\n" % self.link.serial.baudrate) die(s)
options = parser.parse_args() if options.wires == 0: options.wires = 2 serial = xserial.get_serial(options) # resets the device prompt = serial.rx(1) if prompt == '$': # # Get all registers content # for r in nrf_registers: rn, stx, nrx = r ntx = len(stx) ra = ord(stx[0]) cout("Register %-10s: 0x%02X = " % (rn,ra)) flushout() serial.tx('='+chr(ntx)+chr(nrx)+stx) r = serial.rx(nrx+1) if r: if r.endswith(prompt): cout( s2hex(r[:-1])+'\n' ) else: die(" received `%s`\n" % s2hex(r)) else: cout("no response.\n") else: cout("Unexpected prompt: %s\n" % s2hex(prompt)) serial.close()
def execute(self, cmdstr, rlen, repeats=4, **kwargs): """ Send a command, wait for rlen reply bytes. If rlen > 2, check the CRC of the reply. Return the reply without the CRC nor the prompt. A void reply for the 'write_flash_page' command can mean that the inter-char delay is not sufficient and the device has not enough time to compute the CRC. Repeated CRC errors can mean that the device estimated the baudrate not accurately enough. """ dbg("EXECUTE(%s,%d,%d,%s)\n" % (s2hex(cmdstr), rlen, repeats, repr(kwargs))) built = None crcerrors = 0 for repeat in range(repeats): # if repeat>0: # trace() self.resume() self.link.lastchar = "" dbg("EXECUTE: tx(%s) -> " % (s2hex(cmdstr))) self.link.tx(cmdstr) self.ncmds += 1 if rlen == 0: return None if kwargs.has_key('timeout'): # # Wait for a reply until timeout # r = "" t = timer() + kwargs['timeout'] while len(r) < rlen and timer() < t: c = self.link.rx(1) if c: t = timer() + kwargs['timeout'] r += c # if r!= "": # break # if r=="": # for i in range(256): # self.com.tx('\n') # c = self.com.rx(1) # if c=='!': # dbg("Sent %d \\n\n" % (i+1)) # break # # raise Exception("\nCOMMAND FAIL: [%s] -> [%s]\n" % (s2hex(cmdstr), s2hex(r))) # else: # # r += self.com.rx(rlen-1) # prev=timer() # while len(r)<rlen and timer() < t: # r += self.com.rx(1) # last=timer() # if last-prev > 0.001: # trace() # prev=last else: r = self.link.rx(rlen) if r: dbg("%s\n" % s2hex(r)) else: dbg("no reply\n") if self.link.lastchar != '#': dbg(" COMMAND FAILED\n") self.ncmdfails += 1 continue r = r[:-1] if rlen <= 2: return r else: crc = CRC.check(cmdstr + r) if crc == 0: if built: dbg("\nBUILT REPLY: %s:" % s2hex(built)) for i in range(rlen - 1): if built[i] != r[i]: dbg(" #%d" % i) dbg("\nGOOD REPLY: %s\n\n" % s2hex(r)) return r[:-2] dbg(" CRC ERROR: 0x%04X\n" % crc) # # CRC error, try to correct it. # # Bytes are transfered least significant bit # first. Inaccurate synchronization of the device can lead # to the last transmitted bit (msb) of bytes to be # defective: # # * If the device estimated the baudrate a little too # high, the host can sample the stop bit (1) instead of # the msb. This leads to an error only if the original # msb is 0, then it is corrected by setting the msb back # to 0. Only bytes whose received msb is high are # concerned. # # * If the device estimated the baudrate a little too low, # the host can sample bit b6 instead of the msb. This # leads to an error only if the original b6 and b7 (msb) # are different, then it is corrected by setting b7 to # the opposite of b6. # crcerrors += 1 self.ncrcerrors += 1 if len(r) != rlen - 1: # dbg(" BAD REPLY #%d: %s\n" % (repeat,s2hex(r))) continue if not built: # # Display a rule to make finding bytes easier # dbg("\n ") for i in range(rlen - 1): dbg(" %02d" % i) dbg(" BAD REPLY #%d: %s\n" % (repeat, s2hex(r))) updated = False if not built: built = r updated = True else: # # Try to build the correct frame with bytes whose b7,b6 # bits are different # positions = [] for p in range(rlen - 1): if ( (ord(built[p]) & 0xC0) == 0xC0 or (ord(built[p]) & 0xC0) == 0x00 )\ and ord(r[p]) != ord(built[p]): positions.append(p) built = built[:p] + r[p] + built[p + 1:] dbg(" #%d" % p) updated = True if updated: # dbg("\nUPDATED BYTES:") # for p in positions: # dbg(" #%d" % p) if CRC.check(cmdstr + built) == 0: dbg("\nFRAME FULLY CORRECTED\n\n") return built[:-2] dbg("\n") # Device a little too low? # Try setting b7 to the opposite of b6 # if updated: for p in range(rlen - 1): old = ord(built[p]) if (old & 0xC0) == 0x00 or (old & 0xC0) == 0xC0: new = old ^ 0x80 x = built[:p] + chr(new) + built[p + 1:] crc = CRC.check(cmdstr + x) if crc == 0: dbg("\nbyte #%d has been corrected " "from %02X to %02X\n" % (p, old, new)) return x[:-2] # s = "\nCOMMAND [%s] FAILED." % s2hex(cmdstr) if len(cmdstr) > 8: s = "\nCOMMAND [%s ... %s] (%d bytes) FAILED." \ % (s2hex(cmdstr[:4]), s2hex(cmdstr[-2:]), len(cmdstr)) else: s = "\nCOMMAND [%s] FAILED." % s2hex(cmdstr) if crcerrors > 3: s += _("\nMany CRC unrecoverable errors detected. Your baudrate setting (%d) "\ "may be too high for the device.\n" % self.link.serial.baudrate) die(s)
n = 20 while True: if n == 20: n = 0 resyncs += 1 serial.tx('x') # Force resync try: cout("Resync #%d: " % resyncs) serial.sync_5_1() except: die(_("Could not get the application prompt.\n")) continue serial.tx('A') r = serial.rx(5) if len(r) != 5 or r[-1] != '$': cout("ERROR: %d bytes received: %s\n" % (len(r), s2hex(r))) serial.rx(10) else: n += 1 dtn, dt0 = struct.unpack('<HH', r[0:4]) cout("dt0=%d dtn=%d\n" % (dt0, dtn)) tick += 0.05 if tick > time.time(): time.sleep(tick - time.time()) except KeyboardInterrupt: pass serial.close()
def cmd_write(self, address, value): al = (address & 0x00FF) >> 0 ah = (address & 0xFF00) >> 8 self.serial.tx('F'+chr(al)+chr(ah)+chr(value)+'\n') r = self.serial.rx(1) cout("Write 0x%02X at 0x%04X: %s (%s)\n" % (value, address, r, s2hex(r)))