Skip to content

Commit 9894072

Browse files
authored
Merge pull request #33 from IRNAS/dev
Dev
2 parents eaaf1ad + 719a0b5 commit 9894072

File tree

4 files changed

+88
-21
lines changed

4 files changed

+88
-21
lines changed

example.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
3. read stream of data
88
"""
99
import time
10-
from src.ppk2_api.ppk2_api import PPK2_API
10+
from ppk2_api.ppk2_api import PPK2_API
1111

1212
ppk2s_connected = PPK2_API.list_devices()
1313
if(len(ppk2s_connected) == 1):
@@ -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

51-
ppk2_test.stop_measuring()
69+
ppk2_test.stop_measuring()

example_mp.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
3. read stream of data
88
"""
99
import time
10-
from src.ppk2_api.ppk2_api import PPK2_MP as PPK2_API
10+
from ppk2_api.ppk2_api import PPK2_MP as PPK2_API
1111

1212
ppk2s_connected = PPK2_API.list_devices()
1313
if(len(ppk2s_connected) == 1):
@@ -21,6 +21,9 @@
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()

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def read(*names, **kwargs):
2323

2424
setup(
2525
name="ppk2-api",
26-
version="0.9.1",
26+
version="0.9.2",
2727
description="API for Nordic Semiconductor's Power Profiler Kit II (PPK 2).",
2828
url="https://github.com/IRNAS/ppk2-api-python",
2929
packages=find_packages("src"),

src/ppk2_api/ppk2_api.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,8 @@ 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):
193193
masked_value = (value & meas["mask"]) >> meas["pos"]
194-
if meas["pos"] == 24:
195-
if masked_value == 255:
196-
masked_value = -1
197194
return masked_value
198195

199196
def _handle_raw_data(self, adc_value):
@@ -205,10 +202,10 @@ def _handle_raw_data(self, adc_value):
205202
bits = self._get_masked_value(adc_value, self.MEAS_LOGIC)
206203
analog_value = self.get_adc_result(
207204
current_measurement_range, adc_result) * 10**6
208-
return analog_value
205+
return analog_value, bits
209206
except Exception as e:
210207
print("Measurement outside of range!")
211-
return None
208+
return None, None
212209

213210
@staticmethod
214211
def list_devices():
@@ -327,6 +324,26 @@ def _digital_to_analog(self, adc_value):
327324
"""Convert discrete value to analog value"""
328325
return int.from_bytes(adc_value, byteorder="little", signed=False) # convert reading to analog value
329326

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

342360
first_reading = (
343361
self.remainder["sequence"] + buf[0:sample_size-offset])[:4]
344362
adc_val = self._digital_to_analog(first_reading)
345-
measurement = self._handle_raw_data(adc_val)
363+
measurement, bits = self._handle_raw_data(adc_val)
346364
if measurement is not None:
347365
samples.append(measurement)
366+
if bits is not None:
367+
raw_digital_output.append(bits)
348368

349369
offset = sample_size - offset
350370

351371
while offset <= len(buf) - sample_size:
352372
next_val = buf[offset:offset + sample_size]
353373
offset += sample_size
354374
adc_val = self._digital_to_analog(next_val)
355-
measurement = self._handle_raw_data(adc_val)
375+
measurement, bits = self._handle_raw_data(adc_val)
356376
if measurement is not None:
357377
samples.append(measurement)
378+
if bits is not None:
379+
raw_digital_output.append(bits)
358380

359381
self.remainder["sequence"] = buf[offset:len(buf)]
360382
self.remainder["len"] = len(buf)-offset
361383

362-
return samples # return list of samples, handle those lists in PPK2 API wrapper
384+
# return list of samples and raw digital outputs
385+
# handle those lists in PPK2 API wrapper
386+
return samples, raw_digital_output
363387

364388

365389
class PPK_Fetch(threading.Thread):
@@ -401,7 +425,6 @@ def run(self):
401425
self._buffer_q.get()
402426
local_buffer = local_buffer[self._buffer_chunk:]
403427
self._last_timestamp = tm_now
404-
# print(len(d), len(local_buffer), self._buffer_q.qsize())
405428

406429
# calculate stats
407430
s += len(d)

0 commit comments

Comments
 (0)