summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorNathan Conrad <[email protected]>2019-09-21 21:46:46 -0400
committerNathan Conrad <[email protected]>2019-09-21 21:46:46 -0400
commitfa5b5e4561869e26832d817b4a709ba31e666143 (patch)
treecd529acd1fe0daf90f3fd5d60778f9d0937a5ca3 /examples
parent2aa10daf26c03fc9563e8e13f6206f14d7b8b146 (diff)
Lots of updates (especially error handling)
Diffstat (limited to 'examples')
-rw-r--r--examples/device/usbtmc/src/usb_descriptors.c4
-rw-r--r--examples/device/usbtmc/src/usbtmc_app.c51
-rw-r--r--examples/device/usbtmc/visaQuery.py70
3 files changed, 104 insertions, 21 deletions
diff --git a/examples/device/usbtmc/src/usb_descriptors.c b/examples/device/usbtmc/src/usb_descriptors.c
index 45e1001ea..1175ebb99 100644
--- a/examples/device/usbtmc/src/usb_descriptors.c
+++ b/examples/device/usbtmc/src/usb_descriptors.c
@@ -107,13 +107,13 @@ uint8_t const * tud_hid_descriptor_report_cb(void)
# define TUD_USBTMC_DESC_MAIN(_itfnum,_bNumEndpoints) \
TUD_USBTMC_IF_DESCRIPTOR(_itfnum, _bNumEndpoints, /*_stridx = */ 4u, TUD_USBTMC_PROTOCOL_USB488), \
- TUD_USBTMC_BULK_DESCRIPTORS(/* OUT = */0x03, /* IN = */ 0x83, /* packet size = */USBTMCD_MAX_PACKET_SIZE)
+ TUD_USBTMC_BULK_DESCRIPTORS(/* OUT = */0x01, /* IN = */ 0x81, /* packet size = */USBTMCD_MAX_PACKET_SIZE)
#if defined(CFG_TUD_USBTMC_ENABLE_INT_EP)
// Interrupt endpoint should be 2 bytes on a FS USB link
# define TUD_USBTMC_DESC(_itfnum) \
TUD_USBTMC_DESC_MAIN(_itfnum, /* _epCount = */ 3), \
- TUD_USBTMC_INT_DESCRIPTOR(/* INT ep # */ 0x84, /* epMaxSize = */ 2, /* bInterval = */16u )
+ TUD_USBTMC_INT_DESCRIPTOR(/* INT ep # */ 0x82, /* epMaxSize = */ 2, /* bInterval = */16u )
# define USBTMC_DESC_LEN (TUD_USBTMC_IF_DESCRIPTOR_LEN + TUD_USBTMC_BULK_DESCRIPTORS_LEN + TUD_USBTMC_INT_DESCRIPTOR_LEN)
#else
diff --git a/examples/device/usbtmc/src/usbtmc_app.c b/examples/device/usbtmc/src/usbtmc_app.c
index f8c350f68..495746e07 100644
--- a/examples/device/usbtmc/src/usbtmc_app.c
+++ b/examples/device/usbtmc/src/usbtmc_app.c
@@ -84,6 +84,7 @@ static volatile uint32_t idnQuery;
static uint32_t resp_delay = 125u; // Adjustable delay, to allow for better testing
static size_t buffer_len;
+static size_t buffer_tx_ix; // for transmitting using multiple transfers
static uint8_t buffer[225]; // A few packets long should be enough.
@@ -108,6 +109,11 @@ bool tud_usbtmc_app_msgBulkOut_start_cb(uint8_t rhport, usbtmc_msg_request_dev_d
(void)rhport;
(void)msgHeader;
buffer_len = 0;
+ if(msgHeader->TransferSize > sizeof(buffer))
+ {
+
+ return false;
+ }
return true;
}
@@ -122,6 +128,10 @@ bool tud_usbtmc_app_msg_data_cb(uint8_t rhport, void *data, size_t len, bool tra
memcpy(&(buffer[buffer_len]), data, len);
buffer_len += len;
}
+ else
+ {
+ return false; // buffer overflow!
+ }
queryState = transfer_complete;
idnQuery = 0;
@@ -145,8 +155,13 @@ bool tud_usbtmc_app_msg_data_cb(uint8_t rhport, void *data, size_t len, bool tra
bool tud_usbtmc_app_msgBulkIn_complete_cb(uint8_t rhport)
{
(void)rhport;
-
- status &= (uint8_t)~(IEEE4882_STB_MAV); // clear MAV
+ if((buffer_tx_ix == buffer_len) || idnQuery) // done
+ {
+ status &= (uint8_t)~(IEEE4882_STB_MAV); // clear MAV
+ queryState = 0;
+ bulkInStarted = 0;
+ buffer_tx_ix = 0;
+ }
return true;
}
@@ -161,15 +176,25 @@ bool tud_usbtmc_app_msgBulkIn_request_cb(uint8_t rhport, usbtmc_msg_request_dev_
rspMsg.header.bTag = request->header.bTag,
rspMsg.header.bTagInverse = request->header.bTagInverse;
msgReqLen = request->TransferSize;
+
#ifdef xDEBUG
uart_tx_str_sync("MSG_IN_DATA: Requested!\r\n");
#endif
- TU_ASSERT(bulkInStarted == 0);
- bulkInStarted = 1;
-
- // > If a USBTMC interface receives a Bulk-IN request prior to receiving a USBTMC command message
- // that expects a response, the device must NAK the request
+ if(queryState == 0 || (buffer_tx_ix == 0))
+ {
+ TU_ASSERT(bulkInStarted == 0);
+ bulkInStarted = 1;
+ // > If a USBTMC interface receives a Bulk-IN request prior to receiving a USBTMC command message
+ // that expects a response, the device must NAK the request (*not stall*)
+ }
+ else
+ {
+ size_t txlen = tu_min32(buffer_len-buffer_tx_ix,msgReqLen);
+ usbtmcd_transmit_dev_msg_data(rhport, &buffer[buffer_tx_ix], txlen,
+ (buffer_tx_ix+txlen) == buffer_len, false);
+ buffer_tx_ix += txlen;
+ }
// Always return true indicating not to stall the EP.
return true;
}
@@ -197,17 +222,17 @@ void usbtmc_app_task_iter(void) {
}
break;
case 4: // time to transmit;
- if(bulkInStarted) {
- queryState = 0;
- bulkInStarted = 0;
-
+ if(bulkInStarted && (buffer_tx_ix == 0)) {
if(idnQuery)
{
- usbtmcd_transmit_dev_msg_data(rhport, idn, tu_min32(sizeof(idn)-1,msgReqLen),false);
+ usbtmcd_transmit_dev_msg_data(rhport, idn, tu_min32(sizeof(idn)-1,msgReqLen),true,false);
+ queryState = 0;
+ bulkInStarted = 0;
}
else
{
- usbtmcd_transmit_dev_msg_data(rhport, buffer, tu_min32(buffer_len,msgReqLen),false);
+ buffer_tx_ix = tu_min32(buffer_len,msgReqLen);
+ usbtmcd_transmit_dev_msg_data(rhport, buffer, buffer_tx_ix, buffer_tx_ix == buffer_len, false);
}
// MAV is cleared in the transfer complete callback.
}
diff --git a/examples/device/usbtmc/visaQuery.py b/examples/device/usbtmc/visaQuery.py
index 368b66053..4d4b50c4b 100644
--- a/examples/device/usbtmc/visaQuery.py
+++ b/examples/device/usbtmc/visaQuery.py
@@ -6,7 +6,8 @@ import sys
def test_idn():
idn = inst.query("*idn?");
- assert idn == "TinyUSB,ModelNumber,SerialNumber,FirmwareVer123456\r\n"
+ assert (idn == "TinyUSB,ModelNumber,SerialNumber,FirmwareVer123456\r\n")
+ assert (inst.is_4882_compliant)
def test_echo(m,n):
longstr = "0123456789abcdefghijklmnopqrstuvwxyz" * 50
@@ -34,6 +35,8 @@ def test_trig():
def test_mav():
+ inst.write("delay 50")
+ inst.read_stb() # clear STB
assert (inst.read_stb() == 0)
inst.write("123")
time.sleep(0.3)
@@ -60,8 +63,6 @@ def test_srq():
rsp = inst.read()
assert(rsp == "123\r\n")
-
-
def test_read_timeout():
inst.timeout = 500
@@ -78,7 +79,53 @@ def test_read_timeout():
t = time.time() - t0
assert ((t*1000.0) > (inst.timeout - 300))
assert ((t*1000.0) < (inst.timeout + 300))
- print(f"Delay was {t}")
+ print(f"Delay was {t:0.3}")
+ # Response is still in queue, so send a clear (to be more helpful to the next test)
+ inst.clear()
+ time.sleep(0.3)
+ assert(0 == (inst.read_stb() & 0x10)), "MAV not reset after clear"
+
+def test_abort_in():
+ inst.timeout = 200
+ # First read with no MAV
+ inst.read_stb()
+ assert (inst.read_stb() == 0)
+ inst.write("delay 500")
+ inst.write("xxx")
+ t0 = time.time()
+ try:
+ rsp = inst.read()
+ assert(false), "Read should have resulted in timeout"
+ except visa.VisaIOError:
+ print("Got expected exception")
+ t = time.time() - t0
+ assert ((t*1000.0) > (inst.timeout - 300))
+ assert ((t*1000.0) < (inst.timeout + 300))
+ print(f"Delay was {t:0.3}")
+ # Response is still in queue, so send a clear (to be more helpful to the next test)
+ inst.timeout = 800
+ y = inst.read()
+ assert(y == "xxx\r\n")
+
+def test_indicate():
+ # perform indicator pulse
+ usb_iface = inst.get_visa_attribute(visa.constants.VI_ATTR_USB_INTFC_NUM)
+ retv = inst.control_in(request_type_bitmap_field=0xA1, request_id=64, request_value=0x0000, index=usb_iface, length=0x0001)
+ assert((retv[1] == visa.constants.StatusCode(0)) and (retv[0] == b'\x01')), f"indicator pulse failed: retv={retv}"
+
+
+def test_multi_read():
+ old_chunk_size = inst.chunk_size
+ longstr = "0123456789abcdefghijklmnopqrstuvwxyz" * 10
+ timeout = 10
+ x = longstr[0:174]
+ inst.chunk_size = 50 # Seems chunk size only applies to read but not write
+ inst.write(x)
+ # I'm not sure how to request just the remaining bit using a max count... so just read it all.
+ y = inst.read()
+ assert (x + "\r\n" == y)
+ #inst.chunk_size = old_chunk_size
+
rm = visa.ResourceManager("/c/Windows/system32/visa64.dll")
reslist = rm.list_resources("USB?::?*::INSTR")
@@ -89,12 +136,20 @@ if (len(reslist) == 0):
inst = rm.open_resource(reslist[0]);
inst.timeout = 3000
+
inst.clear()
print("+ IDN")
test_idn()
-inst.timeout = 3000
+print("+test abort in")
+test_abort_in()
+
+
+inst.timeout = 2000
+
+print("+ multi read")
+test_multi_read()
print("+ echo delay=0")
@@ -110,7 +165,7 @@ inst.write("delay 150")
test_echo(53,76)
test_echo(165,170)
-print("+ Read timeout (no MAV")
+print("+ Read timeout (no MAV)")
test_read_timeout()
print("+ MAV")
@@ -119,6 +174,9 @@ test_mav()
print("+ SRQ")
test_srq()
+print("+ indicate")
+test_indicate()
+
print("+ TRIG")
test_trig()