-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathps3000acon.c
1340 lines (1117 loc) · 37.8 KB
/
ps3000acon.c
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
*
* Filename: ps3000acon.c
*
* Description:
* This is a console mode program that demonstrates how to use the
* PicoScope 3000 Series (ps3000a) driver functions.
*
* Supported PicoScope models:
*
* PicoScope 3204A/B/D
* PicoScope 3205A/B/D
* PicoScope 3206A/B/D
* PicoScope 3207A/B
* PicoScope 3204 MSO & D MSO
* PicoScope 3205 MSO & D MSO
* PicoScope 3206 MSO & D MSO
* PicoScope 3404A/B/D/D MSO
* PicoScope 3405A/B/D/D MSO
* PicoScope 3406A/B/D/D MSO
*
* Examples:
* Collect a block of samples immediately
* Collect a block of samples when a trigger event occurs
* Collect a block of samples using Equivalent Time Sampling (ETS)
* Collect samples using a rapid block capture with trigger
* Collect a stream of data immediately
* Collect a stream of data when a trigger event occurs
* Set Signal Generator, using standard or custom signals
* Change timebase & voltage scales
* Display data in mV or ADC counts
* Handle power source changes (PicoScope 34XXA/B, 32XX D/D MSO, &
* 34XX D/D MSO devices only)
*
* Digital Examples (MSO variants only):
* Collect a block of digital samples immediately
* Collect a block of digital samples when a trigger event occurs
* Collect a block of analogue & digital samples when analogue AND digital trigger events occurs
* Collect a block of analogue & digital samples when analogue OR digital trigger events occurs
* Collect a stream of digital data immediately
* Collect a stream of digital data and show aggregated values
*
*
* To build this application:
*
* If Microsoft Visual Studio (including Express) is being used:
*
* Select the solution configuration (Debug/Release) and platform (x86/x64)
* Ensure that the 32-/64-bit ps3000a.lib can be located
* Ensure that the ps3000aApi.h and PicoStatus.h files can be located
*
* Otherwise:
*
* Set up a project for a 32-/64-bit console mode application
* Add this file to the project
* Add 32-/64-bit ps3000a.lib to the project
* Add ps3000aApi.h and PicoStatus.h to the project
* Build the project
*
* Linux platforms:
*
* Ensure that the libps3000a driver package has been installed using the
* instructions from https://www.picotech.com/downloads/linux
*
* Place this file in the same folder as the files from the linux-build-files
* folder. In a terminal window, use the following commands to build
* the ps3000acon application:
*
* ./autogen.sh <ENTER>
* make <ENTER>
*
* Copyright (C) 2011 - 2017 Pico Technology Ltd. See LICENSE file for terms.
*
******************************************************************************/
#include <stdio.h>
/* Headers for Windows */
#ifdef _WIN32
#include "windows.h"
#include <conio.h>
#include "ps3000aApi.h"
#else
#include <sys/types.h>
#include <string.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <libps3000a-1.1/ps3000aApi.h>
#ifndef PICO_STATUS
#include <libps3000a-1.1/PicoStatus.h>
#endif
#include <fcntl.h>
#include <sys/signal.h>
#include <sys/time.h>
#include <stdint.h>
#include <time.h>
#define Sleep(a) usleep(1000*a)
#define scanf_s scanf
#define fscanf_s fscanf
#define memcpy_s(a,b,c,d) memcpy(a,c,d)
typedef enum enBOOL{FALSE,TRUE} BOOL;
int serial_port = 0;
/* opens a com-port and returns the file pointer */
int open_port (char * PORT, int speed)
{
struct termios tio;
struct sigaction saio; /* definition of signal action */
int fd; // Filedeskriptor
fd = open (PORT, O_RDWR | O_NOCTTY ); // O_NONBLOCK
if (fd < 0) // negative Zahl bei fehler!
{
fprintf(stderr, "Open_port: unable to open %s\n", PORT);
exit (1); // irreparebel, verlasse Programm
}
memset( &tio, 0, sizeof(tio) );
tio.c_cflag = CS8|CREAD|CLOCAL; /* 8n1 */
tio.c_cc[VMIN] = 0;
tio.c_cc[VTIME] = 0;
if(speed == 9600) {
cfsetospeed( &tio, B9600 );
cfsetispeed( &tio, B9600 );
} else if (speed == 19200) {
cfsetospeed( &tio, B19200 );
cfsetispeed( &tio, B19200 );
} else if (speed == 115200) {
cfsetospeed( &tio, B115200 );
cfsetispeed( &tio, B115200 );
} else {
printf("Baudrate not supported!\n");
}
cfmakeraw(&tio);
tcflush( fd, TCIFLUSH );
tcsetattr( fd, TCSANOW, &tio );
return fd;
}
/* A function to detect a keyboard press on Linux */
int32_t _getch()
{
struct termios oldt, newt;
int32_t ch;
int32_t bytesWaiting;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
setbuf(stdin, NULL);
do {
ioctl(STDIN_FILENO, FIONREAD, &bytesWaiting);
if (bytesWaiting)
getchar();
} while (bytesWaiting);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
return ch;
}
int32_t _kbhit()
{
struct termios oldt, newt;
int32_t bytesWaiting;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
setbuf(stdin, NULL);
ioctl(STDIN_FILENO, FIONREAD, &bytesWaiting);
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
return bytesWaiting;
}
int32_t fopen_s(FILE ** a, const char * b, const char * c)
{
FILE * fp = fopen(b,c);
*a = fp;
return (fp>0)?0:-1;
}
/* A function to get a single character on Linux */
#define max(a,b) ((a) > (b) ? a : b)
#define min(a,b) ((a) < (b) ? a : b)
#endif
#define PREF4 __stdcall
int32_t cycles = 0;
#define TRACES_TO_SKIP 50
#define BUFFER_SIZE 200000
#define TRACE_COUNT 3000
#define QUAD_SCOPE 4
#define DUAL_SCOPE 2
// AWG Parameters
#define AWG_DAC_FREQUENCY 20e6
#define AWG_DAC_FREQUENCY_PS3207B 100e6
#define AWG_PHASE_ACCUMULATOR 4294967296.0
typedef enum
{
ANALOGUE,
DIGITAL,
AGGREGATED,
MIXED
}MODE;
typedef struct
{
int16_t DCcoupled;
int16_t range;
int16_t enabled;
}CHANNEL_SETTINGS;
typedef enum
{
SIGGEN_NONE = 0,
SIGGEN_FUNCTGEN = 1,
SIGGEN_AWG = 2
} SIGGEN_TYPE;
typedef struct tTriggerDirections
{
PS3000A_THRESHOLD_DIRECTION channelA;
PS3000A_THRESHOLD_DIRECTION channelB;
PS3000A_THRESHOLD_DIRECTION channelC;
PS3000A_THRESHOLD_DIRECTION channelD;
PS3000A_THRESHOLD_DIRECTION ext;
PS3000A_THRESHOLD_DIRECTION aux;
}TRIGGER_DIRECTIONS;
typedef struct tPwq
{
PS3000A_PWQ_CONDITIONS_V2 * conditions;
int16_t nConditions;
PS3000A_THRESHOLD_DIRECTION direction;
uint32_t lower;
uint32_t upper;
PS3000A_PULSE_WIDTH_TYPE type;
}PWQ;
typedef struct
{
int16_t handle;
int8_t model[8];
PS3000A_RANGE firstRange ;
PS3000A_RANGE lastRange;
int16_t channelCount;
int16_t maxValue;
int16_t sigGen;
int16_t ETS;
int32_t AWGFileSize;
CHANNEL_SETTINGS channelSettings [PS3000A_MAX_CHANNELS];
int16_t digitalPorts;
}UNIT;
uint32_t timebase = 5;
int16_t oversample = 1;
BOOL scaleVoltages = TRUE;
uint16_t inputRanges [PS3000A_MAX_RANGES] = {10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000};
BOOL g_ready = FALSE;
int32_t g_times [PS3000A_MAX_CHANNELS] = {0, 0, 0, 0};
int16_t g_timeUnit;
int32_t g_sampleCount;
uint32_t g_startIndex;
int16_t g_autoStopped;
int16_t g_trig = 0;
uint32_t g_trigAt = 0;
char BlockFile[200] = "block.txt";
char DigiBlockFile[200] = "digiBlock.txt";
char StreamFile[200] = "stream.txt";
char Filename_custom[200] = "test.txt";
typedef struct tBufferInfo
{
UNIT * unit;
MODE mode;
int16_t **driverBuffers;
int16_t **appBuffers;
int16_t **driverDigBuffers;
int16_t **appDigBuffers;
} BUFFER_INFO;
/****************************************************************************
* Streaming callback
* Used by ps3000a data streaming collection calls, on receipt of data.
* Used to set global flags etc. checked by user routines
****************************************************************************/
void PREF4 callBackStreaming( int16_t handle,
int32_t noOfSamples,
uint32_t startIndex,
int16_t overflow,
uint32_t triggerAt,
int16_t triggered,
int16_t autoStop,
void *pParameter)
{
int32_t channel;
BUFFER_INFO * bufferInfo = NULL;
if (pParameter != NULL)
{
bufferInfo = (BUFFER_INFO *) pParameter;
}
// used for streaming
g_sampleCount = noOfSamples;
g_startIndex = startIndex;
g_autoStopped = autoStop;
// flag to say done reading data
g_ready = TRUE;
// flags to show if & where a trigger has occurred
g_trig = triggered;
g_trigAt = triggerAt;
if (bufferInfo != NULL && noOfSamples)
{
if (bufferInfo->mode == ANALOGUE)
{
for (channel = 0; channel < bufferInfo->unit->channelCount; channel++)
{
if (bufferInfo->unit->channelSettings[channel].enabled)
{
if (bufferInfo->appBuffers && bufferInfo->driverBuffers)
{
if (bufferInfo->appBuffers[channel * 2] && bufferInfo->driverBuffers[channel * 2])
{
memcpy_s (&bufferInfo->appBuffers[channel * 2][startIndex], noOfSamples * sizeof(int16_t),
&bufferInfo->driverBuffers[channel * 2][startIndex], noOfSamples * sizeof(int16_t));
}
if (bufferInfo->appBuffers[channel * 2 + 1] && bufferInfo->driverBuffers[channel * 2 + 1])
{
memcpy_s (&bufferInfo->appBuffers[channel * 2 + 1][startIndex], noOfSamples * sizeof(int16_t),
&bufferInfo->driverBuffers[channel * 2 + 1][startIndex], noOfSamples * sizeof(int16_t));
}
}
}
}
}
else if (bufferInfo->mode == AGGREGATED)
{
for (channel = 0; channel < bufferInfo->unit->digitalPorts; channel++)
{
if (bufferInfo->appDigBuffers && bufferInfo->driverDigBuffers)
{
if (bufferInfo->appDigBuffers[channel * 2] && bufferInfo->driverDigBuffers[channel * 2])
{
memcpy_s (&bufferInfo->appDigBuffers[channel * 2][startIndex], noOfSamples * sizeof(int16_t),
&bufferInfo->driverDigBuffers[channel * 2][startIndex], noOfSamples * sizeof(int16_t));
}
if (bufferInfo->appDigBuffers[channel * 2 + 1] && bufferInfo->driverDigBuffers[channel * 2 + 1])
{
memcpy_s (&bufferInfo->appDigBuffers[channel * 2 + 1][startIndex], noOfSamples * sizeof(int16_t),
&bufferInfo->driverDigBuffers[channel * 2 + 1][startIndex], noOfSamples * sizeof(int16_t));
}
}
}
}
else if (bufferInfo->mode == DIGITAL)
{
for (channel = 0; channel < bufferInfo->unit->digitalPorts; channel++)
{
if (bufferInfo->appDigBuffers && bufferInfo->driverDigBuffers)
{
if (bufferInfo->appDigBuffers[channel] && bufferInfo->driverDigBuffers[channel])
{
memcpy_s (&bufferInfo->appDigBuffers[channel][startIndex], noOfSamples * sizeof(int16_t),
&bufferInfo->driverDigBuffers[channel][startIndex], noOfSamples * sizeof(int16_t));
}
}
}
}
}
}
/****************************************************************************
* Block Callback
* used by ps3000a data block collection calls, on receipt of data.
* used to set global flags etc checked by user routines
****************************************************************************/
void PREF4 callBackBlock( int16_t handle, PICO_STATUS status, void * pParameter)
{
if (status != PICO_CANCELLED)
{
g_ready = TRUE;
}
}
/****************************************************************************
* setDefaults - restore default settings
****************************************************************************/
void setDefaults(UNIT * unit)
{
int32_t i;
PICO_STATUS status;
status = ps3000aSetEts(unit->handle, PS3000A_ETS_OFF, 0, 0, NULL); // Turn off ETS
printf(status?"SetDefaults:ps3000aSetEts------ 0x%08lx \n":"", status);
for (i = 0; i < unit->channelCount; i++) // reset channels to most recent settings
{
status = ps3000aSetChannel(unit->handle, (PS3000A_CHANNEL)(PS3000A_CHANNEL_A + i),
unit->channelSettings[PS3000A_CHANNEL_A + i].enabled,
(PS3000A_COUPLING)unit->channelSettings[PS3000A_CHANNEL_A + i].DCcoupled,
(PS3000A_RANGE)unit->channelSettings[PS3000A_CHANNEL_A + i].range, 0);
printf(status?"SetDefaults:ps3000aSetChannel------ 0x%08lx \n":"", status);
}
}
/****************************************************************************
* setDigitals - enable or disable Digital Channels
****************************************************************************/
PICO_STATUS setDigitals(UNIT *unit, int16_t state)
{
PICO_STATUS status;
int16_t logicLevel;
float logicVoltage = 1.5;
int16_t maxLogicVoltage = 5;
int16_t timebase = 4;
int16_t port;
// Set logic threshold
logicLevel = (int16_t) ((logicVoltage / maxLogicVoltage) * PS3000A_MAX_LOGIC_LEVEL);
// Enable Digital ports
for (port = PS3000A_DIGITAL_PORT0; port <= PS3000A_DIGITAL_PORT1; port++)
{
status = ps3000aSetDigitalPort(unit->handle, (PS3000A_DIGITAL_PORT)port, state, logicLevel);
printf(status?"SetDigitals:PS3000ASetDigitalPort(Port 0x%X) ------ 0x%08lx \n":"", port, status);
}
return status;
}
/****************************************************************************
* disableAnalogue - Disable Analogue Channels
****************************************************************************/
PICO_STATUS disableAnalogue(UNIT *unit)
{
PICO_STATUS status;
int16_t ch;
// Turn off analogue channels, keeping settings
for (ch = 0; ch < unit->channelCount; ch++)
{
unit->channelSettings[ch].enabled = FALSE;
status = ps3000aSetChannel(unit->handle, (PS3000A_CHANNEL) ch, unit->channelSettings[ch].enabled, (PS3000A_COUPLING) unit->channelSettings[ch].DCcoupled,
(PS3000A_RANGE) unit->channelSettings[ch].range, 0);
if(status != PICO_OK)
{
printf("disableAnalogue:ps3000aSetChannel(channel %d) ------ 0x%08lx \n", ch, status);
}
}
return status;
}
/****************************************************************************
* restoreAnalogueSettings - Restores Analogue Channel settings
****************************************************************************/
PICO_STATUS restoreAnalogueSettings(UNIT *unit)
{
PICO_STATUS status;
int16_t ch;
// Turn on analogue channels using previous settings
for (ch = 0; ch < unit->channelCount; ch++)
{
status = ps3000aSetChannel(unit->handle, (PS3000A_CHANNEL) ch, unit->channelSettings[ch].enabled, (PS3000A_COUPLING) unit->channelSettings[ch].DCcoupled,
(PS3000A_RANGE) unit->channelSettings[ch].range, 0);
if(status != PICO_OK)
{
printf("restoreAnalogueSettings:ps3000aSetChannel(channel %d) ------ 0x%08lx \n", ch, status);
}
}
return status;
}
/****************************************************************************
* adc_to_mv
*
* Convert an 16-bit ADC count into millivolts
****************************************************************************/
int32_t adc_to_mv(int32_t raw, int32_t ch, UNIT * unit)
{
return (raw * inputRanges[ch]) / unit->maxValue;
}
/****************************************************************************
* mv_to_adc
*
* Convert a millivolt value into a 16-bit ADC count
*
* (useful for setting trigger thresholds)
****************************************************************************/
int16_t mv_to_adc(int16_t mv, int16_t ch, UNIT * unit)
{
return (mv * unit->maxValue) / inputRanges[ch];
}
/****************************************************************************************
* changePowerSource - function to handle switches between +5 V supply, and USB-only power
* Only applies to PicoScope 34xxA/B/D/D MSO units
******************************************************************************************/
PICO_STATUS changePowerSource(int16_t handle, PICO_STATUS status)
{
char ch;
switch (status)
{
case PICO_POWER_SUPPLY_NOT_CONNECTED: // User must acknowledge they want to power via USB
do
{
printf("\n5 V power supply not connected");
printf("\nDo you want to run using USB only Y/N?\n");
ch = toupper(_getch());
if(ch == 'Y')
{
printf("\nPowering the unit via USB\n");
status = ps3000aChangePowerSource(handle, PICO_POWER_SUPPLY_NOT_CONNECTED); // Tell the driver that's ok
if (status == PICO_POWER_SUPPLY_UNDERVOLTAGE)
{
status = changePowerSource(handle, status);
}
}
}
while(ch != 'Y' && ch != 'N');
printf(ch == 'N'?"Please use the +5 V power supply to power this unit\n":"");
break;
case PICO_POWER_SUPPLY_CONNECTED:
printf("\nUsing +5V power supply voltage\n");
status = ps3000aChangePowerSource(handle, PICO_POWER_SUPPLY_CONNECTED); // Tell the driver we are powered from +5 V supply
break;
case PICO_USB3_0_DEVICE_NON_USB3_0_PORT:
printf("\nUSB 3.0 device on non-USB 3.0 port.\n");
status = ps3000aChangePowerSource(handle, PICO_USB3_0_DEVICE_NON_USB3_0_PORT); // Tell the driver that it is ok for the device to be on a non-USB 3.0 port
break;
case PICO_POWER_SUPPLY_UNDERVOLTAGE:
do
{
printf("\nUSB not supplying required voltage");
printf("\nPlease plug in the +5 V power supply\n");
printf("\nHit any key to continue, or Esc to exit...\n");
ch = _getch();
if (ch == 0x1B) // ESC key
{
exit(0);
}
else
{
status = ps3000aChangePowerSource(handle, PICO_POWER_SUPPLY_CONNECTED); // Tell the driver that's ok
}
}
while (status == PICO_POWER_SUPPLY_REQUEST_INVALID);
break;
}
return status;
}
/****************************************************************************
* clearDataBuffers
*
* stops GetData writing values to memory that has been released
****************************************************************************/
PICO_STATUS clearDataBuffers(UNIT * unit)
{
int32_t i;
PICO_STATUS status;
for (i = 0; i < unit->channelCount; i++)
{
if((status = ps3000aSetDataBuffers(unit->handle, (PS3000A_CHANNEL) i, NULL, NULL, 0, 0, PS3000A_RATIO_MODE_NONE)) != PICO_OK)
{
printf("ClearDataBuffers:ps3000aSetDataBuffers(channel %d) ------ 0x%08lx \n", i, status);
}
}
for (i= 0; i < unit->digitalPorts; i++)
{
if((status = ps3000aSetDataBuffer(unit->handle, (PS3000A_CHANNEL) (i + PS3000A_DIGITAL_PORT0), NULL, 0, 0, PS3000A_RATIO_MODE_NONE))!= PICO_OK)
{
printf("ClearDataBuffers:ps3000aSetDataBuffer(port 0x%X) ------ 0x%08lx \n", i + PS3000A_DIGITAL_PORT0, status);
}
}
return status;
}
/****************************************************************************
* blockDataHandler
* - Used by all block data routines
* - acquires data (user sets trigger mode before calling), displays 10 items
* and saves all to block.txt
* Input :
* - unit : the unit to use.
* - text : the text to display before the display of data slice
* - offset : the offset into the data buffer to start the display's slice.
****************************************************************************/
void blockDataHandler(UNIT * unit, char * text, int32_t offset, MODE mode)
{
int16_t retry;
int16_t bit;
uint16_t bitValue;
uint16_t digiValue;
int16_t * buffers[PS3000A_MAX_CHANNEL_BUFFERS];
int16_t * digiBuffer[PS3000A_MAX_DIGITAL_PORTS];
int32_t i, j;
int32_t timeInterval;
int32_t sampleCount = BUFFER_SIZE;
int32_t maxSamples;
int32_t timeIndisposed;
static uint32_t blocks_written=0;
static char plaintext[18] = {0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,'\n', 0};
FILE * fp = NULL;
FILE * plaintext_fp = NULL;
FILE * digiFp = NULL;
PICO_STATUS status;
PS3000A_RATIO_MODE ratioMode = PS3000A_RATIO_MODE_NONE;
if (mode == ANALOGUE || mode == MIXED) // Analogue or (MSO Only) MIXED
{
for (i = 0; i < unit->channelCount; i++)
{
if(unit->channelSettings[i].enabled)
{
buffers[i * 2] = (int16_t*) calloc(sampleCount, sizeof(int16_t));
buffers[i * 2 + 1] = (int16_t*) calloc(sampleCount, sizeof(int16_t));
status = ps3000aSetDataBuffers(unit->handle, (PS3000A_CHANNEL)i, buffers[i * 2], buffers[i * 2 + 1], sampleCount, 0, ratioMode);
printf(status?"BlockDataHandler:ps3000aSetDataBuffers(channel %d) ------ 0x%08lx \n":"", i, status);
}
}
}
if (mode == DIGITAL || mode == MIXED) // (MSO Only) Digital or MIXED
{
for (i= 0; i < unit->digitalPorts; i++)
{
digiBuffer[i] = (int16_t*) calloc(sampleCount, sizeof(int16_t));
status = ps3000aSetDataBuffer(unit->handle, (PS3000A_CHANNEL) (i + PS3000A_DIGITAL_PORT0), digiBuffer[i], sampleCount, 0, ratioMode);
printf(status?"BlockDataHandler:ps3000aSetDataBuffer(port 0x%X) ------ 0x%08lx \n":"", i + PS3000A_DIGITAL_PORT0, status);
}
}
/* Find the maximum number of samples and the time interval (in nanoseconds) */
while (ps3000aGetTimebase(unit->handle, timebase, sampleCount, &timeInterval, oversample, &maxSamples, 0))
{
timebase++;
}
printf("\nTimebase: %lu Sample interval: %ld ns \n", timebase, timeInterval);
/* Start the device collecting, then wait for completion*/
g_ready = FALSE;
do
{
retry = 0;
status = ps3000aRunBlock(unit->handle, 0, sampleCount, timebase, oversample, &timeIndisposed, 0, callBackBlock, NULL);
if (status != PICO_OK)
{
if (status == PICO_POWER_SUPPLY_CONNECTED || status == PICO_POWER_SUPPLY_NOT_CONNECTED ||
status == PICO_POWER_SUPPLY_UNDERVOLTAGE) // PicoScope 340XA/B/D/D MSO devices...+5 V PSU connected or removed
{
status = changePowerSource(unit->handle, status);
retry = 1;
}
else
{
printf("BlockDataHandler:ps3000aRunBlock ------ 0x%08lx \n", status);
return;
}
}
}
while (retry);
printf("Waiting for trigger...\n");
uint8_t response[16];
char plaintextfile[100];
blocks_written++;
sprintf(plaintextfile, "%s_%dsamples_%dtraces_%dns_Plaintext.txt",Filename_custom,BUFFER_SIZE, TRACE_COUNT, timeInterval);
fopen_s(&plaintext_fp, plaintextfile, "a");
if(plaintext_fp != NULL)
{
for(int i=0; i<16; i++)
{
plaintext[i] = ' ' + rand() % (127-' '); // generate random ascii characters
}
write(serial_port, plaintext, 17);
usleep(30000);
read(serial_port, response, 16);
for(int i=0;i<16;i++) {
printf("%d %d\n", plaintext[i], response[i]);
}
if(!memcmp(plaintext, response, 16))
printf("------------------------data valid-----------------------------\n");
if(blocks_written > TRACES_TO_SKIP)
fprintf(plaintext_fp, "%s", plaintext);
}
while (!g_ready) // while (!g_ready && !_kbhit()) including keypress wait
{
Sleep(0);
}
if (g_ready)
{
status = ps3000aGetValues(unit->handle, 0, (uint32_t*) &sampleCount, 1, ratioMode, 0, NULL);
if (status != PICO_OK)
{
if (status == PICO_POWER_SUPPLY_CONNECTED || status == PICO_POWER_SUPPLY_NOT_CONNECTED || status == PICO_POWER_SUPPLY_UNDERVOLTAGE)
{
if (status == PICO_POWER_SUPPLY_UNDERVOLTAGE)
{
changePowerSource(unit->handle, status);
}
else
{
printf("\nPower Source Changed. Data collection aborted.\n");
}
}
else
{
printf("BlockDataHandler:ps3000aGetValues ------ 0x%08lx \n", status);
}
}
else
{
if (mode == ANALOGUE || mode == MIXED) // If we're doing analogue or MIXED
{
sampleCount = min(sampleCount, BUFFER_SIZE);
sprintf(BlockFile, "%s_%dsamples_%dtraces_%dns.txt",Filename_custom,BUFFER_SIZE, TRACE_COUNT, timeInterval);
fopen_s(&fp, BlockFile, "a");
if (fp != NULL && blocks_written > TRACES_TO_SKIP) // skip the first few traces to account for the offset change
{
printf("Writing Block %d\n", blocks_written);
for (i = 0; i < sampleCount; i++)
{
if (unit->channelSettings[0].enabled) // Print channel A to file (powertrace)
{
fprintf( fp,
"%d",
buffers[0][i]);
}
fprintf(fp, ",");
}
fprintf(fp, "\n");
}
else
{
printf( "Cannot open the file %s for writing.\n"
"Please ensure that you have permission to access.\n", BlockFile);
}
}
}
}
else
{
printf("\nData collection aborted.\n");
_getch();
}
if ((status = ps3000aStop(unit->handle)) != PICO_OK)
{
printf("BlockDataHandler:ps3000aStop ------ 0x%08lx \n", status);
}
if (fp != NULL)
{
fclose(fp);
}
if (digiFp != NULL)
{
fclose(digiFp);
}
if (plaintext_fp != NULL)
fclose(plaintext_fp);
if (mode == ANALOGUE || mode == MIXED) // Only if we allocated these buffers
{
for (i = 0; i < unit->channelCount; i++)
{
if(unit->channelSettings[i].enabled)
{
free(buffers[i * 2]);
free(buffers[i * 2 + 1]);
}
}
}
if (mode == DIGITAL || mode == MIXED) // Only if we allocated these buffers
{
for (i = 0; i < unit->digitalPorts; i++)
{
free(digiBuffer[i]);
}
}
clearDataBuffers(unit);
}
/****************************************************************************
* setTrigger
*
* - Used to call aall the functions required to set up triggering
*
***************************************************************************/
PICO_STATUS setTrigger( UNIT * unit,
struct tPS3000ATriggerChannelProperties * channelProperties,
int16_t nChannelProperties,
PS3000A_TRIGGER_CONDITIONS_V2 * triggerConditionsV2,
int16_t nTriggerConditions,
TRIGGER_DIRECTIONS * directions,
struct tPwq * pwq,
uint32_t delay,
int16_t auxOutputEnabled,
int32_t autoTriggerMs,
PS3000A_DIGITAL_CHANNEL_DIRECTIONS * digitalDirections,
int16_t nDigitalDirections)
{
PICO_STATUS status;
if ((status = ps3000aSetTriggerChannelProperties(unit->handle,
channelProperties,
nChannelProperties,
auxOutputEnabled,
autoTriggerMs)) != PICO_OK)
{
printf("SetTrigger:ps3000aSetTriggerChannelProperties ------ Ox%08lx \n", status);
return status;
}
if ((status = ps3000aSetTriggerChannelConditionsV2(unit->handle, triggerConditionsV2, nTriggerConditions)) != PICO_OK)
{
printf("SetTrigger:ps3000aSetTriggerChannelConditions ------ 0x%08lx \n", status);
return status;
}
if ((status = ps3000aSetTriggerChannelDirections(unit->handle,
directions->channelA,
directions->channelB,
directions->channelC,
directions->channelD,
directions->ext,
directions->aux)) != PICO_OK)
{
printf("SetTrigger:ps3000aSetTriggerChannelDirections ------ 0x%08lx \n", status);
return status;
}
if ((status = ps3000aSetTriggerDelay(unit->handle, delay)) != PICO_OK)
{
printf("SetTrigger:ps3000aSetTriggerDelay ------ 0x%08lx \n", status);
return status;
}
if((status = ps3000aSetPulseWidthQualifierV2(unit->handle,
pwq->conditions,
pwq->nConditions,
pwq->direction,
pwq->lower,
pwq->upper,
pwq->type)) != PICO_OK)
{
printf("SetTrigger:ps3000aSetPulseWidthQualifier ------ 0x%08lx \n", status);
return status;
}
if (unit->digitalPorts)
{
if((status = ps3000aSetTriggerDigitalPortProperties(unit->handle, digitalDirections, nDigitalDirections)) != PICO_OK)
{
printf("SetTrigger:ps3000aSetTriggerDigitalPortProperties ------ 0x%08lx \n", status);
return status;
}
}
return status;
}
/****************************************************************************
* collectBlockTriggered
* this function demonstrates how to collect a single block of data from the
* unit, when a trigger event occurs.
****************************************************************************/
void collectBlockTriggered(UNIT * unit)
{
/*
PS3000A_50MV: 50 mV
PS3000A_100MV: 100 mV
PS3000A_200MV: 200 mV
PS3000A_500MV: 500 mV
PS3000A_1V: 1 V
PS3000A_2V: 2 V
PS3000A_5V: 5 V
PS3000A_10V: 10 V
PS3000A_20V: 20 V
*/
unit->channelSettings[PS3000A_CHANNEL_A].enabled = 1;
unit->channelSettings[PS3000A_CHANNEL_A].DCcoupled = FALSE;
unit->channelSettings[PS3000A_CHANNEL_A].range = PS3000A_50MV;
unit->channelSettings[PS3000A_CHANNEL_B].enabled = 1;
unit->channelSettings[PS3000A_CHANNEL_B].DCcoupled = TRUE;
unit->channelSettings[PS3000A_CHANNEL_B].range = PS3000A_1V;
int16_t triggerVoltage = mv_to_adc(300, unit->channelSettings[PS3000A_CHANNEL_B].range, unit);
struct tPS3000ATriggerChannelProperties sourceDetails = { triggerVoltage,
256 * 10,
triggerVoltage,
256 * 10,
PS3000A_CHANNEL_B,