-
Notifications
You must be signed in to change notification settings - Fork 0
/
HighLevelAnalyzer.py
205 lines (188 loc) · 8.81 KB
/
HighLevelAnalyzer.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# High Level Analyzer
# For more information and documentation, please go to https://support.saleae.com/extensions/high-level-analyzer-extensions
from saleae.analyzers import HighLevelAnalyzer, AnalyzerFrame, StringSetting, NumberSetting, ChoicesSetting
# High level analyzers must subclass the HighLevelAnalyzer class.
class Hla(HighLevelAnalyzer):
# my_string_setting = StringSetting()
# my_number_setting = NumberSetting(min_value=0, max_value=127)
pcf8583_addr_string = ChoicesSetting(label="PCF8583 Address",
choices=('0x50', '0x51'))
pcf8583_addr = 0x50
weekdays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Unknown']
result_types = {
'indexReg': {
'format': 'Set IndexReg={{data.register}}'
},
'data': {
'format': 'R={{data.register}}, D={{data.value}}'
},
'hour': {
'format': 'hour={{data.value}}{{data.ampm}}'
},
'minute': {
'format': 'minute={{data.value}}'
},
'second': {
'format': 'seconds={{data.value}}'
},
'msecond': {
'format': 'mseconds={{data.value}}'
},
'time': {
'format':
'{{data.hour}}:{{data.minute}}:{{data.second}}{{data.ampm}}'
},
'yearday': {
'format': 'Year={{data.year}}, Day={{data.day}}'
},
'weekdaymonth': {
'format': 'Weekday={{data.weekday}}, Month={{data.month}}'
},
'date': {
'format':
'{{data.year}}-{{data.month}}-{{data.day}} ({{data.weekday}})'
}
}
def __init__(self):
'''
Initialize HLA.
Settings can be accessed using the same name used above.
'''
if self.pcf8583_addr_string == '0x51':
self.pcf8583_addr = 0x51
self.last_address = 0
# What register address we use currently
self.register_addr = 0
# if the last frame was a write to the I2C address of the PCF
# Next data is register index
self.last_was_index_write = False
self.second = 0
self.minute = 0
self.hour = 0
self.year = 0
self.month = 0
self.day = 0
self.time_start_time = None
self.date_start_time = None
def __bcd_to_int(self, b):
return (b >> 4) * 10 + (b & 0x0f)
def __frame_data_to_int(self, f):
return self.__bcd_to_int(
int.from_bytes(f.data['data'], byteorder='little'))
def decode(self, frame: AnalyzerFrame):
if frame.type == 'address':
self.last_address = int.from_bytes(frame.data['address'],
byteorder='little')
if self.last_address == self.pcf8583_addr and not frame.data[
'read']:
self.last_was_index_write = True
self.register_addr = 0
return
if self.last_address == self.pcf8583_addr:
if frame.type == 'data':
if self.last_was_index_write:
self.register_addr = int.from_bytes(frame.data['data'],
byteorder='little')
self.last_was_index_write = False
return AnalyzerFrame('indexReg', frame.start_time,
frame.end_time,
{'register': self.register_addr})
else:
self.register_addr = self.register_addr + 1
if self.register_addr - 1 == 1:
return AnalyzerFrame(
'msecond', frame.start_time, frame.end_time,
{'value': self.__frame_data_to_int(frame) * 10})
if self.register_addr - 1 == 2:
self.second = self.__bcd_to_int(
int.from_bytes(frame.data['data'],
byteorder='little'))
self.time_start_time = frame.start_time
return AnalyzerFrame(
'second', frame.start_time, frame.end_time, {
'value':
self.__bcd_to_int(
int.from_bytes(frame.data['data'],
byteorder='little'))
})
if self.register_addr - 1 == 3:
self.minute = self.__bcd_to_int(
int.from_bytes(frame.data['data'],
byteorder='little'))
return AnalyzerFrame(
'minute', frame.start_time, frame.end_time, {
'value':
self.__bcd_to_int(
int.from_bytes(frame.data['data'],
byteorder='little'))
})
# Here comes the hour, try to combine with seconds and minutes we
# probably got earlier
if self.register_addr - 1 == 4:
d = int.from_bytes(frame.data['data'],
byteorder='little')
self.hour = self.__bcd_to_int(d & 0x3f)
if d & 0x80:
if d & 0x40:
ampm = 'pm'
else:
ampm = 'am'
else:
ampm = ''
if self.time_start_time:
return AnalyzerFrame(
'time', self.time_start_time, frame.end_time, {
'second': str(self.second).zfill(2),
'minute': str(self.minute).zfill(2),
'hour': str(self.hour).zfill(2),
'ampm': ampm
})
else:
return AnalyzerFrame('hour', frame.start_time,
frame.end_time, {
'value': self.hour,
'ampm': ampm
})
# Year+Date
if self.register_addr - 1 == 5:
self.date_start_time = frame.start_time
d = int.from_bytes(frame.data['data'],
byteorder='little')
self.year = 2020 + (d >> 6)
self.day = self.__bcd_to_int(d & 0x3f)
return AnalyzerFrame('yearday', frame.start_time,
frame.end_time, {
'year': self.year,
'day': self.day
})
# Weekday+Month
# Try to combine with Year+Date data
if self.register_addr - 1 == 6:
d = int.from_bytes(frame.data['data'],
byteorder='little')
self.month = self.__bcd_to_int(d & 0x1f)
self.weekday = self.weekdays[d >> 5]
if self.date_start_time:
return AnalyzerFrame(
'date', self.date_start_time, frame.end_time, {
'year': self.year,
'month': self.month,
'day': self.day,
'weekday': self.weekday
})
return AnalyzerFrame('weekdaymonth', frame.start_time,
frame.end_time, {
'month': self.month,
'weekday': self.weekday
})
# Anything else we don't recognize
return AnalyzerFrame(
'data', frame.start_time, frame.end_time, {
'register':
self.register_addr - 1,
'value':
hex(
int.from_bytes(frame.data['data'],
byteorder='little'))
})
return