def tt_write_file(p, fileno, buf, expect_end=True, debug=False): # strange ordering: file 0x001234ab (ttwatch naming) becomes 0012ab34 assert (fileno >> 24) == 0 cmdno = bytearray( (0, (fileno >> 16) & 0xff, fileno & 0xff, (fileno >> 8) & 0xff)) tt_send_command(p, cmdno) l = len(buf) p.wr(0x28, struct.pack('<L', len(buf))) # 0x28 = length in/out counter = 0 startat = time.time() checker = tt_crc16_streamer() for ii in range(0, l, 256 * 20 - 2): # write up to 256*20-2 data bytes in 20B chunks end = min(l, ii + 256 * 20 - 2) for jj in range(ii, end, 20): out = buf[jj:min(jj + 20, end)] checker.update(out) if jj + 20 >= end: out += struct.pack('<H', checker.digest()) p.wr(0x2b, out[:20]) if len(out) > 20: p.wr(0x2b, out[20:]) if debug > 1: print "%04x: %s %s" % (jj, hexlify(out), repr(out)) # check CRC16 and ack checker.reset() counter += 1 if end < l or expect_end: rda(p, 0x2e, idata=counter, timeout=20) if debug: print "%d: wrote %d/%d bytes so far (%d/sec)" % ( counter, end, l, end // (time.time() - startat)) rda(p, 0x25, idata=0) return end
def tt_read_file(p, fileno, outf, limit=None, debug=False): # strange ordering: file 0x001234ab (ttwatch naming) becomes 0012ab34 assert (fileno >> 24) == 0 cmdno = bytearray( (1, (fileno >> 16) & 0xff, fileno & 0xff, (fileno >> 8) & 0xff)) tt_send_command(p, cmdno) l = rda(p, 0x28).idata # 0x28 = length in/out counter = 0 startat = time.time() checker = tt_crc16_streamer() for ii in range(0, l, 256 * 20 - 2): # read up to 256*20-2 data bytes in 20B chunks end = min(l, ii + 256 * 20 - 2) for jj in range(ii, end, 20): d = rda(p, 0x2b).data if (end - jj - len(d)) in (-1, 0): d += rda(p, 0x2b).data # tack on CRC16 straggler byte(s) outf.write(d[:min(end - jj, 20)]) checker.update(d) if debug > 1: print "%04x: %s %s" % (jj, hexlify(d), repr(d)) # check CRC16 and ack if checker.digest() != 0: raise AssertionError, checker.hexdigest() checker.reset() counter += 1 p.wr(0x2e, struct.pack('<L', counter), False) if debug: print "%d: read %d/%d bytes so far (%d/sec)" % ( counter, end, l, end // (time.time() - startat)) rda(p, 0x25, idata=0) return end
def tt_write_file(p, fileno, buf, expect_end=True, debug=False): # strange ordering: file 0x001234ab (ttwatch naming) becomes 0012ab34 assert (fileno>>24)==0 cmdno = bytearray((0, (fileno>>16)&0xff, fileno&0xff, (fileno>>8)&0xff)) tt_send_command(p, cmdno) l = len(buf) p.wr(0x28, struct.pack('<L', len(buf))) # 0x28 = length in/out counter = 0 startat = time.time() checker = tt_crc16_streamer() for ii in range(0, l, 256*20-2): # write up to 256*20-2 data bytes in 20B chunks end = min(l, ii+256*20-2) for jj in range(ii, end, 20): out = buf[jj : min(jj+20, end)] checker.update(out) if jj+20>=end: out += struct.pack('<H', checker.digest()) p.wr(0x2b, out[:20]) if len(out)>20: p.wr(0x2b, out[20:]) if debug>1: print "%04x: %s %s" % (jj, hexlify(out), repr(out)) # check CRC16 and ack checker.reset() counter += 1 if end<l or expect_end: rda(p, 0x2e, idata=counter, timeout=20) if debug: print "%d: wrote %d/%d bytes so far (%d/sec)" % (counter, end, l, end // (time.time()-startat)) rda(p, 0x25, idata=0) return end
def tt_read_file(p, fileno, outf, limit=None, debug=False): # strange ordering: file 0x001234ab (ttwatch naming) becomes 0012ab34 assert (fileno>>24)==0 cmdno = bytearray((1, (fileno>>16)&0xff, fileno&0xff, (fileno>>8)&0xff)) tt_send_command(p, cmdno) l = rda(p, 0x28).idata # 0x28 = length in/out counter = 0 startat = time.time() checker = tt_crc16_streamer() for ii in range(0, l, 256*20-2): # read up to 256*20-2 data bytes in 20B chunks end = min(l, ii+256*20-2) for jj in range(ii, end, 20): d = rda(p, 0x2b).data if (end-jj-len(d)) in (-1, 0): d += rda(p, 0x2b).data # tack on CRC16 straggler byte(s) outf.write( d[ : min(end-jj,20) ] ) checker.update(d) if debug>1: print "%04x: %s %s" % (jj, hexlify(d), repr(d)) # check CRC16 and ack if checker.digest()!=0: raise AssertionError, checker.hexdigest() checker.reset() counter += 1 p.wr(0x2e, struct.pack('<L', counter), False) if debug: print "%d: read %d/%d bytes so far (%d/sec)" % (counter, end, l, end // (time.time()-startat)) rda(p, 0x25, idata=0) return end