Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the device ID as the USB serial number for Core #1367

Merged
merged 2 commits into from
Feb 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 23 additions & 40 deletions bootloader/src/core/dfu_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
#include "usb_pwr.h"
#include "dfu_mal.h"
#include "core_hal.h"
#include "deviceid_hal.h"
#include "bytes2hexbuf.h"
#include <string.h>

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
Expand All @@ -42,7 +45,6 @@ uint8_t DeviceStatus[6];

/* Extern variables ----------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void IntToUnicode(uint32_t value, uint8_t* pbuf, uint8_t len);

/* Private functions ---------------------------------------------------------*/

Expand Down Expand Up @@ -87,56 +89,37 @@ void HAL_DFU_USB_Init(void)
USB_Init();
}

/*******************************************************************************
* Function Name : Get_SerialNum.
* Description : Create the serial number string descriptor.
* Input : None.
* Return : None.
*******************************************************************************/
void Get_SerialNum(void)
const int device_id_len = 12;
/* Read the STM32 Device electronic signature */
unsigned HAL_device_ID(uint8_t* dest, unsigned destLen)
{
uint32_t Device_Serial0, Device_Serial1, Device_Serial2;

Device_Serial0 = *(uint32_t*)ID1;
Device_Serial1 = *(uint32_t*)ID2;
Device_Serial2 = *(uint32_t*)ID3;

Device_Serial0 += Device_Serial2;

if (Device_Serial0 != 0)
{
IntToUnicode(Device_Serial0, &DFU_StringSerial[2], 8);
IntToUnicode(Device_Serial1, &DFU_StringSerial[18], 4);
if (dest!=NULL && destLen!=0) {
memcpy(dest, (char*)ID1, destLen<device_id_len ? destLen : device_id_len);
}
return device_id_len;
}

/*******************************************************************************
* Function Name : HexToChar.
* Description : Convert Hex 32Bits value into char.
* Function Name : Get_SerialNum.
* Description : Create the serial number string descriptor.
* Input : None.
* Return : None.
*******************************************************************************/
static void IntToUnicode(uint32_t value, uint8_t* pbuf, uint8_t len)
void Get_SerialNum(void)
{
uint8_t idx = 0;

for (idx = 0; idx < len; idx++)
{
if (((value >> 28)) < 0xA)
{
pbuf[2 * idx] = (value >> 28) + '0';
}
else
{
pbuf[2 * idx] = (value >> 28) + 'A' - 10;
}

value = value << 4;

pbuf[2 * idx + 1] = 0;
}
uint8_t deviceId[16];
char deviceIdHex[sizeof(deviceId) * 2 + 1] = {0};
unsigned deviceIdLen = 0;
deviceIdLen = HAL_device_ID(deviceId, sizeof(deviceId));
bytes2hexbuf(deviceId, deviceIdLen, deviceIdHex);

for (unsigned i = 2, pos = 0; i < DFU_SIZ_STRING_SERIAL && pos < 2 * deviceIdLen; i += 2, pos++) {
DFU_StringSerial[i] = deviceIdHex[pos];
DFU_StringSerial[i + 1] = '\0';
}
}


void platform_startup()
{
}
2 changes: 1 addition & 1 deletion bootloader/src/core/usb_desc.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#define DFU_SIZ_STRING_LANGID 4
#define DFU_SIZ_STRING_VENDOR 38
#define DFU_SIZ_STRING_PRODUCT 20
#define DFU_SIZ_STRING_SERIAL 26
#define DFU_SIZ_STRING_SERIAL 52
#define DFU_SIZ_STRING_INTERFACE0 98 /* Flash Bank 0 */
#define DFU_SIZ_STRING_INTERFACE1 98 /* SPI Flash */

Expand Down
53 changes: 10 additions & 43 deletions hal/src/core/usb_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include "usb_prop.h"
#include "delay_hal.h"
#include "usb_settings.h"
#include "deviceid_hal.h"
#include "bytes2hexbuf.h"

/* Private typedef -----------------------------------------------------------*/

Expand Down Expand Up @@ -63,9 +65,6 @@ __IO uint8_t PrevXferComplete;
/* Extern variables ----------------------------------------------------------*/
extern volatile LINE_CODING linecoding;

/* Private function prototypes -----------------------------------------------*/
static void IntToUnicode (uint32_t value , uint8_t *pbuf , uint8_t len);

#if defined (USB_CDC_ENABLE) || defined (USB_HID_ENABLE)
/*******************************************************************************
* Function Name : SPARK_USB_Setup
Expand All @@ -92,18 +91,14 @@ void SPARK_USB_Setup(void)
*******************************************************************************/
void Get_SerialNum(void)
{
uint32_t Device_Serial0, Device_Serial1, Device_Serial2;

Device_Serial0 = *(uint32_t*)ID1;
Device_Serial1 = *(uint32_t*)ID2;
Device_Serial2 = *(uint32_t*)ID3;

Device_Serial0 += Device_Serial2;

if (Device_Serial0 != 0)
{
IntToUnicode (Device_Serial0, &USB_StringSerial[2] , 8);
IntToUnicode (Device_Serial1, &USB_StringSerial[18], 4);
uint8_t deviceId[16];
char deviceIdHex[sizeof(deviceId) * 2 + 1] = {0};
unsigned deviceIdLen = 0;
deviceIdLen = HAL_device_ID(deviceId, sizeof(deviceId));
bytes2hexbuf(deviceId, deviceIdLen, deviceIdHex);
for (unsigned i = 2, pos = 0; i < USB_SIZ_STRING_SERIAL && pos < 2 * deviceIdLen; i += 2, pos++) {
USB_StringSerial[i] = deviceIdHex[pos];
USB_StringSerial[i + 1] = '\0';
}
}
#endif
Expand Down Expand Up @@ -348,31 +343,3 @@ void USB_HID_Send_Report(void *pHIDReport, size_t reportSize)
}
}
#endif

/*******************************************************************************
* Function Name : HexToChar.
* Description : Convert Hex 32Bits value into char.
* Input : None.
* Output : None.
* Return : None.
*******************************************************************************/
static void IntToUnicode (uint32_t value , uint8_t *pbuf , uint8_t len)
{
uint8_t idx = 0;

for( idx = 0 ; idx < len ; idx ++)
{
if( ((value >> 28)) < 0xA )
{
pbuf[ 2* idx] = (value >> 28) + '0';
}
else
{
pbuf[2* idx] = (value >> 28) + 'A' - 10;
}

value = value << 4;

pbuf[ 2* idx + 1] = 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
#define USB_SIZ_STRING_LANGID 4
#define USB_SIZ_STRING_VENDOR 38
#define USB_SIZ_STRING_PRODUCT 50
#define USB_SIZ_STRING_SERIAL 26
#define USB_SIZ_STRING_SERIAL 52

#define STANDARD_ENDPOINT_DESC_SIZE 0x09

Expand Down