Skip to content

Commit 811098f

Browse files
committed
Replace USBDevice derived callback with mbed::callback
By replacing the callbacks with proper mbed::callback we can use the USBDevice infrastructure without deriving directly from USBDevice. The patch could bring a (tiny, big, ?) performance hit due to the indirection, but would make the USBDevice class usable as a singleton.
1 parent 2b226bf commit 811098f

File tree

8 files changed

+17
-31
lines changed

8 files changed

+17
-31
lines changed

drivers/internal/USBDevice.h

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "USBDevice_Types.h"
2323
#include "USBPhy.h"
2424
#include "mbed_critical.h"
25+
#include "Callback.h"
2526

2627
/**
2728
* \defgroup drivers_USBDevice USBDevice class
@@ -36,7 +37,7 @@
3637
*/
3738
class USBDevice: public USBPhyEvents {
3839
public:
39-
typedef void (USBDevice::*ep_cb_t)();
40+
typedef mbed::Callback<void()> ep_cb_t;
4041

4142
enum RequestResult {
4243
Receive = 0,
@@ -141,21 +142,6 @@ class USBDevice: public USBPhyEvents {
141142
*/
142143
bool endpoint_add(usb_ep_t endpoint, uint32_t max_packet, usb_ep_type_t type, ep_cb_t callback = NULL);
143144

144-
/**
145-
* Add an endpoint
146-
*
147-
* @param endpoint Endpoint to enable
148-
* @param max_packet Maximum size of a packet which can be sent or received on this endpoint
149-
* @param type Endpoint type - USB_EP_TYPE_BULK, USB_EP_TYPE_INT or USB_EP_TYPE_ISO
150-
* @param callback Method pointer to be called when a packet is transferred
151-
* @returns true if successful, false otherwise
152-
*/
153-
template<typename T>
154-
bool endpoint_add(usb_ep_t endpoint, uint32_t max_packet, usb_ep_type_t type, void (T::*callback)())
155-
{
156-
return endpoint_add(endpoint, max_packet, type, static_cast<ep_cb_t>(callback));
157-
}
158-
159145
/**
160146
* Remove an endpoint
161147
*

drivers/source/usb/USBAudio.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,8 @@ void USBAudio::callback_set_configuration(uint8_t configuration)
542542
endpoint_remove_all();
543543

544544
// Configure isochronous endpoint
545-
endpoint_add(_episo_out, _rx_packet_size_max, USB_EP_TYPE_ISO, static_cast<ep_cb_t>(&USBAudio::_receive_isr));
546-
endpoint_add(_episo_in, _tx_packet_size_max, USB_EP_TYPE_ISO, static_cast<ep_cb_t>(&USBAudio::_send_isr));
545+
endpoint_add(_episo_out, _rx_packet_size_max, USB_EP_TYPE_ISO, mbed::callback(this, &USBAudio::_receive_isr));
546+
endpoint_add(_episo_in, _tx_packet_size_max, USB_EP_TYPE_ISO, mbed::callback(this, &USBAudio::_send_isr));
547547

548548
// activate readings on this endpoint
549549
read_start(_episo_out, _rx_packet_buf, _rx_packet_size_max);

drivers/source/usb/USBCDC.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ void USBCDC::callback_set_configuration(uint8_t configuration)
292292
if (configuration == DEFAULT_CONFIGURATION) {
293293
// Configure endpoints > 0
294294
endpoint_add(_int_in, CDC_MAX_PACKET_SIZE, USB_EP_TYPE_INT);
295-
endpoint_add(_bulk_in, CDC_MAX_PACKET_SIZE, USB_EP_TYPE_BULK, &USBCDC::_send_isr);
296-
endpoint_add(_bulk_out, CDC_MAX_PACKET_SIZE, USB_EP_TYPE_BULK, &USBCDC::_receive_isr);
295+
endpoint_add(_bulk_in, CDC_MAX_PACKET_SIZE, USB_EP_TYPE_BULK, mbed::callback(this, &USBCDC::_send_isr));
296+
endpoint_add(_bulk_out, CDC_MAX_PACKET_SIZE, USB_EP_TYPE_BULK, mbed::callback(this, &USBCDC::_receive_isr));
297297

298298
read_start(_bulk_out, _rx_buf, sizeof(_rx_buffer));
299299
_rx_in_progress = true;

drivers/source/usb/USBCDC_ECM.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,9 @@ void USBCDC_ECM::callback_set_interface(uint16_t interface, uint8_t alternate)
318318
if (alternate) {
319319
_packet_filter = 0;
320320

321-
endpoint_add(_int_in, MAX_PACKET_SIZE_INT, USB_EP_TYPE_INT, &USBCDC_ECM::_int_callback);
322-
endpoint_add(_bulk_in, MAX_PACKET_SIZE_BULK, USB_EP_TYPE_BULK, &USBCDC_ECM::_bulk_in_callback);
323-
endpoint_add(_bulk_out, MAX_PACKET_SIZE_BULK, USB_EP_TYPE_BULK, &USBCDC_ECM::_bulk_out_callback);
321+
endpoint_add(_int_in, MAX_PACKET_SIZE_INT, USB_EP_TYPE_INT, mbed::callback(this, &USBCDC_ECM::_int_callback));
322+
endpoint_add(_bulk_in, MAX_PACKET_SIZE_BULK, USB_EP_TYPE_BULK, mbed::callback(this, &USBCDC_ECM::_bulk_in_callback));
323+
endpoint_add(_bulk_out, MAX_PACKET_SIZE_BULK, USB_EP_TYPE_BULK, mbed::callback(this, &USBCDC_ECM::_bulk_out_callback));
324324

325325
read_start(_bulk_out, _bulk_buf, MAX_PACKET_SIZE_BULK);
326326

drivers/source/usb/USBDevice.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,7 @@ void USBDevice::out(usb_ep_t endpoint)
937937
MBED_ASSERT(info->pending >= 1);
938938
info->pending -= 1;
939939
if (info->callback) {
940-
(this->*(info->callback))();
940+
info->callback();
941941
}
942942
}
943943

@@ -955,7 +955,7 @@ void USBDevice::in(usb_ep_t endpoint)
955955
MBED_ASSERT(info->pending >= 1);
956956
info->pending -= 1;
957957
if (info->callback) {
958-
(this->*(info->callback))();
958+
info->callback();
959959
}
960960
}
961961

drivers/source/usb/USBHID.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,8 @@ void USBHID::callback_set_configuration(uint8_t configuration)
376376
}
377377

378378
// Configure endpoints > 0
379-
endpoint_add(_int_in, MAX_HID_REPORT_SIZE, USB_EP_TYPE_INT, &USBHID::_send_isr);
380-
endpoint_add(_int_out, MAX_HID_REPORT_SIZE, USB_EP_TYPE_INT, &USBHID::_read_isr);
379+
endpoint_add(_int_in, MAX_HID_REPORT_SIZE, USB_EP_TYPE_INT, mbed::callback(this, &USBHID::_send_isr));
380+
endpoint_add(_int_out, MAX_HID_REPORT_SIZE, USB_EP_TYPE_INT, mbed::callback(this, &USBHID::_read_isr));
381381

382382
// We activate the endpoint to be able to recceive data
383383
read_start(_int_out, _output_report.data, MAX_HID_REPORT_SIZE);

drivers/source/usb/USBMIDI.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ void USBMIDI::callback_set_configuration(uint8_t configuration)
222222
}
223223

224224
endpoint_remove_all();
225-
endpoint_add(_bulk_in, MaxSize, USB_EP_TYPE_BULK, &USBMIDI::_in_callback);
226-
endpoint_add(_bulk_out, MaxSize, USB_EP_TYPE_BULK, &USBMIDI::_out_callback);
225+
endpoint_add(_bulk_in, MaxSize, USB_EP_TYPE_BULK, mbed::callback(this, &USBMIDI::_in_callback));
226+
endpoint_add(_bulk_out, MaxSize, USB_EP_TYPE_BULK, mbed::callback(this, &USBMIDI::_out_callback));
227227

228228
read_start(_bulk_out, _bulk_buf, MaxSize);
229229

drivers/source/usb/USBMSD.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,8 @@ void USBMSD::_configure()
459459
_mutex.lock();
460460

461461
// Configure endpoints > 0
462-
endpoint_add(_bulk_in, MAX_PACKET, USB_EP_TYPE_BULK, &USBMSD::_isr_in);
463-
endpoint_add(_bulk_out, MAX_PACKET, USB_EP_TYPE_BULK, &USBMSD::_isr_out);
462+
endpoint_add(_bulk_in, MAX_PACKET, USB_EP_TYPE_BULK, mbed::callback(this, &USBMSD::_isr_in));
463+
endpoint_add(_bulk_out, MAX_PACKET, USB_EP_TYPE_BULK, mbed::callback(this, &USBMSD::_isr_out));
464464
MBED_ASSERT(sizeof(_bulk_out_buf) == MAX_PACKET);
465465
MBED_ASSERT(sizeof(_bulk_in_buf) == MAX_PACKET);
466466

0 commit comments

Comments
 (0)