TinyUVC 0.1.0
Header-only USB Video Class (UVC) device: turn an MCU into a USB webcam
Loading...
Searching...
No Matches
UVCTinyUSB.h
Go to the documentation of this file.
1#pragma once
16#include "UVCDevice.h"
17
18#if defined(TINYUVC_DESKTOP)
19#include "desktop/TinyUSBSim.h"
20#else
21#include "tusb.h"
22#include "device/usbd_pvt.h"
23#endif
24
25namespace tinyuvc {
27namespace tusb_glue {
28
29inline uint8_t &rhport() {
30 static uint8_t port = 0;
31 return port;
32}
33
34// usbd_edpt_xfer() got an additional is_isr parameter in TinyUSB 0.18:
35// dispatch on the signature of the function that is actually declared.
36inline bool callXfer(bool (*f)(uint8_t, uint8_t, uint8_t *, uint16_t),
37 uint8_t port, uint8_t ep, uint8_t *buf, uint16_t len) {
38 return f(port, ep, buf, len);
39}
40inline bool callXfer(bool (*f)(uint8_t, uint8_t, uint8_t *, uint16_t, bool),
41 uint8_t port, uint8_t ep, uint8_t *buf, uint16_t len) {
42 return f(port, ep, buf, len, false);
43}
44
46inline bool xfer(uint8_t ep, uint8_t *buf, uint16_t len) {
47 const uint8_t port = rhport();
48 if (!usbd_edpt_claim(port, ep)) return false;
49 if (!callXfer(&usbd_edpt_xfer, port, ep, buf, len)) {
50 usbd_edpt_release(port, ep);
51 return false;
52 }
53 return true;
54}
55
56inline void drvInit(void) {}
57
58inline bool drvDeinit(void) { return true; }
59
60inline void drvReset(uint8_t port) {
61 (void)port;
63 if (dev) dev->usbReset();
64}
65
68inline uint16_t drvOpen(uint8_t port, tusb_desc_interface_t const *itf,
69 uint16_t maxLen) {
71 if (dev == nullptr) return 0;
72 if (itf->bInterfaceClass != uvc_spec::CC_VIDEO ||
73 itf->bInterfaceSubClass != uvc_spec::SC_VIDEOCONTROL ||
74 itf->bInterfaceNumber != dev->controlInterface())
75 return 0;
76 const uint8_t *p = (const uint8_t *)itf;
77 const uint8_t *end = p + maxLen;
78 uint16_t len = 0;
79 uint16_t mps = 0;
80 bool epOpen = false;
81 while (p + 2 <= end) {
82 const uint8_t bLength = p[0], type = p[1];
83 if (bLength < 2 || p + bLength > end) break;
84 if (len > 0) {
85 if (type == uvc_spec::DESC_IAD) break;
86 if (type == uvc_spec::DESC_INTERFACE && p[2] != dev->controlInterface() &&
87 p[2] != dev->streamingInterface())
88 break;
89 }
90 if (type == uvc_spec::DESC_ENDPOINT && p[2] == dev->endpoint()) {
91 if (!usbd_edpt_open(port, (tusb_desc_endpoint_t const *)p)) {
92 TINYUVC_LOGE("could not open endpoint 0x%02x", p[2]);
93 return 0;
94 }
95 mps = (uint16_t)(le::get16(p + 4) & 0x7FF);
96 epOpen = true;
97 }
98 p += bLength;
99 len = (uint16_t)(len + bLength);
100 }
101 if (!epOpen) {
102 TINYUVC_LOGE("no streaming endpoint found in descriptor");
103 return 0;
104 }
105 rhport() = port;
106 dev->usbOpened(&xfer, mps);
107 return len;
108}
109
110inline bool drvControl(uint8_t port, uint8_t stage,
111 tusb_control_request_t const *request) {
113 if (dev == nullptr) return false;
114 UVCRequest r;
115 r.bmRequestType = *(const uint8_t *)request;
116 r.bRequest = request->bRequest;
117 r.wValue = request->wValue;
118 r.wIndex = request->wIndex;
119 r.wLength = request->wLength;
120
121 if (stage == CONTROL_STAGE_SETUP) {
122 uint8_t *data = nullptr;
123 uint16_t len = 0;
124 switch (dev->handleControlSetup(r, data, len)) {
127 return tud_control_xfer(port, request, data, len);
129 return tud_control_status(port, request);
131 return true;
133 default:
134 return false;
135 }
136 }
137 if (stage == CONTROL_STAGE_DATA && !r.isIn() && r.wLength > 0) {
138 return dev->handleControlData(r, r.wLength);
139 }
140 return true;
141}
142
143inline bool drvXfer(uint8_t port, uint8_t ep, xfer_result_t result,
144 uint32_t xferred) {
145 (void)port;
146 (void)xferred;
148 if (dev == nullptr || ep != dev->endpoint()) return false;
150 return true;
151}
152
153// Fields that do not exist in every TinyUSB version are set via SFINAE
154template <typename T>
155auto setName(T &d, int) -> decltype(d.name = "", void()) {
156 d.name = "TinyUVC";
157}
158template <typename T>
159void setName(T &, long) {}
160template <typename T>
161auto setDeinit(T &d, int) -> decltype(d.deinit = &drvDeinit, void()) {
162 d.deinit = &drvDeinit;
163}
164template <typename T>
165void setDeinit(T &, long) {}
166
169 memset(&d, 0, sizeof(d));
170 setName(d, 0);
171 setDeinit(d, 0);
172 d.init = &drvInit;
173 d.reset = &drvReset;
174 d.open = &drvOpen;
176 d.xfer_cb = &drvXfer;
177 return d;
178}
179
182 static usbd_class_driver_t drv = makeDriver();
183 return &drv;
184}
185
186} // namespace tusb_glue
187} // namespace tinyuvc
188
189#if !defined(TINYUVC_NO_APP_DRIVER)
190#if defined(ENABLE_PICOTOOL_USB)
191#error \
192 "TinyUVC: the picotool upload method defines its own TinyUSB application " \
193 "driver. Select Tools > Upload Method > 'Default (UF2)', or define " \
194 "TINYUVC_NO_APP_DRIVER and return both drivers yourself."
195#endif
198 uint8_t *driver_count) {
199 *driver_count = 1;
201}
202#endif
Desktop stand-in for the parts of the TinyUSB device API that UVCTinyUSB.h uses. The calls are record...
tusb_control_request_t
Definition TinyUSBSim.h:65
@ CONTROL_STAGE_DATA
Definition TinyUSBSim.h:78
@ CONTROL_STAGE_SETUP
Definition TinyUSBSim.h:77
tusb_desc_interface_t
Definition TinyUSBSim.h:48
bool tud_control_xfer(uint8_t, tusb_control_request_t const *, void *buf, uint16_t len)
Definition TinyUSBSim.h:184
bool usbd_edpt_open(uint8_t, tusb_desc_endpoint_t const *ep)
Definition TinyUSBSim.h:147
bool usbd_edpt_release(uint8_t, uint8_t ep)
Definition TinyUSBSim.h:162
bool usbd_edpt_xfer(uint8_t, uint8_t ep, uint8_t *buf, uint16_t len, bool is_isr)
Definition TinyUSBSim.h:169
xfer_result_t
Definition TinyUSBSim.h:67
@ XFER_RESULT_SUCCESS
Definition TinyUSBSim.h:68
bool tud_control_status(uint8_t, tusb_control_request_t const *)
Definition TinyUSBSim.h:193
bool usbd_edpt_claim(uint8_t, uint8_t ep)
Definition TinyUSBSim.h:155
tusb_desc_endpoint_t
Definition TinyUSBSim.h:57
The USB Video Class device: descriptors, probe/commit negotiation and streaming of frames....
#define TINYUVC_LOGE(...)
Definition UVCLogger.h:75
usbd_class_driver_t const * usbd_app_driver_get_cb(uint8_t *driver_count)
TinyUSB asks for application class drivers once, in tud_init()
Definition UVCTinyUSB.h:197
USB Video Class (UVC 1.1) device - a USB webcam.
Definition UVCDevice.h:95
bool handleControlData(const UVCRequest &r, uint16_t len)
Definition UVCDevice.h:471
static UVCDevice *& instance()
The active device.
Definition UVCDevice.h:346
void usbOpened(XferFn xfer, uint16_t maxPacketSize)
The host configured the device: the bulk endpoint is open.
Definition UVCDevice.h:384
void usbTransferComplete(bool ok)
A transfer on our endpoint completed.
Definition UVCDevice.h:494
uint8_t controlInterface() const
Definition UVCDevice.h:376
uint8_t endpoint() const
Definition UVCDevice.h:378
UVCControlResult handleControlSetup(const UVCRequest &r, uint8_t *&data, uint16_t &len)
Handles the setup stage of a request to our interfaces/endpoint.
Definition UVCDevice.h:404
void usbReset()
Bus reset / unconfigured.
Definition UVCDevice.h:395
uint8_t streamingInterface() const
Definition UVCDevice.h:377
uint16_t get16(const uint8_t *p)
Definition UVCDefs.h:235
void drvInit(void)
Definition UVCTinyUSB.h:56
bool xfer(uint8_t ep, uint8_t *buf, uint16_t len)
Starts an IN transfer on the bulk endpoint.
Definition UVCTinyUSB.h:46
bool drvControl(uint8_t port, uint8_t stage, tusb_control_request_t const *request)
Definition UVCTinyUSB.h:110
uint8_t & rhport()
Definition UVCTinyUSB.h:29
auto setName(T &d, int) -> decltype(d.name="", void())
Definition UVCTinyUSB.h:155
bool callXfer(bool(*f)(uint8_t, uint8_t, uint8_t *, uint16_t), uint8_t port, uint8_t ep, uint8_t *buf, uint16_t len)
Definition UVCTinyUSB.h:36
bool drvDeinit(void)
Definition UVCTinyUSB.h:58
bool drvXfer(uint8_t port, uint8_t ep, xfer_result_t result, uint32_t xferred)
Definition UVCTinyUSB.h:143
usbd_class_driver_t makeDriver()
Definition UVCTinyUSB.h:167
auto setDeinit(T &d, int) -> decltype(d.deinit=&drvDeinit, void())
Definition UVCTinyUSB.h:161
usbd_class_driver_t const * driver()
The TinyUVC class driver.
Definition UVCTinyUSB.h:181
uint16_t drvOpen(uint8_t port, tusb_desc_interface_t const *itf, uint16_t maxLen)
Definition UVCTinyUSB.h:68
void drvReset(uint8_t port)
Definition UVCTinyUSB.h:60
constexpr uint8_t SC_VIDEOCONTROL
Definition UVCDefs.h:19
constexpr uint8_t DESC_INTERFACE
Definition UVCDefs.h:23
constexpr uint8_t CC_VIDEO
Definition UVCDefs.h:18
constexpr uint8_t DESC_IAD
Definition UVCDefs.h:25
constexpr uint8_t DESC_ENDPOINT
Definition UVCDefs.h:24
TinyUVC: USB Video Class (UVC) device for Arduino and the desktop.
@ DataIn
send data/len to the host
@ DataOut
receive wLength bytes into data, then call handleControlData()
@ Handled
nothing to do (the USB stack acknowledges itself)
@ Status
no data stage: just acknowledge
Setup request as seen by UVCDevice (independent of the USB stack)
Definition UVCDevice.h:49
uint8_t bRequest
Definition UVCDevice.h:51
bool isIn() const
Definition UVCDevice.h:60
uint16_t wLength
Definition UVCDevice.h:54
uint16_t wValue
Definition UVCDevice.h:52
uint16_t wIndex
Definition UVCDevice.h:53
uint8_t bmRequestType
Definition UVCDevice.h:50
Desktop stand-in for TinyUSB's class driver table entry.
Definition TinyUSBSim.h:83
void(* reset)(uint8_t rhport)
Definition TinyUSBSim.h:87
void(* init)(void)
Definition TinyUSBSim.h:85
bool(* xfer_cb)(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes)
Definition TinyUSBSim.h:92
bool(* control_xfer_cb)(uint8_t rhport, uint8_t stage, tusb_control_request_t const *request)
Definition TinyUSBSim.h:90
uint16_t(* open)(uint8_t rhport, tusb_desc_interface_t const *desc_intf, uint16_t max_len)
Definition TinyUSBSim.h:88