-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinearBatteryChargerAnalyzer.py
183 lines (153 loc) · 7.51 KB
/
LinearBatteryChargerAnalyzer.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
# LinearBatteryChargerAnalyzer
#
# This file contains the code that extracts information from each I2C transaction then converts it to a human readable
# format so that the user can easily see what a specific Linear Battery Charger Analyzer is doing.
#
# Author: Lena Voytek
from saleae.analyzers import HighLevelAnalyzer, AnalyzerFrame, StringSetting, NumberSetting, ChoicesSetting
import struct
import BQ25150
CHARGER_I2C_ADDRESS = 0x6B
class LinearBatteryCharger(HighLevelAnalyzer):
# List of settings that a user can set for this High Level Analyzer.
# my_string_setting = StringSetting()
# my_number_setting = NumberSetting(min_value=0, max_value=100)
# my_choices_setting = ChoicesSetting(choices=('A', 'B'))
# An optional list of types this analyzer produces, providing a way to customize the way frames are displayed in Logic 2.
result_types = {
'error': {
'format': 'Error!'
},
'generic_data': {
'format': '{{data.address}} {{data.data}}'
},
'generic_read': {
'format': 'Read {{data.data}} from the {{data.address}} register'
},
'generic_write': {
'format': 'Wrote {{data.data}} to the {{data.data}} register'
},
'unit_read': {
'format': 'Read {{data.address}} of {{data.data}} {{data.units}}'
},
'unit_write': {
'format': 'Wrote {{data.address}} of {{data.data}} {{data.units}}'
}
}
multi_frame = None
temp_frame = None
calc_data = None
is_possible_multi = False
is_multi = False
multi_failed = False
def __init__(self):
'''
Initialize HLA.
Settings can be accessed using the same name used above.
'''
self._continue_analysis = False
self._is_reg_next = True
self._current_register = -1
self._current_conversion_value = 1
def decode(self, frame: AnalyzerFrame):
'''
Process a frame from the input analyzer, and optionally return a single `AnalyzerFrame` or a list of `AnalyzerFrame`s.
The type and data values in `frame` will depend on the input analyzer.
'''
# set our frame to an error frame, which will eventually get over-written as we get data.
if self.temp_frame is None:
self.temp_frame = AnalyzerFrame("error", frame.start_time, frame.end_time, {
"address": "error",
"data": "",
}
)
if (frame.type == "start" or frame.type == "address") and self.temp_frame.type == "error":
self.temp_frame = AnalyzerFrame("generic_data", frame.start_time, frame.end_time, {
"data": "",
}
)
self._is_reg_next = True
if frame.type == "address":
address_byte = frame.data["address"][0]
# Make sure this is a linear battery charger address
if int(address_byte) == CHARGER_I2C_ADDRESS:
self._continue_analysis = True
if(not self._is_reg_next and "read" in frame.data):
if frame.data["read"]:
self.temp_frame.type = "generic_read"
else:
self.temp_frame.type = "generic_write"
else:
self._continue_analysis = False
if frame.type == "data":
if self._continue_analysis:
data_byte = int(frame.data["data"][0])
# I2C is telling device which register to start at
if self._is_reg_next:
self._is_reg_next = False
self._current_register = data_byte
if data_byte in BQ25150.REGISTERS:
self.temp_frame.data["address"] = BQ25150.REGISTERS[data_byte]
else:
self.temp_frame.data["address"] = hex(data_byte)
# Check if this the end of a two-byte reading
if data_byte - 1 in BQ25150.MULTI_BYTE_DATA and self.is_possible_multi:
self.is_multi = True
self.temp_frame.type = self.multi_frame.type
self.temp_frame.start_time = self.multi_frame.start_time
self.temp_frame.data["data"] = self.multi_frame.data["data"]
self.temp_frame.data["address"] = self.multi_frame.data["address"]
elif self.is_possible_multi:
self.multi_failed = True
# Check if this could be the start of a two-byte reading
if data_byte in BQ25150.MULTI_BYTE_DATA:
self.is_possible_multi = True
if "read" in frame.data and frame.data["read"]:
self.multi_frame = AnalyzerFrame("unit_read", self.temp_frame.start_time, frame.end_time, {
})
else:
self.multi_frame = AnalyzerFrame("unit_write", self.temp_frame.start_time, frame.end_time, {
})
self.multi_frame.data["address"] = BQ25150.MULTI_BYTE_DATA[data_byte]["name"]
self.multi_frame.data["data"] = BQ25150.MULTI_BYTE_DATA[data_byte]["units"]
if "convert" in BQ25150.MULTI_BYTE_DATA[data_byte]:
self._current_conversion_value = BQ25150.MULTI_BYTE_DATA[
data_byte]["convert"]
else:
self._current_conversion_value = 1
else:
self.is_possible_multi = False
# I2C read or write register data
else:
# This is a status register, view each bit value
if(self._current_register in BQ25150.DERIVED_STATUS):
for status_bit in BQ25150.DERIVED_STATUS[self._current_register].keys():
if(status_bit & data_byte):
if len(self.temp_frame.data["data"]) > 0:
self.temp_frame.data["data"] += ", "
self.temp_frame.data["data"] += BQ25150.DERIVED_STATUS[self._current_register][status_bit]
# This is a generic register, just show a hex value
else:
self.temp_frame.data["data"] = hex(data_byte)
# Multi-byte calculations
if self.is_multi:
self.calc_data = self.calc_data + data_byte
self.calc_data *= self._current_conversion_value
self.calc_data = int(self.calc_data)
self.temp_frame.data["data"] = str(
self.calc_data) + self.multi_frame.data["data"]
elif self.is_possible_multi:
self.calc_data = data_byte << 8
if frame.type == "stop":
self.temp_frame.end_time = frame.end_time
new_frame = self.temp_frame
self.temp_frame = None
self.is_multi = False
if self._continue_analysis:
self._continue_analysis = False
self._current_register = -1
if self.multi_failed:
prev_frame = self.multi_frame
return prev_frame, new_frame
elif not self.is_possible_multi:
return new_frame