6464// USBTMC 3.2.2 error conditions not strictly followed
6565// No local lock-out, REN, or GTL.
6666// Clear message available status byte at the correct time? (488 4.3.1.3)
67-
67+ // Ability to defer status byte transmission
68+ // Transmission of status byte in response to USB488 SRQ condition
6869
6970#include "tusb_option.h"
7071
8081static char logMsg [150 ];
8182#endif
8283
84+ // Buffer size must be an exact multiple of the max packet size for both
85+ // bulk (up to 64 bytes for FS, 512 bytes for HS). In addation, this driver
86+ // imposes a minimum buffer size of 32 bytes.
87+ #define USBTMCD_BUFFER_SIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
88+
8389/*
8490 * The state machine does not allow simultaneous reading and writing. This is
8591 * consistent with USBTMC.
@@ -120,9 +126,12 @@ typedef struct
120126 uint8_t ep_int_in ;
121127 // IN buffer is only used for first packet, not the remainder
122128 // in order to deal with prepending header
123- CFG_TUSB_MEM_ALIGN uint8_t ep_bulk_in_buf [USBTMCD_MAX_PACKET_SIZE ];
129+ CFG_TUSB_MEM_ALIGN uint8_t ep_bulk_in_buf [USBTMCD_BUFFER_SIZE ];
130+ uint32_t ep_bulk_in_wMaxPacketSize ;
124131 // OUT buffer receives one packet at a time
125- CFG_TUSB_MEM_ALIGN uint8_t ep_bulk_out_buf [USBTMCD_MAX_PACKET_SIZE ];
132+ CFG_TUSB_MEM_ALIGN uint8_t ep_bulk_out_buf [USBTMCD_BUFFER_SIZE ];
133+ uint32_t ep_bulk_out_wMaxPacketSize ;
134+
126135 uint32_t transfer_size_remaining ; // also used for requested length for bulk IN.
127136 uint32_t transfer_size_sent ; // To keep track of data bytes that have been queued in FIFO (not header bytes)
128137
@@ -139,19 +148,15 @@ CFG_TUSB_MEM_SECTION static usbtmc_interface_state_t usbtmc_state =
139148 .itf_id = 0xFF ,
140149};
141150
142- // We need all headers to fit in a single packet in this implementation.
143- TU_VERIFY_STATIC (USBTMCD_MAX_PACKET_SIZE >= 32u ,"USBTMC dev EP packet size too small" );
144- TU_VERIFY_STATIC (
145- (sizeof (usbtmc_state .ep_bulk_in_buf ) % USBTMCD_MAX_PACKET_SIZE ) == 0 ,
146- "packet buffer must be a multiple of the packet size" );
151+ // We need all headers to fit in a single packet in this implementation, 32 bytes will fit all standard USBTMC headers
152+ TU_VERIFY_STATIC (USBTMCD_BUFFER_SIZE >= 32u ,"USBTMC dev buffer size too small" );
147153
148154static bool handle_devMsgOutStart (uint8_t rhport , void * data , size_t len );
149155static bool handle_devMsgOut (uint8_t rhport , void * data , size_t len , size_t packetLen );
150156
151157static uint8_t termChar ;
152158static uint8_t termCharRequested = false;
153159
154-
155160osal_mutex_def_t usbtmcLockBuffer ;
156161static osal_mutex_t usbtmcLock ;
157162
@@ -282,12 +287,15 @@ uint16_t usbtmcd_open_cb(uint8_t rhport, tusb_desc_interface_t const * itf_desc,
282287 tusb_desc_endpoint_t const * ep_desc = (tusb_desc_endpoint_t const * )p_desc ;
283288 switch (ep_desc -> bmAttributes .xfer ) {
284289 case TUSB_XFER_BULK :
285- TU_ASSERT (tu_edpt_packet_size (ep_desc ) == USBTMCD_MAX_PACKET_SIZE , 0 );
290+ // Ensure buffer is an exact multiple of the maxPacketSize
291+ TU_ASSERT ((USBTMCD_BUFFER_SIZE % tu_edpt_packet_size (ep_desc )) == 0 , 0 );
286292 if (tu_edpt_dir (ep_desc -> bEndpointAddress ) == TUSB_DIR_IN )
287293 {
288294 usbtmc_state .ep_bulk_in = ep_desc -> bEndpointAddress ;
295+ usbtmc_state .ep_bulk_in_wMaxPacketSize = tu_edpt_packet_size (ep_desc );
289296 } else {
290297 usbtmc_state .ep_bulk_out = ep_desc -> bEndpointAddress ;
298+ usbtmc_state .ep_bulk_out_wMaxPacketSize = tu_edpt_packet_size (ep_desc );
291299 }
292300
293301 break ;
@@ -395,7 +403,7 @@ static bool handle_devMsgOut(uint8_t rhport, void *data, size_t len, size_t pack
395403 // return true upon failure, as we can assume error is being handled elsewhere.
396404 TU_VERIFY (usbtmc_state .state == STATE_RCV ,true);
397405
398- bool shortPacket = (packetLen < USBTMCD_MAX_PACKET_SIZE );
406+ bool shortPacket = (packetLen < usbtmc_state . ep_bulk_out_wMaxPacketSize );
399407
400408 // Packet is to be considered complete when we get enough data or at a short packet.
401409 bool atEnd = false;
@@ -522,7 +530,7 @@ bool usbtmcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint
522530 break ;
523531
524532 case STATE_TX_INITIATED :
525- if (usbtmc_state .transfer_size_remaining >=sizeof (usbtmc_state .ep_bulk_in_buf ))
533+ if (usbtmc_state .transfer_size_remaining >= sizeof (usbtmc_state .ep_bulk_in_buf ))
526534 {
527535 // FIXME! This removes const below!
528536 TU_VERIFY ( usbd_edpt_xfer (rhport , usbtmc_state .ep_bulk_in ,
@@ -539,7 +547,7 @@ bool usbtmcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint
539547 usbtmc_state .transfer_size_remaining = 0 ;
540548 usbtmc_state .devInBuffer = NULL ;
541549 TU_VERIFY ( usbd_edpt_xfer (rhport , usbtmc_state .ep_bulk_in , usbtmc_state .ep_bulk_in_buf , (uint16_t )packetLen ) );
542- if (((packetLen % USBTMCD_MAX_PACKET_SIZE ) != 0 ) || (packetLen == 0 ))
550+ if (((packetLen % usbtmc_state . ep_bulk_in_wMaxPacketSize ) != 0 ) || (packetLen == 0 ))
543551 {
544552 usbtmc_state .state = STATE_TX_SHORTED ;
545553 }
@@ -680,7 +688,7 @@ bool usbtmcd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request
680688 usbtmc_state .transfer_size_remaining = 0u ;
681689 // Check if we've queued a short packet
682690 criticalEnter ();
683- usbtmc_state .state = ((usbtmc_state .transfer_size_sent % USBTMCD_MAX_PACKET_SIZE ) == 0 ) ?
691+ usbtmc_state .state = ((usbtmc_state .transfer_size_sent % usbtmc_state . ep_bulk_in_wMaxPacketSize ) == 0 ) ?
684692 STATE_ABORTING_BULK_IN : STATE_ABORTING_BULK_IN_SHORTED ;
685693 criticalLeave ();
686694 if (usbtmc_state .transfer_size_sent == 0 )
0 commit comments