THM1176InstrumentDriver  1.0
C++ API for Metrolab THM1176
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 
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 {
26 
27  // USB488 control requests
29  REN_CONTROL = 160,
30  GO_TO_LOCAL = 161,
32 };
33 
34 // Constants for control endpoint transfers.
35 static const I32 USB_MAX_PORT_DEPTH = 7;
37 static const U32 USB_DESCRIPTOR_STRING_LENGTH = 256;
40 static const U16 USBTMC_READ_BUFFER_SIZE = 256;
50 
51 // USBTMC status codes
52 enum
53 {
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;
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 //----------------------------------------------------------------------//
96 {
111 };
112 
114 static const char * USBTMC_ERROR_EXPLANATION[] =
115 {
116  "USBTMC: Success",
117  "USBTMC FindResources: Invalid filter",
118  "USBTMC FindResources: Failed to find USBTMC-USB488 interface",
119  "USBTMC GetDeviceListEntry: Resource not found",
120  "USBTMC Write: invalid transfer count",
121  "USBTMC Read: Buffer is invalid for append read (no room for read header)",
122  "USBTMC Read: Read header is invalid",
123  "USBTMC Read: Message size is wrong",
124  "USBTMC Clear: Response error",
125  "USBTMC Clear BulkIn: Response error",
126  "USBTMC Clear BulkOut: Response error",
127  "USBTMC ReadSTB: Control endpoint response error",
128  "USBTMC ReadSTB: Interrupt endpoint response error",
129  "USBTMC: Instrument is locked by someone else",
130  "USBTMC: Instrument is not open"
131 };
132 
133 //----------------------------------------------------------------------//
135 
140 #define R8(x) ((unsigned)((const U8*)(x))[0])
141 
147 #define RB16(x) (((unsigned)((const U8*)(x))[0] << 8) | \
148 (unsigned)((const U8*)(x))[1])
149 
155 #define RL16(x) (((unsigned)((const U8*)(x))[1] << 8) | \
156 (unsigned)((const U8*)(x))[0])
157 
163 #define RB16S(x) ((int16_t) \
164 (((unsigned)((const U8*)(x))[0] << 8) | \
165 (unsigned)((const U8*)(x))[1]))
166 
172 #define RL16S(x) ((int16_t) \
173 (((unsigned)((const U8*)(x))[1] << 8) | \
174 (unsigned)((const U8*)(x))[0]))
175 
181 #define RB32(x) (((unsigned)((const U8*)(x))[0] << 24) | \
182 ((unsigned)((const U8*)(x))[1] << 16) | \
183 ((unsigned)((const U8*)(x))[2] << 8) | \
184 (unsigned)((const U8*)(x))[3])
185 
191 #define RL32(x) (((unsigned)((const U8*)(x))[3] << 24) | \
192 ((unsigned)((const U8*)(x))[2] << 16) | \
193 ((unsigned)((const U8*)(x))[1] << 8) | \
194 (unsigned)((const U8*)(x))[0])
195 
201 #define RB32S(x) ((int32_t) \
202 (((unsigned)((const U8*)(x))[0] << 24) | \
203 ((unsigned)((const U8*)(x))[1] << 16) | \
204 ((unsigned)((const U8*)(x))[2] << 8) | \
205 (unsigned)((const U8*)(x))[3]))
206 
212 #define RL32S(x) ((int32_t) \
213 (((unsigned)((const U8*)(x))[3] << 24) | \
214 ((unsigned)((const U8*)(x))[2] << 16) | \
215 ((unsigned)((const U8*)(x))[1] << 8) | \
216 (unsigned)((const U8*)(x))[0]))
217 
223 #define RB64(x) (((uint64_t)((const U8*)(x))[0] << 56) | \
224 ((uint64_t)((const U8*)(x))[1] << 48) | \
225 ((uint64_t)((const U8*)(x))[2] << 40) | \
226 ((uint64_t)((const U8*)(x))[3] << 32) | \
227 ((uint64_t)((const U8*)(x))[4] << 24) | \
228 ((uint64_t)((const U8*)(x))[5] << 16) | \
229 ((uint64_t)((const U8*)(x))[6] << 8) | \
230 (uint64_t)((const U8*)(x))[7])
231 
237 #define RL64(x) (((uint64_t)((const U8*)(x))[7] << 56) | \
238 ((uint64_t)((const U8*)(x))[6] << 48) | \
239 ((uint64_t)((const U8*)(x))[5] << 40) | \
240 ((uint64_t)((const U8*)(x))[4] << 32) | \
241 ((uint64_t)((const U8*)(x))[3] << 24) | \
242 ((uint64_t)((const U8*)(x))[2] << 16) | \
243 ((uint64_t)((const U8*)(x))[1] << 8) | \
244 (uint64_t)((const U8*)(x))[0])
245 
251 #define RL64S(x) ((int64_t) \
252 (((uint64_t)((const U8*)(x))[7] << 56) | \
253 ((uint64_t)((const U8*)(x))[6] << 48) | \
254 ((uint64_t)((const U8*)(x))[5] << 40) | \
255 ((uint64_t)((const U8*)(x))[4] << 32) | \
256 ((uint64_t)((const U8*)(x))[3] << 24) | \
257 ((uint64_t)((const U8*)(x))[2] << 16) | \
258 ((uint64_t)((const U8*)(x))[1] << 8) | \
259 (uint64_t)((const U8*)(x))[0]))
260 
266 #define RBFL(x) ((union { uint32_t u; float f; }) { .u = RB32(x) }.f)
267 
273 #define RLFL(x) ((union { uint32_t u; float f; }) { .u = RL32(x) }.f)
274 
280 #define W8(p, x) do { ((U8*)(p))[0] = (U8) (x); } while (0)
281 
287 #define WB16(p, x) do { ((U8*)(p))[1] = (U8) (x); \
288 ((U8*)(p))[0] = (U8)((x)>>8); } while (0)
289 
295 #define WL16(p, x) do { ((U8*)(p))[0] = (U8) (x); \
296 ((U8*)(p))[1] = (U8)((x)>>8); } while (0)
297 
303 #define WB32(p, x) do { ((U8*)(p))[3] = (U8) (x); \
304 ((U8*)(p))[2] = (U8)((x)>>8); \
305 ((U8*)(p))[1] = (U8)((x)>>16); \
306 ((U8*)(p))[0] = (U8)((x)>>24); } while (0)
307 
313 #define WL32(p, x) do { ((U8*)(p))[0] = (U8) (x); \
314 ((U8*)(p))[1] = (U8)((x)>>8); \
315 ((U8*)(p))[2] = (U8)((x)>>16); \
316 ((U8*)(p))[3] = (U8)((x)>>24); } while (0)
317 
323 #define WBFL(p, x) WB32(p, (union { uint32_t u; float f; }) { .f = x }.u)
324 
330 #define WLFL(p, x) WL32(p, (union { uint32_t u; float f; }) { .f = x }.u)
USBTMC_BULK_MIN_BUFFER_SIZE
static const I32 USBTMC_BULK_MIN_BUFFER_SIZE
Definition: USBTMCPrivate.h:77
USBTMC_STATUS_FAILED
@ USBTMC_STATUS_FAILED
Definition: USBTMCPrivate.h:57
USB488_DEV_CAP_SCPI
static const U8 USB488_DEV_CAP_SCPI
Definition: USBTMCPrivate.h:73
EOM
static const U8 EOM
Definition: USBTMCPrivate.h:87
USBTMC_INT_CAP_INDICATOR
static const U8 USBTMC_INT_CAP_INDICATOR
Definition: USBTMCPrivate.h:66
USBTMC_ERROR_FIND_RESRC_BAD_FILTER
@ USBTMC_ERROR_FIND_RESRC_BAD_FILTER
Definition: USBTMCPrivate.h:97
BULKIN_FIFO_BYTES
static const U8 BULKIN_FIFO_BYTES
Definition: USBTMCPrivate.h:91
USBTMC_BM_REQUEST_TYPE_IN_CLS_EP
static const U8 USBTMC_BM_REQUEST_TYPE_IN_CLS_EP
Definition: USBTMCPrivate.h:39
USB488_DEV_CAP_SR1
static const U8 USB488_DEV_CAP_SR1
Definition: USBTMCPrivate.h:72
INITIATE_CLEAR
@ INITIATE_CLEAR
Definition: USBTMCPrivate.h:22
USBTMC_ERROR_GET_RESRC_NOT_FOUND
@ USBTMC_ERROR_GET_RESRC_NOT_FOUND
Definition: USBTMCPrivate.h:99
USB_MAX_PORT_DEPTH
static const I32 USB_MAX_PORT_DEPTH
Definition: USBTMCPrivate.h:35
USBTMC_READSTB_INTR_RESP_LENGTH
static const U16 USBTMC_READSTB_INTR_RESP_LENGTH
Definition: USBTMCPrivate.h:42
USBTMC_ERROR_INSTRUMENT_LOCKED
@ USBTMC_ERROR_INSTRUMENT_LOCKED
Definition: USBTMCPrivate.h:109
USBTMC_ERROR_READSTB_CONTROL_RESPONSE_ERROR
@ USBTMC_ERROR_READSTB_CONTROL_RESPONSE_ERROR
Definition: USBTMCPrivate.h:107
USBTMC_ERROR_CLEAR_RESPONSE_ERROR
@ USBTMC_ERROR_CLEAR_RESPONSE_ERROR
Definition: USBTMCPrivate.h:104
IEEE488InstrumentTypes.h
Abstract IEEE488 instrument class: type definitions.
INITIATE_ABORT_BULK_OUT
@ INITIATE_ABORT_BULK_OUT
Definition: USBTMCPrivate.h:18
USBTMC_INT_CAP_LISTEN_ONLY
static const U8 USBTMC_INT_CAP_LISTEN_ONLY
Definition: USBTMCPrivate.h:64
USBTMC_INITIATE_ABORT_BULKOUT_RESP_LENGTH
static const U16 USBTMC_INITIATE_ABORT_BULKOUT_RESP_LENGTH
Definition: USBTMCPrivate.h:48
USBTMC_READSTB_CTRL_RESP_LENGTH
static const U16 USBTMC_READSTB_CTRL_RESP_LENGTH
Definition: USBTMCPrivate.h:41
USBTMC_STATUS_SUCCESS
@ USBTMC_STATUS_SUCCESS
Definition: USBTMCPrivate.h:54
GO_TO_LOCAL
@ GO_TO_LOCAL
Definition: USBTMCPrivate.h:30
REN_CONTROL
@ REN_CONTROL
Definition: USBTMCPrivate.h:29
USBTMC_DEV_CAP_TERMCHAR
static const U8 USBTMC_DEV_CAP_TERMCHAR
Definition: USBTMCPrivate.h:68
USBTMC_ERROR_READ_WRONG_MSG_SIZE
@ USBTMC_ERROR_READ_WRONG_MSG_SIZE
Definition: USBTMCPrivate.h:103
INITIATE_ABORT_BULK_IN
@ INITIATE_ABORT_BULK_IN
Definition: USBTMCPrivate.h:20
USBTMC_INITIATE_ABORT_BULKIN_RESP_LENGTH
static const U16 USBTMC_INITIATE_ABORT_BULKIN_RESP_LENGTH
Definition: USBTMCPrivate.h:46
USBTMC_USB488
static const U8 USBTMC_USB488
Definition: USBTMCPrivate.h:14
USBTMC_STATUS_PENDING
@ USBTMC_STATUS_PENDING
Definition: USBTMCPrivate.h:55
REQUEST_DEV_DEP_MSG_IN
static const U8 REQUEST_DEV_DEP_MSG_IN
Definition: USBTMCPrivate.h:82
USBTMC_STATUS_INTERRUPT_IN_BUSY
@ USBTMC_STATUS_INTERRUPT_IN_BUSY
Definition: USBTMCPrivate.h:56
GET_CAPABILITIES
@ GET_CAPABILITIES
Definition: USBTMCPrivate.h:24
TRIGGER
static const U8 TRIGGER
Definition: USBTMCPrivate.h:84
USBTMC_BM_REQUEST_TYPE_IN_CLS_IF
static const U8 USBTMC_BM_REQUEST_TYPE_IN_CLS_IF
Definition: USBTMCPrivate.h:38
USBTMC_BULK_TRIGGER_MSG_SIZE
static const I32 USBTMC_BULK_TRIGGER_MSG_SIZE
Definition: USBTMCPrivate.h:78
READ_STATUS_BYTE
@ READ_STATUS_BYTE
Definition: USBTMCPrivate.h:28
USBTMC_BULK_HEADER_SIZE
static const I32 USBTMC_BULK_HEADER_SIZE
Definition: USBTMCPrivate.h:76
USBTMC_STATUS_TRANSFER_NOT_IN_PROGRESS
@ USBTMC_STATUS_TRANSFER_NOT_IN_PROGRESS
Definition: USBTMCPrivate.h:58
SUBCLASS_USBTMC
static const U8 SUBCLASS_USBTMC
Constants for USBTMC and USB488.
Definition: USBTMCPrivate.h:13
USBTMC_STATUS_SPLIT_NOT_IN_PROGRESS
@ USBTMC_STATUS_SPLIT_NOT_IN_PROGRESS
Definition: USBTMCPrivate.h:59
USBTMC_ERROR_READSTB_INTERRUPT_RESPONSE_ERROR
@ USBTMC_ERROR_READSTB_INTERRUPT_RESPONSE_ERROR
Definition: USBTMCPrivate.h:108
USBTMC_ERROR_WRITE_INVALID_TRANSFER_COUNT
@ USBTMC_ERROR_WRITE_INVALID_TRANSFER_COUNT
Definition: USBTMCPrivate.h:100
TERM_CHAR_ENABLED
static const U8 TERM_CHAR_ENABLED
Definition: USBTMCPrivate.h:88
MTL::Instrument
Definition: THM1176.h:75
DEV_DEP_MSG_OUT
static const U8 DEV_DEP_MSG_OUT
Definition: USBTMCPrivate.h:81
USBTMC_READ_BUFFER_SIZE
static const U16 USBTMC_READ_BUFFER_SIZE
Definition: USBTMCPrivate.h:40
INDICATOR_PULSE
@ INDICATOR_PULSE
Definition: USBTMCPrivate.h:25
USBTMC_ERROR_CLEAR_BULKIN_RESPONSE_ERROR
@ USBTMC_ERROR_CLEAR_BULKIN_RESPONSE_ERROR
Definition: USBTMCPrivate.h:105
LOCAL_LOCKOUT
@ LOCAL_LOCKOUT
Definition: USBTMCPrivate.h:31
I32
int I32
32-bit signed integer.
Definition: OSDefines.h:28
CHECK_CLEAR_STATUS
@ CHECK_CLEAR_STATUS
Definition: USBTMCPrivate.h:23
USBTMC_CHECK_CLEAR_STATUS_RESP_LENGTH
static const U16 USBTMC_CHECK_CLEAR_STATUS_RESP_LENGTH
Definition: USBTMCPrivate.h:44
USB_CONTROL_ENDPOINT_NUMBER
static const U16 USB_CONTROL_ENDPOINT_NUMBER
Definition: USBTMCPrivate.h:36
USBTMC_ERROR_FIND_RESRC_NO_USBTMC_USB488
@ USBTMC_ERROR_FIND_RESRC_NO_USBTMC_USB488
Definition: USBTMCPrivate.h:98
USBTMC_INITIATE_CLEAR_RESP_LENGTH
static const U16 USBTMC_INITIATE_CLEAR_RESP_LENGTH
Definition: USBTMCPrivate.h:43
USBTMC_CHECK_ABORT_BULKOUT_STATUS_RESP_LENGTH
static const U16 USBTMC_CHECK_ABORT_BULKOUT_STATUS_RESP_LENGTH
Definition: USBTMCPrivate.h:49
DEV_DEP_MSG_IN
static const U8 DEV_DEP_MSG_IN
Definition: USBTMCPrivate.h:83
USBTMC_CHECK_CLEAR_STATUS_INTERVAL
static const U32 USBTMC_CHECK_CLEAR_STATUS_INTERVAL
Definition: USBTMCPrivate.h:45
CHECK_ABORT_BULK_IN_STATUS
@ CHECK_ABORT_BULK_IN_STATUS
Definition: USBTMCPrivate.h:21
USB488_DEV_CAP_DT1
static const U8 USB488_DEV_CAP_DT1
Definition: USBTMCPrivate.h:70
USBTMC_CHECK_ABORT_BULKIN_STATUS_RESP_LENGTH
static const U16 USBTMC_CHECK_ABORT_BULKIN_STATUS_RESP_LENGTH
Definition: USBTMCPrivate.h:47
USBTMC_ERROR_READ_INVALID_HEADER
@ USBTMC_ERROR_READ_INVALID_HEADER
Definition: USBTMCPrivate.h:102
U8
unsigned char U8
Unsigned byte.
Definition: OSDefines.h:30
USBTMC_ERROR_READ_INVALID_BUFFER_FOR_APPEND
@ USBTMC_ERROR_READ_INVALID_BUFFER_FOR_APPEND
Definition: USBTMCPrivate.h:101
USB488_DEV_CAP_RL1
static const U8 USB488_DEV_CAP_RL1
Definition: USBTMCPrivate.h:71
U16
unsigned short U16
16-bit unsigned integer.
Definition: OSDefines.h:31
USBTMC_ERROR_CLEAR_BULKOUT_RESPONSE_ERROR
@ USBTMC_ERROR_CLEAR_BULKOUT_RESPONSE_ERROR
Definition: USBTMCPrivate.h:106
USBTMC_INT_CAP_TALK_ONLY
static const U8 USBTMC_INT_CAP_TALK_ONLY
Definition: USBTMCPrivate.h:65
USBTMC_ERROR_DEVICE_NOT_OPEN
@ USBTMC_ERROR_DEVICE_NOT_OPEN
Definition: USBTMCPrivate.h:110
USBTMC_ERROR_EXPLANATION
static const char * USBTMC_ERROR_EXPLANATION[]
Error explanations.
Definition: USBTMCPrivate.h:114
USBTMCError
USBTMCError
Error codes. Complementary to libusb errors (0 to -99).
Definition: USBTMCPrivate.h:96
U32
unsigned int U32
32-bit unsigned integer.
Definition: OSDefines.h:32
USBTMC_STATUS_SPLIT_IN_PROGRESS
@ USBTMC_STATUS_SPLIT_IN_PROGRESS
Definition: USBTMCPrivate.h:60
CHECK_ABORT_BULK_OUT_STATUS
@ CHECK_ABORT_BULK_OUT_STATUS
Definition: USBTMCPrivate.h:19
USB_DESCRIPTOR_STRING_LENGTH
static const U32 USB_DESCRIPTOR_STRING_LENGTH
Definition: USBTMCPrivate.h:37