TinyUVC 0.1.0
Header-only USB Video Class (UVC) device: turn an MCU into a USB webcam
Loading...
Searching...
No Matches
UVCPlatformESP32.h
Go to the documentation of this file.
1#pragma once
14#include <Arduino.h>
15#include <USB.h>
16#include <esp_heap_caps.h>
17
18#include "../UVCDevice.h"
19#include "esp32-hal-tinyusb.h"
20
21namespace tinyuvc {
22namespace platform {
23
24inline uint16_t maxPacketSize() {
25#if defined(TUD_OPT_HIGH_SPEED) && TUD_OPT_HIGH_SPEED
26 return 512;
27#else
28 return 64;
29#endif
30}
31
33inline uint16_t esp32LoadDescriptor(uint8_t *dst, uint8_t *itf) {
35 if (dev == nullptr) return 0;
36 const uint8_t str = tinyusb_add_string_descriptor(dev->name());
37 const uint8_t ep = tinyusb_get_free_in_endpoint();
38 if (ep == 0) {
39 TINYUVC_LOGE("no free IN endpoint");
40 return 0;
41 }
42 const uint16_t len = dev->descriptorLength();
43 if (dev->buildDescriptor(dst, len, *itf, (uint8_t)(0x80 | ep), maxPacketSize(),
44 str) != len)
45 return 0;
46 *itf += 2;
47 return len;
48}
49
50inline bool begin(UVCDevice &dev) {
51 // the interface association needs the "miscellaneous / IAD" device class
52 USB.usbClass(TUSB_CLASS_MISC);
53 USB.usbSubClass(MISC_SUBCLASS_COMMON);
54 USB.usbProtocol(MISC_PROTOCOL_IAD);
55 if (tinyusb_enable_interface(USB_INTERFACE_CUSTOM, dev.descriptorLength(),
56 esp32LoadDescriptor) != ESP_OK) {
58 "could not add the camera: USB was already started. Select Tools > "
59 "USB CDC On Boot: Disabled and call begin() before USB.begin()");
60 return false;
61 }
62 return USB.begin();
63}
64
65inline void end(UVCDevice &) {} // the core can not remove interfaces
66
67inline void idle() { delay(1); }
68inline uint32_t millis() { return ::millis(); }
69inline uint32_t micros() { return ::micros(); }
70
71inline uint8_t *allocBuffer(size_t size) {
72 size = (size + 63) & ~(size_t)63;
73 return (uint8_t *)heap_caps_aligned_alloc(64, size,
74 MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
75}
76inline void freeBuffer(uint8_t *buf) { heap_caps_free(buf); }
77
78inline AppLock::AppLock() {}
79inline AppLock::~AppLock() {}
80
81} // namespace platform
82} // namespace tinyuvc
#define TINYUVC_LOGE(...)
Definition UVCLogger.h:75
USB Video Class (UVC 1.1) device - a USB webcam.
Definition UVCDevice.h:95
static UVCDevice *& instance()
The active device.
Definition UVCDevice.h:346
const char * name() const
Definition UVCDevice.h:176
uint16_t descriptorLength() const
Length of the function descriptors.
Definition UVCDevice.h:352
uint16_t buildDescriptor(uint8_t *buf, uint16_t bufLen, uint8_t controlItf, uint8_t endpointIn, uint16_t maxPacketSize, uint8_t stringIndex)
Definition UVCDevice.h:361
void freeBuffer(uint8_t *buf)
Definition UVCPlatformDesktop.h:56
bool begin(UVCDevice &)
Registers the UVC function with the USB stack (and starts it if needed)
Definition UVCPlatformDesktop.h:29
uint8_t * allocBuffer(size_t size)
Allocates the transfer buffer (DMA capable, aligned)
Definition UVCPlatformDesktop.h:53
void end(UVCDevice &)
Unregisters the UVC function, where the USB stack supports that.
Definition UVCPlatformDesktop.h:30
uint16_t maxPacketSize()
Max packet size of the bulk endpoint (64 full speed, 512 high speed)
Definition UVCPlatformDesktop.h:31
uint32_t millis()
Definition UVCPlatformDesktop.h:41
void idle()
Called while waiting: lets the USB stack make progress.
Definition UVCPlatformDesktop.h:33
uint32_t micros()
Definition UVCPlatformDesktop.h:47
uint16_t esp32LoadDescriptor(uint8_t *dst, uint8_t *itf)
Descriptor callback of the core (called from USB.begin())
Definition UVCPlatformESP32.h:33
TinyUVC: USB Video Class (UVC) device for Arduino and the desktop.
~AppLock()
Definition UVCPlatformDesktop.h:59
AppLock()
Definition UVCPlatformDesktop.h:58