Mistake on this page?
Report an issue in GitHub or email us

USBCDC_ECM

USBCDC_ECM class hierarchy

The USBCDC_ECM class emulates an Ethernet interface over USB. You can use the interface to send and receive Ethernet frames over USB.

One application in which you can use this class is a USB to Ethernet adapter. If you add an IP stack and web server on top of this class, you can also create a USB web server that allows you to configure a USB device and to view data using a web browser.

USBCDC_ECM class reference

Public Member Functions
 USBCDC_ECM (bool connect_blocking=true, uint16_t vendor_id=0x0700, uint16_t product_id=0x0101, uint16_t product_release=0x0001)
 Basic constructor. More...
 USBCDC_ECM (USBPhy *phy, uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
 Fully featured constructor. More...
virtual ~USBCDC_ECM ()
 Destroy this object. More...
bool ready ()
 Check if this class is ready. More...
void wait_ready ()
 Block until this device is configured. More...
bool send (uint8_t *buffer, uint32_t size)
 Send a buffer. More...
void receive_nb (uint8_t *buffer, uint32_t size, uint32_t *actual)
 Read from the receive buffer. More...
uint16_t read_packet_filter ()
 Return ethernet packet filter bitmap. More...
void attach_rx (mbed::Callback< void()> cb)
 Attach a callback for when an ethernet packet is received. More...
void attach_filter (mbed::Callback< void()> cb)
 Attach a callback for when a request to configure device ethernet packet filter is received. More...
void init ()
 Initialize this instance. More...
void deinit ()
 Power down this instance. More...
bool configured ()
 Check if the device is configured. More...
void connect ()
 Connect a device. More...
void disconnect ()
 Disconnect a device. More...
void sof_enable ()
 Enable the start of frame interrupt. More...
void sof_disable ()
 Disable the start of frame interrupt. More...
bool endpoint_add (usb_ep_t endpoint, uint32_t max_packet, usb_ep_type_t type, ep_cb_t callback=NULL)
 Add an endpoint. More...
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. More...
void endpoint_remove (usb_ep_t endpoint)
 Remove an endpoint. More...
void endpoint_remove_all ()
 Remove all non-zero endpoints. More...
void endpoint_stall (usb_ep_t endpoint)
 Stall an endpoint. More...
void endpoint_unstall (usb_ep_t endpoint)
 Unstall an endpoint. More...
uint32_t endpoint_max_packet_size (usb_ep_t endpoint)
 Get the current maximum size for this endpoint. More...
void endpoint_abort (usb_ep_t endpoint)
 Abort the current transfer on this endpoint. More...
bool read_start (usb_ep_t endpoint, uint8_t *buffer, uint32_t size)
 start a read on the given endpoint More...
uint32_t read_finish (usb_ep_t endpoint)
 Get the status of a read. More...
bool write_start (usb_ep_t endpoint, uint8_t *buffer, uint32_t size)
 Write a data to the given endpoint. More...
uint32_t write_finish (usb_ep_t endpoint)
 Get the status of a write. More...
Protected Member Functions
virtual void callback_state_change (DeviceState new_state)
 Called when USB changes state. More...
virtual void callback_request_xfer_done (const setup_packet_t *setup, bool aborted)
 Called by USBDevice on data stage completion. More...
virtual void callback_request (const setup_packet_t *setup)
 Called by USBDevice on Endpoint0 request. More...
virtual void callback_reset ()
 Called by USBDevice layer on bus reset. More...
virtual void callback_power (bool powered)
 Called by USBDevice layer on power state change. More...
virtual void callback_sof (int frame_number)
 Called by USBDevice layer on each new USB frame. More...
void complete_request (RequestResult result, uint8_t *data=NULL, uint32_t size=0)
 Called to complete the setup stage of a callback request. More...
void complete_request_xfer_done (bool success)
 Called to complete the data stage of a callback request. More...
void complete_set_configuration (bool success)
 Called to complete a set configuration command. More...
void complete_set_interface (bool success)
 Called to complete a set interface command. More...
uint8_t * find_descriptor (uint8_t descriptor_type, uint8_t index=0)
 Find a descriptor type inside the configuration descriptor. More...
const usb_ep_table_tendpoint_table ()
 Get the endpoint table of this device. More...
virtual void start_process ()
 Callback called to indicate the USB processing needs to be done. More...
virtual void lock ()
 Acquire exclusive access to this instance USBDevice. More...
virtual void unlock ()
 Release exclusive access to this instance USBDevice. More...
virtual void assert_locked ()
 Assert that the current thread of execution holds the lock. More...

Note: Because Windows doesn’t provide native support for the USB CDC-ECM model, you must use third party drivers to use this class on Windows.

USBCDC_ECM example

The example below sends an Ethernet frame that carries "Hello world" payload with a custom EtherType to the host PC. You can capture the frame by using a program called "Wireshark":

  1. Flash the board, and ensure the target's USB is plugged into the PC.
  2. Open Wireshark.
  3. Click Capture > Options to select the correct capture interface.
  4. Click Capture > Start.
  5. Click captured packet from source address 12:34:56:78:9a:bc to see the "Hello world" payload.
/*
* Copyright (c) 2019 ARM Limited. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "mbed.h"
#include "USBCDC_ECM.h"

/* Ethernet II frame */
typedef struct {
    uint8_t dst_mac[6];
    uint8_t src_mac[6];
    uint16_t eth_type;
    char payload[12];
} packet_t;

static packet_t packet = {
    .dst_mac = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
    .src_mac = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc},
    .eth_type = 0xaaaa, /* unused EtherType */
    .payload = "Hello world"
};

USBCDC_ECM ecm;

int main()
{
    while (true) {
        ecm.send((uint8_t *)&packet, sizeof(packet));
        wait(1.0);
    }
}

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.