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(s, x) do { if (x < 0) { fprintf(stderr, "usb %s: %s\n", s, usb_strerror()); usb_reset(sc->usb.dev); return 0; } } 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;
62 unsigned char usbtmc_status(struct scope
*sc
)
65 unsigned char status
[3];
69 r
= usb_control_msg(sc
->usb
.dev
, 0xA1, 128, (sc
->usb
.bTag
& 0x7f), 0, (char*)status
, 3, USB_TIMEOUT
);
70 if ((r
!= 3) || (status
[0] != 0x01) || (status
[1] != (sc
->usb
.bTag
& 0x7f))) {
71 printf("READ_STATUS_BYTE failed: %d 0x%x 0x%x 0x%x\n", r
, status
[0], status
[1], status
[2]);
79 * Send a scpi-command to the scope. The response goes into the buffer
80 * called resp, with a size of resplen. If resp==NULL, no response
83 int usbtmc_sendscpi(struct scope
*sc
, char* cmd
,
84 unsigned char *resp
, int resplen
) {
86 int cmdlen
= strlen(cmd
);
87 struct usbtmc_header
*req
;
91 len
= sizeof(struct usbtmc_header
) + cmdlen
;
101 req
->MsgID
= USBTMC_DEV_DEP_MSG_OUT
;
102 req
->bTag
= sc
->usb
.bTag
;
103 req
->bTagInverse
= ~sc
->usb
.bTag
;
104 req
->TransferSize
= LE32(cmdlen
);
105 req
->bmTransferAttributes
= USBTMC_TRANSFERATTRIB_EOM
;
106 memcpy(req
->msg
, cmd
, cmdlen
);
108 if (sc
->usb
.brokenRigol
) {
109 r
=usb_bulk_write(sc
->usb
.dev
, sc
->usb
.ep_bulk_out
,
110 (char*)req
, sizeof(struct usbtmc_header
),
112 USB_ERROR("USBTMC_DEV_DEP_MSG_OUT1", r
);
114 r
=usb_bulk_write(sc
->usb
.dev
, sc
->usb
.ep_bulk_out
,
115 (char*)&(req
->msg
), len
- sizeof(struct usbtmc_header
),
117 USB_ERROR("USBTMC_DEV_DEP_MSG_OUT2", r
);
119 r
=usb_bulk_write(sc
->usb
.dev
, sc
->usb
.ep_bulk_out
,
120 (char*)req
, len
, USB_TIMEOUT
);
121 USB_ERROR("USBTMC_DEV_DEP_MSG_OUT", r
);
126 if (resp
!= NULL
&& resplen
!= 0) {
128 struct usbtmc_header
*res
;
133 req
= calloc(1, sizeof(struct usbtmc_header
));
139 req
->MsgID
= USBTMC_REQUEST_DEV_DEP_MSG_IN
;
140 req
->bTag
= sc
->usb
.bTag
;
141 req
->bTagInverse
= ~sc
->usb
.bTag
;
142 req
->TransferSize
= LE32(sc
->usb
.wMaxPacketSize_in
);
143 req
->bmTransferAttributes
= USBTMC_TRANSFERATTRIB_EOM
;
146 /* send read command */
147 r
=usb_bulk_write(sc
->usb
.dev
, sc
->usb
.ep_bulk_out
,
148 (char*)req
, sizeof(struct usbtmc_header
), USB_TIMEOUT
);
149 USB_ERROR("USBTMC_REQUEST_DEV_DEP_MSG_IN", r
);
153 buff
=malloc(sc
->usb
.wMaxPacketSize_in
);
159 r
=usb_bulk_read(sc
->usb
.dev
, sc
->usb
.ep_bulk_in
,
160 (char*)buff
, sc
->usb
.wMaxPacketSize_in
, USB_TIMEOUT
);
161 USB_ERROR("USBTMC_DEV_DEP_MSG_IN1", r
);
163 if (r
< sizeof(struct usbtmc_header
)) {
164 fprintf(stderr
, "Short read!\n");
168 bytes_read
= r
- sizeof(struct usbtmc_header
);
170 res
= (struct usbtmc_header
*)buff
;
171 len
= LE32(res
->TransferSize
);
173 memmove(buff
, buff
+ sizeof(struct usbtmc_header
), bytes_read
);
175 buff
= realloc(buff
, len
);
181 while ((len
- bytes_read
) > 0) {
182 r
=usb_bulk_read(sc
->usb
.dev
, sc
->usb
.ep_bulk_in
,
183 (char*)buff
+ bytes_read
, len
- bytes_read
,
185 USB_ERROR("USBTMC_DEV_DEP_MSG_INx", r
);
191 if (bytes_read
> resplen
) {
192 fprintf(stderr
, "Response buffer to small: %d instead of %d bytes!\n",
193 resplen
, bytes_read
);
194 bytes_read
= resplen
;
197 memcpy(resp
, buff
, bytes_read
);
205 void usbtmc_claim(struct scope
*sc
)
207 usb_claim_interface(sc
->usb
.dev
, 0);
210 void usbtmc_release(struct scope
*sc
)
212 usb_release_interface(sc
->usb
.dev
, 0);
215 //Initialize the scope.
216 struct scope
* usbtmc_initscope(void) {
218 unsigned char buff
[10];
223 /* Locate and open the scope */
224 sc
= usbtmc_find_scope();
230 /* The following code isn't really necessary, the program works
231 OK without it too. */
232 r
=usb_control_msg(sc
->usb
.dev
, 0xC8, 9, 0, 0, (char*)buff
, 4, USB_TIMEOUT
);
235 fprintf (stderr
, "Error %d sending init message: %s\n",
237 fprintf (stderr
, "Do you have permission on the USB device?\n");
240 if (LE32((*(uint32_t*)buff
))!=0x40005dc) {
241 fprintf(stderr
,"Init: buff[%i]=%x\n",r
,LE32((*(uint32_t*)buff
)));
246 void usbtmc_close(struct scope
*sc
)
248 /* Free up and exit */
249 usb_close(sc
->usb
.dev
);