Files
AT32F415_Firmware_Library/middlewares/usbd_class/mouse/mouse_desc.c

480 lines
14 KiB
C
Raw Normal View History

2021-12-14 13:34:31 +08:00
/**
**************************************************************************
* @file mouse_desc.c
* @brief usb hid mouse device descriptor
**************************************************************************
* Copyright notice & Disclaimer
*
2022-04-11 19:32:28 +08:00
* The software Board Support Package (BSP) that is made available to
* download from Artery official website is the copyrighted work of Artery.
* Artery authorizes customers to use, copy, and distribute the BSP
* software and its related documentation for the purpose of design and
* development in conjunction with Artery microcontrollers. Use of the
2021-12-14 13:34:31 +08:00
* software is governed by this copyright notice and the following disclaimer.
*
* THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
* GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
* TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
* STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
* INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
*
**************************************************************************
*/
#include "usb_std.h"
#include "usbd_sdr.h"
#include "usbd_core.h"
#include "mouse_desc.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
2022-04-11 19:32:28 +08:00
2021-12-14 13:34:31 +08:00
/** @defgroup USB_mouse_desc
* @brief usb device mouse descriptor
* @{
2022-04-11 19:32:28 +08:00
*/
2021-12-14 13:34:31 +08:00
/** @defgroup USB_mouse_desc_private_functions
* @{
*/
2022-03-03 19:38:35 +08:00
static usbd_desc_t *get_device_descriptor(void);
static usbd_desc_t *get_device_qualifier(void);
static usbd_desc_t *get_device_configuration(void);
static usbd_desc_t *get_device_other_speed(void);
static usbd_desc_t *get_device_lang_id(void);
static usbd_desc_t *get_device_manufacturer_string(void);
static usbd_desc_t *get_device_product_string(void);
static usbd_desc_t *get_device_serial_string(void);
static usbd_desc_t *get_device_interface_string(void);
static usbd_desc_t *get_device_config_string(void);
static uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf);
2021-12-14 13:34:31 +08:00
static void usbd_int_to_unicode (uint32_t value , uint8_t *pbuf , uint8_t len);
static void get_serial_num(void);
static uint8_t g_usbd_desc_buffer[256];
/**
* @brief mouse device descriptor handler structure
*/
usbd_desc_handler mouse_desc_handler =
{
get_device_descriptor,
get_device_qualifier,
get_device_configuration,
get_device_other_speed,
get_device_lang_id,
get_device_manufacturer_string,
get_device_product_string,
get_device_serial_string,
get_device_interface_string,
get_device_config_string,
};
/**
* @brief usb device standard descriptor
*/
2022-01-21 15:54:53 +08:00
#if defined ( __ICCARM__ ) /* iar compiler */
#pragma data_alignment=4
#endif
2022-03-03 19:38:35 +08:00
ALIGNED_HEAD static uint8_t g_usbd_descriptor[USB_DEVICE_DESC_LEN] ALIGNED_TAIL =
2021-12-14 13:34:31 +08:00
{
USB_DEVICE_DESC_LEN, /* bLength */
USB_DESCIPTOR_TYPE_DEVICE, /* bDescriptorType */
0x00, /* bcdUSB */
0x02,
0x00, /* bDeviceClass */
0x00, /* bDeviceSubClass */
0x00, /* bDeviceProtocol */
USB_MAX_EP0_SIZE, /* bMaxPacketSize */
2022-03-03 19:38:35 +08:00
LBYTE(USBD_MOUSE_VENDOR_ID), /* idVendor */
HBYTE(USBD_MOUSE_VENDOR_ID), /* idVendor */
LBYTE(USBD_MOUSE_PRODUCT_ID), /* idProduct */
HBYTE(USBD_MOUSE_PRODUCT_ID), /* idProduct */
2021-12-14 13:34:31 +08:00
0x00, /* bcdDevice rel. 2.00 */
0x02,
USB_MFC_STRING, /* Index of manufacturer string */
USB_PRODUCT_STRING, /* Index of product string */
USB_SERIAL_STRING, /* Index of serial number string */
1 /* bNumConfigurations */
};
/**
* @brief usb configuration standard descriptor
*/
2022-01-21 15:54:53 +08:00
#if defined ( __ICCARM__ ) /* iar compiler */
#pragma data_alignment=4
#endif
2022-03-03 19:38:35 +08:00
ALIGNED_HEAD static uint8_t g_usbd_configuration[USBD_MOUSE_CONFIG_DESC_SIZE] ALIGNED_TAIL =
2021-12-14 13:34:31 +08:00
{
USB_DEVICE_CFG_DESC_LEN, /* bLength: configuration descriptor size */
USB_DESCIPTOR_TYPE_CONFIGURATION, /* bDescriptorType: configuration */
2022-03-03 19:38:35 +08:00
LBYTE(USBD_MOUSE_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
HBYTE(USBD_MOUSE_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
2021-12-14 13:34:31 +08:00
0x01, /* bNumInterfaces: 1 interface */
0x01, /* bConfigurationValue: configuration value */
0x00, /* iConfiguration: index of string descriptor describing
the configuration */
0xE0, /* bmAttributes: self powered and support remote wakeup*/
0x32, /* MaxPower 100 mA: this current is used for detecting vbus */
2022-04-11 19:32:28 +08:00
2021-12-14 13:34:31 +08:00
USB_DEVICE_IF_DESC_LEN, /* bLength: interface descriptor size */
USB_DESCIPTOR_TYPE_INTERFACE, /* bDescriptorType: interface descriptor type */
0x00, /* bInterfaceNumber: number of interface */
0x00, /* bAlternateSetting: alternate set */
0x01, /* bNumEndpoints: number of endpoints */
USB_CLASS_CODE_HID, /* bInterfaceClass: class code hid */
0x01, /* bInterfaceSubClass: subclass code */
0x02, /* bInterfaceProtocol: protocol code */
0x00, /* iInterface: index of string descriptor */
2022-04-11 19:32:28 +08:00
2021-12-14 13:34:31 +08:00
0x09, /* bLength: size of HID descriptor in bytes */
HID_CLASS_DESC_HID, /* bDescriptorType: HID descriptor type */
2022-03-03 19:38:35 +08:00
LBYTE(MOUSE_BCD_NUM),
HBYTE(MOUSE_BCD_NUM), /* bcdHID: HID class specification release number */
2021-12-14 13:34:31 +08:00
0x00, /* bCountryCode: hardware target conutry */
0x01, /* bNumDescriptors: number of HID class descriptor to follow */
HID_CLASS_DESC_REPORT, /* bDescriptorType: report descriptor type */
2022-03-03 19:38:35 +08:00
LBYTE(sizeof(g_usbd_mouse_report)),
HBYTE(sizeof(g_usbd_mouse_report)), /* wDescriptorLength: total length of reprot descriptor */
2022-04-11 19:32:28 +08:00
2021-12-14 13:34:31 +08:00
USB_DEVICE_EPT_LEN, /* bLength: size of endpoint descriptor in bytes */
USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
2022-03-03 19:38:35 +08:00
USBD_MOUSE_IN_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
2021-12-14 13:34:31 +08:00
USB_EPT_DESC_INTERRUPT, /* bmAttributes: endpoint attributes */
2022-03-03 19:38:35 +08:00
LBYTE(USBD_MOUSE_IN_MAXPACKET_SIZE),
HBYTE(USBD_MOUSE_IN_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
MOUSE_BINTERVAL_TIME, /* bInterval: interval for polling endpoint for data transfers */
2021-12-14 13:34:31 +08:00
};
/**
* @brief usb mouse report descriptor
*/
2022-01-21 15:54:53 +08:00
#if defined ( __ICCARM__ ) /* iar compiler */
#pragma data_alignment=4
#endif
2022-04-11 19:32:28 +08:00
ALIGNED_HEAD uint8_t g_usbd_mouse_report[USBD_MOUSE_SIZ_REPORT_DESC] ALIGNED_TAIL =
2021-12-14 13:34:31 +08:00
{
0x05,0x01,
0x09,0x02,
0xA1,0x01,
0x09,0x01,
0xA1,0x00,
0x05,0x09,
0x19,0x01,
0x29,0x03,
0x15,0x00,
0x25,0x01,
0x95,0x03,
0x75,0x01,
0x81,0x02,
0x95,0x01,
0x75,0x05,
0x81,0x01,
0x05,0x01,
0x09,0x30,
0x09,0x31,
0x09,0x38,
0x15,0x81,
0x25,0x7F,
0x75,0x08,
0x95,0x03,
0x81,0x06,
0xC0,0x09,
0x3c,0x05,
0xff,0x09,
0x01,0x15,
0x00,0x25,
0x01,0x75,
0x01,0x95,
0x02,0xb1,
0x22,0x75,
0x06,0x95,
0x01,0xb1,
0x01,0xc0
};
/**
* @brief usb hid descriptor
*/
2022-01-21 15:54:53 +08:00
#if defined ( __ICCARM__ ) /* iar compiler */
#pragma data_alignment=4
#endif
2022-04-11 19:32:28 +08:00
ALIGNED_HEAD uint8_t g_mouse_usb_desc[9] ALIGNED_TAIL =
2021-12-14 13:34:31 +08:00
{
0x09, /* bLength: size of HID descriptor in bytes */
HID_CLASS_DESC_HID, /* bDescriptorType: HID descriptor type */
2022-03-03 19:38:35 +08:00
LBYTE(MOUSE_BCD_NUM),
HBYTE(MOUSE_BCD_NUM), /* bcdHID: HID class specification release number */
2021-12-14 13:34:31 +08:00
0x00, /* bCountryCode: hardware target conutry */
0x01, /* bNumDescriptors: number of HID class descriptor to follow */
HID_CLASS_DESC_REPORT, /* bDescriptorType: report descriptor type */
2022-03-03 19:38:35 +08:00
LBYTE(sizeof(g_usbd_mouse_report)),
HBYTE(sizeof(g_usbd_mouse_report)), /* wDescriptorLength: total length of reprot descriptor */
2021-12-14 13:34:31 +08:00
};
/**
* @brief usb string lang id
*/
2022-01-21 15:54:53 +08:00
#if defined ( __ICCARM__ ) /* iar compiler */
#pragma data_alignment=4
#endif
2022-03-03 19:38:35 +08:00
ALIGNED_HEAD static uint8_t g_string_lang_id[USBD_MOUSE_SIZ_STRING_LANGID] ALIGNED_TAIL =
2021-12-14 13:34:31 +08:00
{
2022-03-03 19:38:35 +08:00
USBD_MOUSE_SIZ_STRING_LANGID,
2021-12-14 13:34:31 +08:00
USB_DESCIPTOR_TYPE_STRING,
0x09,
0x04,
};
/**
* @brief usb string serial
*/
2022-01-21 15:54:53 +08:00
#if defined ( __ICCARM__ ) /* iar compiler */
#pragma data_alignment=4
#endif
2022-03-03 19:38:35 +08:00
ALIGNED_HEAD static uint8_t g_string_serial[USBD_MOUSE_SIZ_STRING_SERIAL] ALIGNED_TAIL =
2021-12-14 13:34:31 +08:00
{
2022-03-03 19:38:35 +08:00
USBD_MOUSE_SIZ_STRING_SERIAL,
2021-12-14 13:34:31 +08:00
USB_DESCIPTOR_TYPE_STRING,
};
/* device descriptor */
2022-03-03 19:38:35 +08:00
static usbd_desc_t device_descriptor =
2021-12-14 13:34:31 +08:00
{
USB_DEVICE_DESC_LEN,
g_usbd_descriptor
};
/* config descriptor */
2022-03-03 19:38:35 +08:00
static usbd_desc_t config_descriptor =
2021-12-14 13:34:31 +08:00
{
2022-03-03 19:38:35 +08:00
USBD_MOUSE_CONFIG_DESC_SIZE,
2021-12-14 13:34:31 +08:00
g_usbd_configuration
};
/* langid descriptor */
2022-03-03 19:38:35 +08:00
static usbd_desc_t langid_descriptor =
2021-12-14 13:34:31 +08:00
{
2022-03-03 19:38:35 +08:00
USBD_MOUSE_SIZ_STRING_LANGID,
2021-12-14 13:34:31 +08:00
g_string_lang_id
};
/* serial descriptor */
2022-03-03 19:38:35 +08:00
static usbd_desc_t serial_descriptor =
2021-12-14 13:34:31 +08:00
{
2022-03-03 19:38:35 +08:00
USBD_MOUSE_SIZ_STRING_SERIAL,
2021-12-14 13:34:31 +08:00
g_string_serial
};
2022-03-03 19:38:35 +08:00
static usbd_desc_t vp_desc;
2021-12-14 13:34:31 +08:00
/**
* @brief standard usb unicode convert
* @param string: source string
* @param unicode_buf: unicode buffer
2022-04-11 19:32:28 +08:00
* @retval length
2021-12-14 13:34:31 +08:00
*/
2022-03-03 19:38:35 +08:00
static uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf)
2021-12-14 13:34:31 +08:00
{
uint16_t str_len = 0, id_pos = 2;
uint8_t *tmp_str = string;
2022-04-11 19:32:28 +08:00
2021-12-14 13:34:31 +08:00
while(*tmp_str != '\0')
{
str_len ++;
unicode_buf[id_pos ++] = *tmp_str ++;
unicode_buf[id_pos ++] = 0x00;
}
2022-04-11 19:32:28 +08:00
2021-12-14 13:34:31 +08:00
str_len = str_len * 2 + 2;
unicode_buf[0] = str_len;
unicode_buf[1] = USB_DESCIPTOR_TYPE_STRING;
2022-04-11 19:32:28 +08:00
2021-12-14 13:34:31 +08:00
return str_len;
}
/**
* @brief usb int convert to unicode
* @param value: int value
* @param pbus: unicode buffer
* @param len: length
2022-04-11 19:32:28 +08:00
* @retval none
2021-12-14 13:34:31 +08:00
*/
static void usbd_int_to_unicode (uint32_t value , uint8_t *pbuf , uint8_t len)
{
uint8_t idx = 0;
2022-04-11 19:32:28 +08:00
2021-12-14 13:34:31 +08:00
for( idx = 0 ; idx < len ; idx ++)
{
if( ((value >> 28)) < 0xA )
{
pbuf[ 2 * idx] = (value >> 28) + '0';
}
else
{
2022-04-11 19:32:28 +08:00
pbuf[2 * idx] = (value >> 28) + 'A' - 10;
2021-12-14 13:34:31 +08:00
}
2022-04-11 19:32:28 +08:00
2021-12-14 13:34:31 +08:00
value = value << 4;
2022-04-11 19:32:28 +08:00
2021-12-14 13:34:31 +08:00
pbuf[2 * idx + 1] = 0;
}
}
/**
* @brief usb get serial number
* @param none
2022-04-11 19:32:28 +08:00
* @retval none
2021-12-14 13:34:31 +08:00
*/
static void get_serial_num(void)
{
uint32_t serial0, serial1, serial2;
2022-04-11 19:32:28 +08:00
2021-12-14 13:34:31 +08:00
serial0 = *(uint32_t*)MCU_ID1;
serial1 = *(uint32_t*)MCU_ID2;
serial2 = *(uint32_t*)MCU_ID3;
2022-04-11 19:32:28 +08:00
2021-12-14 13:34:31 +08:00
serial0 += serial2;
2022-04-11 19:32:28 +08:00
2021-12-14 13:34:31 +08:00
if (serial0 != 0)
{
usbd_int_to_unicode (serial0, &g_string_serial[2] ,8);
usbd_int_to_unicode (serial1, &g_string_serial[18] ,4);
}
}
/**
* @brief get device descriptor
* @param none
2022-04-11 19:32:28 +08:00
* @retval usbd_desc
2021-12-14 13:34:31 +08:00
*/
2022-03-03 19:38:35 +08:00
static usbd_desc_t *get_device_descriptor(void)
2021-12-14 13:34:31 +08:00
{
return &device_descriptor;
}
/**
* @brief get device qualifier
* @param none
2022-04-11 19:32:28 +08:00
* @retval usbd_desc
2021-12-14 13:34:31 +08:00
*/
2022-03-03 19:38:35 +08:00
static usbd_desc_t * get_device_qualifier(void)
2021-12-14 13:34:31 +08:00
{
return NULL;
}
/**
* @brief get config descriptor
* @param none
2022-04-11 19:32:28 +08:00
* @retval usbd_desc
2021-12-14 13:34:31 +08:00
*/
2022-03-03 19:38:35 +08:00
static usbd_desc_t *get_device_configuration(void)
2021-12-14 13:34:31 +08:00
{
return &config_descriptor;
}
/**
* @brief get other speed descriptor
* @param none
2022-04-11 19:32:28 +08:00
* @retval usbd_desc
2021-12-14 13:34:31 +08:00
*/
2022-03-03 19:38:35 +08:00
static usbd_desc_t *get_device_other_speed(void)
2021-12-14 13:34:31 +08:00
{
return NULL;
}
/**
* @brief get lang id descriptor
* @param none
2022-04-11 19:32:28 +08:00
* @retval usbd_desc
2021-12-14 13:34:31 +08:00
*/
2022-03-03 19:38:35 +08:00
static usbd_desc_t *get_device_lang_id(void)
2021-12-14 13:34:31 +08:00
{
return &langid_descriptor;
}
/**
* @brief get manufacturer descriptor
* @param none
2022-04-11 19:32:28 +08:00
* @retval usbd_desc
2021-12-14 13:34:31 +08:00
*/
2022-03-03 19:38:35 +08:00
static usbd_desc_t *get_device_manufacturer_string(void)
2021-12-14 13:34:31 +08:00
{
2022-03-03 19:38:35 +08:00
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_MOUSE_DESC_MANUFACTURER_STRING, g_usbd_desc_buffer);
2021-12-14 13:34:31 +08:00
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get product descriptor
* @param none
2022-04-11 19:32:28 +08:00
* @retval usbd_desc
2021-12-14 13:34:31 +08:00
*/
2022-03-03 19:38:35 +08:00
static usbd_desc_t *get_device_product_string(void)
2021-12-14 13:34:31 +08:00
{
2022-03-03 19:38:35 +08:00
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_MOUSE_DESC_PRODUCT_STRING, g_usbd_desc_buffer);
2021-12-14 13:34:31 +08:00
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get serial descriptor
* @param none
2022-04-11 19:32:28 +08:00
* @retval usbd_desc
2021-12-14 13:34:31 +08:00
*/
2022-03-03 19:38:35 +08:00
static usbd_desc_t *get_device_serial_string(void)
2021-12-14 13:34:31 +08:00
{
get_serial_num();
return &serial_descriptor;
}
/**
* @brief get interface descriptor
* @param none
2022-04-11 19:32:28 +08:00
* @retval usbd_desc
2021-12-14 13:34:31 +08:00
*/
2022-03-03 19:38:35 +08:00
static usbd_desc_t *get_device_interface_string(void)
2021-12-14 13:34:31 +08:00
{
2022-03-03 19:38:35 +08:00
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_MOUSE_DESC_INTERFACE_STRING, g_usbd_desc_buffer);
2021-12-14 13:34:31 +08:00
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get device config descriptor
* @param none
2022-04-11 19:32:28 +08:00
* @retval usbd_desc
2021-12-14 13:34:31 +08:00
*/
2022-03-03 19:38:35 +08:00
static usbd_desc_t *get_device_config_string(void)
2021-12-14 13:34:31 +08:00
{
2022-03-03 19:38:35 +08:00
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_MOUSE_DESC_CONFIGURATION_STRING, g_usbd_desc_buffer);
2021-12-14 13:34:31 +08:00
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @}
2022-04-11 19:32:28 +08:00
*/
2021-12-14 13:34:31 +08:00
/**
* @}
*/
/**
* @}
*/