Skip to content

Commit

Permalink
Replace USBDevice derived callback with mbed::callback
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
facchinm committed Jul 29, 2019
1 parent 82e3a4a commit a0c6d9c
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 31 deletions.
4 changes: 2 additions & 2 deletions usb/device/USBAudio/USBAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,8 @@ void USBAudio::callback_set_configuration(uint8_t configuration)
endpoint_remove_all();

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

// activate readings on this endpoint
read_start(_episo_out, _rx_packet_buf, _rx_packet_size_max);
Expand Down
6 changes: 3 additions & 3 deletions usb/device/USBCDC_ECM/USBCDC_ECM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ void USBCDC_ECM::callback_set_interface(uint16_t interface, uint8_t alternate)
if (alternate) {
_packet_filter = 0;

endpoint_add(_int_in, MAX_PACKET_SIZE_INT, USB_EP_TYPE_INT, &USBCDC_ECM::_int_callback);
endpoint_add(_bulk_in, MAX_PACKET_SIZE_BULK, USB_EP_TYPE_BULK, &USBCDC_ECM::_bulk_in_callback);
endpoint_add(_bulk_out, MAX_PACKET_SIZE_BULK, USB_EP_TYPE_BULK, &USBCDC_ECM::_bulk_out_callback);
endpoint_add(_int_in, MAX_PACKET_SIZE_INT, USB_EP_TYPE_INT, mbed::callback(this, &USBCDC_ECM::_int_callback));
endpoint_add(_bulk_in, MAX_PACKET_SIZE_BULK, USB_EP_TYPE_BULK, mbed::callback(this, &USBCDC_ECM::_bulk_in_callback));
endpoint_add(_bulk_out, MAX_PACKET_SIZE_BULK, USB_EP_TYPE_BULK, mbed::callback(this, &USBCDC_ECM::_bulk_out_callback));

read_start(_bulk_out, _bulk_buf, MAX_PACKET_SIZE_BULK);

Expand Down
4 changes: 2 additions & 2 deletions usb/device/USBDevice/USBDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ void USBDevice::out(usb_ep_t endpoint)
MBED_ASSERT(info->pending >= 1);
info->pending -= 1;
if (info->callback) {
(this->*(info->callback))();
info->callback();
}
}

Expand All @@ -955,7 +955,7 @@ void USBDevice::in(usb_ep_t endpoint)
MBED_ASSERT(info->pending >= 1);
info->pending -= 1;
if (info->callback) {
(this->*(info->callback))();
info->callback();
}
}

Expand Down
18 changes: 2 additions & 16 deletions usb/device/USBDevice/USBDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "USBDevice_Types.h"
#include "USBPhy.h"
#include "mbed_critical.h"
#include "Callback.h"

/**
* \defgroup usb_device USB Device
Expand All @@ -43,7 +44,7 @@
*/
class USBDevice: public USBPhyEvents {
public:
typedef void (USBDevice::*ep_cb_t)();
typedef mbed::Callback<void()> ep_cb_t;

enum RequestResult {
Receive = 0,
Expand Down Expand Up @@ -148,21 +149,6 @@ class USBDevice: public USBPhyEvents {
*/
bool endpoint_add(usb_ep_t endpoint, uint32_t max_packet, usb_ep_type_t type, ep_cb_t callback = NULL);

/**
* Add an endpoint
*
* @param endpoint Endpoint to enable
* @param max_packet Maximum size of a packet which can be sent or received on this endpoint
* @param type Endpoint type - USB_EP_TYPE_BULK, USB_EP_TYPE_INT or USB_EP_TYPE_ISO
* @param callback Method pointer to be called when a packet is transferred
* @returns true if successful, false otherwise
*/
template<typename T>
bool endpoint_add(usb_ep_t endpoint, uint32_t max_packet, usb_ep_type_t type, void (T::*callback)())
{
return endpoint_add(endpoint, max_packet, type, static_cast<ep_cb_t>(callback));
}

/**
* Remove an endpoint
*
Expand Down
4 changes: 2 additions & 2 deletions usb/device/USBHID/USBHID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,8 @@ void USBHID::callback_set_configuration(uint8_t configuration)
}

// Configure endpoints > 0
endpoint_add(_int_in, MAX_HID_REPORT_SIZE, USB_EP_TYPE_INT, &USBHID::_send_isr);
endpoint_add(_int_out, MAX_HID_REPORT_SIZE, USB_EP_TYPE_INT, &USBHID::_read_isr);
endpoint_add(_int_in, MAX_HID_REPORT_SIZE, USB_EP_TYPE_INT, mbed::callback(this, &USBHID::_send_isr));
endpoint_add(_int_out, MAX_HID_REPORT_SIZE, USB_EP_TYPE_INT, mbed::callback(this, &USBHID::_read_isr));

// We activate the endpoint to be able to recceive data
read_start(_int_out, _output_report.data, MAX_HID_REPORT_SIZE);
Expand Down
4 changes: 2 additions & 2 deletions usb/device/USBMIDI/USBMIDI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ void USBMIDI::callback_set_configuration(uint8_t configuration)
}

endpoint_remove_all();
endpoint_add(_bulk_in, MaxSize, USB_EP_TYPE_BULK, &USBMIDI::_in_callback);
endpoint_add(_bulk_out, MaxSize, USB_EP_TYPE_BULK, &USBMIDI::_out_callback);
endpoint_add(_bulk_in, MaxSize, USB_EP_TYPE_BULK, mbed::callback(this, &USBMIDI::_in_callback));
endpoint_add(_bulk_out, MaxSize, USB_EP_TYPE_BULK, mbed::callback(this, &USBMIDI::_out_callback));

read_start(_bulk_out, _bulk_buf, MaxSize);

Expand Down
4 changes: 2 additions & 2 deletions usb/device/USBMSD/USBMSD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,8 @@ void USBMSD::_configure()
_mutex.lock();

// Configure endpoints > 0
endpoint_add(_bulk_in, MAX_PACKET, USB_EP_TYPE_BULK, &USBMSD::_isr_in);
endpoint_add(_bulk_out, MAX_PACKET, USB_EP_TYPE_BULK, &USBMSD::_isr_out);
endpoint_add(_bulk_in, MAX_PACKET, USB_EP_TYPE_BULK, mbed::callback(this, &USBMSD::_isr_in));
endpoint_add(_bulk_out, MAX_PACKET, USB_EP_TYPE_BULK, mbed::callback(this, &USBMSD::_isr_out));
MBED_ASSERT(sizeof(_bulk_out_buf) == MAX_PACKET);
MBED_ASSERT(sizeof(_bulk_in_buf) == MAX_PACKET);

Expand Down
4 changes: 2 additions & 2 deletions usb/device/USBSerial/USBCDC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ void USBCDC::callback_set_configuration(uint8_t configuration)
if (configuration == DEFAULT_CONFIGURATION) {
// Configure endpoints > 0
endpoint_add(_int_in, CDC_MAX_PACKET_SIZE, USB_EP_TYPE_INT);
endpoint_add(_bulk_in, CDC_MAX_PACKET_SIZE, USB_EP_TYPE_BULK, &USBCDC::_send_isr);
endpoint_add(_bulk_out, CDC_MAX_PACKET_SIZE, USB_EP_TYPE_BULK, &USBCDC::_receive_isr);
endpoint_add(_bulk_in, CDC_MAX_PACKET_SIZE, USB_EP_TYPE_BULK, mbed::callback(this, &USBCDC::_send_isr));
endpoint_add(_bulk_out, CDC_MAX_PACKET_SIZE, USB_EP_TYPE_BULK, mbed::callback(this, &USBCDC::_receive_isr));

read_start(_bulk_out, _rx_buf, sizeof(_rx_buffer));
_rx_in_progress = true;
Expand Down

0 comments on commit a0c6d9c

Please sign in to comment.