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 #define USB_ERROR(x) do { if (x < 0) { fprintf(stderr, "usb: %s\n", usb_strerror()); exit(EXIT_FAILURE); } } while(0)
22 /* This routine locates a scope by VID/PID and returns a struct scope* for it */
23 static struct scope
* usbtmc_find_scope() {
25 struct usb_device
*dev
=NULL
;
26 struct usb_dev_handle
*devh
;
33 for (bus
=usb_busses
; bus
; bus
=bus
->next
) {
34 for (dev
=bus
->devices
; dev
; dev
=dev
->next
) {
35 if (dev
->descriptor
.idVendor
== 0x400 && dev
->descriptor
.idProduct
== 0x5dc) {
40 sc
= calloc(1, sizeof(struct scope
));
49 sc
->usb
.brokenRigol
= 1;
50 sc
->usb
.ep_bulk_out
= 1;
51 sc
->usb
.ep_bulk_in
= 2;
52 sc
->usb
.wMaxPacketSize_in
= 0x40;
63 * Send a scpi-command to the scope. The response goes into the buffer
64 * called resp, with a size of resplen. If resp==NULL, no response
67 int usbtmc_sendscpi(struct scope
*sc
, char* cmd
,
68 unsigned char *resp
, int resplen
) {
70 int cmdlen
= strlen(cmd
);
71 struct usbtmc_header
*req
;
75 len
= sizeof(struct usbtmc_header
) + cmdlen
;
85 req
->MsgID
= USBTMC_DEV_DEP_MSG_OUT
;
86 req
->bTag
= sc
->usb
.bTag
;
87 req
->bTagInverse
= ~sc
->usb
.bTag
;
88 req
->TransferSize
= LE32(cmdlen
);
89 req
->bmTransferAttributes
= USBTMC_TRANSFERATTRIB_EOM
;
90 memcpy(req
->msg
, cmd
, cmdlen
);
92 if (sc
->usb
.brokenRigol
) {
93 r
=usb_bulk_write(sc
->usb
.dev
, sc
->usb
.ep_bulk_out
,
94 (char*)req
, sizeof(struct usbtmc_header
),
98 r
=usb_bulk_write(sc
->usb
.dev
, sc
->usb
.ep_bulk_out
,
99 (char*)&(req
->msg
), len
- sizeof(struct usbtmc_header
),
103 r
=usb_bulk_write(sc
->usb
.dev
, sc
->usb
.ep_bulk_out
,
104 (char*)req
, len
, USB_TIMEOUT
);
110 if (resp
!= NULL
&& resplen
!= 0) {
112 struct usbtmc_header
*res
;
117 req
= calloc(1, sizeof(struct usbtmc_header
));
123 req
->MsgID
= USBTMC_REQUEST_DEV_DEP_MSG_IN
;
124 req
->bTag
= sc
->usb
.bTag
;
125 req
->bTagInverse
= ~sc
->usb
.bTag
;
126 req
->TransferSize
= LE32(sc
->usb
.wMaxPacketSize_in
);
127 req
->bmTransferAttributes
= 0;
130 /* send read command */
131 r
=usb_bulk_write(sc
->usb
.dev
, sc
->usb
.ep_bulk_out
,
132 (char*)req
, sizeof(struct usbtmc_header
), USB_TIMEOUT
);
137 buff
=malloc(sc
->usb
.wMaxPacketSize_in
);
143 r
=usb_bulk_read(sc
->usb
.dev
, sc
->usb
.ep_bulk_in
,
144 (char*)buff
, sc
->usb
.wMaxPacketSize_in
, USB_TIMEOUT
);
147 if (r
< sizeof(struct usbtmc_header
)) {
148 fprintf(stderr
, "Short read!\n");
152 bytes_read
= r
- sizeof(struct usbtmc_header
);
154 res
= (struct usbtmc_header
*)buff
;
155 len
= LE32(res
->TransferSize
);
157 memmove(buff
, buff
+ sizeof(struct usbtmc_header
), bytes_read
);
159 buff
= realloc(buff
, len
);
165 while ((len
- bytes_read
) > 0) {
166 r
=usb_bulk_read(sc
->usb
.dev
, sc
->usb
.ep_bulk_in
,
167 (char*)buff
+ bytes_read
, len
- bytes_read
,
175 if (bytes_read
> resplen
) {
176 fprintf(stderr
, "Response buffer to small: %d instead of %d bytes!\n",
177 resplen
, bytes_read
);
178 bytes_read
= resplen
;
181 memcpy(resp
, buff
, bytes_read
);
189 void usbtmc_claim(struct scope
*sc
)
191 usb_claim_interface(sc
->usb
.dev
, 0);
194 void usbtmc_release(struct scope
*sc
)
196 usb_release_interface(sc
->usb
.dev
, 0);
199 //Initialize the scope.
200 struct scope
* usbtmc_initscope(void) {
202 unsigned char buff
[10];
207 /* Locate and open the scope */
208 sc
= usbtmc_find_scope();
214 /* The following code isn't really necessary, the program works
215 OK without it too. */
216 r
=usb_control_msg(sc
->usb
.dev
, 0xC8, 9, 0, 0, (char*)buff
, 4, USB_TIMEOUT
);
219 fprintf (stderr
, "Error %d sending init message: %s\n",
221 fprintf (stderr
, "Do you have permission on the USB device?\n");
224 if (LE32((*(uint32_t*)buff
))!=0x40005dc) {
225 fprintf(stderr
,"Init: buff[%i]=%x\n",r
,LE32((*(uint32_t*)buff
)));
230 void usbtmc_close(struct scope
*sc
)
232 /* Free up and exit */
233 usb_close(sc
->usb
.dev
);