ESP-IDF Firmware
Firmware architecture and call graph
Loading...
Searching...
No Matches
descriptors_control.c File Reference
#include <string.h>
#include "esp_log.h"
#include "descriptors_control.h"
Include dependency graph for descriptors_control.c:

Go to the source code of this file.

Macros

#define MAX_DESC_BUF_SIZE   32

Functions

uint8_t const * tud_descriptor_device_cb (void)
 Invoked when received GET DEVICE DESCRIPTOR. Application returns pointer to descriptor.
uint8_t const * tud_descriptor_configuration_cb (uint8_t index)
 Invoked when received GET CONFIGURATION DESCRIPTOR. Descriptor contents must exist long enough for transfer to complete.
uint16_t const * tud_descriptor_string_cb (uint8_t index, uint16_t langid)
void tusb_set_descriptor (const tusb_desc_device_t *dev_desc, const char **str_desc, const uint8_t *cfg_desc)
tusb_desc_device_t * tusb_get_active_desc (void)
char ** tusb_get_active_str_desc (void)
void tusb_clear_descriptor (void)

Variables

static const char * TAG = "tusb_desc"
static tusb_desc_device_t s_device_descriptor
static const uint8_t * s_configuration_descriptor
static char * s_str_descriptor [USB_STRING_DESCRIPTOR_ARRAY_SIZE]
static uint16_t _desc_str [32]

Macro Definition Documentation

◆ MAX_DESC_BUF_SIZE

#define MAX_DESC_BUF_SIZE   32

Definition at line 15 of file descriptors_control.c.

Referenced by tud_descriptor_string_cb().

Function Documentation

◆ tud_descriptor_configuration_cb()

uint8_t const * tud_descriptor_configuration_cb ( uint8_t index)

Invoked when received GET CONFIGURATION DESCRIPTOR. Descriptor contents must exist long enough for transfer to complete.

Parameters
index
Returns
uint8_t const* Application return pointer to descriptor

Definition at line 39 of file descriptors_control.c.

40{
41 (void)index; // for multiple configurations
43}
static const uint8_t * s_configuration_descriptor

References s_configuration_descriptor.

◆ tud_descriptor_device_cb()

uint8_t const * tud_descriptor_device_cb ( void )

Invoked when received GET DEVICE DESCRIPTOR. Application returns pointer to descriptor.

Returns
uint8_t const*

Definition at line 27 of file descriptors_control.c.

28{
29 return (uint8_t const *)&s_device_descriptor;
30}
static tusb_desc_device_t s_device_descriptor

References s_device_descriptor.

◆ tud_descriptor_string_cb()

uint16_t const * tud_descriptor_string_cb ( uint8_t index,
uint16_t langid )

Definition at line 49 of file descriptors_control.c.

50{
51 (void) langid;
52
53 uint8_t chr_count;
54
55 if ( index == 0) {
56 memcpy(&_desc_str[1], s_str_descriptor[0], 2);
57 chr_count = 1;
58 } else {
59 // Convert ASCII string into UTF-16
60
61 if ( index >= sizeof(s_str_descriptor) / sizeof(s_str_descriptor[0]) ) {
62 ESP_LOGE(TAG, "String index (%u) is out of bounds, check your string descriptor", index);
63 return NULL;
64 }
65
66 if (s_str_descriptor[index] == NULL) {
67 ESP_LOGE(TAG, "String index (%u) points to NULL, check your string descriptor", index);
68 return NULL;
69 }
70
71 const char *str = s_str_descriptor[index];
72
73 // Cap at max char
74 chr_count = strlen(str);
75 if ( chr_count > MAX_DESC_BUF_SIZE - 1 ) {
76 chr_count = MAX_DESC_BUF_SIZE - 1;
77 }
78
79 for (uint8_t i = 0; i < chr_count; i++) {
80 _desc_str[1 + i] = str[i];
81 }
82 }
83
84 // first byte is length (including header), second byte is string type
85 _desc_str[0] = (TUSB_DESC_STRING << 8 ) | (2 * chr_count + 2);
86
87 return _desc_str;
88}
#define MAX_DESC_BUF_SIZE
static char * s_str_descriptor[USB_STRING_DESCRIPTOR_ARRAY_SIZE]
static uint16_t _desc_str[32]
static const char * TAG
Definition main/main.c:31

References _desc_str, MAX_DESC_BUF_SIZE, s_str_descriptor, and TAG.

◆ tusb_clear_descriptor()

void tusb_clear_descriptor ( void )

Definition at line 146 of file descriptors_control.c.

147{
148 memset(&s_device_descriptor, 0, sizeof(s_device_descriptor));
149 memset(&s_str_descriptor, 0, sizeof(s_str_descriptor));
150}

References s_device_descriptor, and s_str_descriptor.

◆ tusb_get_active_desc()

tusb_desc_device_t * tusb_get_active_desc ( void )

Definition at line 136 of file descriptors_control.c.

137{
138 return &s_device_descriptor;
139}

References s_device_descriptor.

◆ tusb_get_active_str_desc()

char ** tusb_get_active_str_desc ( void )

Definition at line 141 of file descriptors_control.c.

142{
143 return s_str_descriptor;
144}

References s_str_descriptor.

◆ tusb_set_descriptor()

void tusb_set_descriptor ( const tusb_desc_device_t * dev_desc,
const char ** str_desc,
const uint8_t * cfg_desc )

Definition at line 94 of file descriptors_control.c.

95{
96 ESP_LOGI(TAG, "\n"
97 "┌─────────────────────────────────┐\n"
98 "│ USB Device Descriptor Summary │\n"
99 "├───────────────────┬─────────────┤\n"
100 "│bDeviceClass │ %-4u │\n"
101 "├───────────────────┼─────────────┤\n"
102 "│bDeviceSubClass │ %-4u │\n"
103 "├───────────────────┼─────────────┤\n"
104 "│bDeviceProtocol │ %-4u │\n"
105 "├───────────────────┼─────────────┤\n"
106 "│bMaxPacketSize0 │ %-4u │\n"
107 "├───────────────────┼─────────────┤\n"
108 "│idVendor │ %-#10x │\n"
109 "├───────────────────┼─────────────┤\n"
110 "│idProduct │ %-#10x │\n"
111 "├───────────────────┼─────────────┤\n"
112 "│bcdDevice │ %-#10x │\n"
113 "├───────────────────┼─────────────┤\n"
114 "│iManufacturer │ %-#10x │\n"
115 "├───────────────────┼─────────────┤\n"
116 "│iProduct │ %-#10x │\n"
117 "├───────────────────┼─────────────┤\n"
118 "│iSerialNumber │ %-#10x │\n"
119 "├───────────────────┼─────────────┤\n"
120 "│bNumConfigurations │ %-#10x │\n"
121 "└───────────────────┴─────────────┘",
122 dev_desc->bDeviceClass, dev_desc->bDeviceSubClass,
123 dev_desc->bDeviceProtocol, dev_desc->bMaxPacketSize0,
124 dev_desc->idVendor, dev_desc->idProduct, dev_desc->bcdDevice,
125 dev_desc->iManufacturer, dev_desc->iProduct, dev_desc->iSerialNumber,
126 dev_desc->bNumConfigurations);
127 s_device_descriptor = *dev_desc;
129
130 if (str_desc != NULL) {
131 memcpy(s_str_descriptor, str_desc,
133 }
134}
#define USB_STRING_DESCRIPTOR_ARRAY_SIZE

References s_configuration_descriptor, s_device_descriptor, s_str_descriptor, TAG, and USB_STRING_DESCRIPTOR_ARRAY_SIZE.

Referenced by tinyusb_driver_install().

Here is the caller graph for this function:

Variable Documentation

◆ _desc_str

uint16_t _desc_str[32]
static

Definition at line 45 of file descriptors_control.c.

Referenced by tud_descriptor_string_cb().

◆ s_configuration_descriptor

const uint8_t* s_configuration_descriptor
static

Definition at line 13 of file descriptors_control.c.

Referenced by tud_descriptor_configuration_cb(), and tusb_set_descriptor().

◆ s_device_descriptor

tusb_desc_device_t s_device_descriptor
static

◆ s_str_descriptor

◆ TAG

const char* TAG = "tusb_desc"
static

Definition at line 11 of file descriptors_control.c.