From a51bf72bc76d2036fc4f58d61d9b14de8a4c53ec Mon Sep 17 00:00:00 2001 From: Stacey Sheldon Date: Sat, 14 Jan 2012 14:17:06 -0500 Subject: [PATCH] usb: allow runtime selection of USB descriptors via hwsettings --- flight/Bootloaders/CopterControl/Makefile | 1 - .../CopterControl/inc/pios_usb_board_data.h | 27 --- flight/Bootloaders/CopterControl/pios_board.c | 10 + .../CopterControl/pios_usb_board_data.c | 77 ++++++- flight/Bootloaders/OpenPilot/Makefile | 2 +- .../OpenPilot/inc/pios_usb_board_data.h | 27 --- flight/Bootloaders/OpenPilot/main.c | 5 - flight/Bootloaders/OpenPilot/op_dfu.c | 7 +- flight/Bootloaders/OpenPilot/pios_board.c | 12 +- .../OpenPilot/pios_usb_board_data.c | 78 ++++++- flight/Bootloaders/PipXtreme/Makefile | 2 +- .../Bootloaders/PipXtreme/inc/pios_config.h | 1 - .../PipXtreme/inc/pios_usb_board_data.h | 27 --- flight/Bootloaders/PipXtreme/main.c | 5 - flight/Bootloaders/PipXtreme/op_dfu.c | 11 +- flight/Bootloaders/PipXtreme/pios_board.c | 9 + .../PipXtreme/pios_usb_board_data.c | 77 ++++++- flight/CopterControl/Makefile | 1 + .../System/inc/pios_usb_board_data.h | 39 +--- flight/CopterControl/System/pios_board.c | 49 ++++- .../System/pios_usb_board_data.c | 77 ++++++- flight/OpenPilot/Makefile | 2 + flight/OpenPilot/System/inc/pios_config.h | 1 + .../System/inc/pios_usb_board_data.h | 37 ---- flight/OpenPilot/System/pios_board.c | 193 +++++++++++++++--- flight/OpenPilot/System/pios_usb_board_data.c | 79 ++++++- flight/PiOS/Common/pios_usb_desc_hid_cdc.c | 183 ++++++++--------- flight/PiOS/Common/pios_usb_desc_hid_only.c | 173 +++++++--------- .../STM32_USB-FS-Device_Driver/inc/usb_core.h | 19 +- .../STM32_USB-FS-Device_Driver/inc/usb_mem.h | 2 +- .../STM32_USB-FS-Device_Driver/src/usb_core.c | 20 +- .../STM32_USB-FS-Device_Driver/src/usb_mem.c | 2 +- flight/PiOS/STM32F10x/pios_usb.c | 7 - flight/PiOS/STM32F10x/pios_usb_cdc.c | 2 +- flight/PiOS/STM32F10x/pios_usbhook.c | 114 +++++------ flight/PiOS/inc/pios_usb_board_data_priv.h | 1 + flight/PiOS/inc/pios_usb_cdc_priv.h | 2 +- flight/PiOS/inc/pios_usb_desc_hid_cdc_priv.h | 8 + flight/PiOS/inc/pios_usb_desc_hid_only_priv.h | 8 + flight/PiOS/inc/pios_usbhook.h | 13 ++ flight/PipXtreme/inc/pios_usb_board_data.h | 27 --- flight/PipXtreme/pios_board.c | 14 +- flight/PipXtreme/pios_usb_board_data.c | 77 ++++++- shared/uavobjectdefinition/hwsettings.xml | 1 + 44 files changed, 980 insertions(+), 549 deletions(-) create mode 100644 flight/PiOS/inc/pios_usb_board_data_priv.h create mode 100644 flight/PiOS/inc/pios_usb_desc_hid_cdc_priv.h create mode 100644 flight/PiOS/inc/pios_usb_desc_hid_only_priv.h diff --git a/flight/Bootloaders/CopterControl/Makefile b/flight/Bootloaders/CopterControl/Makefile index a5effde9c..699fd02e5 100644 --- a/flight/Bootloaders/CopterControl/Makefile +++ b/flight/Bootloaders/CopterControl/Makefile @@ -104,7 +104,6 @@ SRC += $(PIOSSTM32F10X)/pios_irq.c SRC += $(PIOSSTM32F10X)/pios_debug.c SRC += $(PIOSSTM32F10X)/pios_gpio.c - # PIOS USB related files (seperated to make code maintenance more easy) SRC += $(PIOSSTM32F10X)/pios_usb.c SRC += $(PIOSSTM32F10X)/pios_usbhook.c diff --git a/flight/Bootloaders/CopterControl/inc/pios_usb_board_data.h b/flight/Bootloaders/CopterControl/inc/pios_usb_board_data.h index 85f407a9e..f1accee17 100644 --- a/flight/Bootloaders/CopterControl/inc/pios_usb_board_data.h +++ b/flight/Bootloaders/CopterControl/inc/pios_usb_board_data.h @@ -37,33 +37,6 @@ #include "pios_usb_defs.h" /* struct usb_* */ -struct usb_board_config { - struct usb_configuration_desc config; - struct usb_interface_desc hid_if; - struct usb_hid_desc hid; - struct usb_endpoint_desc hid_in; - struct usb_endpoint_desc hid_out; -} __attribute__((packed)); - -extern const struct usb_device_desc PIOS_USB_BOARD_DeviceDescriptor; -extern const struct usb_board_config PIOS_USB_BOARD_Configuration; -extern const struct usb_string_langid PIOS_USB_BOARD_StringLangID; - -/* NOTE NOTE NOTE - * - * Care must be taken to ensure that the _actual_ contents of - * these arrays (in each board's pios_usb_board_data.c) is no - * smaller than the stated sizes here or these descriptors - * will end up with trailing zeros on the wire. - * - * The compiler will catch any time that these definitions are - * too small. - */ -extern const uint8_t PIOS_USB_BOARD_HidReportDescriptor[36]; -extern const uint8_t PIOS_USB_BOARD_StringVendorID[28]; -extern const uint8_t PIOS_USB_BOARD_StringProductID[28]; -extern uint8_t PIOS_USB_BOARD_StringSerial[52]; - #define PIOS_USB_BOARD_PRODUCT_ID USB_PRODUCT_ID_COPTERCONTROL #define PIOS_USB_BOARD_DEVICE_VER USB_OP_DEVICE_VER(USB_OP_BOARD_ID_COPTERCONTROL, USB_OP_BOARD_MODE_BL) diff --git a/flight/Bootloaders/CopterControl/pios_board.c b/flight/Bootloaders/CopterControl/pios_board.c index 972a85be0..a70537fcc 100644 --- a/flight/Bootloaders/CopterControl/pios_board.c +++ b/flight/Bootloaders/CopterControl/pios_board.c @@ -48,6 +48,10 @@ static const struct pios_usb_cfg pios_usb_main_cfg = { }, }, }; + +#include "pios_usb_board_data_priv.h" +#include "pios_usb_desc_hid_only_priv.h" + #endif /* PIOS_INCLUDE_USB */ #if defined(PIOS_INCLUDE_USB_HID) @@ -87,6 +91,12 @@ void PIOS_Board_Init(void) { PIOS_GPIO_Init(); #if defined(PIOS_INCLUDE_USB) + /* Initialize board specific USB data */ + PIOS_USB_BOARD_DATA_Init(); + + /* Activate the HID-only USB configuration */ + PIOS_USB_DESC_HID_ONLY_Init(); + uint32_t pios_usb_id; if (PIOS_USB_Init(&pios_usb_id, &pios_usb_main_cfg)) { PIOS_Assert(0); diff --git a/flight/Bootloaders/CopterControl/pios_usb_board_data.c b/flight/Bootloaders/CopterControl/pios_usb_board_data.c index b39fcbec3..4f02ce424 100644 --- a/flight/Bootloaders/CopterControl/pios_usb_board_data.c +++ b/flight/Bootloaders/CopterControl/pios_usb_board_data.c @@ -29,9 +29,11 @@ */ #include "pios_usb_board_data.h" /* struct usb_*, USB_* */ +#include "pios_sys.h" /* PIOS_SYS_SerialNumberGet */ +#include "pios_usbhook.h" /* PIOS_USBHOOK_* */ -const uint8_t PIOS_USB_BOARD_StringProductID[] = { - sizeof(PIOS_USB_BOARD_StringProductID), +static const uint8_t usb_product_id[28] = { + sizeof(usb_product_id), USB_DESC_TYPE_STRING, 'C', 0, 'o', 0, @@ -48,3 +50,74 @@ const uint8_t PIOS_USB_BOARD_StringProductID[] = { 'l', 0, }; +static uint8_t usb_serial_number[52] = { + sizeof(usb_serial_number), + USB_DESC_TYPE_STRING, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0 +}; + +static const struct usb_string_langid usb_lang_id = { + .bLength = sizeof(usb_lang_id), + .bDescriptorType = USB_DESC_TYPE_STRING, + .bLangID = htousbs(USB_LANGID_ENGLISH_UK), +}; + +static const uint8_t usb_vendor_id[28] = { + sizeof(usb_vendor_id), + USB_DESC_TYPE_STRING, + 'o', 0, + 'p', 0, + 'e', 0, + 'n', 0, + 'p', 0, + 'i', 0, + 'l', 0, + 'o', 0, + 't', 0, + '.', 0, + 'o', 0, + 'r', 0, + 'g', 0 +}; + +int32_t PIOS_USB_BOARD_DATA_Init(void) +{ + /* Load device serial number into serial number string */ + uint8_t sn[25]; + PIOS_SYS_SerialNumberGet((char *)sn); + for (uint8_t i = 0; sn[i] != '\0' && (2 * i) < usb_serial_number[0]; i++) { + usb_serial_number[2 + 2 * i] = sn[i]; + } + + PIOS_USBHOOK_RegisterString(USB_STRING_DESC_PRODUCT, (uint8_t *)&usb_product_id, sizeof(usb_product_id)); + PIOS_USBHOOK_RegisterString(USB_STRING_DESC_SERIAL, (uint8_t *)&usb_serial_number, sizeof(usb_serial_number)); + + PIOS_USBHOOK_RegisterString(USB_STRING_DESC_LANG, (uint8_t *)&usb_lang_id, sizeof(usb_lang_id)); + PIOS_USBHOOK_RegisterString(USB_STRING_DESC_VENDOR, (uint8_t *)&usb_vendor_id, sizeof(usb_vendor_id)); + + return 0; +} diff --git a/flight/Bootloaders/OpenPilot/Makefile b/flight/Bootloaders/OpenPilot/Makefile index 6c3888797..05d1bec1d 100644 --- a/flight/Bootloaders/OpenPilot/Makefile +++ b/flight/Bootloaders/OpenPilot/Makefile @@ -293,7 +293,7 @@ CSTANDARD = -std=gnu99 # Flags for C and C++ (arm-elf-gcc/arm-elf-g++) ifeq ($(DEBUG),YES) -CFLAGS = -DDEBUG +CFLAGS += -DDEBUG endif CFLAGS += -g$(DEBUGF) diff --git a/flight/Bootloaders/OpenPilot/inc/pios_usb_board_data.h b/flight/Bootloaders/OpenPilot/inc/pios_usb_board_data.h index d3e1079d6..ca9c948b0 100644 --- a/flight/Bootloaders/OpenPilot/inc/pios_usb_board_data.h +++ b/flight/Bootloaders/OpenPilot/inc/pios_usb_board_data.h @@ -37,33 +37,6 @@ #include "pios_usb_defs.h" /* struct usb_* */ -struct usb_board_config { - struct usb_configuration_desc config; - struct usb_interface_desc hid_if; - struct usb_hid_desc hid; - struct usb_endpoint_desc hid_in; - struct usb_endpoint_desc hid_out; -} __attribute__((packed)); - -extern const struct usb_device_desc PIOS_USB_BOARD_DeviceDescriptor; -extern const struct usb_board_config PIOS_USB_BOARD_Configuration; -extern const struct usb_string_langid PIOS_USB_BOARD_StringLangID; - -/* NOTE NOTE NOTE - * - * Care must be taken to ensure that the _actual_ contents of - * these arrays (in each board's pios_usb_board_data.c) is no - * smaller than the stated sizes here or these descriptors - * will end up with trailing zeros on the wire. - * - * The compiler will catch any time that these definitions are - * too small. - */ -extern const uint8_t PIOS_USB_BOARD_HidReportDescriptor[36]; -extern const uint8_t PIOS_USB_BOARD_StringVendorID[28]; -extern const uint8_t PIOS_USB_BOARD_StringProductID[20]; -extern uint8_t PIOS_USB_BOARD_StringSerial[52]; - #define PIOS_USB_BOARD_PRODUCT_ID USB_PRODUCT_ID_OPENPILOT_MAIN #define PIOS_USB_BOARD_DEVICE_VER USB_OP_DEVICE_VER(USB_OP_BOARD_ID_OPENPILOT_MAIN, USB_OP_BOARD_MODE_BL) diff --git a/flight/Bootloaders/OpenPilot/main.c b/flight/Bootloaders/OpenPilot/main.c index 8a9e554ef..3a06c9751 100644 --- a/flight/Bootloaders/OpenPilot/main.c +++ b/flight/Bootloaders/OpenPilot/main.c @@ -102,10 +102,6 @@ uint32_t sspTimeSource(); #define RED LED2 #define LED_PWM_TIMER TIM6 int main() { - /* NOTE: Do NOT modify the following start-up sequence */ - /* Any new initialization functions should be added in OpenPilotInit() */ - - /* Brings up System using CMSIS functions, enables the LEDs. */ PIOS_SYS_Init(); if (BSL_HOLD_STATE == 0) USB_connected = TRUE; @@ -228,7 +224,6 @@ void jump_to_app() { RCC_APB1PeriphResetCmd(0xffffffff, DISABLE); _SetCNTR(0); // clear interrupt mask _SetISTR(0); // clear all requests - JumpAddress = *(__IO uint32_t*) (bdinfo->fw_base + 4); Jump_To_Application = (pFunction) JumpAddress; /* Initialize user application's Stack Pointer */ diff --git a/flight/Bootloaders/OpenPilot/op_dfu.c b/flight/Bootloaders/OpenPilot/op_dfu.c index 40ef6fbda..ef30e4897 100644 --- a/flight/Bootloaders/OpenPilot/op_dfu.c +++ b/flight/Bootloaders/OpenPilot/op_dfu.c @@ -34,10 +34,6 @@ #include #include "pios_opahrs.h" #include "ssp.h" -/* Private typedef -----------------------------------------------------------*/ -/* Private define ------------------------------------------------------------*/ -/* Private macro -------------------------------------------------------------*/ -/* Private variables ---------------------------------------------------------*/ //programmable devices Device devicesTable[10]; uint8_t numberOfDevices = 0; @@ -223,7 +219,7 @@ void processComand(uint8_t *xReceive_Buffer) { } } if (result != 1) { - DeviceState = Last_operation_failed;//ok + DeviceState = Last_operation_failed; Aditionals = (uint32_t) Command; } else { @@ -392,7 +388,6 @@ void processComand(uint8_t *xReceive_Buffer) { DeviceState = too_few_packets; } } - break; case Download_Req: #ifdef DEBUG_SSP diff --git a/flight/Bootloaders/OpenPilot/pios_board.c b/flight/Bootloaders/OpenPilot/pios_board.c index 707600b4d..d1ba74d8b 100644 --- a/flight/Bootloaders/OpenPilot/pios_board.c +++ b/flight/Bootloaders/OpenPilot/pios_board.c @@ -215,7 +215,11 @@ static const struct pios_usb_cfg pios_usb_main_cfg = { }, }, }; -#endif /* PIOS_INCLUDE_USB_HID */ + +#include "pios_usb_board_data_priv.h" +#include "pios_usb_desc_hid_only_priv.h" + +#endif /* PIOS_INCLUDE_USB */ #if defined(PIOS_INCLUDE_USB_HID) #include @@ -270,6 +274,12 @@ void PIOS_Board_Init(void) { PIOS_GPIO_Init(); #if defined(PIOS_INCLUDE_USB) + /* Initialize board specific USB data */ + PIOS_USB_BOARD_DATA_Init(); + + /* Activate the HID-only USB configuration */ + PIOS_USB_DESC_HID_ONLY_Init(); + uint32_t pios_usb_id; if (PIOS_USB_Init(&pios_usb_id, &pios_usb_main_cfg)) { PIOS_Assert(0); diff --git a/flight/Bootloaders/OpenPilot/pios_usb_board_data.c b/flight/Bootloaders/OpenPilot/pios_usb_board_data.c index 9492d316e..b73419ba4 100644 --- a/flight/Bootloaders/OpenPilot/pios_usb_board_data.c +++ b/flight/Bootloaders/OpenPilot/pios_usb_board_data.c @@ -29,9 +29,11 @@ */ #include "pios_usb_board_data.h" /* struct usb_*, USB_* */ +#include "pios_sys.h" /* PIOS_SYS_SerialNumberGet */ +#include "pios_usbhook.h" /* PIOS_USBHOOK_* */ -const uint8_t PIOS_USB_BOARD_StringProductID[] = { - sizeof(PIOS_USB_BOARD_StringProductID), +static const uint8_t usb_product_id[20] = { + sizeof(usb_product_id), USB_DESC_TYPE_STRING, 'O', 0, 'p', 0, @@ -43,3 +45,75 @@ const uint8_t PIOS_USB_BOARD_StringProductID[] = { 'o', 0, 't', 0, }; + +static uint8_t usb_serial_number[52] = { + sizeof(usb_serial_number), + USB_DESC_TYPE_STRING, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0 +}; + +static const struct usb_string_langid usb_lang_id = { + .bLength = sizeof(usb_lang_id), + .bDescriptorType = USB_DESC_TYPE_STRING, + .bLangID = htousbs(USB_LANGID_ENGLISH_UK), +}; + +static const uint8_t usb_vendor_id[28] = { + sizeof(usb_vendor_id), + USB_DESC_TYPE_STRING, + 'o', 0, + 'p', 0, + 'e', 0, + 'n', 0, + 'p', 0, + 'i', 0, + 'l', 0, + 'o', 0, + 't', 0, + '.', 0, + 'o', 0, + 'r', 0, + 'g', 0 +}; + +int32_t PIOS_USB_BOARD_DATA_Init(void) +{ + /* Load device serial number into serial number string */ + uint8_t sn[25]; + PIOS_SYS_SerialNumberGet((char *)sn); + for (uint8_t i = 0; sn[i] != '\0' && (2 * i) < usb_serial_number[0]; i++) { + usb_serial_number[2 + 2 * i] = sn[i]; + } + + PIOS_USBHOOK_RegisterString(USB_STRING_DESC_PRODUCT, (uint8_t *)&usb_product_id, sizeof(usb_product_id)); + PIOS_USBHOOK_RegisterString(USB_STRING_DESC_SERIAL, (uint8_t *)&usb_serial_number, sizeof(usb_serial_number)); + + PIOS_USBHOOK_RegisterString(USB_STRING_DESC_LANG, (uint8_t *)&usb_lang_id, sizeof(usb_lang_id)); + PIOS_USBHOOK_RegisterString(USB_STRING_DESC_VENDOR, (uint8_t *)&usb_vendor_id, sizeof(usb_vendor_id)); + + return 0; +} diff --git a/flight/Bootloaders/PipXtreme/Makefile b/flight/Bootloaders/PipXtreme/Makefile index 9bad695f7..a411b6403 100644 --- a/flight/Bootloaders/PipXtreme/Makefile +++ b/flight/Bootloaders/PipXtreme/Makefile @@ -287,7 +287,7 @@ CSTANDARD = -std=gnu99 # Flags for C and C++ (arm-elf-gcc/arm-elf-g++) ifeq ($(DEBUG),YES) -CFLAGS = -DDEBUG +CFLAGS += -DDEBUG endif CFLAGS += -g$(DEBUGF) diff --git a/flight/Bootloaders/PipXtreme/inc/pios_config.h b/flight/Bootloaders/PipXtreme/inc/pios_config.h index 8084f9c0a..cec926057 100644 --- a/flight/Bootloaders/PipXtreme/inc/pios_config.h +++ b/flight/Bootloaders/PipXtreme/inc/pios_config.h @@ -40,7 +40,6 @@ #define PIOS_INCLUDE_USB_HID #define PIOS_INCLUDE_COM_MSG #define PIOS_INCLUDE_GPIO -//#define DEBUG_SSP /* Defaults for Logging */ #define LOG_FILENAME "PIOS.LOG" diff --git a/flight/Bootloaders/PipXtreme/inc/pios_usb_board_data.h b/flight/Bootloaders/PipXtreme/inc/pios_usb_board_data.h index fb6ced1f8..2e35f2fa4 100644 --- a/flight/Bootloaders/PipXtreme/inc/pios_usb_board_data.h +++ b/flight/Bootloaders/PipXtreme/inc/pios_usb_board_data.h @@ -37,33 +37,6 @@ #include "pios_usb_defs.h" /* struct usb_* */ -struct usb_board_config { - struct usb_configuration_desc config; - struct usb_interface_desc hid_if; - struct usb_hid_desc hid; - struct usb_endpoint_desc hid_in; - struct usb_endpoint_desc hid_out; -} __attribute__((packed)); - -extern const struct usb_device_desc PIOS_USB_BOARD_DeviceDescriptor; -extern const struct usb_board_config PIOS_USB_BOARD_Configuration; -extern const struct usb_string_langid PIOS_USB_BOARD_StringLangID; - -/* NOTE NOTE NOTE - * - * Care must be taken to ensure that the _actual_ contents of - * these arrays (in each board's pios_usb_board_data.c) is no - * smaller than the stated sizes here or these descriptors - * will end up with trailing zeros on the wire. - * - * The compiler will catch any time that these definitions are - * too small. - */ -extern const uint8_t PIOS_USB_BOARD_HidReportDescriptor[36]; -extern const uint8_t PIOS_USB_BOARD_StringVendorID[28]; -extern const uint8_t PIOS_USB_BOARD_StringProductID[20]; -extern uint8_t PIOS_USB_BOARD_StringSerial[52]; - #define PIOS_USB_BOARD_PRODUCT_ID USB_PRODUCT_ID_PIPXTREME #define PIOS_USB_BOARD_DEVICE_VER USB_OP_DEVICE_VER(USB_OP_BOARD_ID_PIPXTREME, USB_OP_BOARD_MODE_BL) diff --git a/flight/Bootloaders/PipXtreme/main.c b/flight/Bootloaders/PipXtreme/main.c index 4923f8fc1..602193f5c 100644 --- a/flight/Bootloaders/PipXtreme/main.c +++ b/flight/Bootloaders/PipXtreme/main.c @@ -72,10 +72,6 @@ void jump_to_app(); #define BLUE LED1 #define RED LED4 int main() { - /* NOTE: Do NOT modify the following start-up sequence */ - /* Any new initialization functions should be added in OpenPilotInit() */ - - /* Brings up System using CMSIS functions, enables the LEDs. */ PIOS_SYS_Init(); if (BSL_HOLD_STATE == 0) USB_connected = TRUE; @@ -182,7 +178,6 @@ void jump_to_app() { RCC_APB1PeriphResetCmd(0xffffffff, DISABLE); _SetCNTR(0); // clear interrupt mask _SetISTR(0); // clear all requests - JumpAddress = *(__IO uint32_t*) (bdinfo->fw_base + 4); Jump_To_Application = (pFunction) JumpAddress; /* Initialize user application's Stack Pointer */ diff --git a/flight/Bootloaders/PipXtreme/op_dfu.c b/flight/Bootloaders/PipXtreme/op_dfu.c index 0d5da248f..249e3d320 100644 --- a/flight/Bootloaders/PipXtreme/op_dfu.c +++ b/flight/Bootloaders/PipXtreme/op_dfu.c @@ -32,10 +32,6 @@ #include "pios_bl_helper.h" #include "pios_com_msg.h" #include -/* Private typedef -----------------------------------------------------------*/ -/* Private define ------------------------------------------------------------*/ -/* Private macro -------------------------------------------------------------*/ -/* Private variables ---------------------------------------------------------*/ //programmable devices Device devicesTable[10]; uint8_t numberOfDevices = 0; @@ -145,7 +141,7 @@ void processComand(uint8_t *xReceive_Buffer) { case EnterDFU: if (((DeviceState == BLidle) && (Data0 < numberOfDevices)) || (DeviceState == DFUidle)) { - if (Data0 > 0)//PORQUE??? + if (Data0 > 0) OPDfuIni(TRUE); DeviceState = DFUidle; currentProgrammingDestination = devicesTable[Data0].programmingType; @@ -202,7 +198,7 @@ void processComand(uint8_t *xReceive_Buffer) { } } if (result != 1) { - DeviceState = Last_operation_failed;//ok + DeviceState = Last_operation_failed; Aditionals = (uint32_t) Command; } else { @@ -253,11 +249,9 @@ void processComand(uint8_t *xReceive_Buffer) { ++Next_Packet; } else { - DeviceState = wrong_packet_received; Aditionals = Count; } - // FLASH_ProgramWord(MemLocations[TransferType]+4,++Next_Packet);//+Count,Data); } else { DeviceState = Last_operation_failed; Aditionals = (uint32_t) Command; @@ -331,7 +325,6 @@ void processComand(uint8_t *xReceive_Buffer) { DeviceState = too_few_packets; } } - break; case Download_Req: #ifdef DEBUG_SSP diff --git a/flight/Bootloaders/PipXtreme/pios_board.c b/flight/Bootloaders/PipXtreme/pios_board.c index 3c06d820b..a70537fcc 100644 --- a/flight/Bootloaders/PipXtreme/pios_board.c +++ b/flight/Bootloaders/PipXtreme/pios_board.c @@ -49,6 +49,9 @@ static const struct pios_usb_cfg pios_usb_main_cfg = { }, }; +#include "pios_usb_board_data_priv.h" +#include "pios_usb_desc_hid_only_priv.h" + #endif /* PIOS_INCLUDE_USB */ #if defined(PIOS_INCLUDE_USB_HID) @@ -88,6 +91,12 @@ void PIOS_Board_Init(void) { PIOS_GPIO_Init(); #if defined(PIOS_INCLUDE_USB) + /* Initialize board specific USB data */ + PIOS_USB_BOARD_DATA_Init(); + + /* Activate the HID-only USB configuration */ + PIOS_USB_DESC_HID_ONLY_Init(); + uint32_t pios_usb_id; if (PIOS_USB_Init(&pios_usb_id, &pios_usb_main_cfg)) { PIOS_Assert(0); diff --git a/flight/Bootloaders/PipXtreme/pios_usb_board_data.c b/flight/Bootloaders/PipXtreme/pios_usb_board_data.c index d54ca6565..3e41e5ac9 100644 --- a/flight/Bootloaders/PipXtreme/pios_usb_board_data.c +++ b/flight/Bootloaders/PipXtreme/pios_usb_board_data.c @@ -29,9 +29,11 @@ */ #include "pios_usb_board_data.h" /* struct usb_*, USB_* */ +#include "pios_sys.h" /* PIOS_SYS_SerialNumberGet */ +#include "pios_usbhook.h" /* PIOS_USBHOOK_* */ -const uint8_t PIOS_USB_BOARD_StringProductID[] = { - sizeof(PIOS_USB_BOARD_StringProductID), +static const uint8_t usb_product_id[20] = { + sizeof(usb_product_id), USB_DESC_TYPE_STRING, 'P', 0, 'i', 0, @@ -44,3 +46,74 @@ const uint8_t PIOS_USB_BOARD_StringProductID[] = { 'e', 0, }; +static uint8_t usb_serial_number[52] = { + sizeof(usb_serial_number), + USB_DESC_TYPE_STRING, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0 +}; + +static const struct usb_string_langid usb_lang_id = { + .bLength = sizeof(usb_lang_id), + .bDescriptorType = USB_DESC_TYPE_STRING, + .bLangID = htousbs(USB_LANGID_ENGLISH_UK), +}; + +static const uint8_t usb_vendor_id[28] = { + sizeof(usb_vendor_id), + USB_DESC_TYPE_STRING, + 'o', 0, + 'p', 0, + 'e', 0, + 'n', 0, + 'p', 0, + 'i', 0, + 'l', 0, + 'o', 0, + 't', 0, + '.', 0, + 'o', 0, + 'r', 0, + 'g', 0 +}; + +int32_t PIOS_USB_BOARD_DATA_Init(void) +{ + /* Load device serial number into serial number string */ + uint8_t sn[25]; + PIOS_SYS_SerialNumberGet((char *)sn); + for (uint8_t i = 0; sn[i] != '\0' && (2 * i) < usb_serial_number[0]; i++) { + usb_serial_number[2 + 2 * i] = sn[i]; + } + + PIOS_USBHOOK_RegisterString(USB_STRING_DESC_PRODUCT, (uint8_t *)&usb_product_id, sizeof(usb_product_id)); + PIOS_USBHOOK_RegisterString(USB_STRING_DESC_SERIAL, (uint8_t *)&usb_serial_number, sizeof(usb_serial_number)); + + PIOS_USBHOOK_RegisterString(USB_STRING_DESC_LANG, (uint8_t *)&usb_lang_id, sizeof(usb_lang_id)); + PIOS_USBHOOK_RegisterString(USB_STRING_DESC_VENDOR, (uint8_t *)&usb_vendor_id, sizeof(usb_vendor_id)); + + return 0; +} diff --git a/flight/CopterControl/Makefile b/flight/CopterControl/Makefile index 589251a4a..891e7f6c6 100644 --- a/flight/CopterControl/Makefile +++ b/flight/CopterControl/Makefile @@ -216,6 +216,7 @@ SRC += $(PIOSSTM32F10X)/pios_usb_hid_istr.c SRC += $(PIOSSTM32F10X)/pios_usb_hid_pwr.c SRC += $(OPSYSTEM)/pios_usb_board_data.c SRC += $(PIOSCOMMON)/pios_usb_desc_hid_cdc.c +SRC += $(PIOSCOMMON)/pios_usb_desc_hid_only.c ## PIOS Hardware (Common) SRC += $(PIOSCOMMON)/pios_crc.c diff --git a/flight/CopterControl/System/inc/pios_usb_board_data.h b/flight/CopterControl/System/inc/pios_usb_board_data.h index ee53c7455..9907a4934 100644 --- a/flight/CopterControl/System/inc/pios_usb_board_data.h +++ b/flight/CopterControl/System/inc/pios_usb_board_data.h @@ -37,44 +37,7 @@ #define PIOS_USB_BOARD_EP_NUM 4 -#include "pios_usb_defs.h" /* struct usb_* */ - -struct usb_board_config { - struct usb_configuration_desc config; - struct usb_interface_association_desc iad; - struct usb_interface_desc hid_if; - struct usb_hid_desc hid; - struct usb_endpoint_desc hid_in; - struct usb_endpoint_desc hid_out; - struct usb_interface_desc cdc_control_if; - struct usb_cdc_header_func_desc cdc_header; - struct usb_cdc_callmgmt_func_desc cdc_callmgmt; - struct usb_cdc_acm_func_desc cdc_acm; - struct usb_cdc_union_func_desc cdc_union; - struct usb_endpoint_desc cdc_mgmt_in; - struct usb_interface_desc cdc_data_if; - struct usb_endpoint_desc cdc_in; - struct usb_endpoint_desc cdc_out; -} __attribute__((packed)); - -extern const struct usb_device_desc PIOS_USB_BOARD_DeviceDescriptor; -extern const struct usb_board_config PIOS_USB_BOARD_Configuration; -extern const struct usb_string_langid PIOS_USB_BOARD_StringLangID; - -/* NOTE NOTE NOTE - * - * Care must be taken to ensure that the _actual_ contents of - * these arrays (in each board's pios_usb_board_data.c) is no - * smaller than the stated sizes here or these descriptors - * will end up with trailing zeros on the wire. - * - * The compiler will catch any time that these definitions are - * too small. - */ -extern const uint8_t PIOS_USB_BOARD_HidReportDescriptor[36]; -extern const uint8_t PIOS_USB_BOARD_StringVendorID[28]; -extern const uint8_t PIOS_USB_BOARD_StringProductID[28]; -extern uint8_t PIOS_USB_BOARD_StringSerial[52]; +#include "pios_usb_defs.h" /* USB_* macros */ #define PIOS_USB_BOARD_PRODUCT_ID USB_PRODUCT_ID_COPTERCONTROL #define PIOS_USB_BOARD_DEVICE_VER USB_OP_DEVICE_VER(USB_OP_BOARD_ID_COPTERCONTROL, USB_OP_BOARD_MODE_FW) diff --git a/flight/CopterControl/System/pios_board.c b/flight/CopterControl/System/pios_board.c index 3a4c3e89f..ffdde4759 100644 --- a/flight/CopterControl/System/pios_board.c +++ b/flight/CopterControl/System/pios_board.c @@ -998,6 +998,11 @@ static const struct pios_usb_cfg pios_usb_main_cfg = { }, }, }; + +#include "pios_usb_board_data_priv.h" +#include "pios_usb_desc_hid_cdc_priv.h" +#include "pios_usb_desc_hid_only_priv.h" + #endif /* PIOS_INCLUDE_USB */ #if defined(PIOS_INCLUDE_USB_HID) @@ -1090,14 +1095,50 @@ void PIOS_Board_Init(void) { PIOS_TIM_InitClock(&tim_4_cfg); #if defined(PIOS_INCLUDE_USB) + /* Initialize board specific USB data */ + PIOS_USB_BOARD_DATA_Init(); + + /* Flags to determine if various USB interfaces are advertised */ + bool usb_hid_present = false; + bool usb_cdc_present = false; + + uint8_t hwsettings_usb_devicetype; + HwSettingsUSB_DeviceTypeGet(&hwsettings_usb_devicetype); + + switch (hwsettings_usb_devicetype) { + case HWSETTINGS_USB_DEVICETYPE_HIDONLY: + if (PIOS_USB_DESC_HID_ONLY_Init()) { + PIOS_Assert(0); + } + usb_hid_present = true; + break; + case HWSETTINGS_USB_DEVICETYPE_HIDVCP: + if (PIOS_USB_DESC_HID_CDC_Init()) { + PIOS_Assert(0); + } + usb_hid_present = true; + usb_cdc_present = true; + break; + case HWSETTINGS_USB_DEVICETYPE_VCPONLY: + break; + default: + PIOS_Assert(0); + } + uint32_t pios_usb_id; PIOS_USB_Init(&pios_usb_id, &pios_usb_main_cfg); #if defined(PIOS_INCLUDE_USB_CDC) - /* Configure the USB VCP port */ + uint8_t hwsettings_usb_vcpport; + /* Configure the USB VCP port */ HwSettingsUSB_VCPPortGet(&hwsettings_usb_vcpport); + if (!usb_cdc_present) { + /* Force VCP port function to disabled if we haven't advertised VCP in our USB descriptor */ + hwsettings_usb_vcpport = HWSETTINGS_USB_VCPPORT_DISABLED; + } + switch (hwsettings_usb_vcpport) { case HWSETTINGS_USB_VCPPORT_DISABLED: break; @@ -1136,7 +1177,6 @@ void PIOS_Board_Init(void) { tx_buffer, PIOS_COM_BRIDGE_TX_BUF_LEN)) { PIOS_Assert(0); } - } #endif /* PIOS_INCLUDE_COM */ break; @@ -1148,6 +1188,11 @@ void PIOS_Board_Init(void) { uint8_t hwsettings_usb_hidport; HwSettingsUSB_HIDPortGet(&hwsettings_usb_hidport); + if (!usb_hid_present) { + /* Force HID port function to disabled if we haven't advertised HID in our USB descriptor */ + hwsettings_usb_hidport = HWSETTINGS_USB_HIDPORT_DISABLED; + } + switch (hwsettings_usb_hidport) { case HWSETTINGS_USB_HIDPORT_DISABLED: break; diff --git a/flight/CopterControl/System/pios_usb_board_data.c b/flight/CopterControl/System/pios_usb_board_data.c index b39fcbec3..4f02ce424 100644 --- a/flight/CopterControl/System/pios_usb_board_data.c +++ b/flight/CopterControl/System/pios_usb_board_data.c @@ -29,9 +29,11 @@ */ #include "pios_usb_board_data.h" /* struct usb_*, USB_* */ +#include "pios_sys.h" /* PIOS_SYS_SerialNumberGet */ +#include "pios_usbhook.h" /* PIOS_USBHOOK_* */ -const uint8_t PIOS_USB_BOARD_StringProductID[] = { - sizeof(PIOS_USB_BOARD_StringProductID), +static const uint8_t usb_product_id[28] = { + sizeof(usb_product_id), USB_DESC_TYPE_STRING, 'C', 0, 'o', 0, @@ -48,3 +50,74 @@ const uint8_t PIOS_USB_BOARD_StringProductID[] = { 'l', 0, }; +static uint8_t usb_serial_number[52] = { + sizeof(usb_serial_number), + USB_DESC_TYPE_STRING, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0 +}; + +static const struct usb_string_langid usb_lang_id = { + .bLength = sizeof(usb_lang_id), + .bDescriptorType = USB_DESC_TYPE_STRING, + .bLangID = htousbs(USB_LANGID_ENGLISH_UK), +}; + +static const uint8_t usb_vendor_id[28] = { + sizeof(usb_vendor_id), + USB_DESC_TYPE_STRING, + 'o', 0, + 'p', 0, + 'e', 0, + 'n', 0, + 'p', 0, + 'i', 0, + 'l', 0, + 'o', 0, + 't', 0, + '.', 0, + 'o', 0, + 'r', 0, + 'g', 0 +}; + +int32_t PIOS_USB_BOARD_DATA_Init(void) +{ + /* Load device serial number into serial number string */ + uint8_t sn[25]; + PIOS_SYS_SerialNumberGet((char *)sn); + for (uint8_t i = 0; sn[i] != '\0' && (2 * i) < usb_serial_number[0]; i++) { + usb_serial_number[2 + 2 * i] = sn[i]; + } + + PIOS_USBHOOK_RegisterString(USB_STRING_DESC_PRODUCT, (uint8_t *)&usb_product_id, sizeof(usb_product_id)); + PIOS_USBHOOK_RegisterString(USB_STRING_DESC_SERIAL, (uint8_t *)&usb_serial_number, sizeof(usb_serial_number)); + + PIOS_USBHOOK_RegisterString(USB_STRING_DESC_LANG, (uint8_t *)&usb_lang_id, sizeof(usb_lang_id)); + PIOS_USBHOOK_RegisterString(USB_STRING_DESC_VENDOR, (uint8_t *)&usb_vendor_id, sizeof(usb_vendor_id)); + + return 0; +} diff --git a/flight/OpenPilot/Makefile b/flight/OpenPilot/Makefile index 8f9d9bef9..940c1f117 100644 --- a/flight/OpenPilot/Makefile +++ b/flight/OpenPilot/Makefile @@ -183,9 +183,11 @@ SRC += $(PIOSSTM32F10X)/pios_wdg.c SRC += $(PIOSSTM32F10X)/pios_usb.c SRC += $(PIOSSTM32F10X)/pios_usbhook.c SRC += $(PIOSSTM32F10X)/pios_usb_hid.c +SRC += $(PIOSSTM32F10X)/pios_usb_cdc.c SRC += $(PIOSSTM32F10X)/pios_usb_hid_istr.c SRC += $(PIOSSTM32F10X)/pios_usb_hid_pwr.c SRC += $(OPSYSTEM)/pios_usb_board_data.c +SRC += $(PIOSCOMMON)/pios_usb_desc_hid_cdc.c SRC += $(PIOSCOMMON)/pios_usb_desc_hid_only.c ## PIOS Hardware (Common) diff --git a/flight/OpenPilot/System/inc/pios_config.h b/flight/OpenPilot/System/inc/pios_config.h index 3ec902e8e..ce0484a8d 100644 --- a/flight/OpenPilot/System/inc/pios_config.h +++ b/flight/OpenPilot/System/inc/pios_config.h @@ -57,6 +57,7 @@ #define PIOS_INCLUDE_USART #define PIOS_INCLUDE_USB #define PIOS_INCLUDE_USB_HID +#define PIOS_INCLUDE_USB_CDC #define PIOS_INCLUDE_BMP085 //#define PIOS_INCLUDE_HCSR04 #define PIOS_INCLUDE_OPAHRS diff --git a/flight/OpenPilot/System/inc/pios_usb_board_data.h b/flight/OpenPilot/System/inc/pios_usb_board_data.h index e2f079bc2..08fc3423b 100644 --- a/flight/OpenPilot/System/inc/pios_usb_board_data.h +++ b/flight/OpenPilot/System/inc/pios_usb_board_data.h @@ -39,43 +39,6 @@ #include "pios_usb_defs.h" /* struct usb_* */ -struct usb_board_config { - struct usb_configuration_desc config; - struct usb_interface_association_desc iad; - struct usb_interface_desc hid_if; - struct usb_hid_desc hid; - struct usb_endpoint_desc hid_in; - struct usb_endpoint_desc hid_out; - struct usb_interface_desc cdc_control_if; - struct usb_cdc_header_func_desc cdc_header; - struct usb_cdc_callmgmt_func_desc cdc_callmgmt; - struct usb_cdc_acm_func_desc cdc_acm; - struct usb_cdc_union_func_desc cdc_union; - struct usb_endpoint_desc cdc_mgmt_in; - struct usb_interface_desc cdc_data_if; - struct usb_endpoint_desc cdc_in; - struct usb_endpoint_desc cdc_out; -} __attribute__((packed)); - -extern const struct usb_device_desc PIOS_USB_BOARD_DeviceDescriptor; -extern const struct usb_board_config PIOS_USB_BOARD_Configuration; -extern const struct usb_string_langid PIOS_USB_BOARD_StringLangID; - -/* NOTE NOTE NOTE - * - * Care must be taken to ensure that the _actual_ contents of - * these arrays (in each board's pios_usb_board_data.c) is no - * smaller than the stated sizes here or these descriptors - * will end up with trailing zeros on the wire. - * - * The compiler will catch any time that these definitions are - * too small. - */ -extern const uint8_t PIOS_USB_BOARD_HidReportDescriptor[36]; -extern const uint8_t PIOS_USB_BOARD_StringVendorID[28]; -extern const uint8_t PIOS_USB_BOARD_StringProductID[20]; -extern uint8_t PIOS_USB_BOARD_StringSerial[52]; - #define PIOS_USB_BOARD_PRODUCT_ID USB_PRODUCT_ID_OPENPILOT_MAIN #define PIOS_USB_BOARD_DEVICE_VER USB_OP_DEVICE_VER(USB_OP_BOARD_ID_OPENPILOT_MAIN, USB_OP_BOARD_MODE_FW) diff --git a/flight/OpenPilot/System/pios_board.c b/flight/OpenPilot/System/pios_board.c index 314a8e24d..8dbb5eee2 100644 --- a/flight/OpenPilot/System/pios_board.c +++ b/flight/OpenPilot/System/pios_board.c @@ -611,6 +611,9 @@ static const struct pios_dsm_cfg pios_dsm_cfg = { #define PIOS_COM_TELEM_USB_RX_BUF_LEN 192 #define PIOS_COM_TELEM_USB_TX_BUF_LEN 192 +#define PIOS_COM_BRIDGE_RX_BUF_LEN 65 +#define PIOS_COM_BRIDGE_TX_BUF_LEN 12 + #endif /* PIOS_INCLUDE_COM */ /** @@ -1054,6 +1057,10 @@ static const struct pios_usb_cfg pios_usb_main_cfg = { }, }, }; + +#include "pios_usb_board_data_priv.h" +#include "pios_usb_desc_hid_cdc_priv.h" +#include "pios_usb_desc_hid_only_priv.h" #endif /* PIOS_INCLUDE_USB */ #if defined(PIOS_INCLUDE_USB_HID) @@ -1067,8 +1074,22 @@ const struct pios_usb_hid_cfg pios_usb_hid_cfg = { #endif /* PIOS_INCLUDE_USB_HID */ +#if defined(PIOS_INCLUDE_USB_CDC) +#include + +const struct pios_usb_cdc_cfg pios_usb_cdc_cfg = { + .ctrl_if = 1, + .ctrl_tx_ep = 2, + + .data_if = 2, + .data_rx_ep = 3, + .data_tx_ep = 3, +}; +#endif /* PIOS_INCLUDE_USB_CDC */ + uint32_t pios_com_telem_rf_id; uint32_t pios_com_telem_usb_id; +uint32_t pios_com_vcp_id; uint32_t pios_com_gps_id; uint32_t pios_com_aux_id; uint32_t pios_com_dsm_id; @@ -1109,14 +1130,27 @@ void PIOS_Board_Init(void) { HwSettingsInitialize(); + PIOS_WDG_Init(); + + /* Initialize the alarms library */ + AlarmsInitialize(); + + PIOS_IAP_Init(); + uint16_t boot_count = PIOS_IAP_ReadBootCount(); + if (boot_count < 3) { + PIOS_IAP_WriteBootCount(++boot_count); + AlarmsClear(SYSTEMALARMS_ALARM_BOOTFAULT); + } else { + /* Too many failed boot attempts, force hwsettings to defaults */ + HwSettingsSetDefaults(HwSettingsHandle(), 0); + AlarmsSet(SYSTEMALARMS_ALARM_BOOTFAULT, SYSTEMALARMS_ALARM_CRITICAL); + } + #if defined(PIOS_INCLUDE_RTC) /* Initialize the real-time clock and its associated tick */ PIOS_RTC_Init(&pios_rtc_main_cfg); #endif - /* Initialize the alarms library */ - AlarmsInitialize(); - /* Initialize the task monitor library */ TaskMonitorInitialize(); @@ -1124,7 +1158,6 @@ void PIOS_Board_Init(void) { PIOS_TIM_InitClock(&tim_1_cfg); PIOS_TIM_InitClock(&tim_3_cfg); PIOS_TIM_InitClock(&tim_5_cfg); - PIOS_TIM_InitClock(&tim_4_cfg); PIOS_TIM_InitClock(&tim_8_cfg); @@ -1139,6 +1172,133 @@ void PIOS_Board_Init(void) { /* Bind the AHRS comms layer to the AHRS SPI link */ AhrsConnect(pios_spi_ahrs_id); +#if defined(PIOS_INCLUDE_USB) + /* Initialize board specific USB data */ + PIOS_USB_BOARD_DATA_Init(); + + /* Flags to determine if various USB interfaces are advertised */ + bool usb_hid_present = false; + bool usb_cdc_present = false; + + uint8_t hwsettings_usb_devicetype; + HwSettingsUSB_DeviceTypeGet(&hwsettings_usb_devicetype); + + switch (hwsettings_usb_devicetype) { + case HWSETTINGS_USB_DEVICETYPE_HIDONLY: + if (PIOS_USB_DESC_HID_ONLY_Init()) { + PIOS_Assert(0); + } + usb_hid_present = true; + break; + case HWSETTINGS_USB_DEVICETYPE_HIDVCP: + if (PIOS_USB_DESC_HID_CDC_Init()) { + PIOS_Assert(0); + } + usb_hid_present = true; + usb_cdc_present = true; + break; + case HWSETTINGS_USB_DEVICETYPE_VCPONLY: + break; + default: + PIOS_Assert(0); + } + + uint32_t pios_usb_id; + PIOS_USB_Init(&pios_usb_id, &pios_usb_main_cfg); + +#if defined(PIOS_INCLUDE_USB_CDC) + /* Configure the USB VCP port */ + uint8_t hwsettings_usb_vcpport; + HwSettingsUSB_VCPPortGet(&hwsettings_usb_vcpport); + + if (!usb_cdc_present) { + /* Force VCP port function to disabled if we haven't advertised VCP in our USB descriptor */ + hwsettings_usb_vcpport = HWSETTINGS_USB_VCPPORT_DISABLED; + } + + switch (hwsettings_usb_vcpport) { + case HWSETTINGS_USB_VCPPORT_DISABLED: + break; + case HWSETTINGS_USB_VCPPORT_USBTELEMETRY: +#if defined(PIOS_INCLUDE_COM) + { + uint32_t pios_usb_cdc_id; + if (PIOS_USB_CDC_Init(&pios_usb_cdc_id, &pios_usb_cdc_cfg, pios_usb_id)) { + PIOS_Assert(0); + } + uint8_t * rx_buffer = (uint8_t *) pvPortMalloc(PIOS_COM_TELEM_USB_RX_BUF_LEN); + uint8_t * tx_buffer = (uint8_t *) pvPortMalloc(PIOS_COM_TELEM_USB_TX_BUF_LEN); + PIOS_Assert(rx_buffer); + PIOS_Assert(tx_buffer); + if (PIOS_COM_Init(&pios_com_telem_usb_id, &pios_usb_cdc_com_driver, pios_usb_cdc_id, + rx_buffer, PIOS_COM_TELEM_USB_RX_BUF_LEN, + tx_buffer, PIOS_COM_TELEM_USB_TX_BUF_LEN)) { + PIOS_Assert(0); + } + } +#endif /* PIOS_INCLUDE_COM */ + break; + case HWSETTINGS_USB_VCPPORT_COMBRIDGE: +#if defined(PIOS_INCLUDE_COM) + { + uint32_t pios_usb_cdc_id; + if (PIOS_USB_CDC_Init(&pios_usb_cdc_id, &pios_usb_cdc_cfg, pios_usb_id)) { + PIOS_Assert(0); + } + uint8_t * rx_buffer = (uint8_t *) pvPortMalloc(PIOS_COM_BRIDGE_RX_BUF_LEN); + uint8_t * tx_buffer = (uint8_t *) pvPortMalloc(PIOS_COM_BRIDGE_TX_BUF_LEN); + PIOS_Assert(rx_buffer); + PIOS_Assert(tx_buffer); + if (PIOS_COM_Init(&pios_com_vcp_id, &pios_usb_cdc_com_driver, pios_usb_cdc_id, + rx_buffer, PIOS_COM_BRIDGE_RX_BUF_LEN, + tx_buffer, PIOS_COM_BRIDGE_TX_BUF_LEN)) { + PIOS_Assert(0); + } + + } +#endif /* PIOS_INCLUDE_COM */ + break; + } +#endif /* PIOS_INCLUDE_USB_CDC */ + +#if defined(PIOS_INCLUDE_USB_HID) + /* Configure the usb HID port */ + uint8_t hwsettings_usb_hidport; + HwSettingsUSB_HIDPortGet(&hwsettings_usb_hidport); + + if (!usb_hid_present) { + /* Force HID port function to disabled if we haven't advertised HID in our USB descriptor */ + hwsettings_usb_hidport = HWSETTINGS_USB_HIDPORT_DISABLED; + } + + switch (hwsettings_usb_hidport) { + case HWSETTINGS_USB_HIDPORT_DISABLED: + break; + case HWSETTINGS_USB_HIDPORT_USBTELEMETRY: +#if defined(PIOS_INCLUDE_COM) + { + uint32_t pios_usb_hid_id; + if (PIOS_USB_HID_Init(&pios_usb_hid_id, &pios_usb_hid_cfg, pios_usb_id)) { + PIOS_Assert(0); + } + uint8_t * rx_buffer = (uint8_t *) pvPortMalloc(PIOS_COM_TELEM_USB_RX_BUF_LEN); + uint8_t * tx_buffer = (uint8_t *) pvPortMalloc(PIOS_COM_TELEM_USB_TX_BUF_LEN); + PIOS_Assert(rx_buffer); + PIOS_Assert(tx_buffer); + if (PIOS_COM_Init(&pios_com_telem_usb_id, &pios_usb_hid_com_driver, pios_usb_hid_id, + rx_buffer, PIOS_COM_TELEM_USB_RX_BUF_LEN, + tx_buffer, PIOS_COM_TELEM_USB_TX_BUF_LEN)) { + PIOS_Assert(0); + } + } +#endif /* PIOS_INCLUDE_COM */ + break; + } + +#endif /* PIOS_INCLUDE_USB_HID */ + +#endif /* PIOS_INCLUDE_USB */ + /* Configure the main IO port */ uint8_t hwsettings_op_mainport; HwSettingsOP_MainPortGet(&hwsettings_op_mainport); @@ -1285,36 +1445,11 @@ void PIOS_Board_Init(void) { break; } -#if defined(PIOS_INCLUDE_USB) - uint32_t pios_usb_id; - if (PIOS_USB_Init(&pios_usb_id, &pios_usb_main_cfg)) { - PIOS_Assert(0); - } -#if defined(PIOS_INCLUDE_USB_HID) && defined(PIOS_INCLUDE_COM) - uint32_t pios_usb_hid_id; - if (PIOS_USB_HID_Init(&pios_usb_hid_id, &pios_usb_hid_cfg, pios_usb_id)) { - PIOS_Assert(0); - } - uint8_t * rx_buffer = (uint8_t *) pvPortMalloc(PIOS_COM_TELEM_USB_RX_BUF_LEN); - uint8_t * tx_buffer = (uint8_t *) pvPortMalloc(PIOS_COM_TELEM_USB_TX_BUF_LEN); - PIOS_Assert(rx_buffer); - PIOS_Assert(tx_buffer); - if (PIOS_COM_Init(&pios_com_telem_usb_id, &pios_usb_hid_com_driver, pios_usb_hid_id, - rx_buffer, PIOS_COM_TELEM_USB_RX_BUF_LEN, - tx_buffer, PIOS_COM_TELEM_USB_TX_BUF_LEN)) { - PIOS_Assert(0); - } -#endif /* PIOS_INCLUDE_USB_HID && PIOS_INCLUDE_COM */ - -#endif /* PIOS_INCLUDE_USB */ - #if defined(PIOS_INCLUDE_I2C) if (PIOS_I2C_Init(&pios_i2c_main_adapter_id, &pios_i2c_main_adapter_cfg)) { PIOS_Assert(0); } #endif /* PIOS_INCLUDE_I2C */ - PIOS_IAP_Init(); - PIOS_WDG_Init(); } /** diff --git a/flight/OpenPilot/System/pios_usb_board_data.c b/flight/OpenPilot/System/pios_usb_board_data.c index 9492d316e..a6fa67abe 100644 --- a/flight/OpenPilot/System/pios_usb_board_data.c +++ b/flight/OpenPilot/System/pios_usb_board_data.c @@ -29,9 +29,11 @@ */ #include "pios_usb_board_data.h" /* struct usb_*, USB_* */ +#include "pios_sys.h" /* PIOS_SYS_SerialNumberGet */ +#include "pios_usbhook.h" /* PIOS_USBHOOK_* */ -const uint8_t PIOS_USB_BOARD_StringProductID[] = { - sizeof(PIOS_USB_BOARD_StringProductID), +static const uint8_t usb_product_id[20] = { + sizeof(usb_product_id), USB_DESC_TYPE_STRING, 'O', 0, 'p', 0, @@ -43,3 +45,76 @@ const uint8_t PIOS_USB_BOARD_StringProductID[] = { 'o', 0, 't', 0, }; + +static uint8_t usb_serial_number[52] = { + sizeof(usb_serial_number), + USB_DESC_TYPE_STRING, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0 +}; + +static const struct usb_string_langid usb_lang_id = { + .bLength = sizeof(usb_lang_id), + .bDescriptorType = USB_DESC_TYPE_STRING, + .bLangID = htousbs(USB_LANGID_ENGLISH_UK), +}; + +static const uint8_t usb_vendor_id[28] = { + sizeof(usb_vendor_id), + USB_DESC_TYPE_STRING, + 'o', 0, + 'p', 0, + 'e', 0, + 'n', 0, + 'p', 0, + 'i', 0, + 'l', 0, + 'o', 0, + 't', 0, + '.', 0, + 'o', 0, + 'r', 0, + 'g', 0 +}; + +int32_t PIOS_USB_BOARD_DATA_Init(void) +{ + /* Load device serial number into serial number string */ + uint8_t sn[25]; + PIOS_SYS_SerialNumberGet((char *)sn); + for (uint8_t i = 0; sn[i] != '\0' && (2 * i) < usb_serial_number[0]; i++) { + usb_serial_number[2 + 2 * i] = sn[i]; + } + + PIOS_USBHOOK_RegisterString(USB_STRING_DESC_PRODUCT, (uint8_t *)&usb_product_id, sizeof(usb_product_id)); + PIOS_USBHOOK_RegisterString(USB_STRING_DESC_SERIAL, (uint8_t *)&usb_serial_number, sizeof(usb_serial_number)); + + PIOS_USBHOOK_RegisterString(USB_STRING_DESC_LANG, (uint8_t *)&usb_lang_id, sizeof(usb_lang_id)); + PIOS_USBHOOK_RegisterString(USB_STRING_DESC_VENDOR, (uint8_t *)&usb_vendor_id, sizeof(usb_vendor_id)); + + return 0; +} + diff --git a/flight/PiOS/Common/pios_usb_desc_hid_cdc.c b/flight/PiOS/Common/pios_usb_desc_hid_cdc.c index 3d5748163..cd4e8a1da 100644 --- a/flight/PiOS/Common/pios_usb_desc_hid_cdc.c +++ b/flight/PiOS/Common/pios_usb_desc_hid_cdc.c @@ -28,9 +28,12 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include "pios_usb_board_data.h" /* struct usb_*, USB_* */ +#include "pios_usb_desc_hid_cdc_priv.h" /* exported API */ +#include "pios_usb_defs.h" /* struct usb_*, USB_* */ +#include "pios_usb_board_data.h" /* PIOS_USB_BOARD_* */ +#include "pios_usbhook.h" /* PIOS_USBHOOK_Register* */ -const struct usb_device_desc PIOS_USB_BOARD_DeviceDescriptor = { +static const struct usb_device_desc device_desc = { .bLength = sizeof(struct usb_device_desc), .bDescriptorType = USB_DESC_TYPE_DEVICE, .bcdUSB = htousbs(0x0200), @@ -41,17 +44,79 @@ const struct usb_device_desc PIOS_USB_BOARD_DeviceDescriptor = { .idVendor = htousbs(USB_VENDOR_ID_OPENPILOT), .idProduct = htousbs(PIOS_USB_BOARD_PRODUCT_ID), .bcdDevice = htousbs(PIOS_USB_BOARD_DEVICE_VER), - .iManufacturer = 1, - .iProduct = 2, - .iSerialNumber = 3, + .iManufacturer = USB_STRING_DESC_VENDOR, + .iProduct = USB_STRING_DESC_PRODUCT, + .iSerialNumber = USB_STRING_DESC_SERIAL, .bNumConfigurations = 1, }; -const struct usb_board_config PIOS_USB_BOARD_Configuration = { +static const uint8_t hid_report_desc[36] = { + HID_GLOBAL_ITEM_2 (HID_TAG_GLOBAL_USAGE_PAGE), + 0x9C, 0xFF, /* Usage Page 0xFF9C (Vendor Defined) */ + HID_LOCAL_ITEM_1 (HID_TAG_LOCAL_USAGE), + 0x01, /* Usage ID 0x0001 (0x01-0x1F uaually for top-level collections) */ + + HID_MAIN_ITEM_1 (HID_TAG_MAIN_COLLECTION), + 0x01, /* Application */ + + /* Device -> Host emulated serial channel */ + HID_GLOBAL_ITEM_1 (HID_TAG_GLOBAL_REPORT_ID), + 0x01, /* OpenPilot emulated serial channel (Device -> Host) */ + HID_LOCAL_ITEM_1 (HID_TAG_LOCAL_USAGE), + 0x02, + HID_GLOBAL_ITEM_1 (HID_TAG_GLOBAL_LOGICAL_MIN), + 0x00, /* Values range from min = 0x00 */ + HID_GLOBAL_ITEM_1 (HID_TAG_GLOBAL_LOGICAL_MAX), + 0xFF, /* Values range to max = 0xFF */ + HID_GLOBAL_ITEM_1 (HID_TAG_GLOBAL_REPORT_SIZE), + 0x08, /* 8 bits wide */ + HID_GLOBAL_ITEM_1 (HID_TAG_GLOBAL_REPORT_CNT), + PIOS_USB_BOARD_HID_DATA_LENGTH-1, /* Need to leave room for a report ID */ + HID_MAIN_ITEM_1 (HID_TAG_MAIN_INPUT), + 0x03, /* Variable, Constant (read-only) */ + + /* Host -> Host emulated serial channel */ + HID_GLOBAL_ITEM_1 (HID_TAG_GLOBAL_REPORT_ID), + 0x02, /* OpenPilot emulated Serial Channel (Host -> Device) */ + HID_LOCAL_ITEM_1 (HID_TAG_LOCAL_USAGE), + 0x02, + HID_GLOBAL_ITEM_1 (HID_TAG_GLOBAL_LOGICAL_MIN), + 0x00, /* Values range from min = 0x00 */ + HID_GLOBAL_ITEM_1 (HID_TAG_GLOBAL_LOGICAL_MAX), + 0xFF, /* Values range to max = 0xFF */ + HID_GLOBAL_ITEM_1 (HID_TAG_GLOBAL_REPORT_SIZE), + 0x08, /* 8 bits wide */ + HID_GLOBAL_ITEM_1 (HID_TAG_GLOBAL_REPORT_CNT), + PIOS_USB_BOARD_HID_DATA_LENGTH-1, /* Need to leave room for a report ID */ + HID_MAIN_ITEM_1 (HID_TAG_MAIN_OUTPUT), + 0x82, /* Volatile, Variable */ + + HID_MAIN_ITEM_0 (HID_TAG_MAIN_ENDCOLLECTION), +}; + +struct usb_config_hid_cdc { + struct usb_configuration_desc config; + struct usb_interface_association_desc iad; + struct usb_interface_desc hid_if; + struct usb_hid_desc hid; + struct usb_endpoint_desc hid_in; + struct usb_endpoint_desc hid_out; + struct usb_interface_desc cdc_control_if; + struct usb_cdc_header_func_desc cdc_header; + struct usb_cdc_callmgmt_func_desc cdc_callmgmt; + struct usb_cdc_acm_func_desc cdc_acm; + struct usb_cdc_union_func_desc cdc_union; + struct usb_endpoint_desc cdc_mgmt_in; + struct usb_interface_desc cdc_data_if; + struct usb_endpoint_desc cdc_in; + struct usb_endpoint_desc cdc_out; +} __attribute__((packed)); + +static const struct usb_config_hid_cdc config_hid_cdc = { .config = { .bLength = sizeof(struct usb_configuration_desc), .bDescriptorType = USB_DESC_TYPE_CONFIGURATION, - .wTotalLength = htousbs(sizeof(struct usb_board_config)), + .wTotalLength = htousbs(sizeof(struct usb_config_hid_cdc)), .bNumInterfaces = 3, .bConfigurationValue = 1, .iConfiguration = 0, @@ -86,7 +151,7 @@ const struct usb_board_config PIOS_USB_BOARD_Configuration = { .bCountryCode = 0, .bNumDescriptors = 1, .bClassDescriptorType = USB_DESC_TYPE_REPORT, - .wItemLength = htousbs(sizeof(PIOS_USB_BOARD_HidReportDescriptor)), + .wItemLength = htousbs(sizeof(hid_report_desc)), }, .hid_in = { .bLength = sizeof(struct usb_endpoint_desc), @@ -178,100 +243,14 @@ const struct usb_board_config PIOS_USB_BOARD_Configuration = { }, }; -const uint8_t PIOS_USB_BOARD_HidReportDescriptor[] = { - HID_GLOBAL_ITEM_2 (HID_TAG_GLOBAL_USAGE_PAGE), - 0x9C, 0xFF, /* Usage Page 0xFF9C (Vendor Defined) */ - HID_LOCAL_ITEM_1 (HID_TAG_LOCAL_USAGE), - 0x01, /* Usage ID 0x0001 (0x01-0x1F uaually for top-level collections) */ +int32_t PIOS_USB_DESC_HID_CDC_Init(void) +{ + PIOS_USBHOOK_RegisterConfig(1, (uint8_t *)&config_hid_cdc, sizeof(config_hid_cdc)); - HID_MAIN_ITEM_1 (HID_TAG_MAIN_COLLECTION), - 0x01, /* Application */ + PIOS_USBHOOK_RegisterDevice((uint8_t *)&device_desc, sizeof(device_desc)); - /* Device -> Host emulated serial channel */ - HID_GLOBAL_ITEM_1 (HID_TAG_GLOBAL_REPORT_ID), - 0x01, /* OpenPilot emulated serial channel (Device -> Host) */ - HID_LOCAL_ITEM_1 (HID_TAG_LOCAL_USAGE), - 0x02, - HID_GLOBAL_ITEM_1 (HID_TAG_GLOBAL_LOGICAL_MIN), - 0x00, /* Values range from min = 0x00 */ - HID_GLOBAL_ITEM_1 (HID_TAG_GLOBAL_LOGICAL_MAX), - 0xFF, /* Values range to max = 0xFF */ - HID_GLOBAL_ITEM_1 (HID_TAG_GLOBAL_REPORT_SIZE), - 0x08, /* 8 bits wide */ - HID_GLOBAL_ITEM_1 (HID_TAG_GLOBAL_REPORT_CNT), - PIOS_USB_BOARD_HID_DATA_LENGTH-1, /* Need to leave room for a report ID */ - HID_MAIN_ITEM_1 (HID_TAG_MAIN_INPUT), - 0x03, /* Variable, Constant (read-only) */ + PIOS_USBHOOK_RegisterHidInterface((uint8_t *)&(config_hid_cdc.hid_if), sizeof(config_hid_cdc.hid_if)); + PIOS_USBHOOK_RegisterHidReport((uint8_t *)hid_report_desc, sizeof(hid_report_desc)); - /* Host -> Host emulated serial channel */ - HID_GLOBAL_ITEM_1 (HID_TAG_GLOBAL_REPORT_ID), - 0x02, /* OpenPilot emulated Serial Channel (Host -> Device) */ - HID_LOCAL_ITEM_1 (HID_TAG_LOCAL_USAGE), - 0x02, - HID_GLOBAL_ITEM_1 (HID_TAG_GLOBAL_LOGICAL_MIN), - 0x00, /* Values range from min = 0x00 */ - HID_GLOBAL_ITEM_1 (HID_TAG_GLOBAL_LOGICAL_MAX), - 0xFF, /* Values range to max = 0xFF */ - HID_GLOBAL_ITEM_1 (HID_TAG_GLOBAL_REPORT_SIZE), - 0x08, /* 8 bits wide */ - HID_GLOBAL_ITEM_1 (HID_TAG_GLOBAL_REPORT_CNT), - PIOS_USB_BOARD_HID_DATA_LENGTH-1, /* Need to leave room for a report ID */ - HID_MAIN_ITEM_1 (HID_TAG_MAIN_OUTPUT), - 0x82, /* Volatile, Variable */ - - HID_MAIN_ITEM_0 (HID_TAG_MAIN_ENDCOLLECTION), -}; - -const struct usb_string_langid PIOS_USB_BOARD_StringLangID = { - .bLength = sizeof(PIOS_USB_BOARD_StringLangID), - .bDescriptorType = USB_DESC_TYPE_STRING, - .bLangID = htousbs(USB_LANGID_ENGLISH_UK), -}; - -const uint8_t PIOS_USB_BOARD_StringVendorID[] = { - sizeof(PIOS_USB_BOARD_StringVendorID), - USB_DESC_TYPE_STRING, - 'o', 0, - 'p', 0, - 'e', 0, - 'n', 0, - 'p', 0, - 'i', 0, - 'l', 0, - 'o', 0, - 't', 0, - '.', 0, - 'o', 0, - 'r', 0, - 'g', 0 -}; - -uint8_t PIOS_USB_BOARD_StringSerial[] = { - sizeof(PIOS_USB_BOARD_StringSerial), - USB_DESC_TYPE_STRING, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0 -}; + return 0; +} diff --git a/flight/PiOS/Common/pios_usb_desc_hid_only.c b/flight/PiOS/Common/pios_usb_desc_hid_only.c index 913c18b16..168efe841 100644 --- a/flight/PiOS/Common/pios_usb_desc_hid_only.c +++ b/flight/PiOS/Common/pios_usb_desc_hid_only.c @@ -28,9 +28,12 @@ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include "pios_usb_board_data.h" /* struct usb_*, USB_* */ +#include "pios_usb_desc_hid_only_priv.h" /* exported API */ +#include "pios_usb_defs.h" /* struct usb_*, USB_* */ +#include "pios_usb_board_data.h" /* PIOS_USB_BOARD_* */ +#include "pios_usbhook.h" /* PIOS_USBHOOK_Register* */ -const struct usb_device_desc PIOS_USB_BOARD_DeviceDescriptor = { +static const struct usb_device_desc device_desc = { .bLength = sizeof(struct usb_device_desc), .bDescriptorType = USB_DESC_TYPE_DEVICE, .bcdUSB = htousbs(0x0200), @@ -47,56 +50,7 @@ const struct usb_device_desc PIOS_USB_BOARD_DeviceDescriptor = { .bNumConfigurations = 1, }; -const struct usb_board_config PIOS_USB_BOARD_Configuration = { - .config = { - .bLength = sizeof(struct usb_configuration_desc), - .bDescriptorType = USB_DESC_TYPE_CONFIGURATION, - .wTotalLength = htousbs(sizeof(struct usb_board_config)), - .bNumInterfaces = 1, - .bConfigurationValue = 1, - .iConfiguration = 0, - .bmAttributes = 0xC0, - .bMaxPower = 250/2, /* in units of 2ma */ - }, - .hid_if = { - .bLength = sizeof(struct usb_interface_desc), - .bDescriptorType = USB_DESC_TYPE_INTERFACE, - .bInterfaceNumber = 0, - .bAlternateSetting = 0, - .bNumEndpoints = 2, - .bInterfaceClass = USB_INTERFACE_CLASS_HID, - .bInterfaceSubClass = 0, /* no boot */ - .nInterfaceProtocol = 0, /* none */ - .iInterface = 0, - }, - .hid = { - .bLength = sizeof(struct usb_hid_desc), - .bDescriptorType = USB_DESC_TYPE_HID, - .bcdHID = htousbs(0x0110), - .bCountryCode = 0, - .bNumDescriptors = 1, - .bClassDescriptorType = USB_DESC_TYPE_REPORT, - .wItemLength = htousbs(sizeof(PIOS_USB_BOARD_HidReportDescriptor)), - }, - .hid_in = { - .bLength = sizeof(struct usb_endpoint_desc), - .bDescriptorType = USB_DESC_TYPE_ENDPOINT, - .bEndpointAddress = USB_EP_IN(1), - .bmAttributes = USB_EP_ATTR_TT_INTERRUPT, - .wMaxPacketSize = htousbs(PIOS_USB_BOARD_HID_DATA_LENGTH), - .bInterval = 4, /* ms */ - }, - .hid_out = { - .bLength = sizeof(struct usb_endpoint_desc), - .bDescriptorType = USB_DESC_TYPE_ENDPOINT, - .bEndpointAddress = USB_EP_OUT(1), - .bmAttributes = USB_EP_ATTR_TT_INTERRUPT, - .wMaxPacketSize = htousbs(PIOS_USB_BOARD_HID_DATA_LENGTH), - .bInterval = 4, /* ms */ - }, -}; - -const uint8_t PIOS_USB_BOARD_HidReportDescriptor[] = { +static const uint8_t hid_report_desc[36] = { HID_GLOBAL_ITEM_2 (HID_TAG_GLOBAL_USAGE_PAGE), 0x9C, 0xFF, /* Usage Page 0xFF9C (Vendor Defined) */ HID_LOCAL_ITEM_1 (HID_TAG_LOCAL_USAGE), @@ -140,56 +94,71 @@ const uint8_t PIOS_USB_BOARD_HidReportDescriptor[] = { HID_MAIN_ITEM_0 (HID_TAG_MAIN_ENDCOLLECTION), }; -const struct usb_string_langid PIOS_USB_BOARD_StringLangID = { - .bLength = sizeof(PIOS_USB_BOARD_StringLangID), - .bDescriptorType = USB_DESC_TYPE_STRING, - .bLangID = htousbs(USB_LANGID_ENGLISH_UK), +struct usb_config_hid_only { + struct usb_configuration_desc config; + struct usb_interface_desc hid_if; + struct usb_hid_desc hid; + struct usb_endpoint_desc hid_in; + struct usb_endpoint_desc hid_out; +} __attribute__((packed)); + +const struct usb_config_hid_only config_hid_only = { + .config = { + .bLength = sizeof(struct usb_configuration_desc), + .bDescriptorType = USB_DESC_TYPE_CONFIGURATION, + .wTotalLength = htousbs(sizeof(struct usb_config_hid_only)), + .bNumInterfaces = 1, + .bConfigurationValue = 1, + .iConfiguration = 0, + .bmAttributes = 0xC0, + .bMaxPower = 250/2, /* in units of 2ma */ + }, + .hid_if = { + .bLength = sizeof(struct usb_interface_desc), + .bDescriptorType = USB_DESC_TYPE_INTERFACE, + .bInterfaceNumber = 0, + .bAlternateSetting = 0, + .bNumEndpoints = 2, + .bInterfaceClass = USB_INTERFACE_CLASS_HID, + .bInterfaceSubClass = 0, /* no boot */ + .nInterfaceProtocol = 0, /* none */ + .iInterface = 0, + }, + .hid = { + .bLength = sizeof(struct usb_hid_desc), + .bDescriptorType = USB_DESC_TYPE_HID, + .bcdHID = htousbs(0x0110), + .bCountryCode = 0, + .bNumDescriptors = 1, + .bClassDescriptorType = USB_DESC_TYPE_REPORT, + .wItemLength = htousbs(sizeof(hid_report_desc)), + }, + .hid_in = { + .bLength = sizeof(struct usb_endpoint_desc), + .bDescriptorType = USB_DESC_TYPE_ENDPOINT, + .bEndpointAddress = USB_EP_IN(1), + .bmAttributes = USB_EP_ATTR_TT_INTERRUPT, + .wMaxPacketSize = htousbs(PIOS_USB_BOARD_HID_DATA_LENGTH), + .bInterval = 4, /* ms */ + }, + .hid_out = { + .bLength = sizeof(struct usb_endpoint_desc), + .bDescriptorType = USB_DESC_TYPE_ENDPOINT, + .bEndpointAddress = USB_EP_OUT(1), + .bmAttributes = USB_EP_ATTR_TT_INTERRUPT, + .wMaxPacketSize = htousbs(PIOS_USB_BOARD_HID_DATA_LENGTH), + .bInterval = 4, /* ms */ + }, }; -const uint8_t PIOS_USB_BOARD_StringVendorID[] = { - sizeof(PIOS_USB_BOARD_StringVendorID), - USB_DESC_TYPE_STRING, - 'o', 0, - 'p', 0, - 'e', 0, - 'n', 0, - 'p', 0, - 'i', 0, - 'l', 0, - 'o', 0, - 't', 0, - '.', 0, - 'o', 0, - 'r', 0, - 'g', 0 -}; +int32_t PIOS_USB_DESC_HID_ONLY_Init(void) +{ + PIOS_USBHOOK_RegisterConfig(1, (uint8_t *)&config_hid_only, sizeof(config_hid_only)); -uint8_t PIOS_USB_BOARD_StringSerial[] = { - sizeof(PIOS_USB_BOARD_StringSerial), - USB_DESC_TYPE_STRING, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0, - 0, 0 -}; + PIOS_USBHOOK_RegisterDevice((uint8_t *)&device_desc, sizeof(device_desc)); + + PIOS_USBHOOK_RegisterHidInterface((uint8_t *)&(config_hid_only.hid_if), sizeof(config_hid_only.hid_if)); + PIOS_USBHOOK_RegisterHidReport((uint8_t *)hid_report_desc, sizeof(hid_report_desc)); + + return 0; +} diff --git a/flight/PiOS/STM32F10x/Libraries/STM32_USB-FS-Device_Driver/inc/usb_core.h b/flight/PiOS/STM32F10x/Libraries/STM32_USB-FS-Device_Driver/inc/usb_core.h index 84f975da6..7e606e44b 100644 --- a/flight/PiOS/STM32F10x/Libraries/STM32_USB-FS-Device_Driver/inc/usb_core.h +++ b/flight/PiOS/STM32F10x/Libraries/STM32_USB-FS-Device_Driver/inc/usb_core.h @@ -35,7 +35,7 @@ typedef enum _CONTROL_STATE typedef struct OneDescriptor { - uint8_t *Descriptor; + const uint8_t *Descriptor; uint16_t Descriptor_Size; } ONE_DESCRIPTOR, *PONE_DESCRIPTOR; @@ -80,7 +80,8 @@ typedef struct _ENDPOINT_INFO uint16_t Usb_wLength; uint16_t Usb_wOffset; uint16_t PacketSize; - uint8_t *(*CopyData)(uint16_t Length); + const uint8_t *(*CopyDataIn)(uint16_t Length); + uint8_t *(*CopyDataOut)(uint16_t Length); }ENDPOINT_INFO; /*-*-*-*-*-*-*-*-*-*-*-* Definitions for device level -*-*-*-*-*-*-*-*-*-*-*-*/ @@ -169,9 +170,9 @@ typedef struct _DEVICE_PROP RESULT (*Class_Get_Interface_Setting)(uint8_t Interface, uint8_t AlternateSetting); - uint8_t* (*GetDeviceDescriptor)(uint16_t Length); - uint8_t* (*GetConfigDescriptor)(uint16_t Length); - uint8_t* (*GetStringDescriptor)(uint16_t Length); + const uint8_t* (*GetDeviceDescriptor)(uint16_t Length); + const uint8_t* (*GetConfigDescriptor)(uint16_t Length); + const uint8_t* (*GetStringDescriptor)(uint16_t Length); /* This field is not used in current library version. It is kept only for compatibility with previous versions */ @@ -221,13 +222,13 @@ uint8_t In0_Process(void); RESULT Standard_SetEndPointFeature(void); RESULT Standard_SetDeviceFeature(void); -uint8_t *Standard_GetConfiguration(uint16_t Length); +const uint8_t *Standard_GetConfiguration(uint16_t Length); RESULT Standard_SetConfiguration(void); -uint8_t *Standard_GetInterface(uint16_t Length); +const uint8_t *Standard_GetInterface(uint16_t Length); RESULT Standard_SetInterface(void); -uint8_t *Standard_GetDescriptorData(uint16_t Length, const ONE_DESCRIPTOR * pDesc); +const uint8_t *Standard_GetDescriptorData(uint16_t Length, ONE_DESCRIPTOR *pDesc); -uint8_t *Standard_GetStatus(uint16_t Length); +const uint8_t *Standard_GetStatus(uint16_t Length); RESULT Standard_ClearFeature(void); void SetDeviceAddress(uint8_t); void NOP_Process(void); diff --git a/flight/PiOS/STM32F10x/Libraries/STM32_USB-FS-Device_Driver/inc/usb_mem.h b/flight/PiOS/STM32F10x/Libraries/STM32_USB-FS-Device_Driver/inc/usb_mem.h index 4d43fbe8c..f7f0ace12 100644 --- a/flight/PiOS/STM32F10x/Libraries/STM32_USB-FS-Device_Driver/inc/usb_mem.h +++ b/flight/PiOS/STM32F10x/Libraries/STM32_USB-FS-Device_Driver/inc/usb_mem.h @@ -22,7 +22,7 @@ /* Exported constants --------------------------------------------------------*/ /* Exported macro ------------------------------------------------------------*/ /* Exported functions ------------------------------------------------------- */ -void UserToPMABufferCopy(uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes); +void UserToPMABufferCopy(const uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes); void PMAToUserBufferCopy(uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes); /* External variables --------------------------------------------------------*/ diff --git a/flight/PiOS/STM32F10x/Libraries/STM32_USB-FS-Device_Driver/src/usb_core.c b/flight/PiOS/STM32F10x/Libraries/STM32_USB-FS-Device_Driver/src/usb_core.c index fb5ca3e40..75ddaa42e 100644 --- a/flight/PiOS/STM32F10x/Libraries/STM32_USB-FS-Device_Driver/src/usb_core.c +++ b/flight/PiOS/STM32F10x/Libraries/STM32_USB-FS-Device_Driver/src/usb_core.c @@ -58,7 +58,7 @@ static void Data_Setup0(void); * Return : Return 1 , if the request is invalid when "Length" is 0. * Return "Buffer" if the "Length" is not 0. *******************************************************************************/ -uint8_t *Standard_GetConfiguration(uint16_t Length) +const uint8_t *Standard_GetConfiguration(uint16_t Length) { if (Length == 0) { @@ -104,7 +104,7 @@ RESULT Standard_SetConfiguration(void) * Return : Return 0, if the request is invalid when "Length" is 0. * Return "Buffer" if the "Length" is not 0. *******************************************************************************/ -uint8_t *Standard_GetInterface(uint16_t Length) +const uint8_t *Standard_GetInterface(uint16_t Length) { if (Length == 0) { @@ -160,7 +160,7 @@ RESULT Standard_SetInterface(void) * Return : Return 0, if the request is at end of data block, * or is invalid when "Length" is 0. *******************************************************************************/ -uint8_t *Standard_GetStatus(uint16_t Length) +const uint8_t *Standard_GetStatus(uint16_t Length) { if (Length == 0) { @@ -415,7 +415,7 @@ RESULT Standard_SetDeviceFeature(void) * wOffset The buffer pointed by this address contains at least * Length bytes. *******************************************************************************/ -uint8_t *Standard_GetDescriptorData(uint16_t Length, const ONE_DESCRIPTOR * pDesc) +const uint8_t *Standard_GetDescriptorData(uint16_t Length, PONE_DESCRIPTOR pDesc) { uint32_t wOffset; @@ -443,7 +443,7 @@ void DataStageOut(void) save_rLength = pEPinfo->Usb_rLength; - if (pEPinfo->CopyData && save_rLength) + if (pEPinfo->CopyDataOut && save_rLength) { uint8_t *Buffer; uint32_t Length; @@ -454,7 +454,7 @@ void DataStageOut(void) Length = save_rLength; } - Buffer = (*pEPinfo->CopyData)(Length); + Buffer = (*pEPinfo->CopyDataOut)(Length); pEPinfo->Usb_rLength -= Length; pEPinfo->Usb_rOffset += Length; @@ -503,7 +503,7 @@ void DataStageIn(void) uint32_t save_wLength = pEPinfo->Usb_wLength; uint32_t ControlState = pInformation->ControlState; - uint8_t *DataBuffer; + const uint8_t *DataBuffer; uint32_t Length; if ((save_wLength == 0) && (ControlState == LAST_IN_DATA)) @@ -540,7 +540,7 @@ void DataStageIn(void) Length = save_wLength; } - DataBuffer = (*pEPinfo->CopyData)(Length); + DataBuffer = (*pEPinfo->CopyDataIn)(Length); #ifdef STM32F10X_CL PCD_EP_Write (ENDP0, DataBuffer, Length); @@ -697,7 +697,7 @@ exit_NoData_Setup0: *******************************************************************************/ void Data_Setup0(void) { - uint8_t *(*CopyRoutine)(uint16_t); + const uint8_t *(*CopyRoutine)(uint16_t); RESULT Result; uint32_t Request_No = pInformation->USBbRequest; @@ -802,7 +802,7 @@ void Data_Setup0(void) if (CopyRoutine) { pInformation->Ctrl_Info.Usb_wOffset = wOffset; - pInformation->Ctrl_Info.CopyData = CopyRoutine; + pInformation->Ctrl_Info.CopyDataIn = CopyRoutine; /* sb in the original the cast to word was directly */ /* now the cast is made step by step */ (*CopyRoutine)(0); diff --git a/flight/PiOS/STM32F10x/Libraries/STM32_USB-FS-Device_Driver/src/usb_mem.c b/flight/PiOS/STM32F10x/Libraries/STM32_USB-FS-Device_Driver/src/usb_mem.c index e729b844c..ca50a23c4 100644 --- a/flight/PiOS/STM32F10x/Libraries/STM32_USB-FS-Device_Driver/src/usb_mem.c +++ b/flight/PiOS/STM32F10x/Libraries/STM32_USB-FS-Device_Driver/src/usb_mem.c @@ -33,7 +33,7 @@ * Output : None. * Return : None . *******************************************************************************/ -void UserToPMABufferCopy(uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes) +void UserToPMABufferCopy(const uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes) { uint32_t n = (wNBytes + 1) >> 1; /* n = (wNBytes + 1) / 2 */ uint32_t i, temp1, temp2; diff --git a/flight/PiOS/STM32F10x/pios_usb.c b/flight/PiOS/STM32F10x/pios_usb.c index df17777c7..15decfca1 100644 --- a/flight/PiOS/STM32F10x/pios_usb.c +++ b/flight/PiOS/STM32F10x/pios_usb.c @@ -123,13 +123,6 @@ int32_t PIOS_USB_Init(uint32_t * usb_id, const struct pios_usb_cfg * cfg) /* Enable the USB clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE); - /* Update the USB serial number from the chip */ - uint8_t sn[25]; - PIOS_SYS_SerialNumberGet((char *)sn); - for (uint8_t i = 0; sn[i] != '\0' && (2 * i) < PIOS_USB_BOARD_StringSerial[0]; i++) { - PIOS_USB_BOARD_StringSerial[2 + 2 * i] = sn[i]; - } - USB_Init(); USB_SIL_Init(); diff --git a/flight/PiOS/STM32F10x/pios_usb_cdc.c b/flight/PiOS/STM32F10x/pios_usb_cdc.c index 994c276ea..4bb1303d9 100644 --- a/flight/PiOS/STM32F10x/pios_usb_cdc.c +++ b/flight/PiOS/STM32F10x/pios_usb_cdc.c @@ -346,7 +346,7 @@ RESULT PIOS_USB_CDC_SetLineCoding(void) return USB_SUCCESS; } -uint8_t *PIOS_USB_CDC_GetLineCoding(uint16_t Length) +const uint8_t *PIOS_USB_CDC_GetLineCoding(uint16_t Length) { struct pios_usb_cdc_dev * usb_cdc_dev = (struct pios_usb_cdc_dev *)pios_usb_cdc_id; diff --git a/flight/PiOS/STM32F10x/pios_usbhook.c b/flight/PiOS/STM32F10x/pios_usbhook.c index ade1f8ac1..7800faed1 100644 --- a/flight/PiOS/STM32F10x/pios_usbhook.c +++ b/flight/PiOS/STM32F10x/pios_usbhook.c @@ -36,6 +36,48 @@ #include "pios_usb_cdc_priv.h" /* PIOS_USB_CDC_* */ #include "pios_usb_board_data.h" /* PIOS_USB_BOARD_* */ +static ONE_DESCRIPTOR Device_Descriptor; + +void PIOS_USBHOOK_RegisterDevice(const uint8_t * desc, uint16_t desc_size) +{ + Device_Descriptor.Descriptor = desc; + Device_Descriptor.Descriptor_Size = desc_size; +} + +static ONE_DESCRIPTOR Config_Descriptor; + +void PIOS_USBHOOK_RegisterConfig(uint8_t config_id, const uint8_t * desc, uint16_t desc_size) +{ + Config_Descriptor.Descriptor = desc; + Config_Descriptor.Descriptor_Size = desc_size; +} + +static ONE_DESCRIPTOR String_Descriptor[4]; + +void PIOS_USBHOOK_RegisterString(enum usb_string_desc string_id, const uint8_t * desc, uint16_t desc_size) +{ + if (string_id < NELEMENTS(String_Descriptor)) { + String_Descriptor[string_id].Descriptor = desc; + String_Descriptor[string_id].Descriptor_Size = desc_size; + } +} + +static ONE_DESCRIPTOR Hid_Interface_Descriptor; + +void PIOS_USBHOOK_RegisterHidInterface(const uint8_t * desc, uint16_t desc_size) +{ + Hid_Interface_Descriptor.Descriptor = desc; + Hid_Interface_Descriptor.Descriptor_Size = desc_size; +} + +static ONE_DESCRIPTOR Hid_Report_Descriptor; + +void PIOS_USBHOOK_RegisterHidReport(const uint8_t * desc, uint16_t desc_size) +{ + Hid_Report_Descriptor.Descriptor = desc; + Hid_Report_Descriptor.Descriptor_Size = desc_size; +} + #include "stm32f10x.h" /* __IO */ __IO uint8_t EXTI_Enable; @@ -53,9 +95,9 @@ static void PIOS_USBHOOK_Status_Out(void); static RESULT PIOS_USBHOOK_Data_Setup(uint8_t RequestNo); static RESULT PIOS_USBHOOK_NoData_Setup(uint8_t RequestNo); static RESULT PIOS_USBHOOK_Get_Interface_Setting(uint8_t Interface, uint8_t AlternateSetting); -static uint8_t *PIOS_USBHOOK_GetDeviceDescriptor(uint16_t Length); -static uint8_t *PIOS_USBHOOK_GetConfigDescriptor(uint16_t Length); -static uint8_t *PIOS_USBHOOK_GetStringDescriptor(uint16_t Length); +static const uint8_t *PIOS_USBHOOK_GetDeviceDescriptor(uint16_t Length); +static const uint8_t *PIOS_USBHOOK_GetConfigDescriptor(uint16_t Length); +static const uint8_t *PIOS_USBHOOK_GetStringDescriptor(uint16_t Length); DEVICE_PROP Device_Property = { .Init = PIOS_USBHOOK_Init, @@ -87,49 +129,10 @@ USER_STANDARD_REQUESTS User_Standard_Requests = { .User_SetDeviceAddress = PIOS_USBHOOK_SetDeviceAddress }; -const static ONE_DESCRIPTOR Device_Descriptor = { - (uint8_t *) &PIOS_USB_BOARD_DeviceDescriptor, - sizeof(PIOS_USB_BOARD_DeviceDescriptor), -}; - -static ONE_DESCRIPTOR Config_Descriptor = { - (uint8_t *) &PIOS_USB_BOARD_Configuration, - sizeof(PIOS_USB_BOARD_Configuration), -}; - -const static ONE_DESCRIPTOR Hid_Report_Descriptor = { - (uint8_t *) &PIOS_USB_BOARD_HidReportDescriptor, - sizeof(PIOS_USB_BOARD_HidReportDescriptor), -}; - -const static ONE_DESCRIPTOR Hid_Interface_Descriptor = { - (uint8_t *) &PIOS_USB_BOARD_Configuration.hid_if, - sizeof(PIOS_USB_BOARD_Configuration.hid_if), -}; - -const static ONE_DESCRIPTOR String_Descriptor[] = { - { - (uint8_t *) &PIOS_USB_BOARD_StringLangID, - sizeof(PIOS_USB_BOARD_StringLangID), - }, - { - (uint8_t *) PIOS_USB_BOARD_StringVendorID, - sizeof(PIOS_USB_BOARD_StringVendorID), - }, - { - (uint8_t *) PIOS_USB_BOARD_StringProductID, - sizeof(PIOS_USB_BOARD_StringProductID), - }, - { - (uint8_t *) PIOS_USB_BOARD_StringSerial, - sizeof(PIOS_USB_BOARD_StringSerial), - }, -}; - static RESULT PIOS_USBHOOK_SetProtocol(void); -static uint8_t *PIOS_USBHOOK_GetProtocolValue(uint16_t Length); -static uint8_t *PIOS_USBHOOK_GetReportDescriptor(uint16_t Length); -static uint8_t *PIOS_USBHOOK_GetHIDDescriptor(uint16_t Length); +static const uint8_t *PIOS_USBHOOK_GetProtocolValue(uint16_t Length); +static const uint8_t *PIOS_USBHOOK_GetReportDescriptor(uint16_t Length); +static const uint8_t *PIOS_USBHOOK_GetHIDDescriptor(uint16_t Length); /******************************************************************************* * Function Name : PIOS_USBHOOK_Init. @@ -140,11 +143,8 @@ static uint8_t *PIOS_USBHOOK_GetHIDDescriptor(uint16_t Length); *******************************************************************************/ static void PIOS_USBHOOK_Init(void) { - /* Update the serial number string descriptor with the data from the unique - ID */ - //Get_SerialNum(); - pInformation->Current_Configuration = 0; + /* Connect the device */ PowerOn(); @@ -292,7 +292,7 @@ static void PIOS_USBHOOK_Status_Out(void) *******************************************************************************/ static RESULT PIOS_USBHOOK_Data_Setup(uint8_t RequestNo) { - uint8_t *(*CopyRoutine) (uint16_t); + const uint8_t *(*CopyRoutine) (uint16_t); CopyRoutine = NULL; @@ -350,7 +350,7 @@ static RESULT PIOS_USBHOOK_Data_Setup(uint8_t RequestNo) return USB_UNSUPPORT; } - pInformation->Ctrl_Info.CopyData = CopyRoutine; + pInformation->Ctrl_Info.CopyDataIn = CopyRoutine; pInformation->Ctrl_Info.Usb_wOffset = 0; (*CopyRoutine) (0); return USB_SUCCESS; @@ -405,7 +405,7 @@ static RESULT PIOS_USBHOOK_NoData_Setup(uint8_t RequestNo) * Output : None. * Return : The address of the device descriptor. *******************************************************************************/ -static uint8_t *PIOS_USBHOOK_GetDeviceDescriptor(uint16_t Length) +static const uint8_t *PIOS_USBHOOK_GetDeviceDescriptor(uint16_t Length) { return Standard_GetDescriptorData(Length, &Device_Descriptor); } @@ -417,7 +417,7 @@ static uint8_t *PIOS_USBHOOK_GetDeviceDescriptor(uint16_t Length) * Output : None. * Return : The address of the configuration descriptor. *******************************************************************************/ -static uint8_t *PIOS_USBHOOK_GetConfigDescriptor(uint16_t Length) +static const uint8_t *PIOS_USBHOOK_GetConfigDescriptor(uint16_t Length) { return Standard_GetDescriptorData(Length, &Config_Descriptor); } @@ -429,7 +429,7 @@ static uint8_t *PIOS_USBHOOK_GetConfigDescriptor(uint16_t Length) * Output : None. * Return : The address of the string descriptors. *******************************************************************************/ -static uint8_t *PIOS_USBHOOK_GetStringDescriptor(uint16_t Length) +static const uint8_t *PIOS_USBHOOK_GetStringDescriptor(uint16_t Length) { uint8_t wValue0 = pInformation->USBwValue0; if (wValue0 > 4) { @@ -446,7 +446,7 @@ static uint8_t *PIOS_USBHOOK_GetStringDescriptor(uint16_t Length) * Output : None. * Return : The address of the configuration descriptor. *******************************************************************************/ -static uint8_t *PIOS_USBHOOK_GetReportDescriptor(uint16_t Length) +static const uint8_t *PIOS_USBHOOK_GetReportDescriptor(uint16_t Length) { return Standard_GetDescriptorData(Length, &Hid_Report_Descriptor); } @@ -458,7 +458,7 @@ static uint8_t *PIOS_USBHOOK_GetReportDescriptor(uint16_t Length) * Output : None. * Return : The address of the configuration descriptor. *******************************************************************************/ -static uint8_t *PIOS_USBHOOK_GetHIDDescriptor(uint16_t Length) +static const uint8_t *PIOS_USBHOOK_GetHIDDescriptor(uint16_t Length) { return Standard_GetDescriptorData(Length, &Hid_Interface_Descriptor); } @@ -503,7 +503,7 @@ static RESULT PIOS_USBHOOK_SetProtocol(void) * Output : None. * Return : address of the protcol value. *******************************************************************************/ -static uint8_t *PIOS_USBHOOK_GetProtocolValue(uint16_t Length) +static const uint8_t *PIOS_USBHOOK_GetProtocolValue(uint16_t Length) { if (Length == 0) { pInformation->Ctrl_Info.Usb_wLength = 1; diff --git a/flight/PiOS/inc/pios_usb_board_data_priv.h b/flight/PiOS/inc/pios_usb_board_data_priv.h new file mode 100644 index 000000000..49166c25f --- /dev/null +++ b/flight/PiOS/inc/pios_usb_board_data_priv.h @@ -0,0 +1 @@ +extern int32_t PIOS_USB_BOARD_DATA_Init(void); diff --git a/flight/PiOS/inc/pios_usb_cdc_priv.h b/flight/PiOS/inc/pios_usb_cdc_priv.h index e6b94bad8..74d6e347d 100644 --- a/flight/PiOS/inc/pios_usb_cdc_priv.h +++ b/flight/PiOS/inc/pios_usb_cdc_priv.h @@ -46,7 +46,7 @@ extern const struct pios_com_driver pios_usb_cdc_com_driver; extern int32_t PIOS_USB_CDC_Init(uint32_t * usbcdc_id, const struct pios_usb_cdc_cfg * cfg, uint32_t lower_id); -extern uint8_t *PIOS_USB_CDC_GetLineCoding(uint16_t Length); +extern const uint8_t *PIOS_USB_CDC_GetLineCoding(uint16_t Length); extern RESULT PIOS_USB_CDC_SetControlLineState(void); extern RESULT PIOS_USB_CDC_SetLineCoding(void); diff --git a/flight/PiOS/inc/pios_usb_desc_hid_cdc_priv.h b/flight/PiOS/inc/pios_usb_desc_hid_cdc_priv.h new file mode 100644 index 000000000..4836f4569 --- /dev/null +++ b/flight/PiOS/inc/pios_usb_desc_hid_cdc_priv.h @@ -0,0 +1,8 @@ +#ifndef PIOS_USB_DESC_HID_CDC_PRIV_H +#define PIOS_USB_DESC_HID_CDC_PRIV_H + +#include + +extern int32_t PIOS_USB_DESC_HID_CDC_Init(void); + +#endif /* PIOS_USB_DESC_HID_CDC_PRIV_H */ diff --git a/flight/PiOS/inc/pios_usb_desc_hid_only_priv.h b/flight/PiOS/inc/pios_usb_desc_hid_only_priv.h new file mode 100644 index 000000000..5d161d233 --- /dev/null +++ b/flight/PiOS/inc/pios_usb_desc_hid_only_priv.h @@ -0,0 +1,8 @@ +#ifndef PIOS_USB_DESC_HID_ONLY_PRIV_H +#define PIOS_USB_DESC_HID_ONLY_PRIV_H + +#include + +extern int32_t PIOS_USB_DESC_HID_ONLY_Init(void); + +#endif /* PIOS_USB_DESC_HID_ONLY_PRIV_H */ diff --git a/flight/PiOS/inc/pios_usbhook.h b/flight/PiOS/inc/pios_usbhook.h index d5f032450..f005a6fa1 100644 --- a/flight/PiOS/inc/pios_usbhook.h +++ b/flight/PiOS/inc/pios_usbhook.h @@ -47,5 +47,18 @@ typedef enum CDC_REQUESTS { SET_CONTROL_LINE_STATE = 0x23, } CDC_REQUESTS; +enum usb_string_desc { + USB_STRING_DESC_LANG = 0, + USB_STRING_DESC_VENDOR = 1, + USB_STRING_DESC_PRODUCT = 2, + USB_STRING_DESC_SERIAL = 3, +} __attribute__((packed)); + +extern void PIOS_USBHOOK_RegisterDevice(const uint8_t * desc, uint16_t desc_size); +extern void PIOS_USBHOOK_RegisterConfig(uint8_t config_id, const uint8_t * desc, uint16_t desc_size); +extern void PIOS_USBHOOK_RegisterString(enum usb_string_desc string_id, const uint8_t * desc, uint16_t desc_size); +extern void PIOS_USBHOOK_RegisterHidInterface(const uint8_t * desc, uint16_t desc_size); +extern void PIOS_USBHOOK_RegisterHidReport(const uint8_t * desc, uint16_t desc_size); + #endif /* PIOS_USBHOOK_H */ diff --git a/flight/PipXtreme/inc/pios_usb_board_data.h b/flight/PipXtreme/inc/pios_usb_board_data.h index 95dc03d73..d4f4daf0a 100644 --- a/flight/PipXtreme/inc/pios_usb_board_data.h +++ b/flight/PipXtreme/inc/pios_usb_board_data.h @@ -37,33 +37,6 @@ #include "pios_usb_defs.h" /* struct usb_* */ -struct usb_board_config { - struct usb_configuration_desc config; - struct usb_interface_desc hid_if; - struct usb_hid_desc hid; - struct usb_endpoint_desc hid_in; - struct usb_endpoint_desc hid_out; -} __attribute__((packed)); - -extern const struct usb_device_desc PIOS_USB_BOARD_DeviceDescriptor; -extern const struct usb_board_config PIOS_USB_BOARD_Configuration; -extern const struct usb_string_langid PIOS_USB_BOARD_StringLangID; - -/* NOTE NOTE NOTE - * - * Care must be taken to ensure that the _actual_ contents of - * these arrays (in each board's pios_usb_board_data.c) is no - * smaller than the stated sizes here or these descriptors - * will end up with trailing zeros on the wire. - * - * The compiler will catch any time that these definitions are - * too small. - */ -extern const uint8_t PIOS_USB_BOARD_HidReportDescriptor[36]; -extern const uint8_t PIOS_USB_BOARD_StringVendorID[28]; -extern const uint8_t PIOS_USB_BOARD_StringProductID[20]; -extern uint8_t PIOS_USB_BOARD_StringSerial[52]; - #define PIOS_USB_BOARD_PRODUCT_ID USB_PRODUCT_ID_PIPXTREME #define PIOS_USB_BOARD_DEVICE_VER USB_OP_DEVICE_VER(USB_OP_BOARD_ID_PIPXTREME, USB_OP_BOARD_MODE_FW) diff --git a/flight/PipXtreme/pios_board.c b/flight/PipXtreme/pios_board.c index 7da3705a8..ab6bcffef 100644 --- a/flight/PipXtreme/pios_board.c +++ b/flight/PipXtreme/pios_board.c @@ -324,6 +324,10 @@ static const struct pios_usb_cfg pios_usb_main_cfg = { }, }, }; + +#include "pios_usb_board_data_priv.h" +#include "pios_usb_desc_hid_only_priv.h" + #endif /* PIOS_INCLUDE_USB */ #if defined(PIOS_INCLUDE_USB_HID) @@ -369,10 +373,16 @@ void PIOS_Board_Init(void) { } #if defined(PIOS_INCLUDE_USB) - uint32_t pios_usb_id; - if (PIOS_USB_Init(&pios_usb_id, &pios_usb_main_cfg)) { + /* Initialize board specific USB data */ + PIOS_USB_BOARD_DATA_Init(); + + if (PIOS_USB_DESC_HID_ONLY_Init()) { PIOS_Assert(0); } + + uint32_t pios_usb_id; + PIOS_USB_Init(&pios_usb_id, &pios_usb_main_cfg); + #if defined(PIOS_INCLUDE_USB_HID) && defined(PIOS_INCLUDE_COM) uint32_t pios_usb_hid_id; if (PIOS_USB_HID_Init(&pios_usb_hid_id, &pios_usb_hid_cfg, pios_usb_id)) { diff --git a/flight/PipXtreme/pios_usb_board_data.c b/flight/PipXtreme/pios_usb_board_data.c index d54ca6565..3e41e5ac9 100644 --- a/flight/PipXtreme/pios_usb_board_data.c +++ b/flight/PipXtreme/pios_usb_board_data.c @@ -29,9 +29,11 @@ */ #include "pios_usb_board_data.h" /* struct usb_*, USB_* */ +#include "pios_sys.h" /* PIOS_SYS_SerialNumberGet */ +#include "pios_usbhook.h" /* PIOS_USBHOOK_* */ -const uint8_t PIOS_USB_BOARD_StringProductID[] = { - sizeof(PIOS_USB_BOARD_StringProductID), +static const uint8_t usb_product_id[20] = { + sizeof(usb_product_id), USB_DESC_TYPE_STRING, 'P', 0, 'i', 0, @@ -44,3 +46,74 @@ const uint8_t PIOS_USB_BOARD_StringProductID[] = { 'e', 0, }; +static uint8_t usb_serial_number[52] = { + sizeof(usb_serial_number), + USB_DESC_TYPE_STRING, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0, + 0, 0 +}; + +static const struct usb_string_langid usb_lang_id = { + .bLength = sizeof(usb_lang_id), + .bDescriptorType = USB_DESC_TYPE_STRING, + .bLangID = htousbs(USB_LANGID_ENGLISH_UK), +}; + +static const uint8_t usb_vendor_id[28] = { + sizeof(usb_vendor_id), + USB_DESC_TYPE_STRING, + 'o', 0, + 'p', 0, + 'e', 0, + 'n', 0, + 'p', 0, + 'i', 0, + 'l', 0, + 'o', 0, + 't', 0, + '.', 0, + 'o', 0, + 'r', 0, + 'g', 0 +}; + +int32_t PIOS_USB_BOARD_DATA_Init(void) +{ + /* Load device serial number into serial number string */ + uint8_t sn[25]; + PIOS_SYS_SerialNumberGet((char *)sn); + for (uint8_t i = 0; sn[i] != '\0' && (2 * i) < usb_serial_number[0]; i++) { + usb_serial_number[2 + 2 * i] = sn[i]; + } + + PIOS_USBHOOK_RegisterString(USB_STRING_DESC_PRODUCT, (uint8_t *)&usb_product_id, sizeof(usb_product_id)); + PIOS_USBHOOK_RegisterString(USB_STRING_DESC_SERIAL, (uint8_t *)&usb_serial_number, sizeof(usb_serial_number)); + + PIOS_USBHOOK_RegisterString(USB_STRING_DESC_LANG, (uint8_t *)&usb_lang_id, sizeof(usb_lang_id)); + PIOS_USBHOOK_RegisterString(USB_STRING_DESC_VENDOR, (uint8_t *)&usb_vendor_id, sizeof(usb_vendor_id)); + + return 0; +} diff --git a/shared/uavobjectdefinition/hwsettings.xml b/shared/uavobjectdefinition/hwsettings.xml index 1e34e0808..24727a080 100644 --- a/shared/uavobjectdefinition/hwsettings.xml +++ b/shared/uavobjectdefinition/hwsettings.xml @@ -12,6 +12,7 @@ +