]>
cvs.zerfleddert.de Git - rigol/blob - rigol.c
2 Sprite_tms hack to communicate with a Rigol DS1000-series scope using Linux.
3 This code is licensed under the GPL V3.
5 Warning: This code can in theory fubar the communications with the scope to a
6 point where the Linux USB-stack seems to get confused. Do a
7 rmmod uhci_hcd; modprobe uhci_hcd
8 (or alternately: use ohci_hcd) if that happens and you should be fine.
19 #include <readline/readline.h>
20 #include <readline/history.h>
23 //This routine locates a scope by VID/PID and returns an opened handle to it.
24 usb_dev_handle
*find_scope() {
26 struct usb_device
*dev
=NULL
;
29 for (bus
=usb_busses
; bus
; bus
=bus
->next
) {
30 for (dev
=bus
->devices
; dev
; dev
=dev
->next
) {
31 //fprintf(stderr,"Prod/dev: %04X:%04X\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
32 if (dev
->descriptor
.idVendor
==0x400 && dev
->descriptor
.idProduct
==0x5dc) {
40 //Helper-routine: Convert a little-endian 4-byte word to an int
41 void int2chars(unsigned char *buff
,unsigned int a
) {
48 //Helper-routine: Convert an int to little-endian 4-byte word
49 unsigned int chars2int(unsigned char *buff
) {
58 #define MIN(a,b) (((a)<(b))?(a):(b))
60 inline char printable (char ch
)
62 if (ch
< ' ') return '.';
63 if (ch
> '~') return '.';
67 //Debugging: Print a buffers contents in hex
68 void printb (unsigned char *pkt
, int len
)
72 for (i
=0;i
<len
;i
+= 16) {
74 for (j
=0;j
< MIN(len
-i
, 16);j
++) {
75 printf (" %02x", pkt
[i
+j
]);
76 if (j
== 7) printf (" ");
78 if (j
< 7) printf (" ");
82 for (j
=0;j
< MIN(len
-i
, 16);j
++) {
83 printf ("%c", printable (pkt
[i
+j
]));
89 //Send a scpi-command to the scope. The response goes into the buffer
90 //called resp, with a size of resplen. If resp==NULL, no response
92 int sendscpi(usb_dev_handle
*dev
, char* cmd
,
93 unsigned char *resp
, int resplen
) {
96 int cmdlen
= strlen(cmd
);
97 static unsigned char seq
=0;
103 buff
[1]=seq
; buff
[2]=~seq
; //nseq
105 int2chars(buff
+4, cmdlen
);
110 //fprintf(stderr,"Writing header len=%d\n", cmdlen);
112 r
=usb_bulk_write(dev
, 1, (char*)buff
, 12, 1000);
113 //fprintf(stderr,"%i bytes written. Writing cmd\n",r);
114 //printb(cmd, cmdlen);
115 r
=usb_bulk_write(dev
, 1, cmd
, cmdlen
, 1000);
116 //fprintf(stderr,"%i bytes written.\n",r);
117 if (resp
!= NULL
&& resplen
!= 0) {
121 buff
[1]=seq
; buff
[2]=~seq
; //nseq
122 int2chars(buff
+4,0x40);
127 //fprintf(stderr,"Writing resp req header\n");
129 r
=usb_bulk_write(dev
, 1, (char*)buff
, 12, 1000);
130 //fprintf(stderr,"%i bytes written. Reading response hdr\n",r);
131 r
=usb_bulk_read(dev
, 2, (char*)buff
, 0x40, 1000);
133 len
=chars2int(buff
+4);
134 //fprintf(stderr,"%i bytes read. Resplen=%i\n",r,len);
135 for (i
=0; i
<(r
-12); i
++) {
136 if (i
<resplen
) resp
[i
] = buff
[i
+12];
140 //fprintf(stderr," Reading response:\n");
141 if (resplen
<len
) len
=resplen
;
142 r
=usb_bulk_read(dev
, 2, (char*)resp
+(0x40-12), len
-(0x40-12),1000);
143 //fprintf(stderr,"%i bytes read, wanted %i.\n", r, len-(0x40-12));
151 //Initialize the scope.
152 void initscope(usb_dev_handle
*dev
) {
154 unsigned char buff
[10];
155 usb_claim_interface(dev
,0);
156 //The following code isn't really necessary, the program works
158 r
=usb_control_msg(dev
, 0xC8, 9, 0, 0, (char*)buff
, 4, 1000);
160 fprintf (stderr
, "Error %d sending init message: %s\n",
162 fprintf (stderr
, "Do you have permission on the USB device?\n");
165 if (chars2int(buff
)!=0x40004dc) {
166 fprintf(stderr
,"Init: buff[%i]=%x\n",r
,chars2int(buff
));
171 #define HAVE_READLINE
172 #ifndef HAVE_READLINE
174 char *readline (prompt
)
182 if (fgets (buf
, 1023, stdin
) == NULL
) {
187 if (buf
[l
-1] == '\n') {
193 void add_history (char *buf
)
199 void do_plot (struct usb_dev_handle
*sc
)
201 unsigned char ch1
[1024], ch2
[1024];
204 static FILE *gnuplot
=NULL
;
207 l
= sendscpi(sc
, ":WAV:DATA? CHANEL1", ch1
, 1024);
210 printf ("hmm. didnt' get 1024 bytes. \n");
213 l
= sendscpi(sc
, ":WAV:DATA? CHANNEL2", ch2
, 1024);
216 printf ("hmm. didnt' get 1024 bytes. \n");
220 gnuplot
= popen ("gnuplot", "w");
223 fp
= fopen ("temp.dat", "w");
224 for (i
=0xd4;i
<0x32c;i
++)
225 //for (i=0;i<0x400;i++)
226 fprintf (fp
, "%d %d\n", 255 - ch1
[i
], 255 - ch2
[i
]);
229 fprintf (gnuplot
, "plot 'temp.dat' using 1 with lines, 'temp.dat' using 2 with lines\n");
236 double get_float_from_scope (struct usb_dev_handle
*sc
, char *var
)
238 unsigned char buf
[1024];
242 l
= sendscpi(sc
, var
, buf
, 1024);
244 sscanf ((char*)buf
, "%lf", &temp
);
251 void do_get_buf (struct usb_dev_handle
*sc
)
256 unsigned char ch1
[1024];
257 unsigned char data
[512*1024];
260 sendscpi (sc
, ":STOP", NULL
, 0);
262 sampfreq
= get_float_from_scope (sc
, ":ACQ:SAMP?");
264 printf ("Got sampling freq: %g\n", sampfreq
);
266 sprintf (buf
, ":TIM:SCAL %.15f", 50 / sampfreq
);
267 printf ("sending scale cmd: %s\n", buf
);
268 sendscpi (sc
, buf
, NULL
, 0);
273 for (i
=-254*1024;i
< 254*1024;i
+= 600) {
274 sprintf (buf
, ":TIM:OFFSET %.15f", i
/ sampfreq
);
275 printf ("Sending offset cmd: %s\n", buf
);
276 sendscpi (sc
, buf
, NULL
, 0);
278 l
= sendscpi(sc
, ":WAV:DATA? CHANEL1", ch1
, 1024);
281 printf ("hmm. didnt' get 1024 bytes. \n");
285 data
[bp
++] = ch1
[j
+0xd4];
287 printf ("Got %d bytes of data. \n", bp
);
289 fp
= fopen ("ch1.dump", "w");
290 fwrite (data
, bp
, 1, fp
);
293 sendscpi (sc
, ":TIM:OFFSET 0", NULL
, 0);
298 int main(int argc
, char **argv
)
300 struct usb_dev_handle
*sc
;
306 //Locate and open the scope
309 printf("No scope found.\n");
312 printf("Scope found.\n");
316 buff
= malloc (1024*1024);
318 scpi
= readline ("> ");
321 if (strlen (scpi
) == 0) {
328 if (strncmp (scpi
, "quit", 4) == 0) break;
329 if (strncmp (scpi
, "plot", 4) == 0) {
333 if (strncmp (scpi
, "databuf", 4) == 0) {
339 //printf ("got buf(%d): ", l);
340 //printb (scpi, l+2);
341 if (strchr (scpi
, '?')) {
342 //printf ("Expecting reply\n");
343 l
= sendscpi(sc
, scpi
, buff
, 1024*1024);
344 //printf ("Got replylen = %d.\n", l);
345 buff
[l
] = 0; //zero-terminate
348 //printf ("No reply expected\n");
349 l
=sendscpi(sc
,scpi
,NULL
,0);
353 //Disable keylock, so the user doesn't have to press the 'force'-button
354 l
=sendscpi(sc
, ":KEY:LOCK DISABLE",NULL
,0);
357 usb_release_interface(sc
,0);