1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

pkg/tinyusb: add tinyUSB netdev module

This commit is contained in:
Gunar Schorcht 2022-11-14 07:26:24 +01:00
parent 5a270fe521
commit 295f8339d2
7 changed files with 376 additions and 2 deletions

View File

@ -27,6 +27,9 @@ tinyusb_dfu:
tinyusb_hw:
$(QQ)"$(MAKE)" -C $(RIOTPKG)/$(PKG_NAME)/hw
tinyusb_netdev:
$(QQ)"$(MAKE)" -C $(RIOTPKG)/$(PKG_NAME)/netdev
tinyusb_class_audio:
$(QQ)"$(MAKE)" -C $(PSRC)/class/audio -f $(RIOTBASE)/Makefile.base MODULE=$@

View File

@ -37,8 +37,11 @@ endif
ifneq (,$(filter tinyusb_netdev,$(USEMODULE)))
USEMODULE += luid
USEMODULE += netdev_eth
USEMODULE += tinyusb_class_net
USEMODULE += tinyusb_device
ifeq (,$(filter tinyusb_class_net_%,$(USEMODULE)))
# default to CDC ECM
USEMODULE += tinyusb_class_net_cdc_ecm
endif
endif
ifneq (,$(filter tinyusb_class_net_rndis,$(USEMODULE)))

View File

@ -5,7 +5,7 @@
* @author Gunar Schorcht <gunar@schorcht.net>
* @see https://github.com/hathach/tinyusb
*
* # TinyUSB
* # TinyUSB {#pkg_tinyusb}
*
* tinyUSB is an open-source cross-platform USB Host/Device stack for
* embedded systems.

View File

@ -0,0 +1,16 @@
# Copyright (c) 2022 Gunar Schorcht
#
# This file is subject to the terms and conditions of the GNU Lesser
# General Public License v2.1. See the file LICENSE in the top level
# directory for more details.
#
config MODULE_TINYUSB_NETDEV
bool "TinyUSB Network Device Driver"
depends on HAS_TINYUSB_DEVICE
depends on TEST_KCONFIG
select MODULE_LUID
select MODULE_NETDEV_ETH
select MODULE_TINYUSB_CLASS_NET
select MODULE_TINYUSB_DEVICE
select PACKAGE_TINYUSB

View File

@ -0,0 +1,3 @@
MODULE = tinyusb_netdev
include $(RIOTBASE)/Makefile.base

View File

@ -0,0 +1,92 @@
/*
* Copyright (C) 2022 Gunar Schorcht
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @defgroup pkg_tinyusb_netdev TinyUSB netdev driver
* @ingroup pkg_tinyusb
* @ingroup drivers_netdev
* @brief TinyUSB netdev driver for CDC ECM, CDC NCM and RNDIS protocol
*
* The tinyUSB netdev driver is part of the \ref pkg_tinyusb "tinyUSB package"
* and is enabled by module `tinyusb_netdev`. It is available for boards that
* provide the `tinyusb_device` feature.
*
* @note Since the tinyUSB package is distinct from (and incompatible with)
* the USB stack provided around USBUS in RIOT (see USB), the tinyUSB netdev
* driver cannot be used together with with any USBUS device class.
*
* The tinyUSB netdev driver uses Ethernet over USB and supports the following
* protocols:
*
* - CDC ECM (Ethernet Control Model)
* - CDC NCM (Network Control Model)
* - RNDIS (Microsoft Remote NDIS)
*
* While Linux and macOS support all these protocols, Microsoft Windows
* only supports the RNDIS protocol and since Windows version 11 also the
* CDC NCM protocol. macOS supports the RNDIS protocol since version 10.15
* (Catalina).
*
* Which protocol is used is selected by the corresponding
* pseudomodules `tinyusb_class_net_cdc_ecm`, `tinyusb_class_net_cdc_ncm`
* and `tinyusb_class_net_rndis`.
*
* The CDC ECM protocol (`tinyusb_class_net_cdc_ecm`) and the RNDIS protocol
* (`tinyusb_class_net_rndis`) can be used simultaneously to support all
* operating systems, for example :
* ```
* USEMODULE='tinyusb_netdev tinyusb_class_net_rndis tinyusb_class_net_cdc_ecm' \
* BOARD=... make -C ... flash
* ```
* In this case, the CDC ECM protocol is the default protocol and the RNDIS
* protocol the alternative protocol defined as second device configuration.
*
* The CDC NCM protocol cannot be used together with the CDC ECM or the RNDIS
* protocol.
*
* @{
* @file
* @brief TinyUSB netdev driver for CDC ECM, CDC NCM and RNDIS protocol
*
* @author Gunar Schorcht <gunar@schorcht.net>
*/
#ifndef TINYUSB_NETDEV_H
#define TINYUSB_NETDEV_H
#include "net/ethernet.h"
#include "net/netdev.h"
#include "mutex.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Device descriptor for tinyUSB CDC ECM network devices
*/
typedef struct {
netdev_t netdev; /**< netdev parent struct */
uint8_t mac_addr[ETHERNET_ADDR_LEN]; /**< device MAC address */
uint8_t mac_host[ETHERNET_ADDR_LEN]; /**< host MAC address */
uint16_t rx_len; /**< number of bytes received */
uint16_t tx_len; /**< number of bytes in transmit buffer */
const uint8_t *rx_buf; /**< receive buffer in tinyUSB stack */
uint8_t tx_buf[ETHERNET_MAX_LEN]; /**< transmit buffer */
mutex_t lock; /**< device is already in use */
} tinyusb_netdev_t;
#ifdef __cplusplus
}
#endif
#endif /* TINYUSB_NETDEV_H */
/** @} */

View File

@ -0,0 +1,257 @@
/*
* Copyright (C) 2022 Gunar Schorcht
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup pkg_tinyusb
* @{
* @file TinyUSB Netdev implementation for CDC Ethernet Control Model
*
* @author Gunar Schorcht <gunar@schorcht.net>
* @}
*/
#define USB_H_USER_IS_RIOT_INTERNAL
#include "kernel_defines.h"
#include "iolist.h"
#include "net/eui_provider.h"
#include "net/netdev/eth.h"
#include "od.h"
#include "device/usbd.h"
#include "class/net/net_device.h"
#include "tinyusb_netdev.h"
#define ENABLE_DEBUG_HEXDUMP 0
#define ENABLE_DEBUG 0
#include "debug.h"
/* The symbol is needed by the tinyUSB stack but not used and shall be removed
* later on, see: https://github.com/hathach/tinyusb/issues/718 */
const uint8_t tud_network_mac_address[6] = { 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
static const netdev_driver_t _tusb_driver;
tinyusb_netdev_t tinyusb_netdev;
void tinyusb_netdev_setup(tinyusb_netdev_t *dev)
{
DEBUG("[tinyusb_netdev] %s: %p\n", __func__, dev);
mutex_init(&dev->lock);
dev->netdev.driver = &_tusb_driver;
dev->rx_len = 0;
dev->tx_len = 0;
netdev_register(&dev->netdev, NETDEV_TINYUSB, 0);
}
static int _send(netdev_t *netdev, const iolist_t *iolist)
{
DEBUG("[tinyusb_netdev] %s: %p %p\n", __func__, netdev, iolist);
tinyusb_netdev_t *dev = (tinyusb_netdev_t *)netdev;
assert(dev == &tinyusb_netdev);
assert(iolist);
if (!tud_ready()) {
return -EBUSY;
}
mutex_lock(&dev->lock);
dev->tx_len = 0;
/* load packet data into TX buffer */
for (const iolist_t *iol = iolist; iol; iol = iol->iol_next) {
if (dev->tx_len + iol->iol_len > ETHERNET_MAX_LEN) {
mutex_unlock(&dev->lock);
return -EOVERFLOW;
}
if (iol->iol_len) {
memcpy (dev->tx_buf + dev->tx_len, iol->iol_base, iol->iol_len);
dev->tx_len += iol->iol_len;
}
}
if (IS_ACTIVE(ENABLE_DEBUG)) {
printf("[tinyusb_netdev] %s: send %d byte from %p\n",
__func__, dev->tx_len, dev->tx_buf);
if (IS_ACTIVE(ENABLE_DEBUG_HEXDUMP) && IS_USED(MODULE_OD)) {
od_hex_dump(dev->tx_buf, dev->tx_len, OD_WIDTH_DEFAULT);
}
}
mutex_unlock(&dev->lock);
/* if the network driver can accept another packet, trigger the send */
if (tud_network_can_xmit(dev->tx_len))
{
tud_network_xmit(dev->tx_buf, dev->tx_len);
return 0;
}
return -EBUSY;
}
static int _recv(netdev_t *netdev, void *buf, size_t len, void *info)
{
(void)info;
DEBUG("[tinyusb_netdev] %s: %p %p %u %p\n", __func__, netdev, buf, len, info);
tinyusb_netdev_t *dev = (tinyusb_netdev_t *)netdev;
assert(dev == &tinyusb_netdev);
mutex_lock(&dev->lock);
int size = dev->rx_len;
if (!buf) {
/* get the size of the frame; if len > 0 then also drop the frame */
if (len > 0) {
/* drop frame requested */
dev->rx_len = 0;
}
mutex_unlock(&dev->lock);
return size;
}
if (dev->rx_len > len) {
/* buffer is smaller than the number of received bytes */
DEBUG("[tinyusb_netdev] %s: Not enough space in receive buffer for %d bytes\n",
__func__, dev->rx_len);
mutex_unlock(&dev->lock);
return -ENOBUFS;
}
if (IS_ACTIVE(ENABLE_DEBUG)) {
printf ("[tinyusb_netdev] %s: received %d byte\n", __func__, dev->rx_len);
if (IS_ACTIVE(ENABLE_DEBUG) && IS_USED(MODULE_OD)) {
od_hex_dump(dev->rx_buf, dev->rx_len, OD_WIDTH_DEFAULT);
}
}
/* copy received date and reset the receive length */
memcpy(buf, dev->rx_buf, dev->rx_len);
dev->rx_len = 0;
mutex_unlock(&dev->lock);
/* indicate that receiving the frame has been finished */
tud_network_recv_renew();
return size;
}
static int _init(netdev_t *netdev)
{
DEBUG("[tinyusb_netdev] %s: %p\n", __func__, netdev);
tinyusb_netdev_t *dev = (tinyusb_netdev_t *)netdev;
assert(dev == &tinyusb_netdev);
netdev_eui48_get(netdev, (eui48_t*)&dev->mac_addr);
netdev->event_callback(netdev, NETDEV_EVENT_LINK_UP);
return 0;
}
static int _get(netdev_t *netdev, netopt_t opt, void *value, size_t max_len)
{
DEBUG("[tinyusb_netdev] %s: %p %u %p %u\n",
__func__, netdev, opt, value, max_len);
tinyusb_netdev_t *dev = (tinyusb_netdev_t *)netdev;
assert(dev == &tinyusb_netdev);
(void)max_len;
switch (opt) {
case NETOPT_ADDRESS:
assert(max_len >= ETHERNET_ADDR_LEN);
memcpy(value, dev->mac_addr, ETHERNET_ADDR_LEN);
return ETHERNET_ADDR_LEN;
default:
return netdev_eth_get(netdev, opt, value, max_len);
}
}
static int _set(netdev_t *netdev, netopt_t opt,
const void *value, size_t len)
{
DEBUG("[tinyusb_netdev] %s: %p %u %p %u\n",
__func__, netdev, opt, value, len);
tinyusb_netdev_t *dev = (tinyusb_netdev_t *)netdev;
assert(dev == &tinyusb_netdev);
switch (opt) {
case NETOPT_ADDRESS:
assert(len == ETHERNET_ADDR_LEN);
memcpy(dev->mac_addr, value, ETHERNET_ADDR_LEN);
return ETHERNET_ADDR_LEN;
default:
return netdev_eth_set(&dev->netdev, opt, value, len);
}
}
static void _isr(netdev_t *netdev)
{
tinyusb_netdev_t *dev = (tinyusb_netdev_t *)netdev;
assert(dev == &tinyusb_netdev);
if (dev->rx_len) {
dev->netdev.event_callback(netdev, NETDEV_EVENT_RX_COMPLETE);
}
}
static const netdev_driver_t _tusb_driver = {
.send = _send,
.recv = _recv,
.init = _init,
.isr = _isr,
.get = _get,
.set = _set,
};
void tud_network_init_cb(void)
{
DEBUG("[tinyusb_netdev] %s\n", __func__);
tinyusb_netdev.rx_len = 0;
tinyusb_netdev.tx_len = 0;
}
bool tud_network_recv_cb(const uint8_t *src, uint16_t size)
{
DEBUG("[tinyusb_netdev] %s: %p %u\n", __func__, src, size);
/* if the previous packet is not handled just return with false */
if (tinyusb_netdev.rx_len) {
return false;
}
assert(size <= ETHERNET_MAX_LEN);
if (size)
{
tinyusb_netdev.rx_buf = src;
tinyusb_netdev.rx_len = size;
}
netdev_trigger_event_isr(&tinyusb_netdev.netdev);
return true;
}
uint16_t tud_network_xmit_cb(uint8_t *dst, void *ref, uint16_t arg)
{
DEBUG("[tinyusb_netdev] %s: %p %p %u\n", __func__, dst, ref, arg);
memcpy(dst, ref, arg);
return arg;
}