]> git.zerfleddert.de Git - rigol/blame - usbtmc.c
follow USBTMC spec more closely, more to come...
[rigol] / usbtmc.c
CommitLineData
58a9e276
MG
1#include <string.h>
2#include <stdio.h>
3#include <stdint.h>
4#include <usb.h>
5#include <arpa/inet.h>
6
7#include "usbtmc.h"
8
7a226bb8 9#define USB_TIMEOUT 50000
165abe62 10
58a9e276
MG
11//Helper-routine: Convert a little-endian 4-byte word to an int
12static void int2chars(unsigned char *buff,unsigned int a) {
13 buff[3]=(a>>24)&0xff;
14 buff[2]=(a>>16)&0xff;
15 buff[1]=(a>>8)&0xff;
16 buff[0]=(a)&0xff;
17}
18
19//Helper-routine: Convert an int to little-endian 4-byte word
20unsigned int chars2int(unsigned char *buff) {
21 unsigned int a;
22 a=buff[3]<<24;
23 a+=buff[2]<<16;
24 a+=buff[1]<<8;
25 a+=buff[0];
26 return a;
27}
28
29//This routine locates a scope by VID/PID and returns an opened handle to it.
30static usb_dev_handle *usbtmc_find_scope() {
31 struct usb_bus *bus;
32 struct usb_device *dev=NULL;
33 usb_find_busses();
34 usb_find_devices();
35 for (bus=usb_busses; bus; bus=bus->next) {
36 for (dev=bus->devices; dev; dev=dev->next) {
37 //fprintf(stderr,"Prod/dev: %04X:%04X\n",dev->descriptor.idVendor,dev->descriptor.idProduct);
38 if (dev->descriptor.idVendor==0x400 && dev->descriptor.idProduct==0x5dc) {
39 return usb_open(dev);
40 }
41 }
42 }
43 return NULL;
44}
45
46//Send a scpi-command to the scope. The response goes into the buffer
47//called resp, with a size of resplen. If resp==NULL, no response
48//is requested.
49int usbtmc_sendscpi(usb_dev_handle *dev, char* cmd,
50 unsigned char *resp, int resplen) {
51 unsigned char *buff;
52 int len,r,i;
53 int cmdlen = strlen(cmd);
54 static unsigned char seq=0;
55
56
57 buff=malloc(0x40);
58 seq++;
59 buff[0]=1; //func
60 buff[1]=seq; buff[2]=~seq; //nseq
61 buff[3]=0;
62 int2chars(buff+4, cmdlen);
63 buff[8]=1;
09268769
MG
64 buff[9]=0x00;
65 buff[10]=0x00;
66 buff[11]=0x00;
58a9e276
MG
67 //fprintf(stderr,"Writing header len=%d\n", cmdlen);
68 //printb(buff,12);
165abe62 69 r=usb_bulk_write(dev, 1, (char*)buff, 12, USB_TIMEOUT);
58a9e276
MG
70 //fprintf(stderr,"%i bytes written. Writing cmd\n",r);
71 //printb(cmd, cmdlen);
165abe62 72 r=usb_bulk_write(dev, 1, cmd, cmdlen, USB_TIMEOUT);
58a9e276
MG
73 //fprintf(stderr,"%i bytes written.\n",r);
74 if (resp != NULL && resplen != 0) {
75 //send read command
76 buff[0]=2; //func
77 seq++;
78 buff[1]=seq; buff[2]=~seq; //nseq
79 int2chars(buff+4,0x40);
09268769 80 buff[8]=0;
58a9e276
MG
81 buff[9]=0xA;
82 buff[10]=0;
83 buff[11]=0;
84 //fprintf(stderr,"Writing resp req header\n");
85 //printb(buff,12);
165abe62 86 r=usb_bulk_write(dev, 1, (char*)buff, 12, USB_TIMEOUT);
58a9e276 87 //fprintf(stderr,"%i bytes written. Reading response hdr\n",r);
165abe62 88 r=usb_bulk_read(dev, 2, (char*)buff, 0x40, USB_TIMEOUT);
58a9e276
MG
89 //printb(buff,r);
90 len=chars2int(buff+4);
91 //fprintf(stderr,"%i bytes read. Resplen=%i\n",r,len);
92 for (i=0; i<(r-12); i++) {
93 if (i<resplen) resp[i] = buff[i+12];
94 }
95 //printb(resp,r-12);
96 if (len > 0x40-12) {
97 //fprintf(stderr," Reading response:\n");
98 if (resplen<len) len=resplen;
165abe62 99 r=usb_bulk_read(dev, 2, (char*)resp+(0x40-12), len-(0x40-12), USB_TIMEOUT);
58a9e276
MG
100 //fprintf(stderr,"%i bytes read, wanted %i.\n", r, len-(0x40-12));
101 return r+(0x40-12);
102 }
103 return len;
104 }
105 return 0;
106}
107
ad9fbc05
MG
108void usbtmc_claim(usb_dev_handle *sc)
109{
110 usb_claim_interface(sc, 0);
111}
112
113void usbtmc_release(usb_dev_handle *sc)
114{
115 usb_release_interface(sc, 0);
116}
117
58a9e276
MG
118//Initialize the scope.
119usb_dev_handle* usbtmc_initscope(void) {
120 int r;
121 unsigned char buff[10];
122 usb_dev_handle *dev;
123
124 //Init libusb
125 usb_init();
126 //Locate and open the scope
127 dev = usbtmc_find_scope();
128 if (!dev) {
129 printf("No scope found.\n");
130 exit(1);
58a9e276 131 }
ad9fbc05 132 usbtmc_claim(dev);
58a9e276
MG
133 //The following code isn't really necessary, the program works
134 //OK without it too.
165abe62 135 r=usb_control_msg(dev, 0xC8, 9, 0, 0, (char*)buff, 4, USB_TIMEOUT);
ad9fbc05 136 usbtmc_release(dev);
58a9e276
MG
137 if (r < 0) {
138 fprintf (stderr, "Error %d sending init message: %s\n",
139 r, strerror (-r));
140 fprintf (stderr, "Do you have permission on the USB device?\n");
141 exit (1);
142 }
b74fea90 143 if (chars2int(buff)!=0x40005dc) {
58a9e276
MG
144 fprintf(stderr,"Init: buff[%i]=%x\n",r,chars2int(buff));
145 }
146 return dev;
147}
b74fea90
MG
148
149void usbtmc_close(usb_dev_handle *sc)
150{
151 //Free up and exit
b74fea90
MG
152 usb_close(sc);
153}
Impressum, Datenschutz