Skip to content

Commit 2209469

Browse files
author
n.danilyuk
committed
Added DMA Fast ADC examaple
1 parent d1803b6 commit 2209469

File tree

6 files changed

+177
-10
lines changed

6 files changed

+177
-10
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
// Example of data capture at the Fast ADC IN1 via DMA
2+
// Before starting, you need to connect Fast OUT 1 <=> Fast IN1
3+
4+
// Written by Nikolay Danilyuk
5+
6+
// REQUIRES the following Arduino libraries:
7+
// - SCPI Red Pitaya Library:
8+
// https://github.com/RedPitaya/SCPI-red-pitaya-arduino
9+
10+
#include <Arduino.h>
11+
12+
#include "SCPI_RP.h"
13+
14+
#if defined(ARDUINO_ARCH_AVR)
15+
#include <SoftwareSerial.h>
16+
SoftwareSerial uart(8, 9); // Initializes line 8 as RX and line 9 as TX for
17+
// SCPI communication via UART
18+
#endif
19+
20+
scpi_rp::SCPIRedPitaya rp;
21+
22+
void setup() {
23+
Serial.begin(115200);
24+
25+
#if defined(ARDUINO_ARCH_AVR)
26+
uart.begin(RED_PITAYA_UART_RATE);
27+
rp.initUARTStream(&uart);
28+
#elif defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_SAMD) || \
29+
defined(ARDUINO_ARCH_SAM)
30+
Serial1.begin(RED_PITAYA_UART_RATE);
31+
rp.initUARTStream(&Serial1);
32+
#endif
33+
34+
rp.gen.reset();
35+
rp.gen.wave(scpi_rp::GEN_CH_1, scpi_rp::SINE);
36+
rp.gen.freq(scpi_rp::GEN_CH_1, 10000);
37+
rp.gen.amp(scpi_rp::GEN_CH_1, 0.9);
38+
rp.gen.enable(scpi_rp::GEN_CH_1, true);
39+
rp.gen.sync(scpi_rp::GEN_CH_1);
40+
41+
if (!rp.acq.control.reset()) {
42+
Serial.println("Error reset acq");
43+
}
44+
45+
uint32_t startAddressReserved = 0;
46+
uint32_t sizeReserved = 0;
47+
48+
if (!rp.acq.dma.settings.memoryStartAddressQ(&startAddressReserved)) {
49+
Serial.println("Error get decimation");
50+
} else {
51+
Serial.print("Start address = ");
52+
Serial.println(startAddressReserved);
53+
}
54+
55+
if (!rp.acq.dma.settings.memorySizeQ(&sizeReserved)) {
56+
Serial.println("Error get decimation");
57+
} else {
58+
Serial.print("Size = ");
59+
Serial.println(sizeReserved);
60+
}
61+
62+
uint32_t decimation = 123;
63+
if (!rp.acq.dma.settings.decimation(decimation)) {
64+
Serial.println("Error set decimation");
65+
}
66+
67+
if (!rp.acq.dma.settings.decimationQ(&decimation)) {
68+
Serial.println("Error get decimation");
69+
} else {
70+
Serial.print("Decimation = ");
71+
Serial.println(decimation);
72+
}
73+
74+
uint32_t delay = 1024;
75+
76+
if (!rp.acq.dma.settings.delayCh(scpi_rp::ACQ_CH_1, delay)) {
77+
Serial.println("Error set delay");
78+
}
79+
80+
if (!rp.acq.dma.settings.delayChQ(scpi_rp::ACQ_CH_1, &delay)) {
81+
Serial.println("Error get delay");
82+
} else {
83+
Serial.print("Delay = ");
84+
Serial.println(delay);
85+
}
86+
87+
scpi_rp::EACQDataType dt = scpi_rp::ACQ_DT_RAW;
88+
if (!rp.acq.dma.settings.units(dt)) {
89+
Serial.println("Error set data type");
90+
}
91+
92+
if (!rp.acq.dma.settings.unitsQ(&dt)) {
93+
Serial.println("Error get data type");
94+
} else {
95+
Serial.print("Type = ");
96+
Serial.println(dt);
97+
}
98+
99+
// delay (samples) * 2 = bytes
100+
if (!rp.acq.dma.settings.setAddress(scpi_rp::ACQ_CH_1, startAddressReserved,
101+
delay * 2)) {
102+
Serial.println("Error set address");
103+
}
104+
105+
if (!rp.acq.dma.settings.enable(scpi_rp::ACQ_CH_1, true)) {
106+
Serial.println("Error enable");
107+
}
108+
109+
if (!rp.acq.control.start()) {
110+
Serial.println("Error start ADC");
111+
}
112+
113+
if (!rp.acq.trigger.trigger(scpi_rp::ACQ_CH1_PE)) {
114+
Serial.println("Error set trigger");
115+
}
116+
117+
while (1) {
118+
bool state = false;
119+
rp.acq.trigger.stateQ(&state);
120+
if (state) break;
121+
}
122+
123+
while (1) {
124+
bool state = false;
125+
rp.acq.dma.data.fillChQ(scpi_rp::ACQ_CH_1, &state);
126+
if (state) break;
127+
}
128+
129+
if (!rp.acq.control.stop()) {
130+
Serial.println("Error stop ADC");
131+
}
132+
// Read data
133+
134+
uint32_t wp = 0;
135+
if (!rp.acq.dma.data.writePositionChQ(scpi_rp::ACQ_CH_1, &wp)) {
136+
Serial.println("Error get write pointer");
137+
} else {
138+
Serial.print("Write pointer = ");
139+
Serial.println(wp);
140+
}
141+
142+
uint32_t tp = 0;
143+
if (!rp.acq.dma.data.triggerPositionChQ(scpi_rp::ACQ_CH_1, &tp)) {
144+
Serial.println("Error get trigger pointer");
145+
} else {
146+
Serial.print("Trigger pointer = ");
147+
Serial.println(tp);
148+
}
149+
150+
bool last = false;
151+
float sample = 0;
152+
int idx = 0;
153+
while (!last) {
154+
rp.acq.dma.data.dataStartSizeQ(scpi_rp::ACQ_CH_1, tp, delay, &sample,
155+
&last);
156+
Serial.print(idx++);
157+
Serial.print(" - ");
158+
Serial.print(sample, 6);
159+
Serial.println("");
160+
}
161+
}
162+
163+
void loop() { delay(1000); }

src/SCPI_RP.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,6 @@ void SCPIRedPitaya::initUARTStream(Stream *serial) {
3737
acq.settings.setInterface(u);
3838
acq.data.setInterface(u);
3939
acq.trigger.setInterface(u);
40+
acq.dma.settings.setInterface(u);
41+
acq.dma.data.setInterface(u);
4042
}

src/acq/acq_dma_data.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ bool scpi_rp::getAcqDMAWritePointerCh(BaseIO *io, EACQChannel channel,
4242
auto readValue = [&]() {
4343
auto value = io->read();
4444
if (value.isValid) {
45-
*position = atoi(value.value);
45+
*position = io->atou64_dec(value.value);
4646
io->flushCommand(value.next_value);
4747
return true;
4848
}
@@ -70,7 +70,7 @@ bool scpi_rp::getAcqDMATriggerPointerCh(BaseIO *io, EACQChannel channel,
7070
auto readValue = [&]() {
7171
auto value = io->read();
7272
if (value.isValid) {
73-
*position = atoi(value.value);
73+
*position = io->atou64_dec(value.value);
7474
io->flushCommand(value.next_value);
7575
return true;
7676
}
@@ -121,7 +121,7 @@ bool scpi_rp::getAcqDMAGetDataStartCount(BaseIO *io, EACQChannel channel,
121121
return false;
122122
}
123123

124-
if (!io->writeNumber(start)) {
124+
if (!io->writeNumberU64(start)) {
125125
io->writeCommandSeparator();
126126
return false;
127127
}
@@ -131,7 +131,7 @@ bool scpi_rp::getAcqDMAGetDataStartCount(BaseIO *io, EACQChannel channel,
131131
return false;
132132
}
133133

134-
if (!io->writeNumber(size)) {
134+
if (!io->writeNumberU64(size)) {
135135
io->writeCommandSeparator();
136136
return false;
137137
}

src/acq/acq_dma_settings.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "acq_dma_settings.h"
22

3+
#include <Arduino.h>
34
#include <stdint.h>
45
#include <stdio.h>
56
#include <stdlib.h>
@@ -11,7 +12,7 @@ bool scpi_rp::getAcqDMAStart(BaseIO *io, uint32_t *_value) {
1112
auto readValue = [&]() {
1213
auto value = io->read();
1314
if (value.isValid) {
14-
*_value = atoi(value.value);
15+
*_value = io->atou64_dec(value.value);
1516
io->flushCommand(value.next_value);
1617
return true;
1718
}
@@ -29,7 +30,7 @@ bool scpi_rp::getAcqDMASize(BaseIO *io, uint32_t *_value) {
2930
auto readValue = [&]() {
3031
auto value = io->read();
3132
if (value.isValid) {
32-
*_value = atoi(value.value);
33+
*_value = io->atou64_dec(value.value);
3334
io->flushCommand(value.next_value);
3435
return true;
3536
}
@@ -210,15 +211,15 @@ bool scpi_rp::setAcqDMABufferCh(BaseIO *io, EACQChannel channel, uint32_t start,
210211
io->writeCommandSeparator();
211212
return false;
212213
}
213-
if (!io->writeNumber(start)) {
214+
if (!io->writeNumberU64(start)) {
214215
io->writeCommandSeparator();
215216
return false;
216217
}
217218
if (!io->writeStr(",")) {
218219
io->writeCommandSeparator();
219220
return false;
220221
}
221-
if (!io->writeNumber(size)) {
222+
if (!io->writeNumberU64(size)) {
222223
io->writeCommandSeparator();
223224
return false;
224225
}
@@ -239,7 +240,6 @@ bool scpi_rp::setAcqDMAUnits(BaseIO *io, EACQDataType units) {
239240
constexpr char param[] = "VOLTS\r\n";
240241
return io->writeStr(param);
241242
}
242-
243243
io->writeCommandSeparator();
244244
return false;
245245
}

src/scpi/scpi_rp_acq_common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace scpi_rp {
2626

2727
struct SCPIAcqDMACommon {
2828
SCPIAcqDMASettings settings;
29-
SCPIAcqData data;
29+
SCPIAcqDMAData data;
3030
};
3131

3232
struct SCPIAcqCommon {

src/scpi/scpi_rp_acq_dma_settings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class SCPIAcqDMASettings {
6767
/*!
6868
* Sets the decimation used at acquiring signal for the Deep Memory Mode.
6969
* Used only in split trigger mode
70+
* (currently only STEMlab 125-14 4-Input)
7071
* @param channel Fast ADC channel
7172
* @param decimation Sets decimation on selected channel.
7273
* @return Returns true if the command was called successfully, returns false
@@ -77,6 +78,7 @@ class SCPIAcqDMASettings {
7778
/*!
7879
* Returns the decimation used for acquiring signal for the Deep Memory Mode.
7980
* Used only in split trigger mode
81+
* (currently only STEMlab 125-14 4-Input)
8082
* @param channel Fast ADC channel
8183
* @param decimation Returns decimation
8284
* @return Returns true if the command was called successfully, returns false

0 commit comments

Comments
 (0)