Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
USBMIDI Class Reference
[USBMIDI class]
#include <USBMIDI.h>
Inherits USBDevice.
Public Member Functions | |
USBMIDI (bool connect_blocking=true, uint16_t vendor_id=0x0700, uint16_t product_id=0x0101, uint16_t product_release=0x0001) | |
Basic constructor. | |
USBMIDI (USBPhy *phy, uint16_t vendor_id, uint16_t product_id, uint16_t product_release) | |
Fully featured constructor. | |
virtual | ~USBMIDI () |
Destroy this object. | |
bool | ready () |
Check if this class is ready. | |
void | wait_ready () |
Block until this device is configured. | |
bool | write (MIDIMessage m) |
Send a MIDIMessage. | |
bool | readable () |
Check if a message can be read. | |
bool | read (MIDIMessage *m) |
Read a message. | |
void | attach (mbed::Callback< void()> callback) |
Attach a callback for when a MIDIEvent is received. | |
void | init () |
Initialize this instance. | |
void | deinit () |
Power down this instance. | |
bool | configured () |
Check if the device is configured. | |
void | connect () |
Connect a device This method can also be used to resume USB operation when USB power is detected after it was suspended via USBDevice::deinit. | |
void | disconnect () |
Disconnect a device. | |
void | sof_enable () |
Enable the start of frame interrupt. | |
void | sof_disable () |
Disable the start of frame interrupt. | |
bool | endpoint_add (usb_ep_t endpoint, uint32_t max_packet, usb_ep_type_t type, mbed::Callback< void()> callback=NULL) |
Add an endpoint. | |
template<typename T > | |
bool | endpoint_add (usb_ep_t endpoint, uint32_t max_packet, usb_ep_type_t type, void(T::*callback)()) |
Add an endpoint. | |
void | endpoint_remove (usb_ep_t endpoint) |
Remove an endpoint. | |
void | endpoint_remove_all () |
Remove all non-zero endpoints. | |
void | endpoint_stall (usb_ep_t endpoint) |
Stall an endpoint. | |
void | endpoint_unstall (usb_ep_t endpoint) |
Un-stall an endpoint. | |
uint32_t | endpoint_max_packet_size (usb_ep_t endpoint) |
Get the current maximum size for this endpoint. | |
void | endpoint_abort (usb_ep_t endpoint) |
Abort the current transfer on this endpoint. | |
bool | read_start (usb_ep_t endpoint, uint8_t *buffer, uint32_t size) |
start a read on the given endpoint | |
uint32_t | read_finish (usb_ep_t endpoint) |
Get the status of a read. | |
bool | write_start (usb_ep_t endpoint, uint8_t *buffer, uint32_t size) |
Write a data to the given endpoint. | |
uint32_t | write_finish (usb_ep_t endpoint) |
Get the status of a write. | |
Protected Member Functions | |
virtual void | callback_state_change (DeviceState new_state) |
Called when USB changes state. | |
virtual void | callback_request (const setup_packet_t *setup) |
Called by USBDevice on Endpoint0 request. | |
virtual void | callback_request_xfer_done (const setup_packet_t *setup, bool aborted) |
Called by USBDevice on data stage completion. | |
virtual void | callback_power (bool powered) |
Called by USBDevice layer on power state change. | |
virtual void | callback_sof (int frame_number) |
Called by USBDevice layer on each new USB frame. | |
virtual void | callback_reset () |
Called by USBDevice layer on bus reset. | |
void | complete_request (RequestResult result, uint8_t *data=NULL, uint32_t size=0) |
Called to complete the setup stage of a callback request. | |
void | complete_request_xfer_done (bool success) |
Called to complete the data stage of a callback request. | |
void | complete_set_configuration (bool success) |
Called to complete a set configuration command. | |
void | complete_set_interface (bool success) |
Called to complete a set interface command. | |
uint8_t * | find_descriptor (uint8_t descriptor_type, uint8_t index=0) |
Find a descriptor type inside the configuration descriptor. | |
const usb_ep_table_t * | endpoint_table () |
Get the endpoint table of this device. | |
virtual void | start_process () |
Callback called to indicate the USB processing needs to be done. | |
virtual void | lock () |
Acquire exclusive access to this instance USBDevice. | |
virtual void | unlock () |
Release exclusive access to this instance USBDevice. | |
virtual void | assert_locked () |
Assert that the current thread of execution holds the lock. |
Detailed Description
USBMIDI example.
#include "mbed.h" #include "USBMIDI.h" USBMIDI midi; int main() { while (1) { for(int i=48; i<83; i++) { // send some messages! midi.write(MIDIMessage::NoteOn(i)); wait(0.25); midi.write(MIDIMessage::NoteOff(i)); wait(0.5); } } }
Definition at line 60 of file USBMIDI.h.
Constructor & Destructor Documentation
USBMIDI | ( | bool | connect_blocking = true , |
uint16_t | vendor_id = 0x0700 , |
||
uint16_t | product_id = 0x0101 , |
||
uint16_t | product_release = 0x0001 |
||
) |
Basic constructor.
Construct this object optionally connecting and blocking until it is ready.
- Note:
- Do not use this constructor in derived classes.
- Parameters:
-
connect_blocking true to perform a blocking connect, false to start in a disconnected state vendor_id Your vendor_id product_id Your product_id product_release Your product_release
Definition at line 27 of file USBMIDI.cpp.
Fully featured constructor.
Construct this object with the supplied USBPhy and parameters. The user this object is responsible for calling connect() or init().
- Note:
- Derived classes must use this constructor and call init() or connect() themselves. Derived classes should also call deinit() in their destructor. This ensures that no interrupts can occur when the object is partially constructed or destroyed.
- Parameters:
-
phy USB phy to use vendor_id Your vendor_id product_id Your product_id product_release Your product_release
Definition at line 40 of file USBMIDI.cpp.
~USBMIDI | ( | ) | [virtual] |
Destroy this object.
Any classes which inherit from this class must call deinit before this destructor runs.
Definition at line 48 of file USBMIDI.cpp.
Member Function Documentation
void assert_locked | ( | ) | [protected, virtual, inherited] |
Assert that the current thread of execution holds the lock.
Definition at line 1737 of file USBDevice.cpp.
void attach | ( | mbed::Callback< void()> | callback ) |
Attach a callback for when a MIDIEvent is received.
- Parameters:
-
callback code to call when a packet is received
Definition at line 176 of file USBMIDI.cpp.
virtual void callback_power | ( | bool | powered ) | [protected, virtual, inherited] |
Called by USBDevice layer on power state change.
- Parameters:
-
powered true if device is powered, false otherwise
Warning: Called in ISR context
Definition at line 342 of file USBDevice.h.
void callback_request | ( | const setup_packet_t * | setup ) | [protected, virtual] |
Called by USBDevice on Endpoint0 request.
This is used to handle extensions to standard requests and class specific requests. The function complete_request must be always be called in response to this callback.
Warning: Called in ISR context
Implements USBDevice.
Definition at line 198 of file USBMIDI.cpp.
void callback_request_xfer_done | ( | const setup_packet_t * | setup, |
bool | aborted | ||
) | [protected, virtual] |
Called by USBDevice on data stage completion.
The function complete_request_xfer_done must be always be called in response to this callback.
- Parameters:
-
setup Setup packet of the current request aborted false if the operation was aborted, true otherwise
Warning: Called in ISR context
Implements USBDevice.
Definition at line 209 of file USBMIDI.cpp.
virtual void callback_reset | ( | ) | [protected, virtual, inherited] |
Called by USBDevice layer on bus reset.
complete_reset must be called after the device is fully reset.
Warning: Called in ISR context
Definition at line 370 of file USBDevice.h.
virtual void callback_sof | ( | int | frame_number ) | [protected, virtual, inherited] |
Called by USBDevice layer on each new USB frame.
Callbacks are enabled and disabled by calling sof_enable and sof_disable.
- Parameters:
-
frame_number The current frame number
Warning: Called in ISR context
Definition at line 357 of file USBDevice.h.
void callback_state_change | ( | DeviceState | new_state ) | [protected, virtual] |
Called when USB changes state.
- Parameters:
-
new_state The new state of the USBDevice
Warning: Called in ISR context
Implements USBDevice.
Definition at line 185 of file USBMIDI.cpp.
void complete_request | ( | RequestResult | result, |
uint8_t * | data = NULL , |
||
uint32_t | size = 0 |
||
) | [protected, inherited] |
Called to complete the setup stage of a callback request.
Possible options that can be passed as a result are:
- Receive - Start the data OUT phase of this control transfer
- Send - Start the data IN phase of this control transfer
- Success - Operation was a success so start the status phase
- Failure - Operation failed or is unsupported so send a stall
- PassThrough - Pass on the request for standard processing
- Parameters:
-
result The result of the setup phase. data Buffer to send or receive if the result is Send or Receive size Size to transfer if the result is Send or Receive
Definition at line 694 of file USBDevice.cpp.
void complete_request_xfer_done | ( | bool | success ) | [protected, inherited] |
Called to complete the data stage of a callback request.
- Parameters:
-
success true if the operation was successful, false otherwise
Definition at line 313 of file USBDevice.cpp.
void complete_set_configuration | ( | bool | success ) | [protected, inherited] |
Called to complete a set configuration command.
- Parameters:
-
success true if the configuration was set, false otherwise
Definition at line 386 of file USBDevice.cpp.
void complete_set_interface | ( | bool | success ) | [protected, inherited] |
Called to complete a set interface command.
- Parameters:
-
success true if the interface was set, false otherwise
Definition at line 466 of file USBDevice.cpp.
bool configured | ( | ) | [inherited] |
Check if the device is configured.
- Returns:
- true if configured, false otherwise
Definition at line 1043 of file USBDevice.cpp.
void connect | ( | ) | [inherited] |
Connect a device This method can also be used to resume USB operation when USB power is detected after it was suspended via USBDevice::deinit.
Reimplemented in USBAudio, and USBMSD.
Definition at line 1054 of file USBDevice.cpp.
void deinit | ( | ) | [inherited] |
Power down this instance.
Disable interrupts and stop sending events. This method can be used for temporary power-saving; This call can allow USB to be temporarily disabled to permit power saving. However, it is up to the user to make sure all the transfers have concluded (for example when USB power is lost). USBDevice::connect can be used to resume USB operation.
Definition at line 1030 of file USBDevice.cpp.
void disconnect | ( | void | ) | [inherited] |
Disconnect a device.
Reimplemented in USBAudio, and USBMSD.
Definition at line 1070 of file USBDevice.cpp.
void endpoint_abort | ( | usb_ep_t | endpoint ) | [inherited] |
Abort the current transfer on this endpoint.
- Parameters:
-
endpoint endpoint with transfer to abort
- Note:
- This endpoint must already have been setup with endpoint_add
Definition at line 1396 of file USBDevice.cpp.
bool endpoint_add | ( | usb_ep_t | endpoint, |
uint32_t | max_packet, | ||
usb_ep_type_t | type, | ||
void(T::*)() | callback | ||
) | [inherited] |
Add an endpoint.
- Parameters:
-
endpoint Endpoint to enable max_packet Maximum size of a packet which can be sent or received on this endpoint type Endpoint type - USB_EP_TYPE_BULK, USB_EP_TYPE_INT or USB_EP_TYPE_ISO callback Method pointer to be called when a packet is transferred
- Returns:
- true if successful, false otherwise
Definition at line 162 of file USBDevice.h.
bool endpoint_add | ( | usb_ep_t | endpoint, |
uint32_t | max_packet, | ||
usb_ep_type_t | type, | ||
mbed::Callback< void()> | callback = NULL |
||
) | [inherited] |
Add an endpoint.
- Parameters:
-
endpoint Endpoint to enable max_packet Maximum size of a packet which can be sent or received on this endpoint type Endpoint type - USB_EP_TYPE_BULK, USB_EP_TYPE_INT or USB_EP_TYPE_ISO callback Method pointer to be called when a packet is transferred
- Returns:
- true if successful, false otherwise
Definition at line 1109 of file USBDevice.cpp.
uint32_t endpoint_max_packet_size | ( | usb_ep_t | endpoint ) | [inherited] |
Get the current maximum size for this endpoint.
Return the currently configured maximum packet size, wMaxPacketSize, for this endpoint.
- Note:
- This endpoint must already have been setup with endpoint_add
Definition at line 1380 of file USBDevice.cpp.
void endpoint_remove | ( | usb_ep_t | endpoint ) | [inherited] |
Remove an endpoint.
- Parameters:
-
endpoint Endpoint to disable
- Note:
- This endpoint must already have been setup with endpoint_add
Definition at line 1149 of file USBDevice.cpp.
void endpoint_remove_all | ( | ) | [inherited] |
Remove all non-zero endpoints.
Definition at line 1190 of file USBDevice.cpp.
void endpoint_stall | ( | usb_ep_t | endpoint ) | [inherited] |
Stall an endpoint.
If there is an ongoing transfer on this endpoint then it will be aborted.
- Parameters:
-
endpoint Endpoint to stall
- Note:
- You cannot stall endpoint 0 with this function
- This endpoint must already have been setup with endpoint_add
Definition at line 1204 of file USBDevice.cpp.
const usb_ep_table_t * endpoint_table | ( | ) | [protected, inherited] |
Get the endpoint table of this device.
Definition at line 1321 of file USBDevice.cpp.
void endpoint_unstall | ( | usb_ep_t | endpoint ) | [inherited] |
Un-stall an endpoint.
Un-stalling an endpoint resets data toggle back to DATA0. Additionally, if there is an ongoing transfer on this endpoint it will be aborted.
- Parameters:
-
endpoint Endpoint to un-stall
- Note:
- This endpoint must already have been setup with endpoint_add
Definition at line 1241 of file USBDevice.cpp.
uint8_t * find_descriptor | ( | uint8_t | descriptor_type, |
uint8_t | index = 0 |
||
) | [protected, inherited] |
Find a descriptor type inside the configuration descriptor.
- Parameters:
-
descriptor_type Type of descriptor to find index Configuration descriptor index ( 0 if only one configuration present )
- Returns:
- A descriptor of the given type or NULL if none were found
Definition at line 1278 of file USBDevice.cpp.
void init | ( | ) | [inherited] |
Initialize this instance.
This function must be called before calling any other functions of this class, unless specifically
Definition at line 1017 of file USBDevice.cpp.
void lock | ( | void | ) | [protected, virtual, inherited] |
Acquire exclusive access to this instance USBDevice.
Definition at line 1714 of file USBDevice.cpp.
bool read | ( | MIDIMessage * | m ) |
Read a message.
- Parameters:
-
m The MIDIMessage to fill
- Returns:
- true if a message was read, false otherwise
Definition at line 151 of file USBMIDI.cpp.
uint32_t read_finish | ( | usb_ep_t | endpoint ) | [inherited] |
Get the status of a read.
- Parameters:
-
endpoint endpoint to get the status of
- Returns:
- number of bytes read by this endpoint
Definition at line 1489 of file USBDevice.cpp.
bool read_start | ( | usb_ep_t | endpoint, |
uint8_t * | buffer, | ||
uint32_t | size | ||
) | [inherited] |
start a read on the given endpoint
Start a read on the given endpoint. The data buffer must remain unchanged until the transfer either completes or is aborted.
- Parameters:
-
endpoint endpoint to read data from buffer buffer to fill with read data size The size of data to read. This must be greater than or equal to the max packet size for this endpoint
- Returns:
- true if the read was completed, otherwise false
- Note:
- This endpoint must already have been setup with endpoint_add
Definition at line 1431 of file USBDevice.cpp.
bool readable | ( | ) |
Check if a message can be read.
- Returns:
- true if a packet can be read false otherwise
- Note:
- USBMIDI::attach must be called to enable the receiver
Definition at line 140 of file USBMIDI.cpp.
bool ready | ( | ) |
Check if this class is ready.
- Returns:
- true if configured, false otherwise
Definition at line 69 of file USBMIDI.cpp.
void sof_disable | ( | ) | [inherited] |
Disable the start of frame interrupt.
Stop calling USBDevice::callback_sof.
Definition at line 1100 of file USBDevice.cpp.
void sof_enable | ( | ) | [inherited] |
Enable the start of frame interrupt.
Call USBDevice::callback_sof on every frame.
Definition at line 1091 of file USBDevice.cpp.
void start_process | ( | ) | [protected, virtual, inherited] |
Callback called to indicate the USB processing needs to be done.
Implements USBPhyEvents.
Definition at line 1705 of file USBDevice.cpp.
void unlock | ( | void | ) | [protected, virtual, inherited] |
Release exclusive access to this instance USBDevice.
Definition at line 1721 of file USBDevice.cpp.
void wait_ready | ( | ) |
Block until this device is configured.
Definition at line 74 of file USBMIDI.cpp.
bool write | ( | MIDIMessage | m ) |
Send a MIDIMessage.
- Parameters:
-
m The MIDIMessage to send
- Returns:
- true if the message was sent, false otherwise
Definition at line 80 of file USBMIDI.cpp.
uint32_t write_finish | ( | usb_ep_t | endpoint ) | [inherited] |
Get the status of a write.
- Parameters:
-
endpoint endpoint to get the status of
- Returns:
- number of bytes sent by this endpoint
Definition at line 1583 of file USBDevice.cpp.
bool write_start | ( | usb_ep_t | endpoint, |
uint8_t * | buffer, | ||
uint32_t | size | ||
) | [inherited] |
Write a data to the given endpoint.
Write data to an endpoint. The data sent must remain unchanged until the transfer either completes or is aborted.
- Parameters:
-
endpoint endpoint to write data to buffer data to write size the size of data to send. This must be less than or equal to the max packet size of this endpoint
- Note:
- This endpoint must already have been setup with endpoint_add
Definition at line 1522 of file USBDevice.cpp.
Generated on Tue Jul 12 2022 13:55:37 by
