Skip to content

shared/tinyusb: Add support for USB Network (NCM) interface. #16459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions extmod/extmod.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ set(MICROPY_SOURCE_EXTMOD
${MICROPY_EXTMOD_DIR}/network_lwip.c
${MICROPY_EXTMOD_DIR}/network_ninaw10.c
${MICROPY_EXTMOD_DIR}/network_ppp_lwip.c
${MICROPY_EXTMOD_DIR}/network_usbd_ncm.c
${MICROPY_EXTMOD_DIR}/network_wiznet5k.c
${MICROPY_EXTMOD_DIR}/os_dupterm.c
${MICROPY_EXTMOD_DIR}/vfs.c
Expand Down
1 change: 1 addition & 0 deletions extmod/extmod.mk
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ SRC_EXTMOD_C += \
extmod/network_lwip.c \
extmod/network_ninaw10.c \
extmod/network_ppp_lwip.c \
extmod/network_usbd_ncm.c \
extmod/network_wiznet5k.c \
extmod/os_dupterm.c \
extmod/vfs.c \
Expand Down
9 changes: 8 additions & 1 deletion extmod/lwip-include/lwipopts_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@
#define LWIP_DHCP_DOES_ACD_CHECK 0 // to speed DHCP up
#define LWIP_DNS 1
#define LWIP_DNS_SUPPORT_MDNS_QUERIES 1
#define LWIP_MDNS_RESPONDER 1
#define LWIP_IGMP 1

#ifndef LWIP_MDNS_RESPONDER
#define LWIP_MDNS_RESPONDER 1
#endif

#if MICROPY_PY_LWIP_PPP
#define PPP_SUPPORT 1
#define PAP_SUPPORT 1
Expand Down Expand Up @@ -108,4 +111,8 @@

typedef uint32_t sys_prot_t;

// #define LWIP_DEBUG
// #define LWIP_DBG_MIN_LEVEL LWIP_DBG_LEVEL_ALL
// #define LWIP_DBG_TYPES_ON LWIP_DBG_ON

#endif // MICROPY_INCLUDED_LWIPOPTS_COMMON_H
57 changes: 57 additions & 0 deletions extmod/machine_usb_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,58 @@ static mp_obj_t usb_device_config(size_t n_args, const mp_obj_t *pos_args, mp_ma
}
static MP_DEFINE_CONST_FUN_OBJ_KW(usb_device_config_obj, 1, usb_device_config);

// Per-class control methods
static mp_obj_t usb_device_enable_cdc(size_t n_args, const mp_obj_t *args) {
mp_obj_usb_device_t *self = MP_OBJ_TO_PTR(args[0]);

if (self->active) {
mp_raise_OSError(MP_EINVAL);
}

if (n_args == 1) {
return mp_obj_new_bool(mp_usbd_class_state.cdc_enabled);
} else {
bool enable = mp_obj_is_true(args[1]);
mp_usbd_enable_class_cdc(enable);
return mp_const_none;
}
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(usb_device_enable_cdc_obj, 1, 2, usb_device_enable_cdc);

static mp_obj_t usb_device_enable_msc(size_t n_args, const mp_obj_t *args) {
mp_obj_usb_device_t *self = MP_OBJ_TO_PTR(args[0]);

if (self->active) {
mp_raise_OSError(MP_EINVAL);
}

if (n_args == 1) {
return mp_obj_new_bool(mp_usbd_class_state.msc_enabled);
} else {
bool enable = mp_obj_is_true(args[1]);
mp_usbd_enable_class_msc(enable);
return mp_const_none;
}
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(usb_device_enable_msc_obj, 1, 2, usb_device_enable_msc);

static mp_obj_t usb_device_enable_ncm(size_t n_args, const mp_obj_t *args) {
mp_obj_usb_device_t *self = MP_OBJ_TO_PTR(args[0]);

if (self->active) {
mp_raise_OSError(MP_EINVAL);
}

if (n_args == 1) {
return mp_obj_new_bool(mp_usbd_class_state.ncm_enabled);
} else {
bool enable = mp_obj_is_true(args[1]);
mp_usbd_enable_class_ncm(enable);
return mp_const_none;
}
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(usb_device_enable_ncm_obj, 1, 2, usb_device_enable_ncm);

static const MP_DEFINE_BYTES_OBJ(builtin_default_desc_dev_obj,
&mp_usbd_builtin_desc_dev, sizeof(tusb_desc_device_t));

Expand Down Expand Up @@ -278,6 +330,11 @@ static const mp_rom_map_elem_t usb_device_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_active), MP_ROM_PTR(&usb_device_active_obj) },
{ MP_ROM_QSTR(MP_QSTR_stall), MP_ROM_PTR(&usb_device_stall_obj) },
{ MP_ROM_QSTR(MP_QSTR_remote_wakeup), MP_ROM_PTR(&usb_remote_wakeup_obj) },

// Per-class control methods
{ MP_ROM_QSTR(MP_QSTR_enable_cdc), MP_ROM_PTR(&usb_device_enable_cdc_obj) },
{ MP_ROM_QSTR(MP_QSTR_enable_msc), MP_ROM_PTR(&usb_device_enable_msc_obj) },
{ MP_ROM_QSTR(MP_QSTR_enable_ncm), MP_ROM_PTR(&usb_device_enable_ncm_obj) },

// Built-in driver constants
{ MP_ROM_QSTR(MP_QSTR_BUILTIN_NONE), MP_ROM_PTR(&mp_type_usb_device_builtin_none) },
Expand Down
62 changes: 62 additions & 0 deletions extmod/modlwip.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,68 @@ static const int error_lookup_table[] = {
};
#endif


/*******************************************************************************/
// atoi() used in netif_find()
// Based on (MIT) implementation from https://github.com/littlekernel/lk

int isspace(int c) {
return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v';
}
int isdigit(int c) {
return (c >= '0') && (c <= '9');
}
int isxdigit(int c) {
return isdigit(c) || ((c >= 'a') && (c <= 'f')) || ((c >= 'A') && (c <= 'F'));
}
static int hexval(char c) {
if (c >= '0' && c <= '9') {
return c - '0';
} else if (c >= 'a' && c <= 'f') {
return c - 'a' + 10;
} else if (c >= 'A' && c <= 'F') {
return c - 'A' + 10;
}

return 0;
}

int atoi(const char *num) {
long long value = 0;
int neg = 0;

// skip leading whitespace
while (isspace(num[0])) {
num++;
}

if (num[0] == '0' && num[1] == 'x') {
// hex
num += 2;
while (*num && isxdigit(*num)) {
value = value * 16 + hexval(*num++);
}
} else {
// decimal
if (num[0] == '-') {
neg = 1;
num++;
} else if (num[0] == '+') {
num++;
}

while (*num && isdigit(*num)) {
value = value * 10 + *num++ - '0';
}
}

if (neg) {
value = -value;
}

return (int)value;
}

/*******************************************************************************/
// The socket object provided by lwip.socket.

Expand Down
Loading
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy