Skip to content

Commit 9c280e5

Browse files
committed
Fix adc reading calculation
1 parent bc50c55 commit 9c280e5

File tree

1 file changed

+47
-37
lines changed

1 file changed

+47
-37
lines changed

src/ppk2_api.py

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ class PPK2_Command():
3535
RESET = 0x20
3636
SET_USER_GAINS = 0x25
3737

38+
3839
class PPK2_Modes():
3940
"""PPK2 measurement modes"""
4041
AMPERE_MODE = "AMPERE_MODE"
4142
SOURCE_MODE = "SOURCE_MODE"
4243

44+
4345
class PPK2_API():
4446
def __init__(self, port):
4547

@@ -48,13 +50,13 @@ def __init__(self, port):
4850

4951
self.modifiers = {
5052
"Calibrated": None,
51-
"R": {"0": None, "1": None, "2": None, "3": None, "4": None},
52-
"GS": {"0": None, "1": None, "2": None, "3": None, "4": None},
53-
"GI": {"0": None, "1": None, "2": None, "3": None, "4": None},
54-
"O": {"0": None, "1": None, "2": None, "3": None, "4": None},
55-
"S": {"0": None, "1": None, "2": None, "3": None, "4": None},
56-
"I": {"0": None, "1": None, "2": None, "3": None, "4": None},
57-
"UG": {"0": None, "1": None, "2": None, "3": None, "4": None},
53+
"R": {"0": 1031.64, "1": 101.65, "2": 10.15, "3": 0.94, "4": 0.043},
54+
"GS": {"0": 1, "1": 1, "2": 1, "3": 1, "4": 1},
55+
"GI": {"0": 1, "1": 1, "2": 1, "3": 1, "4": 1},
56+
"O": {"0": 0, "1": 0, "2": 0, "3": 0, "4": 0},
57+
"S": {"0": 0, "1": 0, "2": 0, "3": 0, "4": 0},
58+
"I": {"0": 0, "1": 0, "2": 0, "3": 0, "4": 0},
59+
"UG": {"0": 1, "1": 1, "2": 1, "3": 1, "4": 1},
5860
"HW": None,
5961
"IA": None
6062
}
@@ -70,10 +72,6 @@ def __init__(self, port):
7072
self.MEAS_RANGE = self._generate_mask(3, 14)
7173
self.MEAS_LOGIC = self._generate_mask(8, 24)
7274

73-
self.prev_rolling_avg = None
74-
self.prev_rolling_avg4 = None
75-
self.prev_range = None
76-
7775
self.mode = None
7876

7977
# adc measurement buffer remainder and len of remainder
@@ -147,7 +145,14 @@ def _parse_metadata(self, metadata):
147145
self.modifiers[key] = data_pair[1]
148146
for ind in range(0, 5):
149147
if key+str(ind) == data_pair[0]:
150-
self.modifiers[key][str(ind)] = float(data_pair[1])
148+
if "R" in data_pair[0]:
149+
# problem on some PPK2s with wrong calibration values - this doesn't fix it
150+
if float(data_pair[1]) != 0:
151+
self.modifiers[key][str(ind)] = float(
152+
data_pair[1])
153+
else:
154+
self.modifiers[key][str(ind)] = float(
155+
data_pair[1])
151156

152157
return True
153158
except Exception as e:
@@ -166,14 +171,18 @@ def _get_masked_value(self, value, meas):
166171

167172
def _handle_raw_data(self, adc_value):
168173
"""Convert raw value to analog value"""
169-
current_measurement_range = min(self._get_masked_value(
170-
adc_value, self.MEAS_RANGE), 4) # 5 is the number of parameters
171-
adc_result = self._get_masked_value(adc_value, self.MEAS_ADC) * 4
172-
bits = self._get_masked_value(adc_value, self.MEAS_LOGIC)
173-
analog_value = self.get_adc_result(
174-
current_measurement_range, adc_result) * 10**6
175-
176-
return analog_value
174+
try:
175+
current_measurement_range = min(self._get_masked_value(
176+
adc_value, self.MEAS_RANGE), 5) # 5 is the number of parameters
177+
adc_result = self._get_masked_value(adc_value, self.MEAS_ADC) * 4
178+
# print(f"adc result {adc_result}") # 564
179+
bits = self._get_masked_value(adc_value, self.MEAS_LOGIC)
180+
analog_value = self.get_adc_result(
181+
current_measurement_range, adc_result) * 10**6
182+
return analog_value
183+
except:
184+
print("Measurement outside of range!")
185+
return None
177186

178187
@staticmethod
179188
def list_devices():
@@ -210,40 +219,38 @@ def set_source_voltage(self, mV):
210219
"""
211220
b_1, b_2 = self._convert_source_voltage(mV)
212221
self._write_serial((PPK2_Command.REGULATOR_SET, b_1, b_2))
213-
#self.current_vdd = mV
222+
self.current_vdd = mV
214223

215224
def toggle_DUT_power(self, state):
216225
"""Toggle DUT power based on parameter"""
217226
if state == "ON":
218-
self._write_serial((PPK2_Command.DEVICE_RUNNING_SET, PPK2_Command.TRIGGER_SET)) # 12,1
227+
self._write_serial(
228+
(PPK2_Command.DEVICE_RUNNING_SET, PPK2_Command.TRIGGER_SET)) # 12,1
219229

220230
if state == "OFF":
221-
self._write_serial((PPK2_Command.DEVICE_RUNNING_SET, PPK2_Command.NO_OP)) # 12,0
231+
self._write_serial(
232+
(PPK2_Command.DEVICE_RUNNING_SET, PPK2_Command.NO_OP)) # 12,0
222233

223234
def use_ampere_meter(self):
224235
"""Configure device to use ampere meter"""
225236
self.mode = PPK2_Modes.AMPERE_MODE
226-
self._write_serial((PPK2_Command.SET_POWER_MODE, PPK2_Command.TRIGGER_SET)) # 17,1
237+
self._write_serial((PPK2_Command.SET_POWER_MODE,
238+
PPK2_Command.TRIGGER_SET)) # 17,1
227239

228240
def use_source_meter(self):
229241
"""Configure device to use source meter"""
230242
self.mode = PPK2_Modes.SOURCE_MODE
231-
self._write_serial((PPK2_Command.SET_POWER_MODE, PPK2_Command.AVG_NUM_SET)) # 17,2
243+
self._write_serial((PPK2_Command.SET_POWER_MODE,
244+
PPK2_Command.AVG_NUM_SET)) # 17,2
232245

233246
def get_adc_result(self, current_range, adc_value):
234247
"""Get result of adc conversion"""
235248
current_range = str(current_range)
236249
result_without_gain = (adc_value - self.modifiers["O"][current_range]) * (
237250
self.adc_mult / self.modifiers["R"][current_range])
238251

239-
adc = self.modifiers["UG"][current_range] * (
240-
result_without_gain *
241-
(self.modifiers["GS"][current_range] *
242-
result_without_gain + self.modifiers["GI"][current_range])
243-
# this part is currently not used
244-
+ (self.modifiers["S"][current_range] +
245-
(self.current_vdd / 1000) + self.modifiers["I"][current_range])
246-
)
252+
adc = self.modifiers["UG"][current_range] * (result_without_gain * (self.modifiers["GS"][current_range] * result_without_gain + self.modifiers["GI"][current_range]) + (
253+
self.modifiers["S"][current_range] * (self.current_vdd / 1000) + self.modifiers["I"][current_range]))
247254

248255
self.rolling_avg = adc
249256
self.rolling_avg4 = adc
@@ -266,10 +273,12 @@ def get_samples(self, buf):
266273
offset = self.remainder["len"]
267274
samples = []
268275

269-
first_reading = (self.remainder["sequence"] + buf[0:sample_size-offset])[:4]
276+
first_reading = (
277+
self.remainder["sequence"] + buf[0:sample_size-offset])[:4]
270278
adc_val = self._digital_to_analog(first_reading)
271279
measurement = self._handle_raw_data(adc_val)
272-
samples.append(measurement)
280+
if measurement is not None:
281+
samples.append(measurement)
273282

274283
offset = sample_size - offset
275284

@@ -278,9 +287,10 @@ def get_samples(self, buf):
278287
offset += sample_size
279288
adc_val = self._digital_to_analog(next_val)
280289
measurement = self._handle_raw_data(adc_val)
281-
samples.append(measurement)
290+
if measurement is not None:
291+
samples.append(measurement)
282292

283293
self.remainder["sequence"] = buf[offset:len(buf)]
284294
self.remainder["len"] = len(buf)-offset
285295

286-
return samples # return list of samples, handle those lists in PPK2 API wrapper
296+
return samples # return list of samples, handle those lists in PPK2 API wrapper

0 commit comments

Comments
 (0)