| THM1176InstrumentDriver 1.2 C++ API for Metrolab THM1176

Loading...
Searching...
No Matches
USBTMCPrivate.h
Go to the documentation of this file.
1// Copyright (c) 2020 Metrolab Technology S.A., Geneva, Switzerland (www.metrolab.com)
2// See the included file LICENSE.txt for the licensing conditions.
3
4#pragma once
5
6#include "IEEE488InstrumentTypes.h"
7
8 using namespace MTL::Instrument;
9
10//----------------------------------------------------------------------//
12// Some USBTMC-specific enums, as defined in the USBTMC standard.
13 static const U8 SUBCLASS_USBTMC = 0x03;
14 static const U8 USBTMC_USB488 = 0x01;
15
16// USBTMC control requests
17 enum {
18 INITIATE_ABORT_BULK_OUT = 1,
19 CHECK_ABORT_BULK_OUT_STATUS = 2,
20 INITIATE_ABORT_BULK_IN = 3,
21 CHECK_ABORT_BULK_IN_STATUS = 4,
22 INITIATE_CLEAR = 5,
23 CHECK_CLEAR_STATUS = 6,
24 GET_CAPABILITIES = 7,
25 INDICATOR_PULSE = 64,
26
27 // USB488 control requests
28 READ_STATUS_BYTE = 128,
29 REN_CONTROL = 160,
30 GO_TO_LOCAL = 161,
31 LOCAL_LOCKOUT = 162,
32};
33
34// Constants for control endpoint transfers.
35 static const I32 USB_MAX_PORT_DEPTH = 7;
36 static const U16 USB_CONTROL_ENDPOINT_NUMBER = 0;
37 static const U32 USB_DESCRIPTOR_STRING_LENGTH = 256;
38 static const U8 USBTMC_BM_REQUEST_TYPE_IN_CLS_IF = 0xA1;
39 static const U8 USBTMC_BM_REQUEST_TYPE_IN_CLS_EP = 0xA2;
40 static const U16 USBTMC_READ_BUFFER_SIZE = 256;
41 static const U16 USBTMC_READSTB_CTRL_RESP_LENGTH = 3;
42 static const U16 USBTMC_READSTB_INTR_RESP_LENGTH = 2;
43 static const U16 USBTMC_INITIATE_CLEAR_RESP_LENGTH = 1;
44 static const U16 USBTMC_CHECK_CLEAR_STATUS_RESP_LENGTH = 2;
45 static const U32 USBTMC_CHECK_CLEAR_STATUS_INTERVAL = 10;
46 static const U16 USBTMC_INITIATE_ABORT_BULKIN_RESP_LENGTH = 2;
47 static const U16 USBTMC_CHECK_ABORT_BULKIN_STATUS_RESP_LENGTH = 8;
48 static const U16 USBTMC_INITIATE_ABORT_BULKOUT_RESP_LENGTH = 2;
49 static const U16 USBTMC_CHECK_ABORT_BULKOUT_STATUS_RESP_LENGTH = 8;
50
51// USBTMC status codes
52 enum
53{
54 USBTMC_STATUS_SUCCESS = 0x01,
55 USBTMC_STATUS_PENDING = 0x02,
56 USBTMC_STATUS_INTERRUPT_IN_BUSY = 0x20,
57 USBTMC_STATUS_FAILED = 0x80,
58 USBTMC_STATUS_TRANSFER_NOT_IN_PROGRESS = 0x81,
59 USBTMC_STATUS_SPLIT_NOT_IN_PROGRESS = 0x82,
60 USBTMC_STATUS_SPLIT_IN_PROGRESS = 0x83,
61};
62
63// USBTMC capabilities
64 static const U8 USBTMC_INT_CAP_LISTEN_ONLY = 0x01;
65 static const U8 USBTMC_INT_CAP_TALK_ONLY = 0x02;
66 static const U8 USBTMC_INT_CAP_INDICATOR = 0x04;
67
68 static const U8 USBTMC_DEV_CAP_TERMCHAR = 0x01;
69
70 static const U8 USB488_DEV_CAP_DT1 = 0x01;
71 static const U8 USB488_DEV_CAP_RL1 = 0x02;
72 static const U8 USB488_DEV_CAP_SR1 = 0x04;
73 static const U8 USB488_DEV_CAP_SCPI = 0x08;
74
75// Bulk message constants
76 static const I32 USBTMC_BULK_HEADER_SIZE = 12;
77 static const I32 USBTMC_BULK_MIN_BUFFER_SIZE = 64;
78 static const I32 USBTMC_BULK_TRIGGER_MSG_SIZE = 12;
79
80// Bulk MsgID values
81 static const U8 DEV_DEP_MSG_OUT = 1;
82 static const U8 REQUEST_DEV_DEP_MSG_IN = 2;
83 static const U8 DEV_DEP_MSG_IN = 2;
84 static const U8 TRIGGER = 128;
85
86// bmTransferAttributes
87 static const U8 EOM = 0x01;
88 static const U8 TERM_CHAR_ENABLED = 0x02;
89
90// bmClear
91 static const U8 BULKIN_FIFO_BYTES = 0x01;
92
93//----------------------------------------------------------------------//
95 enum USBTMCError
96{
97 USBTMC_ERROR_FIND_RESRC_BAD_FILTER = 1,
98 USBTMC_ERROR_FIND_RESRC_NO_USBTMC_USB488,
99 USBTMC_ERROR_GET_RESRC_NOT_FOUND,
100 USBTMC_ERROR_WRITE_INVALID_TRANSFER_COUNT,
101 USBTMC_ERROR_READ_INVALID_BUFFER_FOR_APPEND,
102 USBTMC_ERROR_READ_INVALID_HEADER,
103 USBTMC_ERROR_READ_WRONG_MSG_SIZE,
104 USBTMC_ERROR_CLEAR_RESPONSE_ERROR,
105 USBTMC_ERROR_CLEAR_BULKIN_RESPONSE_ERROR,
106 USBTMC_ERROR_CLEAR_BULKOUT_RESPONSE_ERROR,
107 USBTMC_ERROR_READSTB_CONTROL_RESPONSE_ERROR,
108 USBTMC_ERROR_READSTB_INTERRUPT_RESPONSE_ERROR,
109 USBTMC_ERROR_INSTRUMENT_LOCKED,
110 USBTMC_ERROR_DEVICE_NOT_OPEN,
111 USBTMC_ERROR_SRQ_NOT_ENABLED
112};
113
115 static const char * USBTMC_ERROR_EXPLANATION[] =
116{
117 "USBTMC: Success",
118 "USBTMC FindResources: Invalid filter",
119 "USBTMC FindResources: Failed to find USBTMC-USB488 interface",
120 "USBTMC GetDeviceListEntry: Resource not found",
121 "USBTMC Write: invalid transfer count",
122 "USBTMC Read: Buffer is invalid for append read (no room for read header)",
123 "USBTMC Read: Read header is invalid",
124 "USBTMC Read: Message size is wrong",
125 "USBTMC Clear: Response error",
126 "USBTMC Clear BulkIn: Response error",
127 "USBTMC Clear BulkOut: Response error",
128 "USBTMC ReadSTB: Control endpoint response error",
129 "USBTMC ReadSTB: Interrupt endpoint response error",
130 "USBTMC: Instrument is locked by someone else",
131 "USBTMC: Instrument is not open",
132 "USBTMC: Service Requests not enabled"
133};
134
135//----------------------------------------------------------------------//
137
142#define R8(x) ((unsigned)((const U8*)(x))[0])
143
149#define RB16(x) (((unsigned)((const U8*)(x))[0] << 8) | \
150(unsigned)((const U8*)(x))[1])
151
157#define RL16(x) (((unsigned)((const U8*)(x))[1] << 8) | \
158(unsigned)((const U8*)(x))[0])
159
165#define RB16S(x) ((int16_t) \
166(((unsigned)((const U8*)(x))[0] << 8) | \
167(unsigned)((const U8*)(x))[1]))
168
174#define RL16S(x) ((int16_t) \
175(((unsigned)((const U8*)(x))[1] << 8) | \
176(unsigned)((const U8*)(x))[0]))
177
183#define RB32(x) (((unsigned)((const U8*)(x))[0] << 24) | \
184((unsigned)((const U8*)(x))[1] << 16) | \
185((unsigned)((const U8*)(x))[2] << 8) | \
186(unsigned)((const U8*)(x))[3])
187
193#define RL32(x) (((unsigned)((const U8*)(x))[3] << 24) | \
194((unsigned)((const U8*)(x))[2] << 16) | \
195((unsigned)((const U8*)(x))[1] << 8) | \
196(unsigned)((const U8*)(x))[0])
197
203#define RB32S(x) ((int32_t) \
204(((unsigned)((const U8*)(x))[0] << 24) | \
205((unsigned)((const U8*)(x))[1] << 16) | \
206((unsigned)((const U8*)(x))[2] << 8) | \
207(unsigned)((const U8*)(x))[3]))
208
214#define RL32S(x) ((int32_t) \
215(((unsigned)((const U8*)(x))[3] << 24) | \
216((unsigned)((const U8*)(x))[2] << 16) | \
217((unsigned)((const U8*)(x))[1] << 8) | \
218(unsigned)((const U8*)(x))[0]))
219
225#define RB64(x) (((uint64_t)((const U8*)(x))[0] << 56) | \
226((uint64_t)((const U8*)(x))[1] << 48) | \
227((uint64_t)((const U8*)(x))[2] << 40) | \
228((uint64_t)((const U8*)(x))[3] << 32) | \
229((uint64_t)((const U8*)(x))[4] << 24) | \
230((uint64_t)((const U8*)(x))[5] << 16) | \
231((uint64_t)((const U8*)(x))[6] << 8) | \
232(uint64_t)((const U8*)(x))[7])
233
239#define RL64(x) (((uint64_t)((const U8*)(x))[7] << 56) | \
240((uint64_t)((const U8*)(x))[6] << 48) | \
241((uint64_t)((const U8*)(x))[5] << 40) | \
242((uint64_t)((const U8*)(x))[4] << 32) | \
243((uint64_t)((const U8*)(x))[3] << 24) | \
244((uint64_t)((const U8*)(x))[2] << 16) | \
245((uint64_t)((const U8*)(x))[1] << 8) | \
246(uint64_t)((const U8*)(x))[0])
247
253#define RL64S(x) ((int64_t) \
254(((uint64_t)((const U8*)(x))[7] << 56) | \
255((uint64_t)((const U8*)(x))[6] << 48) | \
256((uint64_t)((const U8*)(x))[5] << 40) | \
257((uint64_t)((const U8*)(x))[4] << 32) | \
258((uint64_t)((const U8*)(x))[3] << 24) | \
259((uint64_t)((const U8*)(x))[2] << 16) | \
260((uint64_t)((const U8*)(x))[1] << 8) | \
261(uint64_t)((const U8*)(x))[0]))
262
268#define RBFL(x) ((union { uint32_t u; float f; }) { .u = RB32(x) }.f)
269
275#define RLFL(x) ((union { uint32_t u; float f; }) { .u = RL32(x) }.f)
276
282#define W8(p, x) do { ((U8*)(p))[0] = (U8) (x); } while (0)
283
289#define WB16(p, x) do { ((U8*)(p))[1] = (U8) (x); \
290((U8*)(p))[0] = (U8)((x)>>8); } while (0)
291
297#define WL16(p, x) do { ((U8*)(p))[0] = (U8) (x); \
298((U8*)(p))[1] = (U8)((x)>>8); } while (0)
299
305#define WB32(p, x) do { ((U8*)(p))[3] = (U8) (x); \
306((U8*)(p))[2] = (U8)((x)>>8); \
307((U8*)(p))[1] = (U8)((x)>>16); \
308((U8*)(p))[0] = (U8)((x)>>24); } while (0)
309
315#define WL32(p, x) do { ((U8*)(p))[0] = (U8) (x); \
316((U8*)(p))[1] = (U8)((x)>>8); \
317((U8*)(p))[2] = (U8)((x)>>16); \
318((U8*)(p))[3] = (U8)((x)>>24); } while (0)
319
325#define WBFL(p, x) WB32(p, (union { uint32_t u; float f; }) { .f = x }.u)
326
332#define WLFL(p, x) WL32(p, (union { uint32_t u; float f; }) { .f = x }.u)
IEEE488InstrumentTypes.h
Abstract IEEE488 instrument class: type definitions.
U8
unsigned char U8
Unsigned byte.
Definition OSDefines.h:29
I32
int I32
32-bit signed integer.
Definition OSDefines.h:27
U32
unsigned int U32
32-bit unsigned integer.
Definition OSDefines.h:31
U16
unsigned short U16
16-bit unsigned integer.
Definition OSDefines.h:30
USB488_DEV_CAP_DT1
static const U8 USB488_DEV_CAP_DT1
Definition USBTMCPrivate.h:70
BULKIN_FIFO_BYTES
static const U8 BULKIN_FIFO_BYTES
Definition USBTMCPrivate.h:91
USBTMC_INT_CAP_LISTEN_ONLY
static const U8 USBTMC_INT_CAP_LISTEN_ONLY
Definition USBTMCPrivate.h:64
USB488_DEV_CAP_SCPI
static const U8 USB488_DEV_CAP_SCPI
Definition USBTMCPrivate.h:73
USBTMC_READSTB_CTRL_RESP_LENGTH
static const U16 USBTMC_READSTB_CTRL_RESP_LENGTH
Definition USBTMCPrivate.h:41
DEV_DEP_MSG_IN
static const U8 DEV_DEP_MSG_IN
Definition USBTMCPrivate.h:83
DEV_DEP_MSG_OUT
static const U8 DEV_DEP_MSG_OUT
Definition USBTMCPrivate.h:81
USBTMC_BULK_TRIGGER_MSG_SIZE
static const I32 USBTMC_BULK_TRIGGER_MSG_SIZE
Definition USBTMCPrivate.h:78
USB_CONTROL_ENDPOINT_NUMBER
static const U16 USB_CONTROL_ENDPOINT_NUMBER
Definition USBTMCPrivate.h:36
USBTMC_INITIATE_ABORT_BULKIN_RESP_LENGTH
static const U16 USBTMC_INITIATE_ABORT_BULKIN_RESP_LENGTH
Definition USBTMCPrivate.h:46
REQUEST_DEV_DEP_MSG_IN
static const U8 REQUEST_DEV_DEP_MSG_IN
Definition USBTMCPrivate.h:82
USBTMC_READSTB_INTR_RESP_LENGTH
static const U16 USBTMC_READSTB_INTR_RESP_LENGTH
Definition USBTMCPrivate.h:42
USB_MAX_PORT_DEPTH
static const I32 USB_MAX_PORT_DEPTH
Definition USBTMCPrivate.h:35
USB488_DEV_CAP_RL1
static const U8 USB488_DEV_CAP_RL1
Definition USBTMCPrivate.h:71
EOM
static const U8 EOM
Definition USBTMCPrivate.h:87
TRIGGER
static const U8 TRIGGER
Definition USBTMCPrivate.h:84
SUBCLASS_USBTMC
static const U8 SUBCLASS_USBTMC
Constants for USBTMC and USB488.
Definition USBTMCPrivate.h:13
USB_DESCRIPTOR_STRING_LENGTH
static const U32 USB_DESCRIPTOR_STRING_LENGTH
Definition USBTMCPrivate.h:37
USBTMC_STATUS_SPLIT_IN_PROGRESS
@ USBTMC_STATUS_SPLIT_IN_PROGRESS
Definition USBTMCPrivate.h:60
USBTMC_STATUS_SUCCESS
@ USBTMC_STATUS_SUCCESS
Definition USBTMCPrivate.h:54
USBTMC_STATUS_SPLIT_NOT_IN_PROGRESS
@ USBTMC_STATUS_SPLIT_NOT_IN_PROGRESS
Definition USBTMCPrivate.h:59
USBTMC_STATUS_INTERRUPT_IN_BUSY
@ USBTMC_STATUS_INTERRUPT_IN_BUSY
Definition USBTMCPrivate.h:56
USBTMC_STATUS_TRANSFER_NOT_IN_PROGRESS
@ USBTMC_STATUS_TRANSFER_NOT_IN_PROGRESS
Definition USBTMCPrivate.h:58
USBTMC_STATUS_PENDING
@ USBTMC_STATUS_PENDING
Definition USBTMCPrivate.h:55
USBTMC_STATUS_FAILED
@ USBTMC_STATUS_FAILED
Definition USBTMCPrivate.h:57
USBTMC_INITIATE_CLEAR_RESP_LENGTH
static const U16 USBTMC_INITIATE_CLEAR_RESP_LENGTH
Definition USBTMCPrivate.h:43
USBTMC_READ_BUFFER_SIZE
static const U16 USBTMC_READ_BUFFER_SIZE
Definition USBTMCPrivate.h:40
USBTMC_CHECK_ABORT_BULKIN_STATUS_RESP_LENGTH
static const U16 USBTMC_CHECK_ABORT_BULKIN_STATUS_RESP_LENGTH
Definition USBTMCPrivate.h:47
USBTMC_CHECK_ABORT_BULKOUT_STATUS_RESP_LENGTH
static const U16 USBTMC_CHECK_ABORT_BULKOUT_STATUS_RESP_LENGTH
Definition USBTMCPrivate.h:49
USBTMC_BULK_MIN_BUFFER_SIZE
static const I32 USBTMC_BULK_MIN_BUFFER_SIZE
Definition USBTMCPrivate.h:77
USBTMC_CHECK_CLEAR_STATUS_RESP_LENGTH
static const U16 USBTMC_CHECK_CLEAR_STATUS_RESP_LENGTH
Definition USBTMCPrivate.h:44
TERM_CHAR_ENABLED
static const U8 TERM_CHAR_ENABLED
Definition USBTMCPrivate.h:88
USBTMC_USB488
static const U8 USBTMC_USB488
Definition USBTMCPrivate.h:14
USB488_DEV_CAP_SR1
static const U8 USB488_DEV_CAP_SR1
Definition USBTMCPrivate.h:72
USBTMC_BM_REQUEST_TYPE_IN_CLS_IF
static const U8 USBTMC_BM_REQUEST_TYPE_IN_CLS_IF
Definition USBTMCPrivate.h:38
USBTMC_ERROR_EXPLANATION
static const char * USBTMC_ERROR_EXPLANATION[]
Error explanations.
Definition USBTMCPrivate.h:115
USBTMC_INT_CAP_TALK_ONLY
static const U8 USBTMC_INT_CAP_TALK_ONLY
Definition USBTMCPrivate.h:65
USBTMCError
USBTMCError
Error codes. Complementary to libusb errors (0 to -99).
Definition USBTMCPrivate.h:96
USBTMC_ERROR_CLEAR_BULKOUT_RESPONSE_ERROR
@ USBTMC_ERROR_CLEAR_BULKOUT_RESPONSE_ERROR
Definition USBTMCPrivate.h:106
USBTMC_ERROR_GET_RESRC_NOT_FOUND
@ USBTMC_ERROR_GET_RESRC_NOT_FOUND
Definition USBTMCPrivate.h:99
USBTMC_ERROR_WRITE_INVALID_TRANSFER_COUNT
@ USBTMC_ERROR_WRITE_INVALID_TRANSFER_COUNT
Definition USBTMCPrivate.h:100
USBTMC_ERROR_READSTB_INTERRUPT_RESPONSE_ERROR
@ USBTMC_ERROR_READSTB_INTERRUPT_RESPONSE_ERROR
Definition USBTMCPrivate.h:108
USBTMC_ERROR_READSTB_CONTROL_RESPONSE_ERROR
@ USBTMC_ERROR_READSTB_CONTROL_RESPONSE_ERROR
Definition USBTMCPrivate.h:107
USBTMC_ERROR_READ_INVALID_HEADER
@ USBTMC_ERROR_READ_INVALID_HEADER
Definition USBTMCPrivate.h:102
USBTMC_ERROR_CLEAR_RESPONSE_ERROR
@ USBTMC_ERROR_CLEAR_RESPONSE_ERROR
Definition USBTMCPrivate.h:104
USBTMC_ERROR_READ_WRONG_MSG_SIZE
@ USBTMC_ERROR_READ_WRONG_MSG_SIZE
Definition USBTMCPrivate.h:103
USBTMC_ERROR_READ_INVALID_BUFFER_FOR_APPEND
@ USBTMC_ERROR_READ_INVALID_BUFFER_FOR_APPEND
Definition USBTMCPrivate.h:101
USBTMC_ERROR_CLEAR_BULKIN_RESPONSE_ERROR
@ USBTMC_ERROR_CLEAR_BULKIN_RESPONSE_ERROR
Definition USBTMCPrivate.h:105
USBTMC_ERROR_FIND_RESRC_BAD_FILTER
@ USBTMC_ERROR_FIND_RESRC_BAD_FILTER
Definition USBTMCPrivate.h:97
USBTMC_ERROR_DEVICE_NOT_OPEN
@ USBTMC_ERROR_DEVICE_NOT_OPEN
Definition USBTMCPrivate.h:110
USBTMC_ERROR_SRQ_NOT_ENABLED
@ USBTMC_ERROR_SRQ_NOT_ENABLED
Definition USBTMCPrivate.h:111
USBTMC_ERROR_INSTRUMENT_LOCKED
@ USBTMC_ERROR_INSTRUMENT_LOCKED
Definition USBTMCPrivate.h:109
USBTMC_ERROR_FIND_RESRC_NO_USBTMC_USB488
@ USBTMC_ERROR_FIND_RESRC_NO_USBTMC_USB488
Definition USBTMCPrivate.h:98
USBTMC_BM_REQUEST_TYPE_IN_CLS_EP
static const U8 USBTMC_BM_REQUEST_TYPE_IN_CLS_EP
Definition USBTMCPrivate.h:39
USBTMC_BULK_HEADER_SIZE
static const I32 USBTMC_BULK_HEADER_SIZE
Definition USBTMCPrivate.h:76
USBTMC_INT_CAP_INDICATOR
static const U8 USBTMC_INT_CAP_INDICATOR
Definition USBTMCPrivate.h:66
USBTMC_CHECK_CLEAR_STATUS_INTERVAL
static const U32 USBTMC_CHECK_CLEAR_STATUS_INTERVAL
Definition USBTMCPrivate.h:45
USBTMC_INITIATE_ABORT_BULKOUT_RESP_LENGTH
static const U16 USBTMC_INITIATE_ABORT_BULKOUT_RESP_LENGTH
Definition USBTMCPrivate.h:48
USBTMC_DEV_CAP_TERMCHAR
static const U8 USBTMC_DEV_CAP_TERMCHAR
Definition USBTMCPrivate.h:68
INDICATOR_PULSE
@ INDICATOR_PULSE
Definition USBTMCPrivate.h:25
READ_STATUS_BYTE
@ READ_STATUS_BYTE
Definition USBTMCPrivate.h:28
GET_CAPABILITIES
@ GET_CAPABILITIES
Definition USBTMCPrivate.h:24
INITIATE_CLEAR
@ INITIATE_CLEAR
Definition USBTMCPrivate.h:22
LOCAL_LOCKOUT
@ LOCAL_LOCKOUT
Definition USBTMCPrivate.h:31
INITIATE_ABORT_BULK_OUT
@ INITIATE_ABORT_BULK_OUT
Definition USBTMCPrivate.h:18
CHECK_ABORT_BULK_IN_STATUS
@ CHECK_ABORT_BULK_IN_STATUS
Definition USBTMCPrivate.h:21
REN_CONTROL
@ REN_CONTROL
Definition USBTMCPrivate.h:29
CHECK_CLEAR_STATUS
@ CHECK_CLEAR_STATUS
Definition USBTMCPrivate.h:23
CHECK_ABORT_BULK_OUT_STATUS
@ CHECK_ABORT_BULK_OUT_STATUS
Definition USBTMCPrivate.h:19
INITIATE_ABORT_BULK_IN
@ INITIATE_ABORT_BULK_IN
Definition USBTMCPrivate.h:20
GO_TO_LOCAL
@ GO_TO_LOCAL
Definition USBTMCPrivate.h:30
MTL::Instrument
Definition THM1176.h:74