serialno: Refactored the serial number code, cleaning up its API and localising implementation details
This commit is contained in:
parent
259077a87d
commit
886c790b25
|
@ -608,8 +608,7 @@ static bool cmd_traceswo(target *t, int argc, const char **argv)
|
||||||
traceswo_init(swo_channelmask);
|
traceswo_init(swo_channelmask);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
char serial_no[DFU_SERIAL_LENGTH];
|
serial_no_read();
|
||||||
serial_no_read(serial_no);
|
|
||||||
gdb_outf("Trace enabled for BMP serial %s, USB EP 5\n", serial_no);
|
gdb_outf("Trace enabled for BMP serial %s, USB EP 5\n", serial_no);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,12 @@
|
||||||
#ifndef __SERIALNO_H
|
#ifndef __SERIALNO_H
|
||||||
#define __SERIALNO_H
|
#define __SERIALNO_H
|
||||||
|
|
||||||
char *serial_no_read(char *s);
|
#ifndef DFU_SERIAL_LENGTH
|
||||||
|
#define DFU_SERIAL_LENGTH
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
extern char serial_no[DFU_SERIAL_LENGTH];
|
||||||
|
|
||||||
|
void serial_no_read(void);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
|
@ -26,7 +26,6 @@
|
||||||
#include "cdcacm.h"
|
#include "cdcacm.h"
|
||||||
#include "serialno.h"
|
#include "serialno.h"
|
||||||
|
|
||||||
char serial_no[DFU_SERIAL_LENGTH];
|
|
||||||
usbd_device *usbdev = NULL;
|
usbd_device *usbdev = NULL;
|
||||||
|
|
||||||
/* We need a special large control buffer for this device: */
|
/* We need a special large control buffer for this device: */
|
||||||
|
@ -34,7 +33,7 @@ static uint8_t usbd_control_buffer[256];
|
||||||
|
|
||||||
void blackmagic_usb_init(void)
|
void blackmagic_usb_init(void)
|
||||||
{
|
{
|
||||||
serial_no_read(serial_no);
|
serial_no_read();
|
||||||
|
|
||||||
usbdev = usbd_init(&USB_DRIVER, &dev_desc, &config, usb_strings, sizeof(usb_strings) / sizeof(char *),
|
usbdev = usbd_init(&USB_DRIVER, &dev_desc, &config, usb_strings, sizeof(usb_strings) / sizeof(char *),
|
||||||
usbd_control_buffer, sizeof(usbd_control_buffer));
|
usbd_control_buffer, sizeof(usbd_control_buffer));
|
||||||
|
|
|
@ -26,12 +26,11 @@
|
||||||
#include <libopencm3/usb/dfu.h>
|
#include <libopencm3/usb/dfu.h>
|
||||||
|
|
||||||
#include "usb.h"
|
#include "usb.h"
|
||||||
|
#include "serialno.h"
|
||||||
#include "version.h"
|
#include "version.h"
|
||||||
|
|
||||||
#define BOARD_IDENT "Black Magic Probe " PLATFORM_IDENT FIRMWARE_VERSION
|
#define BOARD_IDENT "Black Magic Probe " PLATFORM_IDENT FIRMWARE_VERSION
|
||||||
|
|
||||||
extern char serial_no[DFU_SERIAL_LENGTH];
|
|
||||||
|
|
||||||
/* Top-level device descriptor */
|
/* Top-level device descriptor */
|
||||||
static const struct usb_device_descriptor dev_desc = {
|
static const struct usb_device_descriptor dev_desc = {
|
||||||
.bLength = USB_DT_DEVICE_SIZE,
|
.bLength = USB_DT_DEVICE_SIZE,
|
||||||
|
|
|
@ -30,6 +30,7 @@
|
||||||
|
|
||||||
extern void trace_tick(void);
|
extern void trace_tick(void);
|
||||||
|
|
||||||
|
char serial_no[DFU_SERIAL_LENGTH];
|
||||||
volatile platform_timeout * volatile head_timeout;
|
volatile platform_timeout * volatile head_timeout;
|
||||||
uint8_t running_status;
|
uint8_t running_status;
|
||||||
static volatile uint32_t time_ms;
|
static volatile uint32_t time_ms;
|
||||||
|
@ -115,19 +116,18 @@ const char *platform_target_voltage(void)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *serial_no_read(char *s)
|
void serial_no_read(void)
|
||||||
{
|
{
|
||||||
/* FIXME: Store a unique serial number somewhere and retreive here */
|
/* FIXME: Store a unique serial number somewhere and retreive here */
|
||||||
uint32_t unique_id = SERIAL_NO;
|
uint32_t unique_id = SERIAL_NO;
|
||||||
|
|
||||||
/* Fetch serial number from chip's unique ID */
|
/* Fetch serial number from chip's unique ID */
|
||||||
for (size_t i = 0; i < DFU_SERIAL_LENGTH - 1; i++) {
|
for (size_t i = 0; i < DFU_SERIAL_LENGTH - 1; i++) {
|
||||||
s[7U - i] = ((unique_id >> (4 * i)) & 0x0FU) + '0';
|
serial_no[7U - i] = ((unique_id >> (4 * i)) & 0x0FU) + '0';
|
||||||
if (s[7U - i] > '9')
|
if (serial_no[7U - i] > '9')
|
||||||
s[7U - i] += 7; /* 'A' - '9' = 8, less 1 gives 7. */
|
serial_no[7U - i] += 7; /* 'A' - '9' = 8, less 1 gives 7. */
|
||||||
}
|
}
|
||||||
s[DFU_SERIAL_LENGTH - 1] = 0;
|
serial_no[DFU_SERIAL_LENGTH - 1] = 0;
|
||||||
return s;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void platform_request_boot(void) { }
|
void platform_request_boot(void) { }
|
||||||
|
|
|
@ -53,7 +53,7 @@ static uint32_t max_address;
|
||||||
|
|
||||||
static enum dfu_state usbdfu_state = STATE_DFU_IDLE;
|
static enum dfu_state usbdfu_state = STATE_DFU_IDLE;
|
||||||
|
|
||||||
static char *get_dev_unique_id(char *serial_no);
|
static void get_dev_unique_id(void);
|
||||||
|
|
||||||
static struct {
|
static struct {
|
||||||
uint8_t buf[sizeof(usbd_control_buffer)];
|
uint8_t buf[sizeof(usbd_control_buffer)];
|
||||||
|
@ -125,7 +125,6 @@ const struct usb_config_descriptor config = {
|
||||||
.interface = ifaces,
|
.interface = ifaces,
|
||||||
};
|
};
|
||||||
|
|
||||||
static char serial_no[DFU_SERIAL_LENGTH];
|
|
||||||
static char if_string[] = DFU_IFACE_STRING;
|
static char if_string[] = DFU_IFACE_STRING;
|
||||||
#define BOARD_IDENT_DFU(BOARD_TYPE) "Black Magic Probe DFU " PLATFORM_IDENT "" FIRMWARE_VERSION
|
#define BOARD_IDENT_DFU(BOARD_TYPE) "Black Magic Probe DFU " PLATFORM_IDENT "" FIRMWARE_VERSION
|
||||||
|
|
||||||
|
@ -291,7 +290,7 @@ static enum usbd_request_return_codes usbdfu_control_request(usbd_device *dev,
|
||||||
|
|
||||||
void dfu_init(const usbd_driver *driver)
|
void dfu_init(const usbd_driver *driver)
|
||||||
{
|
{
|
||||||
get_dev_unique_id(serial_no);
|
get_dev_unique_id();
|
||||||
|
|
||||||
usbdev = usbd_init(driver, &dev_desc, &config,
|
usbdev = usbd_init(driver, &dev_desc, &config,
|
||||||
usb_strings, 4,
|
usb_strings, 4,
|
||||||
|
@ -337,7 +336,7 @@ static void set_dfu_iface_string(uint32_t size)
|
||||||
# define set_dfu_iface_string(x)
|
# define set_dfu_iface_string(x)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static char *get_dev_unique_id(char *s)
|
static void get_dev_unique_id(void)
|
||||||
{
|
{
|
||||||
uint32_t fuse_flash_size;
|
uint32_t fuse_flash_size;
|
||||||
|
|
||||||
|
@ -348,5 +347,5 @@ static char *get_dev_unique_id(char *s)
|
||||||
fuse_flash_size = 0x80;
|
fuse_flash_size = 0x80;
|
||||||
set_dfu_iface_string(fuse_flash_size - 8);
|
set_dfu_iface_string(fuse_flash_size - 8);
|
||||||
max_address = FLASH_BASE + (fuse_flash_size << 10);
|
max_address = FLASH_BASE + (fuse_flash_size << 10);
|
||||||
return serial_no_read(s);
|
serial_no_read();
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,17 +20,19 @@
|
||||||
#include "general.h"
|
#include "general.h"
|
||||||
#include <libopencm3/stm32/desig.h>
|
#include <libopencm3/stm32/desig.h>
|
||||||
|
|
||||||
char *serial_no_read(char *s)
|
char serial_no[DFU_SERIAL_LENGTH];
|
||||||
|
|
||||||
|
void serial_no_read(void)
|
||||||
{
|
{
|
||||||
#if DFU_SERIAL_LENGTH == 9
|
#if DFU_SERIAL_LENGTH == 9
|
||||||
const volatile uint32_t *const unique_id_p = (uint32_t *)DESIG_UNIQUE_ID_BASE;
|
const volatile uint32_t *const unique_id_p = (uint32_t *)DESIG_UNIQUE_ID_BASE;
|
||||||
const uint32_t unique_id = unique_id_p[0] + unique_id_p[1] + unique_id_p[2];
|
const uint32_t unique_id = unique_id_p[0] + unique_id_p[1] + unique_id_p[2];
|
||||||
/* Fetch serial number from chip's unique ID */
|
/* Fetch serial number from chip's unique ID */
|
||||||
for (size_t i = 0; i < 8U; ++i) {
|
for (size_t i = 0; i < 8U; ++i) {
|
||||||
s[7U - i] = ((unique_id >> (i * 4U)) & 0x0FU) + '0';
|
serial_no[7U - i] = ((unique_id >> (i * 4U)) & 0x0FU) + '0';
|
||||||
/* If the character is something above 9, then add the offset to make it ASCII A-F */
|
/* If the character is something above 9, then add the offset to make it ASCII A-F */
|
||||||
if (s[7U - i] > '9')
|
if (serial_no[7U - i] > '9')
|
||||||
s[7U - i] += 7; /* 'A' - '9' = 8, less 1 gives 7. */
|
serial_no[7U - i] += 7; /* 'A' - '9' = 8, less 1 gives 7. */
|
||||||
}
|
}
|
||||||
#elif DFU_SERIAL_LENGTH == 13
|
#elif DFU_SERIAL_LENGTH == 13
|
||||||
/* Use the same serial number as the ST DFU Bootloader.*/
|
/* Use the same serial number as the ST DFU Bootloader.*/
|
||||||
|
@ -40,7 +42,7 @@ char *serial_no_read(char *s)
|
||||||
#elif defined(STM32L0) || defined(STM32F0) || defined(STM32F3)
|
#elif defined(STM32L0) || defined(STM32F0) || defined(STM32F3)
|
||||||
int offset = 5;
|
int offset = 5;
|
||||||
#endif
|
#endif
|
||||||
sprintf(s, "%04X%04X%04X", uid[1] + uid[5], uid[0] + uid[4], uid[offset]);
|
sprintf(serial_no, "%04X%04X%04X", uid[1] + uid[5], uid[0] + uid[4], uid[offset]);
|
||||||
#elif DFU_SERIAL_LENGTH == 25
|
#elif DFU_SERIAL_LENGTH == 25
|
||||||
const volatile uint32_t *const unique_id_p = (uint32_t *)DESIG_UNIQUE_ID_BASE;
|
const volatile uint32_t *const unique_id_p = (uint32_t *)DESIG_UNIQUE_ID_BASE;
|
||||||
uint32_t unique_id = 0;
|
uint32_t unique_id = 0;
|
||||||
|
@ -50,15 +52,14 @@ char *serial_no_read(char *s)
|
||||||
const size_t idx = (chunk << 3U) + (7U - nibble);
|
const size_t idx = (chunk << 3U) + (7U - nibble);
|
||||||
if (nibble == 0)
|
if (nibble == 0)
|
||||||
unique_id = unique_id_p[chunk];
|
unique_id = unique_id_p[chunk];
|
||||||
s[idx] = ((unique_id >> (nibble * 4U)) & 0x0F) + '0';
|
serial_no[idx] = ((unique_id >> (nibble * 4U)) & 0x0F) + '0';
|
||||||
|
|
||||||
/* If the character is something above 9, then add the offset to make it ASCII A-F */
|
/* If the character is something above 9, then add the offset to make it ASCII A-F */
|
||||||
if (s[idx] > '9')
|
if (serial_no[idx] > '9')
|
||||||
s[idx] += 7; /* 'A' - '9' = 8, less 1 gives 7. */
|
serial_no[idx] += 7; /* 'A' - '9' = 8, less 1 gives 7. */
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
#WARNING "Unhandled DFU_SERIAL_LENGTH"
|
#WARNING "Unhandled DFU_SERIAL_LENGTH"
|
||||||
#endif
|
#endif
|
||||||
s[DFU_SERIAL_LENGTH - 1] = '\0';
|
serial_no[DFU_SERIAL_LENGTH - 1] = '\0';
|
||||||
return s;
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue