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