Skip to content

A truly modular keyboard

C

firmware: USB CDC serial with the same config protocol as NUS

Nathan24da197c2955
diff --git a/firmware/Core/Inc/app_conf.h b/firmware/Core/Inc/app_conf.h
index ff3f3a5..e338121 100644
--- a/firmware/Core/Inc/app_conf.h
+++ b/firmware/Core/Inc/app_conf.h
@@ -109,6 +109,7 @@ typedef enum
CFG_TASK_ADV_UPDATE_ID,
CFG_TASK_KEYS_ID,
CFG_TASK_NUS_RX_ID,
+ CFG_TASK_CDC_RX_ID,
CFG_TASK_HCI_ASYNCH_EVT_ID,
CFG_LAST_TASK_ID_WITH_HCICMD, /* shall be last in this list */
} CFG_Task_Id_With_HCI_Cmd_t;
diff --git a/firmware/Core/Inc/proto.h b/firmware/Core/Inc/proto.h
index f6be818..f72c3b4 100644
--- a/firmware/Core/Inc/proto.h
+++ b/firmware/Core/Inc/proto.h
@@ -1,6 +1,7 @@
/*
* Line based config protocol (protocol 1) shared by every transport. See
- * app/PROTOCOL.md for the commands. Lines end in \n, \r\n is accepted.
+ * app/PROTOCOL.md for the commands. Lines end in \n; \r\n and a bare \r
+ * (a terminal's Enter) are accepted too.
*/
#ifndef PROTO_H
#define PROTO_H
@@ -9,10 +10,27 @@
#define KEYWORD_FW_VERSION "0.1.0"
-/* Feed received bytes in any chunk size. */
-void PROTO_Input(const uint8_t *data, uint16_t len);
+#define PROTO_LINE_MAX 128
+#define PROTO_MAX_PORTS 2
-/* Send one line (printf style, \n added). */
+/* One per transport (NUS, USB CDC): its own line buffer and output. The
+ * transport sets connected when someone is listening (NUS: TX notifications
+ * on, CDC: DTR set); a received line also counts. */
+typedef struct
+{
+ void (*write)(const char *data, uint16_t len);
+ volatile uint8_t connected;
+ uint8_t overflow;
+ uint16_t len;
+ char line[PROTO_LINE_MAX];
+} proto_port_t;
+
+void PROTO_AddPort(proto_port_t *port);
+
+/* Feed received bytes in any chunk size. Replies go back to this port. */
+void PROTO_Input(proto_port_t *port, const uint8_t *data, uint16_t len);
+
+/* Unsolicited line (printf style, \n added) to every connected port. */
void PROTO_Send(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
/* 96-bit little endian UID as 24 uppercase hex digits, most significant
diff --git a/firmware/Core/Src/app_entry.c b/firmware/Core/Src/app_entry.c
index 110e9c9..0fc134c 100644
--- a/firmware/Core/Src/app_entry.c
+++ b/firmware/Core/Src/app_entry.c
@@ -19,6 +19,7 @@
#include "stm32_lpm.h"
#include "shci.h"
#include "otp.h"
+#include "keymap.h"
RTC_HandleTypeDef hrtc;
@@ -120,6 +121,9 @@ void MX_APPE_Init(void)
HW_TS_Init(hw_ts_InitMode_Full, &hrtc);
UTIL_LPM_SetOffMode(1 << CFG_LPM_APP, UTIL_LPM_DISABLE);
+ /* Keys run whether or not the radio comes up, USB CDC needs them too. */
+ KEYMAP_Init();
+
/* Transport layer. CPU2 boots once TL_Enable() runs and answers with
* SHCI_SUB_EVT_CODE_READY, which brings up the BLE app. */
TL_MM_Config_t tl_mm_config;
diff --git a/firmware/Core/Src/keymap.c b/firmware/Core/Src/keymap.c
index d478784..a61c041 100644
--- a/firmware/Core/Src/keymap.c
+++ b/firmware/Core/Src/keymap.c
@@ -4,6 +4,7 @@
#include "shci.h"
#include "utilities_conf.h"
#include "hids_app.h"
+#include "app_ble.h"
#include "proto.h"
#include "keymap.h"
@@ -170,9 +171,10 @@ int KEYMAP_Save(void)
HAL_FLASH_Unlock();
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
- SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_ON);
+ /* Without the BLE stack running there is nobody to answer SHCI. */
+ if (APP_BLE_Ready()) SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_ON);
err |= Flash_Op(1, Map_Page(), 0);
- SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_OFF);
+ if (APP_BLE_Ready()) SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_OFF);
uint32_t addr = (uint32_t)Map_Flash();
const uint64_t *src = (const uint64_t *)&map;
diff --git a/firmware/Core/Src/main.c b/firmware/Core/Src/main.c
index 2c7675f..dba08ea 100644
--- a/firmware/Core/Src/main.c
+++ b/firmware/Core/Src/main.c
@@ -22,6 +22,7 @@
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "app_entry.h"
+#include "usb_device.h"
/* USER CODE END Includes */
@@ -325,6 +326,7 @@ static void MX_RF_Init(void)
/* USER CODE END RF_Init 1 */
/* USER CODE BEGIN RF_Init 2 */
MX_APPE_Init(); /* STM32_WPAN: RTC timer server, transport layer, boots CPU2 */
+ MX_USB_Device_Init(); /* USB CDC serial for the config protocol */
/* USER CODE END RF_Init 2 */
}
diff --git a/firmware/Core/Src/proto.c b/firmware/Core/Src/proto.c
index 1b477ef..b3df5f8 100644
--- a/firmware/Core/Src/proto.c
+++ b/firmware/Core/Src/proto.c
@@ -2,26 +2,49 @@
#include <stdio.h>
#include <string.h>
#include "keymap.h"
-#include "nus.h"
#include "proto.h"
-#define LINE_MAX 128
+static proto_port_t *ports[PROTO_MAX_PORTS];
+static int port_count;
-static char line[LINE_MAX];
-static uint16_t line_len;
-static uint8_t line_overflow;
+void PROTO_AddPort(proto_port_t *port)
+{
+ if (port_count < PROTO_MAX_PORTS) ports[port_count++] = port;
+}
+
+/* Formats one line into out with the \n, returns its length or -1. */
+static int Format(char out[PROTO_LINE_MAX], const char *fmt, va_list ap)
+{
+ int n = vsnprintf(out, PROTO_LINE_MAX - 1, fmt, ap);
+ if (n < 0) return -1;
+ if (n > PROTO_LINE_MAX - 2) n = PROTO_LINE_MAX - 2;
+ out[n++] = '\n';
+ return n;
+}
void PROTO_Send(const char *fmt, ...)
{
- char out[LINE_MAX];
+ char out[PROTO_LINE_MAX];
va_list ap;
va_start(ap, fmt);
- int n = vsnprintf(out, sizeof(out) - 1, fmt, ap);
+ int n = Format(out, fmt, ap);
va_end(ap);
if (n < 0) return;
- if (n > (int)sizeof(out) - 2) n = sizeof(out) - 2;
- out[n++] = '\n';
- NUS_Write(out, (uint16_t)n); /* add USB CDC here once there is one */
+ for (int i = 0; i < port_count; i++)
+ {
+ if (ports[i]->connected) ports[i]->write(out, (uint16_t)n);
+ }
+}
+
+static void Reply(proto_port_t *port, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
+static void Reply(proto_port_t *port, const char *fmt, ...)
+{
+ char out[PROTO_LINE_MAX];
+ va_list ap;
+ va_start(ap, fmt);
+ int n = Format(out, fmt, ap);
+ va_end(ap);
+ if (n >= 0) port->write(out, (uint16_t)n);
}
void PROTO_FormatUid(char *out, const uint8_t uid[UID_LEN])
@@ -76,7 +99,7 @@ static int Arg_Uid(const char *args, uint8_t uid[UID_LEN])
return 0;
}
-static void Handle_Line(char *cmd)
+static void Handle_Line(proto_port_t *port, char *cmd)
{
char uid_s[UID_LEN * 2 + 1];
uint8_t uid[UID_LEN];
@@ -86,7 +109,7 @@ static void Handle_Line(char *cmd)
if (strcmp(cmd, "HELLO") == 0)
{
- PROTO_Send("KEYWORD v=%s proto=1", KEYWORD_FW_VERSION);
+ Reply(port, "KEYWORD v=%s proto=1", KEYWORD_FW_VERSION);
}
else if (strcmp(cmd, "SIDES?") == 0)
{
@@ -99,7 +122,7 @@ static void Handle_Line(char *cmd)
r |= k[i].at == 'R';
t |= k[i].at == 'T';
}
- PROTO_Send("SIDES L=%d R=%d T=%d", l, r, t);
+ Reply(port, "SIDES L=%d R=%d T=%d", l, r, t);
}
else if (strcmp(cmd, "KEYS?") == 0)
{
@@ -108,41 +131,41 @@ static void Handle_Line(char *cmd)
for (int i = 0; i < n; i++)
{
PROTO_FormatUid(uid_s, k[i].uid);
- PROTO_Send("KEY+ uid=%s at=%c", uid_s, k[i].at);
+ Reply(port, "KEY+ uid=%s at=%c", uid_s, k[i].at);
}
- PROTO_Send("KEYS END");
+ Reply(port, "KEYS END");
}
else if (strcmp(cmd, "MAP") == 0)
{
uint8_t mods, key;
if (Arg_Uid(args, uid) || Arg_Hex(args, "mods", 2, &mods) || Arg_Hex(args, "key", 2, &key))
{
- PROTO_Send("ERR usage: MAP uid=<24hex> mods=<2hex> key=<2hex>");
+ Reply(port, "ERR usage: MAP uid=<24hex> mods=<2hex> key=<2hex>");
return;
}
if (KEYMAP_Set(uid, mods, key) != 0)
{
- PROTO_Send("ERR keymap full");
+ Reply(port, "ERR keymap full");
return;
}
PROTO_FormatUid(uid_s, uid);
- PROTO_Send("OK MAP uid=%s", uid_s);
+ Reply(port, "OK MAP uid=%s", uid_s);
}
else if (strcmp(cmd, "MAPC") == 0)
{
uint8_t usage[2];
if (Arg_Uid(args, uid) || Arg_Hex(args, "usage", 4, usage))
{
- PROTO_Send("ERR usage: MAPC uid=<24hex> usage=<4hex>");
+ Reply(port, "ERR usage: MAPC uid=<24hex> usage=<4hex>");
return;
}
if (KEYMAP_SetConsumer(uid, (uint16_t)(usage[0] << 8 | usage[1])) != 0)
{
- PROTO_Send("ERR keymap full");
+ Reply(port, "ERR keymap full");
return;
}
PROTO_FormatUid(uid_s, uid);
- PROTO_Send("OK MAPC uid=%s", uid_s);
+ Reply(port, "OK MAPC uid=%s", uid_s);
}
else if (strcmp(cmd, "MAP?") == 0)
{
@@ -150,67 +173,69 @@ static void Handle_Line(char *cmd)
uint16_t code;
if (Arg_Uid(args, uid))
{
- PROTO_Send("ERR usage: MAP? uid=<24hex>");
+ Reply(port, "ERR usage: MAP? uid=<24hex>");
return;
}
PROTO_FormatUid(uid_s, uid);
if (KEYMAP_Get(uid, &mods, &code) == KEYMAP_CONSUMER)
{
- PROTO_Send("MAPC uid=%s usage=%04X", uid_s, code);
+ Reply(port, "MAPC uid=%s usage=%04X", uid_s, code);
}
else
{
- PROTO_Send("MAP uid=%s mods=%02X key=%02X", uid_s, mods, code);
+ Reply(port, "MAP uid=%s mods=%02X key=%02X", uid_s, mods, code);
}
}
else if (strcmp(cmd, "UNMAP") == 0)
{
if (Arg_Uid(args, uid))
{
- PROTO_Send("ERR usage: UNMAP uid=<24hex>");
+ Reply(port, "ERR usage: UNMAP uid=<24hex>");
return;
}
KEYMAP_Unmap(uid);
PROTO_FormatUid(uid_s, uid);
- PROTO_Send("OK UNMAP uid=%s", uid_s);
+ Reply(port, "OK UNMAP uid=%s", uid_s);
}
else if (strcmp(cmd, "CLEAR") == 0)
{
KEYMAP_Clear();
- PROTO_Send("OK CLEAR");
+ Reply(port, "OK CLEAR");
}
else if (strcmp(cmd, "SAVE") == 0)
{
- if (KEYMAP_Save() != 0) PROTO_Send("ERR flash write failed");
- else PROTO_Send("OK SAVE");
+ if (KEYMAP_Save() != 0) Reply(port, "ERR flash write failed");
+ else Reply(port, "OK SAVE");
}
else
{
- PROTO_Send("ERR unknown command");
+ Reply(port, "ERR unknown command");
}
}
-void PROTO_Input(const uint8_t *data, uint16_t len)
+void PROTO_Input(proto_port_t *port, const uint8_t *data, uint16_t len)
{
for (uint16_t i = 0; i < len; i++)
{
char c = (char)data[i];
- if (c == '\n')
+ /* \n ends a line; so does a bare \r, which is what terminals send
+ * on Enter. \r\n just leaves an empty line, which is ignored. */
+ if (c == '\n' || c == '\r')
{
- if (line_len && line[line_len - 1] == '\r') line_len--;
- line[line_len] = 0;
- if (line_overflow) PROTO_Send("ERR line too long");
- else if (line_len) Handle_Line(line);
- line_len = 0;
- line_overflow = 0;
+ port->line[port->len] = 0;
+ if (port->len || port->overflow) port->connected = 1;
+ if (port->overflow) Reply(port, "ERR line too long");
+ else if (port->len) Handle_Line(port, port->line);
+ port->len = 0;
+ port->overflow = 0;
}
- else if (line_len < LINE_MAX - 1)
+ else if (port->len < PROTO_LINE_MAX - 1)
{
- line[line_len++] = c;
+ port->line[port->len++] = c;
}
else
{
- line_overflow = 1;
+ port->overflow = 1;
}
}
}
diff --git a/firmware/Core/Src/stm32wbxx_hal_msp.c b/firmware/Core/Src/stm32wbxx_hal_msp.c
index 9d69dcf..2c61123 100644
--- a/firmware/Core/Src/stm32wbxx_hal_msp.c
+++ b/firmware/Core/Src/stm32wbxx_hal_msp.c
@@ -20,7 +20,7 @@
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* USER CODE BEGIN Includes */
-
+#include "usb_device.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
@@ -282,7 +282,7 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef* hpcd)
/* Peripheral clock enable */
__HAL_RCC_USB_CLK_ENABLE();
/* USER CODE BEGIN USB_MspInit 1 */
-
+ USB_Clock_HSI48(); /* use HSI48 + CRS instead of PLLSAI1, see usb_device.c */
/* USER CODE END USB_MspInit 1 */
}
diff --git a/firmware/Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc/usbd_cdc.h b/firmware/Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc/usbd_cdc.h
new file mode 100644
index 0000000..42ff56c
--- /dev/null
+++ b/firmware/Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc/usbd_cdc.h
@@ -0,0 +1,187 @@
+/**
+ ******************************************************************************
+ * @file usbd_cdc.h
+ * @author MCD Application Team
+ * @brief header file for the usbd_cdc.c file.
+ ******************************************************************************
+ * @attention
+ *
+ * Copyright (c) 2015 STMicroelectronics.
+ * All rights reserved.
+ *
+ * This software is licensed under terms that can be found in the LICENSE file
+ * in the root directory of this software component.
+ * If no LICENSE file comes with this software, it is provided AS-IS.
+ *
+ ******************************************************************************
+ */
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __USB_CDC_H
+#define __USB_CDC_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Includes ------------------------------------------------------------------*/
+#include "usbd_ioreq.h"
+
+/** @addtogroup STM32_USB_DEVICE_LIBRARY
+ * @{
+ */
+
+/** @defgroup usbd_cdc
+ * @brief This file is the Header file for usbd_cdc.c
+ * @{
+ */
+
+
+/** @defgroup usbd_cdc_Exported_Defines
+ * @{
+ */
+#ifndef CDC_IN_EP
+#define CDC_IN_EP 0x81U /* EP1 for data IN */
+#endif /* CDC_IN_EP */
+#ifndef CDC_OUT_EP
+#define CDC_OUT_EP 0x01U /* EP1 for data OUT */
+#endif /* CDC_OUT_EP */
+#ifndef CDC_CMD_EP
+#define CDC_CMD_EP 0x82U /* EP2 for CDC commands */
+#endif /* CDC_CMD_EP */
+
+#ifndef CDC_HS_BINTERVAL
+#define CDC_HS_BINTERVAL 0x10U
+#endif /* CDC_HS_BINTERVAL */
+
+#ifndef CDC_FS_BINTERVAL
+#define CDC_FS_BINTERVAL 0x10U
+#endif /* CDC_FS_BINTERVAL */
+
+#ifndef CDC_CMD_PACKET_SIZE
+#define CDC_CMD_PACKET_SIZE 8U /* Control Endpoint Packet size */
+#endif /* CDC_CMD_PACKET_SIZE */
+
+/* CDC Endpoints parameters: you can fine tune these values depending on the needed baudrates and performance. */
+#define CDC_DATA_HS_MAX_PACKET_SIZE 512U /* Endpoint IN & OUT Packet size */
+#define CDC_DATA_FS_MAX_PACKET_SIZE 64U /* Endpoint IN & OUT Packet size */
+
+#define USB_CDC_CONFIG_DESC_SIZ 67U
+#define CDC_DATA_HS_IN_PACKET_SIZE CDC_DATA_HS_MAX_PACKET_SIZE
+#define CDC_DATA_HS_OUT_PACKET_SIZE CDC_DATA_HS_MAX_PACKET_SIZE
+
+#define CDC_DATA_FS_IN_PACKET_SIZE CDC_DATA_FS_MAX_PACKET_SIZE
+#define CDC_DATA_FS_OUT_PACKET_SIZE CDC_DATA_FS_MAX_PACKET_SIZE
+
+#define CDC_REQ_MAX_DATA_SIZE 0x7U
+/*---------------------------------------------------------------------*/
+/* CDC definitions */
+/*---------------------------------------------------------------------*/
+#define CDC_SEND_ENCAPSULATED_COMMAND 0x00U
+#define CDC_GET_ENCAPSULATED_RESPONSE 0x01U
+#define CDC_SET_COMM_FEATURE 0x02U
+#define CDC_GET_COMM_FEATURE 0x03U
+#define CDC_CLEAR_COMM_FEATURE 0x04U
+#define CDC_SET_LINE_CODING 0x20U
+#define CDC_GET_LINE_CODING 0x21U
+#define CDC_SET_CONTROL_LINE_STATE 0x22U
+#define CDC_SEND_BREAK 0x23U
+
+/**
+ * @}
+ */
+
+
+/** @defgroup USBD_CORE_Exported_TypesDefinitions
+ * @{
+ */
+
+/**
+ * @}
+ */
+typedef struct
+{
+ uint32_t bitrate;
+ uint8_t format;
+ uint8_t paritytype;
+ uint8_t datatype;
+} USBD_CDC_LineCodingTypeDef;
+
+typedef struct _USBD_CDC_Itf
+{
+ int8_t (* Init)(void);
+ int8_t (* DeInit)(void);
+ int8_t (* Control)(uint8_t cmd, uint8_t *pbuf, uint16_t length);
+ int8_t (* Receive)(uint8_t *Buf, uint32_t *Len);
+ int8_t (* TransmitCplt)(uint8_t *Buf, uint32_t *Len, uint8_t epnum);
+} USBD_CDC_ItfTypeDef;
+
+
+typedef struct
+{
+ uint32_t data[CDC_DATA_HS_MAX_PACKET_SIZE / 4U]; /* Force 32-bit alignment */
+ uint8_t CmdOpCode;
+ uint8_t CmdLength;
+ uint8_t *RxBuffer;
+ uint8_t *TxBuffer;
+ uint32_t RxLength;
+ uint32_t TxLength;
+
+ __IO uint32_t TxState;
+ __IO uint32_t RxState;
+} USBD_CDC_HandleTypeDef;
+
+
+
+/** @defgroup USBD_CORE_Exported_Macros
+ * @{
+ */
+
+/**
+ * @}
+ */
+
+/** @defgroup USBD_CORE_Exported_Variables
+ * @{
+ */
+
+extern USBD_ClassTypeDef USBD_CDC;
+#define USBD_CDC_CLASS &USBD_CDC
+/**
+ * @}
+ */
+
+/** @defgroup USB_CORE_Exported_Functions
+ * @{
+ */
+uint8_t USBD_CDC_RegisterInterface(USBD_HandleTypeDef *pdev,
+ USBD_CDC_ItfTypeDef *fops);
+
+#ifdef USE_USBD_COMPOSITE
+uint8_t USBD_CDC_SetTxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff,
+ uint32_t length, uint8_t ClassId);
+uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev, uint8_t ClassId);
+#else
+uint8_t USBD_CDC_SetTxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff,
+ uint32_t length);
+uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev);
+#endif /* USE_USBD_COMPOSITE */
+uint8_t USBD_CDC_SetRxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff);
+uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev);
+/**
+ * @}
+ */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __USB_CDC_H */
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
diff --git a/firmware/Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Src/usbd_cdc.c b/firmware/Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Src/usbd_cdc.c
new file mode 100644
index 0000000..050d996
--- /dev/null
+++ b/firmware/Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Src/usbd_cdc.c
@@ -0,0 +1,896 @@
+/**
+ ******************************************************************************
+ * @file usbd_cdc.c
+ * @author MCD Application Team
+ * @brief This file provides the high layer firmware functions to manage the
+ * following functionalities of the USB CDC Class:
+ * - Initialization and Configuration of high and low layer
+ * - Enumeration as CDC Device (and enumeration for each implemented memory interface)
+ * - OUT/IN data transfer
+ * - Command IN transfer (class requests management)
+ * - Error management
+ *
+ ******************************************************************************
+ * @attention
+ *
+ * Copyright (c) 2015 STMicroelectronics.
+ * All rights reserved.
+ *
+ * This software is licensed under terms that can be found in the LICENSE file
+ * in the root directory of this software component.
+ * If no LICENSE file comes with this software, it is provided AS-IS.
+ *
+ ******************************************************************************
+ * @verbatim
+ *
+ * ===================================================================
+ * CDC Class Driver Description
+ * ===================================================================
+ * This driver manages the "Universal Serial Bus Class Definitions for Communications Devices
+ * Revision 1.2 November 16, 2007" and the sub-protocol specification of "Universal Serial Bus
+ * Communications Class Subclass Specification for PSTN Devices Revision 1.2 February 9, 2007"
+ * This driver implements the following aspects of the specification:
+ * - Device descriptor management
+ * - Configuration descriptor management
+ * - Enumeration as CDC device with 2 data endpoints (IN and OUT) and 1 command endpoint (IN)
+ * - Requests management (as described in section 6.2 in specification)
+ * - Abstract Control Model compliant
+ * - Union Functional collection (using 1 IN endpoint for control)
+ * - Data interface class
+ *
+ * These aspects may be enriched or modified for a specific user application.
+ *
+ * This driver doesn't implement the following aspects of the specification
+ * (but it is possible to manage these features with some modifications on this driver):
+ * - Any class-specific aspect relative to communication classes should be managed by user application.
+ * - All communication classes other than PSTN are not managed
+ *
+ * @endverbatim
+ *
+ ******************************************************************************
+ */
+
+/* BSPDependencies
+- "stm32xxxxx_{eval}{discovery}{nucleo_144}.c"
+- "stm32xxxxx_{eval}{discovery}_io.c"
+EndBSPDependencies */
+
+/* Includes ------------------------------------------------------------------*/
+#include "usbd_cdc.h"
+#include "usbd_ctlreq.h"
+
+
+/** @addtogroup STM32_USB_DEVICE_LIBRARY
+ * @{
+ */
+
+
+/** @defgroup USBD_CDC
+ * @brief usbd core module
+ * @{
+ */
+
+/** @defgroup USBD_CDC_Private_TypesDefinitions
+ * @{
+ */
+/**
+ * @}
+ */
+
+
+/** @defgroup USBD_CDC_Private_Defines
+ * @{
+ */
+/**
+ * @}
+ */
+
+
+/** @defgroup USBD_CDC_Private_Macros
+ * @{
+ */
+
+/**
+ * @}
+ */
+
+
+/** @defgroup USBD_CDC_Private_FunctionPrototypes
+ * @{
+ */
+
+static uint8_t USBD_CDC_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx);
+static uint8_t USBD_CDC_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx);
+static uint8_t USBD_CDC_Setup(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
+static uint8_t USBD_CDC_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum);
+static uint8_t USBD_CDC_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum);
+static uint8_t USBD_CDC_EP0_RxReady(USBD_HandleTypeDef *pdev);
+#ifndef USE_USBD_COMPOSITE
+static uint8_t *USBD_CDC_GetFSCfgDesc(uint16_t *length);
+static uint8_t *USBD_CDC_GetHSCfgDesc(uint16_t *length);
+static uint8_t *USBD_CDC_GetOtherSpeedCfgDesc(uint16_t *length);
+uint8_t *USBD_CDC_GetDeviceQualifierDescriptor(uint16_t *length);
+#endif /* USE_USBD_COMPOSITE */
+
+#ifndef USE_USBD_COMPOSITE
+/* USB Standard Device Descriptor */
+__ALIGN_BEGIN static uint8_t USBD_CDC_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END =
+{
+ USB_LEN_DEV_QUALIFIER_DESC,
+ USB_DESC_TYPE_DEVICE_QUALIFIER,
+ 0x00,
+ 0x02,
+ 0x00,
+ 0x00,
+ 0x00,
+ 0x40,
+ 0x01,
+ 0x00,
+};
+#endif /* USE_USBD_COMPOSITE */
+/**
+ * @}
+ */
+
+/** @defgroup USBD_CDC_Private_Variables
+ * @{
+ */
+
+
+/* CDC interface class callbacks structure */
+USBD_ClassTypeDef USBD_CDC =
+{
+ USBD_CDC_Init,
+ USBD_CDC_DeInit,
+ USBD_CDC_Setup,
+ NULL, /* EP0_TxSent */
+ USBD_CDC_EP0_RxReady,
+ USBD_CDC_DataIn,
+ USBD_CDC_DataOut,
+ NULL,
+ NULL,
+ NULL,
+#ifdef USE_USBD_COMPOSITE
+ NULL,
+ NULL,
+ NULL,
+ NULL,
+#else
+ USBD_CDC_GetHSCfgDesc,
+ USBD_CDC_GetFSCfgDesc,
+ USBD_CDC_GetOtherSpeedCfgDesc,
+ USBD_CDC_GetDeviceQualifierDescriptor,
+#endif /* USE_USBD_COMPOSITE */
+#if (USBD_SUPPORT_USER_STRING_DESC == 1U)
+ NULL,
+#endif /* USBD_SUPPORT_USER_STRING_DESC */
+};
+
+#ifndef USE_USBD_COMPOSITE
+/* USB CDC device Configuration Descriptor */
+__ALIGN_BEGIN static uint8_t USBD_CDC_CfgDesc[USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END =
+{
+ /* Configuration Descriptor */
+ 0x09, /* bLength: Configuration Descriptor size */
+ USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */
+ USB_CDC_CONFIG_DESC_SIZ, /* wTotalLength */
+ 0x00,
+ 0x02, /* bNumInterfaces: 2 interfaces */
+ 0x01, /* bConfigurationValue: Configuration value */
+ 0x00, /* iConfiguration: Index of string descriptor
+ describing the configuration */
+#if (USBD_SELF_POWERED == 1U)
+ 0xC0, /* bmAttributes: Bus Powered according to user configuration */
+#else
+ 0x80, /* bmAttributes: Bus Powered according to user configuration */
+#endif /* USBD_SELF_POWERED */
+ USBD_MAX_POWER, /* MaxPower (mA) */
+
+ /*---------------------------------------------------------------------------*/
+
+ /* Interface Descriptor */
+ 0x09, /* bLength: Interface Descriptor size */
+ USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */
+ /* Interface descriptor type */
+ 0x00, /* bInterfaceNumber: Number of Interface */
+ 0x00, /* bAlternateSetting: Alternate setting */
+ 0x01, /* bNumEndpoints: One endpoint used */
+ 0x02, /* bInterfaceClass: Communication Interface Class */
+ 0x02, /* bInterfaceSubClass: Abstract Control Model */
+ 0x01, /* bInterfaceProtocol: Common AT commands */
+ 0x00, /* iInterface */
+
+ /* Header Functional Descriptor */
+ 0x05, /* bLength: Endpoint Descriptor size */
+ 0x24, /* bDescriptorType: CS_INTERFACE */
+ 0x00, /* bDescriptorSubtype: Header Func Desc */
+ 0x10, /* bcdCDC: spec release number */
+ 0x01,
+
+ /* Call Management Functional Descriptor */
+ 0x05, /* bFunctionLength */
+ 0x24, /* bDescriptorType: CS_INTERFACE */
+ 0x01, /* bDescriptorSubtype: Call Management Func Desc */
+ 0x00, /* bmCapabilities: D0+D1 */
+ 0x01, /* bDataInterface */
+
+ /* ACM Functional Descriptor */
+ 0x04, /* bFunctionLength */
+ 0x24, /* bDescriptorType: CS_INTERFACE */
+ 0x02, /* bDescriptorSubtype: Abstract Control Management desc */
+ 0x02, /* bmCapabilities */
+
+ /* Union Functional Descriptor */
+ 0x05, /* bFunctionLength */
+ 0x24, /* bDescriptorType: CS_INTERFACE */
+ 0x06, /* bDescriptorSubtype: Union func desc */
+ 0x00, /* bMasterInterface: Communication class interface */
+ 0x01, /* bSlaveInterface0: Data Class Interface */
+
+ /* Endpoint 2 Descriptor */
+ 0x07, /* bLength: Endpoint Descriptor size */
+ USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
+ CDC_CMD_EP, /* bEndpointAddress */
+ 0x03, /* bmAttributes: Interrupt */
+ LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize */
+ HIBYTE(CDC_CMD_PACKET_SIZE),
+ CDC_FS_BINTERVAL, /* bInterval */
+ /*---------------------------------------------------------------------------*/
+
+ /* Data class interface descriptor */
+ 0x09, /* bLength: Endpoint Descriptor size */
+ USB_DESC_TYPE_INTERFACE, /* bDescriptorType: */
+ 0x01, /* bInterfaceNumber: Number of Interface */
+ 0x00, /* bAlternateSetting: Alternate setting */
+ 0x02, /* bNumEndpoints: Two endpoints used */
+ 0x0A, /* bInterfaceClass: CDC */
+ 0x00, /* bInterfaceSubClass */
+ 0x00, /* bInterfaceProtocol */
+ 0x00, /* iInterface */
+
+ /* Endpoint OUT Descriptor */
+ 0x07, /* bLength: Endpoint Descriptor size */
+ USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
+ CDC_OUT_EP, /* bEndpointAddress */
+ 0x02, /* bmAttributes: Bulk */
+ LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), /* wMaxPacketSize */
+ HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE),
+ 0x00, /* bInterval */
+
+ /* Endpoint IN Descriptor */
+ 0x07, /* bLength: Endpoint Descriptor size */
+ USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */
+ CDC_IN_EP, /* bEndpointAddress */
+ 0x02, /* bmAttributes: Bulk */
+ LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), /* wMaxPacketSize */
+ HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE),
+ 0x00 /* bInterval */
+};
+#endif /* USE_USBD_COMPOSITE */
+
+static uint8_t CDCInEpAdd = CDC_IN_EP;
+static uint8_t CDCOutEpAdd = CDC_OUT_EP;
+static uint8_t CDCCmdEpAdd = CDC_CMD_EP;
+
+/**
+ * @}
+ */
+
+/** @defgroup USBD_CDC_Private_Functions
+ * @{
+ */
+
+/**
+ * @brief USBD_CDC_Init
+ * Initialize the CDC interface
+ * @param pdev: device instance
+ * @param cfgidx: Configuration index
+ * @retval status
+ */
+static uint8_t USBD_CDC_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
+{
+ UNUSED(cfgidx);
+ USBD_CDC_HandleTypeDef *hcdc;
+
+ hcdc = (USBD_CDC_HandleTypeDef *)USBD_malloc(sizeof(USBD_CDC_HandleTypeDef));
+
+ if (hcdc == NULL)
+ {
+ pdev->pClassDataCmsit[pdev->classId] = NULL;
+ return (uint8_t)USBD_EMEM;
+ }
+
+ (void)USBD_memset(hcdc, 0, sizeof(USBD_CDC_HandleTypeDef));
+
+ pdev->pClassDataCmsit[pdev->classId] = (void *)hcdc;
+ pdev->pClassData = pdev->pClassDataCmsit[pdev->classId];
+
+#ifdef USE_USBD_COMPOSITE
+ /* Get the Endpoints addresses allocated for this class instance */
+ CDCInEpAdd = USBD_CoreGetEPAdd(pdev, USBD_EP_IN, USBD_EP_TYPE_BULK, (uint8_t)pdev->classId);
+ CDCOutEpAdd = USBD_CoreGetEPAdd(pdev, USBD_EP_OUT, USBD_EP_TYPE_BULK, (uint8_t)pdev->classId);
+ CDCCmdEpAdd = USBD_CoreGetEPAdd(pdev, USBD_EP_IN, USBD_EP_TYPE_INTR, (uint8_t)pdev->classId);
+#endif /* USE_USBD_COMPOSITE */
+
+ if (pdev->dev_speed == USBD_SPEED_HIGH)
+ {
+ /* Open EP IN */
+ (void)USBD_LL_OpenEP(pdev, CDCInEpAdd, USBD_EP_TYPE_BULK,
+ CDC_DATA_HS_IN_PACKET_SIZE);
+
+ pdev->ep_in[CDCInEpAdd & 0xFU].is_used = 1U;
+
+ /* Open EP OUT */
+ (void)USBD_LL_OpenEP(pdev, CDCOutEpAdd, USBD_EP_TYPE_BULK,
+ CDC_DATA_HS_OUT_PACKET_SIZE);
+
+ pdev->ep_out[CDCOutEpAdd & 0xFU].is_used = 1U;
+
+ /* Set bInterval for CDC CMD Endpoint */
+ pdev->ep_in[CDCCmdEpAdd & 0xFU].bInterval = CDC_HS_BINTERVAL;
+ }
+ else
+ {
+ /* Open EP IN */
+ (void)USBD_LL_OpenEP(pdev, CDCInEpAdd, USBD_EP_TYPE_BULK,
+ CDC_DATA_FS_IN_PACKET_SIZE);
+
+ pdev->ep_in[CDCInEpAdd & 0xFU].is_used = 1U;
+
+ /* Open EP OUT */
+ (void)USBD_LL_OpenEP(pdev, CDCOutEpAdd, USBD_EP_TYPE_BULK,
+ CDC_DATA_FS_OUT_PACKET_SIZE);
+
+ pdev->ep_out[CDCOutEpAdd & 0xFU].is_used = 1U;
+
+ /* Set bInterval for CMD Endpoint */
+ pdev->ep_in[CDCCmdEpAdd & 0xFU].bInterval = CDC_FS_BINTERVAL;
+ }
+
+ /* Open Command IN EP */
+ (void)USBD_LL_OpenEP(pdev, CDCCmdEpAdd, USBD_EP_TYPE_INTR, CDC_CMD_PACKET_SIZE);
+ pdev->ep_in[CDCCmdEpAdd & 0xFU].is_used = 1U;
+
+ hcdc->RxBuffer = NULL;
+
+ /* Init physical Interface components */
+ ((USBD_CDC_ItfTypeDef *)pdev->pUserData[pdev->classId])->Init();
+
+ /* Init Xfer states */
+ hcdc->TxState = 0U;
+ hcdc->RxState = 0U;
+
+ if (hcdc->RxBuffer == NULL)
+ {
+ return (uint8_t)USBD_EMEM;
+ }
+
+ if (pdev->dev_speed == USBD_SPEED_HIGH)
+ {
+ /* Prepare Out endpoint to receive next packet */
+ (void)USBD_LL_PrepareReceive(pdev, CDCOutEpAdd, hcdc->RxBuffer,
+ CDC_DATA_HS_OUT_PACKET_SIZE);
+ }
+ else
+ {
+ /* Prepare Out endpoint to receive next packet */
+ (void)USBD_LL_PrepareReceive(pdev, CDCOutEpAdd, hcdc->RxBuffer,
+ CDC_DATA_FS_OUT_PACKET_SIZE);
+ }
+
+ return (uint8_t)USBD_OK;
+}
+
+/**
+ * @brief USBD_CDC_Init
+ * DeInitialize the CDC layer
+ * @param pdev: device instance
+ * @param cfgidx: Configuration index
+ * @retval status
+ */
+static uint8_t USBD_CDC_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
+{
+ UNUSED(cfgidx);
+
+
+#ifdef USE_USBD_COMPOSITE
+ /* Get the Endpoints addresses allocated for this CDC class instance */
+ CDCInEpAdd = USBD_CoreGetEPAdd(pdev, USBD_EP_IN, USBD_EP_TYPE_BULK, (uint8_t)pdev->classId);
+ CDCOutEpAdd = USBD_CoreGetEPAdd(pdev, USBD_EP_OUT, USBD_EP_TYPE_BULK, (uint8_t)pdev->classId);
+ CDCCmdEpAdd = USBD_CoreGetEPAdd(pdev, USBD_EP_IN, USBD_EP_TYPE_INTR, (uint8_t)pdev->classId);
+#endif /* USE_USBD_COMPOSITE */
+
+ /* Close EP IN */
+ (void)USBD_LL_CloseEP(pdev, CDCInEpAdd);
+ pdev->ep_in[CDCInEpAdd & 0xFU].is_used = 0U;
+
+ /* Close EP OUT */
+ (void)USBD_LL_CloseEP(pdev, CDCOutEpAdd);
+ pdev->ep_out[CDCOutEpAdd & 0xFU].is_used = 0U;
+
+ /* Close Command IN EP */
+ (void)USBD_LL_CloseEP(pdev, CDCCmdEpAdd);
+ pdev->ep_in[CDCCmdEpAdd & 0xFU].is_used = 0U;
+ pdev->ep_in[CDCCmdEpAdd & 0xFU].bInterval = 0U;
+
+ /* DeInit physical Interface components */
+ if (pdev->pClassDataCmsit[pdev->classId] != NULL)
+ {
+ ((USBD_CDC_ItfTypeDef *)pdev->pUserData[pdev->classId])->DeInit();
+ (void)USBD_free(pdev->pClassDataCmsit[pdev->classId]);
+ pdev->pClassDataCmsit[pdev->classId] = NULL;
+ pdev->pClassData = NULL;
+ }
+
+ return (uint8_t)USBD_OK;
+}
+
+/**
+ * @brief USBD_CDC_Setup
+ * Handle the CDC specific requests
+ * @param pdev: instance
+ * @param req: usb requests
+ * @retval status
+ */
+static uint8_t USBD_CDC_Setup(USBD_HandleTypeDef *pdev,
+ USBD_SetupReqTypedef *req)
+{
+ USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
+ uint16_t len;
+ uint8_t ifalt = 0U;
+ uint16_t status_info = 0U;
+ USBD_StatusTypeDef ret = USBD_OK;
+
+ if (hcdc == NULL)
+ {
+ return (uint8_t)USBD_FAIL;
+ }
+
+ switch (req->bmRequest & USB_REQ_TYPE_MASK)
+ {
+ case USB_REQ_TYPE_CLASS:
+ if (req->wLength != 0U)
+ {
+ if ((req->bmRequest & 0x80U) != 0U)
+ {
+ ((USBD_CDC_ItfTypeDef *)pdev->pUserData[pdev->classId])->Control(req->bRequest,
+ (uint8_t *)hcdc->data,
+ req->wLength);
+
+ len = MIN(CDC_REQ_MAX_DATA_SIZE, req->wLength);
+ (void)USBD_CtlSendData(pdev, (uint8_t *)hcdc->data, len);
+ }
+ else
+ {
+ hcdc->CmdOpCode = req->bRequest;
+ hcdc->CmdLength = (uint8_t)MIN(req->wLength, USB_MAX_EP0_SIZE);
+
+ (void)USBD_CtlPrepareRx(pdev, (uint8_t *)hcdc->data, hcdc->CmdLength);
+ }
+ }
+ else
+ {
+ ((USBD_CDC_ItfTypeDef *)pdev->pUserData[pdev->classId])->Control(req->bRequest,
+ (uint8_t *)req, 0U);
+ }
+ break;
+
+ case USB_REQ_TYPE_STANDARD:
+ switch (req->bRequest)
+ {
+ case USB_REQ_GET_STATUS:
+ if (pdev->dev_state == USBD_STATE_CONFIGURED)
+ {
+ (void)USBD_CtlSendData(pdev, (uint8_t *)&status_info, 2U);
+ }
+ else
+ {
+ USBD_CtlError(pdev, req);
+ ret = USBD_FAIL;
+ }
+ break;
+
+ case USB_REQ_GET_INTERFACE:
+ if (pdev->dev_state == USBD_STATE_CONFIGURED)
+ {
+ (void)USBD_CtlSendData(pdev, &ifalt, 1U);
+ }
+ else
+ {
+ USBD_CtlError(pdev, req);
+ ret = USBD_FAIL;
+ }
+ break;
+
+ case USB_REQ_SET_INTERFACE:
+ if (pdev->dev_state != USBD_STATE_CONFIGURED)
+ {
+ USBD_CtlError(pdev, req);
+ ret = USBD_FAIL;
+ }
+ break;
+
+ case USB_REQ_CLEAR_FEATURE:
+ break;
+
+ default:
+ USBD_CtlError(pdev, req);
+ ret = USBD_FAIL;
+ break;
+ }
+ break;
+
+ default:
+ USBD_CtlError(pdev, req);
+ ret = USBD_FAIL;
+ break;
+ }
+
+ return (uint8_t)ret;
+}
+
+/**
+ * @brief USBD_CDC_DataIn
+ * Data sent on non-control IN endpoint
+ * @param pdev: device instance
+ * @param epnum: endpoint number
+ * @retval status
+ */
+static uint8_t USBD_CDC_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum)
+{
+ USBD_CDC_HandleTypeDef *hcdc;
+ PCD_HandleTypeDef *hpcd = (PCD_HandleTypeDef *)pdev->pData;
+
+ if (pdev->pClassDataCmsit[pdev->classId] == NULL)
+ {
+ return (uint8_t)USBD_FAIL;
+ }
+
+ hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
+
+ if ((pdev->ep_in[epnum & 0xFU].total_length > 0U) &&
+ ((pdev->ep_in[epnum & 0xFU].total_length % hpcd->IN_ep[epnum & 0xFU].maxpacket) == 0U))
+ {
+ /* Update the packet total length */
+ pdev->ep_in[epnum & 0xFU].total_length = 0U;
+
+ /* Send ZLP */
+ (void)USBD_LL_Transmit(pdev, epnum, NULL, 0U);
+ }
+ else
+ {
+ hcdc->TxState = 0U;
+
+ if (((USBD_CDC_ItfTypeDef *)pdev->pUserData[pdev->classId])->TransmitCplt != NULL)
+ {
+ ((USBD_CDC_ItfTypeDef *)pdev->pUserData[pdev->classId])->TransmitCplt(hcdc->TxBuffer, &hcdc->TxLength, epnum);
+ }
+ }
+
+ return (uint8_t)USBD_OK;
+}
+
+/**
+ * @brief USBD_CDC_DataOut
+ * Data received on non-control Out endpoint
+ * @param pdev: device instance
+ * @param epnum: endpoint number
+ * @retval status
+ */
+static uint8_t USBD_CDC_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum)
+{
+ USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
+
+ if (pdev->pClassDataCmsit[pdev->classId] == NULL)
+ {
+ return (uint8_t)USBD_FAIL;
+ }
+
+ /* Get the received data length */
+ hcdc->RxLength = USBD_LL_GetRxDataSize(pdev, epnum);
+
+ /* USB data will be immediately processed, this allow next USB traffic being
+ NAKed till the end of the application Xfer */
+
+ ((USBD_CDC_ItfTypeDef *)pdev->pUserData[pdev->classId])->Receive(hcdc->RxBuffer, &hcdc->RxLength);
+
+ return (uint8_t)USBD_OK;
+}
+
+/**
+ * @brief USBD_CDC_EP0_RxReady
+ * Handle EP0 Rx Ready event
+ * @param pdev: device instance
+ * @retval status
+ */
+static uint8_t USBD_CDC_EP0_RxReady(USBD_HandleTypeDef *pdev)
+{
+ USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
+
+ if (hcdc == NULL)
+ {
+ return (uint8_t)USBD_FAIL;
+ }
+
+ if ((pdev->pUserData[pdev->classId] != NULL) && (hcdc->CmdOpCode != 0xFFU))
+ {
+ ((USBD_CDC_ItfTypeDef *)pdev->pUserData[pdev->classId])->Control(hcdc->CmdOpCode,
+ (uint8_t *)hcdc->data,
+ (uint16_t)hcdc->CmdLength);
+ hcdc->CmdOpCode = 0xFFU;
+ }
+
+ return (uint8_t)USBD_OK;
+}
+#ifndef USE_USBD_COMPOSITE
+/**
+ * @brief USBD_CDC_GetFSCfgDesc
+ * Return configuration descriptor
+ * @param length : pointer data length
+ * @retval pointer to descriptor buffer
+ */
+static uint8_t *USBD_CDC_GetFSCfgDesc(uint16_t *length)
+{
+ USBD_EpDescTypeDef *pEpCmdDesc = USBD_GetEpDesc(USBD_CDC_CfgDesc, CDC_CMD_EP);
+ USBD_EpDescTypeDef *pEpOutDesc = USBD_GetEpDesc(USBD_CDC_CfgDesc, CDC_OUT_EP);
+ USBD_EpDescTypeDef *pEpInDesc = USBD_GetEpDesc(USBD_CDC_CfgDesc, CDC_IN_EP);
+
+ if (pEpCmdDesc != NULL)
+ {
+ pEpCmdDesc->bInterval = CDC_FS_BINTERVAL;
+ }
+
+ if (pEpOutDesc != NULL)
+ {
+ pEpOutDesc->wMaxPacketSize = CDC_DATA_FS_MAX_PACKET_SIZE;
+ }
+
+ if (pEpInDesc != NULL)
+ {
+ pEpInDesc->wMaxPacketSize = CDC_DATA_FS_MAX_PACKET_SIZE;
+ }
+
+ *length = (uint16_t)sizeof(USBD_CDC_CfgDesc);
+ return USBD_CDC_CfgDesc;
+}
+
+/**
+ * @brief USBD_CDC_GetHSCfgDesc
+ * Return configuration descriptor
+ * @param length : pointer data length
+ * @retval pointer to descriptor buffer
+ */
+static uint8_t *USBD_CDC_GetHSCfgDesc(uint16_t *length)
+{
+ USBD_EpDescTypeDef *pEpCmdDesc = USBD_GetEpDesc(USBD_CDC_CfgDesc, CDC_CMD_EP);
+ USBD_EpDescTypeDef *pEpOutDesc = USBD_GetEpDesc(USBD_CDC_CfgDesc, CDC_OUT_EP);
+ USBD_EpDescTypeDef *pEpInDesc = USBD_GetEpDesc(USBD_CDC_CfgDesc, CDC_IN_EP);
+
+ if (pEpCmdDesc != NULL)
+ {
+ pEpCmdDesc->bInterval = CDC_HS_BINTERVAL;
+ }
+
+ if (pEpOutDesc != NULL)
+ {
+ pEpOutDesc->wMaxPacketSize = CDC_DATA_HS_MAX_PACKET_SIZE;
+ }
+
+ if (pEpInDesc != NULL)
+ {
+ pEpInDesc->wMaxPacketSize = CDC_DATA_HS_MAX_PACKET_SIZE;
+ }
+
+ *length = (uint16_t)sizeof(USBD_CDC_CfgDesc);
+ return USBD_CDC_CfgDesc;
+}
+
+/**
+ * @brief USBD_CDC_GetOtherSpeedCfgDesc
+ * Return configuration descriptor
+ * @param length : pointer data length
+ * @retval pointer to descriptor buffer
+ */
+static uint8_t *USBD_CDC_GetOtherSpeedCfgDesc(uint16_t *length)
+{
+ USBD_EpDescTypeDef *pEpCmdDesc = USBD_GetEpDesc(USBD_CDC_CfgDesc, CDC_CMD_EP);
+ USBD_EpDescTypeDef *pEpOutDesc = USBD_GetEpDesc(USBD_CDC_CfgDesc, CDC_OUT_EP);
+ USBD_EpDescTypeDef *pEpInDesc = USBD_GetEpDesc(USBD_CDC_CfgDesc, CDC_IN_EP);
+
+ if (pEpCmdDesc != NULL)
+ {
+ pEpCmdDesc->bInterval = CDC_FS_BINTERVAL;
+ }
+
+ if (pEpOutDesc != NULL)
+ {
+ pEpOutDesc->wMaxPacketSize = CDC_DATA_FS_MAX_PACKET_SIZE;
+ }
+
+ if (pEpInDesc != NULL)
+ {
+ pEpInDesc->wMaxPacketSize = CDC_DATA_FS_MAX_PACKET_SIZE;
+ }
+
+ *length = (uint16_t)sizeof(USBD_CDC_CfgDesc);
+ return USBD_CDC_CfgDesc;
+}
+
+/**
+ * @brief USBD_CDC_GetDeviceQualifierDescriptor
+ * return Device Qualifier descriptor
+ * @param length : pointer data length
+ * @retval pointer to descriptor buffer
+ */
+uint8_t *USBD_CDC_GetDeviceQualifierDescriptor(uint16_t *length)
+{
+ *length = (uint16_t)sizeof(USBD_CDC_DeviceQualifierDesc);
+
+ return USBD_CDC_DeviceQualifierDesc;
+}
+#endif /* USE_USBD_COMPOSITE */
+/**
+ * @brief USBD_CDC_RegisterInterface
+ * @param pdev: device instance
+ * @param fops: CD Interface callback
+ * @retval status
+ */
+uint8_t USBD_CDC_RegisterInterface(USBD_HandleTypeDef *pdev,
+ USBD_CDC_ItfTypeDef *fops)
+{
+ if (fops == NULL)
+ {
+ return (uint8_t)USBD_FAIL;
+ }
+
+ pdev->pUserData[pdev->classId] = fops;
+
+ return (uint8_t)USBD_OK;
+}
+
+
+/**
+ * @brief USBD_CDC_SetTxBuffer
+ * @param pdev: device instance
+ * @param pbuff: Tx Buffer
+ * @param length: length of data to be sent
+ * @param ClassId: The Class ID
+ * @retval status
+ */
+#ifdef USE_USBD_COMPOSITE
+uint8_t USBD_CDC_SetTxBuffer(USBD_HandleTypeDef *pdev,
+ uint8_t *pbuff, uint32_t length, uint8_t ClassId)
+{
+ USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassDataCmsit[ClassId];
+#else
+uint8_t USBD_CDC_SetTxBuffer(USBD_HandleTypeDef *pdev,
+ uint8_t *pbuff, uint32_t length)
+{
+ USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
+#endif /* USE_USBD_COMPOSITE */
+
+ if (hcdc == NULL)
+ {
+ return (uint8_t)USBD_FAIL;
+ }
+
+ hcdc->TxBuffer = pbuff;
+ hcdc->TxLength = length;
+
+ return (uint8_t)USBD_OK;
+}
+
+/**
+ * @brief USBD_CDC_SetRxBuffer
+ * @param pdev: device instance
+ * @param pbuff: Rx Buffer
+ * @retval status
+ */
+uint8_t USBD_CDC_SetRxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff)
+{
+ USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
+
+ if (hcdc == NULL)
+ {
+ return (uint8_t)USBD_FAIL;
+ }
+
+ hcdc->RxBuffer = pbuff;
+
+ return (uint8_t)USBD_OK;
+}
+
+
+/**
+ * @brief USBD_CDC_TransmitPacket
+ * Transmit packet on IN endpoint
+ * @param pdev: device instance
+ * @param ClassId: The Class ID
+ * @retval status
+ */
+#ifdef USE_USBD_COMPOSITE
+uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev, uint8_t ClassId)
+{
+ USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassDataCmsit[ClassId];
+#else
+uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev)
+{
+ USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
+#endif /* USE_USBD_COMPOSITE */
+
+ USBD_StatusTypeDef ret = USBD_BUSY;
+
+#ifdef USE_USBD_COMPOSITE
+ /* Get the Endpoints addresses allocated for this class instance */
+ CDCInEpAdd = USBD_CoreGetEPAdd(pdev, USBD_EP_IN, USBD_EP_TYPE_BULK, ClassId);
+#endif /* USE_USBD_COMPOSITE */
+
+ if (hcdc == NULL)
+ {
+ return (uint8_t)USBD_FAIL;
+ }
+
+ if (hcdc->TxState == 0U)
+ {
+ /* Tx Transfer in progress */
+ hcdc->TxState = 1U;
+
+ /* Update the packet total length */
+ pdev->ep_in[CDCInEpAdd & 0xFU].total_length = hcdc->TxLength;
+
+ /* Transmit next packet */
+ (void)USBD_LL_Transmit(pdev, CDCInEpAdd, hcdc->TxBuffer, hcdc->TxLength);
+
+ ret = USBD_OK;
+ }
+
+ return (uint8_t)ret;
+}
+
+/**
+ * @brief USBD_CDC_ReceivePacket
+ * prepare OUT Endpoint for reception
+ * @param pdev: device instance
+ * @retval status
+ */
+uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev)
+{
+ USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassDataCmsit[pdev->classId];
+
+#ifdef USE_USBD_COMPOSITE
+ /* Get the Endpoints addresses allocated for this class instance */
+ CDCOutEpAdd = USBD_CoreGetEPAdd(pdev, USBD_EP_OUT, USBD_EP_TYPE_BULK, (uint8_t)pdev->classId);
+#endif /* USE_USBD_COMPOSITE */
+
+ if (pdev->pClassDataCmsit[pdev->classId] == NULL)
+ {
+ return (uint8_t)USBD_FAIL;
+ }
+
+ if (pdev->dev_speed == USBD_SPEED_HIGH)
+ {
+ /* Prepare Out endpoint to receive next packet */
+ (void)USBD_LL_PrepareReceive(pdev, CDCOutEpAdd, hcdc->RxBuffer,
+ CDC_DATA_HS_OUT_PACKET_SIZE);
+ }
+ else
+ {
+ /* Prepare Out endpoint to receive next packet */
+ (void)USBD_LL_PrepareReceive(pdev, CDCOutEpAdd, hcdc->RxBuffer,
+ CDC_DATA_FS_OUT_PACKET_SIZE);
+ }
+
+ return (uint8_t)USBD_OK;
+}
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
diff --git a/firmware/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_core.h b/firmware/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_core.h
new file mode 100644
index 0000000..4672921
--- /dev/null
+++ b/firmware/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_core.h
@@ -0,0 +1,175 @@
+/**
+ ******************************************************************************
+ * @file usbd_core.h
+ * @author MCD Application Team
+ * @brief Header file for usbd_core.c file
+ ******************************************************************************
+ * @attention
+ *
+ * Copyright (c) 2015 STMicroelectronics.
+ * All rights reserved.
+ *
+ * This software is licensed under terms that can be found in the LICENSE file
+ * in the root directory of this software component.
+ * If no LICENSE file comes with this software, it is provided AS-IS.
+ *
+ ******************************************************************************
+ */
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __USBD_CORE_H
+#define __USBD_CORE_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Includes ------------------------------------------------------------------*/
+#include "usbd_conf.h"
+#include "usbd_def.h"
+#include "usbd_ioreq.h"
+#include "usbd_ctlreq.h"
+
+/** @addtogroup STM32_USB_DEVICE_LIBRARY
+ * @{
+ */
+
+/** @defgroup USBD_CORE
+ * @brief This file is the Header file for usbd_core.c file
+ * @{
+ */
+
+
+/** @defgroup USBD_CORE_Exported_Defines
+ * @{
+ */
+#ifndef USBD_DEBUG_LEVEL
+#define USBD_DEBUG_LEVEL 0U
+#endif /* USBD_DEBUG_LEVEL */
+/**
+ * @}
+ */
+
+
+/** @defgroup USBD_CORE_Exported_TypesDefinitions
+ * @{
+ */
+
+
+/**
+ * @}
+ */
+
+
+
+/** @defgroup USBD_CORE_Exported_Macros
+ * @{
+ */
+
+/**
+ * @}
+ */
+
+/** @defgroup USBD_CORE_Exported_Variables
+ * @{
+ */
+#define USBD_SOF USBD_LL_SOF
+/**
+ * @}
+ */
+
+/** @defgroup USBD_CORE_Exported_FunctionsPrototype
+ * @{
+ */
+USBD_StatusTypeDef USBD_Init(USBD_HandleTypeDef *pdev, USBD_DescriptorsTypeDef *pdesc, uint8_t id);
+USBD_StatusTypeDef USBD_DeInit(USBD_HandleTypeDef *pdev);
+USBD_StatusTypeDef USBD_Start(USBD_HandleTypeDef *pdev);
+USBD_StatusTypeDef USBD_Stop(USBD_HandleTypeDef *pdev);
+USBD_StatusTypeDef USBD_RegisterClass(USBD_HandleTypeDef *pdev, USBD_ClassTypeDef *pclass);
+#if (USBD_USER_REGISTER_CALLBACK == 1U)
+USBD_StatusTypeDef USBD_RegisterDevStateCallback(USBD_HandleTypeDef *pdev, USBD_DevStateCallbackTypeDef pUserCallback);
+#endif /* USBD_USER_REGISTER_CALLBACK */
+
+#ifdef USE_USBD_COMPOSITE
+USBD_StatusTypeDef USBD_RegisterClassComposite(USBD_HandleTypeDef *pdev, USBD_ClassTypeDef *pclass,
+ USBD_CompositeClassTypeDef classtype, uint8_t *EpAddr);
+
+USBD_StatusTypeDef USBD_UnRegisterClassComposite(USBD_HandleTypeDef *pdev);
+uint8_t USBD_CoreGetEPAdd(USBD_HandleTypeDef *pdev, uint8_t ep_dir, uint8_t ep_type, uint8_t ClassId);
+#endif /* USE_USBD_COMPOSITE */
+
+uint8_t USBD_CoreFindIF(USBD_HandleTypeDef *pdev, uint8_t index);
+uint8_t USBD_CoreFindEP(USBD_HandleTypeDef *pdev, uint8_t index);
+
+USBD_StatusTypeDef USBD_RunTestMode(USBD_HandleTypeDef *pdev);
+USBD_StatusTypeDef USBD_SetClassConfig(USBD_HandleTypeDef *pdev, uint8_t cfgidx);
+USBD_StatusTypeDef USBD_ClrClassConfig(USBD_HandleTypeDef *pdev, uint8_t cfgidx);
+
+USBD_StatusTypeDef USBD_LL_SetupStage(USBD_HandleTypeDef *pdev, uint8_t *psetup);
+USBD_StatusTypeDef USBD_LL_DataOutStage(USBD_HandleTypeDef *pdev, uint8_t epnum, uint8_t *pdata);
+USBD_StatusTypeDef USBD_LL_DataInStage(USBD_HandleTypeDef *pdev, uint8_t epnum, uint8_t *pdata);
+
+USBD_StatusTypeDef USBD_LL_Reset(USBD_HandleTypeDef *pdev);
+USBD_StatusTypeDef USBD_LL_SetSpeed(USBD_HandleTypeDef *pdev, USBD_SpeedTypeDef speed);
+USBD_StatusTypeDef USBD_LL_Suspend(USBD_HandleTypeDef *pdev);
+USBD_StatusTypeDef USBD_LL_Resume(USBD_HandleTypeDef *pdev);
+
+USBD_StatusTypeDef USBD_LL_SOF(USBD_HandleTypeDef *pdev);
+USBD_StatusTypeDef USBD_LL_IsoINIncomplete(USBD_HandleTypeDef *pdev, uint8_t epnum);
+USBD_StatusTypeDef USBD_LL_IsoOUTIncomplete(USBD_HandleTypeDef *pdev, uint8_t epnum);
+
+USBD_StatusTypeDef USBD_LL_DevConnected(USBD_HandleTypeDef *pdev);
+USBD_StatusTypeDef USBD_LL_DevDisconnected(USBD_HandleTypeDef *pdev);
+
+/* USBD Low Level Driver */
+USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev);
+USBD_StatusTypeDef USBD_LL_DeInit(USBD_HandleTypeDef *pdev);
+USBD_StatusTypeDef USBD_LL_Start(USBD_HandleTypeDef *pdev);
+USBD_StatusTypeDef USBD_LL_Stop(USBD_HandleTypeDef *pdev);
+
+USBD_StatusTypeDef USBD_LL_OpenEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr,
+ uint8_t ep_type, uint16_t ep_mps);
+
+USBD_StatusTypeDef USBD_LL_CloseEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr);
+USBD_StatusTypeDef USBD_LL_FlushEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr);
+USBD_StatusTypeDef USBD_LL_StallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr);
+USBD_StatusTypeDef USBD_LL_ClearStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr);
+USBD_StatusTypeDef USBD_LL_SetUSBAddress(USBD_HandleTypeDef *pdev, uint8_t dev_addr);
+
+USBD_StatusTypeDef USBD_LL_Transmit(USBD_HandleTypeDef *pdev, uint8_t ep_addr,
+ uint8_t *pbuf, uint32_t size);
+
+USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev, uint8_t ep_addr,
+ uint8_t *pbuf, uint32_t size);
+
+#ifdef USBD_HS_TESTMODE_ENABLE
+USBD_StatusTypeDef USBD_LL_SetTestMode(USBD_HandleTypeDef *pdev, uint8_t testmode);
+#endif /* USBD_HS_TESTMODE_ENABLE */
+
+uint8_t USBD_LL_IsStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr);
+uint32_t USBD_LL_GetRxDataSize(USBD_HandleTypeDef *pdev, uint8_t ep_addr);
+
+void USBD_LL_Delay(uint32_t Delay);
+
+void *USBD_GetEpDesc(uint8_t *pConfDesc, uint8_t EpAddr);
+USBD_DescHeaderTypeDef *USBD_GetNextDesc(uint8_t *pbuf, uint16_t *ptr);
+
+/**
+ * @}
+ */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __USBD_CORE_H */
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+
diff --git a/firmware/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_ctlreq.h b/firmware/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_ctlreq.h
new file mode 100644
index 0000000..6c45d6c
--- /dev/null
+++ b/firmware/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_ctlreq.h
@@ -0,0 +1,101 @@
+/**
+ ******************************************************************************
+ * @file usbd_req.h
+ * @author MCD Application Team
+ * @brief Header file for the usbd_req.c file
+ ******************************************************************************
+ * @attention
+ *
+ * Copyright (c) 2015 STMicroelectronics.
+ * All rights reserved.
+ *
+ * This software is licensed under terms that can be found in the LICENSE file
+ * in the root directory of this software component.
+ * If no LICENSE file comes with this software, it is provided AS-IS.
+ *
+ ******************************************************************************
+ */
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __USB_REQUEST_H
+#define __USB_REQUEST_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Includes ------------------------------------------------------------------*/
+#include "usbd_def.h"
+
+
+/** @addtogroup STM32_USB_DEVICE_LIBRARY
+ * @{
+ */
+
+/** @defgroup USBD_REQ
+ * @brief header file for the usbd_req.c file
+ * @{
+ */
+
+/** @defgroup USBD_REQ_Exported_Defines
+ * @{
+ */
+/**
+ * @}
+ */
+
+
+/** @defgroup USBD_REQ_Exported_Types
+ * @{
+ */
+/**
+ * @}
+ */
+
+
+
+/** @defgroup USBD_REQ_Exported_Macros
+ * @{
+ */
+/**
+ * @}
+ */
+
+/** @defgroup USBD_REQ_Exported_Variables
+ * @{
+ */
+/**
+ * @}
+ */
+
+/** @defgroup USBD_REQ_Exported_FunctionsPrototype
+ * @{
+ */
+
+USBD_StatusTypeDef USBD_StdDevReq(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
+USBD_StatusTypeDef USBD_StdItfReq(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
+USBD_StatusTypeDef USBD_StdEPReq(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
+
+void USBD_CtlError(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
+void USBD_ParseSetupRequest(USBD_SetupReqTypedef *req, uint8_t *pdata);
+void USBD_GetString(uint8_t *desc, uint8_t *unicode, uint16_t *len);
+
+/**
+ * @}
+ */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __USB_REQUEST_H */
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+
diff --git a/firmware/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_def.h b/firmware/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_def.h
new file mode 100644
index 0000000..5b2394e
--- /dev/null
+++ b/firmware/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_def.h
@@ -0,0 +1,524 @@
+/**
+ ******************************************************************************
+ * @file usbd_def.h
+ * @author MCD Application Team
+ * @brief General defines for the usb device library
+ ******************************************************************************
+ * @attention
+ *
+ * Copyright (c) 2015 STMicroelectronics.
+ * All rights reserved.
+ *
+ * This software is licensed under terms that can be found in the LICENSE file
+ * in the root directory of this software component.
+ * If no LICENSE file comes with this software, it is provided AS-IS.
+ *
+ ******************************************************************************
+ */
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __USBD_DEF_H
+#define __USBD_DEF_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Includes ------------------------------------------------------------------*/
+#include "usbd_conf.h"
+
+/** @addtogroup STM32_USBD_DEVICE_LIBRARY
+ * @{
+ */
+
+/** @defgroup USB_DEF
+ * @brief general defines for the usb device library file
+ * @{
+ */
+
+/** @defgroup USB_DEF_Exported_Defines
+ * @{
+ */
+
+#ifndef NULL
+#define NULL 0U
+#endif /* NULL */
+
+#ifndef USBD_MAX_NUM_INTERFACES
+#define USBD_MAX_NUM_INTERFACES 1U
+#endif /* USBD_MAX_NUM_CONFIGURATION */
+
+#ifndef USBD_MAX_NUM_CONFIGURATION
+#define USBD_MAX_NUM_CONFIGURATION 1U
+#endif /* USBD_MAX_NUM_CONFIGURATION */
+
+#ifdef USE_USBD_COMPOSITE
+#ifndef USBD_MAX_SUPPORTED_CLASS
+#define USBD_MAX_SUPPORTED_CLASS 4U
+#endif /* USBD_MAX_SUPPORTED_CLASS */
+#else
+#ifndef USBD_MAX_SUPPORTED_CLASS
+#define USBD_MAX_SUPPORTED_CLASS 1U
+#endif /* USBD_MAX_SUPPORTED_CLASS */
+#endif /* USE_USBD_COMPOSITE */
+
+#ifndef USBD_MAX_CLASS_ENDPOINTS
+#define USBD_MAX_CLASS_ENDPOINTS 5U
+#endif /* USBD_MAX_CLASS_ENDPOINTS */
+
+#ifndef USBD_MAX_CLASS_INTERFACES
+#define USBD_MAX_CLASS_INTERFACES 5U
+#endif /* USBD_MAX_CLASS_INTERFACES */
+
+#ifndef USBD_LPM_ENABLED
+#define USBD_LPM_ENABLED 0U
+#endif /* USBD_LPM_ENABLED */
+
+#ifndef USBD_SELF_POWERED
+#define USBD_SELF_POWERED 1U
+#endif /*USBD_SELF_POWERED */
+
+#ifndef USBD_MAX_POWER
+#define USBD_MAX_POWER 0x32U /* 100 mA */
+#endif /* USBD_MAX_POWER */
+
+#ifndef USBD_SUPPORT_USER_STRING_DESC
+#define USBD_SUPPORT_USER_STRING_DESC 0U
+#endif /* USBD_SUPPORT_USER_STRING_DESC */
+
+#ifndef USBD_CLASS_USER_STRING_DESC
+#define USBD_CLASS_USER_STRING_DESC 0U
+#endif /* USBD_CLASS_USER_STRING_DESC */
+
+#define USB_LEN_DEV_QUALIFIER_DESC 0x0AU
+#define USB_LEN_DEV_DESC 0x12U
+#define USB_LEN_CFG_DESC 0x09U
+#define USB_LEN_IF_DESC 0x09U
+#define USB_LEN_EP_DESC 0x07U
+#define USB_LEN_OTG_DESC 0x03U
+#define USB_LEN_LANGID_STR_DESC 0x04U
+#define USB_LEN_OTHER_SPEED_DESC_SIZ 0x09U
+
+#define USBD_IDX_LANGID_STR 0x00U
+#define USBD_IDX_MFC_STR 0x01U
+#define USBD_IDX_PRODUCT_STR 0x02U
+#define USBD_IDX_SERIAL_STR 0x03U
+#define USBD_IDX_CONFIG_STR 0x04U
+#define USBD_IDX_INTERFACE_STR 0x05U
+
+#define USB_REQ_TYPE_STANDARD 0x00U
+#define USB_REQ_TYPE_CLASS 0x20U
+#define USB_REQ_TYPE_VENDOR 0x40U
+#define USB_REQ_TYPE_MASK 0x60U
+
+#define USB_REQ_RECIPIENT_DEVICE 0x00U
+#define USB_REQ_RECIPIENT_INTERFACE 0x01U
+#define USB_REQ_RECIPIENT_ENDPOINT 0x02U
+#define USB_REQ_RECIPIENT_MASK 0x03U
+
+#define USB_REQ_GET_STATUS 0x00U
+#define USB_REQ_CLEAR_FEATURE 0x01U
+#define USB_REQ_SET_FEATURE 0x03U
+#define USB_REQ_SET_ADDRESS 0x05U
+#define USB_REQ_GET_DESCRIPTOR 0x06U
+#define USB_REQ_SET_DESCRIPTOR 0x07U
+#define USB_REQ_GET_CONFIGURATION 0x08U
+#define USB_REQ_SET_CONFIGURATION 0x09U
+#define USB_REQ_GET_INTERFACE 0x0AU
+#define USB_REQ_SET_INTERFACE 0x0BU
+#define USB_REQ_SYNCH_FRAME 0x0CU
+
+#define USB_DESC_TYPE_DEVICE 0x01U
+#define USB_DESC_TYPE_CONFIGURATION 0x02U
+#define USB_DESC_TYPE_STRING 0x03U
+#define USB_DESC_TYPE_INTERFACE 0x04U
+#define USB_DESC_TYPE_ENDPOINT 0x05U
+#define USB_DESC_TYPE_DEVICE_QUALIFIER 0x06U
+#define USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION 0x07U
+#define USB_DESC_TYPE_IAD 0x0BU
+#define USB_DESC_TYPE_BOS 0x0FU
+
+#define USB_CONFIG_REMOTE_WAKEUP 0x02U
+#define USB_CONFIG_SELF_POWERED 0x01U
+
+#define USB_FEATURE_EP_HALT 0x00U
+#define USB_FEATURE_REMOTE_WAKEUP 0x01U
+#define USB_FEATURE_TEST_MODE 0x02U
+
+#define USB_DEVICE_CAPABITY_TYPE 0x10U
+
+#define USB_CONF_DESC_SIZE 0x09U
+#define USB_IF_DESC_SIZE 0x09U
+#define USB_EP_DESC_SIZE 0x07U
+#define USB_IAD_DESC_SIZE 0x08U
+
+#define USB_HS_MAX_PACKET_SIZE 512U
+#define USB_FS_MAX_PACKET_SIZE 64U
+#define USB_MAX_EP0_SIZE 64U
+
+/* Device Status */
+#define USBD_STATE_DEFAULT 0x01U
+#define USBD_STATE_ADDRESSED 0x02U
+#define USBD_STATE_CONFIGURED 0x03U
+#define USBD_STATE_SUSPENDED 0x04U
+
+
+/* EP0 State */
+#define USBD_EP0_IDLE 0x00U
+#define USBD_EP0_SETUP 0x01U
+#define USBD_EP0_DATA_IN 0x02U
+#define USBD_EP0_DATA_OUT 0x03U
+#define USBD_EP0_STATUS_IN 0x04U
+#define USBD_EP0_STATUS_OUT 0x05U
+#define USBD_EP0_STALL 0x06U
+
+#define USBD_EP_TYPE_CTRL 0x00U
+#define USBD_EP_TYPE_ISOC 0x01U
+#define USBD_EP_TYPE_BULK 0x02U
+#define USBD_EP_TYPE_INTR 0x03U
+
+#ifdef USE_USBD_COMPOSITE
+#define USBD_EP_IN 0x80U
+#define USBD_EP_OUT 0x00U
+#define USBD_FUNC_DESCRIPTOR_TYPE 0x24U
+#define USBD_DESC_SUBTYPE_ACM 0x0FU
+#define USBD_DESC_ECM_BCD_LOW 0x00U
+#define USBD_DESC_ECM_BCD_HIGH 0x10U
+#endif /* USE_USBD_COMPOSITE */
+/**
+ * @}
+ */
+
+
+/** @defgroup USBD_DEF_Exported_TypesDefinitions
+ * @{
+ */
+
+typedef struct usb_setup_req
+{
+ uint8_t bmRequest;
+ uint8_t bRequest;
+ uint16_t wValue;
+ uint16_t wIndex;
+ uint16_t wLength;
+} USBD_SetupReqTypedef;
+
+typedef struct
+{
+ uint8_t bLength;
+ uint8_t bDescriptorType;
+ uint16_t wTotalLength;
+ uint8_t bNumInterfaces;
+ uint8_t bConfigurationValue;
+ uint8_t iConfiguration;
+ uint8_t bmAttributes;
+ uint8_t bMaxPower;
+} __PACKED USBD_ConfigDescTypeDef;
+
+typedef struct
+{
+ uint8_t bLength;
+ uint8_t bDescriptorType;
+ uint16_t wTotalLength;
+ uint8_t bNumDeviceCaps;
+} USBD_BosDescTypeDef;
+
+typedef struct
+{
+ uint8_t bLength;
+ uint8_t bDescriptorType;
+ uint8_t bEndpointAddress;
+ uint8_t bmAttributes;
+ uint16_t wMaxPacketSize;
+ uint8_t bInterval;
+} __PACKED USBD_EpDescTypeDef;
+
+typedef struct
+{
+ uint8_t bLength;
+ uint8_t bDescriptorType;
+ uint8_t bDescriptorSubType;
+} USBD_DescHeaderTypeDef;
+
+struct _USBD_HandleTypeDef;
+
+typedef struct _Device_cb
+{
+ uint8_t (*Init)(struct _USBD_HandleTypeDef *pdev, uint8_t cfgidx);
+ uint8_t (*DeInit)(struct _USBD_HandleTypeDef *pdev, uint8_t cfgidx);
+ /* Control Endpoints*/
+ uint8_t (*Setup)(struct _USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
+ uint8_t (*EP0_TxSent)(struct _USBD_HandleTypeDef *pdev);
+ uint8_t (*EP0_RxReady)(struct _USBD_HandleTypeDef *pdev);
+ /* Class Specific Endpoints*/
+ uint8_t (*DataIn)(struct _USBD_HandleTypeDef *pdev, uint8_t epnum);
+ uint8_t (*DataOut)(struct _USBD_HandleTypeDef *pdev, uint8_t epnum);
+ uint8_t (*SOF)(struct _USBD_HandleTypeDef *pdev);
+ uint8_t (*IsoINIncomplete)(struct _USBD_HandleTypeDef *pdev, uint8_t epnum);
+ uint8_t (*IsoOUTIncomplete)(struct _USBD_HandleTypeDef *pdev, uint8_t epnum);
+
+ uint8_t *(*GetHSConfigDescriptor)(uint16_t *length);
+ uint8_t *(*GetFSConfigDescriptor)(uint16_t *length);
+ uint8_t *(*GetOtherSpeedConfigDescriptor)(uint16_t *length);
+ uint8_t *(*GetDeviceQualifierDescriptor)(uint16_t *length);
+#if (USBD_SUPPORT_USER_STRING_DESC == 1U)
+ uint8_t *(*GetUsrStrDescriptor)(struct _USBD_HandleTypeDef *pdev, uint8_t index, uint16_t *length);
+#endif /* USBD_SUPPORT_USER_STRING_DESC */
+
+} USBD_ClassTypeDef;
+
+/* Following USB Device Speed */
+typedef enum
+{
+ USBD_SPEED_HIGH = 0U,
+ USBD_SPEED_FULL = 1U,
+ USBD_SPEED_LOW = 2U,
+} USBD_SpeedTypeDef;
+
+/* Following USB Device status */
+typedef enum
+{
+ USBD_OK = 0U,
+ USBD_BUSY,
+ USBD_EMEM,
+ USBD_FAIL,
+} USBD_StatusTypeDef;
+
+/* USB Device descriptors structure */
+typedef struct
+{
+ uint8_t *(*GetDeviceDescriptor)(USBD_SpeedTypeDef speed, uint16_t *length);
+ uint8_t *(*GetLangIDStrDescriptor)(USBD_SpeedTypeDef speed, uint16_t *length);
+ uint8_t *(*GetManufacturerStrDescriptor)(USBD_SpeedTypeDef speed, uint16_t *length);
+ uint8_t *(*GetProductStrDescriptor)(USBD_SpeedTypeDef speed, uint16_t *length);
+ uint8_t *(*GetSerialStrDescriptor)(USBD_SpeedTypeDef speed, uint16_t *length);
+ uint8_t *(*GetConfigurationStrDescriptor)(USBD_SpeedTypeDef speed, uint16_t *length);
+ uint8_t *(*GetInterfaceStrDescriptor)(USBD_SpeedTypeDef speed, uint16_t *length);
+#if (USBD_CLASS_USER_STRING_DESC == 1)
+ uint8_t *(*GetUserStrDescriptor)(USBD_SpeedTypeDef speed, uint8_t idx, uint16_t *length);
+#endif /* USBD_CLASS_USER_STRING_DESC */
+#if ((USBD_LPM_ENABLED == 1U) || (USBD_CLASS_BOS_ENABLED == 1))
+ uint8_t *(*GetBOSDescriptor)(USBD_SpeedTypeDef speed, uint16_t *length);
+#endif /* (USBD_LPM_ENABLED == 1U) || (USBD_CLASS_BOS_ENABLED == 1) */
+} USBD_DescriptorsTypeDef;
+
+/* USB Device handle structure */
+typedef struct
+{
+ uint32_t total_length;
+ uint32_t rem_length;
+ uint32_t bInterval;
+ uint16_t maxpacket;
+ uint8_t status;
+ uint8_t is_used;
+ uint8_t *pbuffer;
+} USBD_EndpointTypeDef;
+
+#ifdef USE_USBD_COMPOSITE
+typedef enum
+{
+ CLASS_TYPE_NONE = 0,
+ CLASS_TYPE_HID = 1,
+ CLASS_TYPE_CDC = 2,
+ CLASS_TYPE_MSC = 3,
+ CLASS_TYPE_DFU = 4,
+ CLASS_TYPE_CHID = 5,
+ CLASS_TYPE_AUDIO = 6,
+ CLASS_TYPE_ECM = 7,
+ CLASS_TYPE_RNDIS = 8,
+ CLASS_TYPE_MTP = 9,
+ CLASS_TYPE_VIDEO = 10,
+ CLASS_TYPE_PRINTER = 11,
+ CLASS_TYPE_CCID = 12,
+} USBD_CompositeClassTypeDef;
+
+
+/* USB Device handle structure */
+typedef struct
+{
+ uint8_t add;
+ uint8_t type;
+ uint8_t size;
+ uint8_t is_used;
+} USBD_EPTypeDef;
+
+/* USB Device handle structure */
+typedef struct
+{
+ USBD_CompositeClassTypeDef ClassType;
+ uint32_t ClassId;
+ uint32_t Active;
+ uint32_t NumEps;
+ USBD_EPTypeDef Eps[USBD_MAX_CLASS_ENDPOINTS];
+ uint8_t *EpAdd;
+ uint32_t NumIf;
+ uint8_t Ifs[USBD_MAX_CLASS_INTERFACES];
+ uint32_t CurrPcktSze;
+} USBD_CompositeElementTypeDef;
+#endif /* USE_USBD_COMPOSITE */
+
+/* USB Device handle structure */
+typedef struct _USBD_HandleTypeDef
+{
+ uint8_t id;
+ uint32_t dev_config;
+ uint32_t dev_default_config;
+ uint32_t dev_config_status;
+ USBD_SpeedTypeDef dev_speed;
+ USBD_EndpointTypeDef ep_in[16];
+ USBD_EndpointTypeDef ep_out[16];
+ __IO uint32_t ep0_state;
+ uint32_t ep0_data_len;
+ __IO uint8_t dev_state;
+ __IO uint8_t dev_old_state;
+ uint8_t dev_address;
+ uint8_t dev_connection_status;
+ uint8_t dev_test_mode;
+ uint32_t dev_remote_wakeup;
+ uint8_t ConfIdx;
+
+ USBD_SetupReqTypedef request;
+ USBD_DescriptorsTypeDef *pDesc;
+ USBD_ClassTypeDef *pClass[USBD_MAX_SUPPORTED_CLASS];
+ void *pClassData;
+ void *pClassDataCmsit[USBD_MAX_SUPPORTED_CLASS];
+ void *pUserData[USBD_MAX_SUPPORTED_CLASS];
+ void *pData;
+ void *pBosDesc;
+ void *pConfDesc;
+ uint32_t classId;
+ uint32_t NumClasses;
+#ifdef USE_USBD_COMPOSITE
+ USBD_CompositeElementTypeDef tclasslist[USBD_MAX_SUPPORTED_CLASS];
+#endif /* USE_USBD_COMPOSITE */
+#if (USBD_USER_REGISTER_CALLBACK == 1U)
+ void (* DevStateCallback)(uint8_t dev_state, uint8_t cfgidx); /*!< User Notification callback */
+#endif /* USBD_USER_REGISTER_CALLBACK */
+} USBD_HandleTypeDef;
+
+#if (USBD_USER_REGISTER_CALLBACK == 1U)
+typedef void (*USBD_DevStateCallbackTypeDef)(uint8_t dev_state, uint8_t cfgidx); /*!< pointer to User callback function */
+#endif /* USBD_USER_REGISTER_CALLBACK */
+
+/* USB Device endpoint direction */
+typedef enum
+{
+ OUT = 0x00,
+ IN = 0x80,
+} USBD_EPDirectionTypeDef;
+
+typedef enum
+{
+ NETWORK_CONNECTION = 0x00,
+ RESPONSE_AVAILABLE = 0x01,
+ CONNECTION_SPEED_CHANGE = 0x2A
+} USBD_CDC_NotifCodeTypeDef;
+/**
+ * @}
+ */
+
+
+
+/** @defgroup USBD_DEF_Exported_Macros
+ * @{
+ */
+__STATIC_INLINE uint16_t SWAPBYTE(uint8_t *addr)
+{
+ uint16_t _SwapVal;
+ uint16_t _Byte1;
+ uint16_t _Byte2;
+ uint8_t *_pbuff = addr;
+
+ _Byte1 = *(uint8_t *)_pbuff;
+ _pbuff++;
+ _Byte2 = *(uint8_t *)_pbuff;
+
+ _SwapVal = (_Byte2 << 8) | _Byte1;
+
+ return _SwapVal;
+}
+
+#ifndef LOBYTE
+#define LOBYTE(x) ((uint8_t)((x) & 0x00FFU))
+#endif /* LOBYTE */
+
+#ifndef HIBYTE
+#define HIBYTE(x) ((uint8_t)(((x) & 0xFF00U) >> 8U))
+#endif /* HIBYTE */
+
+#ifndef MIN
+#define MIN(a, b) (((a) < (b)) ? (a) : (b))
+#endif /* MIN */
+
+#ifndef MAX
+#define MAX(a, b) (((a) > (b)) ? (a) : (b))
+#endif /* MAX */
+
+#if defined ( __GNUC__ )
+#ifndef __weak
+#define __weak __attribute__((weak))
+#endif /* __weak */
+#ifndef __packed
+#define __packed __attribute__((__packed__))
+#endif /* __packed */
+#endif /* __GNUC__ */
+
+
+/* In HS mode and when the DMA is used, all variables and data structures dealing
+ with the DMA during the transaction process should be 4-bytes aligned */
+
+#if defined ( __GNUC__ ) && !defined (__CC_ARM) /* GNU Compiler */
+#ifndef __ALIGN_END
+#define __ALIGN_END __attribute__ ((aligned (4U)))
+#endif /* __ALIGN_END */
+#ifndef __ALIGN_BEGIN
+#define __ALIGN_BEGIN
+#endif /* __ALIGN_BEGIN */
+#else
+#ifndef __ALIGN_END
+#define __ALIGN_END
+#endif /* __ALIGN_END */
+#ifndef __ALIGN_BEGIN
+#if defined (__CC_ARM) /* ARM Compiler */
+#define __ALIGN_BEGIN __align(4U)
+#elif defined (__ICCARM__) /* IAR Compiler */
+#define __ALIGN_BEGIN
+#endif /* __CC_ARM */
+#endif /* __ALIGN_BEGIN */
+#endif /* __GNUC__ */
+
+
+/**
+ * @}
+ */
+
+/** @defgroup USBD_DEF_Exported_Variables
+ * @{
+ */
+
+/**
+ * @}
+ */
+
+/** @defgroup USBD_DEF_Exported_FunctionsPrototype
+ * @{
+ */
+
+/**
+ * @}
+ */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __USBD_DEF_H */
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
diff --git a/firmware/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_ioreq.h b/firmware/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_ioreq.h
new file mode 100644
index 0000000..15197b9
--- /dev/null
+++ b/firmware/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_ioreq.h
@@ -0,0 +1,113 @@
+/**
+ ******************************************************************************
+ * @file usbd_ioreq.h
+ * @author MCD Application Team
+ * @brief Header file for the usbd_ioreq.c file
+ ******************************************************************************
+ * @attention
+ *
+ * Copyright (c) 2015 STMicroelectronics.
+ * All rights reserved.
+ *
+ * This software is licensed under terms that can be found in the LICENSE file
+ * in the root directory of this software component.
+ * If no LICENSE file comes with this software, it is provided AS-IS.
+ *
+ ******************************************************************************
+ */
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __USBD_IOREQ_H
+#define __USBD_IOREQ_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Includes ------------------------------------------------------------------*/
+#include "usbd_def.h"
+#include "usbd_core.h"
+
+/** @addtogroup STM32_USB_DEVICE_LIBRARY
+ * @{
+ */
+
+/** @defgroup USBD_IOREQ
+ * @brief header file for the usbd_ioreq.c file
+ * @{
+ */
+
+/** @defgroup USBD_IOREQ_Exported_Defines
+ * @{
+ */
+/**
+ * @}
+ */
+
+
+/** @defgroup USBD_IOREQ_Exported_Types
+ * @{
+ */
+
+
+/**
+ * @}
+ */
+
+
+
+/** @defgroup USBD_IOREQ_Exported_Macros
+ * @{
+ */
+
+/**
+ * @}
+ */
+
+/** @defgroup USBD_IOREQ_Exported_Variables
+ * @{
+ */
+
+/**
+ * @}
+ */
+
+/** @defgroup USBD_IOREQ_Exported_FunctionsPrototype
+ * @{
+ */
+
+USBD_StatusTypeDef USBD_CtlSendData(USBD_HandleTypeDef *pdev,
+ uint8_t *pbuf, uint32_t len);
+
+USBD_StatusTypeDef USBD_CtlContinueSendData(USBD_HandleTypeDef *pdev,
+ uint8_t *pbuf, uint32_t len);
+
+USBD_StatusTypeDef USBD_CtlPrepareRx(USBD_HandleTypeDef *pdev,
+ uint8_t *pbuf, uint32_t len);
+
+USBD_StatusTypeDef USBD_CtlContinueRx(USBD_HandleTypeDef *pdev,
+ uint8_t *pbuf, uint32_t len);
+
+USBD_StatusTypeDef USBD_CtlSendStatus(USBD_HandleTypeDef *pdev);
+USBD_StatusTypeDef USBD_CtlReceiveStatus(USBD_HandleTypeDef *pdev);
+
+uint32_t USBD_GetRxCount(USBD_HandleTypeDef *pdev, uint8_t ep_addr);
+
+/**
+ * @}
+ */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __USBD_IOREQ_H */
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
diff --git a/firmware/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_core.c b/firmware/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_core.c
new file mode 100644
index 0000000..bcf571e
--- /dev/null
+++ b/firmware/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_core.c
@@ -0,0 +1,1221 @@
+/**
+ ******************************************************************************
+ * @file usbd_core.c
+ * @author MCD Application Team
+ * @brief This file provides all the USBD core functions.
+ ******************************************************************************
+ * @attention
+ *
+ * Copyright (c) 2015 STMicroelectronics.
+ * All rights reserved.
+ *
+ * This software is licensed under terms that can be found in the LICENSE file
+ * in the root directory of this software component.
+ * If no LICENSE file comes with this software, it is provided AS-IS.
+ *
+ ******************************************************************************
+ */
+
+/* Includes ------------------------------------------------------------------*/
+#include "usbd_core.h"
+
+#ifdef USE_USBD_COMPOSITE
+#include "usbd_composite_builder.h"
+#endif /* USE_USBD_COMPOSITE */
+
+/** @addtogroup STM32_USBD_DEVICE_LIBRARY
+ * @{
+ */
+
+
+/** @defgroup USBD_CORE
+ * @brief usbd core module
+ * @{
+ */
+
+/** @defgroup USBD_CORE_Private_TypesDefinitions
+ * @{
+ */
+
+/**
+ * @}
+ */
+
+
+/** @defgroup USBD_CORE_Private_Defines
+ * @{
+ */
+
+/**
+ * @}
+ */
+
+
+/** @defgroup USBD_CORE_Private_Macros
+ * @{
+ */
+
+/**
+ * @}
+ */
+
+
+/** @defgroup USBD_CORE_Private_FunctionPrototypes
+ * @{
+ */
+
+/**
+ * @}
+ */
+
+/** @defgroup USBD_CORE_Private_Variables
+ * @{
+ */
+
+/**
+ * @}
+ */
+
+
+/** @defgroup USBD_CORE_Private_Functions
+ * @{
+ */
+
+/**
+ * @brief USBD_Init
+ * Initialize the device stack and load the class driver
+ * @param pdev: device instance
+ * @param pdesc: Descriptor structure address
+ * @param id: Low level core index
+ * @retval status: USBD Status
+ */
+USBD_StatusTypeDef USBD_Init(USBD_HandleTypeDef *pdev,
+ USBD_DescriptorsTypeDef *pdesc, uint8_t id)
+{
+ USBD_StatusTypeDef ret;
+
+ /* Check whether the USB Host handle is valid */
+ if (pdev == NULL)
+ {
+#if (USBD_DEBUG_LEVEL > 1U)
+ USBD_ErrLog("Invalid Device handle");
+#endif /* (USBD_DEBUG_LEVEL > 1U) */
+ return USBD_FAIL;
+ }
+
+#ifdef USE_USBD_COMPOSITE
+ /* Parse the table of classes in use */
+ for (uint32_t i = 0; i < USBD_MAX_SUPPORTED_CLASS; i++)
+ {
+ /* Unlink previous class*/
+ pdev->pClass[i] = NULL;
+ pdev->pUserData[i] = NULL;
+
+ /* Set class as inactive */
+ pdev->tclasslist[i].Active = 0;
+ pdev->NumClasses = 0;
+ pdev->classId = 0;
+ }
+#else
+ /* Unlink previous class*/
+ pdev->pClass[0] = NULL;
+ pdev->pUserData[0] = NULL;
+#endif /* USE_USBD_COMPOSITE */
+
+ pdev->pConfDesc = NULL;
+
+ /* Assign USBD Descriptors */
+ if (pdesc != NULL)
+ {
+ pdev->pDesc = pdesc;
+ }
+
+ /* Set Device initial State */
+ pdev->dev_state = USBD_STATE_DEFAULT;
+ pdev->id = id;
+
+ /* Initialize low level driver */
+ ret = USBD_LL_Init(pdev);
+
+ return ret;
+}
+
+/**
+ * @brief USBD_DeInit
+ * De-Initialize the device library
+ * @param pdev: device instance
+ * @retval status: USBD Status
+ */
+USBD_StatusTypeDef USBD_DeInit(USBD_HandleTypeDef *pdev)
+{
+ USBD_StatusTypeDef ret;
+
+ /* Disconnect the USB Device */
+ (void)USBD_LL_Stop(pdev);
+
+ /* Set Default State */
+ pdev->dev_state = USBD_STATE_DEFAULT;
+
+#ifdef USE_USBD_COMPOSITE
+ /* Parse the table of classes in use */
+ for (uint32_t i = 0; i < USBD_MAX_SUPPORTED_CLASS; i++)
+ {
+ /* Check if current class is in use */
+ if ((pdev->tclasslist[i].Active) == 1U)
+ {
+ if (pdev->pClass[i] != NULL)
+ {
+ pdev->classId = i;
+ /* Free Class Resources */
+ pdev->pClass[i]->DeInit(pdev, (uint8_t)pdev->dev_config);
+ }
+ }
+ }
+#else
+ /* Free Class Resources */
+ if (pdev->pClass[0] != NULL)
+ {
+ pdev->pClass[0]->DeInit(pdev, (uint8_t)pdev->dev_config);
+ }
+
+ pdev->pUserData[0] = NULL;
+
+#endif /* USE_USBD_COMPOSITE */
+
+ /* Free Device descriptors resources */
+ pdev->pDesc = NULL;
+ pdev->pConfDesc = NULL;
+
+ /* DeInitialize low level driver */
+ ret = USBD_LL_DeInit(pdev);
+
+ return ret;
+}
+
+/**
+ * @brief USBD_RegisterClass
+ * Link class driver to Device Core.
+ * @param pdev: Device Handle
+ * @param pclass: Class handle
+ * @retval USBD Status
+ */
+USBD_StatusTypeDef USBD_RegisterClass(USBD_HandleTypeDef *pdev, USBD_ClassTypeDef *pclass)
+{
+ uint16_t len = 0U;
+
+ if (pclass == NULL)
+ {
+#if (USBD_DEBUG_LEVEL > 1U)
+ USBD_ErrLog("Invalid Class handle");
+#endif /* (USBD_DEBUG_LEVEL > 1U) */
+ return USBD_FAIL;
+ }
+
+ /* link the class to the USB Device handle */
+ pdev->pClass[0] = pclass;
+
+ /* Get Device Configuration Descriptor */
+#ifdef USE_USB_HS
+ if (pdev->pClass[pdev->classId]->GetHSConfigDescriptor != NULL)
+ {
+ pdev->pConfDesc = (void *)pdev->pClass[pdev->classId]->GetHSConfigDescriptor(&len);
+ }
+#else /* Default USE_USB_FS */
+ if (pdev->pClass[pdev->classId]->GetFSConfigDescriptor != NULL)
+ {
+ pdev->pConfDesc = (void *)pdev->pClass[pdev->classId]->GetFSConfigDescriptor(&len);
+ }
+#endif /* USE_USB_FS */
+
+ /* Increment the NumClasses */
+ pdev->NumClasses++;
+
+ return USBD_OK;
+}
+
+#ifdef USE_USBD_COMPOSITE
+/**
+ * @brief USBD_RegisterClassComposite
+ * Link class driver to Device Core.
+ * @param pdev : Device Handle
+ * @param pclass: Class handle
+ * @param classtype: Class type
+ * @param EpAddr: Endpoint Address handle
+ * @retval USBD Status
+ */
+USBD_StatusTypeDef USBD_RegisterClassComposite(USBD_HandleTypeDef *pdev, USBD_ClassTypeDef *pclass,
+ USBD_CompositeClassTypeDef classtype, uint8_t *EpAddr)
+{
+ USBD_StatusTypeDef ret = USBD_OK;
+ uint16_t len = 0U;
+
+ if ((pdev->classId < USBD_MAX_SUPPORTED_CLASS) && (pdev->NumClasses < USBD_MAX_SUPPORTED_CLASS))
+ {
+ if ((uint32_t)pclass != 0U)
+ {
+ /* Link the class to the USB Device handle */
+ pdev->pClass[pdev->classId] = pclass;
+ ret = USBD_OK;
+
+ pdev->tclasslist[pdev->classId].EpAdd = EpAddr;
+
+ /* Call the composite class builder */
+ (void)USBD_CMPSIT_AddClass(pdev, pclass, classtype, 0);
+
+ /* Increment the ClassId for the next occurrence */
+ pdev->classId ++;
+ pdev->NumClasses ++;
+ }
+ else
+ {
+#if (USBD_DEBUG_LEVEL > 1U)
+ USBD_ErrLog("Invalid Class handle");
+#endif /* (USBD_DEBUG_LEVEL > 1U) */
+ ret = USBD_FAIL;
+ }
+ }
+
+ if (ret == USBD_OK)
+ {
+ /* Get Device Configuration Descriptor */
+#ifdef USE_USB_HS
+ pdev->pConfDesc = USBD_CMPSIT.GetHSConfigDescriptor(&len);
+#else /* Default USE_USB_FS */
+ pdev->pConfDesc = USBD_CMPSIT.GetFSConfigDescriptor(&len);
+#endif /* USE_USB_FS */
+ }
+
+ return ret;
+}
+
+/**
+ * @brief USBD_UnRegisterClassComposite
+ * UnLink all composite class drivers from Device Core.
+ * @param pdev: Device Handle
+ * @retval USBD Status
+ */
+USBD_StatusTypeDef USBD_UnRegisterClassComposite(USBD_HandleTypeDef *pdev)
+{
+ USBD_StatusTypeDef ret = USBD_OK;
+ uint8_t idx1;
+ uint8_t idx2;
+
+ /* Unroll all activated classes */
+ for (idx1 = 0; idx1 < pdev->NumClasses; idx1++)
+ {
+ /* Check if the class correspond to the requested type and if it is active */
+ if (pdev->tclasslist[idx1].Active == 1U)
+ {
+ /* Set the new class ID */
+ pdev->classId = idx1;
+
+ /* Free resources used by the selected class */
+ if (pdev->pClass[pdev->classId] != NULL)
+ {
+ /* Free Class Resources */
+ if (pdev->pClass[pdev->classId]->DeInit(pdev, (uint8_t)pdev->dev_config) != 0U)
+ {
+#if (USBD_DEBUG_LEVEL > 1U)
+ USBD_ErrLog("Class DeInit didn't succeed!, can't unregister selected class");
+#endif /* (USBD_DEBUG_LEVEL > 1U) */
+
+ ret = USBD_FAIL;
+ }
+ }
+
+ /* Free the class pointer */
+ pdev->pClass[pdev->classId] = NULL;
+
+ /* Free the class location in classes table and reset its parameters to zero */
+ pdev->tclasslist[pdev->classId].ClassType = CLASS_TYPE_NONE;
+ pdev->tclasslist[pdev->classId].ClassId = 0U;
+ pdev->tclasslist[pdev->classId].Active = 0U;
+ pdev->tclasslist[pdev->classId].NumEps = 0U;
+ pdev->tclasslist[pdev->classId].NumIf = 0U;
+ pdev->tclasslist[pdev->classId].CurrPcktSze = 0U;
+
+ for (idx2 = 0U; idx2 < USBD_MAX_CLASS_ENDPOINTS; idx2++)
+ {
+ pdev->tclasslist[pdev->classId].Eps[idx2].add = 0U;
+ pdev->tclasslist[pdev->classId].Eps[idx2].type = 0U;
+ pdev->tclasslist[pdev->classId].Eps[idx2].size = 0U;
+ pdev->tclasslist[pdev->classId].Eps[idx2].is_used = 0U;
+ }
+
+ for (idx2 = 0U; idx2 < USBD_MAX_CLASS_INTERFACES; idx2++)
+ {
+ pdev->tclasslist[pdev->classId].Ifs[idx2] = 0U;
+ }
+ }
+ }
+
+ /* Reset the configuration descriptor */
+ (void)USBD_CMPST_ClearConfDesc(pdev);
+
+ /* Reset the class ID and number of classes */
+ pdev->classId = 0U;
+ pdev->NumClasses = 0U;
+
+ return ret;
+}
+#endif /* USE_USBD_COMPOSITE */
+
+#if (USBD_USER_REGISTER_CALLBACK == 1U)
+/**
+ * @brief USBD_RegisterDevStateCallback
+ * @param pdev : Device Handle
+ * @param pUserCallback: User Callback
+ * @retval USBD Status
+ */
+USBD_StatusTypeDef USBD_RegisterDevStateCallback(USBD_HandleTypeDef *pdev, USBD_DevStateCallbackTypeDef pUserCallback)
+{
+ pdev->DevStateCallback = pUserCallback;
+
+ return USBD_OK;
+}
+#endif /* USBD_USER_REGISTER_CALLBACK */
+
+/**
+ * @brief USBD_Start
+ * Start the USB Device Core.
+ * @param pdev: Device Handle
+ * @retval USBD Status
+ */
+USBD_StatusTypeDef USBD_Start(USBD_HandleTypeDef *pdev)
+{
+#ifdef USE_USBD_COMPOSITE
+ pdev->classId = 0U;
+#endif /* USE_USBD_COMPOSITE */
+
+ /* Start the low level driver */
+ return USBD_LL_Start(pdev);
+}
+
+/**
+ * @brief USBD_Stop
+ * Stop the USB Device Core.
+ * @param pdev: Device Handle
+ * @retval USBD Status
+ */
+USBD_StatusTypeDef USBD_Stop(USBD_HandleTypeDef *pdev)
+{
+ /* Disconnect USB Device */
+ (void)USBD_LL_Stop(pdev);
+
+ /* Free Class Resources */
+#ifdef USE_USBD_COMPOSITE
+ /* Parse the table of classes in use */
+ for (uint32_t i = 0U; i < USBD_MAX_SUPPORTED_CLASS; i++)
+ {
+ /* Check if current class is in use */
+ if ((pdev->tclasslist[i].Active) == 1U)
+ {
+ if (pdev->pClass[i] != NULL)
+ {
+ pdev->classId = i;
+ /* Free Class Resources */
+ (void)pdev->pClass[i]->DeInit(pdev, (uint8_t)pdev->dev_config);
+ }
+ }
+ }
+
+ /* Reset the class ID */
+ pdev->classId = 0U;
+#else
+ if (pdev->pClass[0] != NULL)
+ {
+ (void)pdev->pClass[0]->DeInit(pdev, (uint8_t)pdev->dev_config);
+ }
+#endif /* USE_USBD_COMPOSITE */
+
+ return USBD_OK;
+}
+
+/**
+ * @brief USBD_RunTestMode
+ * Launch test mode process
+ * @param pdev: device instance
+ * @retval status
+ */
+USBD_StatusTypeDef USBD_RunTestMode(USBD_HandleTypeDef *pdev)
+{
+#ifdef USBD_HS_TESTMODE_ENABLE
+ USBD_StatusTypeDef ret;
+
+ /* Run USB HS test mode */
+ ret = USBD_LL_SetTestMode(pdev, pdev->dev_test_mode);
+
+ return ret;
+#else
+ /* Prevent unused argument compilation warning */
+ UNUSED(pdev);
+
+ return USBD_OK;
+#endif /* USBD_HS_TESTMODE_ENABLE */
+}
+
+/**
+ * @brief USBD_SetClassConfig
+ * Configure device and start the interface
+ * @param pdev: device instance
+ * @param cfgidx: configuration index
+ * @retval status
+ */
+
+USBD_StatusTypeDef USBD_SetClassConfig(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
+{
+ USBD_StatusTypeDef ret = USBD_OK;
+
+#ifdef USE_USBD_COMPOSITE
+ /* Parse the table of classes in use */
+ for (uint32_t i = 0U; i < USBD_MAX_SUPPORTED_CLASS; i++)
+ {
+ /* Check if current class is in use */
+ if ((pdev->tclasslist[i].Active) == 1U)
+ {
+ if (pdev->pClass[i] != NULL)
+ {
+ pdev->classId = i;
+ /* Set configuration and Start the Class*/
+ if (pdev->pClass[i]->Init(pdev, cfgidx) != 0U)
+ {
+ ret = USBD_FAIL;
+ }
+ }
+ }
+ }
+#else
+ if (pdev->pClass[0] != NULL)
+ {
+ /* Set configuration and Start the Class */
+ ret = (USBD_StatusTypeDef)pdev->pClass[0]->Init(pdev, cfgidx);
+ }
+#endif /* USE_USBD_COMPOSITE */
+
+ return ret;
+}
+
+/**
+ * @brief USBD_ClrClassConfig
+ * Clear current configuration
+ * @param pdev: device instance
+ * @param cfgidx: configuration index
+ * @retval status
+ */
+USBD_StatusTypeDef USBD_ClrClassConfig(USBD_HandleTypeDef *pdev, uint8_t cfgidx)
+{
+ USBD_StatusTypeDef ret = USBD_OK;
+
+#ifdef USE_USBD_COMPOSITE
+ /* Parse the table of classes in use */
+ for (uint32_t i = 0U; i < USBD_MAX_SUPPORTED_CLASS; i++)
+ {
+ /* Check if current class is in use */
+ if ((pdev->tclasslist[i].Active) == 1U)
+ {
+ if (pdev->pClass[i] != NULL)
+ {
+ pdev->classId = i;
+ /* Clear configuration and De-initialize the Class process */
+ if (pdev->pClass[i]->DeInit(pdev, cfgidx) != 0U)
+ {
+ ret = USBD_FAIL;
+ }
+ }
+ }
+ }
+#else
+ /* Clear configuration and De-initialize the Class process */
+ if (pdev->pClass[0]->DeInit(pdev, cfgidx) != 0U)
+ {
+ ret = USBD_FAIL;
+ }
+#endif /* USE_USBD_COMPOSITE */
+
+ return ret;
+}
+
+
+/**
+ * @brief USBD_LL_SetupStage
+ * Handle the setup stage
+ * @param pdev: device instance
+ * @param psetup: setup packet buffer pointer
+ * @retval status
+ */
+USBD_StatusTypeDef USBD_LL_SetupStage(USBD_HandleTypeDef *pdev, uint8_t *psetup)
+{
+ USBD_StatusTypeDef ret;
+
+ USBD_ParseSetupRequest(&pdev->request, psetup);
+
+ pdev->ep0_state = USBD_EP0_SETUP;
+
+ pdev->ep0_data_len = pdev->request.wLength;
+
+ switch (pdev->request.bmRequest & 0x1FU)
+ {
+ case USB_REQ_RECIPIENT_DEVICE:
+ ret = USBD_StdDevReq(pdev, &pdev->request);
+ break;
+
+ case USB_REQ_RECIPIENT_INTERFACE:
+ ret = USBD_StdItfReq(pdev, &pdev->request);
+ break;
+
+ case USB_REQ_RECIPIENT_ENDPOINT:
+ ret = USBD_StdEPReq(pdev, &pdev->request);
+ break;
+
+ default:
+ ret = USBD_LL_StallEP(pdev, (pdev->request.bmRequest & 0x80U));
+ break;
+ }
+
+ return ret;
+}
+
+/**
+ * @brief USBD_LL_DataOutStage
+ * Handle data OUT stage
+ * @param pdev: device instance
+ * @param epnum: endpoint index
+ * @param pdata: data pointer
+ * @retval status
+ */
+USBD_StatusTypeDef USBD_LL_DataOutStage(USBD_HandleTypeDef *pdev,
+ uint8_t epnum, uint8_t *pdata)
+{
+ USBD_EndpointTypeDef *pep;
+ USBD_StatusTypeDef ret = USBD_OK;
+ uint8_t idx;
+
+ UNUSED(pdata);
+
+ if (epnum == 0U)
+ {
+ pep = &pdev->ep_out[0];
+
+ if (pdev->ep0_state == USBD_EP0_DATA_OUT)
+ {
+ if (pep->rem_length > pep->maxpacket)
+ {
+ pep->rem_length -= pep->maxpacket;
+ pep->pbuffer += pep->maxpacket;
+
+ (void)USBD_CtlContinueRx(pdev, pep->pbuffer, MAX(pep->rem_length, pep->maxpacket));
+ }
+ else
+ {
+ /* Find the class ID relative to the current request */
+ switch (pdev->request.bmRequest & 0x1FU)
+ {
+ case USB_REQ_RECIPIENT_DEVICE:
+ /* Device requests must be managed by the first instantiated class
+ (or duplicated by all classes for simplicity) */
+ idx = 0U;
+ break;
+
+ case USB_REQ_RECIPIENT_INTERFACE:
+ idx = USBD_CoreFindIF(pdev, LOBYTE(pdev->request.wIndex));
+ break;
+
+ case USB_REQ_RECIPIENT_ENDPOINT:
+ idx = USBD_CoreFindEP(pdev, LOBYTE(pdev->request.wIndex));
+ break;
+
+ default:
+ /* Back to the first class in case of doubt */
+ idx = 0U;
+ break;
+ }
+
+ if (idx < USBD_MAX_SUPPORTED_CLASS)
+ {
+ /* Setup the class ID and route the request to the relative class function */
+ if (pdev->dev_state == USBD_STATE_CONFIGURED)
+ {
+ if (pdev->pClass[idx]->EP0_RxReady != NULL)
+ {
+ pdev->classId = idx;
+ pdev->pClass[idx]->EP0_RxReady(pdev);
+ }
+ }
+ }
+
+ (void)USBD_CtlSendStatus(pdev);
+ }
+ }
+ }
+ else
+ {
+ /* Get the class index relative to this interface */
+ idx = USBD_CoreFindEP(pdev, (epnum & 0x7FU));
+
+ if (((uint16_t)idx != 0xFFU) && (idx < USBD_MAX_SUPPORTED_CLASS))
+ {
+ /* Call the class data out function to manage the request */
+ if (pdev->dev_state == USBD_STATE_CONFIGURED)
+ {
+ if (pdev->pClass[idx]->DataOut != NULL)
+ {
+ pdev->classId = idx;
+ ret = (USBD_StatusTypeDef)pdev->pClass[idx]->DataOut(pdev, epnum);
+ }
+ }
+ if (ret != USBD_OK)
+ {
+ return ret;
+ }
+ }
+ }
+
+ return USBD_OK;
+}
+
+/**
+ * @brief USBD_LL_DataInStage
+ * Handle data in stage
+ * @param pdev: device instance
+ * @param epnum: endpoint index
+ * @param pdata: data pointer
+ * @retval status
+ */
+USBD_StatusTypeDef USBD_LL_DataInStage(USBD_HandleTypeDef *pdev,
+ uint8_t epnum, uint8_t *pdata)
+{
+ USBD_EndpointTypeDef *pep;
+ USBD_StatusTypeDef ret;
+ uint8_t idx;
+
+ UNUSED(pdata);
+
+ if (epnum == 0U)
+ {
+ pep = &pdev->ep_in[0];
+
+ if (pdev->ep0_state == USBD_EP0_DATA_IN)
+ {
+ if (pep->rem_length > pep->maxpacket)
+ {
+ pep->rem_length -= pep->maxpacket;
+ pep->pbuffer += pep->maxpacket;
+
+ (void)USBD_CtlContinueSendData(pdev, pep->pbuffer, pep->rem_length);
+
+ /* Prepare endpoint for premature end of transfer */
+ (void)USBD_LL_PrepareReceive(pdev, 0U, NULL, 0U);
+ }
+ else
+ {
+ /* last packet is MPS multiple, so send ZLP packet */
+ if ((pep->maxpacket == pep->rem_length) &&
+ (pep->total_length >= pep->maxpacket) &&
+ (pep->total_length < pdev->ep0_data_len))
+ {
+ (void)USBD_CtlContinueSendData(pdev, NULL, 0U);
+ pdev->ep0_data_len = 0U;
+
+ /* Prepare endpoint for premature end of transfer */
+ (void)USBD_LL_PrepareReceive(pdev, 0U, NULL, 0U);
+ }
+ else
+ {
+ if (pdev->dev_state == USBD_STATE_CONFIGURED)
+ {
+ if (pdev->pClass[0]->EP0_TxSent != NULL)
+ {
+ pdev->classId = 0U;
+ pdev->pClass[0]->EP0_TxSent(pdev);
+ }
+ }
+ (void)USBD_LL_StallEP(pdev, 0x80U);
+ (void)USBD_CtlReceiveStatus(pdev);
+ }
+ }
+ }
+
+ if (pdev->dev_test_mode != 0U)
+ {
+ (void)USBD_RunTestMode(pdev);
+ pdev->dev_test_mode = 0U;
+ }
+ }
+ else
+ {
+ /* Get the class index relative to this interface */
+ idx = USBD_CoreFindEP(pdev, ((uint8_t)epnum | 0x80U));
+
+ if (((uint16_t)idx != 0xFFU) && (idx < USBD_MAX_SUPPORTED_CLASS))
+ {
+ /* Call the class data out function to manage the request */
+ if (pdev->dev_state == USBD_STATE_CONFIGURED)
+ {
+ if (pdev->pClass[idx]->DataIn != NULL)
+ {
+ pdev->classId = idx;
+ ret = (USBD_StatusTypeDef)pdev->pClass[idx]->DataIn(pdev, epnum);
+
+ if (ret != USBD_OK)
+ {
+ return ret;
+ }
+ }
+ }
+ }
+ }
+
+ return USBD_OK;
+}
+
+/**
+ * @brief USBD_LL_Reset
+ * Handle Reset event
+ * @param pdev: device instance
+ * @retval status
+ */
+USBD_StatusTypeDef USBD_LL_Reset(USBD_HandleTypeDef *pdev)
+{
+ USBD_StatusTypeDef ret = USBD_OK;
+
+ /* Upon Reset call user call back */
+ pdev->dev_state = USBD_STATE_DEFAULT;
+ pdev->ep0_state = USBD_EP0_IDLE;
+ pdev->dev_config = 0U;
+ pdev->dev_remote_wakeup = 0U;
+ pdev->dev_test_mode = 0U;
+
+#ifdef USE_USBD_COMPOSITE
+ /* Parse the table of classes in use */
+ for (uint32_t i = 0U; i < USBD_MAX_SUPPORTED_CLASS; i++)
+ {
+ /* Check if current class is in use */
+ if ((pdev->tclasslist[i].Active) == 1U)
+ {
+ if (pdev->pClass[i] != NULL)
+ {
+ pdev->classId = i;
+ /* Clear configuration and De-initialize the Class process*/
+
+ if (pdev->pClass[i]->DeInit != NULL)
+ {
+ if (pdev->pClass[i]->DeInit(pdev, (uint8_t)pdev->dev_config) != USBD_OK)
+ {
+ ret = USBD_FAIL;
+ }
+ }
+ }
+ }
+ }
+#else
+
+ if (pdev->pClass[0] != NULL)
+ {
+ if (pdev->pClass[0]->DeInit != NULL)
+ {
+ if (pdev->pClass[0]->DeInit(pdev, (uint8_t)pdev->dev_config) != USBD_OK)
+ {
+ ret = USBD_FAIL;
+ }
+ }
+ }
+#endif /* USE_USBD_COMPOSITE */
+
+ /* Open EP0 OUT */
+ (void)USBD_LL_OpenEP(pdev, 0x00U, USBD_EP_TYPE_CTRL, USB_MAX_EP0_SIZE);
+ pdev->ep_out[0x00U & 0xFU].is_used = 1U;
+
+ pdev->ep_out[0].maxpacket = USB_MAX_EP0_SIZE;
+
+ /* Open EP0 IN */
+ (void)USBD_LL_OpenEP(pdev, 0x80U, USBD_EP_TYPE_CTRL, USB_MAX_EP0_SIZE);
+ pdev->ep_in[0x80U & 0xFU].is_used = 1U;
+
+ pdev->ep_in[0].maxpacket = USB_MAX_EP0_SIZE;
+
+ return ret;
+}
+
+/**
+ * @brief USBD_LL_SetSpeed
+ * Handle Reset event
+ * @param pdev: device instance
+ * @retval status
+ */
+USBD_StatusTypeDef USBD_LL_SetSpeed(USBD_HandleTypeDef *pdev,
+ USBD_SpeedTypeDef speed)
+{
+ pdev->dev_speed = speed;
+
+ return USBD_OK;
+}
+
+/**
+ * @brief USBD_LL_Suspend
+ * Handle Suspend event
+ * @param pdev: device instance
+ * @retval status
+ */
+USBD_StatusTypeDef USBD_LL_Suspend(USBD_HandleTypeDef *pdev)
+{
+ if (pdev->dev_state != USBD_STATE_SUSPENDED)
+ {
+ pdev->dev_old_state = pdev->dev_state;
+ }
+
+ pdev->dev_state = USBD_STATE_SUSPENDED;
+
+ return USBD_OK;
+}
+
+/**
+ * @brief USBD_LL_Resume
+ * Handle Resume event
+ * @param pdev: device instance
+ * @retval status
+ */
+USBD_StatusTypeDef USBD_LL_Resume(USBD_HandleTypeDef *pdev)
+{
+ if (pdev->dev_state == USBD_STATE_SUSPENDED)
+ {
+ pdev->dev_state = pdev->dev_old_state;
+ }
+
+ return USBD_OK;
+}
+
+/**
+ * @brief USBD_LL_SOF
+ * Handle SOF event
+ * @param pdev: device instance
+ * @retval status
+ */
+USBD_StatusTypeDef USBD_LL_SOF(USBD_HandleTypeDef *pdev)
+{
+ /* The SOF event can be distributed for all classes that support it */
+ if (pdev->dev_state == USBD_STATE_CONFIGURED)
+ {
+#ifdef USE_USBD_COMPOSITE
+ /* Parse the table of classes in use */
+ for (uint32_t i = 0; i < USBD_MAX_SUPPORTED_CLASS; i++)
+ {
+ /* Check if current class is in use */
+ if ((pdev->tclasslist[i].Active) == 1U)
+ {
+ if (pdev->pClass[i] != NULL)
+ {
+ if (pdev->pClass[i]->SOF != NULL)
+ {
+ pdev->classId = i;
+ (void)pdev->pClass[i]->SOF(pdev);
+ }
+ }
+ }
+ }
+#else
+ if (pdev->pClass[0] != NULL)
+ {
+ if (pdev->pClass[0]->SOF != NULL)
+ {
+ (void)pdev->pClass[0]->SOF(pdev);
+ }
+ }
+#endif /* USE_USBD_COMPOSITE */
+ }
+
+ return USBD_OK;
+}
+
+/**
+ * @brief USBD_LL_IsoINIncomplete
+ * Handle iso in incomplete event
+ * @param pdev: device instance
+ * @param epnum: Endpoint number
+ * @retval status
+ */
+USBD_StatusTypeDef USBD_LL_IsoINIncomplete(USBD_HandleTypeDef *pdev,
+ uint8_t epnum)
+{
+ if (pdev->pClass[pdev->classId] == NULL)
+ {
+ return USBD_FAIL;
+ }
+
+ if (pdev->dev_state == USBD_STATE_CONFIGURED)
+ {
+ if (pdev->pClass[pdev->classId]->IsoINIncomplete != NULL)
+ {
+ (void)pdev->pClass[pdev->classId]->IsoINIncomplete(pdev, epnum);
+ }
+ }
+
+ return USBD_OK;
+}
+
+/**
+ * @brief USBD_LL_IsoOUTIncomplete
+ * Handle iso out incomplete event
+ * @param pdev: device instance
+ * @param epnum: Endpoint number
+ * @retval status
+ */
+USBD_StatusTypeDef USBD_LL_IsoOUTIncomplete(USBD_HandleTypeDef *pdev,
+ uint8_t epnum)
+{
+ if (pdev->pClass[pdev->classId] == NULL)
+ {
+ return USBD_FAIL;
+ }
+
+ if (pdev->dev_state == USBD_STATE_CONFIGURED)
+ {
+ if (pdev->pClass[pdev->classId]->IsoOUTIncomplete != NULL)
+ {
+ (void)pdev->pClass[pdev->classId]->IsoOUTIncomplete(pdev, epnum);
+ }
+ }
+
+ return USBD_OK;
+}
+
+/**
+ * @brief USBD_LL_DevConnected
+ * Handle device connection event
+ * @param pdev: device instance
+ * @retval status
+ */
+USBD_StatusTypeDef USBD_LL_DevConnected(USBD_HandleTypeDef *pdev)
+{
+ /* Prevent unused argument compilation warning */
+ UNUSED(pdev);
+
+ return USBD_OK;
+}
+
+/**
+ * @brief USBD_LL_DevDisconnected
+ * Handle device disconnection event
+ * @param pdev: device instance
+ * @retval status
+ */
+USBD_StatusTypeDef USBD_LL_DevDisconnected(USBD_HandleTypeDef *pdev)
+{
+ USBD_StatusTypeDef ret = USBD_OK;
+
+ /* Free Class Resources */
+ pdev->dev_state = USBD_STATE_DEFAULT;
+
+#ifdef USE_USBD_COMPOSITE
+ /* Parse the table of classes in use */
+ for (uint32_t i = 0; i < USBD_MAX_SUPPORTED_CLASS; i++)
+ {
+ /* Check if current class is in use */
+ if ((pdev->tclasslist[i].Active) == 1U)
+ {
+ if (pdev->pClass[i] != NULL)
+ {
+ pdev->classId = i;
+ /* Clear configuration and De-initialize the Class process*/
+ if (pdev->pClass[i]->DeInit(pdev, (uint8_t)pdev->dev_config) != 0U)
+ {
+ ret = USBD_FAIL;
+ }
+ }
+ }
+ }
+#else
+ if (pdev->pClass[0] != NULL)
+ {
+ if (pdev->pClass[0]->DeInit(pdev, (uint8_t)pdev->dev_config) != 0U)
+ {
+ ret = USBD_FAIL;
+ }
+ }
+#endif /* USE_USBD_COMPOSITE */
+
+ return ret;
+}
+
+/**
+ * @brief USBD_CoreFindIF
+ * return the class index relative to the selected interface
+ * @param pdev: device instance
+ * @param index : selected interface number
+ * @retval index of the class using the selected interface number. OxFF if no class found.
+ */
+uint8_t USBD_CoreFindIF(USBD_HandleTypeDef *pdev, uint8_t index)
+{
+#ifdef USE_USBD_COMPOSITE
+ /* Parse the table of classes in use */
+ for (uint32_t i = 0U; i < USBD_MAX_SUPPORTED_CLASS; i++)
+ {
+ /* Check if current class is in use */
+ if ((pdev->tclasslist[i].Active) == 1U)
+ {
+ /* Parse all interfaces listed in the current class */
+ for (uint32_t j = 0U; j < pdev->tclasslist[i].NumIf; j++)
+ {
+ /* Check if requested Interface matches the current class interface */
+ if (pdev->tclasslist[i].Ifs[j] == index)
+ {
+ if (pdev->pClass[i]->Setup != NULL)
+ {
+ return (uint8_t)i;
+ }
+ }
+ }
+ }
+ }
+
+ return 0xFFU;
+#else
+ UNUSED(pdev);
+ UNUSED(index);
+
+ return 0x00U;
+#endif /* USE_USBD_COMPOSITE */
+}
+
+/**
+ * @brief USBD_CoreFindEP
+ * return the class index relative to the selected endpoint
+ * @param pdev: device instance
+ * @param index : selected endpoint number
+ * @retval index of the class using the selected endpoint number. 0xFF if no class found.
+ */
+uint8_t USBD_CoreFindEP(USBD_HandleTypeDef *pdev, uint8_t index)
+{
+#ifdef USE_USBD_COMPOSITE
+ /* Parse the table of classes in use */
+ for (uint32_t i = 0U; i < USBD_MAX_SUPPORTED_CLASS; i++)
+ {
+ /* Check if current class is in use */
+ if ((pdev->tclasslist[i].Active) == 1U)
+ {
+ /* Parse all endpoints listed in the current class */
+ for (uint32_t j = 0U; j < pdev->tclasslist[i].NumEps; j++)
+ {
+ /* Check if requested endpoint matches the current class endpoint */
+ if (pdev->tclasslist[i].Eps[j].add == index)
+ {
+ if (pdev->pClass[i]->Setup != NULL)
+ {
+ return (uint8_t)i;
+ }
+ }
+ }
+ }
+ }
+
+ return 0xFFU;
+#else
+ UNUSED(pdev);
+ UNUSED(index);
+
+ return 0x00U;
+#endif /* USE_USBD_COMPOSITE */
+}
+
+#ifdef USE_USBD_COMPOSITE
+/**
+ * @brief USBD_CoreGetEPAdd
+ * Get the endpoint address relative to a selected class
+ * @param pdev: device instance
+ * @param ep_dir: USBD_EP_IN or USBD_EP_OUT
+ * @param ep_type: USBD_EP_TYPE_CTRL, USBD_EP_TYPE_ISOC, USBD_EP_TYPE_BULK or USBD_EP_TYPE_INTR
+ * @param ClassId: The Class ID
+ * @retval Address of the selected endpoint or 0xFFU if no endpoint found.
+ */
+uint8_t USBD_CoreGetEPAdd(USBD_HandleTypeDef *pdev, uint8_t ep_dir, uint8_t ep_type, uint8_t ClassId)
+{
+ uint8_t idx;
+
+ /* Find the EP address in the selected class table */
+ for (idx = 0; idx < pdev->tclasslist[ClassId].NumEps; idx++)
+ {
+ if (((pdev->tclasslist[ClassId].Eps[idx].add & USBD_EP_IN) == ep_dir) && \
+ (pdev->tclasslist[ClassId].Eps[idx].type == ep_type) && \
+ (pdev->tclasslist[ClassId].Eps[idx].is_used != 0U))
+ {
+ return (pdev->tclasslist[ClassId].Eps[idx].add);
+ }
+ }
+
+ /* If reaching this point, then no endpoint was found */
+ return 0xFFU;
+}
+#endif /* USE_USBD_COMPOSITE */
+
+/**
+ * @brief USBD_GetEpDesc
+ * This function return the Endpoint descriptor
+ * @param pdev: device instance
+ * @param pConfDesc: pointer to Bos descriptor
+ * @param EpAddr: endpoint address
+ * @retval pointer to video endpoint descriptor
+ */
+void *USBD_GetEpDesc(uint8_t *pConfDesc, uint8_t EpAddr)
+{
+ USBD_DescHeaderTypeDef *pdesc = (USBD_DescHeaderTypeDef *)(void *)pConfDesc;
+ USBD_ConfigDescTypeDef *desc = (USBD_ConfigDescTypeDef *)(void *)pConfDesc;
+ USBD_EpDescTypeDef *pEpDesc = NULL;
+ uint16_t ptr;
+
+ if (desc->wTotalLength > desc->bLength)
+ {
+ ptr = desc->bLength;
+
+ while (ptr < desc->wTotalLength)
+ {
+ pdesc = USBD_GetNextDesc((uint8_t *)pdesc, &ptr);
+
+ if (pdesc->bDescriptorType == USB_DESC_TYPE_ENDPOINT)
+ {
+ pEpDesc = (USBD_EpDescTypeDef *)(void *)pdesc;
+
+ if (pEpDesc->bEndpointAddress == EpAddr)
+ {
+ break;
+ }
+ else
+ {
+ pEpDesc = NULL;
+ }
+ }
+ }
+ }
+
+ return (void *)pEpDesc;
+}
+
+/**
+ * @brief USBD_GetNextDesc
+ * This function return the next descriptor header
+ * @param buf: Buffer where the descriptor is available
+ * @param ptr: data pointer inside the descriptor
+ * @retval next header
+ */
+USBD_DescHeaderTypeDef *USBD_GetNextDesc(uint8_t *pbuf, uint16_t *ptr)
+{
+ USBD_DescHeaderTypeDef *pnext = (USBD_DescHeaderTypeDef *)(void *)pbuf;
+
+ *ptr += pnext->bLength;
+ pnext = (USBD_DescHeaderTypeDef *)(void *)(pbuf + pnext->bLength);
+
+ return (pnext);
+}
+
+/**
+ * @}
+ */
+
+
+/**
+ * @}
+ */
+
+
+/**
+ * @}
+ */
+
diff --git a/firmware/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ctlreq.c b/firmware/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ctlreq.c
new file mode 100644
index 0000000..814b810
--- /dev/null
+++ b/firmware/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ctlreq.c
@@ -0,0 +1,1058 @@
+/**
+ ******************************************************************************
+ * @file usbd_req.c
+ * @author MCD Application Team
+ * @brief This file provides the standard USB requests following chapter 9.
+ ******************************************************************************
+ * @attention
+ *
+ * Copyright (c) 2015 STMicroelectronics.
+ * All rights reserved.
+ *
+ * This software is licensed under terms that can be found in the LICENSE file
+ * in the root directory of this software component.
+ * If no LICENSE file comes with this software, it is provided AS-IS.
+ *
+ ******************************************************************************
+ */
+
+/* Includes ------------------------------------------------------------------*/
+#include "usbd_ctlreq.h"
+#include "usbd_ioreq.h"
+
+#ifdef USE_USBD_COMPOSITE
+#include "usbd_composite_builder.h"
+#endif /* USE_USBD_COMPOSITE */
+
+/** @addtogroup STM32_USBD_STATE_DEVICE_LIBRARY
+ * @{
+ */
+
+
+/** @defgroup USBD_REQ
+ * @brief USB standard requests module
+ * @{
+ */
+
+/** @defgroup USBD_REQ_Private_TypesDefinitions
+ * @{
+ */
+
+/**
+ * @}
+ */
+
+
+/** @defgroup USBD_REQ_Private_Defines
+ * @{
+ */
+#ifndef USBD_MAX_STR_DESC_SIZ
+#define USBD_MAX_STR_DESC_SIZ 64U
+#endif /* USBD_MAX_STR_DESC_SIZ */
+/**
+ * @}
+ */
+
+
+/** @defgroup USBD_REQ_Private_Macros
+ * @{
+ */
+
+/**
+ * @}
+ */
+
+
+/** @defgroup USBD_REQ_Private_Variables
+ * @{
+ */
+
+/**
+ * @}
+ */
+
+
+/** @defgroup USBD_REQ_Private_FunctionPrototypes
+ * @{
+ */
+static void USBD_GetDescriptor(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
+static void USBD_SetAddress(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
+static USBD_StatusTypeDef USBD_SetConfig(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
+static void USBD_GetConfig(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
+static void USBD_GetStatus(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
+static void USBD_SetFeature(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
+static void USBD_ClrFeature(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req);
+static uint8_t USBD_GetLen(uint8_t *buf);
+
+/**
+ * @}
+ */
+
+
+/** @defgroup USBD_REQ_Private_Functions
+ * @{
+ */
+
+
+/**
+ * @brief USBD_StdDevReq
+ * Handle standard usb device requests
+ * @param pdev: device instance
+ * @param req: usb request
+ * @retval status
+ */
+USBD_StatusTypeDef USBD_StdDevReq(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
+{
+ USBD_StatusTypeDef ret = USBD_OK;
+
+ switch (req->bmRequest & USB_REQ_TYPE_MASK)
+ {
+ case USB_REQ_TYPE_CLASS:
+ case USB_REQ_TYPE_VENDOR:
+ ret = (USBD_StatusTypeDef)pdev->pClass[pdev->classId]->Setup(pdev, req);
+ break;
+
+ case USB_REQ_TYPE_STANDARD:
+ switch (req->bRequest)
+ {
+ case USB_REQ_GET_DESCRIPTOR:
+ USBD_GetDescriptor(pdev, req);
+ break;
+
+ case USB_REQ_SET_ADDRESS:
+ USBD_SetAddress(pdev, req);
+ break;
+
+ case USB_REQ_SET_CONFIGURATION:
+ ret = USBD_SetConfig(pdev, req);
+ break;
+
+ case USB_REQ_GET_CONFIGURATION:
+ USBD_GetConfig(pdev, req);
+ break;
+
+ case USB_REQ_GET_STATUS:
+ USBD_GetStatus(pdev, req);
+ break;
+
+ case USB_REQ_SET_FEATURE:
+ USBD_SetFeature(pdev, req);
+ break;
+
+ case USB_REQ_CLEAR_FEATURE:
+ USBD_ClrFeature(pdev, req);
+ break;
+
+ default:
+ USBD_CtlError(pdev, req);
+ break;
+ }
+ break;
+
+ default:
+ USBD_CtlError(pdev, req);
+ break;
+ }
+
+ return ret;
+}
+
+/**
+ * @brief USBD_StdItfReq
+ * Handle standard usb interface requests
+ * @param pdev: device instance
+ * @param req: usb request
+ * @retval status
+ */
+USBD_StatusTypeDef USBD_StdItfReq(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
+{
+ USBD_StatusTypeDef ret = USBD_OK;
+ uint8_t idx;
+
+ switch (req->bmRequest & USB_REQ_TYPE_MASK)
+ {
+ case USB_REQ_TYPE_CLASS:
+ case USB_REQ_TYPE_VENDOR:
+ case USB_REQ_TYPE_STANDARD:
+ switch (pdev->dev_state)
+ {
+ case USBD_STATE_DEFAULT:
+ case USBD_STATE_ADDRESSED:
+ case USBD_STATE_CONFIGURED:
+
+ if (LOBYTE(req->wIndex) <= USBD_MAX_NUM_INTERFACES)
+ {
+ /* Get the class index relative to this interface */
+ idx = USBD_CoreFindIF(pdev, LOBYTE(req->wIndex));
+ if (((uint8_t)idx != 0xFFU) && (idx < USBD_MAX_SUPPORTED_CLASS))
+ {
+ /* Call the class data out function to manage the request */
+ if (pdev->pClass[idx]->Setup != NULL)
+ {
+ pdev->classId = idx;
+ ret = (USBD_StatusTypeDef)(pdev->pClass[idx]->Setup(pdev, req));
+ }
+ else
+ {
+ /* should never reach this condition */
+ ret = USBD_FAIL;
+ }
+ }
+ else
+ {
+ /* No relative interface found */
+ ret = USBD_FAIL;
+ }
+
+ if ((req->wLength == 0U) && (ret == USBD_OK))
+ {
+ (void)USBD_CtlSendStatus(pdev);
+ }
+ }
+ else
+ {
+ USBD_CtlError(pdev, req);
+ }
+ break;
+
+ default:
+ USBD_CtlError(pdev, req);
+ break;
+ }
+ break;
+
+ default:
+ USBD_CtlError(pdev, req);
+ break;
+ }
+
+ return ret;
+}
+
+/**
+ * @brief USBD_StdEPReq
+ * Handle standard usb endpoint requests
+ * @param pdev: device instance
+ * @param req: usb request
+ * @retval status
+ */
+USBD_StatusTypeDef USBD_StdEPReq(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
+{
+ USBD_EndpointTypeDef *pep;
+ uint8_t ep_addr;
+ uint8_t idx;
+ USBD_StatusTypeDef ret = USBD_OK;
+
+ ep_addr = LOBYTE(req->wIndex);
+
+ switch (req->bmRequest & USB_REQ_TYPE_MASK)
+ {
+ case USB_REQ_TYPE_CLASS:
+ case USB_REQ_TYPE_VENDOR:
+ /* Get the class index relative to this endpoint */
+ idx = USBD_CoreFindEP(pdev, ep_addr);
+ if (((uint8_t)idx != 0xFFU) && (idx < USBD_MAX_SUPPORTED_CLASS))
+ {
+ pdev->classId = idx;
+ /* Call the class data out function to manage the request */
+ if (pdev->pClass[idx]->Setup != NULL)
+ {
+ ret = (USBD_StatusTypeDef)pdev->pClass[idx]->Setup(pdev, req);
+ }
+ }
+ break;
+
+ case USB_REQ_TYPE_STANDARD:
+ switch (req->bRequest)
+ {
+ case USB_REQ_SET_FEATURE:
+ switch (pdev->dev_state)
+ {
+ case USBD_STATE_ADDRESSED:
+ if ((ep_addr != 0x00U) && (ep_addr != 0x80U))
+ {
+ (void)USBD_LL_StallEP(pdev, ep_addr);
+ (void)USBD_LL_StallEP(pdev, 0x80U);
+ }
+ else
+ {
+ USBD_CtlError(pdev, req);
+ }
+ break;
+
+ case USBD_STATE_CONFIGURED:
+ if (req->wValue == USB_FEATURE_EP_HALT)
+ {
+ if ((ep_addr != 0x00U) && (ep_addr != 0x80U) && (req->wLength == 0x00U))
+ {
+ (void)USBD_LL_StallEP(pdev, ep_addr);
+ }
+ }
+ (void)USBD_CtlSendStatus(pdev);
+
+ break;
+
+ default:
+ USBD_CtlError(pdev, req);
+ break;
+ }
+ break;
+
+ case USB_REQ_CLEAR_FEATURE:
+
+ switch (pdev->dev_state)
+ {
+ case USBD_STATE_ADDRESSED:
+ if ((ep_addr != 0x00U) && (ep_addr != 0x80U))
+ {
+ (void)USBD_LL_StallEP(pdev, ep_addr);
+ (void)USBD_LL_StallEP(pdev, 0x80U);
+ }
+ else
+ {
+ USBD_CtlError(pdev, req);
+ }
+ break;
+
+ case USBD_STATE_CONFIGURED:
+ if (req->wValue == USB_FEATURE_EP_HALT)
+ {
+ if ((ep_addr & 0x7FU) != 0x00U)
+ {
+ (void)USBD_LL_ClearStallEP(pdev, ep_addr);
+ }
+ (void)USBD_CtlSendStatus(pdev);
+
+ /* Get the class index relative to this interface */
+ idx = USBD_CoreFindEP(pdev, ep_addr);
+ if (((uint8_t)idx != 0xFFU) && (idx < USBD_MAX_SUPPORTED_CLASS))
+ {
+ pdev->classId = idx;
+ /* Call the class data out function to manage the request */
+ if (pdev->pClass[idx]->Setup != NULL)
+ {
+ ret = (USBD_StatusTypeDef)(pdev->pClass[idx]->Setup(pdev, req));
+ }
+ }
+ }
+ break;
+
+ default:
+ USBD_CtlError(pdev, req);
+ break;
+ }
+ break;
+
+ case USB_REQ_GET_STATUS:
+ switch (pdev->dev_state)
+ {
+ case USBD_STATE_ADDRESSED:
+ if ((ep_addr != 0x00U) && (ep_addr != 0x80U))
+ {
+ USBD_CtlError(pdev, req);
+ break;
+ }
+ pep = ((ep_addr & 0x80U) == 0x80U) ? &pdev->ep_in[ep_addr & 0x7FU] : \
+ &pdev->ep_out[ep_addr & 0x7FU];
+
+ pep->status = 0x0000U;
+
+ (void)USBD_CtlSendData(pdev, (uint8_t *)&pep->status, 2U);
+ break;
+
+ case USBD_STATE_CONFIGURED:
+ if ((ep_addr & 0x80U) == 0x80U)
+ {
+ if (pdev->ep_in[ep_addr & 0xFU].is_used == 0U)
+ {
+ USBD_CtlError(pdev, req);
+ break;
+ }
+ }
+ else
+ {
+ if (pdev->ep_out[ep_addr & 0xFU].is_used == 0U)
+ {
+ USBD_CtlError(pdev, req);
+ break;
+ }
+ }
+
+ pep = ((ep_addr & 0x80U) == 0x80U) ? &pdev->ep_in[ep_addr & 0x7FU] : \
+ &pdev->ep_out[ep_addr & 0x7FU];
+
+ if ((ep_addr == 0x00U) || (ep_addr == 0x80U))
+ {
+ pep->status = 0x0000U;
+ }
+ else if (USBD_LL_IsStallEP(pdev, ep_addr) != 0U)
+ {
+ pep->status = 0x0001U;
+ }
+ else
+ {
+ pep->status = 0x0000U;
+ }
+
+ (void)USBD_CtlSendData(pdev, (uint8_t *)&pep->status, 2U);
+ break;
+
+ default:
+ USBD_CtlError(pdev, req);
+ break;
+ }
+ break;
+
+ default:
+ USBD_CtlError(pdev, req);
+ break;
+ }
+ break;
+
+ default:
+ USBD_CtlError(pdev, req);
+ break;
+ }
+
+ return ret;
+}
+
+
+/**
+ * @brief USBD_GetDescriptor
+ * Handle Get Descriptor requests
+ * @param pdev: device instance
+ * @param req: usb request
+ * @retval None
+ */
+static void USBD_GetDescriptor(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
+{
+ uint16_t len = 0U;
+ uint8_t *pbuf = NULL;
+ uint8_t err = 0U;
+
+ switch (req->wValue >> 8)
+ {
+#if ((USBD_LPM_ENABLED == 1U) || (USBD_CLASS_BOS_ENABLED == 1U))
+ case USB_DESC_TYPE_BOS:
+ if (pdev->pDesc->GetBOSDescriptor != NULL)
+ {
+ pbuf = pdev->pDesc->GetBOSDescriptor(pdev->dev_speed, &len);
+ }
+ else
+ {
+ USBD_CtlError(pdev, req);
+ err++;
+ }
+ break;
+#endif /* (USBD_LPM_ENABLED == 1U) || (USBD_CLASS_BOS_ENABLED == 1U) */
+ case USB_DESC_TYPE_DEVICE:
+ pbuf = pdev->pDesc->GetDeviceDescriptor(pdev->dev_speed, &len);
+ break;
+
+ case USB_DESC_TYPE_CONFIGURATION:
+ if (pdev->dev_speed == USBD_SPEED_HIGH)
+ {
+#ifdef USE_USBD_COMPOSITE
+ if ((uint8_t)(pdev->NumClasses) > 0U)
+ {
+ pbuf = (uint8_t *)USBD_CMPSIT.GetHSConfigDescriptor(&len);
+ }
+ else
+#endif /* USE_USBD_COMPOSITE */
+ {
+ pbuf = (uint8_t *)pdev->pClass[0]->GetHSConfigDescriptor(&len);
+ }
+ pbuf[1] = USB_DESC_TYPE_CONFIGURATION;
+ }
+ else
+ {
+#ifdef USE_USBD_COMPOSITE
+ if ((uint8_t)(pdev->NumClasses) > 0U)
+ {
+ pbuf = (uint8_t *)USBD_CMPSIT.GetFSConfigDescriptor(&len);
+ }
+ else
+#endif /* USE_USBD_COMPOSITE */
+ {
+ pbuf = (uint8_t *)pdev->pClass[0]->GetFSConfigDescriptor(&len);
+ }
+ pbuf[1] = USB_DESC_TYPE_CONFIGURATION;
+ }
+ break;
+
+ case USB_DESC_TYPE_STRING:
+ switch ((uint8_t)(req->wValue))
+ {
+ case USBD_IDX_LANGID_STR:
+ if (pdev->pDesc->GetLangIDStrDescriptor != NULL)
+ {
+ pbuf = pdev->pDesc->GetLangIDStrDescriptor(pdev->dev_speed, &len);
+ }
+ else
+ {
+ USBD_CtlError(pdev, req);
+ err++;
+ }
+ break;
+
+ case USBD_IDX_MFC_STR:
+ if (pdev->pDesc->GetManufacturerStrDescriptor != NULL)
+ {
+ pbuf = pdev->pDesc->GetManufacturerStrDescriptor(pdev->dev_speed, &len);
+ }
+ else
+ {
+ USBD_CtlError(pdev, req);
+ err++;
+ }
+ break;
+
+ case USBD_IDX_PRODUCT_STR:
+ if (pdev->pDesc->GetProductStrDescriptor != NULL)
+ {
+ pbuf = pdev->pDesc->GetProductStrDescriptor(pdev->dev_speed, &len);
+ }
+ else
+ {
+ USBD_CtlError(pdev, req);
+ err++;
+ }
+ break;
+
+ case USBD_IDX_SERIAL_STR:
+ if (pdev->pDesc->GetSerialStrDescriptor != NULL)
+ {
+ pbuf = pdev->pDesc->GetSerialStrDescriptor(pdev->dev_speed, &len);
+ }
+ else
+ {
+ USBD_CtlError(pdev, req);
+ err++;
+ }
+ break;
+
+ case USBD_IDX_CONFIG_STR:
+ if (pdev->pDesc->GetConfigurationStrDescriptor != NULL)
+ {
+ pbuf = pdev->pDesc->GetConfigurationStrDescriptor(pdev->dev_speed, &len);
+ }
+ else
+ {
+ USBD_CtlError(pdev, req);
+ err++;
+ }
+ break;
+
+ case USBD_IDX_INTERFACE_STR:
+ if (pdev->pDesc->GetInterfaceStrDescriptor != NULL)
+ {
+ pbuf = pdev->pDesc->GetInterfaceStrDescriptor(pdev->dev_speed, &len);
+ }
+ else
+ {
+ USBD_CtlError(pdev, req);
+ err++;
+ }
+ break;
+
+ default:
+#if (USBD_SUPPORT_USER_STRING_DESC == 1U)
+ pbuf = NULL;
+
+ for (uint32_t idx = 0U; (idx < pdev->NumClasses); idx++)
+ {
+ if (pdev->pClass[idx]->GetUsrStrDescriptor != NULL)
+ {
+ pdev->classId = idx;
+ pbuf = pdev->pClass[idx]->GetUsrStrDescriptor(pdev, LOBYTE(req->wValue), &len);
+
+ if (pbuf == NULL) /* This means that no class recognized the string index */
+ {
+ continue;
+ }
+ else
+ {
+ break;
+ }
+ }
+ }
+#endif /* USBD_SUPPORT_USER_STRING_DESC */
+
+#if (USBD_CLASS_USER_STRING_DESC == 1U)
+ if (pdev->pDesc->GetUserStrDescriptor != NULL)
+ {
+ pbuf = pdev->pDesc->GetUserStrDescriptor(pdev->dev_speed, LOBYTE(req->wValue), &len);
+ }
+ else
+ {
+ USBD_CtlError(pdev, req);
+ err++;
+ }
+#endif /* USBD_SUPPORT_USER_STRING_DESC */
+
+#if ((USBD_CLASS_USER_STRING_DESC == 0U) && (USBD_SUPPORT_USER_STRING_DESC == 0U))
+ USBD_CtlError(pdev, req);
+ err++;
+#endif /* (USBD_CLASS_USER_STRING_DESC == 0U) && (USBD_SUPPORT_USER_STRING_DESC == 0U) */
+ break;
+ }
+ break;
+
+ case USB_DESC_TYPE_DEVICE_QUALIFIER:
+ if (pdev->dev_speed == USBD_SPEED_HIGH)
+ {
+#ifdef USE_USBD_COMPOSITE
+ if ((uint8_t)(pdev->NumClasses) > 0U)
+ {
+ pbuf = (uint8_t *)USBD_CMPSIT.GetDeviceQualifierDescriptor(&len);
+ }
+ else
+#endif /* USE_USBD_COMPOSITE */
+ {
+ pbuf = (uint8_t *)pdev->pClass[0]->GetDeviceQualifierDescriptor(&len);
+ }
+ }
+ else
+ {
+ USBD_CtlError(pdev, req);
+ err++;
+ }
+ break;
+
+ case USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION:
+ if (pdev->dev_speed == USBD_SPEED_HIGH)
+ {
+#ifdef USE_USBD_COMPOSITE
+ if ((uint8_t)(pdev->NumClasses) > 0U)
+ {
+ pbuf = (uint8_t *)USBD_CMPSIT.GetOtherSpeedConfigDescriptor(&len);
+ }
+ else
+#endif /* USE_USBD_COMPOSITE */
+ {
+ pbuf = (uint8_t *)pdev->pClass[0]->GetOtherSpeedConfigDescriptor(&len);
+ }
+ pbuf[1] = USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION;
+ }
+ else
+ {
+ USBD_CtlError(pdev, req);
+ err++;
+ }
+ break;
+
+ default:
+ USBD_CtlError(pdev, req);
+ err++;
+ break;
+ }
+
+ if (err != 0U)
+ {
+ return;
+ }
+
+ if (req->wLength != 0U)
+ {
+ if (len != 0U)
+ {
+ len = MIN(len, req->wLength);
+ (void)USBD_CtlSendData(pdev, pbuf, len);
+ }
+ else
+ {
+ USBD_CtlError(pdev, req);
+ }
+ }
+ else
+ {
+ (void)USBD_CtlSendStatus(pdev);
+ }
+}
+
+
+/**
+ * @brief USBD_SetAddress
+ * Set device address
+ * @param pdev: device instance
+ * @param req: usb request
+ * @retval None
+ */
+static void USBD_SetAddress(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
+{
+ uint8_t dev_addr;
+
+ if ((req->wIndex == 0U) && (req->wLength == 0U) && (req->wValue < 128U))
+ {
+ dev_addr = (uint8_t)(req->wValue) & 0x7FU;
+
+ if (pdev->dev_state == USBD_STATE_CONFIGURED)
+ {
+ USBD_CtlError(pdev, req);
+ }
+ else
+ {
+ pdev->dev_address = dev_addr;
+ (void)USBD_LL_SetUSBAddress(pdev, dev_addr);
+ (void)USBD_CtlSendStatus(pdev);
+
+ if (dev_addr != 0U)
+ {
+ pdev->dev_state = USBD_STATE_ADDRESSED;
+ }
+ else
+ {
+ pdev->dev_state = USBD_STATE_DEFAULT;
+ }
+ }
+ }
+ else
+ {
+ USBD_CtlError(pdev, req);
+ }
+}
+
+/**
+ * @brief USBD_SetConfig
+ * Handle Set device configuration request
+ * @param pdev: device instance
+ * @param req: usb request
+ * @retval status
+ */
+static USBD_StatusTypeDef USBD_SetConfig(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
+{
+ USBD_StatusTypeDef ret = USBD_OK;
+ static uint8_t cfgidx;
+
+ cfgidx = (uint8_t)(req->wValue);
+
+ if (cfgidx > USBD_MAX_NUM_CONFIGURATION)
+ {
+ USBD_CtlError(pdev, req);
+ return USBD_FAIL;
+ }
+
+ switch (pdev->dev_state)
+ {
+ case USBD_STATE_ADDRESSED:
+ if (cfgidx != 0U)
+ {
+ pdev->dev_config = cfgidx;
+
+ ret = USBD_SetClassConfig(pdev, cfgidx);
+
+ if (ret != USBD_OK)
+ {
+ USBD_CtlError(pdev, req);
+ pdev->dev_state = USBD_STATE_ADDRESSED;
+ }
+ else
+ {
+ (void)USBD_CtlSendStatus(pdev);
+ pdev->dev_state = USBD_STATE_CONFIGURED;
+
+#if (USBD_USER_REGISTER_CALLBACK == 1U)
+ if (pdev->DevStateCallback != NULL)
+ {
+ pdev->DevStateCallback(USBD_STATE_CONFIGURED, cfgidx);
+ }
+#endif /* USBD_USER_REGISTER_CALLBACK */
+ }
+ }
+ else
+ {
+ (void)USBD_CtlSendStatus(pdev);
+ }
+ break;
+
+ case USBD_STATE_CONFIGURED:
+ if (cfgidx == 0U)
+ {
+ pdev->dev_state = USBD_STATE_ADDRESSED;
+ pdev->dev_config = cfgidx;
+ (void)USBD_ClrClassConfig(pdev, cfgidx);
+ (void)USBD_CtlSendStatus(pdev);
+ }
+ else if (cfgidx != pdev->dev_config)
+ {
+ /* Clear old configuration */
+ (void)USBD_ClrClassConfig(pdev, (uint8_t)pdev->dev_config);
+
+ /* set new configuration */
+ pdev->dev_config = cfgidx;
+
+ ret = USBD_SetClassConfig(pdev, cfgidx);
+
+ if (ret != USBD_OK)
+ {
+ USBD_CtlError(pdev, req);
+ (void)USBD_ClrClassConfig(pdev, (uint8_t)pdev->dev_config);
+ pdev->dev_state = USBD_STATE_ADDRESSED;
+ }
+ else
+ {
+ (void)USBD_CtlSendStatus(pdev);
+ }
+ }
+ else
+ {
+ (void)USBD_CtlSendStatus(pdev);
+ }
+ break;
+
+ default:
+ USBD_CtlError(pdev, req);
+ (void)USBD_ClrClassConfig(pdev, cfgidx);
+ ret = USBD_FAIL;
+ break;
+ }
+
+ return ret;
+}
+
+/**
+ * @brief USBD_GetConfig
+ * Handle Get device configuration request
+ * @param pdev: device instance
+ * @param req: usb request
+ * @retval None
+ */
+static void USBD_GetConfig(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
+{
+ if (req->wLength != 1U)
+ {
+ USBD_CtlError(pdev, req);
+ }
+ else
+ {
+ switch (pdev->dev_state)
+ {
+ case USBD_STATE_DEFAULT:
+ case USBD_STATE_ADDRESSED:
+ pdev->dev_default_config = 0U;
+ (void)USBD_CtlSendData(pdev, (uint8_t *)&pdev->dev_default_config, 1U);
+ break;
+
+ case USBD_STATE_CONFIGURED:
+ (void)USBD_CtlSendData(pdev, (uint8_t *)&pdev->dev_config, 1U);
+ break;
+
+ default:
+ USBD_CtlError(pdev, req);
+ break;
+ }
+ }
+}
+
+/**
+ * @brief USBD_GetStatus
+ * Handle Get Status request
+ * @param pdev: device instance
+ * @param req: usb request
+ * @retval None
+ */
+static void USBD_GetStatus(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
+{
+ switch (pdev->dev_state)
+ {
+ case USBD_STATE_DEFAULT:
+ case USBD_STATE_ADDRESSED:
+ case USBD_STATE_CONFIGURED:
+ if (req->wLength != 0x2U)
+ {
+ USBD_CtlError(pdev, req);
+ break;
+ }
+
+#if (USBD_SELF_POWERED == 1U)
+ pdev->dev_config_status = USB_CONFIG_SELF_POWERED;
+#else
+ pdev->dev_config_status = 0U;
+#endif /* USBD_SELF_POWERED */
+
+ if (pdev->dev_remote_wakeup != 0U)
+ {
+ pdev->dev_config_status |= USB_CONFIG_REMOTE_WAKEUP;
+ }
+
+ (void)USBD_CtlSendData(pdev, (uint8_t *)&pdev->dev_config_status, 2U);
+ break;
+
+ default:
+ USBD_CtlError(pdev, req);
+ break;
+ }
+}
+
+
+/**
+ * @brief USBD_SetFeature
+ * Handle Set device feature request
+ * @param pdev: device instance
+ * @param req: usb request
+ * @retval None
+ */
+static void USBD_SetFeature(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
+{
+ if (req->wValue == USB_FEATURE_REMOTE_WAKEUP)
+ {
+ pdev->dev_remote_wakeup = 1U;
+ (void)USBD_CtlSendStatus(pdev);
+ }
+ else if (req->wValue == USB_FEATURE_TEST_MODE)
+ {
+ pdev->dev_test_mode = (uint8_t)(req->wIndex >> 8);
+ (void)USBD_CtlSendStatus(pdev);
+ }
+ else
+ {
+ USBD_CtlError(pdev, req);
+ }
+}
+
+
+/**
+ * @brief USBD_ClrFeature
+ * Handle clear device feature request
+ * @param pdev: device instance
+ * @param req: usb request
+ * @retval None
+ */
+static void USBD_ClrFeature(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
+{
+ switch (pdev->dev_state)
+ {
+ case USBD_STATE_DEFAULT:
+ case USBD_STATE_ADDRESSED:
+ case USBD_STATE_CONFIGURED:
+ if (req->wValue == USB_FEATURE_REMOTE_WAKEUP)
+ {
+ pdev->dev_remote_wakeup = 0U;
+ (void)USBD_CtlSendStatus(pdev);
+ }
+ break;
+
+ default:
+ USBD_CtlError(pdev, req);
+ break;
+ }
+}
+
+
+/**
+ * @brief USBD_ParseSetupRequest
+ * Copy buffer into setup structure
+ * @param req: usb request
+ * @param pdata: setup data pointer
+ * @retval None
+ */
+void USBD_ParseSetupRequest(USBD_SetupReqTypedef *req, uint8_t *pdata)
+{
+ uint8_t *pbuff = pdata;
+
+ req->bmRequest = *(uint8_t *)(pbuff);
+
+ pbuff++;
+ req->bRequest = *(uint8_t *)(pbuff);
+
+ pbuff++;
+ req->wValue = SWAPBYTE(pbuff);
+
+ pbuff++;
+ pbuff++;
+ req->wIndex = SWAPBYTE(pbuff);
+
+ pbuff++;
+ pbuff++;
+ req->wLength = SWAPBYTE(pbuff);
+}
+
+
+/**
+ * @brief USBD_CtlError
+ * Handle USB low level Error
+ * @param pdev: device instance
+ * @param req: usb request
+ * @retval None
+ */
+void USBD_CtlError(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req)
+{
+ UNUSED(req);
+
+ (void)USBD_LL_StallEP(pdev, 0x80U);
+ (void)USBD_LL_StallEP(pdev, 0U);
+}
+
+
+/**
+ * @brief USBD_GetString
+ * Convert Ascii string into unicode one
+ * @param desc : descriptor buffer
+ * @param unicode : Formatted string buffer (unicode)
+ * @param len : descriptor length
+ * @retval None
+ */
+void USBD_GetString(uint8_t *desc, uint8_t *unicode, uint16_t *len)
+{
+ uint8_t idx = 0U;
+ uint8_t *pdesc;
+
+ if (desc == NULL)
+ {
+ return;
+ }
+
+ pdesc = desc;
+ *len = MIN(USBD_MAX_STR_DESC_SIZ, ((uint16_t)USBD_GetLen(pdesc) * 2U) + 2U);
+
+ unicode[idx] = *(uint8_t *)len;
+ idx++;
+ unicode[idx] = USB_DESC_TYPE_STRING;
+ idx++;
+
+ while (*pdesc != (uint8_t)'\0')
+ {
+ unicode[idx] = *pdesc;
+ pdesc++;
+ idx++;
+
+ unicode[idx] = 0U;
+ idx++;
+ }
+}
+
+
+/**
+ * @brief USBD_GetLen
+ * return the string length
+ * @param buf : pointer to the ascii string buffer
+ * @retval string length
+ */
+static uint8_t USBD_GetLen(uint8_t *buf)
+{
+ uint8_t len = 0U;
+ uint8_t *pbuff = buf;
+
+ while (*pbuff != (uint8_t)'\0')
+ {
+ len++;
+ pbuff++;
+ }
+
+ return len;
+}
+/**
+ * @}
+ */
+
+
+/**
+ * @}
+ */
+
+
+/**
+ * @}
+ */
+
diff --git a/firmware/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ioreq.c b/firmware/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ioreq.c
new file mode 100644
index 0000000..2171398
--- /dev/null
+++ b/firmware/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ioreq.c
@@ -0,0 +1,226 @@
+/**
+ ******************************************************************************
+ * @file usbd_ioreq.c
+ * @author MCD Application Team
+ * @brief This file provides the IO requests APIs for control endpoints.
+ ******************************************************************************
+ * @attention
+ *
+ * Copyright (c) 2015 STMicroelectronics.
+ * All rights reserved.
+ *
+ * This software is licensed under terms that can be found in the LICENSE file
+ * in the root directory of this software component.
+ * If no LICENSE file comes with this software, it is provided AS-IS.
+ *
+ ******************************************************************************
+ */
+
+/* Includes ------------------------------------------------------------------*/
+#include "usbd_ioreq.h"
+
+/** @addtogroup STM32_USB_DEVICE_LIBRARY
+ * @{
+ */
+
+
+/** @defgroup USBD_IOREQ
+ * @brief control I/O requests module
+ * @{
+ */
+
+/** @defgroup USBD_IOREQ_Private_TypesDefinitions
+ * @{
+ */
+/**
+ * @}
+ */
+
+
+/** @defgroup USBD_IOREQ_Private_Defines
+ * @{
+ */
+
+/**
+ * @}
+ */
+
+
+/** @defgroup USBD_IOREQ_Private_Macros
+ * @{
+ */
+/**
+ * @}
+ */
+
+
+/** @defgroup USBD_IOREQ_Private_Variables
+ * @{
+ */
+
+/**
+ * @}
+ */
+
+
+/** @defgroup USBD_IOREQ_Private_FunctionPrototypes
+ * @{
+ */
+/**
+ * @}
+ */
+
+
+/** @defgroup USBD_IOREQ_Private_Functions
+ * @{
+ */
+
+/**
+ * @brief USBD_CtlSendData
+ * send data on the ctl pipe
+ * @param pdev: device instance
+ * @param buff: pointer to data buffer
+ * @param len: length of data to be sent
+ * @retval status
+ */
+USBD_StatusTypeDef USBD_CtlSendData(USBD_HandleTypeDef *pdev,
+ uint8_t *pbuf, uint32_t len)
+{
+ /* Set EP0 State */
+ pdev->ep0_state = USBD_EP0_DATA_IN;
+ pdev->ep_in[0].total_length = len;
+ pdev->ep_in[0].pbuffer = pbuf;
+
+#ifdef USBD_AVOID_PACKET_SPLIT_MPS
+ pdev->ep_in[0].rem_length = 0U;
+#else
+ pdev->ep_in[0].rem_length = len;
+#endif /* USBD_AVOID_PACKET_SPLIT_MPS */
+
+ /* Start the transfer */
+ (void)USBD_LL_Transmit(pdev, 0x00U, pbuf, len);
+
+ return USBD_OK;
+}
+
+/**
+ * @brief USBD_CtlContinueSendData
+ * continue sending data on the ctl pipe
+ * @param pdev: device instance
+ * @param buff: pointer to data buffer
+ * @param len: length of data to be sent
+ * @retval status
+ */
+USBD_StatusTypeDef USBD_CtlContinueSendData(USBD_HandleTypeDef *pdev,
+ uint8_t *pbuf, uint32_t len)
+{
+ /* Start the next transfer */
+ (void)USBD_LL_Transmit(pdev, 0x00U, pbuf, len);
+
+ return USBD_OK;
+}
+
+/**
+ * @brief USBD_CtlPrepareRx
+ * receive data on the ctl pipe
+ * @param pdev: device instance
+ * @param buff: pointer to data buffer
+ * @param len: length of data to be received
+ * @retval status
+ */
+USBD_StatusTypeDef USBD_CtlPrepareRx(USBD_HandleTypeDef *pdev,
+ uint8_t *pbuf, uint32_t len)
+{
+ /* Set EP0 State */
+ pdev->ep0_state = USBD_EP0_DATA_OUT;
+ pdev->ep_out[0].total_length = len;
+ pdev->ep_out[0].pbuffer = pbuf;
+
+#ifdef USBD_AVOID_PACKET_SPLIT_MPS
+ pdev->ep_out[0].rem_length = 0U;
+#else
+ pdev->ep_out[0].rem_length = len;
+#endif /* USBD_AVOID_PACKET_SPLIT_MPS */
+
+ /* Start the transfer */
+ (void)USBD_LL_PrepareReceive(pdev, 0U, pbuf, len);
+
+ return USBD_OK;
+}
+
+/**
+ * @brief USBD_CtlContinueRx
+ * continue receive data on the ctl pipe
+ * @param pdev: device instance
+ * @param buff: pointer to data buffer
+ * @param len: length of data to be received
+ * @retval status
+ */
+USBD_StatusTypeDef USBD_CtlContinueRx(USBD_HandleTypeDef *pdev,
+ uint8_t *pbuf, uint32_t len)
+{
+ (void)USBD_LL_PrepareReceive(pdev, 0U, pbuf, len);
+
+ return USBD_OK;
+}
+
+/**
+ * @brief USBD_CtlSendStatus
+ * send zero lzngth packet on the ctl pipe
+ * @param pdev: device instance
+ * @retval status
+ */
+USBD_StatusTypeDef USBD_CtlSendStatus(USBD_HandleTypeDef *pdev)
+{
+ /* Set EP0 State */
+ pdev->ep0_state = USBD_EP0_STATUS_IN;
+
+ /* Start the transfer */
+ (void)USBD_LL_Transmit(pdev, 0x00U, NULL, 0U);
+
+ return USBD_OK;
+}
+
+/**
+ * @brief USBD_CtlReceiveStatus
+ * receive zero lzngth packet on the ctl pipe
+ * @param pdev: device instance
+ * @retval status
+ */
+USBD_StatusTypeDef USBD_CtlReceiveStatus(USBD_HandleTypeDef *pdev)
+{
+ /* Set EP0 State */
+ pdev->ep0_state = USBD_EP0_STATUS_OUT;
+
+ /* Start the transfer */
+ (void)USBD_LL_PrepareReceive(pdev, 0U, NULL, 0U);
+
+ return USBD_OK;
+}
+
+/**
+ * @brief USBD_GetRxCount
+ * returns the received data length
+ * @param pdev: device instance
+ * @param ep_addr: endpoint address
+ * @retval Rx Data blength
+ */
+uint32_t USBD_GetRxCount(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
+{
+ return USBD_LL_GetRxDataSize(pdev, ep_addr);
+}
+
+/**
+ * @}
+ */
+
+
+/**
+ * @}
+ */
+
+
+/**
+ * @}
+ */
+
diff --git a/firmware/Middlewares/ST/STM32_USB_Device_Library/LICENSE.md b/firmware/Middlewares/ST/STM32_USB_Device_Library/LICENSE.md
new file mode 100644
index 0000000..5f9caee
--- /dev/null
+++ b/firmware/Middlewares/ST/STM32_USB_Device_Library/LICENSE.md
@@ -0,0 +1,94 @@
+This software component is provided to you as part of a software package and
+applicable license terms are in the Package_license file. If you received this
+software component outside of a package or without applicable license terms,
+the terms of the SLA0044 license shall apply and are fully reproduced below:
+
+SLA0044 Rev6/October 2025
+
+## Software license agreement
+
+### __ULTIMATE LIBERTY SOFTWARE LICENSE AGREEMENT__
+
+BY CLICKING ON THE "I ACCEPT" BUTTON OR BY UNZIPPING, INSTALLING, COPYING,
+DOWNLOADING, ACCESSING OR OTHERWISE USING THIS SOFTWARE OR ANY PART THEREOF,
+INCLUDING ANY RELATED DOCUMENTATION (collectively the “SOFTWARE”)
+FROM STMICROELECTRONICS INTERNATIONAL N.V, SWISS BRANCH AND/OR
+ITS AFFILIATED COMPANIES (collectively “STMICROELECTRONICS”),
+YOU (hereinafter referred also to as “THE RECIPIENT”), ON BEHALF OF YOURSELF,
+OR ON BEHALF OF ANY ENTITY BY WHICH YOU ARE EMPLOYED AND/OR ENGAGED,
+AGREE TO BE BOUND BY THIS AGREEMENT.
+
+Under STMICROELECTRONICS’ intellectual property rights, the redistribution,
+reproduction and use in source and binary forms of the SOFTWARE or any part
+thereof, with or without modification, are permitted provided that the following
+conditions are met:
+
+1. Redistribution of source code (modified or not) must retain any copyright
+notice accompanying the SOFTWARE, this list of conditions and the disclaimer below.
+
+2. Redistributions in binary form, except as embedded into a processing unit device
+manufactured by or for STMicroelectronics or a software update for any such device,
+must reproduce the accompanying copyright notice, this list of conditions,
+and the below disclaimer in capital type, in the documentation and/or
+other materials provided with the distribution.
+
+3. Neither the name of STMicroelectronics nor the names of other contributors
+to the SOFTWARE may be used to endorse or promote products derived
+from the SOFTWARE or part thereof without specific written permission.
+
+4. The SOFTWARE or any part thereof, including modifications and/or
+derivative works of the SOFTWARE, must be used and execute solely
+and exclusively on or in combination with a processing unit device
+manufactured by or for STMicroelectronics.
+
+5. No use, reproduction or redistribution of the SOFTWARE partially
+or totally may be done in any manner that would subject the SOFTWARE
+to any Open Source Terms. “Open Source Terms” shall mean
+any open source license which requires as part of distribution
+of software that the source code of such software is distributed
+therewith or otherwise made available, or open source license
+that substantially complies with the Open Source definition specified
+at www.opensource.org and any other comparable open source license
+such as for example GNU General Public License (GPL),
+Eclipse Public License (EPL), Apache Software License, BSD license
+or MIT license.
+
+6. STMicroelectronics has no obligation to provide any maintenance,
+support or updates for the SOFTWARE.
+
+7. The SOFTWARE is and will remain the exclusive property of
+STMicroelectronics and its licensors. The RECIPIENT will not take
+any action that jeopardizes STMicroelectronics and its
+licensors' proprietary rights or acquire any rights in the SOFTWARE,
+except the limited rights specified hereunder.
+
+8. The RECIPIENT shall comply with all applicable laws and regulations
+affecting the use of the SOFTWARE or any part thereof including
+any applicable export control law or regulation.
+
+9. Redistribution and use of the SOFTWARE or any part thereof other
+than as permitted under this AGREEMENT is void and will automatically
+terminate RECIPIENT’s rights under this AGREEMENT.
+
+10. The RECIPIENT shall be solely liable to determine and verify that
+the SOFTWARE is fit for the RECIPIENT intended use, environment or
+application and comply with all regulatory, safety and security
+related requirements concerning any use.
+
+DISCLAIMER:
+
+THE SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING,BUT NOT LIMITED TO,
+THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
+AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY RIGHTS, ARE
+DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW.
+IN NO EVENT SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THE
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+EXCEPT AS EXPRESSLY PERMITTED HEREUNDER, NO LICENSE OR OTHER RIGHTS,
+WHETHER EXPRESS OR IMPLIED, ARE GRANTED UNDER ANY PATENT OR OTHER INTELLECTUAL
+PROPERTY RIGHTS OF STMICROELECTRONICS OR ANY THIRD PARTY.
diff --git a/firmware/README.md b/firmware/README.md
index 3cec8d2..ee365ae 100644
--- a/firmware/README.md
+++ b/firmware/README.md
@@ -63,15 +63,29 @@ report), Battery and Device Information. Pairing is Just Works with bonding,
so the host just connects and remembers it.
Each Keybit sends whatever its UID is mapped to in `Core/Src/keymap.c`, and
-an unmapped Keybit sends nothing. The map is set over the Nordic UART Service
-(6e400001-b5a3-f393-e0a9-e50e24dcca9e) with the line protocol in
-`Core/Src/proto.c` (`HELLO`, `KEYS?`, `MAP`, `SAVE` and so on). `SAVE` writes
-it to the flash page just under CPU2's area (0x080CF000 with the full stack).
+an unmapped Keybit sends nothing. The map is set with the line protocol in
+`Core/Src/proto.c` (`HELLO`, `KEYS?`, `MAP`, `MAPC`, `SAVE` and so on) over
+either the Nordic UART Service (6e400001-b5a3-f393-e0a9-e50e24dcca9e) or USB.
+`SAVE` writes it to the flash page just under CPU2's area (0x080CF000 with the
+full stack).
+
+## USB serial
+
+Plugged into USB-C the keyword is also a CDC serial port (VID 0x0483, PID
+0x5740, "Keyword", it shows up as `/dev/tty.usbmodem*` on a Mac) talking the
+same protocol. Each port gets its own replies, and KEY+/KEY-/PRESS/SIDES go to
+every port that is open: BLE once TX notifications are on, USB once DTR is set
+or a line comes in. USB runs off HSI48 trimmed by CRS against the host's
+frames instead of PLLSAI1, set in `USB_Device/App/usb_device.c`.
+
+```sh
+screen /dev/tty.usbmodem* 115200 # then type HELLO and enter
+```
The parser has a host check:
```sh
-cc -std=c99 -Itest/stub -ICore/Inc -ISTM32_WPAN/App test/proto_test.c Core/Src/proto.c -o /tmp/proto_test && /tmp/proto_test
+cc -std=c99 -Itest/stub -ICore/Inc test/proto_test.c Core/Src/proto.c -o /tmp/proto_test && /tmp/proto_test
```
### First flash: the CPU2 BLE stack
diff --git a/firmware/STM32-for-VSCode.config.yaml b/firmware/STM32-for-VSCode.config.yaml
index 10c8c20..d2aaf04 100644
--- a/firmware/STM32-for-VSCode.config.yaml
+++ b/firmware/STM32-for-VSCode.config.yaml
@@ -94,6 +94,10 @@ includeDirectories:
- Middlewares/ST/STM32_WPAN/utilities
- Utilities/lpm/tiny_lpm
- Utilities/sequencer
+ - USB_Device/App
+ - USB_Device/Target
+ - Middlewares/ST/STM32_USB_Device_Library/Core/Inc
+ - Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc
# Files that should be included in the compilation.
@@ -129,6 +133,11 @@ sourceFiles:
- Middlewares/ST/STM32_WPAN/utilities/stm_queue.c
- Utilities/lpm/tiny_lpm/stm32_lpm.c
- Utilities/sequencer/stm32_seq.c
+ - USB_Device/**
+ - Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_core.c
+ - Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ctlreq.c
+ - Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ioreq.c
+ - Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Src/usbd_cdc.c
# When no makefile is present it will show a warning pop-up.
diff --git a/firmware/STM32Make.make b/firmware/STM32Make.make
index 35dc2aa..927d5b7 100644
--- a/firmware/STM32Make.make
+++ b/firmware/STM32Make.make
@@ -132,7 +132,15 @@ STM32_WPAN/App/hids_app.c \
STM32_WPAN/App/nus.c \
STM32_WPAN/Target/hw_ipcc.c \
Utilities/lpm/tiny_lpm/stm32_lpm.c \
-Utilities/sequencer/stm32_seq.c
+Utilities/sequencer/stm32_seq.c \
+USB_Device/App/usb_device.c \
+USB_Device/App/usbd_cdc_if.c \
+USB_Device/App/usbd_desc.c \
+USB_Device/Target/usbd_conf.c \
+Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_core.c \
+Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ctlreq.c \
+Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ioreq.c \
+Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Src/usbd_cdc.c
CXX_SOURCES = \
@@ -243,7 +251,11 @@ C_INCLUDES = \
-IMiddlewares/ST/STM32_WPAN/utilities \
-ISTM32_WPAN/App \
-IUtilities/lpm/tiny_lpm \
--IUtilities/sequencer
+-IUtilities/sequencer \
+-IUSB_Device/App \
+-IUSB_Device/Target \
+-IMiddlewares/ST/STM32_USB_Device_Library/Core/Inc \
+-IMiddlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc
diff --git a/firmware/STM32_WPAN/App/app_ble.c b/firmware/STM32_WPAN/App/app_ble.c
index 94220ff..0a1f1ea 100644
--- a/firmware/STM32_WPAN/App/app_ble.c
+++ b/firmware/STM32_WPAN/App/app_ble.c
@@ -29,6 +29,7 @@ static const uint8_t a_BLE_CfgIrValue[16] = CFG_BLE_IR;
static const uint8_t a_BLE_CfgErValue[16] = CFG_BLE_ER;
static uint16_t conn_handle = APP_BLE_NO_CONN;
+static uint8_t ble_ready;
static uint8_t adv_timer_id;
static uint8_t a_BdAddr[6];
@@ -53,6 +54,11 @@ uint16_t APP_BLE_ConnHandle(void)
return conn_handle;
}
+uint8_t APP_BLE_Ready(void)
+{
+ return ble_ready;
+}
+
static void Adv_Slow(void)
{
if (conn_handle == APP_BLE_NO_CONN) Adv_Start(0);
@@ -138,7 +144,7 @@ void APP_BLE_Init(void)
BAS_Update_Char(BATTERY_LEVEL_CHAR_UUID, 0, &level);
HIDSAPP_Init();
- KEYMAP_Init();
+ ble_ready = 1;
Adv_Start(1);
}
diff --git a/firmware/STM32_WPAN/App/app_ble.h b/firmware/STM32_WPAN/App/app_ble.h
index 7351160..6b361f0 100644
--- a/firmware/STM32_WPAN/App/app_ble.h
+++ b/firmware/STM32_WPAN/App/app_ble.h
@@ -15,4 +15,7 @@ void APP_BLE_Init(void);
/* Handle of the current link, APP_BLE_NO_CONN when nobody is connected. */
uint16_t APP_BLE_ConnHandle(void);
+/* 1 once the CPU2 stack is up and the services exist. */
+uint8_t APP_BLE_Ready(void);
+
#endif /* APP_BLE_H */
diff --git a/firmware/STM32_WPAN/App/nus.c b/firmware/STM32_WPAN/App/nus.c
index f9e2a2b..fda33f4 100644
--- a/firmware/STM32_WPAN/App/nus.c
+++ b/firmware/STM32_WPAN/App/nus.c
@@ -21,6 +21,8 @@ static uint16_t tx_head, tx_tail;
static uint8_t rx_buf[RX_SIZE];
static uint16_t rx_head, rx_tail;
+static proto_port_t nus_port = {.write = NUS_Write};
+
static void Tx_Drain(void)
{
while (tx_enabled && tx_tail != tx_head && APP_BLE_ConnHandle() != APP_BLE_NO_CONN)
@@ -53,6 +55,9 @@ void NUS_Write(const char *data, uint16_t len)
void NUS_Disconnected(void)
{
tx_enabled = 0;
+ nus_port.connected = 0;
+ nus_port.len = 0;
+ nus_port.overflow = 0;
tx_head = tx_tail = 0;
rx_head = rx_tail = 0;
}
@@ -63,7 +68,7 @@ static void Rx_Task(void)
{
uint16_t end = rx_head;
uint16_t n = (end > rx_tail ? end : RX_SIZE) - rx_tail;
- PROTO_Input(&rx_buf[rx_tail], n);
+ PROTO_Input(&nus_port, &rx_buf[rx_tail], n);
rx_tail = (rx_tail + n) % RX_SIZE;
}
}
@@ -82,6 +87,7 @@ static SVCCTL_EvtAckStatus_t NUS_Event_Handler(void *Event)
if (mod->Attr_Handle == tx_handle + 2) /* TX CCCD */
{
tx_enabled = mod->Attr_Data[0] & 1;
+ nus_port.connected = tx_enabled;
Tx_Drain();
return SVCCTL_EvtAckFlowEnable;
}
@@ -118,6 +124,7 @@ void NUS_Init(void)
const uint8_t tx_uuid[16] = NUS_UUID(0x03);
SVCCTL_RegisterSvcHandler(NUS_Event_Handler);
+ PROTO_AddPort(&nus_port);
UTIL_SEQ_RegTask(1 << CFG_TASK_NUS_RX_ID, UTIL_SEQ_RFU, Rx_Task);
memcpy(uuid.Char_UUID_128, svc_uuid, 16);
diff --git a/firmware/USB_Device/App/usb_device.c b/firmware/USB_Device/App/usb_device.c
new file mode 100644
index 0000000..559b54c
--- /dev/null
+++ b/firmware/USB_Device/App/usb_device.c
@@ -0,0 +1,53 @@
+#include "main.h"
+#include "app_conf.h"
+#include "usb_device.h"
+#include "usbd_core.h"
+#include "usbd_desc.h"
+#include "usbd_cdc.h"
+#include "usbd_cdc_if.h"
+
+USBD_HandleTypeDef hUsbDeviceFS;
+
+void USB_Clock_HSI48(void)
+{
+ /* CPU2 may switch CLK48 for its RNG unless CPU1 holds this semaphore,
+ * so take it for as long as USB is on (AN5289). */
+ __HAL_RCC_HSEM_CLK_ENABLE();
+ while (LL_HSEM_1StepLock(HSEM, CFG_HW_CLK48_CONFIG_SEMID));
+
+ LL_RCC_HSI48_Enable();
+ while (!LL_RCC_HSI48_IsReady());
+ LL_RCC_SetUSBClockSource(LL_RCC_USB_CLKSOURCE_HSI48);
+ /* CubeMX's MSP just started PLLSAI1 for USB; nothing else uses it. */
+ __HAL_RCC_PLLSAI1_DISABLE();
+
+ RCC_CRSInitTypeDef crs = {0};
+ __HAL_RCC_CRS_CLK_ENABLE();
+ crs.Prescaler = RCC_CRS_SYNC_DIV1;
+ crs.Source = RCC_CRS_SYNC_SOURCE_USB;
+ crs.Polarity = RCC_CRS_SYNC_POLARITY_RISING;
+ crs.ReloadValue = __HAL_RCC_CRS_RELOADVALUE_CALCULATE(48000000, 1000);
+ crs.ErrorLimitValue = RCC_CRS_ERRORLIMIT_DEFAULT;
+ crs.HSI48CalibrationValue = RCC_CRS_HSI48CALIBRATION_DEFAULT;
+ HAL_RCCEx_CRSConfig(&crs);
+}
+
+void MX_USB_Device_Init(void)
+{
+ CDC_PortInit();
+ if (USBD_Init(&hUsbDeviceFS, &CDC_Desc, DEVICE_FS) != USBD_OK
+ || USBD_RegisterClass(&hUsbDeviceFS, &USBD_CDC) != USBD_OK
+ || USBD_CDC_RegisterInterface(&hUsbDeviceFS, &USBD_Interface_fops_FS) != USBD_OK
+ || USBD_Start(&hUsbDeviceFS) != USBD_OK)
+ {
+ Error_Handler();
+ }
+}
+
+/* USB_LP isn't enabled in keyword.ioc, so the handler lives here rather
+ * than in the generated stm32wbxx_it.c. */
+void USB_LP_IRQHandler(void)
+{
+ extern PCD_HandleTypeDef hpcd_USB_FS;
+ HAL_PCD_IRQHandler(&hpcd_USB_FS);
+}
diff --git a/firmware/USB_Device/App/usb_device.h b/firmware/USB_Device/App/usb_device.h
new file mode 100644
index 0000000..932c032
--- /dev/null
+++ b/firmware/USB_Device/App/usb_device.h
@@ -0,0 +1,16 @@
+/*
+ * USB CDC serial (VID 0x0483, PID 0x5740, "Keyword") carrying the same
+ * line protocol as NUS.
+ */
+#ifndef USB_DEVICE_H
+#define USB_DEVICE_H
+
+/* 48 MHz for USB from HSI48 trimmed by CRS against the host's SOFs.
+ * Called from HAL_PCD_MspInit (USER CODE USB_MspInit 1). */
+void USB_Clock_HSI48(void);
+
+/* Starts the device stack. Call after MX_USB_PCD_Init and after the
+ * sequencer and timer server are up (MX_APPE_Init). */
+void MX_USB_Device_Init(void);
+
+#endif /* USB_DEVICE_H */
diff --git a/firmware/USB_Device/App/usbd_cdc_if.c b/firmware/USB_Device/App/usbd_cdc_if.c
new file mode 100644
index 0000000..5c8aa9e
--- /dev/null
+++ b/firmware/USB_Device/App/usbd_cdc_if.c
@@ -0,0 +1,194 @@
+/*
+ * CDC ACM interface: the config protocol over USB serial.
+ *
+ * RX: the USB ISR copies each packet into rx_ring and sets a sequencer
+ * task that feeds proto.c from the main loop. When rx_ring can't take
+ * another full packet the OUT endpoint isn't re-armed, so the host waits
+ * instead of us dropping bytes; the task re-arms it once there's room.
+ *
+ * TX: proto.c writes into tx_ring and Tx_Kick() starts a packet whenever
+ * the IN endpoint is idle. It runs from both the main loop and the
+ * transmit-complete ISR, so it runs with interrupts off.
+ */
+#include "app_common.h"
+#include "main.h"
+#include "stm32_seq.h"
+#include "utilities_conf.h"
+#include "usbd_cdc_if.h"
+#include "proto.h"
+
+#define RX_SIZE 512
+#define TX_SIZE 1024
+/* 63, not 64: a packet shorter than the max size ends the transfer, so
+ * the host never waits for a zero length packet. */
+#define TX_CHUNK (CDC_DATA_FS_MAX_PACKET_SIZE - 1)
+
+extern USBD_HandleTypeDef hUsbDeviceFS;
+
+static uint8_t rx_packet[CDC_DATA_FS_MAX_PACKET_SIZE];
+static uint8_t tx_packet[CDC_DATA_FS_MAX_PACKET_SIZE];
+
+static uint8_t rx_ring[RX_SIZE];
+static volatile uint16_t rx_head, rx_tail;
+static volatile uint8_t rx_paused, rx_reset;
+static char tx_ring[TX_SIZE];
+static volatile uint16_t tx_head, tx_tail;
+
+/* 115200 8N1, only stored and echoed back: the link is USB, not a UART */
+static uint8_t line_coding[7] = {0x00, 0xC2, 0x01, 0x00, 0x00, 0x00, 0x08};
+
+static void CDC_Write(const char *data, uint16_t len);
+static proto_port_t cdc_port = {.write = CDC_Write};
+
+static uint16_t Rx_Free(void)
+{
+ return (uint16_t)((rx_tail - rx_head - 1 + RX_SIZE) % RX_SIZE);
+}
+
+static uint8_t Configured(void)
+{
+ return hUsbDeviceFS.dev_state == USBD_STATE_CONFIGURED && hUsbDeviceFS.pClassData != NULL;
+}
+
+static void Tx_Kick(void)
+{
+ UTILS_ENTER_CRITICAL_SECTION();
+ if (Configured() && tx_tail != tx_head)
+ {
+ uint16_t n = 0;
+ for (uint16_t t = tx_tail; n < TX_CHUNK && t != tx_head; t = (t + 1) % TX_SIZE)
+ {
+ tx_packet[n++] = (uint8_t)tx_ring[t];
+ }
+ USBD_CDC_SetTxBuffer(&hUsbDeviceFS, tx_packet, n);
+ /* USBD_BUSY while the previous packet is in flight: the complete
+ * callback calls us again. */
+ if (USBD_CDC_TransmitPacket(&hUsbDeviceFS) == USBD_OK)
+ {
+ tx_tail = (tx_tail + n) % TX_SIZE;
+ }
+ }
+ UTILS_EXIT_CRITICAL_SECTION();
+}
+
+static void CDC_Write(const char *data, uint16_t len)
+{
+ if (!Configured()) return;
+ for (uint16_t i = 0; i < len; i++)
+ {
+ uint16_t next = (tx_head + 1) % TX_SIZE;
+ if (next == tx_tail) break; /* full: drop the rest, the host can ask again */
+ tx_ring[tx_head] = data[i];
+ tx_head = next;
+ }
+ Tx_Kick();
+}
+
+static void Rx_Task(void)
+{
+ if (rx_reset)
+ {
+ rx_reset = 0;
+ rx_tail = rx_head;
+ cdc_port.len = 0;
+ cdc_port.overflow = 0;
+ }
+ while (rx_tail != rx_head)
+ {
+ uint16_t end = rx_head;
+ uint16_t n = (end > rx_tail ? end : RX_SIZE) - rx_tail;
+ PROTO_Input(&cdc_port, &rx_ring[rx_tail], n);
+ rx_tail = (rx_tail + n) % RX_SIZE;
+ }
+ if (rx_paused && Rx_Free() >= CDC_DATA_FS_MAX_PACKET_SIZE)
+ {
+ rx_paused = 0;
+ if (Configured()) USBD_CDC_ReceivePacket(&hUsbDeviceFS);
+ }
+}
+
+void CDC_Detached(void)
+{
+ cdc_port.connected = 0;
+ tx_tail = tx_head;
+ rx_reset = 1;
+ rx_paused = 0;
+}
+
+static int8_t CDC_Init_FS(void)
+{
+ USBD_CDC_SetTxBuffer(&hUsbDeviceFS, tx_packet, 0);
+ USBD_CDC_SetRxBuffer(&hUsbDeviceFS, rx_packet);
+ return (int8_t)USBD_OK;
+}
+
+static int8_t CDC_DeInit_FS(void)
+{
+ CDC_Detached();
+ return (int8_t)USBD_OK;
+}
+
+static int8_t CDC_Control_FS(uint8_t cmd, uint8_t *pbuf, uint16_t length)
+{
+ switch (cmd)
+ {
+ case CDC_SET_LINE_CODING:
+ if (length >= sizeof(line_coding)) memcpy(line_coding, pbuf, sizeof(line_coding));
+ break;
+ case CDC_GET_LINE_CODING:
+ memcpy(pbuf, line_coding, sizeof(line_coding));
+ break;
+ case CDC_SET_CONTROL_LINE_STATE:
+ {
+ /* bit 0 of wValue is DTR: a terminal or the app opened the port */
+ uint8_t dtr = ((USBD_SetupReqTypedef *)pbuf)->wValue & 1;
+ cdc_port.connected = dtr;
+ if (!dtr) tx_tail = tx_head;
+ break;
+ }
+ default:
+ break;
+ }
+ return (int8_t)USBD_OK;
+}
+
+static int8_t CDC_Receive_FS(uint8_t *Buf, uint32_t *Len)
+{
+ for (uint32_t i = 0; i < *Len; i++)
+ {
+ uint16_t next = (rx_head + 1) % RX_SIZE;
+ if (next == rx_tail) break; /* can't happen: we only re-arm with room for a packet */
+ rx_ring[rx_head] = Buf[i];
+ rx_head = next;
+ }
+ UTIL_SEQ_SetTask(1 << CFG_TASK_CDC_RX_ID, CFG_SCH_PRIO_0);
+
+ USBD_CDC_SetRxBuffer(&hUsbDeviceFS, rx_packet);
+ if (Rx_Free() >= CDC_DATA_FS_MAX_PACKET_SIZE) USBD_CDC_ReceivePacket(&hUsbDeviceFS);
+ else rx_paused = 1;
+ return (int8_t)USBD_OK;
+}
+
+static int8_t CDC_TransmitCplt_FS(uint8_t *Buf, uint32_t *Len, uint8_t epnum)
+{
+ UNUSED(Buf);
+ UNUSED(Len);
+ UNUSED(epnum);
+ Tx_Kick();
+ return (int8_t)USBD_OK;
+}
+
+USBD_CDC_ItfTypeDef USBD_Interface_fops_FS =
+{
+ CDC_Init_FS,
+ CDC_DeInit_FS,
+ CDC_Control_FS,
+ CDC_Receive_FS,
+ CDC_TransmitCplt_FS
+};
+
+void CDC_PortInit(void)
+{
+ UTIL_SEQ_RegTask(1 << CFG_TASK_CDC_RX_ID, UTIL_SEQ_RFU, Rx_Task);
+ PROTO_AddPort(&cdc_port);
+}
diff --git a/firmware/USB_Device/App/usbd_cdc_if.h b/firmware/USB_Device/App/usbd_cdc_if.h
new file mode 100644
index 0000000..cc8a2eb
--- /dev/null
+++ b/firmware/USB_Device/App/usbd_cdc_if.h
@@ -0,0 +1,14 @@
+#ifndef USBD_CDC_IF_H
+#define USBD_CDC_IF_H
+
+#include "usbd_cdc.h"
+
+extern USBD_CDC_ItfTypeDef USBD_Interface_fops_FS;
+
+/* Bus reset, suspend or unplug: drop the session and anything queued. */
+void CDC_Detached(void);
+
+/* Registers the RX task and the protocol port, once at start up. */
+void CDC_PortInit(void);
+
+#endif /* USBD_CDC_IF_H */
diff --git a/firmware/USB_Device/App/usbd_desc.c b/firmware/USB_Device/App/usbd_desc.c
new file mode 100644
index 0000000..6ce04db
--- /dev/null
+++ b/firmware/USB_Device/App/usbd_desc.c
@@ -0,0 +1,396 @@
+/* USER CODE BEGIN Header */
+/**
+ ******************************************************************************
+ * @file USB_Device/CDC_Standalone/USB_Device/App/usbd_desc.c
+ * @author MCD Application Team
+ * @brief This file implements the USB device descriptors.
+ ******************************************************************************
+ * @attention
+ *
+ * Copyright (c) 2019-2021 STMicroelectronics.
+ * All rights reserved.
+ *
+ * This software is licensed under terms that can be found in the LICENSE file
+ * in the root directory of this software component.
+ * If no LICENSE file comes with this software, it is provided AS-IS.
+ *
+ ******************************************************************************
+ */
+/* USER CODE END Header */
+
+/* Includes ------------------------------------------------------------------*/
+#include "usbd_core.h"
+#include "usbd_desc.h"
+#include "usbd_conf.h"
+
+/* USER CODE BEGIN INCLUDE */
+
+/* USER CODE END INCLUDE */
+
+/* Private typedef -----------------------------------------------------------*/
+/* Private define ------------------------------------------------------------*/
+/* Private macro -------------------------------------------------------------*/
+
+/* USER CODE BEGIN PV */
+/* Private variables ---------------------------------------------------------*/
+
+/* USER CODE END PV */
+
+/** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
+ * @{
+ */
+
+/** @addtogroup USBD_DESC
+ * @{
+ */
+
+/** @defgroup USBD_DESC_Private_TypesDefinitions USBD_DESC_Private_TypesDefinitions
+ * @brief Private types.
+ * @{
+ */
+
+/* USER CODE BEGIN PRIVATE_TYPES */
+
+/* USER CODE END PRIVATE_TYPES */
+
+/**
+ * @}
+ */
+
+/** @defgroup USBD_DESC_Private_Defines USBD_DESC_Private_Defines
+ * @brief Private defines.
+ * @{
+ */
+
+#define USBD_VID 0x483
+#define USBD_LANGID_STRING 1033
+#define USBD_MANUFACTURER_STRING "keybytes"
+#define USBD_PID 0x5740
+#define USBD_PRODUCT_STRING "Keyword"
+#define USBD_CONFIGURATION_STRING "CDC Config"
+#define USBD_INTERFACE_STRING "CDC Interface"
+
+/* USER CODE BEGIN PRIVATE_DEFINES */
+
+/* USER CODE END PRIVATE_DEFINES */
+
+/**
+ * @}
+ */
+
+/* USER CODE BEGIN 0 */
+
+/* USER CODE END 0 */
+
+/** @defgroup USBD_DESC_Private_Macros USBD_DESC_Private_Macros
+ * @brief Private macros.
+ * @{
+ */
+
+/* USER CODE BEGIN PRIVATE_MACRO */
+
+/* USER CODE END PRIVATE_MACRO */
+
+/**
+ * @}
+ */
+
+/** @defgroup USBD_DESC_Private_FunctionPrototypes USBD_DESC_Private_FunctionPrototypes
+ * @brief Private functions declaration.
+ * @{
+ */
+
+static void Get_SerialNum(void);
+static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len);
+
+/**
+ * @}
+ */
+
+/** @defgroup USBD_DESC_Private_FunctionPrototypes USBD_DESC_Private_FunctionPrototypes
+ * @brief Private functions declaration.
+ * @{
+ */
+
+uint8_t * USBD_CDC_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
+uint8_t * USBD_CDC_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
+uint8_t * USBD_CDC_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
+uint8_t * USBD_CDC_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
+uint8_t * USBD_CDC_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
+uint8_t * USBD_CDC_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
+uint8_t * USBD_CDC_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
+
+/**
+ * @}
+ */
+
+/** @defgroup USBD_DESC_Private_Variables USBD_DESC_Private_Variables
+ * @brief Private variables.
+ * @{
+ */
+
+USBD_DescriptorsTypeDef CDC_Desc =
+{
+ USBD_CDC_DeviceDescriptor,
+ USBD_CDC_LangIDStrDescriptor,
+ USBD_CDC_ManufacturerStrDescriptor,
+ USBD_CDC_ProductStrDescriptor,
+ USBD_CDC_SerialStrDescriptor,
+ USBD_CDC_ConfigStrDescriptor,
+ USBD_CDC_InterfaceStrDescriptor
+};
+
+#if defined ( __ICCARM__ ) /* IAR Compiler */
+ #pragma data_alignment=4
+#endif /* defined ( __ICCARM__ ) */
+/** USB standard device descriptor. */
+__ALIGN_BEGIN uint8_t USBD_CDC_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END =
+{
+ 0x12, /*bLength */
+ USB_DESC_TYPE_DEVICE, /*bDescriptorType*/
+ 0x00, /*bcdUSB */
+ 0x02,
+ 0x02, /*bDeviceClass*/
+ 0x02, /*bDeviceSubClass*/
+ 0x00, /*bDeviceProtocol*/
+ USB_MAX_EP0_SIZE, /*bMaxPacketSize*/
+ LOBYTE(USBD_VID), /*idVendor*/
+ HIBYTE(USBD_VID), /*idVendor*/
+ LOBYTE(USBD_PID), /*idProduct*/
+ HIBYTE(USBD_PID), /*idProduct*/
+ 0x00, /*bcdDevice rel. 2.00*/
+ 0x02,
+ USBD_IDX_MFC_STR, /*Index of manufacturer string*/
+ USBD_IDX_PRODUCT_STR, /*Index of product string*/
+ USBD_IDX_SERIAL_STR, /*Index of serial number string*/
+ USBD_MAX_NUM_CONFIGURATION /*bNumConfigurations*/
+};
+
+/* USB_DeviceDescriptor */
+
+/**
+ * @}
+ */
+
+/** @defgroup USBD_DESC_Private_Variables USBD_DESC_Private_Variables
+ * @brief Private variables.
+ * @{
+ */
+
+#if defined ( __ICCARM__ ) /* IAR Compiler */
+ #pragma data_alignment=4
+#endif /* defined ( __ICCARM__ ) */
+
+/** USB lang identifier descriptor. */
+__ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END =
+{
+ USB_LEN_LANGID_STR_DESC,
+ USB_DESC_TYPE_STRING,
+ LOBYTE(USBD_LANGID_STRING),
+ HIBYTE(USBD_LANGID_STRING)
+};
+
+#if defined ( __ICCARM__ ) /* IAR Compiler */
+ #pragma data_alignment=4
+#endif /* defined ( __ICCARM__ ) */
+/* Internal string descriptor. */
+__ALIGN_BEGIN uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END;
+
+#if defined ( __ICCARM__ ) /*!< IAR Compiler */
+ #pragma data_alignment=4
+#endif
+__ALIGN_BEGIN uint8_t USBD_StringSerial[USB_SIZ_STRING_SERIAL] __ALIGN_END = {
+ USB_SIZ_STRING_SERIAL,
+ USB_DESC_TYPE_STRING,
+};
+
+/**
+ * @}
+ */
+
+/** @defgroup USBD_DESC_Private_Functions USBD_DESC_Private_Functions
+ * @brief Private functions.
+ * @{
+ */
+
+/**
+ * @brief Return the device descriptor
+ * @param speed : Current device speed
+ * @param length : Pointer to data length variable
+ * @retval Pointer to descriptor buffer
+ */
+uint8_t * USBD_CDC_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
+{
+ UNUSED(speed);
+ *length = sizeof(USBD_CDC_DeviceDesc);
+ return USBD_CDC_DeviceDesc;
+}
+
+/**
+ * @brief Return the LangID string descriptor
+ * @param speed : Current device speed
+ * @param length : Pointer to data length variable
+ * @retval Pointer to descriptor buffer
+ */
+uint8_t * USBD_CDC_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
+{
+ UNUSED(speed);
+ *length = sizeof(USBD_LangIDDesc);
+ return USBD_LangIDDesc;
+}
+
+/**
+ * @brief Return the product string descriptor
+ * @param speed : Current device speed
+ * @param length : Pointer to data length variable
+ * @retval Pointer to descriptor buffer
+ */
+uint8_t * USBD_CDC_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
+{
+ if(speed == 0)
+ {
+ USBD_GetString((uint8_t *)USBD_PRODUCT_STRING, USBD_StrDesc, length);
+ }
+ else
+ {
+ USBD_GetString((uint8_t *)USBD_PRODUCT_STRING, USBD_StrDesc, length);
+ }
+ return USBD_StrDesc;
+}
+
+/**
+ * @brief Return the manufacturer string descriptor
+ * @param speed : Current device speed
+ * @param length : Pointer to data length variable
+ * @retval Pointer to descriptor buffer
+ */
+uint8_t * USBD_CDC_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
+{
+ UNUSED(speed);
+ USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length);
+ return USBD_StrDesc;
+}
+
+/**
+ * @brief Return the serial number string descriptor
+ * @param speed : Current device speed
+ * @param length : Pointer to data length variable
+ * @retval Pointer to descriptor buffer
+ */
+uint8_t * USBD_CDC_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
+{
+ UNUSED(speed);
+ *length = USB_SIZ_STRING_SERIAL;
+
+ /* Update the serial number string descriptor with the data from the unique
+ * ID */
+ Get_SerialNum();
+
+ /* USER CODE BEGIN USBD_CDC_SerialStrDescriptor */
+
+ /* USER CODE END USBD_CDC_SerialStrDescriptor */
+
+ return (uint8_t *) USBD_StringSerial;
+}
+
+/**
+ * @brief Return the configuration string descriptor
+ * @param speed : Current device speed
+ * @param length : Pointer to data length variable
+ * @retval Pointer to descriptor buffer
+ */
+uint8_t * USBD_CDC_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
+{
+ if(speed == USBD_SPEED_HIGH)
+ {
+ USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING, USBD_StrDesc, length);
+ }
+ else
+ {
+ USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING, USBD_StrDesc, length);
+ }
+ return USBD_StrDesc;
+}
+
+/**
+ * @brief Return the interface string descriptor
+ * @param speed : Current device speed
+ * @param length : Pointer to data length variable
+ * @retval Pointer to descriptor buffer
+ */
+uint8_t * USBD_CDC_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
+{
+ if(speed == 0)
+ {
+ USBD_GetString((uint8_t *)USBD_INTERFACE_STRING, USBD_StrDesc, length);
+ }
+ else
+ {
+ USBD_GetString((uint8_t *)USBD_INTERFACE_STRING, USBD_StrDesc, length);
+ }
+ return USBD_StrDesc;
+}
+
+/**
+ * @brief Create the serial number string descriptor
+ * @param None
+ * @retval None
+ */
+static void Get_SerialNum(void)
+{
+ uint32_t deviceserial0;
+ uint32_t deviceserial1;
+ uint32_t deviceserial2;
+
+ deviceserial0 = *(uint32_t *) DEVICE_ID1;
+ deviceserial1 = *(uint32_t *) DEVICE_ID2;
+ deviceserial2 = *(uint32_t *) DEVICE_ID3;
+
+ deviceserial0 += deviceserial2;
+
+ if (deviceserial0 != 0)
+ {
+ IntToUnicode(deviceserial0, &USBD_StringSerial[2], 8);
+ IntToUnicode(deviceserial1, &USBD_StringSerial[18], 4);
+ }
+}
+
+/**
+ * @brief Convert Hex 32Bits value into char
+ * @param value: value to convert
+ * @param pbuf: pointer to the buffer
+ * @param len: buffer length
+ * @retval None
+ */
+static void IntToUnicode(uint32_t value, uint8_t * pbuf, uint8_t len)
+{
+ uint8_t idx = 0;
+
+ for (idx = 0; idx < len; idx++)
+ {
+ if (((value >> 28)) < 0xA)
+ {
+ pbuf[2 * idx] = (value >> 28) + '0';
+ }
+ else
+ {
+ pbuf[2 * idx] = (value >> 28) + 'A' - 10;
+ }
+
+ value = value << 4;
+
+ pbuf[2 * idx + 1] = 0;
+ }
+}
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
diff --git a/firmware/USB_Device/App/usbd_desc.h b/firmware/USB_Device/App/usbd_desc.h
new file mode 100644
index 0000000..1883d33
--- /dev/null
+++ b/firmware/USB_Device/App/usbd_desc.h
@@ -0,0 +1,143 @@
+/* USER CODE BEGIN Header */
+/**
+ ******************************************************************************
+ * @file USB_Device/CDC_Standalone/USB_Device/App/usbd_desc.h
+ * @author MCD Application Team
+ * @brief Header for usbd_desc.c file.
+ ******************************************************************************
+ * @attention
+ *
+ * Copyright (c) 2019-2021 STMicroelectronics.
+ * All rights reserved.
+ *
+ * This software is licensed under terms that can be found in the LICENSE file
+ * in the root directory of this software component.
+ * If no LICENSE file comes with this software, it is provided AS-IS.
+ *
+ ******************************************************************************
+ */
+/* USER CODE END Header */
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __USBD_DESC__C__
+#define __USBD_DESC__C__
+
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+/* Includes ------------------------------------------------------------------*/
+#include "usbd_def.h"
+
+/* USER CODE BEGIN INCLUDE */
+
+/* USER CODE END INCLUDE */
+
+/** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
+ * @{
+ */
+
+/** @defgroup USBD_DESC USBD_DESC
+ * @brief Usb device descriptors module.
+ * @{
+ */
+
+/** @defgroup USBD_DESC_Exported_Constants USBD_DESC_Exported_Constants
+ * @brief Constants.
+ * @{
+ */
+#define DEVICE_ID1 (UID_BASE)
+#define DEVICE_ID2 (UID_BASE + 0x4)
+#define DEVICE_ID3 (UID_BASE + 0x8)
+
+#define USB_SIZ_STRING_SERIAL 0x1A
+
+/* USER CODE BEGIN EXPORTED_CONSTANTS */
+
+/* USER CODE END EXPORTED_CONSTANTS */
+
+/**
+ * @}
+ */
+
+/** @defgroup USBD_DESC_Exported_Defines USBD_DESC_Exported_Defines
+ * @brief Defines.
+ * @{
+ */
+
+/* USER CODE BEGIN EXPORTED_DEFINES */
+
+/* USER CODE END EXPORTED_DEFINES */
+
+/**
+ * @}
+ */
+
+/** @defgroup USBD_DESC_Exported_TypesDefinitions USBD_DESC_Exported_TypesDefinitions
+ * @brief Types.
+ * @{
+ */
+
+/* USER CODE BEGIN EXPORTED_TYPES */
+
+/* USER CODE END EXPORTED_TYPES */
+
+/**
+ * @}
+ */
+
+/** @defgroup USBD_DESC_Exported_Macros USBD_DESC_Exported_Macros
+ * @brief Aliases.
+ * @{
+ */
+
+/* USER CODE BEGIN EXPORTED_MACRO */
+
+/* USER CODE END EXPORTED_MACRO */
+
+/**
+ * @}
+ */
+
+/** @defgroup USBD_DESC_Exported_Variables USBD_DESC_Exported_Variables
+ * @brief Public variables.
+ * @{
+ */
+
+extern USBD_DescriptorsTypeDef CDC_Desc;
+
+/* USER CODE BEGIN EXPORTED_VARIABLES */
+
+/* USER CODE END EXPORTED_VARIABLES */
+
+/**
+ * @}
+ */
+
+/** @defgroup USBD_DESC_Exported_FunctionsPrototype USBD_DESC_Exported_FunctionsPrototype
+ * @brief Public functions declaration.
+ * @{
+ */
+
+/* USER CODE BEGIN EXPORTED_FUNCTIONS */
+
+/* USER CODE END EXPORTED_FUNCTIONS */
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __USBD_DESC__C__ */
+
diff --git a/firmware/USB_Device/Target/usbd_conf.c b/firmware/USB_Device/Target/usbd_conf.c
new file mode 100644
index 0000000..4eabed9
--- /dev/null
+++ b/firmware/USB_Device/Target/usbd_conf.c
@@ -0,0 +1,194 @@
+/*
+ * Glue between ST's USB device library and the HAL PCD driver, based on
+ * ST's CDC_Standalone usbd_conf.c (STM32CubeWB 1.24.0).
+ *
+ * hpcd_USB_FS belongs to main.c: MX_USB_PCD_Init (CubeMX) already ran
+ * HAL_PCD_Init and HAL_PCD_MspInit set up the pins, so USBD_LL_Init only
+ * links the handles, lays out the packet memory and enables the IRQ.
+ */
+#include "main.h"
+#include "usbd_def.h"
+#include "usbd_core.h"
+#include "usbd_cdc.h"
+#include "usbd_cdc_if.h"
+
+extern PCD_HandleTypeDef hpcd_USB_FS;
+
+static USBD_StatusTypeDef USBD_Get_USB_Status(HAL_StatusTypeDef hal_status)
+{
+ switch (hal_status)
+ {
+ case HAL_OK: return USBD_OK;
+ case HAL_BUSY: return USBD_BUSY;
+ default: return USBD_FAIL;
+ }
+}
+
+/* ---- PCD -> USB device library ---- */
+
+void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
+{
+ USBD_LL_SetupStage((USBD_HandleTypeDef *)hpcd->pData, (uint8_t *)hpcd->Setup);
+}
+
+void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
+{
+ USBD_LL_DataOutStage((USBD_HandleTypeDef *)hpcd->pData, epnum, hpcd->OUT_ep[epnum].xfer_buff);
+}
+
+void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
+{
+ USBD_LL_DataInStage((USBD_HandleTypeDef *)hpcd->pData, epnum, hpcd->IN_ep[epnum].xfer_buff);
+}
+
+void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
+{
+ USBD_LL_SOF((USBD_HandleTypeDef *)hpcd->pData);
+}
+
+void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
+{
+ CDC_Detached();
+ USBD_LL_SetSpeed((USBD_HandleTypeDef *)hpcd->pData, USBD_SPEED_FULL);
+ USBD_LL_Reset((USBD_HandleTypeDef *)hpcd->pData);
+}
+
+/* VBUS isn't wired to the MCU (it only goes to the BQ25895), so a bus
+ * suspend is how an unplug shows up. */
+void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
+{
+ CDC_Detached();
+ USBD_LL_Suspend((USBD_HandleTypeDef *)hpcd->pData);
+}
+
+void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
+{
+ USBD_LL_Resume((USBD_HandleTypeDef *)hpcd->pData);
+}
+
+void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
+{
+ USBD_LL_IsoOUTIncomplete((USBD_HandleTypeDef *)hpcd->pData, epnum);
+}
+
+void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
+{
+ USBD_LL_IsoINIncomplete((USBD_HandleTypeDef *)hpcd->pData, epnum);
+}
+
+void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
+{
+ USBD_LL_DevConnected((USBD_HandleTypeDef *)hpcd->pData);
+}
+
+void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
+{
+ CDC_Detached();
+ USBD_LL_DevDisconnected((USBD_HandleTypeDef *)hpcd->pData);
+}
+
+/* ---- USB device library -> PCD ---- */
+
+USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev)
+{
+ hpcd_USB_FS.pData = pdev;
+ pdev->pData = &hpcd_USB_FS;
+
+ /* Packet memory: 8 byte BTABLE for 5 endpoints, then 64 bytes each */
+ HAL_PCDEx_PMAConfig(&hpcd_USB_FS, 0x00, PCD_SNG_BUF, 0x14);
+ HAL_PCDEx_PMAConfig(&hpcd_USB_FS, 0x80, PCD_SNG_BUF, 0x54);
+ HAL_PCDEx_PMAConfig(&hpcd_USB_FS, CDC_IN_EP, PCD_SNG_BUF, 0x94);
+ HAL_PCDEx_PMAConfig(&hpcd_USB_FS, CDC_OUT_EP, PCD_SNG_BUF, 0xD4);
+ HAL_PCDEx_PMAConfig(&hpcd_USB_FS, CDC_CMD_EP, PCD_SNG_BUF, 0x114);
+
+ /* Below the radio's IPCC/RTC IRQs (priority 0). */
+ HAL_NVIC_SetPriority(USB_LP_IRQn, 5, 0);
+ HAL_NVIC_EnableIRQ(USB_LP_IRQn);
+ return USBD_OK;
+}
+
+USBD_StatusTypeDef USBD_LL_DeInit(USBD_HandleTypeDef *pdev)
+{
+ HAL_NVIC_DisableIRQ(USB_LP_IRQn);
+ return USBD_Get_USB_Status(HAL_PCD_DeInit(pdev->pData));
+}
+
+USBD_StatusTypeDef USBD_LL_Start(USBD_HandleTypeDef *pdev)
+{
+ return USBD_Get_USB_Status(HAL_PCD_Start(pdev->pData));
+}
+
+USBD_StatusTypeDef USBD_LL_Stop(USBD_HandleTypeDef *pdev)
+{
+ return USBD_Get_USB_Status(HAL_PCD_Stop(pdev->pData));
+}
+
+USBD_StatusTypeDef USBD_LL_OpenEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t ep_type, uint16_t ep_mps)
+{
+ return USBD_Get_USB_Status(HAL_PCD_EP_Open(pdev->pData, ep_addr, ep_mps, ep_type));
+}
+
+USBD_StatusTypeDef USBD_LL_CloseEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
+{
+ return USBD_Get_USB_Status(HAL_PCD_EP_Close(pdev->pData, ep_addr));
+}
+
+USBD_StatusTypeDef USBD_LL_FlushEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
+{
+ return USBD_Get_USB_Status(HAL_PCD_EP_Flush(pdev->pData, ep_addr));
+}
+
+USBD_StatusTypeDef USBD_LL_StallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
+{
+ return USBD_Get_USB_Status(HAL_PCD_EP_SetStall(pdev->pData, ep_addr));
+}
+
+USBD_StatusTypeDef USBD_LL_ClearStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
+{
+ return USBD_Get_USB_Status(HAL_PCD_EP_ClrStall(pdev->pData, ep_addr));
+}
+
+uint8_t USBD_LL_IsStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
+{
+ PCD_HandleTypeDef *hpcd = (PCD_HandleTypeDef *)pdev->pData;
+ if ((ep_addr & 0x80) == 0x80) return hpcd->IN_ep[ep_addr & 0x7F].is_stall;
+ return hpcd->OUT_ep[ep_addr & 0x7F].is_stall;
+}
+
+USBD_StatusTypeDef USBD_LL_SetUSBAddress(USBD_HandleTypeDef *pdev, uint8_t dev_addr)
+{
+ return USBD_Get_USB_Status(HAL_PCD_SetAddress(pdev->pData, dev_addr));
+}
+
+USBD_StatusTypeDef USBD_LL_Transmit(USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t *pbuf, uint32_t size)
+{
+ return USBD_Get_USB_Status(HAL_PCD_EP_Transmit(pdev->pData, ep_addr, pbuf, size));
+}
+
+USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t *pbuf, uint32_t size)
+{
+ return USBD_Get_USB_Status(HAL_PCD_EP_Receive(pdev->pData, ep_addr, pbuf, size));
+}
+
+uint32_t USBD_LL_GetRxDataSize(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
+{
+ return HAL_PCD_EP_GetRxCount((PCD_HandleTypeDef *)pdev->pData, ep_addr);
+}
+
+void USBD_LL_Delay(uint32_t Delay)
+{
+ HAL_Delay(Delay);
+}
+
+/* The CDC class asks for its handle once, so one static block does. */
+void *USBD_static_malloc(uint32_t size)
+{
+ UNUSED(size);
+ static uint32_t mem[(sizeof(USBD_CDC_HandleTypeDef) / 4) + 1];
+ return mem;
+}
+
+void USBD_static_free(void *p)
+{
+ UNUSED(p);
+}
diff --git a/firmware/USB_Device/Target/usbd_conf.h b/firmware/USB_Device/Target/usbd_conf.h
new file mode 100644
index 0000000..ed4f94f
--- /dev/null
+++ b/firmware/USB_Device/Target/usbd_conf.h
@@ -0,0 +1,174 @@
+/* USER CODE BEGIN Header */
+/**
+ ******************************************************************************
+ * @file USB_Device/CDC_Standalone/USB_Device/Target/usbd_conf.h
+ * @author MCD Application Team
+ * @brief Header for usbd_conf.c file.
+ ******************************************************************************
+ * @attention
+ *
+ * Copyright (c) 2019-2021 STMicroelectronics.
+ * All rights reserved.
+ *
+ * This software is licensed under terms that can be found in the LICENSE file
+ * in the root directory of this software component.
+ * If no LICENSE file comes with this software, it is provided AS-IS.
+ *
+ ******************************************************************************
+ */
+/* USER CODE END Header */
+
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __USBD_CONF__H__
+#define __USBD_CONF__H__
+
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+/* Includes ------------------------------------------------------------------*/
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "stm32wbxx.h"
+#include "stm32wbxx_hal.h"
+
+/* USER CODE BEGIN INCLUDE */
+
+/* USER CODE END INCLUDE */
+
+/** @addtogroup USBD_OTG_DRIVER
+ * @brief Driver for Usb device.
+ * @{
+ */
+
+/** @defgroup USBD_CONF USBD_CONF
+ * @brief Configuration file for Usb otg low level driver.
+ * @{
+ */
+
+/** @defgroup USBD_CONF_Exported_Variables USBD_CONF_Exported_Variables
+ * @brief Public variables.
+ * @{
+ */
+
+/* Private variables ---------------------------------------------------------*/
+/* USER CODE BEGIN PV */
+/* USER CODE END PV */
+/**
+ * @}
+ */
+
+/** @defgroup USBD_CONF_Exported_Defines USBD_CONF_Exported_Defines
+ * @brief Defines for configuration of the Usb device.
+ * @{
+ */
+
+/*---------- -----------*/
+#define USBD_MAX_NUM_INTERFACES 1U
+/*---------- -----------*/
+#define USBD_MAX_NUM_CONFIGURATION 1U
+/*---------- -----------*/
+#define USBD_MAX_STR_DESC_SIZ 100U
+/*---------- -----------*/
+#define USBD_DEBUG_LEVEL 0U
+/*---------- -----------*/
+#define USBD_LPM_ENABLED 0U
+/*---------- -----------*/
+#define USBD_SELF_POWERED 1U
+
+/****************************************/
+/* #define for FS and HS identification */
+#define DEVICE_FS 0
+
+/**
+ * @}
+ */
+
+/** @defgroup USBD_CONF_Exported_Macros USBD_CONF_Exported_Macros
+ * @brief Aliases.
+ * @{
+ */
+
+/* Memory management macros */
+
+/** Alias for memory allocation. */
+#define USBD_malloc (void *)USBD_static_malloc
+
+/** Alias for memory release. */
+#define USBD_free USBD_static_free
+
+/** Alias for memory set. */
+#define USBD_memset memset
+
+/** Alias for memory copy. */
+#define USBD_memcpy memcpy
+
+/** Alias for delay. */
+#define USBD_Delay HAL_Delay
+/* DEBUG macros */
+
+#if (USBD_DEBUG_LEVEL > 0)
+#define USBD_UsrLog(...) printf(__VA_ARGS__);\
+ printf("\n");
+#else
+#define USBD_UsrLog(...)
+#endif /* (USBD_DEBUG_LEVEL > 0U) */
+
+#if (USBD_DEBUG_LEVEL > 1)
+
+#define USBD_ErrLog(...) printf("ERROR: ");\
+ printf(__VA_ARGS__);\
+ printf("\n");
+#else
+#define USBD_ErrLog(...)
+#endif /* (USBD_DEBUG_LEVEL > 1U) */
+
+#if (USBD_DEBUG_LEVEL > 2)
+#define USBD_DbgLog(...) printf("DEBUG : ");\
+ printf(__VA_ARGS__);\
+ printf("\n");
+#else
+#define USBD_DbgLog(...)
+#endif /* (USBD_DEBUG_LEVEL > 2U) */
+
+/**
+ * @}
+ */
+
+/** @defgroup USBD_CONF_Exported_Types USBD_CONF_Exported_Types
+ * @brief Types.
+ * @{
+ */
+
+/**
+ * @}
+ */
+
+/** @defgroup USBD_CONF_Exported_FunctionsPrototype USBD_CONF_Exported_FunctionsPrototype
+ * @brief Declaration of public functions for Usb device.
+ * @{
+ */
+
+/* Exported functions -------------------------------------------------------*/
+void *USBD_static_malloc(uint32_t size);
+void USBD_static_free(void *p);
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+/**
+ * @}
+ */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __USBD_CONF__H__ */
+
diff --git a/firmware/test/proto_test.c b/firmware/test/proto_test.c
index 314ce32..d34f112 100644
--- a/firmware/test/proto_test.c
+++ b/firmware/test/proto_test.c
@@ -1,18 +1,20 @@
/*
* Host check for the config protocol parser in Core/Src/proto.c, with the
- * keymap and NUS replaced by small fakes.
- * cc -std=c99 -Itest/stub -ICore/Inc -ISTM32_WPAN/App test/proto_test.c Core/Src/proto.c -o /tmp/proto_test && /tmp/proto_test
+ * keymap and the two transports (NUS, USB CDC) replaced by small fakes.
+ * cc -std=c99 -Itest/stub -ICore/Inc test/proto_test.c Core/Src/proto.c -o /tmp/proto_test && /tmp/proto_test
*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "keymap.h"
-#include "nus.h"
#include "proto.h"
-static char out[2048];
-
-void NUS_Write(const char *data, uint16_t len) { strncat(out, data, len); }
+/* Two fake transports, like NUS and USB CDC. */
+static char out[2048], out2[2048];
+static void write1(const char *data, uint16_t len) { strncat(out, data, len); }
+static void write2(const char *data, uint16_t len) { strncat(out2, data, len); }
+static proto_port_t port = {.write = write1};
+static proto_port_t port2 = {.write = write2};
static struct { uint8_t uid[UID_LEN], kind, mods; uint16_t code; } table[KEYMAP_MAX];
static int n;
@@ -45,15 +47,18 @@ const keybit_info_t *KEYMAP_Present(int *count) { *count = 2; return present; }
static void expect(const char *in, const char *want)
{
- out[0] = 0;
- PROTO_Input((const uint8_t *)in, (uint16_t)strlen(in));
+ out[0] = out2[0] = 0;
+ PROTO_Input(&port, (const uint8_t *)in, (uint16_t)strlen(in));
if (strcmp(out, want) != 0) { printf("in: %s\nwant: %s\ngot: %s\n", in, want, out); assert(0); }
+ assert(out2[0] == 0); /* replies only go to the port that asked */
}
#define U "0102030405060708090A0B0C"
int main(void)
{
+ PROTO_AddPort(&port);
+ PROTO_AddPort(&port2);
for (int i = 0; i < UID_LEN; i++) present[0].uid[i] = (uint8_t)(12 - i); /* prints as U */
present[0].at = 'L';
memset(present[1].uid, 0xAB, UID_LEN);
@@ -61,6 +66,8 @@ int main(void)
expect("HELLO\n", "KEYWORD v=" KEYWORD_FW_VERSION " proto=1\n");
expect("HELLO\r\n", "KEYWORD v=" KEYWORD_FW_VERSION " proto=1\n");
+ expect("HELLO\r", "KEYWORD v=" KEYWORD_FW_VERSION " proto=1\n"); /* terminal Enter */
+ expect("\r\n\r\n", "");
expect("SIDES?\n", "SIDES L=1 R=0 T=1\n");
expect("KEYS?\n", "KEY+ uid=" U " at=L\nKEY+ uid=ABABABABABABABABABABABAB at=T\nKEYS END\n");
expect("MAP? uid=" U "\n", "MAP uid=" U " mods=00 key=00\n");
@@ -92,10 +99,10 @@ int main(void)
/* chunked like 20 byte BLE writes */
out[0] = 0;
- PROTO_Input((const uint8_t *)"MAP uid=01020304050", 19);
+ PROTO_Input(&port, (const uint8_t *)"MAP uid=01020304050", 19);
assert(out[0] == 0);
- PROTO_Input((const uint8_t *)"60708090A0B0C mods=", 19);
- PROTO_Input((const uint8_t *)"00 key=1E\n", 10);
+ PROTO_Input(&port, (const uint8_t *)"60708090A0B0C mods=", 19);
+ PROTO_Input(&port, (const uint8_t *)"00 key=1E\n", 10);
assert(strcmp(out, "OK MAP uid=" U "\n") == 0);
expect("MAP uid=0102 mods=00 key=04\n", "ERR usage: MAP uid=<24hex> mods=<2hex> key=<2hex>\n");
@@ -115,6 +122,34 @@ int main(void)
expect(longline, "ERR line too long\n");
expect("HELLO\n", "KEYWORD v=" KEYWORD_FW_VERSION " proto=1\n");
+ /* Two transports at once: each has its own line buffer, so half a line
+ * on one doesn't mix with the other, and replies go back where they
+ * came from. */
+ out[0] = out2[0] = 0;
+ PROTO_Input(&port, (const uint8_t *)"HEL", 3);
+ PROTO_Input(&port2, (const uint8_t *)"SIDES?\n", 7);
+ assert(out[0] == 0);
+ assert(strcmp(out2, "SIDES L=1 R=0 T=1\n") == 0);
+ PROTO_Input(&port, (const uint8_t *)"LO\n", 3);
+ assert(strcmp(out, "KEYWORD v=" KEYWORD_FW_VERSION " proto=1\n") == 0);
+ assert(strcmp(out2, "SIDES L=1 R=0 T=1\n") == 0);
+
+ /* Unsolicited events go to every connected port, and only those. */
+ port.connected = 0;
+ port2.connected = 0;
+ out[0] = out2[0] = 0;
+ PROTO_Send("PRESS uid=%s down=%d", U, 1);
+ assert(out[0] == 0 && out2[0] == 0);
+ port2.connected = 1; /* e.g. DTR set on CDC */
+ PROTO_Send("PRESS uid=%s down=%d", U, 1);
+ assert(out[0] == 0 && strcmp(out2, "PRESS uid=" U " down=1\n") == 0);
+ out2[0] = 0;
+ PROTO_Input(&port, (const uint8_t *)"HELLO\n", 6); /* a line marks the port connected */
+ assert(port.connected);
+ out[0] = 0;
+ PROTO_Send("KEY- uid=%s", U);
+ assert(strcmp(out, "KEY- uid=" U "\n") == 0 && strcmp(out2, "KEY- uid=" U "\n") == 0);
+
puts("proto_test: ok");
return 0;
}