Skip to content

Commit 090964c

Browse files
committed
apply TUD_EPBUF_DEF for device: bth, dfu, hid, msc
1 parent f148670 commit 090964c

File tree

6 files changed

+266
-301
lines changed

6 files changed

+266
-301
lines changed

src/class/bth/bth_device.c

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737
//--------------------------------------------------------------------+
3838
// MACRO CONSTANT TYPEDEF
3939
//--------------------------------------------------------------------+
40-
typedef struct
41-
{
40+
typedef struct {
4241
uint8_t itf_num;
4342
uint8_t ep_ev;
4443
uint8_t ep_acl_in;
@@ -49,23 +48,18 @@ typedef struct
4948

5049
// Previous amount of bytes sent when issuing ZLP
5150
uint32_t prev_xferred_bytes;
52-
53-
// Endpoint Transfer buffer
54-
union {
55-
CFG_TUD_MEM_ALIGN bt_hci_cmd_t hci_cmd;
56-
TUD_DCACHE_PADDING;
57-
};
58-
union {
59-
CFG_TUD_MEM_ALIGN uint8_t epout_buf[CFG_TUD_BTH_DATA_EPSIZE];
60-
TUD_DCACHE_PADDING;
61-
};
62-
6351
} btd_interface_t;
6452

53+
typedef struct {
54+
TUD_EPBUF_DEF(epout_buf, CFG_TUD_BTH_DATA_EPSIZE);
55+
TUD_EPBUF_TYPE_DEF(hci_cmd, bt_hci_cmd_t);
56+
} btd_epbuf_t;
57+
6558
//--------------------------------------------------------------------+
6659
// INTERNAL OBJECT & FUNCTION DECLARATION
6760
//--------------------------------------------------------------------+
68-
CFG_TUD_MEM_SECTION btd_interface_t _btd_itf;
61+
static btd_interface_t _btd_itf;
62+
CFG_TUD_MEM_SECTION static btd_epbuf_t _btd_epbuf;
6963

7064
static bool bt_tx_data(uint8_t ep, void *data, uint16_t len)
7165
{
@@ -158,7 +152,7 @@ uint16_t btd_open(uint8_t rhport, tusb_desc_interface_t const *itf_desc, uint16_
158152
itf_desc = (tusb_desc_interface_t const *)tu_desc_next(tu_desc_next(desc_ep));
159153

160154
// Prepare for incoming data from host
161-
TU_ASSERT(usbd_edpt_xfer(rhport, _btd_itf.ep_acl_out, _btd_itf.epout_buf, CFG_TUD_BTH_DATA_EPSIZE), 0);
155+
TU_ASSERT(usbd_edpt_xfer(rhport, _btd_itf.ep_acl_out, _btd_epbuf.epout_buf, CFG_TUD_BTH_DATA_EPSIZE), 0);
162156

163157
drv_len = hci_itf_size;
164158

@@ -249,14 +243,16 @@ bool btd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t c
249243
}
250244
else return false;
251245

252-
return tud_control_xfer(rhport, request, &_btd_itf.hci_cmd, sizeof(_btd_itf.hci_cmd));
246+
return tud_control_xfer(rhport, request, &_btd_epbuf.hci_cmd, sizeof(bt_hci_cmd_t));
253247
}
254248
else if ( stage == CONTROL_STAGE_DATA )
255249
{
256250
// Handle class request only
257251
TU_VERIFY(request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS);
258252

259-
if (tud_bt_hci_cmd_cb) tud_bt_hci_cmd_cb(&_btd_itf.hci_cmd, tu_min16(request->wLength, sizeof(_btd_itf.hci_cmd)));
253+
if (tud_bt_hci_cmd_cb) {
254+
tud_bt_hci_cmd_cb(&_btd_epbuf.hci_cmd, tu_min16(request->wLength, sizeof(bt_hci_cmd_t)));
255+
}
260256
}
261257

262258
return true;
@@ -267,10 +263,10 @@ bool btd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result,
267263
// received new data from host
268264
if (ep_addr == _btd_itf.ep_acl_out)
269265
{
270-
if (tud_bt_acl_data_received_cb) tud_bt_acl_data_received_cb(_btd_itf.epout_buf, xferred_bytes);
266+
if (tud_bt_acl_data_received_cb) tud_bt_acl_data_received_cb(_btd_epbuf.epout_buf, xferred_bytes);
271267

272268
// prepare for next data
273-
TU_ASSERT(usbd_edpt_xfer(rhport, _btd_itf.ep_acl_out, _btd_itf.epout_buf, CFG_TUD_BTH_DATA_EPSIZE));
269+
TU_ASSERT(usbd_edpt_xfer(rhport, _btd_itf.ep_acl_out, _btd_epbuf.epout_buf, CFG_TUD_BTH_DATA_EPSIZE));
274270
}
275271
else if (ep_addr == _btd_itf.ep_ev)
276272
{

src/class/cdc/cdc_device.c

Lines changed: 75 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -67,28 +67,27 @@ typedef struct {
6767

6868
OSAL_MUTEX_DEF(rx_ff_mutex);
6969
OSAL_MUTEX_DEF(tx_ff_mutex);
70-
71-
// Endpoint Transfer buffer
72-
union {
73-
CFG_TUD_MEM_ALIGN uint8_t epout_buf[CFG_TUD_CDC_EP_BUFSIZE];
74-
TUD_DCACHE_PADDING;
75-
};
76-
union {
77-
CFG_TUD_MEM_ALIGN uint8_t epin_buf[CFG_TUD_CDC_EP_BUFSIZE];
78-
TUD_DCACHE_PADDING;
79-
};
8070
} cdcd_interface_t;
8171

8272
#define ITF_MEM_RESET_SIZE offsetof(cdcd_interface_t, wanted_char)
8373

74+
typedef struct {
75+
TUD_EPBUF_DEF(epout, CFG_TUD_CDC_EP_BUFSIZE);
76+
TUD_EPBUF_DEF(epin, CFG_TUD_CDC_EP_BUFSIZE);
77+
} cdcd_epbuf_t;
78+
8479
//--------------------------------------------------------------------+
8580
// INTERNAL OBJECT & FUNCTION DECLARATION
8681
//--------------------------------------------------------------------+
87-
CFG_TUD_MEM_SECTION static cdcd_interface_t _cdcd_itf[CFG_TUD_CDC];
82+
static cdcd_interface_t _cdcd_itf[CFG_TUD_CDC];
83+
CFG_TUD_MEM_SECTION static cdcd_epbuf_t _cdcd_epbuf[CFG_TUD_CDC];
84+
8885
static tud_cdc_configure_fifo_t _cdcd_fifo_cfg;
8986

90-
static bool _prep_out_transaction (cdcd_interface_t* p_cdc) {
91-
uint8_t const rhport = 0;
87+
static bool _prep_out_transaction(uint8_t itf) {
88+
const uint8_t rhport = 0;
89+
cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
90+
cdcd_epbuf_t* p_epbuf = &_cdcd_epbuf[itf];
9291

9392
// Skip if usb is not ready yet
9493
TU_VERIFY(tud_ready() && p_cdc->ep_out);
@@ -99,17 +98,17 @@ static bool _prep_out_transaction (cdcd_interface_t* p_cdc) {
9998
// TODO Actually we can still carry out the transfer, keeping count of received bytes
10099
// and slowly move it to the FIFO when read().
101100
// This pre-check reduces endpoint claiming
102-
TU_VERIFY(available >= sizeof(p_cdc->epout_buf));
101+
TU_VERIFY(available >= CFG_TUD_CDC_EP_BUFSIZE);
103102

104103
// claim endpoint
105104
TU_VERIFY(usbd_edpt_claim(rhport, p_cdc->ep_out));
106105

107106
// fifo can be changed before endpoint is claimed
108107
available = tu_fifo_remaining(&p_cdc->rx_ff);
109108

110-
if ( available >= sizeof(p_cdc->epout_buf) ) {
111-
return usbd_edpt_xfer(rhport, p_cdc->ep_out, p_cdc->epout_buf, sizeof(p_cdc->epout_buf));
112-
}else {
109+
if (available >= CFG_TUD_CDC_EP_BUFSIZE) {
110+
return usbd_edpt_xfer(rhport, p_cdc->ep_out, p_epbuf->epout, CFG_TUD_CDC_EP_BUFSIZE);
111+
} else {
113112
// Release endpoint since we don't make any transfer
114113
usbd_edpt_release(rhport, p_cdc->ep_out);
115114
return false;
@@ -120,7 +119,7 @@ static bool _prep_out_transaction (cdcd_interface_t* p_cdc) {
120119
// APPLICATION API
121120
//--------------------------------------------------------------------+
122121

123-
bool tud_cdc_configure_fifo(tud_cdc_configure_fifo_t const* cfg) {
122+
bool tud_cdc_configure_fifo(const tud_cdc_configure_fifo_t* cfg) {
124123
TU_VERIFY(cfg);
125124
_cdcd_fifo_cfg = (*cfg);
126125
return true;
@@ -157,7 +156,7 @@ uint32_t tud_cdc_n_available(uint8_t itf) {
157156
uint32_t tud_cdc_n_read(uint8_t itf, void* buffer, uint32_t bufsize) {
158157
cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
159158
uint32_t num_read = tu_fifo_read_n(&p_cdc->rx_ff, buffer, (uint16_t) TU_MIN(bufsize, UINT16_MAX));
160-
_prep_out_transaction(p_cdc);
159+
_prep_out_transaction(itf);
161160
return num_read;
162161
}
163162

@@ -168,13 +167,13 @@ bool tud_cdc_n_peek(uint8_t itf, uint8_t* chr) {
168167
void tud_cdc_n_read_flush(uint8_t itf) {
169168
cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
170169
tu_fifo_clear(&p_cdc->rx_ff);
171-
_prep_out_transaction(p_cdc);
170+
_prep_out_transaction(itf);
172171
}
173172

174173
//--------------------------------------------------------------------+
175174
// WRITE API
176175
//--------------------------------------------------------------------+
177-
uint32_t tud_cdc_n_write(uint8_t itf, void const* buffer, uint32_t bufsize) {
176+
uint32_t tud_cdc_n_write(uint8_t itf, const void* buffer, uint32_t bufsize) {
178177
cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
179178
uint16_t ret = tu_fifo_write_n(&p_cdc->tx_ff, buffer, (uint16_t) TU_MIN(bufsize, UINT16_MAX));
180179

@@ -192,23 +191,26 @@ uint32_t tud_cdc_n_write(uint8_t itf, void const* buffer, uint32_t bufsize) {
192191

193192
uint32_t tud_cdc_n_write_flush(uint8_t itf) {
194193
cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
194+
cdcd_epbuf_t* p_epbuf = &_cdcd_epbuf[itf];
195195

196196
// Skip if usb is not ready yet
197197
TU_VERIFY(tud_ready(), 0);
198198

199199
// No data to send
200-
if (!tu_fifo_count(&p_cdc->tx_ff)) return 0;
200+
if (!tu_fifo_count(&p_cdc->tx_ff)) {
201+
return 0;
202+
}
201203

202-
uint8_t const rhport = 0;
204+
const uint8_t rhport = 0;
203205

204206
// Claim the endpoint
205207
TU_VERIFY(usbd_edpt_claim(rhport, p_cdc->ep_in), 0);
206208

207209
// Pull data from FIFO
208-
uint16_t const count = tu_fifo_read_n(&p_cdc->tx_ff, p_cdc->epin_buf, sizeof(p_cdc->epin_buf));
210+
const uint16_t count = tu_fifo_read_n(&p_cdc->tx_ff, p_epbuf->epin, CFG_TUD_CDC_EP_BUFSIZE);
209211

210212
if (count) {
211-
TU_ASSERT(usbd_edpt_xfer(rhport, p_cdc->ep_in, p_cdc->epin_buf, count), 0);
213+
TU_ASSERT(usbd_edpt_xfer(rhport, p_cdc->ep_in, p_epbuf->epin, count), 0);
212214
return count;
213215
} else {
214216
// Release endpoint since we don't make any transfer
@@ -292,32 +294,37 @@ void cdcd_reset(uint8_t rhport) {
292294
cdcd_interface_t* p_cdc = &_cdcd_itf[i];
293295

294296
tu_memclr(p_cdc, ITF_MEM_RESET_SIZE);
295-
if (!_cdcd_fifo_cfg.rx_persistent) tu_fifo_clear(&p_cdc->rx_ff);
296-
if (!_cdcd_fifo_cfg.tx_persistent) tu_fifo_clear(&p_cdc->tx_ff);
297+
if (!_cdcd_fifo_cfg.rx_persistent) {
298+
tu_fifo_clear(&p_cdc->rx_ff);
299+
}
300+
if (!_cdcd_fifo_cfg.tx_persistent) {
301+
tu_fifo_clear(&p_cdc->tx_ff);
302+
}
297303
tu_fifo_set_overwritable(&p_cdc->tx_ff, true);
298304
}
299305
}
300306

301-
uint16_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len) {
307+
uint16_t cdcd_open(uint8_t rhport, const tusb_desc_interface_t* itf_desc, uint16_t max_len) {
302308
// Only support ACM subclass
303309
TU_VERIFY( TUSB_CLASS_CDC == itf_desc->bInterfaceClass &&
304310
CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL == itf_desc->bInterfaceSubClass, 0);
305311

306312
// Find available interface
307-
cdcd_interface_t* p_cdc = NULL;
308-
for (uint8_t cdc_id = 0; cdc_id < CFG_TUD_CDC; cdc_id++) {
309-
if (_cdcd_itf[cdc_id].ep_in == 0) {
310-
p_cdc = &_cdcd_itf[cdc_id];
313+
cdcd_interface_t* p_cdc;
314+
uint8_t cdc_id;
315+
for (cdc_id = 0; cdc_id < CFG_TUD_CDC; cdc_id++) {
316+
p_cdc = &_cdcd_itf[cdc_id];
317+
if (p_cdc->ep_in == 0) {
311318
break;
312319
}
313320
}
314-
TU_ASSERT(p_cdc, 0);
321+
TU_ASSERT(cdc_id < CFG_TUD_CDC, 0);
315322

316323
//------------- Control Interface -------------//
317324
p_cdc->itf_num = itf_desc->bInterfaceNumber;
318325

319326
uint16_t drv_len = sizeof(tusb_desc_interface_t);
320-
uint8_t const* p_desc = tu_desc_next(itf_desc);
327+
const uint8_t* p_desc = tu_desc_next(itf_desc);
321328

322329
// Communication Functional Descriptors
323330
while (TUSB_DESC_CS_INTERFACE == tu_desc_type(p_desc) && drv_len <= max_len) {
@@ -327,7 +334,7 @@ uint16_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint1
327334

328335
if (TUSB_DESC_ENDPOINT == tu_desc_type(p_desc)) {
329336
// notification endpoint
330-
tusb_desc_endpoint_t const* desc_ep = (tusb_desc_endpoint_t const*) p_desc;
337+
const tusb_desc_endpoint_t* desc_ep = (const tusb_desc_endpoint_t*) p_desc;
331338

332339
TU_ASSERT(usbd_edpt_open(rhport, desc_ep), 0);
333340
p_cdc->ep_notif = desc_ep->bEndpointAddress;
@@ -338,7 +345,7 @@ uint16_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint1
338345

339346
//------------- Data Interface (if any) -------------//
340347
if ((TUSB_DESC_INTERFACE == tu_desc_type(p_desc)) &&
341-
(TUSB_CLASS_CDC_DATA == ((tusb_desc_interface_t const*) p_desc)->bInterfaceClass)) {
348+
(TUSB_CLASS_CDC_DATA == ((const tusb_desc_interface_t*) p_desc)->bInterfaceClass)) {
342349
// next to endpoint descriptor
343350
drv_len += tu_desc_len(p_desc);
344351
p_desc = tu_desc_next(p_desc);
@@ -350,35 +357,39 @@ uint16_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint1
350357
}
351358

352359
// Prepare for incoming data
353-
_prep_out_transaction(p_cdc);
360+
_prep_out_transaction(cdc_id);
354361

355362
return drv_len;
356363
}
357364

358365
// Invoked when a control transfer occurred on an interface of this class
359366
// Driver response accordingly to the request and the transfer stage (setup/data/ack)
360367
// return false to stall control endpoint (e.g unsupported request)
361-
bool cdcd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const* request) {
368+
bool cdcd_control_xfer_cb(uint8_t rhport, uint8_t stage, const tusb_control_request_t* request) {
362369
// Handle class request only
363370
TU_VERIFY(request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS);
364371

365-
uint8_t itf = 0;
366-
cdcd_interface_t* p_cdc = _cdcd_itf;
372+
uint8_t itf;
373+
cdcd_interface_t* p_cdc;
367374

368375
// Identify which interface to use
369-
for (;; itf++, p_cdc++) {
370-
if (itf >= TU_ARRAY_SIZE(_cdcd_itf)) return false;
371-
372-
if (p_cdc->itf_num == request->wIndex) break;
376+
for (itf = 0; itf < CFG_TUD_CDC; itf++) {
377+
p_cdc = &_cdcd_itf[itf];
378+
if (p_cdc->itf_num == request->wIndex) {
379+
break;
380+
}
373381
}
382+
TU_VERIFY(itf < CFG_TUD_CDC);
374383

375384
switch (request->bRequest) {
376385
case CDC_REQUEST_SET_LINE_CODING:
377386
if (stage == CONTROL_STAGE_SETUP) {
378387
TU_LOG_DRV(" Set Line Coding\r\n");
379388
tud_control_xfer(rhport, request, &p_cdc->line_coding, sizeof(cdc_line_coding_t));
380389
} else if (stage == CONTROL_STAGE_ACK) {
381-
if (tud_cdc_line_coding_cb) tud_cdc_line_coding_cb(itf, &p_cdc->line_coding);
390+
if (tud_cdc_line_coding_cb) {
391+
tud_cdc_line_coding_cb(itf, &p_cdc->line_coding);
392+
}
382393
}
383394
break;
384395

@@ -409,7 +420,9 @@ bool cdcd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t
409420
TU_LOG_DRV(" Set Control Line State: DTR = %d, RTS = %d\r\n", dtr, rts);
410421

411422
// Invoke callback
412-
if (tud_cdc_line_state_cb) tud_cdc_line_state_cb(itf, dtr, rts);
423+
if (tud_cdc_line_state_cb) {
424+
tud_cdc_line_state_cb(itf, dtr, rts);
425+
}
413426
}
414427
break;
415428

@@ -418,7 +431,9 @@ bool cdcd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t
418431
tud_control_status(rhport, request);
419432
} else if (stage == CONTROL_STAGE_ACK) {
420433
TU_LOG_DRV(" Send Break\r\n");
421-
if (tud_cdc_send_break_cb) tud_cdc_send_break_cb(itf, request->wValue);
434+
if (tud_cdc_send_break_cb) {
435+
tud_cdc_send_break_cb(itf, request->wValue);
436+
}
422437
}
423438
break;
424439

@@ -438,36 +453,43 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_
438453
// Identify which interface to use
439454
for (itf = 0; itf < CFG_TUD_CDC; itf++) {
440455
p_cdc = &_cdcd_itf[itf];
441-
if ((ep_addr == p_cdc->ep_out) || (ep_addr == p_cdc->ep_in)) break;
456+
if ((ep_addr == p_cdc->ep_out) || (ep_addr == p_cdc->ep_in)) {
457+
break;
458+
}
442459
}
443460
TU_ASSERT(itf < CFG_TUD_CDC);
461+
cdcd_epbuf_t* p_epbuf = &_cdcd_epbuf[itf];
444462

445463
// Received new data
446464
if (ep_addr == p_cdc->ep_out) {
447-
tu_fifo_write_n(&p_cdc->rx_ff, p_cdc->epout_buf, (uint16_t) xferred_bytes);
465+
tu_fifo_write_n(&p_cdc->rx_ff, p_epbuf->epout, (uint16_t) xferred_bytes);
448466

449467
// Check for wanted char and invoke callback if needed
450468
if (tud_cdc_rx_wanted_cb && (((signed char) p_cdc->wanted_char) != -1)) {
451469
for (uint32_t i = 0; i < xferred_bytes; i++) {
452-
if ((p_cdc->wanted_char == p_cdc->epout_buf[i]) && !tu_fifo_empty(&p_cdc->rx_ff)) {
470+
if ((p_cdc->wanted_char == p_epbuf->epout[i]) && !tu_fifo_empty(&p_cdc->rx_ff)) {
453471
tud_cdc_rx_wanted_cb(itf, p_cdc->wanted_char);
454472
}
455473
}
456474
}
457475

458476
// invoke receive callback (if there is still data)
459-
if (tud_cdc_rx_cb && !tu_fifo_empty(&p_cdc->rx_ff)) tud_cdc_rx_cb(itf);
477+
if (tud_cdc_rx_cb && !tu_fifo_empty(&p_cdc->rx_ff)) {
478+
tud_cdc_rx_cb(itf);
479+
}
460480

461481
// prepare for OUT transaction
462-
_prep_out_transaction(p_cdc);
482+
_prep_out_transaction(itf);
463483
}
464484

465485
// Data sent to host, we continue to fetch from tx fifo to send.
466486
// Note: This will cause incorrect baudrate set in line coding.
467487
// Though maybe the baudrate is not really important !!!
468488
if (ep_addr == p_cdc->ep_in) {
469489
// invoke transmit callback to possibly refill tx fifo
470-
if (tud_cdc_tx_complete_cb) tud_cdc_tx_complete_cb(itf);
490+
if (tud_cdc_tx_complete_cb) {
491+
tud_cdc_tx_complete_cb(itf);
492+
}
471493

472494
if (0 == tud_cdc_n_write_flush(itf)) {
473495
// If there is no data left, a ZLP should be sent if

0 commit comments

Comments
 (0)