rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
Functions | Variables
usbcfg.cpp File Reference

Functions

static char nib2char (uint8_t nibble)
 
void usbPopulateSerialNumber (const uint8_t *serialNumber, size_t bytes)
 
static const USBDescriptor * get_descriptor (USBDriver *usbp, uint8_t dtype, uint8_t dindex, uint16_t lang)
 
static void usb_event (USBDriver *usbp, usbevent_t event)
 
static void sof_handler (USBDriver *usbp)
 
static bool hybridRequestHook (USBDriver *usbp)
 

Variables

SerialUSBDriver SDU1
 
static const uint8_t vcom_device_descriptor_data [18]
 
static const USBDescriptor vcom_device_descriptor
 
static const uint8_t vcom_configuration_descriptor_data [DESCRIPTOR_SIZE]
 
static const USBDescriptor vcom_configuration_descriptor
 
static const uint8_t vcom_string0 []
 
static const uint8_t vcom_string1 []
 
static const uint8_t vcom_string2 []
 
static const uint8_t vcom_string3 []
 
static const USBDescriptor vcom_strings []
 
static USBInEndpointState msdInstate
 IN MSD state.
 
static USBOutEndpointState msdOutstate
 OUT MSD state.
 
static const USBEndpointConfig msdEpConfig
 MSD initialization structure (both IN and OUT).
 
static USBInEndpointState cdcDataInstate
 
static USBOutEndpointState cdcDataOutstate
 
static const USBEndpointConfig cdcDataEpConfig
 
static USBInEndpointState cdcInterruptInstate
 
static const USBEndpointConfig cdcInterruptEpConfig
 
const USBConfig usbcfg
 
const SerialUSBConfig serusbcfg
 

Function Documentation

◆ get_descriptor()

static const USBDescriptor * get_descriptor ( USBDriver *  usbp,
uint8_t  dtype,
uint8_t  dindex,
uint16_t  lang 
)
static

Definition at line 304 of file usbcfg.cpp.

307 {
308
309 (void)usbp;
310 (void)lang;
311 switch (dtype) {
312 case USB_DESCRIPTOR_DEVICE:
314 case USB_DESCRIPTOR_CONFIGURATION:
316 case USB_DESCRIPTOR_STRING:
317 if (dindex < 4)
318 return &vcom_strings[dindex];
319 }
320 return NULL;
321}
static const USBDescriptor vcom_strings[]
Definition usbcfg.cpp:263
static const USBDescriptor vcom_device_descriptor
Definition usbcfg.cpp:75
static const USBDescriptor vcom_configuration_descriptor
Definition usbcfg.cpp:188

◆ hybridRequestHook()

static bool hybridRequestHook ( USBDriver *  usbp)
static

Definition at line 454 of file usbcfg.cpp.

454 {
455#if HAL_USE_USB_MSD
456 // Try the MSD driver first
457 if (msd_request_hook_new(usbp)) {
458 return true;
459 }
460#endif // HAL_USE_USB_MSD
461
462 // if not MSD, it must be serial
463 return sduRequestsHook(usbp);
464}
bool msd_request_hook_new(USBDriver *usbp)
Here is the call graph for this function:

◆ nib2char()

static char nib2char ( uint8_t  nibble)
static

Definition at line 271 of file usbcfg.cpp.

271 {
272 if (nibble > 0x9) {
273 return nibble - 0xA + 'A';
274 } else {
275 return nibble + '0';
276 }
277}

Referenced by usbPopulateSerialNumber().

Here is the caller graph for this function:

◆ sof_handler()

static void sof_handler ( USBDriver *  usbp)
static

Definition at line 444 of file usbcfg.cpp.

444 {
445
446 (void)usbp;
447
448 osalSysLockFromISR();
449 sduSOFHookI(&SDU1);
450 osalSysUnlockFromISR();
451}
SerialUSBDriver SDU1
Definition usbcfg.cpp:26

◆ usb_event()

static void usb_event ( USBDriver *  usbp,
usbevent_t  event 
)
static

Definition at line 388 of file usbcfg.cpp.

388 {
389 switch (event) {
390 case USB_EVENT_ADDRESS:
391 return;
392 case USB_EVENT_CONFIGURED:
393 chSysLockFromISR();
394
395 /* Enables the endpoints specified into the configuration.
396 Note, this callback is invoked from an ISR so I-Class functions
397 must be used.*/
398#if HAL_USE_USB_MSD
399 usbInitEndpointI(usbp, USB_MSD_DATA_EP, &msdEpConfig);
400#endif
401
402 usbInitEndpointI(usbp, USBD1_DATA_REQUEST_EP, &cdcDataEpConfig);
403 usbInitEndpointI(usbp, USBD1_INTERRUPT_REQUEST_EP, &cdcInterruptEpConfig);
404
405 /* Resetting the state of the CDC subsystem.*/
406 sduConfigureHookI(&SDU1);
407
408 #if HAL_USE_USB_MSD
409 // Tell the MMC thread to wake up and mount the card as a USB device
411 #endif
412
413 chSysUnlockFromISR();
414 return;
415 case USB_EVENT_RESET:
416 /* Falls into.*/
417 case USB_EVENT_UNCONFIGURED:
418 /* Falls into.*/
419 case USB_EVENT_SUSPEND:
420 chSysLockFromISR();
421
422 /* Disconnection event on suspend.*/
423 sduSuspendHookI(&SDU1);
424
425 chSysUnlockFromISR();
426 return;
427 case USB_EVENT_WAKEUP:
428 chSysLockFromISR();
429
430 /* Disconnection event on suspend.*/
431 sduWakeupHookI(&SDU1);
432
433 chSysUnlockFromISR();
434 return;
435 case USB_EVENT_STALLED:
436 return;
437 }
438 return;
439}
void onUsbConnectedNotifyMmcI()
Definition mmc_card.cpp:437
static const USBEndpointConfig msdEpConfig
MSD initialization structure (both IN and OUT).
Definition usbcfg.cpp:337
static const USBEndpointConfig cdcDataEpConfig
Definition usbcfg.cpp:356
static const USBEndpointConfig cdcInterruptEpConfig
Definition usbcfg.cpp:372
Here is the call graph for this function:

◆ usbPopulateSerialNumber()

void usbPopulateSerialNumber ( const uint8_t *  serialNumber,
size_t  bytes 
)

Definition at line 279 of file usbcfg.cpp.

279 {
280 if (bytes > 12) {
281 bytes = 12;
282 }
283
284 // Skip the first two bytes (metadata)
285 uint8_t* dst = &vcom_string3[2];
286
287 for (size_t i = 0; i < bytes; i++) {
288 uint8_t byte = serialNumber[i];
289
290 uint8_t lowNibble = byte & 0xF;
291 uint8_t highNibble = byte >> 4;
292
293 // Descriptor strings are UCS16, so write every other byte
294 dst[4 * i] = nib2char(highNibble);
295 dst[4 * i + 2] = nib2char(lowNibble);
296 }
297}
static const uint8_t vcom_string3[]
Definition usbcfg.cpp:238
static char nib2char(uint8_t nibble)
Definition usbcfg.cpp:271

Referenced by usb_serial_start().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ cdcDataEpConfig

const USBEndpointConfig cdcDataEpConfig
static
Initial value:
= {
USB_EP_MODE_TYPE_BULK,
NULL,
sduDataTransmitted,
sduDataReceived,
0x0040,
0x0040,
4,
NULL
}
static USBInEndpointState cdcDataInstate
Definition usbcfg.cpp:352
static USBOutEndpointState cdcDataOutstate
Definition usbcfg.cpp:354

Definition at line 356 of file usbcfg.cpp.

356 {
357 USB_EP_MODE_TYPE_BULK,
358 NULL,
359 sduDataTransmitted,
360 sduDataReceived,
361 0x0040,
362 0x0040,
365 4,
366 NULL
367};

Referenced by usb_event().

◆ cdcDataInstate

USBInEndpointState cdcDataInstate
static

Definition at line 352 of file usbcfg.cpp.

◆ cdcDataOutstate

USBOutEndpointState cdcDataOutstate
static

Definition at line 354 of file usbcfg.cpp.

◆ cdcInterruptEpConfig

const USBEndpointConfig cdcInterruptEpConfig
static
Initial value:
= {
USB_EP_MODE_TYPE_INTR,
NULL,
sduInterruptTransmitted,
NULL,
0x0010,
0x0000,
NULL,
1,
NULL
}
static USBInEndpointState cdcInterruptInstate
Definition usbcfg.cpp:370

Definition at line 372 of file usbcfg.cpp.

372 {
373 USB_EP_MODE_TYPE_INTR,
374 NULL,
375 sduInterruptTransmitted,
376 NULL,
377 0x0010,
378 0x0000,
380 NULL,
381 1,
382 NULL
383};

Referenced by usb_event().

◆ cdcInterruptInstate

USBInEndpointState cdcInterruptInstate
static

Definition at line 370 of file usbcfg.cpp.

◆ msdEpConfig

const USBEndpointConfig msdEpConfig
static
Initial value:
= {
USB_EP_MODE_TYPE_BULK,
NULL,
NULL,
NULL,
USB_MSD_EP_SIZE,
USB_MSD_EP_SIZE,
4,
NULL
}
static USBOutEndpointState msdOutstate
OUT MSD state.
Definition usbcfg.cpp:332
static USBInEndpointState msdInstate
IN MSD state.
Definition usbcfg.cpp:327

MSD initialization structure (both IN and OUT).

Definition at line 337 of file usbcfg.cpp.

337 {
338 USB_EP_MODE_TYPE_BULK,
339 NULL,
340 NULL,
341 NULL,
342 USB_MSD_EP_SIZE,
343 USB_MSD_EP_SIZE,
344 &msdInstate,
346 4,
347 NULL
348 };

Referenced by usb_event().

◆ msdInstate

USBInEndpointState msdInstate
static

IN MSD state.

Definition at line 327 of file usbcfg.cpp.

◆ msdOutstate

USBOutEndpointState msdOutstate
static

OUT MSD state.

Definition at line 332 of file usbcfg.cpp.

◆ SDU1

SerialUSBDriver SDU1

Definition at line 26 of file usbcfg.cpp.

Referenced by sof_handler(), and usb_event().

◆ serusbcfg

const SerialUSBConfig serusbcfg
Initial value:
= {
#if STM32_USB_USE_OTG1
.usbp = &USBD1,
#elif STM32_USB_USE_OTG2
.usbp = &USBD2,
#else
#error Serial over USB needs OTG1 or OTG2 to be enabled
#endif
.bulk_in = USBD1_DATA_REQUEST_EP,
.bulk_out = USBD1_DATA_AVAILABLE_EP,
.int_in = USBD1_INTERRUPT_REQUEST_EP
}

Definition at line 479 of file usbcfg.cpp.

479 {
480#if STM32_USB_USE_OTG1
481 .usbp = &USBD1,
482#elif STM32_USB_USE_OTG2
483 .usbp = &USBD2,
484#else
485 #error Serial over USB needs OTG1 or OTG2 to be enabled
486#endif
487 .bulk_in = USBD1_DATA_REQUEST_EP,
488 .bulk_out = USBD1_DATA_AVAILABLE_EP,
489 .int_in = USBD1_INTERRUPT_REQUEST_EP
490};

Referenced by usb_serial_flush(), and usb_serial_start().

◆ usbcfg

const USBConfig usbcfg
Initial value:
= {
}
static void usb_event(USBDriver *usbp, usbevent_t event)
Definition usbcfg.cpp:388
static bool hybridRequestHook(USBDriver *usbp)
Definition usbcfg.cpp:454
static const USBDescriptor * get_descriptor(USBDriver *usbp, uint8_t dtype, uint8_t dindex, uint16_t lang)
Definition usbcfg.cpp:304
static void sof_handler(USBDriver *usbp)
Definition usbcfg.cpp:444

Definition at line 469 of file usbcfg.cpp.

469 {
470 usb_event,
474};

Referenced by usb_serial_start().

◆ vcom_configuration_descriptor

const USBDescriptor vcom_configuration_descriptor
static
Initial value:
= {
}
static const uint8_t vcom_configuration_descriptor_data[DESCRIPTOR_SIZE]
Definition usbcfg.cpp:81

Definition at line 188 of file usbcfg.cpp.

Referenced by get_descriptor().

◆ vcom_configuration_descriptor_data

const uint8_t vcom_configuration_descriptor_data[DESCRIPTOR_SIZE]
static

Definition at line 81 of file usbcfg.cpp.

81 {
82 /* Configuration Descriptor.*/
83 USB_DESC_CONFIGURATION(DESCRIPTOR_SIZE,/* wTotalLength. */
84 NUM_INTERFACES,/* bNumInterfaces. */
85 0x01, /* bConfigurationValue. */
86 0, /* iConfiguration. */
87 0x80, /* bmAttributes (bus powered). */
88 200), /* bMaxPower (400mA). */
89#if HAL_USE_USB_MSD
90 USB_DESC_INTERFACE (MSD_IF, /* bInterfaceNumber. */
91 0x00, /* bAlternateSetting. */
92 0x02, /* bNumEndpoints. */
93 0x08, /* bInterfaceClass (mass storage) */
94 0x06, /* bInterfaceSubClass (SCSI
95 transparent storage class). */
96 0x50, /* bInterfaceProtocol (Bulk Only). */
97 0), /* iInterface. */
98 /* Mass Storage Data In Endpoint Descriptor.*/
99 USB_DESC_ENDPOINT (USB_MSD_DATA_EP | 0x80,
100 0x02, /* bmAttributes (Bulk). */
101 64, /* wMaxPacketSize. */
102 0x00), /* bInterval. 1ms */
103 /* Mass Storage Data Out Endpoint Descriptor.*/
104 USB_DESC_ENDPOINT (USB_MSD_DATA_EP,
105 0x02, /* bmAttributes (Bulk). */
106 64, /* wMaxPacketSize. */
107 0x00), /* bInterval. 1ms */
108#endif // HAL_USE_USB_MSD
109 // CDC
110 /* IAD Descriptor - describes that EP2+3 belong to CDC */
111 USB_DESC_INTERFACE_ASSOCIATION(CDC_INT_IF, /* bFirstInterface. */
112 0x02, /* bInterfaceCount. */
113 0x02, /* bFunctionClass (CDC). */
114 0x02, /* bFunctionSubClass. (2) */
115 0x01, /* bFunctionProtocol (1) */
116 2), /* iInterface. */
117 /* Interface Descriptor.*/
118 USB_DESC_INTERFACE (CDC_INT_IF, /* bInterfaceNumber. */
119 0x00, /* bAlternateSetting. */
120 0x01, /* bNumEndpoints. */
121 0x02, /* bInterfaceClass (Communications
122 Interface Class, CDC section
123 4.2). */
124 0x02, /* bInterfaceSubClass (Abstract
125 Control Model, CDC section 4.3). */
126 0x01, /* bInterfaceProtocol (AT commands,
127 CDC section 4.4). */
128 0), /* iInterface. */
129 /* Header Functional Descriptor (CDC section 5.2.3).*/
130 USB_DESC_BYTE (5), /* bLength. */
131 USB_DESC_BYTE (0x24), /* bDescriptorType (CS_INTERFACE). */
132 USB_DESC_BYTE (0x00), /* bDescriptorSubtype (Header
133 Functional Descriptor. */
134 USB_DESC_BCD (0x0110), /* bcdCDC. */
135 /* Call Management Functional Descriptor. */
136 USB_DESC_BYTE (5), /* bFunctionLength. */
137 USB_DESC_BYTE (0x24), /* bDescriptorType (CS_INTERFACE). */
138 USB_DESC_BYTE (0x01), /* bDescriptorSubtype (Call Management
139 Functional Descriptor). */
140 USB_DESC_BYTE (0x00), /* bmCapabilities (D0+D1). */
141 USB_DESC_BYTE (CDC_DATA_IF), /* bDataInterface. */
142 /* ACM Functional Descriptor.*/
143 USB_DESC_BYTE (4), /* bFunctionLength. */
144 USB_DESC_BYTE (0x24), /* bDescriptorType (CS_INTERFACE). */
145 USB_DESC_BYTE (0x02), /* bDescriptorSubtype (Abstract
146 Control Management Descriptor). */
147 USB_DESC_BYTE (0x02), /* bmCapabilities. */
148 /* Union Functional Descriptor.*/
149 USB_DESC_BYTE (5), /* bFunctionLength. */
150 USB_DESC_BYTE (0x24), /* bDescriptorType (CS_INTERFACE). */
151 USB_DESC_BYTE (0x06), /* bDescriptorSubtype (Union
152 Functional Descriptor). */
153 USB_DESC_BYTE (CDC_INT_IF), /* bMasterInterface (Communication
154 Class Interface). */
155 USB_DESC_BYTE (CDC_DATA_IF), /* bSlaveInterface0 (Data Class
156 Interface). */
157 /* Endpoint 3 Descriptor.*/
158 USB_DESC_ENDPOINT (USBD1_INTERRUPT_REQUEST_EP|0x80,
159 0x03, /* bmAttributes (Interrupt). */
160 0x0008, /* wMaxPacketSize. */
161 0xFF), /* bInterval. */
162 /* Interface Descriptor.*/
163 USB_DESC_INTERFACE (CDC_DATA_IF, /* bInterfaceNumber. */
164 0x00, /* bAlternateSetting. */
165 0x02, /* bNumEndpoints. */
166 0x0A, /* bInterfaceClass (Data Class
167 Interface, CDC section 4.5). */
168 0x00, /* bInterfaceSubClass (CDC section
169 4.6). */
170 0x00, /* bInterfaceProtocol (CDC section
171 4.7). */
172 0x00), /* iInterface. */
173 /* Endpoint 2 Descriptor.*/
174 USB_DESC_ENDPOINT (USBD1_DATA_AVAILABLE_EP, /* bEndpointAddress.*/
175 0x02, /* bmAttributes (Bulk). */
176 0x0040, /* wMaxPacketSize. */
177 0x00), /* bInterval. */
178 /* Endpoint 2 Descriptor.*/
179 USB_DESC_ENDPOINT (USBD1_DATA_REQUEST_EP|0x80, /* bEndpointAddress.*/
180 0x02, /* bmAttributes (Bulk). */
181 0x0040, /* wMaxPacketSize. */
182 0x00) /* bInterval. */
183};

◆ vcom_device_descriptor

const USBDescriptor vcom_device_descriptor
static
Initial value:
= {
}
static const uint8_t vcom_device_descriptor_data[18]
Definition usbcfg.cpp:57

Definition at line 75 of file usbcfg.cpp.

Referenced by get_descriptor().

◆ vcom_device_descriptor_data

const uint8_t vcom_device_descriptor_data[18]
static
Initial value:
= {
USB_DESC_DEVICE (0x0110,
0xEF,
0x02,
0x01,
0x40,
0x0483,
0x5740,
0x0200,
1,
2,
3,
1)
}

Definition at line 57 of file usbcfg.cpp.

57 {
58 USB_DESC_DEVICE (0x0110, /* bcdUSB (1.1). */
59 0xEF, /* bDeviceClass ( misc ). */
60 0x02, /* bDeviceSubClass. */
61 0x01, /* bDeviceProtocol. */
62 0x40, /* bMaxPacketSize. */
63 0x0483, /* idVendor (ST). */
64 0x5740, /* idProduct. */
65 0x0200, /* bcdDevice. */
66 1, /* iManufacturer. */
67 2, /* iProduct. */
68 3, /* iSerialNumber. */
69 1) /* bNumConfigurations. */
70};

◆ vcom_string0

const uint8_t vcom_string0[]
static
Initial value:
= {
USB_DESC_BYTE(4),
USB_DESC_BYTE(USB_DESCRIPTOR_STRING),
USB_DESC_WORD(0x0409)
}

Definition at line 196 of file usbcfg.cpp.

196 {
197 USB_DESC_BYTE(4), /* bLength. */
198 USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */
199 USB_DESC_WORD(0x0409) /* wLANGID (U.S. English). */
200};

◆ vcom_string1

const uint8_t vcom_string1[]
static
Initial value:
= {
USB_DESC_BYTE(22),
USB_DESC_BYTE(USB_DESCRIPTOR_STRING),
'r', 0, 'u', 0, 's', 0, 'E', 0, 'F', 0, 'I', 0, ' ', 0, 'L', 0,
'L', 0, 'C', 0
}

Definition at line 205 of file usbcfg.cpp.

205 {
206 USB_DESC_BYTE(22), /* bLength. */
207 USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */
208 'r', 0, 'u', 0, 's', 0, 'E', 0, 'F', 0, 'I', 0, ' ', 0, 'L', 0,
209 'L', 0, 'C', 0
210};

◆ vcom_string2

const uint8_t vcom_string2[]
static
Initial value:
= {
USB_DESC_BYTE(USB_DESCRIPTOR_B_LENGTH),
USB_DESC_BYTE(USB_DESCRIPTOR_STRING),
USB_DESCRIPTOR_STRING_CONTENT, 0
}

Definition at line 226 of file usbcfg.cpp.

226 {
227 USB_DESC_BYTE(USB_DESCRIPTOR_B_LENGTH), /* bLength. */
228 USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */
229 USB_DESCRIPTOR_STRING_CONTENT, 0
230};

◆ vcom_string3

static uint8_t vcom_string3
static
Initial value:
= {
USB_DESC_BYTE(50),
USB_DESC_BYTE(USB_DESCRIPTOR_STRING),
BOARD_SERIAL[ 0], 0, BOARD_SERIAL[ 1], 0, BOARD_SERIAL[ 2], 0, BOARD_SERIAL[ 3], 0,
BOARD_SERIAL[ 4], 0, BOARD_SERIAL[ 5], 0, BOARD_SERIAL[ 6], 0, BOARD_SERIAL[ 7], 0,
BOARD_SERIAL[ 8], 0, BOARD_SERIAL[ 9], 0, BOARD_SERIAL[10], 0, BOARD_SERIAL[11], 0,
BOARD_SERIAL[12], 0, BOARD_SERIAL[13], 0, BOARD_SERIAL[14], 0, BOARD_SERIAL[15], 0,
BOARD_SERIAL[16], 0, BOARD_SERIAL[17], 0, BOARD_SERIAL[18], 0, BOARD_SERIAL[19], 0,
BOARD_SERIAL[20], 0, BOARD_SERIAL[21], 0, BOARD_SERIAL[22], 0, BOARD_SERIAL[23], 0,
0
}

Definition at line 238 of file usbcfg.cpp.

238 {
239 USB_DESC_BYTE(50), /* bLength. */
240 USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */
241 BOARD_SERIAL[ 0], 0, BOARD_SERIAL[ 1], 0, BOARD_SERIAL[ 2], 0, BOARD_SERIAL[ 3], 0,
242 BOARD_SERIAL[ 4], 0, BOARD_SERIAL[ 5], 0, BOARD_SERIAL[ 6], 0, BOARD_SERIAL[ 7], 0,
243 BOARD_SERIAL[ 8], 0, BOARD_SERIAL[ 9], 0, BOARD_SERIAL[10], 0, BOARD_SERIAL[11], 0,
244 BOARD_SERIAL[12], 0, BOARD_SERIAL[13], 0, BOARD_SERIAL[14], 0, BOARD_SERIAL[15], 0,
245 BOARD_SERIAL[16], 0, BOARD_SERIAL[17], 0, BOARD_SERIAL[18], 0, BOARD_SERIAL[19], 0,
246 BOARD_SERIAL[20], 0, BOARD_SERIAL[21], 0, BOARD_SERIAL[22], 0, BOARD_SERIAL[23], 0,
247 0
248};

Referenced by usbPopulateSerialNumber().

◆ vcom_strings

const USBDescriptor vcom_strings[]
static
Initial value:
= {
}
static const uint8_t vcom_string0[]
Definition usbcfg.cpp:196
static const uint8_t vcom_string2[]
Definition usbcfg.cpp:226
static const uint8_t vcom_string1[]
Definition usbcfg.cpp:205

Definition at line 263 of file usbcfg.cpp.

263 {
264 {sizeof vcom_string0, vcom_string0},
265 {sizeof vcom_string1, vcom_string1},
266 {sizeof vcom_string2, vcom_string2},
267 {sizeof vcom_string3, vcom_string3}
268};

Referenced by get_descriptor().

Go to the source code of this file.