From 6d697b08fae43383baf293226dcb8b8877c2e3b9 Mon Sep 17 00:00:00 2001 From: Samuel Sadok Date: Mon, 12 Mar 2018 23:45:17 -0700 Subject: [PATCH] replace UUID_... property by serial_number --- Firmware/Board/v3/Src/main.c | 11 +++++++++++ Firmware/Board/v3/Src/usbd_desc.c | 22 ++++++---------------- Firmware/CHANGELOG.md | 1 + Firmware/MotorControl/commands.cpp | 5 ++--- Firmware/MotorControl/commands.h | 2 ++ 5 files changed, 22 insertions(+), 19 deletions(-) diff --git a/Firmware/Board/v3/Src/main.c b/Firmware/Board/v3/Src/main.c index 53fa15dc..55088a70 100644 --- a/Firmware/Board/v3/Src/main.c +++ b/Firmware/Board/v3/Src/main.c @@ -57,6 +57,8 @@ #include "usart.h" #include "usb_device.h" #include "gpio.h" +#include "utils.h" +#include "commands.h" /* USER CODE BEGIN Includes */ @@ -107,6 +109,15 @@ int main(void) jump_to_builtin_bootloader(); } + // This procedure of building a USB serial number should be identical + // to the way the STM's built-in USB bootloader does it. This means + // that the device will have the same serial number in normal and DFU mode. + uint32_t uuid0 = *(uint32_t *) (ID_UNIQUE_ADDRESS + 0); + uint32_t uuid1 = *(uint32_t *) (ID_UNIQUE_ADDRESS + 4); + uint32_t uuid2 = *(uint32_t *) (ID_UNIQUE_ADDRESS + 8); + uint32_t uuid_mixed_part = uuid0 + uuid2; + serial_number = ((uint64_t)uuid_mixed_part << 16) | (uint64_t)(uuid1 >> 16); + /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ diff --git a/Firmware/Board/v3/Src/usbd_desc.c b/Firmware/Board/v3/Src/usbd_desc.c index 66ceb06c..94dc49b9 100644 --- a/Firmware/Board/v3/Src/usbd_desc.c +++ b/Firmware/Board/v3/Src/usbd_desc.c @@ -51,7 +51,7 @@ #include "usbd_core.h" #include "usbd_desc.h" #include "usbd_conf.h" -#include "utils.h" +#include "commands.h" /* USER CODE BEGIN INCLUDE */ @@ -328,21 +328,11 @@ uint8_t * USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *l */ uint8_t * USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) { - // This procedure of building a USB serial number should be identical - // to the way the STM's built-in USB bootloader does it. This means - // that the device will have the same serial number in normal and DFU mode. - uint32_t uuid0 = *(uint32_t *) (ID_UNIQUE_ADDRESS + 0); - uint32_t uuid1 = *(uint32_t *) (ID_UNIQUE_ADDRESS + 4); - uint32_t uuid2 = *(uint32_t *) (ID_UNIQUE_ADDRESS + 8); - uint32_t uuid_first_part = uuid0 + uuid2; - uint8_t str[13]; - for (size_t i = 0; i < 8; ++i) { - str[i] = "0123456789ABCDEF"[(uuid_first_part >> 28) & 0xf]; - uuid_first_part <<= 4; - } - for (size_t i = 8; i < 12; ++i) { - str[i] = "0123456789ABCDEF"[(uuid1 >> 28) & 0xf]; - uuid1 <<= 4; + uint8_t str[13]; // 12 digits + null termination + uint64_t val = serial_number; + for (size_t i = 0; i < 12; ++i) { + str[i] = "0123456789ABCDEF"[(val >> (48-4)) & 0xf]; + val <<= 4; } str[12] = 0; diff --git a/Firmware/CHANGELOG.md b/Firmware/CHANGELOG.md index b3ffec37..a9aefc64 100644 --- a/Firmware/CHANGELOG.md +++ b/Firmware/CHANGELOG.md @@ -7,6 +7,7 @@ Please add a note of your changes below this heading if you make a Pull Request. ### Changed * The build is now configured using the `tup.config` file instead of editing source files. Make sure you set your board version correctly. See [here](README.md#configuring-the-build) for details. * Update CubeMX generated STM platform code to version 1.19.0 +* Remove `UUID_0`, `UUID_1` and `UUID_2` from USB protocol. Use `serial_number` instead. # Releases diff --git a/Firmware/MotorControl/commands.cpp b/Firmware/MotorControl/commands.cpp index e6edfc9e..d725afa0 100644 --- a/Firmware/MotorControl/commands.cpp +++ b/Firmware/MotorControl/commands.cpp @@ -35,6 +35,7 @@ extern PCD_HandleTypeDef hpcd_USB_OTG_FS; extern USBD_HandleTypeDef hUsbDeviceFS; +uint64_t serial_number; /* Private constant data -----------------------------------------------------*/ // TODO: make command to switch gpio_mode during run-time @@ -115,9 +116,7 @@ void enter_dfu_mode() { // clang-format off const Endpoint endpoints[] = { Endpoint::make_property("vbus_voltage", const_cast(&vbus_voltage)), - Endpoint::make_property("UUID_0", (const uint32_t*)(ID_UNIQUE_ADDRESS + 0*4)), - Endpoint::make_property("UUID_1", (const uint32_t*)(ID_UNIQUE_ADDRESS + 1*4)), - Endpoint::make_property("UUID_2", (const uint32_t*)(ID_UNIQUE_ADDRESS + 2*4)), + Endpoint::make_property("serial_number", const_cast(&serial_number)), Endpoint::make_function("run_anticogging_calibration", &motors_run_anticogging_calibration_func), // No parameters, but still requires a close_tree() Endpoint::close_tree(), diff --git a/Firmware/MotorControl/commands.h b/Firmware/MotorControl/commands.h index 8ce16821..f7a7fe19 100644 --- a/Firmware/MotorControl/commands.h +++ b/Firmware/MotorControl/commands.h @@ -19,6 +19,8 @@ void set_cmd_buffer(uint8_t *buf, uint32_t len); void usb_update_thread(); void USB_receive_packet(const uint8_t *buffer, size_t length); +extern uint64_t serial_number; + #ifdef __cplusplus } #endif