v1
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
/*!
|
||||
* \file usb_device.c
|
||||
*
|
||||
* \brief Target usb device implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usb_device.h"
|
||||
#include "usbd_cdc.h"
|
||||
#include "usbd_cdc_if.h"
|
||||
#include "usbd_core.h"
|
||||
#include "usbd_desc.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/* USB Device Core handle declaration. */
|
||||
USBD_HandleTypeDef hUsbDeviceFS;
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief This function handles USB On The Go FS global interrupt.
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void USB_Handler(void) { HAL_PCD_IRQHandler(&hpcd_USB_OTG_FS); }
|
||||
|
||||
/**
|
||||
* @brief Init USB device Library, add supported class and start the library
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void USB_DEVICE_Init(void)
|
||||
{
|
||||
/* Init Device Library, add supported class and start the library. */
|
||||
if (USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS) != USBD_OK) {
|
||||
USBD_UsrLog("__USB_INIT_FAIL__\r\n");
|
||||
Error_Handler();
|
||||
}
|
||||
if (USBD_RegisterClass(&hUsbDeviceFS, &USBD_CDC) != USBD_OK) {
|
||||
USBD_UsrLog("__USB_CLASS_FAIL__\r\n");
|
||||
Error_Handler();
|
||||
}
|
||||
if (USBD_CDC_RegisterInterface(&hUsbDeviceFS, &USBD_Interface_fops_FS) !=
|
||||
USBD_OK) {
|
||||
USBD_UsrLog("__USB_CDC_FAIL__\r\n");
|
||||
Error_Handler();
|
||||
}
|
||||
if (USBD_Start(&hUsbDeviceFS) != USBD_OK) {
|
||||
USBD_UsrLog("__USB_START_FAIL__\r\n");
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*!
|
||||
* \file usb_device.h
|
||||
*
|
||||
* \brief Target usb device implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __USB_DEVICE_H
|
||||
#define __USB_DEVICE_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usbd_def.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void USB_DEVICE_Init( void );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __USB_DEVICE_H */
|
||||
@@ -0,0 +1,322 @@
|
||||
/*!
|
||||
* \file usbd_cdc_if.c
|
||||
*
|
||||
* \brief Usb device for Virtual Com Port.
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usbd_cdc_if.h"
|
||||
#include "ringbuffer.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @defgroup USBD_CDC_IF_Exported_Variables USBD_CDC_IF_Exported_Variables
|
||||
* @brief Public variables.
|
||||
* @{
|
||||
*/
|
||||
uint8_t Next_ep = 0;
|
||||
extern USBD_HandleTypeDef hUsbDeviceFS;
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Local Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** Received data over USB are stored in this buffer */
|
||||
uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];
|
||||
uint16_t UserRxBufferLen = 0;
|
||||
|
||||
/** Data to send over USB CDC are stored in this buffer */
|
||||
uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];
|
||||
uint8_t UserReTx = false;
|
||||
|
||||
uint8_t TempBuffer[APP_TX_DATA_SIZE / 2];
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Func Prototype
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @defgroup USBD_CDC_IF_Private_FunctionPrototypes
|
||||
* USBD_CDC_IF_Private_FunctionPrototypes
|
||||
* @brief Private functions declaration.
|
||||
* @{
|
||||
*/
|
||||
|
||||
static int8_t CDC_Init_FS(void);
|
||||
static int8_t CDC_DeInit_FS(void);
|
||||
static int8_t CDC_Control_FS(uint8_t cmd, uint8_t *pbuf, uint16_t length);
|
||||
static int8_t CDC_Receive_FS(uint8_t *pbuf, uint32_t *Len);
|
||||
static int8_t CDC_TransmitCplt_FS(uint8_t *pbuf, uint32_t *Len, uint8_t epnum);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
USBD_CDC_ItfTypeDef USBD_Interface_fops_FS = {CDC_Init_FS, CDC_DeInit_FS,
|
||||
CDC_Control_FS, CDC_Receive_FS,
|
||||
CDC_TransmitCplt_FS};
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief Initializes the CDC media low layer over the FS USB IP
|
||||
* @retval USBD_OK if all operations are OK else USBD_FAIL
|
||||
*/
|
||||
static int8_t CDC_Init_FS(void)
|
||||
{
|
||||
/* Set Application Buffers */
|
||||
memset(TempBuffer, 0, sizeof(TempBuffer));
|
||||
UserRxBufferLen = 0;
|
||||
// USB_FlushTxFifo(hpcd_USB_OTG_FS.Instance, 0x10U);
|
||||
USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, 0);
|
||||
USBD_CDC_SetRxBuffer(&hUsbDeviceFS, UserRxBufferFS);
|
||||
|
||||
return (USBD_OK);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DeInitializes the CDC media low layer
|
||||
* @retval USBD_OK if all operations are OK else USBD_FAIL
|
||||
*/
|
||||
static int8_t CDC_DeInit_FS(void) { return (USBD_OK); }
|
||||
|
||||
/**
|
||||
* @brief Manage the CDC class requests
|
||||
* @param cmd: Command code
|
||||
* @param pbuf: Buffer containing command data (request parameters)
|
||||
* @param length: Number of data to be sent (in bytes)
|
||||
* @retval Result of the operation: USBD_OK if all operations are OK else
|
||||
* USBD_FAIL
|
||||
*/
|
||||
static int8_t CDC_Control_FS(uint8_t cmd, uint8_t *pbuf, uint16_t length)
|
||||
{
|
||||
switch (cmd) {
|
||||
case CDC_SEND_ENCAPSULATED_COMMAND:
|
||||
|
||||
break;
|
||||
|
||||
case CDC_GET_ENCAPSULATED_RESPONSE:
|
||||
|
||||
break;
|
||||
|
||||
case CDC_SET_COMM_FEATURE:
|
||||
|
||||
break;
|
||||
|
||||
case CDC_GET_COMM_FEATURE:
|
||||
|
||||
break;
|
||||
|
||||
case CDC_CLEAR_COMM_FEATURE:
|
||||
|
||||
break;
|
||||
|
||||
/*******************************************************************************/
|
||||
/* Line Coding Structure */
|
||||
/*-----------------------------------------------------------------------------*/
|
||||
/* Offset | Field | Size | Value | Description */
|
||||
/* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per
|
||||
* second*/
|
||||
/* 4 | bCharFormat | 1 | Number | Stop bits */
|
||||
/* 0 - 1 Stop bit */
|
||||
/* 1 - 1.5 Stop bits */
|
||||
/* 2 - 2 Stop bits */
|
||||
/* 5 | bParityType | 1 | Number | Parity */
|
||||
/* 0 - None */
|
||||
/* 1 - Odd */
|
||||
/* 2 - Even */
|
||||
/* 3 - Mark */
|
||||
/* 4 - Space */
|
||||
/* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */
|
||||
/*******************************************************************************/
|
||||
case CDC_SET_LINE_CODING:
|
||||
|
||||
break;
|
||||
|
||||
case CDC_GET_LINE_CODING:
|
||||
|
||||
break;
|
||||
|
||||
case CDC_SET_CONTROL_LINE_STATE:
|
||||
|
||||
break;
|
||||
|
||||
case CDC_SEND_BREAK:
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return (USBD_OK);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Data received over USB OUT endpoint are sent over CDC interface
|
||||
* through this function.
|
||||
*
|
||||
* @note
|
||||
* This function will issue a NAK packet on any OUT packet received on
|
||||
* USB endpoint until exiting this function. If you exit this function
|
||||
* before transfer is complete on CDC interface (ie. using DMA
|
||||
* controller) it will result in receiving more data while previous ones are
|
||||
* still not sent.
|
||||
*
|
||||
* @param Buf: Buffer of data to be received
|
||||
* @param Len: Number of data received (in bytes)
|
||||
* @retval Result of the operation: USBD_OK if all operations are OK else
|
||||
* USBD_FAIL
|
||||
*/
|
||||
static int8_t CDC_Receive_FS(uint8_t *Buf, uint32_t *Len)
|
||||
{
|
||||
USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
|
||||
USBD_CDC_ReceivePacket(&hUsbDeviceFS);
|
||||
// Receive Timer Refresh
|
||||
// TIMER_SetUs(XC_TIMER0, CDC_RECV_TIMEOUT_MS);
|
||||
// TIMER_Start_IT(XC_TIMER0);
|
||||
// DEBUG("len %d\n", *Len);
|
||||
//// Delay_Ms(10);
|
||||
// if(*Len != 0)
|
||||
// {
|
||||
// if(UserRxBufferLen < (APP_TX_DATA_SIZE/2))
|
||||
// {
|
||||
// ring_buffer_queue_arr(&CDC_Rx, Buf, *Len);
|
||||
// UserRxBufferLen += *Len;
|
||||
// }
|
||||
// }
|
||||
// DEBUG("u_len %d\n", UserRxBufferLen);
|
||||
CDC_Transmit_FS(UserRxBufferFS, *Len, CDC_IN_EP);
|
||||
|
||||
return (USBD_OK);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CDC_Transmit_FS
|
||||
* Data to send over USB IN endpoint are sent over CDC interface
|
||||
* through this function.
|
||||
* @note
|
||||
*
|
||||
*
|
||||
* @param Buf: Buffer of data to be sent
|
||||
* @param Len: Number of data to be sent (in bytes)
|
||||
* @retval USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY
|
||||
*/
|
||||
uint8_t CDC_Transmit_FS(uint8_t *Buf, uint16_t Len, uint8_t epnum)
|
||||
{
|
||||
uint8_t result = USBD_OK;
|
||||
|
||||
// DEBUG("cdc_epnum: %d\n", epnum);
|
||||
USBD_CDC_HandleTypeDef *hcdc =
|
||||
(USBD_CDC_HandleTypeDef *)hUsbDeviceFS.pClassData;
|
||||
if (hcdc->TxState != 0) {
|
||||
// DEBUG("bsy: %d\n", hcdc->TxState);
|
||||
return USBD_BUSY;
|
||||
}
|
||||
// DEBUG("tx_len %d\r", Len);
|
||||
USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, Len);
|
||||
result = USBD_CDC_TransmitPacket(&hUsbDeviceFS, epnum);
|
||||
|
||||
// uint8_t packetSendCnt = Len / 64;
|
||||
// uint8_t packetSendRem = Len % 64;
|
||||
//
|
||||
// for(uint8_t i=0; i<packetSendCnt; i++)
|
||||
// {
|
||||
// USBD_CDC_SetTxBuffer(&hUsbDeviceFS, &Buf[i * 64], 64);
|
||||
// result = USBD_CDC_TransmitPacket(&hUsbDeviceFS, epnum);
|
||||
// while(hcdc->TxState != 0);
|
||||
// USBD_CDC_SetTxBuffer(&hUsbDeviceFS, NULL, 0);
|
||||
// result = USBD_CDC_TransmitPacket(&hUsbDeviceFS, epnum);
|
||||
// while(hcdc->TxState != 0);
|
||||
// }
|
||||
//
|
||||
// if(packetSendRem != 0)
|
||||
// {
|
||||
// DEBUG("SendRem\r");
|
||||
// USBD_CDC_SetTxBuffer(&hUsbDeviceFS, &Buf[Len - packetSendRem],
|
||||
// packetSendRem); result = USBD_CDC_TransmitPacket(&hUsbDeviceFS,
|
||||
// epnum);
|
||||
// }
|
||||
// while(hcdc->TxState != 0);
|
||||
// memset(TempBuffer, 0, sizeof(TempBuffer));
|
||||
// UserRxBufferLen = 0;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CDC_TransmitCplt_FS
|
||||
* Data transmitted callback
|
||||
*
|
||||
* @note
|
||||
* This function is IN transfer complete callback used to inform user
|
||||
* that the submitted Data is successfully sent over USB.
|
||||
*
|
||||
* @param Buf: Buffer of data to be received
|
||||
* @param Len: Number of data received (in bytes)
|
||||
* @retval Result of the operation: USBD_OK if all operations are OK else
|
||||
* USBD_FAIL
|
||||
*/
|
||||
static int8_t CDC_TransmitCplt_FS(uint8_t *Buf, uint32_t *Len, uint8_t epnum)
|
||||
{
|
||||
uint8_t result = USBD_OK;
|
||||
// static uint8_t offset = 0;
|
||||
|
||||
UNUSED(Buf);
|
||||
UNUSED(Len);
|
||||
UNUSED(epnum);
|
||||
|
||||
// if(UserReTx == true)
|
||||
// {
|
||||
// UserRxBufferLen -= CDC_DATA_FS_MAX_PACKET_SIZE;
|
||||
// offset++;
|
||||
// if(UserRxBufferLen > CDC_DATA_FS_MAX_PACKET_SIZE)
|
||||
// CDC_Transmit_FS(TempBuffer+offset*CDC_DATA_FS_MAX_PACKET_SIZE,
|
||||
// CDC_DATA_FS_MAX_PACKET_SIZE, CDC_IN_EP);
|
||||
// else
|
||||
// {
|
||||
|
||||
// UserReTx = false;
|
||||
// CDC_Transmit_FS(TempBuffer+offset*CDC_DATA_FS_MAX_PACKET_SIZE,
|
||||
// UserRxBufferLen, CDC_IN_EP);
|
||||
// }
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// if(UserRxBufferLen <= CDC_DATA_FS_MAX_PACKET_SIZE)
|
||||
// {
|
||||
// offset = 0;
|
||||
// memset(TempBuffer, 0, sizeof(TempBuffer));
|
||||
// UserRxBufferLen = 0;
|
||||
// }
|
||||
// }
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/*!
|
||||
* \file usbd_cdc_if.h
|
||||
*
|
||||
* \brief Header for usbd_cdc_if.c file.
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __USBD_CDC_IF_H
|
||||
#define __USBD_CDC_IF_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usbd_cdc.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------——----------------------------------------*/
|
||||
/** @defgroup USBD_CDC_IF_Exported_Defines USBD_CDC_IF_Exported_Defines
|
||||
* @brief Defines.
|
||||
* @{
|
||||
*/
|
||||
/* Define size for the receive and transmit buffer over CDC */
|
||||
#define APP_RX_DATA_SIZE 1024
|
||||
#define APP_TX_DATA_SIZE 1024
|
||||
|
||||
#define CDC_RECV_TIMEOUT_MS 5000U
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** CDC Interface callback. */
|
||||
extern USBD_CDC_ItfTypeDef USBD_Interface_fops_FS;
|
||||
extern uint8_t Next_ep;
|
||||
//extern tHandler_callback CDC_Recv_Timer_Cbk[4];
|
||||
extern uint16_t UserRxBufferLen;
|
||||
extern uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];
|
||||
extern uint8_t UserTxEnable;
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len, uint8_t epnum);
|
||||
|
||||
void CDC_Receive_Timeout(uint16_t val);
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __USBD_CDC_IF_H */
|
||||
|
||||
|
||||
@@ -0,0 +1,348 @@
|
||||
/*!
|
||||
* \file usbd_desc.c
|
||||
*
|
||||
* \brief Target the USB device descriptors implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usbd_desc.h"
|
||||
#include "usbd_conf.h"
|
||||
#include "usbd_core.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------
|
||||
-----------------------------------------*/
|
||||
/** @defgroup USBD_DESC_Private_Defines USBD_DESC_Private_Defines
|
||||
* @brief Private defines.
|
||||
* @{
|
||||
*/
|
||||
#define USBD_VID 0x0483
|
||||
#define USBD_LANGID_STRING 0x0409
|
||||
#define USBD_MANUFACTURER_STRING "XinChip"
|
||||
#define USBD_PID_FS 22336
|
||||
#define USBD_PRODUCT_STRING_FS "XinChip Virtual ComPort"
|
||||
#define USBD_CONFIGURATION_STRING_FS "CDC Config"
|
||||
#define USBD_INTERFACE_STRING_FS "CDC Interface"
|
||||
|
||||
#define USB_SIZ_BOS_DESC 0x0C
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Func Prototypes
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @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 for FS.
|
||||
* @{
|
||||
*/
|
||||
|
||||
uint8_t *USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
|
||||
uint8_t *USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
|
||||
uint8_t *USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed,
|
||||
uint16_t *length);
|
||||
uint8_t *USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed,
|
||||
uint16_t *length);
|
||||
uint8_t *USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
|
||||
uint8_t *USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
|
||||
uint8_t *USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed,
|
||||
uint16_t *length);
|
||||
#if (USBD_LPM_ENABLED == 1)
|
||||
uint8_t *USBD_FS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
|
||||
#endif /* (USBD_LPM_ENABLED == 1) */
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Local Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @defgroup USBD_DESC_Private_Variables USBD_DESC_Private_Variables
|
||||
* @brief Private variables.
|
||||
* @{
|
||||
*/
|
||||
|
||||
USBD_DescriptorsTypeDef FS_Desc = {USBD_FS_DeviceDescriptor,
|
||||
USBD_FS_LangIDStrDescriptor,
|
||||
USBD_FS_ManufacturerStrDescriptor,
|
||||
USBD_FS_ProductStrDescriptor,
|
||||
USBD_FS_SerialStrDescriptor,
|
||||
USBD_FS_ConfigStrDescriptor,
|
||||
USBD_FS_InterfaceStrDescriptor
|
||||
#if (USBD_LPM_ENABLED == 1)
|
||||
,
|
||||
USBD_FS_USR_BOSDescriptor
|
||||
#endif /* (USBD_LPM_ENABLED == 1) */
|
||||
};
|
||||
|
||||
#if defined(__ICCARM__) /* IAR Compiler */
|
||||
#pragma data_alignment = 4
|
||||
#endif /* defined ( __ICCARM__ ) */
|
||||
/** USB standard device descriptor. */
|
||||
__ALIGN_BEGIN uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = {
|
||||
0x12, /*bLength */
|
||||
USB_DESC_TYPE_DEVICE, /*bDescriptorType*/
|
||||
#if (USBD_LPM_ENABLED == 1)
|
||||
0x01,
|
||||
/*bcdUSB */ /* changed to USB version 2.01
|
||||
in order to support LPM L1 suspend
|
||||
resume test of USBCV3.0*/
|
||||
#else
|
||||
0x00, /*bcdUSB */
|
||||
#endif /* (USBD_LPM_ENABLED == 1) */
|
||||
0x02,
|
||||
DEV_CLASS, /*bDeviceClass*/
|
||||
DEV_SUB_CLASS, /*bDeviceSubClass*/
|
||||
DEV_PROTOCOL, /*bDeviceProtocol*/
|
||||
USB_MAX_EP0_SIZE, /*bMaxPacketSize*/
|
||||
LOBYTE(USBD_VID), /*idVendor*/
|
||||
HIBYTE(USBD_VID), /*idVendor*/
|
||||
LOBYTE(USBD_PID_FS), /*idProduct*/
|
||||
HIBYTE(USBD_PID_FS), /*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 */
|
||||
/** BOS descriptor. */
|
||||
#if (USBD_LPM_ENABLED == 1)
|
||||
#if defined(__ICCARM__) /* IAR Compiler */
|
||||
#pragma data_alignment = 4
|
||||
#endif /* defined ( __ICCARM__ ) */
|
||||
__ALIGN_BEGIN uint8_t USBD_FS_BOSDesc[USB_SIZ_BOS_DESC] __ALIGN_END = {
|
||||
0x5, USB_DESC_TYPE_BOS, 0xC, 0x0, 0x1, /* 1 device capability*/
|
||||
/* device capability*/
|
||||
0x7, USB_DEVICE_CAPABITY_TYPE, 0x2, 0x2, /* LPM capability bit set*/
|
||||
0x0, 0x0, 0x0};
|
||||
#endif /* (USBD_LPM_ENABLED == 1) */
|
||||
|
||||
/** @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,
|
||||
};
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
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_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
{
|
||||
UNUSED(speed);
|
||||
*length = sizeof(USBD_FS_DeviceDesc);
|
||||
return USBD_FS_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_FS_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_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
{
|
||||
if (speed == 0) {
|
||||
USBD_GetString((uint8_t *)USBD_PRODUCT_STRING_FS, USBD_StrDesc, length);
|
||||
} else {
|
||||
USBD_GetString((uint8_t *)USBD_PRODUCT_STRING_FS, 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_FS_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_FS_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_FS_SerialStrDescriptor */
|
||||
|
||||
/* USER CODE END USBD_FS_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_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
{
|
||||
USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING_FS, 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_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed,
|
||||
uint16_t *length)
|
||||
{
|
||||
if (speed == 0) {
|
||||
USBD_GetString((uint8_t *)USBD_INTERFACE_STRING_FS, USBD_StrDesc,
|
||||
length);
|
||||
} else {
|
||||
USBD_GetString((uint8_t *)USBD_INTERFACE_STRING_FS, USBD_StrDesc,
|
||||
length);
|
||||
}
|
||||
return USBD_StrDesc;
|
||||
}
|
||||
|
||||
#if (USBD_LPM_ENABLED == 1)
|
||||
/**
|
||||
* @brief Return the BOS descriptor
|
||||
* @param speed : Current device speed
|
||||
* @param length : Pointer to data length variable
|
||||
* @retval Pointer to descriptor buffer
|
||||
*/
|
||||
uint8_t *USBD_FS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
{
|
||||
UNUSED(speed);
|
||||
*length = sizeof(USBD_FS_BOSDesc);
|
||||
return (uint8_t *)USBD_FS_BOSDesc;
|
||||
}
|
||||
#endif /* (USBD_LPM_ENABLED == 1) */
|
||||
|
||||
/**
|
||||
* @brief Create the serial number string descriptor
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void Get_SerialNum(void)
|
||||
{
|
||||
uint32_t deviceserial0, deviceserial1, deviceserial2;
|
||||
|
||||
deviceserial0 = DEVICE_ID1;
|
||||
deviceserial1 = DEVICE_ID2;
|
||||
deviceserial2 = 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;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
@@ -0,0 +1,76 @@
|
||||
/*!
|
||||
* \file usbd_desc.h
|
||||
*
|
||||
* \brief Target the USB device descriptors implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __USBD_DESC__C
|
||||
#define __USBD_DESC__C
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usbd_def.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @defgroup USBD_DESC_Exported_Constants USBD_DESC_Exported_Constants
|
||||
* @brief Constants.
|
||||
* @{
|
||||
*/
|
||||
|
||||
#define DEV_CLASS 0x02
|
||||
#define DEV_SUB_CLASS 0x02
|
||||
#define DEV_PROTOCOL 0x00
|
||||
|
||||
#define DEVICE_ID1 0x20220000
|
||||
#define DEVICE_ID2 0x1B0000
|
||||
#define DEVICE_ID3 0x0914
|
||||
|
||||
#define USB_SIZ_STRING_SERIAL 0x1A
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** Descriptor for the Usb device. */
|
||||
extern USBD_DescriptorsTypeDef FS_Desc;
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __USBD_DESC__C */
|
||||
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
/*!
|
||||
* \file usb_device.c
|
||||
*
|
||||
* \brief Target usb device implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usb_device.h"
|
||||
#include "usbd_core.h"
|
||||
#include "usbd_desc.h"
|
||||
#include "usbd_hid.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
#if ((HID_CLASS_MODE & HID_MOUSE) == HID_MOUSE)
|
||||
#define CURSOR_STEP 2U
|
||||
#define CURSOR_WIDTH 200U
|
||||
#elif ((HID_CLASS_MODE & HID_KEYBOARD) == HID_KEYBOARD)
|
||||
#define KEY_CAPS_DATA 0x39
|
||||
#endif
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/* USB Device Core handle declaration. */
|
||||
USBD_HandleTypeDef hUsbDeviceFS;
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief This function handles USB On The Go FS global interrupt.
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void USB_Handler(void) { HAL_PCD_IRQHandler(&hpcd_USB_OTG_FS); }
|
||||
|
||||
/**
|
||||
* @brief Init USB device Library, add supported class and start the library
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void USB_DEVICE_Init(void)
|
||||
{
|
||||
/* Init Device Library, add supported class and start the library. */
|
||||
if (USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS) != USBD_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
if (USBD_RegisterClass(&hUsbDeviceFS, &USBD_HID) != USBD_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
if (USBD_Start(&hUsbDeviceFS) != USBD_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
#if ((HID_CLASS_MODE & HID_MOUSE) == HID_MOUSE)
|
||||
/**
|
||||
* @brief Mouse get pointer datas
|
||||
* @param uint8_t *pbuf
|
||||
* @retval void
|
||||
*/
|
||||
void GetPointerData(uint8_t *pbuf)
|
||||
{
|
||||
static int32_t move_cnt = 0;
|
||||
static uint8_t step_x_y = 0;
|
||||
static int8_t x = 0, y = 0;
|
||||
static uint16_t cnt = 0;
|
||||
|
||||
move_cnt++;
|
||||
if (move_cnt > CURSOR_WIDTH) {
|
||||
step_x_y++;
|
||||
step_x_y = step_x_y % 4;
|
||||
move_cnt = 0;
|
||||
}
|
||||
switch (step_x_y) {
|
||||
case 0: {
|
||||
y = 0;
|
||||
x = CURSOR_STEP;
|
||||
|
||||
} break;
|
||||
|
||||
case 1: {
|
||||
x = 0;
|
||||
y = CURSOR_STEP;
|
||||
|
||||
} break;
|
||||
|
||||
case 2: {
|
||||
|
||||
y = 0;
|
||||
x = (int8_t)(-CURSOR_STEP);
|
||||
|
||||
} break;
|
||||
|
||||
case 3: {
|
||||
x = 0;
|
||||
y = (int8_t)(-CURSOR_STEP);
|
||||
|
||||
} break;
|
||||
}
|
||||
cnt++;
|
||||
pbuf[0] = 0; // 1;
|
||||
if (cnt > 1000) {
|
||||
pbuf[0] = 16; // 17;
|
||||
cnt = 0;
|
||||
}
|
||||
|
||||
pbuf[1] = x;
|
||||
pbuf[2] = y;
|
||||
pbuf[3] = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Usb mouse test send report
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void USB_Mouse_Test_SendReport(void)
|
||||
{
|
||||
uint8_t buff[4] = {0};
|
||||
|
||||
USBD_HID_SendReport(&hUsbDeviceFS, HID_EPIN_ADDR, buff, HID_EPIN_SIZE);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if ((HID_CLASS_MODE & HID_KEYBOARD) == HID_KEYBOARD)
|
||||
/**
|
||||
* @brief Usb Keyboard test send report
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void USB_Keyboard_Test_SendReport(eKeyState key_sta)
|
||||
{
|
||||
uint8_t buff[HID_EPIN_SIZE] = {0};
|
||||
|
||||
if (key_sta == KEY_PRESS) {
|
||||
buff[2] = KEY_CAPS_DATA;
|
||||
USBD_HID_SendReport(&hUsbDeviceFS, HID_EPIN_ADDR, buff, HID_EPIN_SIZE);
|
||||
} else if (key_sta == KEY_RELEASE) {
|
||||
USBD_HID_SendReport(&hUsbDeviceFS, HID_EPIN_ADDR, buff, HID_EPIN_SIZE);
|
||||
} else if (key_sta == KEY_HOLD_PRESS) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if (HID_CLASS_MODE == HID_CUSTOM)
|
||||
/**
|
||||
* @brief Usb custom hid test send report
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void USB_Custom_Test_SendReport(void)
|
||||
{
|
||||
uint8_t usb_tx_data[64] = {0};
|
||||
// usb_tx_data[0] = 0x01;
|
||||
for (uint8_t i = 0; i < 64; i++)
|
||||
usb_tx_data[i] = i;
|
||||
|
||||
USBD_HID_SendReport(&hUsbDeviceFS, HID_EPIN_ADDR, usb_tx_data, 64);
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,66 @@
|
||||
/*!
|
||||
* \file usb_device.h
|
||||
*
|
||||
* \brief Target usb device implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __USB_DEVICE_H__
|
||||
#define __USB_DEVICE_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usbd_def.h"
|
||||
|
||||
|
||||
extern USBD_HandleTypeDef hUsbDeviceFS;
|
||||
|
||||
typedef enum {
|
||||
KEY_RELEASE = 0,
|
||||
KEY_PRESS,
|
||||
KEY_HOLD_PRESS
|
||||
} eKeyState;
|
||||
|
||||
|
||||
extern USBD_HandleTypeDef hUsbDeviceFS;
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void USB_DEVICE_Init( void );
|
||||
|
||||
void USB_Mouse_Test_SendReport( void );
|
||||
void USB_Keyboard_Test_SendReport(eKeyState key_sta);
|
||||
void USB_Custom_Test_SendReport( void );
|
||||
|
||||
void GetPointerData(uint8_t *pbuf);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __USB_DEVICE_H__ */
|
||||
|
||||
@@ -0,0 +1,352 @@
|
||||
/*!
|
||||
* \file usbd_desc.c
|
||||
*
|
||||
* \brief Target the USB device descriptors implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usbd_desc.h"
|
||||
#include "usbd_conf.h"
|
||||
#include "usbd_core.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------
|
||||
-----------------------------------------*/
|
||||
/** @defgroup USBD_DESC_Private_Defines USBD_DESC_Private_Defines
|
||||
* @brief Private defines.
|
||||
* @{
|
||||
*/
|
||||
#define USBD_VID VID_VAL
|
||||
#define USBD_LANGID_STRING LANG_ID_STR
|
||||
#define USBD_MANUFACTURER_STRING "XinChip"
|
||||
#define USBD_PID_FS PID_FS_VAL
|
||||
#define USBD_PRODUCT_STRING_FS \
|
||||
"XinChip Custom Human interface" //"XinChip Human interface"
|
||||
#define USBD_CONFIGURATION_STRING_FS "Custom HID Config" //"HID Config"
|
||||
#define USBD_INTERFACE_STRING_FS "Custom HID Interface" //"HID Interface"
|
||||
|
||||
#define USB_SIZ_BOS_DESC 0x0C
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Func Prototypes
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @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 for FS.
|
||||
* @{
|
||||
*/
|
||||
|
||||
uint8_t *USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
|
||||
uint8_t *USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
|
||||
uint8_t *USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed,
|
||||
uint16_t *length);
|
||||
uint8_t *USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed,
|
||||
uint16_t *length);
|
||||
uint8_t *USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
|
||||
uint8_t *USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
|
||||
uint8_t *USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed,
|
||||
uint16_t *length);
|
||||
#if (USBD_LPM_ENABLED == 1)
|
||||
uint8_t *USBD_FS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
|
||||
#endif /* (USBD_LPM_ENABLED == 1) */
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Local Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @defgroup USBD_DESC_Private_Variables USBD_DESC_Private_Variables
|
||||
* @brief Private variables.
|
||||
* @{
|
||||
*/
|
||||
|
||||
USBD_DescriptorsTypeDef FS_Desc = {USBD_FS_DeviceDescriptor,
|
||||
USBD_FS_LangIDStrDescriptor,
|
||||
USBD_FS_ManufacturerStrDescriptor,
|
||||
USBD_FS_ProductStrDescriptor,
|
||||
USBD_FS_SerialStrDescriptor,
|
||||
USBD_FS_ConfigStrDescriptor,
|
||||
USBD_FS_InterfaceStrDescriptor
|
||||
#if (USBD_LPM_ENABLED == 1)
|
||||
,
|
||||
USBD_FS_USR_BOSDescriptor
|
||||
#endif /* (USBD_LPM_ENABLED == 1) */
|
||||
};
|
||||
|
||||
#if defined(__ICCARM__) /* IAR Compiler */
|
||||
#pragma data_alignment = 4
|
||||
#endif /* defined ( __ICCARM__ ) */
|
||||
/** USB standard device descriptor. */
|
||||
__ALIGN_BEGIN uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = {
|
||||
0x12, /*bLength */
|
||||
USB_DESC_TYPE_DEVICE, /*bDescriptorType*/
|
||||
#if (USBD_LPM_ENABLED == 1)
|
||||
0x01,
|
||||
/*bcdUSB */ /* changed to USB version 2.01
|
||||
in order to support LPM L1 suspend
|
||||
resume test of USBCV3.0*/
|
||||
#else
|
||||
0x00, /*bcdUSB */
|
||||
#endif /* (USBD_LPM_ENABLED == 1) */
|
||||
0x02,
|
||||
DEV_CLASS, /*bDeviceClass*/
|
||||
DEV_SUB_CLASS, /*bDeviceSubClass*/
|
||||
DEV_PROTOCOL, /*bDeviceProtocol*/
|
||||
USB_MAX_EP0_SIZE, /*bMaxPacketSize*/
|
||||
LOBYTE(USBD_VID), /*idVendor*/
|
||||
HIBYTE(USBD_VID), /*idVendor*/
|
||||
LOBYTE(USBD_PID_FS), /*idProduct*/
|
||||
HIBYTE(USBD_PID_FS), /*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 */
|
||||
/** BOS descriptor. */
|
||||
#if (USBD_LPM_ENABLED == 1)
|
||||
#if defined(__ICCARM__) /* IAR Compiler */
|
||||
#pragma data_alignment = 4
|
||||
#endif /* defined ( __ICCARM__ ) */
|
||||
__ALIGN_BEGIN uint8_t USBD_FS_BOSDesc[USB_SIZ_BOS_DESC] __ALIGN_END = {
|
||||
0x5, USB_DESC_TYPE_BOS, 0xC, 0x0, 0x1, /* 1 device capability*/
|
||||
/* device capability*/
|
||||
0x7, USB_DEVICE_CAPABITY_TYPE, 0x2, 0x2, /* LPM capability bit set*/
|
||||
0x0, 0x0, 0x0};
|
||||
#endif /* (USBD_LPM_ENABLED == 1) */
|
||||
|
||||
/** @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,
|
||||
};
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
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_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
{
|
||||
*length = sizeof(USBD_FS_DeviceDesc);
|
||||
return USBD_FS_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_FS_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_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
{
|
||||
if (speed == 0) {
|
||||
USBD_GetString((uint8_t *)USBD_PRODUCT_STRING_FS, USBD_StrDesc, length);
|
||||
} else {
|
||||
USBD_GetString((uint8_t *)USBD_PRODUCT_STRING_FS, 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_FS_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_FS_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_FS_SerialStrDescriptor */
|
||||
|
||||
/* USER CODE END USBD_FS_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_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
{
|
||||
if (speed == USBD_SPEED_HIGH) {
|
||||
USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING_FS, USBD_StrDesc,
|
||||
length);
|
||||
} else {
|
||||
USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING_FS, 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_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed,
|
||||
uint16_t *length)
|
||||
{
|
||||
if (speed == 0) {
|
||||
USBD_GetString((uint8_t *)USBD_INTERFACE_STRING_FS, USBD_StrDesc,
|
||||
length);
|
||||
} else {
|
||||
USBD_GetString((uint8_t *)USBD_INTERFACE_STRING_FS, USBD_StrDesc,
|
||||
length);
|
||||
}
|
||||
return USBD_StrDesc;
|
||||
}
|
||||
|
||||
#if (USBD_LPM_ENABLED == 1)
|
||||
/**
|
||||
* @brief Return the BOS descriptor
|
||||
* @param speed : Current device speed
|
||||
* @param length : Pointer to data length variable
|
||||
* @retval Pointer to descriptor buffer
|
||||
*/
|
||||
uint8_t *USBD_FS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
{
|
||||
UNUSED(speed);
|
||||
*length = sizeof(USBD_FS_BOSDesc);
|
||||
return (uint8_t *)USBD_FS_BOSDesc;
|
||||
}
|
||||
#endif /* (USBD_LPM_ENABLED == 1) */
|
||||
|
||||
/**
|
||||
* @brief Create the serial number string descriptor
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void Get_SerialNum(void)
|
||||
{
|
||||
uint32_t deviceserial0, deviceserial1, deviceserial2;
|
||||
|
||||
deviceserial0 = DEVICE_ID1;
|
||||
deviceserial1 = DEVICE_ID2;
|
||||
deviceserial2 = 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;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
@@ -0,0 +1,120 @@
|
||||
/*!
|
||||
* \file usbd_desc.h
|
||||
*
|
||||
* \brief Target the USB device descriptors implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __USBD_DESC__C__
|
||||
#define __USBD_DESC__C__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usbd_def.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @defgroup USBD_DESC_Exported_Constants USBD_DESC_Exported_Constants
|
||||
* @brief Constants.
|
||||
* @{
|
||||
*/
|
||||
#if (HID_CLASS_MODE == HID_MOUSE)
|
||||
#define VID_VAL 1155
|
||||
#define LANG_ID_STR 1033
|
||||
#define PID_FS_VAL 17799
|
||||
|
||||
#define DEVICE_ID1 0x20220000
|
||||
#define DEVICE_ID2 0x1D0000
|
||||
#define DEVICE_ID3 0x1122
|
||||
#elif (HID_CLASS_MODE == HID_KEYBOARD)
|
||||
#define VID_VAL 1155
|
||||
#define LANG_ID_STR 1033
|
||||
#define PID_FS_VAL 17780
|
||||
|
||||
#define DEVICE_ID1 0x20220000
|
||||
#define DEVICE_ID2 0x1F0000
|
||||
#define DEVICE_ID3 0x1123
|
||||
#elif (HID_CLASS_MODE == HID_CUSTOM)
|
||||
#define VID_VAL 1155
|
||||
#define LANG_ID_STR 1033
|
||||
#define PID_FS_VAL 17781
|
||||
|
||||
#define DEVICE_ID1 0x20220000
|
||||
#define DEVICE_ID2 0x1F0000
|
||||
#define DEVICE_ID3 0x1124
|
||||
#elif (HID_CLASS_MODE == (HID_MOUSE | HID_CUSTOM))
|
||||
#define VID_VAL 1156
|
||||
#define LANG_ID_STR 1033
|
||||
#define PID_FS_VAL 22353
|
||||
|
||||
#define DEVICE_ID1 0x20220000
|
||||
#define DEVICE_ID2 0x1E0000
|
||||
#define DEVICE_ID3 0x1123
|
||||
#elif (HID_CLASS_MODE == (HID_KEYBOARD | HID_CUSTOM))
|
||||
#define VID_VAL 1156
|
||||
#define LANG_ID_STR 1033
|
||||
#define PID_FS_VAL 22354
|
||||
|
||||
#define DEVICE_ID1 0x20220000
|
||||
#define DEVICE_ID2 0x200000
|
||||
#define DEVICE_ID3 0x1215
|
||||
#elif (HID_CLASS_MODE == (HID_KEYBOARD | HID_MOUSE))
|
||||
#define VID_VAL 1157
|
||||
#define LANG_ID_STR 1033
|
||||
#define PID_FS_VAL 22355
|
||||
|
||||
#define DEVICE_ID1 0x20230000
|
||||
#define DEVICE_ID2 0x210000
|
||||
#define DEVICE_ID3 0x0228
|
||||
#endif
|
||||
|
||||
#define DEV_CLASS 0x00
|
||||
#define DEV_SUB_CLASS 0x00
|
||||
#define DEV_PROTOCOL 0x00
|
||||
|
||||
#define USB_SIZ_STRING_SERIAL 0x1A
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** Descriptor for the Usb device. */
|
||||
extern USBD_DescriptorsTypeDef FS_Desc;
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __USBD_DESC__C__ */
|
||||
|
||||
@@ -0,0 +1,194 @@
|
||||
/*!
|
||||
* \file usb_device.c
|
||||
*
|
||||
* \brief Target usb device implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usb_device.h"
|
||||
#include "usbd_core.h"
|
||||
#include "usbd_desc.h"
|
||||
#include "usbd_hid.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
#define CURSOR_STEP 2U
|
||||
#define CURSOR_WIDTH 200U
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/* USB Device Core handle declaration. */
|
||||
USBD_HandleTypeDef hUsbDeviceFS;
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief This function handles USB On The Go FS global interrupt.
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void USB_Handler(void) { HAL_PCD_IRQHandler(&hpcd_USB_OTG_FS); }
|
||||
|
||||
/**
|
||||
* @brief Init USB device Library, add supported class and start the library
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void USB_DEVICE_Init(void)
|
||||
{
|
||||
/* Init Device Library, add supported class and start the library. */
|
||||
if (USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS) != USBD_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
if (USBD_RegisterClass(&hUsbDeviceFS, &USBD_HID) != USBD_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
if (USBD_Start(&hUsbDeviceFS) != USBD_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
#if ((HID_CLASS_MODE & HID_MOUSE) == HID_MOUSE)
|
||||
/**
|
||||
* @brief Mouse get pointer datas
|
||||
* @param uint8_t *pbuf
|
||||
* @retval void
|
||||
*/
|
||||
void GetPointerData(uint8_t *pbuf)
|
||||
{
|
||||
static int32_t move_cnt = 0;
|
||||
static uint8_t step_x_y = 0;
|
||||
static int8_t x = 0, y = 0;
|
||||
static uint16_t cnt = 0;
|
||||
|
||||
move_cnt++;
|
||||
if (move_cnt > CURSOR_WIDTH) {
|
||||
step_x_y++;
|
||||
step_x_y = step_x_y % 4;
|
||||
move_cnt = 0;
|
||||
}
|
||||
switch (step_x_y) {
|
||||
case 0: {
|
||||
y = 0;
|
||||
x = CURSOR_STEP;
|
||||
|
||||
} break;
|
||||
|
||||
case 1: {
|
||||
x = 0;
|
||||
y = CURSOR_STEP;
|
||||
|
||||
} break;
|
||||
|
||||
case 2: {
|
||||
|
||||
y = 0;
|
||||
x = (int8_t)(-CURSOR_STEP);
|
||||
|
||||
} break;
|
||||
|
||||
case 3: {
|
||||
x = 0;
|
||||
y = (int8_t)(-CURSOR_STEP);
|
||||
|
||||
} break;
|
||||
}
|
||||
cnt++;
|
||||
if (cnt > 1000) {
|
||||
if (pbuf[0] != 16)
|
||||
pbuf[0] = 16;
|
||||
else
|
||||
pbuf[0] = 0;
|
||||
|
||||
cnt = 0;
|
||||
}
|
||||
|
||||
pbuf[1] = x;
|
||||
pbuf[2] = y;
|
||||
pbuf[3] = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Usb mouse test send report
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void USB_Mouse_Test_SendReport(void)
|
||||
{
|
||||
uint8_t buff[4] = {0};
|
||||
#if (HID_CLASS_MODE == (HID_KEYBOARD | HID_MOUSE))
|
||||
USBD_Mouse_HID_SendReport(&hUsbDeviceFS, buff, HID_EP2IN_SIZE);
|
||||
#elif (HID_CLASS_MODE == (HID_CUSTOM | HID_MOUSE))
|
||||
USBD_Mouse_HID_SendReport(&hUsbDeviceFS, buff, HID_EPIN_SIZE);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
#if ((HID_CLASS_MODE & HID_CUSTOM) == HID_CUSTOM)
|
||||
#define CUSTOM_SR_LEN 64U
|
||||
/**
|
||||
* @brief Usb custom hid test send report
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void USB_Custom_Test_SendReport(void)
|
||||
{
|
||||
uint8_t usb_tx_data[CUSTOM_SR_LEN] = {0};
|
||||
for (uint8_t i = 0; i < CUSTOM_SR_LEN; i++)
|
||||
usb_tx_data[i] = i;
|
||||
USBD_Custom_HID_SendReport(&hUsbDeviceFS, usb_tx_data, CUSTOM_SR_LEN);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if (HID_CLASS_MODE == HID_DOUBLE_CUSTOM)
|
||||
/**
|
||||
* @brief Usb custom1 hid test send report
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void USB_Custom1_Test_SendReport(void)
|
||||
{
|
||||
uint8_t usb_tx_data[64] = {0};
|
||||
usb_tx_data[0] = 0x81;
|
||||
for (uint8_t i = 1; i < 64; i++)
|
||||
usb_tx_data[i] = i;
|
||||
USBD_Custom1_HID_SendReport(&hUsbDeviceFS, usb_tx_data, 64);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Usb custom2 hid test send report
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void USB_Custom2_Test_SendReport(void)
|
||||
{
|
||||
uint8_t usb_tx_data[64] = {0};
|
||||
usb_tx_data[0] = 0x82;
|
||||
for (uint8_t i = 1; i < 64; i++)
|
||||
usb_tx_data[i] = i;
|
||||
USBD_Custom2_HID_SendReport(&hUsbDeviceFS, usb_tx_data, 64);
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,56 @@
|
||||
/*!
|
||||
* \file usb_device.h
|
||||
*
|
||||
* \brief Target usb device implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __USB_DEVICE_H__
|
||||
#define __USB_DEVICE_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usbd_def.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void USB_DEVICE_Init( void );
|
||||
uint8_t USB_Dev_State_Judge(uint8_t * rtc_cnt);
|
||||
void USB_Mouse_Test_SendReport( void );
|
||||
void USB_Custom_Test_SendReport( void );
|
||||
|
||||
#if (HID_CLASS_MODE == HID_DOUBLE_CUSTOM)
|
||||
void USB_Custom1_Test_SendReport( void );
|
||||
void USB_Custom2_Test_SendReport( void );
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __USB_DEVICE_H__ */
|
||||
|
||||
@@ -0,0 +1,351 @@
|
||||
/*!
|
||||
* \file usbd_desc.c
|
||||
*
|
||||
* \brief Target the USB device descriptors implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usbd_desc.h"
|
||||
#include "usbd_conf.h"
|
||||
#include "usbd_core.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------
|
||||
-----------------------------------------*/
|
||||
/** @defgroup USBD_DESC_Private_Defines USBD_DESC_Private_Defines
|
||||
* @brief Private defines.
|
||||
* @{
|
||||
*/
|
||||
#define USBD_VID VID_VAL
|
||||
#define USBD_LANGID_STRING LANG_ID_STR
|
||||
#define USBD_MANUFACTURER_STRING "XinChip"
|
||||
#define USBD_PID_FS PID_FS_VAL
|
||||
#define USBD_PRODUCT_STRING_FS "XinChip Human interface"
|
||||
#define USBD_CONFIGURATION_STRING_FS "HID Config"
|
||||
#define USBD_INTERFACE_STRING_FS "HID Interface"
|
||||
|
||||
#define USB_SIZ_BOS_DESC 0x0C
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Func Prototypes
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @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 for FS.
|
||||
* @{
|
||||
*/
|
||||
|
||||
uint8_t *USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
|
||||
uint8_t *USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
|
||||
uint8_t *USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed,
|
||||
uint16_t *length);
|
||||
uint8_t *USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed,
|
||||
uint16_t *length);
|
||||
uint8_t *USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
|
||||
uint8_t *USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
|
||||
uint8_t *USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed,
|
||||
uint16_t *length);
|
||||
#if (USBD_LPM_ENABLED == 1)
|
||||
uint8_t *USBD_FS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
|
||||
#endif /* (USBD_LPM_ENABLED == 1) */
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Local Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @defgroup USBD_DESC_Private_Variables USBD_DESC_Private_Variables
|
||||
* @brief Private variables.
|
||||
* @{
|
||||
*/
|
||||
|
||||
USBD_DescriptorsTypeDef FS_Desc = {USBD_FS_DeviceDescriptor,
|
||||
USBD_FS_LangIDStrDescriptor,
|
||||
USBD_FS_ManufacturerStrDescriptor,
|
||||
USBD_FS_ProductStrDescriptor,
|
||||
USBD_FS_SerialStrDescriptor,
|
||||
USBD_FS_ConfigStrDescriptor,
|
||||
USBD_FS_InterfaceStrDescriptor
|
||||
#if (USBD_LPM_ENABLED == 1)
|
||||
,
|
||||
USBD_FS_USR_BOSDescriptor
|
||||
#endif /* (USBD_LPM_ENABLED == 1) */
|
||||
};
|
||||
|
||||
#if defined(__ICCARM__) /* IAR Compiler */
|
||||
#pragma data_alignment = 4
|
||||
#endif /* defined ( __ICCARM__ ) */
|
||||
/** USB standard device descriptor. */
|
||||
__ALIGN_BEGIN uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = {
|
||||
0x12, /*bLength */
|
||||
USB_DESC_TYPE_DEVICE, /*bDescriptorType*/
|
||||
#if (USBD_LPM_ENABLED == 1)
|
||||
0x01,
|
||||
/*bcdUSB */ /* changed to USB version 2.01
|
||||
in order to support LPM L1 suspend
|
||||
resume test of USBCV3.0*/
|
||||
#else
|
||||
0x00, /*bcdUSB */
|
||||
#endif /* (USBD_LPM_ENABLED == 1) */
|
||||
0x02,
|
||||
DEV_CLASS, /*bDeviceClass*/
|
||||
DEV_SUB_CLASS, /*bDeviceSubClass*/
|
||||
DEV_PROTOCOL, /*bDeviceProtocol*/
|
||||
USB_MAX_EP0_SIZE, /*bMaxPacketSize*/
|
||||
LOBYTE(USBD_VID), /*idVendor*/
|
||||
HIBYTE(USBD_VID), /*idVendor*/
|
||||
LOBYTE(USBD_PID_FS), /*idProduct*/
|
||||
HIBYTE(USBD_PID_FS), /*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 */
|
||||
/** BOS descriptor. */
|
||||
#if (USBD_LPM_ENABLED == 1)
|
||||
#if defined(__ICCARM__) /* IAR Compiler */
|
||||
#pragma data_alignment = 4
|
||||
#endif /* defined ( __ICCARM__ ) */
|
||||
__ALIGN_BEGIN uint8_t USBD_FS_BOSDesc[USB_SIZ_BOS_DESC] __ALIGN_END = {
|
||||
0x5, USB_DESC_TYPE_BOS, 0xC, 0x0, 0x1, /* 1 device capability*/
|
||||
/* device capability*/
|
||||
0x7, USB_DEVICE_CAPABITY_TYPE, 0x2, 0x2, /* LPM capability bit set*/
|
||||
0x0, 0x0, 0x0};
|
||||
#endif /* (USBD_LPM_ENABLED == 1) */
|
||||
|
||||
/** @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,
|
||||
};
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
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_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
{
|
||||
*length = sizeof(USBD_FS_DeviceDesc);
|
||||
return USBD_FS_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_FS_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_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
{
|
||||
if (speed == 0) {
|
||||
USBD_GetString((uint8_t *)USBD_PRODUCT_STRING_FS, USBD_StrDesc, length);
|
||||
} else {
|
||||
USBD_GetString((uint8_t *)USBD_PRODUCT_STRING_FS, 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_FS_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_FS_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_FS_SerialStrDescriptor */
|
||||
|
||||
/* USER CODE END USBD_FS_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_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
{
|
||||
if (speed == USBD_SPEED_HIGH) {
|
||||
USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING_FS, USBD_StrDesc,
|
||||
length);
|
||||
} else {
|
||||
USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING_FS, 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_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed,
|
||||
uint16_t *length)
|
||||
{
|
||||
if (speed == 0) {
|
||||
USBD_GetString((uint8_t *)USBD_INTERFACE_STRING_FS, USBD_StrDesc,
|
||||
length);
|
||||
} else {
|
||||
USBD_GetString((uint8_t *)USBD_INTERFACE_STRING_FS, USBD_StrDesc,
|
||||
length);
|
||||
}
|
||||
return USBD_StrDesc;
|
||||
}
|
||||
|
||||
#if (USBD_LPM_ENABLED == 1)
|
||||
/**
|
||||
* @brief Return the BOS descriptor
|
||||
* @param speed : Current device speed
|
||||
* @param length : Pointer to data length variable
|
||||
* @retval Pointer to descriptor buffer
|
||||
*/
|
||||
uint8_t *USBD_FS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
{
|
||||
UNUSED(speed);
|
||||
*length = sizeof(USBD_FS_BOSDesc);
|
||||
return (uint8_t *)USBD_FS_BOSDesc;
|
||||
}
|
||||
#endif /* (USBD_LPM_ENABLED == 1) */
|
||||
|
||||
/**
|
||||
* @brief Create the serial number string descriptor
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void Get_SerialNum(void)
|
||||
{
|
||||
uint32_t deviceserial0, deviceserial1, deviceserial2;
|
||||
|
||||
deviceserial0 = DEVICE_ID1;
|
||||
deviceserial1 = DEVICE_ID2;
|
||||
deviceserial2 = 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;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
@@ -0,0 +1,128 @@
|
||||
/*!
|
||||
* \file usbd_desc.h
|
||||
*
|
||||
* \brief Target the USB device descriptors implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __USBD_DESC__C__
|
||||
#define __USBD_DESC__C__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usbd_def.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @defgroup USBD_DESC_Exported_Constants USBD_DESC_Exported_Constants
|
||||
* @brief Constants.
|
||||
* @{
|
||||
*/
|
||||
#if (HID_CLASS_MODE == HID_MOUSE)
|
||||
#define VID_VAL 1155
|
||||
#define LANG_ID_STR 1033
|
||||
#define PID_FS_VAL 17799
|
||||
|
||||
#define DEVICE_ID1 0x20220000
|
||||
#define DEVICE_ID2 0x1D0000
|
||||
#define DEVICE_ID3 0x1122
|
||||
#elif (HID_CLASS_MODE == HID_KEYBOARD)
|
||||
#define VID_VAL 1155
|
||||
#define LANG_ID_STR 1033
|
||||
#define PID_FS_VAL 17780
|
||||
|
||||
#define DEVICE_ID1 0x20220000
|
||||
#define DEVICE_ID2 0x1F0000
|
||||
#define DEVICE_ID3 0x1123
|
||||
#elif (HID_CLASS_MODE == HID_CUSTOM)
|
||||
#define VID_VAL 1155
|
||||
#define LANG_ID_STR 1033
|
||||
#define PID_FS_VAL 17781
|
||||
|
||||
#define DEVICE_ID1 0x20220000
|
||||
#define DEVICE_ID2 0x1F0000
|
||||
#define DEVICE_ID3 0x1124
|
||||
#elif (HID_CLASS_MODE == (HID_MOUSE | HID_CUSTOM))
|
||||
#define VID_VAL 1156
|
||||
#define LANG_ID_STR 1033
|
||||
#define PID_FS_VAL 22353
|
||||
|
||||
#define DEVICE_ID1 0x20220000
|
||||
#define DEVICE_ID2 0x1E0000
|
||||
#define DEVICE_ID3 0x1123
|
||||
#elif (HID_CLASS_MODE == (HID_KEYBOARD | HID_CUSTOM))
|
||||
#define VID_VAL 1156
|
||||
#define LANG_ID_STR 1033
|
||||
#define PID_FS_VAL 22354
|
||||
|
||||
#define DEVICE_ID1 0x20220000
|
||||
#define DEVICE_ID2 0x200000
|
||||
#define DEVICE_ID3 0x1215
|
||||
#elif (HID_CLASS_MODE == (HID_KEYBOARD | HID_MOUSE))
|
||||
#define VID_VAL 1157
|
||||
#define LANG_ID_STR 1033
|
||||
#define PID_FS_VAL 22355
|
||||
|
||||
#define DEVICE_ID1 0x20230000
|
||||
#define DEVICE_ID2 0x210000
|
||||
#define DEVICE_ID3 0x0228
|
||||
#elif (HID_CLASS_MODE == HID_DOUBLE_CUSTOM)
|
||||
#define VID_VAL 1158
|
||||
#define LANG_ID_STR 1033
|
||||
#define PID_FS_VAL 22356
|
||||
|
||||
#define DEVICE_ID1 0x20230000
|
||||
#define DEVICE_ID2 0x210000
|
||||
#define DEVICE_ID3 0x0816
|
||||
#endif
|
||||
|
||||
#define DEV_CLASS 0x00
|
||||
#define DEV_SUB_CLASS 0x00
|
||||
#define DEV_PROTOCOL 0x00
|
||||
|
||||
#define USB_SIZ_STRING_SERIAL 0x1A
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** Descriptor for the Usb device. */
|
||||
extern USBD_DescriptorsTypeDef FS_Desc;
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __USBD_DESC__C__ */
|
||||
|
||||
@@ -0,0 +1,274 @@
|
||||
/*!
|
||||
* \file msc_flash.c
|
||||
*
|
||||
* \brief Target fatfs flash implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "msc_flash.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Local Variables
|
||||
-------------------------------------------
|
||||
-----------------------------------------*/
|
||||
uint8_t spim_init = false;
|
||||
|
||||
#define FLASH_SECTOR_ALIGN(address) \
|
||||
((uint32_t)(address) & ~(FAT_FLASH_SECTOR_SIZE - 1))
|
||||
#define FLASH_SECTOR_OFFSET(address) \
|
||||
((uint32_t)(address) & (FAT_FLASH_SECTOR_SIZE - 1))
|
||||
|
||||
static uint8_t dataBuffer[FAT_FLASH_SECTOR_SIZE] = {0};
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------
|
||||
-----------------------------------------*/
|
||||
/**
|
||||
* @brief FatFs flash read id
|
||||
* @param void
|
||||
* @retval uint32_t - id
|
||||
*/
|
||||
uint32_t Fat_Flash_ReadID(void)
|
||||
{
|
||||
uint32_t id = 0;
|
||||
DEBUG("id\r");
|
||||
if (XR_OK == spi_flash_rdid(XC_SPI0, (uint8_t *)&id)) {
|
||||
DEBUG("read id :0x%08x\r\n", id);
|
||||
return id;
|
||||
}
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief FatFs flash chip erase
|
||||
* @param void
|
||||
* @retval bool - true or false
|
||||
*/
|
||||
bool Fat_Flash_ChipErase(void)
|
||||
{
|
||||
uint32_t offset_cnt =
|
||||
(FAT_FLASH_END_ADDR - FAT_FLASH_START_ADDR + 1) / FAT_FLASH_SECTOR_SIZE;
|
||||
|
||||
for (uint32_t i = 0; i < offset_cnt; i++) {
|
||||
if (XR_OK !=
|
||||
xc_spi_flash_erase_sector(XC_SPI0, FAT_FLASH_START_ADDR +
|
||||
i * FAT_FLASH_SECTOR_SIZE))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief FatFs flash sector erase
|
||||
* @param void
|
||||
* @retval bool - true or false
|
||||
*/
|
||||
bool Fat_Flash_SectorErase(uint32_t address)
|
||||
{
|
||||
if (XR_OK == xc_spi_flash_erase_sector(XC_SPI0, address))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief FatFs flash write data
|
||||
* @param void
|
||||
* @retval bool - true or false
|
||||
*/
|
||||
bool Fat_Flash_WriteData(uint32_t address, uint8_t *buffer, uint16_t length)
|
||||
{
|
||||
uint16_t writeLen, pageOff;
|
||||
|
||||
while (length > 0) {
|
||||
pageOff = FLASH_PAGE_SIZE - (address % FLASH_PAGE_SIZE);
|
||||
writeLen = length > pageOff ? pageOff : length;
|
||||
|
||||
if (XR_OK == spi_write_bytes(XC_SPI0, address, buffer, writeLen)) {
|
||||
length -= writeLen;
|
||||
address += writeLen;
|
||||
buffer += writeLen;
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief FatFs flash read data
|
||||
* @param void
|
||||
* @retval bool - true or false
|
||||
*/
|
||||
bool Fat_Flash_ReadData(uint32_t address, uint8_t *buffer, uint16_t length)
|
||||
{
|
||||
if (XR_OK == spi_flash_read(XC_SPI0, address, buffer, length))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief FatFs flash data are write in units of 512 bytes
|
||||
* @param void
|
||||
* @retval bool - true or false
|
||||
*/
|
||||
bool Fat_Flash_Write_512B(uint32_t address, uint8_t count, uint8_t *data)
|
||||
{
|
||||
uint32_t sectorAddress;
|
||||
uint32_t sectorOffset;
|
||||
|
||||
address += FAT_FLASH_START_ADDR;
|
||||
if (address >= FAT_FLASH_END_ADDR)
|
||||
return false;
|
||||
|
||||
// count *= 2;
|
||||
while (count > 0) {
|
||||
sectorAddress = FLASH_SECTOR_ALIGN(address);
|
||||
sectorOffset = FLASH_SECTOR_OFFSET(address);
|
||||
|
||||
Fat_Flash_ReadData(sectorAddress, dataBuffer, FAT_FLASH_SECTOR_SIZE);
|
||||
Fat_Flash_SectorErase(address);
|
||||
|
||||
while (count > 0) {
|
||||
memcpy(dataBuffer + sectorOffset, data, DISK_UNIT_SIZE);
|
||||
data += DISK_UNIT_SIZE;
|
||||
address += DISK_UNIT_SIZE;
|
||||
count -= 1;
|
||||
sectorOffset += DISK_UNIT_SIZE;
|
||||
|
||||
if ((sectorOffset & (FAT_FLASH_SECTOR_SIZE - 1)) == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (false == Fat_Flash_WriteData(sectorAddress, dataBuffer,
|
||||
FAT_FLASH_SECTOR_SIZE))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief FatFs flash data are read in units of 512 bytes
|
||||
* @param void
|
||||
* @retval bool - true or false
|
||||
*/
|
||||
bool Fat_Flash_Read_512B(uint32_t address, uint8_t count, uint8_t *data)
|
||||
{
|
||||
address += FAT_FLASH_START_ADDR;
|
||||
if (address >= FAT_FLASH_END_ADDR)
|
||||
return false;
|
||||
|
||||
// count *= 2;
|
||||
while (count--) {
|
||||
if (true == Fat_Flash_ReadData(address, data, (DISK_UNIT_SIZE))) {
|
||||
data += (DISK_UNIT_SIZE);
|
||||
address += (DISK_UNIT_SIZE);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*----------------------------- Disk Map -----------------------------------*/
|
||||
/**
|
||||
* @brief Disk init
|
||||
* @param void
|
||||
* @retval bool - true or false
|
||||
*/
|
||||
bool Disk_Init(void)
|
||||
{
|
||||
if (spim_init == false) {
|
||||
SPI_Flash_Init();
|
||||
spim_init = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Disk erase
|
||||
* @param void
|
||||
* @retval bool - true or false
|
||||
*/
|
||||
bool Disk_Erase(void)
|
||||
{
|
||||
if (true == Fat_Flash_ChipErase())
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Disk unit size
|
||||
* @param void
|
||||
* @retval uint16_t - size
|
||||
*/
|
||||
uint16_t Disk_UnitSize(void) { return DISK_UNIT_SIZE; }
|
||||
|
||||
/**
|
||||
* @brief Disk unit count
|
||||
* @param void
|
||||
* @retval uint32_t - count
|
||||
*/
|
||||
uint32_t Disk_UnitCount(void)
|
||||
{
|
||||
uint32_t count = 0;
|
||||
|
||||
count = (FAT_FLASH_END_ADDR - FAT_FLASH_START_ADDR + 1) /
|
||||
FAT_FLASH_PAGE_SIZE / 2;
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Disk unit data read
|
||||
* @param void
|
||||
* @retval bool - true or false
|
||||
*/
|
||||
bool Disk_UnitRead(uint32_t unitAddr, uint16_t unitCount, uint8_t *data)
|
||||
{
|
||||
if (true == Fat_Flash_Read_512B(unitAddr * 512, unitCount, data))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Disk unit data write
|
||||
* @param void
|
||||
* @retval bool - true or false
|
||||
*/
|
||||
bool Disk_UnitWrite(uint32_t unitAddr, uint16_t unitCount, uint8_t *data)
|
||||
{
|
||||
if (true == Fat_Flash_Write_512B(unitAddr * 512, unitCount, data))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
/*--------------------------------------------------------------------------*/
|
||||
@@ -0,0 +1,72 @@
|
||||
/*!
|
||||
* \file msc_flash.h
|
||||
*
|
||||
* \brief Target msc flash implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __MSC_FLASH_H__
|
||||
#define __MSC_FLASH_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "xc6xxx_hal_spi.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------------------------------------------------*/
|
||||
#define FAT_FLASH_ID 0x12408500 //0x00136085
|
||||
|
||||
#define FAT_FLASH_PAGE_SIZE 256U
|
||||
#define FAT_FLASH_SECTOR_SIZE 4096u
|
||||
|
||||
#define FAT_FLASH_START_ADDR 0x20000 //0x40000
|
||||
#define FAT_FLASH_END_ADDR 0x40000 //0x7FFFF
|
||||
|
||||
#define DISK_UNIT_SIZE 512U
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
------------------------------------------- -----------------------------------------*/
|
||||
uint32_t Fat_Flash_ReadID( void );
|
||||
bool Disk_Init( void );
|
||||
bool Disk_Erase( void );
|
||||
uint16_t Disk_UnitSize( void );
|
||||
uint32_t Disk_UnitCount( void );
|
||||
bool Disk_UnitRead( uint32_t unitAddr, uint16_t unitCount, uint8_t *data );
|
||||
bool Disk_UnitWrite( uint32_t unitAddr, uint16_t unitCount, uint8_t *data );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __MAIN_H */
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
/*!
|
||||
* \file usb_device.c
|
||||
*
|
||||
* \brief Target usb device implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usb_device.h"
|
||||
#include "usbd_core.h"
|
||||
#include "usbd_desc.h"
|
||||
#include "usbd_msc.h"
|
||||
#include "usbd_storage_if.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/* USB Device Core handle declaration. */
|
||||
USBD_HandleTypeDef hUsbDeviceFS;
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief This function handles USB On The Go FS global interrupt.
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void USB_Handler(void) { HAL_PCD_IRQHandler(&hpcd_USB_OTG_FS); }
|
||||
|
||||
/**
|
||||
* @brief Init USB device Library, add supported class and start the library
|
||||
* @param void
|
||||
* @retval void
|
||||
*/
|
||||
void USB_DEVICE_Init(void)
|
||||
{
|
||||
/* Init Device Library, add supported class and start the library. */
|
||||
if (USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS) != USBD_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
if (USBD_RegisterClass(&hUsbDeviceFS, &USBD_MSC) != USBD_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
if (USBD_MSC_RegisterStorage(&hUsbDeviceFS,
|
||||
&USBD_Storage_Interface_fops_FS) != USBD_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
if (USBD_Start(&hUsbDeviceFS) != USBD_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*!
|
||||
* \file usb_device.h
|
||||
*
|
||||
* \brief Target usb device implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __USB_DEVICE_H__
|
||||
#define __USB_DEVICE_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usbd_def.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Exported Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
void USB_DEVICE_Init( void );
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __USB_DEVICE_H__ */
|
||||
@@ -0,0 +1,358 @@
|
||||
/*!
|
||||
* \file usbd_desc.c
|
||||
*
|
||||
* \brief Target the USB device descriptors implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usbd_desc.h"
|
||||
#include "usbd_conf.h"
|
||||
#include "usbd_core.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------
|
||||
-----------------------------------------*/
|
||||
/** @defgroup USBD_DESC_Private_Defines USBD_DESC_Private_Defines
|
||||
* @brief Private defines.
|
||||
* @{
|
||||
*/
|
||||
#define USBD_VID 0x0483 // 1155
|
||||
#define USBD_LANGID_STRING 0x0409 // 1033
|
||||
#define USBD_MANUFACTURER_STRING "XinChip" //"XinChip"
|
||||
#define USBD_PID_FS 22314 // 17788
|
||||
#define USBD_PRODUCT_STRING_FS "XinChip Mass Storage" //"XinChip Mass Storage"
|
||||
#define USBD_CONFIGURATION_STRING_FS "MSC Config"
|
||||
#define USBD_INTERFACE_STRING_FS "MSC Interface"
|
||||
|
||||
#define USB_SIZ_BOS_DESC 0x0C
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Func Prototypes
|
||||
-------------------------------------------
|
||||
-----------------------------------------*/
|
||||
/** @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 for FS.
|
||||
* @{
|
||||
*/
|
||||
|
||||
uint8_t *USBD_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
|
||||
uint8_t *USBD_FS_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
|
||||
uint8_t *USBD_FS_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed,
|
||||
uint16_t *length);
|
||||
uint8_t *USBD_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed,
|
||||
uint16_t *length);
|
||||
uint8_t *USBD_FS_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
|
||||
uint8_t *USBD_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
|
||||
uint8_t *USBD_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed,
|
||||
uint16_t *length);
|
||||
#if (USBD_LPM_ENABLED == 1)
|
||||
uint8_t *USBD_FS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length);
|
||||
#endif /* (USBD_LPM_ENABLED == 1) */
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Local Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @defgroup USBD_DESC_Private_Variables USBD_DESC_Private_Variables
|
||||
* @brief Private variables.
|
||||
* @{
|
||||
*/
|
||||
|
||||
USBD_DescriptorsTypeDef FS_Desc = {USBD_FS_DeviceDescriptor,
|
||||
USBD_FS_LangIDStrDescriptor,
|
||||
USBD_FS_ManufacturerStrDescriptor,
|
||||
USBD_FS_ProductStrDescriptor,
|
||||
USBD_FS_SerialStrDescriptor,
|
||||
USBD_FS_ConfigStrDescriptor,
|
||||
USBD_FS_InterfaceStrDescriptor
|
||||
#if (USBD_LPM_ENABLED == 1)
|
||||
,
|
||||
USBD_FS_USR_BOSDescriptor
|
||||
#endif /* (USBD_LPM_ENABLED == 1) */
|
||||
};
|
||||
|
||||
#if defined(__ICCARM__) /* IAR Compiler */
|
||||
#pragma data_alignment = 4
|
||||
#endif /* defined ( __ICCARM__ ) */
|
||||
/** USB standard device descriptor. */
|
||||
__ALIGN_BEGIN uint8_t USBD_FS_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = {
|
||||
0x12, /*bLength */
|
||||
USB_DESC_TYPE_DEVICE, /*bDescriptorType*/
|
||||
#if (USBD_LPM_ENABLED == 1)
|
||||
0x01,
|
||||
/*bcdUSB */ /* changed to USB version 2.01
|
||||
in order to support LPM L1 suspend
|
||||
resume test of USBCV3.0*/
|
||||
#else
|
||||
0x00, /*bcdUSB */
|
||||
#endif /* (USBD_LPM_ENABLED == 1) */
|
||||
0x02,
|
||||
0x00, /*bDeviceClass*/
|
||||
0x00, /*bDeviceSubClass*/
|
||||
0x00, /*bDeviceProtocol*/
|
||||
USB_MAX_EP0_SIZE, /*bMaxPacketSize*/
|
||||
LOBYTE(USBD_VID), /*idVendor*/
|
||||
HIBYTE(USBD_VID), /*idVendor*/
|
||||
LOBYTE(USBD_PID_FS), /*idProduct*/
|
||||
HIBYTE(USBD_PID_FS), /*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 */
|
||||
/** BOS descriptor. */
|
||||
#if (USBD_LPM_ENABLED == 1)
|
||||
#if defined(__ICCARM__) /* IAR Compiler */
|
||||
#pragma data_alignment = 4
|
||||
#endif /* defined ( __ICCARM__ ) */
|
||||
__ALIGN_BEGIN uint8_t USBD_FS_BOSDesc[USB_SIZ_BOS_DESC] __ALIGN_END = {
|
||||
0x5, USB_DESC_TYPE_BOS, 0xC, 0x0, 0x1, /* 1 device capability*/
|
||||
/* device capability*/
|
||||
0x7, USB_DEVICE_CAPABITY_TYPE, 0x2, 0x2, /* LPM capability bit set*/
|
||||
0x0, 0x0, 0x0};
|
||||
#endif /* (USBD_LPM_ENABLED == 1) */
|
||||
|
||||
/** @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,
|
||||
};
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
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_FS_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
{
|
||||
UNUSED(speed);
|
||||
*length = sizeof(USBD_FS_DeviceDesc);
|
||||
return USBD_FS_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_FS_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_FS_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
{
|
||||
if (speed == 0) {
|
||||
USBD_GetString((uint8_t *)USBD_PRODUCT_STRING_FS, USBD_StrDesc, length);
|
||||
} else {
|
||||
USBD_GetString((uint8_t *)USBD_PRODUCT_STRING_FS, 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_FS_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_FS_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_FS_SerialStrDescriptor */
|
||||
|
||||
/* USER CODE END USBD_FS_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_FS_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
{
|
||||
if (speed == USBD_SPEED_HIGH) {
|
||||
USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING_FS, USBD_StrDesc,
|
||||
length);
|
||||
} else {
|
||||
USBD_GetString((uint8_t *)USBD_CONFIGURATION_STRING_FS, 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_FS_InterfaceStrDescriptor(USBD_SpeedTypeDef speed,
|
||||
uint16_t *length)
|
||||
{
|
||||
if (speed == 0) {
|
||||
USBD_GetString((uint8_t *)USBD_INTERFACE_STRING_FS, USBD_StrDesc,
|
||||
length);
|
||||
} else {
|
||||
USBD_GetString((uint8_t *)USBD_INTERFACE_STRING_FS, USBD_StrDesc,
|
||||
length);
|
||||
}
|
||||
return USBD_StrDesc;
|
||||
}
|
||||
|
||||
#if (USBD_LPM_ENABLED == 1)
|
||||
/**
|
||||
* @brief Return the BOS descriptor
|
||||
* @param speed : Current device speed
|
||||
* @param length : Pointer to data length variable
|
||||
* @retval Pointer to descriptor buffer
|
||||
*/
|
||||
uint8_t *USBD_FS_USR_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *length)
|
||||
{
|
||||
INFO("\n");
|
||||
UNUSED(speed);
|
||||
*length = sizeof(USBD_FS_BOSDesc);
|
||||
return (uint8_t *)USBD_FS_BOSDesc;
|
||||
}
|
||||
#endif /* (USBD_LPM_ENABLED == 1) */
|
||||
|
||||
/**
|
||||
* @brief Create the serial number string descriptor
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void Get_SerialNum(void)
|
||||
{
|
||||
uint32_t deviceserial0, deviceserial1; //, deviceserial2;
|
||||
|
||||
// deviceserial0 = *(uint32_t *) DEVICE_ID1;
|
||||
// deviceserial1 = *(uint32_t *) DEVICE_ID2;
|
||||
// deviceserial2 = *(uint32_t *) DEVICE_ID3;
|
||||
|
||||
deviceserial0 = 0x20220902; // 5177440; //0;//5177402;
|
||||
deviceserial1 = 0x1A0000; // 1395675155; //0x001A0000;
|
||||
// deviceserial2 = //540488500; //0;
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
@@ -0,0 +1,70 @@
|
||||
/*!
|
||||
* \file usbd_desc.h
|
||||
*
|
||||
* \brief Target the USB device descriptors implementation
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __USBD_DESC__C__
|
||||
#define __USBD_DESC__C__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usbd_def.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
------------------------------------------- -----------------------------------------*/
|
||||
/** @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
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** Descriptor for the Usb device. */
|
||||
extern USBD_DescriptorsTypeDef FS_Desc;
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __USBD_DESC__C__ */
|
||||
|
||||
@@ -0,0 +1,242 @@
|
||||
/*!
|
||||
* \file usbd_storage_if.c
|
||||
*
|
||||
* \brief Memory management layer.
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usbd_storage_if.h"
|
||||
#include "msc_flash.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
-------------------------------------------
|
||||
-----------------------------------------*/
|
||||
/** @defgroup USBD_STORAGE_Private_Defines
|
||||
* @brief Private defines.
|
||||
* @{
|
||||
*/
|
||||
|
||||
#define STORAGE_LUN_NBR 1
|
||||
#define STORAGE_BLK_NBR 0x48
|
||||
#define STORAGE_BLK_SIZ 0x200
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Consts
|
||||
-------------------------------------------
|
||||
-----------------------------------------*/
|
||||
/* USER CODE BEGIN INQUIRY_DATA_FS */
|
||||
/** USB Mass storage Standard Inquiry Data. */
|
||||
const int8_t STORAGE_Inquirydata_FS[] = {
|
||||
/* 36 */
|
||||
|
||||
/* LUN 0 */
|
||||
0x00, 0x80, 0x02, 0x02, (STANDARD_INQUIRY_DATA_LEN - 5),
|
||||
0x00, 0x00, 0x00, 'X', 'i',
|
||||
'n', 'C', 'h', 'i', 'p',
|
||||
' ', /* Manufacturer : 8 bytes */
|
||||
'P', 'r', 'o', 'd', 'u',
|
||||
'c', 't', ' ', /* Product : 16 Bytes */
|
||||
' ', ' ', ' ', ' ', ' ',
|
||||
' ', ' ', ' ', '0', '.',
|
||||
'0', '1' /* Version : 4 Bytes */
|
||||
};
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @defgroup USBD_STORAGE_Exported_Variables
|
||||
* @brief Public variables.
|
||||
* @{
|
||||
*/
|
||||
|
||||
extern USBD_HandleTypeDef hUsbDeviceFS;
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Func Prototype
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @defgroup USBD_STORAGE_Private_FunctionPrototypes
|
||||
* @brief Private functions declaration.
|
||||
* @{
|
||||
*/
|
||||
|
||||
static int8_t STORAGE_Init_FS(uint8_t lun);
|
||||
static int8_t STORAGE_GetCapacity_FS(uint8_t lun, uint32_t *block_num,
|
||||
uint16_t *block_size);
|
||||
static int8_t STORAGE_IsReady_FS(uint8_t lun);
|
||||
static int8_t STORAGE_IsWriteProtected_FS(uint8_t lun);
|
||||
static int8_t STORAGE_Read_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr,
|
||||
uint16_t blk_len);
|
||||
static int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr,
|
||||
uint16_t blk_len);
|
||||
static int8_t STORAGE_GetMaxLun_FS(void);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
USBD_StorageTypeDef USBD_Storage_Interface_fops_FS = {
|
||||
STORAGE_Init_FS, STORAGE_GetCapacity_FS,
|
||||
STORAGE_IsReady_FS, STORAGE_IsWriteProtected_FS,
|
||||
STORAGE_Read_FS, STORAGE_Write_FS,
|
||||
STORAGE_GetMaxLun_FS, (int8_t *)STORAGE_Inquirydata_FS};
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Functions
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief Initializes the storage unit (medium) over USB FS IP
|
||||
* @param lun: Logical unit number.
|
||||
* @retval USBD_OK if all operations are OK else USBD_FAIL
|
||||
*/
|
||||
int8_t STORAGE_Init_FS(uint8_t lun)
|
||||
{
|
||||
UNUSED(lun);
|
||||
|
||||
return (USBD_OK);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the medium capacity.
|
||||
* @param lun: Logical unit number.
|
||||
* @param block_num: Number of total block number.
|
||||
* @param block_size: Block size.
|
||||
* @retval USBD_OK if all operations are OK else USBD_FAIL
|
||||
*/
|
||||
int8_t STORAGE_GetCapacity_FS(uint8_t lun, uint32_t *block_num,
|
||||
uint16_t *block_size)
|
||||
{
|
||||
UNUSED(lun);
|
||||
|
||||
#if STORAGE_NO_FLASH
|
||||
// STORAGE_BLK_NBR;
|
||||
// STORAGE_BLK_SIZ;
|
||||
*block_num = STORAGE_BLK_NBR;
|
||||
*block_size = STORAGE_BLK_SIZ;
|
||||
|
||||
#else
|
||||
*block_num = Disk_UnitCount();
|
||||
*block_size = Disk_UnitSize();
|
||||
#endif
|
||||
|
||||
return (USBD_OK);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Checks whether the medium is ready.
|
||||
* @param lun: Logical unit number.
|
||||
* @retval USBD_OK if all operations are OK else USBD_FAIL
|
||||
*/
|
||||
int8_t STORAGE_IsReady_FS(uint8_t lun)
|
||||
{
|
||||
UNUSED(lun);
|
||||
|
||||
return (USBD_OK);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Checks whether the medium is write protected.
|
||||
* @param lun: Logical unit number.
|
||||
* @retval USBD_OK if all operations are OK else USBD_FAIL
|
||||
*/
|
||||
int8_t STORAGE_IsWriteProtected_FS(uint8_t lun)
|
||||
{
|
||||
UNUSED(lun);
|
||||
|
||||
return (USBD_OK);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Reads data from the medium.
|
||||
* @param lun: Logical unit number.
|
||||
* @param buf: data buffer.
|
||||
* @param blk_addr: Logical block address.
|
||||
* @param blk_len: Blocks number.
|
||||
* @retval USBD_OK if all operations are OK else USBD_FAIL
|
||||
*/
|
||||
int8_t STORAGE_Read_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr,
|
||||
uint16_t blk_len)
|
||||
{
|
||||
UNUSED(lun);
|
||||
|
||||
#if STORAGE_NO_FLASH
|
||||
UNUSED(buf);
|
||||
UNUSED(blk_addr);
|
||||
UNUSED(blk_len);
|
||||
// memcpy(buf, temp + (blk_addr * STORAGE_BLK_SIZ), blk_len *
|
||||
// STORAGE_BLK_SIZ);
|
||||
|
||||
#else
|
||||
Disk_UnitRead(blk_addr, blk_len, buf);
|
||||
|
||||
#endif
|
||||
|
||||
return (USBD_OK);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Writes data into the medium.
|
||||
* @param lun: Logical unit number.
|
||||
* @param buf: data buffer.
|
||||
* @param blk_addr: Logical block address.
|
||||
* @param blk_len: Blocks number.
|
||||
* @retval USBD_OK if all operations are OK else USBD_FAIL
|
||||
*/
|
||||
int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr,
|
||||
uint16_t blk_len)
|
||||
{
|
||||
UNUSED(lun);
|
||||
|
||||
#if STORAGE_NO_FLASH
|
||||
UNUSED(buf);
|
||||
UNUSED(blk_addr);
|
||||
UNUSED(blk_len);
|
||||
// memcpy(temp + (blk_addr * STORAGE_BLK_SIZ), buf, blk_len *
|
||||
// STORAGE_BLK_SIZ);
|
||||
|
||||
#else
|
||||
Disk_UnitWrite(blk_addr, blk_len, buf);
|
||||
|
||||
#endif
|
||||
|
||||
return (USBD_OK);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the Max Supported LUNs.
|
||||
* @param None
|
||||
* @retval Lun(s) number.
|
||||
*/
|
||||
int8_t STORAGE_GetMaxLun_FS(void) { return (STORAGE_LUN_NBR - 1); }
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
@@ -0,0 +1,73 @@
|
||||
/*!
|
||||
* \file usbd_storage_if.h
|
||||
*
|
||||
* \brief Header for usbd_storage_if.c file.
|
||||
*
|
||||
* \copyright Revised BSD License, see section \ref LICENSE.
|
||||
*
|
||||
* \code
|
||||
*
|
||||
* _ __ _ ________ _
|
||||
* | |/ /(_)___ / ____/ /_ (_)___
|
||||
* | // / __ \/ / / __ \/ / __ \
|
||||
* / |/ / / / / /___/ / / / / /_/ /
|
||||
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
|
||||
* /_/
|
||||
* (C) 2022-2025 XinChip
|
||||
*
|
||||
* \endcode
|
||||
*
|
||||
* \author ( XinChip ) Alex-J
|
||||
*
|
||||
* \author ( XinChip )
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __USBD_STORAGE_IF_H__
|
||||
#define __USBD_STORAGE_IF_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------------------------------------
|
||||
INCLUDE HEADE FILES
|
||||
------------------------------------------------------------------------------------*/
|
||||
#include "usbd_msc.h"
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Macros
|
||||
------------------------------------------- -----------------------------------------*/
|
||||
/** @defgroup USBD_STORAGE_Exported_Defines USBD_STORAGE_Exported_Defines
|
||||
* @brief Defines.
|
||||
* @{
|
||||
*/
|
||||
|
||||
#define STORAGE_NO_FLASH 0U
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/*------------------------------------------------------------------------------------
|
||||
Global Variables
|
||||
-------------------------------------------------------------------------------------*/
|
||||
/** @defgroup USBD_STORAGE_Exported_Variables USBD_STORAGE_Exported_Variables
|
||||
* @brief Public variables.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** STORAGE Interface callback. */
|
||||
extern USBD_StorageTypeDef USBD_Storage_Interface_fops_FS;
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __USBD_STORAGE_IF_H__ */
|
||||
|
||||
Reference in New Issue
Block a user