浏览代码

usb: Include 16-character serial string based on MCU unique device identifier.

Keir Fraser 5 年之前
父节点
当前提交
b24ac5070b
共有 10 个文件被更改,包括 47 次插入8 次删除
  1. 1 0
      inc/stm32/common.h
  2. 1 0
      inc/stm32/f1.h
  3. 1 0
      inc/stm32/f7.h
  4. 2 0
      inc/util.h
  5. 4 2
      src/usb/config.c
  6. 11 0
      src/usb/core.c
  7. 4 2
      src/usb/defs.h
  8. 2 2
      src/usb/hw_dwc_otg.c
  9. 2 2
      src/usb/hw_f1.c
  10. 19 0
      src/util.c

+ 1 - 0
inc/stm32/common.h

@@ -25,6 +25,7 @@
 #define SPI volatile struct spi * const
 #define SPI volatile struct spi * const
 #define I2C volatile struct i2c * const
 #define I2C volatile struct i2c * const
 #define USART volatile struct usart * const
 #define USART volatile struct usart * const
+#define SER_ID volatile uint32_t * const
 
 
 /* NVIC table */
 /* NVIC table */
 extern uint32_t vector_table[];
 extern uint32_t vector_table[];

+ 1 - 0
inc/stm32/f1.h

@@ -50,6 +50,7 @@ static I2C i2c2 = (struct i2c *)I2C2_BASE;
 static USART usart1 = (struct usart *)USART1_BASE;
 static USART usart1 = (struct usart *)USART1_BASE;
 static USART usart2 = (struct usart *)USART2_BASE;
 static USART usart2 = (struct usart *)USART2_BASE;
 static USART usart3 = (struct usart *)USART3_BASE;
 static USART usart3 = (struct usart *)USART3_BASE;
+static SER_ID ser_id = (uint32_t *)0x1ffff7e8;
 
 
 #define SYSCLK_MHZ 72
 #define SYSCLK_MHZ 72
 #define FLASH_PAGE_SIZE 1024
 #define FLASH_PAGE_SIZE 1024

+ 1 - 0
inc/stm32/f7.h

@@ -65,6 +65,7 @@ static USART usart4 = (struct usart *)USART4_BASE;
 static USART usart5 = (struct usart *)USART5_BASE;
 static USART usart5 = (struct usart *)USART5_BASE;
 static USART usart6 = (struct usart *)USART6_BASE;
 static USART usart6 = (struct usart *)USART6_BASE;
 static HSPHYC hsphyc = (struct hsphyc *)HSPHYC_BASE;
 static HSPHYC hsphyc = (struct hsphyc *)HSPHYC_BASE;
+static SER_ID ser_id = (uint32_t *)0x1ff07a10;
 
 
 #define SYSCLK_MHZ 216
 #define SYSCLK_MHZ 216
 #define AHB_MHZ (SYSCLK_MHZ / 1)  /* 216MHz */
 #define AHB_MHZ (SYSCLK_MHZ / 1)  /* 216MHz */

+ 2 - 0
inc/util.h

@@ -65,6 +65,8 @@ int vsnprintf(char *str, size_t size, const char *format, va_list ap)
 int snprintf(char *str, size_t size, const char *format, ...)
 int snprintf(char *str, size_t size, const char *format, ...)
     __attribute__ ((format (printf, 3, 4)));
     __attribute__ ((format (printf, 3, 4)));
 
 
+void base64(char *dest, const char *src, size_t bits);
+
 #define le16toh(x) (x)
 #define le16toh(x) (x)
 #define le32toh(x) (x)
 #define le32toh(x) (x)
 #define htole16(x) (x)
 #define htole16(x) (x)

+ 4 - 2
src/usb/config.c

@@ -18,7 +18,7 @@ const uint8_t device_descriptor[] aligned(2) = {
     0x09,0x12, /* VID = pid.codes Open Source projects */
     0x09,0x12, /* VID = pid.codes Open Source projects */
     0x01,0x00, /* PID = Test PID #1 */
     0x01,0x00, /* PID = Test PID #1 */
     0,1,       /* Device Release 1.0 */
     0,1,       /* Device Release 1.0 */
-    1,2,0,     /* Manufacturer, Product, Serial */
+    1,2,3,     /* Manufacturer, Product, Serial */
     1          /* Number of configurations */
     1          /* Number of configurations */
 };
 };
 
 
@@ -97,10 +97,12 @@ const uint8_t config_descriptor[] aligned(2) = {
     0x00 /* 6 bInterval */
     0x00 /* 6 bInterval */
 };
 };
 
 
-const char *string_descriptors[] = {
+static char serial_string[32];
+char * const string_descriptors[] = {
     "\x09\x04", /* LANGID: US English */
     "\x09\x04", /* LANGID: US English */
     "Keir Fraser",
     "Keir Fraser",
     "Greaseweazle",
     "Greaseweazle",
+    serial_string,
 };
 };
 
 
 /*
 /*

+ 11 - 0
src/usb/core.c

@@ -11,6 +11,17 @@
 
 
 struct ep0 ep0;
 struct ep0 ep0;
 
 
+void usb_init(void)
+{
+    base64(string_descriptors[3], (const void *)ser_id, 96);
+    hw_usb_init();
+}
+
+void usb_deinit(void)
+{
+    hw_usb_deinit();
+}
+
 static bool_t handle_control_request(void)
 static bool_t handle_control_request(void)
 {
 {
     struct usb_device_request *req = &ep0.req;
     struct usb_device_request *req = &ep0.req;

+ 4 - 2
src/usb/defs.h

@@ -46,8 +46,8 @@ struct packed usb_device_request {
 extern const uint8_t device_descriptor[];
 extern const uint8_t device_descriptor[];
 extern const uint8_t config_descriptor[];
 extern const uint8_t config_descriptor[];
 
 
-#define NR_STRING_DESC 3
-extern const char *string_descriptors[];
+#define NR_STRING_DESC 4
+extern char * const string_descriptors[];
 
 
 extern struct ep0 {
 extern struct ep0 {
     struct usb_device_request req;
     struct usb_device_request req;
@@ -75,6 +75,8 @@ enum { EPT_CONTROL=0, EPT_ISO, EPT_BULK, EPT_INTERRUPT, EPT_DBLBUF };
 void usb_configure_ep(uint8_t ep, uint8_t type, uint32_t size);
 void usb_configure_ep(uint8_t ep, uint8_t type, uint32_t size);
 void usb_stall(uint8_t ep);
 void usb_stall(uint8_t ep);
 void usb_setaddr(uint8_t addr);
 void usb_setaddr(uint8_t addr);
+void hw_usb_init(void);
+void hw_usb_deinit(void);
 
 
 #define WARN printk
 #define WARN printk
 
 

+ 2 - 2
src/usb/hw_dwc_otg.c

@@ -78,7 +78,7 @@ static void write_packet(const void *p, uint8_t ep, int len)
         otg_dfifo[ep].x[0] = *_p++;
         otg_dfifo[ep].x[0] = *_p++;
 }
 }
 
 
-void usb_init(void)
+void hw_usb_init(void)
 {
 {
     int i;
     int i;
 
 
@@ -192,7 +192,7 @@ void usb_init(void)
     delay_ms(3);
     delay_ms(3);
 }
 }
 
 
-void usb_deinit(void)
+void hw_usb_deinit(void)
 {
 {
     switch (conf_port) {
     switch (conf_port) {
     case PORT_FS:
     case PORT_FS:

+ 2 - 2
src/usb/hw_f1.c

@@ -22,7 +22,7 @@ static struct {
     bool_t tx_ready;
     bool_t tx_ready;
 } eps[8];
 } eps[8];
 
 
-void usb_init(void)
+void hw_usb_init(void)
 {
 {
     /* Turn on clock. */
     /* Turn on clock. */
     rcc->apb1enr |= RCC_APB1ENR_USBEN;
     rcc->apb1enr |= RCC_APB1ENR_USBEN;
@@ -42,7 +42,7 @@ void usb_init(void)
     gpio_configure_pin(gpioa, 0, GPO_pushpull(_2MHz, HIGH));
     gpio_configure_pin(gpioa, 0, GPO_pushpull(_2MHz, HIGH));
 }
 }
 
 
-void usb_deinit(void)
+void hw_usb_deinit(void)
 {
 {
     gpio_configure_pin(gpioa, 0, GPI_floating);
     gpio_configure_pin(gpioa, 0, GPI_floating);
     rcc->apb1enr &= ~RCC_APB1ENR_USBEN;
     rcc->apb1enr &= ~RCC_APB1ENR_USBEN;

+ 19 - 0
src/util.c

@@ -142,6 +142,25 @@ char *strcpy(char *dest, const char *src)
     return dest;
     return dest;
 }
 }
 
 
+void base64(char *dest, const char *src, size_t bits)
+{
+    static const char base64[] =
+        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+    char *p = dest;
+    unsigned int i, j, x;
+
+    for (i = 0; i < bits; ) {
+        x = 0;
+        for (j = 0; j < 6; j++, i++) {
+            x <<= 1;
+            x |= (src[i>>3] >> (i&7)) & 1;
+        }
+        *p++ = base64[x];
+    }
+    *p++ = '\0';
+}
+
 /*
 /*
  * Local variables:
  * Local variables:
  * mode: C
  * mode: C