Skip to content

Commit 9a68272

Browse files
sandeepmistrycmaglie
authored andcommitted
Enable USB string descriptors in the bootloader
1 parent 3018c95 commit 9a68272

File tree

2 files changed

+35
-54
lines changed

2 files changed

+35
-54
lines changed

bootloaders/zero/sam_ba_usb.c

Lines changed: 34 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,8 @@ const char devDescriptor[] =
4444
USB_PID_HIGH, // idProduct H
4545
0x00, // bcdDevice L, here matching SAM-BA version
4646
0x02, // bcdDevice H
47-
#if 0 // TODO: pending validation
4847
STRING_INDEX_MANUFACTURER, // iManufacturer
4948
STRING_INDEX_PRODUCT, // iProduct
50-
#else
51-
0x00, // iManufacturer
52-
0x00, // iProduct
53-
#endif // 0
5449
0x00, // SerialNumber, should be based on product unique ID
5550
0x01 // bNumConfigs
5651
};
@@ -195,47 +190,40 @@ void sam_ba_usb_CDC_Enumerate(P_USB_CDC pCdc)
195190
/* Return Device Descriptor */
196191
USB_Write(pCdc->pUsb, devDescriptor, SAM_BA_MIN(sizeof(devDescriptor), wLength), USB_EP_CTRL);
197192
}
198-
else
193+
else if (wValue>>8 == STD_GET_DESCRIPTOR_CONFIGURATION)
199194
{
200-
if (wValue>>8 == STD_GET_DESCRIPTOR_CONFIGURATION)
201-
{
202-
/* Return Configuration Descriptor */
203-
USB_Write(pCdc->pUsb, cfgDescriptor, SAM_BA_MIN(sizeof(cfgDescriptor), wLength), USB_EP_CTRL);
204-
}
205-
else
195+
/* Return Configuration Descriptor */
196+
USB_Write(pCdc->pUsb, cfgDescriptor, SAM_BA_MIN(sizeof(cfgDescriptor), wLength), USB_EP_CTRL);
197+
}
198+
else if (wValue>>8 == STD_GET_DESCRIPTOR_STRING)
199+
{
200+
switch ( wValue & 0xff )
206201
{
207-
#if 0 // TODO: pending validation
208-
if (wValue>>8 == STD_GET_DESCRIPTOR_STRING)
209-
{
210-
switch ( wValue & 0xff )
211-
{
212-
case STRING_INDEX_LANGUAGES:
213-
uint16_t STRING_LANGUAGE[2] = { (STD_GET_DESCRIPTOR_STRING<<8) | 4, 0x0409 };
214-
215-
USB_Write(pCdc->pUsb, (const char*)STRING_LANGUAGE, SAM_BA_MIN(sizeof(STRING_LANGUAGE), wLength), USB_EP_CTRL);
216-
break;
217-
218-
case STRING_INDEX_MANUFACTURER:
219-
USB_SendString(pCdc->pUsb, STRING_MANUFACTURER, strlen(STRING_MANUFACTURER), wLength );
220-
break;
221-
222-
case STRING_INDEX_PRODUCT:
223-
USB_SendString(pCdc->pUsb, STRING_PRODUCT, strlen(STRING_PRODUCT), wLength );
224-
break;
225-
default:
226-
/* Stall the request */
227-
USB_SendStall(pUsb, true);
228-
break;
229-
}
202+
case STRING_INDEX_LANGUAGES: {
203+
uint16_t STRING_LANGUAGE[2] = { (STD_GET_DESCRIPTOR_STRING<<8) | 4, 0x0409 };
204+
205+
USB_Write(pCdc->pUsb, (const char*)STRING_LANGUAGE, SAM_BA_MIN(sizeof(STRING_LANGUAGE), wLength), USB_EP_CTRL);
230206
}
231-
else
232-
#endif // 0
233-
{
207+
break;
208+
209+
case STRING_INDEX_MANUFACTURER:
210+
USB_SendString(pCdc->pUsb, STRING_MANUFACTURER, wLength );
211+
break;
212+
213+
case STRING_INDEX_PRODUCT:
214+
USB_SendString(pCdc->pUsb, STRING_PRODUCT, wLength );
215+
break;
216+
default:
234217
/* Stall the request */
235218
USB_SendStall(pUsb, true);
236-
}
219+
break;
237220
}
238221
}
222+
else
223+
{
224+
/* Stall the request */
225+
USB_SendStall(pUsb, true);
226+
}
239227
break;
240228

241229
case STD_SET_ADDRESS:
@@ -430,27 +418,24 @@ P_USB_CDC usb_init(void)
430418
return &sam_ba_cdc;
431419
}
432420

433-
#if 0 // TODO: pending validation
434421
/*----------------------------------------------------------------------------
435422
* \brief Send a USB descriptor string.
436423
*
437424
* The input string is plain ASCII but is sent out as UTF-16 with the correct 2-byte prefix.
438425
*/
439-
uint32_t USB_SendString(Usb *pUsb, const char* ascii_string, uint8_t length, uint8_t maxLength)
426+
uint32_t USB_SendString(Usb *pUsb, const char* ascii_string, uint8_t maxLength)
440427
{
441428
uint8_t string_descriptor[255]; // Max USB-allowed string length
442429
uint16_t* unicode_string=(uint16_t*)(string_descriptor+2); // point on 3 bytes of descriptor
430+
int resulting_length;
443431

444-
int resulting_length = 1;
432+
string_descriptor[0] = (strlen(ascii_string)<<1) + 2;
433+
string_descriptor[1] = STD_GET_DESCRIPTOR_STRING;
445434

446-
for ( ; *ascii_string && (length>=0) && (resulting_length<(maxLength>>1)) ; ascii_string++, length--, resulting_length++ )
435+
for ( resulting_length = 1 ; *ascii_string && (resulting_length<maxLength>>1) ; resulting_length++ )
447436
{
448-
*unicode_string++ = (uint16_t)(*ascii_string);
437+
*unicode_string++ = (uint16_t)(*ascii_string++);
449438
}
450439

451-
string_descriptor[0] = (resulting_length<<1);
452-
string_descriptor[1] = STD_GET_DESCRIPTOR_STRING;
453-
454-
return USB_Write(pUsb, (const char*)unicode_string, resulting_length, USB_EP_CTRL);
440+
return USB_Write(pUsb, (const char*)string_descriptor, resulting_length<<1, USB_EP_CTRL);
455441
}
456-
#endif // 0

bootloaders/zero/sam_ba_usb.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,9 @@
6666
#define FEATURE_DEVICE_REMOTE_WAKEUP (1u)
6767
#define FEATURE_TEST_MODE (2u)
6868

69-
#if 0 // TODO: pending validation
7069
#define STRING_INDEX_LANGUAGES (0x00u)
7170
#define STRING_INDEX_MANUFACTURER (0x01u)
7271
#define STRING_INDEX_PRODUCT (0x02u)
73-
#endif // 0
7472

7573
#define SAM_BA_MIN(a, b) (((a) < (b)) ? (a) : (b))
7674

@@ -96,9 +94,7 @@ P_USB_CDC usb_init(void);
9694

9795
void sam_ba_usb_CDC_Enumerate(P_USB_CDC pCdc);
9896

99-
#if 0 // TODO: pending validation
100-
uint32_t USB_SendString(Usb *pUsb, const char* ascii_string, uint8_t length, uint8_t maxLength);
101-
#endif // 0
97+
uint32_t USB_SendString(Usb *pUsb, const char* ascii_string, uint8_t maxLength);
10298

10399
extern USB_CDC sam_ba_cdc;
104100

0 commit comments

Comments
 (0)