Skip to content

Commit fdb431b

Browse files
committed
Buffer int msg to ensure alignment and placement correctness.
1 parent 98e85a2 commit fdb431b

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

src/class/usbtmc/usbtmc_device.c

+13-5
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ tu_static char logMsg[150];
8686
// imposes a minimum buffer size of 32 bytes.
8787
#define USBTMCD_BUFFER_SIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
8888

89+
// Interrupt endpoint buffer size, default to 2 bytes as USB488 specification.
90+
#ifndef CFG_TUD_USBTMC_INT_EP_SIZE
91+
#define CFG_TUD_USBTMC_INT_EP_SIZE 2
92+
#endif
93+
8994
/*
9095
* The state machine does not allow simultaneous reading and writing. This is
9196
* consistent with USBTMC.
@@ -124,13 +129,15 @@ typedef struct
124129
uint8_t ep_bulk_in;
125130
uint8_t ep_bulk_out;
126131
uint8_t ep_int_in;
132+
uint32_t ep_bulk_in_wMaxPacketSize;
133+
uint32_t ep_bulk_out_wMaxPacketSize;
127134
// IN buffer is only used for first packet, not the remainder
128135
// in order to deal with prepending header
129136
CFG_TUSB_MEM_ALIGN uint8_t ep_bulk_in_buf[USBTMCD_BUFFER_SIZE];
130-
uint32_t ep_bulk_in_wMaxPacketSize;
131137
// OUT buffer receives one packet at a time
132138
CFG_TUSB_MEM_ALIGN uint8_t ep_bulk_out_buf[USBTMCD_BUFFER_SIZE];
133-
uint32_t ep_bulk_out_wMaxPacketSize;
139+
// Buffer int msg to ensure alignment and placement correctness
140+
CFG_TUSB_MEM_ALIGN uint8_t ep_int_in_buf[CFG_TUD_USBTMC_INT_EP_SIZE];
134141

135142
uint32_t transfer_size_remaining; // also used for requested length for bulk IN.
136143
uint32_t transfer_size_sent; // To keep track of data bytes that have been queued in FIFO (not header bytes)
@@ -243,12 +250,13 @@ bool tud_usbtmc_transmit_dev_msg_data(
243250
bool tud_usbtmc_transmit_notification_data(const void * data, size_t len)
244251
{
245252
#ifndef NDEBUG
246-
TU_ASSERT(len >= 1);
253+
TU_ASSERT(len > 0);
247254
TU_ASSERT(usbtmc_state.ep_int_in != 0);
248255
#endif
249-
if (usbd_edpt_busy(usbtmc_state.rhport, usbtmc_state.ep_int_in)) return false;
256+
TU_VERIFY(usbd_edpt_busy(usbtmc_state.rhport, usbtmc_state.ep_int_in));
250257

251-
TU_VERIFY(usbd_edpt_xfer(usbtmc_state.rhport, usbtmc_state.ep_int_in, (void *)(uintptr_t) data, len));
258+
TU_VERIFY(tu_memcpy_s(usbtmc_state.ep_int_in_buf, sizeof(usbtmc_state.ep_int_in_buf), data, len) == 0);
259+
TU_VERIFY(usbd_edpt_xfer(usbtmc_state.rhport, usbtmc_state.ep_int_in, usbtmc_state.ep_int_in_buf, len));
252260
return true;
253261
}
254262

src/class/usbtmc/usbtmc_device.h

+6-11
Original file line numberDiff line numberDiff line change
@@ -86,21 +86,16 @@ TU_ATTR_WEAK bool tud_usbtmc_msg_trigger_cb(usbtmc_msg_generic_t* msg);
8686
//TU_ATTR_WEAK bool tud_usbtmc_app_go_to_local_cb();
8787
#endif
8888

89-
/*******************************************
90-
* Called from app
91-
*
92-
* We keep a reference to the buffer, so it MUST not change until the app is
93-
* notified that the transfer is complete.
94-
******************************************/
95-
89+
// Called from app
90+
//
91+
// We keep a reference to the buffer, so it MUST not change until the app is
92+
// notified that the transfer is complete.
9693
bool tud_usbtmc_transmit_dev_msg_data(
9794
const void * data, size_t len,
9895
bool endOfMessage, bool usingTermChar);
9996

100-
// Buffers a notification to be sent to the host. The buffer must be
101-
// valid until the tud_usbtmc_notification_complete_cb callback. The
102-
// data starts with the bNotify1 field, see the USBTMC Specification,
103-
// Table 13.
97+
// Buffers a notification to be sent to the host. The data starts
98+
// with the bNotify1 field, see the USBTMC Specification, Table 13.
10499
//
105100
// If the previous notification data has not yet been sent, this
106101
// returns false.

0 commit comments

Comments
 (0)