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