Sync inav to Gitea
Make sure docs are updated / settings_md (push) Canceled after 0s
Build firmware / test (push) Canceled after 0s
Build firmware / build-SITL-Windows (push) Canceled after 0s
Build firmware / build-SITL-Mac (push) Canceled after 0s
Build firmware / build-SITL-Linux (push) Canceled after 0s
Build firmware / build-SITL-Linux-arm64 (push) Canceled after 0s
Build firmware / upload-artifacts (push) Canceled after 0s
Build firmware / build-single-target (push) Canceled after 0s
Build firmware / build (9) (push) Canceled after 0s
Build firmware / build (8) (push) Canceled after 0s
Build firmware / build (7) (push) Canceled after 0s
Build firmware / build (6) (push) Canceled after 0s
Build firmware / build (5) (push) Canceled after 0s
Build firmware / build (4) (push) Canceled after 0s
Build firmware / build (3) (push) Canceled after 0s
Build firmware / build (2) (push) Canceled after 0s
Build firmware / build (14) (push) Canceled after 0s
Build firmware / build (13) (push) Canceled after 0s
Build firmware / build (12) (push) Canceled after 0s
Build firmware / build (11) (push) Canceled after 0s
Build firmware / build (10) (push) Canceled after 0s
Build firmware / build (1) (push) Canceled after 0s
Build firmware / build (0) (push) Canceled after 0s
Build firmware / detect (push) Canceled after 0s
Build pre-release / build (push) Canceled after 0s
Build pre-release / Release (push) Canceled after 0s

This commit is contained in:
2026-08-03 16:37:40 +08:00
commit dac37fd077
14095 changed files with 5603119 additions and 0 deletions
@@ -0,0 +1,439 @@
/**
**************************************************************************
* @file cdc_class.c
* @version v2.1.0
* @date 2022-08-16
* @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 AT32F435_437_middlewares_usbd_class
* @{
*/
/** @defgroup USB_cdc_class
* @brief usb device class cdc demo
* @{
*/
/** @defgroup USB_cdc_class_private_functions
* @{
*/
static usb_sts_type class_init_handler(void *udev);
static usb_sts_type class_clear_handler(void *udev);
static usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup);
static usb_sts_type class_ept0_tx_handler(void *udev);
static usb_sts_type class_ept0_rx_handler(void *udev);
static usb_sts_type class_in_handler(void *udev, uint8_t ept_num);
static usb_sts_type class_out_handler(void *udev, uint8_t ept_num);
static usb_sts_type class_sof_handler(void *udev);
static usb_sts_type class_event_handler(void *udev, usbd_event_type event);
static usb_sts_type cdc_struct_init(cdc_struct_type *pcdc);
extern void usb_usart_config( linecoding_type linecoding);
static void usb_vcp_cmd_process(void *udev, uint8_t cmd, uint8_t *buff, uint16_t len);
linecoding_type linecoding =
{
115200,
0,
0,
8
};
/* cdc data struct */
cdc_struct_type cdc_struct;
/* usb device class handler */
usbd_class_handler cdc_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,
&cdc_struct
};
/**
* @brief initialize usb custom hid endpoint
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
static usb_sts_type class_init_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
cdc_struct_type *pcdc = (cdc_struct_type *)pudev->class_handler->pdata;
/* init cdc struct */
cdc_struct_init(pcdc);
/* open in endpoint */
usbd_ept_open(pudev, USBD_CDC_INT_EPT, EPT_INT_TYPE, USBD_CDC_CMD_MAXPACKET_SIZE);
/* open in endpoint */
usbd_ept_open(pudev, USBD_CDC_BULK_IN_EPT, EPT_BULK_TYPE, USBD_CDC_IN_MAXPACKET_SIZE);
/* open out endpoint */
usbd_ept_open(pudev, USBD_CDC_BULK_OUT_EPT, EPT_BULK_TYPE, USBD_CDC_OUT_MAXPACKET_SIZE);
/* set out endpoint to receive status */
usbd_ept_recv(pudev, USBD_CDC_BULK_OUT_EPT, pcdc->g_rx_buff, USBD_CDC_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
*/
static 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
*/
static 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;
cdc_struct_type *pcdc = (cdc_struct_type *)pudev->class_handler->pdata;
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, pcdc->g_cmd, setup->wLength);
usbd_ctrl_send(pudev, pcdc->g_cmd, setup->wLength);
}
else
{
pcdc->g_req = setup->bRequest;
pcdc->g_len = setup->wLength;
usbd_ctrl_recv(pudev, pcdc->g_cmd, pcdc->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 *)&pcdc->alt_setting, 1);
break;
case USB_STD_REQ_SET_INTERFACE:
pcdc->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
*/
static usb_sts_type class_ept0_tx_handler(void *udev)
{
usb_sts_type status = USB_OK;
/* ...user code... */
UNUSED(udev);
return status;
}
/**
* @brief usb device endpoint 0 out status stage complete
* @param udev: usb device core handler type
* @retval status of usb_sts_type
*/
static usb_sts_type class_ept0_rx_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
cdc_struct_type *pcdc = (cdc_struct_type *)pudev->class_handler->pdata;
uint32_t recv_len = usbd_get_recv_len(pudev, 0);
/* ...user code... */
if( pcdc->g_req == SET_LINE_CODING)
{
/* class process */
usb_vcp_cmd_process(udev, pcdc->g_req, pcdc->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
*/
static usb_sts_type class_in_handler(void *udev, uint8_t ept_num)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
cdc_struct_type *pcdc = (cdc_struct_type *)pudev->class_handler->pdata;
usb_sts_type status = USB_OK;
/* ...user code...
trans next packet data
*/
usbd_flush_tx_fifo(pudev, ept_num);
pcdc->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
*/
static 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;
cdc_struct_type *pcdc = (cdc_struct_type *)pudev->class_handler->pdata;
/* get endpoint receive data length */
pcdc->g_rxlen = usbd_get_recv_len(pudev, ept_num);
/*set recv flag*/
pcdc->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
*/
static usb_sts_type class_sof_handler(void *udev)
{
usb_sts_type status = USB_OK;
/* ...user code... */
UNUSED(udev);
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
*/
static usb_sts_type class_event_handler(void *udev, usbd_event_type event)
{
usb_sts_type status = USB_OK;
UNUSED(udev);
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 cdc init
* @param pcdc: to the structure of cdc_struct
* @retval status of usb_sts_type
*/
static usb_sts_type cdc_struct_init(cdc_struct_type *pcdc)
{
pcdc->g_tx_completed = 1;
pcdc->g_rx_completed = 0;
pcdc->alt_setting = 0;
pcdc->linecoding.bitrate = linecoding.bitrate;
pcdc->linecoding.data = linecoding.data;
pcdc->linecoding.format = linecoding.format;
pcdc->linecoding.parity = linecoding.parity;
return USB_OK;
}
/**
* @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 = 0;
usbd_core_type *pudev = (usbd_core_type *)udev;
cdc_struct_type *pcdc = (cdc_struct_type *)pudev->class_handler->pdata;
if(pcdc->g_rx_completed == 0)
{
return 0;
}
pcdc->g_rx_completed = 0;
tmp_len = pcdc->g_rxlen;
for(i_index = 0; i_index < pcdc->g_rxlen; i_index ++)
{
recv_data[i_index] = pcdc->g_rx_buff[i_index];
}
usbd_ept_recv(pudev, USBD_CDC_BULK_OUT_EPT, pcdc->g_rx_buff, USBD_CDC_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;
cdc_struct_type *pcdc = (cdc_struct_type *)pudev->class_handler->pdata;
if(pcdc->g_tx_completed)
{
pcdc->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
*/
static void usb_vcp_cmd_process(void *udev, uint8_t cmd, uint8_t *buff, uint16_t len)
{
UNUSED(len);
usbd_core_type *pudev = (usbd_core_type *)udev;
cdc_struct_type *pcdc = (cdc_struct_type *)pudev->class_handler->pdata;
switch(cmd)
{
case SET_LINE_CODING:
pcdc->linecoding.bitrate = (uint32_t)(buff[0] | (buff[1] << 8) | (buff[2] << 16) | (buff[3] <<24));
pcdc->linecoding.format = buff[4];
pcdc->linecoding.parity = buff[5];
pcdc->linecoding.data = buff[6];
#ifdef USB_VIRTUAL_COMPORT
/* set hardware usart */
usb_usart_config(pcdc->linecoding);
#endif
break;
case GET_LINE_CODING:
buff[0] = (uint8_t)pcdc->linecoding.bitrate;
buff[1] = (uint8_t)(pcdc->linecoding.bitrate >> 8);
buff[2] = (uint8_t)(pcdc->linecoding.bitrate >> 16);
buff[3] = (uint8_t)(pcdc->linecoding.bitrate >> 24);
buff[4] = (uint8_t)(pcdc->linecoding.format);
buff[5] = (uint8_t)(pcdc->linecoding.parity);
buff[6] = (uint8_t)(pcdc->linecoding.data);
break;
default:
break;
}
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
@@ -0,0 +1,117 @@
/**
**************************************************************************
* @file cdc_class.h
* @version v2.1.0
* @date 2022-08-16
* @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 AT32F435_437_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_CDC_IN_MAXPACKET_SIZE 0x40
#define USBD_CDC_OUT_MAXPACKET_SIZE 0x40
#define USBD_CDC_CMD_MAXPACKET_SIZE 0x08
/**
* @}
*/
/** @defgroup USB_cdc_class_exported_types
* @{
*/
/**
* @brief usb cdc class struct
*/
typedef struct
{
uint32_t alt_setting;
uint8_t g_rx_buff[USBD_CDC_OUT_MAXPACKET_SIZE];
uint8_t g_cmd[USBD_CDC_CMD_MAXPACKET_SIZE];
uint8_t g_req;
uint16_t g_len, g_rxlen;
__IO uint8_t g_tx_completed, g_rx_completed;
linecoding_type linecoding;
}cdc_struct_type;
/**
* @}
*/
/** @defgroup USB_cdc_class_exported_functions
* @{
*/
extern usbd_class_handler cdc_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
@@ -0,0 +1,446 @@
/**
**************************************************************************
* @file cdc_desc.c
* @version v2.1.0
* @date 2022-08-16
* @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 AT32F435_437_middlewares_usbd_class
* @{
*/
/** @defgroup USB_cdc_desc
* @brief usb device cdc descriptor
* @{
*/
/** @defgroup USB_cdc_desc_private_functions
* @{
*/
static usbd_desc_t *get_device_descriptor(void);
static usbd_desc_t *get_device_qualifier(void);
static usbd_desc_t *get_device_configuration(void);
static usbd_desc_t *get_device_other_speed(void);
static usbd_desc_t *get_device_lang_id(void);
static usbd_desc_t *get_device_manufacturer_string(void);
static usbd_desc_t *get_device_product_string(void);
static usbd_desc_t *get_device_serial_string(void);
static usbd_desc_t *get_device_interface_string(void);
static usbd_desc_t *get_device_config_string(void);
static uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf);
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_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
*/
#if defined ( __ICCARM__ ) /* iar compiler */
#pragma data_alignment=4
#endif
ALIGNED_HEAD static uint8_t g_usbd_descriptor[USB_DEVICE_DESC_LEN] ALIGNED_TAIL =
{
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_CDC_VENDOR_ID), /* idVendor */
HBYTE(USBD_CDC_VENDOR_ID), /* idVendor */
LBYTE(USBD_CDC_PRODUCT_ID), /* idProduct */
HBYTE(USBD_CDC_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
*/
#if defined ( __ICCARM__ ) /* iar compiler */
#pragma data_alignment=4
#endif
ALIGNED_HEAD static uint8_t g_usbd_configuration[USBD_CDC_CONFIG_DESC_SIZE] ALIGNED_TAIL =
{
USB_DEVICE_CFG_DESC_LEN, /* bLength: configuration descriptor size */
USB_DESCIPTOR_TYPE_CONFIGURATION, /* bDescriptorType: configuration */
LBYTE(USBD_CDC_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
HBYTE(USBD_CDC_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(CDC_BCD_NUM),
HBYTE(CDC_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_CDC_CMD_MAXPACKET_SIZE),
HBYTE(USBD_CDC_CMD_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
CDC_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_CDC_IN_MAXPACKET_SIZE),
HBYTE(USBD_CDC_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_CDC_OUT_MAXPACKET_SIZE),
HBYTE(USBD_CDC_OUT_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
0x00, /* bInterval: interval for polling endpoint for data transfers */
};
/**
* @brief usb string lang id
*/
#if defined ( __ICCARM__ ) /* iar compiler */
#pragma data_alignment=4
#endif
ALIGNED_HEAD static uint8_t g_string_lang_id[USBD_CDC_SIZ_STRING_LANGID] ALIGNED_TAIL =
{
USBD_CDC_SIZ_STRING_LANGID,
USB_DESCIPTOR_TYPE_STRING,
0x09,
0x04,
};
/**
* @brief usb string serial
*/
#if defined ( __ICCARM__ ) /* iar compiler */
#pragma data_alignment=4
#endif
ALIGNED_HEAD static uint8_t g_string_serial[USBD_CDC_SIZ_STRING_SERIAL] ALIGNED_TAIL =
{
USBD_CDC_SIZ_STRING_SERIAL,
USB_DESCIPTOR_TYPE_STRING,
};
/* device descriptor */
static usbd_desc_t device_descriptor =
{
USB_DEVICE_DESC_LEN,
g_usbd_descriptor
};
/* config descriptor */
static usbd_desc_t config_descriptor =
{
USBD_CDC_CONFIG_DESC_SIZE,
g_usbd_configuration
};
/* langid descriptor */
static usbd_desc_t langid_descriptor =
{
USBD_CDC_SIZ_STRING_LANGID,
g_string_lang_id
};
/* serial descriptor */
static usbd_desc_t serial_descriptor =
{
USBD_CDC_SIZ_STRING_SERIAL,
g_string_serial
};
static usbd_desc_t vp_desc;
/**
* @brief standard usb unicode convert
* @param string: source string
* @param unicode_buf: unicode buffer
* @retval length
*/
static 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
*/
static usbd_desc_t *get_device_descriptor(void)
{
return &device_descriptor;
}
/**
* @brief get device qualifier
* @param none
* @retval usbd_desc
*/
static usbd_desc_t * get_device_qualifier(void)
{
return NULL;
}
/**
* @brief get config descriptor
* @param none
* @retval usbd_desc
*/
static usbd_desc_t *get_device_configuration(void)
{
return &config_descriptor;
}
/**
* @brief get other speed descriptor
* @param none
* @retval usbd_desc
*/
static usbd_desc_t *get_device_other_speed(void)
{
return NULL;
}
/**
* @brief get lang id descriptor
* @param none
* @retval usbd_desc
*/
static usbd_desc_t *get_device_lang_id(void)
{
return &langid_descriptor;
}
/**
* @brief get manufacturer descriptor
* @param none
* @retval usbd_desc
*/
static usbd_desc_t *get_device_manufacturer_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_CDC_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
*/
static usbd_desc_t *get_device_product_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_CDC_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
*/
static usbd_desc_t *get_device_serial_string(void)
{
get_serial_num();
return &serial_descriptor;
}
/**
* @brief get interface descriptor
* @param none
* @retval usbd_desc
*/
static usbd_desc_t *get_device_interface_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_CDC_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
*/
static usbd_desc_t *get_device_config_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_CDC_DESC_CONFIGURATION_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
@@ -0,0 +1,104 @@
/**
**************************************************************************
* @file cdc_desc.h
* @version v2.1.0
* @date 2022-08-16
* @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 AT32F435_437_middlewares_usbd_class
* @{
*/
/** @addtogroup USB_cdc_desc
* @{
*/
/** @defgroup USB_cdc_desc_definition
* @{
*/
/**
* @brief usb bcd number define
*/
#define CDC_BCD_NUM 0x0110
/**
* @brief usb vendor id and product id define
*/
#define USBD_CDC_VENDOR_ID 0x2E3C
#define USBD_CDC_PRODUCT_ID 0x5740
/**
* @brief usb descriptor size define
*/
#define USBD_CDC_CONFIG_DESC_SIZE 67
#define USBD_CDC_SIZ_STRING_LANGID 4
#define USBD_CDC_SIZ_STRING_SERIAL 0x1A
/**
* @brief usb string define(vendor, product configuration, interface)
*/
#define USBD_CDC_DESC_MANUFACTURER_STRING "Artery"
#define USBD_CDC_DESC_PRODUCT_STRING "AT32 Virtual Com Port "
#define USBD_CDC_DESC_CONFIGURATION_STRING "Virtual ComPort Config"
#define USBD_CDC_DESC_INTERFACE_STRING "Virtual ComPort Interface"
/**
* @brief usb endpoint interval define
*/
#define CDC_HID_BINTERVAL_TIME 0xFF
/**
* @brief usb mcu id address deine
*/
#define MCU_ID1 (0x1FFFF7E8)
#define MCU_ID2 (0x1FFFF7EC)
#define MCU_ID3 (0x1FFFF7F0)
/**
* @}
*/
extern usbd_desc_handler cdc_desc_handler;
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,342 @@
/**
**************************************************************************
* @file hid_iap_class.c
* @version v2.1.0
* @date 2022-08-16
* @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 AT32F435_437_middlewares_usbd_class
* @{
*/
/** @defgroup USB_hid_iap_class
* @brief usb device class hid iap demo
* @{
*/
/** @defgroup USB_hid_iap_class_private_functions
* @{
*/
static usb_sts_type class_init_handler(void *udev);
static usb_sts_type class_clear_handler(void *udev);
static usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup);
static usb_sts_type class_ept0_tx_handler(void *udev);
static usb_sts_type class_ept0_rx_handler(void *udev);
static usb_sts_type class_in_handler(void *udev, uint8_t ept_num);
static usb_sts_type class_out_handler(void *udev, uint8_t ept_num);
static usb_sts_type class_sof_handler(void *udev);
static usb_sts_type class_event_handler(void *udev, usbd_event_type event);
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,
&iap_info
};
/**
* @brief initialize usb custom hid endpoint
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
static usb_sts_type class_init_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
iap_info_type *piap = (iap_info_type *)pudev->class_handler->pdata;
/* open hid iap in endpoint */
usbd_ept_open(pudev, USBD_HIDIAP_IN_EPT, EPT_INT_TYPE, USBD_HIDIAP_IN_MAXPACKET_SIZE);
/* open hid iap out endpoint */
usbd_ept_open(pudev, USBD_HIDIAP_OUT_EPT, EPT_INT_TYPE, USBD_HIDIAP_OUT_MAXPACKET_SIZE);
/* set out endpoint to receive status */
usbd_ept_recv(pudev, USBD_HIDIAP_OUT_EPT, piap->g_rxhid_buff, USBD_HIDIAP_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
*/
static 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_HIDIAP_IN_EPT);
/* close hid iap out endpoint */
usbd_ept_close(pudev, USBD_HIDIAP_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
*/
static 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;
iap_info_type *piap = (iap_info_type *)pudev->class_handler->pdata;
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:
piap->hid_protocol = (uint8_t)setup->wValue;
break;
case HID_REQ_GET_PROTOCOL:
usbd_ctrl_send(pudev, (uint8_t *)&piap->hid_protocol, 1);
break;
case HID_REQ_SET_IDLE:
piap->hid_set_idle = (uint8_t)(setup->wValue >> 8);
break;
case HID_REQ_GET_IDLE:
usbd_ctrl_send(pudev, (uint8_t *)&piap->hid_set_idle, 1);
break;
case HID_REQ_SET_REPORT:
piap->hid_state = HID_REQ_SET_REPORT;
usbd_ctrl_recv(pudev, piap->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_HIDIAP_SIZ_REPORT_DESC, setup->wLength);
buf = (uint8_t *)g_usbd_hidiap_report;
}
else if(setup->wValue >> 8 == HID_DESCRIPTOR_TYPE)
{
len = MIN(9, setup->wLength);
buf = (uint8_t *)g_hidiap_usb_desc;
}
usbd_ctrl_send(pudev, (uint8_t *)buf, len);
break;
case USB_STD_REQ_GET_INTERFACE:
usbd_ctrl_send(pudev, (uint8_t *)&piap->alt_setting, 1);
break;
case USB_STD_REQ_SET_INTERFACE:
piap->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
*/
static 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
*/
static usb_sts_type class_ept0_rx_handler(void *udev)
{
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
iap_info_type *piap = (iap_info_type *)pudev->class_handler->pdata;
uint32_t recv_len = usbd_get_recv_len(pudev, 0);
/* ...user code... */
if( piap->hid_state == HID_REQ_SET_REPORT)
{
/* hid buffer process */
piap->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
*/
static 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
*/
static 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;
iap_info_type *piap = (iap_info_type *)pudev->class_handler->pdata;
/* get endpoint receive data length */
uint32_t recv_len = usbd_get_recv_len(pudev, ept_num);
/* hid iap process */
usbd_hid_iap_process(udev, piap->g_rxhid_buff, recv_len);
/* start receive next packet */
usbd_ept_recv(pudev, USBD_HIDIAP_OUT_EPT, piap->g_rxhid_buff, USBD_HIDIAP_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
*/
static 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
*/
static 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 usb_iap_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_HIDIAP_IN_EPT, report, len);
return status;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
@@ -0,0 +1,159 @@
/**
**************************************************************************
* @file hid_iap_class.h
* @version v2.1.0
* @date 2022-08-16
* @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 AT32F435_437_middlewares_usbd_class
* @{
*/
/** @addtogroup USB_hid_iap_class
* @{
*/
/** @defgroup USB_hid_iap_class_definition
* @{
*/
#define USBD_HIDIAP_IN_EPT 0x81
#define USBD_HIDIAP_OUT_EPT 0x01
#define USBD_HIDIAP_IN_MAXPACKET_SIZE 0x40
#define USBD_HIDIAP_OUT_MAXPACKET_SIZE 0x40
#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_HIDIAP_OUT_MAXPACKET_SIZE];
uint8_t iap_tx[USBD_HIDIAP_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;
uint32_t respond_flag;
uint8_t g_rxhid_buff[USBD_HIDIAP_OUT_MAXPACKET_SIZE];
uint32_t hid_protocol;
uint32_t hid_set_idle;
uint32_t alt_setting;
uint32_t hid_state;
uint8_t hid_set_report[64];
iap_machine_state_type state;
}iap_info_type;
extern usbd_class_handler hid_iap_class_handler;
extern iap_info_type iap_info;
usb_sts_type usb_iap_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
@@ -0,0 +1,468 @@
/**
**************************************************************************
* @file hid_iap_desc.c
* @version v2.1.0
* @date 2022-08-16
* @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 AT32F435_437_middlewares_usbd_class
* @{
*/
/** @defgroup USB_hid_iap_desc
* @brief usb device hid_iap descriptor
* @{
*/
/** @defgroup USB_hid_iap_desc_private_functions
* @{
*/
static usbd_desc_t *get_device_descriptor(void);
static usbd_desc_t *get_device_qualifier(void);
static usbd_desc_t *get_device_configuration(void);
static usbd_desc_t *get_device_other_speed(void);
static usbd_desc_t *get_device_lang_id(void);
static usbd_desc_t *get_device_manufacturer_string(void);
static usbd_desc_t *get_device_product_string(void);
static usbd_desc_t *get_device_serial_string(void);
static usbd_desc_t *get_device_interface_string(void);
static usbd_desc_t *get_device_config_string(void);
static uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf);
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
*/
#if defined ( __ICCARM__ ) /* iar compiler */
#pragma data_alignment=4
#endif
ALIGNED_HEAD static uint8_t g_usbd_descriptor[USB_DEVICE_DESC_LEN] ALIGNED_TAIL =
{
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_HIDIAP_VENDOR_ID), /* idVendor */
HBYTE(USBD_HIDIAP_VENDOR_ID), /* idVendor */
LBYTE(USBD_HIDIAP_PRODUCT_ID), /* idProduct */
HBYTE(USBD_HIDIAP_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
*/
#if defined ( __ICCARM__ ) /* iar compiler */
#pragma data_alignment=4
#endif
ALIGNED_HEAD static uint8_t g_usbd_configuration[USBD_HIDIAP_CONFIG_DESC_SIZE] ALIGNED_TAIL =
{
USB_DEVICE_CFG_DESC_LEN, /* bLength: configuration descriptor size */
USB_DESCIPTOR_TYPE_CONFIGURATION, /* bDescriptorType: configuration */
LBYTE(USBD_HIDIAP_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
HBYTE(USBD_HIDIAP_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(HIDIAP_BCD_NUM),
HBYTE(HIDIAP_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_hidiap_report)),
HBYTE(sizeof(g_usbd_hidiap_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_HIDIAP_IN_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
USB_EPT_DESC_INTERRUPT, /* bmAttributes: endpoint attributes */
LBYTE(USBD_HIDIAP_IN_MAXPACKET_SIZE),
HBYTE(USBD_HIDIAP_IN_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
HIDIAP_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_HIDIAP_OUT_EPT, /* bEndpointAddress: the address of endpoint on usb device described by this descriptor */
USB_EPT_DESC_INTERRUPT, /* bmAttributes: endpoint attributes */
LBYTE(USBD_HIDIAP_OUT_MAXPACKET_SIZE),
HBYTE(USBD_HIDIAP_OUT_MAXPACKET_SIZE), /* wMaxPacketSize: maximum packe size this endpoint */
HIDIAP_BINTERVAL_TIME, /* bInterval: interval for polling endpoint for data transfers */
};
/**
* @brief usb hid report descriptor
*/
#if defined ( __ICCARM__ ) /* iar compiler */
#pragma data_alignment=4
#endif
ALIGNED_HEAD uint8_t g_usbd_hidiap_report[USBD_HIDIAP_SIZ_REPORT_DESC] ALIGNED_TAIL =
{
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
*/
#if defined ( __ICCARM__ ) /* iar compiler */
#pragma data_alignment=4
#endif
ALIGNED_HEAD uint8_t g_hidiap_usb_desc[9] ALIGNED_TAIL =
{
0x09, /* bLength: size of HID descriptor in bytes */
HID_CLASS_DESC_HID, /* bDescriptorType: HID descriptor type */
LBYTE(HIDIAP_BCD_NUM),
HBYTE(HIDIAP_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_hidiap_report)),
HBYTE(sizeof(g_usbd_hidiap_report)), /* wDescriptorLength: total length of reprot descriptor */
};
/**
* @brief usb string lang id
*/
#if defined ( __ICCARM__ ) /* iar compiler */
#pragma data_alignment=4
#endif
ALIGNED_HEAD static uint8_t g_string_lang_id[USBD_HIDIAP_SIZ_STRING_LANGID] ALIGNED_TAIL =
{
USBD_HIDIAP_SIZ_STRING_LANGID,
USB_DESCIPTOR_TYPE_STRING,
0x09,
0x04,
};
/**
* @brief usb string serial
*/
#if defined ( __ICCARM__ ) /* iar compiler */
#pragma data_alignment=4
#endif
ALIGNED_HEAD static uint8_t g_string_serial[USBD_HIDIAP_SIZ_STRING_SERIAL] ALIGNED_TAIL =
{
USBD_HIDIAP_SIZ_STRING_SERIAL,
USB_DESCIPTOR_TYPE_STRING,
};
/* device descriptor */
static usbd_desc_t device_descriptor =
{
USB_DEVICE_DESC_LEN,
g_usbd_descriptor
};
/* config descriptor */
static usbd_desc_t config_descriptor =
{
USBD_HIDIAP_CONFIG_DESC_SIZE,
g_usbd_configuration
};
/* langid descriptor */
static usbd_desc_t langid_descriptor =
{
USBD_HIDIAP_SIZ_STRING_LANGID,
g_string_lang_id
};
/* serial descriptor */
static usbd_desc_t serial_descriptor =
{
USBD_HIDIAP_SIZ_STRING_SERIAL,
g_string_serial
};
static usbd_desc_t vp_desc;
/**
* @brief standard usb unicode convert
* @param string: source string
* @param unicode_buf: unicode buffer
* @retval length
*/
static 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
*/
static usbd_desc_t *get_device_descriptor(void)
{
return &device_descriptor;
}
/**
* @brief get device qualifier
* @param none
* @retval usbd_desc
*/
static usbd_desc_t * get_device_qualifier(void)
{
return NULL;
}
/**
* @brief get config descriptor
* @param none
* @retval usbd_desc
*/
static usbd_desc_t *get_device_configuration(void)
{
return &config_descriptor;
}
/**
* @brief get other speed descriptor
* @param none
* @retval usbd_desc
*/
static usbd_desc_t *get_device_other_speed(void)
{
return NULL;
}
/**
* @brief get lang id descriptor
* @param none
* @retval usbd_desc
*/
static usbd_desc_t *get_device_lang_id(void)
{
return &langid_descriptor;
}
/**
* @brief get manufacturer descriptor
* @param none
* @retval usbd_desc
*/
static usbd_desc_t *get_device_manufacturer_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_HIDIAP_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
*/
static usbd_desc_t *get_device_product_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_HIDIAP_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
*/
static usbd_desc_t *get_device_serial_string(void)
{
get_serial_num();
return &serial_descriptor;
}
/**
* @brief get interface descriptor
* @param none
* @retval usbd_desc
*/
static usbd_desc_t *get_device_interface_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_HIDIAP_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
*/
static usbd_desc_t *get_device_config_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_HIDIAP_DESC_CONFIGURATION_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
@@ -0,0 +1,94 @@
/**
**************************************************************************
* @file hid_iap_desc.h
* @version v2.1.0
* @date 2022-08-16
* @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 AT32F435_437_middlewares_usbd_class
* @{
*/
/** @addtogroup USB_hid_iap_desc
* @{
*/
/** @defgroup USB_hid_iap_desc_definition
* @{
*/
#define HIDIAP_BCD_NUM 0x0110
#define USBD_HIDIAP_VENDOR_ID 0x2E3C
#define USBD_HIDIAP_PRODUCT_ID 0xAF01
#define USBD_HIDIAP_CONFIG_DESC_SIZE 41
#define USBD_HIDIAP_SIZ_REPORT_DESC 32
#define USBD_HIDIAP_SIZ_STRING_LANGID 4
#define USBD_HIDIAP_SIZ_STRING_SERIAL 0x1A
#define USBD_HIDIAP_DESC_MANUFACTURER_STRING "Artery"
#define USBD_HIDIAP_DESC_PRODUCT_STRING "HID IAP"
#define USBD_HIDIAP_DESC_CONFIGURATION_STRING "HID IAP Config"
#define USBD_HIDIAP_DESC_INTERFACE_STRING "HID IAP Interface"
#define HIDIAP_BINTERVAL_TIME 0x01
#define MCU_ID1 (0x1FFFF7E8)
#define MCU_ID2 (0x1FFFF7EC)
#define MCU_ID3 (0x1FFFF7F0)
extern uint8_t g_usbd_hidiap_report[USBD_HIDIAP_SIZ_REPORT_DESC];
extern uint8_t g_hidiap_usb_desc[9];
extern usbd_desc_handler hid_iap_desc_handler;
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,835 @@
/**
**************************************************************************
* @file msc_bot_scsi.c
* @version v2.1.0
* @date 2022-08-16
* @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/at32_msc_diskio.h"
/** @addtogroup AT32F435_437_middlewares_usbd_class
* @{
*/
/** @defgroup USB_msc_bot_scsi
* @brief usb device class mass storage demo
* @{
*/
/** @defgroup USB_msc_bot_functions
* @{
*/
#if defined ( __ICCARM__ ) /* iar compiler */
#pragma data_alignment=4
#endif
ALIGNED_HEAD uint8_t page00_inquiry_data[] ALIGNED_TAIL = {
0x00,
0x00,
0x00,
0x00,
0x00,
};
#if defined ( __ICCARM__ ) /* iar compiler */
#pragma data_alignment=4
#endif
ALIGNED_HEAD sense_type sense_data ALIGNED_TAIL =
{
0x70,
0x00,
SENSE_KEY_ILLEGAL_REQUEST,
0x00000000,
0x0A,
0x00000000,
0x20,
0x00,
0x00000000
};
#if defined ( __ICCARM__ ) /* iar compiler */
#pragma data_alignment=4
#endif
ALIGNED_HEAD uint8_t mode_sense6_data[8] ALIGNED_TAIL =
{
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00
};
#if defined ( __ICCARM__ ) /* iar compiler */
#pragma data_alignment=4
#endif
ALIGNED_HEAD uint8_t mode_sense10_data[8] ALIGNED_TAIL =
{
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_type *pmsc = (msc_type *)pudev->class_handler->pdata;
pmsc->msc_state = MSC_STATE_MACHINE_IDLE;
pmsc->bot_status = MSC_BOT_STATE_IDLE;
pmsc->max_lun = MSC_SUPPORT_MAX_LUN - 1;
pmsc->csw_struct.dCSWSignature = CSW_DCSWSIGNATURE;
pmsc->csw_struct.dCSWDataResidue = 0;
pmsc->csw_struct.dCSWSignature = 0;
pmsc->csw_struct.dCSWTag = CSW_BCSWSTATUS_PASS;
usbd_flush_tx_fifo(pudev, USBD_MSC_BULK_IN_EPT&0x7F);
/* set out endpoint to receive status */
usbd_ept_recv(pudev, USBD_MSC_BULK_OUT_EPT, (uint8_t *)&pmsc->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_type *pmsc = (msc_type *)pudev->class_handler->pdata;
pmsc->msc_state = MSC_STATE_MACHINE_IDLE;
pmsc->bot_status = MSC_BOT_STATE_RECOVERY;
pmsc->max_lun = MSC_SUPPORT_MAX_LUN - 1;
usbd_flush_tx_fifo(pudev, USBD_MSC_BULK_IN_EPT&0x7F);
/* set out endpoint to receive status */
usbd_ept_recv(pudev, USBD_MSC_BULK_OUT_EPT, (uint8_t *)&pmsc->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)
{
UNUSED(ept_num);
usbd_core_type *pudev = (usbd_core_type *)udev;
msc_type *pmsc = (msc_type *)pudev->class_handler->pdata;
switch(pmsc->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)
{
UNUSED(ept_num);
usbd_core_type *pudev = (usbd_core_type *)udev;
msc_type *pmsc = (msc_type *)pudev->class_handler->pdata;
switch(pmsc->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;
msc_type *pmsc = (msc_type *)pudev->class_handler->pdata;
pmsc->csw_struct.dCSWTag = pmsc->cbw_struct.dCBWTage;
pmsc->csw_struct.dCSWDataResidue = pmsc->cbw_struct.dCBWDataTransferLength;
/* check param */
if((pmsc->cbw_struct.dCBWSignature != CBW_DCBWSIGNATURE) ||
(usbd_get_recv_len(pudev, USBD_MSC_BULK_OUT_EPT) != CBW_CMD_LENGTH)
|| (pmsc->cbw_struct.bCBWLUN > MSC_SUPPORT_MAX_LUN) ||
(pmsc->cbw_struct.bCBWCBLength < 1) || (pmsc->cbw_struct.bCBWCBLength > 16))
{
bot_scsi_sense_code(udev, SENSE_KEY_ILLEGAL_REQUEST, INVALID_COMMAND);
pmsc->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((pmsc->msc_state != MSC_STATE_MACHINE_DATA_IN) &&
(pmsc->msc_state != MSC_STATE_MACHINE_DATA_OUT) &&
(pmsc->msc_state != MSC_STATE_MACHINE_LAST_DATA))
{
if(pmsc->data_len == 0)
{
bot_scsi_send_csw(udev, CSW_BCSWSTATUS_PASS);
}
else if(pmsc->data_len > 0)
{
bot_scsi_send_data(udev, pmsc->data, pmsc->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;
msc_type *pmsc = (msc_type *)pudev->class_handler->pdata;
uint32_t data_len = MIN(len, pmsc->cbw_struct.dCBWDataTransferLength);
pmsc->csw_struct.dCSWDataResidue -= data_len;
pmsc->csw_struct.bCSWStatus = CSW_BCSWSTATUS_PASS;
pmsc->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;
msc_type *pmsc = (msc_type *)pudev->class_handler->pdata;
pmsc->csw_struct.bCSWStatus = status;
pmsc->csw_struct.dCSWSignature = CSW_DCSWSIGNATURE;
pmsc->msc_state = MSC_STATE_MACHINE_IDLE;
usbd_ept_send(pudev, USBD_MSC_BULK_IN_EPT,
(uint8_t *)&pmsc->csw_struct, CSW_CMD_LENGTH);
usbd_ept_recv(pudev, USBD_MSC_BULK_OUT_EPT,
(uint8_t *)&pmsc->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)
{
UNUSED(udev);
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)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
msc_type *pmsc = (msc_type *)pudev->class_handler->pdata;
if((blk_offset + blk_count) > pmsc->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;
msc_type *pmsc = (msc_type *)pudev->class_handler->pdata;
if((pmsc->cbw_struct.dCBWDataTransferLength != 0) &&
(pmsc->cbw_struct.bmCBWFlags == 0) &&
pmsc->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(pmsc->bot_status == MSC_BOT_STATE_ERROR)
{
usbd_ept_recv(pudev, USBD_MSC_BULK_OUT_EPT,
(uint8_t *)&pmsc->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)
{
UNUSED(lun);
usb_sts_type status = USB_OK;
usbd_core_type *pudev = (usbd_core_type *)udev;
msc_type *pmsc = (msc_type *)pudev->class_handler->pdata;
if(pmsc->cbw_struct.dCBWDataTransferLength != 0)
{
bot_scsi_sense_code(udev, SENSE_KEY_ILLEGAL_REQUEST, INVALID_COMMAND);
return USB_FAIL;
}
pmsc->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;
usbd_core_type *pudev = (usbd_core_type *)udev;
msc_type *pmsc = (msc_type *)pudev->class_handler->pdata;
if(pmsc->cbw_struct.CBWCB[1] & 0x01)
{
pdata = page00_inquiry_data;
trans_len = 5;
}
else
{
pdata = get_inquiry(lun);
if(pmsc->cbw_struct.dCBWDataTransferLength < SCSI_INQUIRY_DATA_LENGTH)
{
trans_len = pmsc->cbw_struct.dCBWDataTransferLength;
}
else
{
trans_len = SCSI_INQUIRY_DATA_LENGTH;
}
}
pmsc->data_len = trans_len;
while(trans_len)
{
trans_len --;
pmsc->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)
{
UNUSED(lun);
usbd_core_type *pudev = (usbd_core_type *)udev;
msc_type *pmsc = (msc_type *)pudev->class_handler->pdata;
pmsc->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)
{
UNUSED(lun);
usbd_core_type *pudev = (usbd_core_type *)udev;
msc_type *pmsc = (msc_type *)pudev->class_handler->pdata;
pmsc->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)
{
UNUSED(lun);
uint8_t data_len = 8;
usbd_core_type *pudev = (usbd_core_type *)udev;
msc_type *pmsc = (msc_type *)pudev->class_handler->pdata;
pmsc->data_len = 8;
while(data_len)
{
data_len --;
pmsc->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)
{
UNUSED(lun);
uint8_t data_len = 8;
usbd_core_type *pudev = (usbd_core_type *)udev;
msc_type *pmsc = (msc_type *)pudev->class_handler->pdata;
pmsc->data_len = 8;
while(data_len)
{
data_len --;
pmsc->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)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
msc_type *pmsc = (msc_type *)pudev->class_handler->pdata;
uint8_t *pdata = pmsc->data;
msc_disk_capacity(lun, &pmsc->blk_nbr[lun], &pmsc->blk_size[lun]);
pdata[0] = (uint8_t)((pmsc->blk_nbr[lun] - 1) >> 24);
pdata[1] = (uint8_t)((pmsc->blk_nbr[lun] - 1) >> 16);
pdata[2] = (uint8_t)((pmsc->blk_nbr[lun] - 1) >> 8);
pdata[3] = (uint8_t)((pmsc->blk_nbr[lun] - 1));
pdata[4] = (uint8_t)((pmsc->blk_size[lun]) >> 24);
pdata[5] = (uint8_t)((pmsc->blk_size[lun]) >> 16);
pdata[6] = (uint8_t)((pmsc->blk_size[lun]) >> 8);
pdata[7] = (uint8_t)((pmsc->blk_size[lun]));
pmsc->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)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
msc_type *pmsc = (msc_type *)pudev->class_handler->pdata;
uint8_t *pdata = pmsc->data;
pdata[0] = 0;
pdata[1] = 0;
pdata[2] = 0;
pdata[3] = 0x08;
msc_disk_capacity(lun, &pmsc->blk_nbr[lun], &pmsc->blk_size[lun]);
pdata[4] = (uint8_t)((pmsc->blk_nbr[lun] - 1) >> 24);
pdata[5] = (uint8_t)((pmsc->blk_nbr[lun] - 1) >> 16);
pdata[6] = (uint8_t)((pmsc->blk_nbr[lun] - 1) >> 8);
pdata[7] = (uint8_t)((pmsc->blk_nbr[lun] - 1));
pdata[8] = 0x02;
pdata[9] = (uint8_t)((pmsc->blk_size[lun]) >> 16);
pdata[10] = (uint8_t)((pmsc->blk_size[lun]) >> 8);
pdata[11] = (uint8_t)((pmsc->blk_size[lun]));
pmsc->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)
{
UNUSED(lun);
uint32_t trans_len = 0x12;
usbd_core_type *pudev = (usbd_core_type *)udev;
msc_type *pmsc = (msc_type *)pudev->class_handler->pdata;
uint8_t *pdata = pmsc->data;
uint8_t *sdata = (uint8_t *)&sense_data;
while(trans_len)
{
trans_len --;
pdata[trans_len] = sdata[trans_len];
}
if(pmsc->cbw_struct.dCBWDataTransferLength < REQ_SENSE_STANDARD_DATA_LEN)
{
pmsc->data_len = pmsc->cbw_struct.dCBWDataTransferLength;
}
else
{
pmsc->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)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
msc_type *pmsc = (msc_type *)pudev->class_handler->pdata;
uint8_t *cmd = pmsc->cbw_struct.CBWCB;
if((pmsc->cbw_struct.CBWCB[1] & 0x02) == 0x02)
{
bot_scsi_sense_code(udev, SENSE_KEY_ILLEGAL_REQUEST, INVALID_FIELED_IN_COMMAND);
return USB_FAIL;
}
pmsc->blk_addr = cmd[2] << 24 | cmd[3] << 16 | cmd[4] << 8 | cmd[5];
pmsc->blk_len = cmd[7] << 8 | cmd[8];
if(bot_scsi_check_address(udev, lun, pmsc->blk_addr, pmsc->blk_len) != USB_OK)
{
return USB_FAIL;
}
pmsc->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)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
msc_type *pmsc = (msc_type *)pudev->class_handler->pdata;
uint8_t *cmd = pmsc->cbw_struct.CBWCB;
uint32_t len;
if(pmsc->msc_state == MSC_STATE_MACHINE_IDLE)
{
if((pmsc->cbw_struct.bmCBWFlags & 0x80) != 0x80)
{
bot_scsi_sense_code(udev, SENSE_KEY_ILLEGAL_REQUEST, INVALID_COMMAND);
return USB_FAIL;
}
pmsc->blk_addr = cmd[2] << 24 | cmd[3] << 16 | cmd[4] << 8 | cmd[5];
pmsc->blk_len = cmd[7] << 8 | cmd[8];
if(bot_scsi_check_address(udev, lun, pmsc->blk_addr, pmsc->blk_len) != USB_OK)
{
return USB_FAIL;
}
pmsc->blk_addr *= pmsc->blk_size[lun];
pmsc->blk_len *= pmsc->blk_size[lun];
if(pmsc->cbw_struct.dCBWDataTransferLength != pmsc->blk_len)
{
bot_scsi_sense_code(udev, SENSE_KEY_ILLEGAL_REQUEST, INVALID_COMMAND);
return USB_FAIL;
}
pmsc->msc_state = MSC_STATE_MACHINE_DATA_IN;
}
pmsc->data_len = MSC_MAX_DATA_BUF_LEN;
len = MIN(pmsc->blk_len, MSC_MAX_DATA_BUF_LEN);
if( msc_disk_read(lun, pmsc->blk_addr, pmsc->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, pmsc->data, len);
pmsc->blk_addr += len;
pmsc->blk_len -= len;
pmsc->csw_struct.dCSWDataResidue -= len;
if(pmsc->blk_len == 0)
{
pmsc->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)
{
usbd_core_type *pudev = (usbd_core_type *)udev;
msc_type *pmsc = (msc_type *)pudev->class_handler->pdata;
uint8_t *cmd = pmsc->cbw_struct.CBWCB;
uint32_t len;
if(pmsc->msc_state == MSC_STATE_MACHINE_IDLE)
{
if((pmsc->cbw_struct.bmCBWFlags & 0x80) == 0x80)
{
bot_scsi_sense_code(udev, SENSE_KEY_ILLEGAL_REQUEST, INVALID_COMMAND);
return USB_FAIL;
}
pmsc->blk_addr = cmd[2] << 24 | cmd[3] << 16 | cmd[4] << 8 | cmd[5];
pmsc->blk_len = cmd[7] << 8 | cmd[8];
if(bot_scsi_check_address(udev, lun, pmsc->blk_addr, pmsc->blk_len) != USB_OK)
{
return USB_FAIL;
}
pmsc->blk_addr *= pmsc->blk_size[lun];
pmsc->blk_len *= pmsc->blk_size[lun];
if(pmsc->cbw_struct.dCBWDataTransferLength != pmsc->blk_len)
{
bot_scsi_sense_code(udev, SENSE_KEY_ILLEGAL_REQUEST, INVALID_COMMAND);
return USB_FAIL;
}
pmsc->msc_state = MSC_STATE_MACHINE_DATA_OUT;
len = MIN(pmsc->blk_len, MSC_MAX_DATA_BUF_LEN);
usbd_ept_recv(pudev, USBD_MSC_BULK_OUT_EPT, (uint8_t *)pmsc->data, len);
}
else
{
len = MIN(pmsc->blk_len, MSC_MAX_DATA_BUF_LEN);
if(msc_disk_write(lun, pmsc->blk_addr, pmsc->data, len) != USB_OK)
{
bot_scsi_sense_code(udev, SENSE_KEY_HARDWARE_ERROR, MEDIUM_NOT_PRESENT);
return USB_FAIL;
}
pmsc->blk_addr += len;
pmsc->blk_len -= len;
pmsc->csw_struct.dCSWDataResidue -= len;
if(pmsc->blk_len == 0)
{
bot_scsi_send_csw(udev, CSW_BCSWSTATUS_PASS);
}
else
{
len = MIN(pmsc->blk_len, MSC_MAX_DATA_BUF_LEN);
usbd_ept_recv(pudev, USBD_MSC_BULK_OUT_EPT, (uint8_t *)pmsc->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;
msc_type *pmsc = (msc_type *)pudev->class_handler->pdata;
if(pmsc->bot_status == MSC_BOT_STATE_ERROR)
{
usbd_set_stall(pudev, USBD_MSC_BULK_IN_EPT);
pmsc->bot_status = MSC_BOT_STATE_IDLE;
}
else if(((ept_num & 0x80) == 0x80) && (pmsc->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;
usbd_core_type *pudev = (usbd_core_type *)udev;
msc_type *pmsc = (msc_type *)pudev->class_handler->pdata;
switch(pmsc->cbw_struct.CBWCB[0])
{
case MSC_CMD_INQUIRY:
status = bot_scsi_inquiry(udev, pmsc->cbw_struct.bCBWLUN);
break;
case MSC_CMD_START_STOP:
status = bot_scsi_start_stop(udev, pmsc->cbw_struct.bCBWLUN);
break;
case MSC_CMD_MODE_SENSE6:
status = bot_scsi_mode_sense6(udev, pmsc->cbw_struct.bCBWLUN);
break;
case MSC_CMD_MODE_SENSE10:
status = bot_scsi_mode_sense10(udev, pmsc->cbw_struct.bCBWLUN);
break;
case MSC_CMD_ALLOW_MEDIUM_REMOVAL:
status = bot_scsi_allow_medium_removal(udev, pmsc->cbw_struct.bCBWLUN);
break;
case MSC_CMD_READ_10:
status = bot_scsi_read10(udev, pmsc->cbw_struct.bCBWLUN);
break;
case MSC_CMD_READ_CAPACITY:
status = bot_scsi_capacity(udev, pmsc->cbw_struct.bCBWLUN);
break;
case MSC_CMD_REQUEST_SENSE:
status = bot_scsi_request_sense(udev, pmsc->cbw_struct.bCBWLUN);
break;
case MSC_CMD_TEST_UNIT:
status = bot_scsi_test_unit(udev, pmsc->cbw_struct.bCBWLUN);
break;
case MSC_CMD_VERIFY:
status = bot_scsi_verify(udev, pmsc->cbw_struct.bCBWLUN);
break;
case MSC_CMD_WRITE_10:
status = bot_scsi_write10(udev, pmsc->cbw_struct.bCBWLUN);
break;
case MSC_CMD_READ_FORMAT_CAPACITY:
status = bot_scsi_format_capacity(udev, pmsc->cbw_struct.bCBWLUN);
break;
default:
bot_scsi_sense_code(udev, SENSE_KEY_ILLEGAL_REQUEST, INVALID_COMMAND);
status = USB_FAIL;
break;
}
return status;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
@@ -0,0 +1,256 @@
/**
**************************************************************************
* @file msc_bot_scsi.h
* @version v2.1.0
* @date 2022-08-16
* @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 AT32F435_437_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;
uint32_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];
uint32_t alt_setting;
cbw_type cbw_struct;
csw_type csw_struct;
}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
@@ -0,0 +1,298 @@
/**
**************************************************************************
* @file msc_class.c
* @version v2.1.0
* @date 2022-08-16
* @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 AT32F435_437_middlewares_usbd_class
* @{
*/
/** @defgroup USB_msc_class
* @brief usb device class msc demo
* @{
*/
/** @defgroup USB_msc_class_private_functions
* @{
*/
static usb_sts_type class_init_handler(void *udev);
static usb_sts_type class_clear_handler(void *udev);
static usb_sts_type class_setup_handler(void *udev, usb_setup_type *setup);
static usb_sts_type class_ept0_tx_handler(void *udev);
static usb_sts_type class_ept0_rx_handler(void *udev);
static usb_sts_type class_in_handler(void *udev, uint8_t ept_num);
static usb_sts_type class_out_handler(void *udev, uint8_t ept_num);
static usb_sts_type class_sof_handler(void *udev);
static usb_sts_type class_event_handler(void *udev, usbd_event_type event);
msc_type msc_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,
&msc_struct
};
/**
* @brief initialize usb endpoint
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
static 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
*/
static 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
*/
static 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;
msc_type *pmsc = (msc_type *)pudev->class_handler->pdata;
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, (uint8_t *)&pmsc->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 *)&pmsc->alt_setting, 1);
break;
case USB_STD_REQ_SET_INTERFACE:
pmsc->alt_setting = setup->wValue;
break;
case USB_STD_REQ_CLEAR_FEATURE:
usbd_ept_close(pudev, (uint8_t)setup->wIndex);
if((setup->wIndex & 0x80) == 0x80)
{
usbd_flush_tx_fifo(pudev, 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
*/
static usb_sts_type class_ept0_tx_handler(void *udev)
{
usb_sts_type status = USB_OK;
/* ...user code... */
UNUSED(udev);
return status;
}
/**
* @brief usb device endpoint 0 out status stage complete
* @param udev: usb device core handler type
* @retval status of usb_sts_type
*/
static 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... */
UNUSED(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
*/
static 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;
usbd_flush_tx_fifo(pudev, ept_num&0x7F);
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
*/
static usb_sts_type class_out_handler(void *udev, uint8_t ept_num)
{
usb_sts_type status = USB_OK;
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
*/
static usb_sts_type class_sof_handler(void *udev)
{
usb_sts_type status = USB_OK;
/* ...user code... */
UNUSED(udev);
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
*/
static usb_sts_type class_event_handler(void *udev, usbd_event_type event)
{
UNUSED(udev);
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;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
@@ -0,0 +1,72 @@
/**
**************************************************************************
* @file msc_class.h
* @version v2.1.0
* @date 2022-08-16
* @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 AT32F435_437_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
@@ -0,0 +1,404 @@
/**
**************************************************************************
* @file msc_desc.c
* @version v2.1.0
* @date 2022-08-16
* @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 AT32F435_437_middlewares_usbd_class
* @{
*/
/** @defgroup USB_msc_desc
* @brief usb device msc descriptor
* @{
*/
/** @defgroup USB_msc_desc_private_functions
* @{
*/
static usbd_desc_t *get_device_descriptor(void);
static usbd_desc_t *get_device_qualifier(void);
static usbd_desc_t *get_device_configuration(void);
static usbd_desc_t *get_device_other_speed(void);
static usbd_desc_t *get_device_lang_id(void);
static usbd_desc_t *get_device_manufacturer_string(void);
static usbd_desc_t *get_device_product_string(void);
static usbd_desc_t *get_device_serial_string(void);
static usbd_desc_t *get_device_interface_string(void);
static usbd_desc_t *get_device_config_string(void);
static uint16_t usbd_unicode_convert(uint8_t *string, uint8_t *unicode_buf);
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
*/
#if defined ( __ICCARM__ ) /* iar compiler */
#pragma data_alignment=4
#endif
ALIGNED_HEAD static uint8_t g_usbd_descriptor[USB_DEVICE_DESC_LEN] ALIGNED_TAIL =
{
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_MSC_VENDOR_ID), /* idVendor */
HBYTE(USBD_MSC_VENDOR_ID), /* idVendor */
LBYTE(USBD_MSC_PRODUCT_ID), /* idProduct */
HBYTE(USBD_MSC_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
*/
#if defined ( __ICCARM__ ) /* iar compiler */
#pragma data_alignment=4
#endif
ALIGNED_HEAD static uint8_t g_usbd_configuration[USBD_MSC_CONFIG_DESC_SIZE] ALIGNED_TAIL =
{
USB_DEVICE_CFG_DESC_LEN, /* bLength: configuration descriptor size */
USB_DESCIPTOR_TYPE_CONFIGURATION, /* bDescriptorType: configuration */
LBYTE(USBD_MSC_CONFIG_DESC_SIZE), /* wTotalLength: bytes returned */
HBYTE(USBD_MSC_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
*/
#if defined ( __ICCARM__ ) /* iar compiler */
#pragma data_alignment=4
#endif
ALIGNED_HEAD static uint8_t g_string_lang_id[USBD_MSC_SIZ_STRING_LANGID] ALIGNED_TAIL =
{
USBD_MSC_SIZ_STRING_LANGID,
USB_DESCIPTOR_TYPE_STRING,
0x09,
0x04,
};
/**
* @brief usb string serial
*/
#if defined ( __ICCARM__ ) /* iar compiler */
#pragma data_alignment=4
#endif
ALIGNED_HEAD static uint8_t g_string_serial[USBD_MSC_SIZ_STRING_SERIAL] ALIGNED_TAIL =
{
USBD_MSC_SIZ_STRING_SERIAL,
USB_DESCIPTOR_TYPE_STRING,
};
/* device descriptor */
static usbd_desc_t device_descriptor =
{
USB_DEVICE_DESC_LEN,
g_usbd_descriptor
};
/* config descriptor */
static usbd_desc_t config_descriptor =
{
USBD_MSC_CONFIG_DESC_SIZE,
g_usbd_configuration
};
/* langid descriptor */
static usbd_desc_t langid_descriptor =
{
USBD_MSC_SIZ_STRING_LANGID,
g_string_lang_id
};
/* serial descriptor */
static usbd_desc_t serial_descriptor =
{
USBD_MSC_SIZ_STRING_SERIAL,
g_string_serial
};
static usbd_desc_t vp_desc;
/**
* @brief standard usb unicode convert
* @param string: source string
* @param unicode_buf: unicode buffer
* @retval length
*/
static 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
*/
static usbd_desc_t *get_device_descriptor(void)
{
return &device_descriptor;
}
/**
* @brief get device qualifier
* @param none
* @retval usbd_desc
*/
static usbd_desc_t * get_device_qualifier(void)
{
return NULL;
}
/**
* @brief get config descriptor
* @param none
* @retval usbd_desc
*/
static usbd_desc_t *get_device_configuration(void)
{
return &config_descriptor;
}
/**
* @brief get other speed descriptor
* @param none
* @retval usbd_desc
*/
static usbd_desc_t *get_device_other_speed(void)
{
return NULL;
}
/**
* @brief get lang id descriptor
* @param none
* @retval usbd_desc
*/
static usbd_desc_t *get_device_lang_id(void)
{
return &langid_descriptor;
}
/**
* @brief get manufacturer descriptor
* @param none
* @retval usbd_desc
*/
static usbd_desc_t *get_device_manufacturer_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_MSC_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
*/
static usbd_desc_t *get_device_product_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_MSC_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
*/
static usbd_desc_t *get_device_serial_string(void)
{
get_serial_num();
return &serial_descriptor;
}
/**
* @brief get interface descriptor
* @param none
* @retval usbd_desc
*/
static usbd_desc_t *get_device_interface_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_MSC_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
*/
static usbd_desc_t *get_device_config_string(void)
{
vp_desc.length = usbd_unicode_convert((uint8_t *)USBD_MSC_DESC_CONFIGURATION_STRING, g_usbd_desc_buffer);
vp_desc.descriptor = g_usbd_desc_buffer;
return &vp_desc;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
@@ -0,0 +1,85 @@
/**
**************************************************************************
* @file msc_desc.h
* @version v2.1.0
* @date 2022-08-16
* @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 AT32F435_437_middlewares_usbd_class
* @{
*/
/** @addtogroup USB_msc_desc
* @{
*/
/** @defgroup USB_msc_desc_definition
* @{
*/
#define MSC_BCD_NUM 0x0110
#define USBD_MSC_VENDOR_ID 0x2E3C
#define USBD_MSC_PRODUCT_ID 0x5720
#define USBD_MSC_CONFIG_DESC_SIZE 32
#define USBD_MSC_SIZ_STRING_LANGID 4
#define USBD_MSC_SIZ_STRING_SERIAL 0x1A
#define USBD_MSC_DESC_MANUFACTURER_STRING "Artery"
#define USBD_MSC_DESC_PRODUCT_STRING "AT32 Mass Storage"
#define USBD_MSC_DESC_CONFIGURATION_STRING "Mass Storage Config"
#define USBD_MSC_DESC_INTERFACE_STRING "Mass Storage Interface"
#define MCU_ID1 (0x1FFFF7E8)
#define MCU_ID2 (0x1FFFF7EC)
#define MCU_ID3 (0x1FFFF7F0)
extern usbd_desc_handler msc_desc_handler;
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,532 @@
/**
**************************************************************************
* @file usbh_cdc_class.c
* @version v2.1.0
* @date 2022-08-16
* @brief usb host 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 "usbh_cdc_class.h"
#include "usb_conf.h"
#include "usbh_core.h"
#include "usbh_ctrl.h"
#include "string.h"
/** @addtogroup AT32F435_437_middlewares_usbh_class
* @{
*/
/** @defgroup usbh_cdc_class
* @brief usb host class cdc demo
* @{
*/
/** @defgroup usbh_cdc_class_private_functions
* @{
*/
static usb_sts_type uhost_init_handler(void *uhost);
static usb_sts_type uhost_reset_handler(void *uhost);
static usb_sts_type uhost_request_handler(void *uhost);
static usb_sts_type uhost_process_handler(void *uhost);
static usb_sts_type get_linecoding(usbh_core_type *uhost, cdc_line_coding_type *linecoding);
static usb_sts_type set_linecoding(usbh_core_type *uhost, cdc_line_coding_type *linecoding);
static void cdc_process_transmission(usbh_core_type *uhost);
static void cdc_process_reception(usbh_core_type *uhost);
usbh_cdc_type usbh_cdc;
usbh_class_handler_type uhost_cdc_class_handler =
{
uhost_init_handler,
uhost_reset_handler,
uhost_request_handler,
uhost_process_handler,
&usbh_cdc
};
/**
* @brief usb host class init handler
* @param uhost: to the structure of usbh_core_type
* @retval status: usb_sts_type status
*/
static usb_sts_type uhost_init_handler(void *uhost)
{
usbh_core_type *puhost = (usbh_core_type *)uhost;
usb_sts_type status = USB_OK;
uint8_t if_x;
usbh_cdc_type *pcdc = &usbh_cdc;
puhost->class_handler->pdata = &usbh_cdc;
memset((void *)pcdc, 0, sizeof(usbh_cdc_type));
if_x = usbh_find_interface(puhost, USB_CLASS_CODE_CDC, ABSTRACT_CONTROL_MODEL, COMMON_AT_COMMAND);
if(if_x == 0xFF)
{
USBH_DEBUG("cannot find the interface for communication interface class!");
return USB_NOT_SUPPORT;
}
else
{
if(if_x < puhost->dev.cfg_desc.cfg.bNumInterfaces)
{
USBH_DEBUG ("switching to interface (#%d)", if_x);
USBH_DEBUG ("class : %xh", puhost->dev.cfg_desc.interface[if_x].interface.bInterfaceClass);
USBH_DEBUG ("subclass : %xh", puhost->dev.cfg_desc.interface[if_x].interface.bInterfaceSubClass );
USBH_DEBUG ("protocol : %xh", puhost->dev.cfg_desc.interface[if_x].interface.bInterfaceProtocol );
if(puhost->dev.cfg_desc.interface[if_x].interface.bInterfaceClass == COMMUNICATION_INTERFACE_CLASS_CODE)
{
USBH_DEBUG("CDC device!");
}
}
else
{
USBH_DEBUG ("cannot select this interface.");
}
/* collect the notification endpoint address and length */
if(puhost->dev.cfg_desc.interface[if_x].endpoint[0].bEndpointAddress & 0x80)
{
pcdc->common_interface.notif_endpoint = puhost->dev.cfg_desc.interface[if_x].endpoint[0].bEndpointAddress;
pcdc->common_interface.notif_endpoint_size = puhost->dev.cfg_desc.interface[if_x].endpoint[0].wMaxPacketSize;
}
/* allocate the length for host channel number in */
pcdc->common_interface.notif_channel = usbh_alloc_channel(puhost, pcdc->common_interface.notif_endpoint);
/* enable channel */
usbh_hc_open(puhost,
pcdc->common_interface.notif_channel,
pcdc->common_interface.notif_endpoint,
puhost->dev.address,
EPT_INT_TYPE,
pcdc->common_interface.notif_endpoint_size,
puhost->dev.speed);
usbh_set_toggle(puhost, pcdc->common_interface.notif_channel, 0);
if_x = usbh_find_interface(puhost, DATA_INTERFACE_CLASS_CODE, RESERVED, NO_CLASS_SPECIFIC_PROTOCOL_CODE);
if(if_x == 0xFF)
{
USBH_DEBUG("cannot find the interface for data interface class!");
return USB_NOT_SUPPORT;
}
else
{
/* collect the class specific endpoint address and length */
if(puhost->dev.cfg_desc.interface[if_x].endpoint[0].bEndpointAddress & 0x80)
{
pcdc->data_interface.in_endpoint = puhost->dev.cfg_desc.interface[if_x].endpoint[0].bEndpointAddress;
pcdc->data_interface.in_endpoint_size = puhost->dev.cfg_desc.interface[if_x].endpoint[0].wMaxPacketSize;
}
else
{
pcdc->data_interface.out_endpoint = puhost->dev.cfg_desc.interface[if_x].endpoint[0].bEndpointAddress;
pcdc->data_interface.out_endpoint_size = puhost->dev.cfg_desc.interface[if_x].endpoint[0].wMaxPacketSize;
}
if(puhost->dev.cfg_desc.interface[if_x].endpoint[1].bEndpointAddress & 0x80)
{
pcdc->data_interface.in_endpoint = puhost->dev.cfg_desc.interface[if_x].endpoint[1].bEndpointAddress;
pcdc->data_interface.in_endpoint_size = puhost->dev.cfg_desc.interface[if_x].endpoint[1].wMaxPacketSize;
}
else
{
pcdc->data_interface.out_endpoint = puhost->dev.cfg_desc.interface[if_x].endpoint[1].bEndpointAddress;
pcdc->data_interface.out_endpoint_size = puhost->dev.cfg_desc.interface[if_x].endpoint[1].wMaxPacketSize;
}
/* allocate the length for host channel number in */
pcdc->data_interface.in_channel = usbh_alloc_channel(puhost, pcdc->data_interface.in_endpoint);
/* allocate the length for host channel number out */
pcdc->data_interface.out_channel = usbh_alloc_channel(puhost, pcdc->data_interface.out_endpoint);
/* enable in channel */
usbh_hc_open(puhost,
pcdc->data_interface.in_channel,
pcdc->data_interface.in_endpoint,
puhost->dev.address,
EPT_BULK_TYPE,
pcdc->data_interface.in_endpoint_size,
puhost->dev.speed);
/* enable out channel */
usbh_hc_open(puhost,
pcdc->data_interface.out_channel,
pcdc->data_interface.out_endpoint,
puhost->dev.address,
EPT_BULK_TYPE,
pcdc->data_interface.out_endpoint_size,
puhost->dev.speed);
usbh_set_toggle(puhost, pcdc->data_interface.in_channel, 0);
usbh_set_toggle(puhost, pcdc->data_interface.out_channel, 0);
pcdc->state = CDC_IDLE_STATE;
}
}
return status;
}
/**
* @brief usb host class reset handler
* @param uhost: to the structure of usbh_core_type
* @retval status: usb_sts_type status
*/
static usb_sts_type uhost_reset_handler(void *uhost)
{
usbh_core_type *puhost = (usbh_core_type *)uhost;
usbh_cdc_type *pcdc = (usbh_cdc_type *)puhost->class_handler->pdata;
usb_sts_type status = USB_OK;
if(puhost->class_handler->pdata == NULL)
{
return status;
}
if(pcdc->common_interface.notif_channel != 0 )
{
usbh_free_channel(puhost, pcdc->common_interface.notif_channel);
usbh_ch_disable(puhost, pcdc->common_interface.notif_channel);
pcdc->common_interface.notif_channel = 0;
}
if(pcdc->data_interface.in_channel != 0 )
{
usbh_free_channel(puhost, pcdc->data_interface.in_channel);
usbh_ch_disable(puhost, pcdc->data_interface.in_channel);
pcdc->data_interface.in_channel = 0;
}
if(pcdc->data_interface.out_channel != 0 )
{
usbh_free_channel(puhost, pcdc->data_interface.out_channel);
usbh_ch_disable(puhost, pcdc->data_interface.out_channel);
pcdc->data_interface.out_channel = 0;
}
return status;
}
/**
* @brief usb host cdc class request handler
* @param uhost: to the structure of usbh_core_type
* @retval status: usb_sts_type status
*/
static usb_sts_type uhost_request_handler(void *uhost)
{
usbh_core_type *puhost = (usbh_core_type *)uhost;
usbh_cdc_type *pcdc = (usbh_cdc_type *)puhost->class_handler->pdata;
usb_sts_type status = USB_WAIT;
status = get_linecoding(uhost, &pcdc->linecoding);
return status;
}
/**
* @brief usb host cdc get linecoding handler
* @param uhost: to the structure of usbh_core_type
* @param linecoding: pointer to the structure of cdc_line_coding_type
* @retval status: usb_sts_type status
*/
static usb_sts_type get_linecoding(usbh_core_type *uhost, cdc_line_coding_type *linecoding)
{
usbh_core_type *puhost = (usbh_core_type *)uhost;
usb_sts_type status = USB_WAIT;
if(puhost->ctrl.state == CONTROL_IDLE )
{
uhost->ctrl.setup.bmRequestType = USB_DIR_D2H | USB_REQ_TYPE_CLASS | USB_REQ_RECIPIENT_INTERFACE;
uhost->ctrl.setup.bRequest = CDC_GET_LINE_CODING;
uhost->ctrl.setup.wValue = 0;
uhost->ctrl.setup.wLength = LINE_CODING_STRUCTURE_SIZE;
uhost->ctrl.setup.wIndex = 0;
usbh_ctrl_request(uhost, linecoding->array, LINE_CODING_STRUCTURE_SIZE);
}
else
{
status = usbh_ctrl_result_check(puhost, CONTROL_IDLE, ENUM_IDLE);
if(status == USB_OK || status == USB_NOT_SUPPORT)
{
status = USB_OK;
}
}
return status;
}
/**
* @brief usb host cdc set linecoding handler
* @param uhost: to the structure of usbh_core_type
* @param linecoding: pointer to the structure of cdc_line_coding_type
* @retval status: usb_sts_type status
*/
static usb_sts_type set_linecoding(usbh_core_type *uhost, cdc_line_coding_type *linecoding)
{
usbh_core_type *puhost = (usbh_core_type *)uhost;
usb_sts_type status = USB_WAIT;
if(puhost->ctrl.state == CONTROL_IDLE )
{
uhost->ctrl.setup.bmRequestType = USB_DIR_H2D | USB_REQ_TYPE_CLASS | USB_REQ_RECIPIENT_INTERFACE;
uhost->ctrl.setup.bRequest = CDC_SET_LINE_CODING;
uhost->ctrl.setup.wValue = 0;
uhost->ctrl.setup.wLength = LINE_CODING_STRUCTURE_SIZE;
uhost->ctrl.setup.wIndex = 0;
status = usbh_ctrl_request(uhost, linecoding->array, LINE_CODING_STRUCTURE_SIZE);
}
else
{
status = usbh_ctrl_result_check(puhost, CONTROL_IDLE, ENUM_IDLE);
if(status == USB_OK || status == USB_NOT_SUPPORT)
{
status = USB_OK;
}
}
return status;
}
/**
* @brief usb host class process handler
* @param uhost: to the structure of usbh_core_type
* @retval status: usb_sts_type status
*/
static usb_sts_type uhost_process_handler(void *uhost)
{
usbh_core_type *puhost = (usbh_core_type *)uhost;
usbh_cdc_type *pcdc = (usbh_cdc_type *)puhost->class_handler->pdata;
usb_sts_type status;
switch(pcdc->state)
{
case CDC_IDLE_STATE:
status = USB_OK;
break;
case CDC_SET_LINE_CODING_STATE:
status = set_linecoding(puhost, pcdc->puserlinecoding);
if(status == USB_OK)
pcdc->state = CDC_GET_LAST_LINE_CODING_STATE;
break;
case CDC_GET_LAST_LINE_CODING_STATE:
status = get_linecoding(puhost, &(pcdc->linecoding));
if(status == USB_OK)
{
pcdc->state = CDC_IDLE_STATE;
if((pcdc->linecoding.line_coding_b.char_format == pcdc->puserlinecoding->line_coding_b.char_format)&&
(pcdc->linecoding.line_coding_b.data_bits == pcdc->puserlinecoding->line_coding_b.data_bits)&&
(pcdc->linecoding.line_coding_b.parity_type == pcdc->puserlinecoding->line_coding_b.parity_type)&&
(pcdc->linecoding.line_coding_b.data_baudrate == pcdc->puserlinecoding->line_coding_b.data_baudrate))
{
/* line coding changed */
}
}
pcdc->state = CDC_IDLE_STATE;
break;
case CDC_TRANSFER_DATA:
cdc_process_transmission(puhost);
cdc_process_reception(puhost);
break;
case CDC_ERROR_STATE:
status = usbh_clear_ept_feature(puhost, 0, pcdc->common_interface.notif_channel);
if(status == USB_OK)
pcdc->state = CDC_IDLE_STATE;
break;
default:
break;
}
return status;
}
/**
* @brief usb host cdc class process transmission handler
* @param uhost: to the structure of usbh_core_type
* @retval status: usb_sts_type status
*/
static void cdc_process_transmission(usbh_core_type *uhost)
{
usbh_core_type *puhost = (usbh_core_type *)uhost;
usbh_cdc_type *pcdc = (usbh_cdc_type *)puhost->class_handler->pdata;
switch(pcdc->data_tx_state)
{
case CDC_SEND_DATA:
if(pcdc->tx_len > pcdc->data_interface.out_endpoint_size)
{
usbh_bulk_send(puhost, pcdc->data_interface.out_channel, (uint8_t*)pcdc->tx_data, pcdc->data_interface.out_endpoint_size);
}
else
{
usbh_bulk_send(puhost, pcdc->data_interface.out_channel, (uint8_t*)pcdc->tx_data, pcdc->tx_len);
}
pcdc->data_tx_state = CDC_SEND_DATA_WAIT;
break;
case CDC_SEND_DATA_WAIT:
if(uhost->urb_state[pcdc->data_interface.out_channel] == URB_DONE)
{
if(pcdc->tx_len > pcdc->data_interface.out_endpoint_size)
{
pcdc->tx_len -= pcdc->data_interface.out_endpoint_size;
pcdc->tx_data += pcdc->data_interface.out_endpoint_size;
pcdc->data_tx_state = CDC_SEND_DATA;
}
else
{
pcdc->tx_len = 0;
pcdc->data_tx_state = CDC_IDLE;
cdc_transmit_complete(uhost);
}
}
else if( uhost->urb_state[pcdc->data_interface.out_channel] == URB_NOTREADY)
{
pcdc->data_tx_state = CDC_SEND_DATA;
}
break;
default:
break;
}
}
/**
* @brief usb host cdc class start transmission handler
* @param uhost: to the structure of usbh_core_type
* @param data: tx data pointer
* @param len: tx data len
* @retval status: usb_sts_type status
*/
void cdc_start_transmission(usbh_core_type *uhost, uint8_t *data, uint32_t len)
{
usbh_core_type *puhost = (usbh_core_type *)uhost;
usbh_cdc_type *pcdc = (usbh_cdc_type *)puhost->class_handler->pdata;
if(pcdc->data_tx_state == CDC_IDLE)
{
pcdc->data_tx_state = CDC_SEND_DATA;
pcdc->state = CDC_TRANSFER_DATA;
pcdc->tx_data = data;
pcdc->tx_len = len;
}
}
/**
* @brief usb host cdc class transmit complete
* @param uhost: to the structure of usbh_core_type
* @retval status: usb_sts_type status
*/
__weak void cdc_transmit_complete(usbh_core_type *uhost)
{
}
/**
* @brief usb host cdc class process reception handler
* @param uhost: to the structure of usbh_core_type
* @retval status: usb_sts_type status
*/
static void cdc_process_reception(usbh_core_type *uhost)
{
usbh_core_type *puhost = (usbh_core_type *)uhost;
usbh_cdc_type *pcdc = (usbh_cdc_type *)puhost->class_handler->pdata;
uint32_t len = 0;
switch(pcdc->data_rx_state)
{
case CDC_RECEIVE_DATA:
usbh_bulk_recv(puhost, pcdc->data_interface.in_channel, (uint8_t*)pcdc->rx_data, pcdc->data_interface.in_endpoint_size);
pcdc->data_rx_state = CDC_RECEIVE_DATA_WAIT;
break;
case CDC_RECEIVE_DATA_WAIT:
if(uhost->urb_state[pcdc->data_interface.in_channel] == URB_DONE)
{
len = uhost->hch[pcdc->data_interface.in_channel].trans_count;
if(pcdc->rx_len > len && len > pcdc->data_interface.in_endpoint_size)
{
pcdc->rx_len -= len;
pcdc->rx_data += len;
pcdc->data_rx_state = CDC_RECEIVE_DATA;
}
else
{
pcdc->data_rx_state = CDC_IDLE;
cdc_receive_complete(uhost);
}
}
break;
default:
break;
}
}
/**
* @brief usb host cdc class start reception handler
* @param uhost: to the structure of usbh_core_type
* @param data: receive data pointer
* @param len: receive data len
* @retval status: usb_sts_type status
*/
void cdc_start_reception(usbh_core_type *uhost, uint8_t *data, uint32_t len)
{
usbh_core_type *puhost = (usbh_core_type *)uhost;
usbh_cdc_type *pcdc = (usbh_cdc_type *)puhost->class_handler->pdata;
if(pcdc->data_rx_state == CDC_IDLE)
{
pcdc->data_rx_state = CDC_RECEIVE_DATA;
pcdc->state = CDC_TRANSFER_DATA;
pcdc->rx_data = data;
pcdc->rx_len = len;
}
}
/**
* @brief usb host cdc class reception complete
* @param uhost: to the structure of usbh_core_type
* @retval status: usb_sts_type status
*/
__weak void cdc_receive_complete(usbh_core_type *uhost)
{
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
@@ -0,0 +1,284 @@
/**
**************************************************************************
* @file usbh_cdc_class.h
* @version v2.1.0
* @date 2022-08-16
* @brief usb host cdc class 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 __USBH_MSC_CLASS_H
#define __USBH_MSC_CLASS_H
#ifdef __cplusplus
extern "C" {
#endif
#include "usbh_core.h"
#include "usb_conf.h"
/** @addtogroup AT32F435_437_middlewares_usbh_class
* @{
*/
/** @addtogroup USBH_cdc_class
* @{
*/
/** @defgroup USBH_cdc_class_definition
* @{
*/
/*Communication Class codes*/
#define USB_CDC_CLASS 0x02
#define COMMUNICATION_INTERFACE_CLASS_CODE 0x02
/*Data Interface Class Codes*/
#define DATA_INTERFACE_CLASS_CODE 0x0A
/*Communication sub class codes*/
#define RESERVED 0x00
#define DIRECT_LINE_CONTROL_MODEL 0x01
#define ABSTRACT_CONTROL_MODEL 0x02
#define TELEPHONE_CONTROL_MODEL 0x03
#define MULTICHANNEL_CONTROL_MODEL 0x04
#define CAPI_CONTROL_MODEL 0x05
#define ETHERNET_NETWORKING_CONTROL_MODEL 0x06
#define ATM_NETWORKING_CONTROL_MODEL 0x07
/*Communication Interface Class Control Protocol Codes*/
#define NO_CLASS_SPECIFIC_PROTOCOL_CODE 0x00
#define COMMON_AT_COMMAND 0x01
#define VENDOR_SPECIFIC 0xFF
#define CS_INTERFACE 0x24
#define CDC_PAGE_SIZE_64 0x40
/*Class-Specific Request Codes*/
#define CDC_SEND_ENCAPSULATED_COMMAND 0x00
#define CDC_GET_ENCAPSULATED_RESPONSE 0x01
#define CDC_SET_COMM_FEATURE 0x02
#define CDC_GET_COMM_FEATURE 0x03
#define CDC_CLEAR_COMM_FEATURE 0x04
#define CDC_SET_AUX_LINE_STATE 0x10
#define CDC_SET_HOOK_STATE 0x11
#define CDC_PULSE_SETUP 0x12
#define CDC_SEND_PULSE 0x13
#define CDC_SET_PULSE_TIME 0x14
#define CDC_RING_AUX_JACK 0x15
#define CDC_SET_LINE_CODING 0x20
#define CDC_GET_LINE_CODING 0x21
#define CDC_SET_CONTROL_LINE_STATE 0x22
#define CDC_SEND_BREAK 0x23
#define CDC_SET_RINGER_PARMS 0x30
#define CDC_GET_RINGER_PARMS 0x31
#define CDC_SET_OPERATION_PARMS 0x32
#define CDC_GET_OPERATION_PARMS 0x33
#define CDC_SET_LINE_PARMS 0x34
#define CDC_GET_LINE_PARMS 0x35
#define CDC_DIAL_DIGITS 0x36
#define CDC_SET_UNIT_PARAMETER 0x37
#define CDC_GET_UNIT_PARAMETER 0x38
#define CDC_CLEAR_UNIT_PARAMETER 0x39
#define CDC_GET_PROFILE 0x3A
#define CDC_SET_ETHERNET_MULTICAST_FILTERS 0x40
#define CDC_SET_ETHERNET_POWER_MANAGEMENT_PATTERN FILTER 0x41
#define CDC_GET_ETHERNET_POWER_MANAGEMENT_PATTERN FILTER 0x42
#define CDC_SET_ETHERNET_PACKET_FILTER 0x43
#define CDC_GET_ETHERNET_STATISTIC 0x44
#define CDC_SET_ATM_DATA_FORMAT 0x50
#define CDC_GET_ATM_DEVICE_STATISTICS 0x51
#define CDC_SET_ATM_DEFAULT_VC 0x52
#define CDC_GET_ATM_VC_STATISTICS 0x53
/* wValue for SetControlLineState*/
#define CDC_ACTIVATE_CARRIER_SIGNAL_RTS 0x0002
#define CDC_DEACTIVATE_CARRIER_SIGNAL_RTS 0x0000
#define CDC_ACTIVATE_SIGNAL_DTR 0x0001
#define CDC_DEACTIVATE_SIGNAL_DTR 0x0000
#define LINE_CODING_STRUCTURE_SIZE 0x07
/* states for cdc state machine */
typedef enum
{
CDC_IDLE = 0x0,
CDC_SEND_DATA = 0x1,
CDC_SEND_DATA_WAIT = 0x2,
CDC_RECEIVE_DATA = 0x3,
CDC_RECEIVE_DATA_WAIT = 0x4,
} cdc_data_state_type;
typedef enum
{
CDC_IDLE_STATE = 0x0,
CDC_SET_LINE_CODING_STATE = 0x1,
CDC_GET_LAST_LINE_CODING_STATE = 0x2,
CDC_TRANSFER_DATA = 0x3,
CDC_ERROR_STATE = 0x4,
} cdc_state_type;
/*line coding structure*/
typedef union _cdc_line_coding_structure
{
uint8_t array[LINE_CODING_STRUCTURE_SIZE];
struct
{
uint32_t data_baudrate; /*data terminal rate, in bits per second*/
uint8_t char_format; /* Stop bits
0 - 1 Stop bit
1 - 1.5 Stop bits
2 - 2 Stop bits*/
uint8_t parity_type; /* parity
0 - none
1 - odd
2 - even
3 - mark
4 - space*/
uint8_t data_bits; /* data bits (5, 6, 7, 8 or 16). */
}line_coding_b;
} cdc_line_coding_type;
/* header functional descriptor */
typedef struct _functional_descriptor_header
{
uint8_t bfunctionlength; /* size of this descriptor. */
uint8_t bdescriptortype; /* cs_interface (0x24) */
uint8_t bdescriptorsubtype; /* header functional descriptor subtype as */
uint16_t bcdcdc; /* usb class definitions for communication
devices specification release number in
binary-coded decimal. */
} cdc_headerfuncdesc_type;
/* call management functional descriptor */
typedef struct _callmgmt_functional_descriptor
{
uint8_t blength; /* size of this functional descriptor, in bytes */
uint8_t bdescriptortype; /* cs_interface (0x24) */
uint8_t bdescriptorsubtype; /* call management functional descriptor subtype */
uint8_t bmcapabilities; /* bmcapabilities: d0+d1 */
uint8_t bdatainterface; /* bdatainterface: 1 */
} cdc_callmgmtfuncdesc_type;
/* abstract control management functional descriptor */
typedef struct _abstractcntrlmgmt_functional_descriptor
{
uint8_t blength; /* size of this functional descriptor, in bytes */
uint8_t bdescriptortype; /* cs_interface (0x24) */
uint8_t bdescriptorsubtype; /* abstract control management functional
descriptor subtype */
uint8_t bmcapabilities; /* the capabilities that this configuration supports */
} cdc_abstcntrlmgmtfuncdesc_type;
/* union functional descriptor */
typedef struct _union_functional_descriptor
{
uint8_t blength; /* size of this functional descriptor, in bytes */
uint8_t bdescriptortype; /* cs_interface (0x24) */
uint8_t bdescriptorsubtype; /* union functional descriptor subtype */
uint8_t bmasterinterface; /* the interface number of the communication or
data class interface */
uint8_t bslaveinterface0; /* interface number of first slave */
} cdc_unionfuncdesc_type;
typedef struct _usbh_cdcinterfacedesc
{
cdc_headerfuncdesc_type cdc_headerfuncdesc;
cdc_callmgmtfuncdesc_type cdc_callmgmtfuncdesc;
cdc_abstcntrlmgmtfuncdesc_type cdc_abstcntrlmgmtfuncdesc;
cdc_unionfuncdesc_type cdc_unionfuncdesc;
} cdc_interfacedesc_type;
/* structure for cdc process */
typedef struct
{
uint8_t notif_channel;
uint8_t notif_endpoint;
uint8_t buff[8];
uint16_t notif_endpoint_size;
} cdc_common_interface_type;
typedef struct
{
uint8_t in_channel;
uint8_t out_channel;
uint8_t out_endpoint;
uint8_t in_endpoint;
uint8_t buff[8];
uint16_t out_endpoint_size;
uint16_t in_endpoint_size;
} cdc_data_interface_type;
/**
* @brief usb cdc struct
*/
typedef struct
{
cdc_common_interface_type common_interface;
cdc_data_interface_type data_interface;
cdc_interfacedesc_type cdc_desc;
cdc_line_coding_type linecoding;
cdc_line_coding_type *puserlinecoding;
cdc_state_type state;
cdc_data_state_type data_tx_state;
cdc_data_state_type data_rx_state;
uint8_t *rx_data;
uint8_t *tx_data;
uint32_t rx_len;
uint32_t tx_len;
}usbh_cdc_type;
extern usbh_class_handler_type uhost_cdc_class_handler;
extern usbh_cdc_type usbh_cdc;
void cdc_start_transmission(usbh_core_type *phost, uint8_t *data, uint32_t len);
void cdc_start_reception(usbh_core_type *uhost, uint8_t *data, uint32_t len);
__weak void cdc_transmit_complete(usbh_core_type *uhost);
__weak void cdc_receive_complete(usbh_core_type *uhost);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,453 @@
/**
**************************************************************************
* @file usbh_hid_class.c
* @version v2.1.0
* @date 2022-08-16
* @brief usb host 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 "usbh_hid_class.h"
#include "usb_conf.h"
#include "usbh_core.h"
#include "usbh_ctrl.h"
#include "usbh_hid_mouse.h"
#include "usbh_hid_keyboard.h"
/** @addtogroup AT32F435_437_middlewares_usbh_class
* @{
*/
/** @defgroup USBH_hid_class
* @brief usb host class hid demo
* @{
*/
/** @defgroup USBH_hid_class_private_functions
* @{
*/
static usb_sts_type uhost_init_handler(void *uhost);
static usb_sts_type uhost_reset_handler(void *uhost);
static usb_sts_type uhost_request_handler(void *uhost);
static usb_sts_type uhost_process_handler(void *uhost);
usbh_hid_type usbh_hid;
usbh_class_handler_type uhost_hid_class_handler =
{
uhost_init_handler,
uhost_reset_handler,
uhost_request_handler,
uhost_process_handler,
&usbh_hid
};
/**
* @brief usb host class init handler
* @param uhost: to the structure of usbh_core_type
* @retval status: usb_sts_type status
*/
static usb_sts_type uhost_init_handler(void *uhost)
{
usbh_core_type *puhost = (usbh_core_type *)uhost;
usb_sts_type status = USB_OK;
uint8_t hidx, eptidx = 0;
usbh_hid_type *phid = &usbh_hid;
puhost->class_handler->pdata = &usbh_hid;
/* get hid interface */
hidx = usbh_find_interface(puhost, USB_CLASS_CODE_HID, 0x01, 0xFF);
if(hidx == 0xFF)
{
USBH_DEBUG("Unsupport Device!");
return USB_NOT_SUPPORT;
}
/* get hid protocol */
phid->protocol = puhost->dev.cfg_desc.interface[hidx].interface.bInterfaceProtocol;
if(phid->protocol == USB_HID_MOUSE_PROTOCOL_CODE)
{
USBH_DEBUG("Mouse Device!");
}
else if(phid->protocol == USB_HID_KEYBOARD_PROTOCOL_CODE)
{
USBH_DEBUG("Keyboard Device!");
}
for(eptidx = 0; eptidx < puhost->dev.cfg_desc.interface[hidx].interface.bNumEndpoints; eptidx ++)
{
if(puhost->dev.cfg_desc.interface[hidx].endpoint[eptidx].bEndpointAddress & 0x80)
{
/* find interface out endpoint information */
phid->eptin = puhost->dev.cfg_desc.interface[hidx].endpoint[eptidx].bEndpointAddress;
phid->in_maxpacket = puhost->dev.cfg_desc.interface[hidx].endpoint[eptidx].wMaxPacketSize;
phid->in_poll = puhost->dev.cfg_desc.interface[hidx].endpoint[eptidx].bInterval;
phid->chin = usbh_alloc_channel(puhost, phid->eptin);
/* enable channel */
usbh_hc_open(puhost, phid->chin,phid->eptin,
puhost->dev.address, EPT_INT_TYPE,
phid->in_maxpacket,
puhost->dev.speed);
usbh_set_toggle(puhost, phid->chin, 0);
}
else
{
/* get interface out endpoint information */
phid->eptout = puhost->dev.cfg_desc.interface[hidx].endpoint[eptidx].bEndpointAddress;
phid->out_maxpacket = puhost->dev.cfg_desc.interface[hidx].endpoint[eptidx].wMaxPacketSize;
phid->out_poll = puhost->dev.cfg_desc.interface[hidx].endpoint[eptidx].bInterval;
phid->chout = usbh_alloc_channel(puhost, usbh_hid.eptout);
/* enable channel */
usbh_hc_open(puhost, phid->chout, phid->eptout,
puhost->dev.address, EPT_INT_TYPE,
phid->out_maxpacket,
puhost->dev.speed);
usbh_set_toggle(puhost, phid->chout, 0);
}
}
phid->ctrl_state = USB_HID_STATE_IDLE;
return status;
}
/**
* @brief usb host class reset handler
* @param uhost: to the structure of usbh_core_type
* @retval status: usb_sts_type status
*/
static usb_sts_type uhost_reset_handler(void *uhost)
{
usbh_core_type *puhost = (usbh_core_type *)uhost;
usbh_hid_type *phid = (usbh_hid_type *)puhost->class_handler->pdata;
usb_sts_type status = USB_OK;
if(puhost->class_handler->pdata == NULL)
{
return status;
}
if(phid->chin != 0)
{
/* free in channel */
usbh_free_channel(puhost, phid->chin);
usbh_ch_disable(puhost, phid->chin);
phid->chin = 0;
}
if(phid->chout != 0)
{
/* free out channel */
usbh_free_channel(puhost, phid->chout);
usbh_ch_disable(puhost, phid->chout);
phid->chout = 0;
}
return status;
}
/**
* @brief usb host hid class get descriptor
* @param uhost: to the structure of usbh_core_type
* @param length: descriptor length
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_hid_get_desc(void *uhost, uint16_t length)
{
usbh_core_type *puhost = (usbh_core_type *)uhost;
usbh_hid_type *phid = (usbh_hid_type *)puhost->class_handler->pdata;
usb_sts_type status = USB_WAIT;
uint8_t bm_req;
uint16_t wvalue;
if(puhost->ctrl.state == CONTROL_IDLE)
{
bm_req = USB_REQ_RECIPIENT_INTERFACE | USB_REQ_TYPE_STANDARD;
wvalue = (0x21 << 8) & 0xFF00;
usbh_get_descriptor(puhost, length, bm_req,
wvalue, puhost->rx_buffer);
}
else
{
if(usbh_ctrl_result_check(puhost, CONTROL_IDLE, ENUM_IDLE) == USB_OK)
{
phid->hid_desc.bLength = puhost->rx_buffer[0];
phid->hid_desc.bDescriptorType = puhost->rx_buffer[1];
phid->hid_desc.bcdHID = SWAPBYTE(puhost->rx_buffer+2);
phid->hid_desc.bCountryCode = puhost->rx_buffer[4];
phid->hid_desc.bNumDescriptors = puhost->rx_buffer[5];
phid->hid_desc.bReportDescriptorType = puhost->rx_buffer[6];
phid->hid_desc.wItemLength = SWAPBYTE(puhost->rx_buffer+7);
status = USB_OK;
}
}
return status;
}
/**
* @brief usb host hid class get report
* @param uhost: to the structure of usbh_core_type
* @param length: reprot length
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_hid_get_report(void *uhost, uint16_t length)
{
usbh_core_type *puhost = (usbh_core_type *)uhost;
usb_sts_type status = USB_WAIT;
uint8_t bm_req;
uint16_t wvalue;
if(puhost->ctrl.state == CONTROL_IDLE)
{
bm_req = USB_REQ_RECIPIENT_INTERFACE | USB_REQ_TYPE_STANDARD;
wvalue = (0x22 << 8) & 0xFF00;
usbh_get_descriptor(puhost, length, bm_req,
wvalue, puhost->rx_buffer);
}
else
{
if(usbh_ctrl_result_check(puhost, CONTROL_IDLE, ENUM_IDLE) == USB_OK)
{
status = USB_OK;
}
}
return status;
}
/**
* @brief usb host hid class set idle
* @param uhost: to the structure of usbh_core_type
* @param id: id
* @param dr: dr
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_hid_set_idle(void *uhost, uint8_t id, uint8_t dr)
{
usbh_core_type *puhost = (usbh_core_type *)uhost;
usb_sts_type status = USB_WAIT;
if(puhost->ctrl.state == CONTROL_IDLE)
{
puhost->ctrl.setup.bmRequestType = USB_DIR_H2D | USB_REQ_RECIPIENT_INTERFACE | USB_REQ_TYPE_CLASS;
puhost->ctrl.setup.bRequest = USB_HID_SET_IDLE;
puhost->ctrl.setup.wValue = (dr << 8) | id;
puhost->ctrl.setup.wIndex = 0;
puhost->ctrl.setup.wLength = 0;
usbh_ctrl_request(puhost, 0, 0);
}
else
{
status = usbh_ctrl_result_check(puhost, CONTROL_IDLE, ENUM_IDLE);
if(status == USB_OK || status == USB_NOT_SUPPORT)
{
status = USB_OK;
}
}
return status;
}
/**
* @brief usb host hid class set protocol
* @param uhost: to the structure of usbh_core_type
* @param protocol: portocol number
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_hid_set_protocol(void *uhost, uint8_t protocol)
{
usbh_core_type *puhost = (usbh_core_type *)uhost;
usb_sts_type status = USB_WAIT;
if(puhost->ctrl.state == CONTROL_IDLE)
{
puhost->ctrl.setup.bmRequestType = USB_DIR_H2D | USB_REQ_RECIPIENT_INTERFACE | USB_REQ_TYPE_CLASS;
puhost->ctrl.setup.bRequest = USB_HID_SET_PROTOCOL;
puhost->ctrl.setup.wValue = protocol;
puhost->ctrl.setup.wIndex = 0;
puhost->ctrl.setup.wLength = 0;
usbh_ctrl_request(puhost, 0, 0);
}
else
{
status = usbh_ctrl_result_check(puhost, CONTROL_IDLE, ENUM_IDLE);
if(status == USB_OK || status == USB_NOT_SUPPORT)
{
status = USB_OK;
}
}
return status;
}
/**
* @brief usb host clear feature
* @param uhost: to the structure of usbh_core_type
* @param ept_num: endpoint number
* @param hc_num: channel number
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_clear_endpoint_feature(usbh_core_type *uhost, uint8_t ept_num, uint8_t hc_num)
{
usb_sts_type status = USB_WAIT;
usbh_core_type *puhost = (usbh_core_type *)uhost;
uint8_t bm_req;
bm_req = USB_REQ_RECIPIENT_ENDPOINT | USB_REQ_TYPE_STANDARD;
if(puhost->ctrl.state == CONTROL_IDLE)
{
puhost->ctrl.setup.bmRequestType = USB_DIR_H2D | bm_req;
puhost->ctrl.setup.bRequest = USB_STD_REQ_CLEAR_FEATURE;
puhost->ctrl.setup.wValue = 0x00;
puhost->ctrl.setup.wLength = 0;
puhost->ctrl.setup.wIndex = ept_num;
if((ept_num & 0x80) == USB_DIR_D2H)
{
puhost->hch[hc_num].toggle_in = 0;
}
else
{
puhost->hch[hc_num].toggle_out = 0;
}
status = usbh_ctrl_request(puhost, 0, 0);
}
if(usbh_ctrl_result_check(puhost, CONTROL_IDLE, ENUM_IDLE) == USB_OK)
{
status = USB_OK;
}
return status;
}
/**
* @brief usb host hid class request handler
* @param uhost: to the structure of usbh_core_type
* @retval status: usb_sts_type status
*/
static usb_sts_type uhost_request_handler(void *uhost)
{
usb_sts_type status = USB_WAIT;
usbh_core_type *puhost = (usbh_core_type *)uhost;
usbh_hid_type *phid = (usbh_hid_type *)puhost->class_handler->pdata;
switch(phid->ctrl_state)
{
case USB_HID_STATE_IDLE:
phid->ctrl_state = USB_HID_STATE_GET_DESC;
break;
case USB_HID_STATE_GET_DESC:
if(usbh_hid_get_desc(puhost, 9) == USB_OK)
{
phid->ctrl_state = USB_HID_STATE_GET_REPORT;
}
break;
case USB_HID_STATE_GET_REPORT:
if(usbh_hid_get_report(puhost, phid->hid_desc.wItemLength) == USB_OK)
{
phid->ctrl_state = USB_HID_STATE_SET_IDLE;
}
break;
case USB_HID_STATE_SET_IDLE:
if(usbh_hid_set_idle(puhost, 0, 0) == USB_OK)
{
phid->ctrl_state = USB_HID_STATE_SET_PROTOCOL;
}
break;
case USB_HID_STATE_SET_PROTOCOL:
if(usbh_hid_set_protocol(puhost, 0) == USB_OK)
{
phid->ctrl_state = USB_HID_STATE_COMPLETE;
}
break;
case USB_HID_STATE_COMPLETE:
phid->state = USB_HID_INIT;
status = USB_OK;
break;
default:
break;
}
return status;
}
/**
* @brief usb host hid class process handler
* @param uhost: to the structure of usbh_core_type
* @retval status: usb_sts_type status
*/
static usb_sts_type uhost_process_handler(void *uhost)
{
usbh_core_type *puhost = (usbh_core_type *)uhost;
usbh_hid_type *phid = (usbh_hid_type *)puhost->class_handler->pdata;
urb_sts_type urb_status;
switch(phid->state)
{
case USB_HID_INIT:
phid->state = USB_HID_GET;
break;
case USB_HID_GET:
usbh_interrupt_recv(puhost, phid->chin, (uint8_t *)phid->buffer, phid->in_maxpacket);
phid->state = USB_HID_POLL;
phid->poll_timer = usbh_get_frame(puhost->usb_reg);
break;
case USB_HID_POLL:
if((usbh_get_frame(puhost->usb_reg) - phid->poll_timer) >= phid->in_poll )
{
phid->state = USB_HID_GET;
}
else
{
urb_status = usbh_get_urb_status(puhost, phid->chin);
if(urb_status == URB_DONE)
{
puhost->urb_state[phid->chin] = URB_IDLE;
if(phid->protocol == USB_HID_MOUSE_PROTOCOL_CODE)
{
usbh_hid_mouse_decode((uint8_t *)phid->buffer);
}
else if(phid->protocol == USB_HID_KEYBOARD_PROTOCOL_CODE)
{
usbh_hid_keyboard_decode((uint8_t *)phid->buffer);
}
}
else if(urb_status == URB_STALL)
{
if(usbh_clear_endpoint_feature(puhost, phid->eptin, phid->chin) == USB_OK)
{
phid->state = USB_HID_GET;
}
}
}
break;
default:
break;
}
return USB_OK;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
@@ -0,0 +1,148 @@
/**
**************************************************************************
* @file usbh_hid_class.h
* @version v2.1.0
* @date 2022-08-16
* @brief usb host hid class 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 __USBH_HID_CLASS_H
#define __USBH_HID_CLASS_H
#ifdef __cplusplus
extern "C" {
#endif
#include "usbh_core.h"
#include "usb_conf.h"
/** @addtogroup AT32F435_437_middlewares_usbh_class
* @{
*/
/** @addtogroup USBH_hid_class
* @{
*/
/** @defgroup USBH_hid_class_definition
* @{
*/
/**
* @brief usb hid protocol code
*/
#define USB_HID_NONE_PROTOCOL_CODE 0x00
#define USB_HID_KEYBOARD_PROTOCOL_CODE 0x01
#define USB_HID_MOUSE_PROTOCOL_CODE 0x02
/**
* @brief usb hid request code
*/
#define USB_HID_GET_REPORT 0x01
#define USB_HID_GET_IDLE 0x02
#define USB_HID_GET_PROTOCOL 0x03
#define USB_HID_SET_REPORT 0x09
#define USB_HID_SET_IDLE 0x0A
#define USB_HID_SET_PROTOCOL 0x0B
/**
* @brief usb hid request state
*/
typedef enum
{
USB_HID_STATE_IDLE,
USB_HID_STATE_GET_DESC,
USB_HID_STATE_GET_REPORT,
USB_HID_STATE_SET_IDLE,
USB_HID_STATE_SET_PROTOCOL,
USB_HID_STATE_COMPLETE,
}usb_hid_ctrl_state_type;
/**
* @brief usb hid process state
*/
typedef enum
{
USB_HID_INIT,
USB_HID_GET,
USB_HID_SEND,
USB_HID_POLL,
USB_HID_BUSY,
USB_HID_ERROR,
}usb_hid_state_type;
/**
* @brief usb hid descriptor type
*/
typedef struct
{
uint8_t bLength;
uint8_t bDescriptorType;
uint16_t bcdHID;
uint8_t bCountryCode;
uint8_t bNumDescriptors;
uint8_t bReportDescriptorType;
uint16_t wItemLength;
}usb_hid_desc_type;
/**
* @brief usb hid struct
*/
typedef struct
{
uint8_t chin;
uint8_t eptin;
uint16_t in_maxpacket;
uint8_t in_poll;
uint8_t chout;
uint8_t eptout;
uint16_t out_maxpacket;
uint8_t out_poll;
uint8_t protocol;
usb_hid_desc_type hid_desc;
usb_hid_ctrl_state_type ctrl_state;
usb_hid_state_type state;
uint16_t poll_timer;
uint32_t buffer[16];
}usbh_hid_type;
extern usbh_class_handler_type uhost_hid_class_handler;
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,255 @@
/**
**************************************************************************
* @file usbh_hid_keyboard.c
* @version v2.1.0
* @date 2022-08-16
* @brief usb host hid keyboard 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 "usbh_hid_keyboard.h"
/** @addtogroup AT32F435_437_middlewares_usbh_class
* @{
*/
/** @defgroup USBH_hid_class_keyboard
* @brief usb host class hid keyboard
* @{
*/
/** @defgroup USBH_hid_class_keyboard_private_functions
* @{
*/
static const uint8_t hid_keyboard_codes[] =
{
0, 0, 0, 0, 31, 50, 48, 33,
19, 34, 35, 36, 24, 37, 38, 39,
52, 51, 25, 26, 17, 20, 32, 21,
23, 49, 18, 47, 22, 46, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11,
43, 110, 15, 16, 61, 12, 13, 27,
28, 29, 42, 40, 41, 1, 53, 54,
55, 30, 112, 113, 114, 115, 116, 117,
118, 119, 120, 121, 122, 123, 124, 125,
126, 75, 80, 85, 76, 81, 86, 89,
79, 84, 83, 90, 95, 100, 105, 106,
108, 93, 98, 103, 92, 97, 102, 91,
96, 101, 99, 104, 45, 129, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 107, 0, 56,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
58, 44, 60, 127, 64, 57, 62, 128,
};
#ifdef QWERTY_KEYBOARD
static const int8_t hid_keyboard_key[] = {
'\0', '`', '1', '2', '3', '4', '5', '6',
'7', '8', '9', '0', '-', '=', '\0', '\r',
'\t', 'q', 'w', 'e', 'r', 't', 'y', 'u',
'i', 'o', 'p', '[', ']', '\\',
'\0', 'a', 's', 'd', 'f', 'g', 'h', 'j',
'k', 'l', ';', '\'', '\0', '\n',
'\0', '\0', 'z', 'x', 'c', 'v', 'b', 'n',
'm', ',', '.', '/', '\0', '\0',
'\0', '\0', '\0', ' ', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\r', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '7', '4', '1',
'\0', '/', '8', '5', '2',
'0', '*', '9', '6', '3',
'.', '-', '+', '\0', '\n', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0'
};
static const int8_t hid_keyboard_key_shift[] = {
'\0', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
'_', '+', '\0', '\0', '\0', 'Q', 'W', 'E', 'R', 'T', 'Y', 'U',
'I', 'O', 'P', '{', '}', '|', '\0', 'A', 'S', 'D', 'F', 'G',
'H', 'J', 'K', 'L', ':', '"', '\0', '\n', '\0', '\0', 'Z', 'X',
'C', 'V', 'B', 'N', 'M', '<', '>', '?', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'
};
#else
static const int8_t hid_keyboard_key[] = {
'\0', '`', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
'-', '=', '\0', '\r', '\t', 'a', 'z', 'e', 'r', 't', 'y', 'u',
'i', 'o', 'p', '[', ']', '\\', '\0', 'q', 's', 'd', 'f', 'g',
'h', 'j', 'k', 'l', 'm', '\0', '\0', '\n', '\0', '\0', 'w', 'x',
'c', 'v', 'b', 'n', ',', ';', ':', '!', '\0', '\0', '\0', '\0',
'\0', ' ', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\r', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '7', '4', '1','\0', '/',
'8', '5', '2', '0', '*', '9', '6', '3', '.', '-', '+', '\0',
'\n', '\0', '\0', '\0', '\0', '\0', '\0','\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'
};
static const int8_t hid_keyboard_key_shift[] = {
'\0', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_',
'+', '\0', '\0', '\0', 'A', 'Z', 'E', 'R', 'T', 'Y', 'U', 'I', 'O',
'P', '{', '}', '*', '\0', 'Q', 'S', 'D', 'F', 'G', 'H', 'J', 'K',
'L', 'M', '%', '\0', '\n', '\0', '\0', 'W', 'X', 'C', 'V', 'B', 'N',
'?', '.', '/', '\0', '\0', '\0','\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'
};
#endif
/**
* @brief usb host hid keyboard decode
* @param data: keyboard data
* @retval none
*/
void usbh_hid_keyboard_decode(uint8_t *data)
{
static uint8_t shift;
static uint8_t keys[KEYBOARD_MAX_NB_PRESSED];
static uint8_t keys_n[KEYBOARD_MAX_NB_PRESSED];
static uint8_t keys_l[KEYBOARD_MAX_NB_PRESSED];
static uint8_t key_newest;
static uint8_t nb_keys;
static uint8_t nb_keys_n;
static uint8_t nb_keys_l;
uint8_t idx;
uint8_t idy;
uint8_t err;
uint8_t out;
nb_keys = 0;
nb_keys_n = 0;
nb_keys_l = 0;
key_newest = 0;
if(data[0] == KEYBOARD_LEFT_SHIFT || data[0] == KEYBOARD_RIGHT_SHIFT)
{
shift = TRUE;
}
else
{
shift = FALSE;
}
err = FALSE;
for(idx = 2; idx < 2 + KEYBOARD_MAX_NB_PRESSED; idx ++)
{
if((data[idx] == 0x01) ||
(data[idx] == 0x02) ||
(data[idx] == 0x03))
{
err = TRUE;
}
}
if(err == TRUE)
{
return;
}
nb_keys = 0;
nb_keys_n = 0;
for(idx = 2; idx < 2 + KEYBOARD_MAX_NB_PRESSED; idx ++)
{
if(data[idx] != 0)
{
keys[nb_keys] = data[idx];
nb_keys ++;
for(idy = 0; idy < nb_keys_l; idy ++)
{
if(data[idx] == keys_l[idy])
{
break;
}
}
if(idy == nb_keys_l)
{
keys_n[nb_keys_n] = data[idx];
nb_keys_n ++;
}
}
}
if(nb_keys_n == 1)
{
key_newest = keys_n[0];
if(shift == TRUE)
{
out = hid_keyboard_key_shift[hid_keyboard_codes[key_newest]];
}
else
{
out = hid_keyboard_key[hid_keyboard_codes[key_newest]];
}
/* callback user handler */
USBH_DEBUG("%c", out);
}
else
{
key_newest = 0;
}
nb_keys_l = nb_keys;
for(idx = 0; idx < KEYBOARD_MAX_NB_PRESSED; idx ++)
{
keys_l[idx] = keys[idx];
}
return;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
@@ -0,0 +1,84 @@
/**
**************************************************************************
* @file usbh_hid_keyboard.h
* @version v2.1.0
* @date 2022-08-16
* @brief usb host 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 __USBH_HID_KEYBOARD_H
#define __USBH_HID_KEYBOARD_H
#ifdef __cplusplus
extern "C" {
#endif
#include "usb_conf.h"
/** @addtogroup AT32F435_437_middlewares_usbh_class
* @{
*/
/** @addtogroup USBH_hid_class_keyboard
* @{
*/
/** @defgroup USBH_hid_class_keyboard_definition
* @{
*/
/**
* @brief usb keyboard option code
*/
#define KEYBOARD_LEFT_CTRL 0x01
#define KEYBOARD_LEFT_SHIFT 0x02
#define KEYBOARD_LEFT_ALT 0x04
#define KEYBOARD_LEFT_GUI 0x08
#define KEYBOARD_RIGHT_CTRL 0x10
#define KEYBOARD_RIGHT_SHIFT 0x20
#define KEYBOARD_RIGHT_ALT 0x40
#define KEYBOARD_RIGHT_GUI 0x80
#define KEYBOARD_MAX_NB_PRESSED 6
#ifndef AZERTY_KEYBOARD
#define QWERTY_KEYBOARD
#endif
void usbh_hid_keyboard_decode(uint8_t *data);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,188 @@
/**
**************************************************************************
* @file usbh_hid_mouse.c
* @version v2.1.0
* @date 2022-08-16
* @brief usb host hid mouse 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 "usbh_hid_mouse.h"
/** @addtogroup AT32F435_437_middlewares_usbh_class
* @{
*/
/** @defgroup USBH_hid_class_mouse
* @brief usb host class hid mouse
* @{
*/
/** @defgroup USBH_hid_class_mouse_private_functions
* @{
*/
usb_hid_mouse_type hid_mouse;
uint16_t x_pos = 0, y_pos = 0;
/**
* @brief usb host hid position
* @param x: x position
* @param y: y position
* @retval none
*/
void usbh_hid_mouse_position(uint8_t x, uint8_t y)
{
if((x != 0) || (y != 0))
{
x_pos += x / 2;
y_pos += y / 2;
if(x_pos > MOUSE_WINDOW_WIDTH - 12)
{
x_pos = MOUSE_WINDOW_WIDTH - 12;
}
if(y_pos > MOUSE_WINDOW_HEIGHT - 12)
{
y_pos = MOUSE_WINDOW_HEIGHT - 12;
}
if(x_pos < 2)
{
x_pos = 2;
}
if(y_pos < 2)
{
y_pos = 2;
}
USBH_DEBUG("Moving Mouse");
}
}
/**
* @brief usb host hid button press
* @param button: button id
* @retval none
*/
void usbh_hid_mouse_button_press(uint8_t button)
{
switch(button)
{
case MOUSE_BUTTON_LEFT:
/* left button */
USBH_DEBUG("Left Button Pressed ");
break;
case MOUSE_BUTTON_RIGHT:
USBH_DEBUG("Right Button Pressed ");
/* left button */
break;
case MOUSE_BUTTON_MIDDLE:
USBH_DEBUG("Middle Button Pressed ");
/* middle button */
break;
}
}
/**
* @brief usb host hid button release
* @param button: button id
* @retval none
*/
void usbh_hid_mouse_button_release(uint8_t button)
{
switch(button)
{
case MOUSE_BUTTON_LEFT:
/* left button */
USBH_DEBUG("Left Button Released ");
break;
case MOUSE_BUTTON_RIGHT:
/* left button */
USBH_DEBUG("Right Button Released ");
break;
case MOUSE_BUTTON_MIDDLE:
/* middle button */
USBH_DEBUG("Middle Button Released ");
break;
}
}
/**
* @brief usb host hid mouse process
* @param mouse: mouse data type
* @retval none
*/
void usbh_hid_mouse_process(usb_hid_mouse_type *mouse)
{
uint8_t idx = 1;
static uint8_t b_state[3] = {0};
if((mouse->x != 0) && (mouse->y != 0))
{
usbh_hid_mouse_position(mouse->x, mouse->y);
}
for(idx = 0; idx < 3; idx ++)
{
if(mouse->button & 1 << idx)
{
if(b_state[idx] == 0)
{
usbh_hid_mouse_button_press(idx);
b_state[idx] = 1;
}
}
else
{
if(b_state[idx] == 1)
{
usbh_hid_mouse_button_release(idx);
b_state[idx] = 0;
}
}
}
}
/**
* @brief usb host hid mouse decode
* @param mouse_data: mouse data
* @retval none
*/
void usbh_hid_mouse_decode(uint8_t *mouse_data)
{
hid_mouse.button = mouse_data[0];
hid_mouse.x = mouse_data[1];
hid_mouse.y = mouse_data[2];
hid_mouse.z = mouse_data[3];
usbh_hid_mouse_process(&hid_mouse);
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
@@ -0,0 +1,93 @@
/**
**************************************************************************
* @file usbh_hid_mouse.h
* @version v2.1.0
* @date 2022-08-16
* @brief usb host 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 __USBH_HID_MOUSE_H
#define __USBH_HID_MOUSE_H
#ifdef __cplusplus
extern "C" {
#endif
#include "usb_conf.h"
/** @addtogroup AT32F435_437_middlewares_usbh_class
* @{
*/
/** @addtogroup USBH_hid_class_mouse
* @{
*/
/** @defgroup USBH_hid_class_mouse_definition
* @{
*/
/**
* @brief usb hid mouse x y
*/
#define MOUSE_WINDOW_X 100
#define MOUSE_WINDOW_Y 220
#define MOUSE_WINDOW_HEIGHT 90
#define MOUSE_WINDOW_WIDTH 128
/**
* @brief usb hid mouse button
*/
#define MOUSE_BUTTON_LEFT 0x00
#define MOUSE_BUTTON_RIGHT 0x01
#define MOUSE_BUTTON_MIDDLE 0x02
/**
* @brief usb hid mouse type
*/
typedef struct
{
uint8_t button;
uint8_t x;
uint8_t y;
uint8_t z;
}usb_hid_mouse_type;
void usbh_hid_mouse_decode(uint8_t *mouse_data);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,671 @@
/**
**************************************************************************
* @file usbh_msc_bot_scsi.c
* @version v2.1.0
* @date 2022-08-16
* @brief usb host msc bulk-only transfer and scsi 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 "usbh_msc_bot_scsi.h"
#include "usbh_msc_class.h"
#include "usb_conf.h"
#include "usbh_core.h"
#include "usbh_ctrl.h"
/** @addtogroup AT32F435_437_middlewares_usbh_class
* @{
*/
/** @defgroup USBH_msc_bot_scsi_class
* @brief usb host class msc bot scsi
* @{
*/
/** @defgroup USBH_msc_bot_scsi_class_private_functions
* @{
*/
static usb_sts_type usbh_bot_cbw(msc_bot_cbw_type *cbw, uint32_t data_length, uint8_t cmd_len, uint8_t flag);
static usb_sts_type usbh_cmd_inquiry(msc_bot_trans_type *bot_trans, uint8_t *cmd, uint8_t lun);
static usb_sts_type usbh_cmd_capacity10(msc_bot_trans_type *bot_trans, uint8_t *cmd, uint8_t lun);
static usb_sts_type usbh_cmd_test_unit_ready(msc_bot_trans_type *bot_trans, uint8_t *cmd, uint8_t lun);
static usb_sts_type usbh_cmd_requset_sense(msc_bot_trans_type *bot_trans, uint8_t *cmd, uint8_t lun);
static usb_sts_type usbh_cmd_write(msc_bot_trans_type *bot_trans, uint8_t *cmd, uint8_t lun,
uint32_t data_len, uint32_t address, uint8_t *buffer);
static usb_sts_type usbh_cmd_read(msc_bot_trans_type *bot_trans, uint8_t *cmd, uint8_t lun,
uint32_t data_len, uint32_t address, uint8_t *buffer);
/**
* @brief usb host bulk-only cbw
* @param cbw: to the structure of msc_bot_cbw_type
* @param data_length: data length
* @param cmd_len: command len
* @param flag: cbw flag
* @retval status: usb_sts_type status
*/
static usb_sts_type usbh_bot_cbw(msc_bot_cbw_type *cbw, uint32_t data_length, uint8_t cmd_len, uint8_t flag)
{
uint8_t i_index;
cbw->dCBWSignature = MSC_CBW_SIGNATURE;
cbw->dCBWTag = MSC_CBW_TAG;
cbw->dCBWDataTransferLength = data_length;
cbw->bmCBWFlags = flag;
cbw->bCBWCBLength = cmd_len;
for(i_index = 0; i_index < MSC_CBW_CB_LEN; i_index ++)
{
cbw->CBWCB[i_index] = 0x00;
}
return USB_OK;
}
/**
* @brief usb host msc command inquiry
* @param bot_trans: to the structure of msc_bot_trans_type
* @param cmd: command buffer
* @param lun: logical unit number
* @retval status: usb_sts_type status
*/
static usb_sts_type usbh_cmd_inquiry(msc_bot_trans_type *bot_trans, uint8_t *cmd, uint8_t lun)
{
usbh_msc_type *msc_struct = (usbh_msc_type *)bot_trans->msc_struct;
cmd[0] = MSC_OPCODE_INQUIRY;
cmd[1] = lun << 5;
cmd[4] = MSC_INQUIRY_DATA_LEN;
bot_trans->data = (uint8_t *)&msc_struct->l_unit_n[lun].inquiry;
return USB_OK;
}
/**
* @brief usb host msc command capacity10
* @param bot_trans: to the structure of msc_bot_trans_type
* @param cmd: command buffer
* @param lun: logical unit number
* @retval status: usb_sts_type status
*/
static usb_sts_type usbh_cmd_capacity10(msc_bot_trans_type *bot_trans, uint8_t *cmd, uint8_t lun)
{
usbh_msc_type *msc_struct = (usbh_msc_type *)bot_trans->msc_struct;
cmd[0] = MSC_OPCODE_CAPACITY;
cmd[1] = lun << 5;
msc_struct->bot_trans.data = (uint8_t *)bot_trans->buffer;
return USB_OK;
}
/**
* @brief usb host msc command test unit ready
* @param bot_trans: to the structure of msc_bot_trans_type
* @param cmd: command buffer
* @param lun: logical unit number
* @retval status: usb_sts_type status
*/
static usb_sts_type usbh_cmd_test_unit_ready(msc_bot_trans_type *bot_trans, uint8_t *cmd, uint8_t lun)
{
cmd[0] = MSC_OPCODE_TEST_UNIT_READY;
cmd[1] = lun << 5;
return USB_OK;
}
/**
* @brief usb host msc command request sense
* @param bot_trans: to the structure of msc_bot_trans_type
* @param cmd: command buffer
* @param lun: logical unit number
* @retval status: usb_sts_type status
*/
static usb_sts_type usbh_cmd_requset_sense(msc_bot_trans_type *bot_trans, uint8_t *cmd, uint8_t lun)
{
cmd[0] = MSC_OPCODE_REQUEST_SENSE;
cmd[1] = lun << 5;
cmd[4] = MSC_REQUEST_SENSE_DATA_LEN;
bot_trans->data = bot_trans->buffer;
return USB_OK;
}
/**
* @brief usb host msc command write
* @param bot_trans: to the structure of msc_bot_trans_type
* @param cmd: command buffer
* @param lun: logical unit number
* @param data_len: transfer data length
* @param address: logical block address
* @param buffer: transfer data buffer
* @retval status: usb_sts_type status
*/
static usb_sts_type usbh_cmd_write(msc_bot_trans_type *bot_trans, uint8_t *cmd, uint8_t lun,
uint32_t data_len, uint32_t address, uint8_t *buffer)
{
cmd[0] = MSC_OPCODE_WRITE10;
cmd[1] = lun << 5;
cmd[2] = (uint8_t)(address >> 24);
cmd[3] = (uint8_t)(address >> 16);
cmd[4] = (uint8_t)(address >> 8);
cmd[5] = (uint8_t)(address & 0xFF);
cmd[7] = data_len >> 8;
cmd[8] = data_len;
bot_trans->data = buffer;
return USB_OK;
}
/**
* @brief usb host msc command read
* @param bot_trans: to the structure of msc_bot_trans_type
* @param cmd: command buffer
* @param lun: logical unit number
* @param data_len: transfer data length
* @param address: logical block address
* @param buffer: transfer data buffer
* @retval status: usb_sts_type status
*/
static usb_sts_type usbh_cmd_read(msc_bot_trans_type *bot_trans, uint8_t *cmd, uint8_t lun,
uint32_t data_len, uint32_t address, uint8_t *buffer)
{
cmd[0] = MSC_OPCODE_READ10;
cmd[1] = lun << 5;
cmd[2] = (uint8_t)(address >> 24);
cmd[3] = (uint8_t)(address >> 16);
cmd[4] = (uint8_t)(address >> 8);
cmd[5] = (uint8_t)(address & 0xFF);
cmd[7] = data_len >> 8;
cmd[8] = data_len;
bot_trans->data = buffer;
return USB_OK;
}
/**
* @brief usb host csw check
* @param cbw: to the structure of msc_bot_cbw_type
* @param csw: to the structure of msc_bot_csw_type
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_check_csw(void *uhost, msc_bot_cbw_type *cbw, msc_bot_csw_type *csw)
{
usb_sts_type status = USB_FAIL;
if(csw->dCBWSignature == MSC_CSW_SIGNATURE)
{
if(csw->dCBWTag == cbw->dCBWTag)
{
if(csw->bCSWStatus == 0)
{
status = USB_OK;
}
}
}
return status;
}
/**
* @brief usb host msc bulk-only request
* @param uhost: to the structure of usbh_core_type
* @param bot_trans: to the structure of msc_bot_trans_type
* @retval status: usb_sts_type status
*/
usb_sts_type usb_bot_request(void *uhost, msc_bot_trans_type *bot_trans)
{
usb_sts_type status = USB_WAIT;
urb_sts_type urb_status;
usb_sts_type clr_status;
usbh_core_type *puhost = (usbh_core_type *)uhost;
usbh_msc_type *msc_struct = (usbh_msc_type *)bot_trans->msc_struct;
switch(bot_trans->bot_state)
{
case BOT_STATE_SEND_CBW:
usbh_bulk_send(puhost, msc_struct->chout, (uint8_t *)(&bot_trans->cbw), MSC_CBW_LEN);
bot_trans->bot_state = BOT_STATE_SEND_CBW_WAIT;
break;
case BOT_STATE_SEND_CBW_WAIT:
urb_status = usbh_get_urb_status(puhost, msc_struct->chout);
if(urb_status == URB_DONE)
{
if(bot_trans->cbw.dCBWDataTransferLength != 0)
{
if(bot_trans->cbw.bmCBWFlags == MSC_CBW_FLAG_IN)
{
bot_trans->bot_state = BOT_STATE_DATA_IN;
}
else
{
bot_trans->bot_state = BOT_STATE_DATA_OUT;
}
}
else
{
bot_trans->bot_state = BOT_STATE_RECV_CSW;
}
}
else if(urb_status == URB_NOTREADY)
{
bot_trans->bot_state = BOT_STATE_SEND_CBW;
}
else if(urb_status == URB_STALL)
{
bot_trans->bot_state = BOT_STATE_ERROR_OUT;
}
break;
case BOT_STATE_DATA_IN:
usbh_bulk_recv(puhost, msc_struct->chin, bot_trans->data,
msc_struct->in_maxpacket);
bot_trans->bot_state = BOT_STATE_DATA_IN_WAIT;
break;
case BOT_STATE_DATA_IN_WAIT:
urb_status = usbh_get_urb_status(puhost, msc_struct->chin);
if(urb_status == URB_DONE)
{
if(bot_trans->cbw.dCBWDataTransferLength > msc_struct->in_maxpacket)
{
bot_trans->data += msc_struct->in_maxpacket;
bot_trans->cbw.dCBWDataTransferLength -= msc_struct->in_maxpacket;
}
else
{
bot_trans->cbw.dCBWDataTransferLength = 0;
}
if(bot_trans->cbw.dCBWDataTransferLength > 0)
{
usbh_bulk_recv(puhost, msc_struct->chin, bot_trans->data,
msc_struct->in_maxpacket);
}
else
{
bot_trans->bot_state = BOT_STATE_RECV_CSW;
}
}
else if(urb_status == URB_STALL)
{
bot_trans->bot_state = BOT_STATE_ERROR_IN;
}
break;
case BOT_STATE_DATA_OUT:
usbh_bulk_send(puhost, msc_struct->chout, bot_trans->data, msc_struct->out_maxpacket);
bot_trans->bot_state = BOT_STATE_DATA_OUT_WAIT;
break;
case BOT_STATE_DATA_OUT_WAIT:
urb_status = usbh_get_urb_status(puhost, msc_struct->chout);
if(urb_status == URB_DONE)
{
if(bot_trans->cbw.dCBWDataTransferLength > msc_struct->out_maxpacket)
{
bot_trans->data += msc_struct->out_maxpacket;
bot_trans->cbw.dCBWDataTransferLength -= msc_struct->out_maxpacket;
}
else
{
bot_trans->cbw.dCBWDataTransferLength = 0;
}
if(bot_trans->cbw.dCBWDataTransferLength > 0)
{
usbh_bulk_send(puhost, msc_struct->chout, bot_trans->data, msc_struct->out_maxpacket);
}
else
{
bot_trans->bot_state = BOT_STATE_RECV_CSW;
}
}
else if(urb_status == URB_NOTREADY)
{
bot_trans->bot_state = BOT_STATE_DATA_OUT;
}
else if(urb_status == URB_STALL)
{
bot_trans->bot_state = BOT_STATE_ERROR_OUT;
}
break;
case BOT_STATE_RECV_CSW:
usbh_bulk_recv(puhost, msc_struct->chin, (uint8_t *)&bot_trans->csw,
MSC_CSW_LEN);
bot_trans->bot_state = BOT_STATE_RECV_CSW_WAIT;
break;
case BOT_STATE_RECV_CSW_WAIT:
urb_status = usbh_get_urb_status(puhost, msc_struct->chin);
if(urb_status == URB_DONE)
{
bot_trans->bot_state = BOT_STATE_SEND_CBW;
bot_trans->cmd_state = CMD_STATE_SEND;
status = usbh_check_csw(uhost, &bot_trans->cbw, &bot_trans->csw);
}
else if(urb_status == URB_STALL)
{
bot_trans->bot_state = BOT_STATE_ERROR_IN;
}
break;
case BOT_STATE_ERROR_IN:
clr_status = usbh_clear_ept_feature(puhost, msc_struct->eptin, msc_struct->chin);
if(clr_status == USB_OK)
{
bot_trans->bot_state = BOT_STATE_RECV_CSW;
usbh_set_toggle(puhost, msc_struct->chin, 0);
}
break;
case BOT_STATE_ERROR_OUT:
clr_status = usbh_clear_ept_feature(puhost, msc_struct->eptout, msc_struct->chout);
if(clr_status == USB_OK)
{
usbh_set_toggle(puhost, msc_struct->chout, 1 - puhost->hch[msc_struct->chout].toggle_out);
usbh_set_toggle(puhost, msc_struct->chin, 0);
bot_trans->bot_state = BOT_STATE_ERROR_IN;
}
break;
case BOT_STATE_COMPLETE:
break;
default:
break;
}
return status;
}
/**
* @brief usb host msc bulk-only get inquiry request
* @param uhost: to the structure of usbh_core_type
* @param bot_trans: to the structure of msc_bot_trans_type
* @param lun: logical unit number
* @param inquiry: to the structure of msc_scsi_data_inquiry
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_msc_bot_scsi_get_inquiry(void *uhost, msc_bot_trans_type *bot_trans,
uint8_t lun, msc_scsi_data_inquiry *inquiry)
{
usb_sts_type status = USB_WAIT;
switch(bot_trans->cmd_state)
{
case CMD_STATE_SEND:
usbh_bot_cbw(&bot_trans->cbw, MSC_INQUIRY_DATA_LEN, MSC_INQUIRY_CMD_LEN, MSC_CBW_FLAG_IN);
bot_trans->cbw.bCBWLUN = lun;
usbh_cmd_inquiry(bot_trans, bot_trans->cbw.CBWCB, lun);
bot_trans->cmd_state = CMD_STATE_WAIT;
bot_trans->bot_state = BOT_STATE_SEND_CBW;
break;
case CMD_STATE_WAIT:
status = usb_bot_request(uhost, bot_trans);
if(status == USB_OK)
{
bot_trans->cmd_state = CMD_STATE_SEND;
}
if(status == USB_FAIL)
{
bot_trans->cmd_state = CMD_STATE_SEND;
}
break;
default:
break;
}
return status;
}
/**
* @brief usb host msc bulk-only capacity request
* @param uhost: to the structure of usbh_core_type
* @param bot_trans: to the structure of msc_bot_trans_type
* @param lun: logical unit number
* @param capacity: to the structure of msc_scsi_data_capacity
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_msc_bot_scsi_capacity(void *uhost, msc_bot_trans_type *bot_trans,
uint8_t lun, msc_scsi_data_capacity *capacity)
{
usb_sts_type status = USB_WAIT;
switch(bot_trans->cmd_state)
{
case CMD_STATE_SEND:
usbh_bot_cbw(&bot_trans->cbw, MSC_CAPACITY10_DATA_LEN, MSC_CAPACITY10_CMD_LEN, MSC_CBW_FLAG_IN);
bot_trans->cbw.bCBWLUN = lun;
usbh_cmd_capacity10(bot_trans, bot_trans->cbw.CBWCB, lun);
bot_trans->cmd_state = CMD_STATE_WAIT;
bot_trans->bot_state = BOT_STATE_SEND_CBW;
break;
case CMD_STATE_WAIT:
status = usb_bot_request(uhost, bot_trans);
if(status == USB_OK)
{
capacity->blk_nbr = bot_trans->buffer[3] | bot_trans->buffer[2] << 8 |
bot_trans->buffer[1] << 16 | bot_trans->buffer[0] << 24;
capacity->blk_size = bot_trans->buffer[7] | bot_trans->buffer[6] << 8 ;
bot_trans->cmd_state = CMD_STATE_SEND;
}
if(status == USB_FAIL)
{
bot_trans->cmd_state = CMD_STATE_SEND;
}
break;
default:
break;
}
return status;
}
/**
* @brief usb host msc bulk-only tet unit ready request
* @param uhost: to the structure of usbh_core_type
* @param bot_trans: to the structure of msc_bot_trans_type
* @param lun: logical unit number
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_msc_bot_scsi_test_unit_ready(void *uhost, msc_bot_trans_type *bot_trans,
uint8_t lun)
{
usb_sts_type status = USB_WAIT;
switch(bot_trans->cmd_state)
{
case CMD_STATE_SEND:
usbh_bot_cbw(&bot_trans->cbw, MSC_TEST_UNIT_READY_DATA_LEN,
MSC_TEST_UNIT_READY_CMD_LEN, MSC_CBW_FLAG_OUT);
bot_trans->cbw.bCBWLUN = lun;
usbh_cmd_test_unit_ready(bot_trans, bot_trans->cbw.CBWCB, lun);
bot_trans->cmd_state = CMD_STATE_WAIT;
bot_trans->bot_state = BOT_STATE_SEND_CBW;
break;
case CMD_STATE_WAIT:
status = usb_bot_request(uhost, bot_trans);
if(status == USB_OK)
{
bot_trans->cmd_state = CMD_STATE_SEND;
}
if(status == USB_FAIL)
{
bot_trans->cmd_state = CMD_STATE_SEND;
}
break;
default:
break;
}
return status;
}
/**
* @brief usb host msc bulk-only request sense request
* @param uhost: to the structure of usbh_core_type
* @param bot_trans: to the structure of msc_bot_trans_type
* @param lun: logical unit number
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_msc_bot_scsi_request_sense(void *uhost, msc_bot_trans_type *bot_trans,
uint8_t lun)
{
usb_sts_type status = USB_WAIT;
switch(bot_trans->cmd_state)
{
case CMD_STATE_SEND:
usbh_bot_cbw(&bot_trans->cbw, MSC_REQUEST_SENSE_DATA_LEN,
MSC_REQUEST_SENSE_CMD_LEN, MSC_CBW_FLAG_IN);
bot_trans->cbw.bCBWLUN = lun;
usbh_cmd_requset_sense(bot_trans, bot_trans->cbw.CBWCB, lun);
bot_trans->cmd_state = CMD_STATE_WAIT;
bot_trans->bot_state = BOT_STATE_SEND_CBW;
break;
case CMD_STATE_WAIT:
status = usb_bot_request(uhost, bot_trans);
if(status == USB_OK)
{
bot_trans->cmd_state = CMD_STATE_SEND;
}
if(status == USB_FAIL)
{
bot_trans->cmd_state = CMD_STATE_SEND;
}
break;
default:
break;
}
return status;
}
/**
* @brief usb host msc bulk-only write request
* @param uhost: to the structure of usbh_core_type
* @param bot_trans: to the structure of msc_bot_trans_type
* @param address: logical block address
* @param write_data: write data buffer
* @param write_len: write data length
* @param lun: logical unit number
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_msc_bot_scsi_write(void *uhost, msc_bot_trans_type *bot_trans,
uint32_t address, uint8_t *write_data,
uint32_t write_len, uint8_t lun)
{
usb_sts_type status = USB_WAIT;
switch(bot_trans->cmd_state)
{
case CMD_STATE_SEND:
usbh_bot_cbw(&bot_trans->cbw, write_len * 512,
MSC_WRITE_CMD_LEN, MSC_CBW_FLAG_OUT);
bot_trans->cbw.bCBWLUN = lun;
usbh_cmd_write(bot_trans, bot_trans->cbw.CBWCB, lun, write_len, address, write_data);
bot_trans->cmd_state = CMD_STATE_WAIT;
bot_trans->bot_state = BOT_STATE_SEND_CBW;
break;
case CMD_STATE_WAIT:
status = usb_bot_request(uhost, bot_trans);
if(status == USB_OK)
{
bot_trans->cmd_state = CMD_STATE_SEND;
}
if(status == USB_FAIL)
{
bot_trans->cmd_state = CMD_STATE_SEND;
}
break;
default:
break;
}
return status;
}
/**
* @brief usb host msc bulk-only read request
* @param uhost: to the structure of usbh_core_type
* @param bot_trans: to the structure of msc_bot_trans_type
* @param address: logical block address
* @param read_data: read data buffer
* @param read_len: read data length
* @param lun: logical unit number
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_msc_bot_scsi_read(void *uhost, msc_bot_trans_type *bot_trans,
uint32_t address, uint8_t *read_data,
uint32_t read_len, uint8_t lun)
{
usb_sts_type status = USB_WAIT;
switch(bot_trans->cmd_state)
{
case CMD_STATE_SEND:
usbh_bot_cbw(&bot_trans->cbw, read_len * 512,
MSC_READ_CMD_LEN, MSC_CBW_FLAG_IN);
bot_trans->cbw.bCBWLUN = lun;
usbh_cmd_read(bot_trans, bot_trans->cbw.CBWCB, lun, read_len, address, read_data);
bot_trans->cmd_state = CMD_STATE_WAIT;
bot_trans->bot_state = BOT_STATE_SEND_CBW;
break;
case CMD_STATE_WAIT:
status = usb_bot_request(uhost, bot_trans);
if(status == USB_OK)
{
bot_trans->cmd_state = CMD_STATE_SEND;
}
if(status == USB_FAIL)
{
bot_trans->cmd_state = CMD_STATE_SEND;
}
break;
default:
break;
}
return status;
}
/**
* @brief usb host msc init
* @param msc_struct: to the structure of usbh_msc_type
* @retval status: usb_sts_type status
*/
usb_sts_type msc_bot_scsi_init(usbh_msc_type *msc_struct)
{
msc_struct->state = USBH_MSC_INIT;
msc_struct->ctrl_state = USBH_MSC_STATE_IDLE;
msc_struct->error = MSC_OK;
msc_struct->cur_lun = 0;
msc_struct->max_lun = 0;
msc_struct->use_lun = 0;
msc_struct->bot_trans.msc_struct = &usbh_msc;
msc_struct->bot_trans.cmd_state = CMD_STATE_SEND;
msc_struct->bot_trans.bot_state = BOT_STATE_SEND_CBW;
return USB_OK;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
@@ -0,0 +1,239 @@
/**
**************************************************************************
* @file usbh_msc_bot_scsi.h
* @version v2.1.0
* @date 2022-08-16
* @brief usb host msc bulk-only transfer and scsi 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 __USBH_MSC_BOT_SCSI_H
#define __USBH_MSC_BOT_SCSI_H
#ifdef __cplusplus
extern "C" {
#endif
#include "usbh_core.h"
#include "usb_conf.h"
/** @addtogroup AT32F435_437_middlewares_usbh_class
* @{
*/
/** @addtogroup USBH_msc_bot_scsi_class
* @{
*/
/** @defgroup USBH_msc_bot_scsi_class_definition
* @{
*/
#define MSC_CBW_SIGNATURE 0x43425355
#define MSC_CSW_SIGNATURE 0x53425355
#define MSC_CBW_TAG 0x50607080
#define MSC_CBW_FLAG_IN 0x80
#define MSC_CBW_FLAG_OUT 0x00
#define MSC_CBW_LEN 31
#define MSC_CSW_LEN 13
#define MSC_CBW_CB_LEN 16
#define MSC_TEST_UNIT_READY_CMD_LEN 12
#define MSC_TEST_UNIT_READY_DATA_LEN 0
#define MSC_INQUIRY_CMD_LEN 12
#define MSC_INQUIRY_DATA_LEN 36
#define MSC_CAPACITY10_CMD_LEN 12
#define MSC_CAPACITY10_DATA_LEN 8
#define MSC_REQUEST_SENSE_CMD_LEN 12
#define MSC_REQUEST_SENSE_DATA_LEN 18
#define MSC_WRITE_CMD_LEN 12
#define MSC_READ_CMD_LEN 10
#define MSC_OPCODE_INQUIRY 0x12
#define MSC_OPCODE_CAPACITY 0x25
#define MSC_OPCODE_TEST_UNIT_READY 0x00
#define MSC_OPCODE_REQUEST_SENSE 0x03
#define MSC_OPCODE_WRITE10 0x2A
#define MSC_OPCODE_READ10 0x28
typedef enum
{
BOT_STATE_IDLE,
BOT_STATE_SEND_CBW,
BOT_STATE_SEND_CBW_WAIT,
BOT_STATE_DATA_IN,
BOT_STATE_DATA_IN_WAIT,
BOT_STATE_DATA_OUT,
BOT_STATE_DATA_OUT_WAIT,
BOT_STATE_RECV_CSW,
BOT_STATE_RECV_CSW_WAIT,
BOT_STATE_ERROR_IN,
BOT_STATE_ERROR_OUT,
BOT_STATE_COMPLETE
}msc_bot_state_type;
typedef enum
{
CMD_STATE_SEND,
CMD_STATE_WAIT,
}msc_cmd_state_type;
/**
* @brief usb msc process state
*/
typedef enum
{
USBH_MSC_INIT,
USBH_MSC_INQUIRY,
USBH_MSC_TEST_UNIT_READY,
USBH_MSC_READ_CAPACITY10,
USBH_MSC_REQUEST_SENSE,
USBH_MSC_READ10,
USBH_MSC_WRITE,
USBH_MSC_BUSY,
USBH_MSC_ERROR,
USBH_MSC_IDLE
}msc_state_type;
typedef enum
{
MSC_OK,
MSC_NOT_READY,
MSC_ERROR
}msc_error_type;
/**
* @brief usb msc inquiry data type
*/
typedef struct
{
uint8_t pdev_type;
uint8_t rmb;
uint8_t version;
uint8_t data_format;
uint8_t length;
uint8_t reserved[3];
uint8_t vendor[8];
uint8_t product[16];
uint8_t revision[4];
}msc_scsi_data_inquiry;
/**
* @brief usb msc capacity data type
*/
typedef struct
{
uint32_t blk_nbr;
uint32_t blk_size;
}msc_scsi_data_capacity;
/**
* @brief usb msc bulk-only command block wrapper type
*/
typedef struct
{
uint32_t dCBWSignature;
uint32_t dCBWTag;
uint32_t dCBWDataTransferLength;
uint8_t bmCBWFlags;
uint8_t bCBWLUN;
uint8_t bCBWCBLength;
uint8_t CBWCB[MSC_CBW_CB_LEN];
}msc_bot_cbw_type;
/**
* @brief usb msc bulk-only command status wrapper type
*/
typedef struct
{
uint32_t dCBWSignature;
uint32_t dCBWTag;
uint32_t dCSWDataResidue;
uint8_t bCSWStatus;
}msc_bot_csw_type;
/**
* @brief usb msc bulk-only transfer control type
*/
typedef struct
{
uint8_t buffer[32];
msc_bot_cbw_type cbw;
msc_bot_csw_type csw;
msc_cmd_state_type cmd_state;
msc_bot_state_type bot_state;
uint8_t *data;
void *msc_struct;
}msc_bot_trans_type;
/**
* @brief usb msc bank type
*/
typedef struct
{
msc_scsi_data_inquiry inquiry;
msc_scsi_data_capacity capacity;
msc_state_type state;
msc_error_type ready;
usb_sts_type pre_state;
uint8_t change;
}usbh_msc_unit_type;
usb_sts_type usb_bot_request(void *uhost, msc_bot_trans_type *bot_trans);
usb_sts_type usbh_msc_bot_scsi_get_inquiry(void *uhost, msc_bot_trans_type *bot_trans,
uint8_t lun, msc_scsi_data_inquiry *inquiry);
usb_sts_type usbh_msc_bot_scsi_capacity(void *uhost, msc_bot_trans_type *bot_trans,
uint8_t lun, msc_scsi_data_capacity *capacity);
usb_sts_type usbh_msc_bot_scsi_test_unit_ready(void *uhost, msc_bot_trans_type *bot_trans,
uint8_t lun);
usb_sts_type usbh_msc_bot_scsi_request_sense(void *uhost, msc_bot_trans_type *bot_trans,
uint8_t lun);
usb_sts_type usbh_msc_bot_scsi_write(void *uhost, msc_bot_trans_type *bot_trans,
uint32_t address, uint8_t *write_data,
uint32_t write_len, uint8_t lun);
usb_sts_type usbh_msc_bot_scsi_read(void *uhost, msc_bot_trans_type *bot_trans,
uint32_t address, uint8_t *read_data,
uint32_t read_len, uint8_t lun);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,523 @@
/**
**************************************************************************
* @file usbh_msc_class.c
* @version v2.1.0
* @date 2022-08-16
* @brief usb host 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 "usbh_msc_class.h"
#include "usb_conf.h"
#include "usbh_core.h"
#include "usbh_ctrl.h"
/** @addtogroup AT32F435_437_middlewares_usbh_class
* @{
*/
/** @defgroup USBH_msc_class
* @brief usb host class msc demo
* @{
*/
/** @defgroup USBH_msc_class_private_functions
* @{
*/
static usb_sts_type uhost_init_handler(void *uhost);
static usb_sts_type uhost_reset_handler(void *uhost);
static usb_sts_type uhost_request_handler(void *uhost);
static usb_sts_type uhost_process_handler(void *uhost);
static usb_sts_type usbh_msc_get_max_lun(void *uhost, uint8_t *lun);
static usb_sts_type usbh_msc_clear_feature(void *uhost, uint8_t ept_num);
usbh_msc_type usbh_msc;
usbh_class_handler_type uhost_msc_class_handler =
{
uhost_init_handler,
uhost_reset_handler,
uhost_request_handler,
uhost_process_handler,
&usbh_msc
};
/**
* @brief usb host class init handler
* @param uhost: to the structure of usbh_core_type
* @retval status: usb_sts_type status
*/
static usb_sts_type uhost_init_handler(void *uhost)
{
usbh_core_type *puhost = (usbh_core_type *)uhost;
usb_sts_type status = USB_OK;
uint8_t if_x, eptidx = 0;
usbh_msc_type *pmsc = &usbh_msc;
puhost->class_handler->pdata = &usbh_msc;
if_x = usbh_find_interface(puhost, USB_CLASS_CODE_MSC, MSC_SUBCLASS_SCSI_TRANS, MSC_PROTOCOL_BBB);
if(if_x == 0xFF)
{
USBH_DEBUG("Unsupport Device!");
return USB_NOT_SUPPORT;
}
pmsc->protocol = puhost->dev.cfg_desc.interface[if_x].interface.bInterfaceProtocol;
for(eptidx = 0; eptidx < puhost->dev.cfg_desc.interface[if_x].interface.bNumEndpoints; eptidx ++)
{
if(puhost->dev.cfg_desc.interface[if_x].endpoint[eptidx].bEndpointAddress & 0x80)
{
pmsc->eptin = puhost->dev.cfg_desc.interface[if_x].endpoint[eptidx].bEndpointAddress;
pmsc->in_maxpacket = puhost->dev.cfg_desc.interface[if_x].endpoint[eptidx].wMaxPacketSize;
pmsc->in_poll = puhost->dev.cfg_desc.interface[if_x].endpoint[eptidx].bInterval;
pmsc->chin = usbh_alloc_channel(puhost, pmsc->eptin);
/* enable channel */
usbh_hc_open(puhost, pmsc->chin, pmsc->eptin,
puhost->dev.address, EPT_BULK_TYPE,
pmsc->in_maxpacket,
puhost->dev.speed);
}
else
{
pmsc->eptout = puhost->dev.cfg_desc.interface[if_x].endpoint[eptidx].bEndpointAddress;
pmsc->out_maxpacket = puhost->dev.cfg_desc.interface[if_x].endpoint[eptidx].wMaxPacketSize;
pmsc->out_poll = puhost->dev.cfg_desc.interface[if_x].endpoint[eptidx].bInterval;
pmsc->chout = usbh_alloc_channel(puhost, pmsc->eptout);
/* enable channel */
usbh_hc_open(puhost, pmsc->chout,pmsc->eptout,
puhost->dev.address, EPT_BULK_TYPE,
pmsc->out_maxpacket,
puhost->dev.speed);
}
}
msc_bot_scsi_init(pmsc);
usbh_set_toggle(puhost, pmsc->chout, 0);
usbh_set_toggle(puhost, pmsc->chin, 0);
return status;
}
/**
* @brief usb host class reset handler
* @param uhost: to the structure of usbh_core_type
* @retval status: usb_sts_type status
*/
static usb_sts_type uhost_reset_handler(void *uhost)
{
usbh_core_type *puhost = (usbh_core_type *)uhost;
usbh_msc_type *pmsc = (usbh_msc_type *)puhost->class_handler->pdata;
usb_sts_type status = USB_OK;
uint8_t i_index = 0;
if(puhost->class_handler->pdata == NULL)
{
return status;
}
for(i_index = 0; i_index < pmsc->max_lun ; i_index ++)
{
pmsc->l_unit_n[i_index].pre_state = USB_FAIL;
pmsc->l_unit_n[i_index].change = 0;
pmsc->l_unit_n[i_index].state = USBH_MSC_INIT;
pmsc->l_unit_n[i_index].ready = MSC_NOT_READY;
}
if(pmsc->chin != 0 )
{
usbh_free_channel(puhost, pmsc->chin);
usbh_ch_disable(puhost, pmsc->chin);
pmsc->chin = 0;
}
if(pmsc->chout != 0 )
{
usbh_free_channel(puhost, pmsc->chout);
usbh_ch_disable(puhost, pmsc->chout);
pmsc->chout = 0;
}
return status;
}
/**
* @brief usb host hid class request handler
* @param uhost: to the structure of usbh_core_type
* @retval status: usb_sts_type status
*/
static usb_sts_type uhost_request_handler(void *uhost)
{
usbh_core_type *puhost = (usbh_core_type *)uhost;
usbh_msc_type *pmsc = (usbh_msc_type *)puhost->class_handler->pdata;
usb_sts_type status = USB_WAIT;
uint8_t i_index = 0;
switch(pmsc->ctrl_state)
{
case USBH_MSC_STATE_IDLE:
pmsc->ctrl_state = USBH_MSC_STATE_GET_LUN;
break;
case USBH_MSC_STATE_GET_LUN:
if((status = usbh_msc_get_max_lun(uhost, (uint8_t *)&pmsc->max_lun)) == USB_OK)
{
pmsc->max_lun = (pmsc->max_lun & 0xFF) > USBH_SUPPORT_MAX_LUN ? USBH_SUPPORT_MAX_LUN:((pmsc->max_lun & 0xFF) + 1);
USBH_DEBUG("Support max lun %d", pmsc->max_lun);
for(i_index = 0; i_index < pmsc->max_lun ; i_index ++)
{
pmsc->l_unit_n[i_index].pre_state = USB_FAIL;
pmsc->l_unit_n[i_index].change = 0;
pmsc->l_unit_n[i_index].state = USBH_MSC_INIT;
}
}
break;
case USBH_MSC_STATE_ERROR:
if((usbh_msc_clear_feature(uhost, 0)) == USB_OK)
{
pmsc->ctrl_state = USBH_MSC_STATE_GET_LUN;
}
break;
default:
break;
}
return status;
}
/**
* @brief usb host class process handler
* @param uhost: to the structure of usbh_core_type
* @retval status: usb_sts_type status
*/
static usb_sts_type uhost_process_handler(void *uhost)
{
usbh_core_type *puhost = (usbh_core_type *)uhost;
usbh_msc_type *pmsc = (usbh_msc_type *)puhost->class_handler->pdata;
uint64_t msize = 0;
usb_sts_type status;
switch(pmsc->state)
{
case USBH_MSC_INIT:
if(pmsc->cur_lun < pmsc->max_lun)
{
switch(pmsc->l_unit_n[pmsc->cur_lun].state)
{
case USBH_MSC_INIT:
pmsc->l_unit_n[pmsc->cur_lun].ready = MSC_NOT_READY;
pmsc->l_unit_n[pmsc->cur_lun].state = USBH_MSC_INQUIRY;
break;
case USBH_MSC_INQUIRY:
status = usbh_msc_bot_scsi_get_inquiry(uhost, &pmsc->bot_trans, pmsc->cur_lun, &pmsc->l_unit_n[pmsc->cur_lun].inquiry);
if(status == USB_OK)
{
pmsc->l_unit_n[pmsc->cur_lun].state = USBH_MSC_TEST_UNIT_READY;
}
else if(status == USB_FAIL)
{
pmsc->l_unit_n[pmsc->cur_lun].state = USBH_MSC_REQUEST_SENSE;
}
break;
case USBH_MSC_TEST_UNIT_READY:
status = usbh_msc_bot_scsi_test_unit_ready(uhost, &pmsc->bot_trans, pmsc->cur_lun);
if(status == USB_OK)
{
pmsc->l_unit_n[pmsc->cur_lun].state = USBH_MSC_READ_CAPACITY10;
pmsc->l_unit_n[pmsc->cur_lun].ready = MSC_OK;
}
else if(status == USB_FAIL)
{
pmsc->l_unit_n[pmsc->cur_lun].state = USBH_MSC_REQUEST_SENSE;
pmsc->l_unit_n[pmsc->cur_lun].ready = MSC_NOT_READY;
}
break;
case USBH_MSC_READ_CAPACITY10:
status = usbh_msc_bot_scsi_capacity(uhost, &pmsc->bot_trans, pmsc->cur_lun, &pmsc->l_unit_n[pmsc->cur_lun].capacity);
if(status == USB_OK)
{
msize = (uint64_t)pmsc->l_unit_n[pmsc->cur_lun].capacity.blk_nbr * (uint64_t)pmsc->l_unit_n[pmsc->cur_lun].capacity.blk_size;
USBH_DEBUG("Device capacity: %llu Byte", msize);
USBH_DEBUG("Block num: %d ", pmsc->l_unit_n[pmsc->cur_lun].capacity.blk_nbr);
USBH_DEBUG("Block size: %d Byte", pmsc->l_unit_n[pmsc->cur_lun].capacity.blk_size);
pmsc->l_unit_n[pmsc->cur_lun].state = USBH_MSC_IDLE;
pmsc->cur_lun ++;
}
else if(status == USB_FAIL)
{
pmsc->l_unit_n[pmsc->cur_lun].state = USBH_MSC_REQUEST_SENSE;
}
break;
case USBH_MSC_REQUEST_SENSE:
status = usbh_msc_bot_scsi_request_sense(uhost, &pmsc->bot_trans, pmsc->cur_lun);
if(status == USB_OK)
{
pmsc->l_unit_n[pmsc->cur_lun].state = USBH_MSC_TEST_UNIT_READY;
}
else if(status == USB_FAIL)
{
pmsc->l_unit_n[pmsc->cur_lun].ready = MSC_NOT_READY;
pmsc->l_unit_n[pmsc->cur_lun].state = USBH_MSC_ERROR;
}
break;
case USBH_MSC_BUSY:
break;
case USBH_MSC_ERROR:
break;
default:
break;
}
}
else
{
pmsc->state = USBH_MSC_IDLE;
}
break;
case USBH_MSC_IDLE:
if(puhost->user_handler->user_application != NULL)
{
puhost->user_handler->user_application();
}
default:
break;
}
return USB_OK;
}
/**
* @brief usb host msc get max lun
* @param uhost: to the structure of usbh_core_type
* @param lun: max lun buffer
* @retval status: usb_sts_type status
*/
static usb_sts_type usbh_msc_get_max_lun(void *uhost, uint8_t *lun)
{
usbh_core_type *puhost = (usbh_core_type *)uhost;
usb_sts_type status = USB_WAIT;
if(puhost->ctrl.state == CONTROL_IDLE )
{
puhost->ctrl.setup.bmRequestType = USB_DIR_D2H | USB_REQ_RECIPIENT_INTERFACE | USB_REQ_TYPE_CLASS;
puhost->ctrl.setup.bRequest = MSC_REQ_GET_MAX_LUN;
puhost->ctrl.setup.wValue = 0;
puhost->ctrl.setup.wIndex = 0;
puhost->ctrl.setup.wLength = 1;
usbh_ctrl_request(puhost, lun, 1);
}
else
{
status = usbh_ctrl_result_check(puhost, CONTROL_IDLE, ENUM_IDLE);
if(status == USB_OK || status == USB_NOT_SUPPORT)
{
status = USB_OK;
}
}
return status;
}
/**
* @brief usb host msc clear feature
* @param uhost: to the structure of usbh_core_type
* @param ept_num: endpoint number
* @retval status: usb_sts_type status
*/
static usb_sts_type usbh_msc_clear_feature(void *uhost, uint8_t ept_num)
{
usbh_core_type *puhost = (usbh_core_type *)uhost;
usb_sts_type status = USB_WAIT;
if(puhost->ctrl.state == CONTROL_IDLE )
{
puhost->ctrl.setup.bmRequestType = USB_DIR_H2D | USB_REQ_RECIPIENT_ENDPOINT | USB_REQ_TYPE_STANDARD;
puhost->ctrl.setup.bRequest = USB_STD_REQ_CLEAR_FEATURE;
puhost->ctrl.setup.wValue = 0;
puhost->ctrl.setup.wIndex = ept_num;
puhost->ctrl.setup.wLength = 0;
usbh_ctrl_request(puhost, 0, 0);
}
else
{
status = usbh_ctrl_result_check(puhost, CONTROL_IDLE, ENUM_IDLE);
if(status == USB_OK || status == USB_NOT_SUPPORT)
{
status = USB_OK;
}
}
return status;
}
/**
* @brief usb host msc clear feature
* @param lun: logical unit number
* @retval msc_error_type status
*/
msc_error_type usbh_msc_is_ready(void *uhost, uint8_t lun)
{
usbh_core_type *puhost = (usbh_core_type *)uhost;
usbh_msc_type *pmsc = (usbh_msc_type *)puhost->class_handler->pdata;
return pmsc->l_unit_n[lun].ready;
}
/**
* @brief usb host msc read and write handle
* @param uhost: to the structure of usbh_core_type
* @param address: logical block address
* @param data_len: transfer data length
* @param buffer: transfer data buffer
* @param lun: logical unit number
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_msc_rw_handle(void *uhost, uint32_t address, uint32_t len, uint8_t *buffer, uint8_t lun)
{
usbh_core_type *puhost = (usbh_core_type *)uhost;
usbh_msc_type *pmsc = (usbh_msc_type *)puhost->class_handler->pdata;
usb_sts_type status = USB_WAIT;
switch(pmsc->l_unit_n[lun].state)
{
case USBH_MSC_READ10:
status = usbh_msc_bot_scsi_read(uhost, &pmsc->bot_trans, address, buffer, len, lun);
if(status == USB_OK)
{
pmsc->l_unit_n[lun].state = USBH_MSC_IDLE;
}
else if(status == USB_FAIL)
{
pmsc->l_unit_n[lun].state = USBH_MSC_REQUEST_SENSE;
status = USB_WAIT;
}
break;
case USBH_MSC_WRITE:
status = usbh_msc_bot_scsi_write(uhost, &pmsc->bot_trans, address, buffer, len, lun);
if(status == USB_OK)
{
pmsc->l_unit_n[lun].state = USBH_MSC_IDLE;
}
else if(status == USB_FAIL)
{
pmsc->l_unit_n[lun].state = USBH_MSC_REQUEST_SENSE;
status = USB_WAIT;
}
break;
case USBH_MSC_REQUEST_SENSE:
status = usbh_msc_bot_scsi_request_sense(uhost, &pmsc->bot_trans, lun);
if(status == USB_OK)
{
pmsc->l_unit_n[lun].state = USBH_MSC_IDLE;
status = USB_FAIL;
}
else if(status == USB_FAIL)
{
USBH_DEBUG("device not support");
}
break;
default:
break;
}
return status;
}
/**
* @brief usb host msc read
* @param uhost: to the structure of usbh_core_type
* @param address: logical block address
* @param data_len: transfer data length
* @param buffer: transfer data buffer
* @param lun: logical unit number
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_msc_read(void *uhost, uint32_t address, uint32_t len, uint8_t *buffer, uint8_t lun)
{
usbh_core_type *puhost = (usbh_core_type *)uhost;
usbh_msc_type *pmsc = (usbh_msc_type *)puhost->class_handler->pdata;
uint32_t timeout = 0;
if(puhost->conn_sts == 0 || puhost->global_state != USBH_CLASS
|| pmsc->l_unit_n[lun].state != USBH_MSC_IDLE)
{
return USB_FAIL;
}
pmsc->bot_trans.msc_struct = &usbh_msc;
pmsc->l_unit_n[lun].state = USBH_MSC_READ10;
pmsc->use_lun = lun;
timeout = puhost->timer;
while(usbh_msc_rw_handle(uhost, address, len, buffer, lun) == USB_WAIT)
{
if(puhost->conn_sts == 0 || (puhost->timer - timeout) > (len * 10000))
{
pmsc->l_unit_n[lun].state = USBH_MSC_IDLE;
return USB_FAIL;
}
}
return USB_OK;
}
/**
* @brief usb host msc write
* @param uhost: to the structure of usbh_core_type
* @param address: logical block address
* @param data_len: transfer data length
* @param buffer: transfer data buffer
* @param lun: logical unit number
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_msc_write(void *uhost, uint32_t address, uint32_t len, uint8_t *buffer, uint8_t lun)
{
usbh_core_type *puhost = (usbh_core_type *)uhost;
usbh_msc_type *pmsc = (usbh_msc_type *)puhost->class_handler->pdata;
uint32_t timeout = 0;
if(puhost->conn_sts == 0 || puhost->global_state != USBH_CLASS
|| pmsc->l_unit_n[lun].state != USBH_MSC_IDLE)
{
return USB_FAIL;
}
pmsc->bot_trans.msc_struct = &usbh_msc;
pmsc->l_unit_n[lun].state = USBH_MSC_WRITE;
pmsc->use_lun = lun;
timeout = puhost->timer;
while(usbh_msc_rw_handle(uhost, address, len, buffer, lun) == USB_WAIT)
{
if(puhost->conn_sts == 0 || (puhost->timer - timeout) > (len * 10000))
{
pmsc->l_unit_n[lun].state = USBH_MSC_IDLE;
return USB_FAIL;
}
}
return USB_OK;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
@@ -0,0 +1,143 @@
/**
**************************************************************************
* @file usbh_msc_class.h
* @version v2.1.0
* @date 2022-08-16
* @brief usb host msc class 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 __USBH_MSC_CLASS_H
#define __USBH_MSC_CLASS_H
#ifdef __cplusplus
extern "C" {
#endif
#include "usbh_core.h"
#include "usb_conf.h"
#include "usbh_msc_bot_scsi.h"
/** @addtogroup AT32F435_437_middlewares_usbh_class
* @{
*/
/** @addtogroup USBH_msc_class
* @{
*/
/** @defgroup USBH_msc_class_definition
* @{
*/
/**
* @brief usb msc subclass code
*/
#define MSC_SUBCLASS_SCSI_TRANS 0x06
/**
* @brief usb msc protocol code
*/
#define MSC_PROTOCOL_BBB 0x50
/**
* @brief usb msc request code
*/
#define MSC_REQ_GET_MAX_LUN 0xFE
#define MSC_REQ_BOMSR 0xFF
/**
* @brief usb hid request code
*/
#define USB_HID_GET_REPORT 0x01
#define USB_HID_GET_IDLE 0x02
#define USB_HID_GET_PROTOCOL 0x03
#define USB_HID_SET_REPORT 0x09
#define USB_HID_SET_IDLE 0x0A
#define USB_HID_SET_PROTOCOL 0x0B
#define USBH_SUPPORT_MAX_LUN 0x2
/**
* @brief usb msc request state
*/
typedef enum
{
USBH_MSC_STATE_IDLE,
USBH_MSC_STATE_GET_LUN,
USBH_MSC_STATE_ERROR,
USBH_MSC_STATE_COMPLETE,
}usbh_msc_ctrl_state_type;
/**
* @brief usb msc struct
*/
typedef struct
{
uint8_t chin;
uint8_t eptin;
uint16_t in_maxpacket;
uint8_t in_poll;
uint8_t chout;
uint8_t eptout;
uint16_t out_maxpacket;
uint8_t out_poll;
uint8_t protocol;
uint32_t max_lun;
uint32_t cur_lun;
uint32_t use_lun;
usbh_msc_ctrl_state_type ctrl_state;
msc_state_type state;
uint8_t error;
msc_bot_trans_type bot_trans;
usbh_msc_unit_type l_unit_n[USBH_SUPPORT_MAX_LUN];
uint16_t poll_timer;
uint8_t buffer[64];
}usbh_msc_type;
extern usbh_class_handler_type uhost_msc_class_handler;
extern usbh_msc_type usbh_msc;
msc_error_type usbh_msc_is_ready(void *uhost, uint8_t lun);
usb_sts_type usbh_msc_write(void *uhost, uint32_t address, uint32_t len, uint8_t *buffer, uint8_t lun);
usb_sts_type usbh_msc_read(void *uhost, uint32_t address, uint32_t len, uint8_t *buffer, uint8_t lun);
usb_sts_type usbh_msc_rw_handle(void *uhost, uint32_t address, uint32_t len, uint8_t *buffer, uint8_t lun);
usb_sts_type msc_bot_scsi_init(usbh_msc_type *msc_struct);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,219 @@
/**
**************************************************************************
* @file usb_conf.h
* @version v2.1.0
* @date 2022-08-16
* @brief usb config 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 __USB_CONF_H
#define __USB_CONF_H
#ifdef __cplusplus
extern "C" {
#endif
#include "at32f435_437_usb.h"
#include "at32f435_437.h"
/** @addtogroup AT32F437_periph_examples
* @{
*/
/** @addtogroup 437_USB_device_hid_app_led3
* @{
*/
/**
* @brief enable usb device mode
*/
#define USE_OTG_DEVICE_MODE
/**
* @brief enable usb host mode
*/
/* #define USE_OTG_HOST_MODE */
/**
* @brief select otgfs1 or otgfs2 define
*/
/* use otgfs1 */
#define OTG_USB_ID 1
/* use otgfs2 */
/* #define OTG_USB_ID 2 */
#if (OTG_USB_ID == 1)
#define USB_ID 0
#define OTG_CLOCK CRM_OTGFS1_PERIPH_CLOCK
#define OTG_IRQ OTGFS1_IRQn
#define OTG_IRQ_HANDLER OTGFS1_IRQHandler
#define OTG_WKUP_IRQ OTGFS1_WKUP_IRQn
#define OTG_WKUP_HANDLER OTGFS1_WKUP_IRQHandler
#define OTG_WKUP_EXINT_LINE EXINT_LINE_18
#define OTG_PIN_GPIO GPIOA
#define OTG_PIN_GPIO_CLOCK CRM_GPIOA_PERIPH_CLOCK
#define OTG_PIN_DP GPIO_PINS_12
#define OTG_PIN_DP_SOURCE GPIO_PINS_SOURCE12
#define OTG_PIN_DM GPIO_PINS_11
#define OTG_PIN_DM_SOURCE GPIO_PINS_SOURCE11
#define OTG_PIN_VBUS GPIO_PINS_9
#define OTG_PIN_VBUS_SOURCE GPIO_PINS_SOURCE9
#define OTG_PIN_ID GPIO_PINS_10
#define OTG_PIN_ID_SOURCE GPIO_PINS_SOURCE10
#define OTG_PIN_SOF_GPIO GPIOA
#define OTG_PIN_SOF_GPIO_CLOCK CRM_GPIOA_PERIPH_CLOCK
#define OTG_PIN_SOF GPIO_PINS_8
#define OTG_PIN_SOF_SOURCE GPIO_PINS_SOURCE8
#define OTG_PIN_MUX GPIO_MUX_10
#endif
#if (OTG_USB_ID == 2)
#define USB_ID 1
#define OTG_CLOCK CRM_OTGFS2_PERIPH_CLOCK
#define OTG_IRQ OTGFS2_IRQn
#define OTG_IRQ_HANDLER OTGFS2_IRQHandler
#define OTG_WKUP_IRQ OTGFS2_WKUP_IRQn
#define OTG_WKUP_HANDLER OTGFS2_WKUP_IRQHandler
#define OTG_WKUP_EXINT_LINE EXINT_LINE_20
#define OTG_PIN_GPIO GPIOB
#define OTG_PIN_GPIO_CLOCK CRM_GPIOB_PERIPH_CLOCK
#define OTG_PIN_DP GPIO_PINS_15
#define OTG_PIN_DP_SOURCE GPIO_PINS_SOURCE15
#define OTG_PIN_DM GPIO_PINS_14
#define OTG_PIN_DM_SOURCE GPIO_PINS_SOURCE14
#define OTG_PIN_VBUS GPIO_PINS_13
#define OTG_PIN_VBUS_SOURCE GPIO_PINS_SOURCE13
#define OTG_PIN_ID GPIO_PINS_12
#define OTG_PIN_ID_SOURCE GPIO_PINS_SOURCE10
#define OTG_PIN_SOF_GPIO GPIOA
#define OTG_PIN_SOF_GPIO_CLOCK CRM_GPIOA_PERIPH_CLOCK
#define OTG_PIN_SOF GPIO_PINS_4
#define OTG_PIN_SOF_SOURCE GPIO_PINS_SOURCE4
#define OTG_PIN_MUX GPIO_MUX_12
#endif
/**
* @brief usb device mode config
*/
#ifdef USE_OTG_DEVICE_MODE
/**
* @brief usb device mode fifo
*/
/* otg1 device fifo */
#define USBD_RX_SIZE 128
#define USBD_EP0_TX_SIZE 24
#define USBD_EP1_TX_SIZE 20
#define USBD_EP2_TX_SIZE 20
#define USBD_EP3_TX_SIZE 20
#define USBD_EP4_TX_SIZE 20
#define USBD_EP5_TX_SIZE 20
#define USBD_EP6_TX_SIZE 20
#define USBD_EP7_TX_SIZE 20
/* otg2 device fifo */
#define USBD2_RX_SIZE 128
#define USBD2_EP0_TX_SIZE 24
#define USBD2_EP1_TX_SIZE 20
#define USBD2_EP2_TX_SIZE 20
#define USBD2_EP3_TX_SIZE 20
#define USBD2_EP4_TX_SIZE 20
#define USBD2_EP5_TX_SIZE 20
#define USBD2_EP6_TX_SIZE 20
#define USBD2_EP7_TX_SIZE 20
/**
* @brief usb endpoint max num define
*/
#ifndef USB_EPT_MAX_NUM
#define USB_EPT_MAX_NUM 8
#endif
#endif
/**
* @brief usb host mode config
*/
#ifdef USE_OTG_HOST_MODE
#ifndef USB_HOST_CHANNEL_NUM
#define USB_HOST_CHANNEL_NUM 16
#endif
/**
* @brief usb host mode fifo
*/
/* otg1 host fifo */
#define USBH_RX_FIFO_SIZE 128
#define USBH_NP_TX_FIFO_SIZE 96
#define USBH_P_TX_FIFO_SIZE 96
/* otg2 host fifo */
#define USBH2_RX_FIFO_SIZE 128
#define USBH2_NP_TX_FIFO_SIZE 96
#define USBH2_P_TX_FIFO_SIZE 96
#endif
/**
* @brief usb sof output enable
*/
/* #define USB_SOF_OUTPUT_ENABLE */
/**
* @brief usb vbus ignore
*/
#define USB_VBUS_IGNORE
/**
* @brief usb low power wakeup handler enable
*/
/* #define USB_LOW_POWER_WAKUP */
void usb_delay_ms(uint32_t ms);
void usb_delay_us(uint32_t us);
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,136 @@
/**
**************************************************************************
* @file usb_core.h
* @version v2.1.0
* @date 2022-08-16
* @brief usb core 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 __USB_CORE_H
#define __USB_CORE_H
#ifdef __cplusplus
extern "C" {
#endif
#include "usb_std.h"
#include "usb_conf.h"
#ifdef USE_OTG_DEVICE_MODE
#include "usbd_core.h"
#endif
#ifdef USE_OTG_HOST_MODE
#include "usbh_core.h"
#endif
/** @addtogroup AT32F435_437_middlewares_usb_drivers
* @{
*/
/** @addtogroup USB_drivers_core
* @{
*/
/** @defgroup USB_core_exported_types
* @{
*/
/**
* @brief usb core speed select
*/
typedef enum
{
USB_LOW_SPEED_CORE_ID, /*!< usb low speed core id */
USB_FULL_SPEED_CORE_ID, /*!< usb full speed core id */
USB_HIGH_SPEED_CORE_ID, /*!< usb high speed core id */
} usb_speed_type;
/**
* @brief usb core cofig struct
*/
typedef struct
{
uint8_t speed; /*!< otg speed */
uint8_t dma_en; /*!< dma enable state, not use*/
uint8_t hc_num; /*!< the otg host support number of channel */
uint8_t ept_num; /*!< the otg device support number of endpoint */
uint16_t max_size; /*!< support max packet size */
uint16_t fifo_size; /*!< the usb otg total file size */
uint8_t phy_itface; /*!< usb phy select */
uint8_t core_id; /*!< the usb otg core id */
uint8_t low_power; /*!< the usb otg low power option */
uint8_t sof_out; /*!< the sof signal output */
uint8_t usb_id; /*!< select otgfs1 or otgfs2 */
uint8_t vbusig; /*!< vbus ignore */
} usb_core_cfg;
/**
* @brief usb otg core struct type
*/
typedef struct
{
usb_reg_type *usb_reg; /*!< the usb otg register type */
#ifdef USE_OTG_DEVICE_MODE
usbd_core_type dev; /*!< the usb device core type */
#endif
#ifdef USE_OTG_HOST_MODE
usbh_core_type host; /*!< the usb host core type */
#endif
usb_core_cfg cfg; /*!< the usb otg core config type */
} otg_core_type;
usb_sts_type usb_core_config(otg_core_type *otgdev, uint8_t core_id);
#ifdef USE_OTG_DEVICE_MODE
usb_sts_type usbd_init(otg_core_type *udev,
uint8_t core_id, uint8_t usb_id,
usbd_class_handler *class_handler,
usbd_desc_handler *desc_handler);
#endif
#ifdef USE_OTG_HOST_MODE
usb_sts_type usbh_init(otg_core_type *hdev,
uint8_t core_id, uint8_t usb_id,
usbh_class_handler_type *class_handler,
usbh_user_handler_type *user_handler);
#endif
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,385 @@
/**
**************************************************************************
* @file usb_std.h
* @version v2.1.0
* @date 2022-08-16
* @brief usb standard 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 __USB_STD_H
#define __USB_STD_H
#ifdef __cplusplus
extern "C" {
#endif
/* includes ------------------------------------------------------------------*/
#include "usb_conf.h"
/** @addtogroup AT32F435_437_middlewares_usb_drivers
* @{
*/
/** @addtogroup USB_standard
* @{
*/
/** @defgroup USB_standard_define
* @{
*/
/**
* @brief usb request recipient
*/
#define USB_REQ_RECIPIENT_DEVICE 0x00 /*!< usb request recipient device */
#define USB_REQ_RECIPIENT_INTERFACE 0x01 /*!< usb request recipient interface */
#define USB_REQ_RECIPIENT_ENDPOINT 0x02 /*!< usb request recipient endpoint */
#define USB_REQ_RECIPIENT_OTHER 0x03 /*!< usb request recipient other */
#define USB_REQ_RECIPIENT_MASK 0x1F /*!< usb request recipient mask */
/**
* @brief usb request type
*/
#define USB_REQ_TYPE_STANDARD 0x00 /*!< usb request type standard */
#define USB_REQ_TYPE_CLASS 0x20 /*!< usb request type class */
#define USB_REQ_TYPE_VENDOR 0x40 /*!< usb request type vendor */
#define USB_REQ_TYPE_RESERVED 0x60 /*!< usb request type reserved */
/**
* @brief usb request data transfer direction
*/
#define USB_REQ_DIR_HTD 0x00 /*!< usb request data transfer direction host to device */
#define USB_REQ_DIR_DTH 0x80 /*!< usb request data transfer direction device to host */
/**
* @brief usb standard device requests codes
*/
#define USB_STD_REQ_GET_STATUS 0 /*!< usb request code status */
#define USB_STD_REQ_CLEAR_FEATURE 1 /*!< usb request code clear feature */
#define USB_STD_REQ_SET_FEATURE 3 /*!< usb request code feature */
#define USB_STD_REQ_SET_ADDRESS 5 /*!< usb request code address */
#define USB_STD_REQ_GET_DESCRIPTOR 6 /*!< usb request code get descriptor */
#define USB_STD_REQ_SET_DESCRIPTOR 7 /*!< usb request code set descriptor */
#define USB_STD_REQ_GET_CONFIGURATION 8 /*!< usb request code get configuration */
#define USB_STD_REQ_SET_CONFIGURATION 9 /*!< usb request code set configuration */
#define USB_STD_REQ_GET_INTERFACE 10 /*!< usb request code get interface */
#define USB_STD_REQ_SET_INTERFACE 11 /*!< usb request code set interface */
#define USB_STD_REQ_SYNCH_FRAME 12 /*!< usb request code synch frame */
/**
* @brief usb standard device type
*/
#define USB_DESCIPTOR_TYPE_DEVICE 1 /*!< usb standard device type device */
#define USB_DESCIPTOR_TYPE_CONFIGURATION 2 /*!< usb standard device type configuration */
#define USB_DESCIPTOR_TYPE_STRING 3 /*!< usb standard device type string */
#define USB_DESCIPTOR_TYPE_INTERFACE 4 /*!< usb standard device type interface */
#define USB_DESCIPTOR_TYPE_ENDPOINT 5 /*!< usb standard device type endpoint */
#define USB_DESCIPTOR_TYPE_DEVICE_QUALIFIER 6 /*!< usb standard device type qualifier */
#define USB_DESCIPTOR_TYPE_OTHER_SPEED 7 /*!< usb standard device type other speed */
#define USB_DESCIPTOR_TYPE_INTERFACE_POWER 8 /*!< usb standard device type interface power */
/**
* @brief usb standard string type
*/
#define USB_LANGID_STRING 0 /*!< usb standard string type lang id */
#define USB_MFC_STRING 1 /*!< usb standard string type mfc */
#define USB_PRODUCT_STRING 2 /*!< usb standard string type product */
#define USB_SERIAL_STRING 3 /*!< usb standard string type serial */
#define USB_CONFIG_STRING 4 /*!< usb standard string type config */
#define USB_INTERFACE_STRING 5 /*!< usb standard string type interface */
/**
* @brief usb configuration attributes
*/
#define USB_CONF_REMOTE_WAKEUP 2 /*!< usb configuration attributes remote wakeup */
#define USB_CONF_SELF_POWERED 1 /*!< usb configuration attributes self powered */
/**
* @brief usb standard feature selectors
*/
#define USB_FEATURE_EPT_HALT 0 /*!< usb standard feature selectors endpoint halt */
#define USB_FEATURE_REMOTE_WAKEUP 1 /*!< usb standard feature selectors remote wakeup */
#define USB_FEATURE_TEST_MODE 2 /*!< usb standard feature selectors test mode */
/**
* @brief usb device connect state
*/
typedef enum
{
USB_CONN_STATE_DEFAULT =1, /*!< usb device connect state default */
USB_CONN_STATE_ADDRESSED, /*!< usb device connect state address */
USB_CONN_STATE_CONFIGURED, /*!< usb device connect state configured */
USB_CONN_STATE_SUSPENDED /*!< usb device connect state suspend */
}usbd_conn_state;
/**
* @brief endpoint 0 state
*/
#define USB_EPT0_IDLE 0 /*!< usb endpoint state idle */
#define USB_EPT0_SETUP 1 /*!< usb endpoint state setup */
#define USB_EPT0_DATA_IN 2 /*!< usb endpoint state data in */
#define USB_EPT0_DATA_OUT 3 /*!< usb endpoint state data out */
#define USB_EPT0_STATUS_IN 4 /*!< usb endpoint state status in */
#define USB_EPT0_STATUS_OUT 5 /*!< usb endpoint state status out */
#define USB_EPT0_STALL 6 /*!< usb endpoint state stall */
/**
* @brief usb descriptor length
*/
#define USB_DEVICE_QUALIFIER_DESC_LEN 0x0A /*!< usb qualifier descriptor length */
#define USB_DEVICE_DESC_LEN 0x12 /*!< usb device descriptor length */
#define USB_DEVICE_CFG_DESC_LEN 0x09 /*!< usb configuration descriptor length */
#define USB_DEVICE_IF_DESC_LEN 0x09 /*!< usb interface descriptor length */
#define USB_DEVICE_EPT_LEN 0x07 /*!< usb endpoint descriptor length */
#define USB_DEVICE_OTG_DESC_LEN 0x03 /*!< usb otg descriptor length */
#define USB_DEVICE_LANGID_STR_DESC_LEN 0x04 /*!< usb lang id string descriptor length */
#define USB_DEVICE_OTHER_SPEED_DESC_SIZ_LEN 0x09 /*!< usb other speed descriptor length */
/**
* @brief usb class code
*/
#define USB_CLASS_CODE_AUDIO 0x01 /*!< usb class code audio */
#define USB_CLASS_CODE_CDC 0x02 /*!< usb class code cdc */
#define USB_CLASS_CODE_HID 0x03 /*!< usb class code hid */
#define USB_CLASS_CODE_PRINTER 0x07 /*!< usb class code printer */
#define USB_CLASS_CODE_MSC 0x08 /*!< usb class code msc */
#define USB_CLASS_CODE_HUB 0x09 /*!< usb class code hub */
#define USB_CLASS_CODE_CDCDATA 0x0A /*!< usb class code cdc data */
#define USB_CLASS_CODE_CCID 0x0B /*!< usb class code ccid */
#define USB_CLASS_CODE_VIDEO 0x0E /*!< usb class code video */
#define USB_CLASS_CODE_VENDOR 0xFF /*!< usb class code vendor */
/**
* @brief usb endpoint type
*/
#define USB_EPT_DESC_CONTROL 0x00 /*!< usb endpoint description type control */
#define USB_EPT_DESC_ISO 0x01 /*!< usb endpoint description type iso */
#define USB_EPT_DESC_BULK 0x02 /*!< usb endpoint description type bulk */
#define USB_EPT_DESC_INTERRUPT 0x03 /*!< usb endpoint description type interrupt */
#define USB_EPT_DESC_NSYNC 0x00 /*!< usb endpoint description nsync */
#define USB_ETP_DESC_ASYNC 0x04 /*!< usb endpoint description async */
#define USB_ETP_DESC_ADAPTIVE 0x08 /*!< usb endpoint description adaptive */
#define USB_ETP_DESC_SYNC 0x0C /*!< usb endpoint description sync */
#define USB_EPT_DESC_DATA_EPT 0x00 /*!< usb endpoint description data */
#define USB_EPT_DESC_FD_EPT 0x10 /*!< usb endpoint description fd */
#define USB_EPT_DESC_FDDATA_EPT 0x20 /*!< usb endpoint description fddata */
/**
* @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 cdc class request code define
*/
#define SET_LINE_CODING 0x20
#define GET_LINE_CODING 0x21
/**
* @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;
/**
* @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 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 endpoint 0 max size
*/
#define USB_MAX_EP0_SIZE 64 /*!< usb endpoint 0 max size */
/**
* @brief usb swap address
*/
#define SWAPBYTE(addr) (uint16_t)(((uint16_t)(*((uint8_t *)(addr)))) + \
(((uint16_t)(*(((uint8_t *)(addr)) + 1))) << 8)) /*!< swap address */
/**
* @brief min and max define
*/
#define MIN(a, b) (uint16_t)(((a) < (b)) ? (a) : (b)) /*!< min define*/
#define MAX(a, b) (uint16_t)(((a) > (b)) ? (a) : (b)) /*!< max define*/
/**
* @brief low byte and high byte define
*/
#define LBYTE(x) ((uint8_t)(x & 0x00FF)) /*!< low byte define */
#define HBYTE(x) ((uint8_t)((x & 0xFF00) >>8)) /*!< high byte define*/
/**
* @brief usb return status
*/
typedef enum
{
USB_OK, /*!< usb status ok */
USB_FAIL, /*!< usb status fail */
USB_WAIT, /*!< usb status wait */
USB_NOT_SUPPORT, /*!< usb status not support */
USB_ERROR, /*!< usb status error */
}usb_sts_type;
/**
* @brief format of usb setup data
*/
typedef struct
{
uint8_t bmRequestType; /*!< characteristics of request */
uint8_t bRequest; /*!< specific request */
uint16_t wValue; /*!< word-sized field that varies according to request */
uint16_t wIndex; /*!< word-sized field that varies according to request
typically used to pass an index or offset */
uint16_t wLength; /*!< number of bytes to transfer if there is a data stage */
} usb_setup_type;
/**
* @brief format of standard device descriptor
*/
typedef struct
{
uint8_t bLength; /*!< size of this descriptor in bytes */
uint8_t bDescriptorType; /*!< device descriptor type */
uint16_t bcdUSB; /*!< usb specification release number */
uint8_t bDeviceClass; /*!< class code (assigned by the usb-if) */
uint8_t bDeviceSubClass; /*!< subclass code (assigned by the usb-if) */
uint8_t bDeviceProtocol; /*!< protocol code ((assigned by the usb-if)) */
uint8_t bMaxPacketSize0; /*!< maximum packet size for endpoint zero */
uint16_t idVendor; /*!< verndor id ((assigned by the usb-if)) */
uint16_t idProduct; /*!< product id ((assigned by the usb-if)) */
uint16_t bcdDevice; /*!< device release number in binary-coded decimal */
uint8_t iManufacturer; /*!< index of string descriptor describing manufacturer */
uint8_t iProduct; /*!< index of string descriptor describing product */
uint8_t iSerialNumber; /*!< index of string descriptor describing serial number */
uint8_t bNumConfigurations; /*!< number of possible configurations */
} usb_device_desc_type;
/**
* @brief format of standard configuration descriptor
*/
typedef struct
{
uint8_t bLength; /*!< size of this descriptor in bytes */
uint8_t bDescriptorType; /*!< configuration descriptor type */
uint16_t wTotalLength; /*!< total length of data returned for this configuration */
uint8_t bNumInterfaces; /*!< number of interfaces supported by this configuration */
uint8_t bConfigurationValue; /*!< value to use as an argument to the SetConfiguration() request */
uint8_t iConfiguration; /*!< index of string descriptor describing this configuration */
uint8_t bmAttributes; /*!< configuration characteristics
D7 reserved
D6 self-powered
D5 remote wakeup
D4~D0 reserved */
uint8_t bMaxPower; /*!< maximum power consumption of the usb device from the bus */
}usb_configuration_desc_type;
/**
* @brief format of standard interface descriptor
*/
typedef struct
{
uint8_t bLength; /*!< size of this descriptor in bytes */
uint8_t bDescriptorType; /*!< interface descriptor type */
uint8_t bInterfaceNumber; /*!< number of this interface */
uint8_t bAlternateSetting; /*!< value used to select this alternate setting for the interface */
uint8_t bNumEndpoints; /*!< number of endpoints used by this interface */
uint8_t bInterfaceClass; /*!< class code (assigned by the usb-if) */
uint8_t bInterfaceSubClass; /*!< subclass code (assigned by the usb-if) */
uint8_t bInterfaceProtocol; /*!< protocol code (assigned by the usb-if) */
uint8_t iInterface; /*!< index of string descriptor describing this interface */
} usb_interface_desc_type;
/**
* @brief format of standard endpoint descriptor
*/
typedef struct
{
uint8_t bLength; /*!< size of this descriptor in bytes */
uint8_t bDescriptorType; /*!< endpoint descriptor type */
uint8_t bEndpointAddress; /*!< the address of the endpoint on the usb device described by this descriptor */
uint8_t bmAttributes; /*!< describes the endpoints attributes when it is configured using bConfiguration value */
uint16_t wMaxPacketSize; /*!< maximum packet size this endpoint */
uint8_t bInterval; /*!< interval for polling endpoint for data transfers */
} usb_endpoint_desc_type;
/**
* @brief format of header
*/
typedef struct
{
uint8_t bLength; /*!< size of this descriptor in bytes */
uint8_t bDescriptorType; /*!< descriptor type */
} usb_header_desc_type;
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,187 @@
/**
**************************************************************************
* @file usbd_core.h
* @version v2.1.0
* @date 2022-08-16
* @brief usb device core 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 __USBD_CORE_H
#define __USBD_CORE_H
#ifdef __cplusplus
extern "C" {
#endif
#include "usb_conf.h"
#include "usb_std.h"
/** @addtogroup AT32F435_437_middlewares_usbd_drivers
* @{
*/
/** @addtogroup USBD_drivers_core
* @{
*/
/** @defgroup USBD_core_exported_types
* @{
*/
#ifdef USE_OTG_DEVICE_MODE
/**
* @brief usb device event
*/
typedef enum
{
USBD_NOP_EVENT, /*!< usb device nop event */
USBD_RESET_EVENT, /*!< usb device reset event */
USBD_SUSPEND_EVENT, /*!< usb device suspend event */
USBD_WAKEUP_EVENT, /*!< usb device wakeup event */
USBD_DISCONNECT_EVNET, /*!< usb device disconnect event */
USBD_INISOINCOM_EVENT, /*!< usb device inisoincom event */
USBD_OUTISOINCOM_EVENT, /*!< usb device outisoincom event */
USBD_ERR_EVENT /*!< usb device error event */
}usbd_event_type;
/**
* @brief usb device descriptor struct
*/
typedef struct
{
uint16_t length; /*!< descriptor length */
uint8_t *descriptor; /*!< descriptor string */
}usbd_desc_t;
/**
* @brief usb device descriptor handler
*/
typedef struct
{
usbd_desc_t *(*get_device_descriptor)(void); /*!< get device descriptor callback */
usbd_desc_t *(*get_device_qualifier)(void); /*!< get device qualifier callback */
usbd_desc_t *(*get_device_configuration)(void); /*!< get device configuration callback */
usbd_desc_t *(*get_device_other_speed)(void); /*!< get device other speed callback */
usbd_desc_t *(*get_device_lang_id)(void); /*!< get device lang id callback */
usbd_desc_t *(*get_device_manufacturer_string)(void); /*!< get device manufacturer callback */
usbd_desc_t *(*get_device_product_string)(void); /*!< get device product callback */
usbd_desc_t *(*get_device_serial_string)(void); /*!< get device serial callback */
usbd_desc_t *(*get_device_interface_string)(void); /*!< get device interface string callback */
usbd_desc_t *(*get_device_config_string)(void); /*!< get device device config callback */
}usbd_desc_handler;
/**
* @brief usb device class handler
*/
typedef struct
{
usb_sts_type (*init_handler)(void *udev); /*!< usb class init handler */
usb_sts_type (*clear_handler)(void *udev); /*!< usb class clear handler */
usb_sts_type (*setup_handler)(void *udev, usb_setup_type *setup); /*!< usb class setup handler */
usb_sts_type (*ept0_tx_handler)(void *udev); /*!< usb class endpoint 0 tx complete handler */
usb_sts_type (*ept0_rx_handler)(void *udev); /*!< usb class endpoint 0 rx complete handler */
usb_sts_type (*in_handler)(void *udev, uint8_t ept_num); /*!< usb class in transfer complete handler */
usb_sts_type (*out_handler)(void *udev, uint8_t ept_num); /*!< usb class out transfer complete handler */
usb_sts_type (*sof_handler)(void *udev); /*!< usb class sof handler */
usb_sts_type (*event_handler)(void *udev, usbd_event_type event); /*!< usb class event handler */
void *pdata; /*!< usb class data pointer */
}usbd_class_handler;
/**
* @brief usb device core struct type
*/
typedef struct
{
usb_reg_type *usb_reg; /*!< usb register pointer */
usbd_class_handler *class_handler; /*!< usb device class handler pointer */
usbd_desc_handler *desc_handler; /*!< usb device descriptor handler pointer */
usb_ept_info ept_in[USB_EPT_MAX_NUM]; /*!< usb in endpoint infomation struct */
usb_ept_info ept_out[USB_EPT_MAX_NUM]; /*!< usb out endpoint infomation struct */
usb_setup_type setup; /*!< usb setup type struct */
uint8_t setup_buffer[12]; /*!< usb setup request buffer */
uint8_t ept0_sts; /*!< usb control endpoint 0 state */
uint8_t speed; /*!< usb speed */
uint16_t ept0_wlength; /*!< usb endpoint 0 transfer length */
usbd_conn_state conn_state; /*!< usb current connect state */
usbd_conn_state old_conn_state; /*!< usb save the previous connect state */
uint8_t device_addr; /*!< device address */
uint8_t remote_wakup; /*!< remote wakeup state */
uint8_t default_config; /*!< usb default config state */
uint8_t dev_config; /*!< usb device config state */
uint32_t config_status; /*!< usb configure status */
}usbd_core_type;
void usbd_core_in_handler(usbd_core_type *udev, uint8_t ept_num);
void usbd_core_out_handler(usbd_core_type *udev, uint8_t ept_num);
void usbd_core_setup_handler(usbd_core_type *udev, uint8_t ept_num);
void usbd_ctrl_unsupport(usbd_core_type *udev);
void usbd_ctrl_send(usbd_core_type *udev, uint8_t *buffer, uint16_t len);
void usbd_ctrl_recv(usbd_core_type *udev, uint8_t *buffer, uint16_t len);
void usbd_ctrl_send_status(usbd_core_type *udev);
void usbd_ctrl_recv_status(usbd_core_type *udev);
void usbd_set_stall(usbd_core_type *udev, uint8_t ept_addr);
void usbd_clear_stall(usbd_core_type *udev, uint8_t ept_addr);
void usbd_ept_open(usbd_core_type *udev, uint8_t ept_addr, uint8_t ept_type, uint16_t maxpacket);
void usbd_ept_close(usbd_core_type *udev, uint8_t ept_addr);
void usbd_ept_send(usbd_core_type *udev, uint8_t ept_num, uint8_t *buffer, uint16_t len);
void usbd_ept_recv(usbd_core_type *udev, uint8_t ept_num, uint8_t *buffer, uint16_t len);
void usbd_connect(usbd_core_type *udev);
void usbd_disconnect(usbd_core_type *udev);
void usbd_set_device_addr(usbd_core_type *udev, uint8_t address);
uint32_t usbd_get_recv_len(usbd_core_type *udev, uint8_t ept_addr);
void usb_ept_defaut_init(usbd_core_type *udev);
usbd_conn_state usbd_connect_state_get(usbd_core_type *udev);
void usbd_remote_wakeup(usbd_core_type *udev);
void usbd_enter_suspend(usbd_core_type *udev);
void usbd_flush_tx_fifo(usbd_core_type *udev, uint8_t ept_num);
void usbd_fifo_alloc(usbd_core_type *udev);
usb_sts_type usbd_core_init(usbd_core_type *udev,
usb_reg_type *usb_reg,
usbd_class_handler *class_handler,
usbd_desc_handler *desc_handler,
uint8_t core_id);
#endif
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,82 @@
/**
**************************************************************************
* @file usbd_int.h
* @version v2.1.0
* @date 2022-08-16
* @brief usb interrupt 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 __USBD_INT_H
#define __USBD_INT_H
#ifdef __cplusplus
extern "C" {
#endif
/** @addtogroup AT32F435_437_middlewares_usbd_drivers
* @{
*/
/** @addtogroup USBD_drivers_int
* @{
*/
/** @defgroup USBD_interrupt_exported_types
* @{
*/
/* includes ------------------------------------------------------------------*/
#include "usbd_core.h"
#include "usb_core.h"
void usbd_irq_handler(otg_core_type *udev);
void usbd_ept_handler(usbd_core_type *udev);
void usbd_reset_handler(usbd_core_type *udev);
void usbd_sof_handler(usbd_core_type *udev);
void usbd_suspend_handler(usbd_core_type *udev);
void usbd_wakeup_handler(usbd_core_type *udev);
void usbd_inept_handler(usbd_core_type *udev);
void usbd_outept_handler(usbd_core_type *udev);
void usbd_enumdone_handler(usbd_core_type *udev);
void usbd_rxflvl_handler(usbd_core_type *udev);
void usbd_incomisioin_handler(usbd_core_type *udev);
void usbd_discon_handler(usbd_core_type *udev);
void usbd_incomisoout_handler(usbd_core_type *udev);
void usb_write_empty_txfifo(usbd_core_type *udev, uint32_t ept_num);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,73 @@
/**
**************************************************************************
* @file usb_sdr.h
* @version v2.1.0
* @date 2022-08-16
* @brief usb 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 __USB_SDR_H
#define __USB_SDR_H
#ifdef __cplusplus
extern "C" {
#endif
/* includes ------------------------------------------------------------------*/
#include "usb_conf.h"
#include "usb_core.h"
/** @addtogroup AT32F435_437_middlewares_usbd_drivers
* @{
*/
/** @addtogroup USBD_drivers_standard_request
* @{
*/
/** @defgroup USBD_sdr_exported_functions
* @{
*/
void usbd_setup_request_parse(usb_setup_type *setup, uint8_t *buf);
usb_sts_type usbd_device_request(usbd_core_type *udev);
usb_sts_type usbd_interface_request(usbd_core_type *udev);
usb_sts_type usbd_endpoint_request(usbd_core_type *udev);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,375 @@
/**
**************************************************************************
* @file usbh_core.h
* @version v2.1.0
* @date 2022-08-16
* @brief usb host core 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 __USBH_CORE_H
#define __USBH_CORE_H
#ifdef __cplusplus
extern "C" {
#endif
#include "usb_conf.h"
#include "usb_std.h"
/** @addtogroup AT32F435_437_middlewares_usbh_drivers
* @{
*/
/** @addtogroup USBH_drivers_core
* @{
*/
/** @defgroup USBH_core_exported_types
* @{
*/
#ifdef USE_OTG_HOST_MODE
/**
* @brief usb channel flag
*/
typedef enum
{
HCH_IDLE, /*!< usb host channel idle */
HCH_XFRC, /*!< usb host channel transfer completed */
HCH_HALTED, /*!< usb host channel halted */
HCH_NAK, /*!< usb host channel nak */
HCH_NYET, /*!< usb host channel nyet */
HCH_STALL, /*!< usb host channel stall */
HCH_XACTERR, /*!< usb host channel transaction error */
HCH_BBLERR, /*!< usb host channel babble error */
HCH_DATATGLERR /*!< usb host channel data toggle error */
} hch_sts_type;
/**
* @brief usb channel state
*/
typedef enum
{
URB_IDLE = 0, /*!< usb request idle state */
URB_DONE, /*!< usb request done state */
URB_NOTREADY, /*!< usb request not ready state */
URB_NYET, /*!< usb request nyet stat e*/
URB_ERROR, /*!< usb request error state */
URB_STALL /*!< usb request stall state */
} urb_sts_type;
/**
* @brief usb control channel flag
*/
typedef enum
{
CTRL_START = 0, /*!< usb control request start */
CTRL_XFERC, /*!< usb control request completed */
CTRL_HALTED, /*!< usb control request halted */
CTRL_NAK, /*!< usb control request nak */
CTRL_STALL, /*!< usb control request stall */
CTRL_XACTERR, /*!< usb control request transaction error */
CTRL_BBLERR, /*!< usb control request babble error */
CTRL_DATATGLERR, /*!< usb control request data toggle error */
CTRL_FAIL /*!< usb control request failed */
} ctrl_sts_type;
/**
* @brief usb host control state machine
*/
typedef enum
{
CONTROL_IDLE, /*!< usb control state idle */
CONTROL_SETUP, /*!< usb control state setup */
CONTROL_SETUP_WAIT, /*!< usb control state setup wait */
CONTROL_DATA_IN, /*!< usb control state data in */
CONTROL_DATA_IN_WAIT, /*!< usb control state data in wait */
CONTROL_DATA_OUT, /*!< usb control state data out */
CONTROL_DATA_OUT_WAIT, /*!< usb control state data out wait */
CONTROL_STATUS_IN, /*!< usb control state status in */
CONTROL_STATUS_IN_WAIT, /*!< usb control state status in wait */
CONTROL_STATUS_OUT, /*!< usb control state out */
CONTROL_STATUS_OUT_WAIT, /*!< usb control state out wait */
CONTROL_ERROR, /*!< usb control state error */
CONTROL_STALL, /*!< usb control state stall */
CONTROL_COMPLETE /*!< usb control state complete */
} ctrl_ept0_sts_type;
/**
* @brief usb host enumration state machine
*/
typedef enum
{
ENUM_IDLE, /*!< usb host enumration state idle */
ENUM_GET_MIN_DESC, /*!< usb host enumration state get descriptor 8 byte*/
ENUM_GET_FULL_DESC, /*!< usb host enumration state get descriptor 18 byte*/
ENUM_SET_ADDR, /*!< usb host enumration state set address */
ENUM_GET_CFG, /*!< usb host enumration state get configuration */
ENUM_GET_FULL_CFG, /*!< usb host enumration state get full configuration */
ENUM_GET_MFC_STRING, /*!< usb host enumration state get manufacturer string */
ENUM_GET_PRODUCT_STRING, /*!< usb host enumration state get product string */
ENUM_GET_SERIALNUM_STRING, /*!< usb host enumration state get serial number string */
ENUM_SET_CONFIG, /*!< usb host enumration state set config */
ENUM_COMPLETE, /*!< usb host enumration state complete */
} usbh_enum_sts_type;
/**
* @brief usb host global state machine
*/
typedef enum
{
USBH_IDLE, /*!< usb host global state idle */
USBH_PORT_EN, /*!< usb host global state port enable */
USBH_ATTACHED, /*!< usb host global state attached */
USBH_DISCONNECT, /*!< usb host global state disconnect */
USBH_DEV_SPEED, /*!< usb host global state device speed */
USBH_ENUMERATION, /*!< usb host global state enumeration */
USBH_CLASS_REQUEST, /*!< usb host global state class request */
USBH_CLASS, /*!< usb host global state class */
USBH_CTRL_XFER, /*!< usb host global state control transfer */
USBH_USER_HANDLER, /*!< usb host global state user handler */
USBH_SUSPEND, /*!< usb host global state suspend */
USBH_SUSPENDED, /*!< usb host have in suspend mode */
USBH_WAKEUP, /*!< usb host global state wakeup */
USBH_UNSUPPORT, /*!< usb host global unsupport device */
USBH_ERROR_STATE, /*!< usb host global state error */
} usbh_gstate_type;
/**
* @brief usb host transfer state
*/
typedef enum
{
CMD_IDLE, /*!< usb host transfer state idle */
CMD_SEND, /*!< usb host transfer state send */
CMD_WAIT /*!< usb host transfer state wait */
} cmd_sts_type;
/**
* @brief usb host channel malloc state
*/
#define HCH_OK 0x0000 /*!< usb channel malloc state ok */
#define HCH_USED 0x8000 /*!< usb channel had used */
#define HCH_ERROR 0xFFFF /*!< usb channel error */
#define HCH_USED_MASK 0x7FFF /*!< usb channel use mask */
/**
* @brief channel pid
*/
#define HCH_PID_DATA0 0 /*!< usb channel pid data 0 */
#define HCH_PID_DATA2 1 /*!< usb channel pid data 2 */
#define HCH_PID_DATA1 2 /*!< usb channel pid data 1 */
#define HCH_PID_SETUP 3 /*!< usb channel pid setup */
/**
* @brief channel data transfer direction
*/
#define USB_REQUEST_DIR_MASK 0x80 /*!< usb request direction mask */
#define USB_DIR_H2D USB_REQ_DIR_HTD /*!< usb request direction host to device */
#define USB_DIR_D2H USB_REQ_DIR_DTH /*!< usb request direction device to host */
/**
* @brief request timeout
*/
#define DATA_STAGE_TIMEOUT 5000 /*!< usb data stage timeout */
#define NODATA_STAGE_TIMEOUT 50 /*!< usb no-data stage timeout */
/**
* @brief max interface and endpoint
*/
#define USBH_MAX_ERROR_COUNT 2 /*!< usb support maximum error */
#define USBH_MAX_INTERFACE 5 /*!< usb support maximum interface */
#define USBH_MAX_ENDPOINT 5 /*!< usb support maximum endpoint */
/**
* @brief interface descriptor
*/
typedef struct
{
usb_interface_desc_type interface; /*!< usb device interface descriptor structure */
usb_endpoint_desc_type endpoint[USBH_MAX_ENDPOINT]; /*!< usb device endpoint descriptor structure array */
} usb_itf_desc_type;
/**
* @brief configure descriptor
*/
typedef struct
{
usb_configuration_desc_type cfg; /*!< usb device configuration descriptor structure */
usb_itf_desc_type interface[USBH_MAX_INTERFACE]; /*!< usb device interface descriptor structure array*/
} usb_cfg_desc_type;
/**
* @brief device descriptor
*/
typedef struct
{
uint8_t address; /*!< usb device address */
uint8_t speed; /*!< usb device speed */
usb_device_desc_type dev_desc; /*!< usb device descriptor */
usb_cfg_desc_type cfg_desc; /*!< usb device configuration */
} usbh_dev_desc_type;
/**
* @brief usb host control struct type
*/
typedef struct
{
uint8_t hch_in; /*!< in channel number */
uint8_t hch_out; /*!< out channel number */
uint8_t ept0_size; /*!< endpoint 0 size */
uint8_t *buffer; /*!< endpoint 0 transfer buffer */
usb_setup_type setup; /*!< control setup type */
uint16_t len; /*!< transfer length */
uint8_t err_cnt; /*!< error counter */
uint32_t timer; /*!< transfer timer */
ctrl_sts_type sts; /*!< control transfer status */
ctrl_ept0_sts_type state; /*!< endpoint 0 state */
} usbh_ctrl_type;
/**
* @brief host class handler type
*/
typedef struct
{
usb_sts_type (*init_handler)(void *uhost); /*!< usb host class init handler */
usb_sts_type (*reset_handler)(void *uhost); /*!< usb host class reset handler */
usb_sts_type (*request_handler)(void *uhost); /*!< usb host class request handler */
usb_sts_type (*process_handler)(void *uhost); /*!< usb host class process handler */
void *pdata; /*!< usb host class data */
} usbh_class_handler_type;
/**
* @brief host user handler type
*/
typedef struct
{
usb_sts_type (*user_init)(void); /*!< usb host user init handler */
usb_sts_type (*user_reset)(void); /*!< usb host user reset handler */
usb_sts_type (*user_attached)(void); /*!< usb host user attached handler */
usb_sts_type (*user_disconnect)(void); /*!< usb host user disconnect handler */
usb_sts_type (*user_speed)(uint8_t speed); /*!< usb host user speed handler */
usb_sts_type (*user_mfc_string)(void *); /*!< usb host user manufacturer string handler */
usb_sts_type (*user_product_string)(void *); /*!< usb host user product string handler */
usb_sts_type (*user_serial_string)(void *); /*!< usb host user serial handler */
usb_sts_type (*user_enumeration_done)(void); /*!< usb host user enumeration done handler */
usb_sts_type (*user_application)(void); /*!< usb host user application handler */
usb_sts_type (*user_active_vbus)(void *uhost, confirm_state state); /*!< usb host user active vbus */
usb_sts_type (*user_not_support)(void); /*!< usb host user not support handler */
} usbh_user_handler_type;
/**
* @brief host host core handler type
*/
typedef struct
{
usb_reg_type *usb_reg; /*!< usb register pointer */
uint8_t global_state; /*!< usb host global state machine */
uint8_t enum_state; /*!< usb host enumeration state machine */
uint8_t req_state; /*!< usb host request state machine */
usbh_dev_desc_type dev; /*!< usb device descriptor */
usbh_ctrl_type ctrl; /*!< usb host control transfer struct */
usbh_class_handler_type *class_handler; /*!< usb host class handler pointer */
usbh_user_handler_type *user_handler; /*!< usb host user handler pointer */
usb_hch_type hch[USB_HOST_CHANNEL_NUM]; /*!< usb host channel array */
uint8_t rx_buffer[USB_MAX_DATA_LENGTH]; /*!< usb host rx buffer */
uint32_t conn_sts; /*!< connect status */
uint32_t port_enable; /*!< port enable status */
uint32_t timer; /*!< sof timer */
uint32_t err_cnt[USB_HOST_CHANNEL_NUM]; /*!< error counter */
uint32_t xfer_cnt[USB_HOST_CHANNEL_NUM]; /*!< xfer counter */
hch_sts_type hch_state[USB_HOST_CHANNEL_NUM];/*!< channel state */
urb_sts_type urb_state[USB_HOST_CHANNEL_NUM];/*!< usb request state */
uint16_t channel[USB_HOST_CHANNEL_NUM]; /*!< channel array */
} usbh_core_type;
void usbh_free_channel(usbh_core_type *uhost, uint8_t index);
uint16_t usbh_get_free_channel(usbh_core_type *uhost);
usb_sts_type usbh_set_toggle(usbh_core_type *uhost, uint8_t hc_num, uint8_t toggle);
usb_sts_type usbh_in_out_request(usbh_core_type *uhost, uint8_t hc_num);
usb_sts_type usbh_interrupt_recv(usbh_core_type *uhost, uint8_t hc_num,
uint8_t *buffer, uint16_t length);
usb_sts_type usbh_interrupt_send(usbh_core_type *uhost, uint8_t hc_num,
uint8_t *buffer, uint16_t length);
usb_sts_type usbh_bulk_recv(usbh_core_type *uhost, uint8_t hc_num,
uint8_t *buffer, uint16_t length);
usb_sts_type usbh_bulk_send(usbh_core_type *uhost, uint8_t hc_num,
uint8_t *buffer, uint16_t length);
usb_sts_type usbh_isoc_send(usbh_core_type *uhost, uint8_t hc_num,
uint8_t *buffer, uint16_t length);
usb_sts_type usbh_isoc_recv(usbh_core_type *uhost, uint8_t hc_num,
uint8_t *buffer, uint16_t length);
usb_sts_type usbh_cfg_default_init(usbh_core_type *uhost);
void usbh_enter_suspend(usbh_core_type *uhost);
void usbh_resume(usbh_core_type *uhost);
uint16_t usbh_alloc_channel(usbh_core_type *uhost, uint8_t ept_addr);
urb_sts_type usbh_get_urb_status(usbh_core_type *uhost, uint8_t ch_num);
usb_sts_type usbh_ctrl_result_check(usbh_core_type *uhost,
ctrl_ept0_sts_type next_ctrl_state,
uint8_t next_enum_state);
uint8_t usbh_alloc_address(void);
void usbh_reset_port(usbh_core_type *uhost);
usb_sts_type usbh_loop_handler(usbh_core_type *uhost);
void usbh_ch_disable(usbh_core_type *uhost, uint8_t chn);
void usbh_hc_open(usbh_core_type *uhost,
uint8_t chn,
uint8_t ept_num,
uint8_t dev_address,
uint8_t type,
uint16_t maxpacket,
uint8_t speed);
void usbh_active_vbus(usbh_core_type *uhost, confirm_state state);
usb_sts_type usbh_core_init(usbh_core_type *uhost,
usb_reg_type *usb_reg,
usbh_class_handler_type *class_handler,
usbh_user_handler_type *user_handler,
uint8_t core_id);
#endif
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,109 @@
/**
**************************************************************************
* @file usbh_ctrl.h
* @version v2.1.0
* @date 2022-08-16
* @brief usb 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 __USBH_CTRL_H
#define __USBH_CTRL_H
#ifdef __cplusplus
extern "C" {
#endif
/* includes ------------------------------------------------------------------*/
#include "usb_conf.h"
#include "usbh_core.h"
/** @addtogroup AT32F435_437_middlewares_usbh_drivers
* @{
*/
/** @addtogroup USBH_drivers_control
* @{
*/
/** @defgroup USBH_ctrl_exported_types
* @{
*/
usb_sts_type usbh_ctrl_send_setup(usbh_core_type *uhost, uint8_t *buffer, uint8_t hc_num);
usb_sts_type usbh_ctrl_recv_data(usbh_core_type *uhost, uint8_t *buffer,
uint16_t length, uint16_t hc_num);
usb_sts_type usbh_ctrl_send_data(usbh_core_type *uhost, uint8_t *buffer,
uint16_t length, uint16_t hc_num);
usb_sts_type usbh_ctrl_setup_handler(usbh_core_type *uhost);
usb_sts_type usbh_ctrl_setup_wait_handler(usbh_core_type *uhost, uint32_t *timeout);
usb_sts_type usbh_ctrl_data_in_handler(usbh_core_type *uhost);
usb_sts_type usbh_ctrl_data_in_wait_handler(usbh_core_type *uhost, uint32_t timeout);
usb_sts_type usbh_ctrl_data_out_handler(usbh_core_type *uhost);
usb_sts_type usbh_ctrl_data_out_wait_handler(usbh_core_type *uhost, uint32_t timeout);
usb_sts_type usbh_ctrl_status_in_handler(usbh_core_type *uhost);
usb_sts_type usbh_ctrl_status_in_wait_handler(usbh_core_type *uhost, uint32_t timeout);
usb_sts_type usbh_ctrl_status_out_handler(usbh_core_type *uhost);
usb_sts_type usbh_ctrl_status_out_wait_handler(usbh_core_type *uhost, uint32_t timeout);
usb_sts_type usbh_ctrl_error_handler(usbh_core_type *uhost);
usb_sts_type usbh_ctrl_stall_handler(usbh_core_type *uhost);
usb_sts_type usbh_ctrl_complete_handler(usbh_core_type *uhost);
usb_sts_type usbh_ctrl_transfer_loop(usbh_core_type *uhost);
usb_sts_type usbh_ctrl_request(usbh_core_type *uhost, uint8_t *buffer, uint16_t length);
usb_sts_type usbh_get_descriptor(usbh_core_type *uhost, uint16_t length,
uint8_t req_type, uint16_t wvalue,
uint8_t *buffer);
void usbh_parse_dev_desc(usbh_core_type *uhost, uint8_t *buffer, uint16_t length);
usb_header_desc_type *usbh_get_next_header(uint8_t *buf, uint16_t *index_len);
void usbh_parse_interface_desc(usb_interface_desc_type *intf, uint8_t *buf);
void usbh_parse_endpoint_desc(usb_endpoint_desc_type *ept_desc, uint8_t *buf);
usb_sts_type usbh_parse_configure_desc(usbh_core_type *uhost,
uint8_t *buffer, uint16_t length);
uint8_t usbh_find_interface(usbh_core_type *uhost, uint8_t class_code, uint8_t sub_class, uint8_t protocol);
void usbh_parse_string_desc(uint8_t *src, uint8_t *dest, uint16_t length);
usb_sts_type usbh_get_device_descriptor(usbh_core_type *uhost, uint16_t length);
usb_sts_type usbh_get_configure_descriptor(usbh_core_type *uhost, uint16_t length);
usb_sts_type usbh_get_sting_descriptor(usbh_core_type *uhost, uint8_t string_id,
uint8_t *buffer, uint16_t length);
usb_sts_type usbh_set_configuration(usbh_core_type *uhost, uint16_t config);
usb_sts_type usbh_set_address(usbh_core_type *uhost, uint8_t address);
usb_sts_type usbh_set_interface(usbh_core_type *uhost, uint8_t ept_num, uint8_t altsetting);
usb_sts_type usbh_set_feature(usbh_core_type *uhost, uint8_t feature, uint16_t index);
usb_sts_type usbh_clear_dev_feature(usbh_core_type *uhost, uint8_t feature, uint16_t index);
usb_sts_type usbh_clear_ept_feature(usbh_core_type *uhost, uint8_t ept_num, uint8_t hc_num);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,77 @@
/**
**************************************************************************
* @file usbh_int.h
* @version v2.1.0
* @date 2022-08-16
* @brief usb 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 __USBH_INT_H
#define __USBH_INT_H
#ifdef __cplusplus
extern "C" {
#endif
/** @addtogroup AT32F435_437_middlewares_usbh_drivers
* @{
*/
/** @addtogroup USBH_drivers_int
* @{
*/
/** @defgroup USBH_interrupt_exported_types
* @{
*/
/* includes ------------------------------------------------------------------*/
#include "usbh_core.h"
#include "usb_core.h"
void usbh_irq_handler(otg_core_type *hdev);
void usbh_hch_handler(usbh_core_type *uhost);
void usbh_port_handler(usbh_core_type *uhost);
void usbh_disconnect_handler(usbh_core_type *uhost);
void usbh_hch_in_handler(usbh_core_type *uhost, uint8_t chn);
void usbh_hch_out_handler(usbh_core_type *uhost, uint8_t chn);
void usbh_rx_qlvl_handler(usbh_core_type *uhost);
void usbh_wakeup_handler(usbh_core_type *uhost);
void usbh_sof_handler(usbh_core_type *uhost);
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,188 @@
/**
**************************************************************************
* @file usb_core.c
* @version v2.1.0
* @date 2022-08-16
* @brief usb driver
**************************************************************************
* 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_core.h"
/** @addtogroup AT32F435_437_middlewares_usb_drivers
* @{
*/
/** @defgroup USB_drivers_core
* @brief usb global drivers core
* @{
*/
/** @defgroup USB_core_private_functions
* @{
*/
usb_sts_type usb_core_config(otg_core_type *udev, uint8_t core_id);
/**
* @brief usb core config
* @param otgdev: to the structure of otg_core_type
* @param core_id: usb core id number (USB_FULL_SPEED_CORE_ID)
* @retval usb_sts_type
*/
usb_sts_type usb_core_config(otg_core_type *otgdev, uint8_t core_id)
{
/* set usb speed and core id */
otgdev->cfg.speed = core_id;
otgdev->cfg.core_id = core_id;
/* default sof out and vbus ignore */
otgdev->cfg.sof_out = FALSE;
otgdev->cfg.vbusig = FALSE;
/* set max size */
otgdev->cfg.max_size = 64;
/* set support number of channel and endpoint */
#ifdef USE_OTG_HOST_MODE
otgdev->cfg.hc_num = USB_HOST_CHANNEL_NUM;
#endif
#ifdef USE_OTG_DEVICE_MODE
otgdev->cfg.ept_num = USB_EPT_MAX_NUM;
#endif
otgdev->cfg.fifo_size = OTG_FIFO_SIZE;
if(core_id == USB_FULL_SPEED_CORE_ID)
{
otgdev->cfg.phy_itface = 2;
}
#ifdef USB_SOF_OUTPUT_ENABLE
otgdev->cfg.sof_out = TRUE;
#endif
#ifdef USB_VBUS_IGNORE
otgdev->cfg.vbusig = TRUE;
#endif
return USB_OK;
}
#ifdef USE_OTG_DEVICE_MODE
/**
* @brief usb device initialization
* @param otgdev: to the structure of otg_core_type
* @param core_id: usb core id number (USB_FULL_SPEED_CORE_ID)
* @param usb_id: select use OTG1 or OTG2
* this parameter can be one of the following values:
* - USB_OTG1_ID
* - USB_OTG2_ID
* @param dev_handler: usb class callback handler
* @param desc_handler: device config callback handler
* @retval usb_sts_type
*/
usb_sts_type usbd_init(otg_core_type *otgdev,
uint8_t core_id, uint8_t usb_id,
usbd_class_handler *class_handler,
usbd_desc_handler *desc_handler)
{
usb_sts_type usb_sts = USB_OK;
/* select use OTG1 or OTG2 */
otgdev->usb_reg = usb_global_select_core(usb_id);
/* usb device core config */
usb_core_config(otgdev, core_id);
if(otgdev->cfg.sof_out)
{
otgdev->usb_reg->gccfg_bit.sofouten = TRUE;
}
if(otgdev->cfg.vbusig)
{
otgdev->usb_reg->gccfg_bit.vbusig = TRUE;
}
/* usb device core init */
usbd_core_init(&(otgdev->dev), otgdev->usb_reg,
class_handler,
desc_handler,
core_id);
return usb_sts;
}
#endif
#ifdef USE_OTG_HOST_MODE
/**
* @brief usb host initialization.
* @param otgdev: to the structure of otg_core_type
* @param core_id: usb core id number (USB_FULL_SPEED_CORE_ID)
* @param usb_id: select use OTG1 or OTG2
* this parameter can be one of the following values:
* - USB_OTG1_ID
* - USB_OTG2_ID
* @param class_handler: usb class callback handler
* @param user_handler: user callback handler
* @retval usb_sts_type
*/
usb_sts_type usbh_init(otg_core_type *otgdev,
uint8_t core_id, uint8_t usb_id,
usbh_class_handler_type *class_handler,
usbh_user_handler_type *user_handler)
{
usb_sts_type status = USB_OK;
/* select use otg1 or otg2 */
otgdev->usb_reg = usb_global_select_core(usb_id);
/* usb core config */
usb_core_config(otgdev, core_id);
if(otgdev->cfg.sof_out)
{
otgdev->usb_reg->gccfg_bit.sofouten = TRUE;
}
if(otgdev->cfg.vbusig)
{
otgdev->usb_reg->gccfg_bit.vbusig = TRUE;
}
/* usb host core init */
usbh_core_init(&otgdev->host, otgdev->usb_reg,
class_handler,
user_handler,
core_id);
return status;
}
#endif
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
@@ -0,0 +1,884 @@
/**
**************************************************************************
* @file usbd_core.c
* @version v2.1.0
* @date 2022-08-16
* @brief usb device driver
**************************************************************************
* 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_core.h"
#include "usbd_core.h"
#include "usbd_sdr.h"
/** @addtogroup AT32F435_437_middlewares_usbd_drivers
* @{
*/
/** @defgroup USBD_drivers_core
* @brief usb device drivers core
* @{
*/
/** @defgroup USBD_core_private_functions
* @{
*/
/**
* @brief usb core in transfer complete handler
* @param udev: to the structure of usbd_core_type
* @param ept_addr: endpoint number
* @retval none
*/
void usbd_core_in_handler(usbd_core_type *udev, uint8_t ept_addr)
{
/* get endpoint info*/
usb_ept_info *ept_info = &udev->ept_in[ept_addr & 0x7F];
if(ept_addr == 0)
{
if(udev->ept0_sts == USB_EPT0_DATA_IN)
{
if(ept_info->rem0_len > ept_info->maxpacket)
{
ept_info->rem0_len -= ept_info->maxpacket;
usbd_ept_send(udev, 0, ept_info->trans_buf,
MIN(ept_info->rem0_len, ept_info->maxpacket));
}
/* endpoint 0 */
else if(ept_info->last_len == ept_info->maxpacket
&& ept_info->ept0_slen >= ept_info->maxpacket
&& ept_info->ept0_slen < udev->ept0_wlength)
{
ept_info->last_len = 0;
usbd_ept_send(udev, 0, 0, 0);
usbd_ept_recv(udev, ept_addr, 0, 0);
}
else
{
if(udev->class_handler->ept0_tx_handler != 0 &&
udev->conn_state == USB_CONN_STATE_CONFIGURED)
{
udev->class_handler->ept0_tx_handler(udev);
}
usbd_ctrl_recv_status(udev);
}
}
}
else if(udev->class_handler->in_handler != 0 &&
udev->conn_state == USB_CONN_STATE_CONFIGURED)
{
/* other user define endpoint */
udev->class_handler->in_handler(udev, ept_addr);
}
}
/**
* @brief usb core out transfer complete handler
* @param udev: to the structure of usbd_core_type
* @param ept_addr: endpoint number
* @retval none
*/
void usbd_core_out_handler(usbd_core_type *udev, uint8_t ept_addr)
{
/* get endpoint info*/
usb_ept_info *ept_info = &udev->ept_out[ept_addr & 0x7F];
if(ept_addr == 0)
{
/* endpoint 0 */
if(udev->ept0_sts == USB_EPT0_DATA_OUT)
{
if(ept_info->rem0_len > ept_info->maxpacket)
{
ept_info->rem0_len -= ept_info->maxpacket;
usbd_ept_recv(udev, ept_addr, ept_info->trans_buf,
MIN(ept_info->rem0_len, ept_info->maxpacket));
}
else
{
if(udev->class_handler->ept0_rx_handler != 0)
{
udev->class_handler->ept0_rx_handler(udev);
}
usbd_ctrl_send_status(udev);
}
}
}
else if(udev->class_handler->out_handler != 0 &&
udev->conn_state == USB_CONN_STATE_CONFIGURED)
{
/* other user define endpoint */
udev->class_handler->out_handler(udev, ept_addr);
}
}
/**
* @brief usb core setup transfer complete handler
* @param udev: to the structure of usbd_core_type
* @param ept_addr: endpoint number
* @retval none
*/
void usbd_core_setup_handler(usbd_core_type *udev, uint8_t ept_num)
{
UNUSED(ept_num);
/* setup parse */
usbd_setup_request_parse(&udev->setup, udev->setup_buffer);
/* set ept0 status */
udev->ept0_sts = USB_EPT0_SETUP;
udev->ept0_wlength = udev->setup.wLength;
switch(udev->setup.bmRequestType & USB_REQ_RECIPIENT_MASK)
{
case USB_REQ_RECIPIENT_DEVICE:
/* recipient device request */
usbd_device_request(udev);
break;
case USB_REQ_RECIPIENT_INTERFACE:
/* recipient interface request */
usbd_interface_request(udev);
break;
case USB_REQ_RECIPIENT_ENDPOINT:
/* recipient endpoint request */
usbd_endpoint_request(udev);
break;
default:
break;
}
}
/**
* @brief usb control endpoint send data
* @param udev: to the structure of usbd_core_type
* @param ept_addr: endpoint number
* @param buffer: send data buffer
* @param len: send data length
* @retval none
*/
void usbd_ctrl_send(usbd_core_type *udev, uint8_t *buffer, uint16_t len)
{
usb_ept_info *ept_info = &udev->ept_in[0];
ept_info->ept0_slen = len;
ept_info->rem0_len = len;
udev->ept0_sts = USB_EPT0_DATA_IN;
usbd_ept_send(udev, 0, buffer, len);
}
/**
* @brief usb control endpoint recv data
* @param udev: to the structure of usbd_core_type
* @param ept_addr: endpoint number
* @param buffer: recv data buffer
* @param len: recv data length
* @retval none
*/
void usbd_ctrl_recv(usbd_core_type *udev, uint8_t *buffer, uint16_t len)
{
usb_ept_info *ept_info = &udev->ept_out[0];
ept_info->ept0_slen = len;
ept_info->rem0_len = len;
udev->ept0_sts = USB_EPT0_DATA_OUT;
usbd_ept_recv(udev, 0, buffer, len);
}
/**
* @brief usb control endpoint send in status
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_ctrl_send_status(usbd_core_type *udev)
{
udev->ept0_sts = USB_EPT0_STATUS_IN;
usbd_ept_send(udev, 0, 0, 0);
}
/**
* @brief usb control endpoint send out status
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_ctrl_recv_status(usbd_core_type *udev)
{
udev->ept0_sts = USB_EPT0_STATUS_OUT;
usbd_ept_recv(udev, 0, 0, 0);
}
/**
* @brief clear endpoint stall
* @param udev: to the structure of usbd_core_type
* @param ept_addr: endpoint number
* @retval none
*/
void usbd_clear_stall(usbd_core_type *udev, uint8_t ept_addr)
{
usb_ept_info *ept_info;
usb_reg_type *usbx = udev->usb_reg;
if(ept_addr & 0x80)
{
/* in endpoint */
ept_info = &udev->ept_in[ept_addr & 0x7F];
}
else
{
/* out endpoint */
ept_info = &udev->ept_out[ept_addr & 0x7F];
}
usb_ept_clear_stall(usbx, ept_info);
ept_info->stall = 0;
}
/**
* @brief usb set endpoint to stall status
* @param udev: to the structure of usbd_core_type
* @param ept_addr: endpoint number
* @retval none
*/
void usbd_set_stall(usbd_core_type *udev, uint8_t ept_addr)
{
usb_ept_info *ept_info;
usb_reg_type *usbx = udev->usb_reg;
if(ept_addr & 0x80)
{
/* in endpoint */
ept_info = &udev->ept_in[ept_addr & 0x7F];
}
else
{
/* out endpoint */
ept_info = &udev->ept_out[ept_addr & 0x7F];
}
usb_ept_stall(usbx, ept_info);
ept_info->stall = 1;
}
/**
* @brief un-support device request
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_ctrl_unsupport(usbd_core_type *udev)
{
/* return stall status */
usbd_set_stall(udev, 0x00);
usbd_set_stall(udev, 0x80);
}
/**
* @brief get endpoint receive data length
* @param udev: to the structure of usbd_core_type
* @param ept_addr: endpoint number
* @retval data receive len
*/
uint32_t usbd_get_recv_len(usbd_core_type *udev, uint8_t ept_addr)
{
usb_ept_info *ept = &udev->ept_out[ept_addr & 0x7F];
return ept->trans_len;
}
/**
* @brief usb open endpoint
* @param udev: to the structure of usbd_core_type
* @param ept_addr: endpoint number
* @param ept_type: endpoint type
* @param maxpacket: endpoint support max buffer size
* @retval none
*/
void usbd_ept_open(usbd_core_type *udev, uint8_t ept_addr, uint8_t ept_type, uint16_t maxpacket)
{
usb_reg_type *usbx = udev->usb_reg;
usb_ept_info *ept_info;
if((ept_addr & 0x80) == 0)
{
/* out endpoint info */
ept_info = &udev->ept_out[ept_addr & 0x7F];
ept_info->inout = EPT_DIR_OUT;
}
else
{
/* in endpoint info */
ept_info = &udev->ept_in[ept_addr & 0x7F];
ept_info->inout = EPT_DIR_IN;
}
/* set endpoint maxpacket and type */
ept_info->maxpacket = maxpacket;
ept_info->trans_type = ept_type;
/* open endpoint */
usb_ept_open(usbx, ept_info);
}
/**
* @brief usb close endpoint
* @param udev: to the structure of usbd_core_type
* @param ept_addr: endpoint number
* @retval none
*/
void usbd_ept_close(usbd_core_type *udev, uint8_t ept_addr)
{
usb_ept_info *ept_info;
if(ept_addr & 0x80)
{
/* in endpoint */
ept_info = &udev->ept_in[ept_addr & 0x7F];
}
else
{
/* out endpoint */
ept_info = &udev->ept_out[ept_addr & 0x7F];
}
/* close endpoint */
usb_ept_close(udev->usb_reg, ept_info);
}
/**
* @brief usb device connect to host
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_connect(usbd_core_type *udev)
{
usb_connect(udev->usb_reg);
}
/**
* @brief usb device disconnect to host
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_disconnect(usbd_core_type *udev)
{
usb_disconnect(udev->usb_reg);
}
/**
* @brief usb device set device address.
* @param udev: to the structure of usbd_core_type
* @param address: host assignment address
* @retval none
*/
void usbd_set_device_addr(usbd_core_type *udev, uint8_t address)
{
usb_set_address(udev->usb_reg, address);
}
/**
* @brief usb endpoint structure initialization
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usb_ept_default_init(usbd_core_type *udev)
{
uint8_t i_index = 0;
/* init in endpoint info structure */
for(i_index = 0; i_index < USB_EPT_MAX_NUM; i_index ++)
{
udev->ept_in[i_index].eptn = i_index;
udev->ept_in[i_index].ept_address = i_index;
udev->ept_in[i_index].inout = EPT_DIR_IN;
udev->ept_in[i_index].maxpacket = 0;
udev->ept_in[i_index].trans_buf = 0;
udev->ept_in[i_index].total_len = 0;
}
/* init out endpoint info structure */
for(i_index = 0; i_index < USB_EPT_MAX_NUM; i_index ++)
{
udev->ept_out[i_index].eptn = i_index;
udev->ept_out[i_index].ept_address = i_index;
udev->ept_out[i_index].inout = EPT_DIR_OUT;
udev->ept_out[i_index].maxpacket = 0;
udev->ept_out[i_index].trans_buf = 0;
udev->ept_out[i_index].total_len = 0;
}
}
/**
* @brief endpoint send data
* @param udev: to the structure of usbd_core_type
* @param ept_addr: endpoint number
* @param buffer: send data buffer
* @param len: send data length
* @retval none
*/
void usbd_ept_send(usbd_core_type *udev, uint8_t ept_addr, uint8_t *buffer, uint16_t len)
{
/* get endpoint info struct and register */
usb_reg_type *usbx = udev->usb_reg;
usb_ept_info *ept_info = &udev->ept_in[ept_addr & 0x7F];
otg_eptin_type *ept_in = USB_INEPT(usbx, ept_info->eptn);
otg_device_type *dev = OTG_DEVICE(usbx);
uint32_t pktcnt;
/* set send data buffer and length */
ept_info->trans_buf = buffer;
ept_info->total_len = len;
ept_info->trans_len = 0;
/* transfer data len is zero */
if(ept_info->total_len == 0)
{
ept_in->dieptsiz_bit.pktcnt = 1;
ept_in->dieptsiz_bit.xfersize = 0;
}
else
{
if((ept_addr & 0x7F) == 0) // endpoint 0
{
/* endpoint 0 */
if(ept_info->total_len > ept_info->maxpacket)
{
ept_info->total_len = ept_info->maxpacket;
}
/* set transfer size */
ept_in->dieptsiz_bit.xfersize = ept_info->total_len;
/* set packet count */
ept_in->dieptsiz_bit.pktcnt = 1;
ept_info->last_len = ept_info->total_len;
}
else
{
/* other endpoint */
/* packet count */
pktcnt = (ept_info->total_len + ept_info->maxpacket - 1) / ept_info->maxpacket;
/* set transfer size */
ept_in->dieptsiz_bit.xfersize = ept_info->total_len;
/* set packet count */
ept_in->dieptsiz_bit.pktcnt = pktcnt;
if(ept_info->trans_type == EPT_ISO_TYPE)
{
ept_in->dieptsiz_bit.mc = 1;
}
}
}
if(ept_info->trans_type != EPT_ISO_TYPE)
{
if(ept_info->total_len > 0)
{
/* set in endpoint tx fifo empty interrupt mask */
dev->diepempmsk |= 1 << ept_info->eptn;
}
}
if(ept_info->trans_type == EPT_ISO_TYPE)
{
if((dev->dsts_bit.soffn & 0x1) == 0)
{
ept_in->diepctl_bit.setd1pid = TRUE;
}
else
{
ept_in->diepctl_bit.setd0pid = TRUE;
}
}
/* clear endpoint nak */
ept_in->diepctl_bit.cnak = TRUE;
/* endpoint enable */
ept_in->diepctl_bit.eptena = TRUE;
if(ept_info->trans_type == EPT_ISO_TYPE)
{
/* write data to fifo */
usb_write_packet(usbx, ept_info->trans_buf, ept_info->eptn, ept_info->total_len);
}
}
/**
* @brief endpoint receive data
* @param udev: to the structure of usbd_core_type
* @param ept_addr: endpoint number
* @param buffer: receive data buffer
* @param len: receive data length
* @retval none
*/
void usbd_ept_recv(usbd_core_type *udev, uint8_t ept_addr, uint8_t *buffer, uint16_t len)
{
/* get endpoint info struct and register */
usb_reg_type *usbx = udev->usb_reg;
usb_ept_info *ept_info = &udev->ept_out[ept_addr & 0x7F];
otg_eptout_type *ept_out = USB_OUTEPT(usbx, ept_info->eptn);
otg_device_type *dev = OTG_DEVICE(usbx);
uint32_t pktcnt;
/* set receive data buffer and length */
ept_info->trans_buf = buffer;
ept_info->total_len = len;
ept_info->trans_len = 0;
if((ept_addr & 0x7F) == 0)
{
/* endpoint 0 */
ept_info->total_len = ept_info->maxpacket;
}
if(ept_info->total_len == 0 || ((ept_addr & 0x7F) == 0))
{
/* set transfer size */
ept_out->doeptsiz_bit.xfersize = ept_info->maxpacket;
/* set packet count */
ept_out->doeptsiz_bit.pktcnt = 1;
}
else
{
pktcnt = (ept_info->total_len + ept_info->maxpacket - 1) / ept_info->maxpacket;
/* set transfer size */
ept_out->doeptsiz_bit.xfersize = ept_info->maxpacket * pktcnt;
/* set packet count */
ept_out->doeptsiz_bit.pktcnt = pktcnt;
}
if(ept_info->trans_type == EPT_ISO_TYPE)
{
if((dev->dsts_bit.soffn & 0x01) == 0)
{
ept_out->doepctl_bit.setd1pid = TRUE;
}
else
{
ept_out->doepctl_bit.setd0pid = TRUE;
}
}
/* clear endpoint nak */
ept_out->doepctl_bit.cnak = TRUE;
/* endpoint enable */
ept_out->doepctl_bit.eptena = TRUE;
}
/**
* @brief get usb connect state
* @param udev: to the structure of usbd_core_type
* @retval usb connect state
*/
usbd_conn_state usbd_connect_state_get(usbd_core_type *udev)
{
return udev->conn_state;
}
/**
* @brief usb device remote wakeup
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_remote_wakeup(usbd_core_type *udev)
{
/* check device is in suspend mode */
if(usb_suspend_status_get(udev->usb_reg) == 1)
{
/* set connect state */
udev->conn_state = udev->old_conn_state;
/* open phy clock */
usb_open_phy_clk(udev->usb_reg);
/* set remote wakeup */
usb_remote_wkup_set(udev->usb_reg);
/* delay 10 ms */
usb_delay_ms(10);
/* clear remote wakup */
usb_remote_wkup_clear(udev->usb_reg);
}
}
/**
* @brief usb device enter suspend mode
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_enter_suspend(usbd_core_type *udev)
{
/* check device is in suspend mode */
if(usb_suspend_status_get(udev->usb_reg) == 1)
{
/* stop phy clk */
usb_stop_phy_clk(udev->usb_reg);
}
}
/**
* @brief usb device flush in endpoint fifo
* @param udev: to the structure of usbd_core_type
* @param ept_num: endpoint number
* @retval none
*/
void usbd_flush_tx_fifo(usbd_core_type *udev, uint8_t ept_num)
{
/* flush endpoint tx fifo */
usb_flush_tx_fifo(udev->usb_reg, ept_num & 0x1F);
}
/**
* @brief usb device endpoint fifo alloc
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_fifo_alloc(usbd_core_type *udev)
{
usb_reg_type *usbx = udev->usb_reg;
if(usbx == OTG1_GLOBAL)
{
/* set receive fifo size */
usb_set_rx_fifo(usbx, USBD_RX_SIZE);
/* set endpoint0 tx fifo size */
usb_set_tx_fifo(usbx, USB_EPT0, USBD_EP0_TX_SIZE);
/* set endpoint1 tx fifo size */
usb_set_tx_fifo(usbx, USB_EPT1, USBD_EP1_TX_SIZE);
/* set endpoint2 tx fifo size */
usb_set_tx_fifo(usbx, USB_EPT2, USBD_EP2_TX_SIZE);
/* set endpoint3 tx fifo size */
usb_set_tx_fifo(usbx, USB_EPT3, USBD_EP3_TX_SIZE);
if(USB_EPT_MAX_NUM == 8)
{
/* set endpoint4 tx fifo size */
usb_set_tx_fifo(usbx, USB_EPT4, USBD_EP4_TX_SIZE);
/* set endpoint5 tx fifo size */
usb_set_tx_fifo(usbx, USB_EPT5, USBD_EP5_TX_SIZE);
/* set endpoint6 tx fifo size */
usb_set_tx_fifo(usbx, USB_EPT6, USBD_EP6_TX_SIZE);
/* set endpoint7 tx fifo size */
usb_set_tx_fifo(usbx, USB_EPT7, USBD_EP7_TX_SIZE);
}
}
#ifdef OTG2_GLOBAL
if(usbx == OTG2_GLOBAL)
{
/* set receive fifo size */
usb_set_rx_fifo(usbx, USBD2_RX_SIZE);
/* set endpoint0 tx fifo size */
usb_set_tx_fifo(usbx, USB_EPT0, USBD2_EP0_TX_SIZE);
/* set endpoint1 tx fifo size */
usb_set_tx_fifo(usbx, USB_EPT1, USBD2_EP1_TX_SIZE);
/* set endpoint2 tx fifo size */
usb_set_tx_fifo(usbx, USB_EPT2, USBD2_EP2_TX_SIZE);
/* set endpoint3 tx fifo size */
usb_set_tx_fifo(usbx, USB_EPT3, USBD2_EP3_TX_SIZE);
if(USB_EPT_MAX_NUM == 8)
{
/* set endpoint4 tx fifo size */
usb_set_tx_fifo(usbx, USB_EPT4, USBD2_EP4_TX_SIZE);
/* set endpoint5 tx fifo size */
usb_set_tx_fifo(usbx, USB_EPT5, USBD2_EP5_TX_SIZE);
/* set endpoint6 tx fifo size */
usb_set_tx_fifo(usbx, USB_EPT6, USBD2_EP6_TX_SIZE);
/* set endpoint7 tx fifo size */
usb_set_tx_fifo(usbx, USB_EPT7, USBD2_EP7_TX_SIZE);
}
}
#endif
}
/**
* @brief usb device core initialization
* @param udev: to the structure of usbd_core_type
* @param usb_reg: usb otgfs peripheral global register
* this parameter can be one of the following values:
* OTG1_GLOBAL , OTG2_GLOBAL
* @param class_handler: usb class handler
* @param desc_handler: device config handler
* @param core_id: usb core id number
* @retval usb_sts_type
*/
usb_sts_type usbd_core_init(usbd_core_type *udev,
usb_reg_type *usb_reg,
usbd_class_handler *class_handler,
usbd_desc_handler *desc_handler,
uint8_t core_id)
{
UNUSED(core_id);
usb_reg_type *usbx;
otg_device_type *dev;
otg_eptin_type *ept_in;
otg_eptout_type *ept_out;
uint32_t i_index;
udev->usb_reg = usb_reg;
usbx = usb_reg;
dev = OTG_DEVICE(usbx);
/* set connect state */
udev->conn_state = USB_CONN_STATE_DEFAULT;
/* device class config */
udev->device_addr = 0;
udev->class_handler = class_handler;
udev->desc_handler = desc_handler;
/* set device disconnect */
usbd_disconnect(udev);
/* set endpoint to default status */
usb_ept_default_init(udev);
/* disable usb global interrupt */
usb_interrupt_disable(usbx);
/* init global register */
usb_global_init(usbx);
/* set device mode */
usb_global_set_mode(usbx, OTG_DEVICE_MODE);
/* open phy clock */
usb_open_phy_clk(udev->usb_reg);
/* set periodic frame interval */
dev->dcfg_bit.perfrint = DCFG_PERFRINT_80;
/* set device speed to full-speed */
dev->dcfg_bit.devspd = USB_DCFG_FULL_SPEED;
/* flush all tx fifo */
usb_flush_tx_fifo(usbx, 16);
/* flush share rx fifo */
usb_flush_rx_fifo(usbx);
/* clear all endpoint interrupt flag and mask */
dev->daint = 0xFFFFFFFF;
dev->daintmsk = 0;
dev->diepmsk = 0;
dev->doepmsk = 0;
for(i_index = 0; i_index < USB_EPT_MAX_NUM; i_index ++)
{
usbx->dieptxfn[i_index] = 0;
}
/* endpoint fifo alloc */
usbd_fifo_alloc(udev);
/* disable all in endpoint */
for(i_index = 0; i_index < USB_EPT_MAX_NUM; i_index ++)
{
ept_in = USB_INEPT(usbx, i_index);
if(ept_in->diepctl_bit.eptena)
{
ept_in->diepctl = 0;
ept_in->diepctl_bit.eptdis = TRUE;
ept_in->diepctl_bit.snak = TRUE;
}
else
{
ept_in->diepctl = 0;
}
ept_in->dieptsiz = 0;
ept_in->diepint = 0xFF;
}
/* disable all out endpoint */
for(i_index = 0; i_index < USB_EPT_MAX_NUM; i_index ++)
{
ept_out = USB_OUTEPT(usbx, i_index);
if(ept_out->doepctl_bit.eptena)
{
ept_out->doepctl = 0;
ept_out->doepctl_bit.eptdis = TRUE;
ept_out->doepctl_bit.snak = TRUE;
}
else
{
ept_out->doepctl = 0;
}
ept_out->doeptsiz = 0;
ept_out->doepint = 0xFF;
}
dev->diepmsk_bit.txfifoudrmsk = TRUE;
/* clear global interrupt and mask */
usbx->gintmsk = 0;
usbx->gintsts = 0xBFFFFFFF;
/* enable global interrupt mask */
usbx->gintmsk = USB_OTG_SOF_INT | USB_OTG_RXFLVL_INT |
USB_OTG_USBSUSP_INT | USB_OTG_USBRST_INT |
USB_OTG_ENUMDONE_INT | USB_OTG_IEPT_INT |
USB_OTG_OEPT_INT | USB_OTG_INCOMISOIN_INT |
USB_OTG_INCOMPIP_INCOMPISOOUT_INT | USB_OTG_WKUP_INT |
USB_OTG_OTGINT_INT;
/* usb connect */
usbd_connect(udev);
/* enable global interrupt */
usb_interrupt_enable(usbx);
return USB_OK;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
@@ -0,0 +1,538 @@
/**
**************************************************************************
* @file usbd_int.c
* @version v2.1.0
* @date 2022-08-16
* @brief usb interrupt request
**************************************************************************
* 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_int.h"
/** @addtogroup AT32F435_437_middlewares_usbd_drivers
* @{
*/
/** @defgroup USBD_drivers_interrupt
* @brief usb device interrupt
* @{
*/
/** @defgroup USBD_int_private_functions
* @{
*/
/**
* @brief usb device interrput request handler.
* @param otgdev: to the structure of otg_core_type
* @retval none
*/
void usbd_irq_handler(otg_core_type *otgdev)
{
otg_global_type *usbx = otgdev->usb_reg;
usbd_core_type *udev = &otgdev->dev;
uint32_t intsts = usb_global_get_all_interrupt(usbx);
/* check current device mode */
if(usbx->gintsts_bit.curmode == 0)
{
/* mode mismatch interrupt */
if(intsts & USB_OTG_MODEMIS_FLAG)
{
usb_global_clear_interrupt(usbx, USB_OTG_MODEMIS_FLAG);
}
/* in endpoint interrupt */
if(intsts & USB_OTG_IEPT_FLAG)
{
usbd_inept_handler(udev);
}
/* out endpoint interrupt */
if(intsts & USB_OTG_OEPT_FLAG)
{
usbd_outept_handler(udev);
}
/* usb reset interrupt */
if(intsts & USB_OTG_USBRST_FLAG)
{
usbd_reset_handler(udev);
usb_global_clear_interrupt(usbx, USB_OTG_USBRST_FLAG);
}
/* sof interrupt */
if(intsts & USB_OTG_SOF_FLAG)
{
usbd_sof_handler(udev);
usb_global_clear_interrupt(usbx, USB_OTG_SOF_FLAG);
}
/* enumeration done interrupt */
if(intsts & USB_OTG_ENUMDONE_FLAG)
{
usbd_enumdone_handler(udev);
usb_global_clear_interrupt(usbx, USB_OTG_ENUMDONE_FLAG);
}
/* rx non-empty interrupt, indicates that there is at least one
data packet pending to be read in rx fifo */
if(intsts & USB_OTG_RXFLVL_FLAG)
{
usbd_rxflvl_handler(udev);
}
/* incomplete isochronous in transfer interrupt */
if(intsts & USB_OTG_INCOMISOIN_FLAG)
{
usbd_incomisioin_handler(udev);
usb_global_clear_interrupt(usbx, USB_OTG_INCOMISOIN_FLAG);
}
#ifndef USB_VBUS_IGNORE
/* disconnect detected interrupt */
if(intsts & USB_OTG_OTGINT_FLAG)
{
uint32_t tmp = udev->usb_reg->gotgint;
if(udev->usb_reg->gotgint_bit.sesenddet)
usbd_discon_handler(udev);
udev->usb_reg->gotgint = tmp;
usb_global_clear_interrupt(usbx, USB_OTG_OTGINT_FLAG);
}
#endif
/* incomplete isochronous out transfer interrupt */
if(intsts & USB_OTG_INCOMPIP_INCOMPISOOUT_FLAG)
{
usbd_incomisoout_handler(udev);
usb_global_clear_interrupt(usbx, USB_OTG_INCOMPIP_INCOMPISOOUT_FLAG);
}
/* resume/remote wakeup interrupt */
if(intsts & USB_OTG_WKUP_FLAG)
{
usbd_wakeup_handler(udev);
usb_global_clear_interrupt(usbx, USB_OTG_WKUP_FLAG);
}
/* usb suspend interrupt */
if(intsts & USB_OTG_USBSUSP_FLAG)
{
usbd_suspend_handler(udev);
usb_global_clear_interrupt(usbx, USB_OTG_USBSUSP_FLAG);
}
}
}
/**
* @brief usb write tx fifo.
* @param udev: to the structure of usbd_core_type
* @param ept_num: endpoint number
* @retval none
*/
void usb_write_empty_txfifo(usbd_core_type *udev, uint32_t ept_num)
{
otg_global_type *usbx = udev->usb_reg;
usb_ept_info *ept_info = &udev->ept_in[ept_num];
uint32_t length = ept_info->total_len - ept_info->trans_len;
uint32_t wlen = 0;
if(length > ept_info->maxpacket)
{
length = ept_info->maxpacket;
}
wlen = (length + 3) / 4;
while((USB_INEPT(usbx, ept_num)->dtxfsts & USB_OTG_DTXFSTS_INEPTFSAV) > wlen &&
(ept_info->trans_len < ept_info->total_len) && (ept_info->total_len != 0))
{
length = ept_info->total_len - ept_info->trans_len;
if(length > ept_info->maxpacket)
{
length = ept_info->maxpacket;
}
wlen = (length + 3) / 4;
usb_write_packet(usbx, ept_info->trans_buf, ept_num, length);
ept_info->trans_buf += length;
ept_info->trans_len += length;
}
if(length <= 0)
{
OTG_DEVICE(usbx)->diepempmsk &= ~(0x1 << ept_num);
}
}
/**
* @brief usb in endpoint handler
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_inept_handler(usbd_core_type *udev)
{
otg_global_type *usbx = udev->usb_reg;
uint32_t ept_num = 0, ept_int;
uint32_t intsts;
/*get all endpoint interrut */
intsts = usb_get_all_in_interrupt(usbx);
while(intsts)
{
if(intsts & 0x1)
{
/* get endpoint interrupt flag */
ept_int = usb_ept_in_interrupt(usbx, ept_num);
/* transfer completed interrupt */
if(ept_int & USB_OTG_DIEPINT_XFERC_FLAG)
{
OTG_DEVICE(usbx)->diepempmsk &= ~(1 << ept_num);
usb_ept_in_clear(usbx, ept_num , USB_OTG_DIEPINT_XFERC_FLAG);
usbd_core_in_handler(udev, ept_num);
}
/* timeout condition interrupt */
if(ept_int & USB_OTG_DIEPINT_TIMEOUT_FLAG)
{
usb_ept_in_clear(usbx, ept_num , USB_OTG_DIEPINT_TIMEOUT_FLAG);
}
/* in token received when tx fifo is empty */
if(ept_int & USB_OTG_DIEPINT_INTKNTXFEMP_FLAG)
{
usb_ept_in_clear(usbx, ept_num , USB_OTG_DIEPINT_INTKNTXFEMP_FLAG);
}
/* in endpoint nak effective */
if(ept_int & USB_OTG_DIEPINT_INEPTNAK_FLAG)
{
usb_ept_in_clear(usbx, ept_num , USB_OTG_DIEPINT_INEPTNAK_FLAG);
}
/* endpoint disable interrupt */
if(ept_int & USB_OTG_DIEPINT_EPTDISD_FLAG)
{
usb_ept_in_clear(usbx, ept_num , USB_OTG_DIEPINT_EPTDISD_FLAG);
}
/* transmit fifo empty interrupt */
if(ept_int & USB_OTG_DIEPINT_TXFEMP_FLAG)
{
usb_write_empty_txfifo(udev, ept_num);
}
}
ept_num ++;
intsts >>= 1;
}
}
/**
* @brief usb out endpoint handler
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_outept_handler(usbd_core_type *udev)
{
otg_global_type *usbx = udev->usb_reg;
uint32_t ept_num = 0, ept_int;
uint32_t intsts;
/* get all out endpoint interrupt */
intsts = usb_get_all_out_interrupt(usbx);
while(intsts)
{
if(intsts & 0x1)
{
/* get out endpoint interrupt */
ept_int = usb_ept_out_interrupt(usbx, ept_num);
/* transfer completed interrupt */
if(ept_int & USB_OTG_DOEPINT_XFERC_FLAG)
{
usb_ept_out_clear(usbx, ept_num , USB_OTG_DOEPINT_XFERC_FLAG);
usbd_core_out_handler(udev, ept_num);
}
/* setup phase done interrupt */
if(ept_int & USB_OTG_DOEPINT_SETUP_FLAG)
{
usb_ept_out_clear(usbx, ept_num , USB_OTG_DOEPINT_SETUP_FLAG);
usbd_core_setup_handler(udev, ept_num);
if(udev->device_addr != 0)
{
OTG_DEVICE(udev->usb_reg)->dcfg_bit.devaddr = udev->device_addr;
udev->device_addr = 0;
}
}
/* endpoint disable interrupt */
if(ept_int & USB_OTG_DOEPINT_OUTTEPD_FLAG)
{
usb_ept_out_clear(usbx, ept_num , USB_OTG_DOEPINT_OUTTEPD_FLAG);
}
}
ept_num ++;
intsts >>= 1;
}
}
/**
* @brief usb enumeration done handler
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_enumdone_handler(usbd_core_type *udev)
{
otg_global_type *usbx = udev->usb_reg;
usb_ept0_setup(usbx);
usbx->gusbcfg_bit.usbtrdtim = USB_TRDTIM_16;
/* open endpoint 0 out */
usbd_ept_open(udev, 0x00, EPT_CONTROL_TYPE, 0x40);
/* open endpoint 0 in */
usbd_ept_open(udev, 0x80, EPT_CONTROL_TYPE, 0x40);
/* usb connect state set to default */
udev->conn_state = USB_CONN_STATE_DEFAULT;
/* clear callback */
if(udev->class_handler->clear_handler != 0)
udev->class_handler->clear_handler(udev);
}
/**
* @brief usb rx non-empty handler
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_rxflvl_handler(usbd_core_type *udev)
{
otg_global_type *usbx = udev->usb_reg;
uint32_t stsp;
uint32_t count;
uint32_t pktsts;
usb_ept_info *ept_info;
/* disable rxflvl interrupt */
usb_global_interrupt_enable(usbx, USB_OTG_RXFLVL_INT, FALSE);
/* get rx status */
stsp = usbx->grxstsp;
/*get the byte count of receive */
count = (stsp & USB_OTG_GRXSTSP_BCNT) >> 4;
/* get packet status */
pktsts = (stsp &USB_OTG_GRXSTSP_PKTSTS) >> 17;
/* get endpoint infomation struct */
ept_info = &udev->ept_out[stsp & USB_OTG_GRXSTSP_EPTNUM];
/* received out data packet */
if(pktsts == USB_OUT_STS_DATA)
{
if(count != 0)
{
/* read packet to buffer */
usb_read_packet(usbx, ept_info->trans_buf, (stsp & USB_OTG_GRXSTSP_EPTNUM), count);
ept_info->trans_buf += count;
ept_info->trans_len += count;
}
}
/* setup data received */
else if ( pktsts == USB_SETUP_STS_DATA)
{
/* read packet to buffer */
usb_read_packet(usbx, udev->setup_buffer, (stsp & USB_OTG_GRXSTSP_EPTNUM), count);
ept_info->trans_len += count;
}
/* enable rxflvl interrupt */
usb_global_interrupt_enable(usbx, USB_OTG_RXFLVL_INT, TRUE);
}
/**
* @brief usb disconnect handler
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_discon_handler(usbd_core_type *udev)
{
/* disconnect callback handler */
if(udev->class_handler->event_handler != 0)
udev->class_handler->event_handler(udev, USBD_DISCONNECT_EVNET);
}
/**
* @brief usb incomplete out handler
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_incomisoout_handler(usbd_core_type *udev)
{
if(udev->class_handler->event_handler != 0)
udev->class_handler->event_handler(udev, USBD_OUTISOINCOM_EVENT);
}
/**
* @brief usb incomplete in handler
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_incomisioin_handler(usbd_core_type *udev)
{
if(udev->class_handler->event_handler != 0)
udev->class_handler->event_handler(udev, USBD_INISOINCOM_EVENT);
}
/**
* @brief usb device reset interrupt request handler.
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_reset_handler(usbd_core_type *udev)
{
otg_global_type *usbx = udev->usb_reg;
otg_device_type *dev = OTG_DEVICE(usbx);
uint32_t i_index = 0;
/* disable remote wakeup singal */
dev->dctl_bit.rwkupsig = FALSE;
/* endpoint fifo alloc */
usbd_fifo_alloc(udev);
/* flush all tx fifo */
usb_flush_tx_fifo(usbx, 0x10);
/* clear in and out endpoint interrupt flag */
for(i_index = 0; i_index < USB_EPT_MAX_NUM; i_index ++)
{
USB_INEPT(usbx, i_index)->diepint = 0xFF;
USB_OUTEPT(usbx, i_index)->doepint = 0xFF;
}
/* clear endpoint flag */
dev->daint = 0xFFFFFFFF;
/*clear endpoint interrupt mask */
dev->daintmsk = 0x10001;
/* enable out endpoint xfer, eptdis, setup interrupt mask */
dev->doepmsk_bit.xfercmsk = TRUE;
dev->doepmsk_bit.eptdismsk = TRUE;
dev->doepmsk_bit.setupmsk = TRUE;
/* enable in endpoint xfer, eptdis, timeout interrupt mask */
dev->diepmsk_bit.xfercmsk = TRUE;
dev->diepmsk_bit.eptdismsk = TRUE;
dev->diepmsk_bit.timeoutmsk = TRUE;
/* set device address to 0 */
usb_set_address(usbx, 0);
/* enable endpoint 0 */
usb_ept0_start(usbx);
/* usb connect state set to default */
udev->conn_state = USB_CONN_STATE_DEFAULT;
/* user define reset event */
if(udev->class_handler->event_handler)
udev->class_handler->event_handler(udev, USBD_RESET_EVENT);
}
/**
* @brief usb device sof interrupt request handler.
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_sof_handler(usbd_core_type *udev)
{
/* user sof handler in class define */
if(udev->class_handler->sof_handler)
udev->class_handler->sof_handler(udev);
}
/**
* @brief usb device suspend interrupt request handler.
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_suspend_handler(usbd_core_type *udev)
{
otg_global_type *usbx = udev->usb_reg;
if(OTG_DEVICE(usbx)->dsts_bit.suspsts)
{
/* save connect state */
udev->old_conn_state = udev->conn_state;
/* set current state to suspend */
udev->conn_state = USB_CONN_STATE_SUSPENDED;
/* enter suspend mode */
usbd_enter_suspend(udev);
/* user suspend handler */
if(udev->class_handler->event_handler != 0)
udev->class_handler->event_handler(udev, USBD_SUSPEND_EVENT);
}
}
/**
* @brief usb device wakup interrupt request handler.
* @param udev: to the structure of usbd_core_type
* @retval none
*/
void usbd_wakeup_handler(usbd_core_type *udev)
{
otg_global_type *usbx = udev->usb_reg;
/* clear remote wakeup bit */
OTG_DEVICE(usbx)->dctl_bit.rwkupsig = FALSE;
/* exit suspend mode */
usb_open_phy_clk(udev->usb_reg);
/* restore connect state */
udev->conn_state = udev->old_conn_state;
/* user suspend handler */
if(udev->class_handler->event_handler != 0)
udev->class_handler->event_handler(udev, USBD_WAKEUP_EVENT);
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
@@ -0,0 +1,535 @@
/**
**************************************************************************
* @file usbd_sdr.c
* @version v2.1.0
* @date 2022-08-16
* @brief usb standard device request
**************************************************************************
* 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_sdr.h"
#include <stddef.h>
/** @addtogroup AT32F435_437_middlewares_usbd_drivers
* @{
*/
/** @defgroup USBD_drivers_standard_request
* @brief usb device standard_request
* @{
*/
/** @defgroup USBD_sdr_private_functions
* @{
*/
static usb_sts_type usbd_get_descriptor(usbd_core_type *udev);
static usb_sts_type usbd_set_address(usbd_core_type *udev);
static usb_sts_type usbd_get_status(usbd_core_type *udev);
static usb_sts_type usbd_clear_feature(usbd_core_type *udev);
static usb_sts_type usbd_set_feature(usbd_core_type *udev);
static usb_sts_type usbd_get_configuration(usbd_core_type *udev);
static usb_sts_type usbd_set_configuration(usbd_core_type *udev);
/**
* @brief usb parse standard setup request
* @param setup: setup structure
* @param buf: setup buffer
* @retval none
*/
void usbd_setup_request_parse(usb_setup_type *setup, uint8_t *buf)
{
setup->bmRequestType = *(uint8_t *) buf;
setup->bRequest = *(uint8_t *) (buf + 1);
setup->wValue = SWAPBYTE(buf + 2);
setup->wIndex = SWAPBYTE(buf + 4);
setup->wLength = SWAPBYTE(buf + 6);
}
/**
* @brief get usb standard device description request
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
static usb_sts_type usbd_get_descriptor(usbd_core_type *udev)
{
usb_sts_type ret = USB_OK;
uint16_t len = 0;
usbd_desc_t *desc = NULL;
uint8_t desc_type = udev->setup.wValue >> 8;
switch(desc_type)
{
case USB_DESCIPTOR_TYPE_DEVICE:
desc = udev->desc_handler->get_device_descriptor();
break;
case USB_DESCIPTOR_TYPE_CONFIGURATION:
desc = udev->desc_handler->get_device_configuration();
break;
case USB_DESCIPTOR_TYPE_STRING:
{
uint8_t str_desc = (uint8_t)udev->setup.wValue;
switch(str_desc)
{
case USB_LANGID_STRING:
desc = udev->desc_handler->get_device_lang_id();
break;
case USB_MFC_STRING:
desc = udev->desc_handler->get_device_manufacturer_string();
break;
case USB_PRODUCT_STRING:
desc = udev->desc_handler->get_device_product_string();
break;
case USB_SERIAL_STRING:
desc = udev->desc_handler->get_device_serial_string();
break;
case USB_CONFIG_STRING:
desc = udev->desc_handler->get_device_config_string();
break;
case USB_INTERFACE_STRING:
desc = udev->desc_handler->get_device_interface_string();
break;
default:
udev->class_handler->setup_handler(udev, &udev->setup);
return ret;
}
break;
}
case USB_DESCIPTOR_TYPE_DEVICE_QUALIFIER:
usbd_ctrl_unsupport(udev);
break;
case USB_DESCIPTOR_TYPE_OTHER_SPEED:
usbd_ctrl_unsupport(udev);
return ret;
default:
usbd_ctrl_unsupport(udev);
return ret;
}
if(desc != NULL)
{
if((desc->length != 0) && (udev->setup.wLength != 0))
{
len = MIN(desc->length , udev->setup.wLength);
usbd_ctrl_send(udev, desc->descriptor, len);
}
}
return ret;
}
/**
* @brief this request sets the device address
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
static usb_sts_type usbd_set_address(usbd_core_type *udev)
{
usb_sts_type ret = USB_OK;
usb_setup_type *setup = &udev->setup;
uint8_t dev_addr;
/* if wIndex or wLength are non-zero, then the behavior of
the device is not specified
*/
if(setup->wIndex == 0 && setup->wLength == 0)
{
dev_addr = (uint8_t)(setup->wValue) & 0x7f;
/* device behavior when this request is received
while the device is in the configured state is not specified.*/
if(udev->conn_state == USB_CONN_STATE_CONFIGURED )
{
usbd_ctrl_unsupport(udev);
}
else
{
udev->device_addr = dev_addr;
if(dev_addr != 0)
{
udev->conn_state = USB_CONN_STATE_ADDRESSED;
}
else
{
udev->conn_state = USB_CONN_STATE_DEFAULT;
}
usbd_ctrl_send_status(udev);
}
}
else
{
usbd_ctrl_unsupport(udev);
}
return ret;
}
/**
* @brief get usb status request
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
static usb_sts_type usbd_get_status(usbd_core_type *udev)
{
usb_sts_type ret = USB_OK;
switch(udev->conn_state)
{
case USB_CONN_STATE_ADDRESSED:
case USB_CONN_STATE_CONFIGURED:
if(udev->remote_wakup)
{
udev->config_status |= USB_CONF_REMOTE_WAKEUP;
}
usbd_ctrl_send(udev, (uint8_t *)(&udev->config_status), 2);
break;
default:
usbd_ctrl_unsupport(udev);
break;
}
return ret;
}
/**
* @brief clear usb feature request
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
static usb_sts_type usbd_clear_feature(usbd_core_type *udev)
{
usb_sts_type ret = USB_OK;
usb_setup_type *setup = &udev->setup;
switch(udev->conn_state)
{
case USB_CONN_STATE_ADDRESSED:
case USB_CONN_STATE_CONFIGURED:
if(setup->wValue == USB_FEATURE_REMOTE_WAKEUP)
{
udev->remote_wakup = 0;
udev->config_status &= ~USB_CONF_REMOTE_WAKEUP;
udev->class_handler->setup_handler(udev, &udev->setup);
usbd_ctrl_send_status(udev);
}
break;
default:
usbd_ctrl_unsupport(udev);
break;
}
return ret;
}
/**
* @brief set usb feature request
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
static usb_sts_type usbd_set_feature(usbd_core_type *udev)
{
usb_sts_type ret = USB_OK;
usb_setup_type *setup = &udev->setup;
if(setup->wValue == USB_FEATURE_REMOTE_WAKEUP)
{
udev->remote_wakup = 1;
udev->class_handler->setup_handler(udev, &udev->setup);
usbd_ctrl_send_status(udev);
}
return ret;
}
/**
* @brief get usb configuration request
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
static usb_sts_type usbd_get_configuration(usbd_core_type *udev)
{
usb_sts_type ret = USB_OK;
usb_setup_type *setup = &udev->setup;
if(setup->wLength != 1)
{
usbd_ctrl_unsupport(udev);
}
else
{
switch(udev->conn_state)
{
case USB_CONN_STATE_ADDRESSED:
udev->default_config = 0;
usbd_ctrl_send(udev, (uint8_t *)(&udev->default_config), 1);
break;
case USB_CONN_STATE_CONFIGURED:
usbd_ctrl_send(udev, (uint8_t *)(&udev->dev_config), 1);
break;
default:
usbd_ctrl_unsupport(udev);
break;
}
}
return ret;
}
/**
* @brief sets the usb device configuration request
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
static usb_sts_type usbd_set_configuration(usbd_core_type *udev)
{
usb_sts_type ret = USB_OK;
static uint8_t config_value;
usb_setup_type *setup = &udev->setup;
config_value = (uint8_t)setup->wValue;
if(setup->wIndex == 0 && setup->wLength == 0)
{
switch(udev->conn_state)
{
case USB_CONN_STATE_ADDRESSED:
if(config_value)
{
udev->dev_config = config_value;
udev->conn_state = USB_CONN_STATE_CONFIGURED;
udev->class_handler->init_handler(udev);
usbd_ctrl_send_status(udev);
}
else
{
usbd_ctrl_send_status(udev);
}
break;
case USB_CONN_STATE_CONFIGURED:
if(config_value == 0)
{
udev->conn_state = USB_CONN_STATE_ADDRESSED;
udev->dev_config = config_value;
udev->class_handler->clear_handler(udev);
usbd_ctrl_send_status(udev);
}
else if(config_value == udev->dev_config)
{
udev->class_handler->clear_handler(udev);
udev->dev_config = config_value;
udev->class_handler->init_handler(udev);
usbd_ctrl_send_status(udev);
}
else
{
usbd_ctrl_send_status(udev);
}
break;
default:
usbd_ctrl_unsupport(udev);
break;
}
}
else
{
usbd_ctrl_unsupport(udev);
}
return ret;
}
/**
* @brief standard usb device requests
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type usbd_device_request(usbd_core_type *udev)
{
usb_sts_type ret = USB_OK;
usb_setup_type *setup = &udev->setup;
if((setup->bmRequestType & USB_REQ_TYPE_RESERVED) != USB_REQ_TYPE_STANDARD)
{
udev->class_handler->setup_handler(udev, &udev->setup);
return ret;
}
switch(udev->setup.bRequest)
{
case USB_STD_REQ_GET_STATUS:
usbd_get_status(udev);
break;
case USB_STD_REQ_CLEAR_FEATURE:
usbd_clear_feature(udev);
break;
case USB_STD_REQ_SET_FEATURE:
usbd_set_feature(udev);
break;
case USB_STD_REQ_SET_ADDRESS:
usbd_set_address(udev);
break;
case USB_STD_REQ_GET_DESCRIPTOR:
usbd_get_descriptor(udev);
break;
case USB_STD_REQ_GET_CONFIGURATION:
usbd_get_configuration(udev);
break;
case USB_STD_REQ_SET_CONFIGURATION:
usbd_set_configuration(udev);
break;
default:
usbd_ctrl_unsupport(udev);
break;
}
return ret;
}
/**
* @brief standard usb interface requests
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type usbd_interface_request(usbd_core_type *udev)
{
usb_sts_type ret = USB_OK;
usb_setup_type *setup = &udev->setup;
switch(udev->conn_state)
{
case USB_CONN_STATE_CONFIGURED:
udev->class_handler->setup_handler(udev, &udev->setup);
if(setup->wLength == 0)
{
usbd_ctrl_send_status(udev);
}
break;
default:
usbd_ctrl_unsupport(udev);
break;
}
return ret;
}
/**
* @brief standard usb endpoint requests
* @param udev: to the structure of usbd_core_type
* @retval status of usb_sts_type
*/
usb_sts_type usbd_endpoint_request(usbd_core_type *udev)
{
usb_sts_type ret = USB_OK;
usb_setup_type *setup = &udev->setup;
uint8_t ept_addr = LBYTE(setup->wIndex);
usb_ept_info *ept_info;
if((setup->bmRequestType & USB_REQ_TYPE_RESERVED) == USB_REQ_TYPE_CLASS)
{
udev->class_handler->setup_handler(udev, &udev->setup);
}
switch(setup->bRequest)
{
case USB_STD_REQ_GET_STATUS:
switch(udev->conn_state)
{
case USB_CONN_STATE_ADDRESSED:
if((ept_addr & 0x7F) != 0)
{
usbd_set_stall(udev, ept_addr);
}
break;
case USB_CONN_STATE_CONFIGURED:
{
if((ept_addr & 0x80) != 0)
{
ept_info = &udev->ept_in[ept_addr & 0x7F];
}
else
{
ept_info = &udev->ept_out[ept_addr & 0x7F];
}
if(ept_info->stall == 1)
{
ept_info->status = 0x0001;
}
else
{
ept_info->status = 0x0000;
}
usbd_ctrl_send(udev, (uint8_t *)(&ept_info->status), 2);
}
break;
default:
usbd_ctrl_unsupport(udev);
break;
}
break;
case USB_STD_REQ_CLEAR_FEATURE:
switch(udev->conn_state)
{
case USB_CONN_STATE_ADDRESSED:
if((ept_addr != 0x00) && (ept_addr != 0x80))
{
usbd_set_stall(udev, ept_addr);
}
break;
case USB_CONN_STATE_CONFIGURED:
if(setup->wValue == USB_FEATURE_EPT_HALT)
{
if((ept_addr & 0x7F) != 0x00 )
{
usbd_clear_stall(udev, ept_addr);
udev->class_handler->setup_handler(udev, &udev->setup);
}
usbd_ctrl_send_status(udev);
}
break;
default:
usbd_ctrl_unsupport(udev);
break;
}
break;
case USB_STD_REQ_SET_FEATURE:
switch(udev->conn_state)
{
case USB_CONN_STATE_ADDRESSED:
if((ept_addr != 0x00) && (ept_addr != 0x80))
{
usbd_set_stall(udev, ept_addr);
}
break;
case USB_CONN_STATE_CONFIGURED:
if(setup->wValue == USB_FEATURE_EPT_HALT)
{
if((ept_addr != 0x00) && (ept_addr != 0x80))
{
usbd_set_stall(udev, ept_addr);
}
}
udev->class_handler->setup_handler(udev, &udev->setup);
usbd_ctrl_send_status(udev);
break;
default:
usbd_ctrl_unsupport(udev);
break;
}
break;
default:
usbd_ctrl_unsupport(udev);
break;
}
return ret;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,976 @@
/**
**************************************************************************
* @file usbh_ctrl.c
* @version v2.1.0
* @date 2022-08-16
* @brief usb host control request
**************************************************************************
* 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 "usbh_ctrl.h"
#include "usbh_core.h"
#include "usb_std.h"
/** @addtogroup AT32F435_437_middlewares_usbh_drivers
* @{
*/
/** @defgroup USBH_drivers_control
* @brief usb host drivers control
* @{
*/
/** @defgroup USBH_ctrl_private_functions
* @{
*/
/* control timeout 5s */
#define CTRL_TIMEOUT 5000
/**
* @brief usb host control send setup packet
* @param uhost: to the structure of usbh_core_type
* @param buffer: usb control setup send buffer
* @param hc_num: channel number
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_ctrl_send_setup(usbh_core_type *uhost, uint8_t *buffer, uint8_t hc_num)
{
uhost->hch[hc_num].dir = 0;
uhost->hch[hc_num].data_pid = HCH_PID_SETUP;
uhost->hch[hc_num].trans_buf = buffer;
uhost->hch[hc_num].trans_len = 8; /*setup */
return usbh_in_out_request(uhost, hc_num);
}
/**
* @brief usb host control receive data from device
* @param uhost: to the structure of usbh_core_type
* @param buffer: usb control receive data buffer
* @param length: usb control receive data length
* @param hc_num: channel number
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_ctrl_recv_data(usbh_core_type *uhost, uint8_t *buffer,
uint16_t length, uint16_t hc_num)
{
uhost->hch[hc_num].dir = 1;
uhost->hch[hc_num].data_pid = HCH_PID_DATA1;
uhost->hch[hc_num].trans_buf = buffer;
uhost->hch[hc_num].trans_len = length;
return usbh_in_out_request(uhost, hc_num);
}
/**
* @brief usb host control send data packet
* @param uhost: to the structure of usbh_core_type
* @param buffer: usb control send data buffer
* @param length: usb control send data length
* @param hc_num: channel number
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_ctrl_send_data(usbh_core_type *uhost, uint8_t *buffer,
uint16_t length, uint16_t hc_num)
{
uhost->hch[hc_num].dir = 0;
uhost->hch[hc_num].trans_buf = buffer;
uhost->hch[hc_num].trans_len = length;
if(length == 0)
{
uhost->hch[uhost->ctrl.hch_out].toggle_out = 1;
}
if(uhost->hch[uhost->ctrl.hch_out].toggle_out == 0)
{
uhost->hch[hc_num].data_pid = HCH_PID_DATA0;
}
else
{
uhost->hch[hc_num].data_pid = HCH_PID_DATA1;
}
return usbh_in_out_request(uhost, hc_num);
}
/**
* @brief usb host control setup request handler
* @param uhost: to the structure of usbh_core_type
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_ctrl_setup_handler(usbh_core_type *uhost)
{
usbh_ctrl_send_setup(uhost, (uint8_t *)(&uhost->ctrl.setup),
uhost->ctrl.hch_out);
uhost->ctrl.state = CONTROL_SETUP_WAIT;
return USB_OK;
}
/**
* @brief usb host control setup request wait handler
* @param uhost: to the structure of usbh_core_type
* @param timeout: pointer of wait timeout
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_ctrl_setup_wait_handler(usbh_core_type *uhost, uint32_t *timeout)
{
urb_sts_type urb_state;
usb_sts_type status = USB_WAIT;
uint8_t dir;
urb_state = uhost->urb_state[uhost->ctrl.hch_out];
if(urb_state == URB_DONE)
{
dir = uhost->ctrl.setup.bmRequestType & USB_REQUEST_DIR_MASK;
if(uhost->ctrl.setup.wLength != 0)
{
*timeout = DATA_STAGE_TIMEOUT;
if(dir == USB_DIR_D2H) //in
{
uhost->ctrl.state = CONTROL_DATA_IN;
}
else //out
{
uhost->ctrl.state = CONTROL_DATA_OUT;
}
}
else
{
*timeout = NODATA_STAGE_TIMEOUT;
if(dir == USB_DIR_D2H) //no data, send status
{
uhost->ctrl.state = CONTROL_STATUS_OUT;
}
else //out
{
uhost->ctrl.state = CONTROL_STATUS_IN;
}
}
status = USB_OK;
}
else if(urb_state == URB_ERROR || urb_state == URB_NOTREADY)
{
uhost->ctrl.state = CONTROL_ERROR;
uhost->ctrl.sts = CTRL_XACTERR;
status = USB_ERROR;
}
else
{
/* wait nak timeout 5s*/
if(uhost->timer - uhost->ctrl.timer > CTRL_TIMEOUT)
{
uhost->ctrl.state = CONTROL_ERROR;
uhost->ctrl.sts = CTRL_XACTERR;
status = USB_ERROR;
}
}
return status;
}
/**
* @brief usb host control data in request handler
* @param uhost: to the structure of usbh_core_type
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_ctrl_data_in_handler(usbh_core_type *uhost)
{
usb_sts_type status = USB_OK;
usbh_ctrl_recv_data(uhost, uhost->ctrl.buffer,
uhost->ctrl.len,
uhost->ctrl.hch_in);
uhost->ctrl.state = CONTROL_DATA_IN_WAIT;
return status;
}
/**
* @brief usb host control data in wait handler
* @param uhost: to the structure of usbh_core_type
* @param timeout: wait timeout
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_ctrl_data_in_wait_handler(usbh_core_type *uhost, uint32_t timeout)
{
usb_sts_type status = USB_OK;
urb_sts_type urb_state;
urb_state = uhost->urb_state[uhost->ctrl.hch_in];
if(urb_state == URB_DONE)
{
uhost->ctrl.state = CONTROL_STATUS_OUT;
}
else if(urb_state == URB_STALL)
{
uhost->ctrl.state = CONTROL_STALL;
}
else if(urb_state == URB_ERROR)
{
uhost->ctrl.state = CONTROL_ERROR;
}
else
{
/* wait nak timeout 5s*/
if(uhost->timer - uhost->ctrl.timer > CTRL_TIMEOUT)
{
uhost->ctrl.state = CONTROL_ERROR;
uhost->ctrl.sts = CTRL_XACTERR;
status = USB_ERROR;
}
}
return status;
}
/**
* @brief usb host control data out request handler
* @param uhost: to the structure of usbh_core_type
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_ctrl_data_out_handler(usbh_core_type *uhost)
{
usb_sts_type status = USB_OK;
uhost->hch[uhost->ctrl.hch_out].toggle_out = 1;
usbh_ctrl_send_data(uhost, uhost->ctrl.buffer,
uhost->ctrl.len,
uhost->ctrl.hch_out);
uhost->ctrl.state = CONTROL_DATA_OUT_WAIT;
return status;
}
/**
* @brief usb host control data out wait handler
* @param uhost: to the structure of usbh_core_type
* @param timeout: wait timeout
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_ctrl_data_out_wait_handler(usbh_core_type *uhost, uint32_t timeout)
{
usb_sts_type status = USB_OK;
urb_sts_type urb_state;
urb_state = uhost->urb_state[uhost->ctrl.hch_out];
if(urb_state == URB_DONE)
{
uhost->ctrl.state = CONTROL_STATUS_IN;
}
else if(urb_state == URB_STALL)
{
uhost->ctrl.state = CONTROL_STALL;
}
else if(urb_state == URB_ERROR)
{
uhost->ctrl.state = CONTROL_ERROR;
}
else if(urb_state == URB_NOTREADY)
{
uhost->ctrl.state = CONTROL_DATA_OUT;
}
else
{
/* wait nak timeout 5s*/
if(uhost->timer - uhost->ctrl.timer > CTRL_TIMEOUT)
{
uhost->ctrl.state = CONTROL_ERROR;
uhost->ctrl.sts = CTRL_XACTERR;
status = USB_ERROR;
}
}
return status;
}
/**
* @brief usb host control status data in handler
* @param uhost: to the structure of usbh_core_type
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_ctrl_status_in_handler(usbh_core_type *uhost)
{
usb_sts_type status = USB_OK;
usbh_ctrl_recv_data(uhost, 0, 0,
uhost->ctrl.hch_in);
uhost->ctrl.state = CONTROL_STATUS_IN_WAIT;
return status;
}
/**
* @brief usb host control status data in wait handler
* @param uhost: to the structure of usbh_core_type
* @param timeout: wait timeout
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_ctrl_status_in_wait_handler(usbh_core_type *uhost, uint32_t timeout)
{
usb_sts_type status = USB_OK;
urb_sts_type urb_state;
urb_state = uhost->urb_state[uhost->ctrl.hch_in];
if(urb_state == URB_DONE)
{
uhost->ctrl.state = CONTROL_COMPLETE;
}
else if(urb_state == URB_STALL)
{
uhost->ctrl.state = CONTROL_STALL;
status = USB_NOT_SUPPORT;
}
else if(urb_state == URB_ERROR)
{
uhost->ctrl.state = CONTROL_ERROR;
}
else
{
/* wait nak timeout 5s*/
if(uhost->timer - uhost->ctrl.timer > CTRL_TIMEOUT)
{
uhost->ctrl.state = CONTROL_ERROR;
uhost->ctrl.sts = CTRL_XACTERR;
status = USB_ERROR;
}
}
return status;
}
/**
* @brief usb host control status data out wait handler
* @param uhost: to the structure of usbh_core_type
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_ctrl_status_out_handler(usbh_core_type *uhost)
{
usb_sts_type status = USB_OK;
uhost->hch[uhost->ctrl.hch_out].toggle_out ^= 1;
usbh_ctrl_send_data(uhost, 0, 0, uhost->ctrl.hch_out);
uhost->ctrl.state = CONTROL_STATUS_OUT_WAIT;
return status;
}
/**
* @brief usb host control status data out wait handler
* @param uhost: to the structure of usbh_core_type
* @param timeout: wait timeout
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_ctrl_status_out_wait_handler(usbh_core_type *uhost, uint32_t timeout)
{
usb_sts_type status = USB_OK;
urb_sts_type urb_state;
urb_state = uhost->urb_state[uhost->ctrl.hch_out];
if(urb_state == URB_DONE)
{
uhost->ctrl.state = CONTROL_COMPLETE;
}
else if(urb_state == URB_STALL)
{
uhost->ctrl.state = CONTROL_STALL;
}
else if(urb_state == URB_ERROR)
{
uhost->ctrl.state = CONTROL_ERROR;
}
else if(urb_state == URB_NOTREADY)
{
uhost->ctrl.state = CONTROL_STATUS_OUT;
}
else
{
/* wait nak timeout 5s*/
if(uhost->timer - uhost->ctrl.timer > CTRL_TIMEOUT)
{
uhost->ctrl.state = CONTROL_ERROR;
uhost->ctrl.sts = CTRL_XACTERR;
status = USB_ERROR;
}
}
return status;
}
/**
* @brief usb host control error handler
* @param uhost: to the structure of usbh_core_type
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_ctrl_error_handler(usbh_core_type *uhost)
{
usb_sts_type status = USB_WAIT;
if(++ uhost->ctrl.err_cnt <= USBH_MAX_ERROR_COUNT)
{
uhost->ctrl.state = CONTROL_SETUP;
}
else
{
uhost->ctrl.sts = CTRL_FAIL;
uhost->global_state = USBH_ERROR_STATE;
uhost->ctrl.err_cnt = 0;
USBH_DEBUG("control error: **** Device No Response ****");
status = USB_ERROR;
}
return status;
}
/**
* @brief usb host control stall handler
* @param uhost: to the structure of usbh_core_type
* @retval usb_sts_type
*/
usb_sts_type usbh_ctrl_stall_handler(usbh_core_type *uhost)
{
return USB_NOT_SUPPORT;
}
/**
* @brief usb host control complete handler
* @param uhost: to the structure of usbh_core_type
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_ctrl_complete_handler(usbh_core_type *uhost)
{
return USB_OK;
}
/**
* @brief usb host control transfer loop function
* @param uhost: to the structure of usbh_core_type
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_ctrl_transfer_loop(usbh_core_type *uhost)
{
usb_sts_type status = USB_WAIT;
static uint32_t timeout = 0;
uhost->ctrl.sts = CTRL_START;
switch(uhost->ctrl.state)
{
case CONTROL_SETUP:
usbh_ctrl_setup_handler(uhost);
uhost->ctrl.timer = uhost->timer;
break;
case CONTROL_SETUP_WAIT:
usbh_ctrl_setup_wait_handler(uhost, &timeout);
break;
case CONTROL_DATA_IN:
usbh_ctrl_data_in_handler(uhost);
uhost->ctrl.timer = uhost->timer;
break;
case CONTROL_DATA_IN_WAIT:
usbh_ctrl_data_in_wait_handler(uhost, timeout);
break;
case CONTROL_DATA_OUT:
usbh_ctrl_data_out_handler(uhost);
uhost->ctrl.timer = uhost->timer;
break;
case CONTROL_DATA_OUT_WAIT:
usbh_ctrl_data_out_wait_handler(uhost, timeout);
break;
case CONTROL_STATUS_IN:
usbh_ctrl_status_in_handler(uhost);
uhost->ctrl.timer = uhost->timer;
break;
case CONTROL_STATUS_IN_WAIT:
usbh_ctrl_status_in_wait_handler(uhost, timeout);
break;
case CONTROL_STATUS_OUT:
usbh_ctrl_status_out_handler(uhost);
uhost->ctrl.timer = uhost->timer;
break;
case CONTROL_STATUS_OUT_WAIT:
usbh_ctrl_status_out_wait_handler(uhost, timeout);
break;
case CONTROL_STALL:
status = usbh_ctrl_stall_handler(uhost);
break;
case CONTROL_ERROR:
status = usbh_ctrl_error_handler(uhost);
break;
case CONTROL_COMPLETE:
status = usbh_ctrl_complete_handler(uhost);
break;
default:
break;
}
return status;
}
/**
* @brief usb host control request
* @param uhost: to the structure of usbh_core_type
* @param buffer: usb request buffer
* @param length: usb request length
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_ctrl_request(usbh_core_type *uhost, uint8_t *buffer, uint16_t length)
{
usb_sts_type status = USB_OK;
if(uhost->req_state == CMD_SEND)
{
uhost->req_state = CMD_WAIT;
uhost->ctrl.buffer = buffer;
uhost->ctrl.len = length;
uhost->ctrl.state = CONTROL_SETUP;
}
return status;
}
/**
* @brief usb host get device descriptor
* @param uhost: to the structure of usbh_core_type
* @param length: get descriptor request length
* @param req_type: usb request type
* @param wvalue: usb wvalue
* @param buffer: request buffer
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_get_descriptor(usbh_core_type *uhost, uint16_t length,
uint8_t req_type, uint16_t wvalue,
uint8_t *buffer)
{
usb_sts_type status;
uhost->ctrl.setup.bmRequestType = USB_DIR_D2H | req_type;
uhost->ctrl.setup.bRequest = USB_STD_REQ_GET_DESCRIPTOR;
uhost->ctrl.setup.wValue = wvalue;
uhost->ctrl.setup.wLength = length;
if((wvalue & 0xFF00) == ((USB_DESCIPTOR_TYPE_STRING << 8) & 0xFF00))
{
uhost->ctrl.setup.wIndex = 0x0409;
}
else
{
uhost->ctrl.setup.wIndex = 0;
}
status = usbh_ctrl_request(uhost, buffer, length);
return status;
}
/**
* @brief usb host parse device descriptor
* @param uhost: to the structure of usbh_core_type
* @param buffer: usb device descriptor buffer
* @param length: usb device descriptor length
* @retval status: usb_sts_type status
*/
void usbh_parse_dev_desc(usbh_core_type *uhost, uint8_t *buffer, uint16_t length)
{
usbh_dev_desc_type *desc = &(uhost->dev);
desc->dev_desc.bLength = *(uint8_t *)(buffer + 0);
desc->dev_desc.bDescriptorType = *(uint8_t *)(buffer + 1);
desc->dev_desc.bcdUSB = SWAPBYTE(buffer + 2);
desc->dev_desc.bDeviceClass = *(uint8_t *)(buffer + 4);
desc->dev_desc.bDeviceSubClass = *(uint8_t *)(buffer + 5);
desc->dev_desc.bDeviceProtocol = *(uint8_t *)(buffer + 6);
desc->dev_desc.bMaxPacketSize0 = *(uint8_t *)(buffer + 7);
if(length > 8)
{
desc->dev_desc.idVendor = SWAPBYTE(buffer + 8);
desc->dev_desc.idProduct = SWAPBYTE(buffer + 10);
desc->dev_desc.bcdDevice = SWAPBYTE(buffer + 12);
desc->dev_desc.iManufacturer = *(uint8_t *)(buffer + 14);
desc->dev_desc.iProduct = *(uint8_t *)(buffer + 15);
desc->dev_desc.iSerialNumber = *(uint8_t *)(buffer + 16);
desc->dev_desc.bNumConfigurations = *(uint8_t *)(buffer + 17);
}
}
/**
* @brief usb host get next header
* @param buffer: usb data buffer
* @param index_len: pointer of index len
* @retval status: usb_sts_type status
*/
usb_header_desc_type *usbh_get_next_header(uint8_t *buf, uint16_t *index_len)
{
*index_len += ((usb_header_desc_type *)buf)->bLength;
return (usb_header_desc_type *)
((uint8_t *)buf + ((usb_header_desc_type *)buf)->bLength);
}
/**
* @brief usb host parse interface descriptor
* @param intf: usb interface description type
* @param buf: interface description data buffer
* @retval none
*/
void usbh_parse_interface_desc(usb_interface_desc_type *intf, uint8_t *buf)
{
intf->bLength = *(uint8_t *)buf;
intf->bDescriptorType = *(uint8_t *)(buf + 1);
intf->bInterfaceNumber = *(uint8_t *)(buf + 2);
intf->bAlternateSetting = *(uint8_t *)(buf + 3);
intf->bNumEndpoints = *(uint8_t *)(buf + 4);
intf->bInterfaceClass = *(uint8_t *)(buf + 5);
intf->bInterfaceSubClass = *(uint8_t *)(buf + 6);
intf->bInterfaceProtocol = *(uint8_t *)(buf + 7);
intf->iInterface = *(uint8_t *)(buf + 8);
}
/**
* @brief usb host parse endpoint descriptor
* @param ept_desc: endpoint type
* @param buf: endpoint description data buffer
* @retval none
*/
void usbh_parse_endpoint_desc(usb_endpoint_desc_type *ept_desc, uint8_t *buf)
{
ept_desc->bLength = *(uint8_t *)(buf + 0);
ept_desc->bDescriptorType = *(uint8_t *)(buf + 1);
ept_desc->bEndpointAddress = *(uint8_t *)(buf + 2);
ept_desc->bmAttributes = *(uint8_t *)(buf + 3);
ept_desc->wMaxPacketSize = SWAPBYTE(buf + 4);
ept_desc->bInterval = *(uint8_t *)(buf + 6);
}
/**
* @brief usb host parse configure descriptor
* @param uhost: to the structure of usbh_core_type
* @param buffer: configure buffer
* @param length: configure length
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_parse_configure_desc(usbh_core_type *uhost,
uint8_t *buffer, uint16_t length)
{
usb_cfg_desc_type *cfg_desc = &(uhost->dev.cfg_desc);
usb_interface_desc_type *intf_desc;
usb_endpoint_desc_type *ept_desc;
usb_header_desc_type *desc;
uint16_t index_len;
uint8_t index_intf = 0;
uint8_t index_ept = 0;
desc = (usb_header_desc_type *)buffer;
cfg_desc->cfg.bLength = *(uint8_t *)buffer;
cfg_desc->cfg.bDescriptorType = *(uint8_t *)(buffer + 1);
cfg_desc->cfg.wTotalLength = SWAPBYTE(buffer + 2);
cfg_desc->cfg.bNumInterfaces = *(uint8_t *)(buffer + 4);
cfg_desc->cfg.bConfigurationValue = *(uint8_t *)(buffer + 5);
cfg_desc->cfg.iConfiguration = *(uint8_t *)(buffer + 6);
cfg_desc->cfg.bmAttributes = *(uint8_t *)(buffer + 7);
cfg_desc->cfg.bMaxPower = *(uint8_t *)(buffer + 8);
if(length > USB_DEVICE_CFG_DESC_LEN)
{
index_len = USB_DEVICE_CFG_DESC_LEN;
while((index_intf < USBH_MAX_INTERFACE) && index_len < cfg_desc->cfg.wTotalLength)
{
desc = usbh_get_next_header((uint8_t *)desc, &index_len);
if(desc->bDescriptorType == USB_DESCIPTOR_TYPE_INTERFACE)
{
index_ept = 0;
intf_desc = &cfg_desc->interface[index_intf].interface;
usbh_parse_interface_desc(intf_desc, (uint8_t *)desc);
while(index_ept < intf_desc->bNumEndpoints && index_len < cfg_desc->cfg.wTotalLength)
{
desc = usbh_get_next_header((uint8_t *)desc, &index_len);
if(desc->bDescriptorType == USB_DESCIPTOR_TYPE_ENDPOINT)
{
ept_desc = &(cfg_desc->interface[index_intf].endpoint[index_ept]);
usbh_parse_endpoint_desc(ept_desc, (uint8_t *)desc);
index_ept ++;
}
}
index_intf ++;
}
}
}
return USB_OK;
}
/**
* @brief usb host find interface
* @param uhost: to the structure of usbh_core_type
* @param class_code: class code
* @param sub_class: subclass code
* @param protocol: prtocol code
* @retval idx: interface index
*/
uint8_t usbh_find_interface(usbh_core_type *uhost, uint8_t class_code, uint8_t sub_class, uint8_t protocol)
{
uint8_t idx = 0;
usb_itf_desc_type *usbitf;
for(idx = 0; idx < uhost->dev.cfg_desc.cfg.bNumInterfaces; idx ++)
{
usbitf = &uhost->dev.cfg_desc.interface[idx];
if(((usbitf->interface.bInterfaceClass == class_code) || (class_code == 0xFF)) &&
((usbitf->interface.bInterfaceSubClass == sub_class) || (sub_class == 0xFF)) &&
((usbitf->interface.bInterfaceProtocol == protocol) || (protocol == 0xFF))
)
{
return idx;
}
}
return 0xFF;
}
/**
* @brief usbh parse string descriptor
* @param src: string source pointer
* @param dest: string destination pointer
* @param length: string length
* @retval none
*/
void usbh_parse_string_desc(uint8_t *src, uint8_t *dest, uint16_t length)
{
uint16_t len;
uint16_t i_index;
if(src[1] == USB_DESCIPTOR_TYPE_STRING)
{
len = ((src[0] - 2) <= length ? (src[0] - 2) : length);
src += 2;
for(i_index = 0; i_index < len; i_index += 2)
{
*dest = src[i_index];
dest ++;
}
*dest = 0;
}
}
/**
* @brief usb host get device descriptor
* @param uhost: to the structure of usbh_core_type
* @param length: get device descriptor length
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_get_device_descriptor(usbh_core_type *uhost, uint16_t length)
{
usb_sts_type status = USB_WAIT;
uint8_t bm_req;
uint16_t wvalue;
bm_req = USB_REQ_RECIPIENT_DEVICE | USB_REQ_TYPE_STANDARD;
wvalue = (USB_DESCIPTOR_TYPE_DEVICE << 8) & 0xFF00;
status = usbh_get_descriptor(uhost, length, bm_req,
wvalue, uhost->rx_buffer);
return status;
}
/**
* @brief usb host get configure descriptor
* @param uhost: to the structure of usbh_core_type
* @param length: get device configure length
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_get_configure_descriptor(usbh_core_type *uhost, uint16_t length)
{
usb_sts_type status = USB_WAIT;
uint8_t bm_req;
uint16_t wvalue;
bm_req = USB_REQ_RECIPIENT_DEVICE | USB_REQ_TYPE_STANDARD;
wvalue = (USB_DESCIPTOR_TYPE_CONFIGURATION << 8) & 0xFF00;
status = usbh_get_descriptor(uhost, length, bm_req,
wvalue, uhost->rx_buffer);
return status;
}
/**
* @brief usb host get string descriptor
* @param uhost: to the structure of usbh_core_type
* @param string_id: string id
* @param buffer: receive data buffer
* @param length: get device string length
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_get_sting_descriptor(usbh_core_type *uhost, uint8_t string_id,
uint8_t *buffer, uint16_t length)
{
usb_sts_type status = USB_WAIT;
uint8_t bm_req;
uint16_t wvalue;
bm_req = USB_REQ_RECIPIENT_DEVICE | USB_REQ_TYPE_STANDARD;
wvalue = (USB_DESCIPTOR_TYPE_STRING << 8) | string_id;
status = usbh_get_descriptor(uhost, length, bm_req,
wvalue, uhost->rx_buffer);
return status;
}
/**
* @brief usb host set configurtion
* @param uhost: to the structure of usbh_core_type
* @param config: usb configuration
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_set_configuration(usbh_core_type *uhost, uint16_t config)
{
usb_sts_type status = USB_WAIT;
uint8_t bm_req;
bm_req = USB_REQ_RECIPIENT_DEVICE | USB_REQ_TYPE_STANDARD;
uhost->ctrl.setup.bmRequestType = USB_DIR_H2D | bm_req;
uhost->ctrl.setup.bRequest = USB_STD_REQ_SET_CONFIGURATION;
uhost->ctrl.setup.wValue = config;
uhost->ctrl.setup.wLength = 0;
uhost->ctrl.setup.wIndex = 0;
status = usbh_ctrl_request(uhost, 0, 0);
return status;
}
/**
* @brief usb host set device address
* @param uhost: to the structure of usbh_core_type
* @param address: device address
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_set_address(usbh_core_type *uhost, uint8_t address)
{
usb_sts_type status = USB_WAIT;
uint8_t bm_req;
bm_req = USB_REQ_RECIPIENT_DEVICE | USB_REQ_TYPE_STANDARD;
uhost->ctrl.setup.bmRequestType = USB_DIR_H2D | bm_req;
uhost->ctrl.setup.bRequest = USB_STD_REQ_SET_ADDRESS;
uhost->ctrl.setup.wValue = (uint16_t)address;
uhost->ctrl.setup.wLength = 0;
uhost->ctrl.setup.wIndex = 0;
status = usbh_ctrl_request(uhost, 0, 0);
return status;
}
/**
* @brief usb host set interface
* @param uhost: to the structure of usbh_core_type
* @param ept_num: endpoint number
* @param altsetting: alter setting
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_set_interface(usbh_core_type *uhost, uint8_t ept_num, uint8_t altsetting)
{
usb_sts_type status = USB_WAIT;
uint8_t bm_req;
bm_req = USB_REQ_RECIPIENT_INTERFACE | USB_REQ_TYPE_STANDARD;
uhost->ctrl.setup.bmRequestType = USB_DIR_H2D | bm_req;
uhost->ctrl.setup.bRequest = USB_STD_REQ_SET_INTERFACE;
uhost->ctrl.setup.wValue = (uint16_t)altsetting;
uhost->ctrl.setup.wLength = 0;
uhost->ctrl.setup.wIndex = ept_num;
status = usbh_ctrl_request(uhost, 0, 0);
return status;
}
/**
* @brief usb host set feature
* @param uhost: to the structure of usbh_core_type
* @param feature: feature number
* @param index: index number
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_set_feature(usbh_core_type *uhost, uint8_t feature, uint16_t index)
{
usb_sts_type status = USB_WAIT;
uint8_t bm_req;
bm_req = USB_REQ_RECIPIENT_DEVICE | USB_REQ_TYPE_STANDARD;
uhost->ctrl.setup.bmRequestType = USB_DIR_H2D | bm_req;
uhost->ctrl.setup.bRequest = USB_STD_REQ_SET_FEATURE;
uhost->ctrl.setup.wValue = (uint16_t)feature;
uhost->ctrl.setup.wLength = 0;
uhost->ctrl.setup.wIndex = index;
status = usbh_ctrl_request(uhost, 0, 0);
return status;
}
/**
* @brief usb host clear device feature
* @param uhost: to the structure of usbh_core_type
* @param feature: feature number
* @param index: index number
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_clear_dev_feature(usbh_core_type *uhost, uint8_t feature, uint16_t index)
{
usb_sts_type status = USB_WAIT;
uint8_t bm_req;
bm_req = USB_REQ_RECIPIENT_DEVICE | USB_REQ_TYPE_STANDARD;
uhost->ctrl.setup.bmRequestType = USB_DIR_H2D | bm_req;
uhost->ctrl.setup.bRequest = USB_STD_REQ_CLEAR_FEATURE;
uhost->ctrl.setup.wValue = (uint16_t)feature;
uhost->ctrl.setup.wLength = 0;
uhost->ctrl.setup.wIndex = index;
status = usbh_ctrl_request(uhost, 0, 0);
return status;
}
/**
* @brief usb host clear endpoint feature
* @param uhost: to the structure of usbh_core_type
* @param ept_num: endpoint number
* @param hc_num: host channel number
* @retval status: usb_sts_type status
*/
usb_sts_type usbh_clear_ept_feature(usbh_core_type *uhost, uint8_t ept_num, uint8_t hc_num)
{
usb_sts_type status = USB_WAIT;
uint8_t bm_req;
if(uhost->ctrl.state == CONTROL_IDLE )
{
bm_req = USB_REQ_RECIPIENT_ENDPOINT | USB_REQ_TYPE_STANDARD;
uhost->ctrl.setup.bmRequestType = USB_DIR_H2D | bm_req;
uhost->ctrl.setup.bRequest = USB_STD_REQ_CLEAR_FEATURE;
uhost->ctrl.setup.wValue = USB_FEATURE_EPT_HALT;
uhost->ctrl.setup.wLength = 0;
uhost->ctrl.setup.wIndex = ept_num;
usbh_ctrl_request(uhost, 0, 0);
}
if(usbh_ctrl_result_check(uhost, CONTROL_IDLE, ENUM_IDLE) == USB_OK)
{
status = USB_OK;
}
return status;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
@@ -0,0 +1,529 @@
/**
**************************************************************************
* @file usbh_int.c
* @version v2.1.0
* @date 2022-08-16
* @brief usb host interrupt request
**************************************************************************
* 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 "usbh_int.h"
/** @addtogroup AT32F435_437_middlewares_usbh_drivers
* @{
*/
/** @defgroup USBH_drivers_interrupt
* @brief usb host interrupt
* @{
*/
/** @defgroup USBH_int_private_functions
* @{
*/
/**
* @brief usb host interrupt handler
* @param otgdev: to the structure of otg_core_type
* @retval none
*/
void usbh_irq_handler(otg_core_type *otgdev)
{
otg_global_type *usbx = otgdev->usb_reg;
usbh_core_type *uhost = &otgdev->host;
uint32_t intsts = usb_global_get_all_interrupt(usbx);
if(usbx->gintsts_bit.curmode == 1)
{
if(intsts & USB_OTG_HCH_FLAG)
{
usbh_hch_handler(uhost);
usb_global_clear_interrupt(usbx, USB_OTG_HCH_FLAG);
}
if(intsts & USB_OTG_SOF_FLAG)
{
usbh_sof_handler(uhost);
usb_global_clear_interrupt(usbx, USB_OTG_SOF_FLAG);
}
if(intsts & USB_OTG_MODEMIS_FLAG)
{
usb_global_clear_interrupt(usbx, USB_OTG_MODEMIS_FLAG);
}
if(intsts & USB_OTG_WKUP_FLAG)
{
usbh_wakeup_handler(uhost);
usb_global_clear_interrupt(usbx, USB_OTG_WKUP_FLAG);
}
while(usbx->gintsts & USB_OTG_RXFLVL_FLAG)
{
usbh_rx_qlvl_handler(uhost);
usb_global_clear_interrupt(usbx, USB_OTG_RXFLVL_FLAG);
}
if(intsts & USB_OTG_DISCON_FLAG)
{
usbh_disconnect_handler(uhost);
usb_global_clear_interrupt(usbx, USB_OTG_DISCON_FLAG);
}
if(intsts & USB_OTG_PRT_FLAG)
{
usbh_port_handler(uhost);
}
if(intsts & USB_OTG_INCOMPIP_INCOMPISOOUT_FLAG)
{
usb_global_clear_interrupt(usbx, USB_OTG_INCOMPIP_INCOMPISOOUT_FLAG);
}
if(intsts & USB_OTG_INCOMISOIN_FLAG)
{
usb_global_clear_interrupt(usbx, USB_OTG_INCOMISOIN_FLAG);
}
if(intsts & USB_OTG_PTXFEMP_FLAG)
{
usb_global_clear_interrupt(usbx, USB_OTG_PTXFEMP_FLAG);
}
if(intsts & USB_OTG_ISOOUTDROP_FLAG)
{
usb_global_clear_interrupt(usbx, USB_OTG_ISOOUTDROP_FLAG);
}
}
}
/**
* @brief usb host wakeup handler
* @param uhost: to the structure of usbh_core_type
* @retval none
*/
void usbh_wakeup_handler(usbh_core_type *uhost)
{
uhost->global_state = USBH_WAKEUP;
}
/**
* @brief usb host sof handler
* @param uhost: to the structure of usbh_core_type
* @retval none
*/
void usbh_sof_handler(usbh_core_type *uhost)
{
uhost->timer ++;
}
/**
* @brief usb host disconnect handler
* @param uhost: to the structure of usbh_core_type
* @retval none
*/
void usbh_disconnect_handler(usbh_core_type *uhost)
{
otg_global_type *usbx = uhost->usb_reg;
uint8_t i_index;
usb_host_disable(usbx);
uhost->conn_sts = 0;
uhost->global_state = USBH_DISCONNECT;
for(i_index = 0; i_index < USB_HOST_CHANNEL_NUM; i_index ++)
{
usbh_free_channel(uhost, i_index);
}
usbh_fsls_clksel(usbx, USB_HCFG_CLK_48M);
}
/**
* @brief usb host in transfer request handler
* @param uhost: to the structure of usbh_core_type
* @param chn: channel number
* @retval none
*/
void usbh_hch_in_handler(usbh_core_type *uhost, uint8_t chn)
{
otg_global_type *usbx = uhost->usb_reg;
otg_hchannel_type *usb_chh = USB_CHL(usbx, chn);
uint32_t hcint_value = usb_chh->hcint & usb_chh->hcintmsk;
if( hcint_value & USB_OTG_HC_ACK_FLAG)
{
usb_chh->hcint = USB_OTG_HC_ACK_FLAG;
}
else if(hcint_value & USB_OTG_HC_STALL_FLAG)
{
usb_chh->hcintmsk_bit.chhltdmsk = TRUE;
usb_chh->hcint = USB_OTG_HC_NAK_FLAG | USB_OTG_HC_STALL_FLAG;
uhost->hch[chn].state = HCH_STALL;
usb_hch_halt(usbx, chn);
}
else if(hcint_value & USB_OTG_HC_DTGLERR_FLAG)
{
usb_chh->hcintmsk_bit.chhltdmsk = TRUE;
usb_hch_halt(usbx, chn);
usb_chh->hcint = USB_OTG_HC_DTGLERR_FLAG | USB_OTG_HC_NAK_FLAG;
uhost->hch[chn].state = HCH_DATATGLERR;
}
else if(hcint_value & USB_OTG_HC_FRMOVRRUN_FLAG)
{
usb_chh->hcintmsk_bit.chhltdmsk = TRUE;
usb_hch_halt(usbx, chn);
usb_chh->hcint = USB_OTG_HC_FRMOVRRUN_FLAG;
}
else if(hcint_value & USB_OTG_HC_XFERC_FLAG)
{
uhost->hch[chn].state = HCH_XFRC;
usb_chh->hcint = USB_OTG_HC_XFERC_FLAG;
if(usb_chh->hcchar_bit.eptype == EPT_BULK_TYPE || usb_chh->hcchar_bit.eptype == EPT_CONTROL_TYPE)
{
usb_chh->hcintmsk_bit.chhltdmsk = TRUE;
usb_hch_halt(usbx, chn);
usb_chh->hcint = USB_OTG_HC_NAK_FLAG;
}
else if(usb_chh->hcchar_bit.eptype == EPT_INT_TYPE)
{
usb_chh->hcchar_bit.oddfrm = TRUE;
uhost->urb_state[chn] = URB_DONE;
}
else if(usb_chh->hcchar_bit.eptype == EPT_ISO_TYPE)
{
uhost->urb_state[chn] = URB_DONE;
}
uhost->hch[chn].toggle_in ^= 1;
}
else if(hcint_value & USB_OTG_HC_CHHLTD_FLAG)
{
usb_chh->hcintmsk_bit.chhltdmsk = FALSE;
if(uhost->hch[chn].state == HCH_XFRC )
{
uhost->urb_state[chn] = URB_DONE;
}
else if(uhost->hch[chn].state == HCH_STALL)
{
uhost->urb_state[chn] = URB_STALL;
}
else if(uhost->hch[chn].state == HCH_XACTERR ||
uhost->hch[chn].state == HCH_DATATGLERR)
{
uhost->err_cnt[chn] ++;
if(uhost->err_cnt[chn] > 3)
{
uhost->urb_state[chn] = URB_ERROR;
uhost->err_cnt[chn] = 0;
}
else
{
uhost->urb_state[chn] = URB_NOTREADY;
}
usb_chh->hcchar_bit.chdis = FALSE;
usb_chh->hcchar_bit.chena = TRUE;
}
else if(uhost->hch[chn].state == HCH_NAK)
{
usb_chh->hcchar_bit.chdis = FALSE;
usb_chh->hcchar_bit.chena = TRUE;
uhost->urb_state[chn] = URB_NOTREADY;
}
usb_chh->hcint = USB_OTG_HC_CHHLTD_FLAG;
}
else if(hcint_value & USB_OTG_HC_XACTERR_FLAG)
{
usb_chh->hcintmsk_bit.chhltdmsk = TRUE;
uhost->hch[chn].state = HCH_XACTERR;
usb_hch_halt(usbx, chn);
uhost->err_cnt[chn] ++;
usb_chh->hcint = USB_OTG_HC_XACTERR_FLAG;
}
else if(hcint_value & USB_OTG_HC_NAK_FLAG)
{
if(usb_chh->hcchar_bit.eptype == EPT_INT_TYPE)
{
uhost->err_cnt[chn] = 0;
usb_chh->hcintmsk_bit.chhltdmsk = TRUE;
usb_hch_halt(usbx, chn);
}
else if(usb_chh->hcchar_bit.eptype == EPT_BULK_TYPE ||
usb_chh->hcchar_bit.eptype == EPT_CONTROL_TYPE)
{
uhost->err_cnt[chn] = 0;
usb_chh->hcintmsk_bit.chhltdmsk = TRUE;
usb_hch_halt(usbx, chn);
}
uhost->hch[chn].state = HCH_NAK;
usb_chh->hcint = USB_OTG_HC_NAK_FLAG;
}
else if(hcint_value & USB_OTG_HC_BBLERR_FLAG)
{
usb_chh->hcint = USB_OTG_HC_BBLERR_FLAG;
}
}
/**
* @brief usb host out transfer request handler
* @param uhost: to the structure of usbh_core_type
* @param chn: channel number
* @retval none
*/
void usbh_hch_out_handler(usbh_core_type *uhost, uint8_t chn)
{
otg_global_type *usbx = uhost->usb_reg;
otg_hchannel_type *usb_chh = USB_CHL(usbx, chn);
uint32_t hcint_value = usb_chh->hcint & usb_chh->hcintmsk;
if( hcint_value & USB_OTG_HC_ACK_FLAG)
{
usb_chh->hcint = USB_OTG_HC_ACK_FLAG;
}
else if( hcint_value & USB_OTG_HC_FRMOVRRUN_FLAG)
{
usb_chh->hcintmsk_bit.chhltdmsk = TRUE;
usb_hch_halt(usbx, chn);
usb_chh->hcint = USB_OTG_HC_FRMOVRRUN_FLAG;
}
else if( hcint_value & USB_OTG_HC_XFERC_FLAG)
{
usb_chh->hcintmsk_bit.chhltdmsk = TRUE;
usb_hch_halt(usbx, chn);
uhost->hch[chn].state = HCH_XFRC;
usb_chh->hcint = USB_OTG_HC_XFERC_FLAG;
}
else if( hcint_value & USB_OTG_HC_STALL_FLAG)
{
usb_chh->hcintmsk_bit.chhltdmsk = TRUE;
usb_chh->hcint = USB_OTG_HC_STALL_FLAG;
uhost->hch[chn].state = HCH_STALL;
usb_hch_halt(usbx, chn);
}
else if( hcint_value & USB_OTG_HC_DTGLERR_FLAG)
{
usb_chh->hcintmsk_bit.chhltdmsk = TRUE;
usb_hch_halt(usbx, chn);
usb_chh->hcint = USB_OTG_HC_DTGLERR_FLAG | USB_OTG_HC_NAK_FLAG;
uhost->hch[chn].state = HCH_DATATGLERR;
}
else if( hcint_value & USB_OTG_HC_CHHLTD_FLAG)
{
usb_chh->hcintmsk_bit.chhltdmsk = FALSE;
if(uhost->hch[chn].state == HCH_XFRC)
{
uhost->urb_state[chn] = URB_DONE;
if(uhost->hch[chn].ept_type == EPT_BULK_TYPE ||
uhost->hch[chn].ept_type == EPT_INT_TYPE)
{
uhost->hch[chn].toggle_out ^= 1;
}
}
else if(uhost->hch[chn].state == HCH_NAK)
{
uhost->urb_state[chn] = URB_NOTREADY;
}
else if(uhost->hch[chn].state == HCH_STALL)
{
uhost->hch[chn].urb_sts = URB_STALL;
}
else if(uhost->hch[chn].state == HCH_XACTERR ||
uhost->hch[chn].state == HCH_DATATGLERR)
{
uhost->err_cnt[chn] ++;
if(uhost->err_cnt[chn] > 3)
{
uhost->urb_state[chn] = URB_ERROR;
uhost->err_cnt[chn] = 0;
}
else
{
uhost->urb_state[chn] = URB_NOTREADY;
}
usb_chh->hcchar_bit.chdis = FALSE;
usb_chh->hcchar_bit.chena = TRUE;
}
usb_chh->hcint = USB_OTG_HC_CHHLTD_FLAG;
}
else if( hcint_value & USB_OTG_HC_XACTERR_FLAG)
{
usb_chh->hcintmsk_bit.chhltdmsk = TRUE;
uhost->err_cnt[chn] ++;
uhost->hch[chn].state = HCH_XACTERR;
usb_hch_halt(usbx, chn);
usb_chh->hcint = USB_OTG_HC_XACTERR_FLAG | USB_OTG_HC_NAK_FLAG;
}
else if( hcint_value & USB_OTG_HC_NAK_FLAG)
{
usb_chh->hcintmsk_bit.chhltdmsk = TRUE;
uhost->err_cnt[chn] = 0;
usb_hch_halt(usbx, chn);
uhost->hch[chn].state = HCH_NAK;
usb_chh->hcint = USB_OTG_HC_NAK_FLAG;
}
}
/**
* @brief usb host channel request handler
* @param uhost: to the structure of usbh_core_type
* @retval none
*/
void usbh_hch_handler(usbh_core_type *uhost)
{
otg_global_type *usbx = uhost->usb_reg;
otg_host_type *usb_host = OTG_HOST(usbx);
uint32_t intsts, i_index;
intsts = usb_host->haint & 0xFFFF;
for(i_index = 0; i_index < 16; i_index ++)
{
if(intsts & (1 << i_index))
{
if(USB_CHL(usbx, i_index)->hcchar_bit.eptdir)
{
//hc in
usbh_hch_in_handler(uhost, i_index);
}
else
{
//hc out
usbh_hch_out_handler(uhost, i_index);
}
}
}
}
/**
* @brief usb host rx buffer not empty request handler
* @param uhost: to the structure of usbh_core_type
* @retval none
*/
void usbh_rx_qlvl_handler(usbh_core_type *uhost)
{
uint8_t chn;
uint32_t pktsts;
uint32_t pktcnt;
uint32_t tmp;
otg_hchannel_type *ch;
otg_global_type *usbx = uhost->usb_reg;
usbx->gintmsk_bit.rxflvlmsk = 0;
tmp = usbx->grxstsp;
chn = tmp & 0xF;
pktsts = (tmp >> 17) & 0xF;
pktcnt = (tmp >> 4) & 0x7FF;
ch = USB_CHL(usbx, chn);
switch(pktsts)
{
case PKTSTS_IN_DATA_PACKET_RECV:
if(pktcnt > 0 && (uhost->hch[chn].trans_buf) != 0)
{
usb_read_packet(usbx, uhost->hch[chn].trans_buf, chn, pktcnt);
uhost->hch[chn].trans_buf += pktcnt;
uhost->hch[chn].trans_count += pktcnt;
if(ch->hctsiz_bit.pktcnt > 0)
{
ch->hcchar_bit.chdis = FALSE;
ch->hcchar_bit.chena = TRUE;
uhost->hch[chn].toggle_in ^= 1;
}
}
break;
case PKTSTS_IN_TRANSFER_COMPLETE:
break;
case PKTSTS_DATA_BIT_ERROR:
break;
case PKTSTS_CHANNEL_STOP:
break;
default:
break;
}
usbx->gintmsk_bit.rxflvlmsk = 1;
}
/**
* @brief usb host port request handler
* @param uhost: to the structure of usbh_core_type
* @retval none
*/
void usbh_port_handler(usbh_core_type *uhost)
{
otg_global_type *usbx = uhost->usb_reg;
otg_host_type *usb_host = OTG_HOST(usbx);
uint32_t prt = 0, prt_0;
prt = usb_host->hprt;
prt_0 = prt;
prt_0 &= ~(USB_OTG_HPRT_PRTENA | USB_OTG_HPRT_PRTENCHNG |
USB_OTG_HPRT_PRTOVRCACT | USB_OTG_HPRT_PRTCONDET);
if(prt & USB_OTG_HPRT_PRTCONDET)
{
if(prt & USB_OTG_HPRT_PRTCONSTS)
{
/* connect callback */
uhost->conn_sts = 1;
}
prt_0 |= USB_OTG_HPRT_PRTCONDET;
}
if(prt & USB_OTG_HPRT_PRTENCHNG)
{
prt_0 |= USB_OTG_HPRT_PRTENCHNG;
if(prt & USB_OTG_HPRT_PRTENA)
{
if((prt & USB_OTG_HPRT_PRTSPD) == (USB_PRTSPD_LOW_SPEED << 17))
{
usbh_fsls_clksel(usbx, USB_HCFG_CLK_6M);
}
else
{
usbh_fsls_clksel(usbx, USB_HCFG_CLK_48M);
}
/* connect callback */
uhost->port_enable = 1;
}
else
{
/* clean up hprt */
uhost->port_enable = 0;
}
}
if(prt & USB_OTG_HPRT_PRTOVRCACT)
{
prt_0 |= USB_OTG_HPRT_PRTOVRCACT;
}
usb_host->hprt = prt_0;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/