USB reports real STM32 Serial Number (#2254)

* mpu util data

* implementation
This commit is contained in:
Matthew Kennedy 2021-01-30 11:51:11 -08:00 committed by GitHub
parent a9067fdd75
commit 198bdec496
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 49 additions and 11 deletions

View File

@ -10,6 +10,8 @@
#include "device_mpu_util.h"
#define MCU_SERIAL_NUMBER_BYTES 12
// 12mhz was chosen because it's the GCD of (168, 180, 216), the three speeds of STM32 currently supported
// https://www.wolframalpha.com/input/?i=common+factors+of+168+180+216
#define US_TO_NT_MULTIPLIER (12)

View File

@ -64,7 +64,7 @@ static const uint8_t vcom_configuration_descriptor_data[67] = {
0x01, /* bConfigurationValue. */
0, /* iConfiguration. */
0xC0, /* bmAttributes (self powered). */
50), /* bMaxPower (100mA). */
100), /* bMaxPower (200mA). */
/* Interface Descriptor.*/
USB_DESC_INTERFACE (0x00, /* bInterfaceNumber. */
0x00, /* bAlternateSetting. */
@ -154,11 +154,10 @@ static const uint8_t vcom_string0[] = {
* Vendor string.
*/
static const uint8_t vcom_string1[] = {
USB_DESC_BYTE(38), /* bLength. */
USB_DESC_BYTE(24), /* bLength. */
USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */
'S', 0, 'T', 0, 'M', 0, 'i', 0, 'c', 0, 'r', 0, 'o', 0, 'e', 0,
'l', 0, 'e', 0, 'c', 0, 't', 0, 'r', 0, 'o', 0, 'n', 0, 'i', 0,
'c', 0, 's', 0
'r', 0, 'u', 0, 's', 0, 'E', 0, 'F', 0, 'I', 0, ' ', 0, 'L', 0,
'L', 0, 'C', 0
};
/*
@ -167,7 +166,7 @@ static const uint8_t vcom_string1[] = {
static const uint8_t vcom_string2[] = {
USB_DESC_BYTE(58), /* bLength. */
USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */
'R', 0, 'u', 0, 's', 0, 'E', 0, 'F', 0, 'I', 0, ' ', 0, 'E', 0,
'r', 0, 'u', 0, 's', 0, 'E', 0, 'F', 0, 'I', 0, ' ', 0, 'E', 0,
'n', 0, 'g', 0, 'i', 0, 'n', 0, 'e', 0, ' ', 0, 'M', 0, 'a', 0,
'n', 0, 'a', 0, 'g', 0, 'e', 0, 'm', 0, 'e', 0, 'n', 0, 't', 0,
' ', 0, 'E', 0, 'C', 0, 'U', 0
@ -176,12 +175,12 @@ static const uint8_t vcom_string2[] = {
/*
* Serial Number string.
*/
static const uint8_t vcom_string3[] = {
USB_DESC_BYTE(8), /* bLength. */
static uint8_t vcom_string3[] = {
USB_DESC_BYTE(52), /* bLength. */
USB_DESC_BYTE(USB_DESCRIPTOR_STRING), /* bDescriptorType. */
'0' + CH_KERNEL_MAJOR, 0,
'0' + CH_KERNEL_MINOR, 0,
'0' + CH_KERNEL_PATCH, 0
'0', 0, '1', 0, '2', 0, '3', 0, '4', 0, '5', 0, '6', 0, '7', 0,
'8', 0, '9', 0, 'A', 0, 'B', 0, 'C', 0, 'D', 0, 'E', 0, 'F', 0,
'0', 0, '1', 0, '2', 0, '3', 0, '4', 0, '5', 0, '6', 0, '7', 0,
};
/*
@ -194,6 +193,34 @@ static const USBDescriptor vcom_strings[] = {
{sizeof vcom_string3, vcom_string3}
};
static char nib2char(uint8_t nibble) {
if (nibble > 0x9) {
return nibble - 0xA + 'A';
} else {
return nibble + '0';
}
}
void usbPopulateSerialNumber(const uint8_t* serialNumber, size_t bytes) {
if (bytes > 12) {
bytes = 12;
}
// Skip the first two bytes (metadata)
uint8_t* dst = &vcom_string3[2];
for (size_t i = 0; i < bytes; i++) {
uint8_t byte = serialNumber[i];
uint8_t lowNibble = byte & 0xF;
uint8_t highNibble = byte >> 4;
// Descriptor strings are UCS16, so write every other byte
dst[4 * i] = nib2char(highNibble);
dst[4 * i + 2] = nib2char(lowNibble);
}
}
/*
* Handles the GET_DESCRIPTOR callback. All required descriptors must be
* handled here.

View File

@ -21,6 +21,8 @@ extern const USBConfig usbcfg;
extern SerialUSBConfig serusbcfg;
extern SerialUSBDriver SDU1;
void usbPopulateSerialNumber(const uint8_t* serialNumber, size_t bytes);
#endif /* USBCFG_H */
/** @} */

View File

@ -13,6 +13,7 @@
#include "usbconsole.h"
#include "usbcfg.h"
#include "mpu_util.h"
static bool isUsbSerialInitialized = false;
@ -20,6 +21,8 @@ static bool isUsbSerialInitialized = false;
* start USB serial using hard-coded communications pins (see comments inside the code)
*/
void usb_serial_start(void) {
usbPopulateSerialNumber(MCU_SERIAL_NUMBER_LOCATION, MCU_SERIAL_NUMBER_BYTES);
/*
* Initializes a serial-over-USB CDC driver.
*/

View File

@ -9,6 +9,8 @@
#include "stm32f4xx_hal_flash_ex.h"
#define MCU_SERIAL_NUMBER_LOCATION (uint8_t*)(0x1FFF7A10)
#define SPI_CR1_8BIT_MODE 0
#define SPI_CR2_8BIT_MODE 0

View File

@ -9,6 +9,8 @@
#include "stm32f7xx_hal_flash_ex.h"
#define MCU_SERIAL_NUMBER_LOCATION (uint8_t*)(0x1FF0F420)
#define SPI_CR1_8BIT_MODE 0
#define SPI_CR2_8BIT_MODE (SPI_CR2_DS_2 | SPI_CR2_DS_1 | SPI_CR2_DS_0)