TinyWifiDongle 0.1.0
Header-only USB WiFi dongle for Arduino
Loading...
Searching...
No Matches
Esp32UsbTaskPin.h
Go to the documentation of this file.
1#pragma once
2#include <Arduino.h>
3#include "tusb.h"
4
5#include "../../core/Logger.h"
6
7namespace twd {
8
23 public:
25 bool begin(int core) {
26 core_ = core;
27 return xTaskCreatePinnedToCore(&Esp32UsbTaskPin::swap, "usbPin", 3072, this, 5, nullptr, core) == pdPASS;
28 }
29
31 bool pinned() const { return pinned_; }
32
33 private:
34 int core_ = 0;
35 volatile bool pinned_ = false;
36
37 static void usbdTask(void*) {
38 for (;;) tud_task();
39 }
40
41 static void swap(void* arg) {
42 Esp32UsbTaskPin* s = (Esp32UsbTaskPin*)arg;
43 for (int tries = 0; tries < 200 && !s->pinned_; tries++) {
44 vTaskDelay(pdMS_TO_TICKS(50));
45 TaskHandle_t h = xTaskGetHandle("usbd");
46 if (!h) continue;
47 // Only take it over while it waits for events, not in the middle of a callback.
48 if (eTaskGetState(h) != eBlocked) continue;
49 vTaskSuspend(h);
50 vTaskDelete(h);
51 if (xTaskCreatePinnedToCore(&Esp32UsbTaskPin::usbdTask, "usbd", 4096, nullptr, configMAX_PRIORITIES - 1, nullptr,
52 s->core_) != pdPASS) {
53 TWD_LOGGER.error("USB: could not restart the USB task on core %d; USB is dead", s->core_);
54 break;
55 }
56 s->pinned_ = true;
57 }
58 if (!s->pinned_) TWD_LOGGER.warning("USB: the USB task was not pinned to core %d; send stalls are more likely", s->core_);
59 vTaskDelete(nullptr);
60 }
61};
62
63} // namespace twd
Printf-style logging through the global TWD_LOGGER object.
static twd::LoggerClass TWD_LOGGER
The logger used by all components; configure it in the sketch with TWD_LOGGER.begin(Serial).
Definition Logger.h:107
Runs the TinyUSB device task on one fixed CPU core.
bool begin(int core)
Pin the USB task to core (0 or 1); call right after USB.begin().
bool pinned() const
True once the pinned task is running.
void warning(const char *fmt,...)
Definition Logger.h:69