Skip to content
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
2 changes: 1 addition & 1 deletion ports/espressif/background.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "freertos/task.h"

void port_background_tick(void) {
#if CIRCUITPY_USB_HOST
#if CIRCUITPY_USB_HOST && !CIRCUITPY_USB_HOST_TASK
// Enumeration delays need polling even when no new USB IRQ arrives.
if (tuh_task_event_ready()) {
usb_background_schedule();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ CIRCUITPY_ESP_FLASH_SIZE=8MB
CIRCUITPY_ESP_PSRAM_SIZE = 2MB
CIRCUITPY_ESP_PSRAM_MODE = qio
CIRCUITPY_ESP_PSRAM_FREQ = 80m

CIRCUITPY_MAX3421E = 0
54 changes: 43 additions & 11 deletions ports/espressif/common-hal/usb_host/Port.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
#include "bindings/espidf/__init__.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/usb_host/Port.h"
#include "supervisor/port.h"
#include "supervisor/usb.h"
#include "supervisor/shared/tick.h"
#include "esp_private/usb_phy.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "tusb.h"

#if defined(CONFIG_IDF_TARGET_ESP32P4)
Expand All @@ -27,10 +29,42 @@
#if CIRCUITPY_USB_DEVICE && CIRCUITPY_USB_DEVICE_INSTANCE == CIRCUITPY_USB_HOST_INSTANCE
#error "USB host and device must use different controllers"
#endif
#if !CIRCUITPY_USB_HOST_TASK
#error "The espressif USB host runs TinyUSB in a task of its own"
#endif

// TinyUSB's host stack runs in this task, which waits on TinyUSB's event queue
// and so runs as soon as the controller interrupt posts an event. The VM
// doesn't have to get to its background tasks first: an isochronous stream
// queues its next transfer within the frame while the VM writes to an SD card
// or collects garbage.
//
// It runs above the VM's priority, like the MAX3421E's host task, so that a
// busy VM doesn't hold it up. Like the other USB tasks it is pinned to the
// same core as CircuitPython, which leaves the other core for networking.
#define USB_HOST_TASK_PRIORITY (5)
// Bytes. Enumeration and the class and Python module callbacks run on it.
#define USB_HOST_TASK_STACK_SIZE (4096)

static usb_host_port_obj_t usb_host_instance;
static usb_phy_handle_t host_phy;

static StackType_t _host_task_stack[USB_HOST_TASK_STACK_SIZE];
static StaticTask_t _host_task_tcb;
static TaskHandle_t _host_task = NULL;

static void _usb_host_task(void *param) {
(void)param;
while (true) {
// Returns once the events that came in are handled. It waits only as
// long as no TinyUSB timer (enumeration delays, control transfer
// timeouts) is due.
tuh_task_ext(UINT32_MAX, false);
// Python code may be waiting for what the events completed.
port_wake_main_task();
}
}

usb_host_port_obj_t *common_hal_usb_host_port_construct(const mcu_pin_obj_t *dp, const mcu_pin_obj_t *dm) {
// The high-speed PHY has dedicated pads, not GPIOs.
if (dp != &pin_USB_HS_DP || dm != &pin_USB_HS_DM) {
Expand All @@ -53,18 +87,16 @@ usb_host_port_obj_t *common_hal_usb_host_port_construct(const mcu_pin_obj_t *dp,
host_phy = NULL;
CHECK_ESP_RESULT(ESP_FAIL);
}
// The port is never deinited, so neither is the task. It is the only
// caller of tuh_task_ext(), which is why it only starts after tuh_init().
// This runs on the VM, so xPortGetCoreID() is CircuitPython's core.
if (_host_task == NULL) {
_host_task = xTaskCreateStaticPinnedToCore(_usb_host_task, "usbh",
USB_HOST_TASK_STACK_SIZE, NULL, USB_HOST_TASK_PRIORITY,
_host_task_stack, &_host_task_tcb, xPortGetCoreID());
}
self->base.type = &usb_host_port_type;
self->dp = dp;
self->dm = dm;
// Keep polling enumeration timers even without an active display.
supervisor_enable_tick();
return self;
}

// TinyUSB installs the controller IRQ. Process its events on the VM task,
// where the Python USB device and keyboard callbacks are safe to run.
void tuh_event_hook_cb(uint8_t rhport, uint32_t eventid, bool in_isr) {
// Don't wait for the background tick: an isochronous stream must queue its
// next transfer within the frame.
usb_background_schedule();
}
6 changes: 6 additions & 0 deletions ports/espressif/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ extern portMUX_TYPE background_task_mutex;
#define CIRCUITPY_ESP32P4_SWAP_LSFS (0)
#endif

// TinyUSB's host stack runs in a FreeRTOS task of its own that waits on its
// event queue, instead of as a background callback of the VM. Completion
// callbacks then run, and isochronous streams queue their next transfer, while
// the VM is busy. See common-hal/usb_host/Port.c.
#define CIRCUITPY_USB_HOST_TASK (CIRCUITPY_USB_HOST)

#if MICROPY_PERSISTENT_CODE_LOAD_NATIVE
// Loaded native code is copied out of the GC heap into executable RAM.
void *esp_native_code_commit(void *buf, size_t len, void *reloc);
Expand Down
29 changes: 29 additions & 0 deletions ports/unix/coverage.c
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,35 @@ static mp_obj_t extra_coverage(void) {
ringbuf_clear(&ringbuf);
ringbuf_put(&ringbuf, 0xaa);
mp_printf(&mp_plat_print, "%d\n", ringbuf_get16(&ringbuf));

// Multi-byte put/get that wrap around, and discard.
{
byte data[RINGBUF_SIZE + 1];
byte out[RINGBUF_SIZE + 1];
for (int i = 0; i < RINGBUF_SIZE + 1; ++i) {
data[i] = i;
}
ringbuf_clear(&ringbuf);
for (int i = 0; i < RINGBUF_SIZE - 10; ++i) {
ringbuf_put(&ringbuf, i);
ringbuf_get(&ringbuf);
}
// 20 bytes from 10 before the end: wraps.
mp_printf(&mp_plat_print, "%d\n", (int)ringbuf_put_n(&ringbuf, data, 20));
mp_printf(&mp_plat_print, "%d %d\n", (int)ringbuf_num_empty(&ringbuf), (int)ringbuf_num_filled(&ringbuf));
// Only what fits.
mp_printf(&mp_plat_print, "%d\n", (int)ringbuf_put_n(&ringbuf, data + 20, RINGBUF_SIZE + 1 - 20));
mp_printf(&mp_plat_print, "%d %d\n", (int)ringbuf_num_empty(&ringbuf), (int)ringbuf_num_filled(&ringbuf));
mp_printf(&mp_plat_print, "%d\n", (int)ringbuf_put_n(&ringbuf, data, 1));
// Discard 5, then get what is left, which wraps.
mp_printf(&mp_plat_print, "%d\n", (int)ringbuf_discard_n(&ringbuf, 5));
mp_printf(&mp_plat_print, "%d\n", (int)ringbuf_get_n(&ringbuf, out, sizeof(out)));
mp_printf(&mp_plat_print, "%d\n", memcmp(out, data + 5, RINGBUF_SIZE - 5) == 0);
mp_printf(&mp_plat_print, "%d %d\n", (int)ringbuf_num_empty(&ringbuf), (int)ringbuf_num_filled(&ringbuf));
// Nothing to get or discard.
mp_printf(&mp_plat_print, "%d\n", (int)ringbuf_get_n(&ringbuf, out, 1));
mp_printf(&mp_plat_print, "%d\n", (int)ringbuf_discard_n(&ringbuf, 1));
}
}

// pairheap
Expand Down
48 changes: 34 additions & 14 deletions py/ringbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
// CIRCUITPY-CHANGE: API and implementation thoroughly reworked
// No attempt to have atomic operations. Add guards if atomicity required.

#include <string.h>

#include "py/misc.h"
#include "ringbuf.h"

bool ringbuf_init(ringbuf_t *r, uint8_t *buf, size_t size) {
Expand Down Expand Up @@ -103,24 +106,41 @@ size_t ringbuf_num_filled(ringbuf_t *r) {
// If the ring buffer fills up, not all bytes will be written.
// Returns how many bytes were successfully written.
size_t ringbuf_put_n(ringbuf_t *r, const uint8_t *buf, size_t bufsize) {
for (size_t i = 0; i < bufsize; i++) {
if (ringbuf_put(r, buf[i]) < 0) {
// If ringbuf is full, give up and return how many bytes
// we wrote so far.
return i;
}
size_t n = MIN(bufsize, r->size - r->used);
if (n == 0) {
return 0;
}
// At most two copies: up to the end of buf, then from its start.
size_t first = MIN(n, r->size - r->next_write);
memcpy(r->buf + r->next_write, buf, first);
memcpy(r->buf, buf + first, n - first);
r->next_write += n;
if (r->next_write >= r->size) {
r->next_write -= r->size;
}
return bufsize;
r->used += n;
return n;
}

// Returns how many bytes were fetched.
size_t ringbuf_get_n(ringbuf_t *r, uint8_t *buf, size_t bufsize) {
for (size_t i = 0; i < bufsize; i++) {
int b = ringbuf_get(r);
if (b < 0) {
return i;
}
buf[i] = b;
size_t n = MIN(bufsize, r->used);
if (n == 0) {
return 0;
}
size_t first = MIN(n, r->size - r->next_read);
memcpy(buf, r->buf + r->next_read, first);
memcpy(buf + first, r->buf, n - first);
return ringbuf_discard_n(r, n);
}

// Drops up to n of the oldest bytes. Returns how many were dropped.
size_t ringbuf_discard_n(ringbuf_t *r, size_t n) {
n = MIN(n, r->used);
r->next_read += n;
if (r->next_read >= r->size) {
r->next_read -= r->size;
}
return bufsize;
r->used -= n;
return n;
}
1 change: 1 addition & 0 deletions py/ringbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ size_t ringbuf_num_empty(ringbuf_t *r);
size_t ringbuf_num_filled(ringbuf_t *r);
size_t ringbuf_put_n(ringbuf_t *r, const uint8_t *buf, size_t bufsize);
size_t ringbuf_get_n(ringbuf_t *r, uint8_t *buf, size_t bufsize);
size_t ringbuf_discard_n(ringbuf_t *r, size_t n);

// Note: big-endian. Return -1 if can't read or write two bytes.
int ringbuf_get16(ringbuf_t *r);
Expand Down
19 changes: 12 additions & 7 deletions shared-bindings/usb_host_audio/USBIn.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,16 @@
//|
//| On RP2 boards, whose host port is PIO-USB, the host controller buffers
//| the stream between reads, in a buffer of ``iso_buffer_size`` bytes that
//| `USBIn` allocates and `deinit` frees. On boards whose host port is the
//| chip's own USB controller, such as the Metro ESP32-P4, there is no such
//| buffer and ``iso_buffer_size`` is not used: each packet is received as
//| it arrives, and packets that arrive while background tasks can't run,
//| such as during a long garbage collection, are lost and set `overflow`.
//| `USBIn` allocates and `deinit` frees. On the ESP32-P4 the USB host runs in a task of its own,
//| so packets keep arriving while Python is busy, such as during an SD
//| card write or a long garbage collection. On other such boards packets
//| that arrive while background tasks can't run are lost and set
//| `overflow`.
//|
//| Audio waits for `record` or playback in a buffer of
//| ``32 * buffer_frames`` frames on the ESP32-P4 and ``8 * buffer_frames``
//| elsewhere. When that fills up, the newest audio is dropped and
//| `overflow` is set.
//|
//| Only one isochronous IN endpoint is received at a time, so only one
//| `USBIn` can stream, and a USB camera cannot stream video meanwhile.
Expand Down Expand Up @@ -194,8 +199,8 @@ static void check_for_deinit(usb_host_audio_usbin_obj_t *self) {
//| (``'B'`` when unsigned) or a ``bytearray`` for 8.
//|
//| Audio is received between calls too, so that consecutive recordings
//| join up. When more arrived than is buffered, the recording starts
//| with the audio that arrives after the call.
//| join up. When more arrived than is buffered, the newest of it was
//| dropped: the recording has the buffered audio, then a gap.
//|
//| :return: The number of samples recorded. If this is less than
//| ``destination_length``, the microphone stopped sending audio."""
Expand Down
29 changes: 22 additions & 7 deletions shared-module/usb/core/Device.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ static inline void usb_host_audio_stop_device(uint8_t device_address) {
}
#endif

// The TinyUSB callbacks below run in tuh_task(), which is a task of its own on
// some ports (CIRCUITPY_USB_HOST_TASK). What they set is read by the VM.

// Track what device numbers are mounted. We can't use tuh_ready() because it is
// true before enumeration completes and TinyUSB drivers are started.
static size_t _mounted_devices = 0;
static volatile size_t _mounted_devices = 0;

void tuh_mount_cb(uint8_t dev_addr) {
_mounted_devices |= 1 << dev_addr;
Expand All @@ -60,8 +63,8 @@ void tuh_umount_cb(uint8_t dev_addr) {
usb_host_bulk_stop_device(dev_addr);
}

static xfer_result_t _xfer_result;
static size_t _actual_len;
static volatile xfer_result_t _xfer_result;
static volatile size_t _actual_len;

#if !CIRCUITPY_ALL_MEMORY_DMA_CAPABLE
// Helper to ensure buffer is DMA-capable for transfer operations
Expand Down Expand Up @@ -110,6 +113,8 @@ void common_hal_usb_core_device_deinit(usb_core_device_obj_t *self) {
return;
}
usb_host_bulk_device_deinit(self);
// A usb_host_audio stream ends with its endpoint, without a completion.
usb_host_audio_stop_device(self->device_address);
size_t open_size = sizeof(self->open_endpoints);
for (size_t i = 0; i < open_size; i++) {
if (self->open_endpoints[i] != 0) {
Expand Down Expand Up @@ -139,11 +144,21 @@ uint16_t common_hal_usb_core_device_get_idProduct(usb_core_device_obj_t *self) {
}

static void _transfer_done_cb(tuh_xfer_t *xfer) {
// Store the result so we stop waiting for the transfer.
_xfer_result = xfer->result;
// The passed in xfer is not the original one we passed in, so we need to
// copy any info out that we want (like actual_len.)
_actual_len = xfer->actual_len;
// Store the result so we stop waiting for the transfer. The waiter reads
// _actual_len once it sees the result, so the result has to come last.
__atomic_thread_fence(__ATOMIC_RELEASE);
_xfer_result = xfer->result;
}

// The result of the transfer, once the callback has stored it.
static xfer_result_t _get_xfer_result(void) {
xfer_result_t result = _xfer_result;
// _actual_len is read after this, and must be at least as new.
__atomic_thread_fence(__ATOMIC_ACQUIRE);
return result;
}

static bool _wait_for_callback(void) {
Expand All @@ -158,7 +173,7 @@ static bool _wait_for_callback(void) {
return false;
}
// Handle callback result code from TinyUSB
xfer_result_t result = _xfer_result;
xfer_result_t result = _get_xfer_result();
_xfer_result = XFER_RESULT_INVALID;
switch (result) {
case XFER_RESULT_SUCCESS:
Expand Down Expand Up @@ -222,7 +237,7 @@ static size_t _handle_timed_transfer_callback(tuh_xfer_t *xfer, mp_int_t timeout
return 0;
}
// Handle transfer result code from TinyUSB
xfer_result_t result = _xfer_result;
xfer_result_t result = _get_xfer_result();
_xfer_result = XFER_RESULT_INVALID;
// Free the bounce buffer only on paths that raise: raising unwinds past
// the caller's cleanup, so it must be freed here. Paths that return
Expand Down
Loading
Loading