Skip to content

Commit 66e96b0

Browse files
committed
tinyusb update
- improve fifo write/read_n with only one lock - added usbd_edpt_xfer/usbd_edpt_busy to replace dcd_edpt_transfer/dcd_edpt_busy() - use usbd_edpt_xfer/usbd_edpt_busy for hid/cdc/msc class driver - replace cdc read's pending_read_from_host by usbd_edpt_busy()
1 parent 99b6de3 commit 66e96b0

File tree

7 files changed

+139
-80
lines changed

7 files changed

+139
-80
lines changed

cores/nRF5/Adafruit_TinyUSB_Core/tinyusb/src/class/cdc/cdc_device.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,21 @@ typedef struct
7373
//--------------------------------------------------------------------+
7474
CFG_TUSB_MEM_SECTION static cdcd_interface_t _cdcd_itf[CFG_TUD_CDC];
7575

76-
// TODO will be replaced by dcd_edpt_busy()
77-
bool pending_read_from_host;
76+
//bool pending_read_from_host;
7877
static void _prep_out_transaction (uint8_t itf)
7978
{
8079
cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
8180

8281
// skip if previous transfer not complete
83-
// dcd_edpt_busy() doesn't work, probably transfer is complete but not properly handled by the stack
84-
// if ( dcd_edpt_busy(TUD_OPT_RHPORT, p_cdc->ep_out) ) return;
85-
if (pending_read_from_host) return;
82+
if ( usbd_edpt_busy(TUD_OPT_RHPORT, p_cdc->ep_out) ) return;
83+
//if (pending_read_from_host) return;
8684

8785
// Prepare for incoming data but only allow what we can store in the ring buffer.
8886
uint16_t max_read = tu_fifo_remaining(&p_cdc->rx_ff);
8987
if ( max_read >= CFG_TUD_CDC_EPSIZE )
9088
{
91-
dcd_edpt_xfer(TUD_OPT_RHPORT, p_cdc->ep_out, p_cdc->epout_buf, CFG_TUD_CDC_EPSIZE);
92-
pending_read_from_host = true;
89+
usbd_edpt_xfer(TUD_OPT_RHPORT, p_cdc->ep_out, p_cdc->epout_buf, CFG_TUD_CDC_EPSIZE);
90+
// pending_read_from_host = true;
9391
}
9492
}
9593

@@ -183,13 +181,13 @@ uint32_t tud_cdc_n_write(uint8_t itf, void const* buffer, uint32_t bufsize)
183181
bool tud_cdc_n_write_flush (uint8_t itf)
184182
{
185183
cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
186-
TU_VERIFY( !dcd_edpt_busy(TUD_OPT_RHPORT, p_cdc->ep_in) ); // skip if previous transfer not complete
184+
TU_VERIFY( !usbd_edpt_busy(TUD_OPT_RHPORT, p_cdc->ep_in) ); // skip if previous transfer not complete
187185

188186
uint16_t count = tu_fifo_read_n(&_cdcd_itf[itf].tx_ff, p_cdc->epin_buf, CFG_TUD_CDC_EPSIZE);
189187
if ( count )
190188
{
191189
TU_VERIFY( tud_cdc_n_connected(itf) ); // fifo is empty if not connected
192-
TU_ASSERT( dcd_edpt_xfer(TUD_OPT_RHPORT, p_cdc->ep_in, p_cdc->epin_buf, count) );
190+
TU_ASSERT( usbd_edpt_xfer(TUD_OPT_RHPORT, p_cdc->ep_in, p_cdc->epin_buf, count) );
193191
}
194192

195193
return true;
@@ -298,7 +296,7 @@ bool cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t
298296
}
299297

300298
// Prepare for incoming data
301-
pending_read_from_host = false;
299+
// pending_read_from_host = false;
302300
_prep_out_transaction(cdc_id);
303301

304302
return true;
@@ -394,7 +392,7 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_
394392
if (tud_cdc_rx_cb && tu_fifo_count(&p_cdc->rx_ff) ) tud_cdc_rx_cb(itf);
395393

396394
// prepare for OUT transaction
397-
pending_read_from_host = false;
395+
// pending_read_from_host = false;
398396
_prep_out_transaction(itf);
399397
}
400398

cores/nRF5/Adafruit_TinyUSB_Core/tinyusb/src/class/hid/hid_device.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ bool tud_hid_ready(void)
7373
{
7474
uint8_t itf = 0;
7575
uint8_t const ep_in = _hidd_itf[itf].ep_in;
76-
return tud_ready() && (ep_in != 0) && !dcd_edpt_busy(TUD_OPT_RHPORT, ep_in);
76+
return tud_ready() && (ep_in != 0) && !usbd_edpt_busy(TUD_OPT_RHPORT, ep_in);
7777
}
7878

7979
bool tud_hid_report(uint8_t report_id, void const* report, uint8_t len)
@@ -94,7 +94,7 @@ bool tud_hid_report(uint8_t report_id, void const* report, uint8_t len)
9494
memcpy(p_hid->epin_buf, report, len);
9595
}
9696

97-
return dcd_edpt_xfer(TUD_OPT_RHPORT, p_hid->ep_in, p_hid->epin_buf, len);
97+
return usbd_edpt_xfer(TUD_OPT_RHPORT, p_hid->ep_in, p_hid->epin_buf, len);
9898
}
9999

100100
bool tud_hid_boot_mode(void)
@@ -180,7 +180,7 @@ bool hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t
180180
*p_len = sizeof(tusb_desc_interface_t) + sizeof(tusb_hid_descriptor_hid_t) + desc_itf->bNumEndpoints*sizeof(tusb_desc_endpoint_t);
181181

182182
// Prepare for output endpoint
183-
if (p_hid->ep_out) TU_ASSERT(dcd_edpt_xfer(rhport, p_hid->ep_out, p_hid->epout_buf, sizeof(p_hid->epout_buf)));
183+
if (p_hid->ep_out) TU_ASSERT(usbd_edpt_xfer(rhport, p_hid->ep_out, p_hid->epout_buf, sizeof(p_hid->epout_buf)));
184184

185185
return true;
186186
}
@@ -303,7 +303,7 @@ bool hidd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_
303303
if (ep_addr == p_hid->ep_out)
304304
{
305305
tud_hid_set_report_cb(0, HID_REPORT_TYPE_INVALID, p_hid->epout_buf, xferred_bytes);
306-
TU_ASSERT(dcd_edpt_xfer(rhport, p_hid->ep_out, p_hid->epout_buf, sizeof(p_hid->epout_buf)));
306+
TU_ASSERT(usbd_edpt_xfer(rhport, p_hid->ep_out, p_hid->epout_buf, sizeof(p_hid->epout_buf)));
307307
}
308308

309309
return true;

cores/nRF5/Adafruit_TinyUSB_Core/tinyusb/src/class/msc/msc_device.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ bool mscd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t
139139
(*p_len) = sizeof(tusb_desc_interface_t) + 2*sizeof(tusb_desc_endpoint_t);
140140

141141
// Prepare for Command Block Wrapper
142-
TU_ASSERT( dcd_edpt_xfer(rhport, p_msc->ep_out, (uint8_t*) &p_msc->cbw, sizeof(msc_cbw_t)) );
142+
TU_ASSERT( usbd_edpt_xfer(rhport, p_msc->ep_out, (uint8_t*) &p_msc->cbw, sizeof(msc_cbw_t)) );
143143

144144
return true;
145145
}
@@ -394,7 +394,7 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
394394
if ( (p_cbw->total_bytes > 0 ) && !tu_bit_test(p_cbw->dir, 7) )
395395
{
396396
// queue transfer
397-
TU_ASSERT( dcd_edpt_xfer(rhport, p_msc->ep_out, _mscd_buf, p_msc->total_len) );
397+
TU_ASSERT( usbd_edpt_xfer(rhport, p_msc->ep_out, _mscd_buf, p_msc->total_len) );
398398
}else
399399
{
400400
int32_t resplen;
@@ -428,7 +428,7 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
428428
if (p_msc->total_len)
429429
{
430430
TU_ASSERT( p_cbw->total_bytes >= p_msc->total_len ); // cannot return more than host expect
431-
TU_ASSERT( dcd_edpt_xfer(rhport, p_msc->ep_in, _mscd_buf, p_msc->total_len) );
431+
TU_ASSERT( usbd_edpt_xfer(rhport, p_msc->ep_in, _mscd_buf, p_msc->total_len) );
432432
}else
433433
{
434434
p_msc->stage = MSC_STAGE_STATUS;
@@ -543,7 +543,7 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
543543
p_msc->stage = MSC_STAGE_CMD;
544544

545545
// Send SCSI Status
546-
TU_ASSERT( dcd_edpt_xfer(rhport, p_msc->ep_in , (uint8_t*) &p_msc->csw, sizeof(msc_csw_t)) );
546+
TU_ASSERT( usbd_edpt_xfer(rhport, p_msc->ep_in , (uint8_t*) &p_msc->csw, sizeof(msc_csw_t)) );
547547

548548
// Invoke complete callback if defined
549549
if ( SCSI_CMD_READ_10 == p_cbw->command[0])
@@ -560,7 +560,7 @@ bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t
560560
}
561561

562562
// Queue for the next CBW
563-
TU_ASSERT( dcd_edpt_xfer(rhport, p_msc->ep_out, (uint8_t*) &p_msc->cbw, sizeof(msc_cbw_t)) );
563+
TU_ASSERT( usbd_edpt_xfer(rhport, p_msc->ep_out, (uint8_t*) &p_msc->cbw, sizeof(msc_cbw_t)) );
564564
}
565565
}
566566

@@ -602,7 +602,7 @@ static void proc_read10_cmd(uint8_t rhport, mscd_interface_t* p_msc)
602602
}
603603
else
604604
{
605-
TU_ASSERT( dcd_edpt_xfer(rhport, p_msc->ep_in, _mscd_buf, nbytes), );
605+
TU_ASSERT( usbd_edpt_xfer(rhport, p_msc->ep_in, _mscd_buf, nbytes), );
606606
}
607607
}
608608

@@ -627,7 +627,7 @@ static void proc_write10_cmd(uint8_t rhport, mscd_interface_t* p_msc)
627627
int32_t nbytes = (int32_t) tu_min32(sizeof(_mscd_buf), p_cbw->total_bytes-p_msc->xferred_len);
628628

629629
// Write10 callback will be called later when usb transfer complete
630-
TU_ASSERT( dcd_edpt_xfer(rhport, p_msc->ep_out, _mscd_buf, nbytes), );
630+
TU_ASSERT( usbd_edpt_xfer(rhport, p_msc->ep_out, _mscd_buf, nbytes), );
631631
}
632632

633633
#endif

cores/nRF5/Adafruit_TinyUSB_Core/tinyusb/src/common/tusb_fifo.c

Lines changed: 61 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,35 @@ bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_si
7171
return true;
7272
}
7373

74+
// retrieve data from fifo
75+
static void _tu_ff_pull(tu_fifo_t* f, void * buffer)
76+
{
77+
memcpy(buffer,
78+
f->buffer + (f->rd_idx * f->item_size),
79+
f->item_size);
80+
81+
f->rd_idx = (f->rd_idx + 1) % f->depth;
82+
f->count--;
83+
}
84+
85+
// send data to fifo
86+
static void _tu_ff_push(tu_fifo_t* f, void const * data)
87+
{
88+
memcpy( f->buffer + (f->wr_idx * f->item_size),
89+
data,
90+
f->item_size);
91+
92+
f->wr_idx = (f->wr_idx + 1) % f->depth;
93+
94+
if (tu_fifo_full(f))
95+
{
96+
f->rd_idx = f->wr_idx; // keep the full state (rd == wr && len = size)
97+
}
98+
else
99+
{
100+
f->count++;
101+
}
102+
}
74103

75104
/******************************************************************************/
76105
/*!
@@ -82,23 +111,19 @@ bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_si
82111
83112
@param[in] f
84113
Pointer to the FIFO buffer to manipulate
85-
@param[in] p_buffer
114+
@param[in] buffer
86115
Pointer to the place holder for data read from the buffer
87116
88117
@returns TRUE if the queue is not empty
89118
*/
90119
/******************************************************************************/
91-
bool tu_fifo_read(tu_fifo_t* f, void * p_buffer)
120+
bool tu_fifo_read(tu_fifo_t* f, void * buffer)
92121
{
93122
if( tu_fifo_empty(f) ) return false;
94123

95124
tu_fifo_lock(f);
96125

97-
memcpy(p_buffer,
98-
f->buffer + (f->rd_idx * f->item_size),
99-
f->item_size);
100-
f->rd_idx = (f->rd_idx + 1) % f->depth;
101-
f->count--;
126+
_tu_ff_pull(f, buffer);
102127

103128
tu_fifo_unlock(f);
104129

@@ -113,35 +138,36 @@ bool tu_fifo_read(tu_fifo_t* f, void * p_buffer)
113138
114139
@param[in] f
115140
Pointer to the FIFO buffer to manipulate
116-
@param[in] p_data
141+
@param[in] buffer
117142
The pointer to data location
118143
@param[in] count
119144
Number of element that buffer can afford
120145
121146
@returns number of items read from the FIFO
122147
*/
123148
/******************************************************************************/
124-
uint16_t tu_fifo_read_n (tu_fifo_t* f, void * p_buffer, uint16_t count)
149+
uint16_t tu_fifo_read_n (tu_fifo_t* f, void * buffer, uint16_t count)
125150
{
126151
if( tu_fifo_empty(f) ) return 0;
127152

153+
tu_fifo_lock(f);
154+
128155
/* Limit up to fifo's count */
129156
if ( count > f->count ) count = f->count;
130157

131-
/* Could copy up to 2 portions marked as 'x' if queue is wrapped around
132-
* case 1: ....RxxxxW.......
133-
* case 2: xxxxxW....Rxxxxxx
134-
*/
135-
// uint16_t index2upper = tu_min16(count, f->count-f->rd_idx);
136-
137-
uint8_t* p_buf = (uint8_t*) p_buffer;
158+
uint8_t* buf8 = (uint8_t*) buffer;
138159
uint16_t len = 0;
139-
while( (len < count) && tu_fifo_read(f, p_buf) )
160+
161+
while (len < count)
140162
{
163+
_tu_ff_pull(f, buf8);
164+
141165
len++;
142-
p_buf += f->item_size;
166+
buf8 += f->item_size;
143167
}
144168

169+
tu_fifo_unlock(f);
170+
145171
return len;
146172
}
147173

@@ -182,33 +208,20 @@ bool tu_fifo_peek_at(tu_fifo_t* f, uint16_t pos, void * p_buffer)
182208
183209
@param[in] f
184210
Pointer to the FIFO buffer to manipulate
185-
@param[in] p_data
211+
@param[in] data
186212
The byte to add to the FIFO
187213
188214
@returns TRUE if the data was written to the FIFO (overwrittable
189215
FIFO will always return TRUE)
190216
*/
191217
/******************************************************************************/
192-
bool tu_fifo_write (tu_fifo_t* f, const void * p_data)
218+
bool tu_fifo_write (tu_fifo_t* f, const void * data)
193219
{
194220
if ( tu_fifo_full(f) && !f->overwritable ) return false;
195221

196222
tu_fifo_lock(f);
197223

198-
memcpy( f->buffer + (f->wr_idx * f->item_size),
199-
p_data,
200-
f->item_size);
201-
202-
f->wr_idx = (f->wr_idx + 1) % f->depth;
203-
204-
if (tu_fifo_full(f))
205-
{
206-
f->rd_idx = f->wr_idx; // keep the full state (rd == wr && len = size)
207-
}
208-
else
209-
{
210-
f->count++;
211-
}
224+
_tu_ff_push(f, data);
212225

213226
tu_fifo_unlock(f);
214227

@@ -223,26 +236,35 @@ bool tu_fifo_write (tu_fifo_t* f, const void * p_data)
223236
224237
@param[in] f
225238
Pointer to the FIFO buffer to manipulate
226-
@param[in] p_data
239+
@param[in] data
227240
The pointer to data to add to the FIFO
228241
@param[in] count
229242
Number of element
230243
@return Number of written elements
231244
*/
232245
/******************************************************************************/
233-
uint16_t tu_fifo_write_n (tu_fifo_t* f, const void * p_data, uint16_t count)
246+
uint16_t tu_fifo_write_n (tu_fifo_t* f, const void * data, uint16_t count)
234247
{
235248
if ( count == 0 ) return 0;
236249

237-
uint8_t const* p_buf = (uint8_t const*) p_data;
250+
tu_fifo_lock(f);
251+
252+
// Not overwritable limit up to full
253+
if (!f->overwritable) count = tu_min16(count, tu_fifo_remaining(f));
238254

255+
uint8_t const* buf8 = (uint8_t const*) data;
239256
uint16_t len = 0;
240-
while( (len < count) && tu_fifo_write(f, p_buf) )
257+
258+
while (len < count)
241259
{
260+
_tu_ff_push(f, buf8);
261+
242262
len++;
243-
p_buf += f->item_size;
263+
buf8 += f->item_size;
244264
}
245265

266+
tu_fifo_unlock(f);
267+
246268
return len;
247269
}
248270

cores/nRF5/Adafruit_TinyUSB_Core/tinyusb/src/device/dcd.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,25 +100,28 @@ void dcd_set_config (uint8_t rhport, uint8_t config_num);
100100
// Wake up host
101101
void dcd_remote_wakeup(uint8_t rhport);
102102

103-
/*------------------------------------------------------------------*/
104-
/* Endpoint API
105-
* - open : Configure endpoint's registers
106-
* - xfer : Submit a transfer. When complete dcd_event_xfer_complete
107-
* must be called to notify the stack
108-
* - busy : Check if endpoint transferring is complete (TODO remove)
109-
* - stall : stall endpoint
110-
* - clear_stall : clear stall, data toggle is also reset to DATA0
111-
*------------------------------------------------------------------*/
103+
//--------------------------------------------------------------------+
104+
// Endpoint API
105+
//--------------------------------------------------------------------+
106+
107+
// Configure endpoint's registers according to descriptor
112108
bool dcd_edpt_open (uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc);
109+
110+
// Submit a transfer, When complete dcd_event_xfer_complete() is invoked to notify the stack
113111
bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes);
112+
113+
// Check if endpoint transferring is complete (TODO remove)
114114
bool dcd_edpt_busy (uint8_t rhport, uint8_t ep_addr);
115115

116+
// Stall endpoint
116117
void dcd_edpt_stall (uint8_t rhport, uint8_t ep_addr);
118+
119+
// clear stall, data toggle is also reset to DATA0
117120
void dcd_edpt_clear_stall (uint8_t rhport, uint8_t ep_addr);
118121

119122
/*------------------------------------------------------------------*/
120123
/* Event Function
121-
* Called by DCD to notify USBD
124+
* Called by DCD to notify device stack
122125
*------------------------------------------------------------------*/
123126
void dcd_event_handler(dcd_event_t const * event, bool in_isr);
124127

0 commit comments

Comments
 (0)