64
64
// USBTMC 3.2.2 error conditions not strictly followed
65
65
// No local lock-out, REN, or GTL.
66
66
// 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
68
69
69
70
#include "tusb_option.h"
70
71
80
81
static char logMsg [150 ];
81
82
#endif
82
83
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
+
83
89
/*
84
90
* The state machine does not allow simultaneous reading and writing. This is
85
91
* consistent with USBTMC.
@@ -120,9 +126,12 @@ typedef struct
120
126
uint8_t ep_int_in ;
121
127
// IN buffer is only used for first packet, not the remainder
122
128
// 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 ;
124
131
// 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
+
126
135
uint32_t transfer_size_remaining ; // also used for requested length for bulk IN.
127
136
uint32_t transfer_size_sent ; // To keep track of data bytes that have been queued in FIFO (not header bytes)
128
137
@@ -139,19 +148,15 @@ CFG_TUSB_MEM_SECTION static usbtmc_interface_state_t usbtmc_state =
139
148
.itf_id = 0xFF ,
140
149
};
141
150
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" );
147
153
148
154
static bool handle_devMsgOutStart (uint8_t rhport , void * data , size_t len );
149
155
static bool handle_devMsgOut (uint8_t rhport , void * data , size_t len , size_t packetLen );
150
156
151
157
static uint8_t termChar ;
152
158
static uint8_t termCharRequested = false;
153
159
154
-
155
160
osal_mutex_def_t usbtmcLockBuffer ;
156
161
static osal_mutex_t usbtmcLock ;
157
162
@@ -282,12 +287,15 @@ uint16_t usbtmcd_open_cb(uint8_t rhport, tusb_desc_interface_t const * itf_desc,
282
287
tusb_desc_endpoint_t const * ep_desc = (tusb_desc_endpoint_t const * )p_desc ;
283
288
switch (ep_desc -> bmAttributes .xfer ) {
284
289
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 );
286
292
if (tu_edpt_dir (ep_desc -> bEndpointAddress ) == TUSB_DIR_IN )
287
293
{
288
294
usbtmc_state .ep_bulk_in = ep_desc -> bEndpointAddress ;
295
+ usbtmc_state .ep_bulk_in_wMaxPacketSize = tu_edpt_packet_size (ep_desc );
289
296
} else {
290
297
usbtmc_state .ep_bulk_out = ep_desc -> bEndpointAddress ;
298
+ usbtmc_state .ep_bulk_out_wMaxPacketSize = tu_edpt_packet_size (ep_desc );
291
299
}
292
300
293
301
break ;
@@ -395,7 +403,7 @@ static bool handle_devMsgOut(uint8_t rhport, void *data, size_t len, size_t pack
395
403
// return true upon failure, as we can assume error is being handled elsewhere.
396
404
TU_VERIFY (usbtmc_state .state == STATE_RCV ,true);
397
405
398
- bool shortPacket = (packetLen < USBTMCD_MAX_PACKET_SIZE );
406
+ bool shortPacket = (packetLen < usbtmc_state . ep_bulk_out_wMaxPacketSize );
399
407
400
408
// Packet is to be considered complete when we get enough data or at a short packet.
401
409
bool atEnd = false;
@@ -522,7 +530,7 @@ bool usbtmcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint
522
530
break ;
523
531
524
532
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 ))
526
534
{
527
535
// FIXME! This removes const below!
528
536
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
539
547
usbtmc_state .transfer_size_remaining = 0 ;
540
548
usbtmc_state .devInBuffer = NULL ;
541
549
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 ))
543
551
{
544
552
usbtmc_state .state = STATE_TX_SHORTED ;
545
553
}
@@ -680,7 +688,7 @@ bool usbtmcd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request
680
688
usbtmc_state .transfer_size_remaining = 0u ;
681
689
// Check if we've queued a short packet
682
690
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 ) ?
684
692
STATE_ABORTING_BULK_IN : STATE_ABORTING_BULK_IN_SHORTED ;
685
693
criticalLeave ();
686
694
if (usbtmc_state .transfer_size_sent == 0 )
0 commit comments