-
Notifications
You must be signed in to change notification settings - Fork 0
/
BluesSerialOverI2CAnalyzer.py
181 lines (153 loc) · 6.63 KB
/
BluesSerialOverI2CAnalyzer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# Blues Serial-Over-I2C Protocol Analyzer
# For more information and documentation, please go to https://dev.blues.io/guides-and-tutorials/notecard-guides/serial-over-i2c-protocol/
from saleae.analyzers import HighLevelAnalyzer, AnalyzerFrame, NumberSetting
# High level analyzers must subclass the HighLevelAnalyzer class.
class Hla(HighLevelAnalyzer):
# Parameters
notecard_i2c_addr = NumberSetting(min_value=0, max_value=127)
# An optional list of types this analyzer produces, providing a way to customize the way frames are displayed in Logic 2.
result_types = {
'addr': {
'format': '{{data.sender}}'
},
'hdr': {
'format': '{{data.action}}: {{data.length}}'
},
'note': {
'format': '{{data.Note}}'
},
'query': {
'format': 'Query Notecard'
},
'request': {
'format': 'Request: {{data.length}}'
}
}
def __init__(self):
self.data = {
'note': [],
'start_time': None
}
self.hdr1 = None
self.hdr2 = None
self.ignore = False
self.request_start_time = None
self.read = None
if self.notecard_i2c_addr == 0x00:
self.notecard_i2c_addr = 0x17
print(f"=======================================")
print(f"Blues Serial-Over-I2C Protocol Analyzer")
print()
print('Settings:')
print(f"- Notecard I2C Address: 0x{int(self.notecard_i2c_addr):02X}")
print()
print(f"https://dev.blues.io/guides-and-tutorials/notecard-guides/serial-over-i2c-protocol/")
def decode(self, frame: AnalyzerFrame):
if frame.type == 'start':
# /--------------------------------------/
# /------/ START FRAME DATA ITEMS /------/
# /--------------------------------------/
# /--------------------------------------/
self.data = {
'note': [],
'start_time': None
}
self.hdr1 = None
self.hdr2 = None
self.ignore = False
self.request_start_time = None
self.read = None
elif frame.type == 'address':
# /----------------------------------------/
# /------/ ADDRESS FRAME DATA ITEMS /------/
# /----------------------------------------/
# Key: ack, Value: True
# Key: address, Value: b'\x17'
# Key: read, Value: False
# /----------------------------------------/
if (frame.data['address'][0] != self.notecard_i2c_addr):
self.ignore = True
return None
print(f"=======================================")
self.read = frame.data['read']
sender = 'Host MCU' if not frame.data['read'] else 'Notecard'
print(f"Sender: {sender}")
return AnalyzerFrame('addr', frame.start_time, frame.end_time, {
'sender': sender
})
elif frame.type == 'data':
# /-------------------------------------/
# /------/ DATA FRAME DATA ITEMS /------/
# /-------------------------------------/
# Key: ack, Value: True
# Key: data, Value: b'^'
# /-------------------------------------/
if self.ignore:
return None
if self.read: # Notecard Response
# Special Handling for Header Byte 1
if self.hdr1 == None:
self.hdr1 = frame.data['data'][0]
# Handle Notecard Response
print(f"Queued: {self.hdr1}")
return AnalyzerFrame('hdr', frame.start_time, frame.end_time, {
'action': 'Queued',
'length': self.hdr1
})
# Special Handling for Header Byte 2
elif self.hdr2 == None:
self.hdr2 = frame.data['data'][0]
# Handle Notecard Response
print(f"Sending: {self.hdr2}")
return AnalyzerFrame('hdr', frame.start_time, frame.end_time, {
'action': 'Sending',
'length': self.hdr2
})
else: # Host MCU Request
# Special Handling for Header Byte 1
if self.hdr1 == None:
self.hdr1 = frame.data['data'][0]
# Handle Host MCU Request
if self.hdr1 == 0:
self.request_start_time = frame.start_time
return None
else:
print(f"Sending: {self.hdr1}")
return AnalyzerFrame('hdr', frame.start_time, frame.end_time, {
'action': 'Sending',
'length': self.hdr1
})
# Special Handling for Header Byte 2
elif self.hdr2 == None:
self.hdr2 = frame.data['data'][0]
# Handle Host MCU Request
if self.request_start_time != None:
if self.hdr2 == 0:
# Handle Query Request
print(f"Query Notecard")
return AnalyzerFrame('query', self.request_start_time, frame.end_time, None)
else:
# Handle Request Bytes
print(f"Request: {self.hdr2}")
return AnalyzerFrame('request', self.request_start_time, frame.end_time, {
'length': self.hdr2
})
# Handle Note Bytes (Request or Response)
if self.data['start_time'] == None:
self.data['start_time'] = frame.start_time
self.data['note'].append(frame.data['data'])
elif frame.type == 'stop':
# /-------------------------------------/
# /------/ STOP FRAME DATA ITEMS /------/
# /-------------------------------------/
# /-------------------------------------/
if len(self.data['note']) > 0:
note = ''.join([byte.decode('ascii') for byte in self.data['note']]).rstrip('\n')
print(f"Note: {note}")
return AnalyzerFrame('note', self.data['start_time'], frame.end_time, {
'Note': note
})
else:
return None
# Return the data frame itself
return None