]>
git.zerfleddert.de Git - rigol/blob - usbtmc.c
eb7737ad287efda71b1f49008d1835da374c6595
10 #define USB_TIMEOUT 50000
12 #if BYTE_ORDER == LITTLE_ENDIAN
14 #elif BYTE_ORDER == BIG_ENDIAN
15 #define LE32(x) ((uint32_t)((((uint32_t)x)>>24) | ((((uint32_t)x)>>8) & 0xff00) | ((((uint32_t)x)<<8) & 0xff0000) | (((uint32_t)x)<<24)))
17 #error BYTE_ORDER not defined/known!
20 //Helper-routine: Convert a little-endian 4-byte word to an int
21 static void int2chars(unsigned char *buff
,unsigned int a
) {
28 //Helper-routine: Convert an int to little-endian 4-byte word
29 unsigned int chars2int(unsigned char *buff
) {
38 //This routine locates a scope by VID/PID and returns an opened handle to it.
39 static usb_dev_handle
*usbtmc_find_scope() {
41 struct usb_device
*dev
=NULL
;
44 for (bus
=usb_busses
; bus
; bus
=bus
->next
) {
45 for (dev
=bus
->devices
; dev
; dev
=dev
->next
) {
46 //fprintf(stderr,"Prod/dev: %04X:%04X\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
47 if (dev
->descriptor
.idVendor
==0x400 && dev
->descriptor
.idProduct
==0x5dc) {
55 //Send a scpi-command to the scope. The response goes into the buffer
56 //called resp, with a size of resplen. If resp==NULL, no response
58 int usbtmc_sendscpi(struct scope
*sc
, char* cmd
,
59 unsigned char *resp
, int resplen
) {
62 int cmdlen
= strlen(cmd
);
63 static unsigned char seq
=0;
69 buff
[1]=seq
; buff
[2]=~seq
; //nseq
71 int2chars(buff
+4, cmdlen
);
76 //fprintf(stderr,"Writing header len=%d\n", cmdlen);
78 r
=usb_bulk_write(sc
->usb
.dev
, 1, (char*)buff
, 12, USB_TIMEOUT
);
79 //fprintf(stderr,"%i bytes written. Writing cmd\n",r);
80 //printb(cmd, cmdlen);
81 r
=usb_bulk_write(sc
->usb
.dev
, 1, cmd
, cmdlen
, USB_TIMEOUT
);
82 //fprintf(stderr,"%i bytes written.\n",r);
83 if (resp
!= NULL
&& resplen
!= 0) {
87 buff
[1]=seq
; buff
[2]=~seq
; //nseq
88 int2chars(buff
+4,0x40);
93 //fprintf(stderr,"Writing resp req header\n");
95 r
=usb_bulk_write(sc
->usb
.dev
, 1, (char*)buff
, 12, USB_TIMEOUT
);
96 //fprintf(stderr,"%i bytes written. Reading response hdr\n",r);
97 r
=usb_bulk_read(sc
->usb
.dev
, 2, (char*)buff
, 0x40, USB_TIMEOUT
);
99 len
=chars2int(buff
+4);
100 //fprintf(stderr,"%i bytes read. Resplen=%i\n",r,len);
101 for (i
=0; i
<(r
-12); i
++) {
102 if (i
<resplen
) resp
[i
] = buff
[i
+12];
106 //fprintf(stderr," Reading response:\n");
107 if (resplen
<len
) len
=resplen
;
108 r
=usb_bulk_read(sc
->usb
.dev
, 2, (char*)resp
+(0x40-12), len
-(0x40-12), USB_TIMEOUT
);
109 //fprintf(stderr,"%i bytes read, wanted %i.\n", r, len-(0x40-12));
117 void usbtmc_claim(struct scope
*sc
)
119 usb_claim_interface(sc
->usb
.dev
, 0);
122 void usbtmc_release(struct scope
*sc
)
124 usb_release_interface(sc
->usb
.dev
, 0);
127 //Initialize the scope.
128 struct scope
* usbtmc_initscope(void) {
130 unsigned char buff
[10];
136 //Locate and open the scope
137 dev
= usbtmc_find_scope();
142 sc
= calloc(1, sizeof(struct scope
));
151 //The following code isn't really necessary, the program works
153 r
=usb_control_msg(sc
->usb
.dev
, 0xC8, 9, 0, 0, (char*)buff
, 4, USB_TIMEOUT
);
156 fprintf (stderr
, "Error %d sending init message: %s\n",
158 fprintf (stderr
, "Do you have permission on the USB device?\n");
161 if (chars2int(buff
)!=0x40005dc) {
162 fprintf(stderr
,"Init: buff[%i]=%x\n",r
,chars2int(buff
));
167 void usbtmc_close(struct scope
*sc
)
170 usb_close(sc
->usb
.dev
);