Skip to content

Commit 2110e27

Browse files
authored
Merge pull request hathach#1531 from pigrew/usbtmc_highspeed
USBTMC: Fixes and support for high-speed USB
2 parents 868f2bc + 54250d7 commit 2110e27

File tree

5 files changed

+137
-59
lines changed

5 files changed

+137
-59
lines changed

examples/device/usbtmc/src/usb_descriptors.c

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
#define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \
3838
_PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4) )
3939

40+
#define USB_VID 0xCafe
41+
#define USB_BCD 0x0200
42+
4043
//--------------------------------------------------------------------+
4144
// Device Descriptors
4245
//--------------------------------------------------------------------+
@@ -51,9 +54,9 @@ tusb_desc_device_t const desc_device =
5154

5255
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
5356

54-
.idVendor = 0xCafe,
57+
.idVendor = USB_VID,
5558
.idProduct = USB_PID,
56-
.bcdDevice = 0x0100,
59+
.bcdDevice = USB_BCD,
5760

5861
.iManufacturer = 0x01,
5962
.iProduct = 0x02,
@@ -75,22 +78,22 @@ uint8_t const * tud_descriptor_device_cb(void)
7578

7679
#if defined(CFG_TUD_USBTMC)
7780

78-
# define TUD_USBTMC_DESC_MAIN(_itfnum,_bNumEndpoints) \
81+
# define TUD_USBTMC_DESC_MAIN(_itfnum,_bNumEndpoints, _bulkMaxPacketLength) \
7982
TUD_USBTMC_IF_DESCRIPTOR(_itfnum, _bNumEndpoints, /*_stridx = */ 4u, TUD_USBTMC_PROTOCOL_USB488), \
80-
TUD_USBTMC_BULK_DESCRIPTORS(/* OUT = */0x01, /* IN = */ 0x81, /* packet size = */USBTMCD_MAX_PACKET_SIZE)
83+
TUD_USBTMC_BULK_DESCRIPTORS(/* OUT = */0x01, /* IN = */ 0x81, /* packet size = */_bulkMaxPacketLength)
8184

8285
#if CFG_TUD_USBTMC_ENABLE_INT_EP
8386
// USBTMC Interrupt xfer always has length of 2, but we use epMaxSize=8 for
8487
// compatibility with mcus that only allow 8, 16, 32 or 64 for FS endpoints
85-
# define TUD_USBTMC_DESC(_itfnum) \
86-
TUD_USBTMC_DESC_MAIN(_itfnum, /* _epCount = */ 3), \
88+
# define TUD_USBTMC_DESC(_itfnum, _bulkMaxPacketLength) \
89+
TUD_USBTMC_DESC_MAIN(_itfnum, /* _epCount = */ 3, _bulkMaxPacketLength), \
8790
TUD_USBTMC_INT_DESCRIPTOR(/* INT ep # */ 0x82, /* epMaxSize = */ 8, /* bInterval = */16u )
8891
# define TUD_USBTMC_DESC_LEN (TUD_USBTMC_IF_DESCRIPTOR_LEN + TUD_USBTMC_BULK_DESCRIPTORS_LEN + TUD_USBTMC_INT_DESCRIPTOR_LEN)
8992

9093
#else
9194

92-
# define TUD_USBTMC_DESC(_itfnum) \
93-
TUD_USBTMC_DESC_MAIN(_itfnum, /* _epCount = */ 2u)
95+
# define TUD_USBTMC_DESC(_itfnum, _bulkMaxPacketLength) \
96+
TUD_USBTMC_DESC_MAIN(_itfnum, /* _epCount = */ 2u, _bulkMaxPacketLength)
9497
# define TUD_USBTMC_DESC_LEN (TUD_USBTMC_IF_DESCRIPTOR_LEN + TUD_USBTMC_BULK_DESCRIPTORS_LEN)
9598

9699
#endif /* CFG_TUD_USBTMC_ENABLE_INT_EP */
@@ -119,21 +122,66 @@ enum
119122
#endif
120123

121124

122-
uint8_t const desc_configuration[] =
125+
uint8_t const desc_fs_configuration[] =
123126
{
124127
// Config number, interface count, string index, total length, attribute, power in mA
125128
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100),
126129

127-
TUD_USBTMC_DESC(ITF_NUM_USBTMC),
130+
TUD_USBTMC_DESC(ITF_NUM_USBTMC, /* _bulkMaxPacketLength = */ 64),
128131
};
129132

133+
#if TUD_OPT_HIGH_SPEED
134+
135+
uint8_t const desc_hs_configuration[] =
136+
{
137+
// Config number, interface count, string index, total length, attribute, power in mA
138+
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100),
139+
140+
TUD_USBTMC_DESC(ITF_NUM_USBTMC, /* _bulkMaxPacketLength = */ 512),
141+
};
142+
143+
// other speed configuration
144+
uint8_t desc_other_speed_config[CONFIG_TOTAL_LEN];
145+
146+
// device qualifier is mostly similar to device descriptor since we don't change configuration based on speed
147+
tusb_desc_device_qualifier_t const desc_device_qualifier =
148+
{
149+
.bLength = sizeof(tusb_desc_device_qualifier_t),
150+
.bDescriptorType = TUSB_DESC_DEVICE_QUALIFIER,
151+
.bcdUSB = USB_BCD,
152+
153+
.bDeviceClass = 0x00,
154+
.bDeviceSubClass = 0x00,
155+
.bDeviceProtocol = 0x00,
156+
157+
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
158+
.bNumConfigurations = 0x01,
159+
.bReserved = 0x00
160+
};
161+
162+
// Invoked when received GET DEVICE QUALIFIER DESCRIPTOR request
163+
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete.
164+
// device_qualifier descriptor describes information about a high-speed capable device that would
165+
// change if the device were operating at the other speed. If not highspeed capable stall this request.
166+
uint8_t const* tud_descriptor_device_qualifier_cb(void)
167+
{
168+
return (uint8_t const*) &desc_device_qualifier;
169+
}
170+
171+
#endif
172+
130173
// Invoked when received GET CONFIGURATION DESCRIPTOR
131174
// Application return pointer to descriptor
132175
// Descriptor contents must exist long enough for transfer to complete
133176
uint8_t const * tud_descriptor_configuration_cb(uint8_t index)
134177
{
135178
(void) index; // for multiple configurations
136-
return desc_configuration;
179+
#if TUD_OPT_HIGH_SPEED
180+
// Although we are highspeed, host may be fullspeed.
181+
return (tud_speed_get() == TUSB_SPEED_HIGH) ? desc_hs_configuration : desc_fs_configuration;
182+
#else
183+
return desc_fs_configuration;
184+
#endif
137185
}
138186

139187
//--------------------------------------------------------------------+

examples/device/usbtmc/visaQuery.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python3
22

3-
import visa
3+
import pyvisa
44
import time
55
import sys
66

@@ -54,9 +54,9 @@ def test_srq():
5454
assert (inst.read_stb() == 0)
5555
inst.write("123")
5656

57-
#inst.enable_event(visa.constants.VI_EVENT_SERVICE_REQ, visa.constants.VI_QUEUE)
58-
#waitrsp = inst.wait_on_event(visa.constants.VI_EVENT_SERVICE_REQ, 5000)
59-
#inst.discard_events(visa.constants.VI_EVENT_SERVICE_REQ, visa.constants.VI_QUEUE)
57+
#inst.enable_event(pyvisa.constants.VI_EVENT_SERVICE_REQ, pyvisa.constants.VI_QUEUE)
58+
#waitrsp = inst.wait_on_event(pyvisa.constants.VI_EVENT_SERVICE_REQ, 5000)
59+
#inst.discard_events(pyvisa.constants.VI_EVENT_SERVICE_REQ, pyvisa.constants.VI_QUEUE)
6060
#inst.wait_for_srq()
6161
time.sleep(0.3)
6262
stb = inst.read_stb()
@@ -77,8 +77,8 @@ def test_read_timeout():
7777
t0 = time.monotonic()
7878
try:
7979
rsp = inst.read()
80-
assert(false), "Read should have resulted in timeout"
81-
except visa.VisaIOError:
80+
assert(False), "Read should have resulted in timeout"
81+
except pyvisa.VisaIOError:
8282
print(" Got expected exception")
8383
t = time.monotonic() - t0
8484
assert ((t*1000.0) > (inst.timeout - 300))
@@ -99,23 +99,27 @@ def test_abort_in():
9999
t0 = time.monotonic()
100100
try:
101101
rsp = inst.read()
102-
assert(false), "Read should have resulted in timeout"
103-
except visa.VisaIOError:
102+
assert(False), "Read should have resulted in timeout"
103+
except pyvisa.VisaIOError:
104104
print(" Got expected exception")
105105
t = time.monotonic() - t0
106106
assert ((t*1000.0) > (inst.timeout - 300))
107107
assert ((t*1000.0) < (inst.timeout + 300))
108108
print(f" Delay was {t:0.3}")
109-
# Response is still in queue, so send a clear (to be more helpful to the next test)
109+
# Response is still in queue, so read it out (to be more helpful to the next test)
110110
inst.timeout = 800
111111
y = inst.read()
112112
assert(y == "xxx\r\n")
113113

114114
def test_indicate():
115115
# perform indicator pulse
116-
usb_iface = inst.get_visa_attribute(visa.constants.VI_ATTR_USB_INTFC_NUM)
116+
usb_iface = inst.get_visa_attribute(pyvisa.constants.VI_ATTR_USB_INTFC_NUM)
117117
retv = inst.control_in(request_type_bitmap_field=0xA1, request_id=64, request_value=0x0000, index=usb_iface, length=0x0001)
118-
assert((retv[1] == visa.constants.StatusCode(0)) and (retv[0] == b'\x01')), f"indicator pulse failed: retv={retv}"
118+
# pyvisa used to return (statuscode,bytes), but now only returns bytes, so we need to handle both cases
119+
if(isinstance(retv,bytes)):
120+
assert(retv == b'\x01')
121+
else:
122+
assert((retv[1] == pyvisa.constants.StatusCode(0)) and (retv[0] == b'\x01')), f"indicator pulse failed: retv={retv}"
119123

120124

121125
def test_multi_read():
@@ -131,19 +135,19 @@ def test_multi_read():
131135
#inst.chunk_size = old_chunk_size
132136

133137
def test_stall_ep0():
134-
usb_iface = inst.get_visa_attribute(visa.constants.VI_ATTR_USB_INTFC_NUM)
138+
usb_iface = inst.get_visa_attribute(pyvisa.constants.VI_ATTR_USB_INTFC_NUM)
135139
inst.read_stb()
136140
# This is an invalid request, should create stall.
137141
try:
138142
retv = inst.control_in(request_type_bitmap_field=0xA1, request_id=60, request_value=0x0000, index=usb_iface, length=0x0001)
139-
assert false
140-
except visa.VisaIOError:
143+
assert(False)
144+
except pyvisa.VisaIOError:
141145
pass
142146

143147
assert (inst.read_stb() == 0)
144148

145149

146-
rm = visa.ResourceManager()
150+
rm = pyvisa.ResourceManager()
147151
reslist = rm.list_resources("USB?::?*::INSTR")
148152
print(reslist)
149153

@@ -167,7 +171,6 @@ def test_stall_ep0():
167171
print("+ multi read")
168172
test_multi_read()
169173

170-
171174
print("+ echo delay=0")
172175
inst.write("delay 0")
173176
test_echo(1,175)

src/class/usbtmc/usbtmc.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,10 @@ typedef enum {
189189
USBTMC_STATUS_FAILED = 0x80,
190190
USBTMC_STATUS_TRANSFER_NOT_IN_PROGRESS = 0x81,
191191
USBTMC_STATUS_SPLIT_NOT_IN_PROGRESS = 0x82,
192-
USBTMC_STATUS_SPLIT_IN_PROGRESS = 0x83
192+
USBTMC_STATUS_SPLIT_IN_PROGRESS = 0x83,
193+
194+
/****** USBTMC 488 *************/
195+
USB488_STATUS_INTERRUPT_IN_BUSY = 0x20
193196
} usbtmc_status_enum;
194197

195198
/************************************************************

0 commit comments

Comments
 (0)