Files
micropython/shared/tinyusb/mp_usbd.h
Damien George 207562dfab
Some checks failed
JavaScript code lint and formatting with Biome / eslint (push) Has been cancelled
Check code formatting / code-formatting (push) Has been cancelled
Check spelling with codespell / codespell (push) Has been cancelled
Build docs / build (push) Has been cancelled
Check examples / embedding (push) Has been cancelled
Package mpremote / build (push) Has been cancelled
.mpy file format and tools / test (push) Has been cancelled
Build ports metadata / build (push) Has been cancelled
alif port / build_alif (alif_ae3_build) (push) Has been cancelled
cc3200 port / build (push) Has been cancelled
esp32 port / build_idf (esp32_build_c2_c5_c6) (push) Has been cancelled
esp32 port / build_idf (esp32_build_cmod_spiram_s2) (push) Has been cancelled
esp32 port / build_idf (esp32_build_s3_c3) (push) Has been cancelled
esp8266 port / build (push) Has been cancelled
mimxrt port / build (push) Has been cancelled
nrf port / build (push) Has been cancelled
powerpc port / build (push) Has been cancelled
qemu port / build_and_test_arm (bigendian) (push) Has been cancelled
qemu port / build_and_test_arm (sabrelite) (push) Has been cancelled
qemu port / build_and_test_arm (thumb_hardfp) (push) Has been cancelled
qemu port / build_and_test_arm (thumb_softfp) (push) Has been cancelled
qemu port / build_and_test_rv32 (push) Has been cancelled
qemu port / build_and_test_rv64 (push) Has been cancelled
renesas-ra port / build_renesas_ra_board (push) Has been cancelled
rp2 port / build (push) Has been cancelled
samd port / build (push) Has been cancelled
stm32 port / build_stm32 (stm32_misc_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_nucleo_build) (push) Has been cancelled
stm32 port / build_stm32 (stm32_pyb_build) (push) Has been cancelled
unix port / minimal (push) Has been cancelled
unix port / reproducible (push) Has been cancelled
unix port / standard (push) Has been cancelled
unix port / standard_v2 (push) Has been cancelled
unix port / coverage (push) Has been cancelled
unix port / coverage_32bit (push) Has been cancelled
unix port / nanbox (push) Has been cancelled
unix port / longlong (push) Has been cancelled
unix port / float (push) Has been cancelled
unix port / gil_enabled (push) Has been cancelled
unix port / stackless_clang (push) Has been cancelled
unix port / float_clang (push) Has been cancelled
unix port / settrace_stackless (push) Has been cancelled
unix port / repr_b (push) Has been cancelled
unix port / macos (push) Has been cancelled
unix port / qemu_mips (push) Has been cancelled
unix port / qemu_arm (push) Has been cancelled
unix port / qemu_riscv64 (push) Has been cancelled
unix port / sanitize_address (push) Has been cancelled
unix port / sanitize_undefined (push) Has been cancelled
webassembly port / build (push) Has been cancelled
windows port / build-vs (Debug, true, x64, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, true, x86, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Debug, x64, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Debug, x86, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x64, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, dev, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, dev, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, standard, 2017, [15, 16)) (push) Has been cancelled
windows port / build-vs (Release, true, x86, standard, 2019, [16, 17)) (push) Has been cancelled
windows port / build-vs (Release, x64, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x64, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, dev, 2022, [17, 18)) (push) Has been cancelled
windows port / build-vs (Release, x86, standard, 2022, [17, 18)) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, dev) (push) Has been cancelled
windows port / build-mingw (i686, mingw32, standard) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, dev) (push) Has been cancelled
windows port / build-mingw (x86_64, mingw64, standard) (push) Has been cancelled
windows port / cross-build-on-linux (push) Has been cancelled
zephyr port / build (push) Has been cancelled
Python code lint and formatting with ruff / ruff (push) Has been cancelled
shared/tinyusb: Remove macro guard for tx_overwritabe_if_not_connected.
It's now available in the version of TinyUSB used by this repository.

Also, update to the newer `tud_cdc_configure_t` struct name and newer
`tud_cdc_configure()` function name.

Signed-off-by: Damien George <damien@micropython.org>
2025-11-24 12:46:20 +11:00

172 lines
5.9 KiB
C

/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2022 Blake W. Felt & Angus Gratton
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_SHARED_TINYUSB_MP_USBD_H
#define MICROPY_INCLUDED_SHARED_TINYUSB_MP_USBD_H
#include "py/mpconfig.h"
#ifndef MICROPY_WRAP_TUD_SOF_CB
#define MICROPY_WRAP_TUD_SOF_CB(name) name
#endif
#ifndef MICROPY_WRAP_TUD_CDC_RX_CB
#define MICROPY_WRAP_TUD_CDC_RX_CB(name) name
#endif
#ifndef MICROPY_WRAP_TUD_CDC_LINE_STATE_CB
#define MICROPY_WRAP_TUD_CDC_LINE_STATE_CB(name) name
#endif
#ifndef MICROPY_WRAP_TUD_EVENT_HOOK_CB
#define MICROPY_WRAP_TUD_EVENT_HOOK_CB(name) name
#endif
#if MICROPY_HW_ENABLE_USBDEV
#include "py/obj.h"
#include "py/objarray.h"
#include "py/runtime.h"
#ifndef NO_QSTR
#include "tusb.h"
#include "device/dcd.h"
#include "class/cdc/cdc_device.h"
#endif
// Initialise TinyUSB device.
static inline void mp_usbd_init_tud(void) {
tusb_init();
#if MICROPY_HW_USB_CDC
tud_cdc_configure_t cfg = {
.rx_persistent = 0,
.tx_persistent = 1,
.tx_overwritabe_if_not_connected = 1,
};
tud_cdc_configure(&cfg);
#endif
}
// Run the TinyUSB device task
void mp_usbd_task(void);
// Schedule a call to mp_usbd_task(), even if no USB interrupt has occurred
void mp_usbd_schedule_task(void);
// Function to be implemented in port code.
// Can write a string up to MICROPY_HW_USB_DESC_STR_MAX characters long, plus terminating byte.
extern void mp_usbd_port_get_serial_number(char *buf);
// Most ports need to write a hexadecimal serial number from a byte array. This
// is a helper function for this. out_str must be long enough to hold a string of total
// length (2 * bytes_len + 1) (including NUL terminator).
void mp_usbd_hex_str(char *out_str, const uint8_t *bytes, size_t bytes_len);
// Length of built-in configuration descriptor
#define MP_USBD_BUILTIN_DESC_CFG_LEN (TUD_CONFIG_DESC_LEN + \
(CFG_TUD_CDC ? (TUD_CDC_DESC_LEN) : 0) + \
(CFG_TUD_MSC ? (TUD_MSC_DESC_LEN) : 0) \
)
// Built-in USB device and configuration descriptor values
extern const tusb_desc_device_t mp_usbd_builtin_desc_dev;
extern const uint8_t mp_usbd_builtin_desc_cfg[MP_USBD_BUILTIN_DESC_CFG_LEN];
void mp_usbd_task_callback(mp_sched_node_t *node);
#if !MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE
static inline void mp_usbd_init(void) {
// Without runtime USB support, this can be a thin wrapper wrapper around tusb_init()
// which is called in the below helper function.
mp_usbd_init_tud();
}
static inline void mp_usbd_deinit(void) {
// Called in soft reset path. No-op if no runtime USB devices require cleanup.
}
#else
// Runtime USB Device support requires more complex init/deinit
void mp_usbd_init(void);
void mp_usbd_deinit(void);
const char *mp_usbd_runtime_string_cb(uint8_t index);
// Maximum number of pending exceptions per single TinyUSB task execution
#define MP_USBD_MAX_PEND_EXCS 2
typedef struct {
mp_obj_base_t base;
mp_obj_t desc_dev; // Device descriptor bytes
mp_obj_t desc_cfg; // Configuration descriptor bytes
mp_obj_t desc_strs; // List/dict/similar to look up string descriptors by index
// Runtime device driver callback functions
mp_obj_t open_itf_cb;
mp_obj_t reset_cb;
mp_obj_t control_xfer_cb;
mp_obj_t xfer_cb;
mp_obj_t builtin_driver; // Points to one of mp_type_usb_device_builtin_nnn
bool active; // Has the user set the USB device active?
bool trigger; // Has the user requested the active state change (or re-activate)?
// Temporary pointers for xfer data in progress on each endpoint
// Ensuring they aren't garbage collected until the xfer completes
mp_obj_t xfer_data[CFG_TUD_ENDPPOINT_MAX][2];
// Pointer to a memoryview that is reused to refer to various pieces of
// control transfer data that are pushed to USB control transfer
// callbacks. Python code can't rely on the memoryview contents
// to remain valid after the callback returns!
mp_obj_array_t *control_data;
// Pointers to exceptions thrown inside Python callbacks. See
// usbd_callback_function_n().
mp_uint_t num_pend_excs;
mp_obj_t pend_excs[MP_USBD_MAX_PEND_EXCS];
} mp_obj_usb_device_t;
// Built-in constant objects, possible values of builtin_driver
//
// (Currently not possible to change built-in drivers at runtime, just enable/disable.)
extern const mp_obj_type_t mp_type_usb_device_builtin_default;
extern const mp_obj_type_t mp_type_usb_device_builtin_none;
// Return true if any built-in driver is enabled
static inline bool mp_usb_device_builtin_enabled(const mp_obj_usb_device_t *usbd) {
return usbd->builtin_driver != MP_OBJ_FROM_PTR(&mp_type_usb_device_builtin_none);
}
#endif // MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE
#endif // MICROPY_HW_ENABLE_USBDEV
#endif // MICROPY_INCLUDED_SHARED_TINYUSB_USBD_H