File: analogin_sync_2m2ks.py

package info (click to toggle)
libm2k 0.9.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 95,580 kB
  • sloc: xml: 1,611,497; cpp: 16,278; python: 4,181; cs: 516; sh: 471; ansic: 403; makefile: 35
file content (163 lines) | stat: -rw-r--r-- 4,972 bytes parent folder | download | duplicates (2)
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
#
# Copyright (c) 2019 Analog Devices Inc.
#
# This file is part of libm2k
# (see http://www.github.com/analogdevicesinc/libm2k).
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

# Requirements: 2x ADALM2000
#
# This example assumes the following connections:
# W1 (1st M2K) -> 1+ (1st M2K) -> 1+ (2nd M2K)
# DIO0 (1st M2K) -> 2+ (1st M2K)
# TO (1st M2K) -> TI (2nd M2K) -> 2+ (2nd M2K)
# GND (1st M2K) -> GND (2nd M2K)
# The application will generate a square wave on W1 of the first M2K. The signal is read on Analog Input 1 of both M2Ks.
# The acquisition on the 1st M2K is triggered by toggling the DIO0 pin. This is forwarded to the TI pin (2nd M2K) via
# the TO pin(1st M2K). This results in a synchronised acquisition on both devices. The trigger can be observed on
# Analog Input 2 of both modules.

import libm2k
import matplotlib.pyplot as plt
import time
import numpy as np

DIGITAL_CH = 0

BUFFER_SIZE = 800000
AMPLITUDE = 4
SAMPLES_PER_PERIOD = 256
PLOTTED_SAMPLES = BUFFER_SIZE
OFFSET = 5

sampling_frequency_in = 10000000
sampling_frequency_out = 750000


def generate_clock_signal():
    buffer = []
    for i in range(256):
        buffer.append(1)
    for i in range(256):
        buffer.append(0)

    for i in range(4):
        buffer.extend(buffer)
    buffer = buffer * AMPLITUDE
    print(buffer)
    return buffer


ctx = libm2k.m2kOpen("replace with uri for 1st M2K")
ctx2 = libm2k.m2kOpen("replace with uri for 2nd M2K")

if ctx is None:
    print("Connection Error: No ADALM2000 device available/connected to your PC.")
    exit(1)

if ctx2 is None:
    print("Connection Error: No second ADALM2000 device available/connected to your PC.")
    exit(1)

# Configure 1st context
ain = ctx.getAnalogIn()
aout = ctx.getAnalogOut()
dig = ctx.getDigital()
trig = ain.getTrigger()

# Prevent bad initial config for ADC and DAC
ain.reset()
aout.reset()
dig.reset()

# Configure 2nd context
ain2 = ctx2.getAnalogIn()
dig2 = ctx2.getDigital()
trig2 = dig2.getTrigger()

# Prevent bad initial config for ADC and DAC
ain2.reset()
dig2.reset()

ctx.calibrateADC()
ctx.calibrateDAC()
ctx2.calibrateADC()
ctx2.calibrateDAC()

if trig.hasExternalTriggerOut():
    trig.setAnalogExternalOutSelect(libm2k.SELECT_ANALOG_IN)  # Forward Analog trigger on TO pin

ain.enableChannel(0, True)
ain.enableChannel(1, True)
ain.setSampleRate(sampling_frequency_in)
ain.setRange(0, libm2k.PLUS_MINUS_2_5V)
ain.setRange(1, libm2k.PLUS_MINUS_2_5V)
dig.setDirection(DIGITAL_CH, libm2k.DIO_OUTPUT)
dig.enableChannel(DIGITAL_CH, True)
dig.setValueRaw(DIGITAL_CH, libm2k.LOW)

ain2.enableChannel(0, True)
ain2.enableChannel(1, True)
ain2.setSampleRate(sampling_frequency_in)
ain2.setRange(0, libm2k.PLUS_MINUS_2_5V)
ain2.setRange(1, libm2k.PLUS_MINUS_2_5V)

# Trigger settings for 1st context
trig.setAnalogSource(1)  # Channel 2 as source
trig.setAnalogCondition(1, libm2k.RISING_EDGE_ANALOG)
trig.setAnalogLevel(1, 0.5)  # Set trigger level at 0.5
trig.setAnalogDelay(0)  # Trigger is centered
trig.setAnalogMode(1, libm2k.ANALOG)  # Trigger condition specified only by analog trigger

# Trigger settings for 2nd context
trig2.setAnalogMode(0, libm2k.EXTERNAL)  # Trigger condition specified only by external trigger (TI)
trig2.setAnalogSource(0)
trig2.setAnalogExternalCondition(0, libm2k.RISING_EDGE_ANALOG)
trig2.setAnalogLevel(0, 0.5)  # Set trigger level at 0.5
trig2.setAnalogDelay(0)  # Trigger is centered

# Configure analog out 1st context
aout.setSampleRate(0, sampling_frequency_out)
aout.setSampleRate(1, sampling_frequency_out)
aout.enableChannel(0, True)
aout.enableChannel(1, True)
aout.setCyclic(False)

ain.startAcquisition(BUFFER_SIZE)
ain2.startAcquisition(BUFFER_SIZE)

# Toggle trigger pin
dig.setValueRaw(DIGITAL_CH, libm2k.LOW)
dig.setValueRaw(DIGITAL_CH, libm2k.HIGH)

buffer = generate_clock_signal()
aout.push([buffer, buffer])

data = ain.getSamples(BUFFER_SIZE)
data2 = ain2.getSamples(BUFFER_SIZE)

# Plot routine
plt.plot(np.array(data[0]), label="Clock Signal(1st M2K)")
plt.plot(np.array(data2[0]) + OFFSET, label="Clock Signal(2nd M2K)")
plt.plot(np.array(data[1]) + 2 * OFFSET, label="Trigger Signal(1st M2K, DIO0)")
plt.plot(np.array(data2[1]) + 3 * OFFSET, label="Trigger Signal(2nd M2K, TI)")
plt.legend(loc="upper right")
plt.grid(True)
plt.show()
time.sleep(0.1)
aout.stop()
libm2k.contextClose(ctx)
libm2k.contextClose(ctx2)