2 import em260_data as em
4 ##########################################
5 # Name: em260_spi_disass.py
7 # Usage: em260_spi_disass.py <file>
9 # Notes: This tool will parse SPI data from a Beagle
10 # dump file taken while monitoring the EM250/EM260.
11 # You can pass it either MOSI, MISO,
12 # or Bidirectional data. This will output to
14 ##########################################
16 #debug = 0 # 0 == off, 1 == on
17 debug = 1 # 0 == off, 1 == on
19 def process_data(ascii_data):
22 while byte != len(ascii_data):
24 spi_byte = int(ascii_data[byte:byte+2],16) # process this byte
27 # ORDER IS IMPORTANT HERE SO LIMIT TABLE ASSOCIATED
28 # WITH SPI BYTE VALUES
31 # Do this first to save on processing
33 #if debug: print ": ".join(outLine)
37 # Do this second to save on processing
40 #outLine.append("Error Byte")
41 outLine.append(ascii_data[byte:byte+2])
44 if int(ascii_data[byte:byte+2],16) != 0xA7:
45 outLine.append("Frame Termination Error")
46 outLine.append(ascii_data[byte:byte+2])
47 print ": ".join(outLine)
51 # SPI Version Response Test
52 # Do this third to keep it from the truncated SPI table
53 # FIXME: A mask should work here!!!
54 #if ((spi_byte & em.spi_version_mask) == 0x80):
55 if ((spi_byte >= 0x81) and (spi_byte <= 0xBF)):
56 outLine.append("SPI Version")
57 outLine.append(hex(spi_byte))
59 if int(ascii_data[byte:byte+2],16) != 0xA7:
60 outLine.append("Frame Termination Error")
61 outLine.append(ascii_data[byte:byte+2])
62 print ": ".join(outLine)
67 try: # Test for value else error
68 outLine.append(em.spi_values[em.spi_bytes.index(spi_byte)])
70 outLine.append("Unknown SPI Byte Value")
71 outLine.append(repr(hex(spi_byte)))
72 print ": ".join(outLine)
75 # SPI Protocol Version and Status Test
76 if spi_byte in em.spi_pro_stat:
78 if int(ascii_data[byte:byte+2],16) != 0xA7:
79 outLine.append("Frame Termination Error")
80 outLine.append(ascii_data[byte:byte+2])
81 print ": ".join(outLine)
89 data_length = int(ascii_data[byte:byte+2],16)
90 data_length *= 2 # For ASCII the length must be doubled
93 outLine.append(str(data_length))
96 # Use Length to get Frame Data
97 data_frame = ascii_data[byte:byte+data_length]
99 outLine.append("Data")
100 outLine.append(str(data_length))
101 byte += data_length # skip frames
103 # Test for Frame Terminator before processing
104 if int(ascii_data[byte:byte+2],16) != 0xA7:
105 outLine.append("Frame Termination Error")
106 outLine.append(ascii_data[byte:byte+2])
107 # Carrying on to next input
108 print ": ".join(outLine)
109 byte += 2 # next byte
115 # Handle Frame Counter
116 outLine.insert(0, data_frame[dfdata:dfdata+2])
119 dfdata += 2 # next byte
120 cmd_data = int(data_frame[dfdata:dfdata+2],16)
123 if (cmd_data & em.cmd_mask):
124 outLine.append("RESP")
126 if (cmd_data & em.resp_over_mask):
127 outLine.append(em.resp_overflow)
128 # Truncated Data Test
129 if (cmd_data & em.resp_trun_mask):
130 outLine.append(em.resp_truncated)
133 # Try in case reserved bits set take em.cmd_control out of range
135 outLine.append(em.cmd_control[cmd_data])
137 outLine.append("Unknown Control Command")
138 # Carrying on to next input
139 byte += 2 # next byte
142 # Frame Identification (A.K.A.: VERB)
143 dfdata += 2 # next byte
144 frame_id_data = int(data_frame[dfdata:dfdata+2],16)
146 outLine.append(em.frame_ids[frame_id_data])
148 outLine.append("Unknown Frame Id")
151 # Not Implemented but we can tack it onto the end
152 # for manual checking
153 dfdata += 2 # next byte
154 if dfdata < len(data_frame):
155 outLine.append("Frame Data")
156 outLine.append(data_frame[dfdata:])
158 # Carrying on to next input
160 print ": ".join(outLine)
161 byte += 2 # next byte
168 # Get Input - Remove whitespaces just in case
169 #ascii_data = sys.argv[1].replace(' ','')
170 if __name__ == "__main__":
171 INF = open(sys.argv[1],'r')
173 for line in INF.readlines():
174 indata = line.replace(' ','').strip()