upload version v2.0.0

This commit is contained in:
Artery-MCU
2021-12-14 13:34:31 +08:00
commit 2f3db2ee8e
1522 changed files with 431225 additions and 0 deletions

View File

@@ -0,0 +1,631 @@
/**
**************************************************************************
* @file audio_class.c
* @version v2.0.0
* @date 2021-11-26
* @brief usb audio class type
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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 "usbd_core.h"
#include "audio_class.h"
#include "audio_desc.h"
#include "audio_codec.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @defgroup USB_audio_class
* @brief usb device class audio demo
* @{
*/
/** @defgroup USB_audio_class_private_functions
* @{
*/
usb_sts_type class_init_handler(void *udev);
usb_sts_type class_clear_handler(void *udev);
usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup);
usb_sts_type class_ept0_tx_handler(void *udev);
usb_sts_type class_ept0_rx_handler(void *udev);
usb_sts_type class_in_handler(void *udev, uint8_t ept_num);
usb_sts_type class_out_handler(void *udev, uint8_t ept_num);
usb_sts_type class_sof_handler(void *udev);
usb_sts_type class_event_handler(void *udev, usbd_event_type event);
void audio_inisoincom_event(void *udev);
void audio_req_get_cur(void *udev, usb_setup_type *setup);
void audio_req_set_cur(void *udev, usb_setup_type *setup);
void audio_req_get_min(void *udev, usb_setup_type *setup);
void audio_req_get_max(void *udev, usb_setup_type *setup);
void audio_req_get_res(void *udev, usb_setup_type *setup);
void audio_get_interface(void *udev, usb_setup_type *setup);
void audio_set_interface(void *udev, usb_setup_type *setup);
usb_audio_type audio_struct = {0, 0, 0, 0, 0, 0x1400, 0, 0, 0, {0x0000, 0x1400, 0x33}, {0x0000, 0x1400, 0x33}};
/* usb device class handler */
usbd_class_handler audio_class_handler =
{
class_init_handler,
class_clear_handler,
class_setup_handler,
class_ept0_tx_handler,
class_ept0_rx_handler,
class_in_handler,
class_out_handler,
class_sof_handler,
class_event_handler,
};
/**
* @brief initialize usb custom hid endpoint
* @param udev: usb device core handler type
* @retval status of usb_sts_type
*/
usb_sts_type class_init_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
/* open microphone in endpoint */
usbd_ept_open(pudev, USBD_AUDIO_MIC_IN_EPT, EPT_ISO_TYPE, AUDIO_MIC_IN_MAXPACKET_SIZE);
/* open speaker out endpoint */
usbd_ept_open(pudev, USBD_AUDIO_SPK_OUT_EPT, EPT_ISO_TYPE, AUDIO_SPK_OUT_MAXPACKET_SIZE);
#if AUDIO_SUPPORT_FEEDBACK
/* open speaker feedback endpoint */
usbd_ept_open(pudev, USBD_AUDIO_FEEDBACK_EPT, EPT_ISO_TYPE, AUDIO_FEEDBACK_MAXPACKET_SIZE);
#endif
/* start receive speaker out data */
usbd_ept_recv(pudev, USBD_AUDIO_SPK_OUT_EPT, audio_struct.audio_spk_data, AUDIO_SPK_OUT_MAXPACKET_SIZE);
return status;
}
/**
* @brief clear endpoint or other state
* @param udev: usb device core handler type
* @retval status of usb_sts_type
*/
usb_sts_type class_clear_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
/* close in endpoint */
usbd_ept_close(pudev, USBD_AUDIO_MIC_IN_EPT);
/* close in endpoint */
usbd_ept_close(pudev, USBD_AUDIO_FEEDBACK_EPT);
/* close out endpoint */
usbd_ept_close(pudev, USBD_AUDIO_SPK_OUT_EPT);
return status;
}
/**
* @brief usb device class setup request handler
* @param udev: usb device core handler type
* @param setup: setup packet
* @retval status of usb_sts_type
*/
usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
uint16_t len;
switch(setup->bmRequestType & USB_REQ_TYPE_RESERVED)
{
/* class request */
case USB_REQ_TYPE_CLASS:
switch(setup->bRequest)
{
case AUDIO_REQ_GET_CUR:
audio_req_get_cur(pudev, setup);
break;
case AUDIO_REQ_SET_CUR:
audio_req_set_cur(pudev, setup);
break;
case AUDIO_REQ_GET_MIN:
audio_req_get_min(pudev, setup);
break;
case AUDIO_REQ_GET_MAX:
audio_req_get_max(pudev, setup);
break;
case AUDIO_REQ_GET_RES:
audio_req_get_res(pudev, setup);
break;
default:
usbd_ctrl_unsupport(pudev);
break;
}
break;
/* standard request */
case USB_REQ_TYPE_STANDARD:
switch(setup->bRequest)
{
case USB_STD_REQ_GET_DESCRIPTOR:
if((setup->wValue >> 8) == AUDIO_DESCRIPTOR_TYPE)
{
len = MIN(AUDIO_DESCRIPTOR_SIZE, setup->wLength);
usbd_ctrl_send(pudev, g_usbd_configuration+18, len);
}
else
{
usbd_ctrl_unsupport(pudev);
}
break;
case USB_STD_REQ_GET_INTERFACE:
audio_get_interface(udev, setup);
break;
case USB_STD_REQ_SET_INTERFACE:
audio_set_interface(udev, setup);
usbd_ctrl_send_status(pudev);
break;
default:
break;
}
break;
default:
usbd_ctrl_unsupport(pudev);
break;
}
return status;
}
/**
* @brief usb device endpoint 0 in status stage complete
* @param udev: usb device core handler type
* @retval status of usb_sts_type
*/
usb_sts_type class_ept0_tx_handler(void *udev)
{
usb_sts_type status = USB_OK;
/* ...user code... */
return status;
}
/**
* @brief usb device endpoint 0 out status stage complete
* @param udev: usb device core handler type
* @retval status of usb_sts_type
*/
usb_sts_type class_ept0_rx_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
uint32_t recv_len = usbd_get_recv_len(pudev, 0);
/* ...user code... */
if( audio_struct.audio_cmd == AUDIO_REQ_SET_CUR)
{
/* class process */
switch(audio_struct.request_no)
{
case AUDIO_VOLUME_CONTROL:
if(audio_struct.interface == AUDIO_SPK_FEATURE_UNIT_ID)
{
audio_struct.spk_volume = (uint16_t)(audio_struct.g_audio_cur[0] |
(audio_struct.g_audio_cur[1] << 8));
/* set volume */
audio_codec_set_spk_volume(audio_struct.spk_volume*100/audio_struct.spk_volume_limits[1]);
}
else
{
audio_struct.mic_volume = (uint16_t)(audio_struct.g_audio_cur[0] |
(audio_struct.g_audio_cur[1] << 8));
audio_codec_set_mic_volume(audio_struct.mic_volume*256/audio_struct.mic_volume_limits[1]);
}
break;
case AUDIO_MUTE_CONTROL:
if(audio_struct.interface == AUDIO_SPK_FEATURE_UNIT_ID)
{
audio_struct.spk_mute = audio_struct.g_audio_cur[0];
audio_codec_set_spk_mute(audio_struct.spk_mute);
}
else
{
audio_struct.mic_mute = audio_struct.g_audio_cur[0];
audio_codec_set_mic_mute(audio_struct.mic_mute);
}
break;
case AUDIO_FREQ_SET_CONTROL:
if(audio_struct.enpd == USBD_AUDIO_MIC_IN_EPT)
{
audio_struct.mic_freq = (audio_struct.g_audio_cur[0] |
(audio_struct.g_audio_cur[1] << 8) |
(audio_struct.g_audio_cur[2] << 16));
audio_codec_set_mic_freq(audio_struct.mic_freq);
}
else
{
audio_struct.spk_freq = (audio_struct.g_audio_cur[0] |
(audio_struct.g_audio_cur[1] << 8) |
(audio_struct.g_audio_cur[2] << 16));
audio_codec_set_spk_freq(audio_struct.spk_freq);
}
break;
default:
break;
}
}
return status;
}
/**
* @brief usb device transmision complete handler
* @param udev: usb device core handler type
* @param ept_num: endpoint number
* @retval status of usb_sts_type
*/
usb_sts_type class_in_handler(void *udev, uint8_t ept_num)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
uint32_t len = 0;
/* ...user code...
trans next packet data
*/
if((ept_num & 0x7F) == (USBD_AUDIO_MIC_IN_EPT & 0x7F))
{
len = audio_codec_mic_get_data(audio_struct.audio_mic_data);
usb_flush_tx_fifo(pudev->usb_reg, USBD_AUDIO_MIC_IN_EPT & 0x7F);
usbd_ept_send(pudev, USBD_AUDIO_MIC_IN_EPT, audio_struct.audio_mic_data, len);
}
else if((ept_num & 0x7F) == (USBD_AUDIO_FEEDBACK_EPT & 0x7F))
{
len = audio_codec_spk_feedback(audio_struct.audio_feed_back);
usb_flush_tx_fifo(pudev->usb_reg, USBD_AUDIO_FEEDBACK_EPT & 0x7F);
usbd_ept_send(pudev, USBD_AUDIO_FEEDBACK_EPT, audio_struct.audio_feed_back, len);
}
return status;
}
/**
* @brief usb device endpoint receive data
* @param udev: usb device core handler type
* @param ept_num: endpoint number
* @retval status of usb_sts_type
*/
usb_sts_type class_out_handler(void *udev, uint8_t ept_num)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
uint16_t g_rxlen;
/* get endpoint receive data length */
g_rxlen = usbd_get_recv_len(pudev, ept_num);
if((ept_num & 0x7F) == (USBD_AUDIO_SPK_OUT_EPT & 0x7F))
{
/* speaker data*/
audio_codec_spk_fifo_write(audio_struct.audio_spk_data, g_rxlen);
/* get next data */
usbd_ept_recv(pudev, USBD_AUDIO_SPK_OUT_EPT, audio_struct.audio_spk_data, AUDIO_SPK_OUT_MAXPACKET_SIZE);
}
return status;
}
/**
* @brief usb device sof handler
* @param udev: usb device core handler type
* @retval status of usb_sts_type
*/
usb_sts_type class_sof_handler(void *udev)
{
usb_sts_type status = USB_OK;
/* ...user code... */
return status;
}
/**
* @brief usb device event handler
* @param udev: usb device core handler type
* @param event: usb device event
* @retval status of usb_sts_type
*/
usb_sts_type class_event_handler(void *udev, usbd_event_type event)
{
usb_sts_type status = USB_OK;
switch(event)
{
case USBD_RESET_EVENT:
/* ...user code... */
break;
case USBD_SUSPEND_EVENT:
audio_struct.spk_alt_setting = 0;
audio_codec_spk_alt_setting(audio_struct.spk_alt_setting);
audio_struct.mic_alt_setting = 0;
audio_codec_mic_alt_setting(audio_struct.spk_alt_setting);
/* ...user code... */
break;
case USBD_WAKEUP_EVENT:
/* ...user code... */
break;
case USBD_INISOINCOM_EVENT:
audio_inisoincom_event(udev);
break;
default:
break;
}
return status;
}
/**
* @brief usb audio in iso incom event
* @param udev: usb device core handler type
* @retval none
*/
void audio_inisoincom_event(void *udev)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
uint32_t fnsof = OTG_DEVICE(pudev->usb_reg)->dsts_bit.soffn;
uint32_t epctl_fb = USB_INEPT(pudev->usb_reg, (USBD_AUDIO_FEEDBACK_EPT&0x7F))->diepctl_bit.dpid;
uint32_t epctl_in = USB_INEPT(pudev->usb_reg, (USBD_AUDIO_MIC_IN_EPT&0x7F))->diepctl_bit.dpid;
uint32_t len = 0;
if((fnsof & 0x1) == epctl_fb)
{
USB_INEPT(pudev->usb_reg, (USBD_AUDIO_FEEDBACK_EPT&0x7F))->diepctl_bit.eptdis = 1;
USB_INEPT(pudev->usb_reg, (USBD_AUDIO_FEEDBACK_EPT&0x7F))->diepctl_bit.snak = 1;
usb_flush_tx_fifo(pudev->usb_reg, USBD_AUDIO_FEEDBACK_EPT&0x7F);
usbd_ept_send(pudev, USBD_AUDIO_FEEDBACK_EPT, audio_struct.audio_feed_back, 3);
}
if((fnsof & 0x1) == epctl_in)
{
USB_INEPT(pudev->usb_reg, (USBD_AUDIO_MIC_IN_EPT&0x7F))->diepctl_bit.eptdis = 1;
USB_INEPT(pudev->usb_reg, (USBD_AUDIO_MIC_IN_EPT&0x7F))->diepctl_bit.snak = 1;
usb_flush_tx_fifo(pudev->usb_reg, USBD_AUDIO_MIC_IN_EPT&0x7F);
len = audio_codec_mic_get_data(audio_struct.audio_mic_data);
usbd_ept_send(pudev, USBD_AUDIO_MIC_IN_EPT, audio_struct.audio_mic_data, len);
}
}
/**
* @brief usb audio request get cur
* @param udev: usb device core handler type
* @param setup: setup class
* @retval none
*/
void audio_req_get_cur(void *udev, usb_setup_type *setup)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
if(HBYTE(setup->wIndex) == AUDIO_SPK_FEATURE_UNIT_ID)
{
if(HBYTE(setup->wValue) == AUDIO_MUTE_CONTROL)
{
usbd_ctrl_send(pudev, &audio_struct.spk_mute, setup->wLength);
}
else
{
usbd_ctrl_send(pudev, (uint8_t *)&audio_struct.spk_volume, setup->wLength);
}
}
else
{
if(HBYTE(setup->wValue) == AUDIO_MUTE_CONTROL)
{
usbd_ctrl_send(pudev, &audio_struct.mic_mute, setup->wLength);
}
else
{
usbd_ctrl_send(pudev, (uint8_t *)&audio_struct.mic_volume, setup->wLength);
}
}
}
/**
* @brief usb audio request set cur
* @param udev: usb device core handler type
* @param setup: setup class
* @retval none
*/
void audio_req_set_cur(void *udev, usb_setup_type *setup)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
if(setup->wLength > 0)
{
usbd_ctrl_recv(pudev, audio_struct.g_audio_cur, setup->wLength);
audio_struct.audio_cmd = AUDIO_REQ_SET_CUR;
audio_struct.audio_cmd_len = setup->wLength;
switch(setup->bmRequestType & AUDIO_REQ_CONTROL_MASK)
{
case AUDIO_REQ_CONTROL_INTERFACE:
audio_struct.interface = HBYTE(setup->wIndex);
if(HBYTE(setup->wValue) == AUDIO_MUTE_CONTROL)
{
audio_struct.request_no = AUDIO_MUTE_CONTROL;
}
else
{
audio_struct.request_no = AUDIO_VOLUME_CONTROL;
}
break;
case AUDIO_REQ_CONTROL_ENDPOINT:
audio_struct.enpd = setup->wIndex;
audio_struct.request_no = AUDIO_FREQ_SET_CONTROL;
break;
default:
break;
}
}
}
/**
* @brief usb audio request get min
* @param udev: usb device core handler type
* @param setup: setup class
* @retval none
*/
void audio_req_get_min(void *udev, usb_setup_type *setup)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
if(HBYTE(setup->wIndex) == AUDIO_SPK_FEATURE_UNIT_ID)
{
usbd_ctrl_send(pudev, (uint8_t *)&audio_struct.spk_volume_limits[0], setup->wLength);
}
else
{
usbd_ctrl_send(pudev, (uint8_t *)&audio_struct.mic_volume_limits[0], setup->wLength);
}
}
/**
* @brief usb audio request get max
* @param udev: usb device core handler type
* @param setup: setup class
* @retval none
*/
void audio_req_get_max(void *udev, usb_setup_type *setup)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
if(HBYTE(setup->wIndex) == AUDIO_SPK_FEATURE_UNIT_ID)
{
usbd_ctrl_send(pudev, (uint8_t *)&audio_struct.spk_volume_limits[1], setup->wLength);
}
else
{
usbd_ctrl_send(pudev, (uint8_t *)&audio_struct.mic_volume_limits[1], setup->wLength);
}
}
/**
* @brief usb audio request get res
* @param udev: usb device core handler type
* @param setup: setup class
* @retval none
*/
void audio_req_get_res(void *udev, usb_setup_type *setup)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
if(HBYTE(setup->wIndex) == AUDIO_SPK_FEATURE_UNIT_ID)
{
usbd_ctrl_send(pudev, (uint8_t *)&audio_struct.spk_volume_limits[2], setup->wLength);
}
else
{
usbd_ctrl_send(pudev, (uint8_t *)&audio_struct.mic_volume_limits[2], setup->wLength);
}
}
/**
* @brief usb audio set interface
* @param udev: usb device core handler type
* @param setup: setup class
* @retval none
*/
void audio_set_interface(void *udev, usb_setup_type *setup)
{
uint32_t len;
usbd_core_type *pudev = (usbd_core_type *)udev;
if(LBYTE(setup->wIndex) == AUDIO_SPK_INTERFACE_NUMBER)
{
audio_struct.spk_alt_setting = setup->wValue;
audio_codec_spk_alt_setting(audio_struct.spk_alt_setting);
if(audio_struct.spk_alt_setting )
{
usbd_ept_recv(pudev, USBD_AUDIO_SPK_OUT_EPT, audio_struct.audio_spk_data, AUDIO_SPK_OUT_MAXPACKET_SIZE);
usbd_ept_send(pudev, USBD_AUDIO_FEEDBACK_EPT, audio_struct.audio_feed_back, 3);
}
}
else if(LBYTE(setup->wIndex) == AUDIO_MIC_INTERFACE_NUMBER)
{
audio_struct.mic_alt_setting = setup->wValue;
audio_codec_mic_alt_setting(audio_struct.mic_alt_setting);
if(audio_struct.mic_alt_setting)
{
len = audio_codec_mic_get_data(audio_struct.audio_mic_data);
usbd_ept_send(pudev, USBD_AUDIO_MIC_IN_EPT, audio_struct.audio_mic_data, len);
}
}
}
/**
* @brief usb audio get interface
* @param udev: usb device core handler type
* @param setup: setup class
* @retval none
*/
void audio_get_interface(void *udev, usb_setup_type *setup)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
if(LBYTE(setup->wIndex) == AUDIO_SPK_INTERFACE_NUMBER)
{
usbd_ctrl_send(pudev, (uint8_t *)&audio_struct.spk_alt_setting, 1);
}
else if(LBYTE(setup->wIndex) == AUDIO_MIC_INTERFACE_NUMBER)
{
usbd_ctrl_send(pudev, (uint8_t *)&audio_struct.mic_alt_setting, 1);
}
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/

View File

@@ -0,0 +1,130 @@
/**
**************************************************************************
* @file audio_class.h
* @version v2.0.0
* @date 2021-11-26
* @brief usb audio class file
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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.
*
**************************************************************************
*/
/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __AUDIO_CLASS_H
#define __AUDIO_CLASS_H
#ifdef __cplusplus
extern "C" {
#endif
#include "usb_std.h"
#include "usbd_core.h"
#include "audio_conf.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @addtogroup USB_audio_class
* @{
*/
/** @defgroup USB_audio_class_definition
* @{
*/
/**
* @brief endpoint define
*/
#define USBD_AUDIO_MIC_IN_EPT 0x81
#define USBD_AUDIO_SPK_OUT_EPT 0x02
#define USBD_AUDIO_FEEDBACK_EPT 0x83
/**
* @brief endpoint support max size
*/
#define AUDIO_REMAIN_SIZE 8
#define AUDIO_MIC_IN_MAXPACKET_SIZE (AUDIO_SUPPORT_MAX_FREQ * AUDIO_MIC_CHANEL_NUM * (AUDIO_MIC_DEFAULT_BITW / 8) + AUDIO_REMAIN_SIZE)
#define AUDIO_SPK_OUT_MAXPACKET_SIZE (AUDIO_SUPPORT_MAX_FREQ * AUDIO_SPK_CHANEL_NUM * (AUDIO_SPK_DEFAULT_BITW / 8) + AUDIO_REMAIN_SIZE)
#define AUDIO_FEEDBACK_MAXPACKET_SIZE 0x3
/**
* @brief request type define
*/
#define AUDIO_REQ_CONTROL_INTERFACE 0x01
#define AUDIO_REQ_CONTROL_ENDPOINT 0x02
#define AUDIO_REQ_CONTROL_MASK 0x03
/**
* @brief audio set cur type define
*/
#define AUDIO_MUTE_CONTROL 0x01
#define AUDIO_VOLUME_CONTROL 0x02
#define AUDIO_FREQ_SET_CONTROL 0x03
/**
* @brief audio descriptor type
*/
#define AUDIO_DESCRIPTOR_TYPE 0x21
#define AUDIO_DESCRIPTOR_SIZE 0x09
/**
* @brief usb audio control struct
*/
typedef struct
{
uint8_t enpd;
uint8_t interface;
uint8_t request_no;
uint8_t spk_mute;
uint8_t mic_mute;
uint16_t spk_volume;
uint16_t mic_volume;
uint32_t spk_freq;
uint32_t mic_freq;
uint16_t spk_volume_limits[3]; /*[0] is mininum value, [1] is maxnum value, [2] is volume resolution */
uint16_t mic_volume_limits[3]; /*[0] is mininum value, [1] is maxnum value, [2] is volume resolution */
uint8_t audio_cmd;
uint32_t audio_cmd_len;
uint32_t spk_alt_setting;
uint32_t mic_alt_setting;
uint8_t g_audio_cur[64];
uint8_t audio_spk_data[AUDIO_SPK_OUT_MAXPACKET_SIZE];
uint8_t audio_mic_data[AUDIO_MIC_IN_MAXPACKET_SIZE];
uint8_t audio_feed_back[AUDIO_FEEDBACK_MAXPACKET_SIZE+1];
}usb_audio_type;
extern usbd_class_handler audio_class_handler;
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,90 @@
/**
**************************************************************************
* @file audio_conf.h
* @version v2.0.0
* @date 2021-11-26
* @brief usb audio config
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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.
*
**************************************************************************
*/
/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __AUDIO_CONF_H
#define __AUDIO_CONF_H
#ifdef __cplusplus
extern "C" {
#endif
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @addtogroup USB_audio_class
* @{
*/
/** @defgroup USB_device_audio_config_definition
* @{
*/
#define AUDIO_SUPPORT_SPK 1
#define AUDIO_SUPPORT_MIC 1
#define AUDIO_SUPPORT_FEEDBACK 0
#define AUDIO_SUPPORT_FREQ_16K 1
#define AUDIO_SUPPORT_FREQ_48K 1
#define AUDIO_SUPPORT_FREQ (AUDIO_SUPPORT_FREQ_16K + \
AUDIO_SUPPORT_FREQ_48K \
)
#define AUDIO_FREQ_16K 16000
#define AUDIO_FREQ_48K 48000
#define AUDIO_BITW_16 16
#define AUDIO_MIC_CHANEL_NUM 2
#define AUDIO_MIC_DEFAULT_BITW AUDIO_BITW_16
#define AUDIO_SPK_CHANEL_NUM 2
#define AUDIO_SPK_DEFAULT_BITW AUDIO_BITW_16
#define AUDIO_SUPPORT_MAX_FREQ 48
#define AUDIO_DEFAULT_FREQ AUDIO_FREQ_48K
#define AUDIO_DEFAULT_BITW AUDIO_BITW_16
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,621 @@
/**
**************************************************************************
* @file audio_desc.c
* @version v2.0.0
* @date 2021-11-26
* @brief usb audio device descriptor
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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 "audio_desc.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @defgroup USB_audio_desc
* @brief usb device audio descriptor
* @{
*/
/** @defgroup USB_audio_desc_private_functions
* @{
*/
usbd_desc_t *get_device_descriptor(void);
usbd_desc_t *get_device_qualifier(void);
usbd_desc_t *get_device_configuration(void);
usbd_desc_t *get_device_other_speed(void);
usbd_desc_t *get_device_lang_id(void);
usbd_desc_t *get_device_manufacturer_string(void);
usbd_desc_t *get_device_product_string(void);
usbd_desc_t *get_device_serial_string(void);
usbd_desc_t *get_device_interface_string(void);
usbd_desc_t *get_device_config_string(void);
uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf);
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 device descriptor handler structure
*/
usbd_desc_handler audio_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
*/
uint8_t g_usbd_descriptor[USB_DEVICE_DESC_LEN] =
{
USB_DEVICE_DESC_LEN, /* bLength */
USB_DESCIPTOR_TYPE_DEVICE, /* bDescriptorType */
0x00, /* bcdUSB */
0x02,
0x00, /* bDeviceClass */
0x00, /* bDeviceSubClass */
0x00, /* bDeviceProtocol */
USB_MAX_EP0_SIZE, /* bMaxPacketSize */
LBYTE(USBD_VENDOR_ID), /* idVendor */
HBYTE(USBD_VENDOR_ID), /* idVendor */
LBYTE(USBD_PRODUCT_ID), /* idProduct */
HBYTE(USBD_PRODUCT_ID), /* idProduct */
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 */
0x01 /* bNumConfigurations */
};
/**
* @brief usb configuration standard descriptor
*/
uint8_t g_usbd_configuration[USBD_CONFIG_DESC_SIZE] =
{
USB_DEVICE_CFG_DESC_LEN, /* bLength: configuration descriptor size */
USB_DESCIPTOR_TYPE_CONFIGURATION, /* bDescriptorType: configuration */
LBYTE(USBD_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
HBYTE(USBD_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
0x1 + AUDIO_INTERFACE_NUM, /* bNumInterfaces: n interface */
0x01, /* bConfigurationValue: configuration value */
0x00, /* iConfiguration: index of string descriptor describing
the configuration */
0xC0, /* bmAttributes: self powered */
0x32, /* MaxPower 100 mA: this current is used for detecting vbus */
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 */
0x00, /* bNumEndpoints: number of endpoints */
USB_CLASS_CODE_AUDIO, /* bInterfaceClass: audio class code */
AUDIO_SUBCLASS_AUDIOCONTROL, /* bInterfaceSubClass: audio control */
AUDIO_PROTOCOL_UNDEFINED, /* bInterfaceProtocol: undefined */
0x00, /* iInterface: index of string descriptor */
0x08+AUDIO_INTERFACE_NUM, /* bLength: size of this descriptor, in bytes 8+n */
AUDIO_CS_INTERFACE, /* bDescriptorType: cs interface descriptor type */
AUDIO_AC_HEADER, /* bDescriptorSubtype: Header function Descriptor*/
LBYTE(BCD_NUM),
HBYTE(BCD_NUM), /* bcdCDC: audio device class specification release number */
LBYTE(AUDIO_INTERFACE_LEN),
HBYTE(AUDIO_INTERFACE_LEN), /* wTotalLength: total number of bytes returned for the class-specific audio control interface */
AUDIO_INTERFACE_NUM, /* bInClollection: the number of audio streaming */
#if (AUDIO_INTERFACE_NUM == 2) /* two interface */
0x02,
0x01,
#else
0x01,
#endif
/* usb microphone config */
#if (AUDIO_SUPPORT_MIC == 1)
AUDIO_INPUT_TERMINAL_SIZE, /* bLength: descriptor size */
AUDIO_CS_INTERFACE, /* bDescriptorType: configuration */
AUDIO_AC_INPUT_TERMINAL, /* bDescriptorSubtype: input_terminal type*/
AUDIO_MIC_INPUT_TERMINAL_ID, /* bTerminalID: id of this input terminal*/
LBYTE(AUDIO_INPUT_TERMINAL_MICROPHONE),
HBYTE(AUDIO_INPUT_TERMINAL_MICROPHONE),/* wTerminalType: terminal is microphone */
0x00, /* bAssocTerminal: no association */
AUDIO_MIC_CHR, /* bNrChannels: two channel */
#if (AUDIO_MIC_CHR == 2)
0x03, /* wChannelConfig: left front and right front */
#endif
#if (AUDIO_MIC_CHR == 1)
0x00, /* wChannelConfig */
#endif
0x00, /* wChannelConfig */
0x00, /* iChannelNames: unused */
0x00, /* iTerminal: unused */
AUDIO_FEATURE_UNIT_SIZE, /* bLength: descriptor size */
AUDIO_CS_INTERFACE, /* bDescriptorType: configuration */
AUDIO_AC_FEATURE_UNIT, /* bDescriptorSubtype: feature unit type*/
AUDIO_MIC_FEATURE_UNIT_ID, /* bUnitID: id of this feature unit */
AUDIO_MIC_INPUT_TERMINAL_ID, /* bSourceID: from input terminal */
0x01, /* bControlSize: 1 byte */
0x01, /* bmaControls0: mute */
0x02, /* bmaControls1: volume */
0x00, /* iFeature: unused */
AUDIO_OUTPUT_TERMINAL_SIZE, /* bLength: descriptor size */
AUDIO_CS_INTERFACE, /* bDescriptorType: configuration */
AUDIO_AC_OUTPUT_TERMINAL, /* bDescriptorSubtype: output_terminal type*/
AUDIO_MIC_OUTPUT_TERMINAL_ID, /* bTerminalID: id of this output terminal*/
LBYTE(AUDIO_TERMINAL_TYPE_STREAMING),
HBYTE(AUDIO_TERMINAL_TYPE_STREAMING), /* wTerminalType: usb streaming */
0x00, /* bAssocTerminal: unused */
AUDIO_MIC_FEATURE_UNIT_ID, /* bSourceID: from feature unit terminal */
0x00, /* iTerminal: unused */
#endif
#if (AUDIO_SUPPORT_SPK == 1)
/* speaker config */
AUDIO_INPUT_TERMINAL_SIZE, /* bLength: descriptor size */
AUDIO_CS_INTERFACE, /* bDescriptorType: configuration */
AUDIO_AC_INPUT_TERMINAL, /* bDescriptorSubtype: input_terminal type*/
AUDIO_SPK_INPUT_TERMINAL_ID, /* bTerminalID: id of this input terminal*/
LBYTE(AUDIO_TERMINAL_TYPE_STREAMING), /* wTerminalType: usb streaming */
HBYTE(AUDIO_TERMINAL_TYPE_STREAMING), /* wTerminalType: usb streaming */
0x00, /* bAssocTerminal: no association */
AUDIO_SPK_CHR, /* bNrChannels: two channel */
#if (AUDIO_SPK_CHR == 2)
0x03, /* wChannelConfig: left front and right front */
#endif
#if (AUDIO_SPK_CHR == 1)
0x00, /* wChannelConfig */
#endif
0x00, /* wChannelConfig */
0x00, /* iChannelNames: unused */
0x00, /* iTerminal: unused */
AUDIO_FEATURE_UNIT_SIZE, /* bLength: descriptor size */
AUDIO_CS_INTERFACE, /* bDescriptorType: configuration */
AUDIO_AC_FEATURE_UNIT, /* bDescriptorSubtype: feature unit type*/
AUDIO_SPK_FEATURE_UNIT_ID, /* bUnitID: id of this feature unit */
AUDIO_SPK_INPUT_TERMINAL_ID, /* bSourceID: from input terminal */
0x01, /* bControlSize: 1 byte */
0x01, /* bmaControls0: mute*/
0x02, /* bmaControls1: volume */
0x00, /* iFeature: unused */
AUDIO_OUTPUT_TERMINAL_SIZE, /* bLength: descriptor size */
AUDIO_CS_INTERFACE, /* bDescriptorType: configuration */
AUDIO_AC_OUTPUT_TERMINAL, /* bDescriptorSubtype: output_terminal type*/
AUDIO_SPK_OUTPUT_TERMINAL_ID, /* bTerminalID: id of this output terminal*/
LBYTE(AUDIO_OUTPUT_TERMINAL_SPEAKER), /* wTerminalType: usb speaker */
HBYTE(AUDIO_OUTPUT_TERMINAL_SPEAKER), /* wTerminalType: usb speaker */
0x00, /* bAssocTerminal: unused */
AUDIO_SPK_FEATURE_UNIT_ID, /* bSourceID: from feature unit terminal */
0x00, /* iTerminal: unused */
#endif
#if (AUDIO_SUPPORT_MIC == 1)
/* microphone interface */
0x09, /* bLength: descriptor size */
USB_DESCIPTOR_TYPE_INTERFACE, /* bDescriptorType: interface descriptor type */
AUDIO_MIC_INTERFACE_NUMBER, /* bInterfaceNumber: index of this interface */
0x00, /* bAlternateSetting: index of this setting */
0x00, /* bNumEndpoints: 0 endpoints */
USB_CLASS_CODE_AUDIO, /* bInterfaceClass: audio */
AUDIO_SUBCLASS_AUDIOSTREAMING, /* bInterfaceSubclass: audio streaming */
0x00, /* bInterfaceProtocol: unused */
0x00, /* iInterface: unused */
0x09, /* bLength: descriptor size */
USB_DESCIPTOR_TYPE_INTERFACE, /* bDescriptorType: interface descriptor type */
AUDIO_MIC_INTERFACE_NUMBER, /* bInterfaceNumber: index of this interface */
0x01, /* bAlternateSetting: index of this setting */
0x01, /* bNumEndpoints: 1 endpoints */
USB_CLASS_CODE_AUDIO, /* bInterfaceClass: audio */
AUDIO_SUBCLASS_AUDIOSTREAMING, /* bInterfaceSubclass: audio streaming */
0x00, /* bInterfaceProtocol: unused */
0x00, /* iInterface: unused */
0x07, /* bLength: configuration descriptor size */
AUDIO_CS_INTERFACE, /* bDescriptorType: interface descriptor type */
AUDIO_AS_GENERAL, /* bDescriptorSubtype: general sub type*/
AUDIO_MIC_OUTPUT_TERMINAL_ID, /* bTerminalLink: unit id of the output terminal */
0x01, /* bDelay: interface delay */
0x01, /* wFormatTag: pcm format*/
0x00, /* wFormatTag: pcm format*/
0x08 + AUDIO_MIC_FREQ_SIZE * 3, /* bLength: descriptor size */
AUDIO_CS_INTERFACE, /* bDescriptorType: interface descriptor type */
AUDIO_AS_FORMAT_TYPE, /* bDescriptorSubtype: format subtype */
AUDIO_FORMAT_TYPE_I, /* bFormatType: format type 1 */
AUDIO_MIC_CHR, /* bNrChannels: channel number */
AUDIO_MIC_BITW / 8, /* bSubFrameSize: per audio subframe */
AUDIO_MIC_BITW, /* bBitResolution: n bits per sample */
AUDIO_MIC_FREQ_SIZE, /* bSamFreqType: n frequency supported */
#if (AUDIO_SUPPORT_FREQ_16K == 1)
SAMPLE_FREQ(AT32_AUDIO_FREQ_16K), /* tSamFreq: 16000hz */
#endif
#if (AUDIO_SUPPORT_FREQ_48K == 1)
SAMPLE_FREQ(AT32_AUDIO_FREQ_48K), /* tSamFreq: 48000hz */
#endif
0x09, /* bLength: size of endpoint descriptor in bytes */
USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
USBD_AUDIO_MIC_IN_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
USB_EPT_DESC_ISO | USB_ETP_DESC_ASYNC, /* bmAttributes: endpoint attributes */
LBYTE(AUDIO_MIC_IN_MAXPACKET_SIZE),
HBYTE(AUDIO_MIC_IN_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
HID_BINTERVAL_TIME, /* bInterval: interval for polling endpoint for data transfers */
0x00, /* bRefresh: unused */
0x00, /* bSynchAddress: unused */
0x07, /* bLength: size of endpoint descriptor in bytes */
AUDIO_CS_ENDPOINT, /* bDescriptorType: cs endpoint descriptor type */
0x01, /* bDescriptorSubtype: general subtype */
0x01, /* bmAttributes */
0x00, /* bLockDelayUnits: unused */
0x00, /* wLockDelay: unused */
0x00, /* wLockDelay: unused */
#endif
#if (AUDIO_SUPPORT_SPK == 1)
/* speaker interface */
0x09, /* bLength: descriptor size */
USB_DESCIPTOR_TYPE_INTERFACE, /* bDescriptorType: interface descriptor type */
AUDIO_SPK_INTERFACE_NUMBER, /* bInterfaceNumber: index of this interface */
0x00, /* bAlternateSetting: index of this setting */
0x00, /* bNumEndpoints: 0 endpoints */
USB_CLASS_CODE_AUDIO, /* bInterfaceClass: audio */
AUDIO_SUBCLASS_AUDIOSTREAMING, /* bInterfaceSubclass: audio streaming */
0x00, /* bInterfaceProtocol: unused */
0x00, /* iInterface: unused */
0x09, /* bLength: descriptor size */
USB_DESCIPTOR_TYPE_INTERFACE, /* bDescriptorType: interface descriptor type */
AUDIO_SPK_INTERFACE_NUMBER, /* bInterfaceNumber: index of this interface */
0x01, /* bAlternateSetting: index of this setting */
0x01 + AUDIO_SUPPORT_FEEDBACK, /* bNumEndpoints: endpoints */
USB_CLASS_CODE_AUDIO, /* bInterfaceClass: audio */
AUDIO_SUBCLASS_AUDIOSTREAMING, /* bInterfaceSubclass: audio streaming */
0x00, /* bInterfaceProtocol: unused */
0x00, /* iInterface: unused */
0x07, /* bLength: configuration descriptor size */
AUDIO_CS_INTERFACE, /* bDescriptorType: interface descriptor type */
AUDIO_AS_GENERAL, /* bDescriptorSubtype: general sub type*/
AUDIO_SPK_INPUT_TERMINAL_ID, /* bTerminalLink: unit id of the input terminal */
0x01, /* bDelay: interface delay */
0x01, /* wFormatTag: pcm format*/
0x00, /* wFormatTag: pcm format*/
0x08 + AUDIO_SPK_FREQ_SIZE * 3, /* bLength: descriptor size */
AUDIO_CS_INTERFACE, /* bDescriptorType: interface descriptor type */
AUDIO_AS_FORMAT_TYPE, /* bDescriptorSubtype: format subtype */
AUDIO_FORMAT_TYPE_I, /* bFormatType: format type 1 */
AUDIO_SPK_CHR, /* bNrChannels: channel number */
AUDIO_SPK_BITW / 8, /* bSubFrameSize: per audio subframe */
AUDIO_SPK_BITW, /* bBitResolution: n bits per sample */
AUDIO_SPK_FREQ_SIZE, /* bSamFreqType: n frequency supported */
#if (AUDIO_SUPPORT_FREQ_16K == 1)
SAMPLE_FREQ(AT32_AUDIO_FREQ_16K), /* tSamFreq: 16000hz */
#endif
#if (AUDIO_SUPPORT_FREQ_48K == 1)
SAMPLE_FREQ(AT32_AUDIO_FREQ_48K), /* tSamFreq: 48000hz */
#endif
0x09, /* bLength: size of endpoint descriptor in bytes */
USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
USBD_AUDIO_SPK_OUT_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
USB_EPT_DESC_ISO | USB_ETP_DESC_ASYNC, /* bmAttributes: endpoint attributes */
LBYTE(AUDIO_SPK_OUT_MAXPACKET_SIZE),
HBYTE(AUDIO_SPK_OUT_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
HID_BINTERVAL_TIME, /* bInterval: interval for polling endpoint for data transfers */
0x00, /* bRefresh: unused */
#if (AUDIO_SUPPORT_FEEDBACK == 1)
USBD_AUDIO_FEEDBACK_EPT, /* bSynchAddress: feedback endpoint */
#else
0x00, /* bSynchAddress: unused */
#endif
0x07, /* bLength: size of endpoint descriptor in bytes */
AUDIO_CS_ENDPOINT, /* bDescriptorType: cs endpoint descriptor type */
0x01, /* bDescriptorSubtype: general subtype */
0x01, /* bmAttributes */
0x00, /* bLockDelayUnits: unused */
0x00, /* wLockDelay: unused */
0x00, /* wLockDelay: unused */
#if (AUDIO_SUPPORT_FEEDBACK == 1)
0x09, /* bLength: size of endpoint descriptor in bytes */
USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
USBD_AUDIO_FEEDBACK_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
0x11, /* bmAttributes: endpoint attributes */
LBYTE(AUDIO_FEEDBACK_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
HBYTE(AUDIO_FEEDBACK_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
HID_BINTERVAL_TIME, /* bInterval: interval for polling endpoint for data transfers */
0x08, /* bRefresh: this field indicates the rate at which an iso syncronization
pipe provides new syncronization feedback data. this rate must be a power of
2, therefore only the power is reported back and the range of this field is from
1(2ms) to 9(512ms) */
0x00 /* bSynchAddress: 0x00*/
#endif
#endif
};
/**
* @brief usb string lang id
*/
uint8_t g_string_lang_id[USBD_SIZ_STRING_LANGID] =
{
USBD_SIZ_STRING_LANGID,
USB_DESCIPTOR_TYPE_STRING,
0x09,
0x04,
};
/**
* @brief usb string serial
*/
uint8_t g_string_serial[USBD_SIZ_STRING_SERIAL] =
{
USBD_SIZ_STRING_SERIAL,
USB_DESCIPTOR_TYPE_STRING,
};
/* device descriptor */
usbd_desc_t device_descriptor =
{
USB_DEVICE_DESC_LEN,
g_usbd_descriptor
};
/* config descriptor */
usbd_desc_t config_descriptor =
{
USBD_CONFIG_DESC_SIZE,
g_usbd_configuration
};
/* langid descriptor */
usbd_desc_t langid_descriptor =
{
USBD_SIZ_STRING_LANGID,
g_string_lang_id
};
/* serial descriptor */
usbd_desc_t serial_descriptor =
{
USBD_SIZ_STRING_SERIAL,
g_string_serial
};
usbd_desc_t vp_desc;
/**
* @brief standard usb unicode convert
* @param string: source string
* @param unicode_buf: unicode buffer
* @retval length
*/
uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf)
{
uint16_t str_len = 0, id_pos = 2;
uint8_t *tmp_str = string;
while(*tmp_str != '\0')
{
str_len ++;
unicode_buf[id_pos ++] = *tmp_str ++;
unicode_buf[id_pos ++] = 0x00;
}
str_len = str_len * 2 + 2;
unicode_buf[0] = str_len;
unicode_buf[1] = USB_DESCIPTOR_TYPE_STRING;
return str_len;
}
/**
* @brief usb int convert to unicode
* @param value: int value
* @param pbus: unicode buffer
* @param len: length
* @retval none
*/
static void usbd_int_to_unicode (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;
}
}
/**
* @brief usb get serial number
* @param none
* @retval none
*/
static void get_serial_num(void)
{
uint32_t serial0, serial1, serial2;
serial0 = *(uint32_t*)MCU_ID1;
serial1 = *(uint32_t*)MCU_ID2;
serial2 = *(uint32_t*)MCU_ID3;
serial0 += serial2;
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
* @retval usbd_desc
*/
usbd_desc_t *get_device_descriptor(void)
{
return &device_descriptor;
}
/**
* @brief get device qualifier
* @param none
* @retval usbd_desc
*/
usbd_desc_t * get_device_qualifier(void)
{
return NULL;
}
/**
* @brief get config descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_configuration(void)
{
return &config_descriptor;
}
/**
* @brief get other speed descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_other_speed(void)
{
return NULL;
}
/**
* @brief get lang id descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_lang_id(void)
{
return &langid_descriptor;
}
/**
* @brief get manufacturer descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_manufacturer_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_MANUFACTURER_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get product descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_product_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_PRODUCT_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get serial descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_serial_string(void)
{
get_serial_num();
return &serial_descriptor;
}
/**
* @brief get interface descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_interface_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_INTERFACE_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get device config descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_config_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_CONFIGURATION_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/

View File

@@ -0,0 +1,239 @@
/**
**************************************************************************
* @file audio_desc.h
* @version v2.0.0
* @date 2021-11-26
* @brief usb audio descriptor header file
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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.
*
**************************************************************************
*/
/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __AUDIO_DESC_H
#define __AUDIO_DESC_H
#ifdef __cplusplus
extern "C" {
#endif
#include "audio_class.h"
#include "usbd_core.h"
#include "audio_conf.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @addtogroup USB_audio_desc
* @{
*/
/** @defgroup USB_audio_desc_definition
* @{
*/
#define BCD_NUM 0x0100
#define USBD_VENDOR_ID 0x2E3C
#define USBD_PRODUCT_ID 0x5730
#define USBD_SIZ_STRING_LANGID 4
#define USBD_SIZ_STRING_SERIAL 0x1A
#define USBD_DESC_MANUFACTURER_STRING "Artery"
#define USBD_DESC_PRODUCT_STRING "AT32 Audio"
#define USBD_DESC_CONFIGURATION_STRING "Audio Config"
#define USBD_DESC_INTERFACE_STRING "Audio Interface"
/**
* @brief audio interface subclass codes
*/
#define AUDIO_SUBCLASS_UNDEFINED 0x00
#define AUDIO_SUBCLASS_AUDIOCONTROL 0x01
#define AUDIO_SUBCLASS_AUDIOSTREAMING 0x02
#define AUDIO_SUBCLASS_MIDISTREMING 0x03
/**
* @brief audio class-specific descriptor types
*/
#define AUDIO_CS_INTERFACE 0x24
#define AUDIO_CS_ENDPOINT 0x25
#define AUDIO_CS_STRING 0x23
#define AUDIO_CS_CONFIGURATION 0x22
#define AUDIO_CS_DEVICE 0x21
#define AUDIO_CS_UNDEFINED 0x20
/**
* @brief audio interface protocol codes
*/
#define AUDIO_PROTOCOL_UNDEFINED 0x00
/**
* @brief audio class-specific ac interface descriptor subtypes
*/
#define AUDIO_AC_DESCRIPTOR_UNDEFINED 0x00
#define AUDIO_AC_HEADER 0x01
#define AUDIO_AC_INPUT_TERMINAL 0x02
#define AUDIO_AC_OUTPUT_TERMINAL 0x03
#define AUDIO_AC_MIXER_UNIT 0x04
#define AUDIO_AC_SELECTOR_UNIT 0x05
#define AUDIO_AC_FEATURE_UNIT 0x06
#define AUDIO_AC_PROCESSING_UNIT 0x07
#define AUDIO_AC_EXTENSION_UNIT 0x08
/**
* @brief audio class-specific as interface descriptor subtypes
*/
#define AUDIO_AS_DESCRIPTOR_UNDEFINED 0x00
#define AUDIO_AS_GENERAL 0x01
#define AUDIO_AS_FORMAT_TYPE 0x02
#define AUDIO_AS_FORMAT_SPECIFIC 0x03
/**
* @brief audio class-specific request codes
*/
#define AUDIO_REQUEST_CODE_UNDEFINED 0x00
#define AUDIO_REQ_SET_CUR 0x01
#define AUDIO_REQ_GET_CUR 0x81
#define AUDIO_REQ_SET_MIN 0x02
#define AUDIO_REQ_GET_MIN 0x82
#define AUDIO_REQ_SET_MAX 0x03
#define AUDIO_REQ_GET_MAX 0x83
#define AUDIO_REQ_SET_RES 0x04
#define AUDIO_REQ_GET_RES 0x84
#define AUDIO_REQ_SET_MEM 0x05
#define AUDIO_REQ_GET_MEM 0x85
#define AUDIO_REQ_GET_STAT 0xFF
/**
* @brief audio feature unit control selectors
*/
#define AUDIO_FU_CONTROL_UNDEFINED 0x00
#define AUDIO_FU_MUTE_CONTROL 0x01
#define AUDIO_FU_VOLUME_CONTROL 0x02
#define AUDIO_FU_BASS_CONTROL 0x03
#define AUDIO_FU_MID_CONTROL 0x04
#define AUDIO_FU_TREBLE_CONTROL 0x05
/**
* @brief audio terminal type
*/
#define AUDIO_TERMINAL_TYPE_UNDEFINED 0x0100
#define AUDIO_TERMINAL_TYPE_STREAMING 0x0101
#define AUDIO_TERMINAL_TYPE_VENDOR 0x01FF
#define AUDIO_INPUT_TERMINAL_UNDEFINED 0x0200
#define AUDIO_INPUT_TERMINAL_MICROPHONE 0x0201
#define AUDIO_OUTPUT_TERMINAL_UNDEFINED 0x0300
#define AUDIO_OUTPUT_TERMINAL_SPEAKER 0x0301
/**
* @brief audio format type 1
*/
#define AUDIO_FORMAT_TYPE_I 0x01
/**
* @brief audio interface config
*/
#define AUDIO_INTERFACE_NUM (AUDIO_SUPPORT_SPK + AUDIO_SUPPORT_MIC)
#define AUDIO_INTERFACE_LEN ((0x08 + AUDIO_INTERFACE_NUM) + AUDIO_INTERFACE_NUM * 0x1E)
#define AUDIO_MIC_INTERFACE 0x01
#define AUDIO_SPK_INTERFACE 0x02
/**
* @brief audio interface descriptor size define
*/
#define AUDIO_INPUT_TERMINAL_SIZE 0x0C
#define AUDIO_OUTPUT_TERMINAL_SIZE 0x09
#define AUDIO_FEATURE_UNIT_SIZE 0x09
/**
* @brief audio terminal id define
*/
#define AUDIO_MIC_INPUT_TERMINAL_ID 0x01
#define AUDIO_MIC_FEATURE_UNIT_ID 0x02
#define AUDIO_MIC_OUTPUT_TERMINAL_ID 0x03
#define AUDIO_SPK_INPUT_TERMINAL_ID 0x04
#define AUDIO_SPK_FEATURE_UNIT_ID 0x05
#define AUDIO_SPK_OUTPUT_TERMINAL_ID 0x06
/**
* @brief audio interface number
*/
#define AUDIO_MIC_INTERFACE_NUMBER 0x01
#if (AUDIO_SUPPORT_MIC == 1)
#define AUDIO_SPK_INTERFACE_NUMBER 0x02
#else
#define AUDIO_SPK_INTERFACE_NUMBER 0x01
#endif
/**
* @brief audio support freq
*/
#define AT32_AUDIO_FREQ_16K 16000
#define AT32_AUDIO_FREQ_48K 48000
/**
* @brief audio microphone freq and channel config
*/
#define AUDIO_MIC_FREQ_SIZE (AUDIO_SUPPORT_FREQ)
#define AUDIO_MIC_CHR AUDIO_MIC_CHANEL_NUM
#define AUDIO_MIC_BITW (AUDIO_MIC_DEFAULT_BITW)
/**
* @brief audio speaker freq and channel config
*/
#define AUDIO_SPK_FREQ_SIZE (AUDIO_SUPPORT_FREQ)
#define AUDIO_SPK_CHR AUDIO_SPK_CHANEL_NUM
#define AUDIO_SPK_BITW (AUDIO_SPK_DEFAULT_BITW)
#define HID_BINTERVAL_TIME 0x01
#define USBD_CONFIG_DESC_SIZE ( 0x12 + AUDIO_INTERFACE_LEN + \
+ (0x31 + AUDIO_SPK_FREQ_SIZE * 3) \
+ (0x31 + AUDIO_MIC_FREQ_SIZE * 3) \
+ (9 * AUDIO_SUPPORT_FEEDBACK) \
)
#define SAMPLE_FREQ(frq) (uint8_t)(frq), (uint8_t)((frq >> 8)), (uint8_t)((frq >> 16))
#define MCU_ID1 (0x1FFFF7E8)
#define MCU_ID2 (0x1FFFF7EC)
#define MCU_ID3 (0x1FFFF7F0)
extern uint8_t g_usbd_descriptor[USB_DEVICE_DESC_LEN];
extern uint8_t g_usbd_configuration[USBD_CONFIG_DESC_SIZE];
extern usbd_desc_handler audio_desc_handler;
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,90 @@
/**
**************************************************************************
* @file audio_conf.h
* @version v2.0.0
* @date 2021-11-26
* @brief usb audio config
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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.
*
**************************************************************************
*/
/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __AUDIO_CONF_H
#define __AUDIO_CONF_H
#ifdef __cplusplus
extern "C" {
#endif
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @addtogroup USB_audio_hid_class
* @{
*/
/** @defgroup USB_device_audio_hid_config_definition
* @{
*/
#define AUDIO_SUPPORT_SPK 1
#define AUDIO_SUPPORT_MIC 1
#define AUDIO_SUPPORT_FEEDBACK 1
#define AUDIO_SUPPORT_FREQ_16K 1
#define AUDIO_SUPPORT_FREQ_48K 1
#define AUDIO_SUPPORT_FREQ (AUDIO_SUPPORT_FREQ_16K + \
AUDIO_SUPPORT_FREQ_48K \
)
#define AUDIO_FREQ_16K 16000
#define AUDIO_FREQ_48K 48000
#define AUDIO_BITW_16 16
#define AUDIO_MIC_CHANEL_NUM 2
#define AUDIO_MIC_DEFAULT_BITW AUDIO_BITW_16
#define AUDIO_SPK_CHANEL_NUM 2
#define AUDIO_SPK_DEFAULT_BITW AUDIO_BITW_16
#define AUDIO_SUPPORT_MAX_FREQ 48
#define AUDIO_DEFAULT_FREQ AUDIO_FREQ_48K
#define AUDIO_DEFAULT_BITW AUDIO_BITW_16
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,864 @@
/**
**************************************************************************
* @file audio_class.c
* @version v2.0.0
* @date 2021-11-26
* @brief usb audio class type
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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 "usbd_core.h"
#include "audio_hid_class.h"
#include "audio_hid_desc.h"
#include "audio_codec.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @defgroup USB_audio_hid_class
* @brief usb device class audio hid demo
* @{
*/
/** @defgroup USB_audio_hid_class_private_functions
* @{
*/
usb_sts_type class_init_handler(void *udev);
usb_sts_type class_clear_handler(void *udev);
usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup);
usb_sts_type class_ept0_tx_handler(void *udev);
usb_sts_type class_ept0_rx_handler(void *udev);
usb_sts_type class_in_handler(void *udev, uint8_t ept_num);
usb_sts_type class_out_handler(void *udev, uint8_t ept_num);
usb_sts_type class_sof_handler(void *udev);
usb_sts_type class_event_handler(void *udev, usbd_event_type event);
void audio_inisoincom_event(void *udev);
void audio_req_get_cur(void *udev, usb_setup_type *setup);
void audio_req_set_cur(void *udev, usb_setup_type *setup);
void audio_req_get_min(void *udev, usb_setup_type *setup);
void audio_req_get_max(void *udev, usb_setup_type *setup);
void audio_req_get_res(void *udev, usb_setup_type *setup);
void audio_get_interface(void *udev, usb_setup_type *setup);
void audio_set_interface(void *udev, usb_setup_type *setup);
void usb_hid_buf_process(void *udev, uint8_t *report, uint16_t len);
/* usb hid rx and tx buffer */
static uint8_t g_rxhid_buff[USBD_OUT_MAXPACKET_SIZE];
static uint8_t g_txhid_buff[USBD_IN_MAXPACKET_SIZE];
usb_audio_type audio_struct = {0, 0, 0, 0, 0, 0x1400, 0, 0, 0, {0x0000, 0x1400, 0x33}, {0x0000, 0x1400, 0x33}};
/* custom hid static variable */
static uint32_t hid_protocol = 0;
static uint32_t hid_set_idle = 0;
static uint32_t alt_setting = 0;
static uint8_t hid_state;
uint8_t hid_set_report[64];
/* usb device class handler */
usbd_class_handler audio_hid_class_handler =
{
class_init_handler,
class_clear_handler,
class_setup_handler,
class_ept0_tx_handler,
class_ept0_rx_handler,
class_in_handler,
class_out_handler,
class_sof_handler,
class_event_handler,
};
/**
* @brief initialize usb custom hid endpoint
* @param udev: usb device core handler type
* @retval status of usb_sts_type
*/
usb_sts_type class_init_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
/* open microphone in endpoint */
usbd_ept_open(pudev, USBD_AUDIO_MIC_IN_EPT, EPT_ISO_TYPE, AUDIO_MIC_IN_MAXPACKET_SIZE);
/* open speaker out endpoint */
usbd_ept_open(pudev, USBD_AUDIO_SPK_OUT_EPT, EPT_ISO_TYPE, AUDIO_SPK_OUT_MAXPACKET_SIZE);
#if AUDIO_SUPPORT_FEEDBACK
/* open speaker feedback endpoint */
usbd_ept_open(pudev, USBD_AUDIO_FEEDBACK_EPT, EPT_ISO_TYPE, AUDIO_FEEDBACK_MAXPACKET_SIZE);
#endif
/* start receive speaker out data */
usbd_ept_recv(pudev, USBD_AUDIO_SPK_OUT_EPT, audio_struct.audio_spk_data, AUDIO_SPK_OUT_MAXPACKET_SIZE);
/*open hid endpoint */
/* open custom hid in endpoint */
usbd_ept_open(pudev, USBD_HID_IN_EPT, EPT_INT_TYPE, USBD_IN_MAXPACKET_SIZE);
/* open custom hid out endpoint */
usbd_ept_open(pudev, USBD_HID_OUT_EPT, EPT_INT_TYPE, USBD_OUT_MAXPACKET_SIZE);
/* set out endpoint to receive status */
usbd_ept_recv(pudev, USBD_HID_OUT_EPT, g_rxhid_buff, USBD_OUT_MAXPACKET_SIZE);
return status;
}
/**
* @brief clear endpoint or other state
* @param udev: usb device core handler type
* @retval status of usb_sts_type
*/
usb_sts_type class_clear_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
/* close in endpoint */
usbd_ept_close(pudev, USBD_AUDIO_MIC_IN_EPT);
/* close in endpoint */
usbd_ept_close(pudev, USBD_AUDIO_FEEDBACK_EPT);
/* close out endpoint */
usbd_ept_close(pudev, USBD_AUDIO_SPK_OUT_EPT);
/* close custom hid in endpoint */
usbd_ept_close(pudev, USBD_HID_IN_EPT);
/* close custom hid out endpoint */
usbd_ept_close(pudev, USBD_HID_OUT_EPT);
return status;
}
/**
* @brief usb device class setup request handler
* @param udev: usb device core handler type
* @param setup: setup packet
* @retval status of usb_sts_type
*/
usb_sts_type class_audio_setup_handler(void *udev, usb_setup_type *setup)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
uint16_t len;
switch(setup->bmRequestType & USB_REQ_TYPE_RESERVED)
{
/* class request */
case USB_REQ_TYPE_CLASS:
switch(setup->bRequest)
{
case AUDIO_REQ_GET_CUR:
audio_req_get_cur(pudev, setup);
break;
case AUDIO_REQ_SET_CUR:
audio_req_set_cur(pudev, setup);
break;
case AUDIO_REQ_GET_MIN:
audio_req_get_min(pudev, setup);
break;
case AUDIO_REQ_GET_MAX:
audio_req_get_max(pudev, setup);
break;
case AUDIO_REQ_GET_RES:
audio_req_get_res(pudev, setup);
break;
default:
usbd_ctrl_unsupport(pudev);
break;
}
break;
/* standard request */
case USB_REQ_TYPE_STANDARD:
switch(setup->bRequest)
{
case USB_STD_REQ_GET_DESCRIPTOR:
if((setup->wValue >> 8) == AUDIO_DESCRIPTOR_TYPE)
{
len = MIN(AUDIO_DESCRIPTOR_SIZE, setup->wLength);
usbd_ctrl_send(pudev, g_usbd_configuration+18, len);
}
else
{
usbd_ctrl_unsupport(pudev);
}
break;
case USB_STD_REQ_GET_INTERFACE:
audio_get_interface(udev, setup);
break;
case USB_STD_REQ_SET_INTERFACE:
audio_set_interface(udev, setup);
usbd_ctrl_send_status(pudev);
break;
default:
break;
}
break;
default:
usbd_ctrl_unsupport(pudev);
break;
}
return status;
}
/**
* @brief usb device class setup request handler
* @param udev: usb device core handler type
* @param setup: setup packet
* @retval status of usb_sts_type
*/
usb_sts_type class_hid_setup_handler(void *udev, usb_setup_type *setup)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
uint16_t len;
uint8_t *buf;
switch(setup->bmRequestType & USB_REQ_TYPE_RESERVED)
{
/* class request */
case USB_REQ_TYPE_CLASS:
switch(setup->bRequest)
{
case HID_REQ_SET_PROTOCOL:
hid_protocol = (uint8_t)setup->wValue;
break;
case HID_REQ_GET_PROTOCOL:
usbd_ctrl_send(pudev, (uint8_t *)&hid_protocol, 1);
break;
case HID_REQ_SET_IDLE:
hid_set_idle = (uint8_t)(setup->wValue >> 8);
break;
case HID_REQ_GET_IDLE:
usbd_ctrl_send(pudev, (uint8_t *)&hid_set_idle, 1);
break;
case HID_REQ_SET_REPORT:
hid_state = HID_REQ_SET_REPORT;
usbd_ctrl_recv(pudev, hid_set_report, setup->wLength);
break;
default:
usbd_ctrl_unsupport(pudev);
break;
}
break;
/* standard request */
case USB_REQ_TYPE_STANDARD:
switch(setup->bRequest)
{
case USB_STD_REQ_GET_DESCRIPTOR:
if(setup->wValue >> 8 == HID_REPORT_DESC)
{
len = MIN(USBD_HID_SIZ_REPORT_DESC, setup->wLength);
buf = (uint8_t *)g_usbd_hid_report;
}
else if(setup->wValue >> 8 == HID_DESCRIPTOR_TYPE)
{
len = MIN(9, setup->wLength);
buf = (uint8_t *)g_hid_usb_desc;
}
usbd_ctrl_send(pudev, (uint8_t *)buf, len);
break;
case USB_STD_REQ_GET_INTERFACE:
usbd_ctrl_send(pudev, (uint8_t *)&alt_setting, 1);
break;
case USB_STD_REQ_SET_INTERFACE:
alt_setting = setup->wValue;
break;
default:
break;
}
break;
default:
usbd_ctrl_unsupport(pudev);
break;
}
return status;
}
/**
* @brief usb device class setup request handler
* @param udev: usb device core handler type
* @param setup: setup packet
* @retval status of usb_sts_type
*/
usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
switch(setup->bmRequestType & USB_REQ_RECIPIENT_MASK)
{
case USB_REQ_RECIPIENT_INTERFACE:
if(setup->wIndex == HID_INTERFACE_NUMBER)
{
class_hid_setup_handler(udev, setup);
}
else
{
class_audio_setup_handler(pudev, setup);
}
break;
case USB_REQ_RECIPIENT_ENDPOINT:
class_audio_setup_handler(pudev, setup);
break;
}
return status;
}
/**
* @brief usb device endpoint 0 in status stage complete
* @param udev: usb device core handler type
* @retval status of usb_sts_type
*/
usb_sts_type class_ept0_tx_handler(void *udev)
{
usb_sts_type status = USB_OK;
/* ...user code... */
return status;
}
/**
* @brief usb device endpoint 0 out status stage complete
* @param udev: usb device core handler type
* @retval status of usb_sts_type
*/
usb_sts_type class_ept0_rx_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
usb_setup_type *setup = &pudev->setup;
uint32_t recv_len = usbd_get_recv_len(pudev, 0);
/* ...user code... */
if(setup->wIndex == HID_INTERFACE_NUMBER)
{
if( hid_state == HID_REQ_SET_REPORT)
{
/* hid buffer process */
usb_hid_buf_process(udev, hid_set_report, recv_len);
hid_state = 0;
}
}
else
{
/* ...user code... */
if( audio_struct.audio_cmd == AUDIO_REQ_SET_CUR)
{
/* class process */
switch(audio_struct.request_no)
{
case AUDIO_VOLUME_CONTROL:
if(audio_struct.interface == AUDIO_SPK_FEATURE_UNIT_ID)
{
audio_struct.spk_volume = (uint16_t)(audio_struct.g_audio_cur[0] |
(audio_struct.g_audio_cur[1] << 8));
/* set volume */
audio_codec_set_spk_volume(audio_struct.spk_volume*100/audio_struct.spk_volume_limits[1]);
}
else
{
audio_struct.mic_volume = (uint16_t)(audio_struct.g_audio_cur[0] |
(audio_struct.g_audio_cur[1] << 8));
audio_codec_set_mic_volume(audio_struct.mic_volume*256/audio_struct.mic_volume_limits[1]);
}
break;
case AUDIO_MUTE_CONTROL:
if(audio_struct.interface == AUDIO_SPK_FEATURE_UNIT_ID)
{
audio_struct.spk_mute = audio_struct.g_audio_cur[0];
audio_codec_set_spk_mute(audio_struct.spk_mute);
}
else
{
audio_struct.mic_mute = audio_struct.g_audio_cur[0];
audio_codec_set_mic_mute(audio_struct.mic_mute);
}
break;
case AUDIO_FREQ_SET_CONTROL:
if(audio_struct.enpd == USBD_AUDIO_MIC_IN_EPT)
{
audio_struct.mic_freq = (audio_struct.g_audio_cur[0] |
(audio_struct.g_audio_cur[1] << 8) |
(audio_struct.g_audio_cur[2] << 16));
audio_codec_set_mic_freq(audio_struct.mic_freq);
}
else
{
audio_struct.spk_freq = (audio_struct.g_audio_cur[0] |
(audio_struct.g_audio_cur[1] << 8) |
(audio_struct.g_audio_cur[2] << 16));
audio_codec_set_spk_freq(audio_struct.spk_freq);
}
break;
default:
break;
}
}
}
return status;
}
/**
* @brief usb device transmision complete handler
* @param udev: usb device core handler type
* @param ept_num: endpoint number
* @retval status of usb_sts_type
*/
usb_sts_type class_in_handler(void *udev, uint8_t ept_num)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
uint32_t len = 0;
/* ...user code...
trans next packet data
*/
if((ept_num & 0x7F) == (USBD_AUDIO_MIC_IN_EPT & 0x7F))
{
len = audio_codec_mic_get_data(audio_struct.audio_mic_data);
usb_flush_tx_fifo(pudev->usb_reg, USBD_AUDIO_MIC_IN_EPT & 0x7F);
usbd_ept_send(pudev, USBD_AUDIO_MIC_IN_EPT, audio_struct.audio_mic_data, len);
}
else if((ept_num & 0x7F) == (USBD_AUDIO_FEEDBACK_EPT & 0x7F))
{
len = audio_codec_spk_feedback(audio_struct.audio_feed_back);
usb_flush_tx_fifo(pudev->usb_reg, USBD_AUDIO_FEEDBACK_EPT & 0x7F);
usbd_ept_send(pudev, USBD_AUDIO_FEEDBACK_EPT, audio_struct.audio_feed_back, len);
}
else if((ept_num & 0x7F) == (USBD_HID_IN_EPT & 0x7F))
{
usb_flush_tx_fifo(pudev->usb_reg, USBD_HID_IN_EPT & 0x7F);
}
return status;
}
/**
* @brief usb device endpoint receive data
* @param udev: usb device core handler type
* @param ept_num: endpoint number
* @retval status of usb_sts_type
*/
usb_sts_type class_out_handler(void *udev, uint8_t ept_num)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
uint16_t g_rxlen;
/* get endpoint receive data length */
g_rxlen = usbd_get_recv_len(pudev, ept_num);
if((ept_num & 0x7F) == (USBD_AUDIO_SPK_OUT_EPT & 0x7F))
{
/* speaker data*/
audio_codec_spk_fifo_write(audio_struct.audio_spk_data, g_rxlen);
/* get next data */
usbd_ept_recv(pudev, USBD_AUDIO_SPK_OUT_EPT, audio_struct.audio_spk_data, AUDIO_SPK_OUT_MAXPACKET_SIZE);
}
else if((ept_num & 0x7F) == (USBD_HID_OUT_EPT & 0x7F))
{
/* hid buffer process */
usb_hid_buf_process(udev, g_rxhid_buff, g_rxlen);
/* start receive next packet */
usbd_ept_recv(pudev, USBD_HID_OUT_EPT, g_rxhid_buff, USBD_OUT_MAXPACKET_SIZE);
}
return status;
}
/**
* @brief usb device sof handler
* @param udev: usb device core handler type
* @retval status of usb_sts_type
*/
usb_sts_type class_sof_handler(void *udev)
{
usb_sts_type status = USB_OK;
/* ...user code... */
return status;
}
/**
* @brief usb device event handler
* @param udev: usb device core handler type
* @param event: usb device event
* @retval status of usb_sts_type
*/
usb_sts_type class_event_handler(void *udev, usbd_event_type event)
{
usb_sts_type status = USB_OK;
switch(event)
{
case USBD_RESET_EVENT:
/* ...user code... */
break;
case USBD_SUSPEND_EVENT:
audio_struct.spk_alt_setting = 0;
audio_codec_spk_alt_setting(audio_struct.spk_alt_setting);
audio_struct.mic_alt_setting = 0;
audio_codec_mic_alt_setting(audio_struct.spk_alt_setting);
/* ...user code... */
break;
case USBD_WAKEUP_EVENT:
/* ...user code... */
break;
case USBD_INISOINCOM_EVENT:
audio_inisoincom_event(udev);
break;
default:
break;
}
return status;
}
/**
* @brief usb audio in iso incom event
* @param udev: usb device core handler type
* @retval none
*/
void audio_inisoincom_event(void *udev)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
uint32_t fnsof = OTG_DEVICE(pudev->usb_reg)->dsts_bit.soffn;
uint32_t epctl_fb = USB_INEPT(pudev->usb_reg, (USBD_AUDIO_FEEDBACK_EPT&0x7F))->diepctl_bit.dpid;
uint32_t epctl_in = USB_INEPT(pudev->usb_reg, (USBD_AUDIO_MIC_IN_EPT&0x7F))->diepctl_bit.dpid;
uint32_t len = 0;
if((fnsof & 0x1) == epctl_fb)
{
USB_INEPT(pudev->usb_reg, (USBD_AUDIO_FEEDBACK_EPT&0x7F))->diepctl_bit.eptdis = 1;
USB_INEPT(pudev->usb_reg, (USBD_AUDIO_FEEDBACK_EPT&0x7F))->diepctl_bit.snak = 1;
usb_flush_tx_fifo(pudev->usb_reg, USBD_AUDIO_FEEDBACK_EPT&0x7F);
usbd_ept_send(pudev, USBD_AUDIO_FEEDBACK_EPT, audio_struct.audio_feed_back, 3);
}
if((fnsof & 0x1) == epctl_in)
{
USB_INEPT(pudev->usb_reg, (USBD_AUDIO_MIC_IN_EPT&0x7F))->diepctl_bit.eptdis = 1;
USB_INEPT(pudev->usb_reg, (USBD_AUDIO_MIC_IN_EPT&0x7F))->diepctl_bit.snak = 1;
usb_flush_tx_fifo(pudev->usb_reg, USBD_AUDIO_MIC_IN_EPT&0x7F);
len = audio_codec_mic_get_data(audio_struct.audio_mic_data);
usbd_ept_send(pudev, USBD_AUDIO_MIC_IN_EPT, audio_struct.audio_mic_data, len);
}
}
/**
* @brief usb audio request get cur
* @param udev: usb device core handler type
* @param setup: setup class
* @retval none
*/
void audio_req_get_cur(void *udev, usb_setup_type *setup)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
if(HBYTE(setup->wIndex) == AUDIO_SPK_FEATURE_UNIT_ID)
{
if(HBYTE(setup->wValue) == AUDIO_MUTE_CONTROL)
{
usbd_ctrl_send(pudev, &audio_struct.spk_mute, setup->wLength);
}
else
{
usbd_ctrl_send(pudev, (uint8_t *)&audio_struct.spk_volume, setup->wLength);
}
}
else
{
if(HBYTE(setup->wValue) == AUDIO_MUTE_CONTROL)
{
usbd_ctrl_send(pudev, &audio_struct.mic_mute, setup->wLength);
}
else
{
usbd_ctrl_send(pudev, (uint8_t *)&audio_struct.mic_volume, setup->wLength);
}
}
}
/**
* @brief usb audio request set cur
* @param udev: usb device core handler type
* @param setup: setup class
* @retval none
*/
void audio_req_set_cur(void *udev, usb_setup_type *setup)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
if(setup->wLength > 0)
{
usbd_ctrl_recv(pudev, audio_struct.g_audio_cur, setup->wLength);
audio_struct.audio_cmd = AUDIO_REQ_SET_CUR;
audio_struct.audio_cmd_len = setup->wLength;
switch(setup->bmRequestType & AUDIO_REQ_CONTROL_MASK)
{
case AUDIO_REQ_CONTROL_INTERFACE:
audio_struct.interface = HBYTE(setup->wIndex);
if(HBYTE(setup->wValue) == AUDIO_MUTE_CONTROL)
{
audio_struct.request_no = AUDIO_MUTE_CONTROL;
}
else
{
audio_struct.request_no = AUDIO_VOLUME_CONTROL;
}
break;
case AUDIO_REQ_CONTROL_ENDPOINT:
audio_struct.enpd = setup->wIndex;
audio_struct.request_no = AUDIO_FREQ_SET_CONTROL;
break;
default:
break;
}
}
}
/**
* @brief usb audio request get min
* @param udev: usb device core handler type
* @param setup: setup class
* @retval none
*/
void audio_req_get_min(void *udev, usb_setup_type *setup)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
if(HBYTE(setup->wIndex) == AUDIO_SPK_FEATURE_UNIT_ID)
{
usbd_ctrl_send(pudev, (uint8_t *)&audio_struct.spk_volume_limits[0], setup->wLength);
}
else
{
usbd_ctrl_send(pudev, (uint8_t *)&audio_struct.mic_volume_limits[0], setup->wLength);
}
}
/**
* @brief usb audio request get max
* @param udev: usb device core handler type
* @param setup: setup class
* @retval none
*/
void audio_req_get_max(void *udev, usb_setup_type *setup)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
if(HBYTE(setup->wIndex) == AUDIO_SPK_FEATURE_UNIT_ID)
{
usbd_ctrl_send(pudev, (uint8_t *)&audio_struct.spk_volume_limits[1], setup->wLength);
}
else
{
usbd_ctrl_send(pudev, (uint8_t *)&audio_struct.mic_volume_limits[1], setup->wLength);
}
}
/**
* @brief usb audio request get res
* @param udev: usb device core handler type
* @param setup: setup class
* @retval none
*/
void audio_req_get_res(void *udev, usb_setup_type *setup)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
if(HBYTE(setup->wIndex) == AUDIO_SPK_FEATURE_UNIT_ID)
{
usbd_ctrl_send(pudev, (uint8_t *)&audio_struct.spk_volume_limits[2], setup->wLength);
}
else
{
usbd_ctrl_send(pudev, (uint8_t *)&audio_struct.mic_volume_limits[2], setup->wLength);
}
}
/**
* @brief usb audio set interface
* @param udev: usb device core handler type
* @param setup: setup class
* @retval none
*/
void audio_set_interface(void *udev, usb_setup_type *setup)
{
uint32_t len;
usbd_core_type *pudev = (usbd_core_type *)udev;
if(LBYTE(setup->wIndex) == AUDIO_SPK_INTERFACE_NUMBER)
{
audio_struct.spk_alt_setting = setup->wValue;
audio_codec_spk_alt_setting(audio_struct.spk_alt_setting);
if(audio_struct.spk_alt_setting )
{
usbd_ept_recv(pudev, USBD_AUDIO_SPK_OUT_EPT, audio_struct.audio_spk_data, AUDIO_SPK_OUT_MAXPACKET_SIZE);
usbd_ept_send(pudev, USBD_AUDIO_FEEDBACK_EPT, audio_struct.audio_feed_back, 3);
}
}
else if(LBYTE(setup->wIndex) == AUDIO_MIC_INTERFACE_NUMBER)
{
audio_struct.mic_alt_setting = setup->wValue;
audio_codec_mic_alt_setting(audio_struct.mic_alt_setting);
if(audio_struct.mic_alt_setting)
{
len = audio_codec_mic_get_data(audio_struct.audio_mic_data);
usbd_ept_send(pudev, USBD_AUDIO_MIC_IN_EPT, audio_struct.audio_mic_data, len);
}
}
}
/**
* @brief usb audio get interface
* @param udev: usb device core handler type
* @param setup: setup class
* @retval none
*/
void audio_get_interface(void *udev, usb_setup_type *setup)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
if(LBYTE(setup->wIndex) == AUDIO_SPK_INTERFACE_NUMBER)
{
usbd_ctrl_send(pudev, (uint8_t *)&audio_struct.spk_alt_setting, 1);
}
else if(LBYTE(setup->wIndex) == AUDIO_MIC_INTERFACE_NUMBER)
{
usbd_ctrl_send(pudev, (uint8_t *)&audio_struct.mic_alt_setting, 1);
}
}
/**
* @brief usb device class send report
* @param udev: to the structure of usbd_core_type
* @param report: report buffer
* @param len: report length
* @retval status of usb_sts_type
*/
usb_sts_type class_send_report(void *udev, uint8_t *report, uint16_t len)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
if(usbd_connect_state_get(pudev) == USB_CONN_STATE_CONFIGURED)
usbd_ept_send(pudev, USBD_HID_IN_EPT, report, len);
return status;
}
/**
* @brief usb device report function
* @param udev: to the structure of usbd_core_type
* @param report: report buffer
* @param len: report length
* @retval none
*/
void usb_hid_buf_process(void *udev, uint8_t *report, uint16_t len)
{
uint32_t i_index;
usbd_core_type *pudev = (usbd_core_type *)udev;
switch(report[0])
{
case HID_REPORT_ID_2:
if(g_rxhid_buff[1] == 0)
{
at32_led_off(LED2);
}
else
{
at32_led_on(LED2);
}
break;
case HID_REPORT_ID_3:
if(g_rxhid_buff[1] == 0)
{
at32_led_off(LED3);
}
else
{
at32_led_on(LED3);
}
break;
case HID_REPORT_ID_4:
if(g_rxhid_buff[1] == 0)
{
at32_led_off(LED4);
}
else
{
at32_led_on(LED4);
}
break;
case HID_REPORT_ID_6:
for(i_index = 0; i_index < len; i_index ++)
{
g_txhid_buff[i_index] = report[i_index];
}
usbd_ept_send(pudev, USBD_HID_IN_EPT, g_txhid_buff, len);
default:
break;
}
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/

View File

@@ -0,0 +1,157 @@
/**
**************************************************************************
* @file audio_class.h
* @version v2.0.0
* @date 2021-11-26
* @brief usb audio class file
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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.
*
**************************************************************************
*/
/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __AUDIO_CLASS_H
#define __AUDIO_CLASS_H
#ifdef __cplusplus
extern "C" {
#endif
#include "usb_std.h"
#include "usbd_core.h"
#include "audio_conf.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @addtogroup USB_audio_hid_class
* @{
*/
/** @defgroup USB_audio_hid_class_definition
* @{
*/
/**
* @brief endpoint define
*/
#define USBD_AUDIO_MIC_IN_EPT 0x81
#define USBD_AUDIO_SPK_OUT_EPT 0x02
#define USBD_AUDIO_FEEDBACK_EPT 0x83
/**
* @brief usb custom hid use endpoint define
*/
#define USBD_HID_IN_EPT 0x82
#define USBD_HID_OUT_EPT 0x01
/**
* @brief usb custom hid in and out max packet size define
*/
#define USBD_IN_MAXPACKET_SIZE 0x40
#define USBD_OUT_MAXPACKET_SIZE 0x40
/**
* @brief endpoint support max size
*/
#define AUDIO_REMAIN_SIZE 8
#define AUDIO_MIC_IN_MAXPACKET_SIZE (AUDIO_SUPPORT_MAX_FREQ * AUDIO_MIC_CHANEL_NUM * (AUDIO_MIC_DEFAULT_BITW / 8) + AUDIO_REMAIN_SIZE)
#define AUDIO_SPK_OUT_MAXPACKET_SIZE (AUDIO_SUPPORT_MAX_FREQ * AUDIO_SPK_CHANEL_NUM * (AUDIO_SPK_DEFAULT_BITW / 8) + AUDIO_REMAIN_SIZE)
#define AUDIO_FEEDBACK_MAXPACKET_SIZE 0x3
/**
* @brief request type define
*/
#define AUDIO_REQ_CONTROL_INTERFACE 0x01
#define AUDIO_REQ_CONTROL_ENDPOINT 0x02
#define AUDIO_REQ_CONTROL_MASK 0x03
/**
* @brief audio set cur type define
*/
#define AUDIO_MUTE_CONTROL 0x01
#define AUDIO_VOLUME_CONTROL 0x02
#define AUDIO_FREQ_SET_CONTROL 0x03
/**
* @brief audio descriptor type
*/
#define AUDIO_DESCRIPTOR_TYPE 0x21
#define AUDIO_DESCRIPTOR_SIZE 0x09
/**
* @brief usb custom hid class request code define
*/
#define HID_REQ_SET_PROTOCOL 0x0B
#define HID_REQ_GET_PROTOCOL 0x03
#define HID_REQ_SET_IDLE 0x0A
#define HID_REQ_GET_IDLE 0x02
#define HID_REQ_SET_REPORT 0x09
#define HID_REQ_GET_REPORT 0x01
#define HID_DESCRIPTOR_TYPE 0x21
#define HID_REPORT_DESC 0x22
/**
* @brief usb audio control struct
*/
typedef struct
{
uint8_t enpd;
uint8_t interface;
uint8_t request_no;
uint8_t spk_mute;
uint8_t mic_mute;
uint16_t spk_volume;
uint16_t mic_volume;
uint32_t spk_freq;
uint32_t mic_freq;
uint16_t spk_volume_limits[3]; /*[0] is mininum value, [1] is maxnum value, [2] is volume resolution */
uint16_t mic_volume_limits[3]; /*[0] is mininum value, [1] is maxnum value, [2] is volume resolution */
uint8_t audio_cmd;
uint32_t audio_cmd_len;
uint32_t spk_alt_setting;
uint32_t mic_alt_setting;
uint8_t g_audio_cur[64];
uint8_t audio_spk_data[AUDIO_SPK_OUT_MAXPACKET_SIZE];
uint8_t audio_mic_data[AUDIO_MIC_IN_MAXPACKET_SIZE];
uint8_t audio_feed_back[AUDIO_FEEDBACK_MAXPACKET_SIZE+1];
}usb_audio_type;
extern usbd_class_handler audio_hid_class_handler;
usb_sts_type class_send_report(void *udev, uint8_t *report, uint16_t len);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,770 @@
/**
**************************************************************************
* @file audio_desc.c
* @version v2.0.0
* @date 2021-11-26
* @brief usb audio device descriptor
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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 "audio_hid_desc.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @defgroup USB_audio_hid_desc
* @brief usb device audio hid descriptor
* @{
*/
/** @defgroup USB_audio_hid_desc_private_functions
* @{
*/
usbd_desc_t *get_device_descriptor(void);
usbd_desc_t *get_device_qualifier(void);
usbd_desc_t *get_device_configuration(void);
usbd_desc_t *get_device_other_speed(void);
usbd_desc_t *get_device_lang_id(void);
usbd_desc_t *get_device_manufacturer_string(void);
usbd_desc_t *get_device_product_string(void);
usbd_desc_t *get_device_serial_string(void);
usbd_desc_t *get_device_interface_string(void);
usbd_desc_t *get_device_config_string(void);
uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf);
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 device descriptor handler structure
*/
usbd_desc_handler audio_hid_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
*/
uint8_t g_usbd_descriptor[USB_DEVICE_DESC_LEN] =
{
USB_DEVICE_DESC_LEN, /* bLength */
USB_DESCIPTOR_TYPE_DEVICE, /* bDescriptorType */
0x00, /* bcdUSB */
0x02,
0x00, /* bDeviceClass */
0x00, /* bDeviceSubClass */
0x00, /* bDeviceProtocol */
USB_MAX_EP0_SIZE, /* bMaxPacketSize */
LBYTE(USBD_VENDOR_ID), /* idVendor */
HBYTE(USBD_VENDOR_ID), /* idVendor */
LBYTE(USBD_PRODUCT_ID), /* idProduct */
HBYTE(USBD_PRODUCT_ID), /* idProduct */
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 */
0x01 /* bNumConfigurations */
};
/**
* @brief usb configuration standard descriptor
*/
uint8_t g_usbd_configuration[USBD_CONFIG_DESC_SIZE] =
{
USB_DEVICE_CFG_DESC_LEN, /* bLength: configuration descriptor size */
USB_DESCIPTOR_TYPE_CONFIGURATION, /* bDescriptorType: configuration */
LBYTE(USBD_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
HBYTE(USBD_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
0x1 + AUDIO_INTERFACE_NUM + 0x1, /* bNumInterfaces: n interface */
0x01, /* bConfigurationValue: configuration value */
0x00, /* iConfiguration: index of string descriptor describing
the configuration */
0xC0, /* bmAttributes: self powered */
0x32, /* MaxPower 100 mA: this current is used for detecting vbus */
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 */
0x00, /* bNumEndpoints: number of endpoints */
USB_CLASS_CODE_AUDIO, /* bInterfaceClass: audio class code */
AUDIO_SUBCLASS_AUDIOCONTROL, /* bInterfaceSubClass: audio control */
AUDIO_PROTOCOL_UNDEFINED, /* bInterfaceProtocol: undefined */
0x00, /* iInterface: index of string descriptor */
0x08+AUDIO_INTERFACE_NUM, /* bLength: size of this descriptor, in bytes 8+n */
AUDIO_CS_INTERFACE, /* bDescriptorType: cs interface descriptor type */
AUDIO_AC_HEADER, /* bDescriptorSubtype: Header function Descriptor*/
LBYTE(BCD_NUM),
HBYTE(BCD_NUM), /* bcdCDC: audio device class specification release number */
LBYTE(AUDIO_INTERFACE_LEN),
HBYTE(AUDIO_INTERFACE_LEN), /* wTotalLength: total number of bytes returned for the class-specific audio control interface */
AUDIO_INTERFACE_NUM, /* bInClollection: the number of audio streaming */
#if (AUDIO_INTERFACE_NUM == 2) /* two interface */
0x02,
0x01,
#else
0x01,
#endif
/* usb microphone config */
#if (AUDIO_SUPPORT_MIC == 1)
AUDIO_INPUT_TERMINAL_SIZE, /* bLength: descriptor size */
AUDIO_CS_INTERFACE, /* bDescriptorType: configuration */
AUDIO_AC_INPUT_TERMINAL, /* bDescriptorSubtype: input_terminal type*/
AUDIO_MIC_INPUT_TERMINAL_ID, /* bTerminalID: id of this input terminal*/
LBYTE(AUDIO_INPUT_TERMINAL_MICROPHONE),
HBYTE(AUDIO_INPUT_TERMINAL_MICROPHONE),/* wTerminalType: terminal is microphone */
0x00, /* bAssocTerminal: no association */
AUDIO_MIC_CHR, /* bNrChannels: two channel */
#if (AUDIO_MIC_CHR == 2)
0x03, /* wChannelConfig: left front and right front */
#endif
#if (AUDIO_MIC_CHR == 1)
0x00, /* wChannelConfig */
#endif
0x00, /* wChannelConfig */
0x00, /* iChannelNames: unused */
0x00, /* iTerminal: unused */
AUDIO_FEATURE_UNIT_SIZE, /* bLength: descriptor size */
AUDIO_CS_INTERFACE, /* bDescriptorType: configuration */
AUDIO_AC_FEATURE_UNIT, /* bDescriptorSubtype: feature unit type*/
AUDIO_MIC_FEATURE_UNIT_ID, /* bUnitID: id of this feature unit */
AUDIO_MIC_INPUT_TERMINAL_ID, /* bSourceID: from input terminal */
0x01, /* bControlSize: 1 byte */
0x01, /* bmaControls0: mute */
0x02, /* bmaControls1: volume */
0x00, /* iFeature: unused */
AUDIO_OUTPUT_TERMINAL_SIZE, /* bLength: descriptor size */
AUDIO_CS_INTERFACE, /* bDescriptorType: configuration */
AUDIO_AC_OUTPUT_TERMINAL, /* bDescriptorSubtype: output_terminal type*/
AUDIO_MIC_OUTPUT_TERMINAL_ID, /* bTerminalID: id of this output terminal*/
LBYTE(AUDIO_TERMINAL_TYPE_STREAMING),
HBYTE(AUDIO_TERMINAL_TYPE_STREAMING), /* wTerminalType: usb streaming */
0x00, /* bAssocTerminal: unused */
AUDIO_MIC_FEATURE_UNIT_ID, /* bSourceID: from feature unit terminal */
0x00, /* iTerminal: unused */
#endif
#if (AUDIO_SUPPORT_SPK == 1)
/* speaker config */
AUDIO_INPUT_TERMINAL_SIZE, /* bLength: descriptor size */
AUDIO_CS_INTERFACE, /* bDescriptorType: configuration */
AUDIO_AC_INPUT_TERMINAL, /* bDescriptorSubtype: input_terminal type*/
AUDIO_SPK_INPUT_TERMINAL_ID, /* bTerminalID: id of this input terminal*/
LBYTE(AUDIO_TERMINAL_TYPE_STREAMING), /* wTerminalType: usb streaming */
HBYTE(AUDIO_TERMINAL_TYPE_STREAMING), /* wTerminalType: usb streaming */
0x00, /* bAssocTerminal: no association */
AUDIO_SPK_CHR, /* bNrChannels: two channel */
#if (AUDIO_SPK_CHR == 2)
0x03, /* wChannelConfig: left front and right front */
#endif
#if (AUDIO_SPK_CHR == 1)
0x00, /* wChannelConfig */
#endif
0x00, /* wChannelConfig */
0x00, /* iChannelNames: unused */
0x00, /* iTerminal: unused */
AUDIO_FEATURE_UNIT_SIZE, /* bLength: descriptor size */
AUDIO_CS_INTERFACE, /* bDescriptorType: configuration */
AUDIO_AC_FEATURE_UNIT, /* bDescriptorSubtype: feature unit type*/
AUDIO_SPK_FEATURE_UNIT_ID, /* bUnitID: id of this feature unit */
AUDIO_SPK_INPUT_TERMINAL_ID, /* bSourceID: from input terminal */
0x01, /* bControlSize: 1 byte */
0x01, /* bmaControls0: mute*/
0x02, /* bmaControls1: volume */
0x00, /* iFeature: unused */
AUDIO_OUTPUT_TERMINAL_SIZE, /* bLength: descriptor size */
AUDIO_CS_INTERFACE, /* bDescriptorType: configuration */
AUDIO_AC_OUTPUT_TERMINAL, /* bDescriptorSubtype: output_terminal type*/
AUDIO_SPK_OUTPUT_TERMINAL_ID, /* bTerminalID: id of this output terminal*/
LBYTE(AUDIO_OUTPUT_TERMINAL_SPEAKER), /* wTerminalType: usb speaker */
HBYTE(AUDIO_OUTPUT_TERMINAL_SPEAKER), /* wTerminalType: usb speaker */
0x00, /* bAssocTerminal: unused */
AUDIO_SPK_FEATURE_UNIT_ID, /* bSourceID: from feature unit terminal */
0x00, /* iTerminal: unused */
#endif
#if (AUDIO_SUPPORT_MIC == 1)
/* microphone interface */
0x09, /* bLength: descriptor size */
USB_DESCIPTOR_TYPE_INTERFACE, /* bDescriptorType: interface descriptor type */
AUDIO_MIC_INTERFACE_NUMBER, /* bInterfaceNumber: index of this interface */
0x00, /* bAlternateSetting: index of this setting */
0x00, /* bNumEndpoints: 0 endpoints */
USB_CLASS_CODE_AUDIO, /* bInterfaceClass: audio */
AUDIO_SUBCLASS_AUDIOSTREAMING, /* bInterfaceSubclass: audio streaming */
0x00, /* bInterfaceProtocol: unused */
0x00, /* iInterface: unused */
0x09, /* bLength: descriptor size */
USB_DESCIPTOR_TYPE_INTERFACE, /* bDescriptorType: interface descriptor type */
AUDIO_MIC_INTERFACE_NUMBER, /* bInterfaceNumber: index of this interface */
0x01, /* bAlternateSetting: index of this setting */
0x01, /* bNumEndpoints: 1 endpoints */
USB_CLASS_CODE_AUDIO, /* bInterfaceClass: audio */
AUDIO_SUBCLASS_AUDIOSTREAMING, /* bInterfaceSubclass: audio streaming */
0x00, /* bInterfaceProtocol: unused */
0x00, /* iInterface: unused */
0x07, /* bLength: configuration descriptor size */
AUDIO_CS_INTERFACE, /* bDescriptorType: interface descriptor type */
AUDIO_AS_GENERAL, /* bDescriptorSubtype: general sub type*/
AUDIO_MIC_OUTPUT_TERMINAL_ID, /* bTerminalLink: unit id of the output terminal */
0x01, /* bDelay: interface delay */
0x01, /* wFormatTag: pcm format*/
0x00, /* wFormatTag: pcm format*/
0x08 + AUDIO_MIC_FREQ_SIZE * 3, /* bLength: descriptor size */
AUDIO_CS_INTERFACE, /* bDescriptorType: interface descriptor type */
AUDIO_AS_FORMAT_TYPE, /* bDescriptorSubtype: format subtype */
AUDIO_FORMAT_TYPE_I, /* bFormatType: format type 1 */
AUDIO_MIC_CHR, /* bNrChannels: channel number */
AUDIO_MIC_BITW / 8, /* bSubFrameSize: per audio subframe */
AUDIO_MIC_BITW, /* bBitResolution: n bits per sample */
AUDIO_MIC_FREQ_SIZE, /* bSamFreqType: n frequency supported */
#if (AUDIO_SUPPORT_FREQ_16K == 1)
SAMPLE_FREQ(AT32_AUDIO_FREQ_16K), /* tSamFreq: 16000hz */
#endif
#if (AUDIO_SUPPORT_FREQ_48K == 1)
SAMPLE_FREQ(AT32_AUDIO_FREQ_48K), /* tSamFreq: 48000hz */
#endif
0x09, /* bLength: size of endpoint descriptor in bytes */
USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
USBD_AUDIO_MIC_IN_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
USB_EPT_DESC_ISO | USB_ETP_DESC_ASYNC, /* bmAttributes: endpoint attributes */
LBYTE(AUDIO_MIC_IN_MAXPACKET_SIZE),
HBYTE(AUDIO_MIC_IN_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
HID_BINTERVAL_TIME, /* bInterval: interval for polling endpoint for data transfers */
0x00, /* bRefresh: unused */
0x00, /* bSynchAddress: unused */
0x07, /* bLength: size of endpoint descriptor in bytes */
AUDIO_CS_ENDPOINT, /* bDescriptorType: cs endpoint descriptor type */
0x01, /* bDescriptorSubtype: general subtype */
0x01, /* bmAttributes */
0x00, /* bLockDelayUnits: unused */
0x00, /* wLockDelay: unused */
0x00, /* wLockDelay: unused */
#endif
#if (AUDIO_SUPPORT_SPK == 1)
/* speaker interface */
0x09, /* bLength: descriptor size */
USB_DESCIPTOR_TYPE_INTERFACE, /* bDescriptorType: interface descriptor type */
AUDIO_SPK_INTERFACE_NUMBER, /* bInterfaceNumber: index of this interface */
0x00, /* bAlternateSetting: index of this setting */
0x00, /* bNumEndpoints: 0 endpoints */
USB_CLASS_CODE_AUDIO, /* bInterfaceClass: audio */
AUDIO_SUBCLASS_AUDIOSTREAMING, /* bInterfaceSubclass: audio streaming */
0x00, /* bInterfaceProtocol: unused */
0x00, /* iInterface: unused */
0x09, /* bLength: descriptor size */
USB_DESCIPTOR_TYPE_INTERFACE, /* bDescriptorType: interface descriptor type */
AUDIO_SPK_INTERFACE_NUMBER, /* bInterfaceNumber: index of this interface */
0x01, /* bAlternateSetting: index of this setting */
0x01 + AUDIO_SUPPORT_FEEDBACK, /* bNumEndpoints: endpoints */
USB_CLASS_CODE_AUDIO, /* bInterfaceClass: audio */
AUDIO_SUBCLASS_AUDIOSTREAMING, /* bInterfaceSubclass: audio streaming */
0x00, /* bInterfaceProtocol: unused */
0x00, /* iInterface: unused */
0x07, /* bLength: configuration descriptor size */
AUDIO_CS_INTERFACE, /* bDescriptorType: interface descriptor type */
AUDIO_AS_GENERAL, /* bDescriptorSubtype: general sub type*/
AUDIO_SPK_INPUT_TERMINAL_ID, /* bTerminalLink: unit id of the input terminal */
0x01, /* bDelay: interface delay */
0x01, /* wFormatTag: pcm format*/
0x00, /* wFormatTag: pcm format*/
0x08 + AUDIO_SPK_FREQ_SIZE * 3, /* bLength: descriptor size */
AUDIO_CS_INTERFACE, /* bDescriptorType: interface descriptor type */
AUDIO_AS_FORMAT_TYPE, /* bDescriptorSubtype: format subtype */
AUDIO_FORMAT_TYPE_I, /* bFormatType: format type 1 */
AUDIO_SPK_CHR, /* bNrChannels: channel number */
AUDIO_SPK_BITW / 8, /* bSubFrameSize: per audio subframe */
AUDIO_SPK_BITW, /* bBitResolution: n bits per sample */
AUDIO_SPK_FREQ_SIZE, /* bSamFreqType: n frequency supported */
#if (AUDIO_SUPPORT_FREQ_16K == 1)
SAMPLE_FREQ(AT32_AUDIO_FREQ_16K), /* tSamFreq: 16000hz */
#endif
#if (AUDIO_SUPPORT_FREQ_48K == 1)
SAMPLE_FREQ(AT32_AUDIO_FREQ_48K), /* tSamFreq: 48000hz */
#endif
0x09, /* bLength: size of endpoint descriptor in bytes */
USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
USBD_AUDIO_SPK_OUT_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
USB_EPT_DESC_ISO | USB_ETP_DESC_ASYNC, /* bmAttributes: endpoint attributes */
LBYTE(AUDIO_SPK_OUT_MAXPACKET_SIZE),
HBYTE(AUDIO_SPK_OUT_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
HID_BINTERVAL_TIME, /* bInterval: interval for polling endpoint for data transfers */
0x00, /* bRefresh: unused */
#if (AUDIO_SUPPORT_FEEDBACK == 1)
USBD_AUDIO_FEEDBACK_EPT, /* bSynchAddress: feedback endpoint */
#else
0x00, /* bSynchAddress: unused */
#endif
0x07, /* bLength: size of endpoint descriptor in bytes */
AUDIO_CS_ENDPOINT, /* bDescriptorType: cs endpoint descriptor type */
0x01, /* bDescriptorSubtype: general subtype */
0x01, /* bmAttributes */
0x00, /* bLockDelayUnits: unused */
0x00, /* wLockDelay: unused */
0x00, /* wLockDelay: unused */
#if (AUDIO_SUPPORT_FEEDBACK == 1)
0x09, /* bLength: size of endpoint descriptor in bytes */
USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
USBD_AUDIO_FEEDBACK_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
0x11, /* bmAttributes: endpoint attributes */
LBYTE(AUDIO_FEEDBACK_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
HBYTE(AUDIO_FEEDBACK_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
HID_BINTERVAL_TIME, /* bInterval: interval for polling endpoint for data transfers */
0x08, /* bRefresh: this field indicates the rate at which an iso syncronization
pipe provides new syncronization feedback data. this rate must be a power of
2, therefore only the power is reported back and the range of this field is from
1(2ms) to 9(512ms) */
0x00, /* bSynchAddress: 0x00*/
#endif
#endif
USB_DEVICE_IF_DESC_LEN, /* bLength: interface descriptor size */
USB_DESCIPTOR_TYPE_INTERFACE, /* bDescriptorType: interface descriptor type */
HID_INTERFACE_NUMBER, /* bInterfaceNumber: number of interface */
0x00, /* bAlternateSetting: alternate set */
0x02, /* bNumEndpoints: number of endpoints */
USB_CLASS_CODE_HID, /* bInterfaceClass: class code hid */
0x00, /* bInterfaceSubClass: subclass code */
0x00, /* bInterfaceProtocol: protocol code */
0x00, /* iInterface: index of string descriptor */
0x09, /* bLength: size of HID descriptor in bytes */
HID_CLASS_DESC_HID, /* bDescriptorType: HID descriptor type */
LBYTE(HID_BCD_NUM),
HBYTE(HID_BCD_NUM), /* bcdHID: HID class specification release number */
0x00, /* bCountryCode: hardware target conutry */
0x01, /* bNumDescriptors: number of HID class descriptor to follow */
HID_CLASS_DESC_REPORT, /* bDescriptorType: report descriptor type */
LBYTE(sizeof(g_usbd_hid_report)),
HBYTE(sizeof(g_usbd_hid_report)), /* wDescriptorLength: total length of reprot descriptor */
USB_DEVICE_EPT_LEN, /* bLength: size of endpoint descriptor in bytes */
USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
USBD_HID_IN_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
USB_EPT_DESC_INTERRUPT, /* bmAttributes: endpoint attributes */
LBYTE(USBD_IN_MAXPACKET_SIZE),
HBYTE(USBD_IN_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
HID_BINTERVAL_TIME, /* bInterval: interval for polling endpoint for data transfers */
USB_DEVICE_EPT_LEN, /* bLength: size of endpoint descriptor in bytes */
USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
USBD_HID_OUT_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
USB_EPT_DESC_INTERRUPT, /* bmAttributes: endpoint attributes */
LBYTE(USBD_OUT_MAXPACKET_SIZE),
HBYTE(USBD_OUT_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
HID_BINTERVAL_TIME, /* bInterval: interval for polling endpoint for data transfers */
};
/**
* @brief usb hid report descriptor
*/
uint8_t g_usbd_hid_report[USBD_HID_SIZ_REPORT_DESC] =
{
0x06, 0xFF, 0x00, /* USAGE_PAGE(Vendor Page:0xFF00) */
0x09, 0x01, /* USAGE (Demo Kit) */
0xa1, 0x01, /* COLLECTION (Application) */
/* 7 */
/* Led 2 */
0x85, HID_REPORT_ID_2, /* REPORT_ID 2 */
0x09, 0x02, /* USAGE (LED 2) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
0x75, 0x08, /* REPORT_SIZE (8) */
0x95, 0x3F, /* REPORT_COUNT (1) */
0xB1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
0x85, 0x02, /* REPORT_ID (2) */
0x09, 0x02, /* USAGE (LED 2) */
0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */
/* 27 */
/* Led 3 */
0x85, HID_REPORT_ID_3, /* REPORT_ID (3) */
0x09, 0x03, /* USAGE (LED 3) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
0x75, 0x08, /* REPORT_SIZE (8) */
0x95, 0x3F, /* REPORT_COUNT (1) */
0xB1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
0x85, 0x03, /* REPORT_ID (3) */
0x09, 0x03, /* USAGE (LED 3) */
0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */
/* 47 */
/* Led 4 */
0x85, HID_REPORT_ID_4, /* REPORT_ID 4) */
0x09, 0x04, /* USAGE (LED 4) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
0x75, 0x08, /* REPORT_SIZE (8) */
0x95, 0x3F, /* REPORT_COUNT (1) */
0xB1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
0x85, 0x04, /* REPORT_ID (4) */
0x09, 0x04, /* USAGE (LED 4) */
0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */
/* 67 */
/* key Push Button */
0x85, HID_REPORT_ID_5, /* REPORT_ID (5) */
0x09, 0x05, /* USAGE (Push Button) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x81, 0x82, /* INPUT (Data,Var,Abs,Vol) */
0x09, 0x05, /* USAGE (Push Button) */
0x75, 0x01, /* REPORT_SIZE (1) */
0xb1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
0x75, 0x07, /* REPORT_SIZE (7) */
0x81, 0x83, /* INPUT (Cnst,Var,Abs,Vol) */
0x85, 0x05, /* REPORT_ID (5) */
0x75, 0x07, /* REPORT_SIZE (7) */
0xb1, 0x83, /* FEATURE (Cnst,Var,Abs,Vol) */
/* 95 */
/* Data OUT */
0x85, HID_REPORT_ID_6, /* REPORT_ID (0xF0) */
0x09, 0x06, /* USAGE */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x26, 0x00,0xff, /* LOGICAL_MAXIMUM (255) */
0x75, 0x08, /* REPORT_SIZE (8) */
0x95, 0x3F, /* REPORT_COUNT (64) */
0x91, 0x02, /* OUTPUT(Data,Var,Abs,Vol) */
/* 110 */
/* Data IN */
0x85, HID_REPORT_ID_6, /* REPORT_ID (0xF0) */
0x09, 0x07, /* USAGE */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x26, 0x00,0xff, /* LOGICAL_MAXIMUM (255) */
0x75, 0x08, /* REPORT_SIZE (8) */
0x95, 0x3F, /* REPORT_COUNT (64) */
0x81, 0x82, /* INPUT(Data,Var,Abs,Vol) */
/* 125 */
0xc0 /* END_COLLECTION */
};
/**
* @brief usb hid descriptor
*/
uint8_t g_hid_usb_desc[9] =
{
0x09, /* bLength: size of HID descriptor in bytes */
HID_CLASS_DESC_HID, /* bDescriptorType: HID descriptor type */
LBYTE(HID_BCD_NUM),
HBYTE(HID_BCD_NUM), /* bcdHID: HID class specification release number */
0x00, /* bCountryCode: hardware target conutry */
0x01, /* bNumDescriptors: number of HID class descriptor to follow */
HID_CLASS_DESC_REPORT, /* bDescriptorType: report descriptor type */
LBYTE(sizeof(g_usbd_hid_report)),
HBYTE(sizeof(g_usbd_hid_report)), /* wDescriptorLength: total length of reprot descriptor */
};
/**
* @brief usb string lang id
*/
uint8_t g_string_lang_id[USBD_SIZ_STRING_LANGID] =
{
USBD_SIZ_STRING_LANGID,
USB_DESCIPTOR_TYPE_STRING,
0x09,
0x04,
};
/**
* @brief usb string serial
*/
uint8_t g_string_serial[USBD_SIZ_STRING_SERIAL] =
{
USBD_SIZ_STRING_SERIAL,
USB_DESCIPTOR_TYPE_STRING,
};
/* device descriptor */
usbd_desc_t device_descriptor =
{
USB_DEVICE_DESC_LEN,
g_usbd_descriptor
};
/* config descriptor */
usbd_desc_t config_descriptor =
{
USBD_CONFIG_DESC_SIZE,
g_usbd_configuration
};
/* langid descriptor */
usbd_desc_t langid_descriptor =
{
USBD_SIZ_STRING_LANGID,
g_string_lang_id
};
/* serial descriptor */
usbd_desc_t serial_descriptor =
{
USBD_SIZ_STRING_SERIAL,
g_string_serial
};
usbd_desc_t vp_desc;
/**
* @brief standard usb unicode convert
* @param string: source string
* @param unicode_buf: unicode buffer
* @retval length
*/
uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf)
{
uint16_t str_len = 0, id_pos = 2;
uint8_t *tmp_str = string;
while(*tmp_str != '\0')
{
str_len ++;
unicode_buf[id_pos ++] = *tmp_str ++;
unicode_buf[id_pos ++] = 0x00;
}
str_len = str_len * 2 + 2;
unicode_buf[0] = str_len;
unicode_buf[1] = USB_DESCIPTOR_TYPE_STRING;
return str_len;
}
/**
* @brief usb int convert to unicode
* @param value: int value
* @param pbus: unicode buffer
* @param len: length
* @retval none
*/
static void usbd_int_to_unicode (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;
}
}
/**
* @brief usb get serial number
* @param none
* @retval none
*/
static void get_serial_num(void)
{
uint32_t serial0, serial1, serial2;
serial0 = *(uint32_t*)MCU_ID1;
serial1 = *(uint32_t*)MCU_ID2;
serial2 = *(uint32_t*)MCU_ID3;
serial0 += serial2;
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
* @retval usbd_desc
*/
usbd_desc_t *get_device_descriptor(void)
{
return &device_descriptor;
}
/**
* @brief get device qualifier
* @param none
* @retval usbd_desc
*/
usbd_desc_t * get_device_qualifier(void)
{
return NULL;
}
/**
* @brief get config descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_configuration(void)
{
return &config_descriptor;
}
/**
* @brief get other speed descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_other_speed(void)
{
return NULL;
}
/**
* @brief get lang id descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_lang_id(void)
{
return &langid_descriptor;
}
/**
* @brief get manufacturer descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_manufacturer_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_MANUFACTURER_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get product descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_product_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_PRODUCT_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get serial descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_serial_string(void)
{
get_serial_num();
return &serial_descriptor;
}
/**
* @brief get interface descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_interface_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_INTERFACE_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get device config descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_config_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_CONFIGURATION_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/

View File

@@ -0,0 +1,266 @@
/**
**************************************************************************
* @file audio_desc.h
* @version v2.0.0
* @date 2021-11-26
* @brief usb audio descriptor header file
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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.
*
**************************************************************************
*/
/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __AUDIO_DESC_H
#define __AUDIO_DESC_H
#ifdef __cplusplus
extern "C" {
#endif
#include "audio_hid_class.h"
#include "usbd_core.h"
#include "audio_conf.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @addtogroup USB_audio_hid_desc
* @{
*/
/** @defgroup USB_audio_hid_desc_definition
* @{
*/
#define BCD_NUM 0x0100
#define USBD_VENDOR_ID 0x2E3C
#define USBD_PRODUCT_ID 0x5550
#define USBD_SIZ_STRING_LANGID 4
#define USBD_SIZ_STRING_SERIAL 0x1A
#define USBD_DESC_MANUFACTURER_STRING "Artery"
#define USBD_DESC_PRODUCT_STRING "AT32 Audio"
#define USBD_DESC_CONFIGURATION_STRING "Audio Config"
#define USBD_DESC_INTERFACE_STRING "Audio Interface"
/**
* @brief audio interface subclass codes
*/
#define AUDIO_SUBCLASS_UNDEFINED 0x00
#define AUDIO_SUBCLASS_AUDIOCONTROL 0x01
#define AUDIO_SUBCLASS_AUDIOSTREAMING 0x02
#define AUDIO_SUBCLASS_MIDISTREMING 0x03
/**
* @brief audio class-specific descriptor types
*/
#define AUDIO_CS_INTERFACE 0x24
#define AUDIO_CS_ENDPOINT 0x25
#define AUDIO_CS_STRING 0x23
#define AUDIO_CS_CONFIGURATION 0x22
#define AUDIO_CS_DEVICE 0x21
#define AUDIO_CS_UNDEFINED 0x20
/**
* @brief audio interface protocol codes
*/
#define AUDIO_PROTOCOL_UNDEFINED 0x00
/**
* @brief audio class-specific ac interface descriptor subtypes
*/
#define AUDIO_AC_DESCRIPTOR_UNDEFINED 0x00
#define AUDIO_AC_HEADER 0x01
#define AUDIO_AC_INPUT_TERMINAL 0x02
#define AUDIO_AC_OUTPUT_TERMINAL 0x03
#define AUDIO_AC_MIXER_UNIT 0x04
#define AUDIO_AC_SELECTOR_UNIT 0x05
#define AUDIO_AC_FEATURE_UNIT 0x06
#define AUDIO_AC_PROCESSING_UNIT 0x07
#define AUDIO_AC_EXTENSION_UNIT 0x08
/**
* @brief audio class-specific as interface descriptor subtypes
*/
#define AUDIO_AS_DESCRIPTOR_UNDEFINED 0x00
#define AUDIO_AS_GENERAL 0x01
#define AUDIO_AS_FORMAT_TYPE 0x02
#define AUDIO_AS_FORMAT_SPECIFIC 0x03
/**
* @brief audio class-specific request codes
*/
#define AUDIO_REQUEST_CODE_UNDEFINED 0x00
#define AUDIO_REQ_SET_CUR 0x01
#define AUDIO_REQ_GET_CUR 0x81
#define AUDIO_REQ_SET_MIN 0x02
#define AUDIO_REQ_GET_MIN 0x82
#define AUDIO_REQ_SET_MAX 0x03
#define AUDIO_REQ_GET_MAX 0x83
#define AUDIO_REQ_SET_RES 0x04
#define AUDIO_REQ_GET_RES 0x84
#define AUDIO_REQ_SET_MEM 0x05
#define AUDIO_REQ_GET_MEM 0x85
#define AUDIO_REQ_GET_STAT 0xFF
/**
* @brief audio feature unit control selectors
*/
#define AUDIO_FU_CONTROL_UNDEFINED 0x00
#define AUDIO_FU_MUTE_CONTROL 0x01
#define AUDIO_FU_VOLUME_CONTROL 0x02
#define AUDIO_FU_BASS_CONTROL 0x03
#define AUDIO_FU_MID_CONTROL 0x04
#define AUDIO_FU_TREBLE_CONTROL 0x05
/**
* @brief audio terminal type
*/
#define AUDIO_TERMINAL_TYPE_UNDEFINED 0x0100
#define AUDIO_TERMINAL_TYPE_STREAMING 0x0101
#define AUDIO_TERMINAL_TYPE_VENDOR 0x01FF
#define AUDIO_INPUT_TERMINAL_UNDEFINED 0x0200
#define AUDIO_INPUT_TERMINAL_MICROPHONE 0x0201
#define AUDIO_OUTPUT_TERMINAL_UNDEFINED 0x0300
#define AUDIO_OUTPUT_TERMINAL_SPEAKER 0x0301
/**
* @brief audio format type 1
*/
#define AUDIO_FORMAT_TYPE_I 0x01
/**
* @brief audio interface config
*/
#define AUDIO_INTERFACE_NUM (AUDIO_SUPPORT_SPK + AUDIO_SUPPORT_MIC)
#define AUDIO_INTERFACE_LEN ((0x08 + AUDIO_INTERFACE_NUM) + AUDIO_INTERFACE_NUM * 0x1E)
#define AUDIO_MIC_INTERFACE 0x01
#define AUDIO_SPK_INTERFACE 0x02
/**
* @brief audio interface descriptor size define
*/
#define AUDIO_INPUT_TERMINAL_SIZE 0x0C
#define AUDIO_OUTPUT_TERMINAL_SIZE 0x09
#define AUDIO_FEATURE_UNIT_SIZE 0x09
/**
* @brief audio terminal id define
*/
#define AUDIO_MIC_INPUT_TERMINAL_ID 0x01
#define AUDIO_MIC_FEATURE_UNIT_ID 0x02
#define AUDIO_MIC_OUTPUT_TERMINAL_ID 0x03
#define AUDIO_SPK_INPUT_TERMINAL_ID 0x04
#define AUDIO_SPK_FEATURE_UNIT_ID 0x05
#define AUDIO_SPK_OUTPUT_TERMINAL_ID 0x06
/**
* @brief audio interface number
*/
#define AUDIO_MIC_INTERFACE_NUMBER 0x01
#if (AUDIO_SUPPORT_MIC == 1)
#define AUDIO_SPK_INTERFACE_NUMBER 0x02
#else
#define AUDIO_SPK_INTERFACE_NUMBER 0x01
#endif
#define HID_INTERFACE_NUMBER 0x03
/**
* @brief audio support freq
*/
#define AT32_AUDIO_FREQ_16K 16000
#define AT32_AUDIO_FREQ_48K 48000
/**
* @brief audio microphone freq and channel config
*/
#define AUDIO_MIC_FREQ_SIZE (AUDIO_SUPPORT_FREQ)
#define AUDIO_MIC_CHR AUDIO_MIC_CHANEL_NUM
#define AUDIO_MIC_BITW (AUDIO_MIC_DEFAULT_BITW)
/**
* @brief audio speaker freq and channel config
*/
#define AUDIO_SPK_FREQ_SIZE (AUDIO_SUPPORT_FREQ)
#define AUDIO_SPK_CHR AUDIO_SPK_CHANEL_NUM
#define AUDIO_SPK_BITW (AUDIO_SPK_DEFAULT_BITW)
#define HID_BINTERVAL_TIME 0x01
/**
* @brief usb bcd number define
*/
#define HID_BCD_NUM 0x0110
/**
* @brief usb hid class descriptor define
*/
#define HID_CLASS_DESC_HID 0x21
#define HID_CLASS_DESC_REPORT 0x22
#define HID_CLASS_DESC_PHYSICAL 0x23
/**
* @brief usb hid report id define
*/
#define HID_REPORT_ID_1 0x01
#define HID_REPORT_ID_2 0x02
#define HID_REPORT_ID_3 0x03
#define HID_REPORT_ID_4 0x04
#define HID_REPORT_ID_5 0x05
#define HID_REPORT_ID_6 0xF0
#define USBD_HID_DESC_SIZE 32
#define USBD_CONFIG_DESC_SIZE ( 0x12 + AUDIO_INTERFACE_LEN + \
+ (0x31 + AUDIO_SPK_FREQ_SIZE * 3) \
+ (0x31 + AUDIO_MIC_FREQ_SIZE * 3) \
+ (9 * AUDIO_SUPPORT_FEEDBACK) \
+ USBD_HID_DESC_SIZE)
#define USBD_HID_SIZ_REPORT_DESC 126
#define SAMPLE_FREQ(frq) (uint8_t)(frq), (uint8_t)((frq >> 8)), (uint8_t)((frq >> 16))
#define MCU_ID1 (0x1FFFF7E8)
#define MCU_ID2 (0x1FFFF7EC)
#define MCU_ID3 (0x1FFFF7F0)
extern uint8_t g_usbd_hid_report[USBD_HID_SIZ_REPORT_DESC];
extern uint8_t g_hid_usb_desc[9];
extern uint8_t g_usbd_descriptor[USB_DEVICE_DESC_LEN];
extern uint8_t g_usbd_configuration[USBD_CONFIG_DESC_SIZE];
extern usbd_desc_handler audio_hid_desc_handler;
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,413 @@
/**
**************************************************************************
* @file cdc_class.c
* @version v2.0.0
* @date 2021-11-26
* @brief usb cdc class type
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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 "usbd_core.h"
#include "cdc_class.h"
#include "cdc_desc.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @defgroup USB_cdc_class
* @brief usb device class cdc demo
* @{
*/
/** @defgroup USB_cdc_class_private_functions
* @{
*/
usb_sts_type class_init_handler(void *udev);
usb_sts_type class_clear_handler(void *udev);
usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup);
usb_sts_type class_ept0_tx_handler(void *udev);
usb_sts_type class_ept0_rx_handler(void *udev);
usb_sts_type class_in_handler(void *udev, uint8_t ept_num);
usb_sts_type class_out_handler(void *udev, uint8_t ept_num);
usb_sts_type class_sof_handler(void *udev);
usb_sts_type class_event_handler(void *udev, usbd_event_type event);
void usb_vcp_cmd_process(void *udev, uint8_t cmd, uint8_t *buff, uint16_t len);
/* usb rx and tx buffer */
static uint32_t alt_setting = 0;
static uint8_t g_rx_buff[USBD_OUT_MAXPACKET_SIZE];
//static uint8_t g_tx_buff[USBD_IN_MAXPACKET_SIZE];
static uint8_t g_cmd[USBD_CMD_MAXPACKET_SIZE];
static uint8_t g_req;
static uint16_t g_len, g_rxlen;
__IO uint8_t g_tx_completed = 1, g_rx_completed = 0;
linecoding_type linecoding =
{
115200,
0x00,
0x00,
0x08
};
/* static variable */
/* usb device class handler */
usbd_class_handler class_handler =
{
class_init_handler,
class_clear_handler,
class_setup_handler,
class_ept0_tx_handler,
class_ept0_rx_handler,
class_in_handler,
class_out_handler,
class_sof_handler,
class_event_handler,
};
/**
* @brief initialize usb custom hid endpoint
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_init_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
/* open in endpoint */
usbd_ept_open(pudev, USBD_CDC_INT_EPT, EPT_INT_TYPE, USBD_CMD_MAXPACKET_SIZE);
/* open in endpoint */
usbd_ept_open(pudev, USBD_CDC_BULK_IN_EPT, EPT_BULK_TYPE, USBD_IN_MAXPACKET_SIZE);
/* open out endpoint */
usbd_ept_open(pudev, USBD_CDC_BULK_OUT_EPT, EPT_BULK_TYPE, USBD_OUT_MAXPACKET_SIZE);
/* set out endpoint to receive status */
usbd_ept_recv(pudev, USBD_CDC_BULK_OUT_EPT, g_rx_buff, USBD_OUT_MAXPACKET_SIZE);
g_tx_completed = 1;
return status;
}
/**
* @brief clear endpoint or other state
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_clear_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
/* close in endpoint */
usbd_ept_close(pudev, USBD_CDC_INT_EPT);
/* close in endpoint */
usbd_ept_close(pudev, USBD_CDC_BULK_IN_EPT);
/* close out endpoint */
usbd_ept_close(pudev, USBD_CDC_BULK_OUT_EPT);
return status;
}
/**
* @brief usb device class setup request handler
* @param udev: to the structure of usbd_core_type
* @param setup: setup packet
* @retval status of usb_sts_type
*/
usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
switch(setup->bmRequestType & USB_REQ_TYPE_RESERVED)
{
/* class request */
case USB_REQ_TYPE_CLASS:
if(setup->wLength)
{
if(setup->bmRequestType & USB_REQ_DIR_DTH)
{
usb_vcp_cmd_process(udev, setup->bRequest, g_cmd, setup->wLength);
usbd_ctrl_send(pudev, g_cmd, setup->wLength);
}
else
{
g_req = setup->bRequest;
g_len = setup->wLength;
usbd_ctrl_recv(pudev, g_cmd, g_len);
}
}
break;
/* standard request */
case USB_REQ_TYPE_STANDARD:
switch(setup->bRequest)
{
case USB_STD_REQ_GET_DESCRIPTOR:
usbd_ctrl_unsupport(pudev);
break;
case USB_STD_REQ_GET_INTERFACE:
usbd_ctrl_send(pudev, (uint8_t *)&alt_setting, 1);
break;
case USB_STD_REQ_SET_INTERFACE:
alt_setting = setup->wValue;
break;
default:
break;
}
break;
default:
usbd_ctrl_unsupport(pudev);
break;
}
return status;
}
/**
* @brief usb device endpoint 0 in status stage complete
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_ept0_tx_handler(void *udev)
{
usb_sts_type status = USB_OK;
/* ...user code... */
return status;
}
/**
* @brief usb device endpoint 0 out status stage complete
* @param udev: usb device core handler type
* @retval status of usb_sts_type
*/
usb_sts_type class_ept0_rx_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
uint32_t recv_len = usbd_get_recv_len(pudev, 0);
/* ...user code... */
if( g_req == SET_LINE_CODING)
{
/* class process */
usb_vcp_cmd_process(udev, g_req, g_cmd, recv_len);
}
return status;
}
/**
* @brief usb device transmision complete handler
* @param udev: to the structure of usbd_core_type
* @param ept_num: endpoint number
* @retval status of usb_sts_type
*/
usb_sts_type class_in_handler(void *udev, uint8_t ept_num)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
usb_sts_type status = USB_OK;
/* ...user code...
trans next packet data
*/
usbd_flush_tx_fifo(pudev, ept_num);
g_tx_completed = 1;
return status;
}
/**
* @brief usb device endpoint receive data
* @param udev: to the structure of usbd_core_type
* @param ept_num: endpoint number
* @retval status of usb_sts_type
*/
usb_sts_type class_out_handler(void *udev, uint8_t ept_num)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
/* get endpoint receive data length */
g_rxlen = usbd_get_recv_len(pudev, ept_num);
/*set recv flag*/
g_rx_completed = 1;
return status;
}
/**
* @brief usb device sof handler
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_sof_handler(void *udev)
{
usb_sts_type status = USB_OK;
/* ...user code... */
return status;
}
/**
* @brief usb device event handler
* @param udev: to the structure of usbd_core_type
* @param event: usb device event
* @retval status of usb_sts_type
*/
usb_sts_type class_event_handler(void *udev, usbd_event_type event)
{
usb_sts_type status = USB_OK;
switch(event)
{
case USBD_RESET_EVENT:
/* ...user code... */
break;
case USBD_SUSPEND_EVENT:
/* ...user code... */
break;
case USBD_WAKEUP_EVENT:
/* ...user code... */
break;
case USBD_INISOINCOM_EVENT:
break;
case USBD_OUTISOINCOM_EVENT:
break;
default:
break;
}
return status;
}
/**
* @brief usb device class rx data process
* @param udev: to the structure of usbd_core_type
* @param recv_data: receive buffer
* @retval receive data len
*/
uint16_t usb_vcp_get_rxdata(void *udev, uint8_t *recv_data)
{
uint16_t i_index = 0;
uint16_t tmp_len = g_rxlen;
usbd_core_type *pudev = (usbd_core_type *)udev;
if(g_rx_completed == 0)
{
return 0;
}
g_rx_completed = 0;
tmp_len = g_rxlen;
for(i_index = 0; i_index < g_rxlen; i_index ++)
{
recv_data[i_index] = g_rx_buff[i_index];
}
usbd_ept_recv(pudev, USBD_CDC_BULK_OUT_EPT, g_rx_buff, USBD_OUT_MAXPACKET_SIZE);
return tmp_len;
}
/**
* @brief usb device class send data
* @param udev: to the structure of usbd_core_type
* @param send_data: send data buffer
* @param len: send length
* @retval error status
*/
error_status usb_vcp_send_data(void *udev, uint8_t *send_data, uint16_t len)
{
error_status status = SUCCESS;
usbd_core_type *pudev = (usbd_core_type *)udev;
if(g_tx_completed)
{
g_tx_completed = 0;
usbd_ept_send(pudev, USBD_CDC_BULK_IN_EPT, send_data, len);
}
else
{
status = ERROR;
}
return status;
}
/**
* @brief usb device function
* @param udev: to the structure of usbd_core_type
* @param cmd: request number
* @param buff: request buffer
* @param len: buffer length
* @retval none
*/
void usb_vcp_cmd_process(void *udev, uint8_t cmd, uint8_t *buff, uint16_t len)
{
switch(cmd)
{
case SET_LINE_CODING:
linecoding.bitrate = (uint32_t)(buff[0] | (buff[1] << 8) | (buff[2] << 16) | (buff[3] <<24));
linecoding.format = buff[4];
linecoding.parity = buff[5];
linecoding.data = buff[6];
break;
case GET_LINE_CODING:
buff[0] = (uint8_t)linecoding.bitrate;
buff[1] = (uint8_t)(linecoding.bitrate >> 8);
buff[2] = (uint8_t)(linecoding.bitrate >> 16);
buff[3] = (uint8_t)(linecoding.bitrate >> 24);
buff[4] = (uint8_t)(linecoding.format);
buff[5] = (uint8_t)(linecoding.parity);
buff[6] = (uint8_t)(linecoding.data);
break;
default:
break;
}
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/

View File

@@ -0,0 +1,119 @@
/**
**************************************************************************
* @file cdc_class.h
* @version v2.0.0
* @date 2021-11-26
* @brief usb cdc class file
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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.
*
**************************************************************************
*/
/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __CDC_CLASS_H
#define __CDC_CLASS_H
#ifdef __cplusplus
extern "C" {
#endif
#include "usb_std.h"
#include "usbd_core.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @addtogroup USB_cdc_class
* @{
*/
/** @defgroup USB_cdc_class_definition
* @{
*/
/**
* @brief usb cdc use endpoint define
*/
#define USBD_CDC_INT_EPT 0x82
#define USBD_CDC_BULK_IN_EPT 0x81
#define USBD_CDC_BULK_OUT_EPT 0x01
/**
* @brief usb cdc in and out max packet size define
*/
#define USBD_IN_MAXPACKET_SIZE 0x40
#define USBD_OUT_MAXPACKET_SIZE 0x40
#define USBD_CMD_MAXPACKET_SIZE 0x08
/**
* @brief usb cdc class request code define
*/
#define SET_LINE_CODING 0x20
#define GET_LINE_CODING 0x21
/**
* @}
*/
/** @defgroup USB_cdc_class_exported_types
* @{
*/
/**
* @brief usb cdc class set line coding struct
*/
typedef struct
{
uint32_t bitrate; /* line coding baud rate */
uint8_t format; /* line coding foramt */
uint8_t parity; /* line coding parity */
uint8_t data; /* line coding data bit */
}linecoding_type;
/**
* @}
*/
/** @defgroup USB_cdc_class_exported_functions
* @{
*/
extern usbd_class_handler class_handler;
uint16_t usb_vcp_get_rxdata(void *udev, uint8_t *recv_data);
error_status usb_vcp_send_data(void *udev, uint8_t *send_data, uint16_t len);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,434 @@
/**
**************************************************************************
* @file cdc_desc.c
* @version v2.0.0
* @date 2021-11-26
* @brief usb cdc device descriptor
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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 "stdio.h"
#include "usb_std.h"
#include "usbd_sdr.h"
#include "usbd_core.h"
#include "cdc_desc.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @defgroup USB_cdc_desc
* @brief usb device cdc descriptor
* @{
*/
/** @defgroup USB_cdc_desc_private_functions
* @{
*/
usbd_desc_t *get_device_descriptor(void);
usbd_desc_t *get_device_qualifier(void);
usbd_desc_t *get_device_configuration(void);
usbd_desc_t *get_device_other_speed(void);
usbd_desc_t *get_device_lang_id(void);
usbd_desc_t *get_device_manufacturer_string(void);
usbd_desc_t *get_device_product_string(void);
usbd_desc_t *get_device_serial_string(void);
usbd_desc_t *get_device_interface_string(void);
usbd_desc_t *get_device_config_string(void);
uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf);
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 device descriptor handler structure
*/
usbd_desc_handler 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
*/
uint8_t g_usbd_descriptor[USB_DEVICE_DESC_LEN] =
{
USB_DEVICE_DESC_LEN, /* bLength */
USB_DESCIPTOR_TYPE_DEVICE, /* bDescriptorType */
0x00, /* bcdUSB */
0x02,
0x02, /* bDeviceClass */
0x00, /* bDeviceSubClass */
0x00, /* bDeviceProtocol */
USB_MAX_EP0_SIZE, /* bMaxPacketSize */
LBYTE(USBD_VENDOR_ID), /* idVendor */
HBYTE(USBD_VENDOR_ID), /* idVendor */
LBYTE(USBD_PRODUCT_ID), /* idProduct */
HBYTE(USBD_PRODUCT_ID), /* idProduct */
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
*/
uint8_t g_usbd_configuration[USBD_CONFIG_DESC_SIZE] =
{
USB_DEVICE_CFG_DESC_LEN, /* bLength: configuration descriptor size */
USB_DESCIPTOR_TYPE_CONFIGURATION, /* bDescriptorType: configuration */
LBYTE(USBD_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
HBYTE(USBD_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
0x02, /* bNumInterfaces: 2 interface */
0x01, /* bConfigurationValue: configuration value */
0x00, /* iConfiguration: index of string descriptor describing
the configuration */
0xC0, /* bmAttributes: self powered */
0x32, /* MaxPower 100 mA: this current is used for detecting vbus */
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_CDC, /* bInterfaceClass: CDC class code */
0x02, /* bInterfaceSubClass: subclass code, Abstract Control Model*/
0x01, /* bInterfaceProtocol: protocol code, AT Command */
0x00, /* iInterface: index of string descriptor */
0x05, /* bFunctionLength: size of this descriptor in bytes */
USBD_CDC_CS_INTERFACE, /* bDescriptorType: CDC interface descriptor type */
USBD_CDC_SUBTYPE_HEADER, /* bDescriptorSubtype: Header function Descriptor 0x00*/
LBYTE(BCD_NUM),
HBYTE(BCD_NUM), /* bcdCDC: USB class definitions for communications */
0x05, /* bFunctionLength: size of this descriptor in bytes */
USBD_CDC_CS_INTERFACE, /* bDescriptorType: CDC interface descriptor type */
USBD_CDC_SUBTYPE_CMF, /* bDescriptorSubtype: Call Management function descriptor subtype 0x01 */
0x00, /* bmCapabilities: 0x00*/
0x01, /* bDataInterface: interface number of data class interface optionally used for call management */
0x04, /* bFunctionLength: size of this descriptor in bytes */
USBD_CDC_CS_INTERFACE, /* bDescriptorType: CDC interface descriptor type */
USBD_CDC_SUBTYPE_ACM, /* bDescriptorSubtype: Abstract Control Management functional descriptor subtype 0x02 */
0x02, /* bmCapabilities: Support Set_Line_Coding and Get_Line_Coding 0x02 */
0x05, /* bFunctionLength: size of this descriptor in bytes */
USBD_CDC_CS_INTERFACE, /* bDescriptorType: CDC interface descriptor type */
USBD_CDC_SUBTYPE_UFD, /* bDescriptorSubtype: Union Function Descriptor subtype 0x06 */
0x00, /* bControlInterface: The interface number of the communications or data class interface 0x00 */
0x01, /* bSubordinateInterface0: interface number of first subordinate interface in the union */
USB_DEVICE_EPT_LEN, /* bLength: size of endpoint descriptor in bytes */
USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
USBD_CDC_INT_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
USB_EPT_DESC_INTERRUPT, /* bmAttributes: endpoint attributes */
LBYTE(USBD_CMD_MAXPACKET_SIZE),
HBYTE(USBD_CMD_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
HID_BINTERVAL_TIME, /* bInterval: interval for polling endpoint for data transfers */
USB_DEVICE_IF_DESC_LEN, /* bLength: interface descriptor size */
USB_DESCIPTOR_TYPE_INTERFACE, /* bDescriptorType: interface descriptor type */
0x01, /* bInterfaceNumber: number of interface */
0x00, /* bAlternateSetting: alternate set */
0x02, /* bNumEndpoints: number of endpoints */
USB_CLASS_CODE_CDCDATA, /* bInterfaceClass: CDC-data class code */
0x00, /* bInterfaceSubClass: Data interface subclass code 0x00*/
0x00, /* bInterfaceProtocol: data class protocol code 0x00 */
0x00, /* iInterface: index of string descriptor */
USB_DEVICE_EPT_LEN, /* bLength: size of endpoint descriptor in bytes */
USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
USBD_CDC_BULK_IN_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
USB_EPT_DESC_BULK, /* bmAttributes: endpoint attributes */
LBYTE(USBD_IN_MAXPACKET_SIZE),
HBYTE(USBD_IN_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
0x00, /* bInterval: interval for polling endpoint for data transfers */
USB_DEVICE_EPT_LEN, /* bLength: size of endpoint descriptor in bytes */
USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
USBD_CDC_BULK_OUT_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
USB_EPT_DESC_BULK, /* bmAttributes: endpoint attributes */
LBYTE(USBD_OUT_MAXPACKET_SIZE),
HBYTE(USBD_OUT_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
0x00, /* bInterval: interval for polling endpoint for data transfers */
};
/**
* @brief usb string lang id
*/
uint8_t g_string_lang_id[USBD_SIZ_STRING_LANGID] =
{
USBD_SIZ_STRING_LANGID,
USB_DESCIPTOR_TYPE_STRING,
0x09,
0x04,
};
/**
* @brief usb string serial
*/
uint8_t g_string_serial[USBD_SIZ_STRING_SERIAL] =
{
USBD_SIZ_STRING_SERIAL,
USB_DESCIPTOR_TYPE_STRING,
};
/* device descriptor */
usbd_desc_t device_descriptor =
{
USB_DEVICE_DESC_LEN,
g_usbd_descriptor
};
/* config descriptor */
usbd_desc_t config_descriptor =
{
USBD_CONFIG_DESC_SIZE,
g_usbd_configuration
};
/* langid descriptor */
usbd_desc_t langid_descriptor =
{
USBD_SIZ_STRING_LANGID,
g_string_lang_id
};
/* serial descriptor */
usbd_desc_t serial_descriptor =
{
USBD_SIZ_STRING_SERIAL,
g_string_serial
};
usbd_desc_t vp_desc;
/**
* @brief standard usb unicode convert
* @param string: source string
* @param unicode_buf: unicode buffer
* @retval length
*/
uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf)
{
uint16_t str_len = 0, id_pos = 2;
uint8_t *tmp_str = string;
while(*tmp_str != '\0')
{
str_len ++;
unicode_buf[id_pos ++] = *tmp_str ++;
unicode_buf[id_pos ++] = 0x00;
}
str_len = str_len * 2 + 2;
unicode_buf[0] = (uint8_t)str_len;
unicode_buf[1] = USB_DESCIPTOR_TYPE_STRING;
return str_len;
}
/**
* @brief usb int convert to unicode
* @param value: int value
* @param pbus: unicode buffer
* @param len: length
* @retval none
*/
static void usbd_int_to_unicode (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;
}
}
/**
* @brief usb get serial number
* @param none
* @retval none
*/
static void get_serial_num(void)
{
uint32_t serial0, serial1, serial2;
serial0 = *(uint32_t*)MCU_ID1;
serial1 = *(uint32_t*)MCU_ID2;
serial2 = *(uint32_t*)MCU_ID3;
serial0 += serial2;
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
* @retval usbd_desc
*/
usbd_desc_t *get_device_descriptor(void)
{
return &device_descriptor;
}
/**
* @brief get device qualifier
* @param none
* @retval usbd_desc
*/
usbd_desc_t * get_device_qualifier(void)
{
return NULL;
}
/**
* @brief get config descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_configuration(void)
{
return &config_descriptor;
}
/**
* @brief get other speed descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_other_speed(void)
{
return NULL;
}
/**
* @brief get lang id descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_lang_id(void)
{
return &langid_descriptor;
}
/**
* @brief get manufacturer descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_manufacturer_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_MANUFACTURER_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get product descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_product_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_PRODUCT_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get serial descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_serial_string(void)
{
get_serial_num();
return &serial_descriptor;
}
/**
* @brief get interface descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_interface_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_INTERFACE_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get device config descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_config_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_CONFIGURATION_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/

View File

@@ -0,0 +1,122 @@
/**
**************************************************************************
* @file cdc_desc.h
* @version v2.0.0
* @date 2021-11-26
* @brief usb cdc descriptor header file
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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.
*
**************************************************************************
*/
/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __CDC_DESC_H
#define __CDC_DESC_H
#ifdef __cplusplus
extern "C" {
#endif
#include "cdc_class.h"
#include "usbd_core.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @addtogroup USB_cdc_desc
* @{
*/
/** @defgroup USB_cdc_desc_definition
* @{
*/
/**
* @brief usb bcd number define
*/
#define BCD_NUM 0x0110
/**
* @brief usb vendor id and product id define
*/
#define USBD_VENDOR_ID 0x2E3C
#define USBD_PRODUCT_ID 0x5740
/**
* @brief usb descriptor size define
*/
#define USBD_CONFIG_DESC_SIZE 67
#define USBD_SIZ_STRING_LANGID 4
#define USBD_SIZ_STRING_SERIAL 0x1A
/**
* @brief usb string define(vendor, product configuration, interface)
*/
#define USBD_DESC_MANUFACTURER_STRING "Artery"
#define USBD_DESC_PRODUCT_STRING "AT32 Virtual Com Port "
#define USBD_DESC_CONFIGURATION_STRING "Virtual ComPort Config"
#define USBD_DESC_INTERFACE_STRING "Virtual ComPort Interface"
/**
* @brief usb endpoint interval define
*/
#define HID_BINTERVAL_TIME 0xFF
/**
* @brief usb hid class descriptor define
*/
#define USBD_CDC_CS_INTERFACE 0x24
#define USBD_CDC_CS_ENDPOINT 0x25
/**
* @brief usb hid class sub-type define
*/
#define USBD_CDC_SUBTYPE_HEADER 0x00
#define USBD_CDC_SUBTYPE_CMF 0x01
#define USBD_CDC_SUBTYPE_ACM 0x02
#define USBD_CDC_SUBTYPE_UFD 0x06
/**
* @brief usb mcu id address deine
*/
#define MCU_ID1 (0x1FFFF7E8)
#define MCU_ID2 (0x1FFFF7EC)
#define MCU_ID3 (0x1FFFF7F0)
/**
* @}
*/
extern uint8_t g_usbd_descriptor[USB_DEVICE_DESC_LEN];
extern uint8_t g_usbd_configuration[USBD_CONFIG_DESC_SIZE];
extern usbd_desc_handler desc_handler;
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,736 @@
/**
**************************************************************************
* @file cdc_keyboard_class.c
* @version v2.0.0
* @date 2021-11-26
* @brief usb cdc and keyboard class type
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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 "usbd_core.h"
#include "cdc_keyboard_class.h"
#include "cdc_keyboard_desc.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @defgroup USB_cdc_keyboard_class
* @brief usb device class composite cdc and keyboard
* @{
*/
/** @defgroup USB_cdc_keyboard_class_private_functions
* @{
*/
usb_sts_type class_init_handler(void *udev);
usb_sts_type class_clear_handler(void *udev);
usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup);
usb_sts_type class_ept0_tx_handler(void *udev);
usb_sts_type class_ept0_rx_handler(void *udev);
usb_sts_type class_in_handler(void *udev, uint8_t ept_num);
usb_sts_type class_out_handler(void *udev, uint8_t ept_num);
usb_sts_type class_sof_handler(void *udev);
usb_sts_type class_event_handler(void *udev, usbd_event_type event);
usb_sts_type cdc_class_setup_handler(void *udev, usb_setup_type *setup);
usb_sts_type keyboard_class_setup_handler(void *udev, usb_setup_type *setup);
void usb_vcp_cmd_process(void *udev, uint8_t cmd, uint8_t *buff, uint16_t len);
/* usb rx and tx buffer */
static uint32_t alt_setting = 0;
static uint32_t hid_protocol = 0;
static uint32_t hid_set_idle = 0;
static uint8_t hid_state;
uint8_t hid_set_report[64];
static uint8_t g_rx_buff[USBD_OUT_MAXPACKET_SIZE];
static uint8_t g_cmd[USBD_CMD_MAXPACKET_SIZE];
static uint8_t g_req;
static uint16_t g_len, g_rxlen;
__IO uint8_t g_tx_completed = 1, g_rx_completed = 0;
uint8_t g_keyboard_tx_completed = 0;
#define SHIFT 0x80
const unsigned char _asciimap[128] =
{
0x00,// NUL
0x00,// SOH
0x00,// STX
0x00,// ETX
0x00,// EOT
0x00,// ENQ
0x00,// ACK
0x00,// BEL
0x2A,// BS Backspace
0x2B,// TAB Tab
0x28,// LF Enter
0x00,// VT
0x00,// FF
0x00,// CR
0x00,// SO
0x00,// SI
0x00,// DEL
0x00,// DC1
0x00,// DC2
0x00,// DC3
0x00,// DC4
0x00,// NAK
0x00,// SYN
0x00,// ETB
0x00,// CAN
0x00,// EM
0x00,// SUB
0x00,// ESC
0x00,// FS
0x00,// GS
0x00,// RS
0x00,// US
0x2C,// ' '
0x1E|SHIFT,// !
0x34|SHIFT,// "
0x20|SHIFT,// #
0x21|SHIFT,// $
0x22|SHIFT,// %
0x24|SHIFT,// &
0x34, // '
0x26|SHIFT,// (
0x27|SHIFT,// )
0x25|SHIFT,// *
0x2E|SHIFT,// +
0x36,// ,
0x2D,// -
0x37,// .
0x38,// /
0x27,// 0
0x1E,// 1
0x1F,// 2
0x20,// 3
0x21,// 4
0x22,// 5
0x23,// 6
0x24,// 7
0x25,// 8
0x26,// 9
0x33|SHIFT,// :
0x33, // ;
0x36|SHIFT,// <
0x2E, // =
0x37|SHIFT,// >
0x38|SHIFT,// ?
0x1F|SHIFT,// @
0x04|SHIFT,// A
0x05|SHIFT,// B
0x06|SHIFT,// C
0x07|SHIFT,// D
0x08|SHIFT,// E
0x09|SHIFT,// F
0x0A|SHIFT,// G
0x0B|SHIFT,// H
0x0C|SHIFT,// I
0x0D|SHIFT,// J
0x0E|SHIFT,// K
0x0F|SHIFT,// L
0x10|SHIFT,// M
0x11|SHIFT,// N
0x12|SHIFT,// O
0x13|SHIFT,// P
0x14|SHIFT,// Q
0x15|SHIFT,// R
0x16|SHIFT,// S
0x17|SHIFT,// T
0x18|SHIFT,// U
0x19|SHIFT,// V
0x1A|SHIFT,// W
0x1B|SHIFT,// X
0x1C|SHIFT,// Y
0x1D|SHIFT,// Z
0x2F, // [
0x31, // bslash
0x30, // ]
0x23|SHIFT,// ^
0x2D|SHIFT,// _
0x35, // `
0x04, // a
0x05, // b
0x06, // c
0x07, // d
0x08, // e
0x09, // f
0x0A, // g
0x0B, // h
0x0C, // i
0x0D, // j
0x0E, // k
0x0F, // l
0x10, // m
0x11, // n
0x12, // o
0x13, // p
0x14, // q
0x15, // r
0x16, // s
0x17, // t
0x18, // u
0x19, // v
0x1A, // w
0x1B, // x
0x1C, // y
0x1D, // z
0x2f|SHIFT,//
0x31|SHIFT,// |
0x30|SHIFT,// }
0x35|SHIFT,// ~
0 // DEL
};
linecoding_type linecoding =
{
115200,
0x00,
0x00,
0x08
};
/* static variable */
/* usb device class handler */
usbd_class_handler cdc_keyboard_class_handler =
{
class_init_handler,
class_clear_handler,
class_setup_handler,
class_ept0_tx_handler,
class_ept0_rx_handler,
class_in_handler,
class_out_handler,
class_sof_handler,
class_event_handler,
};
/**
* @brief initialize usb endpoint
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_init_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
/* open in endpoint */
usbd_ept_open(pudev, USBD_CDC_INT_EPT, EPT_INT_TYPE, USBD_CMD_MAXPACKET_SIZE);
/* open in endpoint */
usbd_ept_open(pudev, USBD_CDC_BULK_IN_EPT, EPT_BULK_TYPE, USBD_OUT_MAXPACKET_SIZE);
/* open out endpoint */
usbd_ept_open(pudev, USBD_CDC_BULK_OUT_EPT, EPT_BULK_TYPE, USBD_OUT_MAXPACKET_SIZE);
/* open hid in endpoint */
usbd_ept_open(pudev, USBD_HID_IN_EPT, EPT_INT_TYPE, USBD_OUT_MAXPACKET_SIZE);
/* set out endpoint to receive status */
usbd_ept_recv(pudev, USBD_CDC_BULK_OUT_EPT, g_rx_buff, USBD_OUT_MAXPACKET_SIZE);
g_tx_completed = 1;
g_keyboard_tx_completed = 1;
return status;
}
/**
* @brief clear endpoint or other state
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_clear_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
/* close in endpoint */
usbd_ept_close(pudev, USBD_CDC_INT_EPT);
/* close in endpoint */
usbd_ept_close(pudev, USBD_CDC_BULK_IN_EPT);
/* close out endpoint */
usbd_ept_close(pudev, USBD_CDC_BULK_OUT_EPT);
/* close in endpoint */
usbd_ept_close(pudev, USBD_HID_IN_EPT);
return status;
}
/**
* @brief usb device class setup request handler
* @param udev: to the structure of usbd_core_type
* @param setup: setup packet
* @retval status of usb_sts_type
*/
usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
switch(setup->bmRequestType & USB_REQ_RECIPIENT_MASK)
{
case USB_REQ_RECIPIENT_INTERFACE:
if(setup->wIndex == HID_KEYBOARD_INTERFACE)
{
keyboard_class_setup_handler(udev, setup);
}
else
{
cdc_class_setup_handler(pudev, setup);
}
break;
case USB_REQ_RECIPIENT_ENDPOINT:
break;
}
return status;
}
/**
* @brief usb device class setup request handler
* @param udev: to the structure of usbd_core_type
* @param setup: setup packet
* @retval status of usb_sts_type
*/
usb_sts_type cdc_class_setup_handler(void *udev, usb_setup_type *setup)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
switch(setup->bmRequestType & USB_REQ_TYPE_RESERVED)
{
/* class request */
case USB_REQ_TYPE_CLASS:
if(setup->wLength)
{
if(setup->bmRequestType & USB_REQ_DIR_DTH)
{
usb_vcp_cmd_process(udev, setup->bRequest, g_cmd, setup->wLength);
usbd_ctrl_send(pudev, g_cmd, setup->wLength);
}
else
{
g_req = setup->bRequest;
g_len = setup->wLength;
usbd_ctrl_recv(pudev, g_cmd, g_len);
}
}
break;
/* standard request */
case USB_REQ_TYPE_STANDARD:
switch(setup->bRequest)
{
case USB_STD_REQ_GET_DESCRIPTOR:
usbd_ctrl_unsupport(pudev);
break;
case USB_STD_REQ_GET_INTERFACE:
usbd_ctrl_send(pudev, (uint8_t *)&alt_setting, 1);
break;
case USB_STD_REQ_SET_INTERFACE:
alt_setting = setup->wValue;
break;
default:
break;
}
break;
default:
usbd_ctrl_unsupport(pudev);
break;
}
return status;
}
/**
* @brief usb device class setup request handler
* @param udev: to the structure of usbd_core_type
* @param setup: setup packet
* @retval status of usb_sts_type
*/
usb_sts_type keyboard_class_setup_handler(void *udev, usb_setup_type *setup)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
uint16_t len;
uint8_t *buf;
switch(setup->bmRequestType & USB_REQ_TYPE_RESERVED)
{
/* class request */
case USB_REQ_TYPE_CLASS:
switch(setup->bRequest)
{
case HID_REQ_SET_PROTOCOL:
hid_protocol = (uint8_t)setup->wValue;
break;
case HID_REQ_GET_PROTOCOL:
usbd_ctrl_send(pudev, (uint8_t *)&hid_protocol, 1);
break;
case HID_REQ_SET_IDLE:
hid_set_idle = (uint8_t)(setup->wValue >> 8);
break;
case HID_REQ_GET_IDLE:
usbd_ctrl_send(pudev, (uint8_t *)&hid_set_idle, 1);
break;
case HID_REQ_SET_REPORT:
hid_state = HID_REQ_SET_REPORT;
usbd_ctrl_recv(pudev, hid_set_report, setup->wLength);
break;
default:
usbd_ctrl_unsupport(pudev);
break;
}
break;
/* standard request */
case USB_REQ_TYPE_STANDARD:
switch(setup->bRequest)
{
case USB_STD_REQ_GET_DESCRIPTOR:
if(setup->wValue >> 8 == HID_REPORT_DESC)
{
len = MIN(USBD_HID_SIZ_REPORT_DESC, setup->wLength);
buf = (uint8_t *)g_usbd_hid_report;
}
else if(setup->wValue >> 8 == HID_DESCRIPTOR_TYPE)
{
len = MIN(9, setup->wLength);
buf = (uint8_t *)g_hid_usb_desc;
}
usbd_ctrl_send(pudev, (uint8_t *)buf, len);
break;
case USB_STD_REQ_GET_INTERFACE:
usbd_ctrl_send(pudev, (uint8_t *)&alt_setting, 1);
break;
case USB_STD_REQ_SET_INTERFACE:
alt_setting = setup->wValue;
break;
default:
break;
}
break;
default:
usbd_ctrl_unsupport(pudev);
break;
}
return status;
}
/**
* @brief usb device class endpoint 0 in status stage complete
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_ept0_tx_handler(void *udev)
{
usb_sts_type status = USB_OK;
/* ...user code... */
return status;
}
/**
* @brief usb device class endpoint 0 out status stage complete
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_ept0_rx_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
uint32_t recv_len = usbd_get_recv_len(pudev, 0);
/* ...user code... */
if( g_req == SET_LINE_CODING)
{
/* class process */
usb_vcp_cmd_process(udev, g_req, g_cmd, recv_len);
}
if( hid_state == HID_REQ_SET_REPORT)
{
/* hid buffer process */
hid_state = 0;
}
return status;
}
/**
* @brief usb device class transmision complete handler
* @param udev: to the structure of usbd_core_type
* @param ept_num: endpoint number
* @retval status of usb_sts_type
*/
usb_sts_type class_in_handler(void *udev, uint8_t ept_num)
{
usb_sts_type status = USB_OK;
/* ...user code...
trans next packet data
*/
if((ept_num & 0x7F) == (USBD_CDC_BULK_IN_EPT & 0x7F))
{
g_tx_completed = 1;
}
if((ept_num & 0x7F) == (USBD_HID_IN_EPT & 0x7F))
{
g_keyboard_tx_completed = 1;
}
return status;
}
/**
* @brief usb device class endpoint receive data
* @param udev: to the structure of usbd_core_type
* @param ept_num: endpoint number
* @retval status of usb_sts_type
*/
usb_sts_type class_out_handler(void *udev, uint8_t ept_num)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
/* get endpoint receive data length */
g_rxlen = usbd_get_recv_len(pudev, ept_num);
/*set recv flag*/
g_rx_completed = 1;
return status;
}
/**
* @brief usb device class sof handler
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_sof_handler(void *udev)
{
usb_sts_type status = USB_OK;
/* ...user code... */
return status;
}
/**
* @brief usb device class event handler
* @param udev: to the structure of usbd_core_type
* @param event: usb device event
* @retval status of usb_sts_type
*/
usb_sts_type class_event_handler(void *udev, usbd_event_type event)
{
usb_sts_type status = USB_OK;
switch(event)
{
case USBD_RESET_EVENT:
/* ...user code... */
break;
case USBD_SUSPEND_EVENT:
/* ...user code... */
break;
case USBD_WAKEUP_EVENT:
/* ...user code... */
break;
default:
break;
}
return status;
}
/**
* @brief usb device class rx data process
* @param udev: to the structure of usbd_core_type
* @param recv_data: receive buffer
* @retval receive data len
*/
uint16_t usb_vcp_get_rxdata(void *udev, uint8_t *recv_data)
{
uint16_t i_index = 0;
uint16_t tmp_len = g_rxlen;
usbd_core_type *pudev = (usbd_core_type *)udev;
if(g_rx_completed == 0)
{
return 0;
}
g_rx_completed = 0;
tmp_len = g_rxlen;
for(i_index = 0; i_index < g_rxlen; i_index ++)
{
recv_data[i_index] = g_rx_buff[i_index];
}
usbd_ept_recv(pudev, USBD_CDC_BULK_OUT_EPT, g_rx_buff, USBD_OUT_MAXPACKET_SIZE);
return tmp_len;
}
/**
* @brief usb device class send data
* @param udev: to the structure of usbd_core_type
* @param send_data: send data buffer
* @param len: send length
* @retval error status
*/
error_status usb_vcp_send_data(void *udev, uint8_t *send_data, uint16_t len)
{
error_status status = SUCCESS;
usbd_core_type *pudev = (usbd_core_type *)udev;
if(g_tx_completed)
{
g_tx_completed = 0;
usbd_ept_send(pudev, USBD_CDC_BULK_IN_EPT, send_data, len);
}
else
{
status = ERROR;
}
return status;
}
/**
* @brief usb device class request function
* @param udev: to the structure of usbd_core_type
* @param cmd: request number
* @param buff: request buffer
* @param len: buffer length
* @retval none
*/
void usb_vcp_cmd_process(void *udev, uint8_t cmd, uint8_t *buff, uint16_t len)
{
switch(cmd)
{
case SET_LINE_CODING:
linecoding.bitrate = (uint32_t)(buff[0] | (buff[1] << 8) | (buff[2] << 16) | (buff[3] <<24));
linecoding.format = buff[4];
linecoding.parity = buff[5];
linecoding.data = buff[6];
break;
case GET_LINE_CODING:
buff[0] = (uint8_t)linecoding.bitrate;
buff[1] = (uint8_t)(linecoding.bitrate >> 8);
buff[2] = (uint8_t)(linecoding.bitrate >> 16);
buff[3] = (uint8_t)(linecoding.bitrate >> 24);
buff[4] = (uint8_t)(linecoding.format);
buff[5] = (uint8_t)(linecoding.parity);
buff[6] = (uint8_t)(linecoding.data);
break;
default:
break;
}
}
/**
* @brief usb device class send report
* @param udev: to the structure of usbd_core_type
* @param report: report buffer
* @param len: report length
* @retval status of usb_sts_type
*/
usb_sts_type class_send_report(void *udev, uint8_t *report, uint16_t len)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
if(usbd_connect_state_get(pudev) == USB_CONN_STATE_CONFIGURED)
usbd_ept_send(pudev, USBD_HID_IN_EPT, report, len);
return status;
}
/**
* @brief usb keyoard send char
* @param udev: to the structure of usbd_core_type
* @param op: operation
* @retval none
*/
void usb_hid_keyboard_send_char(void *udev, uint8_t ascii_code)
{
uint8_t key_data = 0;
static uint8_t temp = 0;
static uint8_t keyboard_buf[8] = {0, 0, 6, 0, 0, 0, 0, 0};
if(ascii_code > 128)
{
ascii_code = 0;
}
else
{
ascii_code = _asciimap[ascii_code];
if(!ascii_code)
{
ascii_code = 0;
}
if(ascii_code & SHIFT)
{
key_data = 0x2;
ascii_code &= 0x7F;
}
}
if((temp == ascii_code) && (ascii_code != 0))
{
keyboard_buf[0] = 0;
keyboard_buf[2] = 0;
class_send_report(udev, keyboard_buf, 8);
}
else
{
keyboard_buf[0] = key_data;
keyboard_buf[2] = ascii_code;
class_send_report(udev, keyboard_buf, 8);
}
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/

View File

@@ -0,0 +1,134 @@
/**
**************************************************************************
* @file cdc_keyboard_class.h
* @version v2.0.0
* @date 2021-11-26
* @brief usb cdc and keyboard class file
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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.
*
**************************************************************************
*/
/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __CDC_KEYBOARD_CLASS_H
#define __CDC_KEYBOARD_CLASS_H
#ifdef __cplusplus
extern "C" {
#endif
#include "usb_std.h"
#include "usbd_core.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @addtogroup USB_cdc_keyboard_class
* @{
*/
/** @defgroup USB_cdc_keyboard_class_definition
* @{
*/
/**
* @brief usb use endpoint define
*/
#define USBD_CDC_INT_EPT 0x82
#define USBD_CDC_BULK_IN_EPT 0x81
#define USBD_CDC_BULK_OUT_EPT 0x01
#define USBD_HID_IN_EPT 0x83
/**
* @brief usb in and out max packet size define
*/
#define USBD_IN_MAXPACKET_SIZE 0x40
#define USBD_OUT_MAXPACKET_SIZE 0x40
#define USBD_CMD_MAXPACKET_SIZE 0x08
/**
* @brief usb cdc class request code define
*/
#define SET_LINE_CODING 0x20
#define GET_LINE_CODING 0x21
/**
* @brief usb hid class request code define
*/
#define HID_REQ_SET_PROTOCOL 0x0B
#define HID_REQ_GET_PROTOCOL 0x03
#define HID_REQ_SET_IDLE 0x0A
#define HID_REQ_GET_IDLE 0x02
#define HID_REQ_SET_REPORT 0x09
#define HID_REQ_GET_REPORT 0x01
#define HID_DESCRIPTOR_TYPE 0x21
#define HID_REPORT_DESC 0x22
/**
* @}
*/
/** @defgroup USB_cdc_class_exported_types
* @{
*/
/**
* @brief usb cdc class set line coding struct
*/
typedef struct
{
uint32_t bitrate; /* line coding baud rate */
uint8_t format; /* line coding foramt */
uint8_t parity; /* line coding parity */
uint8_t data; /* line coding data bit */
}linecoding_type;
/**
* @}
*/
/** @defgroup USB_cdc_keyboar_class_exported_functions
* @{
*/
extern usbd_class_handler cdc_keyboard_class_handler;
extern uint8_t g_keyboard_tx_completed;
uint16_t usb_vcp_get_rxdata(void *udev, uint8_t *recv_data);
error_status usb_vcp_send_data(void *udev, uint8_t *send_data, uint16_t len);
usb_sts_type class_send_report(void *udev, uint8_t *report, uint16_t len);
void usb_hid_keyboard_send_char(void *udev, uint8_t ascii_code);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,532 @@
/**
**************************************************************************
* @file cdc_keyboard_desc.c
* @version v2.0.0
* @date 2021-11-26
* @brief usb cdc and keyboard device descriptor
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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 "cdc_keyboard_desc.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @defgroup USB_cdc_keyboard_desc
* @brief usb device cdc keyboard descriptor
* @{
*/
/** @defgroup USB_cdc_keyboard_desc_private_functions
* @{
*/
usbd_desc_t *get_device_descriptor(void);
usbd_desc_t *get_device_qualifier(void);
usbd_desc_t *get_device_configuration(void);
usbd_desc_t *get_device_other_speed(void);
usbd_desc_t *get_device_lang_id(void);
usbd_desc_t *get_device_manufacturer_string(void);
usbd_desc_t *get_device_product_string(void);
usbd_desc_t *get_device_serial_string(void);
usbd_desc_t *get_device_interface_string(void);
usbd_desc_t *get_device_config_string(void);
uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf);
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 device descriptor handler structure
*/
usbd_desc_handler cdc_keyboard_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
*/
uint8_t g_usbd_descriptor[USB_DEVICE_DESC_LEN] =
{
USB_DEVICE_DESC_LEN, /* bLength */
USB_DESCIPTOR_TYPE_DEVICE, /* bDescriptorType */
0x00, /* bcdUSB */
0x02,
0xEF, /* bDeviceClass */
0x02, /* bDeviceSubClass */
0x01, /* bDeviceProtocol */
USB_MAX_EP0_SIZE, /* bMaxPacketSize */
LBYTE(USBD_VENDOR_ID), /* idVendor */
HBYTE(USBD_VENDOR_ID), /* idVendor */
LBYTE(USBD_PRODUCT_ID), /* idProduct */
HBYTE(USBD_PRODUCT_ID), /* idProduct */
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
*/
uint8_t g_usbd_configuration[USBD_CONFIG_DESC_SIZE] =
{
USB_DEVICE_CFG_DESC_LEN, /* bLength: configuration descriptor size */
USB_DESCIPTOR_TYPE_CONFIGURATION, /* bDescriptorType: configuration */
LBYTE(USBD_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
HBYTE(USBD_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
0x03, /* bNumInterfaces: 2 interface */
0x01, /* bConfigurationValue: configuration value */
0x00, /* iConfiguration: index of string descriptor describing
the configuration */
0xC0, /* bmAttributes: self powered */
0x32, /* MaxPower 100 mA: this current is used for detecting vbus */
0x08, /* bLength */
0x0B, /* bDescriptorType */
0x00, /* bFirstInterface */
0x02, /* bInterfaceCount */
0x02, /* bFunctionClass */
0x02, /* bFunctionSubClass */
0x01, /* bFunctionProtocol */
0x00, /* iFunction (Index of string descriptor describing this function) */
USB_DEVICE_IF_DESC_LEN, /* bLength: interface descriptor size */
USB_DESCIPTOR_TYPE_INTERFACE, /* bDescriptorType: interface descriptor type */
CDC_INTERFACE, /* bInterfaceNumber: number of interface */
0x00, /* bAlternateSetting: alternate set */
0x01, /* bNumEndpoints: number of endpoints */
USB_CLASS_CODE_CDC, /* bInterfaceClass: CDC class code */
0x02, /* bInterfaceSubClass: subclass code, Abstract Control Model*/
0x01, /* bInterfaceProtocol: protocol code, AT Command */
0x00, /* iInterface: index of string descriptor */
0x05, /* bFunctionLength: size of this descriptor in bytes */
USBD_CDC_CS_INTERFACE, /* bDescriptorType: CDC interface descriptor type */
USBD_CDC_SUBTYPE_HEADER, /* bDescriptorSubtype: Header function Descriptor 0x00*/
LBYTE(BCD_NUM),
HBYTE(BCD_NUM), /* bcdCDC: USB class definitions for communications */
0x05, /* bFunctionLength: size of this descriptor in bytes */
USBD_CDC_CS_INTERFACE, /* bDescriptorType: CDC interface descriptor type */
USBD_CDC_SUBTYPE_CMF, /* bDescriptorSubtype: Call Management function descriptor subtype 0x01 */
0x00, /* bmCapabilities: 0x00*/
0x01, /* bDataInterface: interface number of data class interface optionally used for call management */
0x04, /* bFunctionLength: size of this descriptor in bytes */
USBD_CDC_CS_INTERFACE, /* bDescriptorType: CDC interface descriptor type */
USBD_CDC_SUBTYPE_ACM, /* bDescriptorSubtype: Abstract Control Management functional descriptor subtype 0x02 */
0x02, /* bmCapabilities: Support Set_Line_Coding and Get_Line_Coding 0x02 */
0x05, /* bFunctionLength: size of this descriptor in bytes */
USBD_CDC_CS_INTERFACE, /* bDescriptorType: CDC interface descriptor type */
USBD_CDC_SUBTYPE_UFD, /* bDescriptorSubtype: Union Function Descriptor subtype 0x06 */
0x00, /* bControlInterface: The interface number of the communications or data class interface 0x00 */
0x01, /* bSubordinateInterface0: interface number of first subordinate interface in the union */
USB_DEVICE_EPT_LEN, /* bLength: size of endpoint descriptor in bytes */
USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
USBD_CDC_INT_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
USB_EPT_DESC_INTERRUPT, /* bmAttributes: endpoint attributes */
LBYTE(USBD_CMD_MAXPACKET_SIZE),
HBYTE(USBD_CMD_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
HID_BINTERVAL_TIME, /* bInterval: interval for polling endpoint for data transfers */
USB_DEVICE_IF_DESC_LEN, /* bLength: interface descriptor size */
USB_DESCIPTOR_TYPE_INTERFACE, /* bDescriptorType: interface descriptor type */
CDC_DATA_INTERFACE, /* bInterfaceNumber: number of interface */
0x00, /* bAlternateSetting: alternate set */
0x02, /* bNumEndpoints: number of endpoints */
USB_CLASS_CODE_CDCDATA, /* bInterfaceClass: CDC-data class code */
0x00, /* bInterfaceSubClass: Data interface subclass code 0x00*/
0x00, /* bInterfaceProtocol: data class protocol code 0x00 */
0x00, /* iInterface: index of string descriptor */
USB_DEVICE_EPT_LEN, /* bLength: size of endpoint descriptor in bytes */
USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
USBD_CDC_BULK_IN_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
USB_EPT_DESC_BULK, /* bmAttributes: endpoint attributes */
LBYTE(USBD_IN_MAXPACKET_SIZE),
HBYTE(USBD_IN_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
0x00, /* bInterval: interval for polling endpoint for data transfers */
USB_DEVICE_EPT_LEN, /* bLength: size of endpoint descriptor in bytes */
USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
USBD_CDC_BULK_OUT_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
USB_EPT_DESC_BULK, /* bmAttributes: endpoint attributes */
LBYTE(USBD_OUT_MAXPACKET_SIZE),
HBYTE(USBD_OUT_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
0x00, /* bInterval: interval for polling endpoint for data transfers */
0x08, /* bLength */
0x0B, /* bDescriptorType */
0x02, /* bFirstInterface */
0x01, /* bInterfaceCount */
0x03, /* bFunctionClass */
0x00, /* bFunctionSubClass */
0x01, /* bFunctionProtocol */
0x00, /* iFunction (Index of string descriptor describing this function) */
USB_DEVICE_IF_DESC_LEN, /* bLength: interface descriptor size */
USB_DESCIPTOR_TYPE_INTERFACE, /* bDescriptorType: interface descriptor type */
HID_KEYBOARD_INTERFACE, /* 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 */
0x01, /* bInterfaceProtocol: protocol code */
0x00, /* iInterface: index of string descriptor */
0x09, /* bLength: size of HID descriptor in bytes */
HID_CLASS_DESC_HID, /* bDescriptorType: HID descriptor type */
LBYTE(HID_BCD_NUM),
HBYTE(HID_BCD_NUM), /* bcdHID: HID class specification release number */
0x00, /* bCountryCode: hardware target conutry */
0x01, /* bNumDescriptors: number of HID class descriptor to follow */
HID_CLASS_DESC_REPORT, /* bDescriptorType: report descriptor type */
LBYTE(sizeof(g_usbd_hid_report)),
HBYTE(sizeof(g_usbd_hid_report)), /* wDescriptorLength: total length of reprot descriptor */
USB_DEVICE_EPT_LEN, /* bLength: size of endpoint descriptor in bytes */
USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
USBD_HID_IN_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
USB_EPT_DESC_INTERRUPT, /* bmAttributes: endpoint attributes */
LBYTE(USBD_IN_MAXPACKET_SIZE),
HBYTE(USBD_IN_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
HID_BINTERVAL_TIME, /* bInterval: interval for polling endpoint for data transfers */
};
/**
* @brief usb hid descriptor
*/
uint8_t g_hid_usb_desc[9] =
{
0x09, /* bLength: size of HID descriptor in bytes */
HID_CLASS_DESC_HID, /* bDescriptorType: HID descriptor type */
LBYTE(HID_BCD_NUM),
HBYTE(HID_BCD_NUM), /* bcdHID: HID class specification release number */
0x00, /* bCountryCode: hardware target conutry */
0x01, /* bNumDescriptors: number of HID class descriptor to follow */
HID_CLASS_DESC_REPORT, /* bDescriptorType: report descriptor type */
LBYTE(sizeof(g_usbd_hid_report)),
HBYTE(sizeof(g_usbd_hid_report)), /* wDescriptorLength: total length of reprot descriptor */
};
/**
* @brief usb hid keyboard report descriptor
*/
uint8_t g_usbd_hid_report[USBD_HID_SIZ_REPORT_DESC] =
{
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x06, // USAGE (Keyboard)
0xa1, 0x01, // COLLECTION (Application)
0x05, 0x07, // USAGE_PAGE (Keyboard)
0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x08, // REPORT_COUNT (8)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x08, // REPORT_SIZE (8)
0x81, 0x03, // INPUT (Cnst,Var,Abs)
0x95, 0x05, // REPORT_COUNT (5)
0x75, 0x01, // REPORT_SIZE (1)
0x05, 0x08, // USAGE_PAGE (LEDs)
0x19, 0x01, // USAGE_MINIMUM (Num Lock)
0x29, 0x05, // USAGE_MAXIMUM (Kana)
0x91, 0x02, // OUTPUT (Data,Var,Abs)
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x03, // REPORT_SIZE (3)
0x91, 0x03, // OUTPUT (Cnst,Var,Abs)
0x95, 0x06, // REPORT_COUNT (6)
0x75, 0x08, // REPORT_SIZE (8)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0xFF, // LOGICAL_MAXIMUM (255)
0x05, 0x07, // USAGE_PAGE (Keyboard)
0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
0x81, 0x00, // INPUT (Data,Ary,Abs)
0xc0 // END_COLLECTION
};
/**
* @brief usb string lang id
*/
uint8_t g_string_lang_id[USBD_SIZ_STRING_LANGID] =
{
USBD_SIZ_STRING_LANGID,
USB_DESCIPTOR_TYPE_STRING,
0x09,
0x04,
};
/**
* @brief usb string serial
*/
uint8_t g_string_serial[USBD_SIZ_STRING_SERIAL] =
{
USBD_SIZ_STRING_SERIAL,
USB_DESCIPTOR_TYPE_STRING,
};
/* device descriptor */
usbd_desc_t device_descriptor =
{
USB_DEVICE_DESC_LEN,
g_usbd_descriptor
};
/* config descriptor */
usbd_desc_t config_descriptor =
{
USBD_CONFIG_DESC_SIZE,
g_usbd_configuration
};
/* langid descriptor */
usbd_desc_t langid_descriptor =
{
USBD_SIZ_STRING_LANGID,
g_string_lang_id
};
/* serial descriptor */
usbd_desc_t serial_descriptor =
{
USBD_SIZ_STRING_SERIAL,
g_string_serial
};
usbd_desc_t vp_desc;
/**
* @brief standard usb unicode convert
* @param string: source string
* @param unicode_buf: unicode buffer
* @retval length
*/
uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf)
{
uint16_t str_len = 0, id_pos = 2;
uint8_t *tmp_str = string;
while(*tmp_str != '\0')
{
str_len ++;
unicode_buf[id_pos ++] = *tmp_str ++;
unicode_buf[id_pos ++] = 0x00;
}
str_len = str_len * 2 + 2;
unicode_buf[0] = str_len;
unicode_buf[1] = USB_DESCIPTOR_TYPE_STRING;
return str_len;
}
/**
* @brief usb int convert to unicode
* @param value: int value
* @param pbus: unicode buffer
* @param len: length
* @retval none
*/
static void usbd_int_to_unicode (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;
}
}
/**
* @brief usb get serial number
* @param none
* @retval none
*/
static void get_serial_num(void)
{
uint32_t serial0, serial1, serial2;
serial0 = *(uint32_t*)MCU_ID1;
serial1 = *(uint32_t*)MCU_ID2;
serial2 = *(uint32_t*)MCU_ID3;
serial0 += serial2;
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
* @retval usbd_desc
*/
usbd_desc_t *get_device_descriptor(void)
{
return &device_descriptor;
}
/**
* @brief get device qualifier
* @param none
* @retval usbd_desc
*/
usbd_desc_t * get_device_qualifier(void)
{
return NULL;
}
/**
* @brief get config descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_configuration(void)
{
return &config_descriptor;
}
/**
* @brief get other speed descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_other_speed(void)
{
return NULL;
}
/**
* @brief get lang id descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_lang_id(void)
{
return &langid_descriptor;
}
/**
* @brief get manufacturer descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_manufacturer_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_MANUFACTURER_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get product descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_product_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_PRODUCT_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get serial descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_serial_string(void)
{
get_serial_num();
return &serial_descriptor;
}
/**
* @brief get interface descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_interface_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_INTERFACE_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get device config descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_config_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_CONFIGURATION_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/

View File

@@ -0,0 +1,144 @@
/**
**************************************************************************
* @file cdc_keyboard_desc.h
* @version v2.0.0
* @date 2021-11-26
* @brief usb cdc and keyboard descriptor header file
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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.
*
**************************************************************************
*/
/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __CDC_KEYBOARD_DESC_H
#define __CDC_KEYBOARD_DESC_H
#ifdef __cplusplus
extern "C" {
#endif
#include "cdc_keyboard_class.h"
#include "usbd_core.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @addtogroup USB_cdc_keyboard_desc
* @{
*/
/** @defgroup USB_cdc_keyboard_desc_definition
* @{
*/
/**
* @brief usb bcd number define
*/
#define BCD_NUM 0x0110
/**
* @brief usb vendor id and product id define
*/
#define USBD_VENDOR_ID 0x2E3C
#define USBD_PRODUCT_ID 0x5750
/**
* @brief usb bcd number define
*/
#define HID_BCD_NUM 0x0110
/**
* @brief usb hid class descriptor define
*/
#define HID_CLASS_DESC_HID 0x21
#define HID_CLASS_DESC_REPORT 0x22
#define HID_CLASS_DESC_PHYSICAL 0x23
/**
* @brief usb descriptor size define
*/
#define USBD_CONFIG_DESC_SIZE 108
#define USBD_HID_SIZ_REPORT_DESC 63
#define USBD_SIZ_STRING_LANGID 4
#define USBD_SIZ_STRING_SERIAL 0x1A
/**
* @brief usb string define(vendor, product configuration, interface)
*/
#define USBD_DESC_MANUFACTURER_STRING "Artery"
#define USBD_DESC_PRODUCT_STRING "AT32 Composite VCP and Keyboard"
#define USBD_DESC_CONFIGURATION_STRING "Composite VCP and Keyboard Config"
#define USBD_DESC_INTERFACE_STRING "Composite VCP and Keyboard Interface"
/**
* @brief usb endpoint interval define
*/
#define HID_BINTERVAL_TIME 0x0A
/**
* @brief usb cdc class descriptor define
*/
#define USBD_CDC_CS_INTERFACE 0x24
#define USBD_CDC_CS_ENDPOINT 0x25
/**
* @brief usb cdc class sub-type define
*/
#define USBD_CDC_SUBTYPE_HEADER 0x00
#define USBD_CDC_SUBTYPE_CMF 0x01
#define USBD_CDC_SUBTYPE_ACM 0x02
#define USBD_CDC_SUBTYPE_UFD 0x06
/**
* @brief usb interface define
*/
#define CDC_INTERFACE 0x00
#define CDC_DATA_INTERFACE 0x01
#define HID_KEYBOARD_INTERFACE 0x02
/**
* @brief usb mcu id address deine
*/
#define MCU_ID1 (0x1FFFF7E8)
#define MCU_ID2 (0x1FFFF7EC)
#define MCU_ID3 (0x1FFFF7F0)
/**
* @}
*/
extern uint8_t g_usbd_descriptor[USB_DEVICE_DESC_LEN];
extern uint8_t g_usbd_configuration[USBD_CONFIG_DESC_SIZE];
extern uint8_t g_usbd_hid_report[USBD_HID_SIZ_REPORT_DESC];
extern uint8_t g_hid_usb_desc[9];
extern usbd_desc_handler cdc_keyboard_desc_handler;
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,399 @@
/**
**************************************************************************
* @file custom_hid_class.c
* @version v2.0.0
* @date 2021-11-26
* @brief usb custom hid class type
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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 "usbd_core.h"
#include "custom_hid_class.h"
#include "custom_hid_desc.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @defgroup USB_custom_hid_class
* @brief usb device custom hid demo
* @{
*/
/** @defgroup USB_custom_hid_class_private_functions
* @{
*/
usb_sts_type class_init_handler(void *udev);
usb_sts_type class_clear_handler(void *udev);
usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup);
usb_sts_type class_ept0_tx_handler(void *udev);
usb_sts_type class_ept0_rx_handler(void *udev);
usb_sts_type class_in_handler(void *udev, uint8_t ept_num);
usb_sts_type class_out_handler(void *udev, uint8_t ept_num);
usb_sts_type class_sof_handler(void *udev);
usb_sts_type class_event_handler(void *udev, usbd_event_type event);
void usb_hid_buf_process(void *udev, uint8_t *report, uint16_t len);
/* usb hid rx and tx buffer */
static uint8_t g_rxhid_buff[USBD_OUT_MAXPACKET_SIZE];
static uint8_t g_txhid_buff[USBD_IN_MAXPACKET_SIZE];
/* custom hid static variable */
static uint32_t hid_protocol = 0;
static uint32_t hid_set_idle = 0;
static uint32_t alt_setting = 0;
static uint8_t hid_state;
uint8_t hid_set_report[64];
/* usb device class handler */
usbd_class_handler custom_hid_class_handler =
{
class_init_handler,
class_clear_handler,
class_setup_handler,
class_ept0_tx_handler,
class_ept0_rx_handler,
class_in_handler,
class_out_handler,
class_sof_handler,
class_event_handler,
};
/**
* @brief initialize usb custom hid endpoint
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_init_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
/* open custom hid in endpoint */
usbd_ept_open(pudev, USBD_HID_IN_EPT, EPT_INT_TYPE, USBD_IN_MAXPACKET_SIZE);
/* open custom hid out endpoint */
usbd_ept_open(pudev, USBD_HID_OUT_EPT, EPT_INT_TYPE, USBD_OUT_MAXPACKET_SIZE);
/* set out endpoint to receive status */
usbd_ept_recv(pudev, USBD_HID_OUT_EPT, g_rxhid_buff, USBD_OUT_MAXPACKET_SIZE);
return status;
}
/**
* @brief clear endpoint or other state
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_clear_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
/* close custom hid in endpoint */
usbd_ept_close(pudev, USBD_HID_IN_EPT);
/* close custom hid out endpoint */
usbd_ept_close(pudev, USBD_HID_OUT_EPT);
return status;
}
/**
* @brief usb device class setup request handler
* @param udev: to the structure of usbd_core_type
* @param setup: setup packet
* @retval status of usb_sts_type
*/
usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
uint16_t len;
uint8_t *buf;
switch(setup->bmRequestType & USB_REQ_TYPE_RESERVED)
{
/* class request */
case USB_REQ_TYPE_CLASS:
switch(setup->bRequest)
{
case HID_REQ_SET_PROTOCOL:
hid_protocol = (uint8_t)setup->wValue;
break;
case HID_REQ_GET_PROTOCOL:
usbd_ctrl_send(pudev, (uint8_t *)&hid_protocol, 1);
break;
case HID_REQ_SET_IDLE:
hid_set_idle = (uint8_t)(setup->wValue >> 8);
break;
case HID_REQ_GET_IDLE:
usbd_ctrl_send(pudev, (uint8_t *)&hid_set_idle, 1);
break;
case HID_REQ_SET_REPORT:
hid_state = HID_REQ_SET_REPORT;
usbd_ctrl_recv(pudev, hid_set_report, setup->wLength);
break;
default:
usbd_ctrl_unsupport(pudev);
break;
}
break;
/* standard request */
case USB_REQ_TYPE_STANDARD:
switch(setup->bRequest)
{
case USB_STD_REQ_GET_DESCRIPTOR:
if(setup->wValue >> 8 == HID_REPORT_DESC)
{
len = MIN(USBD_HID_SIZ_REPORT_DESC, setup->wLength);
buf = (uint8_t *)g_usbd_hid_report;
}
else if(setup->wValue >> 8 == HID_DESCRIPTOR_TYPE)
{
len = MIN(9, setup->wLength);
buf = (uint8_t *)g_hid_usb_desc;
}
usbd_ctrl_send(pudev, (uint8_t *)buf, len);
break;
case USB_STD_REQ_GET_INTERFACE:
usbd_ctrl_send(pudev, (uint8_t *)&alt_setting, 1);
break;
case USB_STD_REQ_SET_INTERFACE:
alt_setting = setup->wValue;
break;
default:
break;
}
break;
default:
usbd_ctrl_unsupport(pudev);
break;
}
return status;
}
/**
* @brief usb device endpoint 0 in status stage complete
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_ept0_tx_handler(void *udev)
{
usb_sts_type status = USB_OK;
/* ...user code... */
return status;
}
/**
* @brief usb device endpoint 0 out status stage complete
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_ept0_rx_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
uint32_t recv_len = usbd_get_recv_len(pudev, 0);
/* ...user code... */
if( hid_state == HID_REQ_SET_REPORT)
{
/* hid buffer process */
usb_hid_buf_process(udev, hid_set_report, recv_len);
hid_state = 0;
}
return status;
}
/**
* @brief usb device transmision complete handler
* @param udev: to the structure of usbd_core_type
* @param ept_num: endpoint number
* @retval status of usb_sts_type
*/
usb_sts_type class_in_handler(void *udev, uint8_t ept_num)
{
usb_sts_type status = USB_OK;
/* ...user code...
trans next packet data
*/
return status;
}
/**
* @brief usb device endpoint receive data
* @param udev: to the structure of usbd_core_type
* @param ept_num: endpoint number
* @retval status of usb_sts_type
*/
usb_sts_type class_out_handler(void *udev, uint8_t ept_num)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
/* get endpoint receive data length */
uint32_t recv_len = usbd_get_recv_len(pudev, ept_num);
/* hid buffer process */
usb_hid_buf_process(udev, g_rxhid_buff, recv_len);
/* start receive next packet */
usbd_ept_recv(pudev, USBD_HID_OUT_EPT, g_rxhid_buff, recv_len);
return status;
}
/**
* @brief usb device sof handler
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_sof_handler(void *udev)
{
usb_sts_type status = USB_OK;
/* ...user code... */
return status;
}
/**
* @brief usb device event handler
* @param udev: to the structure of usbd_core_type
* @param event: usb device event
* @retval status of usb_sts_type
*/
usb_sts_type class_event_handler(void *udev, usbd_event_type event)
{
usb_sts_type status = USB_OK;
switch(event)
{
case USBD_RESET_EVENT:
/* ...user code... */
break;
case USBD_SUSPEND_EVENT:
/* ...user code... */
break;
case USBD_WAKEUP_EVENT:
/* ...user code... */
break;
default:
break;
}
return status;
}
/**
* @brief usb device class send report
* @param udev: to the structure of usbd_core_type
* @param report: report buffer
* @param len: report length
* @retval status of usb_sts_type
*/
usb_sts_type class_send_report(void *udev, uint8_t *report, uint16_t len)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
if(usbd_connect_state_get(pudev) == USB_CONN_STATE_CONFIGURED)
usbd_ept_send(pudev, USBD_HID_IN_EPT, report, len);
return status;
}
/**
* @brief usb device report function
* @param udev: to the structure of usbd_core_type
* @param report: report buffer
* @param len: report length
* @retval none
*/
void usb_hid_buf_process(void *udev, uint8_t *report, uint16_t len)
{
uint32_t i_index;
usbd_core_type *pudev = (usbd_core_type *)udev;
switch(report[0])
{
case HID_REPORT_ID_2:
if(g_rxhid_buff[1] == 0)
{
at32_led_off(LED2);
}
else
{
at32_led_on(LED2);
}
break;
case HID_REPORT_ID_3:
if(g_rxhid_buff[1] == 0)
{
at32_led_off(LED3);
}
else
{
at32_led_on(LED3);
}
break;
case HID_REPORT_ID_4:
if(g_rxhid_buff[1] == 0)
{
at32_led_off(LED4);
}
else
{
at32_led_on(LED4);
}
break;
case HID_REPORT_ID_6:
for(i_index = 0; i_index < len; i_index ++)
{
g_txhid_buff[i_index] = report[i_index];
}
usbd_ept_send(pudev, USBD_HID_IN_EPT, g_txhid_buff, len);
default:
break;
}
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/

View File

@@ -0,0 +1,105 @@
/**
**************************************************************************
* @file custom_hid_class.h
* @version v2.0.0
* @date 2021-11-26
* @brief usb hid header file
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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.
*
**************************************************************************
*/
/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __CUSTOM_HID_CLASS_H
#define __CUSTOM_HID_CLASS_H
#ifdef __cplusplus
extern "C" {
#endif
#include "usb_std.h"
#include "usbd_core.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @addtogroup USB_custom_hid_class
* @{
*/
/** @defgroup USB_custom_hid_class_endpoint_definition
* @{
*/
/**
* @brief usb custom hid use endpoint define
*/
#define USBD_HID_IN_EPT 0x81
#define USBD_HID_OUT_EPT 0x01
/**
* @brief usb custom hid in and out max packet size define
*/
#define USBD_IN_MAXPACKET_SIZE 0x40
#define USBD_OUT_MAXPACKET_SIZE 0x40
/**
* @}
*/
/** @defgroup USB_custom_hid_class_request_code_definition
* @{
*/
/**
* @brief usb custom hid class request code define
*/
#define HID_REQ_SET_PROTOCOL 0x0B
#define HID_REQ_GET_PROTOCOL 0x03
#define HID_REQ_SET_IDLE 0x0A
#define HID_REQ_GET_IDLE 0x02
#define HID_REQ_SET_REPORT 0x09
#define HID_REQ_GET_REPORT 0x01
#define HID_DESCRIPTOR_TYPE 0x21
#define HID_REPORT_DESC 0x22
/**
* @}
*/
/** @defgroup USB_custom_hid_class_exported_functions
* @{
*/
extern usbd_class_handler custom_hid_class_handler;
usb_sts_type class_send_report(void *udev, uint8_t *report, uint16_t len);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,513 @@
/**
**************************************************************************
* @file custom_hid_desc.c
* @version v2.0.0
* @date 2021-11-26
* @brief usb hid device descriptor
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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 "custom_hid_desc.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @defgroup USB_custom_hid_desc
* @brief usb device custom_hid descriptor
* @{
*/
/** @defgroup USB_custom_hid_desc_private_functions
* @{
*/
usbd_desc_t *get_device_descriptor(void);
usbd_desc_t *get_device_qualifier(void);
usbd_desc_t *get_device_configuration(void);
usbd_desc_t *get_device_other_speed(void);
usbd_desc_t *get_device_lang_id(void);
usbd_desc_t *get_device_manufacturer_string(void);
usbd_desc_t *get_device_product_string(void);
usbd_desc_t *get_device_serial_string(void);
usbd_desc_t *get_device_interface_string(void);
usbd_desc_t *get_device_config_string(void);
uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf);
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 custom hid device descriptor handler structure
*/
usbd_desc_handler hid_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
*/
uint8_t g_usbd_descriptor[USB_DEVICE_DESC_LEN] =
{
USB_DEVICE_DESC_LEN, /* bLength */
USB_DESCIPTOR_TYPE_DEVICE, /* bDescriptorType */
0x00, /* bcdUSB */
0x02,
0x00, /* bDeviceClass */
0x00, /* bDeviceSubClass */
0x00, /* bDeviceProtocol */
USB_MAX_EP0_SIZE, /* bMaxPacketSize */
LBYTE(USBD_VENDOR_ID), /* idVendor */
HBYTE(USBD_VENDOR_ID), /* idVendor */
LBYTE(USBD_PRODUCT_ID), /* idProduct */
HBYTE(USBD_PRODUCT_ID), /* idProduct */
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
*/
uint8_t g_usbd_configuration[USBD_CONFIG_DESC_SIZE] =
{
USB_DEVICE_CFG_DESC_LEN, /* bLength: configuration descriptor size */
USB_DESCIPTOR_TYPE_CONFIGURATION, /* bDescriptorType: configuration */
LBYTE(USBD_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
HBYTE(USBD_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
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 */
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 */
0x02, /* bNumEndpoints: number of endpoints */
USB_CLASS_CODE_HID, /* bInterfaceClass: class code hid */
0x00, /* bInterfaceSubClass: subclass code */
0x00, /* bInterfaceProtocol: protocol code */
0x00, /* iInterface: index of string descriptor */
0x09, /* bLength: size of HID descriptor in bytes */
HID_CLASS_DESC_HID, /* bDescriptorType: HID descriptor type */
LBYTE(HID_BCD_NUM),
HBYTE(HID_BCD_NUM), /* bcdHID: HID class specification release number */
0x00, /* bCountryCode: hardware target conutry */
0x01, /* bNumDescriptors: number of HID class descriptor to follow */
HID_CLASS_DESC_REPORT, /* bDescriptorType: report descriptor type */
LBYTE(sizeof(g_usbd_hid_report)),
HBYTE(sizeof(g_usbd_hid_report)), /* wDescriptorLength: total length of reprot descriptor */
USB_DEVICE_EPT_LEN, /* bLength: size of endpoint descriptor in bytes */
USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
USBD_HID_IN_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
USB_EPT_DESC_INTERRUPT, /* bmAttributes: endpoint attributes */
LBYTE(USBD_IN_MAXPACKET_SIZE),
HBYTE(USBD_IN_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
HID_BINTERVAL_TIME, /* bInterval: interval for polling endpoint for data transfers */
USB_DEVICE_EPT_LEN, /* bLength: size of endpoint descriptor in bytes */
USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
USBD_HID_OUT_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
USB_EPT_DESC_INTERRUPT, /* bmAttributes: endpoint attributes */
LBYTE(USBD_OUT_MAXPACKET_SIZE),
HBYTE(USBD_OUT_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
HID_BINTERVAL_TIME, /* bInterval: interval for polling endpoint for data transfers */
};
/**
* @brief usb hid report descriptor
*/
uint8_t g_usbd_hid_report[USBD_HID_SIZ_REPORT_DESC] =
{
0x06, 0xFF, 0x00, /* USAGE_PAGE(Vendor Page:0xFF00) */
0x09, 0x01, /* USAGE (Demo Kit) */
0xa1, 0x01, /* COLLECTION (Application) */
/* 7 */
/* Led 2 */
0x85, HID_REPORT_ID_2, /* REPORT_ID 2 */
0x09, 0x02, /* USAGE (LED 2) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
0x75, 0x08, /* REPORT_SIZE (8) */
0x95, 0x3F, /* REPORT_COUNT (1) */
0xB1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
0x85, 0x02, /* REPORT_ID (2) */
0x09, 0x02, /* USAGE (LED 2) */
0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */
/* 27 */
/* Led 3 */
0x85, HID_REPORT_ID_3, /* REPORT_ID (3) */
0x09, 0x03, /* USAGE (LED 3) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
0x75, 0x08, /* REPORT_SIZE (8) */
0x95, 0x3F, /* REPORT_COUNT (1) */
0xB1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
0x85, 0x03, /* REPORT_ID (3) */
0x09, 0x03, /* USAGE (LED 3) */
0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */
/* 47 */
/* Led 4 */
0x85, HID_REPORT_ID_4, /* REPORT_ID 4) */
0x09, 0x04, /* USAGE (LED 4) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
0x75, 0x08, /* REPORT_SIZE (8) */
0x95, 0x3F, /* REPORT_COUNT (1) */
0xB1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
0x85, 0x04, /* REPORT_ID (4) */
0x09, 0x04, /* USAGE (LED 4) */
0x91, 0x82, /* OUTPUT (Data,Var,Abs,Vol) */
/* 67 */
/* key Push Button */
0x85, HID_REPORT_ID_5, /* REPORT_ID (5) */
0x09, 0x05, /* USAGE (Push Button) */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
0x75, 0x01, /* REPORT_SIZE (1) */
0x81, 0x82, /* INPUT (Data,Var,Abs,Vol) */
0x09, 0x05, /* USAGE (Push Button) */
0x75, 0x01, /* REPORT_SIZE (1) */
0xb1, 0x82, /* FEATURE (Data,Var,Abs,Vol) */
0x75, 0x07, /* REPORT_SIZE (7) */
0x81, 0x83, /* INPUT (Cnst,Var,Abs,Vol) */
0x85, 0x05, /* REPORT_ID (5) */
0x75, 0x07, /* REPORT_SIZE (7) */
0xb1, 0x83, /* FEATURE (Cnst,Var,Abs,Vol) */
/* 95 */
/* Data OUT */
0x85, HID_REPORT_ID_6, /* REPORT_ID (0xF0) */
0x09, 0x06, /* USAGE */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x26, 0x00,0xff, /* LOGICAL_MAXIMUM (255) */
0x75, 0x08, /* REPORT_SIZE (8) */
0x95, 0x3F, /* REPORT_COUNT (64) */
0x91, 0x02, /* OUTPUT(Data,Var,Abs,Vol) */
/* 110 */
/* Data IN */
0x85, HID_REPORT_ID_6, /* REPORT_ID (0xF0) */
0x09, 0x07, /* USAGE */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x26, 0x00,0xff, /* LOGICAL_MAXIMUM (255) */
0x75, 0x08, /* REPORT_SIZE (8) */
0x95, 0x3F, /* REPORT_COUNT (64) */
0x81, 0x82, /* INPUT(Data,Var,Abs,Vol) */
/* 125 */
0xc0 /* END_COLLECTION */
};
/**
* @brief usb hid descriptor
*/
uint8_t g_hid_usb_desc[9] =
{
0x09, /* bLength: size of HID descriptor in bytes */
HID_CLASS_DESC_HID, /* bDescriptorType: HID descriptor type */
LBYTE(HID_BCD_NUM),
HBYTE(HID_BCD_NUM), /* bcdHID: HID class specification release number */
0x00, /* bCountryCode: hardware target conutry */
0x01, /* bNumDescriptors: number of HID class descriptor to follow */
HID_CLASS_DESC_REPORT, /* bDescriptorType: report descriptor type */
LBYTE(sizeof(g_usbd_hid_report)),
HBYTE(sizeof(g_usbd_hid_report)), /* wDescriptorLength: total length of reprot descriptor */
};
/**
* @brief usb string lang id
*/
uint8_t g_string_lang_id[USBD_SIZ_STRING_LANGID] =
{
USBD_SIZ_STRING_LANGID,
USB_DESCIPTOR_TYPE_STRING,
0x09,
0x04,
};
/**
* @brief usb string serial
*/
uint8_t g_string_serial[USBD_SIZ_STRING_SERIAL] =
{
USBD_SIZ_STRING_SERIAL,
USB_DESCIPTOR_TYPE_STRING,
};
/* device descriptor */
usbd_desc_t device_descriptor =
{
USB_DEVICE_DESC_LEN,
g_usbd_descriptor
};
/* config descriptor */
usbd_desc_t config_descriptor =
{
USBD_CONFIG_DESC_SIZE,
g_usbd_configuration
};
/* langid descriptor */
usbd_desc_t langid_descriptor =
{
USBD_SIZ_STRING_LANGID,
g_string_lang_id
};
/* serial descriptor */
usbd_desc_t serial_descriptor =
{
USBD_SIZ_STRING_SERIAL,
g_string_serial
};
usbd_desc_t vp_desc;
/**
* @brief standard usb unicode convert
* @param string: source string
* @param unicode_buf: unicode buffer
* @retval length
*/
uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf)
{
uint16_t str_len = 0, id_pos = 2;
uint8_t *tmp_str = string;
while(*tmp_str != '\0')
{
str_len ++;
unicode_buf[id_pos ++] = *tmp_str ++;
unicode_buf[id_pos ++] = 0x00;
}
str_len = str_len * 2 + 2;
unicode_buf[0] = str_len;
unicode_buf[1] = USB_DESCIPTOR_TYPE_STRING;
return str_len;
}
/**
* @brief usb int convert to unicode
* @param value: int value
* @param pbus: unicode buffer
* @param len: length
* @retval none
*/
static void usbd_int_to_unicode (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;
}
}
/**
* @brief usb get serial number
* @param none
* @retval none
*/
static void get_serial_num(void)
{
uint32_t serial0, serial1, serial2;
serial0 = *(uint32_t*)MCU_ID1;
serial1 = *(uint32_t*)MCU_ID2;
serial2 = *(uint32_t*)MCU_ID3;
serial0 += serial2;
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
* @retval usbd_desc
*/
usbd_desc_t *get_device_descriptor(void)
{
return &device_descriptor;
}
/**
* @brief get device qualifier
* @param none
* @retval usbd_desc
*/
usbd_desc_t * get_device_qualifier(void)
{
return NULL;
}
/**
* @brief get config descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_configuration(void)
{
return &config_descriptor;
}
/**
* @brief get other speed descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_other_speed(void)
{
return NULL;
}
/**
* @brief get lang id descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_lang_id(void)
{
return &langid_descriptor;
}
/**
* @brief get manufacturer descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_manufacturer_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_MANUFACTURER_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get product descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_product_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_PRODUCT_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get serial descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_serial_string(void)
{
get_serial_num();
return &serial_descriptor;
}
/**
* @brief get interface descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_interface_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_INTERFACE_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get device config descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_config_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_CONFIGURATION_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/

View File

@@ -0,0 +1,129 @@
/**
**************************************************************************
* @file custom_hid_desc.h
* @version v2.0.0
* @date 2021-11-26
* @brief usb custom hid descriptor header file
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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.
*
**************************************************************************
*/
/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __CUSTOM_HID_DESC_H
#define __CUSTOM_HID_DESC_H
#ifdef __cplusplus
extern "C" {
#endif
#include "custom_hid_class.h"
#include "usbd_core.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @addtogroup USB_custom_hid_desc
* @{
*/
/** @defgroup USB_custom_hid_desc_definition
* @{
*/
/**
* @brief usb bcd number define
*/
#define HID_BCD_NUM 0x0110
/**
* @brief usb hid class descriptor define
*/
#define HID_CLASS_DESC_HID 0x21
#define HID_CLASS_DESC_REPORT 0x22
#define HID_CLASS_DESC_PHYSICAL 0x23
/**
* @brief usb vendor id and product id define
*/
#define USBD_VENDOR_ID 0x2E3C
#define USBD_PRODUCT_ID 0x5745
/**
* @brief usb descriptor size define
*/
#define USBD_CONFIG_DESC_SIZE 41
#define USBD_HID_SIZ_REPORT_DESC 126
#define USBD_SIZ_STRING_LANGID 4
#define USBD_SIZ_STRING_SERIAL 0x1A
/**
* @brief usb string define(vendor, product configuration, interface)
*/
#define USBD_DESC_MANUFACTURER_STRING "Artery"
#define USBD_DESC_PRODUCT_STRING "Custom HID"
#define USBD_DESC_CONFIGURATION_STRING "Custom HID Config"
#define USBD_DESC_INTERFACE_STRING "Custom HID Interface"
/**
* @brief usb hid endpoint interval define
*/
#define HID_BINTERVAL_TIME 0x0A
/**
* @brief usb hid report id define
*/
#define HID_REPORT_ID_1 0x01
#define HID_REPORT_ID_2 0x02
#define HID_REPORT_ID_3 0x03
#define HID_REPORT_ID_4 0x04
#define HID_REPORT_ID_5 0x05
#define HID_REPORT_ID_6 0xF0
/**
* @brief usb mcu id address deine
*/
#define MCU_ID1 (0x1FFFF7E8)
#define MCU_ID2 (0x1FFFF7EC)
#define MCU_ID3 (0x1FFFF7F0)
/**
* @}
*/
extern uint8_t g_usbd_descriptor[USB_DEVICE_DESC_LEN];
extern uint8_t g_usbd_configuration[USBD_CONFIG_DESC_SIZE];
extern uint8_t g_usbd_hid_report[USBD_HID_SIZ_REPORT_DESC];
extern uint8_t g_hid_usb_desc[9];
extern usbd_desc_handler hid_desc_handler;
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,349 @@
/**
**************************************************************************
* @file hid_iap_class.c
* @version v2.0.0
* @date 2021-11-26
* @brief usb hid iap class type
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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 "usbd_core.h"
#include "hid_iap_class.h"
#include "hid_iap_desc.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @defgroup USB_hid_iap_class
* @brief usb device class hid iap demo
* @{
*/
/** @defgroup USB_hid_iap_class_private_functions
* @{
*/
usb_sts_type class_init_handler(void *udev);
usb_sts_type class_clear_handler(void *udev);
usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup);
usb_sts_type class_ept0_tx_handler(void *udev);
usb_sts_type class_ept0_rx_handler(void *udev);
usb_sts_type class_in_handler(void *udev, uint8_t ept_num);
usb_sts_type class_out_handler(void *udev, uint8_t ept_num);
usb_sts_type class_sof_handler(void *udev);
usb_sts_type class_event_handler(void *udev, usbd_event_type event);
void usb_hid_buf_process(void *udev, uint8_t *report, uint16_t len);
/* usb hid rx and tx buffer */
static uint8_t g_rxhid_buff[USBD_OUT_MAXPACKET_SIZE];
/* custom hid static variable */
static uint32_t hid_protocol = 0;
static uint32_t hid_set_idle = 0;
static uint32_t alt_setting = 0;
static uint8_t hid_state;
uint8_t hid_set_report[64];
iap_info_type iap_info;
/* usb device class handler */
usbd_class_handler hid_iap_class_handler =
{
class_init_handler,
class_clear_handler,
class_setup_handler,
class_ept0_tx_handler,
class_ept0_rx_handler,
class_in_handler,
class_out_handler,
class_sof_handler,
class_event_handler,
};
/**
* @brief initialize usb custom hid endpoint
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_init_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
/* open hid iap in endpoint */
usbd_ept_open(pudev, USBD_HID_IN_EPT, EPT_INT_TYPE, USBD_IN_MAXPACKET_SIZE);
/* open hid iap out endpoint */
usbd_ept_open(pudev, USBD_HID_OUT_EPT, EPT_INT_TYPE, USBD_OUT_MAXPACKET_SIZE);
/* set out endpoint to receive status */
usbd_ept_recv(pudev, USBD_HID_OUT_EPT, g_rxhid_buff, USBD_OUT_MAXPACKET_SIZE);
return status;
}
/**
* @brief clear endpoint or other state
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_clear_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
/* close hid iap in endpoint */
usbd_ept_close(pudev, USBD_HID_IN_EPT);
/* close hid iap out endpoint */
usbd_ept_close(pudev, USBD_HID_OUT_EPT);
return status;
}
/**
* @brief usb device class setup request handler
* @param udev: to the structure of usbd_core_type
* @param setup: setup packet
* @retval status of usb_sts_type
*/
usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
uint16_t len;
uint8_t *buf;
switch(setup->bmRequestType & USB_REQ_TYPE_RESERVED)
{
/* class request */
case USB_REQ_TYPE_CLASS:
switch(setup->bRequest)
{
case HID_REQ_SET_PROTOCOL:
hid_protocol = (uint8_t)setup->wValue;
break;
case HID_REQ_GET_PROTOCOL:
usbd_ctrl_send(pudev, (uint8_t *)&hid_protocol, 1);
break;
case HID_REQ_SET_IDLE:
hid_set_idle = (uint8_t)(setup->wValue >> 8);
break;
case HID_REQ_GET_IDLE:
usbd_ctrl_send(pudev, (uint8_t *)&hid_set_idle, 1);
break;
case HID_REQ_SET_REPORT:
hid_state = HID_REQ_SET_REPORT;
usbd_ctrl_recv(pudev, hid_set_report, setup->wLength);
break;
default:
usbd_ctrl_unsupport(pudev);
break;
}
break;
/* standard request */
case USB_REQ_TYPE_STANDARD:
switch(setup->bRequest)
{
case USB_STD_REQ_GET_DESCRIPTOR:
if(setup->wValue >> 8 == HID_REPORT_DESC)
{
len = MIN(USBD_HID_SIZ_REPORT_DESC, setup->wLength);
buf = (uint8_t *)g_usbd_hid_report;
}
else if(setup->wValue >> 8 == HID_DESCRIPTOR_TYPE)
{
len = MIN(9, setup->wLength);
buf = (uint8_t *)g_hid_usb_desc;
}
usbd_ctrl_send(pudev, (uint8_t *)buf, len);
break;
case USB_STD_REQ_GET_INTERFACE:
usbd_ctrl_send(pudev, (uint8_t *)&alt_setting, 1);
break;
case USB_STD_REQ_SET_INTERFACE:
alt_setting = setup->wValue;
break;
default:
break;
}
break;
default:
usbd_ctrl_unsupport(pudev);
break;
}
return status;
}
/**
* @brief usb device endpoint 0 in status stage complete
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_ept0_tx_handler(void *udev)
{
usb_sts_type status = USB_OK;
/* ...user code... */
return status;
}
/**
* @brief usb device endpoint 0 out status stage complete
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_ept0_rx_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
uint32_t recv_len = usbd_get_recv_len(pudev, 0);
/* ...user code... */
if( hid_state == HID_REQ_SET_REPORT)
{
/* hid buffer process */
hid_state = 0;
}
return status;
}
/**
* @brief usb device transmision complete handler
* @param udev: to the structure of usbd_core_type
* @param ept_num: endpoint number
* @retval status of usb_sts_type
*/
usb_sts_type class_in_handler(void *udev, uint8_t ept_num)
{
usb_sts_type status = USB_OK;
/* ...user code...
trans next packet data
*/
usbd_hid_iap_in_complete(udev);
return status;
}
/**
* @brief usb device endpoint receive data
* @param udev: to the structure of usbd_core_type
* @param ept_num: endpoint number
* @retval status of usb_sts_type
*/
usb_sts_type class_out_handler(void *udev, uint8_t ept_num)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
/* get endpoint receive data length */
uint32_t recv_len = usbd_get_recv_len(pudev, ept_num);
/* hid iap process */
usbd_hid_iap_process(udev, g_rxhid_buff, recv_len);
/* start receive next packet */
usbd_ept_recv(pudev, USBD_HID_OUT_EPT, g_rxhid_buff, USBD_OUT_MAXPACKET_SIZE);
return status;
}
/**
* @brief usb device sof handler
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_sof_handler(void *udev)
{
usb_sts_type status = USB_OK;
/* ...user code... */
return status;
}
/**
* @brief usb device event handler
* @param udev: to the structure of usbd_core_type
* @param event: usb device event
* @retval status of usb_sts_type
*/
usb_sts_type class_event_handler(void *udev, usbd_event_type event)
{
usb_sts_type status = USB_OK;
switch(event)
{
case USBD_RESET_EVENT:
/* ...user code... */
break;
case USBD_SUSPEND_EVENT:
/* ...user code... */
break;
case USBD_WAKEUP_EVENT:
/* ...user code... */
break;
default:
break;
}
return status;
}
/**
* @brief usb device class send report
* @param udev: to the structure of usbd_core_type
* @param report: report buffer
* @param len: report length
* @retval status of usb_sts_type
*/
usb_sts_type class_send_report(void *udev, uint8_t *report, uint16_t len)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
if(usbd_connect_state_get(pudev) == USB_CONN_STATE_CONFIGURED)
usbd_ept_send(pudev, USBD_HID_IN_EPT, report, len);
return status;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/

View File

@@ -0,0 +1,164 @@
/**
**************************************************************************
* @file hid_iap_class.h
* @version v2.0.0
* @date 2021-11-26
* @brief usb hid iap header file
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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.
*
**************************************************************************
*/
/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __HID_IAP_CLASS_H
#define __HID_IAP_CLASS_H
#ifdef __cplusplus
extern "C" {
#endif
#include "usb_std.h"
#include "usbd_core.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @addtogroup USB_hid_iap_class
* @{
*/
/** @defgroup USB_hid_iap_class_definition
* @{
*/
#define USBD_HID_IN_EPT 0x81
#define USBD_HID_OUT_EPT 0x01
#define USBD_IN_MAXPACKET_SIZE 0x40
#define USBD_OUT_MAXPACKET_SIZE 0x40
#define HID_REQ_SET_PROTOCOL 0x0B
#define HID_REQ_GET_PROTOCOL 0x03
#define HID_REQ_SET_IDLE 0x0A
#define HID_REQ_GET_IDLE 0x02
#define HID_REQ_SET_REPORT 0x09
#define HID_REQ_GET_REPORT 0x01
#define HID_DESCRIPTOR_TYPE 0x21
#define HID_REPORT_DESC 0x22
#define FLASH_SIZE_REG() ((*(uint32_t *)0x1FFFF7E0) & 0xFFFF) /*Get Flash size*/
#define KB_TO_B(kb) ((kb) << 10)
#define SECTOR_SIZE_1K 0x400
#define SECTOR_SIZE_2K 0x800
#define SECTOR_SIZE_4K 0x1000
/**
* @brief iap command
*/
#define IAP_CMD_IDLE 0x5AA0
#define IAP_CMD_START 0x5AA1
#define IAP_CMD_ADDR 0x5AA2
#define IAP_CMD_DATA 0x5AA3
#define IAP_CMD_FINISH 0x5AA4
#define IAP_CMD_CRC 0x5AA5
#define IAP_CMD_JMP 0x5AA6
#define IAP_CMD_GET 0x5AA7
#define HID_IAP_BUFFER_LEN 1024
#define IAP_UPGRADE_COMPLETE_FLAG 0x41544B38
#define CONVERT_ENDIAN(dwValue) ((dwValue >> 24) | ((dwValue >> 8) & 0xFF00) | \
((dwValue << 8) & 0xFF0000) | (dwValue << 24) )
#define IAP_ACK 0xFF00
#define IAP_NACK 0x00FF
typedef enum
{
IAP_SUCCESS,
IAP_WAIT,
IAP_FAILED
}iap_result_type;
typedef enum
{
IAP_STS_IDLE,
IAP_STS_START,
IAP_STS_ADDR,
IAP_STS_DATA,
IAP_STS_FINISH,
IAP_STS_CRC,
IAP_STS_JMP_WAIT,
IAP_STS_JMP,
}iap_machine_state_type;
typedef struct
{
uint8_t iap_fifo[HID_IAP_BUFFER_LEN];
uint8_t iap_rx[USBD_OUT_MAXPACKET_SIZE];
uint8_t iap_tx[USBD_IN_MAXPACKET_SIZE];
uint32_t fifo_length;
uint32_t tx_length;
uint32_t app_address;
uint32_t iap_address;
uint32_t flag_address;
uint32_t flash_start_address;
uint32_t flash_end_address;
uint32_t sector_size;
uint32_t flash_size;
iap_machine_state_type state;
uint8_t respond_flag;
}iap_info_type;
extern usbd_class_handler hid_iap_class_handler;
extern iap_info_type iap_info;
usb_sts_type class_send_report(void *udev, uint8_t *report, uint16_t len);
iap_result_type usbd_hid_iap_process(void *udev, uint8_t *report, uint16_t len);
void usbd_hid_iap_in_complete(void *udev);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,450 @@
/**
**************************************************************************
* @file hid_iap_desc.c
* @version v2.0.0
* @date 2021-11-26
* @brief usb hid iap device descriptor
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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 "hid_iap_desc.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @defgroup USB_hid_iap_desc
* @brief usb device hid_iap descriptor
* @{
*/
/** @defgroup USB_hid_iap_desc_private_functions
* @{
*/
usbd_desc_t *get_device_descriptor(void);
usbd_desc_t *get_device_qualifier(void);
usbd_desc_t *get_device_configuration(void);
usbd_desc_t *get_device_other_speed(void);
usbd_desc_t *get_device_lang_id(void);
usbd_desc_t *get_device_manufacturer_string(void);
usbd_desc_t *get_device_product_string(void);
usbd_desc_t *get_device_serial_string(void);
usbd_desc_t *get_device_interface_string(void);
usbd_desc_t *get_device_config_string(void);
uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf);
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 hid device descriptor handler structure
*/
usbd_desc_handler hid_iap_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
*/
uint8_t g_usbd_descriptor[USB_DEVICE_DESC_LEN] =
{
USB_DEVICE_DESC_LEN, /* bLength */
USB_DESCIPTOR_TYPE_DEVICE, /* bDescriptorType */
0x00, /* bcdUSB */
0x02,
0x00, /* bDeviceClass */
0x00, /* bDeviceSubClass */
0x00, /* bDeviceProtocol */
USB_MAX_EP0_SIZE, /* bMaxPacketSize */
LBYTE(USBD_VENDOR_ID), /* idVendor */
HBYTE(USBD_VENDOR_ID), /* idVendor */
LBYTE(USBD_PRODUCT_ID), /* idProduct */
HBYTE(USBD_PRODUCT_ID), /* idProduct */
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
*/
uint8_t g_usbd_configuration[USBD_CONFIG_DESC_SIZE] =
{
USB_DEVICE_CFG_DESC_LEN, /* bLength: configuration descriptor size */
USB_DESCIPTOR_TYPE_CONFIGURATION, /* bDescriptorType: configuration */
LBYTE(USBD_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
HBYTE(USBD_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
0x01, /* bNumInterfaces: 1 interface */
0x01, /* bConfigurationValue: configuration value */
0x00, /* iConfiguration: index of string descriptor describing
the configuration */
0xC0, /* bmAttributes: self powered */
0x32, /* MaxPower 100 mA: this current is used for detecting vbus */
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 */
0x02, /* bNumEndpoints: number of endpoints */
USB_CLASS_CODE_HID, /* bInterfaceClass: class code hid */
0x00, /* bInterfaceSubClass: subclass code */
0x00, /* bInterfaceProtocol: protocol code */
0x00, /* iInterface: index of string descriptor */
0x09, /* bLength: size of HID descriptor in bytes */
HID_CLASS_DESC_HID, /* bDescriptorType: HID descriptor type */
LBYTE(HID_BCD_NUM),
HBYTE(HID_BCD_NUM), /* bcdHID: HID class specification release number */
0x00, /* bCountryCode: hardware target conutry */
0x01, /* bNumDescriptors: number of HID class descriptor to follow */
HID_CLASS_DESC_REPORT, /* bDescriptorType: report descriptor type */
LBYTE(sizeof(g_usbd_hid_report)),
HBYTE(sizeof(g_usbd_hid_report)), /* wDescriptorLength: total length of reprot descriptor */
USB_DEVICE_EPT_LEN, /* bLength: size of endpoint descriptor in bytes */
USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
USBD_HID_IN_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
USB_EPT_DESC_INTERRUPT, /* bmAttributes: endpoint attributes */
LBYTE(USBD_IN_MAXPACKET_SIZE),
HBYTE(USBD_IN_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
HID_BINTERVAL_TIME, /* bInterval: interval for polling endpoint for data transfers */
USB_DEVICE_EPT_LEN, /* bLength: size of endpoint descriptor in bytes */
USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
USBD_HID_OUT_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
USB_EPT_DESC_INTERRUPT, /* bmAttributes: endpoint attributes */
LBYTE(USBD_OUT_MAXPACKET_SIZE),
HBYTE(USBD_OUT_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
HID_BINTERVAL_TIME, /* bInterval: interval for polling endpoint for data transfers */
};
/**
* @brief usb hid report descriptor
*/
uint8_t g_usbd_hid_report[USBD_HID_SIZ_REPORT_DESC] =
{
0x06, 0xFF, 0x00, /* USAGE_PAGE(Vendor Page:0xFF00) */
0x09, 0x01, /* USAGE (Demo Kit) */
0xa1, 0x01, /* COLLECTION (Application) */
/* 6 */
0x15, 0x00, /* LOGICAL_MINIMUM (0) */
0x25, 0xFF, /* LOGICAL_MAXIMUM (255) */
0x75, 0x08, /* REPORT_SIZE (8) */
0x95, 0x40, /* REPORT_COUNT (64) */
0x09, 0x01,
0x81, 0x02,
0x95, 0x40,
0x09, 0x01,
0x91, 0x02,
0x95, 0x01,
0x09, 0x01,
0xB1, 0x02,
0xC0
};
/**
* @brief usb hid descriptor
*/
uint8_t g_hid_usb_desc[9] =
{
0x09, /* bLength: size of HID descriptor in bytes */
HID_CLASS_DESC_HID, /* bDescriptorType: HID descriptor type */
LBYTE(HID_BCD_NUM),
HBYTE(HID_BCD_NUM), /* bcdHID: HID class specification release number */
0x00, /* bCountryCode: hardware target conutry */
0x01, /* bNumDescriptors: number of HID class descriptor to follow */
HID_CLASS_DESC_REPORT, /* bDescriptorType: report descriptor type */
LBYTE(sizeof(g_usbd_hid_report)),
HBYTE(sizeof(g_usbd_hid_report)), /* wDescriptorLength: total length of reprot descriptor */
};
/**
* @brief usb string lang id
*/
uint8_t g_string_lang_id[USBD_SIZ_STRING_LANGID] =
{
USBD_SIZ_STRING_LANGID,
USB_DESCIPTOR_TYPE_STRING,
0x09,
0x04,
};
/**
* @brief usb string serial
*/
uint8_t g_string_serial[USBD_SIZ_STRING_SERIAL] =
{
USBD_SIZ_STRING_SERIAL,
USB_DESCIPTOR_TYPE_STRING,
};
/* device descriptor */
usbd_desc_t device_descriptor =
{
USB_DEVICE_DESC_LEN,
g_usbd_descriptor
};
/* config descriptor */
usbd_desc_t config_descriptor =
{
USBD_CONFIG_DESC_SIZE,
g_usbd_configuration
};
/* langid descriptor */
usbd_desc_t langid_descriptor =
{
USBD_SIZ_STRING_LANGID,
g_string_lang_id
};
/* serial descriptor */
usbd_desc_t serial_descriptor =
{
USBD_SIZ_STRING_SERIAL,
g_string_serial
};
usbd_desc_t vp_desc;
/**
* @brief standard usb unicode convert
* @param string: source string
* @param unicode_buf: unicode buffer
* @retval length
*/
uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf)
{
uint16_t str_len = 0, id_pos = 2;
uint8_t *tmp_str = string;
while(*tmp_str != '\0')
{
str_len ++;
unicode_buf[id_pos ++] = *tmp_str ++;
unicode_buf[id_pos ++] = 0x00;
}
str_len = str_len * 2 + 2;
unicode_buf[0] = str_len;
unicode_buf[1] = USB_DESCIPTOR_TYPE_STRING;
return str_len;
}
/**
* @brief usb int convert to unicode
* @param value: int value
* @param pbus: unicode buffer
* @param len: length
* @retval none
*/
static void usbd_int_to_unicode (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;
}
}
/**
* @brief usb get serial number
* @param none
* @retval none
*/
static void get_serial_num(void)
{
uint32_t serial0, serial1, serial2;
serial0 = *(uint32_t*)MCU_ID1;
serial1 = *(uint32_t*)MCU_ID2;
serial2 = *(uint32_t*)MCU_ID3;
serial0 += serial2;
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
* @retval usbd_desc
*/
usbd_desc_t *get_device_descriptor(void)
{
return &device_descriptor;
}
/**
* @brief get device qualifier
* @param none
* @retval usbd_desc
*/
usbd_desc_t * get_device_qualifier(void)
{
return NULL;
}
/**
* @brief get config descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_configuration(void)
{
return &config_descriptor;
}
/**
* @brief get other speed descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_other_speed(void)
{
return NULL;
}
/**
* @brief get lang id descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_lang_id(void)
{
return &langid_descriptor;
}
/**
* @brief get manufacturer descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_manufacturer_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_MANUFACTURER_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get product descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_product_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_PRODUCT_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get serial descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_serial_string(void)
{
get_serial_num();
return &serial_descriptor;
}
/**
* @brief get interface descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_interface_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_INTERFACE_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get device config descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_config_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_CONFIGURATION_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/

View File

@@ -0,0 +1,102 @@
/**
**************************************************************************
* @file hid_iap_desc.h
* @version v2.0.0
* @date 2021-11-26
* @brief usb hid iap descriptor header file
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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.
*
**************************************************************************
*/
/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __HID_IAP_DESC_H
#define __HID_IAP_DESC_H
#ifdef __cplusplus
extern "C" {
#endif
#include "hid_iap_class.h"
#include "usbd_core.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @addtogroup USB_hid_iap_desc
* @{
*/
/** @defgroup USB_hid_iap_desc_definition
* @{
*/
#define HID_BCD_NUM 0x0110
#define HID_CLASS_DESC_HID 0x21
#define HID_CLASS_DESC_REPORT 0x22
#define HID_CLASS_DESC_PHYSICAL 0x23
#define USBD_VENDOR_ID 0x2E3C
#define USBD_PRODUCT_ID 0xAF01
#define USBD_CONFIG_DESC_SIZE 41
#define USBD_HID_SIZ_REPORT_DESC 32
#define USBD_SIZ_STRING_LANGID 4
#define USBD_SIZ_STRING_SERIAL 0x1A
#define USBD_DESC_MANUFACTURER_STRING "Artery"
#define USBD_DESC_PRODUCT_STRING "HID IAP"
#define USBD_DESC_CONFIGURATION_STRING "HID IAP Config"
#define USBD_DESC_INTERFACE_STRING "HID IAP Interface"
#define HID_BINTERVAL_TIME 0x01
#define MCU_ID1 (0x1FFFF7E8)
#define MCU_ID2 (0x1FFFF7EC)
#define MCU_ID3 (0x1FFFF7F0)
extern uint8_t g_usbd_descriptor[USB_DEVICE_DESC_LEN];
extern uint8_t g_usbd_configuration[USBD_CONFIG_DESC_SIZE];
extern uint8_t g_usbd_hid_report[USBD_HID_SIZ_REPORT_DESC];
extern uint8_t g_hid_usb_desc[9];
extern usbd_desc_handler hid_iap_desc_handler;
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,504 @@
/**
**************************************************************************
* @file keyboard_class.c
* @version v2.0.0
* @date 2021-11-26
* @brief usb hid keyboard class type
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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 "usbd_core.h"
#include "keyboard_class.h"
#include "keyboard_desc.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @defgroup USB_keyboard_class
* @brief usb device keyboard demo
* @{
*/
/** @defgroup USB_keyboard_class_private_functions
* @{
*/
usb_sts_type class_init_handler(void *udev);
usb_sts_type class_clear_handler(void *udev);
usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup);
usb_sts_type class_ept0_tx_handler(void *udev);
usb_sts_type class_ept0_rx_handler(void *udev);
usb_sts_type class_in_handler(void *udev, uint8_t ept_num);
usb_sts_type class_out_handler(void *udev, uint8_t ept_num);
usb_sts_type class_sof_handler(void *udev);
usb_sts_type class_event_handler(void *udev, usbd_event_type event);
/* hid static variable */
static uint32_t hid_protocol = 0;
static uint32_t hid_set_idle = 0;
static uint32_t alt_setting = 0;
static uint8_t hid_state;
uint8_t hid_set_report[64];
uint8_t hid_suspend_flag = 0;
uint8_t g_u8tx_completed = 0;
#define SHIFT 0x80
const unsigned char _asciimap[128] =
{
0x00,// NUL
0x00,// SOH
0x00,// STX
0x00,// ETX
0x00,// EOT
0x00,// ENQ
0x00,// ACK
0x00,// BEL
0x2A,// BS Backspace
0x2B,// TAB Tab
0x28,// LF Enter
0x00,// VT
0x00,// FF
0x00,// CR
0x00,// SO
0x00,// SI
0x00,// DEL
0x00,// DC1
0x00,// DC2
0x00,// DC3
0x00,// DC4
0x00,// NAK
0x00,// SYN
0x00,// ETB
0x00,// CAN
0x00,// EM
0x00,// SUB
0x00,// ESC
0x00,// FS
0x00,// GS
0x00,// RS
0x00,// US
0x2C,// ' '
0x1E|SHIFT,// !
0x34|SHIFT,// "
0x20|SHIFT,// #
0x21|SHIFT,// $
0x22|SHIFT,// %
0x24|SHIFT,// &
0x34, // '
0x26|SHIFT,// (
0x27|SHIFT,// )
0x25|SHIFT,// *
0x2E|SHIFT,// +
0x36,// ,
0x2D,// -
0x37,// .
0x38,// /
0x27,// 0
0x1E,// 1
0x1F,// 2
0x20,// 3
0x21,// 4
0x22,// 5
0x23,// 6
0x24,// 7
0x25,// 8
0x26,// 9
0x33|SHIFT,// :
0x33, // ;
0x36|SHIFT,// <
0x2E, // =
0x37|SHIFT,// >
0x38|SHIFT,// ?
0x1F|SHIFT,// @
0x04|SHIFT,// A
0x05|SHIFT,// B
0x06|SHIFT,// C
0x07|SHIFT,// D
0x08|SHIFT,// E
0x09|SHIFT,// F
0x0A|SHIFT,// G
0x0B|SHIFT,// H
0x0C|SHIFT,// I
0x0D|SHIFT,// J
0x0E|SHIFT,// K
0x0F|SHIFT,// L
0x10|SHIFT,// M
0x11|SHIFT,// N
0x12|SHIFT,// O
0x13|SHIFT,// P
0x14|SHIFT,// Q
0x15|SHIFT,// R
0x16|SHIFT,// S
0x17|SHIFT,// T
0x18|SHIFT,// U
0x19|SHIFT,// V
0x1A|SHIFT,// W
0x1B|SHIFT,// X
0x1C|SHIFT,// Y
0x1D|SHIFT,// Z
0x2F, // [
0x31, // bslash
0x30, // ]
0x23|SHIFT,// ^
0x2D|SHIFT,// _
0x35, // `
0x04, // a
0x05, // b
0x06, // c
0x07, // d
0x08, // e
0x09, // f
0x0A, // g
0x0B, // h
0x0C, // i
0x0D, // j
0x0E, // k
0x0F, // l
0x10, // m
0x11, // n
0x12, // o
0x13, // p
0x14, // q
0x15, // r
0x16, // s
0x17, // t
0x18, // u
0x19, // v
0x1A, // w
0x1B, // x
0x1C, // y
0x1D, // z
0x2f|SHIFT,//
0x31|SHIFT,// |
0x30|SHIFT,// }
0x35|SHIFT,// ~
0 // DEL
};
/* usb device class handler */
usbd_class_handler keyboard_class_handler =
{
class_init_handler,
class_clear_handler,
class_setup_handler,
class_ept0_tx_handler,
class_ept0_rx_handler,
class_in_handler,
class_out_handler,
class_sof_handler,
class_event_handler,
};
/**
* @brief initialize usb endpoint
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_init_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
/* open hid in endpoint */
usbd_ept_open(pudev, USBD_HID_IN_EPT, EPT_INT_TYPE, USBD_IN_MAXPACKET_SIZE);
g_u8tx_completed = 1;
return status;
}
/**
* @brief clear endpoint or other state
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_clear_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
/* close hid in endpoint */
usbd_ept_close(pudev, USBD_HID_IN_EPT);
return status;
}
/**
* @brief usb device class setup request handler
* @param udev: to the structure of usbd_core_type
* @param setup: setup packet
* @retval status of usb_sts_type
*/
usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
uint16_t len;
uint8_t *buf;
switch(setup->bmRequestType & USB_REQ_TYPE_RESERVED)
{
/* class request */
case USB_REQ_TYPE_CLASS:
switch(setup->bRequest)
{
case HID_REQ_SET_PROTOCOL:
hid_protocol = (uint8_t)setup->wValue;
break;
case HID_REQ_GET_PROTOCOL:
usbd_ctrl_send(pudev, (uint8_t *)&hid_protocol, 1);
break;
case HID_REQ_SET_IDLE:
hid_set_idle = (uint8_t)(setup->wValue >> 8);
break;
case HID_REQ_GET_IDLE:
usbd_ctrl_send(pudev, (uint8_t *)&hid_set_idle, 1);
break;
case HID_REQ_SET_REPORT:
hid_state = HID_REQ_SET_REPORT;
usbd_ctrl_recv(pudev, hid_set_report, setup->wLength);
break;
default:
usbd_ctrl_unsupport(pudev);
break;
}
break;
/* standard request */
case USB_REQ_TYPE_STANDARD:
switch(setup->bRequest)
{
case USB_STD_REQ_GET_DESCRIPTOR:
if(setup->wValue >> 8 == HID_REPORT_DESC)
{
len = MIN(USBD_HID_SIZ_REPORT_DESC, setup->wLength);
buf = (uint8_t *)g_usbd_hid_report;
}
else if(setup->wValue >> 8 == HID_DESCRIPTOR_TYPE)
{
len = MIN(9, setup->wLength);
buf = (uint8_t *)g_hid_usb_desc;
}
usbd_ctrl_send(pudev, (uint8_t *)buf, len);
break;
case USB_STD_REQ_GET_INTERFACE:
usbd_ctrl_send(pudev, (uint8_t *)&alt_setting, 1);
break;
case USB_STD_REQ_SET_INTERFACE:
alt_setting = setup->wValue;
break;
default:
break;
}
break;
default:
usbd_ctrl_unsupport(pudev);
break;
}
return status;
}
/**
* @brief usb device class endpoint 0 in status stage complete
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_ept0_tx_handler(void *udev)
{
usb_sts_type status = USB_OK;
/* ...user code... */
return status;
}
/**
* @brief usb device class endpoint 0 out status stage complete
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_ept0_rx_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
uint32_t recv_len = usbd_get_recv_len(pudev, 0);
/* ...user code... */
if( hid_state == HID_REQ_SET_REPORT)
{
/* hid buffer process */
hid_state = 0;
}
return status;
}
/**
* @brief usb device class transmision complete handler
* @param udev: to the structure of usbd_core_type
* @param ept_num: endpoint number
* @retval status of usb_sts_type
*/
usb_sts_type class_in_handler(void *udev, uint8_t ept_num)
{
usb_sts_type status = USB_OK;
/* ...user code...
trans next packet data
*/
g_u8tx_completed = 1;
return status;
}
/**
* @brief usb device class endpoint receive data
* @param udev: to the structure of usbd_core_type
* @param ept_num: endpoint number
* @retval status of usb_sts_type
*/
usb_sts_type class_out_handler(void *udev, uint8_t ept_num)
{
usb_sts_type status = USB_OK;
return status;
}
/**
* @brief usb device class sof handler
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_sof_handler(void *udev)
{
usb_sts_type status = USB_OK;
/* ...user code... */
return status;
}
/**
* @brief usb device class event handler
* @param udev: to the structure of usbd_core_type
* @param event: usb device event
* @retval status of usb_sts_type
*/
usb_sts_type class_event_handler(void *udev, usbd_event_type event)
{
usb_sts_type status = USB_OK;
switch(event)
{
case USBD_RESET_EVENT:
/* ...user code... */
break;
case USBD_SUSPEND_EVENT:
hid_suspend_flag = 1;
/* ...user code... */
break;
case USBD_WAKEUP_EVENT:
/* ...user code... */
break;
default:
break;
}
return status;
}
/**
* @brief usb device class send report
* @param udev: to the structure of usbd_core_type
* @param report: report buffer
* @param len: report length
* @retval status of usb_sts_type
*/
usb_sts_type class_send_report(void *udev, uint8_t *report, uint16_t len)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
if(usbd_connect_state_get(pudev) == USB_CONN_STATE_CONFIGURED)
usbd_ept_send(pudev, USBD_HID_IN_EPT, report, len);
return status;
}
/**
* @brief usb device class report function
* @param udev: to the structure of usbd_core_type
* @param op: operation
* @retval none
*/
void usb_hid_keyboard_send_char(void *udev, uint8_t ascii_code)
{
uint8_t key_data = 0;
static uint8_t temp = 0;
static uint8_t keyboard_buf[8] = {0, 0, 6, 0, 0, 0, 0, 0};
if(ascii_code > 128)
{
ascii_code = 0;
}
else
{
ascii_code = _asciimap[ascii_code];
if(!ascii_code)
{
ascii_code = 0;
}
if(ascii_code & SHIFT)
{
key_data = 0x2;
ascii_code &= 0x7F;
}
}
if((temp == ascii_code) && (ascii_code != 0))
{
keyboard_buf[0] = 0;
keyboard_buf[2] = 0;
class_send_report(udev, keyboard_buf, 8);
}
else
{
keyboard_buf[0] = key_data;
keyboard_buf[2] = ascii_code;
class_send_report(udev, keyboard_buf, 8);
}
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/

View File

@@ -0,0 +1,108 @@
/**
**************************************************************************
* @file keyboard_class.h
* @version v2.0.0
* @date 2021-11-26
* @brief usb hid keyboard header file
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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.
*
**************************************************************************
*/
/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __KEYBOARD_CLASS_H
#define __KEYBOARD_CLASS_H
#ifdef __cplusplus
extern "C" {
#endif
#include "usb_std.h"
#include "usbd_core.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @addtogroup USB_keyboard_class
* @{
*/
/** @defgroup USB_keyboard_class_endpoint_definition
* @{
*/
/**
* @brief usb hid use endpoint define
*/
#define USBD_HID_IN_EPT 0x81
/**
* @brief usb hid in and out max packet size define
*/
#define USBD_IN_MAXPACKET_SIZE 0x40
#define USBD_OUT_MAXPACKET_SIZE 0x40
/**
* @}
*/
/** @defgroup USB_hid_class_request_code_definition
* @{
*/
/**
* @brief usb hid class request code define
*/
#define HID_REQ_SET_PROTOCOL 0x0B
#define HID_REQ_GET_PROTOCOL 0x03
#define HID_REQ_SET_IDLE 0x0A
#define HID_REQ_GET_IDLE 0x02
#define HID_REQ_SET_REPORT 0x09
#define HID_REQ_GET_REPORT 0x01
#define HID_DESCRIPTOR_TYPE 0x21
#define HID_REPORT_DESC 0x22
/**
* @}
*/
/** @defgroup USB_hid_class_exported_functions
* @{
*/
extern usbd_class_handler keyboard_class_handler;
extern uint8_t g_u8tx_completed;
usb_sts_type class_send_report(void *udev, uint8_t *report, uint16_t len);
void usb_hid_keyboard_send_char(void *udev, uint8_t ascii_code);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,449 @@
/**
**************************************************************************
* @file keyboard_desc.c
* @version v2.0.0
* @date 2021-11-26
* @brief usb hid keyboard device descriptor
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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 "keyboard_desc.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @defgroup USB_keyboard_desc
* @brief usb device keyboard descriptor
* @{
*/
/** @defgroup USB_keyboard_desc_private_functions
* @{
*/
usbd_desc_t *get_device_descriptor(void);
usbd_desc_t *get_device_qualifier(void);
usbd_desc_t *get_device_configuration(void);
usbd_desc_t *get_device_other_speed(void);
usbd_desc_t *get_device_lang_id(void);
usbd_desc_t *get_device_manufacturer_string(void);
usbd_desc_t *get_device_product_string(void);
usbd_desc_t *get_device_serial_string(void);
usbd_desc_t *get_device_interface_string(void);
usbd_desc_t *get_device_config_string(void);
uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf);
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 keyboard device descriptor handler structure
*/
usbd_desc_handler keyboard_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
*/
uint8_t g_usbd_descriptor[USB_DEVICE_DESC_LEN] =
{
USB_DEVICE_DESC_LEN, /* bLength */
USB_DESCIPTOR_TYPE_DEVICE, /* bDescriptorType */
0x00, /* bcdUSB */
0x02,
0x00, /* bDeviceClass */
0x00, /* bDeviceSubClass */
0x00, /* bDeviceProtocol */
USB_MAX_EP0_SIZE, /* bMaxPacketSize */
LBYTE(USBD_VENDOR_ID), /* idVendor */
HBYTE(USBD_VENDOR_ID), /* idVendor */
LBYTE(USBD_PRODUCT_ID), /* idProduct */
HBYTE(USBD_PRODUCT_ID), /* idProduct */
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
*/
uint8_t g_usbd_configuration[USBD_CONFIG_DESC_SIZE] =
{
USB_DEVICE_CFG_DESC_LEN, /* bLength: configuration descriptor size */
USB_DESCIPTOR_TYPE_CONFIGURATION, /* bDescriptorType: configuration */
LBYTE(USBD_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
HBYTE(USBD_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
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 */
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 */
0x01, /* bInterfaceProtocol: protocol code */
0x00, /* iInterface: index of string descriptor */
0x09, /* bLength: size of HID descriptor in bytes */
HID_CLASS_DESC_HID, /* bDescriptorType: HID descriptor type */
LBYTE(HID_BCD_NUM),
HBYTE(HID_BCD_NUM), /* bcdHID: HID class specification release number */
0x00, /* bCountryCode: hardware target conutry */
0x01, /* bNumDescriptors: number of HID class descriptor to follow */
HID_CLASS_DESC_REPORT, /* bDescriptorType: report descriptor type */
LBYTE(sizeof(g_usbd_hid_report)),
HBYTE(sizeof(g_usbd_hid_report)), /* wDescriptorLength: total length of reprot descriptor */
USB_DEVICE_EPT_LEN, /* bLength: size of endpoint descriptor in bytes */
USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
USBD_HID_IN_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
USB_EPT_DESC_INTERRUPT, /* bmAttributes: endpoint attributes */
LBYTE(USBD_IN_MAXPACKET_SIZE),
HBYTE(USBD_IN_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
HID_BINTERVAL_TIME, /* bInterval: interval for polling endpoint for data transfers */
};
/**
* @brief usb mouse report descriptor
*/
uint8_t g_usbd_hid_report[USBD_HID_SIZ_REPORT_DESC] =
{
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x06, // USAGE (Keyboard)
0xa1, 0x01, // COLLECTION (Application)
0x05, 0x07, // USAGE_PAGE (Keyboard)
0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x08, // REPORT_COUNT (8)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x08, // REPORT_SIZE (8)
0x81, 0x03, // INPUT (Cnst,Var,Abs)
0x95, 0x05, // REPORT_COUNT (5)
0x75, 0x01, // REPORT_SIZE (1)
0x05, 0x08, // USAGE_PAGE (LEDs)
0x19, 0x01, // USAGE_MINIMUM (Num Lock)
0x29, 0x05, // USAGE_MAXIMUM (Kana)
0x91, 0x02, // OUTPUT (Data,Var,Abs)
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x03, // REPORT_SIZE (3)
0x91, 0x03, // OUTPUT (Cnst,Var,Abs)
0x95, 0x06, // REPORT_COUNT (6)
0x75, 0x08, // REPORT_SIZE (8)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0xFF, // LOGICAL_MAXIMUM (255)
0x05, 0x07, // USAGE_PAGE (Keyboard)
0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
0x81, 0x00, // INPUT (Data,Ary,Abs)
0xc0 // END_COLLECTION
};
/**
* @brief usb hid descriptor
*/
uint8_t g_hid_usb_desc[9] =
{
0x09, /* bLength: size of HID descriptor in bytes */
HID_CLASS_DESC_HID, /* bDescriptorType: HID descriptor type */
LBYTE(HID_BCD_NUM),
HBYTE(HID_BCD_NUM), /* bcdHID: HID class specification release number */
0x00, /* bCountryCode: hardware target conutry */
0x01, /* bNumDescriptors: number of HID class descriptor to follow */
HID_CLASS_DESC_REPORT, /* bDescriptorType: report descriptor type */
LBYTE(sizeof(g_usbd_hid_report)),
HBYTE(sizeof(g_usbd_hid_report)), /* wDescriptorLength: total length of reprot descriptor */
};
/**
* @brief usb string lang id
*/
uint8_t g_string_lang_id[USBD_SIZ_STRING_LANGID] =
{
USBD_SIZ_STRING_LANGID,
USB_DESCIPTOR_TYPE_STRING,
0x09,
0x04,
};
/**
* @brief usb string serial
*/
uint8_t g_string_serial[USBD_SIZ_STRING_SERIAL] =
{
USBD_SIZ_STRING_SERIAL,
USB_DESCIPTOR_TYPE_STRING,
};
/* device descriptor */
usbd_desc_t device_descriptor =
{
USB_DEVICE_DESC_LEN,
g_usbd_descriptor
};
/* config descriptor */
usbd_desc_t config_descriptor =
{
USBD_CONFIG_DESC_SIZE,
g_usbd_configuration
};
/* langid descriptor */
usbd_desc_t langid_descriptor =
{
USBD_SIZ_STRING_LANGID,
g_string_lang_id
};
/* serial descriptor */
usbd_desc_t serial_descriptor =
{
USBD_SIZ_STRING_SERIAL,
g_string_serial
};
usbd_desc_t vp_desc;
/**
* @brief standard usb unicode convert
* @param string: source string
* @param unicode_buf: unicode buffer
* @retval length
*/
uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf)
{
uint16_t str_len = 0, id_pos = 2;
uint8_t *tmp_str = string;
while(*tmp_str != '\0')
{
str_len ++;
unicode_buf[id_pos ++] = *tmp_str ++;
unicode_buf[id_pos ++] = 0x00;
}
str_len = str_len * 2 + 2;
unicode_buf[0] = str_len;
unicode_buf[1] = USB_DESCIPTOR_TYPE_STRING;
return str_len;
}
/**
* @brief usb int convert to unicode
* @param value: int value
* @param pbus: unicode buffer
* @param len: length
* @retval none
*/
static void usbd_int_to_unicode (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;
}
}
/**
* @brief usb get serial number
* @param none
* @retval none
*/
static void get_serial_num(void)
{
uint32_t serial0, serial1, serial2;
serial0 = *(uint32_t*)MCU_ID1;
serial1 = *(uint32_t*)MCU_ID2;
serial2 = *(uint32_t*)MCU_ID3;
serial0 += serial2;
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
* @retval usbd_desc
*/
usbd_desc_t *get_device_descriptor(void)
{
return &device_descriptor;
}
/**
* @brief get device qualifier
* @param none
* @retval usbd_desc
*/
usbd_desc_t * get_device_qualifier(void)
{
return NULL;
}
/**
* @brief get config descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_configuration(void)
{
return &config_descriptor;
}
/**
* @brief get other speed descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_other_speed(void)
{
return NULL;
}
/**
* @brief get lang id descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_lang_id(void)
{
return &langid_descriptor;
}
/**
* @brief get manufacturer descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_manufacturer_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_MANUFACTURER_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get product descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_product_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_PRODUCT_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get serial descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_serial_string(void)
{
get_serial_num();
return &serial_descriptor;
}
/**
* @brief get interface descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_interface_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_INTERFACE_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get device config descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_config_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_CONFIGURATION_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/

View File

@@ -0,0 +1,119 @@
/**
**************************************************************************
* @file keyboard_desc.h
* @version v2.0.0
* @date 2021-11-26
* @brief usb keyboard descriptor header file
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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.
*
**************************************************************************
*/
/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __KEYBOARD_DESC_H
#define __KEYBOARD_DESC_H
#ifdef __cplusplus
extern "C" {
#endif
#include "keyboard_class.h"
#include "usbd_core.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @addtogroup USB_keyboard_desc
* @{
*/
/** @defgroup USB_keyboard_desc_definition
* @{
*/
/**
* @brief usb bcd number define
*/
#define HID_BCD_NUM 0x0110
/**
* @brief usb hid class descriptor define
*/
#define HID_CLASS_DESC_HID 0x21
#define HID_CLASS_DESC_REPORT 0x22
#define HID_CLASS_DESC_PHYSICAL 0x23
/**
* @brief usb vendor id and product id define
*/
#define USBD_VENDOR_ID 0x2E3C
#define USBD_PRODUCT_ID 0x6040
/**
* @brief usb descriptor size define
*/
#define USBD_CONFIG_DESC_SIZE 34
#define USBD_HID_SIZ_REPORT_DESC 63
#define USBD_SIZ_STRING_LANGID 4
#define USBD_SIZ_STRING_SERIAL 0x1A
/**
* @brief usb string define(vendor, product configuration, interface)
*/
#define USBD_DESC_MANUFACTURER_STRING "Artery"
#define USBD_DESC_PRODUCT_STRING "Keyboard"
#define USBD_DESC_CONFIGURATION_STRING "Keyboard Config"
#define USBD_DESC_INTERFACE_STRING "Keyboard Interface"
/**
* @brief usb hid endpoint interval define
*/
#define HID_BINTERVAL_TIME 0x0A
/**
* @brief usb mcu id address deine
*/
#define MCU_ID1 (0x1FFFF7E8)
#define MCU_ID2 (0x1FFFF7EC)
#define MCU_ID3 (0x1FFFF7F0)
/**
* @}
*/
extern uint8_t g_usbd_descriptor[USB_DEVICE_DESC_LEN];
extern uint8_t g_usbd_configuration[USBD_CONFIG_DESC_SIZE];
extern uint8_t g_usbd_hid_report[USBD_HID_SIZ_REPORT_DESC];
extern uint8_t g_hid_usb_desc[9];
extern usbd_desc_handler keyboard_desc_handler;
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,366 @@
/**
**************************************************************************
* @file mouse_class.c
* @version v2.0.0
* @date 2021-11-26
* @brief usb hid mouse class type
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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 "usbd_core.h"
#include "mouse_class.h"
#include "mouse_desc.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @defgroup USB_mouse_class
* @brief usb device mouse demo
* @{
*/
/** @defgroup USB_mouse_class_private_functions
* @{
*/
usb_sts_type class_init_handler(void *udev);
usb_sts_type class_clear_handler(void *udev);
usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup);
usb_sts_type class_ept0_tx_handler(void *udev);
usb_sts_type class_ept0_rx_handler(void *udev);
usb_sts_type class_in_handler(void *udev, uint8_t ept_num);
usb_sts_type class_out_handler(void *udev, uint8_t ept_num);
usb_sts_type class_sof_handler(void *udev);
usb_sts_type class_event_handler(void *udev, usbd_event_type event);
/* hid static variable */
static uint32_t hid_protocol = 0;
static uint32_t hid_set_idle = 0;
static uint32_t alt_setting = 0;
static uint8_t hid_state;
uint8_t hid_suspend_flag = 0;
uint8_t hid_set_report[64];
/* usb device class handler */
usbd_class_handler mouse_class_handler =
{
class_init_handler,
class_clear_handler,
class_setup_handler,
class_ept0_tx_handler,
class_ept0_rx_handler,
class_in_handler,
class_out_handler,
class_sof_handler,
class_event_handler,
};
/**
* @brief initialize usb endpoint
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_init_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
/* open hid in endpoint */
usbd_ept_open(pudev, USBD_HID_IN_EPT, EPT_INT_TYPE, USBD_IN_MAXPACKET_SIZE);
return status;
}
/**
* @brief clear endpoint or other state
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_clear_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
/* close hid in endpoint */
usbd_ept_close(pudev, USBD_HID_IN_EPT);
return status;
}
/**
* @brief usb device class setup request handler
* @param udev: to the structure of usbd_core_type
* @param setup: setup packet
* @retval status of usb_sts_type
*/
usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
uint16_t len;
uint8_t *buf;
switch(setup->bmRequestType & USB_REQ_TYPE_RESERVED)
{
/* class request */
case USB_REQ_TYPE_CLASS:
switch(setup->bRequest)
{
case HID_REQ_SET_PROTOCOL:
hid_protocol = (uint8_t)setup->wValue;
break;
case HID_REQ_GET_PROTOCOL:
usbd_ctrl_send(pudev, (uint8_t *)&hid_protocol, 1);
break;
case HID_REQ_SET_IDLE:
hid_set_idle = (uint8_t)(setup->wValue >> 8);
break;
case HID_REQ_GET_IDLE:
usbd_ctrl_send(pudev, (uint8_t *)&hid_set_idle, 1);
break;
case HID_REQ_SET_REPORT:
hid_state = HID_REQ_SET_REPORT;
usbd_ctrl_recv(pudev, hid_set_report, setup->wLength);
break;
default:
usbd_ctrl_unsupport(pudev);
break;
}
break;
/* standard request */
case USB_REQ_TYPE_STANDARD:
switch(setup->bRequest)
{
case USB_STD_REQ_GET_DESCRIPTOR:
if(setup->wValue >> 8 == HID_REPORT_DESC)
{
len = MIN(USBD_HID_SIZ_REPORT_DESC, setup->wLength);
buf = (uint8_t *)g_usbd_hid_report;
}
else if(setup->wValue >> 8 == HID_DESCRIPTOR_TYPE)
{
len = MIN(9, setup->wLength);
buf = (uint8_t *)g_hid_usb_desc;
}
usbd_ctrl_send(pudev, (uint8_t *)buf, len);
break;
case USB_STD_REQ_GET_INTERFACE:
usbd_ctrl_send(pudev, (uint8_t *)&alt_setting, 1);
break;
case USB_STD_REQ_SET_INTERFACE:
alt_setting = setup->wValue;
break;
}
break;
default:
usbd_ctrl_unsupport(pudev);
break;
}
return status;
}
/**
* @brief usb device class endpoint 0 in status stage complete
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_ept0_tx_handler(void *udev)
{
usb_sts_type status = USB_OK;
/* ...user code... */
return status;
}
/**
* @brief usb device class endpoint 0 out status stage complete
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_ept0_rx_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
uint32_t recv_len = usbd_get_recv_len(pudev, 0);
/* ...user code... */
if( hid_state == HID_REQ_SET_REPORT)
{
/* hid buffer process */
hid_state = 0;
}
return status;
}
/**
* @brief usb device class transmision complete handler
* @param udev: to the structure of usbd_core_type
* @param ept_num: endpoint number
* @retval status of usb_sts_type
*/
usb_sts_type class_in_handler(void *udev, uint8_t ept_num)
{
usb_sts_type status = USB_OK;
/* ...user code...
trans next packet data
*/
return status;
}
/**
* @brief usb device class endpoint receive data
* @param udev: to the structure of usbd_core_type
* @param ept_num: endpoint number
* @retval status of usb_sts_type
*/
usb_sts_type class_out_handler(void *udev, uint8_t ept_num)
{
usb_sts_type status = USB_OK;
return status;
}
/**
* @brief usb device class sof handler
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_sof_handler(void *udev)
{
usb_sts_type status = USB_OK;
/* ...user code... */
return status;
}
/**
* @brief usb device class event handler
* @param udev: to the structure of usbd_core_type
* @param event: usb device event
* @retval status of usb_sts_type
*/
usb_sts_type class_event_handler(void *udev, usbd_event_type event)
{
usb_sts_type status = USB_OK;
switch(event)
{
case USBD_RESET_EVENT:
/* ...user code... */
break;
case USBD_SUSPEND_EVENT:
hid_suspend_flag = 1;
/* ...user code... */
break;
case USBD_WAKEUP_EVENT:
/* ...user code... */
break;
default:
break;
}
return status;
}
/**
* @brief usb device class send report
* @param udev: to the structure of usbd_core_type
* @param report: report buffer
* @param len: report length
* @retval status of usb_sts_type
*/
usb_sts_type class_send_report(void *udev, uint8_t *report, uint16_t len)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
if(usbd_connect_state_get(pudev) == USB_CONN_STATE_CONFIGURED)
{
usbd_flush_tx_fifo(pudev, USBD_HID_IN_EPT);
usbd_ept_send(pudev, USBD_HID_IN_EPT, report, len);
}
return status;
}
/**
* @brief usb device class report function
* @param udev: to the structure of usbd_core_type
* @param op: operation
* @retval none
*/
void usb_hid_mouse_send(void *udev, uint8_t op)
{
static uint8_t mouse_buffer[4] = {0, 0, 0, 0};
int8_t posx = 0, posy = 0, button = 0;
switch(op)
{
case LEFT_BUTTON:
button = 0x01;
break;
case RIGHT_BUTTON:
button = 0x2;
break;
case UP_MOVE:
posy -= MOVE_STEP;
break;
case DOWN_MOVE:
posy += MOVE_STEP;
break;
case LEFT_MOVE:
posx -= MOVE_STEP;
break;
case RIGHT_MOVE:
posx += MOVE_STEP;
break;
default:
break;
}
mouse_buffer[0] = button;
mouse_buffer[1] = posx;
mouse_buffer[2] = posy;
class_send_report(udev, mouse_buffer, 4);
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/

View File

@@ -0,0 +1,118 @@
/**
**************************************************************************
* @file mouse_class.h
* @version v2.0.0
* @date 2021-11-26
* @brief usb hid mouse header file
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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.
*
**************************************************************************
*/
/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __MOUSE_CLASS_H
#define __MOUSE_CLASS_H
#ifdef __cplusplus
extern "C" {
#endif
#include "usb_std.h"
#include "usbd_core.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @addtogroup USB_mouse_class
* @{
*/
/** @defgroup USB_mouse_class_endpoint_definition
* @{
*/
/**
* @brief usb hid use endpoint define
*/
#define USBD_HID_IN_EPT 0x81
/**
* @brief usb hid in and out max packet size define
*/
#define USBD_IN_MAXPACKET_SIZE 0x40
/**
* @}
*/
/** @defgroup USB_mouse_class_request_code_definition
* @{
*/
/**
* @brief usb hid class request code define
*/
#define HID_REQ_SET_PROTOCOL 0x0B
#define HID_REQ_GET_PROTOCOL 0x03
#define HID_REQ_SET_IDLE 0x0A
#define HID_REQ_GET_IDLE 0x02
#define HID_REQ_SET_REPORT 0x09
#define HID_REQ_GET_REPORT 0x01
#define HID_DESCRIPTOR_TYPE 0x21
#define HID_REPORT_DESC 0x22
/**
* @brief usb hid mouse operation
*/
#define MOVE_STEP 20
#define BUTTON_RELEASE 0
#define LEFT_BUTTON 1
#define RIGHT_BUTTON 2
#define LEFT_MOVE 3
#define RIGHT_MOVE 4
#define UP_MOVE 5
#define DOWN_MOVE 6
/**
* @}
*/
/** @defgroup USB_hid_class_exported_functions
* @{
*/
extern usbd_class_handler mouse_class_handler;
usb_sts_type class_send_report(void *udev, uint8_t *report, uint16_t len);
void usb_hid_mouse_send(void *udev, uint8_t op);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,463 @@
/**
**************************************************************************
* @file mouse_desc.c
* @version v2.0.0
* @date 2021-11-26
* @brief usb hid mouse device descriptor
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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
* @{
*/
/** @defgroup USB_mouse_desc
* @brief usb device mouse descriptor
* @{
*/
/** @defgroup USB_mouse_desc_private_functions
* @{
*/
usbd_desc_t *get_device_descriptor(void);
usbd_desc_t *get_device_qualifier(void);
usbd_desc_t *get_device_configuration(void);
usbd_desc_t *get_device_other_speed(void);
usbd_desc_t *get_device_lang_id(void);
usbd_desc_t *get_device_manufacturer_string(void);
usbd_desc_t *get_device_product_string(void);
usbd_desc_t *get_device_serial_string(void);
usbd_desc_t *get_device_interface_string(void);
usbd_desc_t *get_device_config_string(void);
uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf);
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
*/
uint8_t g_usbd_descriptor[USB_DEVICE_DESC_LEN] =
{
USB_DEVICE_DESC_LEN, /* bLength */
USB_DESCIPTOR_TYPE_DEVICE, /* bDescriptorType */
0x00, /* bcdUSB */
0x02,
0x00, /* bDeviceClass */
0x00, /* bDeviceSubClass */
0x00, /* bDeviceProtocol */
USB_MAX_EP0_SIZE, /* bMaxPacketSize */
LBYTE(USBD_VENDOR_ID), /* idVendor */
HBYTE(USBD_VENDOR_ID), /* idVendor */
LBYTE(USBD_PRODUCT_ID), /* idProduct */
HBYTE(USBD_PRODUCT_ID), /* idProduct */
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
*/
uint8_t g_usbd_configuration[USBD_CONFIG_DESC_SIZE] =
{
USB_DEVICE_CFG_DESC_LEN, /* bLength: configuration descriptor size */
USB_DESCIPTOR_TYPE_CONFIGURATION, /* bDescriptorType: configuration */
LBYTE(USBD_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
HBYTE(USBD_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
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 */
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 */
0x09, /* bLength: size of HID descriptor in bytes */
HID_CLASS_DESC_HID, /* bDescriptorType: HID descriptor type */
LBYTE(HID_BCD_NUM),
HBYTE(HID_BCD_NUM), /* bcdHID: HID class specification release number */
0x00, /* bCountryCode: hardware target conutry */
0x01, /* bNumDescriptors: number of HID class descriptor to follow */
HID_CLASS_DESC_REPORT, /* bDescriptorType: report descriptor type */
LBYTE(sizeof(g_usbd_hid_report)),
HBYTE(sizeof(g_usbd_hid_report)), /* wDescriptorLength: total length of reprot descriptor */
USB_DEVICE_EPT_LEN, /* bLength: size of endpoint descriptor in bytes */
USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
USBD_HID_IN_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
USB_EPT_DESC_INTERRUPT, /* bmAttributes: endpoint attributes */
LBYTE(USBD_IN_MAXPACKET_SIZE),
HBYTE(USBD_IN_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
HID_BINTERVAL_TIME, /* bInterval: interval for polling endpoint for data transfers */
};
/**
* @brief usb mouse report descriptor
*/
uint8_t g_usbd_hid_report[USBD_HID_SIZ_REPORT_DESC] =
{
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
*/
uint8_t g_hid_usb_desc[9] =
{
0x09, /* bLength: size of HID descriptor in bytes */
HID_CLASS_DESC_HID, /* bDescriptorType: HID descriptor type */
LBYTE(HID_BCD_NUM),
HBYTE(HID_BCD_NUM), /* bcdHID: HID class specification release number */
0x00, /* bCountryCode: hardware target conutry */
0x01, /* bNumDescriptors: number of HID class descriptor to follow */
HID_CLASS_DESC_REPORT, /* bDescriptorType: report descriptor type */
LBYTE(sizeof(g_usbd_hid_report)),
HBYTE(sizeof(g_usbd_hid_report)), /* wDescriptorLength: total length of reprot descriptor */
};
/**
* @brief usb string lang id
*/
uint8_t g_string_lang_id[USBD_SIZ_STRING_LANGID] =
{
USBD_SIZ_STRING_LANGID,
USB_DESCIPTOR_TYPE_STRING,
0x09,
0x04,
};
/**
* @brief usb string serial
*/
uint8_t g_string_serial[USBD_SIZ_STRING_SERIAL] =
{
USBD_SIZ_STRING_SERIAL,
USB_DESCIPTOR_TYPE_STRING,
};
/* device descriptor */
usbd_desc_t device_descriptor =
{
USB_DEVICE_DESC_LEN,
g_usbd_descriptor
};
/* config descriptor */
usbd_desc_t config_descriptor =
{
USBD_CONFIG_DESC_SIZE,
g_usbd_configuration
};
/* langid descriptor */
usbd_desc_t langid_descriptor =
{
USBD_SIZ_STRING_LANGID,
g_string_lang_id
};
/* serial descriptor */
usbd_desc_t serial_descriptor =
{
USBD_SIZ_STRING_SERIAL,
g_string_serial
};
usbd_desc_t vp_desc;
/**
* @brief standard usb unicode convert
* @param string: source string
* @param unicode_buf: unicode buffer
* @retval length
*/
uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf)
{
uint16_t str_len = 0, id_pos = 2;
uint8_t *tmp_str = string;
while(*tmp_str != '\0')
{
str_len ++;
unicode_buf[id_pos ++] = *tmp_str ++;
unicode_buf[id_pos ++] = 0x00;
}
str_len = str_len * 2 + 2;
unicode_buf[0] = str_len;
unicode_buf[1] = USB_DESCIPTOR_TYPE_STRING;
return str_len;
}
/**
* @brief usb int convert to unicode
* @param value: int value
* @param pbus: unicode buffer
* @param len: length
* @retval none
*/
static void usbd_int_to_unicode (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;
}
}
/**
* @brief usb get serial number
* @param none
* @retval none
*/
static void get_serial_num(void)
{
uint32_t serial0, serial1, serial2;
serial0 = *(uint32_t*)MCU_ID1;
serial1 = *(uint32_t*)MCU_ID2;
serial2 = *(uint32_t*)MCU_ID3;
serial0 += serial2;
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
* @retval usbd_desc
*/
usbd_desc_t *get_device_descriptor(void)
{
return &device_descriptor;
}
/**
* @brief get device qualifier
* @param none
* @retval usbd_desc
*/
usbd_desc_t * get_device_qualifier(void)
{
return NULL;
}
/**
* @brief get config descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_configuration(void)
{
return &config_descriptor;
}
/**
* @brief get other speed descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_other_speed(void)
{
return NULL;
}
/**
* @brief get lang id descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_lang_id(void)
{
return &langid_descriptor;
}
/**
* @brief get manufacturer descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_manufacturer_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_MANUFACTURER_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get product descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_product_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_PRODUCT_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get serial descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_serial_string(void)
{
get_serial_num();
return &serial_descriptor;
}
/**
* @brief get interface descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_interface_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_INTERFACE_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get device config descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_config_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_CONFIGURATION_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/

View File

@@ -0,0 +1,119 @@
/**
**************************************************************************
* @file mouse_desc.h
* @version v2.0.0
* @date 2021-11-26
* @brief usb mouse descriptor header file
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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.
*
**************************************************************************
*/
/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __MOUSE_DESC_H
#define __MOUSE_DESC_H
#ifdef __cplusplus
extern "C" {
#endif
#include "mouse_class.h"
#include "usbd_core.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @addtogroup USB_mouse_desc
* @{
*/
/** @defgroup USB_mouse_desc_definition
* @{
*/
/**
* @brief usb bcd number define
*/
#define HID_BCD_NUM 0x0110
/**
* @brief usb hid class descriptor define
*/
#define HID_CLASS_DESC_HID 0x21
#define HID_CLASS_DESC_REPORT 0x22
#define HID_CLASS_DESC_PHYSICAL 0x23
/**
* @brief usb vendor id and product id define
*/
#define USBD_VENDOR_ID 0x2E3C
#define USBD_PRODUCT_ID 0x5710
/**
* @brief usb descriptor size define
*/
#define USBD_CONFIG_DESC_SIZE 34
#define USBD_HID_SIZ_REPORT_DESC 74
#define USBD_SIZ_STRING_LANGID 4
#define USBD_SIZ_STRING_SERIAL 0x1A
/**
* @brief usb string define(vendor, product configuration, interface)
*/
#define USBD_DESC_MANUFACTURER_STRING "Artery"
#define USBD_DESC_PRODUCT_STRING "Mouse"
#define USBD_DESC_CONFIGURATION_STRING "Mouse Config"
#define USBD_DESC_INTERFACE_STRING "Mouse Interface"
/**
* @brief usb hid endpoint interval define
*/
#define HID_BINTERVAL_TIME 0x0A
/**
* @brief usb mcu id address deine
*/
#define MCU_ID1 (0x1FFFF7E8)
#define MCU_ID2 (0x1FFFF7EC)
#define MCU_ID3 (0x1FFFF7F0)
/**
* @}
*/
extern uint8_t g_usbd_descriptor[USB_DEVICE_DESC_LEN];
extern uint8_t g_usbd_configuration[USBD_CONFIG_DESC_SIZE];
extern uint8_t g_usbd_hid_report[USBD_HID_SIZ_REPORT_DESC];
extern uint8_t g_hid_usb_desc[9];
extern usbd_desc_handler mouse_desc_handler;
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,782 @@
/**
**************************************************************************
* @file msc_bot_scsi.c
* @version v2.0.0
* @date 2021-11-26
* @brief usb mass storage bulk-only transport and scsi command
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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 "msc_bot_scsi.h"
#include "msc_diskio.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @defgroup USB_msc_bot_scsi
* @brief usb device class mass storage demo
* @{
*/
/** @defgroup USB_msc_bot_functions
* @{
*/
msc_type msc_struct;
cbw_type cbw_struct;
csw_type csw_struct =
{
CSW_DCSWSIGNATURE,
0x00,
0x00,
CSW_BCSWSTATUS_PASS,
};
uint8_t page00_inquiry_data[] = {
0x00,
0x00,
0x00,
0x00,
0x00,
};
sense_type sense_data =
{
0x70,
0x00,
SENSE_KEY_ILLEGAL_REQUEST,
0x00000000,
0x0A,
0x00000000,
0x20,
0x00,
0x00000000
};
uint8_t mode_sense6_data[8] =
{
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00
};
uint8_t mode_sense10_data[8] =
{
0x00,
0x06,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00
};
/**
* @brief initialize bulk-only transport and scsi
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void bot_scsi_init(void *udev)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
msc_struct.msc_state = MSC_STATE_MACHINE_IDLE;
msc_struct.bot_status = MSC_BOT_STATE_IDLE;
msc_struct.max_lun = MSC_SUPPORT_MAX_LUN - 1;
usb_flush_tx_fifo(pudev->usb_reg, USBD_MSC_BULK_IN_EPT&0x7F);
/* set out endpoint to receive status */
usbd_ept_recv(pudev, USBD_MSC_BULK_OUT_EPT, (uint8_t *)&cbw_struct, CBW_CMD_LENGTH);
}
/**
* @brief reset bulk-only transport and scsi
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void bot_scsi_reset(void *udev)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
msc_struct.msc_state = MSC_STATE_MACHINE_IDLE;
msc_struct.bot_status = MSC_BOT_STATE_RECOVERY;
msc_struct.max_lun = MSC_SUPPORT_MAX_LUN - 1;
usb_flush_tx_fifo(pudev->usb_reg, USBD_MSC_BULK_IN_EPT&0x7F);
/* set out endpoint to receive status */
usbd_ept_recv(pudev, USBD_MSC_BULK_OUT_EPT, (uint8_t *)&cbw_struct, CBW_CMD_LENGTH);
}
/**
* @brief bulk-only transport data in handler
* @param udev: to the structure of usbd_core_type
* @param ept_num: endpoint number
* @retval none
*/
void bot_scsi_datain_handler(void *udev, uint8_t ept_num)
{
switch(msc_struct.msc_state)
{
case MSC_STATE_MACHINE_DATA_IN:
if(bot_scsi_cmd_process(udev) != USB_OK)
{
bot_scsi_send_csw(udev, CSW_BCSWSTATUS_FAILED);
}
break;
case MSC_STATE_MACHINE_LAST_DATA:
case MSC_STATE_MACHINE_SEND_DATA:
bot_scsi_send_csw(udev, CSW_BCSWSTATUS_PASS);
break;
default:
break;
}
}
/**
* @brief bulk-only transport data out handler
* @param udev: to the structure of usbd_core_type
* @param ept_num: endpoint number
* @retval none
*/
void bot_scsi_dataout_handler(void *udev, uint8_t ept_num)
{
switch(msc_struct.msc_state)
{
case MSC_STATE_MACHINE_IDLE:
bot_cbw_decode(udev);
break;
case MSC_STATE_MACHINE_DATA_OUT:
if(bot_scsi_cmd_process(udev) != USB_OK)
{
bot_scsi_send_csw(udev, CSW_BCSWSTATUS_FAILED);
}
break;
}
}
/**
* @brief bulk-only cbw decode
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void bot_cbw_decode(void *udev)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
csw_struct.dCSWTag = cbw_struct.dCBWTage;
csw_struct.dCSWDataResidue = cbw_struct.dCBWDataTransferLength;
/* check param */
if((cbw_struct.dCBWSignature != CBW_DCBWSIGNATURE) ||
(usbd_get_recv_len(pudev, USBD_MSC_BULK_OUT_EPT) != CBW_CMD_LENGTH)
|| (cbw_struct.bCBWLUN > MSC_SUPPORT_MAX_LUN) ||
(cbw_struct.bCBWCBLength < 1) || (cbw_struct.bCBWCBLength > 16))
{
bot_scsi_sense_code(udev, SENSE_KEY_ILLEGAL_REQUEST, INVALID_COMMAND);
msc_struct.bot_status = MSC_BOT_STATE_ERROR;
bot_scsi_stall(udev);
}
else
{
if(bot_scsi_cmd_process(udev) != USB_OK)
{
bot_scsi_stall(udev);
}
else if((msc_struct.msc_state != MSC_STATE_MACHINE_DATA_IN) &&
(msc_struct.msc_state != MSC_STATE_MACHINE_DATA_OUT) &&
(msc_struct.msc_state != MSC_STATE_MACHINE_LAST_DATA))
{
if(msc_struct.data_len == 0)
{
bot_scsi_send_csw(udev, CSW_BCSWSTATUS_PASS);
}
else if(msc_struct.data_len > 0)
{
bot_scsi_send_data(udev, msc_struct.data, msc_struct.data_len);
}
}
}
}
/**
* @brief send bot data
* @param udev: to the structure of usbd_core_type
* @param buffer: data buffer
* @param len: data len
* @retval none
*/
void bot_scsi_send_data(void *udev, uint8_t *buffer, uint32_t len)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
uint32_t data_len = MIN(len, cbw_struct.dCBWDataTransferLength);
csw_struct.dCSWDataResidue -= data_len;
csw_struct.bCSWStatus = CSW_BCSWSTATUS_PASS;
msc_struct.msc_state = MSC_STATE_MACHINE_SEND_DATA;
usbd_ept_send(pudev, USBD_MSC_BULK_IN_EPT,
buffer, data_len);
}
/**
* @brief send command status
* @param udev: to the structure of usbd_core_type
* @param status: csw status
* @retval none
*/
void bot_scsi_send_csw(void *udev, uint8_t status)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
csw_struct.bCSWStatus = status;
csw_struct.dCSWSignature = CSW_DCSWSIGNATURE;
msc_struct.msc_state = MSC_STATE_MACHINE_IDLE;
usbd_ept_send(pudev, USBD_MSC_BULK_IN_EPT,
(uint8_t *)&csw_struct, CSW_CMD_LENGTH);
usbd_ept_recv(pudev, USBD_MSC_BULK_OUT_EPT,
(uint8_t *)&cbw_struct, CBW_CMD_LENGTH);
}
/**
* @brief send scsi sense code
* @param udev: to the structure of usbd_core_type
* @param sense_key: sense key
* @param asc: asc
* @retval none
*/
void bot_scsi_sense_code(void *udev, uint8_t sense_key, uint8_t asc)
{
sense_data.sense_key = sense_key;
sense_data.asc = asc;
}
/**
* @brief check address
* @param udev: to the structure of usbd_core_type
* @param lun: logical units number
* @param blk_offset: blk offset address
* @param blk_count: blk number
* @retval usb_sts_type
*/
usb_sts_type bot_scsi_check_address(void *udev, uint8_t lun, uint32_t blk_offset, uint32_t blk_count)
{
if((blk_offset + blk_count) > msc_struct.blk_nbr[lun])
{
bot_scsi_sense_code(udev, SENSE_KEY_ILLEGAL_REQUEST, ADDRESS_OUT_OF_RANGE);
return USB_FAIL;
}
return USB_OK;
}
/**
* @brief bot endpoint stall
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void bot_scsi_stall(void *udev)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
if((cbw_struct.dCBWDataTransferLength != 0) &&
(cbw_struct.bmCBWFlags == 0) &&
msc_struct.bot_status == MSC_BOT_STATE_IDLE)
{
usbd_set_stall(pudev, USBD_MSC_BULK_OUT_EPT);
}
usbd_set_stall(pudev, USBD_MSC_BULK_IN_EPT);
if(msc_struct.bot_status == MSC_BOT_STATE_ERROR)
{
usbd_ept_recv(pudev, USBD_MSC_BULK_OUT_EPT,
(uint8_t *)&cbw_struct, CBW_CMD_LENGTH);
}
}
/**
* @brief bulk-only transport scsi command test unit
* @param udev: to the structure of usbd_core_type
* @param lun: logical units number
* @retval status of usb_sts_type
*/
usb_sts_type bot_scsi_test_unit(void *udev, uint8_t lun)
{
usb_sts_type status = USB_OK;
if(cbw_struct.dCBWDataTransferLength != 0)
{
bot_scsi_sense_code(udev, SENSE_KEY_ILLEGAL_REQUEST, INVALID_COMMAND);
return USB_FAIL;
}
msc_struct.data_len = 0;
return status;
}
/**
* @brief bulk-only transport scsi command inquiry
* @param udev: to the structure of usbd_core_type
* @param lun: logical units number
* @retval status of usb_sts_type
*/
usb_sts_type bot_scsi_inquiry(void *udev, uint8_t lun)
{
uint8_t *pdata;
uint32_t trans_len = 0;
usb_sts_type status = USB_OK;
if(cbw_struct.CBWCB[1] & 0x01)
{
pdata = page00_inquiry_data;
trans_len = 5;
}
else
{
pdata = get_inquiry(lun);
if(cbw_struct.dCBWDataTransferLength < SCSI_INQUIRY_DATA_LENGTH)
{
trans_len = cbw_struct.dCBWDataTransferLength;
}
else
{
trans_len = SCSI_INQUIRY_DATA_LENGTH;
}
}
msc_struct.data_len = trans_len;
while(trans_len)
{
trans_len --;
msc_struct.data[trans_len] = pdata[trans_len];
}
return status;
}
/**
* @brief bulk-only transport scsi command start stop
* @param udev: to the structure of usbd_core_type
* @param lun: logical units number
* @retval status of usb_sts_type
*/
usb_sts_type bot_scsi_start_stop(void *udev, uint8_t lun)
{
msc_struct.data_len = 0;
return USB_OK;
}
/**
* @brief bulk-only transport scsi command meidum removal
* @param udev: to the structure of usbd_core_type
* @param lun: logical units number
* @retval status of usb_sts_type
*/
usb_sts_type bot_scsi_allow_medium_removal(void *udev, uint8_t lun)
{
msc_struct.data_len = 0;
return USB_OK;
}
/**
* @brief bulk-only transport scsi command mode sense6
* @param udev: to the structure of usbd_core_type
* @param lun: logical units number
* @retval status of usb_sts_type
*/
usb_sts_type bot_scsi_mode_sense6(void *udev, uint8_t lun)
{
uint8_t data_len = 8;
msc_struct.data_len = 8;
while(data_len)
{
data_len --;
msc_struct.data[data_len] = mode_sense6_data[data_len];
};
return USB_OK;
}
/**
* @brief bulk-only transport scsi command mode sense10
* @param udev: to the structure of usbd_core_type
* @param lun: logical units number
* @retval status of usb_sts_type
*/
usb_sts_type bot_scsi_mode_sense10(void *udev, uint8_t lun)
{
uint8_t data_len = 8;
msc_struct.data_len = 8;
while(data_len)
{
data_len --;
msc_struct.data[data_len] = mode_sense10_data[data_len];
};
return USB_OK;
}
/**
* @brief bulk-only transport scsi command capacity
* @param udev: to the structure of usbd_core_type
* @param lun: logical units number
* @retval status of usb_sts_type
*/
usb_sts_type bot_scsi_capacity(void *udev, uint8_t lun)
{
uint8_t *pdata = msc_struct.data;
msc_disk_capacity(lun, &msc_struct.blk_nbr[lun], &msc_struct.blk_size[lun]);
pdata[0] = (uint8_t)((msc_struct.blk_nbr[lun] - 1) >> 24);
pdata[1] = (uint8_t)((msc_struct.blk_nbr[lun] - 1) >> 16);
pdata[2] = (uint8_t)((msc_struct.blk_nbr[lun] - 1) >> 8);
pdata[3] = (uint8_t)((msc_struct.blk_nbr[lun] - 1));
pdata[4] = (uint8_t)((msc_struct.blk_size[lun]) >> 24);
pdata[5] = (uint8_t)((msc_struct.blk_size[lun]) >> 16);
pdata[6] = (uint8_t)((msc_struct.blk_size[lun]) >> 8);
pdata[7] = (uint8_t)((msc_struct.blk_size[lun]));
msc_struct.data_len = 8;
return USB_OK;
}
/**
* @brief bulk-only transport scsi command format capacity
* @param udev: to the structure of usbd_core_type
* @param lun: logical units number
* @retval status of usb_sts_type
*/
usb_sts_type bot_scsi_format_capacity(void *udev, uint8_t lun)
{
uint8_t *pdata = msc_struct.data;
pdata[0] = 0;
pdata[1] = 0;
pdata[2] = 0;
pdata[3] = 0x08;
msc_disk_capacity(lun, &msc_struct.blk_nbr[lun], &msc_struct.blk_size[lun]);
pdata[4] = (uint8_t)((msc_struct.blk_nbr[lun] - 1) >> 24);
pdata[5] = (uint8_t)((msc_struct.blk_nbr[lun] - 1) >> 16);
pdata[6] = (uint8_t)((msc_struct.blk_nbr[lun] - 1) >> 8);
pdata[7] = (uint8_t)((msc_struct.blk_nbr[lun] - 1));
pdata[8] = 0x02;
pdata[9] = (uint8_t)((msc_struct.blk_size[lun]) >> 16);
pdata[10] = (uint8_t)((msc_struct.blk_size[lun]) >> 8);
pdata[11] = (uint8_t)((msc_struct.blk_size[lun]));
msc_struct.data_len = 12;
return USB_OK;
}
/**
* @brief bulk-only transport scsi command request sense
* @param udev: to the structure of usbd_core_type
* @param lun: logical units number
* @retval status of usb_sts_type
*/
usb_sts_type bot_scsi_request_sense(void *udev, uint8_t lun)
{
uint32_t trans_len = 0x12;
uint8_t *pdata = msc_struct.data;
uint8_t *sdata = (uint8_t *)&sense_data;
while(trans_len)
{
trans_len --;
pdata[trans_len] = sdata[trans_len];
}
if(cbw_struct.dCBWDataTransferLength < REQ_SENSE_STANDARD_DATA_LEN)
{
msc_struct.data_len = cbw_struct.dCBWDataTransferLength;
}
else
{
msc_struct.data_len = REQ_SENSE_STANDARD_DATA_LEN;
}
return USB_OK;
}
/**
* @brief bulk-only transport scsi command verify
* @param udev: to the structure of usbd_core_type
* @param lun: logical units number
* @retval status of usb_sts_type
*/
usb_sts_type bot_scsi_verify(void *udev, uint8_t lun)
{
uint8_t *cmd = cbw_struct.CBWCB;
if((cbw_struct.CBWCB[1] & 0x02) == 0x02)
{
bot_scsi_sense_code(udev, SENSE_KEY_ILLEGAL_REQUEST, INVALID_FIELED_IN_COMMAND);
return USB_FAIL;
}
msc_struct.blk_addr = cmd[2] << 24 | cmd[3] << 16 | cmd[4] << 8 | cmd[5];
msc_struct.blk_len = cmd[7] << 8 | cmd[8];
if(bot_scsi_check_address(udev, lun, msc_struct.blk_addr, msc_struct.blk_len) != USB_OK)
{
return USB_FAIL;
}
msc_struct.data_len = 0;
return USB_OK;
}
/**
* @brief bulk-only transport scsi command read10
* @param udev: to the structure of usbd_core_type
* @param lun: logical units number
* @retval status of usb_sts_type
*/
usb_sts_type bot_scsi_read10(void *udev, uint8_t lun)
{
uint8_t *cmd = cbw_struct.CBWCB;
usbd_core_type *pudev = (usbd_core_type *)udev;
uint32_t len;
if(msc_struct.msc_state == MSC_STATE_MACHINE_IDLE)
{
if((cbw_struct.bmCBWFlags & 0x80) != 0x80)
{
bot_scsi_sense_code(udev, SENSE_KEY_ILLEGAL_REQUEST, INVALID_COMMAND);
return USB_FAIL;
}
msc_struct.blk_addr = cmd[2] << 24 | cmd[3] << 16 | cmd[4] << 8 | cmd[5];
msc_struct.blk_len = cmd[7] << 8 | cmd[8];
if(bot_scsi_check_address(udev, lun, msc_struct.blk_addr, msc_struct.blk_len) != USB_OK)
{
return USB_FAIL;
}
msc_struct.blk_addr *= msc_struct.blk_size[lun];
msc_struct.blk_len *= msc_struct.blk_size[lun];
if(cbw_struct.dCBWDataTransferLength != msc_struct.blk_len)
{
bot_scsi_sense_code(udev, SENSE_KEY_ILLEGAL_REQUEST, INVALID_COMMAND);
return USB_FAIL;
}
msc_struct.msc_state = MSC_STATE_MACHINE_DATA_IN;
}
msc_struct.data_len = MSC_MAX_DATA_BUF_LEN;
len = MIN(msc_struct.blk_len, MSC_MAX_DATA_BUF_LEN);
if( msc_disk_read(lun, msc_struct.blk_addr, msc_struct.data, len) != USB_OK)
{
bot_scsi_sense_code(udev, SENSE_KEY_HARDWARE_ERROR, MEDIUM_NOT_PRESENT);
return USB_FAIL;
}
usbd_ept_send(pudev, USBD_MSC_BULK_IN_EPT, msc_struct.data, len);
msc_struct.blk_addr += len;
msc_struct.blk_len -= len;
csw_struct.dCSWDataResidue -= len;
if(msc_struct.blk_len == 0)
{
msc_struct.msc_state = MSC_STATE_MACHINE_LAST_DATA;
}
return USB_OK;
}
/**
* @brief bulk-only transport scsi command write10
* @param udev: to the structure of usbd_core_type
* @param lun: logical units number
* @retval status of usb_sts_type
*/
usb_sts_type bot_scsi_write10(void *udev, uint8_t lun)
{
uint8_t *cmd = cbw_struct.CBWCB;
usbd_core_type *pudev = (usbd_core_type *)udev;
uint32_t len;
if(msc_struct.msc_state == MSC_STATE_MACHINE_IDLE)
{
if((cbw_struct.bmCBWFlags & 0x80) == 0x80)
{
bot_scsi_sense_code(udev, SENSE_KEY_ILLEGAL_REQUEST, INVALID_COMMAND);
return USB_FAIL;
}
msc_struct.blk_addr = cmd[2] << 24 | cmd[3] << 16 | cmd[4] << 8 | cmd[5];
msc_struct.blk_len = cmd[7] << 8 | cmd[8];
if(bot_scsi_check_address(udev, lun, msc_struct.blk_addr, msc_struct.blk_len) != USB_OK)
{
return USB_FAIL;
}
msc_struct.blk_addr *= msc_struct.blk_size[lun];
msc_struct.blk_len *= msc_struct.blk_size[lun];
if(cbw_struct.dCBWDataTransferLength != msc_struct.blk_len)
{
bot_scsi_sense_code(udev, SENSE_KEY_ILLEGAL_REQUEST, INVALID_COMMAND);
return USB_FAIL;
}
msc_struct.msc_state = MSC_STATE_MACHINE_DATA_OUT;
len = MIN(msc_struct.blk_len, MSC_MAX_DATA_BUF_LEN);
usbd_ept_recv(pudev, USBD_MSC_BULK_OUT_EPT, (uint8_t *)msc_struct.data, len);
}
else
{
len = MIN(msc_struct.blk_len, MSC_MAX_DATA_BUF_LEN);
if(msc_disk_write(lun, msc_struct.blk_addr, msc_struct.data, len) != USB_OK)
{
bot_scsi_sense_code(udev, SENSE_KEY_HARDWARE_ERROR, MEDIUM_NOT_PRESENT);
return USB_FAIL;
}
msc_struct.blk_addr += len;
msc_struct.blk_len -= len;
csw_struct.dCSWDataResidue -= len;
if(msc_struct.blk_len == 0)
{
bot_scsi_send_csw(udev, CSW_BCSWSTATUS_PASS);
}
else
{
len = MIN(msc_struct.blk_len, MSC_MAX_DATA_BUF_LEN);
usbd_ept_recv(pudev, USBD_MSC_BULK_OUT_EPT, (uint8_t *)msc_struct.data, len);
}
}
return USB_OK;
}
/**
* @brief clear feature
* @param udev: to the structure of usbd_core_type
* @param etp_num: endpoint number
* @retval status of usb_sts_type
*/
void bot_scsi_clear_feature(void *udev, uint8_t ept_num)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
if(msc_struct.bot_status == MSC_BOT_STATE_ERROR)
{
usbd_set_stall(pudev, USBD_MSC_BULK_IN_EPT);
msc_struct.bot_status = MSC_BOT_STATE_IDLE;
}
else if(((ept_num & 0x80) == 0x80) && (msc_struct.bot_status != MSC_BOT_STATE_RECOVERY))
{
bot_scsi_send_csw(udev, CSW_BCSWSTATUS_FAILED);
}
}
/**
* @brief bulk-only transport scsi command process
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type bot_scsi_cmd_process(void *udev)
{
usb_sts_type status = USB_FAIL;
switch(cbw_struct.CBWCB[0])
{
case MSC_CMD_INQUIRY:
status = bot_scsi_inquiry(udev, cbw_struct.bCBWLUN);
break;
case MSC_CMD_START_STOP:
status = bot_scsi_start_stop(udev, cbw_struct.bCBWLUN);
break;
case MSC_CMD_MODE_SENSE6:
status = bot_scsi_mode_sense6(udev, cbw_struct.bCBWLUN);
break;
case MSC_CMD_MODE_SENSE10:
status = bot_scsi_mode_sense10(udev, cbw_struct.bCBWLUN);
break;
case MSC_CMD_ALLOW_MEDIUM_REMOVAL:
status = bot_scsi_allow_medium_removal(udev, cbw_struct.bCBWLUN);
break;
case MSC_CMD_READ_10:
status = bot_scsi_read10(udev, cbw_struct.bCBWLUN);
break;
case MSC_CMD_READ_CAPACITY:
status = bot_scsi_capacity(udev, cbw_struct.bCBWLUN);
break;
case MSC_CMD_REQUEST_SENSE:
status = bot_scsi_request_sense(udev, cbw_struct.bCBWLUN);
break;
case MSC_CMD_TEST_UNIT:
status = bot_scsi_test_unit(udev, cbw_struct.bCBWLUN);
break;
case MSC_CMD_VERIFY:
status = bot_scsi_verify(udev, cbw_struct.bCBWLUN);
break;
case MSC_CMD_WRITE_10:
status = bot_scsi_write10(udev, cbw_struct.bCBWLUN);
break;
case MSC_CMD_READ_FORMAT_CAPACITY:
status = bot_scsi_format_capacity(udev, cbw_struct.bCBWLUN);
break;
default:
bot_scsi_sense_code(udev, SENSE_KEY_ILLEGAL_REQUEST, INVALID_COMMAND);
status = USB_FAIL;
break;
}
return status;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/

View File

@@ -0,0 +1,250 @@
/**
**************************************************************************
* @file msc_bot_scsi.h
* @version v2.0.0
* @date 2021-11-26
* @brief usb mass storage bulk-only transport and scsi command header file
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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.
*
**************************************************************************
*/
/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __MSC_BOT_SCSI_H
#define __MSC_BOT_SCSI_H
#ifdef __cplusplus
extern "C" {
#endif
#include "msc_class.h"
#include "usbd_core.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @addtogroup USB_msc_bot_scsi
* @{
*/
/** @defgroup USB_msc_bot_scsi_definition
* @{
*/
#define MSC_SUPPORT_MAX_LUN 1
#define MSC_MAX_DATA_BUF_LEN 4096
#define MSC_CMD_FORMAT_UNIT 0x04
#define MSC_CMD_INQUIRY 0x12
#define MSC_CMD_START_STOP 0x1B
#define MSC_CMD_MODE_SENSE6 0x1A
#define MSC_CMD_MODE_SENSE10 0x5A
#define MSC_CMD_ALLOW_MEDIUM_REMOVAL 0x1E
#define MSC_CMD_READ_10 0x28
#define MSC_CMD_READ_12 0xA8
#define MSC_CMD_READ_CAPACITY 0x25
#define MSC_CMD_READ_FORMAT_CAPACITY 0x23
#define MSC_CMD_REQUEST_SENSE 0x03
#define MSC_CMD_TEST_UNIT 0x00
#define MSC_CMD_VERIFY 0x2F
#define MSC_CMD_WRITE_10 0x2A
#define MSC_CMD_WRITE_12 0xAA
#define MSC_CMD_WRITE_VERIFY 0x2E
#define MSC_REQ_GET_MAX_LUN 0xFE /*!< get max lun */
#define MSC_REQ_BO_RESET 0xFF /*!< bulk only mass storage reset */
#define SET_LINE_CODING 0x20
#define GET_LINE_CODING 0x21
#define CBW_CMD_LENGTH 31
#define CBW_DCBWSIGNATURE 0x43425355
#define CBW_BMCBWFLAGS_DIR_OUT 0x00
#define CBW_BMCBWFLAGS_DIR_IN 0x80
#define CSW_CMD_LENGTH 13
#define CSW_DCSWSIGNATURE 0x53425355
#define CSW_BCSWSTATUS_PASS 0x00
#define CSW_BCSWSTATUS_FAILED 0x01
#define CSW_BCSWSTATUS_PHASE_ERR 0x02
#define MSC_STATE_MACHINE_CMD 0x00
#define MSC_STATE_MACHINE_DATA_IN 0x01
#define MSC_STATE_MACHINE_DATA_OUT 0x02
#define MSC_STATE_MACHINE_SEND_DATA 0x03
#define MSC_STATE_MACHINE_LAST_DATA 0x04
#define MSC_STATE_MACHINE_STATUS 0x05
#define MSC_STATE_MACHINE_FAILED 0x06
#define MSC_STATE_MACHINE_IDLE 0x07
#define MSC_BOT_STATE_IDLE 0x00
#define MSC_BOT_STATE_RECOVERY 0x01
#define MSC_BOT_STATE_ERROR 0x02
#define REQ_SENSE_STANDARD_DATA_LEN 0x12
#define SENSE_KEY_NO_SENSE 0x00
#define SENSE_KEY_RECOVERED_ERROR 0x01
#define SENSE_KEY_NOT_READY 0x02
#define SENSE_KEY_MEDIUM_ERROR 0x03
#define SENSE_KEY_HARDWARE_ERROR 0x04
#define SENSE_KEY_ILLEGAL_REQUEST 0x05
#define SENSE_KEY_UNIT_ATTENTION 0x06
#define SENSE_KEY_DATA_PROTECT 0x07
#define SENSE_KEY_BLANK_CHECK 0x08
#define SENSE_KEY_VENDERO_SPECIFIC 0x09
#define SENSE_KEY_ABORTED_COMMAND 0x0B
#define SENSE_KEY_VOLUME_OVERFLOW 0x0D
#define SENSE_KEY_MISCOMPARE 0x0E
#define INVALID_COMMAND 0x20
#define INVALID_FIELED_IN_COMMAND 0x24
#define PARAMETER_LIST_LENGTH_ERROR 0x1A
#define INVALID_FIELD_IN_PARAMETER_LIST 0x26
#define ADDRESS_OUT_OF_RANGE 0x21
#define MEDIUM_NOT_PRESENT 0x3A
#define MEDIUM_HAVE_CHANGED 0x28
#define SCSI_INQUIRY_DATA_LENGTH 36
/**
* @brief typical command block description
*/
typedef struct
{
uint8_t opcode;
uint8_t lun;
uint32_t address;
uint8_t reserved1;
uint32_t alloc_length;
uint16_t reserved2;
}cbd_typical_type;
/**
* @brief extended command block description
*/
typedef struct
{
uint8_t opcode;
uint8_t lun;
uint32_t address;
uint8_t reserved1;
uint32_t alloc_length;
uint16_t reserved2;
}cbd_extended_type;
/**
* @brief command block wrapper
*/
typedef struct
{
uint32_t dCBWSignature;
uint32_t dCBWTage;
uint32_t dCBWDataTransferLength;
uint8_t bmCBWFlags;
uint8_t bCBWLUN;
uint8_t bCBWCBLength;
uint8_t CBWCB[16];
}cbw_type;
/**
* @brief command block wrapper
*/
typedef struct
{
uint32_t dCSWSignature;
uint32_t dCSWTag;
uint32_t dCSWDataResidue;
uint32_t bCSWStatus;
}csw_type;
/**
* @brief request sense standard data
*/
typedef struct
{
uint8_t err_code;
uint8_t reserved1;
uint8_t sense_key;
uint32_t information;
uint8_t as_length;
uint32_t reserved2;
uint8_t asc;
uint8_t ascq;
uint32_t reserved3;
}sense_type;
typedef struct
{
uint8_t msc_state;
uint8_t bot_status;
uint8_t max_lun;
uint32_t blk_nbr[MSC_SUPPORT_MAX_LUN];
uint32_t blk_size[MSC_SUPPORT_MAX_LUN];
uint32_t blk_addr;
uint32_t blk_len;
uint32_t data_len;
uint8_t data[MSC_MAX_DATA_BUF_LEN];
}msc_type;
void bot_scsi_init(void *udev);
void bot_scsi_reset(void *udev);
void bot_scsi_datain_handler(void *pudev, uint8_t ept_num);
void bot_scsi_dataout_handler(void *pudev, uint8_t ept_num);
void bot_cbw_decode(void *udev);
void bot_scsi_send_data(void *udev, uint8_t *buffer, uint32_t len);
void bot_scsi_send_csw(void *udev, uint8_t status);
void bot_scsi_sense_code(void *udev, uint8_t sense_key, uint8_t asc);
usb_sts_type bot_scsi_check_address(void *udev, uint8_t lun, uint32_t blk_offset, uint32_t blk_count);
void bot_scsi_stall(void *udev);
usb_sts_type bot_scsi_cmd_process(void *udev);
usb_sts_type bot_scsi_test_unit(void *udev, uint8_t lun);
usb_sts_type bot_scsi_inquiry(void *udev, uint8_t lun);
usb_sts_type bot_scsi_start_stop(void *udev, uint8_t lun);
usb_sts_type bot_scsi_allow_medium_removal(void *udev, uint8_t lun);
usb_sts_type bot_scsi_mode_sense6(void *udev, uint8_t lun);
usb_sts_type bot_scsi_mode_sense10(void *udev, uint8_t lun);
usb_sts_type bot_scsi_read10(void *udev, uint8_t lun);
usb_sts_type bot_scsi_capacity(void *udev, uint8_t lun);
usb_sts_type bot_scsi_format_capacity(void *udev, uint8_t lun);
usb_sts_type bot_scsi_request_sense(void *udev, uint8_t lun);
usb_sts_type bot_scsi_verify(void *udev, uint8_t lun);
usb_sts_type bot_scsi_write10(void *udev, uint8_t lun);
void bot_scsi_clear_feature(void *udev, uint8_t ept_num);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,302 @@
/**
**************************************************************************
* @file msc_class.c
* @version v2.0.0
* @date 2021-11-26
* @brief usb msc class type
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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 "usbd_core.h"
#include "msc_class.h"
#include "msc_desc.h"
#include "msc_bot_scsi.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @defgroup USB_msc_class
* @brief usb device class msc demo
* @{
*/
/** @defgroup USB_msc_class_private_functions
* @{
*/
usb_sts_type class_init_handler(void *udev);
usb_sts_type class_clear_handler(void *udev);
usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup);
usb_sts_type class_ept0_tx_handler(void *udev);
usb_sts_type class_ept0_rx_handler(void *udev);
usb_sts_type class_in_handler(void *udev, uint8_t ept_num);
usb_sts_type class_out_handler(void *udev, uint8_t ept_num);
usb_sts_type class_sof_handler(void *udev);
usb_sts_type class_event_handler(void *udev, usbd_event_type event);
/* usb rx and tx buffer */
static uint32_t alt_setting = 0;
extern msc_type msc_struct;
extern cbw_type cbw_struct;
extern csw_type csw_struct;
/* usb device class handler */
usbd_class_handler msc_class_handler =
{
class_init_handler,
class_clear_handler,
class_setup_handler,
class_ept0_tx_handler,
class_ept0_rx_handler,
class_in_handler,
class_out_handler,
class_sof_handler,
class_event_handler,
};
/**
* @brief initialize usb endpoint
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_init_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
/* open in endpoint */
usbd_ept_open(pudev, USBD_MSC_BULK_IN_EPT, EPT_BULK_TYPE, USBD_OUT_MAXPACKET_SIZE);
/* open out endpoint */
usbd_ept_open(pudev, USBD_MSC_BULK_OUT_EPT, EPT_BULK_TYPE, USBD_OUT_MAXPACKET_SIZE);
bot_scsi_init(udev);
return status;
}
/**
* @brief clear endpoint or other state
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_clear_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
/* close in endpoint */
usbd_ept_close(pudev, USBD_MSC_BULK_IN_EPT);
/* close out endpoint */
usbd_ept_close(pudev, USBD_MSC_BULK_OUT_EPT);
return status;
}
/**
* @brief usb device class setup request handler
* @param udev: to the structure of usbd_core_type
* @param setup: setup packet
* @retval status of usb_sts_type
*/
usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
switch(setup->bmRequestType & USB_REQ_TYPE_RESERVED)
{
/* class request */
case USB_REQ_TYPE_CLASS:
switch(setup->bRequest)
{
case MSC_REQ_GET_MAX_LUN:
usbd_ctrl_send(pudev, &msc_struct.max_lun, 1);
break;
case MSC_REQ_BO_RESET:
bot_scsi_reset(udev);
usbd_ctrl_send_status(pudev);
break;
default:
usbd_ctrl_unsupport(pudev);
break;
}
break;
/* standard request */
case USB_REQ_TYPE_STANDARD:
switch(setup->bRequest)
{
case USB_STD_REQ_GET_DESCRIPTOR:
usbd_ctrl_unsupport(pudev);
break;
case USB_STD_REQ_GET_INTERFACE:
usbd_ctrl_send(pudev, (uint8_t *)&alt_setting, 1);
break;
case USB_STD_REQ_SET_INTERFACE:
alt_setting = setup->wValue;
break;
case USB_STD_REQ_CLEAR_FEATURE:
usbd_ept_close(pudev, (uint8_t)setup->wIndex);
if((setup->wIndex & 0x80) == 0x80)
{
usb_flush_tx_fifo(pudev->usb_reg, setup->wIndex & 0x7F);
usbd_ept_open(pudev, (uint8_t)setup->wIndex, EPT_BULK_TYPE, USBD_IN_MAXPACKET_SIZE);
}
else
{
usbd_ept_open(pudev, (uint8_t)setup->wIndex, EPT_BULK_TYPE, USBD_OUT_MAXPACKET_SIZE);
}
bot_scsi_clear_feature(udev, setup->wIndex);
break;
default:
break;
}
break;
default:
usbd_ctrl_unsupport(pudev);
break;
}
return status;
}
/**
* @brief usb device endpoint 0 in status stage complete
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_ept0_tx_handler(void *udev)
{
usb_sts_type status = USB_OK;
/* ...user code... */
return status;
}
/**
* @brief usb device endpoint 0 out status stage complete
* @param udev: usb device core handler type
* @retval status of usb_sts_type
*/
usb_sts_type class_ept0_rx_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
uint32_t recv_len = usbd_get_recv_len(pudev, 0);
/* ...user code... */
return status;
}
/**
* @brief usb device transmision complete handler
* @param udev: to the structure of usbd_core_type
* @param ept_num: endpoint number
* @retval status of usb_sts_type
*/
usb_sts_type class_in_handler(void *udev, uint8_t ept_num)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
usb_flush_tx_fifo(pudev->usb_reg, ept_num&0x7F);
// if(msc_struct.msc_state != MSC_STATE_MACHINE_IDLE)
// {
// bot_scsi_datain_handler(udev, ept_num);
// }
bot_scsi_datain_handler(udev, ept_num);
return status;
}
/**
* @brief usb device endpoint receive data
* @param udev: to the structure of usbd_core_type
* @param ept_num: endpoint number
* @retval status of usb_sts_type
*/
usb_sts_type class_out_handler(void *udev, uint8_t ept_num)
{
usb_sts_type status = USB_OK;
// if(msc_struct.msc_state == MSC_STATE_MACHINE_IDLE)
// msc_struct.msc_state = MSC_STATE_MACHINE_CMD;
bot_scsi_dataout_handler(udev, ept_num);
return status;
}
/**
* @brief usb device sof handler
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_sof_handler(void *udev)
{
usb_sts_type status = USB_OK;
/* ...user code... */
return status;
}
/**
* @brief usb device event handler
* @param udev: to the structure of usbd_core_type
* @param event: usb device event
* @retval status of usb_sts_type
*/
usb_sts_type class_event_handler(void *udev, usbd_event_type event)
{
usb_sts_type status = USB_OK;
switch(event)
{
case USBD_RESET_EVENT:
/* ...user code... */
break;
case USBD_SUSPEND_EVENT:
/* ...user code... */
break;
case USBD_WAKEUP_EVENT:
/* ...user code... */
break;
default:
break;
}
return status;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/

View File

@@ -0,0 +1,72 @@
/**
**************************************************************************
* @file msc_class.h
* @version v2.0.0
* @date 2021-11-26
* @brief usb msc class file
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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.
*
**************************************************************************
*/
/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __MSC_CLASS_H
#define __MSC_CLASS_H
#ifdef __cplusplus
extern "C" {
#endif
#include "usb_std.h"
#include "usbd_core.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @addtogroup USB_msc_class
* @{
*/
/** @defgroup USB_msc_class_definition
* @{
*/
#define USBD_MSC_BULK_IN_EPT 0x81
#define USBD_MSC_BULK_OUT_EPT 0x01
#define USBD_IN_MAXPACKET_SIZE 0x40
#define USBD_OUT_MAXPACKET_SIZE 0x40
extern usbd_class_handler msc_class_handler;
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,392 @@
/**
**************************************************************************
* @file msc_desc.c
* @version v2.0.0
* @date 2021-11-26
* @brief usb msc device descriptor
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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 "stdio.h"
#include "usb_std.h"
#include "usbd_sdr.h"
#include "usbd_core.h"
#include "msc_desc.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @defgroup USB_msc_desc
* @brief usb device msc descriptor
* @{
*/
/** @defgroup USB_msc_desc_private_functions
* @{
*/
usbd_desc_t *get_device_descriptor(void);
usbd_desc_t *get_device_qualifier(void);
usbd_desc_t *get_device_configuration(void);
usbd_desc_t *get_device_other_speed(void);
usbd_desc_t *get_device_lang_id(void);
usbd_desc_t *get_device_manufacturer_string(void);
usbd_desc_t *get_device_product_string(void);
usbd_desc_t *get_device_serial_string(void);
usbd_desc_t *get_device_interface_string(void);
usbd_desc_t *get_device_config_string(void);
uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf);
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 device descriptor handler structure
*/
usbd_desc_handler msc_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
*/
uint8_t g_usbd_descriptor[USB_DEVICE_DESC_LEN] =
{
USB_DEVICE_DESC_LEN, /* bLength */
USB_DESCIPTOR_TYPE_DEVICE, /* bDescriptorType */
0x00, /* bcdUSB */
0x02,
0x00, /* bDeviceClass */
0x00, /* bDeviceSubClass */
0x00, /* bDeviceProtocol */
USB_MAX_EP0_SIZE, /* bMaxPacketSize */
LBYTE(USBD_VENDOR_ID), /* idVendor */
HBYTE(USBD_VENDOR_ID), /* idVendor */
LBYTE(USBD_PRODUCT_ID), /* idProduct */
HBYTE(USBD_PRODUCT_ID), /* idProduct */
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
*/
uint8_t g_usbd_configuration[USBD_CONFIG_DESC_SIZE] =
{
USB_DEVICE_CFG_DESC_LEN, /* bLength: configuration descriptor size */
USB_DESCIPTOR_TYPE_CONFIGURATION, /* bDescriptorType: configuration */
LBYTE(USBD_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
HBYTE(USBD_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
0x01, /* bNumInterfaces: 2 interface */
0x01, /* bConfigurationValue: configuration value */
0x04, /* iConfiguration: index of string descriptor describing
the configuration */
0xC0, /* bmAttributes: self powered */
0x32, /* MaxPower 100 mA: this current is used for detecting vbus */
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 */
0x02, /* bNumEndpoints: number of endpoints */
USB_CLASS_CODE_MSC, /* bInterfaceClass: msc class code */
0x06, /* bInterfaceSubClass: subclass code scsi */
0x50, /* bInterfaceProtocol: protocol code BBB */
0x05, /* iInterface: index of string descriptor */
USB_DEVICE_EPT_LEN, /* bLength: size of endpoint descriptor in bytes */
USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
USBD_MSC_BULK_IN_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
USB_EPT_DESC_BULK, /* bmAttributes: endpoint attributes */
LBYTE(USBD_IN_MAXPACKET_SIZE),
HBYTE(USBD_IN_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
0x00, /* bInterval: interval for polling endpoint for data transfers */
USB_DEVICE_EPT_LEN, /* bLength: size of endpoint descriptor in bytes */
USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
USBD_MSC_BULK_OUT_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
USB_EPT_DESC_BULK, /* bmAttributes: endpoint attributes */
LBYTE(USBD_OUT_MAXPACKET_SIZE),
HBYTE(USBD_OUT_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
0x00, /* bInterval: interval for polling endpoint for data transfers */
};
/**
* @brief usb string lang id
*/
uint8_t g_string_lang_id[USBD_SIZ_STRING_LANGID] =
{
USBD_SIZ_STRING_LANGID,
USB_DESCIPTOR_TYPE_STRING,
0x09,
0x04,
};
/**
* @brief usb string serial
*/
uint8_t g_string_serial[USBD_SIZ_STRING_SERIAL] =
{
USBD_SIZ_STRING_SERIAL,
USB_DESCIPTOR_TYPE_STRING,
};
/* device descriptor */
usbd_desc_t device_descriptor =
{
USB_DEVICE_DESC_LEN,
g_usbd_descriptor
};
/* config descriptor */
usbd_desc_t config_descriptor =
{
USBD_CONFIG_DESC_SIZE,
g_usbd_configuration
};
/* langid descriptor */
usbd_desc_t langid_descriptor =
{
USBD_SIZ_STRING_LANGID,
g_string_lang_id
};
/* serial descriptor */
usbd_desc_t serial_descriptor =
{
USBD_SIZ_STRING_SERIAL,
g_string_serial
};
usbd_desc_t vp_desc;
/**
* @brief standard usb unicode convert
* @param string: source string
* @param unicode_buf: unicode buffer
* @retval length
*/
uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf)
{
uint16_t str_len = 0, id_pos = 2;
uint8_t *tmp_str = string;
while(*tmp_str != '\0')
{
str_len ++;
unicode_buf[id_pos ++] = *tmp_str ++;
unicode_buf[id_pos ++] = 0x00;
}
str_len = str_len * 2 + 2;
unicode_buf[0] = (uint8_t)str_len;
unicode_buf[1] = USB_DESCIPTOR_TYPE_STRING;
return str_len;
}
/**
* @brief usb int convert to unicode
* @param value: int value
* @param pbus: unicode buffer
* @param len: length
* @retval none
*/
static void usbd_int_to_unicode (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;
}
}
/**
* @brief usb get serial number
* @param none
* @retval none
*/
static void get_serial_num(void)
{
uint32_t serial0, serial1, serial2;
serial0 = *(uint32_t*)MCU_ID1;
serial1 = *(uint32_t*)MCU_ID2;
serial2 = *(uint32_t*)MCU_ID3;
serial0 += serial2;
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
* @retval usbd_desc
*/
usbd_desc_t *get_device_descriptor(void)
{
return &device_descriptor;
}
/**
* @brief get device qualifier
* @param none
* @retval usbd_desc
*/
usbd_desc_t * get_device_qualifier(void)
{
return NULL;
}
/**
* @brief get config descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_configuration(void)
{
return &config_descriptor;
}
/**
* @brief get other speed descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_other_speed(void)
{
return NULL;
}
/**
* @brief get lang id descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_lang_id(void)
{
return &langid_descriptor;
}
/**
* @brief get manufacturer descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_manufacturer_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_MANUFACTURER_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get product descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_product_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_PRODUCT_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get serial descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_serial_string(void)
{
get_serial_num();
return &serial_descriptor;
}
/**
* @brief get interface descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_interface_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_INTERFACE_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get device config descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_config_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_CONFIGURATION_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/

View File

@@ -0,0 +1,98 @@
/**
**************************************************************************
* @file msc_desc.h
* @version v2.0.0
* @date 2021-11-26
* @brief usb msc descriptor header file
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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.
*
**************************************************************************
*/
/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __MSC_DESC_H
#define __MSC_DESC_H
#ifdef __cplusplus
extern "C" {
#endif
#include "msc_class.h"
#include "usbd_core.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @addtogroup USB_msc_desc
* @{
*/
/** @defgroup USB_msc_desc_definition
* @{
*/
#define BCD_NUM 0x0110
#define USBD_VENDOR_ID 0x2E3C
#define USBD_PRODUCT_ID 0x5720
#define USBD_CONFIG_DESC_SIZE 32
#define USBD_SIZ_STRING_LANGID 4
#define USBD_SIZ_STRING_SERIAL 0x1A
#define USBD_DESC_MANUFACTURER_STRING "Artery"
#define USBD_DESC_PRODUCT_STRING "AT32 Mass Storage"
#define USBD_DESC_CONFIGURATION_STRING "Mass Storage Config"
#define USBD_DESC_INTERFACE_STRING "Mass Storage Interface"
#define HID_BINTERVAL_TIME 0xFF
#define USBD_CDC_CS_INTERFACE 0x24
#define USBD_CDC_CS_ENDPOINT 0x25
#define USBD_CDC_SUBTYPE_HEADER 0x00
#define USBD_CDC_SUBTYPE_CMF 0x01
#define USBD_CDC_SUBTYPE_ACM 0x02
#define USBD_CDC_SUBTYPE_UFD 0x06
#define MCU_ID1 (0x1FFFF7E8)
#define MCU_ID2 (0x1FFFF7EC)
#define MCU_ID3 (0x1FFFF7F0)
extern uint8_t g_usbd_descriptor[USB_DEVICE_DESC_LEN];
extern uint8_t g_usbd_configuration[USBD_CONFIG_DESC_SIZE];
extern usbd_desc_handler msc_desc_handler;
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,334 @@
/**
**************************************************************************
* @file printer_class.c
* @version v2.0.0
* @date 2021-11-26
* @brief usb printer class type
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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 "usbd_core.h"
#include "printer_class.h"
#include "printer_desc.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @defgroup USB_printer_class
* @brief usb device class printer demo
* @{
*/
/** @defgroup USB_printer_class_private_functions
* @{
*/
usb_sts_type class_init_handler(void *udev);
usb_sts_type class_clear_handler(void *udev);
usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup);
usb_sts_type class_ept0_tx_handler(void *udev);
usb_sts_type class_ept0_rx_handler(void *udev);
usb_sts_type class_in_handler(void *udev, uint8_t ept_num);
usb_sts_type class_out_handler(void *udev, uint8_t ept_num);
usb_sts_type class_sof_handler(void *udev);
usb_sts_type class_event_handler(void *udev, usbd_event_type event);
/* usb rx and tx buffer */
static uint32_t alt_setting = 0;
static uint8_t g_rx_buff[USBD_OUT_MAXPACKET_SIZE];
__IO uint8_t g_tx_completed = 1, g_rx_completed = 0;
uint8_t PRINTER_DEVICE_ID[PRINTER_DEVICE_ID_LEN] =
{
0x00, 0x16,
'M', 'F', 'G',':','A','r','t','e', 'r', 'y' ,' ',
'C','M', 'D', ':', 'E', 'S', 'C', 'P', 'O', 'S',' ',
};
static uint8_t g_printer_port_status = 0x18;
uint8_t g_printer_data[USBD_OUT_MAXPACKET_SIZE];
/* static variable */
/* usb device class handler */
usbd_class_handler class_handler =
{
class_init_handler,
class_clear_handler,
class_setup_handler,
class_ept0_tx_handler,
class_ept0_rx_handler,
class_in_handler,
class_out_handler,
class_sof_handler,
class_event_handler,
};
/**
* @brief initialize usb custom hid endpoint
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_init_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
/* open in endpoint */
usbd_ept_open(pudev, USBD_PRINTER_BULK_IN_EPT, EPT_BULK_TYPE, USBD_OUT_MAXPACKET_SIZE);
/* open out endpoint */
usbd_ept_open(pudev, USBD_PRINTER_BULK_OUT_EPT, EPT_BULK_TYPE, USBD_OUT_MAXPACKET_SIZE);
/* set out endpoint to receive status */
usbd_ept_recv(pudev, USBD_PRINTER_BULK_OUT_EPT, g_rx_buff, USBD_OUT_MAXPACKET_SIZE);
g_tx_completed = 1;
return status;
}
/**
* @brief clear endpoint or other state
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_clear_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
/* close in endpoint */
usbd_ept_close(pudev, USBD_PRINTER_BULK_IN_EPT);
/* close out endpoint */
usbd_ept_close(pudev, USBD_PRINTER_BULK_OUT_EPT);
return status;
}
/**
* @brief usb device class setup request handler
* @param udev: to the structure of usbd_core_type
* @param setup: setup packet
* @retval status of usb_sts_type
*/
usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
switch(setup->bmRequestType & USB_REQ_TYPE_RESERVED)
{
/* class request */
case USB_REQ_TYPE_CLASS:
switch(setup->bRequest)
{
case PRINTER_REQ_GET_DEVICE_ID:
usbd_ctrl_send(pudev, PRINTER_DEVICE_ID, PRINTER_DEVICE_ID_LEN);
break;
case PRINTER_REQ_GET_PORT_STATUS:
usbd_ctrl_send(pudev, (uint8_t *)&g_printer_port_status, 1);
break;
case PRINTER_REQ_GET_SOFT_RESET:
usbd_ctrl_recv(pudev, g_printer_data, USBD_OUT_MAXPACKET_SIZE);
break;
default:
usbd_ctrl_unsupport(pudev);
break;
}
break;
/* standard request */
case USB_REQ_TYPE_STANDARD:
switch(setup->bRequest)
{
case USB_STD_REQ_GET_DESCRIPTOR:
usbd_ctrl_unsupport(pudev);
break;
case USB_STD_REQ_GET_INTERFACE:
usbd_ctrl_send(pudev, (uint8_t *)&alt_setting, 1);
break;
case USB_STD_REQ_SET_INTERFACE:
alt_setting = setup->wValue;
break;
default:
break;
}
break;
default:
usbd_ctrl_unsupport(pudev);
break;
}
return status;
}
/**
* @brief usb device endpoint 0 in status stage complete
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_ept0_tx_handler(void *udev)
{
usb_sts_type status = USB_OK;
/* ...user code... */
return status;
}
/**
* @brief usb device endpoint 0 out status stage complete
* @param udev: usb device core handler type
* @retval status of usb_sts_type
*/
usb_sts_type class_ept0_rx_handler(void *udev)
{
usb_sts_type status = USB_OK;
/* ...user code... */
return status;
}
/**
* @brief usb device transmision complete handler
* @param udev: to the structure of usbd_core_type
* @param ept_num: endpoint number
* @retval status of usb_sts_type
*/
usb_sts_type class_in_handler(void *udev, uint8_t ept_num)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
usb_sts_type status = USB_OK;
/* ...user code...
trans next packet data
*/
usbd_flush_tx_fifo(pudev, ept_num);
g_tx_completed = 1;
return status;
}
/**
* @brief usb device endpoint receive data
* @param udev: to the structure of usbd_core_type
* @param ept_num: endpoint number
* @retval status of usb_sts_type
*/
usb_sts_type class_out_handler(void *udev, uint8_t ept_num)
{
usb_sts_type status = USB_OK;
/*set recv flag*/
g_rx_completed = 1;
return status;
}
/**
* @brief usb device sof handler
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type class_sof_handler(void *udev)
{
usb_sts_type status = USB_OK;
/* ...user code... */
return status;
}
/**
* @brief usb device event handler
* @param udev: to the structure of usbd_core_type
* @param event: usb device event
* @retval status of usb_sts_type
*/
usb_sts_type class_event_handler(void *udev, usbd_event_type event)
{
usb_sts_type status = USB_OK;
switch(event)
{
case USBD_RESET_EVENT:
/* ...user code... */
break;
case USBD_SUSPEND_EVENT:
/* ...user code... */
break;
case USBD_WAKEUP_EVENT:
/* ...user code... */
break;
case USBD_INISOINCOM_EVENT:
break;
case USBD_OUTISOINCOM_EVENT:
break;
default:
break;
}
return status;
}
/**
* @brief usb device class send data
* @param udev: to the structure of usbd_core_type
* @param send_data: send data buffer
* @param len: send length
* @retval error status
*/
error_status usb_printer_send_data(void *udev, uint8_t *send_data, uint16_t len)
{
error_status status = SUCCESS;
usbd_core_type *pudev = (usbd_core_type *)udev;
if(g_tx_completed)
{
g_tx_completed = 0;
usbd_ept_send(pudev, USBD_PRINTER_BULK_IN_EPT, send_data, len);
}
else
{
status = ERROR;
}
return status;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/

View File

@@ -0,0 +1,96 @@
/**
**************************************************************************
* @file printer_class.h
* @version v2.0.0
* @date 2021-11-26
* @brief usb cdc class file
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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.
*
**************************************************************************
*/
/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __PRINTER_CLASS_H
#define __PRINTER_CLASS_H
#ifdef __cplusplus
extern "C" {
#endif
#include "usb_std.h"
#include "usbd_core.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @addtogroup USB_printer_class
* @{
*/
/** @defgroup USB_printer_class_definition
* @{
*/
#define USBD_PRINTER_BULK_IN_EPT 0x81
#define USBD_PRINTER_BULK_OUT_EPT 0x01
#define USBD_IN_MAXPACKET_SIZE 0x40
#define USBD_OUT_MAXPACKET_SIZE 0x40
#define PRINTER_DEVICE_ID_LEN 24
typedef enum
{
PRINTER_REQ_GET_DEVICE_ID = 0x00,
PRINTER_REQ_GET_PORT_STATUS = 0x01,
PRINTER_REQ_GET_SOFT_RESET = 0x02
}printer_req_type;
#define SET_LINE_CODING 0x20
#define GET_LINE_CODING 0x21
typedef struct
{
uint32_t bitrate;
uint8_t format;
uint8_t parity;
uint8_t data;
}linecoding_type;
extern usbd_class_handler class_handler;
uint16_t usb_printer_get_rxdata(void *udev, uint8_t *recv_data);
error_status usb_printer_send_data(void *udev, uint8_t *send_data, uint16_t len);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,393 @@
/**
**************************************************************************
* @file printer_desc.c
* @version v2.0.0
* @date 2021-11-26
* @brief usb printer device descriptor
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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 "stdio.h"
#include "usb_std.h"
#include "usbd_sdr.h"
#include "usbd_core.h"
#include "printer_desc.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @defgroup USB_printer_desc
* @brief usb device printer descriptor
* @{
*/
/** @defgroup USB_printer_desc_private_functions
* @{
*/
usbd_desc_t *get_device_descriptor(void);
usbd_desc_t *get_device_qualifier(void);
usbd_desc_t *get_device_configuration(void);
usbd_desc_t *get_device_other_speed(void);
usbd_desc_t *get_device_lang_id(void);
usbd_desc_t *get_device_manufacturer_string(void);
usbd_desc_t *get_device_product_string(void);
usbd_desc_t *get_device_serial_string(void);
usbd_desc_t *get_device_interface_string(void);
usbd_desc_t *get_device_config_string(void);
uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf);
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 device descriptor handler structure
*/
usbd_desc_handler 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
*/
uint8_t g_usbd_descriptor[USB_DEVICE_DESC_LEN] =
{
USB_DEVICE_DESC_LEN, /* bLength */
USB_DESCIPTOR_TYPE_DEVICE, /* bDescriptorType */
0x00, /* bcdUSB */
0x02,
0x00, /* bDeviceClass */
0x00, /* bDeviceSubClass */
0x00, /* bDeviceProtocol */
USB_MAX_EP0_SIZE, /* bMaxPacketSize */
LBYTE(USBD_VENDOR_ID), /* idVendor */
HBYTE(USBD_VENDOR_ID), /* idVendor */
LBYTE(USBD_PRODUCT_ID), /* idProduct */
HBYTE(USBD_PRODUCT_ID), /* idProduct */
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
*/
uint8_t g_usbd_configuration[USBD_CONFIG_DESC_SIZE] =
{
USB_DEVICE_CFG_DESC_LEN, /* bLength: configuration descriptor size */
USB_DESCIPTOR_TYPE_CONFIGURATION, /* bDescriptorType: configuration */
LBYTE(USBD_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
HBYTE(USBD_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
0x01, /* bNumInterfaces: 1 interface */
0x01, /* bConfigurationValue: configuration value */
0x00, /* iConfiguration: index of string descriptor describing
the configuration */
0xC0, /* bmAttributes: self powered */
0x32, /* MaxPower 100 mA: this current is used for detecting vbus */
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 */
0x02, /* bNumEndpoints: number of endpoints */
USB_CLASS_CODE_PRINTER, /* bInterfaceClass: printer class code */
0x01, /* bInterfaceSubClass: subclass code*/
PRINTER_PROTOCOL_BI_DIRECTIONAL, /* bInterfaceProtocol: printer interface type */
0x00, /* iInterface: index of string descriptor */
USB_DEVICE_EPT_LEN, /* bLength: size of endpoint descriptor in bytes */
USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
USBD_PRINTER_BULK_IN_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
USB_EPT_DESC_BULK, /* bmAttributes: endpoint attributes */
LBYTE(USBD_IN_MAXPACKET_SIZE),
HBYTE(USBD_IN_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
0x00, /* bInterval: interval for polling endpoint for data transfers */
USB_DEVICE_EPT_LEN, /* bLength: size of endpoint descriptor in bytes */
USB_DESCIPTOR_TYPE_ENDPOINT, /* bDescriptorType: endpoint descriptor type */
USBD_PRINTER_BULK_OUT_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
USB_EPT_DESC_BULK, /* bmAttributes: endpoint attributes */
LBYTE(USBD_OUT_MAXPACKET_SIZE),
HBYTE(USBD_OUT_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
0x00, /* bInterval: interval for polling endpoint for data transfers */
};
/**
* @brief usb string lang id
*/
uint8_t g_string_lang_id[USBD_SIZ_STRING_LANGID] =
{
USBD_SIZ_STRING_LANGID,
USB_DESCIPTOR_TYPE_STRING,
0x09,
0x04,
};
/**
* @brief usb string serial
*/
uint8_t g_string_serial[USBD_SIZ_STRING_SERIAL] =
{
USBD_SIZ_STRING_SERIAL,
USB_DESCIPTOR_TYPE_STRING,
};
/* device descriptor */
usbd_desc_t device_descriptor =
{
USB_DEVICE_DESC_LEN,
g_usbd_descriptor
};
/* config descriptor */
usbd_desc_t config_descriptor =
{
USBD_CONFIG_DESC_SIZE,
g_usbd_configuration
};
/* langid descriptor */
usbd_desc_t langid_descriptor =
{
USBD_SIZ_STRING_LANGID,
g_string_lang_id
};
/* serial descriptor */
usbd_desc_t serial_descriptor =
{
USBD_SIZ_STRING_SERIAL,
g_string_serial
};
usbd_desc_t vp_desc;
/**
* @brief standard usb unicode convert
* @param string: source string
* @param unicode_buf: unicode buffer
* @retval length
*/
uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf)
{
uint16_t str_len = 0, id_pos = 2;
uint8_t *tmp_str = string;
while(*tmp_str != '\0')
{
str_len ++;
unicode_buf[id_pos ++] = *tmp_str ++;
unicode_buf[id_pos ++] = 0x00;
}
str_len = str_len * 2 + 2;
unicode_buf[0] = (uint8_t)str_len;
unicode_buf[1] = USB_DESCIPTOR_TYPE_STRING;
return str_len;
}
/**
* @brief usb int convert to unicode
* @param value: int value
* @param pbus: unicode buffer
* @param len: length
* @retval none
*/
static void usbd_int_to_unicode (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;
}
}
/**
* @brief usb get serial number
* @param none
* @retval none
*/
static void get_serial_num(void)
{
uint32_t serial0, serial1, serial2;
serial0 = *(uint32_t*)MCU_ID1;
serial1 = *(uint32_t*)MCU_ID2;
serial2 = *(uint32_t*)MCU_ID3;
serial0 += serial2;
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
* @retval usbd_desc
*/
usbd_desc_t *get_device_descriptor(void)
{
return &device_descriptor;
}
/**
* @brief get device qualifier
* @param none
* @retval usbd_desc
*/
usbd_desc_t * get_device_qualifier(void)
{
return NULL;
}
/**
* @brief get config descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_configuration(void)
{
return &config_descriptor;
}
/**
* @brief get other speed descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_other_speed(void)
{
return NULL;
}
/**
* @brief get lang id descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_lang_id(void)
{
return &langid_descriptor;
}
/**
* @brief get manufacturer descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_manufacturer_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_MANUFACTURER_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get product descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_product_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_PRODUCT_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get serial descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_serial_string(void)
{
get_serial_num();
return &serial_descriptor;
}
/**
* @brief get interface descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_interface_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_INTERFACE_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @brief get device config descriptor
* @param none
* @retval usbd_desc
*/
usbd_desc_t *get_device_config_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_DESC_CONFIGURATION_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/

View File

@@ -0,0 +1,95 @@
/**
**************************************************************************
* @file printer_desc.h
* @version v2.0.0
* @date 2021-11-26
* @brief usb printer descriptor header file
**************************************************************************
* Copyright notice & Disclaimer
*
* 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
* 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.
*
**************************************************************************
*/
/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __PRINTER_DESC_H
#define __PRINTER_DESC_H
#ifdef __cplusplus
extern "C" {
#endif
#include "printer_class.h"
#include "usbd_core.h"
/** @addtogroup AT32F415_middlewares_usbd_class
* @{
*/
/** @addtogroup USB_printer_desc
* @{
*/
/** @defgroup USB_printer_desc_definition
* @{
*/
#define BCD_NUM 0x0110
#define USBD_VENDOR_ID 0x2E3C
#define USBD_PRODUCT_ID 0x57FF
#define USBD_CONFIG_DESC_SIZE 32
#define USBD_SIZ_STRING_LANGID 4
#define USBD_SIZ_STRING_SERIAL 0x1A
#define USBD_DESC_MANUFACTURER_STRING "Artery"
#define USBD_DESC_PRODUCT_STRING "AT32 Printer"
#define USBD_DESC_CONFIGURATION_STRING "Printer Config"
#define USBD_DESC_INTERFACE_STRING "Printer Interface"
#define PRINTER_PROTOCOL_UNIDIRECTIONAL 0x01
#define PRINTER_PROTOCOL_BI_DIRECTIONAL 0x02
#define PRINTER_PROTOCOL_1284_4 0x03
#define PRINTER_PROTOCOL_VENDOR_SPECIFIC 0xFF
#define HID_BINTERVAL_TIME 0xFF
#define MCU_ID1 (0x1FFFF7E8)
#define MCU_ID2 (0x1FFFF7EC)
#define MCU_ID3 (0x1FFFF7F0)
extern uint8_t g_usbd_descriptor[USB_DEVICE_DESC_LEN];
extern uint8_t g_usbd_configuration[USBD_CONFIG_DESC_SIZE];
extern usbd_desc_handler desc_handler;
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif