Skip to content

Commit dbce1f3

Browse files
committed
Add digital channels
1 parent 3450e73 commit dbce1f3

File tree

3 files changed

+87
-17
lines changed

3 files changed

+87
-17
lines changed

example.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,17 @@
3232
for i in range(0, 1000):
3333
read_data = ppk2_test.get_data()
3434
if read_data != b'':
35-
samples = ppk2_test.get_samples(read_data)
35+
samples, raw_digital = ppk2_test.get_samples(read_data)
3636
print(f"Average of {len(samples)} samples is: {sum(samples)/len(samples)}uA")
37+
38+
# Raw digital contains the raw digital data from the PPK2
39+
# The number of raw samples is equal to the number of samples in the samples list
40+
# We have to process the raw digital data to get the actual digital data
41+
digital_channels = ppk2_test.digital_channels(raw_digital)
42+
for ch in digital_channels:
43+
# Print last 10 values of each channel
44+
print(ch[-10:])
45+
print()
3746
time.sleep(0.01)
3847

3948
ppk2_test.toggle_DUT_power("OFF") # disable DUT power
@@ -44,8 +53,17 @@
4453
for i in range(0, 1000):
4554
read_data = ppk2_test.get_data()
4655
if read_data != b'':
47-
samples = ppk2_test.get_samples(read_data)
56+
samples, raw_digital = ppk2_test.get_samples(read_data)
4857
print(f"Average of {len(samples)} samples is: {sum(samples)/len(samples)}uA")
58+
59+
# Raw digital contains the raw digital data from the PPK2
60+
# The number of raw samples is equal to the number of samples in the samples list
61+
# We have to process the raw digital data to get the actual digital data
62+
digital_channels = ppk2_test.digital_channels(raw_digital)
63+
for ch in digital_channels:
64+
# Print last 10 values of each channel
65+
print(ch[-10:])
66+
print()
4967
time.sleep(0.01) # lower time between sampling -> less samples read in one sampling period
5068

5169
ppk2_test.stop_measuring()

example_mp.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717
print(f'Too many connected PPK2\'s: {ppk2s_connected}')
1818
exit()
1919

20-
ppk2_test = PPK2_API(ppk2_port, buffer_max_size_seconds=1, buffer_chunk_seconds=0.01)
20+
ppk2_test = PPK2_API(ppk2_port, buffer_max_size_seconds=70, buffer_chunk_seconds=0.5)
2121
ppk2_test.get_modifiers()
2222
ppk2_test.set_source_voltage(3300)
2323

24+
"""
25+
Source mode example
26+
"""
2427
ppk2_test.use_source_meter() # set source meter mode
2528
ppk2_test.toggle_DUT_power("ON") # enable DUT power
2629

@@ -29,23 +32,46 @@
2932
# the number of measurements in one sampling period depends on the wait between serial reads
3033
# it appears the maximum number of bytes received is 1024
3134
# the sampling rate of the PPK2 is 100 samples per millisecond
32-
for i in range(0, 1000):
35+
while True:
3336
read_data = ppk2_test.get_data()
3437
if read_data != b'':
35-
samples = ppk2_test.get_samples(read_data)
38+
samples, raw_digital = ppk2_test.get_samples(read_data)
3639
print(f"Average of {len(samples)} samples is: {sum(samples)/len(samples)}uA")
40+
41+
# Raw digital contains the raw digital data from the PPK2
42+
# The number of raw samples is equal to the number of samples in the samples list
43+
# We have to process the raw digital data to get the actual digital data
44+
digital_channels = ppk2_test.digital_channels(raw_digital)
45+
for ch in digital_channels:
46+
# Print last 10 values of each channel
47+
print(ch[-10:])
48+
print()
49+
3750
time.sleep(0.001)
3851

3952
ppk2_test.toggle_DUT_power("OFF") # disable DUT power
53+
ppk2_test.stop_measuring()
4054

55+
"""
56+
Ampere mode example
57+
"""
4158
ppk2_test.use_ampere_meter() # set ampere meter mode
4259

4360
ppk2_test.start_measuring()
44-
for i in range(0, 1000):
61+
while True:
4562
read_data = ppk2_test.get_data()
4663
if read_data != b'':
47-
samples = ppk2_test.get_samples(read_data)
64+
samples, raw_digital = ppk2_test.get_samples(read_data)
4865
print(f"Average of {len(samples)} samples is: {sum(samples)/len(samples)}uA")
66+
67+
# Raw digital contains the raw digital data from the PPK2
68+
# The number of raw samples is equal to the number of samples in the samples list
69+
# We have to process the raw digital data to get the actual digital data
70+
digital_channels = ppk2_test.digital_channels(raw_digital)
71+
for ch in digital_channels:
72+
# Print last 10 values of each channel
73+
print(ch[-10:])
74+
print()
4975
time.sleep(0.001) # lower time between sampling -> less samples read in one sampling period
5076

51-
ppk2_test.stop_measuring()
77+
# ppk2_test.stop_measuring()

src/ppk2_api/ppk2_api.py

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,10 @@ def _generate_mask(self, bits, pos):
189189
mask = self._twos_comp(mask)
190190
return {"mask": mask, "pos": pos}
191191

192-
def _get_masked_value(self, value, meas):
192+
def _get_masked_value(self, value, meas, is_bits=False):
193+
# print(f"Value: {value}")
194+
# print(f"Meas: {meas}")
193195
masked_value = (value & meas["mask"]) >> meas["pos"]
194-
if meas["pos"] == 24:
195-
if masked_value == 255:
196-
masked_value = -1
197196
return masked_value
198197

199198
def _handle_raw_data(self, adc_value):
@@ -205,10 +204,10 @@ def _handle_raw_data(self, adc_value):
205204
bits = self._get_masked_value(adc_value, self.MEAS_LOGIC)
206205
analog_value = self.get_adc_result(
207206
current_measurement_range, adc_result) * 10**6
208-
return analog_value
207+
return analog_value, bits
209208
except Exception as e:
210209
print("Measurement outside of range!")
211-
return None
210+
return None, None
212211

213212
@staticmethod
214213
def list_devices():
@@ -327,6 +326,26 @@ def _digital_to_analog(self, adc_value):
327326
"""Convert discrete value to analog value"""
328327
return int.from_bytes(adc_value, byteorder="little", signed=False) # convert reading to analog value
329328

329+
def digital_channels(self, bits):
330+
"""
331+
Convert raw digital data to digital channels.
332+
333+
Returns a 2d matrix with 8 rows (one for each channel). Each row contains HIGH and LOW values for the selected channel.
334+
"""
335+
336+
# Prepare 2d matrix with 8 rows (one for each channel)
337+
digital_channels = [[], [], [], [], [], [], [], []]
338+
for sample in bits:
339+
digital_channels[0].append((sample & 1) >> 0)
340+
digital_channels[1].append((sample & 2) >> 1)
341+
digital_channels[2].append((sample & 4) >> 2)
342+
digital_channels[3].append((sample & 8) >> 3)
343+
digital_channels[4].append((sample & 16) >> 4)
344+
digital_channels[5].append((sample & 32) >> 5)
345+
digital_channels[6].append((sample & 64) >> 6)
346+
digital_channels[7].append((sample & 128) >> 7)
347+
return digital_channels
348+
330349
def get_samples(self, buf):
331350
"""
332351
Returns list of samples read in one sampling period.
@@ -338,28 +357,35 @@ def get_samples(self, buf):
338357
sample_size = 4 # one analog value is 4 bytes in size
339358
offset = self.remainder["len"]
340359
samples = []
360+
raw_digital_output = []
341361

342362
first_reading = (
343363
self.remainder["sequence"] + buf[0:sample_size-offset])[:4]
344364
adc_val = self._digital_to_analog(first_reading)
345-
measurement = self._handle_raw_data(adc_val)
365+
measurement, bits = self._handle_raw_data(adc_val)
346366
if measurement is not None:
347367
samples.append(measurement)
368+
if bits is not None:
369+
raw_digital_output.append(bits)
348370

349371
offset = sample_size - offset
350372

351373
while offset <= len(buf) - sample_size:
352374
next_val = buf[offset:offset + sample_size]
353375
offset += sample_size
354376
adc_val = self._digital_to_analog(next_val)
355-
measurement = self._handle_raw_data(adc_val)
377+
measurement, bits = self._handle_raw_data(adc_val)
356378
if measurement is not None:
357379
samples.append(measurement)
380+
if bits is not None:
381+
raw_digital_output.append(bits)
358382

359383
self.remainder["sequence"] = buf[offset:len(buf)]
360384
self.remainder["len"] = len(buf)-offset
361385

362-
return samples # return list of samples, handle those lists in PPK2 API wrapper
386+
# return list of samples and raw digital outputs
387+
# handle those lists in PPK2 API wrapper
388+
return samples, raw_digital_output
363389

364390

365391
class PPK_Fetch(threading.Thread):

0 commit comments

Comments
 (0)