Mercurial > avr_jtag
diff py_avrjtag/cmd_proto.py @ 4:6b1594fb668f
Improve performance of jtag.c and test it with Python scripts.
- cp_tditdo.py send bits to TDI and hope it will feed back from TDO.
- Expand loops in jtag.c to improve performance.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sun, 22 Feb 2009 12:54:45 +0800 |
parents | |
children | 61f27549de57 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/py_avrjtag/cmd_proto.py Sun Feb 22 12:54:45 2009 +0800 @@ -0,0 +1,67 @@ +import struct + +CPCMD_PING = 1 +CPCMD_SHIFT_TDI = 2 +CPCMD_SHIFT_TMS = 3 +CPCMD_SHIFT_TDI_TDO = 4 +CPCMD_PONG = 5 +CPCMD_DATA = 6 +CPCMD_ACK = 7 +CPCMD_NAK = 8 + +def csum_add(csum, c): + return (((csum << 3) | (csum >> 5)) ^ c) & 0xff + +class cmd(object): + OFF_SEQ = 2 + OFF_DSZ = 3 + OFF_CODE = 4 + FRAME_OVERHEAD = 5 + + def __init__(self, seq=0, code=0, data=''): + self.seq = seq + self.code = code + self.data = data + pass + + def to_frame(self): + csum = 0 + csum = csum_add(0, self.code) + for c in self.data: + csum = csum_add(csum, ord(c)) + pass + csum = csum_add(csum, 0) + + frame = struct.pack('BBBBB', ord('J'), ord('C'), self.seq, + len(self.data) + 1, self.code) + frame = frame + self.data + chr(csum) + return frame + + def from_frame(self, frame): + if frame[:2] != 'JC': + return -1 + + csum = 0 + for c in frame[self.OFF_CODE:]: + csum = csum_add(csum, ord(c)) + pass + if csum: + return -1 + + data_sz = ord(frame[self.OFF_DSZ]) + if data_sz != (len(frame) - self.FRAME_OVERHEAD): + return -1 + + if not data_sz: + return -1 + + self.seq = ord(frame[self.OFF_SEQ]) + self.code = ord(frame[self.OFF_CODE]) + self.data = frame[self.OFF_CODE + 1:-1] + pass + + def __repr__(self): + return '<%s {seq: %d, code: %d, data: %s}>' % \ + (self.__class__.__name__, self.seq, self.code, repr(self.data)) + pass +