diff --git a/cores/arduino/stm32/usb/cdc/cdc_queue.c b/cores/arduino/stm32/usb/cdc/cdc_queue.c index 2d2330c195..2b1f6d9672 100644 --- a/cores/arduino/stm32/usb/cdc/cdc_queue.c +++ b/cores/arduino/stm32/usb/cdc/cdc_queue.c @@ -10,27 +10,28 @@ * *

© COPYRIGHT(c) 2017 STMicroelectronics

* - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************** */ @@ -43,46 +44,53 @@ // Initialize read and write position of queue void CDC_TransmitQueue_Init(CDC_TransmitQueue_TypeDef *queue) { - queue->read = 0; + queue->read = 0; queue->write = 0; } // Determine size, available for write in queue int CDC_TransmitQueue_WriteSize(CDC_TransmitQueue_TypeDef *queue) { - return (queue->read + CDC_TRANSMIT_QUEUE_BUFFER_SIZE - queue->write - 1) - % CDC_TRANSMIT_QUEUE_BUFFER_SIZE; + return (queue->read + CDC_TRANSMIT_QUEUE_BUFFER_SIZE - queue->write - 1) % + CDC_TRANSMIT_QUEUE_BUFFER_SIZE; } // Determine size of data, stored in queue int CDC_TransmitQueue_ReadSize(CDC_TransmitQueue_TypeDef *queue) { - return (queue->write + CDC_TRANSMIT_QUEUE_BUFFER_SIZE - queue->read) - % CDC_TRANSMIT_QUEUE_BUFFER_SIZE; + return (queue->write + CDC_TRANSMIT_QUEUE_BUFFER_SIZE - queue->read) % + CDC_TRANSMIT_QUEUE_BUFFER_SIZE; } // Write provided data into queue. void CDC_TransmitQueue_Enqueue(CDC_TransmitQueue_TypeDef *queue, - const uint8_t *buffer, uint32_t size) + const uint8_t *buffer, + uint32_t size) { uint32_t sizeToEnd = CDC_TRANSMIT_QUEUE_BUFFER_SIZE - queue->write; - if (sizeToEnd > size) { + if (sizeToEnd > size) + { memcpy(&queue->buffer[queue->write], &buffer[0], size); - } else { + } + else + { memcpy(&queue->buffer[queue->write], &buffer[0], sizeToEnd); memcpy(&queue->buffer[0], &buffer[sizeToEnd], size - sizeToEnd); } - queue->write = (uint16_t)((queue->write + size) % - CDC_TRANSMIT_QUEUE_BUFFER_SIZE); + queue->write = + (uint16_t)((queue->write + size) % CDC_TRANSMIT_QUEUE_BUFFER_SIZE); } // Read flat block from queue biggest as possible, but max QUEUE_MAX_PACKET_SIZE uint8_t *CDC_TransmitQueue_ReadBlock(CDC_TransmitQueue_TypeDef *queue, uint16_t *size) { - if (queue->write >= queue->read) { + if (queue->write >= queue->read) + { *size = queue->write - queue->read; - } else { + } + else + { *size = CDC_TRANSMIT_QUEUE_BUFFER_SIZE - queue->read; } queue->reserved = *size; @@ -91,15 +99,15 @@ uint8_t *CDC_TransmitQueue_ReadBlock(CDC_TransmitQueue_TypeDef *queue, void CDC_TransmitQueue_CommitRead(CDC_TransmitQueue_TypeDef *queue) { - queue->read = (queue->read + queue->reserved) % - CDC_TRANSMIT_QUEUE_BUFFER_SIZE; + queue->read = + (queue->read + queue->reserved) % CDC_TRANSMIT_QUEUE_BUFFER_SIZE; } // Initialize read and write position of queue. void CDC_ReceiveQueue_Init(CDC_ReceiveQueue_TypeDef *queue) { - queue->read = 0; - queue->write = 0; + queue->read = 0; + queue->write = 0; queue->length = CDC_RECEIVE_QUEUE_BUFFER_SIZE; } @@ -107,22 +115,28 @@ void CDC_ReceiveQueue_Init(CDC_ReceiveQueue_TypeDef *queue) uint8_t *CDC_ReceiveQueue_ReserveBlock(CDC_ReceiveQueue_TypeDef *queue) { const uint16_t limit = - CDC_RECEIVE_QUEUE_BUFFER_SIZE - CDC_QUEUE_MAX_PACKET_SIZE; + CDC_RECEIVE_QUEUE_BUFFER_SIZE - CDC_QUEUE_MAX_PACKET_SIZE; volatile uint16_t read = queue->read; - if (read <= queue->write) { + if (read <= queue->write) + { // if write is limited only by buffer size. - if (queue->write < limit || (queue->write == limit && read > 0)) { + if (queue->write < limit || (queue->write == limit && read > 0)) + { // if size in the rest of buffer is enough for full packet plus 1 byte // or if it tight enough and write position can be set to 0 return queue->buffer + queue->write; - } else if (read > CDC_QUEUE_MAX_PACKET_SIZE) { + } + else if (read > CDC_QUEUE_MAX_PACKET_SIZE) + { // if size in the rest is not enough, but enough size in head queue->length = queue->write; - queue->write = 0; + queue->write = 0; return queue->buffer + queue->write; } - } else if (queue->write + CDC_QUEUE_MAX_PACKET_SIZE < read) { + } + else if (queue->write + CDC_QUEUE_MAX_PACKET_SIZE < read) + { // write position must be less than read position // after reading largest possible packet return queue->buffer + queue->write; @@ -135,10 +149,12 @@ void CDC_ReceiveQueue_CommitBlock(CDC_ReceiveQueue_TypeDef *queue, uint16_t size) { queue->write += size; - if (queue->write >= queue->length) { + if (queue->write >= queue->length) + { queue->length = CDC_RECEIVE_QUEUE_BUFFER_SIZE; } - if (queue->write >= CDC_RECEIVE_QUEUE_BUFFER_SIZE) { + if (queue->write >= CDC_RECEIVE_QUEUE_BUFFER_SIZE) + { queue->write = 0; } } @@ -148,9 +164,10 @@ int CDC_ReceiveQueue_ReadSize(CDC_ReceiveQueue_TypeDef *queue) { // reading length after write make guarantee, that length >= write // and determined reading size will be smaller or equal than real one. - volatile uint16_t write = queue->write; + volatile uint16_t write = queue->write; volatile uint16_t length = queue->length; - if (write >= queue->read) { + if (write >= queue->read) + { return write - queue->read; } return length + write - queue->read; @@ -159,16 +176,19 @@ int CDC_ReceiveQueue_ReadSize(CDC_ReceiveQueue_TypeDef *queue) // Read one byte from queue. int CDC_ReceiveQueue_Dequeue(CDC_ReceiveQueue_TypeDef *queue) { - volatile uint16_t write = queue->write; + volatile uint16_t write = queue->write; volatile uint16_t length = queue->length; - if (queue->read == length) { + if (queue->read == length) + { queue->read = 0; } - if (write == queue->read) { + if (write == queue->read) + { return -1; } uint8_t ch = queue->buffer[queue->read++]; - if (queue->read >= length) { + if (queue->read >= length) + { queue->read = 0; } return ch; @@ -177,84 +197,108 @@ int CDC_ReceiveQueue_Dequeue(CDC_ReceiveQueue_TypeDef *queue) // Peek byte from queue. int CDC_ReceiveQueue_Peek(CDC_ReceiveQueue_TypeDef *queue) { - volatile uint16_t write = queue->write; + volatile uint16_t write = queue->write; volatile uint16_t length = queue->length; - if (queue->read >= length) { + if (queue->read >= length) + { queue->read = 0; } - if (write == queue->read) { + if (write == queue->read) + { return -1; } return queue->buffer[queue->read]; } uint16_t CDC_ReceiveQueue_Read(CDC_ReceiveQueue_TypeDef *queue, - uint8_t *buffer, uint16_t size) + uint8_t *buffer, + uint16_t size) { - volatile uint16_t write = queue->write; + volatile uint16_t write = queue->write; volatile uint16_t length = queue->length; uint16_t available; - if (queue->read >= length) { + if (queue->read >= length) + { queue->read = 0; } - if (write >= queue->read) { + if (write >= queue->read) + { available = write - queue->read; - } else { + } + else + { available = length - queue->read; } - if (available < size) { + if (available < size) + { size = available; } memcpy(buffer, &queue->buffer[queue->read], size); queue->read = queue->read + size; - if (queue->read >= length) { + if (queue->read >= length) + { queue->read = 0; } return size; } bool CDC_ReceiveQueue_ReadUntil(CDC_ReceiveQueue_TypeDef *queue, - uint8_t terminator, uint8_t *buffer, uint16_t size, uint16_t *fetched) + uint8_t terminator, + uint8_t *buffer, + uint16_t size, + uint16_t *fetched) { - volatile uint16_t write = queue->write; + volatile uint16_t write = queue->write; volatile uint16_t length = queue->length; uint16_t available; - if (queue->read >= length) { + if (queue->read >= length) + { queue->read = 0; } - if (write >= queue->read) { + if (write >= queue->read) + { available = write - queue->read; - } else { + } + else + { available = length - queue->read; } - if (available < size) { + if (available < size) + { size = available; } uint8_t *start = &queue->buffer[queue->read]; - for (uint16_t i = 0; i < size; i++) { + for (uint16_t i = 0; i < size; i++) + { uint8_t ch = start[i]; - if (ch == terminator) { + if (ch == terminator) + { queue->read += (uint16_t)(i + 1); - if (queue->read >= length) { + if (queue->read >= length) + { queue->read = 0; } *fetched = i; return true; - } else { + } + else + { buffer[i] = ch; } } *fetched = size; queue->read += size; - if (queue->read >= length) { + if (queue->read >= length) + { queue->read = 0; } return false; } + #endif /* USBD_USE_CDC */ #endif /* USBCON */ diff --git a/cores/arduino/stm32/usb/cdc/cdc_queue.h b/cores/arduino/stm32/usb/cdc/cdc_queue.h index b87754b87c..2836fa7c17 100644 --- a/cores/arduino/stm32/usb/cdc/cdc_queue.h +++ b/cores/arduino/stm32/usb/cdc/cdc_queue.h @@ -10,32 +10,32 @@ * *

© COPYRIGHT(c) 2016 STMicroelectronics

* - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************** */ - /* Define to prevent recursive inclusion -------------------------------------*/ #ifndef __CDC_QUEUE_H #define __CDC_QUEUE_H @@ -48,21 +48,23 @@ extern "C" { #endif #if USE_USB_HS -#define CDC_QUEUE_MAX_PACKET_SIZE USB_HS_MAX_PACKET_SIZE +#define CDC_QUEUE_MAX_PACKET_SIZE USB_HS_MAX_PACKET_SIZE #else -#define CDC_QUEUE_MAX_PACKET_SIZE USB_FS_MAX_PACKET_SIZE +#define CDC_QUEUE_MAX_PACKET_SIZE USB_FS_MAX_PACKET_SIZE #endif #define CDC_TRANSMIT_QUEUE_BUFFER_SIZE ((uint16_t)(CDC_QUEUE_MAX_PACKET_SIZE * 2)) -#define CDC_RECEIVE_QUEUE_BUFFER_SIZE ((uint16_t)(CDC_QUEUE_MAX_PACKET_SIZE * 3)) +#define CDC_RECEIVE_QUEUE_BUFFER_SIZE ((uint16_t)(CDC_QUEUE_MAX_PACKET_SIZE * 3)) -typedef struct { +typedef struct +{ uint8_t buffer[CDC_TRANSMIT_QUEUE_BUFFER_SIZE]; volatile uint16_t write; volatile uint16_t read; volatile uint16_t reserved; } CDC_TransmitQueue_TypeDef; -typedef struct { +typedef struct +{ uint8_t buffer[CDC_RECEIVE_QUEUE_BUFFER_SIZE]; volatile uint16_t write; volatile uint16_t read; @@ -70,24 +72,44 @@ typedef struct { } CDC_ReceiveQueue_TypeDef; void CDC_TransmitQueue_Init(CDC_TransmitQueue_TypeDef *queue); + int CDC_TransmitQueue_WriteSize(CDC_TransmitQueue_TypeDef *queue); + int CDC_TransmitQueue_ReadSize(CDC_TransmitQueue_TypeDef *queue); -void CDC_TransmitQueue_Enqueue(CDC_TransmitQueue_TypeDef *queue, const uint8_t *buffer, uint32_t size); -uint8_t *CDC_TransmitQueue_ReadBlock(CDC_TransmitQueue_TypeDef *queue, uint16_t *size); + +void CDC_TransmitQueue_Enqueue(CDC_TransmitQueue_TypeDef *queue, + const uint8_t *buffer, uint32_t size); + +uint8_t *CDC_TransmitQueue_ReadBlock(CDC_TransmitQueue_TypeDef *queue, + uint16_t *size); + void CDC_TransmitQueue_CommitRead(CDC_TransmitQueue_TypeDef *queue); void CDC_ReceiveQueue_Init(CDC_ReceiveQueue_TypeDef *queue); + int CDC_ReceiveQueue_ReadSize(CDC_ReceiveQueue_TypeDef *queue); + int CDC_ReceiveQueue_Dequeue(CDC_ReceiveQueue_TypeDef *queue); + int CDC_ReceiveQueue_Peek(CDC_ReceiveQueue_TypeDef *queue); -uint16_t CDC_ReceiveQueue_Read(CDC_ReceiveQueue_TypeDef *queue, uint8_t *buffer, uint16_t size); -bool CDC_ReceiveQueue_ReadUntil(CDC_ReceiveQueue_TypeDef *queue, uint8_t terminator, uint8_t *buffer, - uint16_t size, uint16_t *fetched); + +uint16_t CDC_ReceiveQueue_Read(CDC_ReceiveQueue_TypeDef *queue, + uint8_t *buffer, + uint16_t size); + +bool CDC_ReceiveQueue_ReadUntil(CDC_ReceiveQueue_TypeDef *queue, + uint8_t terminator, + uint8_t *buffer, + uint16_t size, + uint16_t *fetched); + uint8_t *CDC_ReceiveQueue_ReserveBlock(CDC_ReceiveQueue_TypeDef *queue); -void CDC_ReceiveQueue_CommitBlock(CDC_ReceiveQueue_TypeDef *queue, uint16_t size); + +void CDC_ReceiveQueue_CommitBlock(CDC_ReceiveQueue_TypeDef *queue, + uint16_t size); #ifdef __cplusplus } #endif -#endif // __CDC_QUEUE_H \ No newline at end of file +#endif // __CDC_QUEUE_H diff --git a/cores/arduino/stm32/usb/cdc/usbd_cdc.c b/cores/arduino/stm32/usb/cdc/usbd_cdc.c index 99f57945d4..18ca7ea052 100644 --- a/cores/arduino/stm32/usb/cdc/usbd_cdc.c +++ b/cores/arduino/stm32/usb/cdc/usbd_cdc.c @@ -1,887 +1,926 @@ -/** - ****************************************************************************** - * @file usbd_cdc.c - * @author MCD Application Team - * @brief This file provides the high layer firmware functions to manage the - * following functionalities of the USB CDC Class: - * - Initialization and Configuration of high and low layer - * - Enumeration as CDC Device (and enumeration for each implemented memory interface) - * - OUT/IN data transfer - * - Command IN transfer (class requests management) - * - Error management - * - * @verbatim - * - * =================================================================== - * CDC Class Driver Description - * =================================================================== - * This driver manages the "Universal Serial Bus Class Definitions for Communications Devices - * Revision 1.2 November 16, 2007" and the sub-protocol specification of "Universal Serial Bus - * Communications Class Subclass Specification for PSTN Devices Revision 1.2 February 9, 2007" - * This driver implements the following aspects of the specification: - * - Device descriptor management - * - Configuration descriptor management - * - Enumeration as CDC device with 2 data endpoints (IN and OUT) and 1 command endpoint (IN) - * - Requests management (as described in section 6.2 in specification) - * - Abstract Control Model compliant - * - Union Functional collection (using 1 IN endpoint for control) - * - Data interface class - * - * These aspects may be enriched or modified for a specific user application. - * - * This driver doesn't implement the following aspects of the specification - * (but it is possible to manage these features with some modifications on this driver): - * - Any class-specific aspect relative to communication classes should be managed by user application. - * - All communication classes other than PSTN are not managed - * - * @endverbatim - * - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -#ifdef USBCON -#ifdef USBD_USE_CDC - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_cdc.h" -#include "usbd_ctlreq.h" - - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - - -/** @defgroup USBD_CDC - * @brief usbd core module - * @{ - */ - -/** @defgroup USBD_CDC_Private_TypesDefinitions - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBD_CDC_Private_Defines - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBD_CDC_Private_Macros - * @{ - */ - -/** - * @} - */ - - -/** @defgroup USBD_CDC_Private_FunctionPrototypes - * @{ - */ - - -static uint8_t USBD_CDC_Init(USBD_HandleTypeDef *pdev, - uint8_t cfgidx); - -static uint8_t USBD_CDC_DeInit(USBD_HandleTypeDef *pdev, - uint8_t cfgidx); - -static uint8_t USBD_CDC_Setup(USBD_HandleTypeDef *pdev, - USBD_SetupReqTypedef *req); - -static uint8_t USBD_CDC_DataIn(USBD_HandleTypeDef *pdev, - uint8_t epnum); - -static uint8_t USBD_CDC_DataOut(USBD_HandleTypeDef *pdev, - uint8_t epnum); - -static uint8_t USBD_CDC_EP0_RxReady(USBD_HandleTypeDef *pdev); - -static uint8_t *USBD_CDC_GetFSCfgDesc(uint16_t *length); - -static uint8_t *USBD_CDC_GetHSCfgDesc(uint16_t *length); - -static uint8_t *USBD_CDC_GetOtherSpeedCfgDesc(uint16_t *length); - -static uint8_t *USBD_CDC_GetOtherSpeedCfgDesc(uint16_t *length); - -uint8_t *USBD_CDC_GetDeviceQualifierDescriptor(uint16_t *length); - -/* USB Standard Device Descriptor */ -__ALIGN_BEGIN static uint8_t USBD_CDC_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END = { - USB_LEN_DEV_QUALIFIER_DESC, - USB_DESC_TYPE_DEVICE_QUALIFIER, - 0x00, - 0x02, - 0x00, - 0x00, - 0x00, - 0x40, - 0x01, - 0x00, -}; - -/** - * @} - */ - -/** @defgroup USBD_CDC_Private_Variables - * @{ - */ - - -/* CDC interface class callbacks structure */ -USBD_ClassTypeDef USBD_CDC = { - USBD_CDC_Init, - USBD_CDC_DeInit, - USBD_CDC_Setup, - NULL, /* EP0_TxSent, */ - USBD_CDC_EP0_RxReady, - USBD_CDC_DataIn, - USBD_CDC_DataOut, - NULL, - NULL, - NULL, - USBD_CDC_GetHSCfgDesc, - USBD_CDC_GetFSCfgDesc, - USBD_CDC_GetOtherSpeedCfgDesc, - USBD_CDC_GetDeviceQualifierDescriptor, -}; - -/* USB CDC device Configuration Descriptor */ -__ALIGN_BEGIN uint8_t USBD_CDC_CfgHSDesc[USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END = { - /*Configuration Descriptor*/ - 0x09, /* bLength: Configuration Descriptor size */ - USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ - USB_CDC_CONFIG_DESC_SIZ, /* wTotalLength:no of returned bytes */ - 0x00, - 0x02, /* bNumInterfaces: 2 interface */ - 0x01, /* bConfigurationValue: Configuration value */ - 0x00, /* iConfiguration: Index of string descriptor describing the configuration */ - 0xC0, /* bmAttributes: self powered */ - 0x32, /* MaxPower 0 mA */ - - /*---------------------------------------------------------------------------*/ - - /*Interface Descriptor */ - 0x09, /* bLength: Interface Descriptor size */ - USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */ - /* Interface descriptor type */ - 0x00, /* bInterfaceNumber: Number of Interface */ - 0x00, /* bAlternateSetting: Alternate setting */ - 0x01, /* bNumEndpoints: One endpoints used */ - 0x02, /* bInterfaceClass: Communication Interface Class */ - 0x02, /* bInterfaceSubClass: Abstract Control Model */ - 0x01, /* bInterfaceProtocol: Common AT commands */ - 0x00, /* iInterface: */ - - /*Header Functional Descriptor*/ - 0x05, /* bLength: Endpoint Descriptor size */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x00, /* bDescriptorSubtype: Header Func Desc */ - 0x10, /* bcdCDC: spec release number */ - 0x01, - - /*Call Management Functional Descriptor*/ - 0x05, /* bFunctionLength */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x01, /* bDescriptorSubtype: Call Management Func Desc */ - 0x00, /* bmCapabilities: D0+D1 */ - 0x01, /* bDataInterface: 1 */ - - /*ACM Functional Descriptor*/ - 0x04, /* bFunctionLength */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x02, /* bDescriptorSubtype: Abstract Control Management desc */ - 0x02, /* bmCapabilities */ - - /*Union Functional Descriptor*/ - 0x05, /* bFunctionLength */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x06, /* bDescriptorSubtype: Union func desc */ - 0x00, /* bMasterInterface: Communication class interface */ - 0x01, /* bSlaveInterface0: Data Class Interface */ - - /*Endpoint 2 Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ - CDC_CMD_EP, /* bEndpointAddress */ - 0x03, /* bmAttributes: Interrupt */ - LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize: */ - HIBYTE(CDC_CMD_PACKET_SIZE), - CDC_HS_BINTERVAL, /* bInterval: */ - /*---------------------------------------------------------------------------*/ - - /*Data class interface descriptor*/ - 0x09, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_INTERFACE, /* bDescriptorType: */ - 0x01, /* bInterfaceNumber: Number of Interface */ - 0x00, /* bAlternateSetting: Alternate setting */ - 0x02, /* bNumEndpoints: Two endpoints used */ - 0x0A, /* bInterfaceClass: CDC */ - 0x00, /* bInterfaceSubClass: */ - 0x00, /* bInterfaceProtocol: */ - 0x00, /* iInterface: */ - - /*Endpoint OUT Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ - CDC_OUT_EP, /* bEndpointAddress */ - 0x02, /* bmAttributes: Bulk */ - LOBYTE(CDC_DATA_HS_MAX_PACKET_SIZE), /* wMaxPacketSize: */ - HIBYTE(CDC_DATA_HS_MAX_PACKET_SIZE), - 0x00, /* bInterval: ignore for Bulk transfer */ - - /*Endpoint IN Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ - CDC_IN_EP, /* bEndpointAddress */ - 0x02, /* bmAttributes: Bulk */ - LOBYTE(CDC_DATA_HS_MAX_PACKET_SIZE), /* wMaxPacketSize: */ - HIBYTE(CDC_DATA_HS_MAX_PACKET_SIZE), - 0x00 /* bInterval: ignore for Bulk transfer */ -} ; - - -/* USB CDC device Configuration Descriptor */ -__ALIGN_BEGIN uint8_t USBD_CDC_CfgFSDesc[USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END = { - /*Configuration Descriptor*/ - 0x09, /* bLength: Configuration Descriptor size */ - USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ - USB_CDC_CONFIG_DESC_SIZ, /* wTotalLength:no of returned bytes */ - 0x00, - 0x02, /* bNumInterfaces: 2 interface */ - 0x01, /* bConfigurationValue: Configuration value */ - 0x00, /* iConfiguration: Index of string descriptor describing the configuration */ - 0xC0, /* bmAttributes: self powered */ - 0x32, /* MaxPower 0 mA */ - - /*---------------------------------------------------------------------------*/ - - /*Interface Descriptor */ - 0x09, /* bLength: Interface Descriptor size */ - USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */ - /* Interface descriptor type */ - 0x00, /* bInterfaceNumber: Number of Interface */ - 0x00, /* bAlternateSetting: Alternate setting */ - 0x01, /* bNumEndpoints: One endpoints used */ - 0x02, /* bInterfaceClass: Communication Interface Class */ - 0x02, /* bInterfaceSubClass: Abstract Control Model */ - 0x01, /* bInterfaceProtocol: Common AT commands */ - 0x00, /* iInterface: */ - - /*Header Functional Descriptor*/ - 0x05, /* bLength: Endpoint Descriptor size */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x00, /* bDescriptorSubtype: Header Func Desc */ - 0x10, /* bcdCDC: spec release number */ - 0x01, - - /*Call Management Functional Descriptor*/ - 0x05, /* bFunctionLength */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x01, /* bDescriptorSubtype: Call Management Func Desc */ - 0x00, /* bmCapabilities: D0+D1 */ - 0x01, /* bDataInterface: 1 */ - - /*ACM Functional Descriptor*/ - 0x04, /* bFunctionLength */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x02, /* bDescriptorSubtype: Abstract Control Management desc */ - 0x02, /* bmCapabilities */ - - /*Union Functional Descriptor*/ - 0x05, /* bFunctionLength */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x06, /* bDescriptorSubtype: Union func desc */ - 0x00, /* bMasterInterface: Communication class interface */ - 0x01, /* bSlaveInterface0: Data Class Interface */ - - /*Endpoint 2 Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ - CDC_CMD_EP, /* bEndpointAddress */ - 0x03, /* bmAttributes: Interrupt */ - LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize: */ - HIBYTE(CDC_CMD_PACKET_SIZE), - CDC_FS_BINTERVAL, /* bInterval: */ - /*---------------------------------------------------------------------------*/ - - /*Data class interface descriptor*/ - 0x09, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_INTERFACE, /* bDescriptorType: */ - 0x01, /* bInterfaceNumber: Number of Interface */ - 0x00, /* bAlternateSetting: Alternate setting */ - 0x02, /* bNumEndpoints: Two endpoints used */ - 0x0A, /* bInterfaceClass: CDC */ - 0x00, /* bInterfaceSubClass: */ - 0x00, /* bInterfaceProtocol: */ - 0x00, /* iInterface: */ - - /*Endpoint OUT Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ - CDC_OUT_EP, /* bEndpointAddress */ - 0x02, /* bmAttributes: Bulk */ - LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), /* wMaxPacketSize: */ - HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), - 0x00, /* bInterval: ignore for Bulk transfer */ - - /*Endpoint IN Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ - CDC_IN_EP, /* bEndpointAddress */ - 0x02, /* bmAttributes: Bulk */ - LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), /* wMaxPacketSize: */ - HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), - 0x00 /* bInterval: ignore for Bulk transfer */ -} ; - -__ALIGN_BEGIN uint8_t USBD_CDC_OtherSpeedCfgDesc[USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END = { - 0x09, /* bLength: Configuation Descriptor size */ - USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION, - USB_CDC_CONFIG_DESC_SIZ, - 0x00, - 0x02, /* bNumInterfaces: 2 interfaces */ - 0x01, /* bConfigurationValue: */ - 0x04, /* iConfiguration: */ - 0xC0, /* bmAttributes: */ - 0x32, /* MaxPower 100 mA */ - - /*Interface Descriptor */ - 0x09, /* bLength: Interface Descriptor size */ - USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */ - /* Interface descriptor type */ - 0x00, /* bInterfaceNumber: Number of Interface */ - 0x00, /* bAlternateSetting: Alternate setting */ - 0x01, /* bNumEndpoints: One endpoints used */ - 0x02, /* bInterfaceClass: Communication Interface Class */ - 0x02, /* bInterfaceSubClass: Abstract Control Model */ - 0x01, /* bInterfaceProtocol: Common AT commands */ - 0x00, /* iInterface: */ - - /*Header Functional Descriptor*/ - 0x05, /* bLength: Endpoint Descriptor size */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x00, /* bDescriptorSubtype: Header Func Desc */ - 0x10, /* bcdCDC: spec release number */ - 0x01, - - /*Call Management Functional Descriptor*/ - 0x05, /* bFunctionLength */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x01, /* bDescriptorSubtype: Call Management Func Desc */ - 0x00, /* bmCapabilities: D0+D1 */ - 0x01, /* bDataInterface: 1 */ - - /*ACM Functional Descriptor*/ - 0x04, /* bFunctionLength */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x02, /* bDescriptorSubtype: Abstract Control Management desc */ - 0x02, /* bmCapabilities */ - - /*Union Functional Descriptor*/ - 0x05, /* bFunctionLength */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x06, /* bDescriptorSubtype: Union func desc */ - 0x00, /* bMasterInterface: Communication class interface */ - 0x01, /* bSlaveInterface0: Data Class Interface */ - - /*Endpoint 2 Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ - CDC_CMD_EP, /* bEndpointAddress */ - 0x03, /* bmAttributes: Interrupt */ - LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize: */ - HIBYTE(CDC_CMD_PACKET_SIZE), - CDC_FS_BINTERVAL, /* bInterval: */ - - /*---------------------------------------------------------------------------*/ - - /*Data class interface descriptor*/ - 0x09, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_INTERFACE, /* bDescriptorType: */ - 0x01, /* bInterfaceNumber: Number of Interface */ - 0x00, /* bAlternateSetting: Alternate setting */ - 0x02, /* bNumEndpoints: Two endpoints used */ - 0x0A, /* bInterfaceClass: CDC */ - 0x00, /* bInterfaceSubClass: */ - 0x00, /* bInterfaceProtocol: */ - 0x00, /* iInterface: */ - - /*Endpoint OUT Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ - CDC_OUT_EP, /* bEndpointAddress */ - 0x02, /* bmAttributes: Bulk */ - 0x40, /* wMaxPacketSize: */ - 0x00, - 0x00, /* bInterval: ignore for Bulk transfer */ - - /*Endpoint IN Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ - CDC_IN_EP, /* bEndpointAddress */ - 0x02, /* bmAttributes: Bulk */ - 0x40, /* wMaxPacketSize: */ - 0x00, - 0x00 /* bInterval */ -}; - -/** - * @} - */ - -/** @defgroup USBD_CDC_Private_Functions - * @{ - */ - -/** - * @brief USBD_CDC_Init - * Initialize the CDC interface - * @param pdev: device instance - * @param cfgidx: Configuration index - * @retval status - */ -static uint8_t USBD_CDC_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx) -{ - uint8_t ret = 0U; - UNUSED(cfgidx); - USBD_CDC_HandleTypeDef *hcdc; - - if (pdev->dev_speed == USBD_SPEED_HIGH) { - /* Open EP IN */ - USBD_LL_OpenEP(pdev, CDC_IN_EP, USBD_EP_TYPE_BULK, - CDC_DATA_HS_IN_PACKET_SIZE); - - pdev->ep_in[CDC_IN_EP & 0xFU].is_used = 1U; - - /* Open EP OUT */ - USBD_LL_OpenEP(pdev, CDC_OUT_EP, USBD_EP_TYPE_BULK, - CDC_DATA_HS_OUT_PACKET_SIZE); - - pdev->ep_out[CDC_OUT_EP & 0xFU].is_used = 1U; - - } else { - /* Open EP IN */ - USBD_LL_OpenEP(pdev, CDC_IN_EP, USBD_EP_TYPE_BULK, - CDC_DATA_FS_IN_PACKET_SIZE); - - pdev->ep_in[CDC_IN_EP & 0xFU].is_used = 1U; - - /* Open EP OUT */ - USBD_LL_OpenEP(pdev, CDC_OUT_EP, USBD_EP_TYPE_BULK, - CDC_DATA_FS_OUT_PACKET_SIZE); - - pdev->ep_out[CDC_OUT_EP & 0xFU].is_used = 1U; - } - /* Open Command IN EP */ - USBD_LL_OpenEP(pdev, CDC_CMD_EP, USBD_EP_TYPE_INTR, CDC_CMD_PACKET_SIZE); - pdev->ep_in[CDC_CMD_EP & 0xFU].is_used = 1U; - - pdev->pClassData = USBD_malloc(sizeof(USBD_CDC_HandleTypeDef)); - - if (pdev->pClassData == NULL) { - ret = 1U; - } else { - hcdc = (USBD_CDC_HandleTypeDef *) pdev->pClassData; - - /* Init physical Interface components */ - ((USBD_CDC_ItfTypeDef *)pdev->pUserData)->Init(); - - /* Init Xfer states */ - hcdc->TxState = 0U; - hcdc->RxState = 0U; - - if (pdev->dev_speed == USBD_SPEED_HIGH) { - /* Prepare Out endpoint to receive next packet */ - USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, hcdc->RxBuffer, - CDC_DATA_HS_OUT_PACKET_SIZE); - } else { - /* Prepare Out endpoint to receive next packet */ - USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, hcdc->RxBuffer, - CDC_DATA_FS_OUT_PACKET_SIZE); - } - } - return ret; -} - -/** - * @brief USBD_CDC_Init - * DeInitialize the CDC layer - * @param pdev: device instance - * @param cfgidx: Configuration index - * @retval status - */ -static uint8_t USBD_CDC_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx) -{ - uint8_t ret = 0U; - UNUSED(cfgidx); - - /* Close EP IN */ - USBD_LL_CloseEP(pdev, CDC_IN_EP); - pdev->ep_in[CDC_IN_EP & 0xFU].is_used = 0U; - - /* Close EP OUT */ - USBD_LL_CloseEP(pdev, CDC_OUT_EP); - pdev->ep_out[CDC_OUT_EP & 0xFU].is_used = 0U; - - /* Close Command IN EP */ - USBD_LL_CloseEP(pdev, CDC_CMD_EP); - pdev->ep_in[CDC_CMD_EP & 0xFU].is_used = 0U; - - /* DeInit physical Interface components */ - if (pdev->pClassData != NULL) { - ((USBD_CDC_ItfTypeDef *)pdev->pUserData)->DeInit(); - USBD_free(pdev->pClassData); - pdev->pClassData = NULL; - } - - return ret; -} - -/** - * @brief USBD_CDC_Setup - * Handle the CDC specific requests - * @param pdev: instance - * @param req: usb requests - * @retval status - */ -static uint8_t USBD_CDC_Setup(USBD_HandleTypeDef *pdev, - USBD_SetupReqTypedef *req) -{ - USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *) pdev->pClassData; - uint8_t ifalt = 0U; - uint16_t status_info = 0U; - uint8_t ret = USBD_OK; - - switch (req->bmRequest & USB_REQ_TYPE_MASK) { - case USB_REQ_TYPE_CLASS : - if (req->wLength) { - if (req->bmRequest & 0x80U) { - ((USBD_CDC_ItfTypeDef *)pdev->pUserData)->Control(req->bRequest, - (uint8_t *)(void *)hcdc->data, - req->wLength); - - USBD_CtlSendData(pdev, (uint8_t *)(void *)hcdc->data, req->wLength); - } else { - hcdc->CmdOpCode = req->bRequest; - hcdc->CmdLength = (uint8_t)req->wLength; - - USBD_CtlPrepareRx(pdev, (uint8_t *)(void *)hcdc->data, req->wLength); - } - } else { - ((USBD_CDC_ItfTypeDef *)pdev->pUserData)->Control(req->bRequest, - (uint8_t *)(void *)req, 0U); - } - break; - - case USB_REQ_TYPE_STANDARD: - switch (req->bRequest) { - case USB_REQ_GET_STATUS: - if (pdev->dev_state == USBD_STATE_CONFIGURED) { - USBD_CtlSendData(pdev, (uint8_t *)(void *)&status_info, 2U); - } else { - USBD_CtlError(pdev, req); - ret = USBD_FAIL; - } - break; - - case USB_REQ_GET_INTERFACE: - if (pdev->dev_state == USBD_STATE_CONFIGURED) { - USBD_CtlSendData(pdev, &ifalt, 1U); - } else { - USBD_CtlError(pdev, req); - ret = USBD_FAIL; - } - break; - - case USB_REQ_SET_INTERFACE: - if (pdev->dev_state != USBD_STATE_CONFIGURED) { - USBD_CtlError(pdev, req); - ret = USBD_FAIL; - } - break; - - default: - USBD_CtlError(pdev, req); - ret = USBD_FAIL; - break; - } - break; - - default: - USBD_CtlError(pdev, req); - ret = USBD_FAIL; - break; - } - - return ret; -} - -/** - * @brief USBD_CDC_DataIn - * Data sent on non-control IN endpoint - * @param pdev: device instance - * @param epnum: endpoint number - * @retval status - */ -static uint8_t USBD_CDC_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum) -{ - USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassData; - PCD_HandleTypeDef *hpcd = pdev->pData; - USBD_CDC_ItfTypeDef *ctrl = (USBD_CDC_ItfTypeDef *)pdev->pUserData; - - if (pdev->pClassData != NULL) { - if ((pdev->ep_in[epnum].total_length > 0U) && ((pdev->ep_in[epnum].total_length % hpcd->IN_ep[epnum].maxpacket) == 0U)) { - /* Update the packet total length */ - pdev->ep_in[epnum].total_length = 0U; - - /* Send ZLP */ - USBD_LL_Transmit(pdev, epnum, NULL, 0U); - } else { - hcdc->TxState = 0U; - if (ctrl->Transferred) { - ctrl->Transferred(); - } - } - return USBD_OK; - } else { - return USBD_FAIL; - } -} - -/** - * @brief USBD_CDC_DataOut - * Data received on non-control Out endpoint - * @param pdev: device instance - * @param epnum: endpoint number - * @retval status - */ -static uint8_t USBD_CDC_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum) -{ - USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *) pdev->pClassData; - - /* Get the received data length */ - hcdc->RxLength = USBD_LL_GetRxDataSize(pdev, epnum); - - /* USB data will be immediately processed, this allow next USB traffic being - NAKed till the end of the application Xfer */ - if (pdev->pClassData != NULL) { - ((USBD_CDC_ItfTypeDef *)pdev->pUserData)->Receive(hcdc->RxBuffer, &hcdc->RxLength); - - return USBD_OK; - } else { - return USBD_FAIL; - } -} - -/** - * @brief USBD_CDC_EP0_RxReady - * Handle EP0 Rx Ready event - * @param pdev: device instance - * @retval status - */ -static uint8_t USBD_CDC_EP0_RxReady(USBD_HandleTypeDef *pdev) -{ - USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *) pdev->pClassData; - - if ((pdev->pUserData != NULL) && (hcdc->CmdOpCode != 0xFFU)) { - ((USBD_CDC_ItfTypeDef *)pdev->pUserData)->Control(hcdc->CmdOpCode, - (uint8_t *)(void *)hcdc->data, - (uint16_t)hcdc->CmdLength); - hcdc->CmdOpCode = 0xFFU; - - } - return USBD_OK; -} - -/** - * @brief USBD_CDC_GetFSCfgDesc - * Return configuration descriptor - * @param speed : current device speed - * @param length : pointer data length - * @retval pointer to descriptor buffer - */ -static uint8_t *USBD_CDC_GetFSCfgDesc(uint16_t *length) -{ - *length = sizeof(USBD_CDC_CfgFSDesc); - return USBD_CDC_CfgFSDesc; -} - -/** - * @brief USBD_CDC_GetHSCfgDesc - * Return configuration descriptor - * @param speed : current device speed - * @param length : pointer data length - * @retval pointer to descriptor buffer - */ -static uint8_t *USBD_CDC_GetHSCfgDesc(uint16_t *length) -{ - *length = sizeof(USBD_CDC_CfgHSDesc); - return USBD_CDC_CfgHSDesc; -} - -/** - * @brief USBD_CDC_GetCfgDesc - * Return configuration descriptor - * @param speed : current device speed - * @param length : pointer data length - * @retval pointer to descriptor buffer - */ -static uint8_t *USBD_CDC_GetOtherSpeedCfgDesc(uint16_t *length) -{ - *length = sizeof(USBD_CDC_OtherSpeedCfgDesc); - return USBD_CDC_OtherSpeedCfgDesc; -} - -/** -* @brief DeviceQualifierDescriptor -* return Device Qualifier descriptor -* @param length : pointer data length -* @retval pointer to descriptor buffer -*/ -uint8_t *USBD_CDC_GetDeviceQualifierDescriptor(uint16_t *length) -{ - *length = sizeof(USBD_CDC_DeviceQualifierDesc); - return USBD_CDC_DeviceQualifierDesc; -} - -/** -* @brief USBD_CDC_RegisterInterface - * @param pdev: device instance - * @param fops: CD Interface callback - * @retval status - */ -uint8_t USBD_CDC_RegisterInterface(USBD_HandleTypeDef *pdev, - USBD_CDC_ItfTypeDef *fops) -{ - uint8_t ret = USBD_FAIL; - - if (fops != NULL) { - pdev->pUserData = fops; - ret = USBD_OK; - } - - return ret; -} - -/** - * @brief USBD_CDC_SetTxBuffer - * @param pdev: device instance - * @param pbuff: Tx Buffer - * @retval status - */ -uint8_t USBD_CDC_SetTxBuffer(USBD_HandleTypeDef *pdev, - uint8_t *pbuff, - uint16_t length) -{ - USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *) pdev->pClassData; - - hcdc->TxBuffer = pbuff; - hcdc->TxLength = length; - - return USBD_OK; -} - - -/** - * @brief USBD_CDC_SetRxBuffer - * @param pdev: device instance - * @param pbuff: Rx Buffer - * @retval status - */ -uint8_t USBD_CDC_SetRxBuffer(USBD_HandleTypeDef *pdev, - uint8_t *pbuff) -{ - USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *) pdev->pClassData; - - hcdc->RxBuffer = pbuff; - - return USBD_OK; -} - -/** - * @brief USBD_CDC_TransmitPacket - * Transmit packet on IN endpoint - * @param pdev: device instance - * @retval status - */ -uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev) -{ - USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *) pdev->pClassData; - - if (pdev->pClassData != NULL) { - if (hcdc->TxState == 0U) { - /* Tx Transfer in progress */ - hcdc->TxState = 1U; - - /* Update the packet total length */ - pdev->ep_in[CDC_IN_EP & 0xFU].total_length = hcdc->TxLength; - - /* Transmit next packet */ - USBD_LL_Transmit(pdev, CDC_IN_EP, hcdc->TxBuffer, - (uint16_t)hcdc->TxLength); - - return USBD_OK; - } else { - return USBD_BUSY; - } - } else { - return USBD_FAIL; - } -} - - -/** - * @brief USBD_CDC_ReceivePacket - * prepare OUT Endpoint for reception - * @param pdev: device instance - * @retval status - */ -uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev) -{ - USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *) pdev->pClassData; - - /* Suspend or Resume USB Out process */ - if (pdev->pClassData != NULL) { - if (pdev->dev_speed == USBD_SPEED_HIGH) { - /* Prepare Out endpoint to receive next packet */ - USBD_LL_PrepareReceive(pdev, - CDC_OUT_EP, - hcdc->RxBuffer, - CDC_DATA_HS_OUT_PACKET_SIZE); - } else { - /* Prepare Out endpoint to receive next packet */ - USBD_LL_PrepareReceive(pdev, - CDC_OUT_EP, - hcdc->RxBuffer, - CDC_DATA_FS_OUT_PACKET_SIZE); - } - return USBD_OK; - } else { - return USBD_FAIL; - } -} - -#endif /* USBD_USE_CDC */ -#endif /* USBCON */ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_cdc.c + * @author MCD Application Team + * @brief This file provides the high layer firmware functions to manage the + * following functionalities of the USB CDC Class: + * - Initialization and Configuration of high and low layer + * - Enumeration as CDC Device (and enumeration for each implemented + * memory interface) + * - OUT/IN data transfer + * - Command IN transfer (class requests management) + * - Error management + * + * @verbatim + * + * =================================================================== + * CDC Class Driver Description + * =================================================================== + * This driver manages the "Universal Serial Bus Class Definitions + * for Communications Devices Revision 1.2 November 16, 2007" and the + * sub-protocol specification of "Universal Serial Bus Communications + * Class Subclass Specification for PSTN Devices Revision 1.2 + * February 9, 2007" This driver implements the following aspects of + * the specification: + * - Device descriptor management + * - Configuration descriptor management + * - Enumeration as CDC device with 2 data endpoints (IN and OUT) + * and 1 command endpoint (IN) + * - Requests management (as described in section 6.2 in + * specification) + * - Abstract Control Model compliant + * - Union Functional collection (using 1 IN endpoint for control) + * - Data interface class + * + * These aspects may be enriched or modified for a specific user + * application. + * + * This driver doesn't implement the following aspects of the + * specification (but it is possible to manage these features with + * some modifications on this driver): + * - Any class-specific aspect relative to communication classes + * should be managed by user application. + * - All communication classes other than PSTN are not managed + * + * @endverbatim + * + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +#ifdef USBCON +#ifdef USBD_USE_CDC + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_cdc.h" +#include "usbd_ctlreq.h" + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + +/** @defgroup USBD_CDC + * @brief usbd core module + * @{ + */ + +/** @defgroup USBD_CDC_Private_TypesDefinitions + * @{ + */ +/** + * @} + */ + +/** @defgroup USBD_CDC_Private_Defines + * @{ + */ +/** + * @} + */ + +/** @defgroup USBD_CDC_Private_Macros + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_CDC_Private_FunctionPrototypes + * @{ + */ + +static uint8_t USBD_CDC_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx); + +static uint8_t USBD_CDC_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx); + +static uint8_t USBD_CDC_Setup(USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req); + +static uint8_t USBD_CDC_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum); + +static uint8_t USBD_CDC_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum); + +static uint8_t USBD_CDC_EP0_RxReady(USBD_HandleTypeDef *pdev); + +static uint8_t *USBD_CDC_GetFSCfgDesc(uint16_t *length); + +static uint8_t *USBD_CDC_GetHSCfgDesc(uint16_t *length); + +static uint8_t *USBD_CDC_GetOtherSpeedCfgDesc(uint16_t *length); + +static uint8_t *USBD_CDC_GetOtherSpeedCfgDesc(uint16_t *length); + +uint8_t *USBD_CDC_GetDeviceQualifierDescriptor(uint16_t *length); + +/* USB Standard Device Descriptor */ +__ALIGN_BEGIN static uint8_t + USBD_CDC_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END = { + USB_LEN_DEV_QUALIFIER_DESC, + USB_DESC_TYPE_DEVICE_QUALIFIER, + 0x00, + 0x02, + 0x00, + 0x00, + 0x00, + 0x40, + 0x01, + 0x00, +}; + +/** + * @} + */ + +/** @defgroup USBD_CDC_Private_Variables + * @{ + */ + +/* CDC interface class callbacks structure */ +USBD_ClassTypeDef USBD_CDC = { + USBD_CDC_Init, + USBD_CDC_DeInit, + USBD_CDC_Setup, + NULL, /* EP0_TxSent, */ + USBD_CDC_EP0_RxReady, + USBD_CDC_DataIn, + USBD_CDC_DataOut, + NULL, + NULL, + NULL, + USBD_CDC_GetHSCfgDesc, + USBD_CDC_GetFSCfgDesc, + USBD_CDC_GetOtherSpeedCfgDesc, + USBD_CDC_GetDeviceQualifierDescriptor, +}; + +/* USB CDC device Configuration Descriptor */ +__ALIGN_BEGIN uint8_t + USBD_CDC_CfgHSDesc[USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END = { + /*Configuration Descriptor*/ + 0x09, /* bLength: Configuration Descriptor size */ + USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ + USB_CDC_CONFIG_DESC_SIZ, /* wTotalLength:no of returned bytes */ + 0x00, 0x02, /* bNumInterfaces: 2 interface */ + 0x01, /* bConfigurationValue: Configuration value */ + 0x00, /* iConfiguration: Index of string descriptor describing the + configuration */ + 0xC0, /* bmAttributes: self powered */ + 0x32, /* MaxPower 0 mA */ + + /*--------------------------------------------------------------------------*/ + + /*Interface Descriptor */ + 0x09, /* bLength: Interface Descriptor size */ + USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */ + /* Interface descriptor type */ + 0x00, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x01, /* bNumEndpoints: One endpoints used */ + 0x02, /* bInterfaceClass: Communication Interface Class */ + 0x02, /* bInterfaceSubClass: Abstract Control Model */ + 0x01, /* bInterfaceProtocol: Common AT commands */ + 0x00, /* iInterface: */ + + /*Header Functional Descriptor*/ + 0x05, /* bLength: Endpoint Descriptor size */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x00, /* bDescriptorSubtype: Header Func Desc */ + 0x10, /* bcdCDC: spec release number */ + 0x01, + + /*Call Management Functional Descriptor*/ + 0x05, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x01, /* bDescriptorSubtype: Call Management Func Desc */ + 0x00, /* bmCapabilities: D0+D1 */ + 0x01, /* bDataInterface: 1 */ + + /*ACM Functional Descriptor*/ + 0x04, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x02, /* bDescriptorSubtype: Abstract Control Management desc */ + 0x02, /* bmCapabilities */ + + /*Union Functional Descriptor*/ + 0x05, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x06, /* bDescriptorSubtype: Union func desc */ + 0x00, /* bMasterInterface: Communication class interface */ + 0x01, /* bSlaveInterface0: Data Class Interface */ + + /*Endpoint 2 Descriptor*/ + 0x07, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ + CDC_CMD_EP, /* bEndpointAddress */ + 0x03, /* bmAttributes: Interrupt */ + LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize: */ + HIBYTE(CDC_CMD_PACKET_SIZE), + CDC_HS_BINTERVAL, /* bInterval: */ + /*--------------------------------------------------------------------------*/ + + /*Data class interface descriptor*/ + 0x09, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_INTERFACE, /* bDescriptorType: */ + 0x01, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x02, /* bNumEndpoints: Two endpoints used */ + 0x0A, /* bInterfaceClass: CDC */ + 0x00, /* bInterfaceSubClass: */ + 0x00, /* bInterfaceProtocol: */ + 0x00, /* iInterface: */ + + /*Endpoint OUT Descriptor*/ + 0x07, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ + CDC_OUT_EP, /* bEndpointAddress */ + 0x02, /* bmAttributes: Bulk */ + LOBYTE(CDC_DATA_HS_MAX_PACKET_SIZE), /* wMaxPacketSize: */ + HIBYTE(CDC_DATA_HS_MAX_PACKET_SIZE), + 0x00, /* bInterval: ignore for Bulk transfer */ + + /*Endpoint IN Descriptor*/ + 0x07, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ + CDC_IN_EP, /* bEndpointAddress */ + 0x02, /* bmAttributes: Bulk */ + LOBYTE(CDC_DATA_HS_MAX_PACKET_SIZE), /* wMaxPacketSize: */ + HIBYTE(CDC_DATA_HS_MAX_PACKET_SIZE), + 0x00 /* bInterval: ignore for Bulk transfer */ +}; + +/* USB CDC device Configuration Descriptor */ +__ALIGN_BEGIN uint8_t + USBD_CDC_CfgFSDesc[USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END = { + /*Configuration Descriptor*/ + 0x09, /* bLength: Configuration Descriptor size */ + USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ + USB_CDC_CONFIG_DESC_SIZ, /* wTotalLength:no of returned bytes */ + 0x00, 0x02, /* bNumInterfaces: 2 interface */ + 0x01, /* bConfigurationValue: Configuration value */ + 0x00, /* iConfiguration: Index of string descriptor describing the + configuration */ + 0xC0, /* bmAttributes: self powered */ + 0x32, /* MaxPower 0 mA */ + + /*--------------------------------------------------------------------------*/ + + /*Interface Descriptor */ + 0x09, /* bLength: Interface Descriptor size */ + USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */ + /* Interface descriptor type */ + 0x00, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x01, /* bNumEndpoints: One endpoints used */ + 0x02, /* bInterfaceClass: Communication Interface Class */ + 0x02, /* bInterfaceSubClass: Abstract Control Model */ + 0x01, /* bInterfaceProtocol: Common AT commands */ + 0x00, /* iInterface: */ + + /*Header Functional Descriptor*/ + 0x05, /* bLength: Endpoint Descriptor size */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x00, /* bDescriptorSubtype: Header Func Desc */ + 0x10, /* bcdCDC: spec release number */ + 0x01, + + /*Call Management Functional Descriptor*/ + 0x05, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x01, /* bDescriptorSubtype: Call Management Func Desc */ + 0x00, /* bmCapabilities: D0+D1 */ + 0x01, /* bDataInterface: 1 */ + + /*ACM Functional Descriptor*/ + 0x04, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x02, /* bDescriptorSubtype: Abstract Control Management desc */ + 0x02, /* bmCapabilities */ + + /*Union Functional Descriptor*/ + 0x05, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x06, /* bDescriptorSubtype: Union func desc */ + 0x00, /* bMasterInterface: Communication class interface */ + 0x01, /* bSlaveInterface0: Data Class Interface */ + + /*Endpoint 2 Descriptor*/ + 0x07, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ + CDC_CMD_EP, /* bEndpointAddress */ + 0x03, /* bmAttributes: Interrupt */ + LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize: */ + HIBYTE(CDC_CMD_PACKET_SIZE), + CDC_FS_BINTERVAL, /* bInterval: */ + /*--------------------------------------------------------------------------*/ + + /*Data class interface descriptor*/ + 0x09, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_INTERFACE, /* bDescriptorType: */ + 0x01, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x02, /* bNumEndpoints: Two endpoints used */ + 0x0A, /* bInterfaceClass: CDC */ + 0x00, /* bInterfaceSubClass: */ + 0x00, /* bInterfaceProtocol: */ + 0x00, /* iInterface: */ + + /*Endpoint OUT Descriptor*/ + 0x07, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ + CDC_OUT_EP, /* bEndpointAddress */ + 0x02, /* bmAttributes: Bulk */ + LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), /* wMaxPacketSize: */ + HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), + 0x00, /* bInterval: ignore for Bulk transfer */ + + /*Endpoint IN Descriptor*/ + 0x07, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ + CDC_IN_EP, /* bEndpointAddress */ + 0x02, /* bmAttributes: Bulk */ + LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), /* wMaxPacketSize: */ + HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), + 0x00 /* bInterval: ignore for Bulk transfer */ +}; + +__ALIGN_BEGIN uint8_t + USBD_CDC_OtherSpeedCfgDesc[USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END = { + 0x09, /* bLength: Configuation Descriptor size */ + USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION, USB_CDC_CONFIG_DESC_SIZ, 0x00, + 0x02, /* bNumInterfaces: 2 interfaces */ + 0x01, /* bConfigurationValue: */ + 0x04, /* iConfiguration: */ + 0xC0, /* bmAttributes: */ + 0x32, /* MaxPower 100 mA */ + + /*Interface Descriptor */ + 0x09, /* bLength: Interface Descriptor size */ + USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */ + /* Interface descriptor type */ + 0x00, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x01, /* bNumEndpoints: One endpoints used */ + 0x02, /* bInterfaceClass: Communication Interface Class */ + 0x02, /* bInterfaceSubClass: Abstract Control Model */ + 0x01, /* bInterfaceProtocol: Common AT commands */ + 0x00, /* iInterface: */ + + /*Header Functional Descriptor*/ + 0x05, /* bLength: Endpoint Descriptor size */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x00, /* bDescriptorSubtype: Header Func Desc */ + 0x10, /* bcdCDC: spec release number */ + 0x01, + + /*Call Management Functional Descriptor*/ + 0x05, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x01, /* bDescriptorSubtype: Call Management Func Desc */ + 0x00, /* bmCapabilities: D0+D1 */ + 0x01, /* bDataInterface: 1 */ + + /*ACM Functional Descriptor*/ + 0x04, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x02, /* bDescriptorSubtype: Abstract Control Management desc */ + 0x02, /* bmCapabilities */ + + /*Union Functional Descriptor*/ + 0x05, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x06, /* bDescriptorSubtype: Union func desc */ + 0x00, /* bMasterInterface: Communication class interface */ + 0x01, /* bSlaveInterface0: Data Class Interface */ + + /*Endpoint 2 Descriptor*/ + 0x07, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ + CDC_CMD_EP, /* bEndpointAddress */ + 0x03, /* bmAttributes: Interrupt */ + LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize: */ + HIBYTE(CDC_CMD_PACKET_SIZE), + CDC_FS_BINTERVAL, /* bInterval: */ + + /*--------------------------------------------------------------------------*/ + + /*Data class interface descriptor*/ + 0x09, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_INTERFACE, /* bDescriptorType: */ + 0x01, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x02, /* bNumEndpoints: Two endpoints used */ + 0x0A, /* bInterfaceClass: CDC */ + 0x00, /* bInterfaceSubClass: */ + 0x00, /* bInterfaceProtocol: */ + 0x00, /* iInterface: */ + + /*Endpoint OUT Descriptor*/ + 0x07, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ + CDC_OUT_EP, /* bEndpointAddress */ + 0x02, /* bmAttributes: Bulk */ + 0x40, /* wMaxPacketSize: */ + 0x00, 0x00, /* bInterval: ignore for Bulk transfer */ + + /*Endpoint IN Descriptor*/ + 0x07, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ + CDC_IN_EP, /* bEndpointAddress */ + 0x02, /* bmAttributes: Bulk */ + 0x40, /* wMaxPacketSize: */ + 0x00, 0x00 /* bInterval */ +}; + +/** + * @} + */ + +/** @defgroup USBD_CDC_Private_Functions + * @{ + */ + +/** + * @brief USBD_CDC_Init + * Initialize the CDC interface + * @param pdev: device instance + * @param cfgidx: Configuration index + * @retval status + */ +static uint8_t USBD_CDC_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx) +{ + uint8_t ret = 0U; + UNUSED(cfgidx); + USBD_CDC_HandleTypeDef *hcdc; + + if (pdev->dev_speed == USBD_SPEED_HIGH) + { + /* Open EP IN */ + USBD_LL_OpenEP(pdev, CDC_IN_EP, USBD_EP_TYPE_BULK, + CDC_DATA_HS_IN_PACKET_SIZE); + + pdev->ep_in[CDC_IN_EP & 0xFU].is_used = 1U; + + /* Open EP OUT */ + USBD_LL_OpenEP(pdev, CDC_OUT_EP, USBD_EP_TYPE_BULK, + CDC_DATA_HS_OUT_PACKET_SIZE); + + pdev->ep_out[CDC_OUT_EP & 0xFU].is_used = 1U; + } + else + { + /* Open EP IN */ + USBD_LL_OpenEP(pdev, CDC_IN_EP, USBD_EP_TYPE_BULK, + CDC_DATA_FS_IN_PACKET_SIZE); + + pdev->ep_in[CDC_IN_EP & 0xFU].is_used = 1U; + + /* Open EP OUT */ + USBD_LL_OpenEP(pdev, CDC_OUT_EP, USBD_EP_TYPE_BULK, + CDC_DATA_FS_OUT_PACKET_SIZE); + + pdev->ep_out[CDC_OUT_EP & 0xFU].is_used = 1U; + } + /* Open Command IN EP */ + USBD_LL_OpenEP(pdev, CDC_CMD_EP, USBD_EP_TYPE_INTR, CDC_CMD_PACKET_SIZE); + pdev->ep_in[CDC_CMD_EP & 0xFU].is_used = 1U; + + pdev->pClassData = USBD_malloc(sizeof(USBD_CDC_HandleTypeDef)); + + if (pdev->pClassData == NULL) + { + ret = 1U; + } + else + { + hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassData; + + /* Init physical Interface components */ + ((USBD_CDC_ItfTypeDef *)pdev->pUserData)->Init(); + + /* Init Xfer states */ + hcdc->TxState = 0U; + hcdc->RxState = 0U; + + if (pdev->dev_speed == USBD_SPEED_HIGH) + { + /* Prepare Out endpoint to receive next packet */ + USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, hcdc->RxBuffer, + CDC_DATA_HS_OUT_PACKET_SIZE); + } + else + { + /* Prepare Out endpoint to receive next packet */ + USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, hcdc->RxBuffer, + CDC_DATA_FS_OUT_PACKET_SIZE); + } + } + return ret; +} + +/** + * @brief USBD_CDC_Init + * DeInitialize the CDC layer + * @param pdev: device instance + * @param cfgidx: Configuration index + * @retval status + */ +static uint8_t USBD_CDC_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx) +{ + uint8_t ret = 0U; + UNUSED(cfgidx); + + /* Close EP IN */ + USBD_LL_CloseEP(pdev, CDC_IN_EP); + pdev->ep_in[CDC_IN_EP & 0xFU].is_used = 0U; + + /* Close EP OUT */ + USBD_LL_CloseEP(pdev, CDC_OUT_EP); + pdev->ep_out[CDC_OUT_EP & 0xFU].is_used = 0U; + + /* Close Command IN EP */ + USBD_LL_CloseEP(pdev, CDC_CMD_EP); + pdev->ep_in[CDC_CMD_EP & 0xFU].is_used = 0U; + + /* DeInit physical Interface components */ + if (pdev->pClassData != NULL) + { + ((USBD_CDC_ItfTypeDef *)pdev->pUserData)->DeInit(); + USBD_free(pdev->pClassData); + pdev->pClassData = NULL; + } + + return ret; +} + +/** + * @brief USBD_CDC_Setup + * Handle the CDC specific requests + * @param pdev: instance + * @param req: usb requests + * @retval status + */ +static uint8_t USBD_CDC_Setup(USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req) +{ + USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassData; + uint8_t ifalt = 0U; + uint16_t status_info = 0U; + uint8_t ret = USBD_OK; + + switch (req->bmRequest & USB_REQ_TYPE_MASK) + { + case USB_REQ_TYPE_CLASS: + if (req->wLength) + { + if (req->bmRequest & 0x80U) + { + ((USBD_CDC_ItfTypeDef *)pdev->pUserData) + ->Control(req->bRequest, (uint8_t *)(void *)hcdc->data, + req->wLength); + + USBD_CtlSendData(pdev, (uint8_t *)(void *)hcdc->data, req->wLength); + } + else + { + hcdc->CmdOpCode = req->bRequest; + hcdc->CmdLength = (uint8_t)req->wLength; + + USBD_CtlPrepareRx(pdev, (uint8_t *)(void *)hcdc->data, req->wLength); + } + } + else + { + ((USBD_CDC_ItfTypeDef *)pdev->pUserData) + ->Control(req->bRequest, (uint8_t *)(void *)req, 0U); + } + break; + + case USB_REQ_TYPE_STANDARD: + switch (req->bRequest) + { + case USB_REQ_GET_STATUS: + if (pdev->dev_state == USBD_STATE_CONFIGURED) + { + USBD_CtlSendData(pdev, (uint8_t *)(void *)&status_info, 2U); + } + else + { + USBD_CtlError(pdev, req); + ret = USBD_FAIL; + } + break; + + case USB_REQ_GET_INTERFACE: + if (pdev->dev_state == USBD_STATE_CONFIGURED) + { + USBD_CtlSendData(pdev, &ifalt, 1U); + } + else + { + USBD_CtlError(pdev, req); + ret = USBD_FAIL; + } + break; + + case USB_REQ_SET_INTERFACE: + if (pdev->dev_state != USBD_STATE_CONFIGURED) + { + USBD_CtlError(pdev, req); + ret = USBD_FAIL; + } + break; + + default: + USBD_CtlError(pdev, req); + ret = USBD_FAIL; + break; + } + break; + + default: + USBD_CtlError(pdev, req); + ret = USBD_FAIL; + break; + } + + return ret; +} + +/** + * @brief USBD_CDC_DataIn + * Data sent on non-control IN endpoint + * @param pdev: device instance + * @param epnum: endpoint number + * @retval status + */ +static uint8_t USBD_CDC_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum) +{ + USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassData; + PCD_HandleTypeDef *hpcd = pdev->pData; + USBD_CDC_ItfTypeDef *ctrl = (USBD_CDC_ItfTypeDef *)pdev->pUserData; + + if (pdev->pClassData != NULL) + { + if ((pdev->ep_in[epnum].total_length > 0U) && + ((pdev->ep_in[epnum].total_length % hpcd->IN_ep[epnum].maxpacket) == + 0U)) + { + /* Update the packet total length */ + pdev->ep_in[epnum].total_length = 0U; + + /* Send ZLP */ + USBD_LL_Transmit(pdev, epnum, NULL, 0U); + } + else + { + hcdc->TxState = 0U; + if (ctrl->Transferred) + { + ctrl->Transferred(); + } + } + return USBD_OK; + } + else + { + return USBD_FAIL; + } +} + +/** + * @brief USBD_CDC_DataOut + * Data received on non-control Out endpoint + * @param pdev: device instance + * @param epnum: endpoint number + * @retval status + */ +static uint8_t USBD_CDC_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum) +{ + USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassData; + + /* Get the received data length */ + hcdc->RxLength = USBD_LL_GetRxDataSize(pdev, epnum); + + /* USB data will be immediately processed, this allow next USB traffic being + NAKed till the end of the application Xfer */ + if (pdev->pClassData != NULL) + { + ((USBD_CDC_ItfTypeDef *)pdev->pUserData) + ->Receive(hcdc->RxBuffer, &hcdc->RxLength); + + return USBD_OK; + } + else + { + return USBD_FAIL; + } +} + +/** + * @brief USBD_CDC_EP0_RxReady + * Handle EP0 Rx Ready event + * @param pdev: device instance + * @retval status + */ +static uint8_t USBD_CDC_EP0_RxReady(USBD_HandleTypeDef *pdev) +{ + USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassData; + + if ((pdev->pUserData != NULL) && (hcdc->CmdOpCode != 0xFFU)) + { + ((USBD_CDC_ItfTypeDef *)pdev->pUserData) + ->Control(hcdc->CmdOpCode, (uint8_t *)(void *)hcdc->data, + (uint16_t)hcdc->CmdLength); + hcdc->CmdOpCode = 0xFFU; + } + return USBD_OK; +} + +/** + * @brief USBD_CDC_GetFSCfgDesc + * Return configuration descriptor + * @param speed : current device speed + * @param length : pointer data length + * @retval pointer to descriptor buffer + */ +static uint8_t *USBD_CDC_GetFSCfgDesc(uint16_t *length) +{ + *length = sizeof(USBD_CDC_CfgFSDesc); + return USBD_CDC_CfgFSDesc; +} + +/** + * @brief USBD_CDC_GetHSCfgDesc + * Return configuration descriptor + * @param speed : current device speed + * @param length : pointer data length + * @retval pointer to descriptor buffer + */ +static uint8_t *USBD_CDC_GetHSCfgDesc(uint16_t *length) +{ + *length = sizeof(USBD_CDC_CfgHSDesc); + return USBD_CDC_CfgHSDesc; +} + +/** + * @brief USBD_CDC_GetCfgDesc + * Return configuration descriptor + * @param speed : current device speed + * @param length : pointer data length + * @retval pointer to descriptor buffer + */ +static uint8_t *USBD_CDC_GetOtherSpeedCfgDesc(uint16_t *length) +{ + *length = sizeof(USBD_CDC_OtherSpeedCfgDesc); + return USBD_CDC_OtherSpeedCfgDesc; +} + +/** + * @brief DeviceQualifierDescriptor + * return Device Qualifier descriptor + * @param length : pointer data length + * @retval pointer to descriptor buffer + */ +uint8_t *USBD_CDC_GetDeviceQualifierDescriptor(uint16_t *length) +{ + *length = sizeof(USBD_CDC_DeviceQualifierDesc); + return USBD_CDC_DeviceQualifierDesc; +} + +/** + * @brief USBD_CDC_RegisterInterface + * @param pdev: device instance + * @param fops: CD Interface callback + * @retval status + */ +uint8_t USBD_CDC_RegisterInterface(USBD_HandleTypeDef *pdev, + USBD_CDC_ItfTypeDef *fops) +{ + uint8_t ret = USBD_FAIL; + + if (fops != NULL) + { + pdev->pUserData = fops; + ret = USBD_OK; + } + + return ret; +} + +/** + * @brief USBD_CDC_SetTxBuffer + * @param pdev: device instance + * @param pbuff: Tx Buffer + * @retval status + */ +uint8_t USBD_CDC_SetTxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff, + uint16_t length) +{ + USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassData; + + hcdc->TxBuffer = pbuff; + hcdc->TxLength = length; + + return USBD_OK; +} + +/** + * @brief USBD_CDC_SetRxBuffer + * @param pdev: device instance + * @param pbuff: Rx Buffer + * @retval status + */ +uint8_t USBD_CDC_SetRxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff) +{ + USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassData; + + hcdc->RxBuffer = pbuff; + + return USBD_OK; +} + +/** + * @brief USBD_CDC_TransmitPacket + * Transmit packet on IN endpoint + * @param pdev: device instance + * @retval status + */ +uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev) +{ + USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassData; + + if (pdev->pClassData != NULL) + { + if (hcdc->TxState == 0U) + { + /* Tx Transfer in progress */ + hcdc->TxState = 1U; + + /* Update the packet total length */ + pdev->ep_in[CDC_IN_EP & 0xFU].total_length = hcdc->TxLength; + + /* Transmit next packet */ + USBD_LL_Transmit(pdev, CDC_IN_EP, hcdc->TxBuffer, + (uint16_t)hcdc->TxLength); + + return USBD_OK; + } + else + { + return USBD_BUSY; + } + } + else + { + return USBD_FAIL; + } +} + +/** + * @brief USBD_CDC_ReceivePacket + * prepare OUT Endpoint for reception + * @param pdev: device instance + * @retval status + */ +uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev) +{ + USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassData; + + /* Suspend or Resume USB Out process */ + if (pdev->pClassData != NULL) + { + if (pdev->dev_speed == USBD_SPEED_HIGH) + { + /* Prepare Out endpoint to receive next packet */ + USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, hcdc->RxBuffer, + CDC_DATA_HS_OUT_PACKET_SIZE); + } + else + { + /* Prepare Out endpoint to receive next packet */ + USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, hcdc->RxBuffer, + CDC_DATA_FS_OUT_PACKET_SIZE); + } + return USBD_OK; + } + else + { + return USBD_FAIL; + } +} + +#endif /* USBD_USE_CDC */ +#endif /* USBCON */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/cores/arduino/stm32/usb/cdc/usbd_cdc.h b/cores/arduino/stm32/usb/cdc/usbd_cdc.h index df518b80ff..65072d11f0 100644 --- a/cores/arduino/stm32/usb/cdc/usbd_cdc.h +++ b/cores/arduino/stm32/usb/cdc/usbd_cdc.h @@ -1,177 +1,177 @@ -/** - ****************************************************************************** - * @file usbd_cdc.h - * @author MCD Application Team - * @brief header file for the usbd_cdc.c file. - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_CDC_H -#define __USB_CDC_H - -#ifdef __cplusplus -extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_ioreq.h" - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - -/** @defgroup usbd_cdc - * @brief This file is the Header file for usbd_cdc.c - * @{ - */ - - -/** @defgroup usbd_cdc_Exported_Defines - * @{ - */ -#define CDC_IN_EP 0x81U /* EP1 for data IN */ -#define CDC_OUT_EP 0x01U /* EP1 for data OUT */ -#define CDC_CMD_EP 0x82U /* EP2 for CDC commands */ - -#ifndef CDC_HS_BINTERVAL -#define CDC_HS_BINTERVAL 0x10U -#endif /* CDC_HS_BINTERVAL */ - -#ifndef CDC_FS_BINTERVAL -#define CDC_FS_BINTERVAL 0x10U -#endif /* CDC_FS_BINTERVAL */ - -/* CDC Endpoints parameters: you can fine tune these values depending on the needed baudrates and performance. */ -#define CDC_DATA_HS_MAX_PACKET_SIZE 512U /* Endpoint IN & OUT Packet size */ -#define CDC_DATA_FS_MAX_PACKET_SIZE 64U /* Endpoint IN & OUT Packet size */ -#define CDC_CMD_PACKET_SIZE 8U /* Control Endpoint Packet size */ - -#define USB_CDC_CONFIG_DESC_SIZ 67U -#define CDC_DATA_HS_IN_PACKET_SIZE CDC_DATA_HS_MAX_PACKET_SIZE -#define CDC_DATA_HS_OUT_PACKET_SIZE CDC_DATA_HS_MAX_PACKET_SIZE - -#define CDC_DATA_FS_IN_PACKET_SIZE CDC_DATA_FS_MAX_PACKET_SIZE -#define CDC_DATA_FS_OUT_PACKET_SIZE CDC_DATA_FS_MAX_PACKET_SIZE - -/*---------------------------------------------------------------------*/ -/* CDC definitions */ -/*---------------------------------------------------------------------*/ -#define CDC_SEND_ENCAPSULATED_COMMAND 0x00U -#define CDC_GET_ENCAPSULATED_RESPONSE 0x01U -#define CDC_SET_COMM_FEATURE 0x02U -#define CDC_GET_COMM_FEATURE 0x03U -#define CDC_CLEAR_COMM_FEATURE 0x04U -#define CDC_SET_LINE_CODING 0x20U -#define CDC_GET_LINE_CODING 0x21U -#define CDC_SET_CONTROL_LINE_STATE 0x22U -#define CDC_SEND_BREAK 0x23U - -/** - * @} - */ - - -/** @defgroup USBD_CORE_Exported_TypesDefinitions - * @{ - */ - -/** - * @} - */ -typedef struct { - uint32_t bitrate; - uint8_t format; - uint8_t paritytype; - uint8_t datatype; -} USBD_CDC_LineCodingTypeDef; - -typedef struct _USBD_CDC_Itf { - int8_t (* Init)(void); - int8_t (* DeInit)(void); - int8_t (* Control)(uint8_t cmd, uint8_t *pbuf, uint16_t length); - int8_t (* Receive)(uint8_t *Buf, uint32_t *Len); - int8_t (* Transferred)(void); - -} USBD_CDC_ItfTypeDef; - - -typedef struct { - uint32_t data[CDC_DATA_HS_MAX_PACKET_SIZE / 4U]; /* Force 32bits alignment */ - uint8_t CmdOpCode; - uint8_t CmdLength; - uint8_t *RxBuffer; - uint8_t *TxBuffer; - uint32_t RxLength; - uint32_t TxLength; - - __IO uint32_t TxState; - __IO uint32_t RxState; -} -USBD_CDC_HandleTypeDef; - - - -/** @defgroup USBD_CORE_Exported_Macros - * @{ - */ - -/** - * @} - */ - -/** @defgroup USBD_CORE_Exported_Variables - * @{ - */ - -extern USBD_ClassTypeDef USBD_CDC; -#define USBD_CDC_CLASS &USBD_CDC -/** - * @} - */ - -/** @defgroup USB_CORE_Exported_Functions - * @{ - */ -uint8_t USBD_CDC_RegisterInterface(USBD_HandleTypeDef *pdev, - USBD_CDC_ItfTypeDef *fops); - -uint8_t USBD_CDC_SetTxBuffer(USBD_HandleTypeDef *pdev, - uint8_t *pbuff, - uint16_t length); - -uint8_t USBD_CDC_SetRxBuffer(USBD_HandleTypeDef *pdev, - uint8_t *pbuff); - -uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev); - -uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev); -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USB_CDC_H */ -/** - * @} - */ - -/** - * @} - */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_cdc.h + * @author MCD Application Team + * @brief header file for the usbd_cdc.c file. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USB_CDC_H +#define __USB_CDC_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_ioreq.h" + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + +/** @defgroup usbd_cdc + * @brief This file is the Header file for usbd_cdc.c + * @{ + */ + +/** @defgroup usbd_cdc_Exported_Defines + * @{ + */ + +#define CDC_IN_EP 0x81U /* EP1 for data IN */ +#define CDC_OUT_EP 0x01U /* EP1 for data OUT */ +#define CDC_CMD_EP 0x82U /* EP2 for CDC commands */ + +#ifndef CDC_HS_BINTERVAL +#define CDC_HS_BINTERVAL 0x10U +#endif /* CDC_HS_BINTERVAL */ + +#ifndef CDC_FS_BINTERVAL +#define CDC_FS_BINTERVAL 0x10U +#endif /* CDC_FS_BINTERVAL */ + +/* CDC Endpoints parameters: you can fine tune these values depending on the + * needed baudrates and performance. */ +#define CDC_DATA_HS_MAX_PACKET_SIZE 512U /* Endpoint IN & OUT Packet size */ +#define CDC_DATA_FS_MAX_PACKET_SIZE 64U /* Endpoint IN & OUT Packet size */ +#define CDC_CMD_PACKET_SIZE 8U /* Control Endpoint Packet size */ + +#define USB_CDC_CONFIG_DESC_SIZ 67U +#define CDC_DATA_HS_IN_PACKET_SIZE CDC_DATA_HS_MAX_PACKET_SIZE +#define CDC_DATA_HS_OUT_PACKET_SIZE CDC_DATA_HS_MAX_PACKET_SIZE + +#define CDC_DATA_FS_IN_PACKET_SIZE CDC_DATA_FS_MAX_PACKET_SIZE +#define CDC_DATA_FS_OUT_PACKET_SIZE CDC_DATA_FS_MAX_PACKET_SIZE + +/*---------------------------------------------------------------------*/ +/* CDC definitions */ +/*---------------------------------------------------------------------*/ +#define CDC_SEND_ENCAPSULATED_COMMAND 0x00U +#define CDC_GET_ENCAPSULATED_RESPONSE 0x01U +#define CDC_SET_COMM_FEATURE 0x02U +#define CDC_GET_COMM_FEATURE 0x03U +#define CDC_CLEAR_COMM_FEATURE 0x04U +#define CDC_SET_LINE_CODING 0x20U +#define CDC_GET_LINE_CODING 0x21U +#define CDC_SET_CONTROL_LINE_STATE 0x22U +#define CDC_SEND_BREAK 0x23U + +/** + * @} + */ + +/** @defgroup USBD_CORE_Exported_TypesDefinitions + * @{ + */ + +/** + * @} + */ +typedef struct +{ + uint32_t bitrate; + uint8_t format; + uint8_t paritytype; + uint8_t datatype; +} USBD_CDC_LineCodingTypeDef; + +typedef struct _USBD_CDC_Itf +{ + int8_t (*Init)(void); + int8_t (*DeInit)(void); + int8_t (*Control)(uint8_t cmd, uint8_t *pbuf, uint16_t length); + int8_t (*Receive)(uint8_t *Buf, uint32_t *Len); + int8_t (*Transferred)(void); + +} USBD_CDC_ItfTypeDef; + +typedef struct +{ + uint32_t data[CDC_DATA_HS_MAX_PACKET_SIZE / 4U]; /* Force 32bits alignment */ + uint8_t CmdOpCode; + uint8_t CmdLength; + uint8_t *RxBuffer; + uint8_t *TxBuffer; + uint32_t RxLength; + uint32_t TxLength; + + __IO uint32_t TxState; + __IO uint32_t RxState; +} USBD_CDC_HandleTypeDef; + +/** @defgroup USBD_CORE_Exported_Macros + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_CORE_Exported_Variables + * @{ + */ + +extern USBD_ClassTypeDef USBD_CDC; +#define USBD_CDC_CLASS &USBD_CDC + +/** + * @} + */ + +/** @defgroup USB_CORE_Exported_Functions + * @{ + */ + +uint8_t USBD_CDC_RegisterInterface(USBD_HandleTypeDef *pdev, + USBD_CDC_ItfTypeDef *fops); + +uint8_t USBD_CDC_SetTxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff, + uint16_t length); + +uint8_t USBD_CDC_SetRxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff); + +uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev); + +uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev); + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USB_CDC_H */ +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/cores/arduino/stm32/usb/cdc/usbd_cdc_if.c b/cores/arduino/stm32/usb/cdc/usbd_cdc_if.c index f55b74aea2..a9a5b6de05 100644 --- a/cores/arduino/stm32/usb/cdc/usbd_cdc_if.c +++ b/cores/arduino/stm32/usb/cdc/usbd_cdc_if.c @@ -1,294 +1,302 @@ -/** - ****************************************************************************** - * @file usbd_cdc_if.c - * @author MCD Application Team - * @brief Generic media access Layer. - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -#ifdef USBCON -#ifdef USBD_USE_CDC - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_desc.h" -#include "usbd_cdc_if.h" - -#ifdef USE_USB_HS -#define CDC_MAX_PACKET_SIZE USB_OTG_HS_MAX_PACKET_SIZE -#elif defined(USB_OTG_FS) || defined(USB_OTG_FS_MAX_PACKET_SIZE) -#define CDC_MAX_PACKET_SIZE USB_OTG_FS_MAX_PACKET_SIZE -#else /* USB */ -#define CDC_MAX_PACKET_SIZE USB_MAX_EP0_SIZE -#endif - -/* USBD_CDC Private Variables */ -/* USB Device Core CDC handle declaration */ -USBD_HandleTypeDef hUSBD_Device_CDC; - -static bool CDC_initialized = false; - -/* Received Data over USB are stored in this buffer */ -CDC_TransmitQueue_TypeDef TransmitQueue; -CDC_ReceiveQueue_TypeDef ReceiveQueue; -__IO uint32_t lineState = 0; -__IO bool receivePended = true; - - -/** USBD_CDC Private Function Prototypes */ - -static int8_t USBD_CDC_Init(void); -static int8_t USBD_CDC_DeInit(void); -static int8_t USBD_CDC_Control(uint8_t cmd, uint8_t *pbuf, uint16_t length); -static int8_t USBD_CDC_Receive(uint8_t *pbuf, uint32_t *Len); -static int8_t USBD_CDC_Transferred(void); - -USBD_CDC_ItfTypeDef USBD_CDC_fops = { - USBD_CDC_Init, - USBD_CDC_DeInit, - USBD_CDC_Control, - USBD_CDC_Receive, - USBD_CDC_Transferred -}; - -USBD_CDC_LineCodingTypeDef linecoding = { - 115200, /* baud rate*/ - 0x00, /* stop bits-1*/ - 0x00, /* parity - none*/ - 0x08 /* nb. of bits 8*/ -}; - -/* Private functions ---------------------------------------------------------*/ - -/** - * @brief USBD_CDC_Init - * Initializes the CDC media low layer - * @param None - * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL - */ -static int8_t USBD_CDC_Init(void) -{ - /* Set Application Buffers */ - CDC_TransmitQueue_Init(&TransmitQueue); - CDC_ReceiveQueue_Init(&ReceiveQueue); - receivePended = true; - USBD_CDC_SetRxBuffer(&hUSBD_Device_CDC, CDC_ReceiveQueue_ReserveBlock(&ReceiveQueue)); - - return (USBD_OK); -} - -/** - * @brief USBD_CDC_DeInit - * DeInitializes the CDC media low layer - * @param None - * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL - */ -static int8_t USBD_CDC_DeInit(void) -{ - return (USBD_OK); -} - - -/** - * @brief USBD_CDC_Control - * Manage the CDC class requests - * @param cmd: Command code - * @param pbuf: Buffer containing command data (request parameters) - * @param length: Number of data to be sent (in bytes) - * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL - */ -static int8_t USBD_CDC_Control(uint8_t cmd, uint8_t *pbuf, uint16_t length) -{ - UNUSED(length); - switch (cmd) { - case CDC_SEND_ENCAPSULATED_COMMAND: - - break; - - case CDC_GET_ENCAPSULATED_RESPONSE: - - break; - - case CDC_SET_COMM_FEATURE: - - break; - - case CDC_GET_COMM_FEATURE: - - break; - - case CDC_CLEAR_COMM_FEATURE: - - break; - - /*******************************************************************************/ - /* Line Coding Structure */ - /*-----------------------------------------------------------------------------*/ - /* Offset | Field | Size | Value | Description */ - /* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per second*/ - /* 4 | bCharFormat | 1 | Number | Stop bits */ - /* 0 - 1 Stop bit */ - /* 1 - 1.5 Stop bits */ - /* 2 - 2 Stop bits */ - /* 5 | bParityType | 1 | Number | Parity */ - /* 0 - None */ - /* 1 - Odd */ - /* 2 - Even */ - /* 3 - Mark */ - /* 4 - Space */ - /* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */ - /*******************************************************************************/ - case CDC_SET_LINE_CODING: - linecoding.bitrate = (uint32_t)(pbuf[0] | (pbuf[1] << 8) | \ - (pbuf[2] << 16) | (pbuf[3] << 24)); - linecoding.format = pbuf[4]; - linecoding.paritytype = pbuf[5]; - linecoding.datatype = pbuf[6]; - break; - - case CDC_GET_LINE_CODING: - pbuf[0] = (uint8_t)(linecoding.bitrate); - pbuf[1] = (uint8_t)(linecoding.bitrate >> 8); - pbuf[2] = (uint8_t)(linecoding.bitrate >> 16); - pbuf[3] = (uint8_t)(linecoding.bitrate >> 24); - pbuf[4] = linecoding.format; - pbuf[5] = linecoding.paritytype; - pbuf[6] = linecoding.datatype; - break; - - case CDC_SET_CONTROL_LINE_STATE: - lineState = - (((USBD_SetupReqTypedef *)pbuf)->wValue & 0x01) != 0; // Check DTR state - break; - - case CDC_SEND_BREAK: - - break; - - default: - break; - } - - return (USBD_OK); -} - -/** - * @brief USBD_CDC_Receive - * Data received over USB OUT endpoint are sent over CDC interface - * through this function. - * - * @note - * This function will issue a NAK packet on any OUT packet received on - * USB endpoint untill exiting this function. If you exit this function - * before transfer is complete on CDC interface (ie. using DMA controller) - * it will result in receiving more data while previous ones are still - * not sent. - * - * @param Buf: Buffer of data to be received - * @param Len: Number of data received (in bytes) - * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL - */ -static int8_t USBD_CDC_Receive(uint8_t *Buf, uint32_t *Len) -{ - UNUSED(Buf); - /* It always contains required amount of free space for writing */ - CDC_ReceiveQueue_CommitBlock(&ReceiveQueue, (uint16_t)(*Len)); - receivePended = false; - /* If enough space in the queue for a full buffer then continue receive */ - CDC_resume_receive(); - return USBD_OK; -} - - -static int8_t USBD_CDC_Transferred(void) -{ - CDC_TransmitQueue_CommitRead(&TransmitQueue); - CDC_continue_transmit(); - return (USBD_OK); -} - -void CDC_init(void) -{ - if (!CDC_initialized) { - /* Init Device Library */ - if (USBD_Init(&hUSBD_Device_CDC, &CDC_Desc, 0) == USBD_OK) { - /* Add Supported Class */ - if (USBD_RegisterClass(&hUSBD_Device_CDC, USBD_CDC_CLASS) == USBD_OK) { - /* Add CDC Interface Class */ - if (USBD_CDC_RegisterInterface(&hUSBD_Device_CDC, &USBD_CDC_fops) == USBD_OK) { - /* Start Device Process */ - USBD_Start(&hUSBD_Device_CDC); - CDC_initialized = true; - } - } - } - } -} - -void CDC_deInit(void) -{ - if (CDC_initialized) { - USBD_Stop(&hUSBD_Device_CDC); - USBD_CDC_DeInit(); - USBD_DeInit(&hUSBD_Device_CDC); - CDC_initialized = false; - } -} - -void CDC_continue_transmit(void) -{ - uint16_t size; - uint8_t *buffer; - USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *) hUSBD_Device_CDC.pClassData; - /* - * TS: This method can be called both in the main thread - * (via USBSerial::write) and in the IRQ stream (via USBD_CDC_Transferred), - * BUT the main thread cannot pass this condition while waiting for a IRQ! - * This is not possible because TxState is not zero while waiting for data - * transfer ending! The IRQ thread is uninterrupted, since its priority - * is higher than that of the main thread. So this method is thread safe. - */ - if (hcdc->TxState == 0U) { - buffer = CDC_TransmitQueue_ReadBlock(&TransmitQueue, &size); - if (size > 0) { - USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, buffer, size); - /* - * size never exceed PMA buffer and USBD_CDC_TransmitPacket make full - * copy of block in PMA, so no need to worry about buffer damage - */ - USBD_CDC_TransmitPacket(&hUSBD_Device_CDC); - } - } -} - -void CDC_resume_receive(void) -{ - /* - * TS: main and IRQ threads can't pass it at same time, because - * IRQ may occur only if receivePended is true. So it is thread-safe! - */ - if (!receivePended) { - uint8_t *block = CDC_ReceiveQueue_ReserveBlock(&ReceiveQueue); - if (block != NULL) { - receivePended = true; - /* Set new buffer */ - USBD_CDC_SetRxBuffer(&hUSBD_Device_CDC, block); - USBD_CDC_ReceivePacket(&hUSBD_Device_CDC); - } - } -} - -#endif /* USBD_USE_CDC */ -#endif /* USBCON */ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - +/** + ****************************************************************************** + * @file usbd_cdc_if.c + * @author MCD Application Team + * @brief Generic media access Layer. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +#ifdef USBCON +#ifdef USBD_USE_CDC + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_desc.h" +#include "usbd_cdc_if.h" + +#ifdef USE_USB_HS +#define CDC_MAX_PACKET_SIZE USB_OTG_HS_MAX_PACKET_SIZE +#elif defined(USB_OTG_FS) || defined(USB_OTG_FS_MAX_PACKET_SIZE) +#define CDC_MAX_PACKET_SIZE USB_OTG_FS_MAX_PACKET_SIZE +#else /* USB */ +#define CDC_MAX_PACKET_SIZE USB_MAX_EP0_SIZE +#endif + +/* USBD_CDC Private Variables */ +/* USB Device Core CDC handle declaration */ +USBD_HandleTypeDef hUSBD_Device_CDC; + +static bool CDC_initialized = false; + +/* Received Data over USB are stored in this buffer */ +CDC_TransmitQueue_TypeDef TransmitQueue; +CDC_ReceiveQueue_TypeDef ReceiveQueue; +__IO uint32_t lineState = 0; +__IO bool receivePended = true; + +/* USBD_CDC Private Function Prototypes */ + +static int8_t USBD_CDC_Init(void); +static int8_t USBD_CDC_DeInit(void); +static int8_t USBD_CDC_Control(uint8_t cmd, uint8_t *pbuf, uint16_t length); +static int8_t USBD_CDC_Receive(uint8_t *pbuf, uint32_t *Len); +static int8_t USBD_CDC_Transferred(void); + +USBD_CDC_ItfTypeDef USBD_CDC_fops = {USBD_CDC_Init, USBD_CDC_DeInit, + USBD_CDC_Control, USBD_CDC_Receive, + USBD_CDC_Transferred}; + +USBD_CDC_LineCodingTypeDef linecoding = { + 115200, /* baud rate*/ + 0x00, /* stop bits-1*/ + 0x00, /* parity - none*/ + 0x08 /* nb. of bits 8*/ +}; + +/* Private functions ---------------------------------------------------------*/ + +/** + * @brief USBD_CDC_Init + * Initializes the CDC media low layer + * @param None + * @retval Result of the operation: USBD_OK if all operations are OK else + * USBD_FAIL + */ +static int8_t USBD_CDC_Init(void) +{ + /* Set Application Buffers */ + CDC_TransmitQueue_Init(&TransmitQueue); + CDC_ReceiveQueue_Init(&ReceiveQueue); + receivePended = true; + USBD_CDC_SetRxBuffer(&hUSBD_Device_CDC, + CDC_ReceiveQueue_ReserveBlock(&ReceiveQueue)); + + return (USBD_OK); +} + +/** + * @brief USBD_CDC_DeInit + * DeInitializes the CDC media low layer + * @param None + * @retval Result of the operation: USBD_OK if all operations are OK else + * USBD_FAIL + */ +static int8_t USBD_CDC_DeInit(void) { return (USBD_OK); } + +/** + * @brief USBD_CDC_Control + * Manage the CDC class requests + * @param cmd: Command code + * @param pbuf: Buffer containing command data (request parameters) + * @param length: Number of data to be sent (in bytes) + * @retval Result of the operation: USBD_OK if all operations are OK else + * USBD_FAIL + */ +static int8_t USBD_CDC_Control(uint8_t cmd, uint8_t *pbuf, uint16_t length) +{ + UNUSED(length); + switch (cmd) + { + case CDC_SEND_ENCAPSULATED_COMMAND: + + break; + + case CDC_GET_ENCAPSULATED_RESPONSE: + + break; + + case CDC_SET_COMM_FEATURE: + + break; + + case CDC_GET_COMM_FEATURE: + + break; + + case CDC_CLEAR_COMM_FEATURE: + + break; + + /***************************************************************************** + * Line Coding Structure * + *----------------------------------------------------------------------------* + * Offset| Field |Size| Value | Description * + * 0 | dwDTERate | 4 | Number | Data terminal rate, in bits per second * + * 4 | bCharFormat | 1 | Number | Stop bits * + * 0 - 1 Stop bit * + * 1 - 1.5 Stop bits * + * 2 - 2 Stop bits * + * 5 | bParityType | 1 | Number | Parity * + * 0 - None * + * 1 - Odd * + * 2 - Even * + * 3 - Mark * + * 4 - Space * + * 6 | bDataBits | 1 | Number | Data bits (5, 6, 7, 8 or 16). * + *****************************************************************************/ + case CDC_SET_LINE_CODING: + linecoding.bitrate = (uint32_t)(pbuf[0] | (pbuf[1] << 8) | (pbuf[2] << 16) | + (pbuf[3] << 24)); + linecoding.format = pbuf[4]; + linecoding.paritytype = pbuf[5]; + linecoding.datatype = pbuf[6]; + break; + + case CDC_GET_LINE_CODING: + pbuf[0] = (uint8_t)(linecoding.bitrate); + pbuf[1] = (uint8_t)(linecoding.bitrate >> 8); + pbuf[2] = (uint8_t)(linecoding.bitrate >> 16); + pbuf[3] = (uint8_t)(linecoding.bitrate >> 24); + pbuf[4] = linecoding.format; + pbuf[5] = linecoding.paritytype; + pbuf[6] = linecoding.datatype; + break; + + case CDC_SET_CONTROL_LINE_STATE: + // Check DTR state + lineState = (uint32_t)((((USBD_SetupReqTypedef *)pbuf)->wValue & 0x01) != 0); + break; + + case CDC_SEND_BREAK: + + break; + + default: + break; + } + + return (USBD_OK); +} + +/** + * @brief USBD_CDC_Receive + * Data received over USB OUT endpoint are sent over CDC interface + * through this function. + * + * @note + * This function will issue a NAK packet on any OUT packet received on + * USB endpoint untill exiting this function. If you exit this function + * before transfer is complete on CDC interface (ie. using DMA + * controller) it will result in receiving more data while previous + * ones are still not sent. + * + * @param Buf: Buffer of data to be received + * @param Len: Number of data received (in bytes) + * @retval Result of the operation: USBD_OK if all operations are OK else + * USBD_FAIL + */ +static int8_t USBD_CDC_Receive(uint8_t *Buf, uint32_t *Len) +{ + UNUSED(Buf); + /* It always contains required amount of free space for writing */ + CDC_ReceiveQueue_CommitBlock(&ReceiveQueue, (uint16_t)(*Len)); + receivePended = false; + /* If enough space in the queue for a full buffer then continue receive */ + CDC_resume_receive(); + return USBD_OK; +} + +static int8_t USBD_CDC_Transferred(void) +{ + CDC_TransmitQueue_CommitRead(&TransmitQueue); + CDC_continue_transmit(); + return (USBD_OK); +} + +void CDC_init(void) +{ + if (!CDC_initialized) + { + /* Init Device Library */ + if (USBD_Init(&hUSBD_Device_CDC, &CDC_Desc, 0) == USBD_OK) + { + /* Add Supported Class */ + if (USBD_RegisterClass(&hUSBD_Device_CDC, USBD_CDC_CLASS) == USBD_OK) + { + /* Add CDC Interface Class */ + if (USBD_CDC_RegisterInterface(&hUSBD_Device_CDC, &USBD_CDC_fops) == + USBD_OK) + { + /* Start Device Process */ + USBD_Start(&hUSBD_Device_CDC); + CDC_initialized = true; + } + } + } + } +} + +void CDC_deInit(void) +{ + if (CDC_initialized) + { + USBD_Stop(&hUSBD_Device_CDC); + USBD_CDC_DeInit(); + USBD_DeInit(&hUSBD_Device_CDC); + CDC_initialized = false; + } +} + +void CDC_continue_transmit(void) +{ + uint16_t size; + uint8_t *buffer; + USBD_CDC_HandleTypeDef *hcdc = + (USBD_CDC_HandleTypeDef *)hUSBD_Device_CDC.pClassData; + /* + * TS: This method can be called both in the main thread + * (via USBSerial::write) and in the IRQ stream (via USBD_CDC_Transferred), + * BUT the main thread cannot pass this condition while waiting for a IRQ! + * This is not possible because TxState is not zero while waiting for data + * transfer ending! The IRQ thread is uninterrupted, since its priority + * is higher than that of the main thread. So this method is thread safe. + */ + if (hcdc->TxState == 0U) + { + buffer = CDC_TransmitQueue_ReadBlock(&TransmitQueue, &size); + if (size > 0) + { + USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, buffer, size); + /* + * size never exceed PMA buffer and USBD_CDC_TransmitPacket make full + * copy of block in PMA, so no need to worry about buffer damage + */ + USBD_CDC_TransmitPacket(&hUSBD_Device_CDC); + } + } +} + +void CDC_resume_receive(void) +{ + /* + * TS: main and IRQ threads can't pass it at same time, because + * IRQ may occur only if receivePended is true. So it is thread-safe! + */ + if (!receivePended) + { + uint8_t *block = CDC_ReceiveQueue_ReserveBlock(&ReceiveQueue); + if (block != NULL) + { + receivePended = true; + /* Set new buffer */ + USBD_CDC_SetRxBuffer(&hUSBD_Device_CDC, block); + USBD_CDC_ReceivePacket(&hUSBD_Device_CDC); + } + } +} + +#endif /* USBD_USE_CDC */ +#endif /* USBCON */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + diff --git a/cores/arduino/stm32/usb/cdc/usbd_cdc_if.h b/cores/arduino/stm32/usb/cdc/usbd_cdc_if.h index 155b61b973..e7c1d9b1c6 100644 --- a/cores/arduino/stm32/usb/cdc/usbd_cdc_if.h +++ b/cores/arduino/stm32/usb/cdc/usbd_cdc_if.h @@ -1,61 +1,60 @@ -/** - ****************************************************************************** - * @file usbd_cdc_if.h - * @author MCD Application Team - * @brief Header for usbd_cdc_if.c file. - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USBD_CDC_IF_H -#define __USBD_CDC_IF_H - -#ifdef USBCON -#ifdef USBD_USE_CDC - -#ifdef __cplusplus -extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_cdc.h" -#include "cdc_queue.h" - -/* Periodically, the state of the buffer "UserTxBuffer" is checked. - The period depends on CDC_POLLING_INTERVAL */ -#define CDC_POLLING_INTERVAL 2 /* in ms. The max is 65 and the min is 1 */ - -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ - -extern USBD_CDC_ItfTypeDef USBD_CDC_fops; -extern CDC_TransmitQueue_TypeDef TransmitQueue; -extern CDC_ReceiveQueue_TypeDef ReceiveQueue; - - -/* Exported macro ------------------------------------------------------------*/ -/* Exported functions ------------------------------------------------------- */ -void CDC_continue_transmit(void); -void CDC_resume_receive(void); -void CDC_init(void); -void CDC_deInit(void); - -#ifdef __cplusplus -} -#endif -#endif /* USBD_USE_CDC */ -#endif /* USBCON */ -#endif /* __USBD_CDC_IF_H */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_cdc_if.h + * @author MCD Application Team + * @brief Header for usbd_cdc_if.c file. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USBD_CDC_IF_H +#define __USBD_CDC_IF_H + +#ifdef USBCON +#ifdef USBD_USE_CDC + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_cdc.h" +#include "cdc_queue.h" + +/* Periodically, the state of the buffer "UserTxBuffer" is checked. + The period depends on CDC_POLLING_INTERVAL */ +#define CDC_POLLING_INTERVAL 2 /* in ms. The max is 65 and the min is 1 */ + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ + +extern USBD_CDC_ItfTypeDef USBD_CDC_fops; +extern CDC_TransmitQueue_TypeDef TransmitQueue; +extern CDC_ReceiveQueue_TypeDef ReceiveQueue; + +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ +void CDC_continue_transmit(void); +void CDC_resume_receive(void); +void CDC_init(void); +void CDC_deInit(void); + +#ifdef __cplusplus +} +#endif +#endif /* USBD_USE_CDC */ +#endif /* USBCON */ +#endif /* __USBD_CDC_IF_H */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/cores/arduino/stm32/usb/hid/usbd_hid_composite.c b/cores/arduino/stm32/usb/hid/usbd_hid_composite.c index 20f4de57b6..3268f406d4 100644 --- a/cores/arduino/stm32/usb/hid/usbd_hid_composite.c +++ b/cores/arduino/stm32/usb/hid/usbd_hid_composite.c @@ -1,966 +1,990 @@ -/** - ****************************************************************************** - * @file usbd_hid_composite.c - * @author MCD Application Team - * @brief This file provides the HID core functions. - * - * @verbatim - * - * =================================================================== - * HID Class Description - * =================================================================== - * This module manages the HID class V1.11 following the "Device Class Definition - * for Human Interface Devices (HID) Version 1.11 Jun 27, 2001". - * This driver implements the following aspects of the specification: - * - The Boot Interface Subclass - * - The Mouse protocol - * - Usage Page : Generic Desktop - * - Usage : Joystick - * - Collection : Application - * - * @note In HS mode and when the DMA is used, all variables and data structures - * dealing with the DMA during the transaction process should be 32-bit aligned. - * - * - * @endverbatim - * - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -#ifdef USBCON -#ifdef USBD_USE_HID_COMPOSITE - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_hid_composite.h" -#include "usbd_ctlreq.h" - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - - -/** @defgroup USBD_HID - * @brief usbd core module - * @{ - */ - -/** @defgroup USBD_HID_Private_TypesDefinitions - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBD_HID_Private_Defines - * @{ - */ - -/** - * @} - */ - - -/** @defgroup USBD_HID_Private_Macros - * @{ - */ -/** - * @} - */ - - - - -/** @defgroup USBD_HID_Private_FunctionPrototypes - * @{ - */ - -static uint8_t USBD_HID_Init(USBD_HandleTypeDef *pdev, - uint8_t cfgidx); - -static uint8_t USBD_HID_DeInit(USBD_HandleTypeDef *pdev, - uint8_t cfgidx); - -static uint8_t USBD_COMPOSITE_HID_Setup(USBD_HandleTypeDef *pdev, - USBD_SetupReqTypedef *req); - -static uint8_t USBD_HID_MOUSE_Setup(USBD_HandleTypeDef *pdev, - USBD_SetupReqTypedef *req); - -static uint8_t USBD_HID_KEYBOARD_Setup(USBD_HandleTypeDef *pdev, - USBD_SetupReqTypedef *req); - -static uint8_t *USBD_HID_GetFSCfgDesc(uint16_t *length); - -static uint8_t *USBD_HID_GetHSCfgDesc(uint16_t *length); - -static uint8_t *USBD_HID_GetOtherSpeedCfgDesc(uint16_t *length); - -static uint8_t *USBD_HID_GetDeviceQualifierDesc(uint16_t *length); - -static uint8_t USBD_HID_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum); -/** - * @} - */ - -/** @defgroup USBD_HID_Private_Variables - * @{ - */ - -USBD_ClassTypeDef USBD_COMPOSITE_HID = { - USBD_HID_Init, - USBD_HID_DeInit, - USBD_COMPOSITE_HID_Setup, - NULL, /*EP0_TxSent*/ - NULL, /*EP0_RxReady*/ - USBD_HID_DataIn, /*DataIn*/ - NULL, /*DataOut*/ - NULL, /*SOF */ - NULL, - NULL, - USBD_HID_GetHSCfgDesc, - USBD_HID_GetFSCfgDesc, - USBD_HID_GetOtherSpeedCfgDesc, - USBD_HID_GetDeviceQualifierDesc, -}; - -/* USB HID device FS Configuration Descriptor */ -__ALIGN_BEGIN static uint8_t USBD_HID_CfgFSDesc[USB_COMPOSITE_HID_CONFIG_DESC_SIZ] __ALIGN_END = { - 0x09, /* bLength: Configuration Descriptor size */ - USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ - LOBYTE(USB_COMPOSITE_HID_CONFIG_DESC_SIZ), /* wTotalLength: Bytes returned */ - HIBYTE(USB_COMPOSITE_HID_CONFIG_DESC_SIZ), - 0x02, /*bNumInterfaces: 2 interface*/ - 0x01, /*bConfigurationValue: Configuration value*/ - 0x00, /*iConfiguration: Index of string descriptor describing - the configuration*/ - 0xC0, /*bmAttributes: bus powered and no Support Remote Wake-up */ - 0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/ - - /************** Descriptor of Joystick Mouse interface ****************/ - /* 09 */ - 0x09, /*bLength: Interface Descriptor size*/ - USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/ - HID_MOUSE_INTERFACE, /*bInterfaceNumber: Number of Interface*/ - 0x00, /*bAlternateSetting: Alternate setting*/ - 0x01, /*bNumEndpoints*/ - 0x03, /*bInterfaceClass: HID*/ - 0x01, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/ - 0x02, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/ - 0x00, /*iInterface: Index of string descriptor*/ - /******************** Descriptor of Joystick Mouse HID ********************/ - /* 18 */ - 0x09, /*bLength: HID Descriptor size*/ - HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/ - 0x11, /*bcdHID: HID Class Spec release number*/ - 0x01, - 0x00, /*bCountryCode: Hardware target country*/ - 0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/ - 0x22, /*bDescriptorType*/ - HID_MOUSE_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ - 0x00, - /******************** Descriptor of Mouse endpoint ********************/ - /* 27 */ - 0x07, /*bLength: Endpoint Descriptor size*/ - USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/ - - HID_MOUSE_EPIN_ADDR, /*bEndpointAddress: Endpoint Address (IN)*/ - 0x03, /*bmAttributes: Interrupt endpoint*/ - HID_MOUSE_EPIN_SIZE, /*wMaxPacketSize: 4 Byte max */ - 0x00, - HID_FS_BINTERVAL, /*bInterval: Polling Interval*/ - - /******************************************************************** - Keyboard - *********************************************************************/ - /************** Descriptor of HID Keyboard interface ****************/ - /* 34 */ - 0x09, /*bLength: Interface Descriptor size*/ - USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/ - HID_KEYBOARD_INTERFACE, /*bInterfaceNumber: Number of Interface*/ - 0x00, /*bAlternateSetting: Alternate setting*/ - 0x01, /*bNumEndpoints*/ - 0x03, /*bInterfaceClass: HID*/ - 0x01, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/ - 0x01, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/ - 0x00, /*iInterface: Index of string descriptor*/ - /******************** HID Descriptor ********************/ - /* 43 */ - 0x09, /*bLength: HID Descriptor size*/ - HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/ - 0x11, /*bcdHID: HID Class Spec release number*/ - 0x01, - 0x00, /*bCountryCode: Hardware target country*/ - 0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/ - 0x22, /*bDescriptorType*/ - HID_KEYBOARD_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ - 0x00, - /******************** Descriptor of Keyboard endpoint ********************/ - /* 52 */ - 0x07, /*bLength: Endpoint Descriptor size*/ - USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/ - HID_KEYBOARD_EPIN_ADDR, /*bEndpointAddress: Endpoint Address (IN)*/ - 0x03, /*bmAttributes: Interrupt endpoint*/ - HID_KEYBOARD_EPIN_SIZE, /*wMaxPacketSize: 8 Byte max */ - 0x00, - HID_FS_BINTERVAL /*bInterval: Polling Interval*/ - /* 59 */ -} ; - -/* USB HID device HS Configuration Descriptor */ -__ALIGN_BEGIN static uint8_t USBD_HID_CfgHSDesc[USB_COMPOSITE_HID_CONFIG_DESC_SIZ] __ALIGN_END = { - 0x09, /* bLength: Configuration Descriptor size */ - USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ - LOBYTE(USB_COMPOSITE_HID_CONFIG_DESC_SIZ), /* wTotalLength: Bytes returned */ - HIBYTE(USB_COMPOSITE_HID_CONFIG_DESC_SIZ), - 0x02, /*bNumInterfaces: 2 interface*/ - 0x01, /*bConfigurationValue: Configuration value*/ - 0x00, /*iConfiguration: Index of string descriptor describing - the configuration*/ - 0xC0, /*bmAttributes: bus powered and no Support Remote Wake-up */ - 0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/ - - /************** Descriptor of Joystick Mouse interface ****************/ - /* 09 */ - 0x09, /*bLength: Interface Descriptor size*/ - USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/ - HID_MOUSE_INTERFACE, /*bInterfaceNumber: Number of Interface*/ - 0x00, /*bAlternateSetting: Alternate setting*/ - 0x01, /*bNumEndpoints*/ - 0x03, /*bInterfaceClass: HID*/ - 0x01, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/ - 0x02, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/ - 0, /*iInterface: Index of string descriptor*/ - /******************** Descriptor of Joystick Mouse HID ********************/ - /* 18 */ - 0x09, /*bLength: HID Descriptor size*/ - HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/ - 0x11, /*bcdHID: HID Class Spec release number*/ - 0x01, - 0x00, /*bCountryCode: Hardware target country*/ - 0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/ - 0x22, /*bDescriptorType*/ - HID_MOUSE_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ - 0x00, - /******************** Descriptor of Mouse endpoint ********************/ - /* 27 */ - 0x07, /*bLength: Endpoint Descriptor size*/ - USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/ - - HID_MOUSE_EPIN_ADDR, /*bEndpointAddress: Endpoint Address (IN)*/ - 0x03, /*bmAttributes: Interrupt endpoint*/ - HID_MOUSE_EPIN_SIZE, /*wMaxPacketSize: 4 Byte max */ - 0x00, - HID_HS_BINTERVAL, /*bInterval: Polling Interval*/ - - /******************************************************************** - Keyboard - *********************************************************************/ - /************** Descriptor of HID Keyboard interface ****************/ - /* 34 */ - 0x09, /*bLength: Interface Descriptor size*/ - USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/ - HID_KEYBOARD_INTERFACE, /*bInterfaceNumber: Number of Interface*/ - 0x00, /*bAlternateSetting: Alternate setting*/ - 0x01, /*bNumEndpoints*/ - 0x03, /*bInterfaceClass: HID*/ - 0x01, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/ - 0x01, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/ - 0x00, /*iInterface: Index of string descriptor*/ - /******************** HID Descriptor ********************/ - /* 43 */ - 0x09, /*bLength: HID Descriptor size*/ - HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/ - 0x11, /*bcdHID: HID Class Spec release number*/ - 0x01, - 0x00, /*bCountryCode: Hardware target country*/ - 0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/ - 0x22, /*bDescriptorType*/ - HID_KEYBOARD_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ - 0x00, - /******************** Descriptor of Keyboard endpoint ********************/ - /* 52 */ - 0x07, /*bLength: Endpoint Descriptor size*/ - USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/ - HID_KEYBOARD_EPIN_ADDR, /*bEndpointAddress: Endpoint Address (IN)*/ - 0x03, /*bmAttributes: Interrupt endpoint*/ - HID_KEYBOARD_EPIN_SIZE, /*wMaxPacketSize: 8 Byte max */ - 0x00, - HID_HS_BINTERVAL /*bInterval: Polling Interval*/ - /* 59 */ -} ; - -/* USB HID device Other Speed Configuration Descriptor */ -__ALIGN_BEGIN static uint8_t USBD_HID_OtherSpeedCfgDesc[USB_COMPOSITE_HID_CONFIG_DESC_SIZ] __ALIGN_END = { - 0x09, /* bLength: Configuration Descriptor size */ - USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ - LOBYTE(USB_COMPOSITE_HID_CONFIG_DESC_SIZ), /* wTotalLength: Bytes returned */ - HIBYTE(USB_COMPOSITE_HID_CONFIG_DESC_SIZ), - 0x02, /*bNumInterfaces: 2 interface*/ - 0x01, /*bConfigurationValue: Configuration value*/ - 0x00, /*iConfiguration: Index of string descriptor describing - the configuration*/ - 0xC0, /*bmAttributes: bus powered and no Support Remote Wake-up */ - 0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/ - - /************** Descriptor of Joystick Mouse interface ****************/ - /* 09 */ - 0x09, /*bLength: Interface Descriptor size*/ - USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/ - HID_MOUSE_INTERFACE, /*bInterfaceNumber: Number of Interface*/ - 0x00, /*bAlternateSetting: Alternate setting*/ - 0x01, /*bNumEndpoints*/ - 0x03, /*bInterfaceClass: HID*/ - 0x01, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/ - 0x02, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/ - 0, /*iInterface: Index of string descriptor*/ - /******************** Descriptor of Joystick Mouse HID ********************/ - /* 18 */ - 0x09, /*bLength: HID Descriptor size*/ - HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/ - 0x11, /*bcdHID: HID Class Spec release number*/ - 0x01, - 0x00, /*bCountryCode: Hardware target country*/ - 0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/ - 0x22, /*bDescriptorType*/ - HID_MOUSE_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ - 0x00, - /******************** Descriptor of Mouse endpoint ********************/ - /* 27 */ - 0x07, /*bLength: Endpoint Descriptor size*/ - USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/ - - HID_MOUSE_EPIN_ADDR, /*bEndpointAddress: Endpoint Address (IN)*/ - 0x03, /*bmAttributes: Interrupt endpoint*/ - HID_MOUSE_EPIN_SIZE, /*wMaxPacketSize: 4 Byte max */ - 0x00, - HID_FS_BINTERVAL, /*bInterval: Polling Interval*/ - - /******************************************************************** - Keyboard - *********************************************************************/ - /************** Descriptor of HID Keyboard interface ****************/ - /* 34 */ - 0x09, /*bLength: Interface Descriptor size*/ - USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/ - HID_KEYBOARD_INTERFACE, /*bInterfaceNumber: Number of Interface*/ - 0x00, /*bAlternateSetting: Alternate setting*/ - 0x01, /*bNumEndpoints*/ - 0x03, /*bInterfaceClass: HID*/ - 0x01, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/ - 0x01, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/ - 0x00, /*iInterface: Index of string descriptor*/ - /******************** HID Descriptor ********************/ - /* 43 */ - 0x09, /*bLength: HID Descriptor size*/ - HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/ - 0x11, /*bcdHID: HID Class Spec release number*/ - 0x01, - 0x00, /*bCountryCode: Hardware target country*/ - 0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/ - 0x22, /*bDescriptorType*/ - HID_KEYBOARD_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ - 0x00, - /******************** Descriptor of Keyboard endpoint ********************/ - /* 52 */ - 0x07, /*bLength: Endpoint Descriptor size*/ - USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/ - HID_KEYBOARD_EPIN_ADDR, /*bEndpointAddress: Endpoint Address (IN)*/ - 0x03, /*bmAttributes: Interrupt endpoint*/ - HID_KEYBOARD_EPIN_SIZE, /*wMaxPacketSize: 8 Byte max */ - 0x00, - HID_FS_BINTERVAL /*bInterval: Polling Interval*/ - /* 59 */ -} ; - -/* USB HID device Configuration Descriptor */ -__ALIGN_BEGIN static uint8_t USBD_MOUSE_HID_Desc[USB_HID_DESC_SIZ] __ALIGN_END = { - 0x09, /*bLength: HID Descriptor size*/ - HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/ - 0x11, /*bcdHID: HID Class Spec release number*/ - 0x01, - 0x00, /*bCountryCode: Hardware target country*/ - 0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/ - 0x22, /*bDescriptorType*/ - HID_MOUSE_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ - 0x00 -}; - -/* USB HID device Configuration Descriptor */ -__ALIGN_BEGIN static uint8_t USBD_KEYBOARD_HID_Desc[USB_HID_DESC_SIZ] __ALIGN_END = { - 0x09, /*bLength: HID Descriptor size*/ - HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/ - 0x11, /*bcdHID: HID Class Spec release number*/ - 0x01, - 0x00, /*bCountryCode: Hardware target country*/ - 0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/ - 0x22, /*bDescriptorType*/ - HID_KEYBOARD_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ - 0x00, -}; - -/* USB Standard Device Descriptor */ -__ALIGN_BEGIN static uint8_t USBD_HID_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END = { - USB_LEN_DEV_QUALIFIER_DESC, - USB_DESC_TYPE_DEVICE_QUALIFIER, - 0x00, - 0x02, - 0x00, - 0x00, - 0x00, - 0x40, - 0x01, - 0x00, -}; - -__ALIGN_BEGIN static uint8_t HID_MOUSE_ReportDesc[HID_MOUSE_REPORT_DESC_SIZE] __ALIGN_END = { - 0x05, 0x01, - 0x09, 0x02, - 0xA1, 0x01, - 0x09, 0x01, - - 0xA1, 0x00, - 0x05, 0x09, - 0x19, 0x01, - 0x29, 0x03, - - 0x15, 0x00, - 0x25, 0x01, - 0x95, 0x03, - 0x75, 0x01, - - 0x81, 0x02, - 0x95, 0x01, - 0x75, 0x05, - 0x81, 0x01, - - 0x05, 0x01, - 0x09, 0x30, - 0x09, 0x31, - 0x09, 0x38, - - 0x15, 0x81, - 0x25, 0x7F, - 0x75, 0x08, - 0x95, 0x03, - - 0x81, 0x06, - 0xC0, 0x09, - 0x3c, 0x05, - 0xff, 0x09, - - 0x01, 0x15, - 0x00, 0x25, - 0x01, 0x75, - 0x01, 0x95, - - 0x02, 0xb1, - 0x22, 0x75, - 0x06, 0x95, - 0x01, 0xb1, - - 0x01, 0xc0 -}; - -__ALIGN_BEGIN static uint8_t HID_KEYBOARD_ReportDesc[HID_KEYBOARD_REPORT_DESC_SIZE] __ALIGN_END = { - 0x05, 0x01, // Usage Page (Generic Desktop) - 0x09, 0x06, // Usage (Keyboard) - 0xA1, 0x01, // Collection (Application) - - 0x05, 0x07, // Usage Page (Key Codes) - 0x19, 0xE0, // Usage Minimum (224) - 0x29, 0xE7, // Usage Maximum (231) - 0x15, 0x00, // Logical Minimum (0) - 0x25, 0x01, // Logical Maximum (1) - 0x75, 0x01, // Report Size (1) - 0x95, 0x08, // Report Count (8) - 0x81, 0x02, // Input (Data, Variable, Absolute) - - 0x95, 0x01, // Report Count (1) - 0x75, 0x08, // Report Size (8) - 0x81, 0x03, // Input (Constant) - - 0x95, 0x06, // Report Count (6) - 0x75, 0x08, // Report Size (8) - 0x15, 0x00, // Logical Minimum (0) - 0x25, 0x65, // Logical Maximum(101) - 0x05, 0x07, // Usage Page (Key Codes) - 0x19, 0x00, // Usage Minimum (0) - 0x29, 0x65, // Usage Maximum (101) - 0x81, 0x00, // Input (Data, Array) - - 0xC0 // End Collection -}; - -/** - * @} - */ - -/** @defgroup USBD_HID_Private_Functions - * @{ - */ - -/** - * @brief USBD_HID_Init - * Initialize the HID interface - * @param pdev: device instance - * @param cfgidx: Configuration index - * @retval status - */ -static uint8_t USBD_HID_Init(USBD_HandleTypeDef *pdev, - uint8_t cfgidx) -{ - UNUSED(cfgidx); - uint8_t ret = USBD_OK; - - /* Open EP IN */ - USBD_LL_OpenEP(pdev, - HID_MOUSE_EPIN_ADDR, - USBD_EP_TYPE_INTR, - HID_MOUSE_EPIN_SIZE); - pdev->ep_in[HID_MOUSE_EPIN_ADDR & 0xFU].is_used = 1U; - - /* Open EP IN */ - USBD_LL_OpenEP(pdev, - HID_KEYBOARD_EPIN_ADDR, - USBD_EP_TYPE_INTR, - HID_KEYBOARD_EPIN_SIZE); - pdev->ep_in[HID_KEYBOARD_EPIN_ADDR & 0xFU].is_used = 1U; - - pdev->pClassData = USBD_malloc(sizeof(USBD_HID_HandleTypeDef)); - - if (pdev->pClassData == NULL) { - ret = USBD_FAIL; - } else { - ((USBD_HID_HandleTypeDef *)pdev->pClassData)->Mousestate = HID_IDLE; - ((USBD_HID_HandleTypeDef *)pdev->pClassData)->Keyboardstate = HID_IDLE; - } - return ret; -} - -/** - * @brief USBD_HID_Init - * DeInitialize the HID layer - * @param pdev: device instance - * @param cfgidx: Configuration index - * @retval status - */ -static uint8_t USBD_HID_DeInit(USBD_HandleTypeDef *pdev, - uint8_t cfgidx) -{ - UNUSED(cfgidx); - /* Close HID EPs */ - USBD_LL_CloseEP(pdev, - HID_MOUSE_EPIN_ADDR); - pdev->ep_in[HID_MOUSE_EPIN_ADDR & 0xFU].is_used = 0U; - USBD_LL_CloseEP(pdev, - HID_KEYBOARD_EPIN_ADDR); - pdev->ep_in[HID_KEYBOARD_EPIN_ADDR & 0xFU].is_used = 0U; - - /* Free allocated memory */ - if (pdev->pClassData != NULL) { - USBD_free(pdev->pClassData); - pdev->pClassData = NULL; - } - - return USBD_OK; -} - -/** - * @brief USBD_COMPOSITE_HID_Setup - * Handle the HID specific requests - * @param pdev: instance - * @param req: usb requests - * @retval status - */ -static uint8_t USBD_COMPOSITE_HID_Setup(USBD_HandleTypeDef *pdev, - USBD_SetupReqTypedef *req) -{ - /* Check which interface is targetted by this request */ - if ((req->wIndex & 0x00FF) == HID_KEYBOARD_INTERFACE) { - return USBD_HID_KEYBOARD_Setup(pdev, req); - } else { - return USBD_HID_MOUSE_Setup(pdev, req); - } -} - -/** - * @brief USBD_HID_MOUSE_Setup - * Handle the HID specific requests - * @param pdev: instance - * @param req: usb requests - * @retval status - */ -static uint8_t USBD_HID_MOUSE_Setup(USBD_HandleTypeDef *pdev, - USBD_SetupReqTypedef *req) -{ - USBD_HID_HandleTypeDef *hhid = (USBD_HID_HandleTypeDef *) pdev->pClassData; - uint16_t len = 0U; - uint8_t *pbuf = NULL; - uint16_t status_info = 0U; - USBD_StatusTypeDef ret = USBD_OK; - - switch (req->bmRequest & USB_REQ_TYPE_MASK) { - case USB_REQ_TYPE_CLASS : - switch (req->bRequest) { - - - case HID_REQ_SET_PROTOCOL: - hhid->Protocol = (uint8_t)(req->wValue); - break; - - case HID_REQ_GET_PROTOCOL: - USBD_CtlSendData(pdev, - (uint8_t *)&hhid->Protocol, - 1U); - break; - - case HID_REQ_SET_IDLE: - hhid->IdleState = (uint8_t)(req->wValue >> 8); - break; - - case HID_REQ_GET_IDLE: - USBD_CtlSendData(pdev, - (uint8_t *)&hhid->IdleState, - 1U); - break; - - default: - USBD_CtlError(pdev, req); - ret = USBD_FAIL; - break; - } - break; - - case USB_REQ_TYPE_STANDARD: - switch (req->bRequest) { - case USB_REQ_GET_STATUS: - if (pdev->dev_state == USBD_STATE_CONFIGURED) { - USBD_CtlSendData(pdev, (uint8_t *)(void *)&status_info, 2U); - } else { - USBD_CtlError(pdev, req); - ret = USBD_FAIL; - } - break; - case USB_REQ_GET_DESCRIPTOR: - if (req->wValue >> 8 == HID_REPORT_DESC) { - len = MIN(HID_MOUSE_REPORT_DESC_SIZE, req->wLength); - pbuf = HID_MOUSE_ReportDesc; - } else if (req->wValue >> 8 == HID_DESCRIPTOR_TYPE) { - pbuf = USBD_MOUSE_HID_Desc; - len = MIN(USB_HID_DESC_SIZ, req->wLength); - } else { - USBD_CtlError(pdev, req); - ret = USBD_FAIL; - break; - } - USBD_CtlSendData(pdev, - pbuf, - len); - break; - - case USB_REQ_GET_INTERFACE : - if (pdev->dev_state == USBD_STATE_CONFIGURED) { - USBD_CtlSendData(pdev, (uint8_t *)&hhid->AltSetting, 1U); - } else { - USBD_CtlError(pdev, req); - ret = USBD_FAIL; - } - break; - - case USB_REQ_SET_INTERFACE : - if (pdev->dev_state == USBD_STATE_CONFIGURED) { - hhid->AltSetting = (uint8_t)(req->wValue); - } else { - USBD_CtlError(pdev, req); - ret = USBD_FAIL; - } - break; - - default: - USBD_CtlError(pdev, req); - ret = USBD_FAIL; - break; - } - break; - - default: - USBD_CtlError(pdev, req); - ret = USBD_FAIL; - break; - } - - return ret; -} - -/** - * @brief USBD_HID_KEYBOARD_Setup - * Handle the HID specific requests - * @param pdev: instance - * @param req: usb requests - * @retval status - */ -static uint8_t USBD_HID_KEYBOARD_Setup(USBD_HandleTypeDef *pdev, - USBD_SetupReqTypedef *req) -{ - USBD_HID_HandleTypeDef *hhid = (USBD_HID_HandleTypeDef *) pdev->pClassData; - uint16_t len = 0U; - uint8_t *pbuf = NULL; - uint16_t status_info = 0U; - USBD_StatusTypeDef ret = USBD_OK; - - switch (req->bmRequest & USB_REQ_TYPE_MASK) { - case USB_REQ_TYPE_CLASS : - switch (req->bRequest) { - - - case HID_REQ_SET_PROTOCOL: - hhid->Protocol = (uint8_t)(req->wValue); - break; - - case HID_REQ_GET_PROTOCOL: - USBD_CtlSendData(pdev, - (uint8_t *)&hhid->Protocol, - 1U); - break; - - case HID_REQ_SET_IDLE: - hhid->IdleState = (uint8_t)(req->wValue >> 8); - break; - - case HID_REQ_GET_IDLE: - USBD_CtlSendData(pdev, - (uint8_t *)&hhid->IdleState, - 1U); - break; - - default: - USBD_CtlError(pdev, req); - ret = USBD_FAIL; - break; - } - break; - - case USB_REQ_TYPE_STANDARD: - switch (req->bRequest) { - case USB_REQ_GET_STATUS: - if (pdev->dev_state == USBD_STATE_CONFIGURED) { - USBD_CtlSendData(pdev, (uint8_t *)(void *)&status_info, 2U); - } else { - USBD_CtlError(pdev, req); - ret = USBD_FAIL; - } - break; - case USB_REQ_GET_DESCRIPTOR: - if (req->wValue >> 8 == HID_REPORT_DESC) { - len = MIN(HID_KEYBOARD_REPORT_DESC_SIZE, req->wLength); - pbuf = HID_KEYBOARD_ReportDesc; - } else if (req->wValue >> 8 == HID_DESCRIPTOR_TYPE) { - pbuf = USBD_KEYBOARD_HID_Desc; - len = MIN(USB_HID_DESC_SIZ, req->wLength); - } else { - USBD_CtlError(pdev, req); - ret = USBD_FAIL; - break; - } - USBD_CtlSendData(pdev, - pbuf, - len); - - break; - - case USB_REQ_GET_INTERFACE : - if (pdev->dev_state == USBD_STATE_CONFIGURED) { - USBD_CtlSendData(pdev, (uint8_t *)&hhid->AltSetting, 1U); - } else { - USBD_CtlError(pdev, req); - ret = USBD_FAIL; - } - break; - - case USB_REQ_SET_INTERFACE : - if (pdev->dev_state == USBD_STATE_CONFIGURED) { - hhid->AltSetting = (uint8_t)(req->wValue); - } else { - USBD_CtlError(pdev, req); - ret = USBD_FAIL; - } - break; - - default: - USBD_CtlError(pdev, req); - ret = USBD_FAIL; - break; - } - break; - - default: - USBD_CtlError(pdev, req); - ret = USBD_FAIL; - break; - } - - return ret; -} - -/** - * @brief USBD_HID_SendReport - * Send HID Report - * @param pdev: device instance - * @param buff: pointer to report - * @retval status - */ -uint8_t USBD_HID_MOUSE_SendReport(USBD_HandleTypeDef *pdev, - uint8_t *report, - uint16_t len) -{ - USBD_HID_HandleTypeDef *hhid = (USBD_HID_HandleTypeDef *)pdev->pClassData; - - if (pdev->dev_state == USBD_STATE_CONFIGURED) { - if (hhid->Mousestate == HID_IDLE) { - hhid->Mousestate = HID_BUSY; - USBD_LL_Transmit(pdev, - HID_MOUSE_EPIN_ADDR, - report, - len); - } - } - return USBD_OK; -} - -/** - * @brief USBD_HID_SendReport - * Send HID Report - * @param pdev: device instance - * @param buff: pointer to report - * @retval status - */ -uint8_t USBD_HID_KEYBOARD_SendReport(USBD_HandleTypeDef *pdev, - uint8_t *report, - uint16_t len) -{ - USBD_HID_HandleTypeDef *hhid = (USBD_HID_HandleTypeDef *)pdev->pClassData; - - if (pdev->dev_state == USBD_STATE_CONFIGURED) { - if (hhid->Keyboardstate == HID_IDLE) { - hhid->Keyboardstate = HID_BUSY; - USBD_LL_Transmit(pdev, - HID_KEYBOARD_EPIN_ADDR, - report, - len); - } - } - return USBD_OK; -} - -/** - * @brief USBD_HID_GetPollingInterval - * return polling interval from endpoint descriptor - * @param pdev: device instance - * @retval polling interval - */ -uint32_t USBD_HID_GetPollingInterval(USBD_HandleTypeDef *pdev) -{ - uint32_t polling_interval = 0U; - - /* HIGH-speed endpoints */ - if (pdev->dev_speed == USBD_SPEED_HIGH) { - /* Sets the data transfer polling interval for high speed transfers. - Values between 1..16 are allowed. Values correspond to interval - of 2 ^ (bInterval-1). This option (8 ms, corresponds to HID_HS_BINTERVAL */ - polling_interval = (((1U << (HID_HS_BINTERVAL - 1U))) / 8U); - } else { /* LOW and FULL-speed endpoints */ - /* Sets the data transfer polling interval for low and full - speed transfers */ - polling_interval = HID_FS_BINTERVAL; - } - - return ((uint32_t)(polling_interval)); -} - -/** - * @brief USBD_HID_GetCfgFSDesc - * return FS configuration descriptor - * @param speed : current device speed - * @param length : pointer data length - * @retval pointer to descriptor buffer - */ -static uint8_t *USBD_HID_GetFSCfgDesc(uint16_t *length) -{ - *length = sizeof(USBD_HID_CfgFSDesc); - return USBD_HID_CfgFSDesc; -} - -/** - * @brief USBD_HID_GetCfgHSDesc - * return HS configuration descriptor - * @param speed : current device speed - * @param length : pointer data length - * @retval pointer to descriptor buffer - */ -static uint8_t *USBD_HID_GetHSCfgDesc(uint16_t *length) -{ - *length = sizeof(USBD_HID_CfgHSDesc); - return USBD_HID_CfgHSDesc; -} - -/** - * @brief USBD_HID_GetOtherSpeedCfgDesc - * return other speed configuration descriptor - * @param speed : current device speed - * @param length : pointer data length - * @retval pointer to descriptor buffer - */ -static uint8_t *USBD_HID_GetOtherSpeedCfgDesc(uint16_t *length) -{ - *length = sizeof(USBD_HID_OtherSpeedCfgDesc); - return USBD_HID_OtherSpeedCfgDesc; -} - -/** - * @brief USBD_HID_DataIn - * handle data IN Stage - * @param pdev: device instance - * @param epnum: endpoint index - * @retval status - */ -static uint8_t USBD_HID_DataIn(USBD_HandleTypeDef *pdev, - uint8_t epnum) -{ - - /* Ensure that the FIFO is empty before a new transfer, this condition could - be caused by a new transfer before the end of the previous transfer */ - if (epnum == (HID_KEYBOARD_EPIN_ADDR & 0x7F)) { - ((USBD_HID_HandleTypeDef *)pdev->pClassData)->Keyboardstate = HID_IDLE; - } else if (epnum == (HID_MOUSE_EPIN_ADDR & 0x7F)) { - ((USBD_HID_HandleTypeDef *)pdev->pClassData)->Mousestate = HID_IDLE; - } - return USBD_OK; -} - - -/** -* @brief DeviceQualifierDescriptor -* return Device Qualifier descriptor -* @param length : pointer data length -* @retval pointer to descriptor buffer -*/ -static uint8_t *USBD_HID_GetDeviceQualifierDesc(uint16_t *length) -{ - *length = sizeof(USBD_HID_DeviceQualifierDesc); - return USBD_HID_DeviceQualifierDesc; -} -#endif /* USBD_USE_HID_COMPOSITE */ -#endif /* USBCON */ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_hid_composite.c + * @author MCD Application Team + * @brief This file provides the HID core functions. + * + * @verbatim + * + * =================================================================== + * HID Class Description + * =================================================================== + * This module manages the HID class V1.11 following the "Device + * Class Definition for Human Interface Devices (HID) Version 1.11 + * Jun 27, 2001". This driver implements the following aspects of the + * specification: + * - The Boot Interface Subclass + * - The Mouse protocol + * - Usage Page : Generic Desktop + * - Usage : Joystick + * - Collection : Application + * + * @note In HS mode and when the DMA is used, all variables and data + * structures dealing with the DMA during the transaction process + * should be 32-bit aligned. + * + * + * @endverbatim + * + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +#ifdef USBCON +#ifdef USBD_USE_HID_COMPOSITE + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_hid_composite.h" +#include "usbd_ctlreq.h" + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + +/** @defgroup USBD_HID + * @brief usbd core module + * @{ + */ + +/** @defgroup USBD_HID_Private_TypesDefinitions + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_HID_Private_Defines + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_HID_Private_Macros + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_HID_Private_FunctionPrototypes + * @{ + */ + +static uint8_t USBD_HID_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx); + +static uint8_t USBD_HID_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx); + +static uint8_t USBD_COMPOSITE_HID_Setup(USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req); + +static uint8_t USBD_HID_MOUSE_Setup(USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req); + +static uint8_t USBD_HID_KEYBOARD_Setup(USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req); + +static uint8_t *USBD_HID_GetFSCfgDesc(uint16_t *length); + +static uint8_t *USBD_HID_GetHSCfgDesc(uint16_t *length); + +static uint8_t *USBD_HID_GetOtherSpeedCfgDesc(uint16_t *length); + +static uint8_t *USBD_HID_GetDeviceQualifierDesc(uint16_t *length); + +static uint8_t USBD_HID_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum); + +/** + * @} + */ + +/** @defgroup USBD_HID_Private_Variables + * @{ + */ + +USBD_ClassTypeDef USBD_COMPOSITE_HID = { + USBD_HID_Init, + USBD_HID_DeInit, + USBD_COMPOSITE_HID_Setup, + NULL, /* EP0_TxSent */ + NULL, /* EP0_RxReady */ + USBD_HID_DataIn, /* DataIn */ + NULL, /* DataOut */ + NULL, /* SOF */ + NULL, + NULL, + USBD_HID_GetHSCfgDesc, + USBD_HID_GetFSCfgDesc, + USBD_HID_GetOtherSpeedCfgDesc, + USBD_HID_GetDeviceQualifierDesc, +}; + +/* USB HID device FS Configuration Descriptor */ +__ALIGN_BEGIN static uint8_t + USBD_HID_CfgFSDesc[USB_COMPOSITE_HID_CONFIG_DESC_SIZ] __ALIGN_END = { + 0x09, /* bLength: Configuration Descriptor size */ + USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ + LOBYTE(USB_COMPOSITE_HID_CONFIG_DESC_SIZ), /* wTotalLength: Bytes + returned */ + HIBYTE(USB_COMPOSITE_HID_CONFIG_DESC_SIZ), + 0x02, /* bNumInterfaces: 2 interface */ + 0x01, /* bConfigurationValue: Configuration value */ + 0x00, /* iConfiguration: Index of string descriptor describing +the configuration */ + 0xC0, /* bmAttributes: bus powered and no Support Remote Wake-up */ + 0x32, /* MaxPower 100 mA: this current is used for detecting Vbus */ + + /***************** Descriptor of Joystick Mouse interface *******************/ + /* 09 */ + 0x09, /* bLength: Interface Descriptor size */ + USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */ + HID_MOUSE_INTERFACE, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x01, /* bNumEndpoints */ + 0x03, /* bInterfaceClass: HID */ + 0x01, /* bInterfaceSubClass : 1=BOOT, 0=no boot */ + 0x02, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */ + 0x00, /* iInterface: Index of string descriptor */ + /********************* Descriptor of Joystick Mouse HID *********************/ + /* 18 */ + 0x09, /* bLength: HID Descriptor size */ + HID_DESCRIPTOR_TYPE, /* bDescriptorType: HID */ + 0x11, /* bcdHID: HID Class Spec release number */ + 0x01, 0x00, /* bCountryCode: Hardware target country */ + 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */ + 0x22, /* bDescriptorType */ + HID_MOUSE_REPORT_DESC_SIZE, /* wItemLength: Total length of Report + descriptor */ + 0x00, + /*********************** Descriptor of Mouse endpoint ***********************/ + /* 27 */ + 0x07, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType:*/ + + HID_MOUSE_EPIN_ADDR, /* bEndpointAddress: Endpoint Address (IN)*/ + 0x03, /* bmAttributes: Interrupt endpoint */ + HID_MOUSE_EPIN_SIZE, /* wMaxPacketSize: 4 Byte max */ + 0x00, HID_FS_BINTERVAL, /* bInterval: Polling Interval */ + + /***************************************************************************** + Keyboard + *****************************************************************************/ + /****************** Descriptor of HID Keyboard interface ********************/ + /* 34 */ + 0x09, /* bLength: Interface Descriptor size */ + USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */ + HID_KEYBOARD_INTERFACE, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x01, /* bNumEndpoints */ + 0x03, /* bInterfaceClass: HID */ + 0x01, /* bInterfaceSubClass : 1=BOOT, 0=no boot */ + 0x01, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */ + 0x00, /* iInterface: Index of string descriptor */ + /****************************** HID Descriptor ******************************/ + /* 43 */ + 0x09, /* bLength: HID Descriptor size */ + HID_DESCRIPTOR_TYPE, /* bDescriptorType: HID */ + 0x11, /* bcdHID: HID Class Spec release number */ + 0x01, 0x00, /* bCountryCode: Hardware target country */ + 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */ + 0x22, /* bDescriptorType */ + HID_KEYBOARD_REPORT_DESC_SIZE, /* wItemLength: Total length of Report + descriptor */ + 0x00, + /********************** Descriptor of Keyboard endpoint *********************/ + /* 52 */ + 0x07, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType:*/ + HID_KEYBOARD_EPIN_ADDR, /* bEndpointAddress: Endpoint Address (IN)*/ + 0x03, /* bmAttributes: Interrupt endpoint */ + HID_KEYBOARD_EPIN_SIZE, /* wMaxPacketSize: 8 Byte max */ + 0x00, HID_FS_BINTERVAL /* bInterval: Polling Interval */ + /* 59 */ +}; + +/* USB HID device HS Configuration Descriptor */ +__ALIGN_BEGIN static uint8_t + USBD_HID_CfgHSDesc[USB_COMPOSITE_HID_CONFIG_DESC_SIZ] __ALIGN_END = { + 0x09, /* bLength: Configuration Descriptor size */ + USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ + LOBYTE(USB_COMPOSITE_HID_CONFIG_DESC_SIZ), /* wTotalLength: Bytes + returned */ + HIBYTE(USB_COMPOSITE_HID_CONFIG_DESC_SIZ), + 0x02, /* bNumInterfaces: 2 interface */ + 0x01, /* bConfigurationValue: Configuration value */ + 0x00, /* iConfiguration: Index of string descriptor describing +the configuration */ + 0xC0, /* bmAttributes: bus powered and no Support Remote Wake-up */ + 0x32, /* MaxPower 100 mA: this current is used for detecting Vbus */ + + /***************** Descriptor of Joystick Mouse interface *******************/ + /* 09 */ + 0x09, /* bLength: Interface Descriptor size */ + USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */ + HID_MOUSE_INTERFACE, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x01, /* bNumEndpoints */ + 0x03, /* bInterfaceClass: HID */ + 0x01, /* bInterfaceSubClass : 1=BOOT, 0=no boot */ + 0x02, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */ + 0, /* iInterface: Index of string descriptor */ + /********************* Descriptor of Joystick Mouse HID *********************/ + /* 18 */ + 0x09, /* bLength: HID Descriptor size */ + HID_DESCRIPTOR_TYPE, /* bDescriptorType: HID */ + 0x11, /* bcdHID: HID Class Spec release number */ + 0x01, 0x00, /* bCountryCode: Hardware target country */ + 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */ + 0x22, /* bDescriptorType */ + HID_MOUSE_REPORT_DESC_SIZE, /* wItemLength: Total length of Report + descriptor */ + 0x00, + /*********************** Descriptor of Mouse endpoint ***********************/ + /* 27 */ + 0x07, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType:*/ + + HID_MOUSE_EPIN_ADDR, /* bEndpointAddress: Endpoint Address (IN)*/ + 0x03, /* bmAttributes: Interrupt endpoint */ + HID_MOUSE_EPIN_SIZE, /* wMaxPacketSize: 4 Byte max */ + 0x00, HID_HS_BINTERVAL, /* bInterval: Polling Interval */ + + /***************************************************************************** + Keyboard + *****************************************************************************/ + /****************** Descriptor of HID Keyboard interface ********************/ + /* 34 */ + 0x09, /* bLength: Interface Descriptor size */ + USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */ + HID_KEYBOARD_INTERFACE, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x01, /* bNumEndpoints */ + 0x03, /* bInterfaceClass: HID */ + 0x01, /* bInterfaceSubClass : 1=BOOT, 0=no boot */ + 0x01, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */ + 0x00, /* iInterface: Index of string descriptor */ + /****************************** HID Descriptor ******************************/ + /* 43 */ + 0x09, /* bLength: HID Descriptor size */ + HID_DESCRIPTOR_TYPE, /* bDescriptorType: HID */ + 0x11, /* bcdHID: HID Class Spec release number */ + 0x01, 0x00, /* bCountryCode: Hardware target country */ + 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */ + 0x22, /* bDescriptorType */ + HID_KEYBOARD_REPORT_DESC_SIZE, /* wItemLength: Total length of Report + descriptor */ + 0x00, + /********************** Descriptor of Keyboard endpoint *********************/ + /* 52 */ + 0x07, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType:*/ + HID_KEYBOARD_EPIN_ADDR, /* bEndpointAddress: Endpoint Address (IN)*/ + 0x03, /* bmAttributes: Interrupt endpoint */ + HID_KEYBOARD_EPIN_SIZE, /* wMaxPacketSize: 8 Byte max */ + 0x00, HID_HS_BINTERVAL /* bInterval: Polling Interval */ + /* 59 */ +}; + +/* USB HID device Other Speed Configuration Descriptor */ +__ALIGN_BEGIN static uint8_t + USBD_HID_OtherSpeedCfgDesc[USB_COMPOSITE_HID_CONFIG_DESC_SIZ] __ALIGN_END = { + 0x09, /* bLength: Configuration Descriptor size */ + USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ + LOBYTE(USB_COMPOSITE_HID_CONFIG_DESC_SIZ), /* wTotalLength: Bytes + returned */ + HIBYTE(USB_COMPOSITE_HID_CONFIG_DESC_SIZ), + 0x02, /* bNumInterfaces: 2 interface */ + 0x01, /* bConfigurationValue: Configuration value */ + 0x00, /* iConfiguration: Index of string descriptor describing the configuration */ + 0xC0, /* bmAttributes: bus powered and no Support Remote Wake-up */ + 0x32, /* MaxPower 100 mA: this current is used for detecting Vbus */ + + /***************** Descriptor of Joystick Mouse interface *******************/ + /* 09 */ + 0x09, /* bLength: Interface Descriptor size */ + USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */ + HID_MOUSE_INTERFACE, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x01, /* bNumEndpoints */ + 0x03, /* bInterfaceClass: HID */ + 0x01, /* bInterfaceSubClass : 1=BOOT, 0=no boot */ + 0x02, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */ + 0, /* iInterface: Index of string descriptor */ + /********************* Descriptor of Joystick Mouse HID *********************/ + /* 18 */ + 0x09, /* bLength: HID Descriptor size */ + HID_DESCRIPTOR_TYPE, /* bDescriptorType: HID */ + 0x11, /* bcdHID: HID Class Spec release number */ + 0x01, 0x00, /* bCountryCode: Hardware target country */ + 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */ + 0x22, /* bDescriptorType */ + HID_MOUSE_REPORT_DESC_SIZE, /* wItemLength: Total length of Report + descriptor */ + 0x00, + /*********************** Descriptor of Mouse endpoint ***********************/ + /* 27 */ + 0x07, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType:*/ + + HID_MOUSE_EPIN_ADDR, /* bEndpointAddress: Endpoint Address (IN)*/ + 0x03, /* bmAttributes: Interrupt endpoint */ + HID_MOUSE_EPIN_SIZE, /* wMaxPacketSize: 4 Byte max */ + 0x00, HID_FS_BINTERVAL, /* bInterval: Polling Interval */ + + /***************************************************************************** + Keyboard + *****************************************************************************/ + /****************** Descriptor of HID Keyboard interface ********************/ + /* 34 */ + 0x09, /* bLength: Interface Descriptor size */ + USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface descriptor type */ + HID_KEYBOARD_INTERFACE, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x01, /* bNumEndpoints */ + 0x03, /* bInterfaceClass: HID */ + 0x01, /* bInterfaceSubClass : 1=BOOT, 0=no boot */ + 0x01, /* nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse */ + 0x00, /* iInterface: Index of string descriptor */ + /****************************** HID Descriptor ******************************/ + /* 43 */ + 0x09, /* bLength: HID Descriptor size */ + HID_DESCRIPTOR_TYPE, /* bDescriptorType: HID */ + 0x11, /* bcdHID: HID Class Spec release number */ + 0x01, 0x00, /* bCountryCode: Hardware target country */ + 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */ + 0x22, /* bDescriptorType */ + HID_KEYBOARD_REPORT_DESC_SIZE, /* wItemLength: Total length of Report + descriptor */ + 0x00, + /********************** Descriptor of Keyboard endpoint *********************/ + /* 52 */ + 0x07, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType:*/ + HID_KEYBOARD_EPIN_ADDR, /* bEndpointAddress: Endpoint Address (IN)*/ + 0x03, /* bmAttributes: Interrupt endpoint */ + HID_KEYBOARD_EPIN_SIZE, /* wMaxPacketSize: 8 Byte max */ + 0x00, HID_FS_BINTERVAL /* bInterval: Polling Interval */ + /* 59 */ +}; + +/* USB HID device Configuration Descriptor */ +__ALIGN_BEGIN static uint8_t + USBD_MOUSE_HID_Desc[USB_HID_DESC_SIZ] __ALIGN_END = { + 0x09, /* bLength: HID Descriptor size */ + HID_DESCRIPTOR_TYPE, /* bDescriptorType: HID */ + 0x11, /* bcdHID: HID Class Spec release number */ + 0x01, + 0x00, /* bCountryCode: Hardware target country */ + 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */ + 0x22, /* bDescriptorType */ + HID_MOUSE_REPORT_DESC_SIZE, /* wItemLength: Total length of Report descriptor + */ + 0x00 +}; + +/* USB HID device Configuration Descriptor */ +__ALIGN_BEGIN static uint8_t + USBD_KEYBOARD_HID_Desc[USB_HID_DESC_SIZ] __ALIGN_END = { + 0x09, /* bLength: HID Descriptor size */ + HID_DESCRIPTOR_TYPE, /* bDescriptorType: HID */ + 0x11, /* bcdHID: HID Class Spec release number */ + 0x01, + 0x00, /* bCountryCode: Hardware target country */ + 0x01, /* bNumDescriptors: Number of HID class descriptors to follow */ + 0x22, /* bDescriptorType */ + HID_KEYBOARD_REPORT_DESC_SIZE, /* wItemLength: Total length of Report + * descriptor */ + 0x00, +}; + +/* USB Standard Device Descriptor */ +__ALIGN_BEGIN static uint8_t + USBD_HID_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END = { + USB_LEN_DEV_QUALIFIER_DESC, + USB_DESC_TYPE_DEVICE_QUALIFIER, + 0x00, + 0x02, + 0x00, + 0x00, + 0x00, + 0x40, + 0x01, + 0x00, +}; + +__ALIGN_BEGIN static uint8_t + HID_MOUSE_ReportDesc[HID_MOUSE_REPORT_DESC_SIZE] __ALIGN_END = { + 0x05, 0x01, + 0x09, 0x02, + 0xA1, 0x01, + 0x09, 0x01, + + 0xA1, 0x00, + 0x05, 0x09, + 0x19, 0x01, + 0x29, 0x03, + + 0x15, 0x00, + 0x25, 0x01, + 0x95, 0x03, + 0x75, 0x01, + + 0x81, 0x02, + 0x95, 0x01, + 0x75, 0x05, + 0x81, 0x01, + + 0x05, 0x01, + 0x09, 0x30, + 0x09, 0x31, + 0x09, 0x38, + + 0x15, 0x81, + 0x25, 0x7F, + 0x75, 0x08, + 0x95, 0x03, + + 0x81, 0x06, + 0xC0, 0x09, + 0x3c, 0x05, + 0xff, 0x09, + + 0x01, 0x15, + 0x00, 0x25, + 0x01, 0x75, + 0x01, 0x95, + + 0x02, 0xb1, + 0x22, 0x75, + 0x06, 0x95, + 0x01, 0xb1, + + 0x01, 0xc0 +}; + +__ALIGN_BEGIN static uint8_t + HID_KEYBOARD_ReportDesc[HID_KEYBOARD_REPORT_DESC_SIZE] __ALIGN_END = { + 0x05, 0x01, // Usage Page (Generic Desktop) + 0x09, 0x06, // Usage (Keyboard) + 0xA1, 0x01, // Collection (Application) + + 0x05, 0x07, // Usage Page (Key Codes) + 0x19, 0xE0, // Usage Minimum (224) + 0x29, 0xE7, // Usage Maximum (231) + 0x15, 0x00, // Logical Minimum (0) + 0x25, 0x01, // Logical Maximum (1) + 0x75, 0x01, // Report Size (1) + 0x95, 0x08, // Report Count (8) + 0x81, 0x02, // Input (Data, Variable, Absolute) + + 0x95, 0x01, // Report Count (1) + 0x75, 0x08, // Report Size (8) + 0x81, 0x03, // Input (Constant) + + 0x95, 0x06, // Report Count (6) + 0x75, 0x08, // Report Size (8) + 0x15, 0x00, // Logical Minimum (0) + 0x25, 0x65, // Logical Maximum(101) + 0x05, 0x07, // Usage Page (Key Codes) + 0x19, 0x00, // Usage Minimum (0) + 0x29, 0x65, // Usage Maximum (101) + 0x81, 0x00, // Input (Data, Array) + + 0xC0 // End Collection +}; + +/** + * @} + */ + +/** @defgroup USBD_HID_Private_Functions + * @{ + */ + +/** + * @brief USBD_HID_Init + * Initialize the HID interface + * @param pdev: device instance + * @param cfgidx: Configuration index + * @retval status + */ +static uint8_t USBD_HID_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx) +{ + UNUSED(cfgidx); + uint8_t ret = USBD_OK; + + /* Open EP IN */ + USBD_LL_OpenEP(pdev, HID_MOUSE_EPIN_ADDR, USBD_EP_TYPE_INTR, + HID_MOUSE_EPIN_SIZE); + pdev->ep_in[HID_MOUSE_EPIN_ADDR & 0xFU].is_used = 1U; + + /* Open EP IN */ + USBD_LL_OpenEP(pdev, HID_KEYBOARD_EPIN_ADDR, USBD_EP_TYPE_INTR, + HID_KEYBOARD_EPIN_SIZE); + pdev->ep_in[HID_KEYBOARD_EPIN_ADDR & 0xFU].is_used = 1U; + + pdev->pClassData = USBD_malloc(sizeof(USBD_HID_HandleTypeDef)); + + if (pdev->pClassData == NULL) + { + ret = USBD_FAIL; + } + else + { + ((USBD_HID_HandleTypeDef *)pdev->pClassData)->Mousestate = HID_IDLE; + ((USBD_HID_HandleTypeDef *)pdev->pClassData)->Keyboardstate = HID_IDLE; + } + return ret; +} + +/** + * @brief USBD_HID_Init + * DeInitialize the HID layer + * @param pdev: device instance + * @param cfgidx: Configuration index + * @retval status + */ +static uint8_t USBD_HID_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx) +{ + UNUSED(cfgidx); + /* Close HID EPs */ + USBD_LL_CloseEP(pdev, HID_MOUSE_EPIN_ADDR); + pdev->ep_in[HID_MOUSE_EPIN_ADDR & 0xFU].is_used = 0U; + USBD_LL_CloseEP(pdev, HID_KEYBOARD_EPIN_ADDR); + pdev->ep_in[HID_KEYBOARD_EPIN_ADDR & 0xFU].is_used = 0U; + + /* Free allocated memory */ + if (pdev->pClassData != NULL) + { + USBD_free(pdev->pClassData); + pdev->pClassData = NULL; + } + + return USBD_OK; +} + +/** + * @brief USBD_COMPOSITE_HID_Setup + * Handle the HID specific requests + * @param pdev: instance + * @param req: usb requests + * @retval status + */ +static uint8_t USBD_COMPOSITE_HID_Setup(USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req) +{ + /* Check which interface is targetted by this request */ + if ((req->wIndex & 0x00FF) == HID_KEYBOARD_INTERFACE) + { + return USBD_HID_KEYBOARD_Setup(pdev, req); + } + else + { + return USBD_HID_MOUSE_Setup(pdev, req); + } +} + +/** + * @brief USBD_HID_MOUSE_Setup + * Handle the HID specific requests + * @param pdev: instance + * @param req: usb requests + * @retval status + */ +static uint8_t USBD_HID_MOUSE_Setup(USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req) +{ + USBD_HID_HandleTypeDef *hhid = (USBD_HID_HandleTypeDef *)pdev->pClassData; + uint16_t len = 0U; + uint8_t *pbuf = NULL; + uint16_t status_info = 0U; + USBD_StatusTypeDef ret = USBD_OK; + + switch (req->bmRequest & USB_REQ_TYPE_MASK) + { + case USB_REQ_TYPE_CLASS: + switch (req->bRequest) + { + + case HID_REQ_SET_PROTOCOL: + hhid->Protocol = (uint8_t)(req->wValue); + break; + + case HID_REQ_GET_PROTOCOL: + USBD_CtlSendData(pdev, (uint8_t *)&hhid->Protocol, 1U); + break; + + case HID_REQ_SET_IDLE: + hhid->IdleState = (uint8_t)(req->wValue >> 8); + break; + + case HID_REQ_GET_IDLE: + USBD_CtlSendData(pdev, (uint8_t *)&hhid->IdleState, 1U); + break; + + default: + USBD_CtlError(pdev, req); + ret = USBD_FAIL; + break; + } + break; + + case USB_REQ_TYPE_STANDARD: + switch (req->bRequest) + { + case USB_REQ_GET_STATUS: + if (pdev->dev_state == USBD_STATE_CONFIGURED) + { + USBD_CtlSendData(pdev, (uint8_t *)(void *)&status_info, 2U); + } + else + { + USBD_CtlError(pdev, req); + ret = USBD_FAIL; + } + break; + case USB_REQ_GET_DESCRIPTOR: + if (req->wValue >> 8 == HID_REPORT_DESC) + { + len = MIN(HID_MOUSE_REPORT_DESC_SIZE, req->wLength); + pbuf = HID_MOUSE_ReportDesc; + } + else if (req->wValue >> 8 == HID_DESCRIPTOR_TYPE) + { + pbuf = USBD_MOUSE_HID_Desc; + len = MIN(USB_HID_DESC_SIZ, req->wLength); + } + else + { + USBD_CtlError(pdev, req); + ret = USBD_FAIL; + break; + } + USBD_CtlSendData(pdev, pbuf, len); + break; + + case USB_REQ_GET_INTERFACE: + if (pdev->dev_state == USBD_STATE_CONFIGURED) + { + USBD_CtlSendData(pdev, (uint8_t *)&hhid->AltSetting, 1U); + } + else + { + USBD_CtlError(pdev, req); + ret = USBD_FAIL; + } + break; + + case USB_REQ_SET_INTERFACE: + if (pdev->dev_state == USBD_STATE_CONFIGURED) + { + hhid->AltSetting = (uint8_t)(req->wValue); + } + else + { + USBD_CtlError(pdev, req); + ret = USBD_FAIL; + } + break; + + default: + USBD_CtlError(pdev, req); + ret = USBD_FAIL; + break; + } + break; + + default: + USBD_CtlError(pdev, req); + ret = USBD_FAIL; + break; + } + + return ret; +} + +/** + * @brief USBD_HID_KEYBOARD_Setup + * Handle the HID specific requests + * @param pdev: instance + * @param req: usb requests + * @retval status + */ +static uint8_t USBD_HID_KEYBOARD_Setup(USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req) +{ + USBD_HID_HandleTypeDef *hhid = (USBD_HID_HandleTypeDef *)pdev->pClassData; + uint16_t len = 0U; + uint8_t *pbuf = NULL; + uint16_t status_info = 0U; + USBD_StatusTypeDef ret = USBD_OK; + + switch (req->bmRequest & USB_REQ_TYPE_MASK) + { + case USB_REQ_TYPE_CLASS: + switch (req->bRequest) + { + + case HID_REQ_SET_PROTOCOL: + hhid->Protocol = (uint8_t)(req->wValue); + break; + + case HID_REQ_GET_PROTOCOL: + USBD_CtlSendData(pdev, (uint8_t *)&hhid->Protocol, 1U); + break; + + case HID_REQ_SET_IDLE: + hhid->IdleState = (uint8_t)(req->wValue >> 8); + break; + + case HID_REQ_GET_IDLE: + USBD_CtlSendData(pdev, (uint8_t *)&hhid->IdleState, 1U); + break; + + default: + USBD_CtlError(pdev, req); + ret = USBD_FAIL; + break; + } + break; + + case USB_REQ_TYPE_STANDARD: + switch (req->bRequest) + { + case USB_REQ_GET_STATUS: + if (pdev->dev_state == USBD_STATE_CONFIGURED) + { + USBD_CtlSendData(pdev, (uint8_t *)(void *)&status_info, 2U); + } + else + { + USBD_CtlError(pdev, req); + ret = USBD_FAIL; + } + break; + case USB_REQ_GET_DESCRIPTOR: + if (req->wValue >> 8 == HID_REPORT_DESC) + { + len = MIN(HID_KEYBOARD_REPORT_DESC_SIZE, req->wLength); + pbuf = HID_KEYBOARD_ReportDesc; + } + else if (req->wValue >> 8 == HID_DESCRIPTOR_TYPE) + { + pbuf = USBD_KEYBOARD_HID_Desc; + len = MIN(USB_HID_DESC_SIZ, req->wLength); + } + else + { + USBD_CtlError(pdev, req); + ret = USBD_FAIL; + break; + } + USBD_CtlSendData(pdev, pbuf, len); + + break; + + case USB_REQ_GET_INTERFACE: + if (pdev->dev_state == USBD_STATE_CONFIGURED) + { + USBD_CtlSendData(pdev, (uint8_t *)&hhid->AltSetting, 1U); + } + else + { + USBD_CtlError(pdev, req); + ret = USBD_FAIL; + } + break; + + case USB_REQ_SET_INTERFACE: + if (pdev->dev_state == USBD_STATE_CONFIGURED) + { + hhid->AltSetting = (uint8_t)(req->wValue); + } + else + { + USBD_CtlError(pdev, req); + ret = USBD_FAIL; + } + break; + + default: + USBD_CtlError(pdev, req); + ret = USBD_FAIL; + break; + } + break; + + default: + USBD_CtlError(pdev, req); + ret = USBD_FAIL; + break; + } + + return ret; +} + +/** + * @brief USBD_HID_SendReport + * Send HID Report + * @param pdev: device instance + * @param buff: pointer to report + * @retval status + */ +uint8_t USBD_HID_MOUSE_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, + uint16_t len) +{ + USBD_HID_HandleTypeDef *hhid = (USBD_HID_HandleTypeDef *)pdev->pClassData; + + if (pdev->dev_state == USBD_STATE_CONFIGURED) + { + if (hhid->Mousestate == HID_IDLE) + { + hhid->Mousestate = HID_BUSY; + USBD_LL_Transmit(pdev, HID_MOUSE_EPIN_ADDR, report, len); + } + } + return USBD_OK; +} + +/** + * @brief USBD_HID_SendReport + * Send HID Report + * @param pdev: device instance + * @param buff: pointer to report + * @retval status + */ +uint8_t USBD_HID_KEYBOARD_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, + uint16_t len) +{ + USBD_HID_HandleTypeDef *hhid = (USBD_HID_HandleTypeDef *)pdev->pClassData; + + if (pdev->dev_state == USBD_STATE_CONFIGURED) + { + if (hhid->Keyboardstate == HID_IDLE) + { + hhid->Keyboardstate = HID_BUSY; + USBD_LL_Transmit(pdev, HID_KEYBOARD_EPIN_ADDR, report, len); + } + } + return USBD_OK; +} + +/** + * @brief USBD_HID_GetPollingInterval + * return polling interval from endpoint descriptor + * @param pdev: device instance + * @retval polling interval + */ +uint32_t USBD_HID_GetPollingInterval(USBD_HandleTypeDef *pdev) +{ + uint32_t polling_interval = 0U; + + /* HIGH-speed endpoints */ + if (pdev->dev_speed == USBD_SPEED_HIGH) + { + /* Sets the data transfer polling interval for high speed transfers. + Values between 1..16 are allowed. Values correspond to interval + of 2 ^ (bInterval-1). This option (8 ms, corresponds to HID_HS_BINTERVAL */ + polling_interval = (((1U << (HID_HS_BINTERVAL - 1U))) / 8U); + } + else + { /* LOW and FULL-speed endpoints */ + /* Sets the data transfer polling interval for low and full + speed transfers */ + polling_interval = HID_FS_BINTERVAL; + } + + return ((uint32_t)(polling_interval)); +} + +/** + * @brief USBD_HID_GetCfgFSDesc + * return FS configuration descriptor + * @param speed : current device speed + * @param length : pointer data length + * @retval pointer to descriptor buffer + */ +static uint8_t *USBD_HID_GetFSCfgDesc(uint16_t *length) +{ + *length = sizeof(USBD_HID_CfgFSDesc); + return USBD_HID_CfgFSDesc; +} + +/** + * @brief USBD_HID_GetCfgHSDesc + * return HS configuration descriptor + * @param speed : current device speed + * @param length : pointer data length + * @retval pointer to descriptor buffer + */ +static uint8_t *USBD_HID_GetHSCfgDesc(uint16_t *length) +{ + *length = sizeof(USBD_HID_CfgHSDesc); + return USBD_HID_CfgHSDesc; +} + +/** + * @brief USBD_HID_GetOtherSpeedCfgDesc + * return other speed configuration descriptor + * @param speed : current device speed + * @param length : pointer data length + * @retval pointer to descriptor buffer + */ +static uint8_t *USBD_HID_GetOtherSpeedCfgDesc(uint16_t *length) +{ + *length = sizeof(USBD_HID_OtherSpeedCfgDesc); + return USBD_HID_OtherSpeedCfgDesc; +} + +/** + * @brief USBD_HID_DataIn + * handle data IN Stage + * @param pdev: device instance + * @param epnum: endpoint index + * @retval status + */ +static uint8_t USBD_HID_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum) +{ + + /* Ensure that the FIFO is empty before a new transfer, this condition could + be caused by a new transfer before the end of the previous transfer */ + if (epnum == (HID_KEYBOARD_EPIN_ADDR & 0x7F)) + { + ((USBD_HID_HandleTypeDef *)pdev->pClassData)->Keyboardstate = HID_IDLE; + } + else if (epnum == (HID_MOUSE_EPIN_ADDR & 0x7F)) + { + ((USBD_HID_HandleTypeDef *)pdev->pClassData)->Mousestate = HID_IDLE; + } + return USBD_OK; +} + +/** + * @brief DeviceQualifierDescriptor + * return Device Qualifier descriptor + * @param length : pointer data length + * @retval pointer to descriptor buffer + */ +static uint8_t *USBD_HID_GetDeviceQualifierDesc(uint16_t *length) +{ + *length = sizeof(USBD_HID_DeviceQualifierDesc); + return USBD_HID_DeviceQualifierDesc; +} + +#endif /* USBD_USE_HID_COMPOSITE */ +#endif /* USBCON */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/cores/arduino/stm32/usb/hid/usbd_hid_composite.h b/cores/arduino/stm32/usb/hid/usbd_hid_composite.h index 012b8b3fbe..887bd01bc3 100644 --- a/cores/arduino/stm32/usb/hid/usbd_hid_composite.h +++ b/cores/arduino/stm32/usb/hid/usbd_hid_composite.h @@ -1,153 +1,155 @@ -/** - ****************************************************************************** - * @file usbd_hid_composite.h - * @author MCD Application Team - * @brief Header file for the usbd_hid_core.c file. - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_HID_COMPOSITE_H -#define __USB_HID_COMPOSITE_H - -#ifdef USBCON -#ifdef USBD_USE_HID_COMPOSITE - -#ifdef __cplusplus -extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_ioreq.h" - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ -/** @defgroup USBD_HID - * @brief This file is the Header file for usbd_hid.c - * @{ - */ - - -/** @defgroup USBD_HID_Exported_Defines - * @{ - */ -#define HID_MOUSE_INTERFACE 0x00U -#define HID_KEYBOARD_INTERFACE 0x01U - -#define HID_MOUSE_EPIN_ADDR 0x81U -#define HID_MOUSE_EPIN_SIZE 0x04U - -#define HID_KEYBOARD_EPIN_ADDR 0x82U -#define HID_KEYBOARD_EPIN_SIZE 0x08U - -#define USB_COMPOSITE_HID_CONFIG_DESC_SIZ 59U -#define USB_HID_DESC_SIZ 9U -#define HID_MOUSE_REPORT_DESC_SIZE 74U -#define HID_KEYBOARD_REPORT_DESC_SIZE 45U - -#define HID_DESCRIPTOR_TYPE 0x21 -#define HID_REPORT_DESC 0x22 - -#ifndef HID_HS_BINTERVAL -#define HID_HS_BINTERVAL 0x07U -#endif /* HID_HS_BINTERVAL */ - -#ifndef HID_FS_BINTERVAL -#define HID_FS_BINTERVAL 0x0AU -#endif /* HID_FS_BINTERVAL */ - -#define HID_REQ_SET_PROTOCOL 0x0BU -#define HID_REQ_GET_PROTOCOL 0x03U - -#define HID_REQ_SET_IDLE 0x0AU -#define HID_REQ_GET_IDLE 0x02U - -#define HID_REQ_SET_REPORT 0x09U -#define HID_REQ_GET_REPORT 0x01U -/** - * @} - */ - - -/** @defgroup USBD_CORE_Exported_TypesDefinitions - * @{ - */ -typedef enum { - HID_IDLE = 0, - HID_BUSY, -} -HID_StateTypeDef; - - -typedef struct { - uint32_t Protocol; - uint32_t IdleState; - uint32_t AltSetting; - HID_StateTypeDef Mousestate; - HID_StateTypeDef Keyboardstate; -} -USBD_HID_HandleTypeDef; -/** - * @} - */ - - - -/** @defgroup USBD_CORE_Exported_Macros - * @{ - */ - -/** - * @} - */ - -/** @defgroup USBD_CORE_Exported_Variables - * @{ - */ - -extern USBD_ClassTypeDef USBD_COMPOSITE_HID; -#define USBD_COMPOSITE_HID_CLASS &USBD_COMPOSITE_HID -/** - * @} - */ - -/** @defgroup USB_CORE_Exported_Functions - * @{ - */ -uint8_t USBD_HID_MOUSE_SendReport(USBD_HandleTypeDef *pdev, - uint8_t *report, - uint16_t len); -uint8_t USBD_HID_KEYBOARD_SendReport(USBD_HandleTypeDef *pdev, - uint8_t *report, - uint16_t len); - -uint32_t USBD_HID_GetPollingInterval(USBD_HandleTypeDef *pdev); - -#ifdef __cplusplus -} -#endif - -#endif /* USBD_USE_HID_COMPOSITE */ -#endif /* USBCON */ -#endif /* __USB_HID_COMPOSITE_H */ -/** - * @} - */ - -/** - * @} - */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_hid_composite.h + * @author MCD Application Team + * @brief Header file for the usbd_hid_core.c file. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USB_HID_COMPOSITE_H +#define __USB_HID_COMPOSITE_H + +#ifdef USBCON +#ifdef USBD_USE_HID_COMPOSITE + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_ioreq.h" + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + + +/** @defgroup USBD_HID + * @brief This file is the Header file for usbd_hid.c + * @{ + */ + +/** @defgroup USBD_HID_Exported_Defines + * @{ + */ + +#define HID_MOUSE_INTERFACE 0x00U +#define HID_KEYBOARD_INTERFACE 0x01U + +#define HID_MOUSE_EPIN_ADDR 0x81U +#define HID_MOUSE_EPIN_SIZE 0x04U + +#define HID_KEYBOARD_EPIN_ADDR 0x82U +#define HID_KEYBOARD_EPIN_SIZE 0x08U + +#define USB_COMPOSITE_HID_CONFIG_DESC_SIZ 59U +#define USB_HID_DESC_SIZ 9U +#define HID_MOUSE_REPORT_DESC_SIZE 74U +#define HID_KEYBOARD_REPORT_DESC_SIZE 45U + +#define HID_DESCRIPTOR_TYPE 0x21 +#define HID_REPORT_DESC 0x22 + +#ifndef HID_HS_BINTERVAL +#define HID_HS_BINTERVAL 0x07U +#endif /* HID_HS_BINTERVAL */ + +#ifndef HID_FS_BINTERVAL +#define HID_FS_BINTERVAL 0x0AU +#endif /* HID_FS_BINTERVAL */ + +#define HID_REQ_SET_PROTOCOL 0x0BU +#define HID_REQ_GET_PROTOCOL 0x03U + +#define HID_REQ_SET_IDLE 0x0AU +#define HID_REQ_GET_IDLE 0x02U + +#define HID_REQ_SET_REPORT 0x09U +#define HID_REQ_GET_REPORT 0x01U + +/** + * @} + */ + +/** @defgroup USBD_CORE_Exported_TypesDefinitions + * @{ + */ + +typedef enum +{ + HID_IDLE = 0, + HID_BUSY, +} HID_StateTypeDef; + +typedef struct +{ + uint32_t Protocol; + uint32_t IdleState; + uint32_t AltSetting; + HID_StateTypeDef Mousestate; + HID_StateTypeDef Keyboardstate; +} USBD_HID_HandleTypeDef; + +/** + * @} + */ + +/** @defgroup USBD_CORE_Exported_Macros + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_CORE_Exported_Variables + * @{ + */ + +extern USBD_ClassTypeDef USBD_COMPOSITE_HID; +#define USBD_COMPOSITE_HID_CLASS &USBD_COMPOSITE_HID + +/** + * @} + */ + +/** @defgroup USB_CORE_Exported_Functions + * @{ + */ + +uint8_t USBD_HID_MOUSE_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, + uint16_t len); +uint8_t USBD_HID_KEYBOARD_SendReport(USBD_HandleTypeDef *pdev, uint8_t *report, + uint16_t len); + +uint32_t USBD_HID_GetPollingInterval(USBD_HandleTypeDef *pdev); + +#ifdef __cplusplus +} +#endif + +#endif /* USBD_USE_HID_COMPOSITE */ +#endif /* USBCON */ +#endif /* __USB_HID_COMPOSITE_H */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/cores/arduino/stm32/usb/hid/usbd_hid_composite_if.c b/cores/arduino/stm32/usb/hid/usbd_hid_composite_if.c index 68982f4a7f..30656d5229 100644 --- a/cores/arduino/stm32/usb/hid/usbd_hid_composite_if.c +++ b/cores/arduino/stm32/usb/hid/usbd_hid_composite_if.c @@ -8,27 +8,28 @@ * *

© COPYRIGHT(c) 2016 STMicroelectronics

* - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************** */ diff --git a/cores/arduino/stm32/usb/hid/usbd_hid_composite_if.h b/cores/arduino/stm32/usb/hid/usbd_hid_composite_if.h index eb271985a9..3a8d56f743 100644 --- a/cores/arduino/stm32/usb/hid/usbd_hid_composite_if.h +++ b/cores/arduino/stm32/usb/hid/usbd_hid_composite_if.h @@ -7,27 +7,28 @@ * *

© COPYRIGHT(c) 2016 STMicroelectronics

* - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. Neither the name of STMicroelectronics nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************** */ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Class/AUDIO/Inc/usbd_audio.h b/system/Middlewares/ST/STM32_USB_Device_Library/Class/AUDIO/Inc/usbd_audio.h index aa29ce48a6..a97003b674 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Class/AUDIO/Inc/usbd_audio.h +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Class/AUDIO/Inc/usbd_audio.h @@ -1,211 +1,211 @@ -/** - ****************************************************************************** - * @file usbd_audio.h - * @author MCD Application Team - * @brief header file for the usbd_audio.c file. - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_AUDIO_H -#define __USB_AUDIO_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_ioreq.h" - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - -/** @defgroup USBD_AUDIO - * @brief This file is the Header file for usbd_audio.c - * @{ - */ - - -/** @defgroup USBD_AUDIO_Exported_Defines - * @{ - */ -#ifndef USBD_AUDIO_FREQ -/* AUDIO Class Config */ -#define USBD_AUDIO_FREQ 48000U -#endif /* USBD_AUDIO_FREQ */ - -#ifndef USBD_MAX_NUM_INTERFACES -#define USBD_MAX_NUM_INTERFACES 1U -#endif /* USBD_AUDIO_FREQ */ - -#define AUDIO_OUT_EP 0x01U -#define USB_AUDIO_CONFIG_DESC_SIZ 0x6DU -#define AUDIO_INTERFACE_DESC_SIZE 0x09U -#define USB_AUDIO_DESC_SIZ 0x09U -#define AUDIO_STANDARD_ENDPOINT_DESC_SIZE 0x09U -#define AUDIO_STREAMING_ENDPOINT_DESC_SIZE 0x07U - -#define AUDIO_DESCRIPTOR_TYPE 0x21U -#define USB_DEVICE_CLASS_AUDIO 0x01U -#define AUDIO_SUBCLASS_AUDIOCONTROL 0x01U -#define AUDIO_SUBCLASS_AUDIOSTREAMING 0x02U -#define AUDIO_PROTOCOL_UNDEFINED 0x00U -#define AUDIO_STREAMING_GENERAL 0x01U -#define AUDIO_STREAMING_FORMAT_TYPE 0x02U - -/* Audio Descriptor Types */ -#define AUDIO_INTERFACE_DESCRIPTOR_TYPE 0x24U -#define AUDIO_ENDPOINT_DESCRIPTOR_TYPE 0x25U - -/* Audio Control Interface Descriptor Subtypes */ -#define AUDIO_CONTROL_HEADER 0x01U -#define AUDIO_CONTROL_INPUT_TERMINAL 0x02U -#define AUDIO_CONTROL_OUTPUT_TERMINAL 0x03U -#define AUDIO_CONTROL_FEATURE_UNIT 0x06U - -#define AUDIO_INPUT_TERMINAL_DESC_SIZE 0x0CU -#define AUDIO_OUTPUT_TERMINAL_DESC_SIZE 0x09U -#define AUDIO_STREAMING_INTERFACE_DESC_SIZE 0x07U - -#define AUDIO_CONTROL_MUTE 0x0001U - -#define AUDIO_FORMAT_TYPE_I 0x01U -#define AUDIO_FORMAT_TYPE_III 0x03U - -#define AUDIO_ENDPOINT_GENERAL 0x01U - -#define AUDIO_REQ_GET_CUR 0x81U -#define AUDIO_REQ_SET_CUR 0x01U - -#define AUDIO_OUT_STREAMING_CTRL 0x02U - - -#define AUDIO_OUT_PACKET (uint16_t)(((USBD_AUDIO_FREQ * 2U * 2U) / 1000U)) -#define AUDIO_DEFAULT_VOLUME 70U - -/* Number of sub-packets in the audio transfer buffer. You can modify this value but always make sure - that it is an even number and higher than 3 */ -#define AUDIO_OUT_PACKET_NUM 80U -/* Total size of the audio transfer buffer */ -#define AUDIO_TOTAL_BUF_SIZE ((uint16_t)(AUDIO_OUT_PACKET * AUDIO_OUT_PACKET_NUM)) - - /* Audio Commands enumeration */ -typedef enum -{ - AUDIO_CMD_START = 1, - AUDIO_CMD_PLAY, - AUDIO_CMD_STOP, -}AUDIO_CMD_TypeDef; - - -typedef enum -{ - AUDIO_OFFSET_NONE = 0, - AUDIO_OFFSET_HALF, - AUDIO_OFFSET_FULL, - AUDIO_OFFSET_UNKNOWN, -} -AUDIO_OffsetTypeDef; -/** - * @} - */ - - -/** @defgroup USBD_CORE_Exported_TypesDefinitions - * @{ - */ - typedef struct -{ - uint8_t cmd; - uint8_t data[USB_MAX_EP0_SIZE]; - uint8_t len; - uint8_t unit; -} -USBD_AUDIO_ControlTypeDef; - - - -typedef struct -{ - uint32_t alt_setting; - uint8_t buffer[AUDIO_TOTAL_BUF_SIZE]; - AUDIO_OffsetTypeDef offset; - uint8_t rd_enable; - uint16_t rd_ptr; - uint16_t wr_ptr; - USBD_AUDIO_ControlTypeDef control; -} -USBD_AUDIO_HandleTypeDef; - - -typedef struct -{ - int8_t (*Init) (uint32_t AudioFreq, uint32_t Volume, uint32_t options); - int8_t (*DeInit) (uint32_t options); - int8_t (*AudioCmd) (uint8_t* pbuf, uint32_t size, uint8_t cmd); - int8_t (*VolumeCtl) (uint8_t vol); - int8_t (*MuteCtl) (uint8_t cmd); - int8_t (*PeriodicTC) (uint8_t cmd); - int8_t (*GetState) (void); -}USBD_AUDIO_ItfTypeDef; -/** - * @} - */ - - - -/** @defgroup USBD_CORE_Exported_Macros - * @{ - */ - -/** - * @} - */ - -/** @defgroup USBD_CORE_Exported_Variables - * @{ - */ - -extern USBD_ClassTypeDef USBD_AUDIO; -#define USBD_AUDIO_CLASS &USBD_AUDIO -/** - * @} - */ - -/** @defgroup USB_CORE_Exported_Functions - * @{ - */ -uint8_t USBD_AUDIO_RegisterInterface (USBD_HandleTypeDef *pdev, - USBD_AUDIO_ItfTypeDef *fops); - -void USBD_AUDIO_Sync (USBD_HandleTypeDef *pdev, AUDIO_OffsetTypeDef offset); -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USB_AUDIO_H */ -/** - * @} - */ - -/** - * @} - */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_audio.h + * @author MCD Application Team + * @brief header file for the usbd_audio.c file. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USB_AUDIO_H +#define __USB_AUDIO_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_ioreq.h" + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + +/** @defgroup USBD_AUDIO + * @brief This file is the Header file for usbd_audio.c + * @{ + */ + + +/** @defgroup USBD_AUDIO_Exported_Defines + * @{ + */ +#ifndef USBD_AUDIO_FREQ +/* AUDIO Class Config */ +#define USBD_AUDIO_FREQ 48000U +#endif /* USBD_AUDIO_FREQ */ + +#ifndef USBD_MAX_NUM_INTERFACES +#define USBD_MAX_NUM_INTERFACES 1U +#endif /* USBD_AUDIO_FREQ */ + +#define AUDIO_OUT_EP 0x01U +#define USB_AUDIO_CONFIG_DESC_SIZ 0x6DU +#define AUDIO_INTERFACE_DESC_SIZE 0x09U +#define USB_AUDIO_DESC_SIZ 0x09U +#define AUDIO_STANDARD_ENDPOINT_DESC_SIZE 0x09U +#define AUDIO_STREAMING_ENDPOINT_DESC_SIZE 0x07U + +#define AUDIO_DESCRIPTOR_TYPE 0x21U +#define USB_DEVICE_CLASS_AUDIO 0x01U +#define AUDIO_SUBCLASS_AUDIOCONTROL 0x01U +#define AUDIO_SUBCLASS_AUDIOSTREAMING 0x02U +#define AUDIO_PROTOCOL_UNDEFINED 0x00U +#define AUDIO_STREAMING_GENERAL 0x01U +#define AUDIO_STREAMING_FORMAT_TYPE 0x02U + +/* Audio Descriptor Types */ +#define AUDIO_INTERFACE_DESCRIPTOR_TYPE 0x24U +#define AUDIO_ENDPOINT_DESCRIPTOR_TYPE 0x25U + +/* Audio Control Interface Descriptor Subtypes */ +#define AUDIO_CONTROL_HEADER 0x01U +#define AUDIO_CONTROL_INPUT_TERMINAL 0x02U +#define AUDIO_CONTROL_OUTPUT_TERMINAL 0x03U +#define AUDIO_CONTROL_FEATURE_UNIT 0x06U + +#define AUDIO_INPUT_TERMINAL_DESC_SIZE 0x0CU +#define AUDIO_OUTPUT_TERMINAL_DESC_SIZE 0x09U +#define AUDIO_STREAMING_INTERFACE_DESC_SIZE 0x07U + +#define AUDIO_CONTROL_MUTE 0x0001U + +#define AUDIO_FORMAT_TYPE_I 0x01U +#define AUDIO_FORMAT_TYPE_III 0x03U + +#define AUDIO_ENDPOINT_GENERAL 0x01U + +#define AUDIO_REQ_GET_CUR 0x81U +#define AUDIO_REQ_SET_CUR 0x01U + +#define AUDIO_OUT_STREAMING_CTRL 0x02U + + +#define AUDIO_OUT_PACKET (uint16_t)(((USBD_AUDIO_FREQ * 2U * 2U) / 1000U)) +#define AUDIO_DEFAULT_VOLUME 70U + +/* Number of sub-packets in the audio transfer buffer. You can modify this value but always make sure + that it is an even number and higher than 3 */ +#define AUDIO_OUT_PACKET_NUM 80U +/* Total size of the audio transfer buffer */ +#define AUDIO_TOTAL_BUF_SIZE ((uint16_t)(AUDIO_OUT_PACKET * AUDIO_OUT_PACKET_NUM)) + + /* Audio Commands enumeration */ +typedef enum +{ + AUDIO_CMD_START = 1, + AUDIO_CMD_PLAY, + AUDIO_CMD_STOP, +}AUDIO_CMD_TypeDef; + + +typedef enum +{ + AUDIO_OFFSET_NONE = 0, + AUDIO_OFFSET_HALF, + AUDIO_OFFSET_FULL, + AUDIO_OFFSET_UNKNOWN, +} +AUDIO_OffsetTypeDef; +/** + * @} + */ + + +/** @defgroup USBD_CORE_Exported_TypesDefinitions + * @{ + */ + typedef struct +{ + uint8_t cmd; + uint8_t data[USB_MAX_EP0_SIZE]; + uint8_t len; + uint8_t unit; +} +USBD_AUDIO_ControlTypeDef; + + + +typedef struct +{ + uint32_t alt_setting; + uint8_t buffer[AUDIO_TOTAL_BUF_SIZE]; + AUDIO_OffsetTypeDef offset; + uint8_t rd_enable; + uint16_t rd_ptr; + uint16_t wr_ptr; + USBD_AUDIO_ControlTypeDef control; +} +USBD_AUDIO_HandleTypeDef; + + +typedef struct +{ + int8_t (*Init) (uint32_t AudioFreq, uint32_t Volume, uint32_t options); + int8_t (*DeInit) (uint32_t options); + int8_t (*AudioCmd) (uint8_t* pbuf, uint32_t size, uint8_t cmd); + int8_t (*VolumeCtl) (uint8_t vol); + int8_t (*MuteCtl) (uint8_t cmd); + int8_t (*PeriodicTC) (uint8_t cmd); + int8_t (*GetState) (void); +}USBD_AUDIO_ItfTypeDef; +/** + * @} + */ + + + +/** @defgroup USBD_CORE_Exported_Macros + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_CORE_Exported_Variables + * @{ + */ + +extern USBD_ClassTypeDef USBD_AUDIO; +#define USBD_AUDIO_CLASS &USBD_AUDIO +/** + * @} + */ + +/** @defgroup USB_CORE_Exported_Functions + * @{ + */ +uint8_t USBD_AUDIO_RegisterInterface (USBD_HandleTypeDef *pdev, + USBD_AUDIO_ItfTypeDef *fops); + +void USBD_AUDIO_Sync (USBD_HandleTypeDef *pdev, AUDIO_OffsetTypeDef offset); +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USB_AUDIO_H */ +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Class/AUDIO/Inc/usbd_audio_if_template.h b/system/Middlewares/ST/STM32_USB_Device_Library/Class/AUDIO/Inc/usbd_audio_if_template.h index 8437087aa3..60a9c1224d 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Class/AUDIO/Inc/usbd_audio_if_template.h +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Class/AUDIO/Inc/usbd_audio_if_template.h @@ -1,45 +1,45 @@ -/** - ****************************************************************************** - * @file usbd_audio_if_template.h - * @author MCD Application Team - * @brief Header for usbd_audio_if_template.c file. - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USBD_AUDIO_IF_TEMPLATE_H -#define __USBD_AUDIO_IF_TEMPLATE_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_audio.h" - -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ - -extern USBD_AUDIO_ItfTypeDef USBD_AUDIO_Template_fops; - -/* Exported macro ------------------------------------------------------------*/ -/* Exported functions ------------------------------------------------------- */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USBD_AUDIO_IF_TEMPLATE_H */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_audio_if_template.h + * @author MCD Application Team + * @brief Header for usbd_audio_if_template.c file. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USBD_AUDIO_IF_TEMPLATE_H +#define __USBD_AUDIO_IF_TEMPLATE_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_audio.h" + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ + +extern USBD_AUDIO_ItfTypeDef USBD_AUDIO_Template_fops; + +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USBD_AUDIO_IF_TEMPLATE_H */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc/usbd_cdc.h b/system/Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc/usbd_cdc.h index 1cddfeaf8b..f8d1e1d9d8 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc/usbd_cdc.h +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc/usbd_cdc.h @@ -1,179 +1,176 @@ -/** - ****************************************************************************** - * @file usbd_cdc.h - * @author MCD Application Team - * @brief header file for the usbd_cdc.c file. - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_CDC_H -#define __USB_CDC_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_ioreq.h" - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - -/** @defgroup usbd_cdc - * @brief This file is the Header file for usbd_cdc.c - * @{ - */ - - -/** @defgroup usbd_cdc_Exported_Defines - * @{ - */ -#define CDC_IN_EP 0x81U /* EP1 for data IN */ -#define CDC_OUT_EP 0x01U /* EP1 for data OUT */ -#define CDC_CMD_EP 0x82U /* EP2 for CDC commands */ - -#ifndef CDC_HS_BINTERVAL - #define CDC_HS_BINTERVAL 0x10U -#endif /* CDC_HS_BINTERVAL */ - -#ifndef CDC_FS_BINTERVAL - #define CDC_FS_BINTERVAL 0x10U -#endif /* CDC_FS_BINTERVAL */ - -/* CDC Endpoints parameters: you can fine tune these values depending on the needed baudrates and performance. */ -#define CDC_DATA_HS_MAX_PACKET_SIZE 512U /* Endpoint IN & OUT Packet size */ -#define CDC_DATA_FS_MAX_PACKET_SIZE 64U /* Endpoint IN & OUT Packet size */ -#define CDC_CMD_PACKET_SIZE 8U /* Control Endpoint Packet size */ - -#define USB_CDC_CONFIG_DESC_SIZ 67U -#define CDC_DATA_HS_IN_PACKET_SIZE CDC_DATA_HS_MAX_PACKET_SIZE -#define CDC_DATA_HS_OUT_PACKET_SIZE CDC_DATA_HS_MAX_PACKET_SIZE - -#define CDC_DATA_FS_IN_PACKET_SIZE CDC_DATA_FS_MAX_PACKET_SIZE -#define CDC_DATA_FS_OUT_PACKET_SIZE CDC_DATA_FS_MAX_PACKET_SIZE - -/*---------------------------------------------------------------------*/ -/* CDC definitions */ -/*---------------------------------------------------------------------*/ -#define CDC_SEND_ENCAPSULATED_COMMAND 0x00U -#define CDC_GET_ENCAPSULATED_RESPONSE 0x01U -#define CDC_SET_COMM_FEATURE 0x02U -#define CDC_GET_COMM_FEATURE 0x03U -#define CDC_CLEAR_COMM_FEATURE 0x04U -#define CDC_SET_LINE_CODING 0x20U -#define CDC_GET_LINE_CODING 0x21U -#define CDC_SET_CONTROL_LINE_STATE 0x22U -#define CDC_SEND_BREAK 0x23U - -/** - * @} - */ - - -/** @defgroup USBD_CORE_Exported_TypesDefinitions - * @{ - */ - -/** - * @} - */ -typedef struct -{ - uint32_t bitrate; - uint8_t format; - uint8_t paritytype; - uint8_t datatype; -}USBD_CDC_LineCodingTypeDef; - -typedef struct _USBD_CDC_Itf -{ - int8_t (* Init) (void); - int8_t (* DeInit) (void); - int8_t (* Control) (uint8_t cmd, uint8_t* pbuf, uint16_t length); - int8_t (* Receive) (uint8_t* Buf, uint32_t *Len); - -}USBD_CDC_ItfTypeDef; - - -typedef struct -{ - uint32_t data[CDC_DATA_HS_MAX_PACKET_SIZE / 4U]; /* Force 32bits alignment */ - uint8_t CmdOpCode; - uint8_t CmdLength; - uint8_t *RxBuffer; - uint8_t *TxBuffer; - uint32_t RxLength; - uint32_t TxLength; - - __IO uint32_t TxState; - __IO uint32_t RxState; -} -USBD_CDC_HandleTypeDef; - - - -/** @defgroup USBD_CORE_Exported_Macros - * @{ - */ - -/** - * @} - */ - -/** @defgroup USBD_CORE_Exported_Variables - * @{ - */ - -extern USBD_ClassTypeDef USBD_CDC; -#define USBD_CDC_CLASS &USBD_CDC -/** - * @} - */ - -/** @defgroup USB_CORE_Exported_Functions - * @{ - */ -uint8_t USBD_CDC_RegisterInterface (USBD_HandleTypeDef *pdev, - USBD_CDC_ItfTypeDef *fops); - -uint8_t USBD_CDC_SetTxBuffer (USBD_HandleTypeDef *pdev, - uint8_t *pbuff, - uint16_t length); - -uint8_t USBD_CDC_SetRxBuffer (USBD_HandleTypeDef *pdev, - uint8_t *pbuff); - -uint8_t USBD_CDC_ReceivePacket (USBD_HandleTypeDef *pdev); - -uint8_t USBD_CDC_TransmitPacket (USBD_HandleTypeDef *pdev); -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USB_CDC_H */ -/** - * @} - */ - -/** - * @} - */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_cdc.h + * @author MCD Application Team + * @brief header file for the usbd_cdc.c file. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USB_CDC_H +#define __USB_CDC_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_ioreq.h" + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + +/** @defgroup usbd_cdc + * @brief This file is the Header file for usbd_cdc.c + * @{ + */ + +/** @defgroup usbd_cdc_Exported_Defines + * @{ + */ + +#define CDC_IN_EP 0x81U /* EP1 for data IN */ +#define CDC_OUT_EP 0x01U /* EP1 for data OUT */ +#define CDC_CMD_EP 0x82U /* EP2 for CDC commands */ + +#ifndef CDC_HS_BINTERVAL +#define CDC_HS_BINTERVAL 0x10U +#endif /* CDC_HS_BINTERVAL */ + +#ifndef CDC_FS_BINTERVAL +#define CDC_FS_BINTERVAL 0x10U +#endif /* CDC_FS_BINTERVAL */ + +/* CDC Endpoints parameters: you can fine tune these values depending on the + * needed baudrates and performance. */ +#define CDC_DATA_HS_MAX_PACKET_SIZE 512U /* Endpoint IN & OUT Packet size */ +#define CDC_DATA_FS_MAX_PACKET_SIZE 64U /* Endpoint IN & OUT Packet size */ +#define CDC_CMD_PACKET_SIZE 8U /* Control Endpoint Packet size */ + +#define USB_CDC_CONFIG_DESC_SIZ 67U +#define CDC_DATA_HS_IN_PACKET_SIZE CDC_DATA_HS_MAX_PACKET_SIZE +#define CDC_DATA_HS_OUT_PACKET_SIZE CDC_DATA_HS_MAX_PACKET_SIZE + +#define CDC_DATA_FS_IN_PACKET_SIZE CDC_DATA_FS_MAX_PACKET_SIZE +#define CDC_DATA_FS_OUT_PACKET_SIZE CDC_DATA_FS_MAX_PACKET_SIZE + +/*---------------------------------------------------------------------*/ +/* CDC definitions */ +/*---------------------------------------------------------------------*/ +#define CDC_SEND_ENCAPSULATED_COMMAND 0x00U +#define CDC_GET_ENCAPSULATED_RESPONSE 0x01U +#define CDC_SET_COMM_FEATURE 0x02U +#define CDC_GET_COMM_FEATURE 0x03U +#define CDC_CLEAR_COMM_FEATURE 0x04U +#define CDC_SET_LINE_CODING 0x20U +#define CDC_GET_LINE_CODING 0x21U +#define CDC_SET_CONTROL_LINE_STATE 0x22U +#define CDC_SEND_BREAK 0x23U + +/** + * @} + */ + +/** @defgroup USBD_CORE_Exported_TypesDefinitions + * @{ + */ + +/** + * @} + */ + +typedef struct +{ + uint32_t bitrate; + uint8_t format; + uint8_t paritytype; + uint8_t datatype; +} USBD_CDC_LineCodingTypeDef; + +typedef struct _USBD_CDC_Itf +{ + int8_t (*Init)(void); + int8_t (*DeInit)(void); + int8_t (*Control)(uint8_t cmd, uint8_t *pbuf, uint16_t length); + int8_t (*Receive)(uint8_t *Buf, uint32_t *Len); + +} USBD_CDC_ItfTypeDef; + +typedef struct +{ + uint32_t data[CDC_DATA_HS_MAX_PACKET_SIZE / 4U]; /* Force 32bits alignment */ + uint8_t CmdOpCode; + uint8_t CmdLength; + uint8_t *RxBuffer; + uint8_t *TxBuffer; + uint32_t RxLength; + uint32_t TxLength; + + __IO uint32_t TxState; + __IO uint32_t RxState; +} USBD_CDC_HandleTypeDef; + +/** @defgroup USBD_CORE_Exported_Macros + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_CORE_Exported_Variables + * @{ + */ + +extern USBD_ClassTypeDef USBD_CDC; +#define USBD_CDC_CLASS &USBD_CDC +/** + * @} + */ + +/** @defgroup USB_CORE_Exported_Functions + * @{ + */ +uint8_t USBD_CDC_RegisterInterface(USBD_HandleTypeDef *pdev, + USBD_CDC_ItfTypeDef *fops); + +uint8_t USBD_CDC_SetTxBuffer(USBD_HandleTypeDef *pdev, + uint8_t *pbuff, + uint16_t length); + +uint8_t USBD_CDC_SetRxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff); + +uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev); + +uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev); + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USB_CDC_H */ +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc/usbd_cdc_if_template.h b/system/Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc/usbd_cdc_if_template.h index f0be9c13e4..240e5bf466 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc/usbd_cdc_if_template.h +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc/usbd_cdc_if_template.h @@ -1,45 +1,45 @@ -/** - ****************************************************************************** - * @file usbd_cdc_if_template.h - * @author MCD Application Team - * @brief Header for usbd_cdc_if_template.c file. - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USBD_CDC_IF_TEMPLATE_H -#define __USBD_CDC_IF_TEMPLATE_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_cdc.h" - -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ - -extern USBD_CDC_ItfTypeDef USBD_CDC_Template_fops; - -/* Exported macro ------------------------------------------------------------*/ -/* Exported functions ------------------------------------------------------- */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USBD_CDC_IF_TEMPLATE_H */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_cdc_if_template.h + * @author MCD Application Team + * @brief Header for usbd_cdc_if_template.c file. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USBD_CDC_IF_TEMPLATE_H +#define __USBD_CDC_IF_TEMPLATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_cdc.h" + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ + +extern USBD_CDC_ItfTypeDef USBD_CDC_Template_fops; + +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USBD_CDC_IF_TEMPLATE_H */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Src/usbd_cdc.c b/system/Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Src/usbd_cdc.c index 7f94e6c126..20c78e5e2d 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Src/usbd_cdc.c +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Src/usbd_cdc.c @@ -1,945 +1,930 @@ -/** - ****************************************************************************** - * @file usbd_cdc.c - * @author MCD Application Team - * @brief This file provides the high layer firmware functions to manage the - * following functionalities of the USB CDC Class: - * - Initialization and Configuration of high and low layer - * - Enumeration as CDC Device (and enumeration for each implemented memory interface) - * - OUT/IN data transfer - * - Command IN transfer (class requests management) - * - Error management - * - * @verbatim - * - * =================================================================== - * CDC Class Driver Description - * =================================================================== - * This driver manages the "Universal Serial Bus Class Definitions for Communications Devices - * Revision 1.2 November 16, 2007" and the sub-protocol specification of "Universal Serial Bus - * Communications Class Subclass Specification for PSTN Devices Revision 1.2 February 9, 2007" - * This driver implements the following aspects of the specification: - * - Device descriptor management - * - Configuration descriptor management - * - Enumeration as CDC device with 2 data endpoints (IN and OUT) and 1 command endpoint (IN) - * - Requests management (as described in section 6.2 in specification) - * - Abstract Control Model compliant - * - Union Functional collection (using 1 IN endpoint for control) - * - Data interface class - * - * These aspects may be enriched or modified for a specific user application. - * - * This driver doesn't implement the following aspects of the specification - * (but it is possible to manage these features with some modifications on this driver): - * - Any class-specific aspect relative to communication classes should be managed by user application. - * - All communication classes other than PSTN are not managed - * - * @endverbatim - * - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - - /* BSPDependencies - - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" - - "stm32xxxxx_{eval}{discovery}_io.c" - EndBSPDependencies */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_cdc.h" -#include "usbd_ctlreq.h" - - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - - -/** @defgroup USBD_CDC - * @brief usbd core module - * @{ - */ - -/** @defgroup USBD_CDC_Private_TypesDefinitions - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBD_CDC_Private_Defines - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBD_CDC_Private_Macros - * @{ - */ - -/** - * @} - */ - - -/** @defgroup USBD_CDC_Private_FunctionPrototypes - * @{ - */ - - -static uint8_t USBD_CDC_Init (USBD_HandleTypeDef *pdev, - uint8_t cfgidx); - -static uint8_t USBD_CDC_DeInit (USBD_HandleTypeDef *pdev, - uint8_t cfgidx); - -static uint8_t USBD_CDC_Setup (USBD_HandleTypeDef *pdev, - USBD_SetupReqTypedef *req); - -static uint8_t USBD_CDC_DataIn (USBD_HandleTypeDef *pdev, - uint8_t epnum); - -static uint8_t USBD_CDC_DataOut (USBD_HandleTypeDef *pdev, - uint8_t epnum); - -static uint8_t USBD_CDC_EP0_RxReady (USBD_HandleTypeDef *pdev); - -static uint8_t *USBD_CDC_GetFSCfgDesc (uint16_t *length); - -static uint8_t *USBD_CDC_GetHSCfgDesc (uint16_t *length); - -static uint8_t *USBD_CDC_GetOtherSpeedCfgDesc (uint16_t *length); - -static uint8_t *USBD_CDC_GetOtherSpeedCfgDesc (uint16_t *length); - -uint8_t *USBD_CDC_GetDeviceQualifierDescriptor (uint16_t *length); - -/* USB Standard Device Descriptor */ -__ALIGN_BEGIN static uint8_t USBD_CDC_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END = -{ - USB_LEN_DEV_QUALIFIER_DESC, - USB_DESC_TYPE_DEVICE_QUALIFIER, - 0x00, - 0x02, - 0x00, - 0x00, - 0x00, - 0x40, - 0x01, - 0x00, -}; - -/** - * @} - */ - -/** @defgroup USBD_CDC_Private_Variables - * @{ - */ - - -/* CDC interface class callbacks structure */ -USBD_ClassTypeDef USBD_CDC = -{ - USBD_CDC_Init, - USBD_CDC_DeInit, - USBD_CDC_Setup, - NULL, /* EP0_TxSent, */ - USBD_CDC_EP0_RxReady, - USBD_CDC_DataIn, - USBD_CDC_DataOut, - NULL, - NULL, - NULL, - USBD_CDC_GetHSCfgDesc, - USBD_CDC_GetFSCfgDesc, - USBD_CDC_GetOtherSpeedCfgDesc, - USBD_CDC_GetDeviceQualifierDescriptor, -}; - -/* USB CDC device Configuration Descriptor */ -__ALIGN_BEGIN uint8_t USBD_CDC_CfgHSDesc[USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END = -{ - /*Configuration Descriptor*/ - 0x09, /* bLength: Configuration Descriptor size */ - USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ - USB_CDC_CONFIG_DESC_SIZ, /* wTotalLength:no of returned bytes */ - 0x00, - 0x02, /* bNumInterfaces: 2 interface */ - 0x01, /* bConfigurationValue: Configuration value */ - 0x00, /* iConfiguration: Index of string descriptor describing the configuration */ - 0xC0, /* bmAttributes: self powered */ - 0x32, /* MaxPower 0 mA */ - - /*---------------------------------------------------------------------------*/ - - /*Interface Descriptor */ - 0x09, /* bLength: Interface Descriptor size */ - USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */ - /* Interface descriptor type */ - 0x00, /* bInterfaceNumber: Number of Interface */ - 0x00, /* bAlternateSetting: Alternate setting */ - 0x01, /* bNumEndpoints: One endpoints used */ - 0x02, /* bInterfaceClass: Communication Interface Class */ - 0x02, /* bInterfaceSubClass: Abstract Control Model */ - 0x01, /* bInterfaceProtocol: Common AT commands */ - 0x00, /* iInterface: */ - - /*Header Functional Descriptor*/ - 0x05, /* bLength: Endpoint Descriptor size */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x00, /* bDescriptorSubtype: Header Func Desc */ - 0x10, /* bcdCDC: spec release number */ - 0x01, - - /*Call Management Functional Descriptor*/ - 0x05, /* bFunctionLength */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x01, /* bDescriptorSubtype: Call Management Func Desc */ - 0x00, /* bmCapabilities: D0+D1 */ - 0x01, /* bDataInterface: 1 */ - - /*ACM Functional Descriptor*/ - 0x04, /* bFunctionLength */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x02, /* bDescriptorSubtype: Abstract Control Management desc */ - 0x02, /* bmCapabilities */ - - /*Union Functional Descriptor*/ - 0x05, /* bFunctionLength */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x06, /* bDescriptorSubtype: Union func desc */ - 0x00, /* bMasterInterface: Communication class interface */ - 0x01, /* bSlaveInterface0: Data Class Interface */ - - /*Endpoint 2 Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ - CDC_CMD_EP, /* bEndpointAddress */ - 0x03, /* bmAttributes: Interrupt */ - LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize: */ - HIBYTE(CDC_CMD_PACKET_SIZE), - CDC_HS_BINTERVAL, /* bInterval: */ - /*---------------------------------------------------------------------------*/ - - /*Data class interface descriptor*/ - 0x09, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_INTERFACE, /* bDescriptorType: */ - 0x01, /* bInterfaceNumber: Number of Interface */ - 0x00, /* bAlternateSetting: Alternate setting */ - 0x02, /* bNumEndpoints: Two endpoints used */ - 0x0A, /* bInterfaceClass: CDC */ - 0x00, /* bInterfaceSubClass: */ - 0x00, /* bInterfaceProtocol: */ - 0x00, /* iInterface: */ - - /*Endpoint OUT Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ - CDC_OUT_EP, /* bEndpointAddress */ - 0x02, /* bmAttributes: Bulk */ - LOBYTE(CDC_DATA_HS_MAX_PACKET_SIZE), /* wMaxPacketSize: */ - HIBYTE(CDC_DATA_HS_MAX_PACKET_SIZE), - 0x00, /* bInterval: ignore for Bulk transfer */ - - /*Endpoint IN Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ - CDC_IN_EP, /* bEndpointAddress */ - 0x02, /* bmAttributes: Bulk */ - LOBYTE(CDC_DATA_HS_MAX_PACKET_SIZE), /* wMaxPacketSize: */ - HIBYTE(CDC_DATA_HS_MAX_PACKET_SIZE), - 0x00 /* bInterval: ignore for Bulk transfer */ -} ; - - -/* USB CDC device Configuration Descriptor */ -__ALIGN_BEGIN uint8_t USBD_CDC_CfgFSDesc[USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END = -{ - /*Configuration Descriptor*/ - 0x09, /* bLength: Configuration Descriptor size */ - USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ - USB_CDC_CONFIG_DESC_SIZ, /* wTotalLength:no of returned bytes */ - 0x00, - 0x02, /* bNumInterfaces: 2 interface */ - 0x01, /* bConfigurationValue: Configuration value */ - 0x00, /* iConfiguration: Index of string descriptor describing the configuration */ - 0xC0, /* bmAttributes: self powered */ - 0x32, /* MaxPower 0 mA */ - - /*---------------------------------------------------------------------------*/ - - /*Interface Descriptor */ - 0x09, /* bLength: Interface Descriptor size */ - USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */ - /* Interface descriptor type */ - 0x00, /* bInterfaceNumber: Number of Interface */ - 0x00, /* bAlternateSetting: Alternate setting */ - 0x01, /* bNumEndpoints: One endpoints used */ - 0x02, /* bInterfaceClass: Communication Interface Class */ - 0x02, /* bInterfaceSubClass: Abstract Control Model */ - 0x01, /* bInterfaceProtocol: Common AT commands */ - 0x00, /* iInterface: */ - - /*Header Functional Descriptor*/ - 0x05, /* bLength: Endpoint Descriptor size */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x00, /* bDescriptorSubtype: Header Func Desc */ - 0x10, /* bcdCDC: spec release number */ - 0x01, - - /*Call Management Functional Descriptor*/ - 0x05, /* bFunctionLength */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x01, /* bDescriptorSubtype: Call Management Func Desc */ - 0x00, /* bmCapabilities: D0+D1 */ - 0x01, /* bDataInterface: 1 */ - - /*ACM Functional Descriptor*/ - 0x04, /* bFunctionLength */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x02, /* bDescriptorSubtype: Abstract Control Management desc */ - 0x02, /* bmCapabilities */ - - /*Union Functional Descriptor*/ - 0x05, /* bFunctionLength */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x06, /* bDescriptorSubtype: Union func desc */ - 0x00, /* bMasterInterface: Communication class interface */ - 0x01, /* bSlaveInterface0: Data Class Interface */ - - /*Endpoint 2 Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ - CDC_CMD_EP, /* bEndpointAddress */ - 0x03, /* bmAttributes: Interrupt */ - LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize: */ - HIBYTE(CDC_CMD_PACKET_SIZE), - CDC_FS_BINTERVAL, /* bInterval: */ - /*---------------------------------------------------------------------------*/ - - /*Data class interface descriptor*/ - 0x09, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_INTERFACE, /* bDescriptorType: */ - 0x01, /* bInterfaceNumber: Number of Interface */ - 0x00, /* bAlternateSetting: Alternate setting */ - 0x02, /* bNumEndpoints: Two endpoints used */ - 0x0A, /* bInterfaceClass: CDC */ - 0x00, /* bInterfaceSubClass: */ - 0x00, /* bInterfaceProtocol: */ - 0x00, /* iInterface: */ - - /*Endpoint OUT Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ - CDC_OUT_EP, /* bEndpointAddress */ - 0x02, /* bmAttributes: Bulk */ - LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), /* wMaxPacketSize: */ - HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), - 0x00, /* bInterval: ignore for Bulk transfer */ - - /*Endpoint IN Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ - CDC_IN_EP, /* bEndpointAddress */ - 0x02, /* bmAttributes: Bulk */ - LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), /* wMaxPacketSize: */ - HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), - 0x00 /* bInterval: ignore for Bulk transfer */ -} ; - -__ALIGN_BEGIN uint8_t USBD_CDC_OtherSpeedCfgDesc[USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END = -{ - 0x09, /* bLength: Configuation Descriptor size */ - USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION, - USB_CDC_CONFIG_DESC_SIZ, - 0x00, - 0x02, /* bNumInterfaces: 2 interfaces */ - 0x01, /* bConfigurationValue: */ - 0x04, /* iConfiguration: */ - 0xC0, /* bmAttributes: */ - 0x32, /* MaxPower 100 mA */ - - /*Interface Descriptor */ - 0x09, /* bLength: Interface Descriptor size */ - USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */ - /* Interface descriptor type */ - 0x00, /* bInterfaceNumber: Number of Interface */ - 0x00, /* bAlternateSetting: Alternate setting */ - 0x01, /* bNumEndpoints: One endpoints used */ - 0x02, /* bInterfaceClass: Communication Interface Class */ - 0x02, /* bInterfaceSubClass: Abstract Control Model */ - 0x01, /* bInterfaceProtocol: Common AT commands */ - 0x00, /* iInterface: */ - - /*Header Functional Descriptor*/ - 0x05, /* bLength: Endpoint Descriptor size */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x00, /* bDescriptorSubtype: Header Func Desc */ - 0x10, /* bcdCDC: spec release number */ - 0x01, - - /*Call Management Functional Descriptor*/ - 0x05, /* bFunctionLength */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x01, /* bDescriptorSubtype: Call Management Func Desc */ - 0x00, /* bmCapabilities: D0+D1 */ - 0x01, /* bDataInterface: 1 */ - - /*ACM Functional Descriptor*/ - 0x04, /* bFunctionLength */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x02, /* bDescriptorSubtype: Abstract Control Management desc */ - 0x02, /* bmCapabilities */ - - /*Union Functional Descriptor*/ - 0x05, /* bFunctionLength */ - 0x24, /* bDescriptorType: CS_INTERFACE */ - 0x06, /* bDescriptorSubtype: Union func desc */ - 0x00, /* bMasterInterface: Communication class interface */ - 0x01, /* bSlaveInterface0: Data Class Interface */ - - /*Endpoint 2 Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT , /* bDescriptorType: Endpoint */ - CDC_CMD_EP, /* bEndpointAddress */ - 0x03, /* bmAttributes: Interrupt */ - LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize: */ - HIBYTE(CDC_CMD_PACKET_SIZE), - CDC_FS_BINTERVAL, /* bInterval: */ - - /*---------------------------------------------------------------------------*/ - - /*Data class interface descriptor*/ - 0x09, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_INTERFACE, /* bDescriptorType: */ - 0x01, /* bInterfaceNumber: Number of Interface */ - 0x00, /* bAlternateSetting: Alternate setting */ - 0x02, /* bNumEndpoints: Two endpoints used */ - 0x0A, /* bInterfaceClass: CDC */ - 0x00, /* bInterfaceSubClass: */ - 0x00, /* bInterfaceProtocol: */ - 0x00, /* iInterface: */ - - /*Endpoint OUT Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ - CDC_OUT_EP, /* bEndpointAddress */ - 0x02, /* bmAttributes: Bulk */ - 0x40, /* wMaxPacketSize: */ - 0x00, - 0x00, /* bInterval: ignore for Bulk transfer */ - - /*Endpoint IN Descriptor*/ - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ - CDC_IN_EP, /* bEndpointAddress */ - 0x02, /* bmAttributes: Bulk */ - 0x40, /* wMaxPacketSize: */ - 0x00, - 0x00 /* bInterval */ -}; - -/** - * @} - */ - -/** @defgroup USBD_CDC_Private_Functions - * @{ - */ - -/** - * @brief USBD_CDC_Init - * Initialize the CDC interface - * @param pdev: device instance - * @param cfgidx: Configuration index - * @retval status - */ -static uint8_t USBD_CDC_Init (USBD_HandleTypeDef *pdev, uint8_t cfgidx) -{ - uint8_t ret = 0U; - USBD_CDC_HandleTypeDef *hcdc; - - if(pdev->dev_speed == USBD_SPEED_HIGH) - { - /* Open EP IN */ - USBD_LL_OpenEP(pdev, CDC_IN_EP, USBD_EP_TYPE_BULK, - CDC_DATA_HS_IN_PACKET_SIZE); - - pdev->ep_in[CDC_IN_EP & 0xFU].is_used = 1U; - - /* Open EP OUT */ - USBD_LL_OpenEP(pdev, CDC_OUT_EP, USBD_EP_TYPE_BULK, - CDC_DATA_HS_OUT_PACKET_SIZE); - - pdev->ep_out[CDC_OUT_EP & 0xFU].is_used = 1U; - - } - else - { - /* Open EP IN */ - USBD_LL_OpenEP(pdev, CDC_IN_EP, USBD_EP_TYPE_BULK, - CDC_DATA_FS_IN_PACKET_SIZE); - - pdev->ep_in[CDC_IN_EP & 0xFU].is_used = 1U; - - /* Open EP OUT */ - USBD_LL_OpenEP(pdev, CDC_OUT_EP, USBD_EP_TYPE_BULK, - CDC_DATA_FS_OUT_PACKET_SIZE); - - pdev->ep_out[CDC_OUT_EP & 0xFU].is_used = 1U; - } - /* Open Command IN EP */ - USBD_LL_OpenEP(pdev, CDC_CMD_EP, USBD_EP_TYPE_INTR, CDC_CMD_PACKET_SIZE); - pdev->ep_in[CDC_CMD_EP & 0xFU].is_used = 1U; - - pdev->pClassData = USBD_malloc(sizeof (USBD_CDC_HandleTypeDef)); - - if(pdev->pClassData == NULL) - { - ret = 1U; - } - else - { - hcdc = (USBD_CDC_HandleTypeDef*) pdev->pClassData; - - /* Init physical Interface components */ - ((USBD_CDC_ItfTypeDef *)pdev->pUserData)->Init(); - - /* Init Xfer states */ - hcdc->TxState = 0U; - hcdc->RxState = 0U; - - if(pdev->dev_speed == USBD_SPEED_HIGH) - { - /* Prepare Out endpoint to receive next packet */ - USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, hcdc->RxBuffer, - CDC_DATA_HS_OUT_PACKET_SIZE); - } - else - { - /* Prepare Out endpoint to receive next packet */ - USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, hcdc->RxBuffer, - CDC_DATA_FS_OUT_PACKET_SIZE); - } - } - return ret; -} - -/** - * @brief USBD_CDC_Init - * DeInitialize the CDC layer - * @param pdev: device instance - * @param cfgidx: Configuration index - * @retval status - */ -static uint8_t USBD_CDC_DeInit (USBD_HandleTypeDef *pdev, uint8_t cfgidx) -{ - uint8_t ret = 0U; - - /* Close EP IN */ - USBD_LL_CloseEP(pdev, CDC_IN_EP); - pdev->ep_in[CDC_IN_EP & 0xFU].is_used = 0U; - - /* Close EP OUT */ - USBD_LL_CloseEP(pdev, CDC_OUT_EP); - pdev->ep_out[CDC_OUT_EP & 0xFU].is_used = 0U; - - /* Close Command IN EP */ - USBD_LL_CloseEP(pdev, CDC_CMD_EP); - pdev->ep_in[CDC_CMD_EP & 0xFU].is_used = 0U; - - /* DeInit physical Interface components */ - if(pdev->pClassData != NULL) - { - ((USBD_CDC_ItfTypeDef *)pdev->pUserData)->DeInit(); - USBD_free(pdev->pClassData); - pdev->pClassData = NULL; - } - - return ret; -} - -/** - * @brief USBD_CDC_Setup - * Handle the CDC specific requests - * @param pdev: instance - * @param req: usb requests - * @retval status - */ -static uint8_t USBD_CDC_Setup (USBD_HandleTypeDef *pdev, - USBD_SetupReqTypedef *req) -{ - USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*) pdev->pClassData; - uint8_t ifalt = 0U; - uint16_t status_info = 0U; - uint8_t ret = USBD_OK; - - switch (req->bmRequest & USB_REQ_TYPE_MASK) - { - case USB_REQ_TYPE_CLASS : - if (req->wLength) - { - if (req->bmRequest & 0x80U) - { - ((USBD_CDC_ItfTypeDef *)pdev->pUserData)->Control(req->bRequest, - (uint8_t *)(void *)hcdc->data, - req->wLength); - - USBD_CtlSendData (pdev, (uint8_t *)(void *)hcdc->data, req->wLength); - } - else - { - hcdc->CmdOpCode = req->bRequest; - hcdc->CmdLength = (uint8_t)req->wLength; - - USBD_CtlPrepareRx (pdev, (uint8_t *)(void *)hcdc->data, req->wLength); - } - } - else - { - ((USBD_CDC_ItfTypeDef *)pdev->pUserData)->Control(req->bRequest, - (uint8_t *)(void *)req, 0U); - } - break; - - case USB_REQ_TYPE_STANDARD: - switch (req->bRequest) - { - case USB_REQ_GET_STATUS: - if (pdev->dev_state == USBD_STATE_CONFIGURED) - { - USBD_CtlSendData (pdev, (uint8_t *)(void *)&status_info, 2U); - } - else - { - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - } - break; - - case USB_REQ_GET_INTERFACE: - if (pdev->dev_state == USBD_STATE_CONFIGURED) - { - USBD_CtlSendData (pdev, &ifalt, 1U); - } - else - { - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - } - break; - - case USB_REQ_SET_INTERFACE: - if (pdev->dev_state != USBD_STATE_CONFIGURED) - { - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - } - break; - - default: - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - break; - } - break; - - default: - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - break; - } - - return ret; -} - -/** - * @brief USBD_CDC_DataIn - * Data sent on non-control IN endpoint - * @param pdev: device instance - * @param epnum: endpoint number - * @retval status - */ -static uint8_t USBD_CDC_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum) -{ - USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)pdev->pClassData; - PCD_HandleTypeDef *hpcd = pdev->pData; - - if(pdev->pClassData != NULL) - { - if((pdev->ep_in[epnum].total_length > 0U) && ((pdev->ep_in[epnum].total_length % hpcd->IN_ep[epnum].maxpacket) == 0U)) - { - /* Update the packet total length */ - pdev->ep_in[epnum].total_length = 0U; - - /* Send ZLP */ - USBD_LL_Transmit (pdev, epnum, NULL, 0U); - } - else - { - hcdc->TxState = 0U; - } - return USBD_OK; - } - else - { - return USBD_FAIL; - } -} - -/** - * @brief USBD_CDC_DataOut - * Data received on non-control Out endpoint - * @param pdev: device instance - * @param epnum: endpoint number - * @retval status - */ -static uint8_t USBD_CDC_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum) -{ - USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*) pdev->pClassData; - - /* Get the received data length */ - hcdc->RxLength = USBD_LL_GetRxDataSize (pdev, epnum); - - /* USB data will be immediately processed, this allow next USB traffic being - NAKed till the end of the application Xfer */ - if(pdev->pClassData != NULL) - { - ((USBD_CDC_ItfTypeDef *)pdev->pUserData)->Receive(hcdc->RxBuffer, &hcdc->RxLength); - - return USBD_OK; - } - else - { - return USBD_FAIL; - } -} - -/** - * @brief USBD_CDC_EP0_RxReady - * Handle EP0 Rx Ready event - * @param pdev: device instance - * @retval status - */ -static uint8_t USBD_CDC_EP0_RxReady (USBD_HandleTypeDef *pdev) -{ - USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*) pdev->pClassData; - - if((pdev->pUserData != NULL) && (hcdc->CmdOpCode != 0xFFU)) - { - ((USBD_CDC_ItfTypeDef *)pdev->pUserData)->Control(hcdc->CmdOpCode, - (uint8_t *)(void *)hcdc->data, - (uint16_t)hcdc->CmdLength); - hcdc->CmdOpCode = 0xFFU; - - } - return USBD_OK; -} - -/** - * @brief USBD_CDC_GetFSCfgDesc - * Return configuration descriptor - * @param speed : current device speed - * @param length : pointer data length - * @retval pointer to descriptor buffer - */ -static uint8_t *USBD_CDC_GetFSCfgDesc (uint16_t *length) -{ - *length = sizeof (USBD_CDC_CfgFSDesc); - return USBD_CDC_CfgFSDesc; -} - -/** - * @brief USBD_CDC_GetHSCfgDesc - * Return configuration descriptor - * @param speed : current device speed - * @param length : pointer data length - * @retval pointer to descriptor buffer - */ -static uint8_t *USBD_CDC_GetHSCfgDesc (uint16_t *length) -{ - *length = sizeof (USBD_CDC_CfgHSDesc); - return USBD_CDC_CfgHSDesc; -} - -/** - * @brief USBD_CDC_GetCfgDesc - * Return configuration descriptor - * @param speed : current device speed - * @param length : pointer data length - * @retval pointer to descriptor buffer - */ -static uint8_t *USBD_CDC_GetOtherSpeedCfgDesc (uint16_t *length) -{ - *length = sizeof (USBD_CDC_OtherSpeedCfgDesc); - return USBD_CDC_OtherSpeedCfgDesc; -} - -/** -* @brief DeviceQualifierDescriptor -* return Device Qualifier descriptor -* @param length : pointer data length -* @retval pointer to descriptor buffer -*/ -uint8_t *USBD_CDC_GetDeviceQualifierDescriptor (uint16_t *length) -{ - *length = sizeof (USBD_CDC_DeviceQualifierDesc); - return USBD_CDC_DeviceQualifierDesc; -} - -/** -* @brief USBD_CDC_RegisterInterface - * @param pdev: device instance - * @param fops: CD Interface callback - * @retval status - */ -uint8_t USBD_CDC_RegisterInterface (USBD_HandleTypeDef *pdev, - USBD_CDC_ItfTypeDef *fops) -{ - uint8_t ret = USBD_FAIL; - - if(fops != NULL) - { - pdev->pUserData= fops; - ret = USBD_OK; - } - - return ret; -} - -/** - * @brief USBD_CDC_SetTxBuffer - * @param pdev: device instance - * @param pbuff: Tx Buffer - * @retval status - */ -uint8_t USBD_CDC_SetTxBuffer (USBD_HandleTypeDef *pdev, - uint8_t *pbuff, - uint16_t length) -{ - USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*) pdev->pClassData; - - hcdc->TxBuffer = pbuff; - hcdc->TxLength = length; - - return USBD_OK; -} - - -/** - * @brief USBD_CDC_SetRxBuffer - * @param pdev: device instance - * @param pbuff: Rx Buffer - * @retval status - */ -uint8_t USBD_CDC_SetRxBuffer (USBD_HandleTypeDef *pdev, - uint8_t *pbuff) -{ - USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*) pdev->pClassData; - - hcdc->RxBuffer = pbuff; - - return USBD_OK; -} - -/** - * @brief USBD_CDC_TransmitPacket - * Transmit packet on IN endpoint - * @param pdev: device instance - * @retval status - */ -uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev) -{ - USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*) pdev->pClassData; - - if(pdev->pClassData != NULL) - { - if(hcdc->TxState == 0U) - { - /* Tx Transfer in progress */ - hcdc->TxState = 1U; - - /* Update the packet total length */ - pdev->ep_in[CDC_IN_EP & 0xFU].total_length = hcdc->TxLength; - - /* Transmit next packet */ - USBD_LL_Transmit(pdev, CDC_IN_EP, hcdc->TxBuffer, - (uint16_t)hcdc->TxLength); - - return USBD_OK; - } - else - { - return USBD_BUSY; - } - } - else - { - return USBD_FAIL; - } -} - - -/** - * @brief USBD_CDC_ReceivePacket - * prepare OUT Endpoint for reception - * @param pdev: device instance - * @retval status - */ -uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev) -{ - USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*) pdev->pClassData; - - /* Suspend or Resume USB Out process */ - if(pdev->pClassData != NULL) - { - if(pdev->dev_speed == USBD_SPEED_HIGH ) - { - /* Prepare Out endpoint to receive next packet */ - USBD_LL_PrepareReceive(pdev, - CDC_OUT_EP, - hcdc->RxBuffer, - CDC_DATA_HS_OUT_PACKET_SIZE); - } - else - { - /* Prepare Out endpoint to receive next packet */ - USBD_LL_PrepareReceive(pdev, - CDC_OUT_EP, - hcdc->RxBuffer, - CDC_DATA_FS_OUT_PACKET_SIZE); - } - return USBD_OK; - } - else - { - return USBD_FAIL; - } -} -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_cdc.c + * @author MCD Application Team + * @brief This file provides the high layer firmware functions to manage the + * following functionalities of the USB CDC Class: + * - Initialization and Configuration of high and low layer + * - Enumeration as CDC Device (and enumeration for each implemented + * memory interface) + * - OUT/IN data transfer + * - Command IN transfer (class requests management) + * - Error management + * + * @verbatim + * + * =================================================================== + * CDC Class Driver Description + * =================================================================== + * This driver manages the "Universal Serial Bus Class Definitions + * for Communications Devices Revision 1.2 November 16, 2007" and the + * sub-protocol specification of "Universal Serial Bus Communications + * Class Subclass Specification for PSTN Devices Revision 1.2 + * February 9, 2007" This driver implements the following aspects of + * the specification: + * - Device descriptor management + * - Configuration descriptor management + * - Enumeration as CDC device with 2 data endpoints (IN and OUT) + * and 1 command endpoint (IN) + * - Requests management (as described in section 6.2 in + * specification) + * - Abstract Control Model compliant + * - Union Functional collection (using 1 IN endpoint for control) + * - Data interface class + * + * These aspects may be enriched or modified for a specific user + * application. + * + * This driver doesn't implement the following aspects of the + * specification (but it is possible to manage these features with + * some modifications on this driver): + * - Any class-specific aspect relative to communication classes + * should be managed by user application. + * - All communication classes other than PSTN are not managed + * + * @endverbatim + * + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* BSPDependencies +- "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" +- "stm32xxxxx_{eval}{discovery}_io.c" +EndBSPDependencies */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_cdc.h" +#include "usbd_ctlreq.h" + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + +/** @defgroup USBD_CDC + * @brief usbd core module + * @{ + */ + +/** @defgroup USBD_CDC_Private_TypesDefinitions + * @{ + */ +/** + * @} + */ + +/** @defgroup USBD_CDC_Private_Defines + * @{ + */ +/** + * @} + */ + +/** @defgroup USBD_CDC_Private_Macros + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_CDC_Private_FunctionPrototypes + * @{ + */ + +static uint8_t USBD_CDC_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx); + +static uint8_t USBD_CDC_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx); + +static uint8_t USBD_CDC_Setup(USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req); + +static uint8_t USBD_CDC_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum); + +static uint8_t USBD_CDC_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum); + +static uint8_t USBD_CDC_EP0_RxReady(USBD_HandleTypeDef *pdev); + +static uint8_t *USBD_CDC_GetFSCfgDesc(uint16_t *length); + +static uint8_t *USBD_CDC_GetHSCfgDesc(uint16_t *length); + +static uint8_t *USBD_CDC_GetOtherSpeedCfgDesc(uint16_t *length); + +static uint8_t *USBD_CDC_GetOtherSpeedCfgDesc(uint16_t *length); + +uint8_t *USBD_CDC_GetDeviceQualifierDescriptor(uint16_t *length); + +/* USB Standard Device Descriptor */ +__ALIGN_BEGIN static uint8_t + USBD_CDC_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END = { + USB_LEN_DEV_QUALIFIER_DESC, + USB_DESC_TYPE_DEVICE_QUALIFIER, + 0x00, + 0x02, + 0x00, + 0x00, + 0x00, + 0x40, + 0x01, + 0x00, +}; + +/** + * @} + */ + +/** @defgroup USBD_CDC_Private_Variables + * @{ + */ + +/* CDC interface class callbacks structure */ +USBD_ClassTypeDef USBD_CDC = { + USBD_CDC_Init, + USBD_CDC_DeInit, + USBD_CDC_Setup, + NULL, /* EP0_TxSent, */ + USBD_CDC_EP0_RxReady, + USBD_CDC_DataIn, + USBD_CDC_DataOut, + NULL, + NULL, + NULL, + USBD_CDC_GetHSCfgDesc, + USBD_CDC_GetFSCfgDesc, + USBD_CDC_GetOtherSpeedCfgDesc, + USBD_CDC_GetDeviceQualifierDescriptor, +}; + +/* USB CDC device Configuration Descriptor */ +__ALIGN_BEGIN uint8_t + USBD_CDC_CfgHSDesc[USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END = { + /* Configuration Descriptor */ + 0x09, /* bLength: Configuration Descriptor size */ + USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ + USB_CDC_CONFIG_DESC_SIZ, /* wTotalLength:no of returned bytes */ + 0x00, 0x02, /* bNumInterfaces: 2 interface */ + 0x01, /* bConfigurationValue: Configuration value */ + 0x00, /* iConfiguration: Index of string descriptor describing the + configuration */ + 0xC0, /* bmAttributes: self powered */ + 0x32, /* MaxPower 0 mA */ + + /*--------------------------------------------------------------------------*/ + + /* Interface Descriptor */ + 0x09, /* bLength: Interface Descriptor size */ + USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */ + /* Interface descriptor type */ + 0x00, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x01, /* bNumEndpoints: One endpoints used */ + 0x02, /* bInterfaceClass: Communication Interface Class */ + 0x02, /* bInterfaceSubClass: Abstract Control Model */ + 0x01, /* bInterfaceProtocol: Common AT commands */ + 0x00, /* iInterface: */ + + /* Header Functional Descriptor */ + 0x05, /* bLength: Endpoint Descriptor size */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x00, /* bDescriptorSubtype: Header Func Desc */ + 0x10, /* bcdCDC: spec release number */ + 0x01, + + /* Call Management Functional Descriptor */ + 0x05, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x01, /* bDescriptorSubtype: Call Management Func Desc */ + 0x00, /* bmCapabilities: D0+D1 */ + 0x01, /* bDataInterface: 1 */ + + /* ACM Functional Descriptor */ + 0x04, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x02, /* bDescriptorSubtype: Abstract Control Management desc */ + 0x02, /* bmCapabilities */ + + /* Union Functional Descriptor */ + 0x05, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x06, /* bDescriptorSubtype: Union func desc */ + 0x00, /* bMasterInterface: Communication class interface */ + 0x01, /* bSlaveInterface0: Data Class Interface */ + + /* Endpoint 2 Descriptor */ + 0x07, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ + CDC_CMD_EP, /* bEndpointAddress */ + 0x03, /* bmAttributes: Interrupt */ + LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize: */ + HIBYTE(CDC_CMD_PACKET_SIZE), + CDC_HS_BINTERVAL, /* bInterval: */ + /*--------------------------------------------------------------------------*/ + + /* Data class interface descriptor */ + 0x09, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_INTERFACE, /* bDescriptorType: */ + 0x01, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x02, /* bNumEndpoints: Two endpoints used */ + 0x0A, /* bInterfaceClass: CDC */ + 0x00, /* bInterfaceSubClass: */ + 0x00, /* bInterfaceProtocol: */ + 0x00, /* iInterface: */ + + /* Endpoint OUT Descriptor */ + 0x07, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ + CDC_OUT_EP, /* bEndpointAddress */ + 0x02, /* bmAttributes: Bulk */ + LOBYTE(CDC_DATA_HS_MAX_PACKET_SIZE), /* wMaxPacketSize: */ + HIBYTE(CDC_DATA_HS_MAX_PACKET_SIZE), + 0x00, /* bInterval: ignore for Bulk transfer */ + + /* Endpoint IN Descriptor */ + 0x07, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ + CDC_IN_EP, /* bEndpointAddress */ + 0x02, /* bmAttributes: Bulk */ + LOBYTE(CDC_DATA_HS_MAX_PACKET_SIZE), /* wMaxPacketSize: */ + HIBYTE(CDC_DATA_HS_MAX_PACKET_SIZE), + 0x00 /* bInterval: ignore for Bulk transfer */ +}; + +/* USB CDC device Configuration Descriptor */ +__ALIGN_BEGIN uint8_t + USBD_CDC_CfgFSDesc[USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END = { + /* Configuration Descriptor */ + 0x09, /* bLength: Configuration Descriptor size */ + USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ + USB_CDC_CONFIG_DESC_SIZ, /* wTotalLength:no of returned bytes */ + 0x00, 0x02, /* bNumInterfaces: 2 interface */ + 0x01, /* bConfigurationValue: Configuration value */ + 0x00, /* iConfiguration: Index of string descriptor describing the + configuration */ + 0xC0, /* bmAttributes: self powered */ + 0x32, /* MaxPower 0 mA */ + + /*--------------------------------------------------------------------------*/ + + /* Interface Descriptor */ + 0x09, /* bLength: Interface Descriptor size */ + USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */ + /* Interface descriptor type */ + 0x00, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x01, /* bNumEndpoints: One endpoints used */ + 0x02, /* bInterfaceClass: Communication Interface Class */ + 0x02, /* bInterfaceSubClass: Abstract Control Model */ + 0x01, /* bInterfaceProtocol: Common AT commands */ + 0x00, /* iInterface: */ + + /* Header Functional Descriptor */ + 0x05, /* bLength: Endpoint Descriptor size */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x00, /* bDescriptorSubtype: Header Func Desc */ + 0x10, /* bcdCDC: spec release number */ + 0x01, + + /* Call Management Functional Descriptor */ + 0x05, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x01, /* bDescriptorSubtype: Call Management Func Desc */ + 0x00, /* bmCapabilities: D0+D1 */ + 0x01, /* bDataInterface: 1 */ + + /* ACM Functional Descriptor */ + 0x04, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x02, /* bDescriptorSubtype: Abstract Control Management desc */ + 0x02, /* bmCapabilities */ + + /* Union Functional Descriptor */ + 0x05, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x06, /* bDescriptorSubtype: Union func desc */ + 0x00, /* bMasterInterface: Communication class interface */ + 0x01, /* bSlaveInterface0: Data Class Interface */ + + /* Endpoint 2 Descriptor */ + 0x07, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ + CDC_CMD_EP, /* bEndpointAddress */ + 0x03, /* bmAttributes: Interrupt */ + LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize: */ + HIBYTE(CDC_CMD_PACKET_SIZE), + CDC_FS_BINTERVAL, /* bInterval: */ + /*--------------------------------------------------------------------------*/ + + /* Data class interface descriptor */ + 0x09, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_INTERFACE, /* bDescriptorType: */ + 0x01, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x02, /* bNumEndpoints: Two endpoints used */ + 0x0A, /* bInterfaceClass: CDC */ + 0x00, /* bInterfaceSubClass: */ + 0x00, /* bInterfaceProtocol: */ + 0x00, /* iInterface: */ + + /* Endpoint OUT Descriptor */ + 0x07, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ + CDC_OUT_EP, /* bEndpointAddress */ + 0x02, /* bmAttributes: Bulk */ + LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), /* wMaxPacketSize: */ + HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), + 0x00, /* bInterval: ignore for Bulk transfer */ + + /* Endpoint IN Descriptor */ + 0x07, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ + CDC_IN_EP, /* bEndpointAddress */ + 0x02, /* bmAttributes: Bulk */ + LOBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), /* wMaxPacketSize: */ + HIBYTE(CDC_DATA_FS_MAX_PACKET_SIZE), + 0x00 /* bInterval: ignore for Bulk transfer */ +}; + +__ALIGN_BEGIN uint8_t + USBD_CDC_OtherSpeedCfgDesc[USB_CDC_CONFIG_DESC_SIZ] __ALIGN_END = { + 0x09, /* bLength: Configuation Descriptor size */ + USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION, USB_CDC_CONFIG_DESC_SIZ, 0x00, + 0x02, /* bNumInterfaces: 2 interfaces */ + 0x01, /* bConfigurationValue: */ + 0x04, /* iConfiguration: */ + 0xC0, /* bmAttributes: */ + 0x32, /* MaxPower 100 mA */ + + /* Interface Descriptor */ + 0x09, /* bLength: Interface Descriptor size */ + USB_DESC_TYPE_INTERFACE, /* bDescriptorType: Interface */ + /* Interface descriptor type */ + 0x00, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x01, /* bNumEndpoints: One endpoints used */ + 0x02, /* bInterfaceClass: Communication Interface Class */ + 0x02, /* bInterfaceSubClass: Abstract Control Model */ + 0x01, /* bInterfaceProtocol: Common AT commands */ + 0x00, /* iInterface: */ + + /* Header Functional Descriptor */ + 0x05, /* bLength: Endpoint Descriptor size */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x00, /* bDescriptorSubtype: Header Func Desc */ + 0x10, /* bcdCDC: spec release number */ + 0x01, + + /* Call Management Functional Descriptor */ + 0x05, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x01, /* bDescriptorSubtype: Call Management Func Desc */ + 0x00, /* bmCapabilities: D0+D1 */ + 0x01, /* bDataInterface: 1 */ + + /* ACM Functional Descriptor */ + 0x04, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x02, /* bDescriptorSubtype: Abstract Control Management desc */ + 0x02, /* bmCapabilities */ + + /* Union Functional Descriptor */ + 0x05, /* bFunctionLength */ + 0x24, /* bDescriptorType: CS_INTERFACE */ + 0x06, /* bDescriptorSubtype: Union func desc */ + 0x00, /* bMasterInterface: Communication class interface */ + 0x01, /* bSlaveInterface0: Data Class Interface */ + + /* Endpoint 2 Descriptor */ + 0x07, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ + CDC_CMD_EP, /* bEndpointAddress */ + 0x03, /* bmAttributes: Interrupt */ + LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize: */ + HIBYTE(CDC_CMD_PACKET_SIZE), CDC_FS_BINTERVAL, /* bInterval: */ + + /*---------------------------------------------------------------------------*/ + + /* Data class interface descriptor */ + 0x09, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_INTERFACE, /* bDescriptorType: */ + 0x01, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x02, /* bNumEndpoints: Two endpoints used */ + 0x0A, /* bInterfaceClass: CDC */ + 0x00, /* bInterfaceSubClass: */ + 0x00, /* bInterfaceProtocol: */ + 0x00, /* iInterface: */ + + /* Endpoint OUT Descriptor */ + 0x07, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ + CDC_OUT_EP, /* bEndpointAddress */ + 0x02, /* bmAttributes: Bulk */ + 0x40, /* wMaxPacketSize: */ + 0x00, 0x00, /* bInterval: ignore for Bulk transfer */ + + /* Endpoint IN Descriptor */ + 0x07, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: Endpoint */ + CDC_IN_EP, /* bEndpointAddress */ + 0x02, /* bmAttributes: Bulk */ + 0x40, /* wMaxPacketSize: */ + 0x00, 0x00 /* bInterval */ +}; + +/** + * @} + */ + +/** @defgroup USBD_CDC_Private_Functions + * @{ + */ + +/** + * @brief USBD_CDC_Init + * Initialize the CDC interface + * @param pdev: device instance + * @param cfgidx: Configuration index + * @retval status + */ +static uint8_t USBD_CDC_Init(USBD_HandleTypeDef *pdev, uint8_t cfgidx) +{ + uint8_t ret = 0U; + USBD_CDC_HandleTypeDef *hcdc; + + if (pdev->dev_speed == USBD_SPEED_HIGH) + { + /* Open EP IN */ + USBD_LL_OpenEP(pdev, CDC_IN_EP, USBD_EP_TYPE_BULK, + CDC_DATA_HS_IN_PACKET_SIZE); + + pdev->ep_in[CDC_IN_EP & 0xFU].is_used = 1U; + + /* Open EP OUT */ + USBD_LL_OpenEP(pdev, CDC_OUT_EP, USBD_EP_TYPE_BULK, + CDC_DATA_HS_OUT_PACKET_SIZE); + + pdev->ep_out[CDC_OUT_EP & 0xFU].is_used = 1U; + } + else + { + /* Open EP IN */ + USBD_LL_OpenEP(pdev, CDC_IN_EP, USBD_EP_TYPE_BULK, + CDC_DATA_FS_IN_PACKET_SIZE); + + pdev->ep_in[CDC_IN_EP & 0xFU].is_used = 1U; + + /* Open EP OUT */ + USBD_LL_OpenEP(pdev, CDC_OUT_EP, USBD_EP_TYPE_BULK, + CDC_DATA_FS_OUT_PACKET_SIZE); + + pdev->ep_out[CDC_OUT_EP & 0xFU].is_used = 1U; + } + /* Open Command IN EP */ + USBD_LL_OpenEP(pdev, CDC_CMD_EP, USBD_EP_TYPE_INTR, CDC_CMD_PACKET_SIZE); + pdev->ep_in[CDC_CMD_EP & 0xFU].is_used = 1U; + + pdev->pClassData = USBD_malloc(sizeof(USBD_CDC_HandleTypeDef)); + + if (pdev->pClassData == NULL) + { + ret = 1U; + } + else + { + hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassData; + + /* Init physical Interface components */ + ((USBD_CDC_ItfTypeDef *)pdev->pUserData)->Init(); + + /* Init Xfer states */ + hcdc->TxState = 0U; + hcdc->RxState = 0U; + + if (pdev->dev_speed == USBD_SPEED_HIGH) + { + /* Prepare Out endpoint to receive next packet */ + USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, hcdc->RxBuffer, + CDC_DATA_HS_OUT_PACKET_SIZE); + } + else + { + /* Prepare Out endpoint to receive next packet */ + USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, hcdc->RxBuffer, + CDC_DATA_FS_OUT_PACKET_SIZE); + } + } + return ret; +} + +/** + * @brief USBD_CDC_Init + * DeInitialize the CDC layer + * @param pdev: device instance + * @param cfgidx: Configuration index + * @retval status + */ +static uint8_t USBD_CDC_DeInit(USBD_HandleTypeDef *pdev, uint8_t cfgidx) +{ + uint8_t ret = 0U; + + /* Close EP IN */ + USBD_LL_CloseEP(pdev, CDC_IN_EP); + pdev->ep_in[CDC_IN_EP & 0xFU].is_used = 0U; + + /* Close EP OUT */ + USBD_LL_CloseEP(pdev, CDC_OUT_EP); + pdev->ep_out[CDC_OUT_EP & 0xFU].is_used = 0U; + + /* Close Command IN EP */ + USBD_LL_CloseEP(pdev, CDC_CMD_EP); + pdev->ep_in[CDC_CMD_EP & 0xFU].is_used = 0U; + + /* DeInit physical Interface components */ + if (pdev->pClassData != NULL) + { + ((USBD_CDC_ItfTypeDef *)pdev->pUserData)->DeInit(); + USBD_free(pdev->pClassData); + pdev->pClassData = NULL; + } + + return ret; +} + +/** + * @brief USBD_CDC_Setup + * Handle the CDC specific requests + * @param pdev: instance + * @param req: usb requests + * @retval status + */ +static uint8_t USBD_CDC_Setup(USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req) +{ + USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassData; + uint8_t ifalt = 0U; + uint16_t status_info = 0U; + uint8_t ret = USBD_OK; + + switch (req->bmRequest & USB_REQ_TYPE_MASK) + { + case USB_REQ_TYPE_CLASS: + if (req->wLength) + { + if (req->bmRequest & 0x80U) + { + ((USBD_CDC_ItfTypeDef *)pdev->pUserData) + ->Control(req->bRequest, + (uint8_t *)(void *)hcdc->data, + req->wLength); + + USBD_CtlSendData(pdev, (uint8_t *)(void *)hcdc->data, req->wLength); + } + else + { + hcdc->CmdOpCode = req->bRequest; + hcdc->CmdLength = (uint8_t)req->wLength; + + USBD_CtlPrepareRx(pdev, (uint8_t *)(void *)hcdc->data, req->wLength); + } + } + else + { + ((USBD_CDC_ItfTypeDef *)pdev->pUserData) + ->Control(req->bRequest, (uint8_t *)(void *)req, 0U); + } + break; + + case USB_REQ_TYPE_STANDARD: + switch (req->bRequest) + { + case USB_REQ_GET_STATUS: + if (pdev->dev_state == USBD_STATE_CONFIGURED) + { + USBD_CtlSendData(pdev, (uint8_t *)(void *)&status_info, 2U); + } + else + { + USBD_CtlError(pdev, req); + ret = USBD_FAIL; + } + break; + + case USB_REQ_GET_INTERFACE: + if (pdev->dev_state == USBD_STATE_CONFIGURED) + { + USBD_CtlSendData(pdev, &ifalt, 1U); + } + else + { + USBD_CtlError(pdev, req); + ret = USBD_FAIL; + } + break; + + case USB_REQ_SET_INTERFACE: + if (pdev->dev_state != USBD_STATE_CONFIGURED) + { + USBD_CtlError(pdev, req); + ret = USBD_FAIL; + } + break; + + default: + USBD_CtlError(pdev, req); + ret = USBD_FAIL; + break; + } + break; + + default: + USBD_CtlError(pdev, req); + ret = USBD_FAIL; + break; + } + + return ret; +} + +/** + * @brief USBD_CDC_DataIn + * Data sent on non-control IN endpoint + * @param pdev: device instance + * @param epnum: endpoint number + * @retval status + */ +static uint8_t USBD_CDC_DataIn(USBD_HandleTypeDef *pdev, uint8_t epnum) +{ + USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassData; + PCD_HandleTypeDef *hpcd = pdev->pData; + + if (pdev->pClassData != NULL) + { + if ((pdev->ep_in[epnum].total_length > 0U) && + ((pdev->ep_in[epnum].total_length % hpcd->IN_ep[epnum].maxpacket) == + 0U)) + { + /* Update the packet total length */ + pdev->ep_in[epnum].total_length = 0U; + + /* Send ZLP */ + USBD_LL_Transmit(pdev, epnum, NULL, 0U); + } + else + { + hcdc->TxState = 0U; + } + return USBD_OK; + } + else + { + return USBD_FAIL; + } +} + +/** + * @brief USBD_CDC_DataOut + * Data received on non-control Out endpoint + * @param pdev: device instance + * @param epnum: endpoint number + * @retval status + */ +static uint8_t USBD_CDC_DataOut(USBD_HandleTypeDef *pdev, uint8_t epnum) +{ + USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassData; + + /* Get the received data length */ + hcdc->RxLength = USBD_LL_GetRxDataSize(pdev, epnum); + + /* USB data will be immediately processed, this allow next USB traffic being + NAKed till the end of the application Xfer */ + if (pdev->pClassData != NULL) + { + ((USBD_CDC_ItfTypeDef *)pdev->pUserData) + ->Receive(hcdc->RxBuffer, &hcdc->RxLength); + + return USBD_OK; + } + else + { + return USBD_FAIL; + } +} + +/** + * @brief USBD_CDC_EP0_RxReady + * Handle EP0 Rx Ready event + * @param pdev: device instance + * @retval status + */ +static uint8_t USBD_CDC_EP0_RxReady(USBD_HandleTypeDef *pdev) +{ + USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassData; + + if ((pdev->pUserData != NULL) && (hcdc->CmdOpCode != 0xFFU)) + { + ((USBD_CDC_ItfTypeDef *)pdev->pUserData) + ->Control(hcdc->CmdOpCode, (uint8_t *)(void *)hcdc->data, + (uint16_t)hcdc->CmdLength); + hcdc->CmdOpCode = 0xFFU; + } + return USBD_OK; +} + +/** + * @brief USBD_CDC_GetFSCfgDesc + * Return configuration descriptor + * @param speed : current device speed + * @param length : pointer data length + * @retval pointer to descriptor buffer + */ +static uint8_t *USBD_CDC_GetFSCfgDesc(uint16_t *length) +{ + *length = sizeof(USBD_CDC_CfgFSDesc); + return USBD_CDC_CfgFSDesc; +} + +/** + * @brief USBD_CDC_GetHSCfgDesc + * Return configuration descriptor + * @param speed : current device speed + * @param length : pointer data length + * @retval pointer to descriptor buffer + */ +static uint8_t *USBD_CDC_GetHSCfgDesc(uint16_t *length) +{ + *length = sizeof(USBD_CDC_CfgHSDesc); + return USBD_CDC_CfgHSDesc; +} + +/** + * @brief USBD_CDC_GetCfgDesc + * Return configuration descriptor + * @param speed : current device speed + * @param length : pointer data length + * @retval pointer to descriptor buffer + */ +static uint8_t *USBD_CDC_GetOtherSpeedCfgDesc(uint16_t *length) +{ + *length = sizeof(USBD_CDC_OtherSpeedCfgDesc); + return USBD_CDC_OtherSpeedCfgDesc; +} + +/** + * @brief DeviceQualifierDescriptor + * return Device Qualifier descriptor + * @param length : pointer data length + * @retval pointer to descriptor buffer + */ +uint8_t *USBD_CDC_GetDeviceQualifierDescriptor(uint16_t *length) +{ + *length = sizeof(USBD_CDC_DeviceQualifierDesc); + return USBD_CDC_DeviceQualifierDesc; +} + +/** + * @brief USBD_CDC_RegisterInterface + * @param pdev: device instance + * @param fops: CD Interface callback + * @retval status + */ +uint8_t USBD_CDC_RegisterInterface(USBD_HandleTypeDef *pdev, + USBD_CDC_ItfTypeDef *fops) +{ + uint8_t ret = USBD_FAIL; + + if (fops != NULL) + { + pdev->pUserData = fops; + ret = USBD_OK; + } + + return ret; +} + +/** + * @brief USBD_CDC_SetTxBuffer + * @param pdev: device instance + * @param pbuff: Tx Buffer + * @retval status + */ +uint8_t USBD_CDC_SetTxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff, + uint16_t length) +{ + USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassData; + + hcdc->TxBuffer = pbuff; + hcdc->TxLength = length; + + return USBD_OK; +} + +/** + * @brief USBD_CDC_SetRxBuffer + * @param pdev: device instance + * @param pbuff: Rx Buffer + * @retval status + */ +uint8_t USBD_CDC_SetRxBuffer(USBD_HandleTypeDef *pdev, uint8_t *pbuff) +{ + USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassData; + + hcdc->RxBuffer = pbuff; + + return USBD_OK; +} + +/** + * @brief USBD_CDC_TransmitPacket + * Transmit packet on IN endpoint + * @param pdev: device instance + * @retval status + */ +uint8_t USBD_CDC_TransmitPacket(USBD_HandleTypeDef *pdev) +{ + USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassData; + + if (pdev->pClassData != NULL) + { + if (hcdc->TxState == 0U) + { + /* Tx Transfer in progress */ + hcdc->TxState = 1U; + + /* Update the packet total length */ + pdev->ep_in[CDC_IN_EP & 0xFU].total_length = hcdc->TxLength; + + /* Transmit next packet */ + USBD_LL_Transmit(pdev, CDC_IN_EP, hcdc->TxBuffer, + (uint16_t)hcdc->TxLength); + + return USBD_OK; + } + else + { + return USBD_BUSY; + } + } + else + { + return USBD_FAIL; + } +} + +/** + * @brief USBD_CDC_ReceivePacket + * prepare OUT Endpoint for reception + * @param pdev: device instance + * @retval status + */ +uint8_t USBD_CDC_ReceivePacket(USBD_HandleTypeDef *pdev) +{ + USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *)pdev->pClassData; + + /* Suspend or Resume USB Out process */ + if (pdev->pClassData != NULL) + { + if (pdev->dev_speed == USBD_SPEED_HIGH) + { + /* Prepare Out endpoint to receive next packet */ + USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, hcdc->RxBuffer, + CDC_DATA_HS_OUT_PACKET_SIZE); + } + else + { + /* Prepare Out endpoint to receive next packet */ + USBD_LL_PrepareReceive(pdev, CDC_OUT_EP, hcdc->RxBuffer, + CDC_DATA_FS_OUT_PACKET_SIZE); + } + return USBD_OK; + } + else + { + return USBD_FAIL; + } +} + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Src/usbd_cdc_if_template.c b/system/Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Src/usbd_cdc_if_template.c index 788d0cfa50..52cf907ee3 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Src/usbd_cdc_if_template.c +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Src/usbd_cdc_if_template.c @@ -1,223 +1,214 @@ -/** - ****************************************************************************** - * @file usbd_cdc_if_template.c - * @author MCD Application Team - * @brief Generic media access Layer. - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - - /* BSPDependencies - - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" - - "stm32xxxxx_{eval}{discovery}_io.c" - EndBSPDependencies */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_cdc_if_template.h" - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - - -/** @defgroup USBD_CDC - * @brief usbd core module - * @{ - */ - -/** @defgroup USBD_CDC_Private_TypesDefinitions - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBD_CDC_Private_Defines - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBD_CDC_Private_Macros - * @{ - */ - -/** - * @} - */ - - -/** @defgroup USBD_CDC_Private_FunctionPrototypes - * @{ - */ - -static int8_t TEMPLATE_Init (void); -static int8_t TEMPLATE_DeInit (void); -static int8_t TEMPLATE_Control (uint8_t cmd, uint8_t* pbuf, uint16_t length); -static int8_t TEMPLATE_Receive (uint8_t* pbuf, uint32_t *Len); - -USBD_CDC_ItfTypeDef USBD_CDC_Template_fops = -{ - TEMPLATE_Init, - TEMPLATE_DeInit, - TEMPLATE_Control, - TEMPLATE_Receive -}; - -USBD_CDC_LineCodingTypeDef linecoding = - { - 115200, /* baud rate*/ - 0x00, /* stop bits-1*/ - 0x00, /* parity - none*/ - 0x08 /* nb. of bits 8*/ - }; - -/* Private functions ---------------------------------------------------------*/ - -/** - * @brief TEMPLATE_Init - * Initializes the CDC media low layer - * @param None - * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL - */ -static int8_t TEMPLATE_Init(void) -{ - /* - Add your initialization code here - */ - return (0); -} - -/** - * @brief TEMPLATE_DeInit - * DeInitializes the CDC media low layer - * @param None - * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL - */ -static int8_t TEMPLATE_DeInit(void) -{ - /* - Add your deinitialization code here - */ - return (0); -} - - -/** - * @brief TEMPLATE_Control - * Manage the CDC class requests - * @param Cmd: Command code - * @param Buf: Buffer containing command data (request parameters) - * @param Len: Number of data to be sent (in bytes) - * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL - */ -static int8_t TEMPLATE_Control (uint8_t cmd, uint8_t* pbuf, uint16_t length) -{ - switch (cmd) - { - case CDC_SEND_ENCAPSULATED_COMMAND: - /* Add your code here */ - break; - - case CDC_GET_ENCAPSULATED_RESPONSE: - /* Add your code here */ - break; - - case CDC_SET_COMM_FEATURE: - /* Add your code here */ - break; - - case CDC_GET_COMM_FEATURE: - /* Add your code here */ - break; - - case CDC_CLEAR_COMM_FEATURE: - /* Add your code here */ - break; - - case CDC_SET_LINE_CODING: - linecoding.bitrate = (uint32_t)(pbuf[0] | (pbuf[1] << 8) |\ - (pbuf[2] << 16) | (pbuf[3] << 24)); - linecoding.format = pbuf[4]; - linecoding.paritytype = pbuf[5]; - linecoding.datatype = pbuf[6]; - - /* Add your code here */ - break; - - case CDC_GET_LINE_CODING: - pbuf[0] = (uint8_t)(linecoding.bitrate); - pbuf[1] = (uint8_t)(linecoding.bitrate >> 8); - pbuf[2] = (uint8_t)(linecoding.bitrate >> 16); - pbuf[3] = (uint8_t)(linecoding.bitrate >> 24); - pbuf[4] = linecoding.format; - pbuf[5] = linecoding.paritytype; - pbuf[6] = linecoding.datatype; - - /* Add your code here */ - break; - - case CDC_SET_CONTROL_LINE_STATE: - /* Add your code here */ - break; - - case CDC_SEND_BREAK: - /* Add your code here */ - break; - - default: - break; - } - - return (0); -} - -/** - * @brief TEMPLATE_Receive - * Data received over USB OUT endpoint are sent over CDC interface - * through this function. - * - * @note - * This function will issue a NAK packet on any OUT packet received on - * USB endpoint untill exiting this function. If you exit this function - * before transfer is complete on CDC interface (ie. using DMA controller) - * it will result in receiving more data while previous ones are still - * not sent. - * - * @param Buf: Buffer of data to be received - * @param Len: Number of data received (in bytes) - * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL - */ -static int8_t TEMPLATE_Receive (uint8_t* Buf, uint32_t *Len) -{ - - return (0); -} - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - +/** + ****************************************************************************** + * @file usbd_cdc_if_template.c + * @author MCD Application Team + * @brief Generic media access Layer. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* BSPDependencies +- "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" +- "stm32xxxxx_{eval}{discovery}_io.c" +EndBSPDependencies */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_cdc_if_template.h" + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + +/** @defgroup USBD_CDC + * @brief usbd core module + * @{ + */ + +/** @defgroup USBD_CDC_Private_TypesDefinitions + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_CDC_Private_Defines + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_CDC_Private_Macros + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_CDC_Private_FunctionPrototypes + * @{ + */ + +static int8_t TEMPLATE_Init(void); +static int8_t TEMPLATE_DeInit(void); +static int8_t TEMPLATE_Control(uint8_t cmd, uint8_t *pbuf, uint16_t length); +static int8_t TEMPLATE_Receive(uint8_t *pbuf, uint32_t *Len); + +USBD_CDC_ItfTypeDef USBD_CDC_Template_fops = { + TEMPLATE_Init, TEMPLATE_DeInit, TEMPLATE_Control, TEMPLATE_Receive}; + +USBD_CDC_LineCodingTypeDef linecoding = { + 115200, /* baud rate*/ + 0x00, /* stop bits-1*/ + 0x00, /* parity - none*/ + 0x08 /* nb. of bits 8*/ +}; + +/* Private functions ---------------------------------------------------------*/ + +/** + * @brief TEMPLATE_Init + * Initializes the CDC media low layer + * @param None + * @retval Result of the operation: USBD_OK if all operations are OK else + * USBD_FAIL + */ +static int8_t TEMPLATE_Init(void) +{ + /* + Add your initialization code here + */ + return (0); +} + +/** + * @brief TEMPLATE_DeInit + * DeInitializes the CDC media low layer + * @param None + * @retval Result of the operation: USBD_OK if all operations are OK else + * USBD_FAIL + */ +static int8_t TEMPLATE_DeInit(void) +{ + /* + Add your deinitialization code here + */ + return (0); +} + +/** + * @brief TEMPLATE_Control + * Manage the CDC class requests + * @param Cmd: Command code + * @param Buf: Buffer containing command data (request parameters) + * @param Len: Number of data to be sent (in bytes) + * @retval Result of the operation: USBD_OK if all operations are OK else + * USBD_FAIL + */ +static int8_t TEMPLATE_Control(uint8_t cmd, uint8_t *pbuf, uint16_t length) +{ + switch (cmd) + { + case CDC_SEND_ENCAPSULATED_COMMAND: + /* Add your code here */ + break; + + case CDC_GET_ENCAPSULATED_RESPONSE: + /* Add your code here */ + break; + + case CDC_SET_COMM_FEATURE: + /* Add your code here */ + break; + + case CDC_GET_COMM_FEATURE: + /* Add your code here */ + break; + + case CDC_CLEAR_COMM_FEATURE: + /* Add your code here */ + break; + + case CDC_SET_LINE_CODING: + linecoding.bitrate = (uint32_t)(pbuf[0] | (pbuf[1] << 8) | (pbuf[2] << 16) | + (pbuf[3] << 24)); + linecoding.format = pbuf[4]; + linecoding.paritytype = pbuf[5]; + linecoding.datatype = pbuf[6]; + + /* Add your code here */ + break; + + case CDC_GET_LINE_CODING: + pbuf[0] = (uint8_t)(linecoding.bitrate); + pbuf[1] = (uint8_t)(linecoding.bitrate >> 8); + pbuf[2] = (uint8_t)(linecoding.bitrate >> 16); + pbuf[3] = (uint8_t)(linecoding.bitrate >> 24); + pbuf[4] = linecoding.format; + pbuf[5] = linecoding.paritytype; + pbuf[6] = linecoding.datatype; + + /* Add your code here */ + break; + + case CDC_SET_CONTROL_LINE_STATE: + /* Add your code here */ + break; + + case CDC_SEND_BREAK: + /* Add your code here */ + break; + + default: + break; + } + + return (0); +} + +/** + * @brief TEMPLATE_Receive + * Data received over USB OUT endpoint are sent over CDC interface + * through this function. + * + * @note + * This function will issue a NAK packet on any OUT packet received on + * USB endpoint untill exiting this function. If you exit this function + * before transfer is complete on CDC interface (ie. using DMA + * controller) it will result in receiving more data while previous ones are + * still not sent. + * + * @param Buf: Buffer of data to be received + * @param Len: Number of data received (in bytes) + * @retval Result of the operation: USBD_OK if all operations are OK else + * USBD_FAIL + */ +static int8_t TEMPLATE_Receive(uint8_t *Buf, uint32_t *Len) { return (0); } + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Class/CustomHID/Inc/usbd_customhid.h b/system/Middlewares/ST/STM32_USB_Device_Library/Class/CustomHID/Inc/usbd_customhid.h index 513aa9fc1c..c157b18a86 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Class/CustomHID/Inc/usbd_customhid.h +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Class/CustomHID/Inc/usbd_customhid.h @@ -1,166 +1,166 @@ -/** - ****************************************************************************** - * @file usbd_customhid.h - * @author MCD Application Team - * @brief header file for the usbd_customhid.c file. - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_CUSTOMHID_H -#define __USB_CUSTOMHID_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_ioreq.h" - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - -/** @defgroup USBD_CUSTOM_HID - * @brief This file is the Header file for USBD_customhid.c - * @{ - */ - - -/** @defgroup USBD_CUSTOM_HID_Exported_Defines - * @{ - */ -#define CUSTOM_HID_EPIN_ADDR 0x81U -#define CUSTOM_HID_EPIN_SIZE 0x02U - -#define CUSTOM_HID_EPOUT_ADDR 0x01U -#define CUSTOM_HID_EPOUT_SIZE 0x02U - -#define USB_CUSTOM_HID_CONFIG_DESC_SIZ 41U -#define USB_CUSTOM_HID_DESC_SIZ 9U - -#ifndef CUSTOM_HID_HS_BINTERVAL - #define CUSTOM_HID_HS_BINTERVAL 0x05U -#endif /* CUSTOM_HID_HS_BINTERVAL */ - -#ifndef CUSTOM_HID_FS_BINTERVAL - #define CUSTOM_HID_FS_BINTERVAL 0x05U -#endif /* CUSTOM_HID_FS_BINTERVAL */ - -#ifndef USBD_CUSTOMHID_OUTREPORT_BUF_SIZE - #define USBD_CUSTOMHID_OUTREPORT_BUF_SIZE 0x02U -#endif /* USBD_CUSTOMHID_OUTREPORT_BUF_SIZE */ -#ifndef USBD_CUSTOM_HID_REPORT_DESC_SIZE - #define USBD_CUSTOM_HID_REPORT_DESC_SIZE 163U -#endif /* USBD_CUSTOM_HID_REPORT_DESC_SIZE */ - -#define CUSTOM_HID_DESCRIPTOR_TYPE 0x21U -#define CUSTOM_HID_REPORT_DESC 0x22U - -#define CUSTOM_HID_REQ_SET_PROTOCOL 0x0BU -#define CUSTOM_HID_REQ_GET_PROTOCOL 0x03U - -#define CUSTOM_HID_REQ_SET_IDLE 0x0AU -#define CUSTOM_HID_REQ_GET_IDLE 0x02U - -#define CUSTOM_HID_REQ_SET_REPORT 0x09U -#define CUSTOM_HID_REQ_GET_REPORT 0x01U -/** - * @} - */ - - -/** @defgroup USBD_CORE_Exported_TypesDefinitions - * @{ - */ -typedef enum -{ - CUSTOM_HID_IDLE = 0U, - CUSTOM_HID_BUSY, -} -CUSTOM_HID_StateTypeDef; - -typedef struct _USBD_CUSTOM_HID_Itf -{ - uint8_t *pReport; - int8_t (* Init) (void); - int8_t (* DeInit) (void); - int8_t (* OutEvent) (uint8_t event_idx, uint8_t state); - -}USBD_CUSTOM_HID_ItfTypeDef; - -typedef struct -{ - uint8_t Report_buf[USBD_CUSTOMHID_OUTREPORT_BUF_SIZE]; - uint32_t Protocol; - uint32_t IdleState; - uint32_t AltSetting; - uint32_t IsReportAvailable; - CUSTOM_HID_StateTypeDef state; -} -USBD_CUSTOM_HID_HandleTypeDef; -/** - * @} - */ - - - -/** @defgroup USBD_CORE_Exported_Macros - * @{ - */ - -/** - * @} - */ - -/** @defgroup USBD_CORE_Exported_Variables - * @{ - */ - -extern USBD_ClassTypeDef USBD_CUSTOM_HID; -#define USBD_CUSTOM_HID_CLASS &USBD_CUSTOM_HID -/** - * @} - */ - -/** @defgroup USB_CORE_Exported_Functions - * @{ - */ -uint8_t USBD_CUSTOM_HID_SendReport (USBD_HandleTypeDef *pdev, - uint8_t *report, - uint16_t len); - - - -uint8_t USBD_CUSTOM_HID_RegisterInterface (USBD_HandleTypeDef *pdev, - USBD_CUSTOM_HID_ItfTypeDef *fops); - -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USB_CUSTOMHID_H */ -/** - * @} - */ - -/** - * @} - */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_customhid.h + * @author MCD Application Team + * @brief header file for the usbd_customhid.c file. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USB_CUSTOMHID_H +#define __USB_CUSTOMHID_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_ioreq.h" + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + +/** @defgroup USBD_CUSTOM_HID + * @brief This file is the Header file for USBD_customhid.c + * @{ + */ + + +/** @defgroup USBD_CUSTOM_HID_Exported_Defines + * @{ + */ +#define CUSTOM_HID_EPIN_ADDR 0x81U +#define CUSTOM_HID_EPIN_SIZE 0x02U + +#define CUSTOM_HID_EPOUT_ADDR 0x01U +#define CUSTOM_HID_EPOUT_SIZE 0x02U + +#define USB_CUSTOM_HID_CONFIG_DESC_SIZ 41U +#define USB_CUSTOM_HID_DESC_SIZ 9U + +#ifndef CUSTOM_HID_HS_BINTERVAL + #define CUSTOM_HID_HS_BINTERVAL 0x05U +#endif /* CUSTOM_HID_HS_BINTERVAL */ + +#ifndef CUSTOM_HID_FS_BINTERVAL + #define CUSTOM_HID_FS_BINTERVAL 0x05U +#endif /* CUSTOM_HID_FS_BINTERVAL */ + +#ifndef USBD_CUSTOMHID_OUTREPORT_BUF_SIZE + #define USBD_CUSTOMHID_OUTREPORT_BUF_SIZE 0x02U +#endif /* USBD_CUSTOMHID_OUTREPORT_BUF_SIZE */ +#ifndef USBD_CUSTOM_HID_REPORT_DESC_SIZE + #define USBD_CUSTOM_HID_REPORT_DESC_SIZE 163U +#endif /* USBD_CUSTOM_HID_REPORT_DESC_SIZE */ + +#define CUSTOM_HID_DESCRIPTOR_TYPE 0x21U +#define CUSTOM_HID_REPORT_DESC 0x22U + +#define CUSTOM_HID_REQ_SET_PROTOCOL 0x0BU +#define CUSTOM_HID_REQ_GET_PROTOCOL 0x03U + +#define CUSTOM_HID_REQ_SET_IDLE 0x0AU +#define CUSTOM_HID_REQ_GET_IDLE 0x02U + +#define CUSTOM_HID_REQ_SET_REPORT 0x09U +#define CUSTOM_HID_REQ_GET_REPORT 0x01U +/** + * @} + */ + + +/** @defgroup USBD_CORE_Exported_TypesDefinitions + * @{ + */ +typedef enum +{ + CUSTOM_HID_IDLE = 0U, + CUSTOM_HID_BUSY, +} +CUSTOM_HID_StateTypeDef; + +typedef struct _USBD_CUSTOM_HID_Itf +{ + uint8_t *pReport; + int8_t (* Init) (void); + int8_t (* DeInit) (void); + int8_t (* OutEvent) (uint8_t event_idx, uint8_t state); + +}USBD_CUSTOM_HID_ItfTypeDef; + +typedef struct +{ + uint8_t Report_buf[USBD_CUSTOMHID_OUTREPORT_BUF_SIZE]; + uint32_t Protocol; + uint32_t IdleState; + uint32_t AltSetting; + uint32_t IsReportAvailable; + CUSTOM_HID_StateTypeDef state; +} +USBD_CUSTOM_HID_HandleTypeDef; +/** + * @} + */ + + + +/** @defgroup USBD_CORE_Exported_Macros + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_CORE_Exported_Variables + * @{ + */ + +extern USBD_ClassTypeDef USBD_CUSTOM_HID; +#define USBD_CUSTOM_HID_CLASS &USBD_CUSTOM_HID +/** + * @} + */ + +/** @defgroup USB_CORE_Exported_Functions + * @{ + */ +uint8_t USBD_CUSTOM_HID_SendReport (USBD_HandleTypeDef *pdev, + uint8_t *report, + uint16_t len); + + + +uint8_t USBD_CUSTOM_HID_RegisterInterface (USBD_HandleTypeDef *pdev, + USBD_CUSTOM_HID_ItfTypeDef *fops); + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USB_CUSTOMHID_H */ +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Class/CustomHID/Inc/usbd_customhid_if_template.h b/system/Middlewares/ST/STM32_USB_Device_Library/Class/CustomHID/Inc/usbd_customhid_if_template.h index 030c01eee2..d138bbabf4 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Class/CustomHID/Inc/usbd_customhid_if_template.h +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Class/CustomHID/Inc/usbd_customhid_if_template.h @@ -1,43 +1,43 @@ -/** - ****************************************************************************** - * @file usbd_customhid_if_template.h - * @author MCD Application Team - * @brief Header for usbd_customhid_if_template.c file. - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USBD_CUSTOMHID_IF_TEMPLATE_H -#define __USBD_CUSTOMHID_IF_TEMPLATE_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_customhid.h" - -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ -/* Exported macro ------------------------------------------------------------*/ -/* Exported functions ------------------------------------------------------- */ -extern USBD_CUSTOM_HID_ItfTypeDef USBD_CustomHID_template_fops; - -#ifdef __cplusplus -} -#endif - -#endif /* __USBD_CUSTOMHID_IF_TEMPLATE_H */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_customhid_if_template.h + * @author MCD Application Team + * @brief Header for usbd_customhid_if_template.c file. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USBD_CUSTOMHID_IF_TEMPLATE_H +#define __USBD_CUSTOMHID_IF_TEMPLATE_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_customhid.h" + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ +extern USBD_CUSTOM_HID_ItfTypeDef USBD_CustomHID_template_fops; + +#ifdef __cplusplus +} +#endif + +#endif /* __USBD_CUSTOMHID_IF_TEMPLATE_H */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Class/CustomHID/Src/usbd_customhid.c b/system/Middlewares/ST/STM32_USB_Device_Library/Class/CustomHID/Src/usbd_customhid.c index c1295e3c2a..73686bbe98 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Class/CustomHID/Src/usbd_customhid.c +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Class/CustomHID/Src/usbd_customhid.c @@ -1,716 +1,716 @@ -/** - ****************************************************************************** - * @file usbd_customhid.c - * @author MCD Application Team - * @brief This file provides the CUSTOM_HID core functions. - * - * @verbatim - * - * =================================================================== - * CUSTOM_HID Class Description - * =================================================================== - * This module manages the CUSTOM_HID class V1.11 following the "Device Class Definition - * for Human Interface Devices (CUSTOM_HID) Version 1.11 Jun 27, 2001". - * This driver implements the following aspects of the specification: - * - The Boot Interface Subclass - * - Usage Page : Generic Desktop - * - Usage : Vendor - * - Collection : Application - * - * @note In HS mode and when the DMA is used, all variables and data structures - * dealing with the DMA during the transaction process should be 32-bit aligned. - * - * - * @endverbatim - * - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - - /* BSPDependencies - - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" - - "stm32xxxxx_{eval}{discovery}_io.c" - EndBSPDependencies */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_customhid.h" -#include "usbd_ctlreq.h" - - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - - -/** @defgroup USBD_CUSTOM_HID - * @brief usbd core module - * @{ - */ - -/** @defgroup USBD_CUSTOM_HID_Private_TypesDefinitions - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBD_CUSTOM_HID_Private_Defines - * @{ - */ - -/** - * @} - */ - - -/** @defgroup USBD_CUSTOM_HID_Private_Macros - * @{ - */ -/** - * @} - */ -/** @defgroup USBD_CUSTOM_HID_Private_FunctionPrototypes - * @{ - */ - - -static uint8_t USBD_CUSTOM_HID_Init (USBD_HandleTypeDef *pdev, - uint8_t cfgidx); - -static uint8_t USBD_CUSTOM_HID_DeInit (USBD_HandleTypeDef *pdev, - uint8_t cfgidx); - -static uint8_t USBD_CUSTOM_HID_Setup (USBD_HandleTypeDef *pdev, - USBD_SetupReqTypedef *req); - -static uint8_t *USBD_CUSTOM_HID_GetFSCfgDesc (uint16_t *length); - -static uint8_t *USBD_CUSTOM_HID_GetHSCfgDesc (uint16_t *length); - -static uint8_t *USBD_CUSTOM_HID_GetOtherSpeedCfgDesc (uint16_t *length); - -static uint8_t *USBD_CUSTOM_HID_GetDeviceQualifierDesc (uint16_t *length); - -static uint8_t USBD_CUSTOM_HID_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum); - -static uint8_t USBD_CUSTOM_HID_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum); -static uint8_t USBD_CUSTOM_HID_EP0_RxReady (USBD_HandleTypeDef *pdev); -/** - * @} - */ - -/** @defgroup USBD_CUSTOM_HID_Private_Variables - * @{ - */ - -USBD_ClassTypeDef USBD_CUSTOM_HID = -{ - USBD_CUSTOM_HID_Init, - USBD_CUSTOM_HID_DeInit, - USBD_CUSTOM_HID_Setup, - NULL, /*EP0_TxSent*/ - USBD_CUSTOM_HID_EP0_RxReady, /*EP0_RxReady*/ /* STATUS STAGE IN */ - USBD_CUSTOM_HID_DataIn, /*DataIn*/ - USBD_CUSTOM_HID_DataOut, - NULL, /*SOF */ - NULL, - NULL, - USBD_CUSTOM_HID_GetHSCfgDesc, - USBD_CUSTOM_HID_GetFSCfgDesc, - USBD_CUSTOM_HID_GetOtherSpeedCfgDesc, - USBD_CUSTOM_HID_GetDeviceQualifierDesc, -}; - -/* USB CUSTOM_HID device FS Configuration Descriptor */ -__ALIGN_BEGIN static uint8_t USBD_CUSTOM_HID_CfgFSDesc[USB_CUSTOM_HID_CONFIG_DESC_SIZ] __ALIGN_END = -{ - 0x09, /* bLength: Configuration Descriptor size */ - USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ - USB_CUSTOM_HID_CONFIG_DESC_SIZ, - /* wTotalLength: Bytes returned */ - 0x00, - 0x01, /*bNumInterfaces: 1 interface*/ - 0x01, /*bConfigurationValue: Configuration value*/ - 0x00, /*iConfiguration: Index of string descriptor describing - the configuration*/ - 0xC0, /*bmAttributes: bus powered */ - 0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/ - - /************** Descriptor of CUSTOM HID interface ****************/ - /* 09 */ - 0x09, /*bLength: Interface Descriptor size*/ - USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/ - 0x00, /*bInterfaceNumber: Number of Interface*/ - 0x00, /*bAlternateSetting: Alternate setting*/ - 0x02, /*bNumEndpoints*/ - 0x03, /*bInterfaceClass: CUSTOM_HID*/ - 0x00, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/ - 0x00, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/ - 0, /*iInterface: Index of string descriptor*/ - /******************** Descriptor of CUSTOM_HID *************************/ - /* 18 */ - 0x09, /*bLength: CUSTOM_HID Descriptor size*/ - CUSTOM_HID_DESCRIPTOR_TYPE, /*bDescriptorType: CUSTOM_HID*/ - 0x11, /*bCUSTOM_HIDUSTOM_HID: CUSTOM_HID Class Spec release number*/ - 0x01, - 0x00, /*bCountryCode: Hardware target country*/ - 0x01, /*bNumDescriptors: Number of CUSTOM_HID class descriptors to follow*/ - 0x22, /*bDescriptorType*/ - USBD_CUSTOM_HID_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ - 0x00, - /******************** Descriptor of Custom HID endpoints ********************/ - /* 27 */ - 0x07, /*bLength: Endpoint Descriptor size*/ - USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/ - - CUSTOM_HID_EPIN_ADDR, /*bEndpointAddress: Endpoint Address (IN)*/ - 0x03, /*bmAttributes: Interrupt endpoint*/ - CUSTOM_HID_EPIN_SIZE, /*wMaxPacketSize: 2 Byte max */ - 0x00, - CUSTOM_HID_FS_BINTERVAL, /*bInterval: Polling Interval */ - /* 34 */ - - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: */ - CUSTOM_HID_EPOUT_ADDR, /*bEndpointAddress: Endpoint Address (OUT)*/ - 0x03, /* bmAttributes: Interrupt endpoint */ - CUSTOM_HID_EPOUT_SIZE, /* wMaxPacketSize: 2 Bytes max */ - 0x00, - CUSTOM_HID_FS_BINTERVAL, /* bInterval: Polling Interval */ - /* 41 */ -}; - -/* USB CUSTOM_HID device HS Configuration Descriptor */ -__ALIGN_BEGIN static uint8_t USBD_CUSTOM_HID_CfgHSDesc[USB_CUSTOM_HID_CONFIG_DESC_SIZ] __ALIGN_END = -{ - 0x09, /* bLength: Configuration Descriptor size */ - USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ - USB_CUSTOM_HID_CONFIG_DESC_SIZ, - /* wTotalLength: Bytes returned */ - 0x00, - 0x01, /*bNumInterfaces: 1 interface*/ - 0x01, /*bConfigurationValue: Configuration value*/ - 0x00, /*iConfiguration: Index of string descriptor describing - the configuration*/ - 0xC0, /*bmAttributes: bus powered */ - 0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/ - - /************** Descriptor of CUSTOM HID interface ****************/ - /* 09 */ - 0x09, /*bLength: Interface Descriptor size*/ - USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/ - 0x00, /*bInterfaceNumber: Number of Interface*/ - 0x00, /*bAlternateSetting: Alternate setting*/ - 0x02, /*bNumEndpoints*/ - 0x03, /*bInterfaceClass: CUSTOM_HID*/ - 0x00, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/ - 0x00, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/ - 0, /*iInterface: Index of string descriptor*/ - /******************** Descriptor of CUSTOM_HID *************************/ - /* 18 */ - 0x09, /*bLength: CUSTOM_HID Descriptor size*/ - CUSTOM_HID_DESCRIPTOR_TYPE, /*bDescriptorType: CUSTOM_HID*/ - 0x11, /*bCUSTOM_HIDUSTOM_HID: CUSTOM_HID Class Spec release number*/ - 0x01, - 0x00, /*bCountryCode: Hardware target country*/ - 0x01, /*bNumDescriptors: Number of CUSTOM_HID class descriptors to follow*/ - 0x22, /*bDescriptorType*/ - USBD_CUSTOM_HID_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ - 0x00, - /******************** Descriptor of Custom HID endpoints ********************/ - /* 27 */ - 0x07, /*bLength: Endpoint Descriptor size*/ - USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/ - - CUSTOM_HID_EPIN_ADDR, /*bEndpointAddress: Endpoint Address (IN)*/ - 0x03, /*bmAttributes: Interrupt endpoint*/ - CUSTOM_HID_EPIN_SIZE, /*wMaxPacketSize: 2 Byte max */ - 0x00, - CUSTOM_HID_HS_BINTERVAL, /*bInterval: Polling Interval */ - /* 34 */ - - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: */ - CUSTOM_HID_EPOUT_ADDR, /*bEndpointAddress: Endpoint Address (OUT)*/ - 0x03, /* bmAttributes: Interrupt endpoint */ - CUSTOM_HID_EPOUT_SIZE, /* wMaxPacketSize: 2 Bytes max */ - 0x00, - CUSTOM_HID_HS_BINTERVAL, /* bInterval: Polling Interval */ - /* 41 */ -}; - -/* USB CUSTOM_HID device Other Speed Configuration Descriptor */ -__ALIGN_BEGIN static uint8_t USBD_CUSTOM_HID_OtherSpeedCfgDesc[USB_CUSTOM_HID_CONFIG_DESC_SIZ] __ALIGN_END = -{ - 0x09, /* bLength: Configuration Descriptor size */ - USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ - USB_CUSTOM_HID_CONFIG_DESC_SIZ, - /* wTotalLength: Bytes returned */ - 0x00, - 0x01, /*bNumInterfaces: 1 interface*/ - 0x01, /*bConfigurationValue: Configuration value*/ - 0x00, /*iConfiguration: Index of string descriptor describing - the configuration*/ - 0xC0, /*bmAttributes: bus powered */ - 0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/ - - /************** Descriptor of CUSTOM HID interface ****************/ - /* 09 */ - 0x09, /*bLength: Interface Descriptor size*/ - USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/ - 0x00, /*bInterfaceNumber: Number of Interface*/ - 0x00, /*bAlternateSetting: Alternate setting*/ - 0x02, /*bNumEndpoints*/ - 0x03, /*bInterfaceClass: CUSTOM_HID*/ - 0x00, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/ - 0x00, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/ - 0, /*iInterface: Index of string descriptor*/ - /******************** Descriptor of CUSTOM_HID *************************/ - /* 18 */ - 0x09, /*bLength: CUSTOM_HID Descriptor size*/ - CUSTOM_HID_DESCRIPTOR_TYPE, /*bDescriptorType: CUSTOM_HID*/ - 0x11, /*bCUSTOM_HIDUSTOM_HID: CUSTOM_HID Class Spec release number*/ - 0x01, - 0x00, /*bCountryCode: Hardware target country*/ - 0x01, /*bNumDescriptors: Number of CUSTOM_HID class descriptors to follow*/ - 0x22, /*bDescriptorType*/ - USBD_CUSTOM_HID_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ - 0x00, - /******************** Descriptor of Custom HID endpoints ********************/ - /* 27 */ - 0x07, /*bLength: Endpoint Descriptor size*/ - USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/ - - CUSTOM_HID_EPIN_ADDR, /*bEndpointAddress: Endpoint Address (IN)*/ - 0x03, /*bmAttributes: Interrupt endpoint*/ - CUSTOM_HID_EPIN_SIZE, /*wMaxPacketSize: 2 Byte max */ - 0x00, - CUSTOM_HID_FS_BINTERVAL, /*bInterval: Polling Interval */ - /* 34 */ - - 0x07, /* bLength: Endpoint Descriptor size */ - USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: */ - CUSTOM_HID_EPOUT_ADDR, /*bEndpointAddress: Endpoint Address (OUT)*/ - 0x03, /* bmAttributes: Interrupt endpoint */ - CUSTOM_HID_EPOUT_SIZE, /* wMaxPacketSize: 2 Bytes max */ - 0x00, - CUSTOM_HID_FS_BINTERVAL, /* bInterval: Polling Interval */ - /* 41 */ -}; - -/* USB CUSTOM_HID device Configuration Descriptor */ -__ALIGN_BEGIN static uint8_t USBD_CUSTOM_HID_Desc[USB_CUSTOM_HID_DESC_SIZ] __ALIGN_END = -{ - /* 18 */ - 0x09, /*bLength: CUSTOM_HID Descriptor size*/ - CUSTOM_HID_DESCRIPTOR_TYPE, /*bDescriptorType: CUSTOM_HID*/ - 0x11, /*bCUSTOM_HIDUSTOM_HID: CUSTOM_HID Class Spec release number*/ - 0x01, - 0x00, /*bCountryCode: Hardware target country*/ - 0x01, /*bNumDescriptors: Number of CUSTOM_HID class descriptors to follow*/ - 0x22, /*bDescriptorType*/ - USBD_CUSTOM_HID_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ - 0x00, -}; - -/* USB Standard Device Descriptor */ -__ALIGN_BEGIN static uint8_t USBD_CUSTOM_HID_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END = -{ - USB_LEN_DEV_QUALIFIER_DESC, - USB_DESC_TYPE_DEVICE_QUALIFIER, - 0x00, - 0x02, - 0x00, - 0x00, - 0x00, - 0x40, - 0x01, - 0x00, -}; - -/** - * @} - */ - -/** @defgroup USBD_CUSTOM_HID_Private_Functions - * @{ - */ - -/** - * @brief USBD_CUSTOM_HID_Init - * Initialize the CUSTOM_HID interface - * @param pdev: device instance - * @param cfgidx: Configuration index - * @retval status - */ -static uint8_t USBD_CUSTOM_HID_Init (USBD_HandleTypeDef *pdev, - uint8_t cfgidx) -{ - uint8_t ret = 0U; - USBD_CUSTOM_HID_HandleTypeDef *hhid; - - /* Open EP IN */ - USBD_LL_OpenEP(pdev, CUSTOM_HID_EPIN_ADDR, USBD_EP_TYPE_INTR, - CUSTOM_HID_EPIN_SIZE); - - pdev->ep_in[CUSTOM_HID_EPIN_ADDR & 0xFU].is_used = 1U; - - /* Open EP OUT */ - USBD_LL_OpenEP(pdev, CUSTOM_HID_EPOUT_ADDR, USBD_EP_TYPE_INTR, - CUSTOM_HID_EPOUT_SIZE); - - pdev->ep_out[CUSTOM_HID_EPOUT_ADDR & 0xFU].is_used = 1U; - - pdev->pClassData = USBD_malloc(sizeof (USBD_CUSTOM_HID_HandleTypeDef)); - - if(pdev->pClassData == NULL) - { - ret = 1U; - } - else - { - hhid = (USBD_CUSTOM_HID_HandleTypeDef*) pdev->pClassData; - - hhid->state = CUSTOM_HID_IDLE; - ((USBD_CUSTOM_HID_ItfTypeDef *)pdev->pUserData)->Init(); - - /* Prepare Out endpoint to receive 1st packet */ - USBD_LL_PrepareReceive(pdev, CUSTOM_HID_EPOUT_ADDR, hhid->Report_buf, - USBD_CUSTOMHID_OUTREPORT_BUF_SIZE); - } - - return ret; -} - -/** - * @brief USBD_CUSTOM_HID_Init - * DeInitialize the CUSTOM_HID layer - * @param pdev: device instance - * @param cfgidx: Configuration index - * @retval status - */ -static uint8_t USBD_CUSTOM_HID_DeInit (USBD_HandleTypeDef *pdev, - uint8_t cfgidx) -{ - /* Close CUSTOM_HID EP IN */ - USBD_LL_CloseEP(pdev, CUSTOM_HID_EPIN_ADDR); - pdev->ep_in[CUSTOM_HID_EPIN_ADDR & 0xFU].is_used = 0U; - - /* Close CUSTOM_HID EP OUT */ - USBD_LL_CloseEP(pdev, CUSTOM_HID_EPOUT_ADDR); - pdev->ep_out[CUSTOM_HID_EPOUT_ADDR & 0xFU].is_used = 0U; - - /* FRee allocated memory */ - if(pdev->pClassData != NULL) - { - ((USBD_CUSTOM_HID_ItfTypeDef *)pdev->pUserData)->DeInit(); - USBD_free(pdev->pClassData); - pdev->pClassData = NULL; - } - return USBD_OK; -} - -/** - * @brief USBD_CUSTOM_HID_Setup - * Handle the CUSTOM_HID specific requests - * @param pdev: instance - * @param req: usb requests - * @retval status - */ -static uint8_t USBD_CUSTOM_HID_Setup (USBD_HandleTypeDef *pdev, - USBD_SetupReqTypedef *req) -{ - USBD_CUSTOM_HID_HandleTypeDef *hhid = (USBD_CUSTOM_HID_HandleTypeDef*)pdev->pClassData; - uint16_t len = 0U; - uint8_t *pbuf = NULL; - uint16_t status_info = 0U; - uint8_t ret = USBD_OK; - - switch (req->bmRequest & USB_REQ_TYPE_MASK) - { - case USB_REQ_TYPE_CLASS : - switch (req->bRequest) - { - case CUSTOM_HID_REQ_SET_PROTOCOL: - hhid->Protocol = (uint8_t)(req->wValue); - break; - - case CUSTOM_HID_REQ_GET_PROTOCOL: - USBD_CtlSendData (pdev, (uint8_t *)(void *)&hhid->Protocol, 1U); - break; - - case CUSTOM_HID_REQ_SET_IDLE: - hhid->IdleState = (uint8_t)(req->wValue >> 8); - break; - - case CUSTOM_HID_REQ_GET_IDLE: - USBD_CtlSendData (pdev, (uint8_t *)(void *)&hhid->IdleState, 1U); - break; - - case CUSTOM_HID_REQ_SET_REPORT: - hhid->IsReportAvailable = 1U; - USBD_CtlPrepareRx (pdev, hhid->Report_buf, req->wLength); - break; - - default: - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - break; - } - break; - - case USB_REQ_TYPE_STANDARD: - switch (req->bRequest) - { - case USB_REQ_GET_STATUS: - if (pdev->dev_state == USBD_STATE_CONFIGURED) - { - USBD_CtlSendData (pdev, (uint8_t *)(void *)&status_info, 2U); - } - else - { - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - } - break; - - case USB_REQ_GET_DESCRIPTOR: - if( req->wValue >> 8 == CUSTOM_HID_REPORT_DESC) - { - len = MIN(USBD_CUSTOM_HID_REPORT_DESC_SIZE , req->wLength); - pbuf = ((USBD_CUSTOM_HID_ItfTypeDef *)pdev->pUserData)->pReport; - } - else - { - if( req->wValue >> 8 == CUSTOM_HID_DESCRIPTOR_TYPE) - { - pbuf = USBD_CUSTOM_HID_Desc; - len = MIN(USB_CUSTOM_HID_DESC_SIZ , req->wLength); - } - } - - USBD_CtlSendData (pdev, pbuf, len); - break; - - case USB_REQ_GET_INTERFACE : - if (pdev->dev_state == USBD_STATE_CONFIGURED) - { - USBD_CtlSendData (pdev, (uint8_t *)(void *)&hhid->AltSetting, 1U); - } - else - { - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - } - break; - - case USB_REQ_SET_INTERFACE : - if (pdev->dev_state == USBD_STATE_CONFIGURED) - { - hhid->AltSetting = (uint8_t)(req->wValue); - } - else - { - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - } - break; - - default: - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - break; - } - break; - - default: - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - break; - } - return ret; -} - -/** - * @brief USBD_CUSTOM_HID_SendReport - * Send CUSTOM_HID Report - * @param pdev: device instance - * @param buff: pointer to report - * @retval status - */ -uint8_t USBD_CUSTOM_HID_SendReport (USBD_HandleTypeDef *pdev, - uint8_t *report, - uint16_t len) -{ - USBD_CUSTOM_HID_HandleTypeDef *hhid = (USBD_CUSTOM_HID_HandleTypeDef*)pdev->pClassData; - - if (pdev->dev_state == USBD_STATE_CONFIGURED ) - { - if(hhid->state == CUSTOM_HID_IDLE) - { - hhid->state = CUSTOM_HID_BUSY; - USBD_LL_Transmit (pdev, CUSTOM_HID_EPIN_ADDR, report, len); - } - else - { - return USBD_BUSY; - } - } - return USBD_OK; -} - -/** - * @brief USBD_CUSTOM_HID_GetFSCfgDesc - * return FS configuration descriptor - * @param speed : current device speed - * @param length : pointer data length - * @retval pointer to descriptor buffer - */ -static uint8_t *USBD_CUSTOM_HID_GetFSCfgDesc (uint16_t *length) -{ - *length = sizeof (USBD_CUSTOM_HID_CfgFSDesc); - return USBD_CUSTOM_HID_CfgFSDesc; -} - -/** - * @brief USBD_CUSTOM_HID_GetHSCfgDesc - * return HS configuration descriptor - * @param speed : current device speed - * @param length : pointer data length - * @retval pointer to descriptor buffer - */ -static uint8_t *USBD_CUSTOM_HID_GetHSCfgDesc (uint16_t *length) -{ - *length = sizeof (USBD_CUSTOM_HID_CfgHSDesc); - return USBD_CUSTOM_HID_CfgHSDesc; -} - -/** - * @brief USBD_CUSTOM_HID_GetOtherSpeedCfgDesc - * return other speed configuration descriptor - * @param speed : current device speed - * @param length : pointer data length - * @retval pointer to descriptor buffer - */ -static uint8_t *USBD_CUSTOM_HID_GetOtherSpeedCfgDesc (uint16_t *length) -{ - *length = sizeof (USBD_CUSTOM_HID_OtherSpeedCfgDesc); - return USBD_CUSTOM_HID_OtherSpeedCfgDesc; -} - -/** - * @brief USBD_CUSTOM_HID_DataIn - * handle data IN Stage - * @param pdev: device instance - * @param epnum: endpoint index - * @retval status - */ -static uint8_t USBD_CUSTOM_HID_DataIn (USBD_HandleTypeDef *pdev, - uint8_t epnum) -{ - /* Ensure that the FIFO is empty before a new transfer, this condition could - be caused by a new transfer before the end of the previous transfer */ - ((USBD_CUSTOM_HID_HandleTypeDef *)pdev->pClassData)->state = CUSTOM_HID_IDLE; - - return USBD_OK; -} - -/** - * @brief USBD_CUSTOM_HID_DataOut - * handle data OUT Stage - * @param pdev: device instance - * @param epnum: endpoint index - * @retval status - */ -static uint8_t USBD_CUSTOM_HID_DataOut (USBD_HandleTypeDef *pdev, - uint8_t epnum) -{ - - USBD_CUSTOM_HID_HandleTypeDef *hhid = (USBD_CUSTOM_HID_HandleTypeDef*)pdev->pClassData; - - ((USBD_CUSTOM_HID_ItfTypeDef *)pdev->pUserData)->OutEvent(hhid->Report_buf[0], - hhid->Report_buf[1]); - - USBD_LL_PrepareReceive(pdev, CUSTOM_HID_EPOUT_ADDR , hhid->Report_buf, - USBD_CUSTOMHID_OUTREPORT_BUF_SIZE); - - return USBD_OK; -} - -/** - * @brief USBD_CUSTOM_HID_EP0_RxReady - * Handles control request data. - * @param pdev: device instance - * @retval status - */ -static uint8_t USBD_CUSTOM_HID_EP0_RxReady(USBD_HandleTypeDef *pdev) -{ - USBD_CUSTOM_HID_HandleTypeDef *hhid = (USBD_CUSTOM_HID_HandleTypeDef*)pdev->pClassData; - - if (hhid->IsReportAvailable == 1U) - { - ((USBD_CUSTOM_HID_ItfTypeDef *)pdev->pUserData)->OutEvent(hhid->Report_buf[0], - hhid->Report_buf[1]); - hhid->IsReportAvailable = 0U; - } - - return USBD_OK; -} - -/** -* @brief DeviceQualifierDescriptor -* return Device Qualifier descriptor -* @param length : pointer data length -* @retval pointer to descriptor buffer -*/ -static uint8_t *USBD_CUSTOM_HID_GetDeviceQualifierDesc (uint16_t *length) -{ - *length = sizeof (USBD_CUSTOM_HID_DeviceQualifierDesc); - return USBD_CUSTOM_HID_DeviceQualifierDesc; -} - -/** -* @brief USBD_CUSTOM_HID_RegisterInterface - * @param pdev: device instance - * @param fops: CUSTOMHID Interface callback - * @retval status - */ -uint8_t USBD_CUSTOM_HID_RegisterInterface (USBD_HandleTypeDef *pdev, - USBD_CUSTOM_HID_ItfTypeDef *fops) -{ - uint8_t ret = USBD_FAIL; - - if(fops != NULL) - { - pdev->pUserData= fops; - ret = USBD_OK; - } - - return ret; -} -/** - * @} - */ - - -/** - * @} - */ - - -/** - * @} - */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_customhid.c + * @author MCD Application Team + * @brief This file provides the CUSTOM_HID core functions. + * + * @verbatim + * + * =================================================================== + * CUSTOM_HID Class Description + * =================================================================== + * This module manages the CUSTOM_HID class V1.11 following the "Device Class Definition + * for Human Interface Devices (CUSTOM_HID) Version 1.11 Jun 27, 2001". + * This driver implements the following aspects of the specification: + * - The Boot Interface Subclass + * - Usage Page : Generic Desktop + * - Usage : Vendor + * - Collection : Application + * + * @note In HS mode and when the DMA is used, all variables and data structures + * dealing with the DMA during the transaction process should be 32-bit aligned. + * + * + * @endverbatim + * + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + + /* BSPDependencies + - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" + - "stm32xxxxx_{eval}{discovery}_io.c" + EndBSPDependencies */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_customhid.h" +#include "usbd_ctlreq.h" + + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + + +/** @defgroup USBD_CUSTOM_HID + * @brief usbd core module + * @{ + */ + +/** @defgroup USBD_CUSTOM_HID_Private_TypesDefinitions + * @{ + */ +/** + * @} + */ + + +/** @defgroup USBD_CUSTOM_HID_Private_Defines + * @{ + */ + +/** + * @} + */ + + +/** @defgroup USBD_CUSTOM_HID_Private_Macros + * @{ + */ +/** + * @} + */ +/** @defgroup USBD_CUSTOM_HID_Private_FunctionPrototypes + * @{ + */ + + +static uint8_t USBD_CUSTOM_HID_Init (USBD_HandleTypeDef *pdev, + uint8_t cfgidx); + +static uint8_t USBD_CUSTOM_HID_DeInit (USBD_HandleTypeDef *pdev, + uint8_t cfgidx); + +static uint8_t USBD_CUSTOM_HID_Setup (USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req); + +static uint8_t *USBD_CUSTOM_HID_GetFSCfgDesc (uint16_t *length); + +static uint8_t *USBD_CUSTOM_HID_GetHSCfgDesc (uint16_t *length); + +static uint8_t *USBD_CUSTOM_HID_GetOtherSpeedCfgDesc (uint16_t *length); + +static uint8_t *USBD_CUSTOM_HID_GetDeviceQualifierDesc (uint16_t *length); + +static uint8_t USBD_CUSTOM_HID_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum); + +static uint8_t USBD_CUSTOM_HID_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum); +static uint8_t USBD_CUSTOM_HID_EP0_RxReady (USBD_HandleTypeDef *pdev); +/** + * @} + */ + +/** @defgroup USBD_CUSTOM_HID_Private_Variables + * @{ + */ + +USBD_ClassTypeDef USBD_CUSTOM_HID = +{ + USBD_CUSTOM_HID_Init, + USBD_CUSTOM_HID_DeInit, + USBD_CUSTOM_HID_Setup, + NULL, /*EP0_TxSent*/ + USBD_CUSTOM_HID_EP0_RxReady, /*EP0_RxReady*/ /* STATUS STAGE IN */ + USBD_CUSTOM_HID_DataIn, /*DataIn*/ + USBD_CUSTOM_HID_DataOut, + NULL, /*SOF */ + NULL, + NULL, + USBD_CUSTOM_HID_GetHSCfgDesc, + USBD_CUSTOM_HID_GetFSCfgDesc, + USBD_CUSTOM_HID_GetOtherSpeedCfgDesc, + USBD_CUSTOM_HID_GetDeviceQualifierDesc, +}; + +/* USB CUSTOM_HID device FS Configuration Descriptor */ +__ALIGN_BEGIN static uint8_t USBD_CUSTOM_HID_CfgFSDesc[USB_CUSTOM_HID_CONFIG_DESC_SIZ] __ALIGN_END = +{ + 0x09, /* bLength: Configuration Descriptor size */ + USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ + USB_CUSTOM_HID_CONFIG_DESC_SIZ, + /* wTotalLength: Bytes returned */ + 0x00, + 0x01, /*bNumInterfaces: 1 interface*/ + 0x01, /*bConfigurationValue: Configuration value*/ + 0x00, /*iConfiguration: Index of string descriptor describing + the configuration*/ + 0xC0, /*bmAttributes: bus powered */ + 0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/ + + /************** Descriptor of CUSTOM HID interface ****************/ + /* 09 */ + 0x09, /*bLength: Interface Descriptor size*/ + USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/ + 0x00, /*bInterfaceNumber: Number of Interface*/ + 0x00, /*bAlternateSetting: Alternate setting*/ + 0x02, /*bNumEndpoints*/ + 0x03, /*bInterfaceClass: CUSTOM_HID*/ + 0x00, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/ + 0x00, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/ + 0, /*iInterface: Index of string descriptor*/ + /******************** Descriptor of CUSTOM_HID *************************/ + /* 18 */ + 0x09, /*bLength: CUSTOM_HID Descriptor size*/ + CUSTOM_HID_DESCRIPTOR_TYPE, /*bDescriptorType: CUSTOM_HID*/ + 0x11, /*bCUSTOM_HIDUSTOM_HID: CUSTOM_HID Class Spec release number*/ + 0x01, + 0x00, /*bCountryCode: Hardware target country*/ + 0x01, /*bNumDescriptors: Number of CUSTOM_HID class descriptors to follow*/ + 0x22, /*bDescriptorType*/ + USBD_CUSTOM_HID_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ + 0x00, + /******************** Descriptor of Custom HID endpoints ********************/ + /* 27 */ + 0x07, /*bLength: Endpoint Descriptor size*/ + USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/ + + CUSTOM_HID_EPIN_ADDR, /*bEndpointAddress: Endpoint Address (IN)*/ + 0x03, /*bmAttributes: Interrupt endpoint*/ + CUSTOM_HID_EPIN_SIZE, /*wMaxPacketSize: 2 Byte max */ + 0x00, + CUSTOM_HID_FS_BINTERVAL, /*bInterval: Polling Interval */ + /* 34 */ + + 0x07, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: */ + CUSTOM_HID_EPOUT_ADDR, /*bEndpointAddress: Endpoint Address (OUT)*/ + 0x03, /* bmAttributes: Interrupt endpoint */ + CUSTOM_HID_EPOUT_SIZE, /* wMaxPacketSize: 2 Bytes max */ + 0x00, + CUSTOM_HID_FS_BINTERVAL, /* bInterval: Polling Interval */ + /* 41 */ +}; + +/* USB CUSTOM_HID device HS Configuration Descriptor */ +__ALIGN_BEGIN static uint8_t USBD_CUSTOM_HID_CfgHSDesc[USB_CUSTOM_HID_CONFIG_DESC_SIZ] __ALIGN_END = +{ + 0x09, /* bLength: Configuration Descriptor size */ + USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ + USB_CUSTOM_HID_CONFIG_DESC_SIZ, + /* wTotalLength: Bytes returned */ + 0x00, + 0x01, /*bNumInterfaces: 1 interface*/ + 0x01, /*bConfigurationValue: Configuration value*/ + 0x00, /*iConfiguration: Index of string descriptor describing + the configuration*/ + 0xC0, /*bmAttributes: bus powered */ + 0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/ + + /************** Descriptor of CUSTOM HID interface ****************/ + /* 09 */ + 0x09, /*bLength: Interface Descriptor size*/ + USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/ + 0x00, /*bInterfaceNumber: Number of Interface*/ + 0x00, /*bAlternateSetting: Alternate setting*/ + 0x02, /*bNumEndpoints*/ + 0x03, /*bInterfaceClass: CUSTOM_HID*/ + 0x00, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/ + 0x00, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/ + 0, /*iInterface: Index of string descriptor*/ + /******************** Descriptor of CUSTOM_HID *************************/ + /* 18 */ + 0x09, /*bLength: CUSTOM_HID Descriptor size*/ + CUSTOM_HID_DESCRIPTOR_TYPE, /*bDescriptorType: CUSTOM_HID*/ + 0x11, /*bCUSTOM_HIDUSTOM_HID: CUSTOM_HID Class Spec release number*/ + 0x01, + 0x00, /*bCountryCode: Hardware target country*/ + 0x01, /*bNumDescriptors: Number of CUSTOM_HID class descriptors to follow*/ + 0x22, /*bDescriptorType*/ + USBD_CUSTOM_HID_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ + 0x00, + /******************** Descriptor of Custom HID endpoints ********************/ + /* 27 */ + 0x07, /*bLength: Endpoint Descriptor size*/ + USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/ + + CUSTOM_HID_EPIN_ADDR, /*bEndpointAddress: Endpoint Address (IN)*/ + 0x03, /*bmAttributes: Interrupt endpoint*/ + CUSTOM_HID_EPIN_SIZE, /*wMaxPacketSize: 2 Byte max */ + 0x00, + CUSTOM_HID_HS_BINTERVAL, /*bInterval: Polling Interval */ + /* 34 */ + + 0x07, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: */ + CUSTOM_HID_EPOUT_ADDR, /*bEndpointAddress: Endpoint Address (OUT)*/ + 0x03, /* bmAttributes: Interrupt endpoint */ + CUSTOM_HID_EPOUT_SIZE, /* wMaxPacketSize: 2 Bytes max */ + 0x00, + CUSTOM_HID_HS_BINTERVAL, /* bInterval: Polling Interval */ + /* 41 */ +}; + +/* USB CUSTOM_HID device Other Speed Configuration Descriptor */ +__ALIGN_BEGIN static uint8_t USBD_CUSTOM_HID_OtherSpeedCfgDesc[USB_CUSTOM_HID_CONFIG_DESC_SIZ] __ALIGN_END = +{ + 0x09, /* bLength: Configuration Descriptor size */ + USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ + USB_CUSTOM_HID_CONFIG_DESC_SIZ, + /* wTotalLength: Bytes returned */ + 0x00, + 0x01, /*bNumInterfaces: 1 interface*/ + 0x01, /*bConfigurationValue: Configuration value*/ + 0x00, /*iConfiguration: Index of string descriptor describing + the configuration*/ + 0xC0, /*bmAttributes: bus powered */ + 0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/ + + /************** Descriptor of CUSTOM HID interface ****************/ + /* 09 */ + 0x09, /*bLength: Interface Descriptor size*/ + USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/ + 0x00, /*bInterfaceNumber: Number of Interface*/ + 0x00, /*bAlternateSetting: Alternate setting*/ + 0x02, /*bNumEndpoints*/ + 0x03, /*bInterfaceClass: CUSTOM_HID*/ + 0x00, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/ + 0x00, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/ + 0, /*iInterface: Index of string descriptor*/ + /******************** Descriptor of CUSTOM_HID *************************/ + /* 18 */ + 0x09, /*bLength: CUSTOM_HID Descriptor size*/ + CUSTOM_HID_DESCRIPTOR_TYPE, /*bDescriptorType: CUSTOM_HID*/ + 0x11, /*bCUSTOM_HIDUSTOM_HID: CUSTOM_HID Class Spec release number*/ + 0x01, + 0x00, /*bCountryCode: Hardware target country*/ + 0x01, /*bNumDescriptors: Number of CUSTOM_HID class descriptors to follow*/ + 0x22, /*bDescriptorType*/ + USBD_CUSTOM_HID_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ + 0x00, + /******************** Descriptor of Custom HID endpoints ********************/ + /* 27 */ + 0x07, /*bLength: Endpoint Descriptor size*/ + USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/ + + CUSTOM_HID_EPIN_ADDR, /*bEndpointAddress: Endpoint Address (IN)*/ + 0x03, /*bmAttributes: Interrupt endpoint*/ + CUSTOM_HID_EPIN_SIZE, /*wMaxPacketSize: 2 Byte max */ + 0x00, + CUSTOM_HID_FS_BINTERVAL, /*bInterval: Polling Interval */ + /* 34 */ + + 0x07, /* bLength: Endpoint Descriptor size */ + USB_DESC_TYPE_ENDPOINT, /* bDescriptorType: */ + CUSTOM_HID_EPOUT_ADDR, /*bEndpointAddress: Endpoint Address (OUT)*/ + 0x03, /* bmAttributes: Interrupt endpoint */ + CUSTOM_HID_EPOUT_SIZE, /* wMaxPacketSize: 2 Bytes max */ + 0x00, + CUSTOM_HID_FS_BINTERVAL, /* bInterval: Polling Interval */ + /* 41 */ +}; + +/* USB CUSTOM_HID device Configuration Descriptor */ +__ALIGN_BEGIN static uint8_t USBD_CUSTOM_HID_Desc[USB_CUSTOM_HID_DESC_SIZ] __ALIGN_END = +{ + /* 18 */ + 0x09, /*bLength: CUSTOM_HID Descriptor size*/ + CUSTOM_HID_DESCRIPTOR_TYPE, /*bDescriptorType: CUSTOM_HID*/ + 0x11, /*bCUSTOM_HIDUSTOM_HID: CUSTOM_HID Class Spec release number*/ + 0x01, + 0x00, /*bCountryCode: Hardware target country*/ + 0x01, /*bNumDescriptors: Number of CUSTOM_HID class descriptors to follow*/ + 0x22, /*bDescriptorType*/ + USBD_CUSTOM_HID_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ + 0x00, +}; + +/* USB Standard Device Descriptor */ +__ALIGN_BEGIN static uint8_t USBD_CUSTOM_HID_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END = +{ + USB_LEN_DEV_QUALIFIER_DESC, + USB_DESC_TYPE_DEVICE_QUALIFIER, + 0x00, + 0x02, + 0x00, + 0x00, + 0x00, + 0x40, + 0x01, + 0x00, +}; + +/** + * @} + */ + +/** @defgroup USBD_CUSTOM_HID_Private_Functions + * @{ + */ + +/** + * @brief USBD_CUSTOM_HID_Init + * Initialize the CUSTOM_HID interface + * @param pdev: device instance + * @param cfgidx: Configuration index + * @retval status + */ +static uint8_t USBD_CUSTOM_HID_Init (USBD_HandleTypeDef *pdev, + uint8_t cfgidx) +{ + uint8_t ret = 0U; + USBD_CUSTOM_HID_HandleTypeDef *hhid; + + /* Open EP IN */ + USBD_LL_OpenEP(pdev, CUSTOM_HID_EPIN_ADDR, USBD_EP_TYPE_INTR, + CUSTOM_HID_EPIN_SIZE); + + pdev->ep_in[CUSTOM_HID_EPIN_ADDR & 0xFU].is_used = 1U; + + /* Open EP OUT */ + USBD_LL_OpenEP(pdev, CUSTOM_HID_EPOUT_ADDR, USBD_EP_TYPE_INTR, + CUSTOM_HID_EPOUT_SIZE); + + pdev->ep_out[CUSTOM_HID_EPOUT_ADDR & 0xFU].is_used = 1U; + + pdev->pClassData = USBD_malloc(sizeof (USBD_CUSTOM_HID_HandleTypeDef)); + + if(pdev->pClassData == NULL) + { + ret = 1U; + } + else + { + hhid = (USBD_CUSTOM_HID_HandleTypeDef*) pdev->pClassData; + + hhid->state = CUSTOM_HID_IDLE; + ((USBD_CUSTOM_HID_ItfTypeDef *)pdev->pUserData)->Init(); + + /* Prepare Out endpoint to receive 1st packet */ + USBD_LL_PrepareReceive(pdev, CUSTOM_HID_EPOUT_ADDR, hhid->Report_buf, + USBD_CUSTOMHID_OUTREPORT_BUF_SIZE); + } + + return ret; +} + +/** + * @brief USBD_CUSTOM_HID_Init + * DeInitialize the CUSTOM_HID layer + * @param pdev: device instance + * @param cfgidx: Configuration index + * @retval status + */ +static uint8_t USBD_CUSTOM_HID_DeInit (USBD_HandleTypeDef *pdev, + uint8_t cfgidx) +{ + /* Close CUSTOM_HID EP IN */ + USBD_LL_CloseEP(pdev, CUSTOM_HID_EPIN_ADDR); + pdev->ep_in[CUSTOM_HID_EPIN_ADDR & 0xFU].is_used = 0U; + + /* Close CUSTOM_HID EP OUT */ + USBD_LL_CloseEP(pdev, CUSTOM_HID_EPOUT_ADDR); + pdev->ep_out[CUSTOM_HID_EPOUT_ADDR & 0xFU].is_used = 0U; + + /* FRee allocated memory */ + if(pdev->pClassData != NULL) + { + ((USBD_CUSTOM_HID_ItfTypeDef *)pdev->pUserData)->DeInit(); + USBD_free(pdev->pClassData); + pdev->pClassData = NULL; + } + return USBD_OK; +} + +/** + * @brief USBD_CUSTOM_HID_Setup + * Handle the CUSTOM_HID specific requests + * @param pdev: instance + * @param req: usb requests + * @retval status + */ +static uint8_t USBD_CUSTOM_HID_Setup (USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req) +{ + USBD_CUSTOM_HID_HandleTypeDef *hhid = (USBD_CUSTOM_HID_HandleTypeDef*)pdev->pClassData; + uint16_t len = 0U; + uint8_t *pbuf = NULL; + uint16_t status_info = 0U; + uint8_t ret = USBD_OK; + + switch (req->bmRequest & USB_REQ_TYPE_MASK) + { + case USB_REQ_TYPE_CLASS : + switch (req->bRequest) + { + case CUSTOM_HID_REQ_SET_PROTOCOL: + hhid->Protocol = (uint8_t)(req->wValue); + break; + + case CUSTOM_HID_REQ_GET_PROTOCOL: + USBD_CtlSendData (pdev, (uint8_t *)(void *)&hhid->Protocol, 1U); + break; + + case CUSTOM_HID_REQ_SET_IDLE: + hhid->IdleState = (uint8_t)(req->wValue >> 8); + break; + + case CUSTOM_HID_REQ_GET_IDLE: + USBD_CtlSendData (pdev, (uint8_t *)(void *)&hhid->IdleState, 1U); + break; + + case CUSTOM_HID_REQ_SET_REPORT: + hhid->IsReportAvailable = 1U; + USBD_CtlPrepareRx (pdev, hhid->Report_buf, req->wLength); + break; + + default: + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + break; + } + break; + + case USB_REQ_TYPE_STANDARD: + switch (req->bRequest) + { + case USB_REQ_GET_STATUS: + if (pdev->dev_state == USBD_STATE_CONFIGURED) + { + USBD_CtlSendData (pdev, (uint8_t *)(void *)&status_info, 2U); + } + else + { + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + } + break; + + case USB_REQ_GET_DESCRIPTOR: + if( req->wValue >> 8 == CUSTOM_HID_REPORT_DESC) + { + len = MIN(USBD_CUSTOM_HID_REPORT_DESC_SIZE , req->wLength); + pbuf = ((USBD_CUSTOM_HID_ItfTypeDef *)pdev->pUserData)->pReport; + } + else + { + if( req->wValue >> 8 == CUSTOM_HID_DESCRIPTOR_TYPE) + { + pbuf = USBD_CUSTOM_HID_Desc; + len = MIN(USB_CUSTOM_HID_DESC_SIZ , req->wLength); + } + } + + USBD_CtlSendData (pdev, pbuf, len); + break; + + case USB_REQ_GET_INTERFACE : + if (pdev->dev_state == USBD_STATE_CONFIGURED) + { + USBD_CtlSendData (pdev, (uint8_t *)(void *)&hhid->AltSetting, 1U); + } + else + { + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + } + break; + + case USB_REQ_SET_INTERFACE : + if (pdev->dev_state == USBD_STATE_CONFIGURED) + { + hhid->AltSetting = (uint8_t)(req->wValue); + } + else + { + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + } + break; + + default: + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + break; + } + break; + + default: + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + break; + } + return ret; +} + +/** + * @brief USBD_CUSTOM_HID_SendReport + * Send CUSTOM_HID Report + * @param pdev: device instance + * @param buff: pointer to report + * @retval status + */ +uint8_t USBD_CUSTOM_HID_SendReport (USBD_HandleTypeDef *pdev, + uint8_t *report, + uint16_t len) +{ + USBD_CUSTOM_HID_HandleTypeDef *hhid = (USBD_CUSTOM_HID_HandleTypeDef*)pdev->pClassData; + + if (pdev->dev_state == USBD_STATE_CONFIGURED ) + { + if(hhid->state == CUSTOM_HID_IDLE) + { + hhid->state = CUSTOM_HID_BUSY; + USBD_LL_Transmit (pdev, CUSTOM_HID_EPIN_ADDR, report, len); + } + else + { + return USBD_BUSY; + } + } + return USBD_OK; +} + +/** + * @brief USBD_CUSTOM_HID_GetFSCfgDesc + * return FS configuration descriptor + * @param speed : current device speed + * @param length : pointer data length + * @retval pointer to descriptor buffer + */ +static uint8_t *USBD_CUSTOM_HID_GetFSCfgDesc (uint16_t *length) +{ + *length = sizeof (USBD_CUSTOM_HID_CfgFSDesc); + return USBD_CUSTOM_HID_CfgFSDesc; +} + +/** + * @brief USBD_CUSTOM_HID_GetHSCfgDesc + * return HS configuration descriptor + * @param speed : current device speed + * @param length : pointer data length + * @retval pointer to descriptor buffer + */ +static uint8_t *USBD_CUSTOM_HID_GetHSCfgDesc (uint16_t *length) +{ + *length = sizeof (USBD_CUSTOM_HID_CfgHSDesc); + return USBD_CUSTOM_HID_CfgHSDesc; +} + +/** + * @brief USBD_CUSTOM_HID_GetOtherSpeedCfgDesc + * return other speed configuration descriptor + * @param speed : current device speed + * @param length : pointer data length + * @retval pointer to descriptor buffer + */ +static uint8_t *USBD_CUSTOM_HID_GetOtherSpeedCfgDesc (uint16_t *length) +{ + *length = sizeof (USBD_CUSTOM_HID_OtherSpeedCfgDesc); + return USBD_CUSTOM_HID_OtherSpeedCfgDesc; +} + +/** + * @brief USBD_CUSTOM_HID_DataIn + * handle data IN Stage + * @param pdev: device instance + * @param epnum: endpoint index + * @retval status + */ +static uint8_t USBD_CUSTOM_HID_DataIn (USBD_HandleTypeDef *pdev, + uint8_t epnum) +{ + /* Ensure that the FIFO is empty before a new transfer, this condition could + be caused by a new transfer before the end of the previous transfer */ + ((USBD_CUSTOM_HID_HandleTypeDef *)pdev->pClassData)->state = CUSTOM_HID_IDLE; + + return USBD_OK; +} + +/** + * @brief USBD_CUSTOM_HID_DataOut + * handle data OUT Stage + * @param pdev: device instance + * @param epnum: endpoint index + * @retval status + */ +static uint8_t USBD_CUSTOM_HID_DataOut (USBD_HandleTypeDef *pdev, + uint8_t epnum) +{ + + USBD_CUSTOM_HID_HandleTypeDef *hhid = (USBD_CUSTOM_HID_HandleTypeDef*)pdev->pClassData; + + ((USBD_CUSTOM_HID_ItfTypeDef *)pdev->pUserData)->OutEvent(hhid->Report_buf[0], + hhid->Report_buf[1]); + + USBD_LL_PrepareReceive(pdev, CUSTOM_HID_EPOUT_ADDR , hhid->Report_buf, + USBD_CUSTOMHID_OUTREPORT_BUF_SIZE); + + return USBD_OK; +} + +/** + * @brief USBD_CUSTOM_HID_EP0_RxReady + * Handles control request data. + * @param pdev: device instance + * @retval status + */ +static uint8_t USBD_CUSTOM_HID_EP0_RxReady(USBD_HandleTypeDef *pdev) +{ + USBD_CUSTOM_HID_HandleTypeDef *hhid = (USBD_CUSTOM_HID_HandleTypeDef*)pdev->pClassData; + + if (hhid->IsReportAvailable == 1U) + { + ((USBD_CUSTOM_HID_ItfTypeDef *)pdev->pUserData)->OutEvent(hhid->Report_buf[0], + hhid->Report_buf[1]); + hhid->IsReportAvailable = 0U; + } + + return USBD_OK; +} + +/** +* @brief DeviceQualifierDescriptor +* return Device Qualifier descriptor +* @param length : pointer data length +* @retval pointer to descriptor buffer +*/ +static uint8_t *USBD_CUSTOM_HID_GetDeviceQualifierDesc (uint16_t *length) +{ + *length = sizeof (USBD_CUSTOM_HID_DeviceQualifierDesc); + return USBD_CUSTOM_HID_DeviceQualifierDesc; +} + +/** +* @brief USBD_CUSTOM_HID_RegisterInterface + * @param pdev: device instance + * @param fops: CUSTOMHID Interface callback + * @retval status + */ +uint8_t USBD_CUSTOM_HID_RegisterInterface (USBD_HandleTypeDef *pdev, + USBD_CUSTOM_HID_ItfTypeDef *fops) +{ + uint8_t ret = USBD_FAIL; + + if(fops != NULL) + { + pdev->pUserData= fops; + ret = USBD_OK; + } + + return ret; +} +/** + * @} + */ + + +/** + * @} + */ + + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Class/CustomHID/Src/usbd_customhid_if_template.c b/system/Middlewares/ST/STM32_USB_Device_Library/Class/CustomHID/Src/usbd_customhid_if_template.c index ef22454746..36e85d2f56 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Class/CustomHID/Src/usbd_customhid_if_template.c +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Class/CustomHID/Src/usbd_customhid_if_template.c @@ -1,87 +1,87 @@ -/** - ****************************************************************************** - * @file usbd_customhid_if_template.c - * @author MCD Application Team - * @brief USB Device Custom HID interface file. - * This template should be copied to the user folder, renamed and customized - * following user needs. - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - - /* BSPDependencies - - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" - - "stm32xxxxx_{eval}{discovery}_io.c" - EndBSPDependencies */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_customhid_if_template.h" -/* Private typedef -----------------------------------------------------------*/ -/* Private define ------------------------------------------------------------*/ -/* Private macro -------------------------------------------------------------*/ -/* Private function prototypes -----------------------------------------------*/ - -static int8_t TEMPLATE_CUSTOM_HID_Init (void); -static int8_t TEMPLATE_CUSTOM_HID_DeInit (void); -static int8_t TEMPLATE_CUSTOM_HID_OutEvent (uint8_t event_idx, uint8_t state); -/* Private variables ---------------------------------------------------------*/ -USBD_CUSTOM_HID_ItfTypeDef USBD_CustomHID_template_fops = -{ - TEMPLATE_CUSTOM_HID_ReportDesc, - TEMPLATE_CUSTOM_HID_Init, - TEMPLATE_CUSTOM_HID_DeInit, - TEMPLATE_CUSTOM_HID_OutEvent, -}; - -/* Private functions ---------------------------------------------------------*/ - -/** - * @brief TEMPLATE_CUSTOM_HID_Init - * Initializes the CUSTOM HID media low layer - * @param None - * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL - */ -static int8_t TEMPLATE_CUSTOM_HID_Init(void) -{ - - return (0); -} - -/** - * @brief TEMPLATE_CUSTOM_HID_DeInit - * DeInitializes the CUSTOM HID media low layer - * @param None - * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL - */ -static int8_t TEMPLATE_CUSTOM_HID_DeInit(void) -{ - /* - Add your deinitialization code here - */ - return (0); -} - - -/** - * @brief TEMPLATE_CUSTOM_HID_Control - * Manage the CUSTOM HID class events - * @param event_idx: event index - * @param state: event state - * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL - */ -static int8_t TEMPLATE_CUSTOM_HID_OutEvent (uint8_t event_idx, uint8_t state) -{ - - return (0); -} -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_customhid_if_template.c + * @author MCD Application Team + * @brief USB Device Custom HID interface file. + * This template should be copied to the user folder, renamed and customized + * following user needs. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + + /* BSPDependencies + - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" + - "stm32xxxxx_{eval}{discovery}_io.c" + EndBSPDependencies */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_customhid_if_template.h" +/* Private typedef -----------------------------------------------------------*/ +/* Private define ------------------------------------------------------------*/ +/* Private macro -------------------------------------------------------------*/ +/* Private function prototypes -----------------------------------------------*/ + +static int8_t TEMPLATE_CUSTOM_HID_Init (void); +static int8_t TEMPLATE_CUSTOM_HID_DeInit (void); +static int8_t TEMPLATE_CUSTOM_HID_OutEvent (uint8_t event_idx, uint8_t state); +/* Private variables ---------------------------------------------------------*/ +USBD_CUSTOM_HID_ItfTypeDef USBD_CustomHID_template_fops = +{ + TEMPLATE_CUSTOM_HID_ReportDesc, + TEMPLATE_CUSTOM_HID_Init, + TEMPLATE_CUSTOM_HID_DeInit, + TEMPLATE_CUSTOM_HID_OutEvent, +}; + +/* Private functions ---------------------------------------------------------*/ + +/** + * @brief TEMPLATE_CUSTOM_HID_Init + * Initializes the CUSTOM HID media low layer + * @param None + * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL + */ +static int8_t TEMPLATE_CUSTOM_HID_Init(void) +{ + + return (0); +} + +/** + * @brief TEMPLATE_CUSTOM_HID_DeInit + * DeInitializes the CUSTOM HID media low layer + * @param None + * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL + */ +static int8_t TEMPLATE_CUSTOM_HID_DeInit(void) +{ + /* + Add your deinitialization code here + */ + return (0); +} + + +/** + * @brief TEMPLATE_CUSTOM_HID_Control + * Manage the CUSTOM HID class events + * @param event_idx: event index + * @param state: event state + * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL + */ +static int8_t TEMPLATE_CUSTOM_HID_OutEvent (uint8_t event_idx, uint8_t state) +{ + + return (0); +} +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Class/DFU/Inc/usbd_dfu.h b/system/Middlewares/ST/STM32_USB_Device_Library/Class/DFU/Inc/usbd_dfu.h index 12c863bb5e..4bc64e4902 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Class/DFU/Inc/usbd_dfu.h +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Class/DFU/Inc/usbd_dfu.h @@ -1,237 +1,237 @@ -/** - ****************************************************************************** - * @file usbd_dfu.h - * @author MCD Application Team - * @brief Header file for the usbd_dfu.c file. - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_DFU_H -#define __USB_DFU_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_ioreq.h" - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - -/** @defgroup USBD_DFU - * @brief This file is the Header file for usbd_dfu.c - * @{ - */ - - -/** @defgroup USBD_DFU_Exported_Defines - * @{ - */ -#ifndef USBD_DFU_MAX_ITF_NUM -#define USBD_DFU_MAX_ITF_NUM 1U -#endif /* USBD_DFU_MAX_ITF_NUM */ - -#ifndef USBD_DFU_XFER_SIZE -#define USBD_DFU_XFER_SIZE 1024U -#endif /* USBD_DFU_XFER_SIZE */ - -#ifndef USBD_DFU_APP_DEFAULT_ADD -#define USBD_DFU_APP_DEFAULT_ADD 0x08008000U /* The first sector (32 KB) is reserved for DFU code */ -#endif /* USBD_DFU_APP_DEFAULT_ADD */ - -#define USB_DFU_CONFIG_DESC_SIZ (18U + (9U * USBD_DFU_MAX_ITF_NUM)) -#define USB_DFU_DESC_SIZ 9U - -#define DFU_DESCRIPTOR_TYPE 0x21U - - -/**************************************************/ -/* DFU Requests DFU states */ -/**************************************************/ -#define APP_STATE_IDLE 0U -#define APP_STATE_DETACH 1U -#define DFU_STATE_IDLE 2U -#define DFU_STATE_DNLOAD_SYNC 3U -#define DFU_STATE_DNLOAD_BUSY 4U -#define DFU_STATE_DNLOAD_IDLE 5U -#define DFU_STATE_MANIFEST_SYNC 6U -#define DFU_STATE_MANIFEST 7U -#define DFU_STATE_MANIFEST_WAIT_RESET 8U -#define DFU_STATE_UPLOAD_IDLE 9U -#define DFU_STATE_ERROR 10U - -/**************************************************/ -/* DFU errors */ -/**************************************************/ -#define DFU_ERROR_NONE 0x00U -#define DFU_ERROR_TARGET 0x01U -#define DFU_ERROR_FILE 0x02U -#define DFU_ERROR_WRITE 0x03U -#define DFU_ERROR_ERASE 0x04U -#define DFU_ERROR_CHECK_ERASED 0x05U -#define DFU_ERROR_PROG 0x06U -#define DFU_ERROR_VERIFY 0x07U -#define DFU_ERROR_ADDRESS 0x08U -#define DFU_ERROR_NOTDONE 0x09U -#define DFU_ERROR_FIRMWARE 0x0AU -#define DFU_ERROR_VENDOR 0x0BU -#define DFU_ERROR_USB 0x0CU -#define DFU_ERROR_POR 0x0DU -#define DFU_ERROR_UNKNOWN 0x0EU -#define DFU_ERROR_STALLEDPKT 0x0FU - -/**************************************************/ -/* DFU Manifestation State */ -/**************************************************/ -#define DFU_MANIFEST_COMPLETE 0x00U -#define DFU_MANIFEST_IN_PROGRESS 0x01U - - -/**************************************************/ -/* Special Commands with Download Request */ -/**************************************************/ -#define DFU_CMD_GETCOMMANDS 0x00U -#define DFU_CMD_SETADDRESSPOINTER 0x21U -#define DFU_CMD_ERASE 0x41U - -#define DFU_MEDIA_ERASE 0x00U -#define DFU_MEDIA_PROGRAM 0x01U - -/**************************************************/ -/* Other defines */ -/**************************************************/ -/* Bit Detach capable = bit 3 in bmAttributes field */ -#define DFU_DETACH_MASK (uint8_t)(1 << 4) -#define DFU_STATUS_DEPTH 6U - -typedef enum -{ - DFU_DETACH = 0U, - DFU_DNLOAD , - DFU_UPLOAD, - DFU_GETSTATUS, - DFU_CLRSTATUS, - DFU_GETSTATE, - DFU_ABORT -} DFU_RequestTypeDef; - -typedef void (*pFunction)(void); - - -/********** Descriptor of DFU interface 0 Alternate setting n ****************/ -#define USBD_DFU_IF_DESC(n) 0x09, /* bLength: Interface Descriptor size */ \ - USB_DESC_TYPE_INTERFACE, /* bDescriptorType */ \ - 0x00, /* bInterfaceNumber: Number of Interface */ \ - (n), /* bAlternateSetting: Alternate setting */ \ - 0x00, /* bNumEndpoints*/ \ - 0xFE, /* bInterfaceClass: Application Specific Class Code */ \ - 0x01, /* bInterfaceSubClass : Device Firmware Upgrade Code */ \ - 0x02, /* nInterfaceProtocol: DFU mode protocol */ \ - USBD_IDX_INTERFACE_STR + (n) + 1U /* iInterface: Index of string descriptor */ \ - -#define TRANSFER_SIZE_BYTES(size) ((uint8_t)(size)), /* XFERSIZEB0 */\ - ((uint8_t)(size >> 8)) /* XFERSIZEB1 */ - -#define IS_PROTECTED_AREA(add) (uint8_t)(((add >= 0x08000000) && (add < (APP_DEFAULT_ADD)))? 1:0) - -/** - * @} - */ - - -/** @defgroup USBD_CORE_Exported_TypesDefinitions - * @{ - */ - -typedef struct -{ - union - { - uint32_t d32[USBD_DFU_XFER_SIZE / 4U]; - uint8_t d8[USBD_DFU_XFER_SIZE]; - }buffer; - - uint32_t wblock_num; - uint32_t wlength; - uint32_t data_ptr; - uint32_t alt_setting; - - uint8_t dev_status[DFU_STATUS_DEPTH]; - uint8_t ReservedForAlign[2]; - uint8_t dev_state; - uint8_t manif_state; -} -USBD_DFU_HandleTypeDef; - -typedef struct -{ - const uint8_t* pStrDesc; - uint16_t (* Init) (void); - uint16_t (* DeInit) (void); - uint16_t (* Erase) (uint32_t Add); - uint16_t (* Write) (uint8_t *src, uint8_t *dest, uint32_t Len); - uint8_t* (* Read) (uint8_t *src, uint8_t *dest, uint32_t Len); - uint16_t (* GetStatus)(uint32_t Add, uint8_t cmd, uint8_t *buff); -} -USBD_DFU_MediaTypeDef; -/** - * @} - */ - - - -/** @defgroup USBD_CORE_Exported_Macros - * @{ - */ - -/** - * @} - */ - -/** @defgroup USBD_CORE_Exported_Variables - * @{ - */ - -extern USBD_ClassTypeDef USBD_DFU; -#define USBD_DFU_CLASS &USBD_DFU -/** - * @} - */ - -/** @defgroup USB_CORE_Exported_Functions - * @{ - */ -uint8_t USBD_DFU_RegisterMedia (USBD_HandleTypeDef *pdev, - USBD_DFU_MediaTypeDef *fops); -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USB_DFU_H */ -/** - * @} - */ - -/** - * @} - */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_dfu.h + * @author MCD Application Team + * @brief Header file for the usbd_dfu.c file. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USB_DFU_H +#define __USB_DFU_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_ioreq.h" + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + +/** @defgroup USBD_DFU + * @brief This file is the Header file for usbd_dfu.c + * @{ + */ + + +/** @defgroup USBD_DFU_Exported_Defines + * @{ + */ +#ifndef USBD_DFU_MAX_ITF_NUM +#define USBD_DFU_MAX_ITF_NUM 1U +#endif /* USBD_DFU_MAX_ITF_NUM */ + +#ifndef USBD_DFU_XFER_SIZE +#define USBD_DFU_XFER_SIZE 1024U +#endif /* USBD_DFU_XFER_SIZE */ + +#ifndef USBD_DFU_APP_DEFAULT_ADD +#define USBD_DFU_APP_DEFAULT_ADD 0x08008000U /* The first sector (32 KB) is reserved for DFU code */ +#endif /* USBD_DFU_APP_DEFAULT_ADD */ + +#define USB_DFU_CONFIG_DESC_SIZ (18U + (9U * USBD_DFU_MAX_ITF_NUM)) +#define USB_DFU_DESC_SIZ 9U + +#define DFU_DESCRIPTOR_TYPE 0x21U + + +/**************************************************/ +/* DFU Requests DFU states */ +/**************************************************/ +#define APP_STATE_IDLE 0U +#define APP_STATE_DETACH 1U +#define DFU_STATE_IDLE 2U +#define DFU_STATE_DNLOAD_SYNC 3U +#define DFU_STATE_DNLOAD_BUSY 4U +#define DFU_STATE_DNLOAD_IDLE 5U +#define DFU_STATE_MANIFEST_SYNC 6U +#define DFU_STATE_MANIFEST 7U +#define DFU_STATE_MANIFEST_WAIT_RESET 8U +#define DFU_STATE_UPLOAD_IDLE 9U +#define DFU_STATE_ERROR 10U + +/**************************************************/ +/* DFU errors */ +/**************************************************/ +#define DFU_ERROR_NONE 0x00U +#define DFU_ERROR_TARGET 0x01U +#define DFU_ERROR_FILE 0x02U +#define DFU_ERROR_WRITE 0x03U +#define DFU_ERROR_ERASE 0x04U +#define DFU_ERROR_CHECK_ERASED 0x05U +#define DFU_ERROR_PROG 0x06U +#define DFU_ERROR_VERIFY 0x07U +#define DFU_ERROR_ADDRESS 0x08U +#define DFU_ERROR_NOTDONE 0x09U +#define DFU_ERROR_FIRMWARE 0x0AU +#define DFU_ERROR_VENDOR 0x0BU +#define DFU_ERROR_USB 0x0CU +#define DFU_ERROR_POR 0x0DU +#define DFU_ERROR_UNKNOWN 0x0EU +#define DFU_ERROR_STALLEDPKT 0x0FU + +/**************************************************/ +/* DFU Manifestation State */ +/**************************************************/ +#define DFU_MANIFEST_COMPLETE 0x00U +#define DFU_MANIFEST_IN_PROGRESS 0x01U + + +/**************************************************/ +/* Special Commands with Download Request */ +/**************************************************/ +#define DFU_CMD_GETCOMMANDS 0x00U +#define DFU_CMD_SETADDRESSPOINTER 0x21U +#define DFU_CMD_ERASE 0x41U + +#define DFU_MEDIA_ERASE 0x00U +#define DFU_MEDIA_PROGRAM 0x01U + +/**************************************************/ +/* Other defines */ +/**************************************************/ +/* Bit Detach capable = bit 3 in bmAttributes field */ +#define DFU_DETACH_MASK (uint8_t)(1 << 4) +#define DFU_STATUS_DEPTH 6U + +typedef enum +{ + DFU_DETACH = 0U, + DFU_DNLOAD , + DFU_UPLOAD, + DFU_GETSTATUS, + DFU_CLRSTATUS, + DFU_GETSTATE, + DFU_ABORT +} DFU_RequestTypeDef; + +typedef void (*pFunction)(void); + + +/********** Descriptor of DFU interface 0 Alternate setting n ****************/ +#define USBD_DFU_IF_DESC(n) 0x09, /* bLength: Interface Descriptor size */ \ + USB_DESC_TYPE_INTERFACE, /* bDescriptorType */ \ + 0x00, /* bInterfaceNumber: Number of Interface */ \ + (n), /* bAlternateSetting: Alternate setting */ \ + 0x00, /* bNumEndpoints*/ \ + 0xFE, /* bInterfaceClass: Application Specific Class Code */ \ + 0x01, /* bInterfaceSubClass : Device Firmware Upgrade Code */ \ + 0x02, /* nInterfaceProtocol: DFU mode protocol */ \ + USBD_IDX_INTERFACE_STR + (n) + 1U /* iInterface: Index of string descriptor */ \ + +#define TRANSFER_SIZE_BYTES(size) ((uint8_t)(size)), /* XFERSIZEB0 */\ + ((uint8_t)(size >> 8)) /* XFERSIZEB1 */ + +#define IS_PROTECTED_AREA(add) (uint8_t)(((add >= 0x08000000) && (add < (APP_DEFAULT_ADD)))? 1:0) + +/** + * @} + */ + + +/** @defgroup USBD_CORE_Exported_TypesDefinitions + * @{ + */ + +typedef struct +{ + union + { + uint32_t d32[USBD_DFU_XFER_SIZE / 4U]; + uint8_t d8[USBD_DFU_XFER_SIZE]; + }buffer; + + uint32_t wblock_num; + uint32_t wlength; + uint32_t data_ptr; + uint32_t alt_setting; + + uint8_t dev_status[DFU_STATUS_DEPTH]; + uint8_t ReservedForAlign[2]; + uint8_t dev_state; + uint8_t manif_state; +} +USBD_DFU_HandleTypeDef; + +typedef struct +{ + const uint8_t* pStrDesc; + uint16_t (* Init) (void); + uint16_t (* DeInit) (void); + uint16_t (* Erase) (uint32_t Add); + uint16_t (* Write) (uint8_t *src, uint8_t *dest, uint32_t Len); + uint8_t* (* Read) (uint8_t *src, uint8_t *dest, uint32_t Len); + uint16_t (* GetStatus)(uint32_t Add, uint8_t cmd, uint8_t *buff); +} +USBD_DFU_MediaTypeDef; +/** + * @} + */ + + + +/** @defgroup USBD_CORE_Exported_Macros + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_CORE_Exported_Variables + * @{ + */ + +extern USBD_ClassTypeDef USBD_DFU; +#define USBD_DFU_CLASS &USBD_DFU +/** + * @} + */ + +/** @defgroup USB_CORE_Exported_Functions + * @{ + */ +uint8_t USBD_DFU_RegisterMedia (USBD_HandleTypeDef *pdev, + USBD_DFU_MediaTypeDef *fops); +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USB_DFU_H */ +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Class/DFU/Inc/usbd_dfu_media_template.h b/system/Middlewares/ST/STM32_USB_Device_Library/Class/DFU/Inc/usbd_dfu_media_template.h index deb52f3389..195ccbbfac 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Class/DFU/Inc/usbd_dfu_media_template.h +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Class/DFU/Inc/usbd_dfu_media_template.h @@ -1,97 +1,97 @@ -/** - ****************************************************************************** - * @file usbd_dfu_media_template.h - * @author MCD Application Team - * @brief header file for the usbd_dfu_media_template.c file - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USBD_DFU_MEDIA_TEMPLATE_H -#define __USBD_DFU_MEDIA_TEMPLATE_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_dfu.h" - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - -/** @defgroup USBD_MEDIA - * @brief header file for the usbd_dfu_media_template.c file - * @{ - */ - -/** @defgroup USBD_MEDIA_Exported_Defines - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBD_MEDIA_Exported_Types - * @{ - */ - - -/** - * @} - */ - - - -/** @defgroup USBD_MEDIA_Exported_Macros - * @{ - */ - -/** - * @} - */ - -/** @defgroup USBD_MEDIA_Exported_Variables - * @{ - */ -extern USBD_DFU_MediaTypeDef USBD_DFU_MEDIA_Template_fops; -/** - * @} - */ - -/** @defgroup USBD_MEDIA_Exported_FunctionsPrototype - * @{ - */ - - -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USBD_DFU_MEDIA_TEMPLATE_H */ - -/** - * @} - */ - -/** -* @} -*/ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_dfu_media_template.h + * @author MCD Application Team + * @brief header file for the usbd_dfu_media_template.c file + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USBD_DFU_MEDIA_TEMPLATE_H +#define __USBD_DFU_MEDIA_TEMPLATE_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_dfu.h" + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + +/** @defgroup USBD_MEDIA + * @brief header file for the usbd_dfu_media_template.c file + * @{ + */ + +/** @defgroup USBD_MEDIA_Exported_Defines + * @{ + */ +/** + * @} + */ + + +/** @defgroup USBD_MEDIA_Exported_Types + * @{ + */ + + +/** + * @} + */ + + + +/** @defgroup USBD_MEDIA_Exported_Macros + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_MEDIA_Exported_Variables + * @{ + */ +extern USBD_DFU_MediaTypeDef USBD_DFU_MEDIA_Template_fops; +/** + * @} + */ + +/** @defgroup USBD_MEDIA_Exported_FunctionsPrototype + * @{ + */ + + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USBD_DFU_MEDIA_TEMPLATE_H */ + +/** + * @} + */ + +/** +* @} +*/ +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Class/DFU/Src/usbd_dfu.c b/system/Middlewares/ST/STM32_USB_Device_Library/Class/DFU/Src/usbd_dfu.c index 25da12524d..98bdf495cc 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Class/DFU/Src/usbd_dfu.c +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Class/DFU/Src/usbd_dfu.c @@ -1,1134 +1,1134 @@ -/** - ****************************************************************************** - * @file usbd_dfu.c - * @author MCD Application Team - * @brief This file provides the DFU core functions. - * - * @verbatim - * - * =================================================================== - * DFU Class Driver Description - * =================================================================== - * This driver manages the DFU class V1.1 following the "Device Class Specification for - * Device Firmware Upgrade Version 1.1 Aug 5, 2004". - * This driver implements the following aspects of the specification: - * - Device descriptor management - * - Configuration descriptor management - * - Enumeration as DFU device (in DFU mode only) - * - Requests management (supporting ST DFU sub-protocol) - * - Memory operations management (Download/Upload/Erase/Detach/GetState/GetStatus) - * - DFU state machine implementation. - * - * @note - * ST DFU sub-protocol is compliant with DFU protocol and use sub-requests to manage - * memory addressing, commands processing, specific memories operations (ie. Erase) ... - * As required by the DFU specification, only endpoint 0 is used in this application. - * Other endpoints and functions may be added to the application (ie. DFU ...) - * - * These aspects may be enriched or modified for a specific user application. - * - * This driver doesn't implement the following aspects of the specification - * (but it is possible to manage these features with some modifications on this driver): - * - Manifestation Tolerant mode - * - * @endverbatim - * - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - - /* BSPDependencies - - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" - - "stm32xxxxx_{eval}{discovery}_io.c" - EndBSPDependencies */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_dfu.h" -#include "usbd_ctlreq.h" - - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - - -/** @defgroup USBD_DFU - * @brief usbd core module - * @{ - */ - -/** @defgroup USBD_DFU_Private_TypesDefinitions - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBD_DFU_Private_Defines - * @{ - */ - -/** - * @} - */ - - -/** @defgroup USBD_DFU_Private_Macros - * @{ - */ - -/** - * @} - */ - - - - -/** @defgroup USBD_DFU_Private_FunctionPrototypes - * @{ - */ - - -static uint8_t USBD_DFU_Init (USBD_HandleTypeDef *pdev, - uint8_t cfgidx); - -static uint8_t USBD_DFU_DeInit (USBD_HandleTypeDef *pdev, - uint8_t cfgidx); - -static uint8_t USBD_DFU_Setup (USBD_HandleTypeDef *pdev, - USBD_SetupReqTypedef *req); - -static uint8_t *USBD_DFU_GetCfgDesc (uint16_t *length); - -static uint8_t *USBD_DFU_GetDeviceQualifierDesc (uint16_t *length); - -static uint8_t USBD_DFU_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum); - -static uint8_t USBD_DFU_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum); - -static uint8_t USBD_DFU_EP0_RxReady (USBD_HandleTypeDef *pdev); - -static uint8_t USBD_DFU_EP0_TxReady (USBD_HandleTypeDef *pdev); - -static uint8_t USBD_DFU_SOF (USBD_HandleTypeDef *pdev); - -static uint8_t USBD_DFU_IsoINIncomplete (USBD_HandleTypeDef *pdev, uint8_t epnum); - -static uint8_t USBD_DFU_IsoOutIncomplete (USBD_HandleTypeDef *pdev, uint8_t epnum); - -#if (USBD_SUPPORT_USER_STRING == 1U) -static uint8_t* USBD_DFU_GetUsrStringDesc ( USBD_HandleTypeDef *pdev, uint8_t index , uint16_t *length); -#endif - -static void DFU_Detach (USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req); - -static void DFU_Download (USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req); - -static void DFU_Upload (USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req); - -static void DFU_GetStatus (USBD_HandleTypeDef *pdev); - -static void DFU_ClearStatus (USBD_HandleTypeDef *pdev); - -static void DFU_GetState (USBD_HandleTypeDef *pdev); - -static void DFU_Abort (USBD_HandleTypeDef *pdev); - -static void DFU_Leave (USBD_HandleTypeDef *pdev); - - -/** - * @} - */ - -/** @defgroup USBD_DFU_Private_Variables - * @{ - */ - -USBD_ClassTypeDef USBD_DFU = -{ - USBD_DFU_Init, - USBD_DFU_DeInit, - USBD_DFU_Setup, - USBD_DFU_EP0_TxReady, - USBD_DFU_EP0_RxReady, - USBD_DFU_DataIn, - USBD_DFU_DataOut, - USBD_DFU_SOF, - USBD_DFU_IsoINIncomplete, - USBD_DFU_IsoOutIncomplete, - USBD_DFU_GetCfgDesc, - USBD_DFU_GetCfgDesc, - USBD_DFU_GetCfgDesc, - USBD_DFU_GetDeviceQualifierDesc, -#if (USBD_SUPPORT_USER_STRING == 1U) - USBD_DFU_GetUsrStringDesc -#endif -}; - -/* USB DFU device Configuration Descriptor */ -__ALIGN_BEGIN static uint8_t USBD_DFU_CfgDesc[USB_DFU_CONFIG_DESC_SIZ] __ALIGN_END = -{ - 0x09, /* bLength: Configuation Descriptor size */ - USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ - USB_DFU_CONFIG_DESC_SIZ, - /* wTotalLength: Bytes returned */ - 0x00, - 0x01, /*bNumInterfaces: 1 interface*/ - 0x01, /*bConfigurationValue: Configuration value*/ - 0x02, /*iConfiguration: Index of string descriptor describing the configuration*/ - 0xC0, /*bmAttributes: bus powered and Supprts Remote Wakeup */ - 0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/ - /* 09 */ - - /********** Descriptor of DFU interface 0 Alternate setting 0 **************/ - USBD_DFU_IF_DESC(0U), /* This interface is mandatory for all devices */ - -#if (USBD_DFU_MAX_ITF_NUM > 1U) - /********** Descriptor of DFU interface 0 Alternate setting 1 **************/ - USBD_DFU_IF_DESC(1), -#endif /* (USBD_DFU_MAX_ITF_NUM > 1) */ - -#if (USBD_DFU_MAX_ITF_NUM > 2U) - /********** Descriptor of DFU interface 0 Alternate setting 2 **************/ - USBD_DFU_IF_DESC(2), -#endif /* (USBD_DFU_MAX_ITF_NUM > 2) */ - -#if (USBD_DFU_MAX_ITF_NUM > 3U) - /********** Descriptor of DFU interface 0 Alternate setting 3 **************/ - USBD_DFU_IF_DESC(3), -#endif /* (USBD_DFU_MAX_ITF_NUM > 3) */ - -#if (USBD_DFU_MAX_ITF_NUM > 4U) - /********** Descriptor of DFU interface 0 Alternate setting 4 **************/ - USBD_DFU_IF_DESC(4), -#endif /* (USBD_DFU_MAX_ITF_NUM > 4) */ - -#if (USBD_DFU_MAX_ITF_NUM > 5U) - /********** Descriptor of DFU interface 0 Alternate setting 5 **************/ - USBD_DFU_IF_DESC(5), -#endif /* (USBD_DFU_MAX_ITF_NUM > 5) */ - -#if (USBD_DFU_MAX_ITF_NUM > 6U) -#error "ERROR: usbd_dfu_core.c: Modify the file to support more descriptors!" -#endif /* (USBD_DFU_MAX_ITF_NUM > 6) */ - - /******************** DFU Functional Descriptor********************/ - 0x09, /*blength = 9 Bytes*/ - DFU_DESCRIPTOR_TYPE, /* DFU Functional Descriptor*/ - 0x0B, /*bmAttribute - bitCanDnload = 1 (bit 0) - bitCanUpload = 1 (bit 1) - bitManifestationTolerant = 0 (bit 2) - bitWillDetach = 1 (bit 3) - Reserved (bit4-6) - bitAcceleratedST = 0 (bit 7)*/ - 0xFF, /*DetachTimeOut= 255 ms*/ - 0x00, - /*WARNING: In DMA mode the multiple MPS packets feature is still not supported - ==> In this case, when using DMA USBD_DFU_XFER_SIZE should be set to 64 in usbd_conf.h */ - TRANSFER_SIZE_BYTES(USBD_DFU_XFER_SIZE), /* TransferSize = 1024 Byte*/ - 0x1A, /* bcdDFUVersion*/ - 0x01 - /***********************************************************/ - /* 9*/ -}; - -/* USB Standard Device Descriptor */ -__ALIGN_BEGIN static uint8_t USBD_DFU_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END = -{ - USB_LEN_DEV_QUALIFIER_DESC, - USB_DESC_TYPE_DEVICE_QUALIFIER, - 0x00, - 0x02, - 0x00, - 0x00, - 0x00, - 0x40, - 0x01, - 0x00, -}; - -/** - * @} - */ - -/** @defgroup USBD_DFU_Private_Functions - * @{ - */ - -/** - * @brief USBD_DFU_Init - * Initialize the DFU interface - * @param pdev: device instance - * @param cfgidx: Configuration index - * @retval status - */ -static uint8_t USBD_DFU_Init (USBD_HandleTypeDef *pdev, - uint8_t cfgidx) -{ - USBD_DFU_HandleTypeDef *hdfu; - - /* Allocate Audio structure */ - pdev->pClassData = USBD_malloc(sizeof (USBD_DFU_HandleTypeDef)); - - if(pdev->pClassData == NULL) - { - return USBD_FAIL; - } - else - { - hdfu = (USBD_DFU_HandleTypeDef*) pdev->pClassData; - - hdfu->alt_setting = 0U; - hdfu->data_ptr = USBD_DFU_APP_DEFAULT_ADD; - hdfu->wblock_num = 0U; - hdfu->wlength = 0U; - - hdfu->manif_state = DFU_MANIFEST_COMPLETE; - hdfu->dev_state = DFU_STATE_IDLE; - - hdfu->dev_status[0] = DFU_ERROR_NONE; - hdfu->dev_status[1] = 0U; - hdfu->dev_status[2] = 0U; - hdfu->dev_status[3] = 0U; - hdfu->dev_status[4] = DFU_STATE_IDLE; - hdfu->dev_status[5] = 0U; - - /* Initialize Hardware layer */ - if (((USBD_DFU_MediaTypeDef *)pdev->pUserData)->Init() != USBD_OK) - { - return USBD_FAIL; - } - } - return USBD_OK; -} - -/** - * @brief USBD_DFU_Init - * De-Initialize the DFU layer - * @param pdev: device instance - * @param cfgidx: Configuration index - * @retval status - */ -static uint8_t USBD_DFU_DeInit (USBD_HandleTypeDef *pdev, - uint8_t cfgidx) -{ - USBD_DFU_HandleTypeDef *hdfu; - hdfu = (USBD_DFU_HandleTypeDef*) pdev->pClassData; - - hdfu->wblock_num = 0U; - hdfu->wlength = 0U; - - hdfu->dev_state = DFU_STATE_IDLE; - hdfu->dev_status[0] = DFU_ERROR_NONE; - hdfu->dev_status[4] = DFU_STATE_IDLE; - - /* DeInit physical Interface components */ - if(pdev->pClassData != NULL) - { - /* De-Initialize Hardware layer */ - ((USBD_DFU_MediaTypeDef *)pdev->pUserData)->DeInit(); - USBD_free(pdev->pClassData); - pdev->pClassData = NULL; - } - - return USBD_OK; -} - -/** - * @brief USBD_DFU_Setup - * Handle the DFU specific requests - * @param pdev: instance - * @param req: usb requests - * @retval status - */ -static uint8_t USBD_DFU_Setup (USBD_HandleTypeDef *pdev, - USBD_SetupReqTypedef *req) -{ - USBD_DFU_HandleTypeDef *hdfu; - uint8_t *pbuf = 0U; - uint16_t len = 0U; - uint16_t status_info = 0U; - uint8_t ret = USBD_OK; - - hdfu = (USBD_DFU_HandleTypeDef*) pdev->pClassData; - - switch (req->bmRequest & USB_REQ_TYPE_MASK) - { - case USB_REQ_TYPE_CLASS: - switch (req->bRequest) - { - case DFU_DNLOAD: - DFU_Download(pdev, req); - break; - - case DFU_UPLOAD: - DFU_Upload(pdev, req); - break; - - case DFU_GETSTATUS: - DFU_GetStatus(pdev); - break; - - case DFU_CLRSTATUS: - DFU_ClearStatus(pdev); - break; - - case DFU_GETSTATE: - DFU_GetState(pdev); - break; - - case DFU_ABORT: - DFU_Abort(pdev); - break; - - case DFU_DETACH: - DFU_Detach(pdev, req); - break; - - default: - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - break; - } - break; - - case USB_REQ_TYPE_STANDARD: - switch (req->bRequest) - { - case USB_REQ_GET_STATUS: - if (pdev->dev_state == USBD_STATE_CONFIGURED) - { - USBD_CtlSendData (pdev, (uint8_t *)(void *)&status_info, 2U); - } - else - { - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - } - break; - - case USB_REQ_GET_DESCRIPTOR: - if( (req->wValue >> 8) == DFU_DESCRIPTOR_TYPE) - { - pbuf = USBD_DFU_CfgDesc + (9U * (USBD_DFU_MAX_ITF_NUM + 1U)); - len = MIN(USB_DFU_DESC_SIZ , req->wLength); - } - - USBD_CtlSendData (pdev, pbuf, len); - break; - - case USB_REQ_GET_INTERFACE: - if (pdev->dev_state == USBD_STATE_CONFIGURED) - { - USBD_CtlSendData (pdev, (uint8_t *)(void *)&hdfu->alt_setting, 1U); - } - else - { - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - } - break; - - case USB_REQ_SET_INTERFACE: - if ((uint8_t)(req->wValue) < USBD_DFU_MAX_ITF_NUM) - { - if (pdev->dev_state == USBD_STATE_CONFIGURED) - { - hdfu->alt_setting = (uint8_t)(req->wValue); - } - else - { - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - } - } - else - { - /* Call the error management function (command will be nacked */ - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - } - break; - - default: - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - break; - } - break; - - default: - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - break; - } - - return ret; -} - - -/** - * @brief USBD_DFU_GetCfgDesc - * return configuration descriptor - * @param speed : current device speed - * @param length : pointer data length - * @retval pointer to descriptor buffer - */ -static uint8_t *USBD_DFU_GetCfgDesc (uint16_t *length) -{ - *length = sizeof (USBD_DFU_CfgDesc); - return USBD_DFU_CfgDesc; -} - -/** - * @brief USBD_DFU_DataIn - * handle data IN Stage - * @param pdev: device instance - * @param epnum: endpoint index - * @retval status - */ -static uint8_t USBD_DFU_DataIn (USBD_HandleTypeDef *pdev, - uint8_t epnum) -{ - - return USBD_OK; -} - -/** - * @brief USBD_DFU_EP0_RxReady - * handle EP0 Rx Ready event - * @param pdev: device instance - * @retval status - */ -static uint8_t USBD_DFU_EP0_RxReady (USBD_HandleTypeDef *pdev) -{ - - return USBD_OK; -} -/** - * @brief USBD_DFU_EP0_TxReady - * handle EP0 TRx Ready event - * @param pdev: device instance - * @retval status - */ -static uint8_t USBD_DFU_EP0_TxReady (USBD_HandleTypeDef *pdev) -{ - uint32_t addr; - USBD_SetupReqTypedef req; - USBD_DFU_HandleTypeDef *hdfu; - - hdfu = (USBD_DFU_HandleTypeDef*) pdev->pClassData; - - if (hdfu->dev_state == DFU_STATE_DNLOAD_BUSY) - { - /* Decode the Special Command*/ - if (hdfu->wblock_num == 0U) - { - if ((hdfu->buffer.d8[0] == DFU_CMD_GETCOMMANDS) && (hdfu->wlength == 1U)) - { - - } - else if ((hdfu->buffer.d8[0] == DFU_CMD_SETADDRESSPOINTER) && (hdfu->wlength == 5U)) - { - hdfu->data_ptr = hdfu->buffer.d8[1]; - hdfu->data_ptr += (uint32_t)hdfu->buffer.d8[2] << 8; - hdfu->data_ptr += (uint32_t)hdfu->buffer.d8[3] << 16; - hdfu->data_ptr += (uint32_t)hdfu->buffer.d8[4] << 24; - } - else if ((hdfu->buffer.d8[0] == DFU_CMD_ERASE) && (hdfu->wlength == 5U)) - { - hdfu->data_ptr = hdfu->buffer.d8[1]; - hdfu->data_ptr += (uint32_t)hdfu->buffer.d8[2] << 8; - hdfu->data_ptr += (uint32_t)hdfu->buffer.d8[3] << 16; - hdfu->data_ptr += (uint32_t)hdfu->buffer.d8[4] << 24; - - if (((USBD_DFU_MediaTypeDef *)pdev->pUserData)->Erase(hdfu->data_ptr) != USBD_OK) - { - return USBD_FAIL; - } - } - else - { - /* Reset the global length and block number */ - hdfu->wlength = 0U; - hdfu->wblock_num = 0U; - /* Call the error management function (command will be nacked) */ - req.bmRequest = 0U; - req.wLength = 1U; - USBD_CtlError (pdev, &req); - } - } - /* Regular Download Command */ - else - { - if (hdfu->wblock_num > 1U) - { - /* Decode the required address */ - addr = ((hdfu->wblock_num - 2U) * USBD_DFU_XFER_SIZE) + hdfu->data_ptr; - - /* Preform the write operation */ - if (((USBD_DFU_MediaTypeDef *)pdev->pUserData)->Write(hdfu->buffer.d8, - (uint8_t *)addr, hdfu->wlength) != USBD_OK) - { - return USBD_FAIL; - } - } - } - - /* Reset the global length and block number */ - hdfu->wlength = 0U; - hdfu->wblock_num = 0U; - - /* Update the state machine */ - hdfu->dev_state = DFU_STATE_DNLOAD_SYNC; - - hdfu->dev_status[1] = 0U; - hdfu->dev_status[2] = 0U; - hdfu->dev_status[3] = 0U; - hdfu->dev_status[4] = hdfu->dev_state; - return USBD_OK; - } - else - { - if (hdfu->dev_state == DFU_STATE_MANIFEST)/* Manifestation in progress */ - { - /* Start leaving DFU mode */ - DFU_Leave(pdev); - } - } - - return USBD_OK; -} -/** - * @brief USBD_DFU_SOF - * handle SOF event - * @param pdev: device instance - * @retval status - */ -static uint8_t USBD_DFU_SOF (USBD_HandleTypeDef *pdev) -{ - - return USBD_OK; -} -/** - * @brief USBD_DFU_IsoINIncomplete - * handle data ISO IN Incomplete event - * @param pdev: device instance - * @param epnum: endpoint index - * @retval status - */ -static uint8_t USBD_DFU_IsoINIncomplete (USBD_HandleTypeDef *pdev, uint8_t epnum) -{ - - return USBD_OK; -} -/** - * @brief USBD_DFU_IsoOutIncomplete - * handle data ISO OUT Incomplete event - * @param pdev: device instance - * @param epnum: endpoint index - * @retval status - */ -static uint8_t USBD_DFU_IsoOutIncomplete (USBD_HandleTypeDef *pdev, uint8_t epnum) -{ - - return USBD_OK; -} -/** - * @brief USBD_DFU_DataOut - * handle data OUT Stage - * @param pdev: device instance - * @param epnum: endpoint index - * @retval status - */ -static uint8_t USBD_DFU_DataOut (USBD_HandleTypeDef *pdev, - uint8_t epnum) -{ - - return USBD_OK; -} - -/** -* @brief DeviceQualifierDescriptor -* return Device Qualifier descriptor -* @param length : pointer data length -* @retval pointer to descriptor buffer -*/ -static uint8_t *USBD_DFU_GetDeviceQualifierDesc (uint16_t *length) -{ - *length = sizeof (USBD_DFU_DeviceQualifierDesc); - return USBD_DFU_DeviceQualifierDesc; -} - -/** - * @brief USBD_DFU_GetUsrStringDesc - * Manages the transfer of memory interfaces string descriptors. - * @param speed : current device speed - * @param index: desciptor index - * @param length : pointer data length - * @retval pointer to the descriptor table or NULL if the descriptor is not supported. - */ -#if (USBD_SUPPORT_USER_STRING == 1U) -static uint8_t* USBD_DFU_GetUsrStringDesc (USBD_HandleTypeDef *pdev, uint8_t index , uint16_t *length) -{ - static uint8_t USBD_StrDesc[255]; - /* Check if the requested string interface is supported */ - if (index <= (USBD_IDX_INTERFACE_STR + USBD_DFU_MAX_ITF_NUM)) - { - USBD_GetString ((uint8_t *)((USBD_DFU_MediaTypeDef *)pdev->pUserData)->pStrDesc, USBD_StrDesc, length); - return USBD_StrDesc; - } - /* Not supported Interface Descriptor index */ - else - { - return NULL; - } -} -#endif - -/** -* @brief USBD_MSC_RegisterStorage -* @param fops: storage callback -* @retval status -*/ -uint8_t USBD_DFU_RegisterMedia (USBD_HandleTypeDef *pdev, - USBD_DFU_MediaTypeDef *fops) -{ - if(fops != NULL) - { - pdev->pUserData= fops; - } - return 0U; -} - -/****************************************************************************** - DFU Class requests management -******************************************************************************/ -/** - * @brief DFU_Detach - * Handles the DFU DETACH request. - * @param pdev: device instance - * @param req: pointer to the request structure. - * @retval None. - */ -static void DFU_Detach(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req) -{ - USBD_DFU_HandleTypeDef *hdfu; - - hdfu = (USBD_DFU_HandleTypeDef*) pdev->pClassData; - - if (hdfu->dev_state == DFU_STATE_IDLE || hdfu->dev_state == DFU_STATE_DNLOAD_SYNC - || hdfu->dev_state == DFU_STATE_DNLOAD_IDLE || hdfu->dev_state == DFU_STATE_MANIFEST_SYNC - || hdfu->dev_state == DFU_STATE_UPLOAD_IDLE ) - { - /* Update the state machine */ - hdfu->dev_state = DFU_STATE_IDLE; - hdfu->dev_status[0] = DFU_ERROR_NONE; - hdfu->dev_status[1] = 0U; - hdfu->dev_status[2] = 0U; - hdfu->dev_status[3] = 0U; /*bwPollTimeout=0ms*/ - hdfu->dev_status[4] = hdfu->dev_state; - hdfu->dev_status[5] = 0U; /*iString*/ - hdfu->wblock_num = 0U; - hdfu->wlength = 0U; - } - - /* Check the detach capability in the DFU functional descriptor */ - if ((USBD_DFU_CfgDesc[12U + (9U * USBD_DFU_MAX_ITF_NUM)]) & DFU_DETACH_MASK) - { - /* Perform an Attach-Detach operation on USB bus */ - USBD_Stop (pdev); - USBD_Start (pdev); - } - else - { - /* Wait for the period of time specified in Detach request */ - USBD_Delay ((uint32_t)req->wValue); - } -} - -/** - * @brief DFU_Download - * Handles the DFU DNLOAD request. - * @param pdev: device instance - * @param req: pointer to the request structure - * @retval None - */ -static void DFU_Download(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req) -{ - USBD_DFU_HandleTypeDef *hdfu; - - hdfu = (USBD_DFU_HandleTypeDef*) pdev->pClassData; - - /* Data setup request */ - if (req->wLength > 0U) - { - if ((hdfu->dev_state == DFU_STATE_IDLE) || (hdfu->dev_state == DFU_STATE_DNLOAD_IDLE)) - { - /* Update the global length and block number */ - hdfu->wblock_num = req->wValue; - hdfu->wlength = req->wLength; - - /* Update the state machine */ - hdfu->dev_state = DFU_STATE_DNLOAD_SYNC; - hdfu->dev_status[4] = hdfu->dev_state; - - /* Prepare the reception of the buffer over EP0 */ - USBD_CtlPrepareRx (pdev, (uint8_t*)hdfu->buffer.d8, - (uint16_t)hdfu->wlength); - } - /* Unsupported state */ - else - { - /* Call the error management function (command will be nacked */ - USBD_CtlError (pdev, req); - } - } - /* 0 Data DNLOAD request */ - else - { - /* End of DNLOAD operation*/ - if (hdfu->dev_state == DFU_STATE_DNLOAD_IDLE || hdfu->dev_state == DFU_STATE_IDLE ) - { - hdfu->manif_state = DFU_MANIFEST_IN_PROGRESS; - hdfu->dev_state = DFU_STATE_MANIFEST_SYNC; - hdfu->dev_status[1] = 0U; - hdfu->dev_status[2] = 0U; - hdfu->dev_status[3] = 0U; - hdfu->dev_status[4] = hdfu->dev_state; - } - else - { - /* Call the error management function (command will be nacked */ - USBD_CtlError (pdev, req); - } - } -} - -/** - * @brief DFU_Upload - * Handles the DFU UPLOAD request. - * @param pdev: instance - * @param req: pointer to the request structure - * @retval status - */ -static void DFU_Upload(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req) -{ - USBD_DFU_HandleTypeDef *hdfu; - - hdfu = (USBD_DFU_HandleTypeDef*) pdev->pClassData; - - uint8_t *phaddr = NULL; - uint32_t addr = 0U; - - /* Data setup request */ - if (req->wLength > 0U) - { - if ((hdfu->dev_state == DFU_STATE_IDLE) || (hdfu->dev_state == DFU_STATE_UPLOAD_IDLE)) - { - /* Update the global length and block number */ - hdfu->wblock_num = req->wValue; - hdfu->wlength = req->wLength; - - /* DFU Get Command */ - if (hdfu->wblock_num == 0U) - { - /* Update the state machine */ - hdfu->dev_state = (hdfu->wlength > 3U)? DFU_STATE_IDLE:DFU_STATE_UPLOAD_IDLE; - - hdfu->dev_status[1] = 0U; - hdfu->dev_status[2] = 0U; - hdfu->dev_status[3] = 0U; - hdfu->dev_status[4] = hdfu->dev_state; - - /* Store the values of all supported commands */ - hdfu->buffer.d8[0] = DFU_CMD_GETCOMMANDS; - hdfu->buffer.d8[1] = DFU_CMD_SETADDRESSPOINTER; - hdfu->buffer.d8[2] = DFU_CMD_ERASE; - - /* Send the status data over EP0 */ - USBD_CtlSendData (pdev, (uint8_t *)(&(hdfu->buffer.d8[0])), 3U); - } - else if (hdfu->wblock_num > 1U) - { - hdfu->dev_state = DFU_STATE_UPLOAD_IDLE; - - hdfu->dev_status[1] = 0U; - hdfu->dev_status[2] = 0U; - hdfu->dev_status[3] = 0U; - hdfu->dev_status[4] = hdfu->dev_state; - - addr = ((hdfu->wblock_num - 2U) * USBD_DFU_XFER_SIZE) + hdfu->data_ptr; /* Change is Accelerated*/ - - /* Return the physical address where data are stored */ - phaddr = ((USBD_DFU_MediaTypeDef *)pdev->pUserData)->Read((uint8_t *)addr, hdfu->buffer.d8, hdfu->wlength); - - /* Send the status data over EP0 */ - USBD_CtlSendData (pdev, phaddr, (uint16_t)hdfu->wlength); - } - else /* unsupported hdfu->wblock_num */ - { - hdfu->dev_state = DFU_ERROR_STALLEDPKT; - - hdfu->dev_status[1] = 0U; - hdfu->dev_status[2] = 0U; - hdfu->dev_status[3] = 0U; - hdfu->dev_status[4] = hdfu->dev_state; - - /* Call the error management function (command will be nacked */ - USBD_CtlError (pdev, req); - } - } - /* Unsupported state */ - else - { - hdfu->wlength = 0U; - hdfu->wblock_num = 0U; - /* Call the error management function (command will be nacked */ - USBD_CtlError (pdev, req); - } - } - /* No Data setup request */ - else - { - hdfu->dev_state = DFU_STATE_IDLE; - - hdfu->dev_status[1] = 0U; - hdfu->dev_status[2] = 0U; - hdfu->dev_status[3] = 0U; - hdfu->dev_status[4] = hdfu->dev_state; - } -} - -/** - * @brief DFU_GetStatus - * Handles the DFU GETSTATUS request. - * @param pdev: instance - * @retval status - */ -static void DFU_GetStatus(USBD_HandleTypeDef *pdev) -{ - USBD_DFU_HandleTypeDef *hdfu; - - hdfu = (USBD_DFU_HandleTypeDef*) pdev->pClassData; - - switch (hdfu->dev_state) - { - case DFU_STATE_DNLOAD_SYNC: - if (hdfu->wlength != 0U) - { - hdfu->dev_state = DFU_STATE_DNLOAD_BUSY; - - hdfu->dev_status[1] = 0U; - hdfu->dev_status[2] = 0U; - hdfu->dev_status[3] = 0U; - hdfu->dev_status[4] = hdfu->dev_state; - - if ((hdfu->wblock_num == 0U) && (hdfu->buffer.d8[0] == DFU_CMD_ERASE)) - { - ((USBD_DFU_MediaTypeDef *)pdev->pUserData)->GetStatus(hdfu->data_ptr, DFU_MEDIA_ERASE, hdfu->dev_status); - } - else - { - ((USBD_DFU_MediaTypeDef *)pdev->pUserData)->GetStatus(hdfu->data_ptr, DFU_MEDIA_PROGRAM, hdfu->dev_status); - } - } - else /* (hdfu->wlength==0)*/ - { - hdfu->dev_state = DFU_STATE_DNLOAD_IDLE; - - hdfu->dev_status[1] = 0U; - hdfu->dev_status[2] = 0U; - hdfu->dev_status[3] = 0U; - hdfu->dev_status[4] = hdfu->dev_state; - } - break; - - case DFU_STATE_MANIFEST_SYNC : - if (hdfu->manif_state == DFU_MANIFEST_IN_PROGRESS) - { - hdfu->dev_state = DFU_STATE_MANIFEST; - - hdfu->dev_status[1] = 1U; /*bwPollTimeout = 1ms*/ - hdfu->dev_status[2] = 0U; - hdfu->dev_status[3] = 0U; - hdfu->dev_status[4] = hdfu->dev_state; - } - else - { - if ((hdfu->manif_state == DFU_MANIFEST_COMPLETE) && - ((USBD_DFU_CfgDesc[(11U + (9U * USBD_DFU_MAX_ITF_NUM))]) & 0x04U)) - { - hdfu->dev_state = DFU_STATE_IDLE; - - hdfu->dev_status[1] = 0U; - hdfu->dev_status[2] = 0U; - hdfu->dev_status[3] = 0U; - hdfu->dev_status[4] = hdfu->dev_state; - } - } - break; - - default : - break; - } - - /* Send the status data over EP0 */ - USBD_CtlSendData (pdev, (uint8_t *)(&(hdfu->dev_status[0])), 6U); -} - -/** - * @brief DFU_ClearStatus - * Handles the DFU CLRSTATUS request. - * @param pdev: device instance - * @retval status - */ -static void DFU_ClearStatus(USBD_HandleTypeDef *pdev) -{ - USBD_DFU_HandleTypeDef *hdfu; - - hdfu = (USBD_DFU_HandleTypeDef*) pdev->pClassData; - - if (hdfu->dev_state == DFU_STATE_ERROR) - { - hdfu->dev_state = DFU_STATE_IDLE; - hdfu->dev_status[0] = DFU_ERROR_NONE;/*bStatus*/ - hdfu->dev_status[1] = 0U; - hdfu->dev_status[2] = 0U; - hdfu->dev_status[3] = 0U; /*bwPollTimeout=0ms*/ - hdfu->dev_status[4] = hdfu->dev_state;/*bState*/ - hdfu->dev_status[5] = 0U;/*iString*/ - } - else - { /*State Error*/ - hdfu->dev_state = DFU_STATE_ERROR; - hdfu->dev_status[0] = DFU_ERROR_UNKNOWN;/*bStatus*/ - hdfu->dev_status[1] = 0U; - hdfu->dev_status[2] = 0U; - hdfu->dev_status[3] = 0U; /*bwPollTimeout=0ms*/ - hdfu->dev_status[4] = hdfu->dev_state;/*bState*/ - hdfu->dev_status[5] = 0U;/*iString*/ - } -} - -/** - * @brief DFU_GetState - * Handles the DFU GETSTATE request. - * @param pdev: device instance - * @retval None - */ -static void DFU_GetState(USBD_HandleTypeDef *pdev) -{ - USBD_DFU_HandleTypeDef *hdfu; - - hdfu = (USBD_DFU_HandleTypeDef*) pdev->pClassData; - - /* Return the current state of the DFU interface */ - USBD_CtlSendData (pdev, &hdfu->dev_state, 1U); -} - -/** - * @brief DFU_Abort - * Handles the DFU ABORT request. - * @param pdev: device instance - * @retval None - */ -static void DFU_Abort(USBD_HandleTypeDef *pdev) -{ - USBD_DFU_HandleTypeDef *hdfu; - - hdfu = (USBD_DFU_HandleTypeDef*) pdev->pClassData; - - if (hdfu->dev_state == DFU_STATE_IDLE || hdfu->dev_state == DFU_STATE_DNLOAD_SYNC - || hdfu->dev_state == DFU_STATE_DNLOAD_IDLE || hdfu->dev_state == DFU_STATE_MANIFEST_SYNC - || hdfu->dev_state == DFU_STATE_UPLOAD_IDLE ) - { - hdfu->dev_state = DFU_STATE_IDLE; - hdfu->dev_status[0] = DFU_ERROR_NONE; - hdfu->dev_status[1] = 0U; - hdfu->dev_status[2] = 0U; - hdfu->dev_status[3] = 0U; /*bwPollTimeout=0ms*/ - hdfu->dev_status[4] = hdfu->dev_state; - hdfu->dev_status[5] = 0U; /*iString*/ - hdfu->wblock_num = 0U; - hdfu->wlength = 0U; - } -} - -/** - * @brief DFU_Leave - * Handles the sub-protocol DFU leave DFU mode request (leaves DFU mode - * and resets device to jump to user loaded code). - * @param pdev: device instance - * @retval None - */ -static void DFU_Leave(USBD_HandleTypeDef *pdev) -{ - USBD_DFU_HandleTypeDef *hdfu; - - hdfu = (USBD_DFU_HandleTypeDef*) pdev->pClassData; - - hdfu->manif_state = DFU_MANIFEST_COMPLETE; - - if ((USBD_DFU_CfgDesc[(11U + (9U * USBD_DFU_MAX_ITF_NUM))]) & 0x04U) - { - hdfu->dev_state = DFU_STATE_MANIFEST_SYNC; - - hdfu->dev_status[1] = 0U; - hdfu->dev_status[2] = 0U; - hdfu->dev_status[3] = 0U; - hdfu->dev_status[4] = hdfu->dev_state; - return; - } - else - { - - hdfu->dev_state = DFU_STATE_MANIFEST_WAIT_RESET; - - hdfu->dev_status[1] = 0U; - hdfu->dev_status[2] = 0U; - hdfu->dev_status[3] = 0U; - hdfu->dev_status[4] = hdfu->dev_state; - - /* Disconnect the USB device */ - USBD_Stop (pdev); - - /* DeInitilialize the MAL(Media Access Layer) */ - ((USBD_DFU_MediaTypeDef *)pdev->pUserData)->DeInit(); - - /* Generate system reset to allow jumping to the user code */ - NVIC_SystemReset(); - - /* This instruction will not be reached (system reset) */ - for(;;){} - } -} - -/** - * @} - */ - - -/** - * @} - */ - - -/** - * @} - */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_dfu.c + * @author MCD Application Team + * @brief This file provides the DFU core functions. + * + * @verbatim + * + * =================================================================== + * DFU Class Driver Description + * =================================================================== + * This driver manages the DFU class V1.1 following the "Device Class Specification for + * Device Firmware Upgrade Version 1.1 Aug 5, 2004". + * This driver implements the following aspects of the specification: + * - Device descriptor management + * - Configuration descriptor management + * - Enumeration as DFU device (in DFU mode only) + * - Requests management (supporting ST DFU sub-protocol) + * - Memory operations management (Download/Upload/Erase/Detach/GetState/GetStatus) + * - DFU state machine implementation. + * + * @note + * ST DFU sub-protocol is compliant with DFU protocol and use sub-requests to manage + * memory addressing, commands processing, specific memories operations (ie. Erase) ... + * As required by the DFU specification, only endpoint 0 is used in this application. + * Other endpoints and functions may be added to the application (ie. DFU ...) + * + * These aspects may be enriched or modified for a specific user application. + * + * This driver doesn't implement the following aspects of the specification + * (but it is possible to manage these features with some modifications on this driver): + * - Manifestation Tolerant mode + * + * @endverbatim + * + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + + /* BSPDependencies + - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" + - "stm32xxxxx_{eval}{discovery}_io.c" + EndBSPDependencies */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_dfu.h" +#include "usbd_ctlreq.h" + + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + + +/** @defgroup USBD_DFU + * @brief usbd core module + * @{ + */ + +/** @defgroup USBD_DFU_Private_TypesDefinitions + * @{ + */ +/** + * @} + */ + + +/** @defgroup USBD_DFU_Private_Defines + * @{ + */ + +/** + * @} + */ + + +/** @defgroup USBD_DFU_Private_Macros + * @{ + */ + +/** + * @} + */ + + + + +/** @defgroup USBD_DFU_Private_FunctionPrototypes + * @{ + */ + + +static uint8_t USBD_DFU_Init (USBD_HandleTypeDef *pdev, + uint8_t cfgidx); + +static uint8_t USBD_DFU_DeInit (USBD_HandleTypeDef *pdev, + uint8_t cfgidx); + +static uint8_t USBD_DFU_Setup (USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req); + +static uint8_t *USBD_DFU_GetCfgDesc (uint16_t *length); + +static uint8_t *USBD_DFU_GetDeviceQualifierDesc (uint16_t *length); + +static uint8_t USBD_DFU_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum); + +static uint8_t USBD_DFU_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum); + +static uint8_t USBD_DFU_EP0_RxReady (USBD_HandleTypeDef *pdev); + +static uint8_t USBD_DFU_EP0_TxReady (USBD_HandleTypeDef *pdev); + +static uint8_t USBD_DFU_SOF (USBD_HandleTypeDef *pdev); + +static uint8_t USBD_DFU_IsoINIncomplete (USBD_HandleTypeDef *pdev, uint8_t epnum); + +static uint8_t USBD_DFU_IsoOutIncomplete (USBD_HandleTypeDef *pdev, uint8_t epnum); + +#if (USBD_SUPPORT_USER_STRING == 1U) +static uint8_t* USBD_DFU_GetUsrStringDesc ( USBD_HandleTypeDef *pdev, uint8_t index , uint16_t *length); +#endif + +static void DFU_Detach (USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req); + +static void DFU_Download (USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req); + +static void DFU_Upload (USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req); + +static void DFU_GetStatus (USBD_HandleTypeDef *pdev); + +static void DFU_ClearStatus (USBD_HandleTypeDef *pdev); + +static void DFU_GetState (USBD_HandleTypeDef *pdev); + +static void DFU_Abort (USBD_HandleTypeDef *pdev); + +static void DFU_Leave (USBD_HandleTypeDef *pdev); + + +/** + * @} + */ + +/** @defgroup USBD_DFU_Private_Variables + * @{ + */ + +USBD_ClassTypeDef USBD_DFU = +{ + USBD_DFU_Init, + USBD_DFU_DeInit, + USBD_DFU_Setup, + USBD_DFU_EP0_TxReady, + USBD_DFU_EP0_RxReady, + USBD_DFU_DataIn, + USBD_DFU_DataOut, + USBD_DFU_SOF, + USBD_DFU_IsoINIncomplete, + USBD_DFU_IsoOutIncomplete, + USBD_DFU_GetCfgDesc, + USBD_DFU_GetCfgDesc, + USBD_DFU_GetCfgDesc, + USBD_DFU_GetDeviceQualifierDesc, +#if (USBD_SUPPORT_USER_STRING == 1U) + USBD_DFU_GetUsrStringDesc +#endif +}; + +/* USB DFU device Configuration Descriptor */ +__ALIGN_BEGIN static uint8_t USBD_DFU_CfgDesc[USB_DFU_CONFIG_DESC_SIZ] __ALIGN_END = +{ + 0x09, /* bLength: Configuation Descriptor size */ + USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ + USB_DFU_CONFIG_DESC_SIZ, + /* wTotalLength: Bytes returned */ + 0x00, + 0x01, /*bNumInterfaces: 1 interface*/ + 0x01, /*bConfigurationValue: Configuration value*/ + 0x02, /*iConfiguration: Index of string descriptor describing the configuration*/ + 0xC0, /*bmAttributes: bus powered and Supprts Remote Wakeup */ + 0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/ + /* 09 */ + + /********** Descriptor of DFU interface 0 Alternate setting 0 **************/ + USBD_DFU_IF_DESC(0U), /* This interface is mandatory for all devices */ + +#if (USBD_DFU_MAX_ITF_NUM > 1U) + /********** Descriptor of DFU interface 0 Alternate setting 1 **************/ + USBD_DFU_IF_DESC(1), +#endif /* (USBD_DFU_MAX_ITF_NUM > 1) */ + +#if (USBD_DFU_MAX_ITF_NUM > 2U) + /********** Descriptor of DFU interface 0 Alternate setting 2 **************/ + USBD_DFU_IF_DESC(2), +#endif /* (USBD_DFU_MAX_ITF_NUM > 2) */ + +#if (USBD_DFU_MAX_ITF_NUM > 3U) + /********** Descriptor of DFU interface 0 Alternate setting 3 **************/ + USBD_DFU_IF_DESC(3), +#endif /* (USBD_DFU_MAX_ITF_NUM > 3) */ + +#if (USBD_DFU_MAX_ITF_NUM > 4U) + /********** Descriptor of DFU interface 0 Alternate setting 4 **************/ + USBD_DFU_IF_DESC(4), +#endif /* (USBD_DFU_MAX_ITF_NUM > 4) */ + +#if (USBD_DFU_MAX_ITF_NUM > 5U) + /********** Descriptor of DFU interface 0 Alternate setting 5 **************/ + USBD_DFU_IF_DESC(5), +#endif /* (USBD_DFU_MAX_ITF_NUM > 5) */ + +#if (USBD_DFU_MAX_ITF_NUM > 6U) +#error "ERROR: usbd_dfu_core.c: Modify the file to support more descriptors!" +#endif /* (USBD_DFU_MAX_ITF_NUM > 6) */ + + /******************** DFU Functional Descriptor********************/ + 0x09, /*blength = 9 Bytes*/ + DFU_DESCRIPTOR_TYPE, /* DFU Functional Descriptor*/ + 0x0B, /*bmAttribute + bitCanDnload = 1 (bit 0) + bitCanUpload = 1 (bit 1) + bitManifestationTolerant = 0 (bit 2) + bitWillDetach = 1 (bit 3) + Reserved (bit4-6) + bitAcceleratedST = 0 (bit 7)*/ + 0xFF, /*DetachTimeOut= 255 ms*/ + 0x00, + /*WARNING: In DMA mode the multiple MPS packets feature is still not supported + ==> In this case, when using DMA USBD_DFU_XFER_SIZE should be set to 64 in usbd_conf.h */ + TRANSFER_SIZE_BYTES(USBD_DFU_XFER_SIZE), /* TransferSize = 1024 Byte*/ + 0x1A, /* bcdDFUVersion*/ + 0x01 + /***********************************************************/ + /* 9*/ +}; + +/* USB Standard Device Descriptor */ +__ALIGN_BEGIN static uint8_t USBD_DFU_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END = +{ + USB_LEN_DEV_QUALIFIER_DESC, + USB_DESC_TYPE_DEVICE_QUALIFIER, + 0x00, + 0x02, + 0x00, + 0x00, + 0x00, + 0x40, + 0x01, + 0x00, +}; + +/** + * @} + */ + +/** @defgroup USBD_DFU_Private_Functions + * @{ + */ + +/** + * @brief USBD_DFU_Init + * Initialize the DFU interface + * @param pdev: device instance + * @param cfgidx: Configuration index + * @retval status + */ +static uint8_t USBD_DFU_Init (USBD_HandleTypeDef *pdev, + uint8_t cfgidx) +{ + USBD_DFU_HandleTypeDef *hdfu; + + /* Allocate Audio structure */ + pdev->pClassData = USBD_malloc(sizeof (USBD_DFU_HandleTypeDef)); + + if(pdev->pClassData == NULL) + { + return USBD_FAIL; + } + else + { + hdfu = (USBD_DFU_HandleTypeDef*) pdev->pClassData; + + hdfu->alt_setting = 0U; + hdfu->data_ptr = USBD_DFU_APP_DEFAULT_ADD; + hdfu->wblock_num = 0U; + hdfu->wlength = 0U; + + hdfu->manif_state = DFU_MANIFEST_COMPLETE; + hdfu->dev_state = DFU_STATE_IDLE; + + hdfu->dev_status[0] = DFU_ERROR_NONE; + hdfu->dev_status[1] = 0U; + hdfu->dev_status[2] = 0U; + hdfu->dev_status[3] = 0U; + hdfu->dev_status[4] = DFU_STATE_IDLE; + hdfu->dev_status[5] = 0U; + + /* Initialize Hardware layer */ + if (((USBD_DFU_MediaTypeDef *)pdev->pUserData)->Init() != USBD_OK) + { + return USBD_FAIL; + } + } + return USBD_OK; +} + +/** + * @brief USBD_DFU_Init + * De-Initialize the DFU layer + * @param pdev: device instance + * @param cfgidx: Configuration index + * @retval status + */ +static uint8_t USBD_DFU_DeInit (USBD_HandleTypeDef *pdev, + uint8_t cfgidx) +{ + USBD_DFU_HandleTypeDef *hdfu; + hdfu = (USBD_DFU_HandleTypeDef*) pdev->pClassData; + + hdfu->wblock_num = 0U; + hdfu->wlength = 0U; + + hdfu->dev_state = DFU_STATE_IDLE; + hdfu->dev_status[0] = DFU_ERROR_NONE; + hdfu->dev_status[4] = DFU_STATE_IDLE; + + /* DeInit physical Interface components */ + if(pdev->pClassData != NULL) + { + /* De-Initialize Hardware layer */ + ((USBD_DFU_MediaTypeDef *)pdev->pUserData)->DeInit(); + USBD_free(pdev->pClassData); + pdev->pClassData = NULL; + } + + return USBD_OK; +} + +/** + * @brief USBD_DFU_Setup + * Handle the DFU specific requests + * @param pdev: instance + * @param req: usb requests + * @retval status + */ +static uint8_t USBD_DFU_Setup (USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req) +{ + USBD_DFU_HandleTypeDef *hdfu; + uint8_t *pbuf = 0U; + uint16_t len = 0U; + uint16_t status_info = 0U; + uint8_t ret = USBD_OK; + + hdfu = (USBD_DFU_HandleTypeDef*) pdev->pClassData; + + switch (req->bmRequest & USB_REQ_TYPE_MASK) + { + case USB_REQ_TYPE_CLASS: + switch (req->bRequest) + { + case DFU_DNLOAD: + DFU_Download(pdev, req); + break; + + case DFU_UPLOAD: + DFU_Upload(pdev, req); + break; + + case DFU_GETSTATUS: + DFU_GetStatus(pdev); + break; + + case DFU_CLRSTATUS: + DFU_ClearStatus(pdev); + break; + + case DFU_GETSTATE: + DFU_GetState(pdev); + break; + + case DFU_ABORT: + DFU_Abort(pdev); + break; + + case DFU_DETACH: + DFU_Detach(pdev, req); + break; + + default: + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + break; + } + break; + + case USB_REQ_TYPE_STANDARD: + switch (req->bRequest) + { + case USB_REQ_GET_STATUS: + if (pdev->dev_state == USBD_STATE_CONFIGURED) + { + USBD_CtlSendData (pdev, (uint8_t *)(void *)&status_info, 2U); + } + else + { + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + } + break; + + case USB_REQ_GET_DESCRIPTOR: + if( (req->wValue >> 8) == DFU_DESCRIPTOR_TYPE) + { + pbuf = USBD_DFU_CfgDesc + (9U * (USBD_DFU_MAX_ITF_NUM + 1U)); + len = MIN(USB_DFU_DESC_SIZ , req->wLength); + } + + USBD_CtlSendData (pdev, pbuf, len); + break; + + case USB_REQ_GET_INTERFACE: + if (pdev->dev_state == USBD_STATE_CONFIGURED) + { + USBD_CtlSendData (pdev, (uint8_t *)(void *)&hdfu->alt_setting, 1U); + } + else + { + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + } + break; + + case USB_REQ_SET_INTERFACE: + if ((uint8_t)(req->wValue) < USBD_DFU_MAX_ITF_NUM) + { + if (pdev->dev_state == USBD_STATE_CONFIGURED) + { + hdfu->alt_setting = (uint8_t)(req->wValue); + } + else + { + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + } + } + else + { + /* Call the error management function (command will be nacked */ + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + } + break; + + default: + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + break; + } + break; + + default: + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + break; + } + + return ret; +} + + +/** + * @brief USBD_DFU_GetCfgDesc + * return configuration descriptor + * @param speed : current device speed + * @param length : pointer data length + * @retval pointer to descriptor buffer + */ +static uint8_t *USBD_DFU_GetCfgDesc (uint16_t *length) +{ + *length = sizeof (USBD_DFU_CfgDesc); + return USBD_DFU_CfgDesc; +} + +/** + * @brief USBD_DFU_DataIn + * handle data IN Stage + * @param pdev: device instance + * @param epnum: endpoint index + * @retval status + */ +static uint8_t USBD_DFU_DataIn (USBD_HandleTypeDef *pdev, + uint8_t epnum) +{ + + return USBD_OK; +} + +/** + * @brief USBD_DFU_EP0_RxReady + * handle EP0 Rx Ready event + * @param pdev: device instance + * @retval status + */ +static uint8_t USBD_DFU_EP0_RxReady (USBD_HandleTypeDef *pdev) +{ + + return USBD_OK; +} +/** + * @brief USBD_DFU_EP0_TxReady + * handle EP0 TRx Ready event + * @param pdev: device instance + * @retval status + */ +static uint8_t USBD_DFU_EP0_TxReady (USBD_HandleTypeDef *pdev) +{ + uint32_t addr; + USBD_SetupReqTypedef req; + USBD_DFU_HandleTypeDef *hdfu; + + hdfu = (USBD_DFU_HandleTypeDef*) pdev->pClassData; + + if (hdfu->dev_state == DFU_STATE_DNLOAD_BUSY) + { + /* Decode the Special Command*/ + if (hdfu->wblock_num == 0U) + { + if ((hdfu->buffer.d8[0] == DFU_CMD_GETCOMMANDS) && (hdfu->wlength == 1U)) + { + + } + else if ((hdfu->buffer.d8[0] == DFU_CMD_SETADDRESSPOINTER) && (hdfu->wlength == 5U)) + { + hdfu->data_ptr = hdfu->buffer.d8[1]; + hdfu->data_ptr += (uint32_t)hdfu->buffer.d8[2] << 8; + hdfu->data_ptr += (uint32_t)hdfu->buffer.d8[3] << 16; + hdfu->data_ptr += (uint32_t)hdfu->buffer.d8[4] << 24; + } + else if ((hdfu->buffer.d8[0] == DFU_CMD_ERASE) && (hdfu->wlength == 5U)) + { + hdfu->data_ptr = hdfu->buffer.d8[1]; + hdfu->data_ptr += (uint32_t)hdfu->buffer.d8[2] << 8; + hdfu->data_ptr += (uint32_t)hdfu->buffer.d8[3] << 16; + hdfu->data_ptr += (uint32_t)hdfu->buffer.d8[4] << 24; + + if (((USBD_DFU_MediaTypeDef *)pdev->pUserData)->Erase(hdfu->data_ptr) != USBD_OK) + { + return USBD_FAIL; + } + } + else + { + /* Reset the global length and block number */ + hdfu->wlength = 0U; + hdfu->wblock_num = 0U; + /* Call the error management function (command will be nacked) */ + req.bmRequest = 0U; + req.wLength = 1U; + USBD_CtlError (pdev, &req); + } + } + /* Regular Download Command */ + else + { + if (hdfu->wblock_num > 1U) + { + /* Decode the required address */ + addr = ((hdfu->wblock_num - 2U) * USBD_DFU_XFER_SIZE) + hdfu->data_ptr; + + /* Preform the write operation */ + if (((USBD_DFU_MediaTypeDef *)pdev->pUserData)->Write(hdfu->buffer.d8, + (uint8_t *)addr, hdfu->wlength) != USBD_OK) + { + return USBD_FAIL; + } + } + } + + /* Reset the global length and block number */ + hdfu->wlength = 0U; + hdfu->wblock_num = 0U; + + /* Update the state machine */ + hdfu->dev_state = DFU_STATE_DNLOAD_SYNC; + + hdfu->dev_status[1] = 0U; + hdfu->dev_status[2] = 0U; + hdfu->dev_status[3] = 0U; + hdfu->dev_status[4] = hdfu->dev_state; + return USBD_OK; + } + else + { + if (hdfu->dev_state == DFU_STATE_MANIFEST)/* Manifestation in progress */ + { + /* Start leaving DFU mode */ + DFU_Leave(pdev); + } + } + + return USBD_OK; +} +/** + * @brief USBD_DFU_SOF + * handle SOF event + * @param pdev: device instance + * @retval status + */ +static uint8_t USBD_DFU_SOF (USBD_HandleTypeDef *pdev) +{ + + return USBD_OK; +} +/** + * @brief USBD_DFU_IsoINIncomplete + * handle data ISO IN Incomplete event + * @param pdev: device instance + * @param epnum: endpoint index + * @retval status + */ +static uint8_t USBD_DFU_IsoINIncomplete (USBD_HandleTypeDef *pdev, uint8_t epnum) +{ + + return USBD_OK; +} +/** + * @brief USBD_DFU_IsoOutIncomplete + * handle data ISO OUT Incomplete event + * @param pdev: device instance + * @param epnum: endpoint index + * @retval status + */ +static uint8_t USBD_DFU_IsoOutIncomplete (USBD_HandleTypeDef *pdev, uint8_t epnum) +{ + + return USBD_OK; +} +/** + * @brief USBD_DFU_DataOut + * handle data OUT Stage + * @param pdev: device instance + * @param epnum: endpoint index + * @retval status + */ +static uint8_t USBD_DFU_DataOut (USBD_HandleTypeDef *pdev, + uint8_t epnum) +{ + + return USBD_OK; +} + +/** +* @brief DeviceQualifierDescriptor +* return Device Qualifier descriptor +* @param length : pointer data length +* @retval pointer to descriptor buffer +*/ +static uint8_t *USBD_DFU_GetDeviceQualifierDesc (uint16_t *length) +{ + *length = sizeof (USBD_DFU_DeviceQualifierDesc); + return USBD_DFU_DeviceQualifierDesc; +} + +/** + * @brief USBD_DFU_GetUsrStringDesc + * Manages the transfer of memory interfaces string descriptors. + * @param speed : current device speed + * @param index: desciptor index + * @param length : pointer data length + * @retval pointer to the descriptor table or NULL if the descriptor is not supported. + */ +#if (USBD_SUPPORT_USER_STRING == 1U) +static uint8_t* USBD_DFU_GetUsrStringDesc (USBD_HandleTypeDef *pdev, uint8_t index , uint16_t *length) +{ + static uint8_t USBD_StrDesc[255]; + /* Check if the requested string interface is supported */ + if (index <= (USBD_IDX_INTERFACE_STR + USBD_DFU_MAX_ITF_NUM)) + { + USBD_GetString ((uint8_t *)((USBD_DFU_MediaTypeDef *)pdev->pUserData)->pStrDesc, USBD_StrDesc, length); + return USBD_StrDesc; + } + /* Not supported Interface Descriptor index */ + else + { + return NULL; + } +} +#endif + +/** +* @brief USBD_MSC_RegisterStorage +* @param fops: storage callback +* @retval status +*/ +uint8_t USBD_DFU_RegisterMedia (USBD_HandleTypeDef *pdev, + USBD_DFU_MediaTypeDef *fops) +{ + if(fops != NULL) + { + pdev->pUserData= fops; + } + return 0U; +} + +/****************************************************************************** + DFU Class requests management +******************************************************************************/ +/** + * @brief DFU_Detach + * Handles the DFU DETACH request. + * @param pdev: device instance + * @param req: pointer to the request structure. + * @retval None. + */ +static void DFU_Detach(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req) +{ + USBD_DFU_HandleTypeDef *hdfu; + + hdfu = (USBD_DFU_HandleTypeDef*) pdev->pClassData; + + if (hdfu->dev_state == DFU_STATE_IDLE || hdfu->dev_state == DFU_STATE_DNLOAD_SYNC + || hdfu->dev_state == DFU_STATE_DNLOAD_IDLE || hdfu->dev_state == DFU_STATE_MANIFEST_SYNC + || hdfu->dev_state == DFU_STATE_UPLOAD_IDLE ) + { + /* Update the state machine */ + hdfu->dev_state = DFU_STATE_IDLE; + hdfu->dev_status[0] = DFU_ERROR_NONE; + hdfu->dev_status[1] = 0U; + hdfu->dev_status[2] = 0U; + hdfu->dev_status[3] = 0U; /*bwPollTimeout=0ms*/ + hdfu->dev_status[4] = hdfu->dev_state; + hdfu->dev_status[5] = 0U; /*iString*/ + hdfu->wblock_num = 0U; + hdfu->wlength = 0U; + } + + /* Check the detach capability in the DFU functional descriptor */ + if ((USBD_DFU_CfgDesc[12U + (9U * USBD_DFU_MAX_ITF_NUM)]) & DFU_DETACH_MASK) + { + /* Perform an Attach-Detach operation on USB bus */ + USBD_Stop (pdev); + USBD_Start (pdev); + } + else + { + /* Wait for the period of time specified in Detach request */ + USBD_Delay ((uint32_t)req->wValue); + } +} + +/** + * @brief DFU_Download + * Handles the DFU DNLOAD request. + * @param pdev: device instance + * @param req: pointer to the request structure + * @retval None + */ +static void DFU_Download(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req) +{ + USBD_DFU_HandleTypeDef *hdfu; + + hdfu = (USBD_DFU_HandleTypeDef*) pdev->pClassData; + + /* Data setup request */ + if (req->wLength > 0U) + { + if ((hdfu->dev_state == DFU_STATE_IDLE) || (hdfu->dev_state == DFU_STATE_DNLOAD_IDLE)) + { + /* Update the global length and block number */ + hdfu->wblock_num = req->wValue; + hdfu->wlength = req->wLength; + + /* Update the state machine */ + hdfu->dev_state = DFU_STATE_DNLOAD_SYNC; + hdfu->dev_status[4] = hdfu->dev_state; + + /* Prepare the reception of the buffer over EP0 */ + USBD_CtlPrepareRx (pdev, (uint8_t*)hdfu->buffer.d8, + (uint16_t)hdfu->wlength); + } + /* Unsupported state */ + else + { + /* Call the error management function (command will be nacked */ + USBD_CtlError (pdev, req); + } + } + /* 0 Data DNLOAD request */ + else + { + /* End of DNLOAD operation*/ + if (hdfu->dev_state == DFU_STATE_DNLOAD_IDLE || hdfu->dev_state == DFU_STATE_IDLE ) + { + hdfu->manif_state = DFU_MANIFEST_IN_PROGRESS; + hdfu->dev_state = DFU_STATE_MANIFEST_SYNC; + hdfu->dev_status[1] = 0U; + hdfu->dev_status[2] = 0U; + hdfu->dev_status[3] = 0U; + hdfu->dev_status[4] = hdfu->dev_state; + } + else + { + /* Call the error management function (command will be nacked */ + USBD_CtlError (pdev, req); + } + } +} + +/** + * @brief DFU_Upload + * Handles the DFU UPLOAD request. + * @param pdev: instance + * @param req: pointer to the request structure + * @retval status + */ +static void DFU_Upload(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req) +{ + USBD_DFU_HandleTypeDef *hdfu; + + hdfu = (USBD_DFU_HandleTypeDef*) pdev->pClassData; + + uint8_t *phaddr = NULL; + uint32_t addr = 0U; + + /* Data setup request */ + if (req->wLength > 0U) + { + if ((hdfu->dev_state == DFU_STATE_IDLE) || (hdfu->dev_state == DFU_STATE_UPLOAD_IDLE)) + { + /* Update the global length and block number */ + hdfu->wblock_num = req->wValue; + hdfu->wlength = req->wLength; + + /* DFU Get Command */ + if (hdfu->wblock_num == 0U) + { + /* Update the state machine */ + hdfu->dev_state = (hdfu->wlength > 3U)? DFU_STATE_IDLE:DFU_STATE_UPLOAD_IDLE; + + hdfu->dev_status[1] = 0U; + hdfu->dev_status[2] = 0U; + hdfu->dev_status[3] = 0U; + hdfu->dev_status[4] = hdfu->dev_state; + + /* Store the values of all supported commands */ + hdfu->buffer.d8[0] = DFU_CMD_GETCOMMANDS; + hdfu->buffer.d8[1] = DFU_CMD_SETADDRESSPOINTER; + hdfu->buffer.d8[2] = DFU_CMD_ERASE; + + /* Send the status data over EP0 */ + USBD_CtlSendData (pdev, (uint8_t *)(&(hdfu->buffer.d8[0])), 3U); + } + else if (hdfu->wblock_num > 1U) + { + hdfu->dev_state = DFU_STATE_UPLOAD_IDLE; + + hdfu->dev_status[1] = 0U; + hdfu->dev_status[2] = 0U; + hdfu->dev_status[3] = 0U; + hdfu->dev_status[4] = hdfu->dev_state; + + addr = ((hdfu->wblock_num - 2U) * USBD_DFU_XFER_SIZE) + hdfu->data_ptr; /* Change is Accelerated*/ + + /* Return the physical address where data are stored */ + phaddr = ((USBD_DFU_MediaTypeDef *)pdev->pUserData)->Read((uint8_t *)addr, hdfu->buffer.d8, hdfu->wlength); + + /* Send the status data over EP0 */ + USBD_CtlSendData (pdev, phaddr, (uint16_t)hdfu->wlength); + } + else /* unsupported hdfu->wblock_num */ + { + hdfu->dev_state = DFU_ERROR_STALLEDPKT; + + hdfu->dev_status[1] = 0U; + hdfu->dev_status[2] = 0U; + hdfu->dev_status[3] = 0U; + hdfu->dev_status[4] = hdfu->dev_state; + + /* Call the error management function (command will be nacked */ + USBD_CtlError (pdev, req); + } + } + /* Unsupported state */ + else + { + hdfu->wlength = 0U; + hdfu->wblock_num = 0U; + /* Call the error management function (command will be nacked */ + USBD_CtlError (pdev, req); + } + } + /* No Data setup request */ + else + { + hdfu->dev_state = DFU_STATE_IDLE; + + hdfu->dev_status[1] = 0U; + hdfu->dev_status[2] = 0U; + hdfu->dev_status[3] = 0U; + hdfu->dev_status[4] = hdfu->dev_state; + } +} + +/** + * @brief DFU_GetStatus + * Handles the DFU GETSTATUS request. + * @param pdev: instance + * @retval status + */ +static void DFU_GetStatus(USBD_HandleTypeDef *pdev) +{ + USBD_DFU_HandleTypeDef *hdfu; + + hdfu = (USBD_DFU_HandleTypeDef*) pdev->pClassData; + + switch (hdfu->dev_state) + { + case DFU_STATE_DNLOAD_SYNC: + if (hdfu->wlength != 0U) + { + hdfu->dev_state = DFU_STATE_DNLOAD_BUSY; + + hdfu->dev_status[1] = 0U; + hdfu->dev_status[2] = 0U; + hdfu->dev_status[3] = 0U; + hdfu->dev_status[4] = hdfu->dev_state; + + if ((hdfu->wblock_num == 0U) && (hdfu->buffer.d8[0] == DFU_CMD_ERASE)) + { + ((USBD_DFU_MediaTypeDef *)pdev->pUserData)->GetStatus(hdfu->data_ptr, DFU_MEDIA_ERASE, hdfu->dev_status); + } + else + { + ((USBD_DFU_MediaTypeDef *)pdev->pUserData)->GetStatus(hdfu->data_ptr, DFU_MEDIA_PROGRAM, hdfu->dev_status); + } + } + else /* (hdfu->wlength==0)*/ + { + hdfu->dev_state = DFU_STATE_DNLOAD_IDLE; + + hdfu->dev_status[1] = 0U; + hdfu->dev_status[2] = 0U; + hdfu->dev_status[3] = 0U; + hdfu->dev_status[4] = hdfu->dev_state; + } + break; + + case DFU_STATE_MANIFEST_SYNC : + if (hdfu->manif_state == DFU_MANIFEST_IN_PROGRESS) + { + hdfu->dev_state = DFU_STATE_MANIFEST; + + hdfu->dev_status[1] = 1U; /*bwPollTimeout = 1ms*/ + hdfu->dev_status[2] = 0U; + hdfu->dev_status[3] = 0U; + hdfu->dev_status[4] = hdfu->dev_state; + } + else + { + if ((hdfu->manif_state == DFU_MANIFEST_COMPLETE) && + ((USBD_DFU_CfgDesc[(11U + (9U * USBD_DFU_MAX_ITF_NUM))]) & 0x04U)) + { + hdfu->dev_state = DFU_STATE_IDLE; + + hdfu->dev_status[1] = 0U; + hdfu->dev_status[2] = 0U; + hdfu->dev_status[3] = 0U; + hdfu->dev_status[4] = hdfu->dev_state; + } + } + break; + + default : + break; + } + + /* Send the status data over EP0 */ + USBD_CtlSendData (pdev, (uint8_t *)(&(hdfu->dev_status[0])), 6U); +} + +/** + * @brief DFU_ClearStatus + * Handles the DFU CLRSTATUS request. + * @param pdev: device instance + * @retval status + */ +static void DFU_ClearStatus(USBD_HandleTypeDef *pdev) +{ + USBD_DFU_HandleTypeDef *hdfu; + + hdfu = (USBD_DFU_HandleTypeDef*) pdev->pClassData; + + if (hdfu->dev_state == DFU_STATE_ERROR) + { + hdfu->dev_state = DFU_STATE_IDLE; + hdfu->dev_status[0] = DFU_ERROR_NONE;/*bStatus*/ + hdfu->dev_status[1] = 0U; + hdfu->dev_status[2] = 0U; + hdfu->dev_status[3] = 0U; /*bwPollTimeout=0ms*/ + hdfu->dev_status[4] = hdfu->dev_state;/*bState*/ + hdfu->dev_status[5] = 0U;/*iString*/ + } + else + { /*State Error*/ + hdfu->dev_state = DFU_STATE_ERROR; + hdfu->dev_status[0] = DFU_ERROR_UNKNOWN;/*bStatus*/ + hdfu->dev_status[1] = 0U; + hdfu->dev_status[2] = 0U; + hdfu->dev_status[3] = 0U; /*bwPollTimeout=0ms*/ + hdfu->dev_status[4] = hdfu->dev_state;/*bState*/ + hdfu->dev_status[5] = 0U;/*iString*/ + } +} + +/** + * @brief DFU_GetState + * Handles the DFU GETSTATE request. + * @param pdev: device instance + * @retval None + */ +static void DFU_GetState(USBD_HandleTypeDef *pdev) +{ + USBD_DFU_HandleTypeDef *hdfu; + + hdfu = (USBD_DFU_HandleTypeDef*) pdev->pClassData; + + /* Return the current state of the DFU interface */ + USBD_CtlSendData (pdev, &hdfu->dev_state, 1U); +} + +/** + * @brief DFU_Abort + * Handles the DFU ABORT request. + * @param pdev: device instance + * @retval None + */ +static void DFU_Abort(USBD_HandleTypeDef *pdev) +{ + USBD_DFU_HandleTypeDef *hdfu; + + hdfu = (USBD_DFU_HandleTypeDef*) pdev->pClassData; + + if (hdfu->dev_state == DFU_STATE_IDLE || hdfu->dev_state == DFU_STATE_DNLOAD_SYNC + || hdfu->dev_state == DFU_STATE_DNLOAD_IDLE || hdfu->dev_state == DFU_STATE_MANIFEST_SYNC + || hdfu->dev_state == DFU_STATE_UPLOAD_IDLE ) + { + hdfu->dev_state = DFU_STATE_IDLE; + hdfu->dev_status[0] = DFU_ERROR_NONE; + hdfu->dev_status[1] = 0U; + hdfu->dev_status[2] = 0U; + hdfu->dev_status[3] = 0U; /*bwPollTimeout=0ms*/ + hdfu->dev_status[4] = hdfu->dev_state; + hdfu->dev_status[5] = 0U; /*iString*/ + hdfu->wblock_num = 0U; + hdfu->wlength = 0U; + } +} + +/** + * @brief DFU_Leave + * Handles the sub-protocol DFU leave DFU mode request (leaves DFU mode + * and resets device to jump to user loaded code). + * @param pdev: device instance + * @retval None + */ +static void DFU_Leave(USBD_HandleTypeDef *pdev) +{ + USBD_DFU_HandleTypeDef *hdfu; + + hdfu = (USBD_DFU_HandleTypeDef*) pdev->pClassData; + + hdfu->manif_state = DFU_MANIFEST_COMPLETE; + + if ((USBD_DFU_CfgDesc[(11U + (9U * USBD_DFU_MAX_ITF_NUM))]) & 0x04U) + { + hdfu->dev_state = DFU_STATE_MANIFEST_SYNC; + + hdfu->dev_status[1] = 0U; + hdfu->dev_status[2] = 0U; + hdfu->dev_status[3] = 0U; + hdfu->dev_status[4] = hdfu->dev_state; + return; + } + else + { + + hdfu->dev_state = DFU_STATE_MANIFEST_WAIT_RESET; + + hdfu->dev_status[1] = 0U; + hdfu->dev_status[2] = 0U; + hdfu->dev_status[3] = 0U; + hdfu->dev_status[4] = hdfu->dev_state; + + /* Disconnect the USB device */ + USBD_Stop (pdev); + + /* DeInitilialize the MAL(Media Access Layer) */ + ((USBD_DFU_MediaTypeDef *)pdev->pUserData)->DeInit(); + + /* Generate system reset to allow jumping to the user code */ + NVIC_SystemReset(); + + /* This instruction will not be reached (system reset) */ + for(;;){} + } +} + +/** + * @} + */ + + +/** + * @} + */ + + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Class/DFU/Src/usbd_dfu_media_template.c b/system/Middlewares/ST/STM32_USB_Device_Library/Class/DFU/Src/usbd_dfu_media_template.c index 7a61fe1496..537135627f 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Class/DFU/Src/usbd_dfu_media_template.c +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Class/DFU/Src/usbd_dfu_media_template.c @@ -1,135 +1,135 @@ -/** - ****************************************************************************** - * @file usbd_dfu_media_template.c - * @author MCD Application Team - * @brief Memory management layer - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - - /* BSPDependencies - - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" - - "stm32xxxxx_{eval}{discovery}_io.c" - EndBSPDependencies */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_dfu_media_template.h" - - -/* Private typedef -----------------------------------------------------------*/ -/* Private define ------------------------------------------------------------*/ -/* Private macro -------------------------------------------------------------*/ -/* Private variables ---------------------------------------------------------*/ -/* Private function prototypes -----------------------------------------------*/ -/* Extern function prototypes ------------------------------------------------*/ -/* Private functions ---------------------------------------------------------*/ -uint16_t MEM_If_Init(void); -uint16_t MEM_If_Erase (uint32_t Add); -uint16_t MEM_If_Write (uint8_t *src, uint8_t *dest, uint32_t Len); -uint8_t *MEM_If_Read (uint8_t *src, uint8_t *dest, uint32_t Len); -uint16_t MEM_If_DeInit(void); -uint16_t MEM_If_GetStatus (uint32_t Add, uint8_t Cmd, uint8_t *buffer); - -USBD_DFU_MediaTypeDef USBD_DFU_MEDIA_Template_fops = -{ - (uint8_t *)"DFU MEDIA", - MEM_If_Init, - MEM_If_DeInit, - MEM_If_Erase, - MEM_If_Write, - MEM_If_Read, - MEM_If_GetStatus, - -}; -/** - * @brief MEM_If_Init - * Memory initialization routine. - * @param None - * @retval 0 if operation is successful, MAL_FAIL else. - */ -uint16_t MEM_If_Init(void) -{ - return 0; -} - -/** - * @brief MEM_If_DeInit - * Memory deinitialization routine. - * @param None - * @retval 0 if operation is successful, MAL_FAIL else. - */ -uint16_t MEM_If_DeInit(void) -{ - return 0; -} - -/** - * @brief MEM_If_Erase - * Erase sector. - * @param Add: Address of sector to be erased. - * @retval 0 if operation is successful, MAL_FAIL else. - */ -uint16_t MEM_If_Erase(uint32_t Add) -{ - return 0; -} - -/** - * @brief MEM_If_Write - * Memory write routine. - * @param Add: Address to be written to. - * @param Len: Number of data to be written (in bytes). - * @retval 0 if operation is successful, MAL_FAIL else. - */ -uint16_t MEM_If_Write(uint8_t *src, uint8_t *dest, uint32_t Len) -{ - return 0; -} - -/** - * @brief MEM_If_Read - * Memory read routine. - * @param Add: Address to be read from. - * @param Len: Number of data to be read (in bytes). - * @retval Pointer to the physical address where data should be read. - */ -uint8_t *MEM_If_Read (uint8_t *src, uint8_t *dest, uint32_t Len) -{ - /* Return a valid address to avoid HardFault */ - return (uint8_t*)(0); -} - -/** - * @brief Flash_If_GetStatus - * Memory read routine. - * @param Add: Address to be read from. - * @param cmd: Number of data to be read (in bytes). - * @retval Pointer to the physical address where data should be read. - */ -uint16_t MEM_If_GetStatus (uint32_t Add, uint8_t Cmd, uint8_t *buffer) -{ - switch (Cmd) - { - case DFU_MEDIA_PROGRAM: - - break; - - case DFU_MEDIA_ERASE: - default: - - break; - } - return (0); -} -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - +/** + ****************************************************************************** + * @file usbd_dfu_media_template.c + * @author MCD Application Team + * @brief Memory management layer + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + + /* BSPDependencies + - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" + - "stm32xxxxx_{eval}{discovery}_io.c" + EndBSPDependencies */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_dfu_media_template.h" + + +/* Private typedef -----------------------------------------------------------*/ +/* Private define ------------------------------------------------------------*/ +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +/* Private function prototypes -----------------------------------------------*/ +/* Extern function prototypes ------------------------------------------------*/ +/* Private functions ---------------------------------------------------------*/ +uint16_t MEM_If_Init(void); +uint16_t MEM_If_Erase (uint32_t Add); +uint16_t MEM_If_Write (uint8_t *src, uint8_t *dest, uint32_t Len); +uint8_t *MEM_If_Read (uint8_t *src, uint8_t *dest, uint32_t Len); +uint16_t MEM_If_DeInit(void); +uint16_t MEM_If_GetStatus (uint32_t Add, uint8_t Cmd, uint8_t *buffer); + +USBD_DFU_MediaTypeDef USBD_DFU_MEDIA_Template_fops = +{ + (uint8_t *)"DFU MEDIA", + MEM_If_Init, + MEM_If_DeInit, + MEM_If_Erase, + MEM_If_Write, + MEM_If_Read, + MEM_If_GetStatus, + +}; +/** + * @brief MEM_If_Init + * Memory initialization routine. + * @param None + * @retval 0 if operation is successful, MAL_FAIL else. + */ +uint16_t MEM_If_Init(void) +{ + return 0; +} + +/** + * @brief MEM_If_DeInit + * Memory deinitialization routine. + * @param None + * @retval 0 if operation is successful, MAL_FAIL else. + */ +uint16_t MEM_If_DeInit(void) +{ + return 0; +} + +/** + * @brief MEM_If_Erase + * Erase sector. + * @param Add: Address of sector to be erased. + * @retval 0 if operation is successful, MAL_FAIL else. + */ +uint16_t MEM_If_Erase(uint32_t Add) +{ + return 0; +} + +/** + * @brief MEM_If_Write + * Memory write routine. + * @param Add: Address to be written to. + * @param Len: Number of data to be written (in bytes). + * @retval 0 if operation is successful, MAL_FAIL else. + */ +uint16_t MEM_If_Write(uint8_t *src, uint8_t *dest, uint32_t Len) +{ + return 0; +} + +/** + * @brief MEM_If_Read + * Memory read routine. + * @param Add: Address to be read from. + * @param Len: Number of data to be read (in bytes). + * @retval Pointer to the physical address where data should be read. + */ +uint8_t *MEM_If_Read (uint8_t *src, uint8_t *dest, uint32_t Len) +{ + /* Return a valid address to avoid HardFault */ + return (uint8_t*)(0); +} + +/** + * @brief Flash_If_GetStatus + * Memory read routine. + * @param Add: Address to be read from. + * @param cmd: Number of data to be read (in bytes). + * @retval Pointer to the physical address where data should be read. + */ +uint16_t MEM_If_GetStatus (uint32_t Add, uint8_t Cmd, uint8_t *buffer) +{ + switch (Cmd) + { + case DFU_MEDIA_PROGRAM: + + break; + + case DFU_MEDIA_ERASE: + default: + + break; + } + return (0); +} +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Class/HID/Inc/usbd_hid.h b/system/Middlewares/ST/STM32_USB_Device_Library/Class/HID/Inc/usbd_hid.h index 34e1ed3152..d92dbaacc9 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Class/HID/Inc/usbd_hid.h +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Class/HID/Inc/usbd_hid.h @@ -1,144 +1,144 @@ -/** - ****************************************************************************** - * @file usbd_hid.h - * @author MCD Application Team - * @brief Header file for the usbd_hid_core.c file. - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_HID_H -#define __USB_HID_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_ioreq.h" - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - -/** @defgroup USBD_HID - * @brief This file is the Header file for usbd_hid.c - * @{ - */ - - -/** @defgroup USBD_HID_Exported_Defines - * @{ - */ -#define HID_EPIN_ADDR 0x81U -#define HID_EPIN_SIZE 0x04U - -#define USB_HID_CONFIG_DESC_SIZ 34U -#define USB_HID_DESC_SIZ 9U -#define HID_MOUSE_REPORT_DESC_SIZE 74U - -#define HID_DESCRIPTOR_TYPE 0x21U -#define HID_REPORT_DESC 0x22U - -#ifndef HID_HS_BINTERVAL - #define HID_HS_BINTERVAL 0x07U -#endif /* HID_HS_BINTERVAL */ - -#ifndef HID_FS_BINTERVAL - #define HID_FS_BINTERVAL 0x0AU -#endif /* HID_FS_BINTERVAL */ - -#define HID_REQ_SET_PROTOCOL 0x0BU -#define HID_REQ_GET_PROTOCOL 0x03U - -#define HID_REQ_SET_IDLE 0x0AU -#define HID_REQ_GET_IDLE 0x02U - -#define HID_REQ_SET_REPORT 0x09U -#define HID_REQ_GET_REPORT 0x01U -/** - * @} - */ - - -/** @defgroup USBD_CORE_Exported_TypesDefinitions - * @{ - */ -typedef enum -{ - HID_IDLE = 0, - HID_BUSY, -} -HID_StateTypeDef; - - -typedef struct -{ - uint32_t Protocol; - uint32_t IdleState; - uint32_t AltSetting; - HID_StateTypeDef state; -} -USBD_HID_HandleTypeDef; -/** - * @} - */ - - - -/** @defgroup USBD_CORE_Exported_Macros - * @{ - */ - -/** - * @} - */ - -/** @defgroup USBD_CORE_Exported_Variables - * @{ - */ - -extern USBD_ClassTypeDef USBD_HID; -#define USBD_HID_CLASS &USBD_HID -/** - * @} - */ - -/** @defgroup USB_CORE_Exported_Functions - * @{ - */ -uint8_t USBD_HID_SendReport (USBD_HandleTypeDef *pdev, - uint8_t *report, - uint16_t len); - -uint32_t USBD_HID_GetPollingInterval (USBD_HandleTypeDef *pdev); - -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USB_HID_H */ -/** - * @} - */ - -/** - * @} - */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_hid.h + * @author MCD Application Team + * @brief Header file for the usbd_hid_core.c file. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USB_HID_H +#define __USB_HID_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_ioreq.h" + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + +/** @defgroup USBD_HID + * @brief This file is the Header file for usbd_hid.c + * @{ + */ + + +/** @defgroup USBD_HID_Exported_Defines + * @{ + */ +#define HID_EPIN_ADDR 0x81U +#define HID_EPIN_SIZE 0x04U + +#define USB_HID_CONFIG_DESC_SIZ 34U +#define USB_HID_DESC_SIZ 9U +#define HID_MOUSE_REPORT_DESC_SIZE 74U + +#define HID_DESCRIPTOR_TYPE 0x21U +#define HID_REPORT_DESC 0x22U + +#ifndef HID_HS_BINTERVAL + #define HID_HS_BINTERVAL 0x07U +#endif /* HID_HS_BINTERVAL */ + +#ifndef HID_FS_BINTERVAL + #define HID_FS_BINTERVAL 0x0AU +#endif /* HID_FS_BINTERVAL */ + +#define HID_REQ_SET_PROTOCOL 0x0BU +#define HID_REQ_GET_PROTOCOL 0x03U + +#define HID_REQ_SET_IDLE 0x0AU +#define HID_REQ_GET_IDLE 0x02U + +#define HID_REQ_SET_REPORT 0x09U +#define HID_REQ_GET_REPORT 0x01U +/** + * @} + */ + + +/** @defgroup USBD_CORE_Exported_TypesDefinitions + * @{ + */ +typedef enum +{ + HID_IDLE = 0, + HID_BUSY, +} +HID_StateTypeDef; + + +typedef struct +{ + uint32_t Protocol; + uint32_t IdleState; + uint32_t AltSetting; + HID_StateTypeDef state; +} +USBD_HID_HandleTypeDef; +/** + * @} + */ + + + +/** @defgroup USBD_CORE_Exported_Macros + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_CORE_Exported_Variables + * @{ + */ + +extern USBD_ClassTypeDef USBD_HID; +#define USBD_HID_CLASS &USBD_HID +/** + * @} + */ + +/** @defgroup USB_CORE_Exported_Functions + * @{ + */ +uint8_t USBD_HID_SendReport (USBD_HandleTypeDef *pdev, + uint8_t *report, + uint16_t len); + +uint32_t USBD_HID_GetPollingInterval (USBD_HandleTypeDef *pdev); + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USB_HID_H */ +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Class/HID/Src/usbd_hid.c b/system/Middlewares/ST/STM32_USB_Device_Library/Class/HID/Src/usbd_hid.c index 2abc3d5e10..70a2355311 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Class/HID/Src/usbd_hid.c +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Class/HID/Src/usbd_hid.c @@ -1,681 +1,681 @@ -/** - ****************************************************************************** - * @file usbd_hid.c - * @author MCD Application Team - * @brief This file provides the HID core functions. - * - * @verbatim - * - * =================================================================== - * HID Class Description - * =================================================================== - * This module manages the HID class V1.11 following the "Device Class Definition - * for Human Interface Devices (HID) Version 1.11 Jun 27, 2001". - * This driver implements the following aspects of the specification: - * - The Boot Interface Subclass - * - The Mouse protocol - * - Usage Page : Generic Desktop - * - Usage : Joystick - * - Collection : Application - * - * @note In HS mode and when the DMA is used, all variables and data structures - * dealing with the DMA during the transaction process should be 32-bit aligned. - * - * - * @endverbatim - * - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - - /* BSPDependencies - - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" - - "stm32xxxxx_{eval}{discovery}_io.c" - EndBSPDependencies */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_hid.h" -#include "usbd_ctlreq.h" - - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - - -/** @defgroup USBD_HID - * @brief usbd core module - * @{ - */ - -/** @defgroup USBD_HID_Private_TypesDefinitions - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBD_HID_Private_Defines - * @{ - */ - -/** - * @} - */ - - -/** @defgroup USBD_HID_Private_Macros - * @{ - */ -/** - * @} - */ - - - - -/** @defgroup USBD_HID_Private_FunctionPrototypes - * @{ - */ - - -static uint8_t USBD_HID_Init (USBD_HandleTypeDef *pdev, - uint8_t cfgidx); - -static uint8_t USBD_HID_DeInit (USBD_HandleTypeDef *pdev, - uint8_t cfgidx); - -static uint8_t USBD_HID_Setup (USBD_HandleTypeDef *pdev, - USBD_SetupReqTypedef *req); - -static uint8_t *USBD_HID_GetFSCfgDesc (uint16_t *length); - -static uint8_t *USBD_HID_GetHSCfgDesc (uint16_t *length); - -static uint8_t *USBD_HID_GetOtherSpeedCfgDesc (uint16_t *length); - -static uint8_t *USBD_HID_GetDeviceQualifierDesc (uint16_t *length); - -static uint8_t USBD_HID_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum); -/** - * @} - */ - -/** @defgroup USBD_HID_Private_Variables - * @{ - */ - -USBD_ClassTypeDef USBD_HID = -{ - USBD_HID_Init, - USBD_HID_DeInit, - USBD_HID_Setup, - NULL, /*EP0_TxSent*/ - NULL, /*EP0_RxReady*/ - USBD_HID_DataIn, /*DataIn*/ - NULL, /*DataOut*/ - NULL, /*SOF */ - NULL, - NULL, - USBD_HID_GetHSCfgDesc, - USBD_HID_GetFSCfgDesc, - USBD_HID_GetOtherSpeedCfgDesc, - USBD_HID_GetDeviceQualifierDesc, -}; - -/* USB HID device FS Configuration Descriptor */ -__ALIGN_BEGIN static uint8_t USBD_HID_CfgFSDesc[USB_HID_CONFIG_DESC_SIZ] __ALIGN_END = -{ - 0x09, /* bLength: Configuration Descriptor size */ - USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ - USB_HID_CONFIG_DESC_SIZ, - /* wTotalLength: Bytes returned */ - 0x00, - 0x01, /*bNumInterfaces: 1 interface*/ - 0x01, /*bConfigurationValue: Configuration value*/ - 0x00, /*iConfiguration: Index of string descriptor describing - the configuration*/ - 0xE0, /*bmAttributes: bus powered and Support Remote Wake-up */ - 0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/ - - /************** Descriptor of Joystick Mouse interface ****************/ - /* 09 */ - 0x09, /*bLength: Interface Descriptor size*/ - USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/ - 0x00, /*bInterfaceNumber: Number of Interface*/ - 0x00, /*bAlternateSetting: Alternate setting*/ - 0x01, /*bNumEndpoints*/ - 0x03, /*bInterfaceClass: HID*/ - 0x01, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/ - 0x02, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/ - 0, /*iInterface: Index of string descriptor*/ - /******************** Descriptor of Joystick Mouse HID ********************/ - /* 18 */ - 0x09, /*bLength: HID Descriptor size*/ - HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/ - 0x11, /*bcdHID: HID Class Spec release number*/ - 0x01, - 0x00, /*bCountryCode: Hardware target country*/ - 0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/ - 0x22, /*bDescriptorType*/ - HID_MOUSE_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ - 0x00, - /******************** Descriptor of Mouse endpoint ********************/ - /* 27 */ - 0x07, /*bLength: Endpoint Descriptor size*/ - USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/ - - HID_EPIN_ADDR, /*bEndpointAddress: Endpoint Address (IN)*/ - 0x03, /*bmAttributes: Interrupt endpoint*/ - HID_EPIN_SIZE, /*wMaxPacketSize: 4 Byte max */ - 0x00, - HID_FS_BINTERVAL, /*bInterval: Polling Interval */ - /* 34 */ -}; - -/* USB HID device HS Configuration Descriptor */ -__ALIGN_BEGIN static uint8_t USBD_HID_CfgHSDesc[USB_HID_CONFIG_DESC_SIZ] __ALIGN_END = -{ - 0x09, /* bLength: Configuration Descriptor size */ - USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ - USB_HID_CONFIG_DESC_SIZ, - /* wTotalLength: Bytes returned */ - 0x00, - 0x01, /*bNumInterfaces: 1 interface*/ - 0x01, /*bConfigurationValue: Configuration value*/ - 0x00, /*iConfiguration: Index of string descriptor describing - the configuration*/ - 0xE0, /*bmAttributes: bus powered and Support Remote Wake-up */ - 0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/ - - /************** Descriptor of Joystick Mouse interface ****************/ - /* 09 */ - 0x09, /*bLength: Interface Descriptor size*/ - USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/ - 0x00, /*bInterfaceNumber: Number of Interface*/ - 0x00, /*bAlternateSetting: Alternate setting*/ - 0x01, /*bNumEndpoints*/ - 0x03, /*bInterfaceClass: HID*/ - 0x01, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/ - 0x02, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/ - 0, /*iInterface: Index of string descriptor*/ - /******************** Descriptor of Joystick Mouse HID ********************/ - /* 18 */ - 0x09, /*bLength: HID Descriptor size*/ - HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/ - 0x11, /*bcdHID: HID Class Spec release number*/ - 0x01, - 0x00, /*bCountryCode: Hardware target country*/ - 0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/ - 0x22, /*bDescriptorType*/ - HID_MOUSE_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ - 0x00, - /******************** Descriptor of Mouse endpoint ********************/ - /* 27 */ - 0x07, /*bLength: Endpoint Descriptor size*/ - USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/ - - HID_EPIN_ADDR, /*bEndpointAddress: Endpoint Address (IN)*/ - 0x03, /*bmAttributes: Interrupt endpoint*/ - HID_EPIN_SIZE, /*wMaxPacketSize: 4 Byte max */ - 0x00, - HID_HS_BINTERVAL, /*bInterval: Polling Interval */ - /* 34 */ -}; - -/* USB HID device Other Speed Configuration Descriptor */ -__ALIGN_BEGIN static uint8_t USBD_HID_OtherSpeedCfgDesc[USB_HID_CONFIG_DESC_SIZ] __ALIGN_END = -{ - 0x09, /* bLength: Configuration Descriptor size */ - USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ - USB_HID_CONFIG_DESC_SIZ, - /* wTotalLength: Bytes returned */ - 0x00, - 0x01, /*bNumInterfaces: 1 interface*/ - 0x01, /*bConfigurationValue: Configuration value*/ - 0x00, /*iConfiguration: Index of string descriptor describing - the configuration*/ - 0xE0, /*bmAttributes: bus powered and Support Remote Wake-up */ - 0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/ - - /************** Descriptor of Joystick Mouse interface ****************/ - /* 09 */ - 0x09, /*bLength: Interface Descriptor size*/ - USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/ - 0x00, /*bInterfaceNumber: Number of Interface*/ - 0x00, /*bAlternateSetting: Alternate setting*/ - 0x01, /*bNumEndpoints*/ - 0x03, /*bInterfaceClass: HID*/ - 0x01, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/ - 0x02, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/ - 0, /*iInterface: Index of string descriptor*/ - /******************** Descriptor of Joystick Mouse HID ********************/ - /* 18 */ - 0x09, /*bLength: HID Descriptor size*/ - HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/ - 0x11, /*bcdHID: HID Class Spec release number*/ - 0x01, - 0x00, /*bCountryCode: Hardware target country*/ - 0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/ - 0x22, /*bDescriptorType*/ - HID_MOUSE_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ - 0x00, - /******************** Descriptor of Mouse endpoint ********************/ - /* 27 */ - 0x07, /*bLength: Endpoint Descriptor size*/ - USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/ - - HID_EPIN_ADDR, /*bEndpointAddress: Endpoint Address (IN)*/ - 0x03, /*bmAttributes: Interrupt endpoint*/ - HID_EPIN_SIZE, /*wMaxPacketSize: 4 Byte max */ - 0x00, - HID_FS_BINTERVAL, /*bInterval: Polling Interval */ - /* 34 */ -}; - - -/* USB HID device Configuration Descriptor */ -__ALIGN_BEGIN static uint8_t USBD_HID_Desc[USB_HID_DESC_SIZ] __ALIGN_END = -{ - /* 18 */ - 0x09, /*bLength: HID Descriptor size*/ - HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/ - 0x11, /*bcdHID: HID Class Spec release number*/ - 0x01, - 0x00, /*bCountryCode: Hardware target country*/ - 0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/ - 0x22, /*bDescriptorType*/ - HID_MOUSE_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ - 0x00, -}; - -/* USB Standard Device Descriptor */ -__ALIGN_BEGIN static uint8_t USBD_HID_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END = -{ - USB_LEN_DEV_QUALIFIER_DESC, - USB_DESC_TYPE_DEVICE_QUALIFIER, - 0x00, - 0x02, - 0x00, - 0x00, - 0x00, - 0x40, - 0x01, - 0x00, -}; - -__ALIGN_BEGIN static uint8_t HID_MOUSE_ReportDesc[HID_MOUSE_REPORT_DESC_SIZE] __ALIGN_END = -{ - 0x05, 0x01, - 0x09, 0x02, - 0xA1, 0x01, - 0x09, 0x01, - - 0xA1, 0x00, - 0x05, 0x09, - 0x19, 0x01, - 0x29, 0x03, - - 0x15, 0x00, - 0x25, 0x01, - 0x95, 0x03, - 0x75, 0x01, - - 0x81, 0x02, - 0x95, 0x01, - 0x75, 0x05, - 0x81, 0x01, - - 0x05, 0x01, - 0x09, 0x30, - 0x09, 0x31, - 0x09, 0x38, - - 0x15, 0x81, - 0x25, 0x7F, - 0x75, 0x08, - 0x95, 0x03, - - 0x81, 0x06, - 0xC0, 0x09, - 0x3c, 0x05, - 0xff, 0x09, - - 0x01, 0x15, - 0x00, 0x25, - 0x01, 0x75, - 0x01, 0x95, - - 0x02, 0xb1, - 0x22, 0x75, - 0x06, 0x95, - 0x01, 0xb1, - - 0x01, 0xc0 -}; - -/** - * @} - */ - -/** @defgroup USBD_HID_Private_Functions - * @{ - */ - -/** - * @brief USBD_HID_Init - * Initialize the HID interface - * @param pdev: device instance - * @param cfgidx: Configuration index - * @retval status - */ -static uint8_t USBD_HID_Init (USBD_HandleTypeDef *pdev, uint8_t cfgidx) -{ - /* Open EP IN */ - USBD_LL_OpenEP(pdev, HID_EPIN_ADDR, USBD_EP_TYPE_INTR, HID_EPIN_SIZE); - pdev->ep_in[HID_EPIN_ADDR & 0xFU].is_used = 1U; - - pdev->pClassData = USBD_malloc(sizeof (USBD_HID_HandleTypeDef)); - - if (pdev->pClassData == NULL) - { - return USBD_FAIL; - } - - ((USBD_HID_HandleTypeDef *)pdev->pClassData)->state = HID_IDLE; - - return USBD_OK; -} - -/** - * @brief USBD_HID_Init - * DeInitialize the HID layer - * @param pdev: device instance - * @param cfgidx: Configuration index - * @retval status - */ -static uint8_t USBD_HID_DeInit (USBD_HandleTypeDef *pdev, - uint8_t cfgidx) -{ - /* Close HID EPs */ - USBD_LL_CloseEP(pdev, HID_EPIN_ADDR); - pdev->ep_in[HID_EPIN_ADDR & 0xFU].is_used = 0U; - - /* FRee allocated memory */ - if(pdev->pClassData != NULL) - { - USBD_free(pdev->pClassData); - pdev->pClassData = NULL; - } - - return USBD_OK; -} - -/** - * @brief USBD_HID_Setup - * Handle the HID specific requests - * @param pdev: instance - * @param req: usb requests - * @retval status - */ -static uint8_t USBD_HID_Setup (USBD_HandleTypeDef *pdev, - USBD_SetupReqTypedef *req) -{ - USBD_HID_HandleTypeDef *hhid = (USBD_HID_HandleTypeDef*) pdev->pClassData; - uint16_t len = 0U; - uint8_t *pbuf = NULL; - uint16_t status_info = 0U; - USBD_StatusTypeDef ret = USBD_OK; - - switch (req->bmRequest & USB_REQ_TYPE_MASK) - { - case USB_REQ_TYPE_CLASS : - switch (req->bRequest) - { - case HID_REQ_SET_PROTOCOL: - hhid->Protocol = (uint8_t)(req->wValue); - break; - - case HID_REQ_GET_PROTOCOL: - USBD_CtlSendData (pdev, (uint8_t *)(void *)&hhid->Protocol, 1U); - break; - - case HID_REQ_SET_IDLE: - hhid->IdleState = (uint8_t)(req->wValue >> 8); - break; - - case HID_REQ_GET_IDLE: - USBD_CtlSendData (pdev, (uint8_t *)(void *)&hhid->IdleState, 1U); - break; - - default: - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - break; - } - break; - case USB_REQ_TYPE_STANDARD: - switch (req->bRequest) - { - case USB_REQ_GET_STATUS: - if (pdev->dev_state == USBD_STATE_CONFIGURED) - { - USBD_CtlSendData (pdev, (uint8_t *)(void *)&status_info, 2U); - } - else - { - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - } - break; - - case USB_REQ_GET_DESCRIPTOR: - if(req->wValue >> 8 == HID_REPORT_DESC) - { - len = MIN(HID_MOUSE_REPORT_DESC_SIZE , req->wLength); - pbuf = HID_MOUSE_ReportDesc; - } - else if(req->wValue >> 8 == HID_DESCRIPTOR_TYPE) - { - pbuf = USBD_HID_Desc; - len = MIN(USB_HID_DESC_SIZ, req->wLength); - } - else - { - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - break; - } - USBD_CtlSendData (pdev, pbuf, len); - break; - - case USB_REQ_GET_INTERFACE : - if (pdev->dev_state == USBD_STATE_CONFIGURED) - { - USBD_CtlSendData (pdev, (uint8_t *)(void *)&hhid->AltSetting, 1U); - } - else - { - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - } - break; - - case USB_REQ_SET_INTERFACE : - if (pdev->dev_state == USBD_STATE_CONFIGURED) - { - hhid->AltSetting = (uint8_t)(req->wValue); - } - else - { - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - } - break; - - default: - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - break; - } - break; - - default: - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - break; - } - - return ret; -} - -/** - * @brief USBD_HID_SendReport - * Send HID Report - * @param pdev: device instance - * @param buff: pointer to report - * @retval status - */ -uint8_t USBD_HID_SendReport (USBD_HandleTypeDef *pdev, - uint8_t *report, - uint16_t len) -{ - USBD_HID_HandleTypeDef *hhid = (USBD_HID_HandleTypeDef*)pdev->pClassData; - - if (pdev->dev_state == USBD_STATE_CONFIGURED ) - { - if(hhid->state == HID_IDLE) - { - hhid->state = HID_BUSY; - USBD_LL_Transmit (pdev, - HID_EPIN_ADDR, - report, - len); - } - } - return USBD_OK; -} - -/** - * @brief USBD_HID_GetPollingInterval - * return polling interval from endpoint descriptor - * @param pdev: device instance - * @retval polling interval - */ -uint32_t USBD_HID_GetPollingInterval (USBD_HandleTypeDef *pdev) -{ - uint32_t polling_interval = 0U; - - /* HIGH-speed endpoints */ - if(pdev->dev_speed == USBD_SPEED_HIGH) - { - /* Sets the data transfer polling interval for high speed transfers. - Values between 1..16 are allowed. Values correspond to interval - of 2 ^ (bInterval-1). This option (8 ms, corresponds to HID_HS_BINTERVAL */ - polling_interval = (((1U <<(HID_HS_BINTERVAL - 1U))) / 8U); - } - else /* LOW and FULL-speed endpoints */ - { - /* Sets the data transfer polling interval for low and full - speed transfers */ - polling_interval = HID_FS_BINTERVAL; - } - - return ((uint32_t)(polling_interval)); -} - -/** - * @brief USBD_HID_GetCfgFSDesc - * return FS configuration descriptor - * @param speed : current device speed - * @param length : pointer data length - * @retval pointer to descriptor buffer - */ -static uint8_t *USBD_HID_GetFSCfgDesc (uint16_t *length) -{ - *length = sizeof (USBD_HID_CfgFSDesc); - return USBD_HID_CfgFSDesc; -} - -/** - * @brief USBD_HID_GetCfgHSDesc - * return HS configuration descriptor - * @param speed : current device speed - * @param length : pointer data length - * @retval pointer to descriptor buffer - */ -static uint8_t *USBD_HID_GetHSCfgDesc (uint16_t *length) -{ - *length = sizeof (USBD_HID_CfgHSDesc); - return USBD_HID_CfgHSDesc; -} - -/** - * @brief USBD_HID_GetOtherSpeedCfgDesc - * return other speed configuration descriptor - * @param speed : current device speed - * @param length : pointer data length - * @retval pointer to descriptor buffer - */ -static uint8_t *USBD_HID_GetOtherSpeedCfgDesc (uint16_t *length) -{ - *length = sizeof (USBD_HID_OtherSpeedCfgDesc); - return USBD_HID_OtherSpeedCfgDesc; -} - -/** - * @brief USBD_HID_DataIn - * handle data IN Stage - * @param pdev: device instance - * @param epnum: endpoint index - * @retval status - */ -static uint8_t USBD_HID_DataIn (USBD_HandleTypeDef *pdev, - uint8_t epnum) -{ - - /* Ensure that the FIFO is empty before a new transfer, this condition could - be caused by a new transfer before the end of the previous transfer */ - ((USBD_HID_HandleTypeDef *)pdev->pClassData)->state = HID_IDLE; - return USBD_OK; -} - - -/** -* @brief DeviceQualifierDescriptor -* return Device Qualifier descriptor -* @param length : pointer data length -* @retval pointer to descriptor buffer -*/ -static uint8_t *USBD_HID_GetDeviceQualifierDesc (uint16_t *length) -{ - *length = sizeof (USBD_HID_DeviceQualifierDesc); - return USBD_HID_DeviceQualifierDesc; -} - -/** - * @} - */ - - -/** - * @} - */ - - -/** - * @} - */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_hid.c + * @author MCD Application Team + * @brief This file provides the HID core functions. + * + * @verbatim + * + * =================================================================== + * HID Class Description + * =================================================================== + * This module manages the HID class V1.11 following the "Device Class Definition + * for Human Interface Devices (HID) Version 1.11 Jun 27, 2001". + * This driver implements the following aspects of the specification: + * - The Boot Interface Subclass + * - The Mouse protocol + * - Usage Page : Generic Desktop + * - Usage : Joystick + * - Collection : Application + * + * @note In HS mode and when the DMA is used, all variables and data structures + * dealing with the DMA during the transaction process should be 32-bit aligned. + * + * + * @endverbatim + * + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + + /* BSPDependencies + - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" + - "stm32xxxxx_{eval}{discovery}_io.c" + EndBSPDependencies */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_hid.h" +#include "usbd_ctlreq.h" + + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + + +/** @defgroup USBD_HID + * @brief usbd core module + * @{ + */ + +/** @defgroup USBD_HID_Private_TypesDefinitions + * @{ + */ +/** + * @} + */ + + +/** @defgroup USBD_HID_Private_Defines + * @{ + */ + +/** + * @} + */ + + +/** @defgroup USBD_HID_Private_Macros + * @{ + */ +/** + * @} + */ + + + + +/** @defgroup USBD_HID_Private_FunctionPrototypes + * @{ + */ + + +static uint8_t USBD_HID_Init (USBD_HandleTypeDef *pdev, + uint8_t cfgidx); + +static uint8_t USBD_HID_DeInit (USBD_HandleTypeDef *pdev, + uint8_t cfgidx); + +static uint8_t USBD_HID_Setup (USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req); + +static uint8_t *USBD_HID_GetFSCfgDesc (uint16_t *length); + +static uint8_t *USBD_HID_GetHSCfgDesc (uint16_t *length); + +static uint8_t *USBD_HID_GetOtherSpeedCfgDesc (uint16_t *length); + +static uint8_t *USBD_HID_GetDeviceQualifierDesc (uint16_t *length); + +static uint8_t USBD_HID_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum); +/** + * @} + */ + +/** @defgroup USBD_HID_Private_Variables + * @{ + */ + +USBD_ClassTypeDef USBD_HID = +{ + USBD_HID_Init, + USBD_HID_DeInit, + USBD_HID_Setup, + NULL, /*EP0_TxSent*/ + NULL, /*EP0_RxReady*/ + USBD_HID_DataIn, /*DataIn*/ + NULL, /*DataOut*/ + NULL, /*SOF */ + NULL, + NULL, + USBD_HID_GetHSCfgDesc, + USBD_HID_GetFSCfgDesc, + USBD_HID_GetOtherSpeedCfgDesc, + USBD_HID_GetDeviceQualifierDesc, +}; + +/* USB HID device FS Configuration Descriptor */ +__ALIGN_BEGIN static uint8_t USBD_HID_CfgFSDesc[USB_HID_CONFIG_DESC_SIZ] __ALIGN_END = +{ + 0x09, /* bLength: Configuration Descriptor size */ + USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ + USB_HID_CONFIG_DESC_SIZ, + /* wTotalLength: Bytes returned */ + 0x00, + 0x01, /*bNumInterfaces: 1 interface*/ + 0x01, /*bConfigurationValue: Configuration value*/ + 0x00, /*iConfiguration: Index of string descriptor describing + the configuration*/ + 0xE0, /*bmAttributes: bus powered and Support Remote Wake-up */ + 0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/ + + /************** Descriptor of Joystick Mouse interface ****************/ + /* 09 */ + 0x09, /*bLength: Interface Descriptor size*/ + USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/ + 0x00, /*bInterfaceNumber: Number of Interface*/ + 0x00, /*bAlternateSetting: Alternate setting*/ + 0x01, /*bNumEndpoints*/ + 0x03, /*bInterfaceClass: HID*/ + 0x01, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/ + 0x02, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/ + 0, /*iInterface: Index of string descriptor*/ + /******************** Descriptor of Joystick Mouse HID ********************/ + /* 18 */ + 0x09, /*bLength: HID Descriptor size*/ + HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/ + 0x11, /*bcdHID: HID Class Spec release number*/ + 0x01, + 0x00, /*bCountryCode: Hardware target country*/ + 0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/ + 0x22, /*bDescriptorType*/ + HID_MOUSE_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ + 0x00, + /******************** Descriptor of Mouse endpoint ********************/ + /* 27 */ + 0x07, /*bLength: Endpoint Descriptor size*/ + USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/ + + HID_EPIN_ADDR, /*bEndpointAddress: Endpoint Address (IN)*/ + 0x03, /*bmAttributes: Interrupt endpoint*/ + HID_EPIN_SIZE, /*wMaxPacketSize: 4 Byte max */ + 0x00, + HID_FS_BINTERVAL, /*bInterval: Polling Interval */ + /* 34 */ +}; + +/* USB HID device HS Configuration Descriptor */ +__ALIGN_BEGIN static uint8_t USBD_HID_CfgHSDesc[USB_HID_CONFIG_DESC_SIZ] __ALIGN_END = +{ + 0x09, /* bLength: Configuration Descriptor size */ + USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ + USB_HID_CONFIG_DESC_SIZ, + /* wTotalLength: Bytes returned */ + 0x00, + 0x01, /*bNumInterfaces: 1 interface*/ + 0x01, /*bConfigurationValue: Configuration value*/ + 0x00, /*iConfiguration: Index of string descriptor describing + the configuration*/ + 0xE0, /*bmAttributes: bus powered and Support Remote Wake-up */ + 0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/ + + /************** Descriptor of Joystick Mouse interface ****************/ + /* 09 */ + 0x09, /*bLength: Interface Descriptor size*/ + USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/ + 0x00, /*bInterfaceNumber: Number of Interface*/ + 0x00, /*bAlternateSetting: Alternate setting*/ + 0x01, /*bNumEndpoints*/ + 0x03, /*bInterfaceClass: HID*/ + 0x01, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/ + 0x02, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/ + 0, /*iInterface: Index of string descriptor*/ + /******************** Descriptor of Joystick Mouse HID ********************/ + /* 18 */ + 0x09, /*bLength: HID Descriptor size*/ + HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/ + 0x11, /*bcdHID: HID Class Spec release number*/ + 0x01, + 0x00, /*bCountryCode: Hardware target country*/ + 0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/ + 0x22, /*bDescriptorType*/ + HID_MOUSE_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ + 0x00, + /******************** Descriptor of Mouse endpoint ********************/ + /* 27 */ + 0x07, /*bLength: Endpoint Descriptor size*/ + USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/ + + HID_EPIN_ADDR, /*bEndpointAddress: Endpoint Address (IN)*/ + 0x03, /*bmAttributes: Interrupt endpoint*/ + HID_EPIN_SIZE, /*wMaxPacketSize: 4 Byte max */ + 0x00, + HID_HS_BINTERVAL, /*bInterval: Polling Interval */ + /* 34 */ +}; + +/* USB HID device Other Speed Configuration Descriptor */ +__ALIGN_BEGIN static uint8_t USBD_HID_OtherSpeedCfgDesc[USB_HID_CONFIG_DESC_SIZ] __ALIGN_END = +{ + 0x09, /* bLength: Configuration Descriptor size */ + USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ + USB_HID_CONFIG_DESC_SIZ, + /* wTotalLength: Bytes returned */ + 0x00, + 0x01, /*bNumInterfaces: 1 interface*/ + 0x01, /*bConfigurationValue: Configuration value*/ + 0x00, /*iConfiguration: Index of string descriptor describing + the configuration*/ + 0xE0, /*bmAttributes: bus powered and Support Remote Wake-up */ + 0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/ + + /************** Descriptor of Joystick Mouse interface ****************/ + /* 09 */ + 0x09, /*bLength: Interface Descriptor size*/ + USB_DESC_TYPE_INTERFACE,/*bDescriptorType: Interface descriptor type*/ + 0x00, /*bInterfaceNumber: Number of Interface*/ + 0x00, /*bAlternateSetting: Alternate setting*/ + 0x01, /*bNumEndpoints*/ + 0x03, /*bInterfaceClass: HID*/ + 0x01, /*bInterfaceSubClass : 1=BOOT, 0=no boot*/ + 0x02, /*nInterfaceProtocol : 0=none, 1=keyboard, 2=mouse*/ + 0, /*iInterface: Index of string descriptor*/ + /******************** Descriptor of Joystick Mouse HID ********************/ + /* 18 */ + 0x09, /*bLength: HID Descriptor size*/ + HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/ + 0x11, /*bcdHID: HID Class Spec release number*/ + 0x01, + 0x00, /*bCountryCode: Hardware target country*/ + 0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/ + 0x22, /*bDescriptorType*/ + HID_MOUSE_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ + 0x00, + /******************** Descriptor of Mouse endpoint ********************/ + /* 27 */ + 0x07, /*bLength: Endpoint Descriptor size*/ + USB_DESC_TYPE_ENDPOINT, /*bDescriptorType:*/ + + HID_EPIN_ADDR, /*bEndpointAddress: Endpoint Address (IN)*/ + 0x03, /*bmAttributes: Interrupt endpoint*/ + HID_EPIN_SIZE, /*wMaxPacketSize: 4 Byte max */ + 0x00, + HID_FS_BINTERVAL, /*bInterval: Polling Interval */ + /* 34 */ +}; + + +/* USB HID device Configuration Descriptor */ +__ALIGN_BEGIN static uint8_t USBD_HID_Desc[USB_HID_DESC_SIZ] __ALIGN_END = +{ + /* 18 */ + 0x09, /*bLength: HID Descriptor size*/ + HID_DESCRIPTOR_TYPE, /*bDescriptorType: HID*/ + 0x11, /*bcdHID: HID Class Spec release number*/ + 0x01, + 0x00, /*bCountryCode: Hardware target country*/ + 0x01, /*bNumDescriptors: Number of HID class descriptors to follow*/ + 0x22, /*bDescriptorType*/ + HID_MOUSE_REPORT_DESC_SIZE,/*wItemLength: Total length of Report descriptor*/ + 0x00, +}; + +/* USB Standard Device Descriptor */ +__ALIGN_BEGIN static uint8_t USBD_HID_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END = +{ + USB_LEN_DEV_QUALIFIER_DESC, + USB_DESC_TYPE_DEVICE_QUALIFIER, + 0x00, + 0x02, + 0x00, + 0x00, + 0x00, + 0x40, + 0x01, + 0x00, +}; + +__ALIGN_BEGIN static uint8_t HID_MOUSE_ReportDesc[HID_MOUSE_REPORT_DESC_SIZE] __ALIGN_END = +{ + 0x05, 0x01, + 0x09, 0x02, + 0xA1, 0x01, + 0x09, 0x01, + + 0xA1, 0x00, + 0x05, 0x09, + 0x19, 0x01, + 0x29, 0x03, + + 0x15, 0x00, + 0x25, 0x01, + 0x95, 0x03, + 0x75, 0x01, + + 0x81, 0x02, + 0x95, 0x01, + 0x75, 0x05, + 0x81, 0x01, + + 0x05, 0x01, + 0x09, 0x30, + 0x09, 0x31, + 0x09, 0x38, + + 0x15, 0x81, + 0x25, 0x7F, + 0x75, 0x08, + 0x95, 0x03, + + 0x81, 0x06, + 0xC0, 0x09, + 0x3c, 0x05, + 0xff, 0x09, + + 0x01, 0x15, + 0x00, 0x25, + 0x01, 0x75, + 0x01, 0x95, + + 0x02, 0xb1, + 0x22, 0x75, + 0x06, 0x95, + 0x01, 0xb1, + + 0x01, 0xc0 +}; + +/** + * @} + */ + +/** @defgroup USBD_HID_Private_Functions + * @{ + */ + +/** + * @brief USBD_HID_Init + * Initialize the HID interface + * @param pdev: device instance + * @param cfgidx: Configuration index + * @retval status + */ +static uint8_t USBD_HID_Init (USBD_HandleTypeDef *pdev, uint8_t cfgidx) +{ + /* Open EP IN */ + USBD_LL_OpenEP(pdev, HID_EPIN_ADDR, USBD_EP_TYPE_INTR, HID_EPIN_SIZE); + pdev->ep_in[HID_EPIN_ADDR & 0xFU].is_used = 1U; + + pdev->pClassData = USBD_malloc(sizeof (USBD_HID_HandleTypeDef)); + + if (pdev->pClassData == NULL) + { + return USBD_FAIL; + } + + ((USBD_HID_HandleTypeDef *)pdev->pClassData)->state = HID_IDLE; + + return USBD_OK; +} + +/** + * @brief USBD_HID_Init + * DeInitialize the HID layer + * @param pdev: device instance + * @param cfgidx: Configuration index + * @retval status + */ +static uint8_t USBD_HID_DeInit (USBD_HandleTypeDef *pdev, + uint8_t cfgidx) +{ + /* Close HID EPs */ + USBD_LL_CloseEP(pdev, HID_EPIN_ADDR); + pdev->ep_in[HID_EPIN_ADDR & 0xFU].is_used = 0U; + + /* FRee allocated memory */ + if(pdev->pClassData != NULL) + { + USBD_free(pdev->pClassData); + pdev->pClassData = NULL; + } + + return USBD_OK; +} + +/** + * @brief USBD_HID_Setup + * Handle the HID specific requests + * @param pdev: instance + * @param req: usb requests + * @retval status + */ +static uint8_t USBD_HID_Setup (USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req) +{ + USBD_HID_HandleTypeDef *hhid = (USBD_HID_HandleTypeDef*) pdev->pClassData; + uint16_t len = 0U; + uint8_t *pbuf = NULL; + uint16_t status_info = 0U; + USBD_StatusTypeDef ret = USBD_OK; + + switch (req->bmRequest & USB_REQ_TYPE_MASK) + { + case USB_REQ_TYPE_CLASS : + switch (req->bRequest) + { + case HID_REQ_SET_PROTOCOL: + hhid->Protocol = (uint8_t)(req->wValue); + break; + + case HID_REQ_GET_PROTOCOL: + USBD_CtlSendData (pdev, (uint8_t *)(void *)&hhid->Protocol, 1U); + break; + + case HID_REQ_SET_IDLE: + hhid->IdleState = (uint8_t)(req->wValue >> 8); + break; + + case HID_REQ_GET_IDLE: + USBD_CtlSendData (pdev, (uint8_t *)(void *)&hhid->IdleState, 1U); + break; + + default: + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + break; + } + break; + case USB_REQ_TYPE_STANDARD: + switch (req->bRequest) + { + case USB_REQ_GET_STATUS: + if (pdev->dev_state == USBD_STATE_CONFIGURED) + { + USBD_CtlSendData (pdev, (uint8_t *)(void *)&status_info, 2U); + } + else + { + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + } + break; + + case USB_REQ_GET_DESCRIPTOR: + if(req->wValue >> 8 == HID_REPORT_DESC) + { + len = MIN(HID_MOUSE_REPORT_DESC_SIZE , req->wLength); + pbuf = HID_MOUSE_ReportDesc; + } + else if(req->wValue >> 8 == HID_DESCRIPTOR_TYPE) + { + pbuf = USBD_HID_Desc; + len = MIN(USB_HID_DESC_SIZ, req->wLength); + } + else + { + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + break; + } + USBD_CtlSendData (pdev, pbuf, len); + break; + + case USB_REQ_GET_INTERFACE : + if (pdev->dev_state == USBD_STATE_CONFIGURED) + { + USBD_CtlSendData (pdev, (uint8_t *)(void *)&hhid->AltSetting, 1U); + } + else + { + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + } + break; + + case USB_REQ_SET_INTERFACE : + if (pdev->dev_state == USBD_STATE_CONFIGURED) + { + hhid->AltSetting = (uint8_t)(req->wValue); + } + else + { + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + } + break; + + default: + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + break; + } + break; + + default: + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + break; + } + + return ret; +} + +/** + * @brief USBD_HID_SendReport + * Send HID Report + * @param pdev: device instance + * @param buff: pointer to report + * @retval status + */ +uint8_t USBD_HID_SendReport (USBD_HandleTypeDef *pdev, + uint8_t *report, + uint16_t len) +{ + USBD_HID_HandleTypeDef *hhid = (USBD_HID_HandleTypeDef*)pdev->pClassData; + + if (pdev->dev_state == USBD_STATE_CONFIGURED ) + { + if(hhid->state == HID_IDLE) + { + hhid->state = HID_BUSY; + USBD_LL_Transmit (pdev, + HID_EPIN_ADDR, + report, + len); + } + } + return USBD_OK; +} + +/** + * @brief USBD_HID_GetPollingInterval + * return polling interval from endpoint descriptor + * @param pdev: device instance + * @retval polling interval + */ +uint32_t USBD_HID_GetPollingInterval (USBD_HandleTypeDef *pdev) +{ + uint32_t polling_interval = 0U; + + /* HIGH-speed endpoints */ + if(pdev->dev_speed == USBD_SPEED_HIGH) + { + /* Sets the data transfer polling interval for high speed transfers. + Values between 1..16 are allowed. Values correspond to interval + of 2 ^ (bInterval-1). This option (8 ms, corresponds to HID_HS_BINTERVAL */ + polling_interval = (((1U <<(HID_HS_BINTERVAL - 1U))) / 8U); + } + else /* LOW and FULL-speed endpoints */ + { + /* Sets the data transfer polling interval for low and full + speed transfers */ + polling_interval = HID_FS_BINTERVAL; + } + + return ((uint32_t)(polling_interval)); +} + +/** + * @brief USBD_HID_GetCfgFSDesc + * return FS configuration descriptor + * @param speed : current device speed + * @param length : pointer data length + * @retval pointer to descriptor buffer + */ +static uint8_t *USBD_HID_GetFSCfgDesc (uint16_t *length) +{ + *length = sizeof (USBD_HID_CfgFSDesc); + return USBD_HID_CfgFSDesc; +} + +/** + * @brief USBD_HID_GetCfgHSDesc + * return HS configuration descriptor + * @param speed : current device speed + * @param length : pointer data length + * @retval pointer to descriptor buffer + */ +static uint8_t *USBD_HID_GetHSCfgDesc (uint16_t *length) +{ + *length = sizeof (USBD_HID_CfgHSDesc); + return USBD_HID_CfgHSDesc; +} + +/** + * @brief USBD_HID_GetOtherSpeedCfgDesc + * return other speed configuration descriptor + * @param speed : current device speed + * @param length : pointer data length + * @retval pointer to descriptor buffer + */ +static uint8_t *USBD_HID_GetOtherSpeedCfgDesc (uint16_t *length) +{ + *length = sizeof (USBD_HID_OtherSpeedCfgDesc); + return USBD_HID_OtherSpeedCfgDesc; +} + +/** + * @brief USBD_HID_DataIn + * handle data IN Stage + * @param pdev: device instance + * @param epnum: endpoint index + * @retval status + */ +static uint8_t USBD_HID_DataIn (USBD_HandleTypeDef *pdev, + uint8_t epnum) +{ + + /* Ensure that the FIFO is empty before a new transfer, this condition could + be caused by a new transfer before the end of the previous transfer */ + ((USBD_HID_HandleTypeDef *)pdev->pClassData)->state = HID_IDLE; + return USBD_OK; +} + + +/** +* @brief DeviceQualifierDescriptor +* return Device Qualifier descriptor +* @param length : pointer data length +* @retval pointer to descriptor buffer +*/ +static uint8_t *USBD_HID_GetDeviceQualifierDesc (uint16_t *length) +{ + *length = sizeof (USBD_HID_DeviceQualifierDesc); + return USBD_HID_DeviceQualifierDesc; +} + +/** + * @} + */ + + +/** + * @} + */ + + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Inc/usbd_msc.h b/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Inc/usbd_msc.h index b7bc342208..176b227664 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Inc/usbd_msc.h +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Inc/usbd_msc.h @@ -1,129 +1,129 @@ -/** - ****************************************************************************** - * @file usbd_msc.h - * @author MCD Application Team - * @brief Header for the usbd_msc.c file - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USBD_MSC_H -#define __USBD_MSC_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_msc_bot.h" -#include "usbd_msc_scsi.h" -#include "usbd_ioreq.h" - -/** @addtogroup USBD_MSC_BOT - * @{ - */ - -/** @defgroup USBD_MSC - * @brief This file is the Header file for usbd_msc.c - * @{ - */ - - -/** @defgroup USBD_BOT_Exported_Defines - * @{ - */ -/* MSC Class Config */ -#ifndef MSC_MEDIA_PACKET -#define MSC_MEDIA_PACKET 512U -#endif /* MSC_MEDIA_PACKET */ - -#define MSC_MAX_FS_PACKET 0x40U -#define MSC_MAX_HS_PACKET 0x200U - -#define BOT_GET_MAX_LUN 0xFE -#define BOT_RESET 0xFF -#define USB_MSC_CONFIG_DESC_SIZ 32 - - -#define MSC_EPIN_ADDR 0x81U -#define MSC_EPOUT_ADDR 0x01U - -/** - * @} - */ - -/** @defgroup USB_CORE_Exported_Types - * @{ - */ -typedef struct _USBD_STORAGE -{ - int8_t (* Init) (uint8_t lun); - int8_t (* GetCapacity) (uint8_t lun, uint32_t *block_num, uint16_t *block_size); - int8_t (* IsReady) (uint8_t lun); - int8_t (* IsWriteProtected) (uint8_t lun); - int8_t (* Read) (uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len); - int8_t (* Write)(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len); - int8_t (* GetMaxLun)(void); - int8_t *pInquiry; - -}USBD_StorageTypeDef; - - -typedef struct -{ - uint32_t max_lun; - uint32_t interface; - uint8_t bot_state; - uint8_t bot_status; - uint16_t bot_data_length; - uint8_t bot_data[MSC_MEDIA_PACKET]; - USBD_MSC_BOT_CBWTypeDef cbw; - USBD_MSC_BOT_CSWTypeDef csw; - - USBD_SCSI_SenseTypeDef scsi_sense [SENSE_LIST_DEEPTH]; - uint8_t scsi_sense_head; - uint8_t scsi_sense_tail; - - uint16_t scsi_blk_size; - uint32_t scsi_blk_nbr; - - uint32_t scsi_blk_addr; - uint32_t scsi_blk_len; -} -USBD_MSC_BOT_HandleTypeDef; - -/* Structure for MSC process */ -extern USBD_ClassTypeDef USBD_MSC; -#define USBD_MSC_CLASS &USBD_MSC - -uint8_t USBD_MSC_RegisterStorage (USBD_HandleTypeDef *pdev, - USBD_StorageTypeDef *fops); -/** - * @} - */ - -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USBD_MSC_H */ -/** - * @} - */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_msc.h + * @author MCD Application Team + * @brief Header for the usbd_msc.c file + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USBD_MSC_H +#define __USBD_MSC_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_msc_bot.h" +#include "usbd_msc_scsi.h" +#include "usbd_ioreq.h" + +/** @addtogroup USBD_MSC_BOT + * @{ + */ + +/** @defgroup USBD_MSC + * @brief This file is the Header file for usbd_msc.c + * @{ + */ + + +/** @defgroup USBD_BOT_Exported_Defines + * @{ + */ +/* MSC Class Config */ +#ifndef MSC_MEDIA_PACKET +#define MSC_MEDIA_PACKET 512U +#endif /* MSC_MEDIA_PACKET */ + +#define MSC_MAX_FS_PACKET 0x40U +#define MSC_MAX_HS_PACKET 0x200U + +#define BOT_GET_MAX_LUN 0xFE +#define BOT_RESET 0xFF +#define USB_MSC_CONFIG_DESC_SIZ 32 + + +#define MSC_EPIN_ADDR 0x81U +#define MSC_EPOUT_ADDR 0x01U + +/** + * @} + */ + +/** @defgroup USB_CORE_Exported_Types + * @{ + */ +typedef struct _USBD_STORAGE +{ + int8_t (* Init) (uint8_t lun); + int8_t (* GetCapacity) (uint8_t lun, uint32_t *block_num, uint16_t *block_size); + int8_t (* IsReady) (uint8_t lun); + int8_t (* IsWriteProtected) (uint8_t lun); + int8_t (* Read) (uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len); + int8_t (* Write)(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len); + int8_t (* GetMaxLun)(void); + int8_t *pInquiry; + +}USBD_StorageTypeDef; + + +typedef struct +{ + uint32_t max_lun; + uint32_t interface; + uint8_t bot_state; + uint8_t bot_status; + uint16_t bot_data_length; + uint8_t bot_data[MSC_MEDIA_PACKET]; + USBD_MSC_BOT_CBWTypeDef cbw; + USBD_MSC_BOT_CSWTypeDef csw; + + USBD_SCSI_SenseTypeDef scsi_sense [SENSE_LIST_DEEPTH]; + uint8_t scsi_sense_head; + uint8_t scsi_sense_tail; + + uint16_t scsi_blk_size; + uint32_t scsi_blk_nbr; + + uint32_t scsi_blk_addr; + uint32_t scsi_blk_len; +} +USBD_MSC_BOT_HandleTypeDef; + +/* Structure for MSC process */ +extern USBD_ClassTypeDef USBD_MSC; +#define USBD_MSC_CLASS &USBD_MSC + +uint8_t USBD_MSC_RegisterStorage (USBD_HandleTypeDef *pdev, + USBD_StorageTypeDef *fops); +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USBD_MSC_H */ +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Inc/usbd_msc_bot.h b/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Inc/usbd_msc_bot.h index 932aaccc4e..2c4911771b 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Inc/usbd_msc_bot.h +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Inc/usbd_msc_bot.h @@ -1,150 +1,150 @@ -/** - ****************************************************************************** - * @file usbd_msc_bot.h - * @author MCD Application Team - * @brief Header for the usbd_msc_bot.c file - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USBD_MSC_BOT_H -#define __USBD_MSC_BOT_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_core.h" - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - -/** @defgroup MSC_BOT - * @brief This file is the Header file for usbd_msc_bot.c - * @{ - */ - - -/** @defgroup USBD_CORE_Exported_Defines - * @{ - */ -#define USBD_BOT_IDLE 0U /* Idle state */ -#define USBD_BOT_DATA_OUT 1U /* Data Out state */ -#define USBD_BOT_DATA_IN 2U /* Data In state */ -#define USBD_BOT_LAST_DATA_IN 3U /* Last Data In Last */ -#define USBD_BOT_SEND_DATA 4U /* Send Immediate data */ -#define USBD_BOT_NO_DATA 5U /* No data Stage */ - -#define USBD_BOT_CBW_SIGNATURE 0x43425355U -#define USBD_BOT_CSW_SIGNATURE 0x53425355U -#define USBD_BOT_CBW_LENGTH 31U -#define USBD_BOT_CSW_LENGTH 13U -#define USBD_BOT_MAX_DATA 256U - -/* CSW Status Definitions */ -#define USBD_CSW_CMD_PASSED 0x00U -#define USBD_CSW_CMD_FAILED 0x01U -#define USBD_CSW_PHASE_ERROR 0x02U - -/* BOT Status */ -#define USBD_BOT_STATUS_NORMAL 0U -#define USBD_BOT_STATUS_RECOVERY 1U -#define USBD_BOT_STATUS_ERROR 2U - - -#define USBD_DIR_IN 0U -#define USBD_DIR_OUT 1U -#define USBD_BOTH_DIR 2U - -/** - * @} - */ - -/** @defgroup MSC_CORE_Private_TypesDefinitions - * @{ - */ - -typedef struct -{ - uint32_t dSignature; - uint32_t dTag; - uint32_t dDataLength; - uint8_t bmFlags; - uint8_t bLUN; - uint8_t bCBLength; - uint8_t CB[16]; - uint8_t ReservedForAlign; -} -USBD_MSC_BOT_CBWTypeDef; - - -typedef struct -{ - uint32_t dSignature; - uint32_t dTag; - uint32_t dDataResidue; - uint8_t bStatus; - uint8_t ReservedForAlign[3]; -} -USBD_MSC_BOT_CSWTypeDef; - -/** - * @} - */ - - -/** @defgroup USBD_CORE_Exported_Types - * @{ - */ - -/** - * @} - */ -/** @defgroup USBD_CORE_Exported_FunctionsPrototypes - * @{ - */ -void MSC_BOT_Init (USBD_HandleTypeDef *pdev); -void MSC_BOT_Reset (USBD_HandleTypeDef *pdev); -void MSC_BOT_DeInit (USBD_HandleTypeDef *pdev); -void MSC_BOT_DataIn (USBD_HandleTypeDef *pdev, - uint8_t epnum); - -void MSC_BOT_DataOut (USBD_HandleTypeDef *pdev, - uint8_t epnum); - -void MSC_BOT_SendCSW (USBD_HandleTypeDef *pdev, - uint8_t CSW_Status); - -void MSC_BOT_CplClrFeature (USBD_HandleTypeDef *pdev, - uint8_t epnum); -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USBD_MSC_BOT_H */ -/** - * @} - */ - -/** -* @} -*/ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - +/** + ****************************************************************************** + * @file usbd_msc_bot.h + * @author MCD Application Team + * @brief Header for the usbd_msc_bot.c file + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USBD_MSC_BOT_H +#define __USBD_MSC_BOT_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_core.h" + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + +/** @defgroup MSC_BOT + * @brief This file is the Header file for usbd_msc_bot.c + * @{ + */ + + +/** @defgroup USBD_CORE_Exported_Defines + * @{ + */ +#define USBD_BOT_IDLE 0U /* Idle state */ +#define USBD_BOT_DATA_OUT 1U /* Data Out state */ +#define USBD_BOT_DATA_IN 2U /* Data In state */ +#define USBD_BOT_LAST_DATA_IN 3U /* Last Data In Last */ +#define USBD_BOT_SEND_DATA 4U /* Send Immediate data */ +#define USBD_BOT_NO_DATA 5U /* No data Stage */ + +#define USBD_BOT_CBW_SIGNATURE 0x43425355U +#define USBD_BOT_CSW_SIGNATURE 0x53425355U +#define USBD_BOT_CBW_LENGTH 31U +#define USBD_BOT_CSW_LENGTH 13U +#define USBD_BOT_MAX_DATA 256U + +/* CSW Status Definitions */ +#define USBD_CSW_CMD_PASSED 0x00U +#define USBD_CSW_CMD_FAILED 0x01U +#define USBD_CSW_PHASE_ERROR 0x02U + +/* BOT Status */ +#define USBD_BOT_STATUS_NORMAL 0U +#define USBD_BOT_STATUS_RECOVERY 1U +#define USBD_BOT_STATUS_ERROR 2U + + +#define USBD_DIR_IN 0U +#define USBD_DIR_OUT 1U +#define USBD_BOTH_DIR 2U + +/** + * @} + */ + +/** @defgroup MSC_CORE_Private_TypesDefinitions + * @{ + */ + +typedef struct +{ + uint32_t dSignature; + uint32_t dTag; + uint32_t dDataLength; + uint8_t bmFlags; + uint8_t bLUN; + uint8_t bCBLength; + uint8_t CB[16]; + uint8_t ReservedForAlign; +} +USBD_MSC_BOT_CBWTypeDef; + + +typedef struct +{ + uint32_t dSignature; + uint32_t dTag; + uint32_t dDataResidue; + uint8_t bStatus; + uint8_t ReservedForAlign[3]; +} +USBD_MSC_BOT_CSWTypeDef; + +/** + * @} + */ + + +/** @defgroup USBD_CORE_Exported_Types + * @{ + */ + +/** + * @} + */ +/** @defgroup USBD_CORE_Exported_FunctionsPrototypes + * @{ + */ +void MSC_BOT_Init (USBD_HandleTypeDef *pdev); +void MSC_BOT_Reset (USBD_HandleTypeDef *pdev); +void MSC_BOT_DeInit (USBD_HandleTypeDef *pdev); +void MSC_BOT_DataIn (USBD_HandleTypeDef *pdev, + uint8_t epnum); + +void MSC_BOT_DataOut (USBD_HandleTypeDef *pdev, + uint8_t epnum); + +void MSC_BOT_SendCSW (USBD_HandleTypeDef *pdev, + uint8_t CSW_Status); + +void MSC_BOT_CplClrFeature (USBD_HandleTypeDef *pdev, + uint8_t epnum); +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USBD_MSC_BOT_H */ +/** + * @} + */ + +/** +* @} +*/ +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Inc/usbd_msc_data.h b/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Inc/usbd_msc_data.h index ce9fdec2c5..26beb1792b 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Inc/usbd_msc_data.h +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Inc/usbd_msc_data.h @@ -1,103 +1,103 @@ -/** - ****************************************************************************** - * @file usbd_msc_data.h - * @author MCD Application Team - * @brief Header for the usbd_msc_data.c file - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USBD_MSC_DATA_H -#define __USBD_MSC_DATA_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_conf.h" - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - -/** @defgroup USB_INFO - * @brief general defines for the usb device library file - * @{ - */ - -/** @defgroup USB_INFO_Exported_Defines - * @{ - */ -#define MODE_SENSE6_LEN 8U -#define MODE_SENSE10_LEN 8U -#define LENGTH_INQUIRY_PAGE00 7U -#define LENGTH_FORMAT_CAPACITIES 20U - -/** - * @} - */ - - -/** @defgroup USBD_INFO_Exported_TypesDefinitions - * @{ - */ -/** - * @} - */ - - - -/** @defgroup USBD_INFO_Exported_Macros - * @{ - */ - -/** - * @} - */ - -/** @defgroup USBD_INFO_Exported_Variables - * @{ - */ -extern const uint8_t MSC_Page00_Inquiry_Data[]; -extern const uint8_t MSC_Mode_Sense6_data[]; -extern const uint8_t MSC_Mode_Sense10_data[] ; - -/** - * @} - */ - -/** @defgroup USBD_INFO_Exported_FunctionsPrototype - * @{ - */ - -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USBD_MSC_DATA_H */ - -/** - * @} - */ - -/** -* @} -*/ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_msc_data.h + * @author MCD Application Team + * @brief Header for the usbd_msc_data.c file + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USBD_MSC_DATA_H +#define __USBD_MSC_DATA_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_conf.h" + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + +/** @defgroup USB_INFO + * @brief general defines for the usb device library file + * @{ + */ + +/** @defgroup USB_INFO_Exported_Defines + * @{ + */ +#define MODE_SENSE6_LEN 8U +#define MODE_SENSE10_LEN 8U +#define LENGTH_INQUIRY_PAGE00 7U +#define LENGTH_FORMAT_CAPACITIES 20U + +/** + * @} + */ + + +/** @defgroup USBD_INFO_Exported_TypesDefinitions + * @{ + */ +/** + * @} + */ + + + +/** @defgroup USBD_INFO_Exported_Macros + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_INFO_Exported_Variables + * @{ + */ +extern const uint8_t MSC_Page00_Inquiry_Data[]; +extern const uint8_t MSC_Mode_Sense6_data[]; +extern const uint8_t MSC_Mode_Sense10_data[] ; + +/** + * @} + */ + +/** @defgroup USBD_INFO_Exported_FunctionsPrototype + * @{ + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USBD_MSC_DATA_H */ + +/** + * @} + */ + +/** +* @} +*/ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Inc/usbd_msc_scsi.h b/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Inc/usbd_msc_scsi.h index a588202fce..f90a5d8e34 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Inc/usbd_msc_scsi.h +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Inc/usbd_msc_scsi.h @@ -1,189 +1,189 @@ -/** - ****************************************************************************** - * @file usbd_msc_scsi.h - * @author MCD Application Team - * @brief Header for the usbd_msc_scsi.c file - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USBD_MSC_SCSI_H -#define __USBD_MSC_SCSI_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_def.h" - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - -/** @defgroup USBD_SCSI - * @brief header file for the storage disk file - * @{ - */ - -/** @defgroup USBD_SCSI_Exported_Defines - * @{ - */ - -#define SENSE_LIST_DEEPTH 4U - -/* SCSI Commands */ -#define SCSI_FORMAT_UNIT 0x04U -#define SCSI_INQUIRY 0x12U -#define SCSI_MODE_SELECT6 0x15U -#define SCSI_MODE_SELECT10 0x55U -#define SCSI_MODE_SENSE6 0x1AU -#define SCSI_MODE_SENSE10 0x5AU -#define SCSI_ALLOW_MEDIUM_REMOVAL 0x1EU -#define SCSI_READ6 0x08U -#define SCSI_READ10 0x28U -#define SCSI_READ12 0xA8U -#define SCSI_READ16 0x88U - -#define SCSI_READ_CAPACITY10 0x25U -#define SCSI_READ_CAPACITY16 0x9EU - -#define SCSI_REQUEST_SENSE 0x03U -#define SCSI_START_STOP_UNIT 0x1BU -#define SCSI_TEST_UNIT_READY 0x00U -#define SCSI_WRITE6 0x0AU -#define SCSI_WRITE10 0x2AU -#define SCSI_WRITE12 0xAAU -#define SCSI_WRITE16 0x8AU - -#define SCSI_VERIFY10 0x2FU -#define SCSI_VERIFY12 0xAFU -#define SCSI_VERIFY16 0x8FU - -#define SCSI_SEND_DIAGNOSTIC 0x1DU -#define SCSI_READ_FORMAT_CAPACITIES 0x23U - -#define NO_SENSE 0U -#define RECOVERED_ERROR 1U -#define NOT_READY 2U -#define MEDIUM_ERROR 3U -#define HARDWARE_ERROR 4U -#define ILLEGAL_REQUEST 5U -#define UNIT_ATTENTION 6U -#define DATA_PROTECT 7U -#define BLANK_CHECK 8U -#define VENDOR_SPECIFIC 9U -#define COPY_ABORTED 10U -#define ABORTED_COMMAND 11U -#define VOLUME_OVERFLOW 13U -#define MISCOMPARE 14U - - -#define INVALID_CDB 0x20U -#define INVALID_FIELED_IN_COMMAND 0x24U -#define PARAMETER_LIST_LENGTH_ERROR 0x1AU -#define INVALID_FIELD_IN_PARAMETER_LIST 0x26U -#define ADDRESS_OUT_OF_RANGE 0x21U -#define MEDIUM_NOT_PRESENT 0x3AU -#define MEDIUM_HAVE_CHANGED 0x28U -#define WRITE_PROTECTED 0x27U -#define UNRECOVERED_READ_ERROR 0x11U -#define WRITE_FAULT 0x03U - -#define READ_FORMAT_CAPACITY_DATA_LEN 0x0CU -#define READ_CAPACITY10_DATA_LEN 0x08U -#define MODE_SENSE10_DATA_LEN 0x08U -#define MODE_SENSE6_DATA_LEN 0x04U -#define REQUEST_SENSE_DATA_LEN 0x12U -#define STANDARD_INQUIRY_DATA_LEN 0x24U -#define BLKVFY 0x04U - -extern uint8_t Page00_Inquiry_Data[]; -extern uint8_t Standard_Inquiry_Data[]; -extern uint8_t Standard_Inquiry_Data2[]; -extern uint8_t Mode_Sense6_data[]; -extern uint8_t Mode_Sense10_data[]; -extern uint8_t Scsi_Sense_Data[]; -extern uint8_t ReadCapacity10_Data[]; -extern uint8_t ReadFormatCapacity_Data []; -/** - * @} - */ - - -/** @defgroup USBD_SCSI_Exported_TypesDefinitions - * @{ - */ - -typedef struct _SENSE_ITEM { - char Skey; - union { - struct _ASCs { - char ASC; - char ASCQ; - }b; - uint8_t ASC; - char *pData; - } w; -} USBD_SCSI_SenseTypeDef; -/** - * @} - */ - -/** @defgroup USBD_SCSI_Exported_Macros - * @{ - */ - -/** - * @} - */ - -/** @defgroup USBD_SCSI_Exported_Variables - * @{ - */ - -/** - * @} - */ -/** @defgroup USBD_SCSI_Exported_FunctionsPrototype - * @{ - */ -int8_t SCSI_ProcessCmd(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *cmd); - -void SCSI_SenseCode(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t sKey, - uint8_t ASC); - -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USBD_MSC_SCSI_H */ -/** - * @} - */ - -/** - * @} - */ - -/** -* @} -*/ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - +/** + ****************************************************************************** + * @file usbd_msc_scsi.h + * @author MCD Application Team + * @brief Header for the usbd_msc_scsi.c file + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USBD_MSC_SCSI_H +#define __USBD_MSC_SCSI_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_def.h" + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + +/** @defgroup USBD_SCSI + * @brief header file for the storage disk file + * @{ + */ + +/** @defgroup USBD_SCSI_Exported_Defines + * @{ + */ + +#define SENSE_LIST_DEEPTH 4U + +/* SCSI Commands */ +#define SCSI_FORMAT_UNIT 0x04U +#define SCSI_INQUIRY 0x12U +#define SCSI_MODE_SELECT6 0x15U +#define SCSI_MODE_SELECT10 0x55U +#define SCSI_MODE_SENSE6 0x1AU +#define SCSI_MODE_SENSE10 0x5AU +#define SCSI_ALLOW_MEDIUM_REMOVAL 0x1EU +#define SCSI_READ6 0x08U +#define SCSI_READ10 0x28U +#define SCSI_READ12 0xA8U +#define SCSI_READ16 0x88U + +#define SCSI_READ_CAPACITY10 0x25U +#define SCSI_READ_CAPACITY16 0x9EU + +#define SCSI_REQUEST_SENSE 0x03U +#define SCSI_START_STOP_UNIT 0x1BU +#define SCSI_TEST_UNIT_READY 0x00U +#define SCSI_WRITE6 0x0AU +#define SCSI_WRITE10 0x2AU +#define SCSI_WRITE12 0xAAU +#define SCSI_WRITE16 0x8AU + +#define SCSI_VERIFY10 0x2FU +#define SCSI_VERIFY12 0xAFU +#define SCSI_VERIFY16 0x8FU + +#define SCSI_SEND_DIAGNOSTIC 0x1DU +#define SCSI_READ_FORMAT_CAPACITIES 0x23U + +#define NO_SENSE 0U +#define RECOVERED_ERROR 1U +#define NOT_READY 2U +#define MEDIUM_ERROR 3U +#define HARDWARE_ERROR 4U +#define ILLEGAL_REQUEST 5U +#define UNIT_ATTENTION 6U +#define DATA_PROTECT 7U +#define BLANK_CHECK 8U +#define VENDOR_SPECIFIC 9U +#define COPY_ABORTED 10U +#define ABORTED_COMMAND 11U +#define VOLUME_OVERFLOW 13U +#define MISCOMPARE 14U + + +#define INVALID_CDB 0x20U +#define INVALID_FIELED_IN_COMMAND 0x24U +#define PARAMETER_LIST_LENGTH_ERROR 0x1AU +#define INVALID_FIELD_IN_PARAMETER_LIST 0x26U +#define ADDRESS_OUT_OF_RANGE 0x21U +#define MEDIUM_NOT_PRESENT 0x3AU +#define MEDIUM_HAVE_CHANGED 0x28U +#define WRITE_PROTECTED 0x27U +#define UNRECOVERED_READ_ERROR 0x11U +#define WRITE_FAULT 0x03U + +#define READ_FORMAT_CAPACITY_DATA_LEN 0x0CU +#define READ_CAPACITY10_DATA_LEN 0x08U +#define MODE_SENSE10_DATA_LEN 0x08U +#define MODE_SENSE6_DATA_LEN 0x04U +#define REQUEST_SENSE_DATA_LEN 0x12U +#define STANDARD_INQUIRY_DATA_LEN 0x24U +#define BLKVFY 0x04U + +extern uint8_t Page00_Inquiry_Data[]; +extern uint8_t Standard_Inquiry_Data[]; +extern uint8_t Standard_Inquiry_Data2[]; +extern uint8_t Mode_Sense6_data[]; +extern uint8_t Mode_Sense10_data[]; +extern uint8_t Scsi_Sense_Data[]; +extern uint8_t ReadCapacity10_Data[]; +extern uint8_t ReadFormatCapacity_Data []; +/** + * @} + */ + + +/** @defgroup USBD_SCSI_Exported_TypesDefinitions + * @{ + */ + +typedef struct _SENSE_ITEM { + char Skey; + union { + struct _ASCs { + char ASC; + char ASCQ; + }b; + uint8_t ASC; + char *pData; + } w; +} USBD_SCSI_SenseTypeDef; +/** + * @} + */ + +/** @defgroup USBD_SCSI_Exported_Macros + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_SCSI_Exported_Variables + * @{ + */ + +/** + * @} + */ +/** @defgroup USBD_SCSI_Exported_FunctionsPrototype + * @{ + */ +int8_t SCSI_ProcessCmd(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *cmd); + +void SCSI_SenseCode(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t sKey, + uint8_t ASC); + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USBD_MSC_SCSI_H */ +/** + * @} + */ + +/** + * @} + */ + +/** +* @} +*/ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Inc/usbd_msc_storage_template.h b/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Inc/usbd_msc_storage_template.h index 297c0d5e37..311e7f26f4 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Inc/usbd_msc_storage_template.h +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Inc/usbd_msc_storage_template.h @@ -1,97 +1,97 @@ -/** - ****************************************************************************** - * @file usbd_msc_storage.h - * @author MCD Application Team - * @brief Header file for the usbd_msc_storage.c file - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USBD_MSC_STORAGE_H -#define __USBD_MSC_STORAGE_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_msc.h" - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - -/** @defgroup USBD_STORAGE - * @brief header file for the usbd_msc_storage.c file - * @{ - */ - -/** @defgroup USBD_STORAGE_Exported_Defines - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBD_STORAGE_Exported_Types - * @{ - */ - - -/** - * @} - */ - - - -/** @defgroup USBD_STORAGE_Exported_Macros - * @{ - */ - -/** - * @} - */ - -/** @defgroup USBD_STORAGE_Exported_Variables - * @{ - */ -extern USBD_StorageTypeDef USBD_MSC_Template_fops; -/** - * @} - */ - -/** @defgroup USBD_STORAGE_Exported_FunctionsPrototype - * @{ - */ - - -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USBD_MSC_STORAGE_H */ - -/** - * @} - */ - -/** -* @} -*/ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_msc_storage.h + * @author MCD Application Team + * @brief Header file for the usbd_msc_storage.c file + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USBD_MSC_STORAGE_H +#define __USBD_MSC_STORAGE_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_msc.h" + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + +/** @defgroup USBD_STORAGE + * @brief header file for the usbd_msc_storage.c file + * @{ + */ + +/** @defgroup USBD_STORAGE_Exported_Defines + * @{ + */ +/** + * @} + */ + + +/** @defgroup USBD_STORAGE_Exported_Types + * @{ + */ + + +/** + * @} + */ + + + +/** @defgroup USBD_STORAGE_Exported_Macros + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_STORAGE_Exported_Variables + * @{ + */ +extern USBD_StorageTypeDef USBD_MSC_Template_fops; +/** + * @} + */ + +/** @defgroup USBD_STORAGE_Exported_FunctionsPrototype + * @{ + */ + + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USBD_MSC_STORAGE_H */ + +/** + * @} + */ + +/** +* @} +*/ +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc.c b/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc.c index 95cff9b4e3..1f9ac66704 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc.c +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc.c @@ -1,619 +1,619 @@ -/** - ****************************************************************************** - * @file usbd_msc.c - * @author MCD Application Team - * @brief This file provides all the MSC core functions. - * - * @verbatim - * - * =================================================================== - * MSC Class Description - * =================================================================== - * This module manages the MSC class V1.0 following the "Universal - * Serial Bus Mass Storage Class (MSC) Bulk-Only Transport (BOT) Version 1.0 - * Sep. 31, 1999". - * This driver implements the following aspects of the specification: - * - Bulk-Only Transport protocol - * - Subclass : SCSI transparent command set (ref. SCSI Primary Commands - 3 (SPC-3)) - * - * @endverbatim - * - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - - /* BSPDependencies - - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" - - "stm32xxxxx_{eval}{discovery}_io.c" - - "stm32xxxxx_{eval}{discovery}{adafruit}_sd.c" - EndBSPDependencies */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_msc.h" - - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - - -/** @defgroup MSC_CORE - * @brief Mass storage core module - * @{ - */ - -/** @defgroup MSC_CORE_Private_TypesDefinitions - * @{ - */ -/** - * @} - */ - - -/** @defgroup MSC_CORE_Private_Defines - * @{ - */ - -/** - * @} - */ - - -/** @defgroup MSC_CORE_Private_Macros - * @{ - */ -/** - * @} - */ - - -/** @defgroup MSC_CORE_Private_FunctionPrototypes - * @{ - */ -uint8_t USBD_MSC_Init (USBD_HandleTypeDef *pdev, - uint8_t cfgidx); - -uint8_t USBD_MSC_DeInit (USBD_HandleTypeDef *pdev, - uint8_t cfgidx); - -uint8_t USBD_MSC_Setup (USBD_HandleTypeDef *pdev, - USBD_SetupReqTypedef *req); - -uint8_t USBD_MSC_DataIn (USBD_HandleTypeDef *pdev, - uint8_t epnum); - - -uint8_t USBD_MSC_DataOut (USBD_HandleTypeDef *pdev, - uint8_t epnum); - -uint8_t *USBD_MSC_GetHSCfgDesc (uint16_t *length); - -uint8_t *USBD_MSC_GetFSCfgDesc (uint16_t *length); - -uint8_t *USBD_MSC_GetOtherSpeedCfgDesc (uint16_t *length); - -uint8_t *USBD_MSC_GetDeviceQualifierDescriptor (uint16_t *length); - - -/** - * @} - */ - - -/** @defgroup MSC_CORE_Private_Variables - * @{ - */ - - -USBD_ClassTypeDef USBD_MSC = -{ - USBD_MSC_Init, - USBD_MSC_DeInit, - USBD_MSC_Setup, - NULL, /*EP0_TxSent*/ - NULL, /*EP0_RxReady*/ - USBD_MSC_DataIn, - USBD_MSC_DataOut, - NULL, /*SOF */ - NULL, - NULL, - USBD_MSC_GetHSCfgDesc, - USBD_MSC_GetFSCfgDesc, - USBD_MSC_GetOtherSpeedCfgDesc, - USBD_MSC_GetDeviceQualifierDescriptor, -}; - -/* USB Mass storage device Configuration Descriptor */ -/* All Descriptors (Configuration, Interface, Endpoint, Class, Vendor */ -__ALIGN_BEGIN uint8_t USBD_MSC_CfgHSDesc[USB_MSC_CONFIG_DESC_SIZ] __ALIGN_END = -{ - - 0x09, /* bLength: Configuation Descriptor size */ - USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ - USB_MSC_CONFIG_DESC_SIZ, - - 0x00, - 0x01, /* bNumInterfaces: 1 interface */ - 0x01, /* bConfigurationValue: */ - 0x04, /* iConfiguration: */ - 0xC0, /* bmAttributes: */ - 0x32, /* MaxPower 100 mA */ - - /******************** Mass Storage interface ********************/ - 0x09, /* bLength: Interface Descriptor size */ - 0x04, /* bDescriptorType: */ - 0x00, /* bInterfaceNumber: Number of Interface */ - 0x00, /* bAlternateSetting: Alternate setting */ - 0x02, /* bNumEndpoints*/ - 0x08, /* bInterfaceClass: MSC Class */ - 0x06, /* bInterfaceSubClass : SCSI transparent*/ - 0x50, /* nInterfaceProtocol */ - 0x05, /* iInterface: */ - /******************** Mass Storage Endpoints ********************/ - 0x07, /*Endpoint descriptor length = 7*/ - 0x05, /*Endpoint descriptor type */ - MSC_EPIN_ADDR, /*Endpoint address (IN, address 1) */ - 0x02, /*Bulk endpoint type */ - LOBYTE(MSC_MAX_HS_PACKET), - HIBYTE(MSC_MAX_HS_PACKET), - 0x00, /*Polling interval in milliseconds */ - - 0x07, /*Endpoint descriptor length = 7 */ - 0x05, /*Endpoint descriptor type */ - MSC_EPOUT_ADDR, /*Endpoint address (OUT, address 1) */ - 0x02, /*Bulk endpoint type */ - LOBYTE(MSC_MAX_HS_PACKET), - HIBYTE(MSC_MAX_HS_PACKET), - 0x00 /*Polling interval in milliseconds*/ -}; - -/* USB Mass storage device Configuration Descriptor */ -/* All Descriptors (Configuration, Interface, Endpoint, Class, Vendor */ -uint8_t USBD_MSC_CfgFSDesc[USB_MSC_CONFIG_DESC_SIZ] __ALIGN_END = -{ - - 0x09, /* bLength: Configuation Descriptor size */ - USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ - USB_MSC_CONFIG_DESC_SIZ, - - 0x00, - 0x01, /* bNumInterfaces: 1 interface */ - 0x01, /* bConfigurationValue: */ - 0x04, /* iConfiguration: */ - 0xC0, /* bmAttributes: */ - 0x32, /* MaxPower 100 mA */ - - /******************** Mass Storage interface ********************/ - 0x09, /* bLength: Interface Descriptor size */ - 0x04, /* bDescriptorType: */ - 0x00, /* bInterfaceNumber: Number of Interface */ - 0x00, /* bAlternateSetting: Alternate setting */ - 0x02, /* bNumEndpoints*/ - 0x08, /* bInterfaceClass: MSC Class */ - 0x06, /* bInterfaceSubClass : SCSI transparent*/ - 0x50, /* nInterfaceProtocol */ - 0x05, /* iInterface: */ - /******************** Mass Storage Endpoints ********************/ - 0x07, /*Endpoint descriptor length = 7*/ - 0x05, /*Endpoint descriptor type */ - MSC_EPIN_ADDR, /*Endpoint address (IN, address 1) */ - 0x02, /*Bulk endpoint type */ - LOBYTE(MSC_MAX_FS_PACKET), - HIBYTE(MSC_MAX_FS_PACKET), - 0x00, /*Polling interval in milliseconds */ - - 0x07, /*Endpoint descriptor length = 7 */ - 0x05, /*Endpoint descriptor type */ - MSC_EPOUT_ADDR, /*Endpoint address (OUT, address 1) */ - 0x02, /*Bulk endpoint type */ - LOBYTE(MSC_MAX_FS_PACKET), - HIBYTE(MSC_MAX_FS_PACKET), - 0x00 /*Polling interval in milliseconds*/ -}; - -__ALIGN_BEGIN uint8_t USBD_MSC_OtherSpeedCfgDesc[USB_MSC_CONFIG_DESC_SIZ] __ALIGN_END = -{ - - 0x09, /* bLength: Configuation Descriptor size */ - USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION, - USB_MSC_CONFIG_DESC_SIZ, - - 0x00, - 0x01, /* bNumInterfaces: 1 interface */ - 0x01, /* bConfigurationValue: */ - 0x04, /* iConfiguration: */ - 0xC0, /* bmAttributes: */ - 0x32, /* MaxPower 100 mA */ - - /******************** Mass Storage interface ********************/ - 0x09, /* bLength: Interface Descriptor size */ - 0x04, /* bDescriptorType: */ - 0x00, /* bInterfaceNumber: Number of Interface */ - 0x00, /* bAlternateSetting: Alternate setting */ - 0x02, /* bNumEndpoints*/ - 0x08, /* bInterfaceClass: MSC Class */ - 0x06, /* bInterfaceSubClass : SCSI transparent command set*/ - 0x50, /* nInterfaceProtocol */ - 0x05, /* iInterface: */ - /******************** Mass Storage Endpoints ********************/ - 0x07, /*Endpoint descriptor length = 7*/ - 0x05, /*Endpoint descriptor type */ - MSC_EPIN_ADDR, /*Endpoint address (IN, address 1) */ - 0x02, /*Bulk endpoint type */ - 0x40, - 0x00, - 0x00, /*Polling interval in milliseconds */ - - 0x07, /*Endpoint descriptor length = 7 */ - 0x05, /*Endpoint descriptor type */ - MSC_EPOUT_ADDR, /*Endpoint address (OUT, address 1) */ - 0x02, /*Bulk endpoint type */ - 0x40, - 0x00, - 0x00 /*Polling interval in milliseconds*/ -}; - -/* USB Standard Device Descriptor */ -__ALIGN_BEGIN uint8_t USBD_MSC_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END = -{ - USB_LEN_DEV_QUALIFIER_DESC, - USB_DESC_TYPE_DEVICE_QUALIFIER, - 0x00, - 0x02, - 0x00, - 0x00, - 0x00, - MSC_MAX_FS_PACKET, - 0x01, - 0x00, -}; -/** - * @} - */ - - -/** @defgroup MSC_CORE_Private_Functions - * @{ - */ - -/** - * @brief USBD_MSC_Init - * Initialize the mass storage configuration - * @param pdev: device instance - * @param cfgidx: configuration index - * @retval status - */ -uint8_t USBD_MSC_Init (USBD_HandleTypeDef *pdev, uint8_t cfgidx) -{ - if(pdev->dev_speed == USBD_SPEED_HIGH) - { - /* Open EP OUT */ - USBD_LL_OpenEP(pdev, MSC_EPOUT_ADDR, USBD_EP_TYPE_BULK, MSC_MAX_HS_PACKET); - pdev->ep_out[MSC_EPOUT_ADDR & 0xFU].is_used = 1U; - - /* Open EP IN */ - USBD_LL_OpenEP(pdev, MSC_EPIN_ADDR, USBD_EP_TYPE_BULK, MSC_MAX_HS_PACKET); - pdev->ep_in[MSC_EPIN_ADDR & 0xFU].is_used = 1U; - } - else - { - /* Open EP OUT */ - USBD_LL_OpenEP(pdev, MSC_EPOUT_ADDR, USBD_EP_TYPE_BULK, MSC_MAX_FS_PACKET); - pdev->ep_out[MSC_EPOUT_ADDR & 0xFU].is_used = 1U; - - /* Open EP IN */ - USBD_LL_OpenEP(pdev, MSC_EPIN_ADDR, USBD_EP_TYPE_BULK, MSC_MAX_FS_PACKET); - pdev->ep_in[MSC_EPIN_ADDR & 0xFU].is_used = 1U; - } - pdev->pClassData = USBD_malloc(sizeof (USBD_MSC_BOT_HandleTypeDef)); - - if(pdev->pClassData == NULL) - { - return USBD_FAIL; - } - - /* Init the BOT layer */ - MSC_BOT_Init(pdev); - - return USBD_OK; -} - -/** - * @brief USBD_MSC_DeInit - * DeInitilaize the mass storage configuration - * @param pdev: device instance - * @param cfgidx: configuration index - * @retval status - */ -uint8_t USBD_MSC_DeInit (USBD_HandleTypeDef *pdev, - uint8_t cfgidx) -{ - /* Close MSC EPs */ - USBD_LL_CloseEP(pdev, MSC_EPOUT_ADDR); - pdev->ep_out[MSC_EPOUT_ADDR & 0xFU].is_used = 0U; - - /* Close EP IN */ - USBD_LL_CloseEP(pdev, MSC_EPIN_ADDR); - pdev->ep_in[MSC_EPIN_ADDR & 0xFU].is_used = 0U; - - /* De-Init the BOT layer */ - MSC_BOT_DeInit(pdev); - - /* Free MSC Class Resources */ - if(pdev->pClassData != NULL) - { - USBD_free(pdev->pClassData); - pdev->pClassData = NULL; - } - return USBD_OK; -} -/** -* @brief USBD_MSC_Setup -* Handle the MSC specific requests -* @param pdev: device instance -* @param req: USB request -* @retval status -*/ -uint8_t USBD_MSC_Setup (USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req) -{ - USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*) pdev->pClassData; - uint8_t ret = USBD_OK; - uint16_t status_info = 0U; - - switch (req->bmRequest & USB_REQ_TYPE_MASK) - { - - /* Class request */ - case USB_REQ_TYPE_CLASS: - switch (req->bRequest) - { - case BOT_GET_MAX_LUN: - if((req->wValue == 0U) && (req->wLength == 1U) && - ((req->bmRequest & 0x80U) == 0x80U)) - { - hmsc->max_lun = (uint32_t)((USBD_StorageTypeDef *)pdev->pUserData)->GetMaxLun(); - USBD_CtlSendData (pdev, (uint8_t *)(void *)&hmsc->max_lun, 1U); - } - else - { - USBD_CtlError(pdev, req); - ret = USBD_FAIL; - } - break; - - case BOT_RESET : - if((req->wValue == 0U) && (req->wLength == 0U) && - ((req->bmRequest & 0x80U) != 0x80U)) - { - MSC_BOT_Reset(pdev); - } - else - { - USBD_CtlError(pdev , req); - ret = USBD_FAIL; - } - break; - - default: - USBD_CtlError(pdev , req); - ret = USBD_FAIL; - break; - } - break; - /* Interface & Endpoint request */ - case USB_REQ_TYPE_STANDARD: - switch (req->bRequest) - { - case USB_REQ_GET_STATUS: - if (pdev->dev_state == USBD_STATE_CONFIGURED) - { - USBD_CtlSendData (pdev, (uint8_t *)(void *)&status_info, 2U); - } - else - { - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - } - break; - - case USB_REQ_GET_INTERFACE: - if (pdev->dev_state == USBD_STATE_CONFIGURED) - { - USBD_CtlSendData (pdev, (uint8_t *)(void *)&hmsc->interface, 1U); - } - else - { - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - } - break; - - case USB_REQ_SET_INTERFACE: - if (pdev->dev_state == USBD_STATE_CONFIGURED) - { - hmsc->interface = (uint8_t)(req->wValue); - } - else - { - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - } - break; - - case USB_REQ_CLEAR_FEATURE: - - /* Flush the FIFO and Clear the stall status */ - USBD_LL_FlushEP(pdev, (uint8_t)req->wIndex); - - /* Reactivate the EP */ - USBD_LL_CloseEP (pdev , (uint8_t)req->wIndex); - if((((uint8_t)req->wIndex) & 0x80U) == 0x80U) - { - pdev->ep_in[(uint8_t)req->wIndex & 0xFU].is_used = 0U; - if(pdev->dev_speed == USBD_SPEED_HIGH) - { - /* Open EP IN */ - USBD_LL_OpenEP(pdev, MSC_EPIN_ADDR, USBD_EP_TYPE_BULK, - MSC_MAX_HS_PACKET); - } - else - { - /* Open EP IN */ - USBD_LL_OpenEP(pdev, MSC_EPIN_ADDR, USBD_EP_TYPE_BULK, - MSC_MAX_FS_PACKET); - } - pdev->ep_in[MSC_EPIN_ADDR & 0xFU].is_used = 1U; - } - else - { - pdev->ep_out[(uint8_t)req->wIndex & 0xFU].is_used = 0U; - if(pdev->dev_speed == USBD_SPEED_HIGH) - { - /* Open EP OUT */ - USBD_LL_OpenEP(pdev, MSC_EPOUT_ADDR, USBD_EP_TYPE_BULK, - MSC_MAX_HS_PACKET); - } - else - { - /* Open EP OUT */ - USBD_LL_OpenEP(pdev, MSC_EPOUT_ADDR, USBD_EP_TYPE_BULK, - MSC_MAX_FS_PACKET); - } - pdev->ep_out[MSC_EPOUT_ADDR & 0xFU].is_used = 1U; - } - - /* Handle BOT error */ - MSC_BOT_CplClrFeature(pdev, (uint8_t)req->wIndex); - break; - - default: - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - break; - } - break; - - default: - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - break; - } - - return ret; -} - -/** -* @brief USBD_MSC_DataIn -* handle data IN Stage -* @param pdev: device instance -* @param epnum: endpoint index -* @retval status -*/ -uint8_t USBD_MSC_DataIn (USBD_HandleTypeDef *pdev, - uint8_t epnum) -{ - MSC_BOT_DataIn(pdev , epnum); - return USBD_OK; -} - -/** -* @brief USBD_MSC_DataOut -* handle data OUT Stage -* @param pdev: device instance -* @param epnum: endpoint index -* @retval status -*/ -uint8_t USBD_MSC_DataOut (USBD_HandleTypeDef *pdev, - uint8_t epnum) -{ - MSC_BOT_DataOut(pdev , epnum); - return USBD_OK; -} - -/** -* @brief USBD_MSC_GetHSCfgDesc -* return configuration descriptor -* @param length : pointer data length -* @retval pointer to descriptor buffer -*/ -uint8_t *USBD_MSC_GetHSCfgDesc (uint16_t *length) -{ - *length = sizeof (USBD_MSC_CfgHSDesc); - return USBD_MSC_CfgHSDesc; -} - -/** -* @brief USBD_MSC_GetFSCfgDesc -* return configuration descriptor -* @param length : pointer data length -* @retval pointer to descriptor buffer -*/ -uint8_t *USBD_MSC_GetFSCfgDesc (uint16_t *length) -{ - *length = sizeof (USBD_MSC_CfgFSDesc); - return USBD_MSC_CfgFSDesc; -} - -/** -* @brief USBD_MSC_GetOtherSpeedCfgDesc -* return other speed configuration descriptor -* @param length : pointer data length -* @retval pointer to descriptor buffer -*/ -uint8_t *USBD_MSC_GetOtherSpeedCfgDesc (uint16_t *length) -{ - *length = sizeof (USBD_MSC_OtherSpeedCfgDesc); - return USBD_MSC_OtherSpeedCfgDesc; -} -/** -* @brief DeviceQualifierDescriptor -* return Device Qualifier descriptor -* @param length : pointer data length -* @retval pointer to descriptor buffer -*/ -uint8_t *USBD_MSC_GetDeviceQualifierDescriptor (uint16_t *length) -{ - *length = sizeof (USBD_MSC_DeviceQualifierDesc); - return USBD_MSC_DeviceQualifierDesc; -} - -/** -* @brief USBD_MSC_RegisterStorage -* @param fops: storage callback -* @retval status -*/ -uint8_t USBD_MSC_RegisterStorage (USBD_HandleTypeDef *pdev, - USBD_StorageTypeDef *fops) -{ - if(fops != NULL) - { - pdev->pUserData = fops; - } - return USBD_OK; -} - -/** - * @} - */ - - -/** - * @} - */ - - -/** - * @} - */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_msc.c + * @author MCD Application Team + * @brief This file provides all the MSC core functions. + * + * @verbatim + * + * =================================================================== + * MSC Class Description + * =================================================================== + * This module manages the MSC class V1.0 following the "Universal + * Serial Bus Mass Storage Class (MSC) Bulk-Only Transport (BOT) Version 1.0 + * Sep. 31, 1999". + * This driver implements the following aspects of the specification: + * - Bulk-Only Transport protocol + * - Subclass : SCSI transparent command set (ref. SCSI Primary Commands - 3 (SPC-3)) + * + * @endverbatim + * + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + + /* BSPDependencies + - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" + - "stm32xxxxx_{eval}{discovery}_io.c" + - "stm32xxxxx_{eval}{discovery}{adafruit}_sd.c" + EndBSPDependencies */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_msc.h" + + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + + +/** @defgroup MSC_CORE + * @brief Mass storage core module + * @{ + */ + +/** @defgroup MSC_CORE_Private_TypesDefinitions + * @{ + */ +/** + * @} + */ + + +/** @defgroup MSC_CORE_Private_Defines + * @{ + */ + +/** + * @} + */ + + +/** @defgroup MSC_CORE_Private_Macros + * @{ + */ +/** + * @} + */ + + +/** @defgroup MSC_CORE_Private_FunctionPrototypes + * @{ + */ +uint8_t USBD_MSC_Init (USBD_HandleTypeDef *pdev, + uint8_t cfgidx); + +uint8_t USBD_MSC_DeInit (USBD_HandleTypeDef *pdev, + uint8_t cfgidx); + +uint8_t USBD_MSC_Setup (USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req); + +uint8_t USBD_MSC_DataIn (USBD_HandleTypeDef *pdev, + uint8_t epnum); + + +uint8_t USBD_MSC_DataOut (USBD_HandleTypeDef *pdev, + uint8_t epnum); + +uint8_t *USBD_MSC_GetHSCfgDesc (uint16_t *length); + +uint8_t *USBD_MSC_GetFSCfgDesc (uint16_t *length); + +uint8_t *USBD_MSC_GetOtherSpeedCfgDesc (uint16_t *length); + +uint8_t *USBD_MSC_GetDeviceQualifierDescriptor (uint16_t *length); + + +/** + * @} + */ + + +/** @defgroup MSC_CORE_Private_Variables + * @{ + */ + + +USBD_ClassTypeDef USBD_MSC = +{ + USBD_MSC_Init, + USBD_MSC_DeInit, + USBD_MSC_Setup, + NULL, /*EP0_TxSent*/ + NULL, /*EP0_RxReady*/ + USBD_MSC_DataIn, + USBD_MSC_DataOut, + NULL, /*SOF */ + NULL, + NULL, + USBD_MSC_GetHSCfgDesc, + USBD_MSC_GetFSCfgDesc, + USBD_MSC_GetOtherSpeedCfgDesc, + USBD_MSC_GetDeviceQualifierDescriptor, +}; + +/* USB Mass storage device Configuration Descriptor */ +/* All Descriptors (Configuration, Interface, Endpoint, Class, Vendor */ +__ALIGN_BEGIN uint8_t USBD_MSC_CfgHSDesc[USB_MSC_CONFIG_DESC_SIZ] __ALIGN_END = +{ + + 0x09, /* bLength: Configuation Descriptor size */ + USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ + USB_MSC_CONFIG_DESC_SIZ, + + 0x00, + 0x01, /* bNumInterfaces: 1 interface */ + 0x01, /* bConfigurationValue: */ + 0x04, /* iConfiguration: */ + 0xC0, /* bmAttributes: */ + 0x32, /* MaxPower 100 mA */ + + /******************** Mass Storage interface ********************/ + 0x09, /* bLength: Interface Descriptor size */ + 0x04, /* bDescriptorType: */ + 0x00, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x02, /* bNumEndpoints*/ + 0x08, /* bInterfaceClass: MSC Class */ + 0x06, /* bInterfaceSubClass : SCSI transparent*/ + 0x50, /* nInterfaceProtocol */ + 0x05, /* iInterface: */ + /******************** Mass Storage Endpoints ********************/ + 0x07, /*Endpoint descriptor length = 7*/ + 0x05, /*Endpoint descriptor type */ + MSC_EPIN_ADDR, /*Endpoint address (IN, address 1) */ + 0x02, /*Bulk endpoint type */ + LOBYTE(MSC_MAX_HS_PACKET), + HIBYTE(MSC_MAX_HS_PACKET), + 0x00, /*Polling interval in milliseconds */ + + 0x07, /*Endpoint descriptor length = 7 */ + 0x05, /*Endpoint descriptor type */ + MSC_EPOUT_ADDR, /*Endpoint address (OUT, address 1) */ + 0x02, /*Bulk endpoint type */ + LOBYTE(MSC_MAX_HS_PACKET), + HIBYTE(MSC_MAX_HS_PACKET), + 0x00 /*Polling interval in milliseconds*/ +}; + +/* USB Mass storage device Configuration Descriptor */ +/* All Descriptors (Configuration, Interface, Endpoint, Class, Vendor */ +uint8_t USBD_MSC_CfgFSDesc[USB_MSC_CONFIG_DESC_SIZ] __ALIGN_END = +{ + + 0x09, /* bLength: Configuation Descriptor size */ + USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ + USB_MSC_CONFIG_DESC_SIZ, + + 0x00, + 0x01, /* bNumInterfaces: 1 interface */ + 0x01, /* bConfigurationValue: */ + 0x04, /* iConfiguration: */ + 0xC0, /* bmAttributes: */ + 0x32, /* MaxPower 100 mA */ + + /******************** Mass Storage interface ********************/ + 0x09, /* bLength: Interface Descriptor size */ + 0x04, /* bDescriptorType: */ + 0x00, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x02, /* bNumEndpoints*/ + 0x08, /* bInterfaceClass: MSC Class */ + 0x06, /* bInterfaceSubClass : SCSI transparent*/ + 0x50, /* nInterfaceProtocol */ + 0x05, /* iInterface: */ + /******************** Mass Storage Endpoints ********************/ + 0x07, /*Endpoint descriptor length = 7*/ + 0x05, /*Endpoint descriptor type */ + MSC_EPIN_ADDR, /*Endpoint address (IN, address 1) */ + 0x02, /*Bulk endpoint type */ + LOBYTE(MSC_MAX_FS_PACKET), + HIBYTE(MSC_MAX_FS_PACKET), + 0x00, /*Polling interval in milliseconds */ + + 0x07, /*Endpoint descriptor length = 7 */ + 0x05, /*Endpoint descriptor type */ + MSC_EPOUT_ADDR, /*Endpoint address (OUT, address 1) */ + 0x02, /*Bulk endpoint type */ + LOBYTE(MSC_MAX_FS_PACKET), + HIBYTE(MSC_MAX_FS_PACKET), + 0x00 /*Polling interval in milliseconds*/ +}; + +__ALIGN_BEGIN uint8_t USBD_MSC_OtherSpeedCfgDesc[USB_MSC_CONFIG_DESC_SIZ] __ALIGN_END = +{ + + 0x09, /* bLength: Configuation Descriptor size */ + USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION, + USB_MSC_CONFIG_DESC_SIZ, + + 0x00, + 0x01, /* bNumInterfaces: 1 interface */ + 0x01, /* bConfigurationValue: */ + 0x04, /* iConfiguration: */ + 0xC0, /* bmAttributes: */ + 0x32, /* MaxPower 100 mA */ + + /******************** Mass Storage interface ********************/ + 0x09, /* bLength: Interface Descriptor size */ + 0x04, /* bDescriptorType: */ + 0x00, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x02, /* bNumEndpoints*/ + 0x08, /* bInterfaceClass: MSC Class */ + 0x06, /* bInterfaceSubClass : SCSI transparent command set*/ + 0x50, /* nInterfaceProtocol */ + 0x05, /* iInterface: */ + /******************** Mass Storage Endpoints ********************/ + 0x07, /*Endpoint descriptor length = 7*/ + 0x05, /*Endpoint descriptor type */ + MSC_EPIN_ADDR, /*Endpoint address (IN, address 1) */ + 0x02, /*Bulk endpoint type */ + 0x40, + 0x00, + 0x00, /*Polling interval in milliseconds */ + + 0x07, /*Endpoint descriptor length = 7 */ + 0x05, /*Endpoint descriptor type */ + MSC_EPOUT_ADDR, /*Endpoint address (OUT, address 1) */ + 0x02, /*Bulk endpoint type */ + 0x40, + 0x00, + 0x00 /*Polling interval in milliseconds*/ +}; + +/* USB Standard Device Descriptor */ +__ALIGN_BEGIN uint8_t USBD_MSC_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] __ALIGN_END = +{ + USB_LEN_DEV_QUALIFIER_DESC, + USB_DESC_TYPE_DEVICE_QUALIFIER, + 0x00, + 0x02, + 0x00, + 0x00, + 0x00, + MSC_MAX_FS_PACKET, + 0x01, + 0x00, +}; +/** + * @} + */ + + +/** @defgroup MSC_CORE_Private_Functions + * @{ + */ + +/** + * @brief USBD_MSC_Init + * Initialize the mass storage configuration + * @param pdev: device instance + * @param cfgidx: configuration index + * @retval status + */ +uint8_t USBD_MSC_Init (USBD_HandleTypeDef *pdev, uint8_t cfgidx) +{ + if(pdev->dev_speed == USBD_SPEED_HIGH) + { + /* Open EP OUT */ + USBD_LL_OpenEP(pdev, MSC_EPOUT_ADDR, USBD_EP_TYPE_BULK, MSC_MAX_HS_PACKET); + pdev->ep_out[MSC_EPOUT_ADDR & 0xFU].is_used = 1U; + + /* Open EP IN */ + USBD_LL_OpenEP(pdev, MSC_EPIN_ADDR, USBD_EP_TYPE_BULK, MSC_MAX_HS_PACKET); + pdev->ep_in[MSC_EPIN_ADDR & 0xFU].is_used = 1U; + } + else + { + /* Open EP OUT */ + USBD_LL_OpenEP(pdev, MSC_EPOUT_ADDR, USBD_EP_TYPE_BULK, MSC_MAX_FS_PACKET); + pdev->ep_out[MSC_EPOUT_ADDR & 0xFU].is_used = 1U; + + /* Open EP IN */ + USBD_LL_OpenEP(pdev, MSC_EPIN_ADDR, USBD_EP_TYPE_BULK, MSC_MAX_FS_PACKET); + pdev->ep_in[MSC_EPIN_ADDR & 0xFU].is_used = 1U; + } + pdev->pClassData = USBD_malloc(sizeof (USBD_MSC_BOT_HandleTypeDef)); + + if(pdev->pClassData == NULL) + { + return USBD_FAIL; + } + + /* Init the BOT layer */ + MSC_BOT_Init(pdev); + + return USBD_OK; +} + +/** + * @brief USBD_MSC_DeInit + * DeInitilaize the mass storage configuration + * @param pdev: device instance + * @param cfgidx: configuration index + * @retval status + */ +uint8_t USBD_MSC_DeInit (USBD_HandleTypeDef *pdev, + uint8_t cfgidx) +{ + /* Close MSC EPs */ + USBD_LL_CloseEP(pdev, MSC_EPOUT_ADDR); + pdev->ep_out[MSC_EPOUT_ADDR & 0xFU].is_used = 0U; + + /* Close EP IN */ + USBD_LL_CloseEP(pdev, MSC_EPIN_ADDR); + pdev->ep_in[MSC_EPIN_ADDR & 0xFU].is_used = 0U; + + /* De-Init the BOT layer */ + MSC_BOT_DeInit(pdev); + + /* Free MSC Class Resources */ + if(pdev->pClassData != NULL) + { + USBD_free(pdev->pClassData); + pdev->pClassData = NULL; + } + return USBD_OK; +} +/** +* @brief USBD_MSC_Setup +* Handle the MSC specific requests +* @param pdev: device instance +* @param req: USB request +* @retval status +*/ +uint8_t USBD_MSC_Setup (USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req) +{ + USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*) pdev->pClassData; + uint8_t ret = USBD_OK; + uint16_t status_info = 0U; + + switch (req->bmRequest & USB_REQ_TYPE_MASK) + { + + /* Class request */ + case USB_REQ_TYPE_CLASS: + switch (req->bRequest) + { + case BOT_GET_MAX_LUN: + if((req->wValue == 0U) && (req->wLength == 1U) && + ((req->bmRequest & 0x80U) == 0x80U)) + { + hmsc->max_lun = (uint32_t)((USBD_StorageTypeDef *)pdev->pUserData)->GetMaxLun(); + USBD_CtlSendData (pdev, (uint8_t *)(void *)&hmsc->max_lun, 1U); + } + else + { + USBD_CtlError(pdev, req); + ret = USBD_FAIL; + } + break; + + case BOT_RESET : + if((req->wValue == 0U) && (req->wLength == 0U) && + ((req->bmRequest & 0x80U) != 0x80U)) + { + MSC_BOT_Reset(pdev); + } + else + { + USBD_CtlError(pdev , req); + ret = USBD_FAIL; + } + break; + + default: + USBD_CtlError(pdev , req); + ret = USBD_FAIL; + break; + } + break; + /* Interface & Endpoint request */ + case USB_REQ_TYPE_STANDARD: + switch (req->bRequest) + { + case USB_REQ_GET_STATUS: + if (pdev->dev_state == USBD_STATE_CONFIGURED) + { + USBD_CtlSendData (pdev, (uint8_t *)(void *)&status_info, 2U); + } + else + { + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + } + break; + + case USB_REQ_GET_INTERFACE: + if (pdev->dev_state == USBD_STATE_CONFIGURED) + { + USBD_CtlSendData (pdev, (uint8_t *)(void *)&hmsc->interface, 1U); + } + else + { + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + } + break; + + case USB_REQ_SET_INTERFACE: + if (pdev->dev_state == USBD_STATE_CONFIGURED) + { + hmsc->interface = (uint8_t)(req->wValue); + } + else + { + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + } + break; + + case USB_REQ_CLEAR_FEATURE: + + /* Flush the FIFO and Clear the stall status */ + USBD_LL_FlushEP(pdev, (uint8_t)req->wIndex); + + /* Reactivate the EP */ + USBD_LL_CloseEP (pdev , (uint8_t)req->wIndex); + if((((uint8_t)req->wIndex) & 0x80U) == 0x80U) + { + pdev->ep_in[(uint8_t)req->wIndex & 0xFU].is_used = 0U; + if(pdev->dev_speed == USBD_SPEED_HIGH) + { + /* Open EP IN */ + USBD_LL_OpenEP(pdev, MSC_EPIN_ADDR, USBD_EP_TYPE_BULK, + MSC_MAX_HS_PACKET); + } + else + { + /* Open EP IN */ + USBD_LL_OpenEP(pdev, MSC_EPIN_ADDR, USBD_EP_TYPE_BULK, + MSC_MAX_FS_PACKET); + } + pdev->ep_in[MSC_EPIN_ADDR & 0xFU].is_used = 1U; + } + else + { + pdev->ep_out[(uint8_t)req->wIndex & 0xFU].is_used = 0U; + if(pdev->dev_speed == USBD_SPEED_HIGH) + { + /* Open EP OUT */ + USBD_LL_OpenEP(pdev, MSC_EPOUT_ADDR, USBD_EP_TYPE_BULK, + MSC_MAX_HS_PACKET); + } + else + { + /* Open EP OUT */ + USBD_LL_OpenEP(pdev, MSC_EPOUT_ADDR, USBD_EP_TYPE_BULK, + MSC_MAX_FS_PACKET); + } + pdev->ep_out[MSC_EPOUT_ADDR & 0xFU].is_used = 1U; + } + + /* Handle BOT error */ + MSC_BOT_CplClrFeature(pdev, (uint8_t)req->wIndex); + break; + + default: + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + break; + } + break; + + default: + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + break; + } + + return ret; +} + +/** +* @brief USBD_MSC_DataIn +* handle data IN Stage +* @param pdev: device instance +* @param epnum: endpoint index +* @retval status +*/ +uint8_t USBD_MSC_DataIn (USBD_HandleTypeDef *pdev, + uint8_t epnum) +{ + MSC_BOT_DataIn(pdev , epnum); + return USBD_OK; +} + +/** +* @brief USBD_MSC_DataOut +* handle data OUT Stage +* @param pdev: device instance +* @param epnum: endpoint index +* @retval status +*/ +uint8_t USBD_MSC_DataOut (USBD_HandleTypeDef *pdev, + uint8_t epnum) +{ + MSC_BOT_DataOut(pdev , epnum); + return USBD_OK; +} + +/** +* @brief USBD_MSC_GetHSCfgDesc +* return configuration descriptor +* @param length : pointer data length +* @retval pointer to descriptor buffer +*/ +uint8_t *USBD_MSC_GetHSCfgDesc (uint16_t *length) +{ + *length = sizeof (USBD_MSC_CfgHSDesc); + return USBD_MSC_CfgHSDesc; +} + +/** +* @brief USBD_MSC_GetFSCfgDesc +* return configuration descriptor +* @param length : pointer data length +* @retval pointer to descriptor buffer +*/ +uint8_t *USBD_MSC_GetFSCfgDesc (uint16_t *length) +{ + *length = sizeof (USBD_MSC_CfgFSDesc); + return USBD_MSC_CfgFSDesc; +} + +/** +* @brief USBD_MSC_GetOtherSpeedCfgDesc +* return other speed configuration descriptor +* @param length : pointer data length +* @retval pointer to descriptor buffer +*/ +uint8_t *USBD_MSC_GetOtherSpeedCfgDesc (uint16_t *length) +{ + *length = sizeof (USBD_MSC_OtherSpeedCfgDesc); + return USBD_MSC_OtherSpeedCfgDesc; +} +/** +* @brief DeviceQualifierDescriptor +* return Device Qualifier descriptor +* @param length : pointer data length +* @retval pointer to descriptor buffer +*/ +uint8_t *USBD_MSC_GetDeviceQualifierDescriptor (uint16_t *length) +{ + *length = sizeof (USBD_MSC_DeviceQualifierDesc); + return USBD_MSC_DeviceQualifierDesc; +} + +/** +* @brief USBD_MSC_RegisterStorage +* @param fops: storage callback +* @retval status +*/ +uint8_t USBD_MSC_RegisterStorage (USBD_HandleTypeDef *pdev, + USBD_StorageTypeDef *fops) +{ + if(fops != NULL) + { + pdev->pUserData = fops; + } + return USBD_OK; +} + +/** + * @} + */ + + +/** + * @} + */ + + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_bot.c b/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_bot.c index aed9e8e5a1..dc4383b96a 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_bot.c +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_bot.c @@ -1,394 +1,394 @@ -/** - ****************************************************************************** - * @file usbd_msc_bot.c - * @author MCD Application Team - * @brief This file provides all the BOT protocol core functions. - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - - /* BSPDependencies - - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" - - "stm32xxxxx_{eval}{discovery}_io.c" - - "stm32xxxxx_{eval}{discovery}{adafruit}_sd.c" - EndBSPDependencies */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_msc_bot.h" -#include "usbd_msc.h" -#include "usbd_msc_scsi.h" -#include "usbd_ioreq.h" - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - - -/** @defgroup MSC_BOT - * @brief BOT protocol module - * @{ - */ - -/** @defgroup MSC_BOT_Private_TypesDefinitions - * @{ - */ -/** - * @} - */ - - -/** @defgroup MSC_BOT_Private_Defines - * @{ - */ - -/** - * @} - */ - - -/** @defgroup MSC_BOT_Private_Macros - * @{ - */ -/** - * @} - */ - - -/** @defgroup MSC_BOT_Private_Variables - * @{ - */ - -/** - * @} - */ - - -/** @defgroup MSC_BOT_Private_FunctionPrototypes - * @{ - */ -static void MSC_BOT_CBW_Decode (USBD_HandleTypeDef *pdev); - -static void MSC_BOT_SendData (USBD_HandleTypeDef *pdev, uint8_t* pbuf, - uint16_t len); - -static void MSC_BOT_Abort(USBD_HandleTypeDef *pdev); -/** - * @} - */ - - -/** @defgroup MSC_BOT_Private_Functions - * @{ - */ - - - -/** -* @brief MSC_BOT_Init -* Initialize the BOT Process -* @param pdev: device instance -* @retval None -*/ -void MSC_BOT_Init (USBD_HandleTypeDef *pdev) -{ - USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; - - hmsc->bot_state = USBD_BOT_IDLE; - hmsc->bot_status = USBD_BOT_STATUS_NORMAL; - - hmsc->scsi_sense_tail = 0U; - hmsc->scsi_sense_head = 0U; - - ((USBD_StorageTypeDef *)pdev->pUserData)->Init(0U); - - USBD_LL_FlushEP(pdev, MSC_EPOUT_ADDR); - USBD_LL_FlushEP(pdev, MSC_EPIN_ADDR); - - /* Prapare EP to Receive First BOT Cmd */ - USBD_LL_PrepareReceive (pdev, MSC_EPOUT_ADDR, (uint8_t *)(void *)&hmsc->cbw, - USBD_BOT_CBW_LENGTH); -} - -/** -* @brief MSC_BOT_Reset -* Reset the BOT Machine -* @param pdev: device instance -* @retval None -*/ -void MSC_BOT_Reset (USBD_HandleTypeDef *pdev) -{ - USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; - - hmsc->bot_state = USBD_BOT_IDLE; - hmsc->bot_status = USBD_BOT_STATUS_RECOVERY; - - /* Prapare EP to Receive First BOT Cmd */ - USBD_LL_PrepareReceive (pdev, MSC_EPOUT_ADDR, (uint8_t *)(void *)&hmsc->cbw, - USBD_BOT_CBW_LENGTH); -} - -/** -* @brief MSC_BOT_DeInit -* Deinitialize the BOT Machine -* @param pdev: device instance -* @retval None -*/ -void MSC_BOT_DeInit (USBD_HandleTypeDef *pdev) -{ - USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; - hmsc->bot_state = USBD_BOT_IDLE; -} - -/** -* @brief MSC_BOT_DataIn -* Handle BOT IN data stage -* @param pdev: device instance -* @param epnum: endpoint index -* @retval None -*/ -void MSC_BOT_DataIn (USBD_HandleTypeDef *pdev, - uint8_t epnum) -{ - USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; - - switch (hmsc->bot_state) - { - case USBD_BOT_DATA_IN: - if(SCSI_ProcessCmd(pdev, - hmsc->cbw.bLUN, - &hmsc->cbw.CB[0]) < 0) - { - MSC_BOT_SendCSW (pdev, USBD_CSW_CMD_FAILED); - } - break; - - case USBD_BOT_SEND_DATA: - case USBD_BOT_LAST_DATA_IN: - MSC_BOT_SendCSW (pdev, USBD_CSW_CMD_PASSED); - - break; - - default: - break; - } -} -/** -* @brief MSC_BOT_DataOut -* Process MSC OUT data -* @param pdev: device instance -* @param epnum: endpoint index -* @retval None -*/ -void MSC_BOT_DataOut (USBD_HandleTypeDef *pdev, - uint8_t epnum) -{ - USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; - - switch (hmsc->bot_state) - { - case USBD_BOT_IDLE: - MSC_BOT_CBW_Decode(pdev); - break; - - case USBD_BOT_DATA_OUT: - - if(SCSI_ProcessCmd(pdev, - hmsc->cbw.bLUN, - &hmsc->cbw.CB[0]) < 0) - { - MSC_BOT_SendCSW (pdev, USBD_CSW_CMD_FAILED); - } - - break; - - default: - break; - } -} - -/** -* @brief MSC_BOT_CBW_Decode -* Decode the CBW command and set the BOT state machine accordingly -* @param pdev: device instance -* @retval None -*/ -static void MSC_BOT_CBW_Decode (USBD_HandleTypeDef *pdev) -{ - USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; - - hmsc->csw.dTag = hmsc->cbw.dTag; - hmsc->csw.dDataResidue = hmsc->cbw.dDataLength; - - if ((USBD_LL_GetRxDataSize (pdev ,MSC_EPOUT_ADDR) != USBD_BOT_CBW_LENGTH) || - (hmsc->cbw.dSignature != USBD_BOT_CBW_SIGNATURE) || - (hmsc->cbw.bLUN > 1U) || - (hmsc->cbw.bCBLength < 1U) || (hmsc->cbw.bCBLength > 16U)) - { - - SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST, INVALID_CDB); - - hmsc->bot_status = USBD_BOT_STATUS_ERROR; - MSC_BOT_Abort(pdev); - } - else - { - if(SCSI_ProcessCmd(pdev, hmsc->cbw.bLUN, &hmsc->cbw.CB[0]) < 0) - { - if(hmsc->bot_state == USBD_BOT_NO_DATA) - { - MSC_BOT_SendCSW (pdev, USBD_CSW_CMD_FAILED); - } - else - { - MSC_BOT_Abort(pdev); - } - } - /*Burst xfer handled internally*/ - else if ((hmsc->bot_state != USBD_BOT_DATA_IN) && - (hmsc->bot_state != USBD_BOT_DATA_OUT) && - (hmsc->bot_state != USBD_BOT_LAST_DATA_IN)) - { - if (hmsc->bot_data_length > 0U) - { - MSC_BOT_SendData(pdev, hmsc->bot_data, hmsc->bot_data_length); - } - else if (hmsc->bot_data_length == 0U) - { - MSC_BOT_SendCSW (pdev, USBD_CSW_CMD_PASSED); - } - else - { - MSC_BOT_Abort(pdev); - } - } - else - { - return; - } - } -} - -/** -* @brief MSC_BOT_SendData -* Send the requested data -* @param pdev: device instance -* @param buf: pointer to data buffer -* @param len: Data Length -* @retval None -*/ -static void MSC_BOT_SendData(USBD_HandleTypeDef *pdev, uint8_t* pbuf, - uint16_t len) -{ - USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; - - uint16_t length = (uint16_t)MIN(hmsc->cbw.dDataLength, len); - - hmsc->csw.dDataResidue -= len; - hmsc->csw.bStatus = USBD_CSW_CMD_PASSED; - hmsc->bot_state = USBD_BOT_SEND_DATA; - - USBD_LL_Transmit(pdev, MSC_EPIN_ADDR, pbuf, length); -} - -/** -* @brief MSC_BOT_SendCSW -* Send the Command Status Wrapper -* @param pdev: device instance -* @param status : CSW status -* @retval None -*/ -void MSC_BOT_SendCSW (USBD_HandleTypeDef *pdev, - uint8_t CSW_Status) -{ - USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; - - hmsc->csw.dSignature = USBD_BOT_CSW_SIGNATURE; - hmsc->csw.bStatus = CSW_Status; - hmsc->bot_state = USBD_BOT_IDLE; - - USBD_LL_Transmit (pdev, MSC_EPIN_ADDR, (uint8_t *)(void *)&hmsc->csw, - USBD_BOT_CSW_LENGTH); - - /* Prepare EP to Receive next Cmd */ - USBD_LL_PrepareReceive (pdev, MSC_EPOUT_ADDR, (uint8_t *)(void *)&hmsc->cbw, - USBD_BOT_CBW_LENGTH); -} - -/** -* @brief MSC_BOT_Abort -* Abort the current transfer -* @param pdev: device instance -* @retval status -*/ - -static void MSC_BOT_Abort (USBD_HandleTypeDef *pdev) -{ - USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; - - if ((hmsc->cbw.bmFlags == 0U) && - (hmsc->cbw.dDataLength != 0U) && - (hmsc->bot_status == USBD_BOT_STATUS_NORMAL)) - { - USBD_LL_StallEP(pdev, MSC_EPOUT_ADDR ); - } - - USBD_LL_StallEP(pdev, MSC_EPIN_ADDR); - - if(hmsc->bot_status == USBD_BOT_STATUS_ERROR) - { - USBD_LL_PrepareReceive (pdev, MSC_EPOUT_ADDR, (uint8_t *)(void *)&hmsc->cbw, - USBD_BOT_CBW_LENGTH); - } -} - -/** -* @brief MSC_BOT_CplClrFeature -* Complete the clear feature request -* @param pdev: device instance -* @param epnum: endpoint index -* @retval None -*/ - -void MSC_BOT_CplClrFeature (USBD_HandleTypeDef *pdev, uint8_t epnum) -{ - USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; - - if(hmsc->bot_status == USBD_BOT_STATUS_ERROR)/* Bad CBW Signature */ - { - USBD_LL_StallEP(pdev, MSC_EPIN_ADDR); - hmsc->bot_status = USBD_BOT_STATUS_NORMAL; - } - else if(((epnum & 0x80U) == 0x80U) && (hmsc->bot_status != USBD_BOT_STATUS_RECOVERY)) - { - MSC_BOT_SendCSW (pdev, USBD_CSW_CMD_FAILED); - } - else - { - return; - } -} -/** - * @} - */ - - -/** - * @} - */ - - -/** - * @} - */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_msc_bot.c + * @author MCD Application Team + * @brief This file provides all the BOT protocol core functions. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + + /* BSPDependencies + - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" + - "stm32xxxxx_{eval}{discovery}_io.c" + - "stm32xxxxx_{eval}{discovery}{adafruit}_sd.c" + EndBSPDependencies */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_msc_bot.h" +#include "usbd_msc.h" +#include "usbd_msc_scsi.h" +#include "usbd_ioreq.h" + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + + +/** @defgroup MSC_BOT + * @brief BOT protocol module + * @{ + */ + +/** @defgroup MSC_BOT_Private_TypesDefinitions + * @{ + */ +/** + * @} + */ + + +/** @defgroup MSC_BOT_Private_Defines + * @{ + */ + +/** + * @} + */ + + +/** @defgroup MSC_BOT_Private_Macros + * @{ + */ +/** + * @} + */ + + +/** @defgroup MSC_BOT_Private_Variables + * @{ + */ + +/** + * @} + */ + + +/** @defgroup MSC_BOT_Private_FunctionPrototypes + * @{ + */ +static void MSC_BOT_CBW_Decode (USBD_HandleTypeDef *pdev); + +static void MSC_BOT_SendData (USBD_HandleTypeDef *pdev, uint8_t* pbuf, + uint16_t len); + +static void MSC_BOT_Abort(USBD_HandleTypeDef *pdev); +/** + * @} + */ + + +/** @defgroup MSC_BOT_Private_Functions + * @{ + */ + + + +/** +* @brief MSC_BOT_Init +* Initialize the BOT Process +* @param pdev: device instance +* @retval None +*/ +void MSC_BOT_Init (USBD_HandleTypeDef *pdev) +{ + USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; + + hmsc->bot_state = USBD_BOT_IDLE; + hmsc->bot_status = USBD_BOT_STATUS_NORMAL; + + hmsc->scsi_sense_tail = 0U; + hmsc->scsi_sense_head = 0U; + + ((USBD_StorageTypeDef *)pdev->pUserData)->Init(0U); + + USBD_LL_FlushEP(pdev, MSC_EPOUT_ADDR); + USBD_LL_FlushEP(pdev, MSC_EPIN_ADDR); + + /* Prapare EP to Receive First BOT Cmd */ + USBD_LL_PrepareReceive (pdev, MSC_EPOUT_ADDR, (uint8_t *)(void *)&hmsc->cbw, + USBD_BOT_CBW_LENGTH); +} + +/** +* @brief MSC_BOT_Reset +* Reset the BOT Machine +* @param pdev: device instance +* @retval None +*/ +void MSC_BOT_Reset (USBD_HandleTypeDef *pdev) +{ + USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; + + hmsc->bot_state = USBD_BOT_IDLE; + hmsc->bot_status = USBD_BOT_STATUS_RECOVERY; + + /* Prapare EP to Receive First BOT Cmd */ + USBD_LL_PrepareReceive (pdev, MSC_EPOUT_ADDR, (uint8_t *)(void *)&hmsc->cbw, + USBD_BOT_CBW_LENGTH); +} + +/** +* @brief MSC_BOT_DeInit +* Deinitialize the BOT Machine +* @param pdev: device instance +* @retval None +*/ +void MSC_BOT_DeInit (USBD_HandleTypeDef *pdev) +{ + USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; + hmsc->bot_state = USBD_BOT_IDLE; +} + +/** +* @brief MSC_BOT_DataIn +* Handle BOT IN data stage +* @param pdev: device instance +* @param epnum: endpoint index +* @retval None +*/ +void MSC_BOT_DataIn (USBD_HandleTypeDef *pdev, + uint8_t epnum) +{ + USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; + + switch (hmsc->bot_state) + { + case USBD_BOT_DATA_IN: + if(SCSI_ProcessCmd(pdev, + hmsc->cbw.bLUN, + &hmsc->cbw.CB[0]) < 0) + { + MSC_BOT_SendCSW (pdev, USBD_CSW_CMD_FAILED); + } + break; + + case USBD_BOT_SEND_DATA: + case USBD_BOT_LAST_DATA_IN: + MSC_BOT_SendCSW (pdev, USBD_CSW_CMD_PASSED); + + break; + + default: + break; + } +} +/** +* @brief MSC_BOT_DataOut +* Process MSC OUT data +* @param pdev: device instance +* @param epnum: endpoint index +* @retval None +*/ +void MSC_BOT_DataOut (USBD_HandleTypeDef *pdev, + uint8_t epnum) +{ + USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; + + switch (hmsc->bot_state) + { + case USBD_BOT_IDLE: + MSC_BOT_CBW_Decode(pdev); + break; + + case USBD_BOT_DATA_OUT: + + if(SCSI_ProcessCmd(pdev, + hmsc->cbw.bLUN, + &hmsc->cbw.CB[0]) < 0) + { + MSC_BOT_SendCSW (pdev, USBD_CSW_CMD_FAILED); + } + + break; + + default: + break; + } +} + +/** +* @brief MSC_BOT_CBW_Decode +* Decode the CBW command and set the BOT state machine accordingly +* @param pdev: device instance +* @retval None +*/ +static void MSC_BOT_CBW_Decode (USBD_HandleTypeDef *pdev) +{ + USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; + + hmsc->csw.dTag = hmsc->cbw.dTag; + hmsc->csw.dDataResidue = hmsc->cbw.dDataLength; + + if ((USBD_LL_GetRxDataSize (pdev ,MSC_EPOUT_ADDR) != USBD_BOT_CBW_LENGTH) || + (hmsc->cbw.dSignature != USBD_BOT_CBW_SIGNATURE) || + (hmsc->cbw.bLUN > 1U) || + (hmsc->cbw.bCBLength < 1U) || (hmsc->cbw.bCBLength > 16U)) + { + + SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST, INVALID_CDB); + + hmsc->bot_status = USBD_BOT_STATUS_ERROR; + MSC_BOT_Abort(pdev); + } + else + { + if(SCSI_ProcessCmd(pdev, hmsc->cbw.bLUN, &hmsc->cbw.CB[0]) < 0) + { + if(hmsc->bot_state == USBD_BOT_NO_DATA) + { + MSC_BOT_SendCSW (pdev, USBD_CSW_CMD_FAILED); + } + else + { + MSC_BOT_Abort(pdev); + } + } + /*Burst xfer handled internally*/ + else if ((hmsc->bot_state != USBD_BOT_DATA_IN) && + (hmsc->bot_state != USBD_BOT_DATA_OUT) && + (hmsc->bot_state != USBD_BOT_LAST_DATA_IN)) + { + if (hmsc->bot_data_length > 0U) + { + MSC_BOT_SendData(pdev, hmsc->bot_data, hmsc->bot_data_length); + } + else if (hmsc->bot_data_length == 0U) + { + MSC_BOT_SendCSW (pdev, USBD_CSW_CMD_PASSED); + } + else + { + MSC_BOT_Abort(pdev); + } + } + else + { + return; + } + } +} + +/** +* @brief MSC_BOT_SendData +* Send the requested data +* @param pdev: device instance +* @param buf: pointer to data buffer +* @param len: Data Length +* @retval None +*/ +static void MSC_BOT_SendData(USBD_HandleTypeDef *pdev, uint8_t* pbuf, + uint16_t len) +{ + USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; + + uint16_t length = (uint16_t)MIN(hmsc->cbw.dDataLength, len); + + hmsc->csw.dDataResidue -= len; + hmsc->csw.bStatus = USBD_CSW_CMD_PASSED; + hmsc->bot_state = USBD_BOT_SEND_DATA; + + USBD_LL_Transmit(pdev, MSC_EPIN_ADDR, pbuf, length); +} + +/** +* @brief MSC_BOT_SendCSW +* Send the Command Status Wrapper +* @param pdev: device instance +* @param status : CSW status +* @retval None +*/ +void MSC_BOT_SendCSW (USBD_HandleTypeDef *pdev, + uint8_t CSW_Status) +{ + USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; + + hmsc->csw.dSignature = USBD_BOT_CSW_SIGNATURE; + hmsc->csw.bStatus = CSW_Status; + hmsc->bot_state = USBD_BOT_IDLE; + + USBD_LL_Transmit (pdev, MSC_EPIN_ADDR, (uint8_t *)(void *)&hmsc->csw, + USBD_BOT_CSW_LENGTH); + + /* Prepare EP to Receive next Cmd */ + USBD_LL_PrepareReceive (pdev, MSC_EPOUT_ADDR, (uint8_t *)(void *)&hmsc->cbw, + USBD_BOT_CBW_LENGTH); +} + +/** +* @brief MSC_BOT_Abort +* Abort the current transfer +* @param pdev: device instance +* @retval status +*/ + +static void MSC_BOT_Abort (USBD_HandleTypeDef *pdev) +{ + USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; + + if ((hmsc->cbw.bmFlags == 0U) && + (hmsc->cbw.dDataLength != 0U) && + (hmsc->bot_status == USBD_BOT_STATUS_NORMAL)) + { + USBD_LL_StallEP(pdev, MSC_EPOUT_ADDR ); + } + + USBD_LL_StallEP(pdev, MSC_EPIN_ADDR); + + if(hmsc->bot_status == USBD_BOT_STATUS_ERROR) + { + USBD_LL_PrepareReceive (pdev, MSC_EPOUT_ADDR, (uint8_t *)(void *)&hmsc->cbw, + USBD_BOT_CBW_LENGTH); + } +} + +/** +* @brief MSC_BOT_CplClrFeature +* Complete the clear feature request +* @param pdev: device instance +* @param epnum: endpoint index +* @retval None +*/ + +void MSC_BOT_CplClrFeature (USBD_HandleTypeDef *pdev, uint8_t epnum) +{ + USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; + + if(hmsc->bot_status == USBD_BOT_STATUS_ERROR)/* Bad CBW Signature */ + { + USBD_LL_StallEP(pdev, MSC_EPIN_ADDR); + hmsc->bot_status = USBD_BOT_STATUS_NORMAL; + } + else if(((epnum & 0x80U) == 0x80U) && (hmsc->bot_status != USBD_BOT_STATUS_RECOVERY)) + { + MSC_BOT_SendCSW (pdev, USBD_CSW_CMD_FAILED); + } + else + { + return; + } +} +/** + * @} + */ + + +/** + * @} + */ + + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_data.c b/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_data.c index bd3937e4b1..fcee700d02 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_data.c +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_data.c @@ -1,132 +1,132 @@ -/** - ****************************************************************************** - * @file usbd_msc_data.c - * @author MCD Application Team - * @brief This file provides all the vital inquiry pages and sense data. - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - - /* BSPDependencies - - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" - - "stm32xxxxx_{eval}{discovery}_io.c" - - "stm32xxxxx_{eval}{discovery}{adafruit}_sd.c" - EndBSPDependencies */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_msc_data.h" - - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - - -/** @defgroup MSC_DATA - * @brief Mass storage info/data module - * @{ - */ - -/** @defgroup MSC_DATA_Private_TypesDefinitions - * @{ - */ -/** - * @} - */ - - -/** @defgroup MSC_DATA_Private_Defines - * @{ - */ -/** - * @} - */ - - -/** @defgroup MSC_DATA_Private_Macros - * @{ - */ -/** - * @} - */ - - -/** @defgroup MSC_DATA_Private_Variables - * @{ - */ - - -/* USB Mass storage Page 0 Inquiry Data */ -const uint8_t MSC_Page00_Inquiry_Data[] = { - 0x00, - 0x00, - 0x00, - (LENGTH_INQUIRY_PAGE00 - 4U), - 0x00, - 0x80, - 0x83 -}; -/* USB Mass storage sense 6 Data */ -const uint8_t MSC_Mode_Sense6_data[] = { - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00 -}; -/* USB Mass storage sense 10 Data */ -const uint8_t MSC_Mode_Sense10_data[] = { - 0x00, - 0x06, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00 -}; -/** - * @} - */ - - -/** @defgroup MSC_DATA_Private_FunctionPrototypes - * @{ - */ -/** - * @} - */ - - -/** @defgroup MSC_DATA_Private_Functions - * @{ - */ - -/** - * @} - */ - - -/** - * @} - */ - - -/** - * @} - */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_msc_data.c + * @author MCD Application Team + * @brief This file provides all the vital inquiry pages and sense data. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + + /* BSPDependencies + - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" + - "stm32xxxxx_{eval}{discovery}_io.c" + - "stm32xxxxx_{eval}{discovery}{adafruit}_sd.c" + EndBSPDependencies */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_msc_data.h" + + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + + +/** @defgroup MSC_DATA + * @brief Mass storage info/data module + * @{ + */ + +/** @defgroup MSC_DATA_Private_TypesDefinitions + * @{ + */ +/** + * @} + */ + + +/** @defgroup MSC_DATA_Private_Defines + * @{ + */ +/** + * @} + */ + + +/** @defgroup MSC_DATA_Private_Macros + * @{ + */ +/** + * @} + */ + + +/** @defgroup MSC_DATA_Private_Variables + * @{ + */ + + +/* USB Mass storage Page 0 Inquiry Data */ +const uint8_t MSC_Page00_Inquiry_Data[] = { + 0x00, + 0x00, + 0x00, + (LENGTH_INQUIRY_PAGE00 - 4U), + 0x00, + 0x80, + 0x83 +}; +/* USB Mass storage sense 6 Data */ +const uint8_t MSC_Mode_Sense6_data[] = { + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00 +}; +/* USB Mass storage sense 10 Data */ +const uint8_t MSC_Mode_Sense10_data[] = { + 0x00, + 0x06, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00, + 0x00 +}; +/** + * @} + */ + + +/** @defgroup MSC_DATA_Private_FunctionPrototypes + * @{ + */ +/** + * @} + */ + + +/** @defgroup MSC_DATA_Private_Functions + * @{ + */ + +/** + * @} + */ + + +/** + * @} + */ + + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_scsi.c b/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_scsi.c index 37fe748628..b6f0e8f02b 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_scsi.c +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_scsi.c @@ -1,714 +1,714 @@ -/** - ****************************************************************************** - * @file usbd_msc_scsi.c - * @author MCD Application Team - * @brief This file provides all the USBD SCSI layer functions. - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - - /* BSPDependencies - - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" - - "stm32xxxxx_{eval}{discovery}_io.c" - - "stm32xxxxx_{eval}{discovery}{adafruit}_sd.c" - EndBSPDependencies */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_msc_bot.h" -#include "usbd_msc_scsi.h" -#include "usbd_msc.h" -#include "usbd_msc_data.h" - - - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - - -/** @defgroup MSC_SCSI - * @brief Mass storage SCSI layer module - * @{ - */ - -/** @defgroup MSC_SCSI_Private_TypesDefinitions - * @{ - */ -/** - * @} - */ - - -/** @defgroup MSC_SCSI_Private_Defines - * @{ - */ - -/** - * @} - */ - - -/** @defgroup MSC_SCSI_Private_Macros - * @{ - */ -/** - * @} - */ - - -/** @defgroup MSC_SCSI_Private_Variables - * @{ - */ - -/** - * @} - */ - - -/** @defgroup MSC_SCSI_Private_FunctionPrototypes - * @{ - */ -static int8_t SCSI_TestUnitReady(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params); -static int8_t SCSI_Inquiry(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params); -static int8_t SCSI_ReadFormatCapacity(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params); -static int8_t SCSI_ReadCapacity10(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params); -static int8_t SCSI_RequestSense (USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params); -static int8_t SCSI_StartStopUnit(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params); -static int8_t SCSI_ModeSense6 (USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params); -static int8_t SCSI_ModeSense10 (USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params); -static int8_t SCSI_Write10(USBD_HandleTypeDef *pdev, uint8_t lun , uint8_t *params); -static int8_t SCSI_Read10(USBD_HandleTypeDef *pdev, uint8_t lun , uint8_t *params); -static int8_t SCSI_Verify10(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params); -static int8_t SCSI_CheckAddressRange (USBD_HandleTypeDef *pdev, uint8_t lun, - uint32_t blk_offset, uint32_t blk_nbr); - -static int8_t SCSI_ProcessRead (USBD_HandleTypeDef *pdev, uint8_t lun); -static int8_t SCSI_ProcessWrite (USBD_HandleTypeDef *pdev, uint8_t lun); -/** - * @} - */ - - -/** @defgroup MSC_SCSI_Private_Functions - * @{ - */ - - -/** -* @brief SCSI_ProcessCmd -* Process SCSI commands -* @param pdev: device instance -* @param lun: Logical unit number -* @param params: Command parameters -* @retval status -*/ -int8_t SCSI_ProcessCmd(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *cmd) -{ - switch (cmd[0]) - { - case SCSI_TEST_UNIT_READY: - SCSI_TestUnitReady(pdev, lun, cmd); - break; - - case SCSI_REQUEST_SENSE: - SCSI_RequestSense (pdev, lun, cmd); - break; - case SCSI_INQUIRY: - SCSI_Inquiry(pdev, lun, cmd); - break; - - case SCSI_START_STOP_UNIT: - SCSI_StartStopUnit(pdev, lun, cmd); - break; - - case SCSI_ALLOW_MEDIUM_REMOVAL: - SCSI_StartStopUnit(pdev, lun, cmd); - break; - - case SCSI_MODE_SENSE6: - SCSI_ModeSense6 (pdev, lun, cmd); - break; - - case SCSI_MODE_SENSE10: - SCSI_ModeSense10 (pdev, lun, cmd); - break; - - case SCSI_READ_FORMAT_CAPACITIES: - SCSI_ReadFormatCapacity(pdev, lun, cmd); - break; - - case SCSI_READ_CAPACITY10: - SCSI_ReadCapacity10(pdev, lun, cmd); - break; - - case SCSI_READ10: - SCSI_Read10(pdev, lun, cmd); - break; - - case SCSI_WRITE10: - SCSI_Write10(pdev, lun, cmd); - break; - - case SCSI_VERIFY10: - SCSI_Verify10(pdev, lun, cmd); - break; - - default: - SCSI_SenseCode(pdev, lun, ILLEGAL_REQUEST, INVALID_CDB); - return -1; - } - return 0; -} - - -/** -* @brief SCSI_TestUnitReady -* Process SCSI Test Unit Ready Command -* @param lun: Logical unit number -* @param params: Command parameters -* @retval status -*/ -static int8_t SCSI_TestUnitReady(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) -{ - USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; - - /* case 9 : Hi > D0 */ - if (hmsc->cbw.dDataLength != 0U) - { - SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST, INVALID_CDB); - - return -1; - } - - if(((USBD_StorageTypeDef *)pdev->pUserData)->IsReady(lun) != 0) - { - SCSI_SenseCode(pdev, lun, NOT_READY, MEDIUM_NOT_PRESENT); - hmsc->bot_state = USBD_BOT_NO_DATA; - - return -1; - } - hmsc->bot_data_length = 0U; - - return 0; -} - -/** -* @brief SCSI_Inquiry -* Process Inquiry command -* @param lun: Logical unit number -* @param params: Command parameters -* @retval status -*/ -static int8_t SCSI_Inquiry(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) -{ - uint8_t* pPage; - uint16_t len; - USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; - - if (params[1] & 0x01U)/*Evpd is set*/ - { - len = LENGTH_INQUIRY_PAGE00; - hmsc->bot_data_length = len; - - while (len) - { - len--; - hmsc->bot_data[len] = MSC_Page00_Inquiry_Data[len]; - } - } - else - { - pPage = (uint8_t *)(void *)&((USBD_StorageTypeDef *)pdev->pUserData)->pInquiry[lun * STANDARD_INQUIRY_DATA_LEN]; - len = (uint16_t)pPage[4] + 5U; - - if (params[4] <= len) - { - len = params[4]; - } - hmsc->bot_data_length = len; - - while (len) - { - len--; - hmsc->bot_data[len] = pPage[len]; - } - } - - return 0; -} - -/** -* @brief SCSI_ReadCapacity10 -* Process Read Capacity 10 command -* @param lun: Logical unit number -* @param params: Command parameters -* @retval status -*/ -static int8_t SCSI_ReadCapacity10(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) -{ - USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; - - if(((USBD_StorageTypeDef *)pdev->pUserData)->GetCapacity(lun, &hmsc->scsi_blk_nbr, &hmsc->scsi_blk_size) != 0) - { - SCSI_SenseCode(pdev, lun, NOT_READY, MEDIUM_NOT_PRESENT); - return -1; - } - else - { - - hmsc->bot_data[0] = (uint8_t)((hmsc->scsi_blk_nbr - 1U) >> 24); - hmsc->bot_data[1] = (uint8_t)((hmsc->scsi_blk_nbr - 1U) >> 16); - hmsc->bot_data[2] = (uint8_t)((hmsc->scsi_blk_nbr - 1U) >> 8); - hmsc->bot_data[3] = (uint8_t)(hmsc->scsi_blk_nbr - 1U); - - hmsc->bot_data[4] = (uint8_t)(hmsc->scsi_blk_size >> 24); - hmsc->bot_data[5] = (uint8_t)(hmsc->scsi_blk_size >> 16); - hmsc->bot_data[6] = (uint8_t)(hmsc->scsi_blk_size >> 8); - hmsc->bot_data[7] = (uint8_t)(hmsc->scsi_blk_size); - - hmsc->bot_data_length = 8U; - return 0; - } -} -/** -* @brief SCSI_ReadFormatCapacity -* Process Read Format Capacity command -* @param lun: Logical unit number -* @param params: Command parameters -* @retval status -*/ -static int8_t SCSI_ReadFormatCapacity(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) -{ - USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; - - uint16_t blk_size; - uint32_t blk_nbr; - uint16_t i; - - for(i = 0U; i < 12U ; i++) - { - hmsc->bot_data[i] = 0U; - } - - if(((USBD_StorageTypeDef *)pdev->pUserData)->GetCapacity(lun, &blk_nbr, &blk_size) != 0U) - { - SCSI_SenseCode(pdev, lun, NOT_READY, MEDIUM_NOT_PRESENT); - return -1; - } - else - { - hmsc->bot_data[3] = 0x08U; - hmsc->bot_data[4] = (uint8_t)((blk_nbr - 1U) >> 24); - hmsc->bot_data[5] = (uint8_t)((blk_nbr - 1U) >> 16); - hmsc->bot_data[6] = (uint8_t)((blk_nbr - 1U) >> 8); - hmsc->bot_data[7] = (uint8_t)(blk_nbr - 1U); - - hmsc->bot_data[8] = 0x02U; - hmsc->bot_data[9] = (uint8_t)(blk_size >> 16); - hmsc->bot_data[10] = (uint8_t)(blk_size >> 8); - hmsc->bot_data[11] = (uint8_t)(blk_size); - - hmsc->bot_data_length = 12U; - return 0; - } -} -/** -* @brief SCSI_ModeSense6 -* Process Mode Sense6 command -* @param lun: Logical unit number -* @param params: Command parameters -* @retval status -*/ -static int8_t SCSI_ModeSense6 (USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) -{ - USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; - uint16_t len = 8U; - hmsc->bot_data_length = len; - - while (len) - { - len--; - hmsc->bot_data[len] = MSC_Mode_Sense6_data[len]; - } - return 0; -} - -/** -* @brief SCSI_ModeSense10 -* Process Mode Sense10 command -* @param lun: Logical unit number -* @param params: Command parameters -* @retval status -*/ -static int8_t SCSI_ModeSense10 (USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) -{ - uint16_t len = 8U; - USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; - - hmsc->bot_data_length = len; - - while (len) - { - len--; - hmsc->bot_data[len] = MSC_Mode_Sense10_data[len]; - } - - return 0; -} - -/** -* @brief SCSI_RequestSense -* Process Request Sense command -* @param lun: Logical unit number -* @param params: Command parameters -* @retval status -*/ - -static int8_t SCSI_RequestSense (USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) -{ - uint8_t i; - USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; - - for(i = 0U ;i < REQUEST_SENSE_DATA_LEN; i++) - { - hmsc->bot_data[i] = 0U; - } - - hmsc->bot_data[0] = 0x70U; - hmsc->bot_data[7] = REQUEST_SENSE_DATA_LEN - 6U; - - if((hmsc->scsi_sense_head != hmsc->scsi_sense_tail)) { - - hmsc->bot_data[2] = hmsc->scsi_sense[hmsc->scsi_sense_head].Skey; - hmsc->bot_data[12] = hmsc->scsi_sense[hmsc->scsi_sense_head].w.b.ASCQ; - hmsc->bot_data[13] = hmsc->scsi_sense[hmsc->scsi_sense_head].w.b.ASC; - hmsc->scsi_sense_head++; - - if (hmsc->scsi_sense_head == SENSE_LIST_DEEPTH) - { - hmsc->scsi_sense_head = 0U; - } - } - hmsc->bot_data_length = REQUEST_SENSE_DATA_LEN; - - if (params[4] <= REQUEST_SENSE_DATA_LEN) - { - hmsc->bot_data_length = params[4]; - } - return 0; -} - -/** -* @brief SCSI_SenseCode -* Load the last error code in the error list -* @param lun: Logical unit number -* @param sKey: Sense Key -* @param ASC: Additional Sense Key -* @retval none - -*/ -void SCSI_SenseCode(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t sKey, uint8_t ASC) -{ - USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; - - hmsc->scsi_sense[hmsc->scsi_sense_tail].Skey = sKey; - hmsc->scsi_sense[hmsc->scsi_sense_tail].w.ASC = ASC << 8; - hmsc->scsi_sense_tail++; - if (hmsc->scsi_sense_tail == SENSE_LIST_DEEPTH) - { - hmsc->scsi_sense_tail = 0U; - } -} -/** -* @brief SCSI_StartStopUnit -* Process Start Stop Unit command -* @param lun: Logical unit number -* @param params: Command parameters -* @retval status -*/ -static int8_t SCSI_StartStopUnit(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) -{ - USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*) pdev->pClassData; - hmsc->bot_data_length = 0U; - return 0; -} - -/** -* @brief SCSI_Read10 -* Process Read10 command -* @param lun: Logical unit number -* @param params: Command parameters -* @retval status -*/ -static int8_t SCSI_Read10(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) -{ - USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*) pdev->pClassData; - - if(hmsc->bot_state == USBD_BOT_IDLE) /* Idle */ - { - /* case 10 : Ho <> Di */ - if ((hmsc->cbw.bmFlags & 0x80U) != 0x80U) - { - SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST, INVALID_CDB); - return -1; - } - - if(((USBD_StorageTypeDef *)pdev->pUserData)->IsReady(lun) != 0) - { - SCSI_SenseCode(pdev, lun, NOT_READY, MEDIUM_NOT_PRESENT); - return -1; - } - - hmsc->scsi_blk_addr = ((uint32_t)params[2] << 24) | - ((uint32_t)params[3] << 16) | - ((uint32_t)params[4] << 8) | - (uint32_t)params[5]; - - hmsc->scsi_blk_len = ((uint32_t)params[7] << 8) | (uint32_t)params[8]; - - if(SCSI_CheckAddressRange(pdev, lun, hmsc->scsi_blk_addr, - hmsc->scsi_blk_len) < 0) - { - return -1; /* error */ - } - - hmsc->bot_state = USBD_BOT_DATA_IN; - - /* cases 4,5 : Hi <> Dn */ - if (hmsc->cbw.dDataLength != (hmsc->scsi_blk_len * hmsc->scsi_blk_size)) - { - SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST, INVALID_CDB); - return -1; - } - } - hmsc->bot_data_length = MSC_MEDIA_PACKET; - - return SCSI_ProcessRead(pdev, lun); -} - -/** -* @brief SCSI_Write10 -* Process Write10 command -* @param lun: Logical unit number -* @param params: Command parameters -* @retval status -*/ - -static int8_t SCSI_Write10 (USBD_HandleTypeDef *pdev, uint8_t lun , uint8_t *params) -{ - USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*) pdev->pClassData; - uint32_t len; - - if (hmsc->bot_state == USBD_BOT_IDLE) /* Idle */ - { - /* case 8 : Hi <> Do */ - if ((hmsc->cbw.bmFlags & 0x80U) == 0x80U) - { - SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST, INVALID_CDB); - return -1; - } - - /* Check whether Media is ready */ - if(((USBD_StorageTypeDef *)pdev->pUserData)->IsReady(lun) != 0) - { - SCSI_SenseCode(pdev, lun, NOT_READY, MEDIUM_NOT_PRESENT); - return -1; - } - - /* Check If media is write-protected */ - if(((USBD_StorageTypeDef *)pdev->pUserData)->IsWriteProtected(lun) != 0) - { - SCSI_SenseCode(pdev, lun, NOT_READY, WRITE_PROTECTED); - return -1; - } - - hmsc->scsi_blk_addr = ((uint32_t)params[2] << 24) | - ((uint32_t)params[3] << 16) | - ((uint32_t)params[4] << 8) | - (uint32_t)params[5]; - - hmsc->scsi_blk_len = ((uint32_t)params[7] << 8) | - (uint32_t)params[8]; - - /* check if LBA address is in the right range */ - if(SCSI_CheckAddressRange(pdev, lun, hmsc->scsi_blk_addr, - hmsc->scsi_blk_len) < 0) - { - return -1; /* error */ - } - - len = hmsc->scsi_blk_len * hmsc->scsi_blk_size; - - /* cases 3,11,13 : Hn,Ho <> D0 */ - if (hmsc->cbw.dDataLength != len) - { - SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST, INVALID_CDB); - return -1; - } - - len = MIN(len, MSC_MEDIA_PACKET); - - /* Prepare EP to receive first data packet */ - hmsc->bot_state = USBD_BOT_DATA_OUT; - USBD_LL_PrepareReceive (pdev, MSC_EPOUT_ADDR, hmsc->bot_data, len); - } - else /* Write Process ongoing */ - { - return SCSI_ProcessWrite(pdev, lun); - } - return 0; -} - - -/** -* @brief SCSI_Verify10 -* Process Verify10 command -* @param lun: Logical unit number -* @param params: Command parameters -* @retval status -*/ - -static int8_t SCSI_Verify10(USBD_HandleTypeDef *pdev, uint8_t lun , uint8_t *params) -{ - USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*) pdev->pClassData; - - if ((params[1]& 0x02U) == 0x02U) - { - SCSI_SenseCode(pdev, lun, ILLEGAL_REQUEST, INVALID_FIELED_IN_COMMAND); - return -1; /* Error, Verify Mode Not supported*/ - } - - if(SCSI_CheckAddressRange(pdev, lun, hmsc->scsi_blk_addr, - hmsc->scsi_blk_len) < 0) - { - return -1; /* error */ - } - hmsc->bot_data_length = 0U; - return 0; -} - -/** -* @brief SCSI_CheckAddressRange -* Check address range -* @param lun: Logical unit number -* @param blk_offset: first block address -* @param blk_nbr: number of block to be processed -* @retval status -*/ -static int8_t SCSI_CheckAddressRange (USBD_HandleTypeDef *pdev, uint8_t lun, - uint32_t blk_offset, uint32_t blk_nbr) -{ - USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*) pdev->pClassData; - - if ((blk_offset + blk_nbr) > hmsc->scsi_blk_nbr) - { - SCSI_SenseCode(pdev, lun, ILLEGAL_REQUEST, ADDRESS_OUT_OF_RANGE); - return -1; - } - return 0; -} - -/** -* @brief SCSI_ProcessRead -* Handle Read Process -* @param lun: Logical unit number -* @retval status -*/ -static int8_t SCSI_ProcessRead (USBD_HandleTypeDef *pdev, uint8_t lun) -{ - USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; - uint32_t len = hmsc->scsi_blk_len * hmsc->scsi_blk_size; - - len = MIN(len, MSC_MEDIA_PACKET); - - if( ((USBD_StorageTypeDef *)pdev->pUserData)->Read(lun, - hmsc->bot_data, - hmsc->scsi_blk_addr, - (len / hmsc->scsi_blk_size)) < 0) - { - SCSI_SenseCode(pdev, lun, HARDWARE_ERROR, UNRECOVERED_READ_ERROR); - return -1; - } - - USBD_LL_Transmit (pdev, MSC_EPIN_ADDR, hmsc->bot_data, len); - - hmsc->scsi_blk_addr += (len / hmsc->scsi_blk_size); - hmsc->scsi_blk_len -= (len / hmsc->scsi_blk_size); - - /* case 6 : Hi = Di */ - hmsc->csw.dDataResidue -= len; - - if (hmsc->scsi_blk_len == 0U) - { - hmsc->bot_state = USBD_BOT_LAST_DATA_IN; - } - return 0; -} - -/** -* @brief SCSI_ProcessWrite -* Handle Write Process -* @param lun: Logical unit number -* @retval status -*/ - -static int8_t SCSI_ProcessWrite (USBD_HandleTypeDef *pdev, uint8_t lun) -{ - USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*) pdev->pClassData; - uint32_t len = hmsc->scsi_blk_len * hmsc->scsi_blk_size; - - len = MIN(len, MSC_MEDIA_PACKET); - - if(((USBD_StorageTypeDef *)pdev->pUserData)->Write(lun, hmsc->bot_data, - hmsc->scsi_blk_addr, - (len / hmsc->scsi_blk_size)) < 0) - { - SCSI_SenseCode(pdev, lun, HARDWARE_ERROR, WRITE_FAULT); - - return -1; - } - - hmsc->scsi_blk_addr += (len / hmsc->scsi_blk_size); - hmsc->scsi_blk_len -= (len / hmsc->scsi_blk_size); - - /* case 12 : Ho = Do */ - hmsc->csw.dDataResidue -= len; - - if (hmsc->scsi_blk_len == 0U) - { - MSC_BOT_SendCSW (pdev, USBD_CSW_CMD_PASSED); - } - else - { - len = MIN((hmsc->scsi_blk_len * hmsc->scsi_blk_size), MSC_MEDIA_PACKET); - /* Prepare EP to Receive next packet */ - USBD_LL_PrepareReceive (pdev, MSC_EPOUT_ADDR, hmsc->bot_data, len); - } - - return 0; -} -/** - * @} - */ - - -/** - * @} - */ - - -/** - * @} - */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_msc_scsi.c + * @author MCD Application Team + * @brief This file provides all the USBD SCSI layer functions. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + + /* BSPDependencies + - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" + - "stm32xxxxx_{eval}{discovery}_io.c" + - "stm32xxxxx_{eval}{discovery}{adafruit}_sd.c" + EndBSPDependencies */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_msc_bot.h" +#include "usbd_msc_scsi.h" +#include "usbd_msc.h" +#include "usbd_msc_data.h" + + + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + + +/** @defgroup MSC_SCSI + * @brief Mass storage SCSI layer module + * @{ + */ + +/** @defgroup MSC_SCSI_Private_TypesDefinitions + * @{ + */ +/** + * @} + */ + + +/** @defgroup MSC_SCSI_Private_Defines + * @{ + */ + +/** + * @} + */ + + +/** @defgroup MSC_SCSI_Private_Macros + * @{ + */ +/** + * @} + */ + + +/** @defgroup MSC_SCSI_Private_Variables + * @{ + */ + +/** + * @} + */ + + +/** @defgroup MSC_SCSI_Private_FunctionPrototypes + * @{ + */ +static int8_t SCSI_TestUnitReady(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params); +static int8_t SCSI_Inquiry(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params); +static int8_t SCSI_ReadFormatCapacity(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params); +static int8_t SCSI_ReadCapacity10(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params); +static int8_t SCSI_RequestSense (USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params); +static int8_t SCSI_StartStopUnit(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params); +static int8_t SCSI_ModeSense6 (USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params); +static int8_t SCSI_ModeSense10 (USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params); +static int8_t SCSI_Write10(USBD_HandleTypeDef *pdev, uint8_t lun , uint8_t *params); +static int8_t SCSI_Read10(USBD_HandleTypeDef *pdev, uint8_t lun , uint8_t *params); +static int8_t SCSI_Verify10(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params); +static int8_t SCSI_CheckAddressRange (USBD_HandleTypeDef *pdev, uint8_t lun, + uint32_t blk_offset, uint32_t blk_nbr); + +static int8_t SCSI_ProcessRead (USBD_HandleTypeDef *pdev, uint8_t lun); +static int8_t SCSI_ProcessWrite (USBD_HandleTypeDef *pdev, uint8_t lun); +/** + * @} + */ + + +/** @defgroup MSC_SCSI_Private_Functions + * @{ + */ + + +/** +* @brief SCSI_ProcessCmd +* Process SCSI commands +* @param pdev: device instance +* @param lun: Logical unit number +* @param params: Command parameters +* @retval status +*/ +int8_t SCSI_ProcessCmd(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *cmd) +{ + switch (cmd[0]) + { + case SCSI_TEST_UNIT_READY: + SCSI_TestUnitReady(pdev, lun, cmd); + break; + + case SCSI_REQUEST_SENSE: + SCSI_RequestSense (pdev, lun, cmd); + break; + case SCSI_INQUIRY: + SCSI_Inquiry(pdev, lun, cmd); + break; + + case SCSI_START_STOP_UNIT: + SCSI_StartStopUnit(pdev, lun, cmd); + break; + + case SCSI_ALLOW_MEDIUM_REMOVAL: + SCSI_StartStopUnit(pdev, lun, cmd); + break; + + case SCSI_MODE_SENSE6: + SCSI_ModeSense6 (pdev, lun, cmd); + break; + + case SCSI_MODE_SENSE10: + SCSI_ModeSense10 (pdev, lun, cmd); + break; + + case SCSI_READ_FORMAT_CAPACITIES: + SCSI_ReadFormatCapacity(pdev, lun, cmd); + break; + + case SCSI_READ_CAPACITY10: + SCSI_ReadCapacity10(pdev, lun, cmd); + break; + + case SCSI_READ10: + SCSI_Read10(pdev, lun, cmd); + break; + + case SCSI_WRITE10: + SCSI_Write10(pdev, lun, cmd); + break; + + case SCSI_VERIFY10: + SCSI_Verify10(pdev, lun, cmd); + break; + + default: + SCSI_SenseCode(pdev, lun, ILLEGAL_REQUEST, INVALID_CDB); + return -1; + } + return 0; +} + + +/** +* @brief SCSI_TestUnitReady +* Process SCSI Test Unit Ready Command +* @param lun: Logical unit number +* @param params: Command parameters +* @retval status +*/ +static int8_t SCSI_TestUnitReady(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) +{ + USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; + + /* case 9 : Hi > D0 */ + if (hmsc->cbw.dDataLength != 0U) + { + SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST, INVALID_CDB); + + return -1; + } + + if(((USBD_StorageTypeDef *)pdev->pUserData)->IsReady(lun) != 0) + { + SCSI_SenseCode(pdev, lun, NOT_READY, MEDIUM_NOT_PRESENT); + hmsc->bot_state = USBD_BOT_NO_DATA; + + return -1; + } + hmsc->bot_data_length = 0U; + + return 0; +} + +/** +* @brief SCSI_Inquiry +* Process Inquiry command +* @param lun: Logical unit number +* @param params: Command parameters +* @retval status +*/ +static int8_t SCSI_Inquiry(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) +{ + uint8_t* pPage; + uint16_t len; + USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; + + if (params[1] & 0x01U)/*Evpd is set*/ + { + len = LENGTH_INQUIRY_PAGE00; + hmsc->bot_data_length = len; + + while (len) + { + len--; + hmsc->bot_data[len] = MSC_Page00_Inquiry_Data[len]; + } + } + else + { + pPage = (uint8_t *)(void *)&((USBD_StorageTypeDef *)pdev->pUserData)->pInquiry[lun * STANDARD_INQUIRY_DATA_LEN]; + len = (uint16_t)pPage[4] + 5U; + + if (params[4] <= len) + { + len = params[4]; + } + hmsc->bot_data_length = len; + + while (len) + { + len--; + hmsc->bot_data[len] = pPage[len]; + } + } + + return 0; +} + +/** +* @brief SCSI_ReadCapacity10 +* Process Read Capacity 10 command +* @param lun: Logical unit number +* @param params: Command parameters +* @retval status +*/ +static int8_t SCSI_ReadCapacity10(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) +{ + USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; + + if(((USBD_StorageTypeDef *)pdev->pUserData)->GetCapacity(lun, &hmsc->scsi_blk_nbr, &hmsc->scsi_blk_size) != 0) + { + SCSI_SenseCode(pdev, lun, NOT_READY, MEDIUM_NOT_PRESENT); + return -1; + } + else + { + + hmsc->bot_data[0] = (uint8_t)((hmsc->scsi_blk_nbr - 1U) >> 24); + hmsc->bot_data[1] = (uint8_t)((hmsc->scsi_blk_nbr - 1U) >> 16); + hmsc->bot_data[2] = (uint8_t)((hmsc->scsi_blk_nbr - 1U) >> 8); + hmsc->bot_data[3] = (uint8_t)(hmsc->scsi_blk_nbr - 1U); + + hmsc->bot_data[4] = (uint8_t)(hmsc->scsi_blk_size >> 24); + hmsc->bot_data[5] = (uint8_t)(hmsc->scsi_blk_size >> 16); + hmsc->bot_data[6] = (uint8_t)(hmsc->scsi_blk_size >> 8); + hmsc->bot_data[7] = (uint8_t)(hmsc->scsi_blk_size); + + hmsc->bot_data_length = 8U; + return 0; + } +} +/** +* @brief SCSI_ReadFormatCapacity +* Process Read Format Capacity command +* @param lun: Logical unit number +* @param params: Command parameters +* @retval status +*/ +static int8_t SCSI_ReadFormatCapacity(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) +{ + USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; + + uint16_t blk_size; + uint32_t blk_nbr; + uint16_t i; + + for(i = 0U; i < 12U ; i++) + { + hmsc->bot_data[i] = 0U; + } + + if(((USBD_StorageTypeDef *)pdev->pUserData)->GetCapacity(lun, &blk_nbr, &blk_size) != 0U) + { + SCSI_SenseCode(pdev, lun, NOT_READY, MEDIUM_NOT_PRESENT); + return -1; + } + else + { + hmsc->bot_data[3] = 0x08U; + hmsc->bot_data[4] = (uint8_t)((blk_nbr - 1U) >> 24); + hmsc->bot_data[5] = (uint8_t)((blk_nbr - 1U) >> 16); + hmsc->bot_data[6] = (uint8_t)((blk_nbr - 1U) >> 8); + hmsc->bot_data[7] = (uint8_t)(blk_nbr - 1U); + + hmsc->bot_data[8] = 0x02U; + hmsc->bot_data[9] = (uint8_t)(blk_size >> 16); + hmsc->bot_data[10] = (uint8_t)(blk_size >> 8); + hmsc->bot_data[11] = (uint8_t)(blk_size); + + hmsc->bot_data_length = 12U; + return 0; + } +} +/** +* @brief SCSI_ModeSense6 +* Process Mode Sense6 command +* @param lun: Logical unit number +* @param params: Command parameters +* @retval status +*/ +static int8_t SCSI_ModeSense6 (USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) +{ + USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; + uint16_t len = 8U; + hmsc->bot_data_length = len; + + while (len) + { + len--; + hmsc->bot_data[len] = MSC_Mode_Sense6_data[len]; + } + return 0; +} + +/** +* @brief SCSI_ModeSense10 +* Process Mode Sense10 command +* @param lun: Logical unit number +* @param params: Command parameters +* @retval status +*/ +static int8_t SCSI_ModeSense10 (USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) +{ + uint16_t len = 8U; + USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; + + hmsc->bot_data_length = len; + + while (len) + { + len--; + hmsc->bot_data[len] = MSC_Mode_Sense10_data[len]; + } + + return 0; +} + +/** +* @brief SCSI_RequestSense +* Process Request Sense command +* @param lun: Logical unit number +* @param params: Command parameters +* @retval status +*/ + +static int8_t SCSI_RequestSense (USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) +{ + uint8_t i; + USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; + + for(i = 0U ;i < REQUEST_SENSE_DATA_LEN; i++) + { + hmsc->bot_data[i] = 0U; + } + + hmsc->bot_data[0] = 0x70U; + hmsc->bot_data[7] = REQUEST_SENSE_DATA_LEN - 6U; + + if((hmsc->scsi_sense_head != hmsc->scsi_sense_tail)) { + + hmsc->bot_data[2] = hmsc->scsi_sense[hmsc->scsi_sense_head].Skey; + hmsc->bot_data[12] = hmsc->scsi_sense[hmsc->scsi_sense_head].w.b.ASCQ; + hmsc->bot_data[13] = hmsc->scsi_sense[hmsc->scsi_sense_head].w.b.ASC; + hmsc->scsi_sense_head++; + + if (hmsc->scsi_sense_head == SENSE_LIST_DEEPTH) + { + hmsc->scsi_sense_head = 0U; + } + } + hmsc->bot_data_length = REQUEST_SENSE_DATA_LEN; + + if (params[4] <= REQUEST_SENSE_DATA_LEN) + { + hmsc->bot_data_length = params[4]; + } + return 0; +} + +/** +* @brief SCSI_SenseCode +* Load the last error code in the error list +* @param lun: Logical unit number +* @param sKey: Sense Key +* @param ASC: Additional Sense Key +* @retval none + +*/ +void SCSI_SenseCode(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t sKey, uint8_t ASC) +{ + USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; + + hmsc->scsi_sense[hmsc->scsi_sense_tail].Skey = sKey; + hmsc->scsi_sense[hmsc->scsi_sense_tail].w.ASC = ASC << 8; + hmsc->scsi_sense_tail++; + if (hmsc->scsi_sense_tail == SENSE_LIST_DEEPTH) + { + hmsc->scsi_sense_tail = 0U; + } +} +/** +* @brief SCSI_StartStopUnit +* Process Start Stop Unit command +* @param lun: Logical unit number +* @param params: Command parameters +* @retval status +*/ +static int8_t SCSI_StartStopUnit(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) +{ + USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*) pdev->pClassData; + hmsc->bot_data_length = 0U; + return 0; +} + +/** +* @brief SCSI_Read10 +* Process Read10 command +* @param lun: Logical unit number +* @param params: Command parameters +* @retval status +*/ +static int8_t SCSI_Read10(USBD_HandleTypeDef *pdev, uint8_t lun, uint8_t *params) +{ + USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*) pdev->pClassData; + + if(hmsc->bot_state == USBD_BOT_IDLE) /* Idle */ + { + /* case 10 : Ho <> Di */ + if ((hmsc->cbw.bmFlags & 0x80U) != 0x80U) + { + SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST, INVALID_CDB); + return -1; + } + + if(((USBD_StorageTypeDef *)pdev->pUserData)->IsReady(lun) != 0) + { + SCSI_SenseCode(pdev, lun, NOT_READY, MEDIUM_NOT_PRESENT); + return -1; + } + + hmsc->scsi_blk_addr = ((uint32_t)params[2] << 24) | + ((uint32_t)params[3] << 16) | + ((uint32_t)params[4] << 8) | + (uint32_t)params[5]; + + hmsc->scsi_blk_len = ((uint32_t)params[7] << 8) | (uint32_t)params[8]; + + if(SCSI_CheckAddressRange(pdev, lun, hmsc->scsi_blk_addr, + hmsc->scsi_blk_len) < 0) + { + return -1; /* error */ + } + + hmsc->bot_state = USBD_BOT_DATA_IN; + + /* cases 4,5 : Hi <> Dn */ + if (hmsc->cbw.dDataLength != (hmsc->scsi_blk_len * hmsc->scsi_blk_size)) + { + SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST, INVALID_CDB); + return -1; + } + } + hmsc->bot_data_length = MSC_MEDIA_PACKET; + + return SCSI_ProcessRead(pdev, lun); +} + +/** +* @brief SCSI_Write10 +* Process Write10 command +* @param lun: Logical unit number +* @param params: Command parameters +* @retval status +*/ + +static int8_t SCSI_Write10 (USBD_HandleTypeDef *pdev, uint8_t lun , uint8_t *params) +{ + USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*) pdev->pClassData; + uint32_t len; + + if (hmsc->bot_state == USBD_BOT_IDLE) /* Idle */ + { + /* case 8 : Hi <> Do */ + if ((hmsc->cbw.bmFlags & 0x80U) == 0x80U) + { + SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST, INVALID_CDB); + return -1; + } + + /* Check whether Media is ready */ + if(((USBD_StorageTypeDef *)pdev->pUserData)->IsReady(lun) != 0) + { + SCSI_SenseCode(pdev, lun, NOT_READY, MEDIUM_NOT_PRESENT); + return -1; + } + + /* Check If media is write-protected */ + if(((USBD_StorageTypeDef *)pdev->pUserData)->IsWriteProtected(lun) != 0) + { + SCSI_SenseCode(pdev, lun, NOT_READY, WRITE_PROTECTED); + return -1; + } + + hmsc->scsi_blk_addr = ((uint32_t)params[2] << 24) | + ((uint32_t)params[3] << 16) | + ((uint32_t)params[4] << 8) | + (uint32_t)params[5]; + + hmsc->scsi_blk_len = ((uint32_t)params[7] << 8) | + (uint32_t)params[8]; + + /* check if LBA address is in the right range */ + if(SCSI_CheckAddressRange(pdev, lun, hmsc->scsi_blk_addr, + hmsc->scsi_blk_len) < 0) + { + return -1; /* error */ + } + + len = hmsc->scsi_blk_len * hmsc->scsi_blk_size; + + /* cases 3,11,13 : Hn,Ho <> D0 */ + if (hmsc->cbw.dDataLength != len) + { + SCSI_SenseCode(pdev, hmsc->cbw.bLUN, ILLEGAL_REQUEST, INVALID_CDB); + return -1; + } + + len = MIN(len, MSC_MEDIA_PACKET); + + /* Prepare EP to receive first data packet */ + hmsc->bot_state = USBD_BOT_DATA_OUT; + USBD_LL_PrepareReceive (pdev, MSC_EPOUT_ADDR, hmsc->bot_data, len); + } + else /* Write Process ongoing */ + { + return SCSI_ProcessWrite(pdev, lun); + } + return 0; +} + + +/** +* @brief SCSI_Verify10 +* Process Verify10 command +* @param lun: Logical unit number +* @param params: Command parameters +* @retval status +*/ + +static int8_t SCSI_Verify10(USBD_HandleTypeDef *pdev, uint8_t lun , uint8_t *params) +{ + USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*) pdev->pClassData; + + if ((params[1]& 0x02U) == 0x02U) + { + SCSI_SenseCode(pdev, lun, ILLEGAL_REQUEST, INVALID_FIELED_IN_COMMAND); + return -1; /* Error, Verify Mode Not supported*/ + } + + if(SCSI_CheckAddressRange(pdev, lun, hmsc->scsi_blk_addr, + hmsc->scsi_blk_len) < 0) + { + return -1; /* error */ + } + hmsc->bot_data_length = 0U; + return 0; +} + +/** +* @brief SCSI_CheckAddressRange +* Check address range +* @param lun: Logical unit number +* @param blk_offset: first block address +* @param blk_nbr: number of block to be processed +* @retval status +*/ +static int8_t SCSI_CheckAddressRange (USBD_HandleTypeDef *pdev, uint8_t lun, + uint32_t blk_offset, uint32_t blk_nbr) +{ + USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*) pdev->pClassData; + + if ((blk_offset + blk_nbr) > hmsc->scsi_blk_nbr) + { + SCSI_SenseCode(pdev, lun, ILLEGAL_REQUEST, ADDRESS_OUT_OF_RANGE); + return -1; + } + return 0; +} + +/** +* @brief SCSI_ProcessRead +* Handle Read Process +* @param lun: Logical unit number +* @retval status +*/ +static int8_t SCSI_ProcessRead (USBD_HandleTypeDef *pdev, uint8_t lun) +{ + USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*)pdev->pClassData; + uint32_t len = hmsc->scsi_blk_len * hmsc->scsi_blk_size; + + len = MIN(len, MSC_MEDIA_PACKET); + + if( ((USBD_StorageTypeDef *)pdev->pUserData)->Read(lun, + hmsc->bot_data, + hmsc->scsi_blk_addr, + (len / hmsc->scsi_blk_size)) < 0) + { + SCSI_SenseCode(pdev, lun, HARDWARE_ERROR, UNRECOVERED_READ_ERROR); + return -1; + } + + USBD_LL_Transmit (pdev, MSC_EPIN_ADDR, hmsc->bot_data, len); + + hmsc->scsi_blk_addr += (len / hmsc->scsi_blk_size); + hmsc->scsi_blk_len -= (len / hmsc->scsi_blk_size); + + /* case 6 : Hi = Di */ + hmsc->csw.dDataResidue -= len; + + if (hmsc->scsi_blk_len == 0U) + { + hmsc->bot_state = USBD_BOT_LAST_DATA_IN; + } + return 0; +} + +/** +* @brief SCSI_ProcessWrite +* Handle Write Process +* @param lun: Logical unit number +* @retval status +*/ + +static int8_t SCSI_ProcessWrite (USBD_HandleTypeDef *pdev, uint8_t lun) +{ + USBD_MSC_BOT_HandleTypeDef *hmsc = (USBD_MSC_BOT_HandleTypeDef*) pdev->pClassData; + uint32_t len = hmsc->scsi_blk_len * hmsc->scsi_blk_size; + + len = MIN(len, MSC_MEDIA_PACKET); + + if(((USBD_StorageTypeDef *)pdev->pUserData)->Write(lun, hmsc->bot_data, + hmsc->scsi_blk_addr, + (len / hmsc->scsi_blk_size)) < 0) + { + SCSI_SenseCode(pdev, lun, HARDWARE_ERROR, WRITE_FAULT); + + return -1; + } + + hmsc->scsi_blk_addr += (len / hmsc->scsi_blk_size); + hmsc->scsi_blk_len -= (len / hmsc->scsi_blk_size); + + /* case 12 : Ho = Do */ + hmsc->csw.dDataResidue -= len; + + if (hmsc->scsi_blk_len == 0U) + { + MSC_BOT_SendCSW (pdev, USBD_CSW_CMD_PASSED); + } + else + { + len = MIN((hmsc->scsi_blk_len * hmsc->scsi_blk_size), MSC_MEDIA_PACKET); + /* Prepare EP to Receive next packet */ + USBD_LL_PrepareReceive (pdev, MSC_EPOUT_ADDR, hmsc->bot_data, len); + } + + return 0; +} +/** + * @} + */ + + +/** + * @} + */ + + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_storage_template.c b/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_storage_template.c index d9b621d000..38745e3aba 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_storage_template.c +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Class/MSC/Src/usbd_msc_storage_template.c @@ -1,185 +1,185 @@ -/** - ****************************************************************************** - * @file usbd_msc_storage_template.c - * @author MCD Application Team - * @brief Memory management layer - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - - /* BSPDependencies - - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" - - "stm32xxxxx_{eval}{discovery}_io.c" - - "stm32xxxxx_{eval}{discovery}{adafruit}_sd.c" - EndBSPDependencies */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_msc_storage_template.h" - - -/* Private typedef -----------------------------------------------------------*/ -/* Private define ------------------------------------------------------------*/ -/* Private macro -------------------------------------------------------------*/ -/* Private variables ---------------------------------------------------------*/ -/* Private function prototypes -----------------------------------------------*/ -/* Extern function prototypes ------------------------------------------------*/ -/* Private functions ---------------------------------------------------------*/ - -#define STORAGE_LUN_NBR 1 -#define STORAGE_BLK_NBR 0x10000 -#define STORAGE_BLK_SIZ 0x200 - -int8_t STORAGE_Init (uint8_t lun); - -int8_t STORAGE_GetCapacity (uint8_t lun, - uint32_t *block_num, - uint16_t *block_size); - -int8_t STORAGE_IsReady (uint8_t lun); - -int8_t STORAGE_IsWriteProtected (uint8_t lun); - -int8_t STORAGE_Read (uint8_t lun, - uint8_t *buf, - uint32_t blk_addr, - uint16_t blk_len); - -int8_t STORAGE_Write (uint8_t lun, - uint8_t *buf, - uint32_t blk_addr, - uint16_t blk_len); - -int8_t STORAGE_GetMaxLun (void); - -/* USB Mass storage Standard Inquiry Data */ -int8_t STORAGE_Inquirydata[] = {//36 - - /* LUN 0 */ - 0x00, - 0x80, - 0x02, - 0x02, - (STANDARD_INQUIRY_DATA_LEN - 5), - 0x00, - 0x00, - 0x00, - 'S', 'T', 'M', ' ', ' ', ' ', ' ', ' ', /* Manufacturer : 8 bytes */ - 'P', 'r', 'o', 'd', 'u', 'c', 't', ' ', /* Product : 16 Bytes */ - ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', - '0', '.', '0' ,'1', /* Version : 4 Bytes */ -}; - -USBD_StorageTypeDef USBD_MSC_Template_fops = -{ - STORAGE_Init, - STORAGE_GetCapacity, - STORAGE_IsReady, - STORAGE_IsWriteProtected, - STORAGE_Read, - STORAGE_Write, - STORAGE_GetMaxLun, - STORAGE_Inquirydata, - -}; -/******************************************************************************* -* Function Name : Read_Memory -* Description : Handle the Read operation from the microSD card. -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -int8_t STORAGE_Init (uint8_t lun) -{ - return (0); -} - -/******************************************************************************* -* Function Name : Read_Memory -* Description : Handle the Read operation from the STORAGE card. -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint16_t *block_size) -{ - *block_num = STORAGE_BLK_NBR; - *block_size = STORAGE_BLK_SIZ; - return (0); -} - -/******************************************************************************* -* Function Name : Read_Memory -* Description : Handle the Read operation from the STORAGE card. -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -int8_t STORAGE_IsReady (uint8_t lun) -{ - return (0); -} - -/******************************************************************************* -* Function Name : Read_Memory -* Description : Handle the Read operation from the STORAGE card. -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -int8_t STORAGE_IsWriteProtected (uint8_t lun) -{ - return 0; -} - -/******************************************************************************* -* Function Name : Read_Memory -* Description : Handle the Read operation from the STORAGE card. -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -int8_t STORAGE_Read (uint8_t lun, - uint8_t *buf, - uint32_t blk_addr, - uint16_t blk_len) -{ - return 0; -} -/******************************************************************************* -* Function Name : Write_Memory -* Description : Handle the Write operation to the STORAGE card. -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -int8_t STORAGE_Write (uint8_t lun, - uint8_t *buf, - uint32_t blk_addr, - uint16_t blk_len) -{ - return (0); -} -/******************************************************************************* -* Function Name : Write_Memory -* Description : Handle the Write operation to the STORAGE card. -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -int8_t STORAGE_GetMaxLun (void) -{ - return (STORAGE_LUN_NBR - 1); -} - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - +/** + ****************************************************************************** + * @file usbd_msc_storage_template.c + * @author MCD Application Team + * @brief Memory management layer + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + + /* BSPDependencies + - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" + - "stm32xxxxx_{eval}{discovery}_io.c" + - "stm32xxxxx_{eval}{discovery}{adafruit}_sd.c" + EndBSPDependencies */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_msc_storage_template.h" + + +/* Private typedef -----------------------------------------------------------*/ +/* Private define ------------------------------------------------------------*/ +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +/* Private function prototypes -----------------------------------------------*/ +/* Extern function prototypes ------------------------------------------------*/ +/* Private functions ---------------------------------------------------------*/ + +#define STORAGE_LUN_NBR 1 +#define STORAGE_BLK_NBR 0x10000 +#define STORAGE_BLK_SIZ 0x200 + +int8_t STORAGE_Init (uint8_t lun); + +int8_t STORAGE_GetCapacity (uint8_t lun, + uint32_t *block_num, + uint16_t *block_size); + +int8_t STORAGE_IsReady (uint8_t lun); + +int8_t STORAGE_IsWriteProtected (uint8_t lun); + +int8_t STORAGE_Read (uint8_t lun, + uint8_t *buf, + uint32_t blk_addr, + uint16_t blk_len); + +int8_t STORAGE_Write (uint8_t lun, + uint8_t *buf, + uint32_t blk_addr, + uint16_t blk_len); + +int8_t STORAGE_GetMaxLun (void); + +/* USB Mass storage Standard Inquiry Data */ +int8_t STORAGE_Inquirydata[] = {//36 + + /* LUN 0 */ + 0x00, + 0x80, + 0x02, + 0x02, + (STANDARD_INQUIRY_DATA_LEN - 5), + 0x00, + 0x00, + 0x00, + 'S', 'T', 'M', ' ', ' ', ' ', ' ', ' ', /* Manufacturer : 8 bytes */ + 'P', 'r', 'o', 'd', 'u', 'c', 't', ' ', /* Product : 16 Bytes */ + ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', + '0', '.', '0' ,'1', /* Version : 4 Bytes */ +}; + +USBD_StorageTypeDef USBD_MSC_Template_fops = +{ + STORAGE_Init, + STORAGE_GetCapacity, + STORAGE_IsReady, + STORAGE_IsWriteProtected, + STORAGE_Read, + STORAGE_Write, + STORAGE_GetMaxLun, + STORAGE_Inquirydata, + +}; +/******************************************************************************* +* Function Name : Read_Memory +* Description : Handle the Read operation from the microSD card. +* Input : None. +* Output : None. +* Return : None. +*******************************************************************************/ +int8_t STORAGE_Init (uint8_t lun) +{ + return (0); +} + +/******************************************************************************* +* Function Name : Read_Memory +* Description : Handle the Read operation from the STORAGE card. +* Input : None. +* Output : None. +* Return : None. +*******************************************************************************/ +int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint16_t *block_size) +{ + *block_num = STORAGE_BLK_NBR; + *block_size = STORAGE_BLK_SIZ; + return (0); +} + +/******************************************************************************* +* Function Name : Read_Memory +* Description : Handle the Read operation from the STORAGE card. +* Input : None. +* Output : None. +* Return : None. +*******************************************************************************/ +int8_t STORAGE_IsReady (uint8_t lun) +{ + return (0); +} + +/******************************************************************************* +* Function Name : Read_Memory +* Description : Handle the Read operation from the STORAGE card. +* Input : None. +* Output : None. +* Return : None. +*******************************************************************************/ +int8_t STORAGE_IsWriteProtected (uint8_t lun) +{ + return 0; +} + +/******************************************************************************* +* Function Name : Read_Memory +* Description : Handle the Read operation from the STORAGE card. +* Input : None. +* Output : None. +* Return : None. +*******************************************************************************/ +int8_t STORAGE_Read (uint8_t lun, + uint8_t *buf, + uint32_t blk_addr, + uint16_t blk_len) +{ + return 0; +} +/******************************************************************************* +* Function Name : Write_Memory +* Description : Handle the Write operation to the STORAGE card. +* Input : None. +* Output : None. +* Return : None. +*******************************************************************************/ +int8_t STORAGE_Write (uint8_t lun, + uint8_t *buf, + uint32_t blk_addr, + uint16_t blk_len) +{ + return (0); +} +/******************************************************************************* +* Function Name : Write_Memory +* Description : Handle the Write operation to the STORAGE card. +* Input : None. +* Output : None. +* Return : None. +*******************************************************************************/ +int8_t STORAGE_GetMaxLun (void) +{ + return (STORAGE_LUN_NBR - 1); +} + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Class/Template/Inc/usbd_template.h b/system/Middlewares/ST/STM32_USB_Device_Library/Class/Template/Inc/usbd_template.h index e9792e8c81..a8fb4c9e7c 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Class/Template/Inc/usbd_template.h +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Class/Template/Inc/usbd_template.h @@ -1,101 +1,101 @@ -/** - ****************************************************************************** - * @file usbd_template_core.h - * @author MCD Application Team - * @brief Header file for the usbd_template_core.c file. - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_TEMPLATE_CORE_H -#define __USB_TEMPLATE_CORE_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_ioreq.h" - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - -/** @defgroup USBD_TEMPLATE - * @brief This file is the header file for usbd_template_core.c - * @{ - */ - - -/** @defgroup USBD_TEMPLATE_Exported_Defines - * @{ - */ -#define TEMPLATE_EPIN_ADDR 0x81 -#define TEMPLATE_EPIN_SIZE 0x10 - -#define USB_TEMPLATE_CONFIG_DESC_SIZ 64 - -/** - * @} - */ - - -/** @defgroup USBD_CORE_Exported_TypesDefinitions - * @{ - */ - -/** - * @} - */ - - - -/** @defgroup USBD_CORE_Exported_Macros - * @{ - */ - -/** - * @} - */ - -/** @defgroup USBD_CORE_Exported_Variables - * @{ - */ - -extern USBD_ClassTypeDef USBD_TEMPLATE_ClassDriver; -/** - * @} - */ - -/** @defgroup USB_CORE_Exported_Functions - * @{ - */ -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USB_TEMPLATE_CORE_H */ -/** - * @} - */ - -/** - * @} - */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_template_core.h + * @author MCD Application Team + * @brief Header file for the usbd_template_core.c file. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USB_TEMPLATE_CORE_H +#define __USB_TEMPLATE_CORE_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_ioreq.h" + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + +/** @defgroup USBD_TEMPLATE + * @brief This file is the header file for usbd_template_core.c + * @{ + */ + + +/** @defgroup USBD_TEMPLATE_Exported_Defines + * @{ + */ +#define TEMPLATE_EPIN_ADDR 0x81 +#define TEMPLATE_EPIN_SIZE 0x10 + +#define USB_TEMPLATE_CONFIG_DESC_SIZ 64 + +/** + * @} + */ + + +/** @defgroup USBD_CORE_Exported_TypesDefinitions + * @{ + */ + +/** + * @} + */ + + + +/** @defgroup USBD_CORE_Exported_Macros + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_CORE_Exported_Variables + * @{ + */ + +extern USBD_ClassTypeDef USBD_TEMPLATE_ClassDriver; +/** + * @} + */ + +/** @defgroup USB_CORE_Exported_Functions + * @{ + */ +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USB_TEMPLATE_CORE_H */ +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Class/Template/Src/usbd_template.c b/system/Middlewares/ST/STM32_USB_Device_Library/Class/Template/Src/usbd_template.c index 533b89dfc2..43cfe2823b 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Class/Template/Src/usbd_template.c +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Class/Template/Src/usbd_template.c @@ -1,397 +1,397 @@ -/** - ****************************************************************************** - * @file usbd_template.c - * @author MCD Application Team - * @brief This file provides the HID core functions. - * - * @verbatim - * - * =================================================================== - * TEMPLATE Class Description - * =================================================================== - * - * - * - * - * - * - * @note In HS mode and when the DMA is used, all variables and data structures - * dealing with the DMA during the transaction process should be 32-bit aligned. - * - * - * @endverbatim - * - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_template.h" -#include "usbd_ctlreq.h" - - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - - -/** @defgroup USBD_TEMPLATE - * @brief usbd core module - * @{ - */ - -/** @defgroup USBD_TEMPLATE_Private_TypesDefinitions - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBD_TEMPLATE_Private_Defines - * @{ - */ - -/** - * @} - */ - - -/** @defgroup USBD_TEMPLATE_Private_Macros - * @{ - */ - -/** - * @} - */ - - - - -/** @defgroup USBD_TEMPLATE_Private_FunctionPrototypes - * @{ - */ - - -static uint8_t USBD_TEMPLATE_Init (USBD_HandleTypeDef *pdev, - uint8_t cfgidx); - -static uint8_t USBD_TEMPLATE_DeInit (USBD_HandleTypeDef *pdev, - uint8_t cfgidx); - -static uint8_t USBD_TEMPLATE_Setup (USBD_HandleTypeDef *pdev, - USBD_SetupReqTypedef *req); - -static uint8_t *USBD_TEMPLATE_GetCfgDesc (uint16_t *length); - -static uint8_t *USBD_TEMPLATE_GetDeviceQualifierDesc (uint16_t *length); - -static uint8_t USBD_TEMPLATE_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum); - -static uint8_t USBD_TEMPLATE_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum); - -static uint8_t USBD_TEMPLATE_EP0_RxReady (USBD_HandleTypeDef *pdev); - -static uint8_t USBD_TEMPLATE_EP0_TxReady (USBD_HandleTypeDef *pdev); - -static uint8_t USBD_TEMPLATE_SOF (USBD_HandleTypeDef *pdev); - -static uint8_t USBD_TEMPLATE_IsoINIncomplete (USBD_HandleTypeDef *pdev, uint8_t epnum); - -static uint8_t USBD_TEMPLATE_IsoOutIncomplete (USBD_HandleTypeDef *pdev, uint8_t epnum); - -/** - * @} - */ - -/** @defgroup USBD_TEMPLATE_Private_Variables - * @{ - */ - -USBD_ClassTypeDef USBD_TEMPLATE_ClassDriver = -{ - USBD_TEMPLATE_Init, - USBD_TEMPLATE_DeInit, - USBD_TEMPLATE_Setup, - USBD_TEMPLATE_EP0_TxReady, - USBD_TEMPLATE_EP0_RxReady, - USBD_TEMPLATE_DataIn, - USBD_TEMPLATE_DataOut, - USBD_TEMPLATE_SOF, - USBD_TEMPLATE_IsoINIncomplete, - USBD_TEMPLATE_IsoOutIncomplete, - USBD_TEMPLATE_GetCfgDesc, - USBD_TEMPLATE_GetCfgDesc, - USBD_TEMPLATE_GetCfgDesc, - USBD_TEMPLATE_GetDeviceQualifierDesc, -}; - -#if defined ( __ICCARM__ ) /*!< IAR Compiler */ - #pragma data_alignment=4 -#endif -/* USB TEMPLATE device Configuration Descriptor */ -static uint8_t USBD_TEMPLATE_CfgDesc[USB_TEMPLATE_CONFIG_DESC_SIZ] = -{ - 0x09, /* bLength: Configuation Descriptor size */ - USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION, /* bDescriptorType: Configuration */ - USB_TEMPLATE_CONFIG_DESC_SIZ, - /* wTotalLength: Bytes returned */ - 0x00, - 0x01, /*bNumInterfaces: 1 interface*/ - 0x01, /*bConfigurationValue: Configuration value*/ - 0x02, /*iConfiguration: Index of string descriptor describing the configuration*/ - 0xC0, /*bmAttributes: bus powered and Supports Remote Wakeup */ - 0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/ - /* 09 */ - - /********** Descriptor of TEMPLATE interface 0 Alternate setting 0 **************/ - -}; - -#if defined ( __ICCARM__ ) /*!< IAR Compiler */ - #pragma data_alignment=4 -#endif -/* USB Standard Device Descriptor */ -static uint8_t USBD_TEMPLATE_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] = -{ - USB_LEN_DEV_QUALIFIER_DESC, - USB_DESC_TYPE_DEVICE_QUALIFIER, - 0x00, - 0x02, - 0x00, - 0x00, - 0x00, - 0x40, - 0x01, - 0x00, -}; - -/** - * @} - */ - -/** @defgroup USBD_TEMPLATE_Private_Functions - * @{ - */ - -/** - * @brief USBD_TEMPLATE_Init - * Initialize the TEMPLATE interface - * @param pdev: device instance - * @param cfgidx: Configuration index - * @retval status - */ -static uint8_t USBD_TEMPLATE_Init (USBD_HandleTypeDef *pdev, - uint8_t cfgidx) -{ - uint8_t ret = 0; - - - return ret; -} - -/** - * @brief USBD_TEMPLATE_Init - * DeInitialize the TEMPLATE layer - * @param pdev: device instance - * @param cfgidx: Configuration index - * @retval status - */ -static uint8_t USBD_TEMPLATE_DeInit (USBD_HandleTypeDef *pdev, - uint8_t cfgidx) -{ - - return USBD_OK; -} - -/** - * @brief USBD_TEMPLATE_Setup - * Handle the TEMPLATE specific requests - * @param pdev: instance - * @param req: usb requests - * @retval status - */ -static uint8_t USBD_TEMPLATE_Setup (USBD_HandleTypeDef *pdev, - USBD_SetupReqTypedef *req) -{ - USBD_StatusTypeDef ret = USBD_OK; - - switch (req->bmRequest & USB_REQ_TYPE_MASK) - { - case USB_REQ_TYPE_CLASS : - switch (req->bRequest) - { - default: - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - break; - } - break; - - case USB_REQ_TYPE_STANDARD: - switch (req->bRequest) - { - default: - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - break; - } - break; - - default: - USBD_CtlError (pdev, req); - ret = USBD_FAIL; - break; - } - - return ret; -} - - -/** - * @brief USBD_TEMPLATE_GetCfgDesc - * return configuration descriptor - * @param length : pointer data length - * @retval pointer to descriptor buffer - */ -static uint8_t *USBD_TEMPLATE_GetCfgDesc (uint16_t *length) -{ - *length = sizeof (USBD_TEMPLATE_CfgDesc); - return USBD_TEMPLATE_CfgDesc; -} - -/** -* @brief DeviceQualifierDescriptor -* return Device Qualifier descriptor -* @param length : pointer data length -* @retval pointer to descriptor buffer -*/ -uint8_t *USBD_TEMPLATE_DeviceQualifierDescriptor (uint16_t *length) -{ - *length = sizeof (USBD_TEMPLATE_DeviceQualifierDesc); - return USBD_TEMPLATE_DeviceQualifierDesc; -} - - -/** - * @brief USBD_TEMPLATE_DataIn - * handle data IN Stage - * @param pdev: device instance - * @param epnum: endpoint index - * @retval status - */ -static uint8_t USBD_TEMPLATE_DataIn (USBD_HandleTypeDef *pdev, - uint8_t epnum) -{ - - return USBD_OK; -} - -/** - * @brief USBD_TEMPLATE_EP0_RxReady - * handle EP0 Rx Ready event - * @param pdev: device instance - * @retval status - */ -static uint8_t USBD_TEMPLATE_EP0_RxReady (USBD_HandleTypeDef *pdev) -{ - - return USBD_OK; -} -/** - * @brief USBD_TEMPLATE_EP0_TxReady - * handle EP0 TRx Ready event - * @param pdev: device instance - * @retval status - */ -static uint8_t USBD_TEMPLATE_EP0_TxReady (USBD_HandleTypeDef *pdev) -{ - - return USBD_OK; -} -/** - * @brief USBD_TEMPLATE_SOF - * handle SOF event - * @param pdev: device instance - * @retval status - */ -static uint8_t USBD_TEMPLATE_SOF (USBD_HandleTypeDef *pdev) -{ - - return USBD_OK; -} -/** - * @brief USBD_TEMPLATE_IsoINIncomplete - * handle data ISO IN Incomplete event - * @param pdev: device instance - * @param epnum: endpoint index - * @retval status - */ -static uint8_t USBD_TEMPLATE_IsoINIncomplete (USBD_HandleTypeDef *pdev, uint8_t epnum) -{ - - return USBD_OK; -} -/** - * @brief USBD_TEMPLATE_IsoOutIncomplete - * handle data ISO OUT Incomplete event - * @param pdev: device instance - * @param epnum: endpoint index - * @retval status - */ -static uint8_t USBD_TEMPLATE_IsoOutIncomplete (USBD_HandleTypeDef *pdev, uint8_t epnum) -{ - - return USBD_OK; -} -/** - * @brief USBD_TEMPLATE_DataOut - * handle data OUT Stage - * @param pdev: device instance - * @param epnum: endpoint index - * @retval status - */ -static uint8_t USBD_TEMPLATE_DataOut (USBD_HandleTypeDef *pdev, - uint8_t epnum) -{ - - return USBD_OK; -} - -/** -* @brief DeviceQualifierDescriptor -* return Device Qualifier descriptor -* @param length : pointer data length -* @retval pointer to descriptor buffer -*/ -uint8_t *USBD_TEMPLATE_GetDeviceQualifierDesc (uint16_t *length) -{ - *length = sizeof (USBD_TEMPLATE_DeviceQualifierDesc); - return USBD_TEMPLATE_DeviceQualifierDesc; -} - -/** - * @} - */ - - -/** - * @} - */ - - -/** - * @} - */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_template.c + * @author MCD Application Team + * @brief This file provides the HID core functions. + * + * @verbatim + * + * =================================================================== + * TEMPLATE Class Description + * =================================================================== + * + * + * + * + * + * + * @note In HS mode and when the DMA is used, all variables and data structures + * dealing with the DMA during the transaction process should be 32-bit aligned. + * + * + * @endverbatim + * + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_template.h" +#include "usbd_ctlreq.h" + + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + + +/** @defgroup USBD_TEMPLATE + * @brief usbd core module + * @{ + */ + +/** @defgroup USBD_TEMPLATE_Private_TypesDefinitions + * @{ + */ +/** + * @} + */ + + +/** @defgroup USBD_TEMPLATE_Private_Defines + * @{ + */ + +/** + * @} + */ + + +/** @defgroup USBD_TEMPLATE_Private_Macros + * @{ + */ + +/** + * @} + */ + + + + +/** @defgroup USBD_TEMPLATE_Private_FunctionPrototypes + * @{ + */ + + +static uint8_t USBD_TEMPLATE_Init (USBD_HandleTypeDef *pdev, + uint8_t cfgidx); + +static uint8_t USBD_TEMPLATE_DeInit (USBD_HandleTypeDef *pdev, + uint8_t cfgidx); + +static uint8_t USBD_TEMPLATE_Setup (USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req); + +static uint8_t *USBD_TEMPLATE_GetCfgDesc (uint16_t *length); + +static uint8_t *USBD_TEMPLATE_GetDeviceQualifierDesc (uint16_t *length); + +static uint8_t USBD_TEMPLATE_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum); + +static uint8_t USBD_TEMPLATE_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum); + +static uint8_t USBD_TEMPLATE_EP0_RxReady (USBD_HandleTypeDef *pdev); + +static uint8_t USBD_TEMPLATE_EP0_TxReady (USBD_HandleTypeDef *pdev); + +static uint8_t USBD_TEMPLATE_SOF (USBD_HandleTypeDef *pdev); + +static uint8_t USBD_TEMPLATE_IsoINIncomplete (USBD_HandleTypeDef *pdev, uint8_t epnum); + +static uint8_t USBD_TEMPLATE_IsoOutIncomplete (USBD_HandleTypeDef *pdev, uint8_t epnum); + +/** + * @} + */ + +/** @defgroup USBD_TEMPLATE_Private_Variables + * @{ + */ + +USBD_ClassTypeDef USBD_TEMPLATE_ClassDriver = +{ + USBD_TEMPLATE_Init, + USBD_TEMPLATE_DeInit, + USBD_TEMPLATE_Setup, + USBD_TEMPLATE_EP0_TxReady, + USBD_TEMPLATE_EP0_RxReady, + USBD_TEMPLATE_DataIn, + USBD_TEMPLATE_DataOut, + USBD_TEMPLATE_SOF, + USBD_TEMPLATE_IsoINIncomplete, + USBD_TEMPLATE_IsoOutIncomplete, + USBD_TEMPLATE_GetCfgDesc, + USBD_TEMPLATE_GetCfgDesc, + USBD_TEMPLATE_GetCfgDesc, + USBD_TEMPLATE_GetDeviceQualifierDesc, +}; + +#if defined ( __ICCARM__ ) /*!< IAR Compiler */ + #pragma data_alignment=4 +#endif +/* USB TEMPLATE device Configuration Descriptor */ +static uint8_t USBD_TEMPLATE_CfgDesc[USB_TEMPLATE_CONFIG_DESC_SIZ] = +{ + 0x09, /* bLength: Configuation Descriptor size */ + USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION, /* bDescriptorType: Configuration */ + USB_TEMPLATE_CONFIG_DESC_SIZ, + /* wTotalLength: Bytes returned */ + 0x00, + 0x01, /*bNumInterfaces: 1 interface*/ + 0x01, /*bConfigurationValue: Configuration value*/ + 0x02, /*iConfiguration: Index of string descriptor describing the configuration*/ + 0xC0, /*bmAttributes: bus powered and Supports Remote Wakeup */ + 0x32, /*MaxPower 100 mA: this current is used for detecting Vbus*/ + /* 09 */ + + /********** Descriptor of TEMPLATE interface 0 Alternate setting 0 **************/ + +}; + +#if defined ( __ICCARM__ ) /*!< IAR Compiler */ + #pragma data_alignment=4 +#endif +/* USB Standard Device Descriptor */ +static uint8_t USBD_TEMPLATE_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC] = +{ + USB_LEN_DEV_QUALIFIER_DESC, + USB_DESC_TYPE_DEVICE_QUALIFIER, + 0x00, + 0x02, + 0x00, + 0x00, + 0x00, + 0x40, + 0x01, + 0x00, +}; + +/** + * @} + */ + +/** @defgroup USBD_TEMPLATE_Private_Functions + * @{ + */ + +/** + * @brief USBD_TEMPLATE_Init + * Initialize the TEMPLATE interface + * @param pdev: device instance + * @param cfgidx: Configuration index + * @retval status + */ +static uint8_t USBD_TEMPLATE_Init (USBD_HandleTypeDef *pdev, + uint8_t cfgidx) +{ + uint8_t ret = 0; + + + return ret; +} + +/** + * @brief USBD_TEMPLATE_Init + * DeInitialize the TEMPLATE layer + * @param pdev: device instance + * @param cfgidx: Configuration index + * @retval status + */ +static uint8_t USBD_TEMPLATE_DeInit (USBD_HandleTypeDef *pdev, + uint8_t cfgidx) +{ + + return USBD_OK; +} + +/** + * @brief USBD_TEMPLATE_Setup + * Handle the TEMPLATE specific requests + * @param pdev: instance + * @param req: usb requests + * @retval status + */ +static uint8_t USBD_TEMPLATE_Setup (USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req) +{ + USBD_StatusTypeDef ret = USBD_OK; + + switch (req->bmRequest & USB_REQ_TYPE_MASK) + { + case USB_REQ_TYPE_CLASS : + switch (req->bRequest) + { + default: + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + break; + } + break; + + case USB_REQ_TYPE_STANDARD: + switch (req->bRequest) + { + default: + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + break; + } + break; + + default: + USBD_CtlError (pdev, req); + ret = USBD_FAIL; + break; + } + + return ret; +} + + +/** + * @brief USBD_TEMPLATE_GetCfgDesc + * return configuration descriptor + * @param length : pointer data length + * @retval pointer to descriptor buffer + */ +static uint8_t *USBD_TEMPLATE_GetCfgDesc (uint16_t *length) +{ + *length = sizeof (USBD_TEMPLATE_CfgDesc); + return USBD_TEMPLATE_CfgDesc; +} + +/** +* @brief DeviceQualifierDescriptor +* return Device Qualifier descriptor +* @param length : pointer data length +* @retval pointer to descriptor buffer +*/ +uint8_t *USBD_TEMPLATE_DeviceQualifierDescriptor (uint16_t *length) +{ + *length = sizeof (USBD_TEMPLATE_DeviceQualifierDesc); + return USBD_TEMPLATE_DeviceQualifierDesc; +} + + +/** + * @brief USBD_TEMPLATE_DataIn + * handle data IN Stage + * @param pdev: device instance + * @param epnum: endpoint index + * @retval status + */ +static uint8_t USBD_TEMPLATE_DataIn (USBD_HandleTypeDef *pdev, + uint8_t epnum) +{ + + return USBD_OK; +} + +/** + * @brief USBD_TEMPLATE_EP0_RxReady + * handle EP0 Rx Ready event + * @param pdev: device instance + * @retval status + */ +static uint8_t USBD_TEMPLATE_EP0_RxReady (USBD_HandleTypeDef *pdev) +{ + + return USBD_OK; +} +/** + * @brief USBD_TEMPLATE_EP0_TxReady + * handle EP0 TRx Ready event + * @param pdev: device instance + * @retval status + */ +static uint8_t USBD_TEMPLATE_EP0_TxReady (USBD_HandleTypeDef *pdev) +{ + + return USBD_OK; +} +/** + * @brief USBD_TEMPLATE_SOF + * handle SOF event + * @param pdev: device instance + * @retval status + */ +static uint8_t USBD_TEMPLATE_SOF (USBD_HandleTypeDef *pdev) +{ + + return USBD_OK; +} +/** + * @brief USBD_TEMPLATE_IsoINIncomplete + * handle data ISO IN Incomplete event + * @param pdev: device instance + * @param epnum: endpoint index + * @retval status + */ +static uint8_t USBD_TEMPLATE_IsoINIncomplete (USBD_HandleTypeDef *pdev, uint8_t epnum) +{ + + return USBD_OK; +} +/** + * @brief USBD_TEMPLATE_IsoOutIncomplete + * handle data ISO OUT Incomplete event + * @param pdev: device instance + * @param epnum: endpoint index + * @retval status + */ +static uint8_t USBD_TEMPLATE_IsoOutIncomplete (USBD_HandleTypeDef *pdev, uint8_t epnum) +{ + + return USBD_OK; +} +/** + * @brief USBD_TEMPLATE_DataOut + * handle data OUT Stage + * @param pdev: device instance + * @param epnum: endpoint index + * @retval status + */ +static uint8_t USBD_TEMPLATE_DataOut (USBD_HandleTypeDef *pdev, + uint8_t epnum) +{ + + return USBD_OK; +} + +/** +* @brief DeviceQualifierDescriptor +* return Device Qualifier descriptor +* @param length : pointer data length +* @retval pointer to descriptor buffer +*/ +uint8_t *USBD_TEMPLATE_GetDeviceQualifierDesc (uint16_t *length) +{ + *length = sizeof (USBD_TEMPLATE_DeviceQualifierDesc); + return USBD_TEMPLATE_DeviceQualifierDesc; +} + +/** + * @} + */ + + +/** + * @} + */ + + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_conf_template.h b/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_conf_template.h index 7d91fa4068..434d7ac6b4 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_conf_template.h +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_conf_template.h @@ -1,162 +1,163 @@ -/** - ****************************************************************************** - * @file usbd_conf_template.h - * @author MCD Application Team - * @brief Header file for the usbd_conf_template.c file - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USBD_CONF_TEMPLATE_H -#define __USBD_CONF_TEMPLATE_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "stm32fxxx.h" /* replace 'stm32xxx' with your HAL driver header filename, ex: stm32f4xx.h */ -#include -#include -#include - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - -/** @defgroup USBD_CONF - * @brief USB device low level driver configuration file - * @{ - */ - -/** @defgroup USBD_CONF_Exported_Defines - * @{ - */ - -#define USBD_MAX_NUM_INTERFACES 1U -#define USBD_MAX_NUM_CONFIGURATION 1U -#define USBD_MAX_STR_DESC_SIZ 0x100U -#define USBD_SUPPORT_USER_STRING 0U -#define USBD_SELF_POWERED 1U -#define USBD_DEBUG_LEVEL 2U - -/* MSC Class Config */ -#define MSC_MEDIA_PACKET 8192U - -/* CDC Class Config */ -#define USBD_CDC_INTERVAL 2000U - - /* DFU Class Config */ -#define USBD_DFU_MAX_ITF_NUM 1U -#define USBD_DFU_XFERS_IZE 1024U - - /* AUDIO Class Config */ -#define USBD_AUDIO_FREQ 22100U - -/** @defgroup USBD_Exported_Macros - * @{ - */ - - /* Memory management macros */ -#define USBD_malloc malloc -#define USBD_free free -#define USBD_memset memset -#define USBD_memcpy memcpy - -/* DEBUG macros */ -#if (USBD_DEBUG_LEVEL > 0U) -#define USBD_UsrLog(...) do { \ - printf(__VA_ARGS__); \ - printf("\n"); \ -} while (0) -#else -#define USBD_UsrLog(...) do {} while (0) -#endif - -#if (USBD_DEBUG_LEVEL > 1U) - -#define USBD_ErrLog(...) do { \ - printf("ERROR: ") ; \ - printf(__VA_ARGS__); \ - printf("\n"); \ -} while (0) -#else -#define USBD_ErrLog(...) do {} while (0) -#endif - -#if (USBD_DEBUG_LEVEL > 2U) -#define USBD_DbgLog(...) do { \ - printf("DEBUG : ") ; \ - printf(__VA_ARGS__); \ - printf("\n"); \ -} while (0) -#else -#define USBD_DbgLog(...) do {} while (0) -#endif - -/** - * @} - */ - - - -/** - * @} - */ - - -/** @defgroup USBD_CONF_Exported_Types - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBD_CONF_Exported_Macros - * @{ - */ -/** - * @} - */ - -/** @defgroup USBD_CONF_Exported_Variables - * @{ - */ -/** - * @} - */ - -/** @defgroup USBD_CONF_Exported_FunctionsPrototype - * @{ - */ -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USBD_CONF_TEMPLATE_H */ - - -/** - * @} - */ - -/** - * @} - */ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_conf_template.h + * @author MCD Application Team + * @brief Header file for the usbd_conf_template.c file + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USBD_CONF_TEMPLATE_H +#define __USBD_CONF_TEMPLATE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +/* replace 'stm32xxx' with your HAL driver header filename, ex: stm32f4xx.h */ +#include "stm32fxxx.h" +#include +#include +#include + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + +/** @defgroup USBD_CONF + * @brief USB device low level driver configuration file + * @{ + */ + +/** @defgroup USBD_CONF_Exported_Defines + * @{ + */ + +#define USBD_MAX_NUM_INTERFACES 1U +#define USBD_MAX_NUM_CONFIGURATION 1U +#define USBD_MAX_STR_DESC_SIZ 0x100U +#define USBD_SUPPORT_USER_STRING 0U +#define USBD_SELF_POWERED 1U +#define USBD_DEBUG_LEVEL 2U + +/* MSC Class Config */ +#define MSC_MEDIA_PACKET 8192U + +/* CDC Class Config */ +#define USBD_CDC_INTERVAL 2000U + +/* DFU Class Config */ +#define USBD_DFU_MAX_ITF_NUM 1U +#define USBD_DFU_XFERS_IZE 1024U + +/* AUDIO Class Config */ +#define USBD_AUDIO_FREQ 22100U + +/** @defgroup USBD_Exported_Macros + * @{ + */ + +/* Memory management macros */ +#define USBD_malloc malloc +#define USBD_free free +#define USBD_memset memset +#define USBD_memcpy memcpy + +/* DEBUG macros */ +#if (USBD_DEBUG_LEVEL > 0U) +#define USBD_UsrLog(...) do { \ + printf(__VA_ARGS__); \ + printf("\n"); \ + } while (0) +#else +#define USBD_UsrLog(...) do {} while (0) +#endif + +#if (USBD_DEBUG_LEVEL > 1U) + +#define USBD_ErrLog(...) do { \ + printf("ERROR: "); \ + printf(__VA_ARGS__); \ + printf("\n"); \ + } while (0) +#else +#define USBD_ErrLog(...) do {} while (0) +#endif + +#if (USBD_DEBUG_LEVEL > 2U) +#define USBD_DbgLog(...) do { \ + printf("DEBUG : "); \ + printf(__VA_ARGS__); \ + printf("\n"); \ + } while (0) +#else +#define USBD_DbgLog(...) do {} while (0) +#endif + +/** + * @} + */ + +/** + * @} + */ + +/** @defgroup USBD_CONF_Exported_Types + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_CONF_Exported_Macros + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_CONF_Exported_Variables + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_CONF_Exported_FunctionsPrototype + * @{ + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USBD_CONF_TEMPLATE_H */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_core.h b/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_core.h index 31ec3b026d..b7ff2b1688 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_core.h +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_core.h @@ -1,161 +1,198 @@ -/** - ****************************************************************************** - * @file usbd_core.h - * @author MCD Application Team - * @brief Header file for usbd_core.c file - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USBD_CORE_H -#define __USBD_CORE_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_conf.h" -#include "usbd_def.h" -#include "usbd_ioreq.h" -#include "usbd_ctlreq.h" - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - -/** @defgroup USBD_CORE - * @brief This file is the Header file for usbd_core.c file - * @{ - */ - - -/** @defgroup USBD_CORE_Exported_Defines - * @{ - */ -#ifndef USBD_DEBUG_LEVEL -#define USBD_DEBUG_LEVEL 0U -#endif /* USBD_DEBUG_LEVEL */ -/** - * @} - */ - - -/** @defgroup USBD_CORE_Exported_TypesDefinitions - * @{ - */ - - -/** - * @} - */ - - - -/** @defgroup USBD_CORE_Exported_Macros - * @{ - */ - -/** - * @} - */ - -/** @defgroup USBD_CORE_Exported_Variables - * @{ - */ -#define USBD_SOF USBD_LL_SOF -/** - * @} - */ - -/** @defgroup USBD_CORE_Exported_FunctionsPrototype - * @{ - */ -USBD_StatusTypeDef USBD_Init(USBD_HandleTypeDef *pdev, USBD_DescriptorsTypeDef *pdesc, uint8_t id); -USBD_StatusTypeDef USBD_DeInit(USBD_HandleTypeDef *pdev); -USBD_StatusTypeDef USBD_Start (USBD_HandleTypeDef *pdev); -USBD_StatusTypeDef USBD_Stop (USBD_HandleTypeDef *pdev); -USBD_StatusTypeDef USBD_RegisterClass(USBD_HandleTypeDef *pdev, USBD_ClassTypeDef *pclass); - -USBD_StatusTypeDef USBD_RunTestMode (USBD_HandleTypeDef *pdev); -USBD_StatusTypeDef USBD_SetClassConfig(USBD_HandleTypeDef *pdev, uint8_t cfgidx); -USBD_StatusTypeDef USBD_ClrClassConfig(USBD_HandleTypeDef *pdev, uint8_t cfgidx); - -USBD_StatusTypeDef USBD_LL_SetupStage(USBD_HandleTypeDef *pdev, uint8_t *psetup); -USBD_StatusTypeDef USBD_LL_DataOutStage(USBD_HandleTypeDef *pdev , uint8_t epnum, uint8_t *pdata); -USBD_StatusTypeDef USBD_LL_DataInStage(USBD_HandleTypeDef *pdev , uint8_t epnum, uint8_t *pdata); - -USBD_StatusTypeDef USBD_LL_Reset(USBD_HandleTypeDef *pdev); -USBD_StatusTypeDef USBD_LL_SetSpeed(USBD_HandleTypeDef *pdev, USBD_SpeedTypeDef speed); -USBD_StatusTypeDef USBD_LL_Suspend(USBD_HandleTypeDef *pdev); -USBD_StatusTypeDef USBD_LL_Resume(USBD_HandleTypeDef *pdev); - -USBD_StatusTypeDef USBD_LL_SOF(USBD_HandleTypeDef *pdev); -USBD_StatusTypeDef USBD_LL_IsoINIncomplete(USBD_HandleTypeDef *pdev, uint8_t epnum); -USBD_StatusTypeDef USBD_LL_IsoOUTIncomplete(USBD_HandleTypeDef *pdev, uint8_t epnum); - -USBD_StatusTypeDef USBD_LL_DevConnected(USBD_HandleTypeDef *pdev); -USBD_StatusTypeDef USBD_LL_DevDisconnected(USBD_HandleTypeDef *pdev); - -/* USBD Low Level Driver */ -USBD_StatusTypeDef USBD_LL_Init (USBD_HandleTypeDef *pdev); -USBD_StatusTypeDef USBD_LL_DeInit (USBD_HandleTypeDef *pdev); -USBD_StatusTypeDef USBD_LL_Start(USBD_HandleTypeDef *pdev); -USBD_StatusTypeDef USBD_LL_Stop (USBD_HandleTypeDef *pdev); -USBD_StatusTypeDef USBD_LL_OpenEP (USBD_HandleTypeDef *pdev, - uint8_t ep_addr, - uint8_t ep_type, - uint16_t ep_mps); - -USBD_StatusTypeDef USBD_LL_CloseEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr); -USBD_StatusTypeDef USBD_LL_FlushEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr); -USBD_StatusTypeDef USBD_LL_StallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr); -USBD_StatusTypeDef USBD_LL_ClearStallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr); -uint8_t USBD_LL_IsStallEP (USBD_HandleTypeDef *pdev, uint8_t ep_addr); -USBD_StatusTypeDef USBD_LL_SetUSBAddress (USBD_HandleTypeDef *pdev, uint8_t dev_addr); -USBD_StatusTypeDef USBD_LL_Transmit (USBD_HandleTypeDef *pdev, - uint8_t ep_addr, - uint8_t *pbuf, - uint16_t size); - -USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev, - uint8_t ep_addr, - uint8_t *pbuf, - uint16_t size); - -uint32_t USBD_LL_GetRxDataSize (USBD_HandleTypeDef *pdev, uint8_t ep_addr); -void USBD_LL_Delay (uint32_t Delay); - -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USBD_CORE_H */ - -/** - * @} - */ - -/** -* @} -*/ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - - - +/** + ****************************************************************************** + * @file usbd_core.h + * @author MCD Application Team + * @brief Header file for usbd_core.c file + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USBD_CORE_H +#define __USBD_CORE_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_conf.h" +#include "usbd_def.h" +#include "usbd_ioreq.h" +#include "usbd_ctlreq.h" + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + +/** @defgroup USBD_CORE + * @brief This file is the Header file for usbd_core.c file + * @{ + */ + +/** @defgroup USBD_CORE_Exported_Defines + * @{ + */ + +#ifndef USBD_DEBUG_LEVEL +#define USBD_DEBUG_LEVEL 0U +#endif /* USBD_DEBUG_LEVEL */ + +/** + * @} + */ + +/** @defgroup USBD_CORE_Exported_TypesDefinitions + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_CORE_Exported_Macros + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_CORE_Exported_Variables + * @{ + */ + +#define USBD_SOF USBD_LL_SOF + +/** + * @} + */ + +/** @defgroup USBD_CORE_Exported_FunctionsPrototype + * @{ + */ + +USBD_StatusTypeDef USBD_Init(USBD_HandleTypeDef *pdev, + USBD_DescriptorsTypeDef *pdesc, + uint8_t id); + +USBD_StatusTypeDef USBD_DeInit(USBD_HandleTypeDef *pdev); + +USBD_StatusTypeDef USBD_Start(USBD_HandleTypeDef *pdev); + +USBD_StatusTypeDef USBD_Stop(USBD_HandleTypeDef *pdev); + +USBD_StatusTypeDef USBD_RegisterClass(USBD_HandleTypeDef *pdev, + USBD_ClassTypeDef *pclass); + +USBD_StatusTypeDef USBD_RunTestMode(USBD_HandleTypeDef *pdev); + +USBD_StatusTypeDef USBD_SetClassConfig(USBD_HandleTypeDef *pdev, + uint8_t cfgidx); + +USBD_StatusTypeDef USBD_ClrClassConfig(USBD_HandleTypeDef *pdev, + uint8_t cfgidx); + +USBD_StatusTypeDef USBD_LL_SetupStage(USBD_HandleTypeDef *pdev, + uint8_t *psetup); + +USBD_StatusTypeDef USBD_LL_DataOutStage(USBD_HandleTypeDef *pdev, + uint8_t epnum, + uint8_t *pdata); + +USBD_StatusTypeDef USBD_LL_DataInStage(USBD_HandleTypeDef *pdev, + uint8_t epnum, + uint8_t *pdata); + +USBD_StatusTypeDef USBD_LL_Reset(USBD_HandleTypeDef *pdev); + +USBD_StatusTypeDef USBD_LL_SetSpeed(USBD_HandleTypeDef *pdev, + USBD_SpeedTypeDef speed); + +USBD_StatusTypeDef USBD_LL_Suspend(USBD_HandleTypeDef *pdev); + +USBD_StatusTypeDef USBD_LL_Resume(USBD_HandleTypeDef *pdev); + +USBD_StatusTypeDef USBD_LL_SOF(USBD_HandleTypeDef *pdev); + +USBD_StatusTypeDef USBD_LL_IsoINIncomplete(USBD_HandleTypeDef *pdev, + uint8_t epnum); + +USBD_StatusTypeDef USBD_LL_IsoOUTIncomplete(USBD_HandleTypeDef *pdev, + uint8_t epnum); + +USBD_StatusTypeDef USBD_LL_DevConnected(USBD_HandleTypeDef *pdev); + +USBD_StatusTypeDef USBD_LL_DevDisconnected(USBD_HandleTypeDef *pdev); + +/* USBD Low Level Driver */ +USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev); + +USBD_StatusTypeDef USBD_LL_DeInit(USBD_HandleTypeDef *pdev); + +USBD_StatusTypeDef USBD_LL_Start(USBD_HandleTypeDef *pdev); + +USBD_StatusTypeDef USBD_LL_Stop(USBD_HandleTypeDef *pdev); + +USBD_StatusTypeDef USBD_LL_OpenEP(USBD_HandleTypeDef *pdev, + uint8_t ep_addr, + uint8_t ep_type, + uint16_t ep_mps); + +USBD_StatusTypeDef USBD_LL_CloseEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr); + +USBD_StatusTypeDef USBD_LL_FlushEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr); + +USBD_StatusTypeDef USBD_LL_StallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr); + +USBD_StatusTypeDef USBD_LL_ClearStallEP(USBD_HandleTypeDef *pdev, + uint8_t ep_addr); + +uint8_t USBD_LL_IsStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr); + +USBD_StatusTypeDef USBD_LL_SetUSBAddress(USBD_HandleTypeDef *pdev, + uint8_t dev_addr); + +USBD_StatusTypeDef USBD_LL_Transmit(USBD_HandleTypeDef *pdev, + uint8_t ep_addr, + uint8_t *pbuf, + uint16_t size); + +USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev, + uint8_t ep_addr, + uint8_t *pbuf, + uint16_t size); + +uint32_t USBD_LL_GetRxDataSize(USBD_HandleTypeDef *pdev, uint8_t ep_addr); + +void USBD_LL_Delay(uint32_t Delay); + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USBD_CORE_H */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_ctlreq.h b/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_ctlreq.h index 9fa2873ad2..c882b07166 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_ctlreq.h +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_ctlreq.h @@ -1,105 +1,109 @@ -/** - ****************************************************************************** - * @file usbd_req.h - * @author MCD Application Team - * @brief Header file for the usbd_req.c file - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_REQUEST_H -#define __USB_REQUEST_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_def.h" - - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - -/** @defgroup USBD_REQ - * @brief header file for the usbd_req.c file - * @{ - */ - -/** @defgroup USBD_REQ_Exported_Defines - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBD_REQ_Exported_Types - * @{ - */ -/** - * @} - */ - - - -/** @defgroup USBD_REQ_Exported_Macros - * @{ - */ -/** - * @} - */ - -/** @defgroup USBD_REQ_Exported_Variables - * @{ - */ -/** - * @} - */ - -/** @defgroup USBD_REQ_Exported_FunctionsPrototype - * @{ - */ - -USBD_StatusTypeDef USBD_StdDevReq (USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req); -USBD_StatusTypeDef USBD_StdItfReq (USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req); -USBD_StatusTypeDef USBD_StdEPReq (USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req); - - -void USBD_CtlError (USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req); - -void USBD_ParseSetupRequest (USBD_SetupReqTypedef *req, uint8_t *pdata); - -void USBD_GetString (uint8_t *desc, uint8_t *unicode, uint16_t *len); -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USB_REQUEST_H */ - -/** - * @} - */ - -/** -* @} -*/ - - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_req.h + * @author MCD Application Team + * @brief Header file for the usbd_req.c file + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USB_REQUEST_H +#define __USB_REQUEST_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_def.h" + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + +/** @defgroup USBD_REQ + * @brief header file for the usbd_req.c file + * @{ + */ + +/** @defgroup USBD_REQ_Exported_Defines + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_REQ_Exported_Types + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_REQ_Exported_Macros + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_REQ_Exported_Variables + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_REQ_Exported_FunctionsPrototype + * @{ + */ + +USBD_StatusTypeDef USBD_StdDevReq(USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req); + +USBD_StatusTypeDef USBD_StdItfReq(USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req); + +USBD_StatusTypeDef USBD_StdEPReq(USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req); + +void USBD_CtlError(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req); + +void USBD_ParseSetupRequest(USBD_SetupReqTypedef *req, uint8_t *pdata); + +void USBD_GetString(uint8_t *desc, uint8_t *unicode, uint16_t *len); + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USB_REQUEST_H */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_def.h b/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_def.h index b1e7968d2d..a56c45eff6 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_def.h +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_def.h @@ -1,342 +1,341 @@ -/** - ****************************************************************************** - * @file usbd_def.h - * @author MCD Application Team - * @brief General defines for the usb device library - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USBD_DEF_H -#define __USBD_DEF_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_conf.h" - -/** @addtogroup STM32_USBD_DEVICE_LIBRARY - * @{ - */ - -/** @defgroup USB_DEF - * @brief general defines for the usb device library file - * @{ - */ - -/** @defgroup USB_DEF_Exported_Defines - * @{ - */ - -#ifndef NULL -#define NULL 0U -#endif /* NULL */ - -#ifndef USBD_MAX_NUM_INTERFACES -#define USBD_MAX_NUM_INTERFACES 1U -#endif /* USBD_MAX_NUM_CONFIGURATION */ - -#ifndef USBD_MAX_NUM_CONFIGURATION -#define USBD_MAX_NUM_CONFIGURATION 1U -#endif /* USBD_MAX_NUM_CONFIGURATION */ - -#ifndef USBD_LPM_ENABLED -#define USBD_LPM_ENABLED 0U -#endif /* USBD_LPM_ENABLED */ - -#ifndef USBD_SELF_POWERED -#define USBD_SELF_POWERED 1U -#endif /*USBD_SELF_POWERED */ - -#ifndef USBD_SUPPORT_USER_STRING -#define USBD_SUPPORT_USER_STRING 0U -#endif /* USBD_SUPPORT_USER_STRING */ - -#define USB_LEN_DEV_QUALIFIER_DESC 0x0AU -#define USB_LEN_DEV_DESC 0x12U -#define USB_LEN_CFG_DESC 0x09U -#define USB_LEN_IF_DESC 0x09U -#define USB_LEN_EP_DESC 0x07U -#define USB_LEN_OTG_DESC 0x03U -#define USB_LEN_LANGID_STR_DESC 0x04U -#define USB_LEN_OTHER_SPEED_DESC_SIZ 0x09U - -#define USBD_IDX_LANGID_STR 0x00U -#define USBD_IDX_MFC_STR 0x01U -#define USBD_IDX_PRODUCT_STR 0x02U -#define USBD_IDX_SERIAL_STR 0x03U -#define USBD_IDX_CONFIG_STR 0x04U -#define USBD_IDX_INTERFACE_STR 0x05U - -#define USB_REQ_TYPE_STANDARD 0x00U -#define USB_REQ_TYPE_CLASS 0x20U -#define USB_REQ_TYPE_VENDOR 0x40U -#define USB_REQ_TYPE_MASK 0x60U - -#define USB_REQ_RECIPIENT_DEVICE 0x00U -#define USB_REQ_RECIPIENT_INTERFACE 0x01U -#define USB_REQ_RECIPIENT_ENDPOINT 0x02U -#define USB_REQ_RECIPIENT_MASK 0x03U - -#define USB_REQ_GET_STATUS 0x00U -#define USB_REQ_CLEAR_FEATURE 0x01U -#define USB_REQ_SET_FEATURE 0x03U -#define USB_REQ_SET_ADDRESS 0x05U -#define USB_REQ_GET_DESCRIPTOR 0x06U -#define USB_REQ_SET_DESCRIPTOR 0x07U -#define USB_REQ_GET_CONFIGURATION 0x08U -#define USB_REQ_SET_CONFIGURATION 0x09U -#define USB_REQ_GET_INTERFACE 0x0AU -#define USB_REQ_SET_INTERFACE 0x0BU -#define USB_REQ_SYNCH_FRAME 0x0CU - -#define USB_DESC_TYPE_DEVICE 0x01U -#define USB_DESC_TYPE_CONFIGURATION 0x02U -#define USB_DESC_TYPE_STRING 0x03U -#define USB_DESC_TYPE_INTERFACE 0x04U -#define USB_DESC_TYPE_ENDPOINT 0x05U -#define USB_DESC_TYPE_DEVICE_QUALIFIER 0x06U -#define USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION 0x07U -#define USB_DESC_TYPE_BOS 0x0FU - -#define USB_CONFIG_REMOTE_WAKEUP 0x02U -#define USB_CONFIG_SELF_POWERED 0x01U - -#define USB_FEATURE_EP_HALT 0x00U -#define USB_FEATURE_REMOTE_WAKEUP 0x01U -#define USB_FEATURE_TEST_MODE 0x02U - -#define USB_DEVICE_CAPABITY_TYPE 0x10U - -#define USB_HS_MAX_PACKET_SIZE 512U -#define USB_FS_MAX_PACKET_SIZE 64U -#define USB_MAX_EP0_SIZE 64U - -/* Device Status */ -#define USBD_STATE_DEFAULT 0x01U -#define USBD_STATE_ADDRESSED 0x02U -#define USBD_STATE_CONFIGURED 0x03U -#define USBD_STATE_SUSPENDED 0x04U - - -/* EP0 State */ -#define USBD_EP0_IDLE 0x00U -#define USBD_EP0_SETUP 0x01U -#define USBD_EP0_DATA_IN 0x02U -#define USBD_EP0_DATA_OUT 0x03U -#define USBD_EP0_STATUS_IN 0x04U -#define USBD_EP0_STATUS_OUT 0x05U -#define USBD_EP0_STALL 0x06U - -#define USBD_EP_TYPE_CTRL 0x00U -#define USBD_EP_TYPE_ISOC 0x01U -#define USBD_EP_TYPE_BULK 0x02U -#define USBD_EP_TYPE_INTR 0x03U - - -/** - * @} - */ - - -/** @defgroup USBD_DEF_Exported_TypesDefinitions - * @{ - */ - -typedef struct usb_setup_req -{ - - uint8_t bmRequest; - uint8_t bRequest; - uint16_t wValue; - uint16_t wIndex; - uint16_t wLength; -}USBD_SetupReqTypedef; - -struct _USBD_HandleTypeDef; - -typedef struct _Device_cb -{ - uint8_t (*Init) (struct _USBD_HandleTypeDef *pdev , uint8_t cfgidx); - uint8_t (*DeInit) (struct _USBD_HandleTypeDef *pdev , uint8_t cfgidx); - /* Control Endpoints*/ - uint8_t (*Setup) (struct _USBD_HandleTypeDef *pdev , USBD_SetupReqTypedef *req); - uint8_t (*EP0_TxSent) (struct _USBD_HandleTypeDef *pdev ); - uint8_t (*EP0_RxReady) (struct _USBD_HandleTypeDef *pdev ); - /* Class Specific Endpoints*/ - uint8_t (*DataIn) (struct _USBD_HandleTypeDef *pdev , uint8_t epnum); - uint8_t (*DataOut) (struct _USBD_HandleTypeDef *pdev , uint8_t epnum); - uint8_t (*SOF) (struct _USBD_HandleTypeDef *pdev); - uint8_t (*IsoINIncomplete) (struct _USBD_HandleTypeDef *pdev , uint8_t epnum); - uint8_t (*IsoOUTIncomplete) (struct _USBD_HandleTypeDef *pdev , uint8_t epnum); - - uint8_t *(*GetHSConfigDescriptor)(uint16_t *length); - uint8_t *(*GetFSConfigDescriptor)(uint16_t *length); - uint8_t *(*GetOtherSpeedConfigDescriptor)(uint16_t *length); - uint8_t *(*GetDeviceQualifierDescriptor)(uint16_t *length); -#if (USBD_SUPPORT_USER_STRING == 1U) - uint8_t *(*GetUsrStrDescriptor)(struct _USBD_HandleTypeDef *pdev ,uint8_t index, uint16_t *length); -#endif - -} USBD_ClassTypeDef; - -/* Following USB Device Speed */ -typedef enum -{ - USBD_SPEED_HIGH = 0U, - USBD_SPEED_FULL = 1U, - USBD_SPEED_LOW = 2U, -}USBD_SpeedTypeDef; - -/* Following USB Device status */ -typedef enum { - USBD_OK = 0U, - USBD_BUSY, - USBD_FAIL, -}USBD_StatusTypeDef; - -/* USB Device descriptors structure */ -typedef struct -{ - uint8_t *(*GetDeviceDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length); - uint8_t *(*GetLangIDStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length); - uint8_t *(*GetManufacturerStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length); - uint8_t *(*GetProductStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length); - uint8_t *(*GetSerialStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length); - uint8_t *(*GetConfigurationStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length); - uint8_t *(*GetInterfaceStrDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length); -#if (USBD_LPM_ENABLED == 1U) - uint8_t *(*GetBOSDescriptor)( USBD_SpeedTypeDef speed , uint16_t *length); -#endif -} USBD_DescriptorsTypeDef; - -/* USB Device handle structure */ -typedef struct -{ - uint32_t status; - uint32_t is_used; - uint32_t total_length; - uint32_t rem_length; - uint32_t maxpacket; -} USBD_EndpointTypeDef; - -/* USB Device handle structure */ -typedef struct _USBD_HandleTypeDef -{ - uint8_t id; - uint32_t dev_config; - uint32_t dev_default_config; - uint32_t dev_config_status; - USBD_SpeedTypeDef dev_speed; - USBD_EndpointTypeDef ep_in[15]; - USBD_EndpointTypeDef ep_out[15]; - uint32_t ep0_state; - uint32_t ep0_data_len; - uint8_t dev_state; - uint8_t dev_old_state; - uint8_t dev_address; - uint8_t dev_connection_status; - uint8_t dev_test_mode; - uint32_t dev_remote_wakeup; - - USBD_SetupReqTypedef request; - USBD_DescriptorsTypeDef *pDesc; - USBD_ClassTypeDef *pClass; - void *pClassData; - void *pUserData; - void *pData; -} USBD_HandleTypeDef; - -/** - * @} - */ - - - -/** @defgroup USBD_DEF_Exported_Macros - * @{ - */ -#define SWAPBYTE(addr) (((uint16_t)(*((uint8_t *)(addr)))) + \ - (((uint16_t)(*(((uint8_t *)(addr)) + 1U))) << 8U)) - -#define LOBYTE(x) ((uint8_t)(x & 0x00FFU)) -#define HIBYTE(x) ((uint8_t)((x & 0xFF00U) >> 8U)) -#define MIN(a, b) (((a) < (b)) ? (a) : (b)) -#define MAX(a, b) (((a) > (b)) ? (a) : (b)) - - -#if defined ( __GNUC__ ) - #ifndef __weak - #define __weak __attribute__((weak)) - #endif /* __weak */ - #ifndef __packed - #define __packed __attribute__((__packed__)) - #endif /* __packed */ -#endif /* __GNUC__ */ - - -/* In HS mode and when the DMA is used, all variables and data structures dealing - with the DMA during the transaction process should be 4-bytes aligned */ - -#if defined (__GNUC__) /* GNU Compiler */ - #define __ALIGN_END __attribute__ ((aligned (4))) - #define __ALIGN_BEGIN -#else - #define __ALIGN_END - #if defined (__CC_ARM) /* ARM Compiler */ - #define __ALIGN_BEGIN __align(4) - #elif defined (__ICCARM__) /* IAR Compiler */ - #define __ALIGN_BEGIN - #elif defined (__TASKING__) /* TASKING Compiler */ - #define __ALIGN_BEGIN __align(4) - #endif /* __CC_ARM */ -#endif /* __GNUC__ */ - - -/** - * @} - */ - -/** @defgroup USBD_DEF_Exported_Variables - * @{ - */ - -/** - * @} - */ - -/** @defgroup USBD_DEF_Exported_FunctionsPrototype - * @{ - */ - -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USBD_DEF_H */ - -/** - * @} - */ - -/** -* @} -*/ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_def.h + * @author MCD Application Team + * @brief General defines for the usb device library + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USBD_DEF_H +#define __USBD_DEF_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_conf.h" + +/** @addtogroup STM32_USBD_DEVICE_LIBRARY + * @{ + */ + +/** @defgroup USB_DEF + * @brief general defines for the usb device library file + * @{ + */ + +/** @defgroup USB_DEF_Exported_Defines + * @{ + */ + +#ifndef NULL +#define NULL 0U +#endif /* NULL */ + +#ifndef USBD_MAX_NUM_INTERFACES +#define USBD_MAX_NUM_INTERFACES 1U +#endif /* USBD_MAX_NUM_CONFIGURATION */ + +#ifndef USBD_MAX_NUM_CONFIGURATION +#define USBD_MAX_NUM_CONFIGURATION 1U +#endif /* USBD_MAX_NUM_CONFIGURATION */ + +#ifndef USBD_LPM_ENABLED +#define USBD_LPM_ENABLED 0U +#endif /* USBD_LPM_ENABLED */ + +#ifndef USBD_SELF_POWERED +#define USBD_SELF_POWERED 1U +#endif /*USBD_SELF_POWERED */ + +#ifndef USBD_SUPPORT_USER_STRING +#define USBD_SUPPORT_USER_STRING 0U +#endif /* USBD_SUPPORT_USER_STRING */ + +#define USB_LEN_DEV_QUALIFIER_DESC 0x0AU +#define USB_LEN_DEV_DESC 0x12U +#define USB_LEN_CFG_DESC 0x09U +#define USB_LEN_IF_DESC 0x09U +#define USB_LEN_EP_DESC 0x07U +#define USB_LEN_OTG_DESC 0x03U +#define USB_LEN_LANGID_STR_DESC 0x04U +#define USB_LEN_OTHER_SPEED_DESC_SIZ 0x09U + +#define USBD_IDX_LANGID_STR 0x00U +#define USBD_IDX_MFC_STR 0x01U +#define USBD_IDX_PRODUCT_STR 0x02U +#define USBD_IDX_SERIAL_STR 0x03U +#define USBD_IDX_CONFIG_STR 0x04U +#define USBD_IDX_INTERFACE_STR 0x05U + +#define USB_REQ_TYPE_STANDARD 0x00U +#define USB_REQ_TYPE_CLASS 0x20U +#define USB_REQ_TYPE_VENDOR 0x40U +#define USB_REQ_TYPE_MASK 0x60U + +#define USB_REQ_RECIPIENT_DEVICE 0x00U +#define USB_REQ_RECIPIENT_INTERFACE 0x01U +#define USB_REQ_RECIPIENT_ENDPOINT 0x02U +#define USB_REQ_RECIPIENT_MASK 0x03U + +#define USB_REQ_GET_STATUS 0x00U +#define USB_REQ_CLEAR_FEATURE 0x01U +#define USB_REQ_SET_FEATURE 0x03U +#define USB_REQ_SET_ADDRESS 0x05U +#define USB_REQ_GET_DESCRIPTOR 0x06U +#define USB_REQ_SET_DESCRIPTOR 0x07U +#define USB_REQ_GET_CONFIGURATION 0x08U +#define USB_REQ_SET_CONFIGURATION 0x09U +#define USB_REQ_GET_INTERFACE 0x0AU +#define USB_REQ_SET_INTERFACE 0x0BU +#define USB_REQ_SYNCH_FRAME 0x0CU + +#define USB_DESC_TYPE_DEVICE 0x01U +#define USB_DESC_TYPE_CONFIGURATION 0x02U +#define USB_DESC_TYPE_STRING 0x03U +#define USB_DESC_TYPE_INTERFACE 0x04U +#define USB_DESC_TYPE_ENDPOINT 0x05U +#define USB_DESC_TYPE_DEVICE_QUALIFIER 0x06U +#define USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION 0x07U +#define USB_DESC_TYPE_BOS 0x0FU + +#define USB_CONFIG_REMOTE_WAKEUP 0x02U +#define USB_CONFIG_SELF_POWERED 0x01U + +#define USB_FEATURE_EP_HALT 0x00U +#define USB_FEATURE_REMOTE_WAKEUP 0x01U +#define USB_FEATURE_TEST_MODE 0x02U + +#define USB_DEVICE_CAPABITY_TYPE 0x10U + +#define USB_HS_MAX_PACKET_SIZE 512U +#define USB_FS_MAX_PACKET_SIZE 64U +#define USB_MAX_EP0_SIZE 64U + +/* Device Status */ +#define USBD_STATE_DEFAULT 0x01U +#define USBD_STATE_ADDRESSED 0x02U +#define USBD_STATE_CONFIGURED 0x03U +#define USBD_STATE_SUSPENDED 0x04U + +/* EP0 State */ +#define USBD_EP0_IDLE 0x00U +#define USBD_EP0_SETUP 0x01U +#define USBD_EP0_DATA_IN 0x02U +#define USBD_EP0_DATA_OUT 0x03U +#define USBD_EP0_STATUS_IN 0x04U +#define USBD_EP0_STATUS_OUT 0x05U +#define USBD_EP0_STALL 0x06U + +#define USBD_EP_TYPE_CTRL 0x00U +#define USBD_EP_TYPE_ISOC 0x01U +#define USBD_EP_TYPE_BULK 0x02U +#define USBD_EP_TYPE_INTR 0x03U + +/** + * @} + */ + +/** @defgroup USBD_DEF_Exported_TypesDefinitions + * @{ + */ + +typedef struct usb_setup_req +{ + uint8_t bmRequest; + uint8_t bRequest; + uint16_t wValue; + uint16_t wIndex; + uint16_t wLength; +} USBD_SetupReqTypedef; + +struct _USBD_HandleTypeDef; + +typedef struct _Device_cb +{ + uint8_t (*Init)(struct _USBD_HandleTypeDef *pdev, uint8_t cfgidx); + uint8_t (*DeInit)(struct _USBD_HandleTypeDef *pdev, uint8_t cfgidx); + /* Control Endpoints*/ + uint8_t (*Setup)(struct _USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req); + uint8_t (*EP0_TxSent)(struct _USBD_HandleTypeDef *pdev); + uint8_t (*EP0_RxReady)(struct _USBD_HandleTypeDef *pdev); + /* Class Specific Endpoints*/ + uint8_t (*DataIn)(struct _USBD_HandleTypeDef *pdev, uint8_t epnum); + uint8_t (*DataOut)(struct _USBD_HandleTypeDef *pdev, uint8_t epnum); + uint8_t (*SOF)(struct _USBD_HandleTypeDef *pdev); + uint8_t (*IsoINIncomplete)(struct _USBD_HandleTypeDef *pdev, uint8_t epnum); + uint8_t (*IsoOUTIncomplete)(struct _USBD_HandleTypeDef *pdev, uint8_t epnum); + + uint8_t *(*GetHSConfigDescriptor)(uint16_t *length); + uint8_t *(*GetFSConfigDescriptor)(uint16_t *length); + uint8_t *(*GetOtherSpeedConfigDescriptor)(uint16_t *length); + uint8_t *(*GetDeviceQualifierDescriptor)(uint16_t *length); +#if (USBD_SUPPORT_USER_STRING == 1U) + uint8_t *(*GetUsrStrDescriptor)(struct _USBD_HandleTypeDef *pdev, + uint8_t index, uint16_t *length); +#endif + +} USBD_ClassTypeDef; + +/* Following USB Device Speed */ +typedef enum +{ + USBD_SPEED_HIGH = 0U, + USBD_SPEED_FULL = 1U, + USBD_SPEED_LOW = 2U, +} USBD_SpeedTypeDef; + +/* Following USB Device status */ +typedef enum +{ + USBD_OK = 0U, + USBD_BUSY, + USBD_FAIL, +} USBD_StatusTypeDef; + +/* USB Device descriptors structure */ +typedef struct +{ + uint8_t *(*GetDeviceDescriptor)(USBD_SpeedTypeDef speed, uint16_t *length); + uint8_t *(*GetLangIDStrDescriptor)(USBD_SpeedTypeDef speed, uint16_t *length); + uint8_t *(*GetManufacturerStrDescriptor)(USBD_SpeedTypeDef speed, + uint16_t *length); + uint8_t *(*GetProductStrDescriptor)(USBD_SpeedTypeDef speed, + uint16_t *length); + uint8_t *(*GetSerialStrDescriptor)(USBD_SpeedTypeDef speed, uint16_t *length); + uint8_t *(*GetConfigurationStrDescriptor)(USBD_SpeedTypeDef speed, + uint16_t *length); + uint8_t *(*GetInterfaceStrDescriptor)(USBD_SpeedTypeDef speed, + uint16_t *length); +#if (USBD_LPM_ENABLED == 1U) + uint8_t *(*GetBOSDescriptor)(USBD_SpeedTypeDef speed, uint16_t *length); +#endif +} USBD_DescriptorsTypeDef; + +/* USB Device handle structure */ +typedef struct +{ + uint32_t status; + uint32_t is_used; + uint32_t total_length; + uint32_t rem_length; + uint32_t maxpacket; +} USBD_EndpointTypeDef; + +/* USB Device handle structure */ +typedef struct _USBD_HandleTypeDef +{ + uint8_t id; + uint32_t dev_config; + uint32_t dev_default_config; + uint32_t dev_config_status; + USBD_SpeedTypeDef dev_speed; + USBD_EndpointTypeDef ep_in[15]; + USBD_EndpointTypeDef ep_out[15]; + uint32_t ep0_state; + uint32_t ep0_data_len; + uint8_t dev_state; + uint8_t dev_old_state; + uint8_t dev_address; + uint8_t dev_connection_status; + uint8_t dev_test_mode; + uint32_t dev_remote_wakeup; + + USBD_SetupReqTypedef request; + USBD_DescriptorsTypeDef *pDesc; + USBD_ClassTypeDef *pClass; + void *pClassData; + void *pUserData; + void *pData; +} USBD_HandleTypeDef; + +/** + * @} + */ + +/** @defgroup USBD_DEF_Exported_Macros + * @{ + */ + +#define LOBYTE(x) ((uint8_t)(x & 0x00FFU)) +#define HIBYTE(x) ((uint8_t)((x & 0xFF00U) >> 8U)) +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) +#define MAX(a, b) (((a) > (b)) ? (a) : (b)) + +#if defined(__GNUC__) +#ifndef __weak +#define __weak __attribute__((weak)) +#endif /* __weak */ +#ifndef __packed +#define __packed __attribute__((__packed__)) +#endif /* __packed */ +#endif /* __GNUC__ */ + +/* In HS mode and when the DMA is used, all variables and data structures + * dealing with the DMA during the transaction process should be 4-bytes + * aligned */ + +#if !defined(__ALIGN_BEGIN) || !defined(__ALIGN_END) +#if defined(__GNUC__) && /* GNU Compiler */ +#define __ALIGN_END __attribute__((aligned(4))) +#define __ALIGN_BEGIN +#else +#define __ALIGN_END +#if defined(__CC_ARM) /* ARM Compiler */ +#define __ALIGN_BEGIN __align(4) +#elif defined(__ICCARM__) /* IAR Compiler */ +#define __ALIGN_BEGIN +#elif defined(__TASKING__) /* TASKING Compiler */ +#define __ALIGN_BEGIN __align(4) +#endif /* __CC_ARM */ +#endif /* __GNUC__ */ +#endif + +/** + * @} + */ + +/** @defgroup USBD_DEF_Exported_Variables + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_DEF_Exported_FunctionsPrototype + * @{ + */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USBD_DEF_H */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_desc_template.h b/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_desc_template.h index ff0581876c..e9f7f8aa0a 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_desc_template.h +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_desc_template.h @@ -1,41 +1,42 @@ -/** - ****************************************************************************** - * @file usbd_desc_template.h - * @author MCD Application Team - * @brief Header for usbd_desc_template.c module - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USBD_DESC_TEMPLATE_H -#define __USBD_DESC_TEMPLATE_H - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_def.h" - -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ -#define DEVICE_ID1 (0x1FFF7A10) -#define DEVICE_ID2 (0x1FFF7A14) -#define DEVICE_ID3 (0x1FFF7A18) - -#define USB_SIZ_STRING_SERIAL 0x1A - -/* Exported macro ------------------------------------------------------------*/ -/* Exported functions ------------------------------------------------------- */ -extern USBD_DescriptorsTypeDef XXX_Desc; /* Replace 'XXX_Desc' with your active USB device class, ex: HID_Desc */ - -#endif /* __USBD_DESC_TEMPLATE_H*/ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_desc_template.h + * @author MCD Application Team + * @brief Header for usbd_desc_template.c module + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USBD_DESC_TEMPLATE_H +#define __USBD_DESC_TEMPLATE_H + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_def.h" + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ +#define DEVICE_ID1 (0x1FFF7A10) +#define DEVICE_ID2 (0x1FFF7A14) +#define DEVICE_ID3 (0x1FFF7A18) + +#define USB_SIZ_STRING_SERIAL 0x1A + +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ +extern USBD_DescriptorsTypeDef XXX_Desc; /* Replace 'XXX_Desc' with your active + * USB device class, ex: HID_Desc */ + +#endif /* __USBD_DESC_TEMPLATE_H*/ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_ioreq.h b/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_ioreq.h index 0552e00f63..8aecc4d5a1 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_ioreq.h +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Core/Inc/usbd_ioreq.h @@ -1,119 +1,117 @@ -/** - ****************************************************************************** - * @file usbd_ioreq.h - * @author MCD Application Team - * @brief Header file for the usbd_ioreq.c file - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USBD_IOREQ_H -#define __USBD_IOREQ_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_def.h" -#include "usbd_core.h" - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - -/** @defgroup USBD_IOREQ - * @brief header file for the usbd_ioreq.c file - * @{ - */ - -/** @defgroup USBD_IOREQ_Exported_Defines - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBD_IOREQ_Exported_Types - * @{ - */ - - -/** - * @} - */ - - - -/** @defgroup USBD_IOREQ_Exported_Macros - * @{ - */ - -/** - * @} - */ - -/** @defgroup USBD_IOREQ_Exported_Variables - * @{ - */ - -/** - * @} - */ - -/** @defgroup USBD_IOREQ_Exported_FunctionsPrototype - * @{ - */ - -USBD_StatusTypeDef USBD_CtlSendData (USBD_HandleTypeDef *pdev, - uint8_t *pbuf, - uint16_t len); - -USBD_StatusTypeDef USBD_CtlContinueSendData (USBD_HandleTypeDef *pdev, - uint8_t *pbuf, - uint16_t len); - -USBD_StatusTypeDef USBD_CtlPrepareRx (USBD_HandleTypeDef *pdev, - uint8_t *pbuf, - uint16_t len); - -USBD_StatusTypeDef USBD_CtlContinueRx (USBD_HandleTypeDef *pdev, - uint8_t *pbuf, - uint16_t len); - -USBD_StatusTypeDef USBD_CtlSendStatus (USBD_HandleTypeDef *pdev); - -USBD_StatusTypeDef USBD_CtlReceiveStatus (USBD_HandleTypeDef *pdev); - -uint32_t USBD_GetRxCount (USBD_HandleTypeDef *pdev, uint8_t ep_addr); - -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USBD_IOREQ_H */ - -/** - * @} - */ - -/** -* @} -*/ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_ioreq.h + * @author MCD Application Team + * @brief Header file for the usbd_ioreq.c file + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USBD_IOREQ_H +#define __USBD_IOREQ_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_def.h" +#include "usbd_core.h" + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + +/** @defgroup USBD_IOREQ + * @brief header file for the usbd_ioreq.c file + * @{ + */ + +/** @defgroup USBD_IOREQ_Exported_Defines + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_IOREQ_Exported_Types + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_IOREQ_Exported_Macros + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_IOREQ_Exported_Variables + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_IOREQ_Exported_FunctionsPrototype + * @{ + */ + +USBD_StatusTypeDef USBD_CtlSendData(USBD_HandleTypeDef *pdev, + uint8_t *pbuf, + uint16_t len); + +USBD_StatusTypeDef USBD_CtlContinueSendData(USBD_HandleTypeDef *pdev, + uint8_t *pbuf, + uint16_t len); + +USBD_StatusTypeDef USBD_CtlPrepareRx(USBD_HandleTypeDef *pdev, + uint8_t *pbuf, + uint16_t len); + +USBD_StatusTypeDef USBD_CtlContinueRx(USBD_HandleTypeDef *pdev, + uint8_t *pbuf, + uint16_t len); + +USBD_StatusTypeDef USBD_CtlSendStatus(USBD_HandleTypeDef *pdev); + +USBD_StatusTypeDef USBD_CtlReceiveStatus(USBD_HandleTypeDef *pdev); + +uint32_t USBD_GetRxCount(USBD_HandleTypeDef *pdev, uint8_t ep_addr); + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USBD_IOREQ_H */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_conf_template.c b/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_conf_template.c index 983e763eb0..2bce5e82d4 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_conf_template.c +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_conf_template.c @@ -1,204 +1,189 @@ -/** - ****************************************************************************** - * @file usbd_conf_template.c - * @author MCD Application Team - * @brief USB Device configuration and interface file - * This template should be copied to the user folder, renamed and customized - * following user needs. - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_core.h" -/* Private typedef -----------------------------------------------------------*/ -/* Private define ------------------------------------------------------------*/ -/* Private macro -------------------------------------------------------------*/ -/* Private variables ---------------------------------------------------------*/ -/* Private function prototypes -----------------------------------------------*/ -/* Private functions ---------------------------------------------------------*/ -/** - * @brief Initializes the Low Level portion of the Device driver. - * @param pdev: Device handle - * @retval USBD Status - */ -USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev) -{ - return USBD_OK; -} - -/** - * @brief De-Initializes the Low Level portion of the Device driver. - * @param pdev: Device handle - * @retval USBD Status - */ -USBD_StatusTypeDef USBD_LL_DeInit(USBD_HandleTypeDef *pdev) -{ - return USBD_OK; -} - -/** - * @brief Starts the Low Level portion of the Device driver. - * @param pdev: Device handle - * @retval USBD Status - */ -USBD_StatusTypeDef USBD_LL_Start(USBD_HandleTypeDef *pdev) -{ - return USBD_OK; -} - -/** - * @brief Stops the Low Level portion of the Device driver. - * @param pdev: Device handle - * @retval USBD Status - */ -USBD_StatusTypeDef USBD_LL_Stop(USBD_HandleTypeDef *pdev) -{ - return USBD_OK; -} - -/** - * @brief Opens an endpoint of the Low Level Driver. - * @param pdev: Device handle - * @param ep_addr: Endpoint Number - * @param ep_type: Endpoint Type - * @param ep_mps: Endpoint Max Packet Size - * @retval USBD Status - */ -USBD_StatusTypeDef USBD_LL_OpenEP(USBD_HandleTypeDef *pdev, - uint8_t ep_addr, - uint8_t ep_type, - uint16_t ep_mps) -{ - return USBD_OK; -} - -/** - * @brief Closes an endpoint of the Low Level Driver. - * @param pdev: Device handle - * @param ep_addr: Endpoint Number - * @retval USBD Status - */ -USBD_StatusTypeDef USBD_LL_CloseEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) -{ - return USBD_OK; -} - -/** - * @brief Flushes an endpoint of the Low Level Driver. - * @param pdev: Device handle - * @param ep_addr: Endpoint Number - * @retval USBD Status - */ -USBD_StatusTypeDef USBD_LL_FlushEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) -{ - return USBD_OK; -} - -/** - * @brief Sets a Stall condition on an endpoint of the Low Level Driver. - * @param pdev: Device handle - * @param ep_addr: Endpoint Number - * @retval USBD Status - */ -USBD_StatusTypeDef USBD_LL_StallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) -{ - return USBD_OK; -} - -/** - * @brief Clears a Stall condition on an endpoint of the Low Level Driver. - * @param pdev: Device handle - * @param ep_addr: Endpoint Number - * @retval USBD Status - */ -USBD_StatusTypeDef USBD_LL_ClearStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) -{ - return USBD_OK; -} - -/** - * @brief Returns Stall condition. - * @param pdev: Device handle - * @param ep_addr: Endpoint Number - * @retval Stall (1: Yes, 0: No) - */ -uint8_t USBD_LL_IsStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) -{ - return 0; -} - -/** - * @brief Assigns a USB address to the device. - * @param pdev: Device handle - * @param ep_addr: Endpoint Number - * @retval USBD Status - */ -USBD_StatusTypeDef USBD_LL_SetUSBAddress(USBD_HandleTypeDef *pdev, uint8_t dev_addr) -{ - return USBD_OK; -} - -/** - * @brief Transmits data over an endpoint. - * @param pdev: Device handle - * @param ep_addr: Endpoint Number - * @param pbuf: Pointer to data to be sent - * @param size: Data size - * @retval USBD Status - */ -USBD_StatusTypeDef USBD_LL_Transmit(USBD_HandleTypeDef *pdev, - uint8_t ep_addr, - uint8_t *pbuf, - uint16_t size) -{ - return USBD_OK; -} - -/** - * @brief Prepares an endpoint for reception. - * @param pdev: Device handle - * @param ep_addr: Endpoint Number - * @param pbuf: Pointer to data to be received - * @param size: Data size - * @retval USBD Status - */ -USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev, - uint8_t ep_addr, - uint8_t *pbuf, - uint16_t size) -{ - return USBD_OK; -} - -/** - * @brief Returns the last transferred packet size. - * @param pdev: Device handle - * @param ep_addr: Endpoint Number - * @retval Recived Data Size - */ -uint32_t USBD_LL_GetRxDataSize(USBD_HandleTypeDef *pdev, uint8_t ep_addr) -{ - return 0; -} - -/** - * @brief Delays routine for the USB Device Library. - * @param Delay: Delay in ms - * @retval None - */ -void USBD_LL_Delay(uint32_t Delay) -{ -} -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - +/** + ****************************************************************************** + * @file usbd_conf_template.c + * @author MCD Application Team + * @brief USB Device configuration and interface file + * This template should be copied to the user folder, renamed and + * customized following user needs. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_core.h" +/* Private typedef -----------------------------------------------------------*/ +/* Private define ------------------------------------------------------------*/ +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +/* Private function prototypes -----------------------------------------------*/ +/* Private functions ---------------------------------------------------------*/ + +/** + * @brief Initializes the Low Level portion of the Device driver. + * @param pdev: Device handle + * @retval USBD Status + */ +USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev) { return USBD_OK; } + +/** + * @brief De-Initializes the Low Level portion of the Device driver. + * @param pdev: Device handle + * @retval USBD Status + */ +USBD_StatusTypeDef USBD_LL_DeInit(USBD_HandleTypeDef *pdev) { return USBD_OK; } + +/** + * @brief Starts the Low Level portion of the Device driver. + * @param pdev: Device handle + * @retval USBD Status + */ +USBD_StatusTypeDef USBD_LL_Start(USBD_HandleTypeDef *pdev) { return USBD_OK; } + +/** + * @brief Stops the Low Level portion of the Device driver. + * @param pdev: Device handle + * @retval USBD Status + */ +USBD_StatusTypeDef USBD_LL_Stop(USBD_HandleTypeDef *pdev) { return USBD_OK; } + +/** + * @brief Opens an endpoint of the Low Level Driver. + * @param pdev: Device handle + * @param ep_addr: Endpoint Number + * @param ep_type: Endpoint Type + * @param ep_mps: Endpoint Max Packet Size + * @retval USBD Status + */ +USBD_StatusTypeDef USBD_LL_OpenEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr, + uint8_t ep_type, uint16_t ep_mps) +{ + return USBD_OK; +} + +/** + * @brief Closes an endpoint of the Low Level Driver. + * @param pdev: Device handle + * @param ep_addr: Endpoint Number + * @retval USBD Status + */ +USBD_StatusTypeDef USBD_LL_CloseEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) +{ + return USBD_OK; +} + +/** + * @brief Flushes an endpoint of the Low Level Driver. + * @param pdev: Device handle + * @param ep_addr: Endpoint Number + * @retval USBD Status + */ +USBD_StatusTypeDef USBD_LL_FlushEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) +{ + return USBD_OK; +} + +/** + * @brief Sets a Stall condition on an endpoint of the Low Level Driver. + * @param pdev: Device handle + * @param ep_addr: Endpoint Number + * @retval USBD Status + */ +USBD_StatusTypeDef USBD_LL_StallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) +{ + return USBD_OK; +} + +/** + * @brief Clears a Stall condition on an endpoint of the Low Level Driver. + * @param pdev: Device handle + * @param ep_addr: Endpoint Number + * @retval USBD Status + */ +USBD_StatusTypeDef USBD_LL_ClearStallEP(USBD_HandleTypeDef *pdev, + uint8_t ep_addr) +{ + return USBD_OK; +} + +/** + * @brief Returns Stall condition. + * @param pdev: Device handle + * @param ep_addr: Endpoint Number + * @retval Stall (1: Yes, 0: No) + */ +uint8_t USBD_LL_IsStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr) +{ + return 0; +} + +/** + * @brief Assigns a USB address to the device. + * @param pdev: Device handle + * @param ep_addr: Endpoint Number + * @retval USBD Status + */ +USBD_StatusTypeDef USBD_LL_SetUSBAddress(USBD_HandleTypeDef *pdev, + uint8_t dev_addr) +{ + return USBD_OK; +} + +/** + * @brief Transmits data over an endpoint. + * @param pdev: Device handle + * @param ep_addr: Endpoint Number + * @param pbuf: Pointer to data to be sent + * @param size: Data size + * @retval USBD Status + */ +USBD_StatusTypeDef USBD_LL_Transmit(USBD_HandleTypeDef *pdev, uint8_t ep_addr, + uint8_t *pbuf, uint16_t size) +{ + return USBD_OK; +} + +/** + * @brief Prepares an endpoint for reception. + * @param pdev: Device handle + * @param ep_addr: Endpoint Number + * @param pbuf: Pointer to data to be received + * @param size: Data size + * @retval USBD Status + */ +USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev, + uint8_t ep_addr, uint8_t *pbuf, + uint16_t size) +{ + return USBD_OK; +} + +/** + * @brief Returns the last transferred packet size. + * @param pdev: Device handle + * @param ep_addr: Endpoint Number + * @retval Recived Data Size + */ +uint32_t USBD_LL_GetRxDataSize(USBD_HandleTypeDef *pdev, uint8_t ep_addr) +{ + return 0; +} + +/** + * @brief Delays routine for the USB Device Library. + * @param Delay: Delay in ms + * @retval None + */ +void USBD_LL_Delay(uint32_t Delay) {} + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_core.c b/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_core.c index 8ccf17ee5c..8121f70aed 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_core.c +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_core.c @@ -1,603 +1,595 @@ -/** - ****************************************************************************** - * @file usbd_core.c - * @author MCD Application Team - * @brief This file provides all the USBD core functions. - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_core.h" - -/** @addtogroup STM32_USBD_DEVICE_LIBRARY -* @{ -*/ - - -/** @defgroup USBD_CORE -* @brief usbd core module -* @{ -*/ - -/** @defgroup USBD_CORE_Private_TypesDefinitions -* @{ -*/ -/** -* @} -*/ - - -/** @defgroup USBD_CORE_Private_Defines -* @{ -*/ - -/** -* @} -*/ - - -/** @defgroup USBD_CORE_Private_Macros -* @{ -*/ -/** -* @} -*/ - - - - -/** @defgroup USBD_CORE_Private_FunctionPrototypes -* @{ -*/ - -/** -* @} -*/ - -/** @defgroup USBD_CORE_Private_Variables -* @{ -*/ - -/** -* @} -*/ - -/** @defgroup USBD_CORE_Private_Functions -* @{ -*/ - -/** -* @brief USBD_Init -* Initializes the device stack and load the class driver -* @param pdev: device instance -* @param pdesc: Descriptor structure address -* @param id: Low level core index -* @retval None -*/ -USBD_StatusTypeDef USBD_Init(USBD_HandleTypeDef *pdev, USBD_DescriptorsTypeDef *pdesc, uint8_t id) -{ - /* Check whether the USB Host handle is valid */ - if(pdev == NULL) - { -#if (USBD_DEBUG_LEVEL > 1U) - USBD_ErrLog("Invalid Device handle"); -#endif - return USBD_FAIL; - } - - /* Unlink previous class*/ - if(pdev->pClass != NULL) - { - pdev->pClass = NULL; - } - - /* Assign USBD Descriptors */ - if(pdesc != NULL) - { - pdev->pDesc = pdesc; - } - - /* Set Device initial State */ - pdev->dev_state = USBD_STATE_DEFAULT; - pdev->id = id; - /* Initialize low level driver */ - USBD_LL_Init(pdev); - - return USBD_OK; -} - -/** -* @brief USBD_DeInit -* Re-Initialize th device library -* @param pdev: device instance -* @retval status: status -*/ -USBD_StatusTypeDef USBD_DeInit(USBD_HandleTypeDef *pdev) -{ - /* Set Default State */ - pdev->dev_state = USBD_STATE_DEFAULT; - - /* Free Class Resources */ - pdev->pClass->DeInit(pdev, (uint8_t)pdev->dev_config); - - /* Stop the low level driver */ - USBD_LL_Stop(pdev); - - /* Initialize low level driver */ - USBD_LL_DeInit(pdev); - - return USBD_OK; -} - -/** - * @brief USBD_RegisterClass - * Link class driver to Device Core. - * @param pDevice : Device Handle - * @param pclass: Class handle - * @retval USBD Status - */ -USBD_StatusTypeDef USBD_RegisterClass(USBD_HandleTypeDef *pdev, USBD_ClassTypeDef *pclass) -{ - USBD_StatusTypeDef status = USBD_OK; - if(pclass != 0) - { - /* link the class to the USB Device handle */ - pdev->pClass = pclass; - status = USBD_OK; - } - else - { -#if (USBD_DEBUG_LEVEL > 1U) - USBD_ErrLog("Invalid Class handle"); -#endif - status = USBD_FAIL; - } - - return status; -} - -/** - * @brief USBD_Start - * Start the USB Device Core. - * @param pdev: Device Handle - * @retval USBD Status - */ -USBD_StatusTypeDef USBD_Start (USBD_HandleTypeDef *pdev) -{ - - /* Start the low level driver */ - USBD_LL_Start(pdev); - - return USBD_OK; -} - -/** - * @brief USBD_Stop - * Stop the USB Device Core. - * @param pdev: Device Handle - * @retval USBD Status - */ -USBD_StatusTypeDef USBD_Stop (USBD_HandleTypeDef *pdev) -{ - /* Free Class Resources */ - pdev->pClass->DeInit(pdev, (uint8_t)pdev->dev_config); - - /* Stop the low level driver */ - USBD_LL_Stop(pdev); - - return USBD_OK; -} - -/** -* @brief USBD_RunTestMode -* Launch test mode process -* @param pdev: device instance -* @retval status -*/ -USBD_StatusTypeDef USBD_RunTestMode (USBD_HandleTypeDef *pdev) -{ - /* Prevent unused argument compilation warning */ - UNUSED(pdev); - - return USBD_OK; -} - -/** -* @brief USBD_SetClassConfig -* Configure device and start the interface -* @param pdev: device instance -* @param cfgidx: configuration index -* @retval status -*/ - -USBD_StatusTypeDef USBD_SetClassConfig(USBD_HandleTypeDef *pdev, uint8_t cfgidx) -{ - USBD_StatusTypeDef ret = USBD_FAIL; - - if(pdev->pClass != NULL) - { - /* Set configuration and Start the Class*/ - if(pdev->pClass->Init(pdev, cfgidx) == 0U) - { - ret = USBD_OK; - } - } - - return ret; -} - -/** -* @brief USBD_ClrClassConfig -* Clear current configuration -* @param pdev: device instance -* @param cfgidx: configuration index -* @retval status: USBD_StatusTypeDef -*/ -USBD_StatusTypeDef USBD_ClrClassConfig(USBD_HandleTypeDef *pdev, uint8_t cfgidx) -{ - /* Clear configuration and De-initialize the Class process*/ - pdev->pClass->DeInit(pdev, cfgidx); - return USBD_OK; -} - - -/** -* @brief USBD_SetupStage -* Handle the setup stage -* @param pdev: device instance -* @retval status -*/ -USBD_StatusTypeDef USBD_LL_SetupStage(USBD_HandleTypeDef *pdev, uint8_t *psetup) -{ - USBD_ParseSetupRequest(&pdev->request, psetup); - - pdev->ep0_state = USBD_EP0_SETUP; - - pdev->ep0_data_len = pdev->request.wLength; - - switch (pdev->request.bmRequest & 0x1FU) - { - case USB_REQ_RECIPIENT_DEVICE: - USBD_StdDevReq (pdev, &pdev->request); - break; - - case USB_REQ_RECIPIENT_INTERFACE: - USBD_StdItfReq(pdev, &pdev->request); - break; - - case USB_REQ_RECIPIENT_ENDPOINT: - USBD_StdEPReq(pdev, &pdev->request); - break; - - default: - USBD_LL_StallEP(pdev, (pdev->request.bmRequest & 0x80U)); - break; - } - - return USBD_OK; -} - -/** -* @brief USBD_DataOutStage -* Handle data OUT stage -* @param pdev: device instance -* @param epnum: endpoint index -* @retval status -*/ -USBD_StatusTypeDef USBD_LL_DataOutStage(USBD_HandleTypeDef *pdev, - uint8_t epnum, uint8_t *pdata) -{ - USBD_EndpointTypeDef *pep; - - if(epnum == 0U) - { - pep = &pdev->ep_out[0]; - - if ( pdev->ep0_state == USBD_EP0_DATA_OUT) - { - if(pep->rem_length > pep->maxpacket) - { - pep->rem_length -= pep->maxpacket; - - USBD_CtlContinueRx (pdev, - pdata, - (uint16_t)MIN(pep->rem_length, pep->maxpacket)); - } - else - { - if((pdev->pClass->EP0_RxReady != NULL)&& - (pdev->dev_state == USBD_STATE_CONFIGURED)) - { - pdev->pClass->EP0_RxReady(pdev); - } - USBD_CtlSendStatus(pdev); - } - } - else - { - if (pdev->ep0_state == USBD_EP0_STATUS_OUT) - { - /* - * STATUS PHASE completed, update ep0_state to idle - */ - pdev->ep0_state = USBD_EP0_IDLE; - USBD_LL_StallEP(pdev, 0U); - } - } - } - else if((pdev->pClass->DataOut != NULL) && - (pdev->dev_state == USBD_STATE_CONFIGURED)) - { - pdev->pClass->DataOut(pdev, epnum); - } - else - { - /* should never be in this condition */ - return USBD_FAIL; - } - - return USBD_OK; -} - -/** -* @brief USBD_DataInStage -* Handle data in stage -* @param pdev: device instance -* @param epnum: endpoint index -* @retval status -*/ -USBD_StatusTypeDef USBD_LL_DataInStage(USBD_HandleTypeDef *pdev, uint8_t epnum, - uint8_t *pdata) -{ - USBD_EndpointTypeDef *pep; - - if(epnum == 0U) - { - pep = &pdev->ep_in[0]; - - if ( pdev->ep0_state == USBD_EP0_DATA_IN) - { - if(pep->rem_length > pep->maxpacket) - { - pep->rem_length -= pep->maxpacket; - - USBD_CtlContinueSendData (pdev, pdata, (uint16_t)pep->rem_length); - - /* Prepare endpoint for premature end of transfer */ - USBD_LL_PrepareReceive (pdev, 0U, NULL, 0U); - } - else - { /* last packet is MPS multiple, so send ZLP packet */ - if((pep->total_length % pep->maxpacket == 0U) && - (pep->total_length >= pep->maxpacket) && - (pep->total_length < pdev->ep0_data_len)) - { - USBD_CtlContinueSendData(pdev, NULL, 0U); - pdev->ep0_data_len = 0U; - - /* Prepare endpoint for premature end of transfer */ - USBD_LL_PrepareReceive (pdev, 0U, NULL, 0U); - } - else - { - if((pdev->pClass->EP0_TxSent != NULL)&& - (pdev->dev_state == USBD_STATE_CONFIGURED)) - { - pdev->pClass->EP0_TxSent(pdev); - } - /* - * Fix EPO STALL issue in STM32 USB Device library 2.5.1 - * Issue raised by @makarenya, see #388 - * USB Specification EP0 should never STALL. - */ - /* USBD_LL_StallEP(pdev, 0x80U); */ - USBD_CtlReceiveStatus(pdev); - } - } - } - else - { - if ((pdev->ep0_state == USBD_EP0_STATUS_IN) || - (pdev->ep0_state == USBD_EP0_IDLE)) - { - USBD_LL_StallEP(pdev, 0x80U); - } - } - - if (pdev->dev_test_mode == 1U) - { - USBD_RunTestMode(pdev); - pdev->dev_test_mode = 0U; - } - } - else if((pdev->pClass->DataIn != NULL) && - (pdev->dev_state == USBD_STATE_CONFIGURED)) - { - pdev->pClass->DataIn(pdev, epnum); - } - else - { - /* should never be in this condition */ - return USBD_FAIL; - } - - return USBD_OK; -} - -/** -* @brief USBD_LL_Reset -* Handle Reset event -* @param pdev: device instance -* @retval status -*/ - -USBD_StatusTypeDef USBD_LL_Reset(USBD_HandleTypeDef *pdev) -{ - /* Open EP0 OUT */ - USBD_LL_OpenEP(pdev, 0x00U, USBD_EP_TYPE_CTRL, USB_MAX_EP0_SIZE); - pdev->ep_out[0x00U & 0xFU].is_used = 1U; - - pdev->ep_out[0].maxpacket = USB_MAX_EP0_SIZE; - - /* Open EP0 IN */ - USBD_LL_OpenEP(pdev, 0x80U, USBD_EP_TYPE_CTRL, USB_MAX_EP0_SIZE); - pdev->ep_in[0x80U & 0xFU].is_used = 1U; - - pdev->ep_in[0].maxpacket = USB_MAX_EP0_SIZE; - /* Upon Reset call user call back */ - pdev->dev_state = USBD_STATE_DEFAULT; - pdev->ep0_state = USBD_EP0_IDLE; - pdev->dev_config= 0U; - pdev->dev_remote_wakeup = 0U; - - if (pdev->pClassData) - { - pdev->pClass->DeInit(pdev, (uint8_t)pdev->dev_config); - } - - return USBD_OK; -} - -/** -* @brief USBD_LL_Reset -* Handle Reset event -* @param pdev: device instance -* @retval status -*/ -USBD_StatusTypeDef USBD_LL_SetSpeed(USBD_HandleTypeDef *pdev, USBD_SpeedTypeDef speed) -{ - pdev->dev_speed = speed; - return USBD_OK; -} - -/** -* @brief USBD_Suspend -* Handle Suspend event -* @param pdev: device instance -* @retval status -*/ - -USBD_StatusTypeDef USBD_LL_Suspend(USBD_HandleTypeDef *pdev) -{ - pdev->dev_old_state = pdev->dev_state; - pdev->dev_state = USBD_STATE_SUSPENDED; - return USBD_OK; -} - -/** -* @brief USBD_Resume -* Handle Resume event -* @param pdev: device instance -* @retval status -*/ - -USBD_StatusTypeDef USBD_LL_Resume(USBD_HandleTypeDef *pdev) -{ - pdev->dev_state = pdev->dev_old_state; - return USBD_OK; -} - -/** -* @brief USBD_SOF -* Handle SOF event -* @param pdev: device instance -* @retval status -*/ - -USBD_StatusTypeDef USBD_LL_SOF(USBD_HandleTypeDef *pdev) -{ - if(pdev->dev_state == USBD_STATE_CONFIGURED) - { - if(pdev->pClass->SOF != NULL) - { - pdev->pClass->SOF(pdev); - } - } - return USBD_OK; -} - -/** -* @brief USBD_IsoINIncomplete -* Handle iso in incomplete event -* @param pdev: device instance -* @retval status -*/ -USBD_StatusTypeDef USBD_LL_IsoINIncomplete(USBD_HandleTypeDef *pdev, uint8_t epnum) -{ - /* Prevent unused arguments compilation warning */ - UNUSED(pdev); - UNUSED(epnum); - - return USBD_OK; -} - -/** -* @brief USBD_IsoOUTIncomplete -* Handle iso out incomplete event -* @param pdev: device instance -* @retval status -*/ -USBD_StatusTypeDef USBD_LL_IsoOUTIncomplete(USBD_HandleTypeDef *pdev, uint8_t epnum) -{ - /* Prevent unused arguments compilation warning */ - UNUSED(pdev); - UNUSED(epnum); - - return USBD_OK; -} - -/** -* @brief USBD_DevConnected -* Handle device connection event -* @param pdev: device instance -* @retval status -*/ -USBD_StatusTypeDef USBD_LL_DevConnected(USBD_HandleTypeDef *pdev) -{ - /* Prevent unused argument compilation warning */ - UNUSED(pdev); - - return USBD_OK; -} - -/** -* @brief USBD_DevDisconnected -* Handle device disconnection event -* @param pdev: device instance -* @retval status -*/ -USBD_StatusTypeDef USBD_LL_DevDisconnected(USBD_HandleTypeDef *pdev) -{ - /* Free Class Resources */ - pdev->dev_state = USBD_STATE_DEFAULT; - pdev->pClass->DeInit(pdev, (uint8_t)pdev->dev_config); - - return USBD_OK; -} -/** -* @} -*/ - - -/** -* @} -*/ - - -/** -* @} -*/ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - +/** + ****************************************************************************** + * @file usbd_core.c + * @author MCD Application Team + * @brief This file provides all the USBD core functions. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_core.h" + +/** @addtogroup STM32_USBD_DEVICE_LIBRARY + * @{ + */ + +/** @defgroup USBD_CORE + * @brief usbd core module + * @{ + */ + +/** @defgroup USBD_CORE_Private_TypesDefinitions + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_CORE_Private_Defines + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_CORE_Private_Macros + * @{ + */ +/** + * @} + */ + +/** @defgroup USBD_CORE_Private_FunctionPrototypes + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_CORE_Private_Variables + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_CORE_Private_Functions + * @{ + */ + +/** + * @brief USBD_Init + * Initializes the device stack and load the class driver + * @param pdev: device instance + * @param pdesc: Descriptor structure address + * @param id: Low level core index + * @retval None + */ +USBD_StatusTypeDef USBD_Init(USBD_HandleTypeDef *pdev, + USBD_DescriptorsTypeDef *pdesc, uint8_t id) +{ + /* Check whether the USB Host handle is valid */ + if (pdev == NULL) + { +#if (USBD_DEBUG_LEVEL > 1U) + USBD_ErrLog("Invalid Device handle"); +#endif + return USBD_FAIL; + } + + /* Unlink previous class*/ + if (pdev->pClass != NULL) + { + pdev->pClass = NULL; + } + + /* Assign USBD Descriptors */ + if (pdesc != NULL) + { + pdev->pDesc = pdesc; + } + + /* Set Device initial State */ + pdev->dev_state = USBD_STATE_DEFAULT; + pdev->id = id; + /* Initialize low level driver */ + USBD_LL_Init(pdev); + + return USBD_OK; +} + +/** + * @brief USBD_DeInit + * Re-Initialize th device library + * @param pdev: device instance + * @retval status: status + */ +USBD_StatusTypeDef USBD_DeInit(USBD_HandleTypeDef *pdev) +{ + /* Set Default State */ + pdev->dev_state = USBD_STATE_DEFAULT; + + /* Free Class Resources */ + pdev->pClass->DeInit(pdev, (uint8_t)pdev->dev_config); + + /* Stop the low level driver */ + USBD_LL_Stop(pdev); + + /* Initialize low level driver */ + USBD_LL_DeInit(pdev); + + return USBD_OK; +} + +/** + * @brief USBD_RegisterClass + * Link class driver to Device Core. + * @param pDevice : Device Handle + * @param pclass: Class handle + * @retval USBD Status + */ +USBD_StatusTypeDef USBD_RegisterClass(USBD_HandleTypeDef *pdev, + USBD_ClassTypeDef *pclass) +{ + USBD_StatusTypeDef status = USBD_OK; + if (pclass != 0) + { + /* link the class to the USB Device handle */ + pdev->pClass = pclass; + } + else + { +#if (USBD_DEBUG_LEVEL > 1U) + USBD_ErrLog("Invalid Class handle"); +#endif + status = USBD_FAIL; + } + + return status; +} + +/** + * @brief USBD_Start + * Start the USB Device Core. + * @param pdev: Device Handle + * @retval USBD Status + */ +USBD_StatusTypeDef USBD_Start(USBD_HandleTypeDef *pdev) +{ + /* Start the low level driver */ + USBD_LL_Start(pdev); + + return USBD_OK; +} + +/** + * @brief USBD_Stop + * Stop the USB Device Core. + * @param pdev: Device Handle + * @retval USBD Status + */ +USBD_StatusTypeDef USBD_Stop(USBD_HandleTypeDef *pdev) +{ + /* Free Class Resources */ + pdev->pClass->DeInit(pdev, (uint8_t)pdev->dev_config); + + /* Stop the low level driver */ + USBD_LL_Stop(pdev); + + return USBD_OK; +} + +/** + * @brief USBD_RunTestMode + * Launch test mode process + * @param pdev: device instance + * @retval status + */ +USBD_StatusTypeDef USBD_RunTestMode(USBD_HandleTypeDef *pdev) +{ + /* Prevent unused argument compilation warning */ + UNUSED(pdev); + + return USBD_OK; +} + +/** + * @brief USBD_SetClassConfig + * Configure device and start the interface + * @param pdev: device instance + * @param cfgidx: configuration index + * @retval status + */ +USBD_StatusTypeDef USBD_SetClassConfig(USBD_HandleTypeDef *pdev, uint8_t cfgidx) +{ + USBD_StatusTypeDef ret = USBD_FAIL; + + if (pdev->pClass != NULL) + { + /* Set configuration and Start the Class*/ + if (pdev->pClass->Init(pdev, cfgidx) == 0U) + { + ret = USBD_OK; + } + } + + return ret; +} + +/** + * @brief USBD_ClrClassConfig + * Clear current configuration + * @param pdev: device instance + * @param cfgidx: configuration index + * @retval status: USBD_StatusTypeDef + */ +USBD_StatusTypeDef USBD_ClrClassConfig(USBD_HandleTypeDef *pdev, uint8_t cfgidx) +{ + /* Clear configuration and De-initialize the Class process*/ + pdev->pClass->DeInit(pdev, cfgidx); + return USBD_OK; +} + +/** + * @brief USBD_SetupStage + * Handle the setup stage + * @param pdev: device instance + * @retval status + */ +USBD_StatusTypeDef USBD_LL_SetupStage(USBD_HandleTypeDef *pdev, uint8_t *psetup) +{ + USBD_ParseSetupRequest(&pdev->request, psetup); + + pdev->ep0_state = USBD_EP0_SETUP; + + pdev->ep0_data_len = pdev->request.wLength; + + switch (pdev->request.bmRequest & 0x1FU) + { + case USB_REQ_RECIPIENT_DEVICE: + USBD_StdDevReq(pdev, &pdev->request); + break; + + case USB_REQ_RECIPIENT_INTERFACE: + USBD_StdItfReq(pdev, &pdev->request); + break; + + case USB_REQ_RECIPIENT_ENDPOINT: + USBD_StdEPReq(pdev, &pdev->request); + break; + + default: + USBD_LL_StallEP(pdev, (uint8_t) (pdev->request.bmRequest & 0x80U)); + break; + } + + return USBD_OK; +} + +/** + * @brief USBD_DataOutStage + * Handle data OUT stage + * @param pdev: device instance + * @param epnum: endpoint index + * @retval status + */ +USBD_StatusTypeDef USBD_LL_DataOutStage(USBD_HandleTypeDef *pdev, + uint8_t epnum, + uint8_t *pdata) +{ + USBD_EndpointTypeDef *pep; + + if (epnum == 0U) + { + pep = &pdev->ep_out[0]; + + if (pdev->ep0_state == USBD_EP0_DATA_OUT) + { + if (pep->rem_length > pep->maxpacket) + { + pep->rem_length -= pep->maxpacket; + + USBD_CtlContinueRx(pdev, pdata, + (uint16_t)MIN(pep->rem_length, pep->maxpacket)); + } + else + { + if ((pdev->pClass->EP0_RxReady != NULL) && + (pdev->dev_state == USBD_STATE_CONFIGURED)) + { + pdev->pClass->EP0_RxReady(pdev); + } + USBD_CtlSendStatus(pdev); + } + } + else + { + if (pdev->ep0_state == USBD_EP0_STATUS_OUT) + { + /* + * STATUS PHASE completed, update ep0_state to idle + */ + pdev->ep0_state = USBD_EP0_IDLE; + USBD_LL_StallEP(pdev, 0U); + } + } + } + else if ((pdev->pClass->DataOut != NULL) && + (pdev->dev_state == USBD_STATE_CONFIGURED)) + { + pdev->pClass->DataOut(pdev, epnum); + } + else + { + /* should never be in this condition */ + return USBD_FAIL; + } + + return USBD_OK; +} + +/** + * @brief USBD_DataInStage + * Handle data in stage + * @param pdev: device instance + * @param epnum: endpoint index + * @retval status + */ +USBD_StatusTypeDef USBD_LL_DataInStage(USBD_HandleTypeDef *pdev, + uint8_t epnum, + uint8_t *pdata) +{ + USBD_EndpointTypeDef *pep; + + if (epnum == 0U) + { + pep = &pdev->ep_in[0]; + + if (pdev->ep0_state == USBD_EP0_DATA_IN) + { + if (pep->rem_length > pep->maxpacket) + { + pep->rem_length -= pep->maxpacket; + + USBD_CtlContinueSendData(pdev, pdata, (uint16_t)pep->rem_length); + + /* Prepare endpoint for premature end of transfer */ + USBD_LL_PrepareReceive(pdev, 0U, NULL, 0U); + } + else + { /* last packet is MPS multiple, so send ZLP packet */ + if ((pep->total_length % pep->maxpacket == 0U) && + (pep->total_length >= pep->maxpacket) && + (pep->total_length < pdev->ep0_data_len)) + { + USBD_CtlContinueSendData(pdev, NULL, 0U); + pdev->ep0_data_len = 0U; + + /* Prepare endpoint for premature end of transfer */ + USBD_LL_PrepareReceive(pdev, 0U, NULL, 0U); + } + else + { + if ((pdev->pClass->EP0_TxSent != NULL) && + (pdev->dev_state == USBD_STATE_CONFIGURED)) + { + pdev->pClass->EP0_TxSent(pdev); + } + /* + * Fix EPO STALL issue in STM32 USB Device library 2.5.1 + * Issue raised by @makarenya, see #388 + * USB Specification EP0 should never STALL. + */ + /* USBD_LL_StallEP(pdev, 0x80U); */ + USBD_CtlReceiveStatus(pdev); + } + } + } + else + { + if ((pdev->ep0_state == USBD_EP0_STATUS_IN) || + (pdev->ep0_state == USBD_EP0_IDLE)) + { + USBD_LL_StallEP(pdev, 0x80U); + } + } + + if (pdev->dev_test_mode == 1U) + { + USBD_RunTestMode(pdev); + pdev->dev_test_mode = 0U; + } + } + else if ((pdev->pClass->DataIn != NULL) && + (pdev->dev_state == USBD_STATE_CONFIGURED)) + { + pdev->pClass->DataIn(pdev, epnum); + } + else + { + /* should never be in this condition */ + return USBD_FAIL; + } + + return USBD_OK; +} + +/** + * @brief USBD_LL_Reset + * Handle Reset event + * @param pdev: device instance + * @retval status + */ +USBD_StatusTypeDef USBD_LL_Reset(USBD_HandleTypeDef *pdev) +{ + /* Open EP0 OUT */ + USBD_LL_OpenEP(pdev, 0x00U, USBD_EP_TYPE_CTRL, USB_MAX_EP0_SIZE); + pdev->ep_out[0x00U & 0xFU].is_used = 1U; + + pdev->ep_out[0].maxpacket = USB_MAX_EP0_SIZE; + + /* Open EP0 IN */ + USBD_LL_OpenEP(pdev, 0x80U, USBD_EP_TYPE_CTRL, USB_MAX_EP0_SIZE); + pdev->ep_in[0x80U & 0xFU].is_used = 1U; + + pdev->ep_in[0].maxpacket = USB_MAX_EP0_SIZE; + /* Upon Reset call user call back */ + pdev->dev_state = USBD_STATE_DEFAULT; + pdev->ep0_state = USBD_EP0_IDLE; + pdev->dev_config = 0U; + pdev->dev_remote_wakeup = 0U; + + if (pdev->pClassData) + { + pdev->pClass->DeInit(pdev, (uint8_t)pdev->dev_config); + } + + return USBD_OK; +} + +/** + * @brief USBD_LL_Reset + * Handle Reset event + * @param pdev: device instance + * @retval status + */ +USBD_StatusTypeDef USBD_LL_SetSpeed(USBD_HandleTypeDef *pdev, + USBD_SpeedTypeDef speed) +{ + pdev->dev_speed = speed; + return USBD_OK; +} + +/** + * @brief USBD_Suspend + * Handle Suspend event + * @param pdev: device instance + * @retval status + */ +USBD_StatusTypeDef USBD_LL_Suspend(USBD_HandleTypeDef *pdev) +{ + pdev->dev_old_state = pdev->dev_state; + pdev->dev_state = USBD_STATE_SUSPENDED; + return USBD_OK; +} + +/** + * @brief USBD_Resume + * Handle Resume event + * @param pdev: device instance + * @retval status + */ +USBD_StatusTypeDef USBD_LL_Resume(USBD_HandleTypeDef *pdev) +{ + pdev->dev_state = pdev->dev_old_state; + return USBD_OK; +} + +/** + * @brief USBD_SOF + * Handle SOF event + * @param pdev: device instance + * @retval status + */ +USBD_StatusTypeDef USBD_LL_SOF(USBD_HandleTypeDef *pdev) +{ + if (pdev->dev_state == USBD_STATE_CONFIGURED) + { + if (pdev->pClass->SOF != NULL) + { + pdev->pClass->SOF(pdev); + } + } + return USBD_OK; +} + +/** + * @brief USBD_IsoINIncomplete + * Handle iso in incomplete event + * @param pdev: device instance + * @retval status + */ +USBD_StatusTypeDef USBD_LL_IsoINIncomplete(USBD_HandleTypeDef *pdev, + uint8_t epnum) +{ + /* Prevent unused arguments compilation warning */ + UNUSED(pdev); + UNUSED(epnum); + + return USBD_OK; +} + +/** + * @brief USBD_IsoOUTIncomplete + * Handle iso out incomplete event + * @param pdev: device instance + * @retval status + */ +USBD_StatusTypeDef USBD_LL_IsoOUTIncomplete(USBD_HandleTypeDef *pdev, + uint8_t epnum) +{ + /* Prevent unused arguments compilation warning */ + UNUSED(pdev); + UNUSED(epnum); + + return USBD_OK; +} + +/** + * @brief USBD_DevConnected + * Handle device connection event + * @param pdev: device instance + * @retval status + */ +USBD_StatusTypeDef USBD_LL_DevConnected(USBD_HandleTypeDef *pdev) +{ + /* Prevent unused argument compilation warning */ + UNUSED(pdev); + + return USBD_OK; +} + +/** + * @brief USBD_DevDisconnected + * Handle device disconnection event + * @param pdev: device instance + * @retval status + */ +USBD_StatusTypeDef USBD_LL_DevDisconnected(USBD_HandleTypeDef *pdev) +{ + /* Free Class Resources */ + pdev->dev_state = USBD_STATE_DEFAULT; + pdev->pClass->DeInit(pdev, (uint8_t)pdev->dev_config); + + return USBD_OK; +} + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ctlreq.c b/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ctlreq.c index c3e7ecbe80..f1caf1f8e2 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ctlreq.c +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ctlreq.c @@ -1,848 +1,832 @@ -/** - ****************************************************************************** - * @file usbd_req.c - * @author MCD Application Team - * @brief This file provides the standard USB requests following chapter 9. - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_ctlreq.h" -#include "usbd_ioreq.h" - - -/** @addtogroup STM32_USBD_STATE_DEVICE_LIBRARY - * @{ - */ - - -/** @defgroup USBD_REQ - * @brief USB standard requests module - * @{ - */ - -/** @defgroup USBD_REQ_Private_TypesDefinitions - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBD_REQ_Private_Defines - * @{ - */ - -/** - * @} - */ - - -/** @defgroup USBD_REQ_Private_Macros - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBD_REQ_Private_Variables - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBD_REQ_Private_FunctionPrototypes - * @{ - */ -static void USBD_GetDescriptor(USBD_HandleTypeDef *pdev , - USBD_SetupReqTypedef *req); - -static void USBD_SetAddress(USBD_HandleTypeDef *pdev , - USBD_SetupReqTypedef *req); - -static void USBD_SetConfig(USBD_HandleTypeDef *pdev , - USBD_SetupReqTypedef *req); - -static void USBD_GetConfig(USBD_HandleTypeDef *pdev , - USBD_SetupReqTypedef *req); - -static void USBD_GetStatus(USBD_HandleTypeDef *pdev , - USBD_SetupReqTypedef *req); - -static void USBD_SetFeature(USBD_HandleTypeDef *pdev , - USBD_SetupReqTypedef *req); - -static void USBD_ClrFeature(USBD_HandleTypeDef *pdev , - USBD_SetupReqTypedef *req); - -static uint8_t USBD_GetLen(uint8_t *buf); - -/** - * @} - */ - - -/** @defgroup USBD_REQ_Private_Functions - * @{ - */ - - -/** -* @brief USBD_StdDevReq -* Handle standard usb device requests -* @param pdev: device instance -* @param req: usb request -* @retval status -*/ -USBD_StatusTypeDef USBD_StdDevReq (USBD_HandleTypeDef *pdev , USBD_SetupReqTypedef *req) -{ - USBD_StatusTypeDef ret = USBD_OK; - - switch (req->bmRequest & USB_REQ_TYPE_MASK) - { - case USB_REQ_TYPE_CLASS: - case USB_REQ_TYPE_VENDOR: - pdev->pClass->Setup(pdev, req); - break; - - case USB_REQ_TYPE_STANDARD: - - switch (req->bRequest) - { - case USB_REQ_GET_DESCRIPTOR: - - USBD_GetDescriptor (pdev, req); - break; - - case USB_REQ_SET_ADDRESS: - USBD_SetAddress (pdev, req); - break; - - case USB_REQ_SET_CONFIGURATION: - USBD_SetConfig (pdev, req); - break; - - case USB_REQ_GET_CONFIGURATION: - USBD_GetConfig (pdev, req); - break; - - case USB_REQ_GET_STATUS: - USBD_GetStatus (pdev, req); - break; - - - case USB_REQ_SET_FEATURE: - USBD_SetFeature (pdev, req); - break; - - case USB_REQ_CLEAR_FEATURE: - USBD_ClrFeature (pdev, req); - break; - - default: - USBD_CtlError(pdev, req); - break; - } - break; - - default: - USBD_CtlError(pdev, req); - break; - } - - return ret; -} - -/** -* @brief USBD_StdItfReq -* Handle standard usb interface requests -* @param pdev: device instance -* @param req: usb request -* @retval status -*/ -USBD_StatusTypeDef USBD_StdItfReq (USBD_HandleTypeDef *pdev , USBD_SetupReqTypedef *req) -{ - USBD_StatusTypeDef ret = USBD_OK; - - switch (req->bmRequest & USB_REQ_TYPE_MASK) - { - case USB_REQ_TYPE_CLASS: - case USB_REQ_TYPE_VENDOR: - case USB_REQ_TYPE_STANDARD: - switch (pdev->dev_state) - { - case USBD_STATE_DEFAULT: - case USBD_STATE_ADDRESSED: - case USBD_STATE_CONFIGURED: - - if (LOBYTE(req->wIndex) <= USBD_MAX_NUM_INTERFACES) - { - ret = (USBD_StatusTypeDef)pdev->pClass->Setup (pdev, req); - - if ((req->wLength == 0U) && (ret == USBD_OK)) - { - USBD_CtlSendStatus(pdev); - } - } - else - { - USBD_CtlError(pdev, req); - } - break; - - default: - USBD_CtlError(pdev, req); - break; - } - break; - - default: - USBD_CtlError(pdev, req); - break; - } - - return USBD_OK; -} - -/** -* @brief USBD_StdEPReq -* Handle standard usb endpoint requests -* @param pdev: device instance -* @param req: usb request -* @retval status -*/ -USBD_StatusTypeDef USBD_StdEPReq (USBD_HandleTypeDef *pdev , USBD_SetupReqTypedef *req) -{ - - uint8_t ep_addr; - USBD_StatusTypeDef ret = USBD_OK; - USBD_EndpointTypeDef *pep; - ep_addr = LOBYTE(req->wIndex); - - switch (req->bmRequest & USB_REQ_TYPE_MASK) - { - - case USB_REQ_TYPE_CLASS: - case USB_REQ_TYPE_VENDOR: - pdev->pClass->Setup (pdev, req); - break; - - case USB_REQ_TYPE_STANDARD: - /* Check if it is a class request */ - if ((req->bmRequest & 0x60U) == 0x20U) - { - ret = (USBD_StatusTypeDef)pdev->pClass->Setup (pdev, req); - - return ret; - } - - switch (req->bRequest) - { - - case USB_REQ_SET_FEATURE : - - switch (pdev->dev_state) - { - case USBD_STATE_ADDRESSED: - if ((ep_addr != 0x00U) && (ep_addr != 0x80U)) - { - USBD_LL_StallEP(pdev, ep_addr); - USBD_LL_StallEP(pdev, 0x80U); - } - else - { - USBD_CtlError(pdev, req); - } - break; - - case USBD_STATE_CONFIGURED: - if (req->wValue == USB_FEATURE_EP_HALT) - { - if ((ep_addr != 0x00U) && (ep_addr != 0x80U) && (req->wLength == 0x00U)) - { - USBD_LL_StallEP(pdev, ep_addr); - } - } - USBD_CtlSendStatus(pdev); - - break; - - default: - USBD_CtlError(pdev, req); - break; - } - break; - - case USB_REQ_CLEAR_FEATURE : - - switch (pdev->dev_state) - { - case USBD_STATE_ADDRESSED: - if ((ep_addr != 0x00U) && (ep_addr != 0x80U)) - { - USBD_LL_StallEP(pdev, ep_addr); - USBD_LL_StallEP(pdev, 0x80U); - } - else - { - USBD_CtlError(pdev, req); - } - break; - - case USBD_STATE_CONFIGURED: - if (req->wValue == USB_FEATURE_EP_HALT) - { - if ((ep_addr & 0x7FU) != 0x00U) - { - USBD_LL_ClearStallEP(pdev, ep_addr); - } - USBD_CtlSendStatus(pdev); - } - break; - - default: - USBD_CtlError(pdev, req); - break; - } - break; - - case USB_REQ_GET_STATUS: - switch (pdev->dev_state) - { - case USBD_STATE_ADDRESSED: - if ((ep_addr != 0x00U) && (ep_addr != 0x80U)) - { - USBD_CtlError(pdev, req); - break; - } - pep = ((ep_addr & 0x80U) == 0x80U) ? &pdev->ep_in[ep_addr & 0x7FU]:\ - &pdev->ep_out[ep_addr & 0x7FU]; - - pep->status = 0x0000U; - - USBD_CtlSendData (pdev, (uint8_t *)(void *)&pep->status, 2U); - break; - - case USBD_STATE_CONFIGURED: - if((ep_addr & 0x80U) == 0x80U) - { - if (pdev->ep_in[ep_addr & 0xFU].is_used == 0U) - { - USBD_CtlError(pdev, req); - break; - } - } - else - { - if (pdev->ep_out[ep_addr & 0xFU].is_used == 0U) - { - USBD_CtlError(pdev, req); - break; - } - } - - pep = ((ep_addr & 0x80U) == 0x80U) ? &pdev->ep_in[ep_addr & 0x7FU]:\ - &pdev->ep_out[ep_addr & 0x7FU]; - - if ((ep_addr == 0x00U) || (ep_addr == 0x80U)) - { - pep->status = 0x0000U; - } - else if(USBD_LL_IsStallEP(pdev, ep_addr)) - { - pep->status = 0x0001U; - } - else - { - pep->status = 0x0000U; - } - - USBD_CtlSendData (pdev, (uint8_t *)(void *)&pep->status, 2U); - break; - - default: - USBD_CtlError(pdev, req); - break; - } - break; - - default: - USBD_CtlError(pdev, req); - break; - } - break; - - default: - USBD_CtlError(pdev, req); - break; - } - - return ret; -} -/** -* @brief USBD_GetDescriptor -* Handle Get Descriptor requests -* @param pdev: device instance -* @param req: usb request -* @retval status -*/ -static void USBD_GetDescriptor(USBD_HandleTypeDef *pdev , - USBD_SetupReqTypedef *req) -{ - uint16_t len; - uint8_t *pbuf; - - - switch (req->wValue >> 8) - { -#if (USBD_LPM_ENABLED == 1U) - case USB_DESC_TYPE_BOS: - pbuf = pdev->pDesc->GetBOSDescriptor(pdev->dev_speed, &len); - break; -#endif - case USB_DESC_TYPE_DEVICE: - pbuf = pdev->pDesc->GetDeviceDescriptor(pdev->dev_speed, &len); - break; - - case USB_DESC_TYPE_CONFIGURATION: - if(pdev->dev_speed == USBD_SPEED_HIGH ) - { - pbuf = (uint8_t *)pdev->pClass->GetHSConfigDescriptor(&len); - pbuf[1] = USB_DESC_TYPE_CONFIGURATION; - } - else - { - pbuf = (uint8_t *)pdev->pClass->GetFSConfigDescriptor(&len); - pbuf[1] = USB_DESC_TYPE_CONFIGURATION; - } - break; - - case USB_DESC_TYPE_STRING: - switch ((uint8_t)(req->wValue)) - { - case USBD_IDX_LANGID_STR: - pbuf = pdev->pDesc->GetLangIDStrDescriptor(pdev->dev_speed, &len); - break; - - case USBD_IDX_MFC_STR: - pbuf = pdev->pDesc->GetManufacturerStrDescriptor(pdev->dev_speed, &len); - break; - - case USBD_IDX_PRODUCT_STR: - pbuf = pdev->pDesc->GetProductStrDescriptor(pdev->dev_speed, &len); - break; - - case USBD_IDX_SERIAL_STR: - pbuf = pdev->pDesc->GetSerialStrDescriptor(pdev->dev_speed, &len); - break; - - case USBD_IDX_CONFIG_STR: - pbuf = pdev->pDesc->GetConfigurationStrDescriptor(pdev->dev_speed, &len); - break; - - case USBD_IDX_INTERFACE_STR: - pbuf = pdev->pDesc->GetInterfaceStrDescriptor(pdev->dev_speed, &len); - break; - - default: -#if (USBD_SUPPORT_USER_STRING == 1U) - pbuf = pdev->pClass->GetUsrStrDescriptor(pdev, (req->wValue) , &len); - break; -#else - USBD_CtlError(pdev , req); - return; -#endif - } - break; - case USB_DESC_TYPE_DEVICE_QUALIFIER: - - if(pdev->dev_speed == USBD_SPEED_HIGH) - { - pbuf = (uint8_t *)pdev->pClass->GetDeviceQualifierDescriptor(&len); - break; - } - else - { - USBD_CtlError(pdev , req); - return; - } - - case USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION: - if(pdev->dev_speed == USBD_SPEED_HIGH ) - { - pbuf = (uint8_t *)pdev->pClass->GetOtherSpeedConfigDescriptor(&len); - pbuf[1] = USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION; - break; - } - else - { - USBD_CtlError(pdev , req); - return; - } - - default: - USBD_CtlError(pdev , req); - return; - } - - if((len != 0U) && (req->wLength != 0U)) - { - - len = MIN(len, req->wLength); - - USBD_CtlSendData (pdev, pbuf, len); - } - - if(req->wLength == 0U) - { - USBD_CtlSendStatus(pdev); - } -} - -/** -* @brief USBD_SetAddress -* Set device address -* @param pdev: device instance -* @param req: usb request -* @retval status -*/ -static void USBD_SetAddress(USBD_HandleTypeDef *pdev , - USBD_SetupReqTypedef *req) -{ - uint8_t dev_addr; - - if ((req->wIndex == 0U) && (req->wLength == 0U) && (req->wValue < 128U)) - { - dev_addr = (uint8_t)(req->wValue) & 0x7FU; - - if (pdev->dev_state == USBD_STATE_CONFIGURED) - { - USBD_CtlError(pdev , req); - } - else - { - pdev->dev_address = dev_addr; - USBD_LL_SetUSBAddress(pdev, dev_addr); - USBD_CtlSendStatus(pdev); - - if (dev_addr != 0U) - { - pdev->dev_state = USBD_STATE_ADDRESSED; - } - else - { - pdev->dev_state = USBD_STATE_DEFAULT; - } - } - } - else - { - USBD_CtlError(pdev, req); - } -} - -/** -* @brief USBD_SetConfig -* Handle Set device configuration request -* @param pdev: device instance -* @param req: usb request -* @retval status -*/ -static void USBD_SetConfig(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req) -{ - static uint8_t cfgidx; - - cfgidx = (uint8_t)(req->wValue); - - if (cfgidx > USBD_MAX_NUM_CONFIGURATION) - { - USBD_CtlError(pdev, req); - } - else - { - switch (pdev->dev_state) - { - case USBD_STATE_ADDRESSED: - if (cfgidx) - { - pdev->dev_config = cfgidx; - pdev->dev_state = USBD_STATE_CONFIGURED; - if(USBD_SetClassConfig(pdev, cfgidx) == USBD_FAIL) - { - USBD_CtlError(pdev, req); - return; - } - USBD_CtlSendStatus(pdev); - } - else - { - USBD_CtlSendStatus(pdev); - } - break; - - case USBD_STATE_CONFIGURED: - if (cfgidx == 0U) - { - pdev->dev_state = USBD_STATE_ADDRESSED; - pdev->dev_config = cfgidx; - USBD_ClrClassConfig(pdev, cfgidx); - USBD_CtlSendStatus(pdev); - } - else if (cfgidx != pdev->dev_config) - { - /* Clear old configuration */ - USBD_ClrClassConfig(pdev, (uint8_t)pdev->dev_config); - - /* set new configuration */ - pdev->dev_config = cfgidx; - if(USBD_SetClassConfig(pdev, cfgidx) == USBD_FAIL) - { - USBD_CtlError(pdev, req); - return; - } - USBD_CtlSendStatus(pdev); - } - else - { - USBD_CtlSendStatus(pdev); - } - break; - - default: - USBD_CtlError(pdev, req); - USBD_ClrClassConfig(pdev, cfgidx); - break; - } - } -} - -/** -* @brief USBD_GetConfig -* Handle Get device configuration request -* @param pdev: device instance -* @param req: usb request -* @retval status -*/ -static void USBD_GetConfig(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req) -{ - if (req->wLength != 1U) - { - USBD_CtlError(pdev , req); - } - else - { - switch (pdev->dev_state) - { - case USBD_STATE_DEFAULT: - case USBD_STATE_ADDRESSED: - pdev->dev_default_config = 0U; - USBD_CtlSendData (pdev, (uint8_t *)(void *)&pdev->dev_default_config, 1U); - break; - - case USBD_STATE_CONFIGURED: - USBD_CtlSendData (pdev, (uint8_t *)(void *)&pdev->dev_config, 1U); - break; - - default: - USBD_CtlError(pdev , req); - break; - } - } -} - -/** -* @brief USBD_GetStatus -* Handle Get Status request -* @param pdev: device instance -* @param req: usb request -* @retval status -*/ -static void USBD_GetStatus(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req) -{ - switch (pdev->dev_state) - { - case USBD_STATE_DEFAULT: - case USBD_STATE_ADDRESSED: - case USBD_STATE_CONFIGURED: - if(req->wLength != 0x2U) - { - USBD_CtlError(pdev, req); - break; - } - -#if ( USBD_SELF_POWERED == 1U) - pdev->dev_config_status = USB_CONFIG_SELF_POWERED; -#else - pdev->dev_config_status = 0U; -#endif - - if (pdev->dev_remote_wakeup) - { - pdev->dev_config_status |= USB_CONFIG_REMOTE_WAKEUP; - } - - USBD_CtlSendData (pdev, (uint8_t *)(void *)&pdev->dev_config_status, 2U); - break; - - default : - USBD_CtlError(pdev , req); - break; - } -} - - -/** -* @brief USBD_SetFeature -* Handle Set device feature request -* @param pdev: device instance -* @param req: usb request -* @retval status -*/ -static void USBD_SetFeature(USBD_HandleTypeDef *pdev , - USBD_SetupReqTypedef *req) -{ - - if (req->wValue == USB_FEATURE_REMOTE_WAKEUP) - { - pdev->dev_remote_wakeup = 1U; - USBD_CtlSendStatus(pdev); - } - -} - - -/** -* @brief USBD_ClrFeature -* Handle clear device feature request -* @param pdev: device instance -* @param req: usb request -* @retval status -*/ -static void USBD_ClrFeature(USBD_HandleTypeDef *pdev , - USBD_SetupReqTypedef *req) -{ - switch (pdev->dev_state) - { - case USBD_STATE_DEFAULT: - case USBD_STATE_ADDRESSED: - case USBD_STATE_CONFIGURED: - if (req->wValue == USB_FEATURE_REMOTE_WAKEUP) - { - pdev->dev_remote_wakeup = 0U; - USBD_CtlSendStatus(pdev); - } - break; - - default : - USBD_CtlError(pdev , req); - break; - } -} - -/** -* @brief USBD_ParseSetupRequest -* Copy buffer into setup structure -* @param pdev: device instance -* @param req: usb request -* @retval None -*/ - -void USBD_ParseSetupRequest(USBD_SetupReqTypedef *req, uint8_t *pdata) -{ - req->bmRequest = *(uint8_t *) (pdata); - req->bRequest = *(uint8_t *) (pdata + 1); - req->wValue = SWAPBYTE (pdata + 2); - req->wIndex = SWAPBYTE (pdata + 4); - req->wLength = SWAPBYTE (pdata + 6); - -} - -/** -* @brief USBD_CtlError -* Handle USB low level Error -* @param pdev: device instance -* @param req: usb request -* @retval None -*/ - -void USBD_CtlError( USBD_HandleTypeDef *pdev , - USBD_SetupReqTypedef *req) -{ - USBD_LL_StallEP(pdev , 0x80U); - USBD_LL_StallEP(pdev , 0U); -} - - -/** - * @brief USBD_GetString - * Convert Ascii string into unicode one - * @param desc : descriptor buffer - * @param unicode : Formatted string buffer (unicode) - * @param len : descriptor length - * @retval None - */ -void USBD_GetString(uint8_t *desc, uint8_t *unicode, uint16_t *len) -{ - uint8_t idx = 0U; - - if (desc != NULL) - { - *len = (uint16_t)USBD_GetLen(desc) * 2U + 2U; - unicode[idx++] = *(uint8_t *)(void *)len; - unicode[idx++] = USB_DESC_TYPE_STRING; - - while (*desc != '\0') - { - unicode[idx++] = *desc++; - unicode[idx++] = 0U; - } - } -} - -/** - * @brief USBD_GetLen - * return the string length - * @param buf : pointer to the ascii string buffer - * @retval string length - */ -static uint8_t USBD_GetLen(uint8_t *buf) -{ - uint8_t len = 0U; - - while (*buf != '\0') - { - len++; - buf++; - } - - return len; -} -/** - * @} - */ - - -/** - * @} - */ - - -/** - * @} - */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_req.c + * @author MCD Application Team + * @brief This file provides the standard USB requests following chapter 9. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_ctlreq.h" +#include "usbd_ioreq.h" + +/** @addtogroup STM32_USBD_STATE_DEVICE_LIBRARY + * @{ + */ + +/** @defgroup USBD_REQ + * @brief USB standard requests module + * @{ + */ + +/** @defgroup USBD_REQ_Private_TypesDefinitions + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_REQ_Private_Defines + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_REQ_Private_Macros + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_REQ_Private_Variables + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_REQ_Private_FunctionPrototypes + * @{ + */ +static void USBD_GetDescriptor(USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req); + +static void USBD_SetAddress(USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req); + +static void USBD_SetConfig(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req); + +static void USBD_GetConfig(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req); + +static void USBD_GetStatus(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req); + +static void USBD_SetFeature(USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req); + +static void USBD_ClrFeature(USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req); + +static uint8_t USBD_GetLen(uint8_t *buf); + +/** + * @} + */ + +/** @defgroup USBD_REQ_Private_Functions + * @{ + */ + +/** + * @brief USBD_StdDevReq + * Handle standard usb device requests + * @param pdev: device instance + * @param req: usb request + * @retval status + */ +USBD_StatusTypeDef USBD_StdDevReq(USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req) +{ + USBD_StatusTypeDef ret = USBD_OK; + + switch (req->bmRequest & USB_REQ_TYPE_MASK) + { + case USB_REQ_TYPE_CLASS: + case USB_REQ_TYPE_VENDOR: + pdev->pClass->Setup(pdev, req); + break; + + case USB_REQ_TYPE_STANDARD: + + switch (req->bRequest) + { + case USB_REQ_GET_DESCRIPTOR: + + USBD_GetDescriptor(pdev, req); + break; + + case USB_REQ_SET_ADDRESS: + USBD_SetAddress(pdev, req); + break; + + case USB_REQ_SET_CONFIGURATION: + USBD_SetConfig(pdev, req); + break; + + case USB_REQ_GET_CONFIGURATION: + USBD_GetConfig(pdev, req); + break; + + case USB_REQ_GET_STATUS: + USBD_GetStatus(pdev, req); + break; + + case USB_REQ_SET_FEATURE: + USBD_SetFeature(pdev, req); + break; + + case USB_REQ_CLEAR_FEATURE: + USBD_ClrFeature(pdev, req); + break; + + default: + USBD_CtlError(pdev, req); + break; + } + break; + + default: + USBD_CtlError(pdev, req); + break; + } + + return ret; +} + +/** + * @brief USBD_StdItfReq + * Handle standard usb interface requests + * @param pdev: device instance + * @param req: usb request + * @retval status + */ +USBD_StatusTypeDef USBD_StdItfReq(USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req) +{ + USBD_StatusTypeDef ret = USBD_OK; + + switch (req->bmRequest & USB_REQ_TYPE_MASK) + { + case USB_REQ_TYPE_CLASS: + case USB_REQ_TYPE_VENDOR: + case USB_REQ_TYPE_STANDARD: + switch (pdev->dev_state) + { + case USBD_STATE_DEFAULT: + case USBD_STATE_ADDRESSED: + case USBD_STATE_CONFIGURED: + + if (LOBYTE(req->wIndex) <= USBD_MAX_NUM_INTERFACES) + { + ret = (USBD_StatusTypeDef)pdev->pClass->Setup(pdev, req); + + if ((req->wLength == 0U) && (ret == USBD_OK)) + { + USBD_CtlSendStatus(pdev); + } + } + else + { + USBD_CtlError(pdev, req); + } + break; + + default: + USBD_CtlError(pdev, req); + break; + } + break; + + default: + USBD_CtlError(pdev, req); + break; + } + + return ret; +} + +/** + * @brief USBD_StdEPReq + * Handle standard usb endpoint requests + * @param pdev: device instance + * @param req: usb request + * @retval status + */ +USBD_StatusTypeDef USBD_StdEPReq(USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req) +{ + + uint8_t ep_addr; + USBD_StatusTypeDef ret = USBD_OK; + USBD_EndpointTypeDef *pep; + ep_addr = LOBYTE(req->wIndex); + + switch (req->bmRequest & USB_REQ_TYPE_MASK) + { + + case USB_REQ_TYPE_CLASS: + case USB_REQ_TYPE_VENDOR: + pdev->pClass->Setup(pdev, req); + break; + + case USB_REQ_TYPE_STANDARD: + /* Check if it is a class request */ + if ((req->bmRequest & 0x60U) == 0x20U) + { + ret = (USBD_StatusTypeDef)pdev->pClass->Setup(pdev, req); + + return ret; + } + + switch (req->bRequest) + { + + case USB_REQ_SET_FEATURE: + + switch (pdev->dev_state) + { + case USBD_STATE_ADDRESSED: + if ((ep_addr != 0x00U) && (ep_addr != 0x80U)) + { + USBD_LL_StallEP(pdev, ep_addr); + USBD_LL_StallEP(pdev, 0x80U); + } + else + { + USBD_CtlError(pdev, req); + } + break; + + case USBD_STATE_CONFIGURED: + if (req->wValue == USB_FEATURE_EP_HALT) + { + if ((ep_addr != 0x00U) && (ep_addr != 0x80U) && + (req->wLength == 0x00U)) + { + USBD_LL_StallEP(pdev, ep_addr); + } + } + USBD_CtlSendStatus(pdev); + + break; + + default: + USBD_CtlError(pdev, req); + break; + } + break; + + case USB_REQ_CLEAR_FEATURE: + + switch (pdev->dev_state) + { + case USBD_STATE_ADDRESSED: + if ((ep_addr != 0x00U) && (ep_addr != 0x80U)) + { + USBD_LL_StallEP(pdev, ep_addr); + USBD_LL_StallEP(pdev, 0x80U); + } + else + { + USBD_CtlError(pdev, req); + } + break; + + case USBD_STATE_CONFIGURED: + if (req->wValue == USB_FEATURE_EP_HALT) + { + if ((ep_addr & 0x7FU) != 0x00U) + { + USBD_LL_ClearStallEP(pdev, ep_addr); + } + USBD_CtlSendStatus(pdev); + } + break; + + default: + USBD_CtlError(pdev, req); + break; + } + break; + + case USB_REQ_GET_STATUS: + switch (pdev->dev_state) + { + case USBD_STATE_ADDRESSED: + if ((ep_addr != 0x00U) && (ep_addr != 0x80U)) + { + USBD_CtlError(pdev, req); + break; + } + pep = ((ep_addr & 0x80U) == 0x80U) ? &pdev->ep_in[ep_addr & 0x7FU] + : &pdev->ep_out[ep_addr & 0x7FU]; + + pep->status = 0x0000U; + + USBD_CtlSendData(pdev, (uint8_t *)(void *)&pep->status, 2U); + break; + + case USBD_STATE_CONFIGURED: + if ((ep_addr & 0x80U) == 0x80U) + { + if (pdev->ep_in[ep_addr & 0xFU].is_used == 0U) + { + USBD_CtlError(pdev, req); + break; + } + } + else + { + if (pdev->ep_out[ep_addr & 0xFU].is_used == 0U) + { + USBD_CtlError(pdev, req); + break; + } + } + + pep = ((ep_addr & 0x80U) == 0x80U) ? &pdev->ep_in[ep_addr & 0x7FU] + : &pdev->ep_out[ep_addr & 0x7FU]; + + if ((ep_addr == 0x00U) || (ep_addr == 0x80U)) + { + pep->status = 0x0000U; + } + else if (USBD_LL_IsStallEP(pdev, ep_addr)) + { + pep->status = 0x0001U; + } + else + { + pep->status = 0x0000U; + } + + USBD_CtlSendData(pdev, (uint8_t *)(void *)&pep->status, 2U); + break; + + default: + USBD_CtlError(pdev, req); + break; + } + break; + + default: + USBD_CtlError(pdev, req); + break; + } + break; + + default: + USBD_CtlError(pdev, req); + break; + } + + return ret; +} + +/** + * @brief USBD_GetDescriptor + * Handle Get Descriptor requests + * @param pdev: device instance + * @param req: usb request + * @retval status + */ +static void USBD_GetDescriptor(USBD_HandleTypeDef *pdev, + USBD_SetupReqTypedef *req) +{ + uint16_t len; + uint8_t *pbuf; + + switch (req->wValue >> 8) + { +#if (USBD_LPM_ENABLED == 1U) + case USB_DESC_TYPE_BOS: + pbuf = pdev->pDesc->GetBOSDescriptor(pdev->dev_speed, &len); + break; +#endif + case USB_DESC_TYPE_DEVICE: + pbuf = pdev->pDesc->GetDeviceDescriptor(pdev->dev_speed, &len); + break; + + case USB_DESC_TYPE_CONFIGURATION: + if (pdev->dev_speed == USBD_SPEED_HIGH) + { + pbuf = pdev->pClass->GetHSConfigDescriptor(&len); + pbuf[1] = USB_DESC_TYPE_CONFIGURATION; + } + else + { + pbuf = pdev->pClass->GetFSConfigDescriptor(&len); + pbuf[1] = USB_DESC_TYPE_CONFIGURATION; + } + break; + + case USB_DESC_TYPE_STRING: + switch ((uint8_t)(req->wValue)) + { + case USBD_IDX_LANGID_STR: + pbuf = pdev->pDesc->GetLangIDStrDescriptor(pdev->dev_speed, &len); + break; + + case USBD_IDX_MFC_STR: + pbuf = pdev->pDesc->GetManufacturerStrDescriptor(pdev->dev_speed, &len); + break; + + case USBD_IDX_PRODUCT_STR: + pbuf = pdev->pDesc->GetProductStrDescriptor(pdev->dev_speed, &len); + break; + + case USBD_IDX_SERIAL_STR: + pbuf = pdev->pDesc->GetSerialStrDescriptor(pdev->dev_speed, &len); + break; + + case USBD_IDX_CONFIG_STR: + pbuf = pdev->pDesc->GetConfigurationStrDescriptor(pdev->dev_speed, &len); + break; + + case USBD_IDX_INTERFACE_STR: + pbuf = pdev->pDesc->GetInterfaceStrDescriptor(pdev->dev_speed, &len); + break; + + default: +#if (USBD_SUPPORT_USER_STRING == 1U) + pbuf = pdev->pClass->GetUsrStrDescriptor(pdev, (req->wValue), &len); + break; +#else + USBD_CtlError(pdev, req); + return; +#endif + } + break; + case USB_DESC_TYPE_DEVICE_QUALIFIER: + + if (pdev->dev_speed == USBD_SPEED_HIGH) + { + pbuf = pdev->pClass->GetDeviceQualifierDescriptor(&len); + break; + } + else + { + USBD_CtlError(pdev, req); + return; + } + + case USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION: + if (pdev->dev_speed == USBD_SPEED_HIGH) + { + pbuf = pdev->pClass->GetOtherSpeedConfigDescriptor(&len); + pbuf[1] = USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION; + break; + } + else + { + USBD_CtlError(pdev, req); + return; + } + + default: + USBD_CtlError(pdev, req); + return; + } + + if ((len != 0U) && (req->wLength != 0U)) + { + + len = MIN(len, req->wLength); + + USBD_CtlSendData(pdev, pbuf, len); + } + + if (req->wLength == 0U) + { + USBD_CtlSendStatus(pdev); + } +} + +/** + * @brief USBD_SetAddress + * Set device address + * @param pdev: device instance + * @param req: usb request + * @retval status + */ +static void USBD_SetAddress(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req) +{ + uint8_t dev_addr; + + if ((req->wIndex == 0U) && (req->wLength == 0U) && (req->wValue < 128U)) + { + dev_addr = (uint8_t) (req->wValue & 0x7FU); + + if (pdev->dev_state == USBD_STATE_CONFIGURED) + { + USBD_CtlError(pdev, req); + } + else + { + pdev->dev_address = dev_addr; + USBD_LL_SetUSBAddress(pdev, dev_addr); + USBD_CtlSendStatus(pdev); + + if (dev_addr != 0U) + { + pdev->dev_state = USBD_STATE_ADDRESSED; + } + else + { + pdev->dev_state = USBD_STATE_DEFAULT; + } + } + } + else + { + USBD_CtlError(pdev, req); + } +} + +/** + * @brief USBD_SetConfig + * Handle Set device configuration request + * @param pdev: device instance + * @param req: usb request + * @retval status + */ +static void USBD_SetConfig(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req) +{ + static uint8_t cfgidx; + + cfgidx = (uint8_t)(req->wValue); + + if (cfgidx > USBD_MAX_NUM_CONFIGURATION) + { + USBD_CtlError(pdev, req); + } + else + { + switch (pdev->dev_state) + { + case USBD_STATE_ADDRESSED: + if (cfgidx) + { + pdev->dev_config = cfgidx; + pdev->dev_state = USBD_STATE_CONFIGURED; + if (USBD_SetClassConfig(pdev, cfgidx) == USBD_FAIL) + { + USBD_CtlError(pdev, req); + return; + } + USBD_CtlSendStatus(pdev); + } + else + { + USBD_CtlSendStatus(pdev); + } + break; + + case USBD_STATE_CONFIGURED: + if (cfgidx == 0U) + { + pdev->dev_state = USBD_STATE_ADDRESSED; + pdev->dev_config = cfgidx; + USBD_ClrClassConfig(pdev, cfgidx); + USBD_CtlSendStatus(pdev); + } + else if (cfgidx != pdev->dev_config) + { + /* Clear old configuration */ + USBD_ClrClassConfig(pdev, (uint8_t)pdev->dev_config); + + /* set new configuration */ + pdev->dev_config = cfgidx; + if (USBD_SetClassConfig(pdev, cfgidx) == USBD_FAIL) + { + USBD_CtlError(pdev, req); + return; + } + USBD_CtlSendStatus(pdev); + } + else + { + USBD_CtlSendStatus(pdev); + } + break; + + default: + USBD_CtlError(pdev, req); + USBD_ClrClassConfig(pdev, cfgidx); + break; + } + } +} + +/** + * @brief USBD_GetConfig + * Handle Get device configuration request + * @param pdev: device instance + * @param req: usb request + * @retval status + */ +static void USBD_GetConfig(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req) +{ + if (req->wLength != 1U) + { + USBD_CtlError(pdev, req); + } + else + { + switch (pdev->dev_state) + { + case USBD_STATE_DEFAULT: + case USBD_STATE_ADDRESSED: + pdev->dev_default_config = 0U; + USBD_CtlSendData(pdev, (uint8_t *)(void *)&pdev->dev_default_config, 1U); + break; + + case USBD_STATE_CONFIGURED: + USBD_CtlSendData(pdev, (uint8_t *)(void *)&pdev->dev_config, 1U); + break; + + default: + USBD_CtlError(pdev, req); + break; + } + } +} + +/** + * @brief USBD_GetStatus + * Handle Get Status request + * @param pdev: device instance + * @param req: usb request + * @retval status + */ +static void USBD_GetStatus(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req) +{ + switch (pdev->dev_state) + { + case USBD_STATE_DEFAULT: + case USBD_STATE_ADDRESSED: + case USBD_STATE_CONFIGURED: + if (req->wLength != 0x2U) + { + USBD_CtlError(pdev, req); + break; + } + +#if (USBD_SELF_POWERED == 1U) + pdev->dev_config_status = USB_CONFIG_SELF_POWERED; +#else + pdev->dev_config_status = 0U; +#endif + + if (pdev->dev_remote_wakeup) + { + pdev->dev_config_status |= USB_CONFIG_REMOTE_WAKEUP; + } + + USBD_CtlSendData(pdev, (uint8_t *)(void *)&pdev->dev_config_status, 2U); + break; + + default: + USBD_CtlError(pdev, req); + break; + } +} + +/** + * @brief USBD_SetFeature + * Handle Set device feature request + * @param pdev: device instance + * @param req: usb request + * @retval status + */ +static void USBD_SetFeature(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req) +{ + + if (req->wValue == USB_FEATURE_REMOTE_WAKEUP) + { + pdev->dev_remote_wakeup = 1U; + USBD_CtlSendStatus(pdev); + } +} + +/** + * @brief USBD_ClrFeature + * Handle clear device feature request + * @param pdev: device instance + * @param req: usb request + * @retval status + */ +static void USBD_ClrFeature(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req) +{ + switch (pdev->dev_state) + { + case USBD_STATE_DEFAULT: + case USBD_STATE_ADDRESSED: + case USBD_STATE_CONFIGURED: + if (req->wValue == USB_FEATURE_REMOTE_WAKEUP) + { + pdev->dev_remote_wakeup = 0U; + USBD_CtlSendStatus(pdev); + } + break; + + default: + USBD_CtlError(pdev, req); + break; + } +} + +/** + * @brief USBD_ParseSetupRequest + * Copy buffer into setup structure + * @param pdev: device instance + * @param req: usb request + * @retval None + */ +void USBD_ParseSetupRequest(USBD_SetupReqTypedef *req, uint8_t *pdata) +{ + req->bmRequest = *pdata; + req->bRequest = *(pdata + 1); + req->wValue = *(uint16_t*)(pdata + 2); + req->wIndex = *(uint16_t*)(pdata + 4); + req->wLength = *(uint16_t*)(pdata + 6); +} + +/** + * @brief USBD_CtlError + * Handle USB low level Error + * @param pdev: device instance + * @param req: usb request + * @retval None + */ +void USBD_CtlError(USBD_HandleTypeDef *pdev, USBD_SetupReqTypedef *req) +{ + UNUSED(req); + USBD_LL_StallEP(pdev, 0x80U); + USBD_LL_StallEP(pdev, 0U); +} + +/** + * @brief USBD_GetString + * Convert Ascii string into unicode one + * @param desc : descriptor buffer + * @param unicode : Formatted string buffer (unicode) + * @param len : descriptor length + * @retval None + */ +void USBD_GetString(uint8_t *desc, uint8_t *unicode, uint16_t *len) +{ + uint8_t idx = 0U; + + if (desc != NULL) + { + *len = (uint16_t) (USBD_GetLen(desc) * 2U + 2U); + unicode[idx++] = *(uint8_t *)(void *)len; + unicode[idx++] = USB_DESC_TYPE_STRING; + + while (*desc != '\0') + { + unicode[idx++] = *desc++; + unicode[idx++] = 0U; + } + } +} + +/** + * @brief USBD_GetLen + * return the string length + * @param buf : pointer to the ascii string buffer + * @retval string length + */ +static uint8_t USBD_GetLen(uint8_t *buf) +{ + uint8_t len = 0U; + + while (*buf != '\0') + { + len++; + buf++; + } + + return len; +} + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_desc_template.c b/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_desc_template.c index e8fcd2d78d..bb8a7847a9 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_desc_template.c +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_desc_template.c @@ -1,274 +1,293 @@ -/** - ****************************************************************************** - * @file usbd_desc_template.c - * @author MCD Application Team - * @brief This file provides the USBD descriptors and string formatting method. - * This template should be copied to the user folder, renamed and customized - * following user needs. - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_core.h" -#include "usbd_desc.h" -#include "usbd_conf.h" - -/* Private typedef -----------------------------------------------------------*/ -/* Private define ------------------------------------------------------------*/ -#define USBD_VID 0x0483 -#define USBD_PID 0xaaaa /* Replace '0xaaaa' with your device product ID */ -#define USBD_LANGID_STRING 0xbbb /* Replace '0xbbb' with your device language ID */ -#define USBD_MANUFACTURER_STRING "xxxxx" /* Add your manufacturer string */ -#define USBD_PRODUCT_HS_STRING "xxxxx" /* Add your product High Speed string */ -#define USBD_PRODUCT_FS_STRING "xxxxx" /* Add your product Full Speed string */ -#define USBD_CONFIGURATION_HS_STRING "xxxxx" /* Add your configuration High Speed string */ -#define USBD_INTERFACE_HS_STRING "xxxxx" /* Add your Interface High Speed string */ -#define USBD_CONFIGURATION_FS_STRING "xxxxx" /* Add your configuration Full Speed string */ -#define USBD_INTERFACE_FS_STRING "xxxxx" /* Add your Interface Full Speed string */ - -/* Private macro -------------------------------------------------------------*/ -/* Private function prototypes -----------------------------------------------*/ -uint8_t *USBD_Class_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); -uint8_t *USBD_Class_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); -uint8_t *USBD_Class_ManufacturerStrDescriptor (USBD_SpeedTypeDef speed, uint16_t *length); -uint8_t *USBD_Class_ProductStrDescriptor (USBD_SpeedTypeDef speed, uint16_t *length); -uint8_t *USBD_Class_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); -uint8_t *USBD_Class_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); -uint8_t *USBD_Class_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); -#ifdef USB_SUPPORT_USER_STRING_DESC -uint8_t *USBD_Class_USRStringDesc (USBD_SpeedTypeDef speed, uint8_t idx, uint16_t *length); -#endif /* USB_SUPPORT_USER_STRING_DESC */ - -/* Private variables ---------------------------------------------------------*/ -USBD_DescriptorsTypeDef Class_Desc = { - USBD_Class_DeviceDescriptor, - USBD_Class_LangIDStrDescriptor, - USBD_Class_ManufacturerStrDescriptor, - USBD_Class_ProductStrDescriptor, - USBD_Class_SerialStrDescriptor, - USBD_Class_ConfigStrDescriptor, - USBD_Class_InterfaceStrDescriptor, -}; - -/* USB Standard Device Descriptor */ -#if defined ( __ICCARM__ ) /*!< IAR Compiler */ - #pragma data_alignment=4 -#endif -__ALIGN_BEGIN uint8_t USBD_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = { - 0x12, /* bLength */ - USB_DESC_TYPE_DEVICE, /* bDescriptorType */ - 0x00, /* bcdUSB */ - 0x02, - 0x00, /* bDeviceClass */ - 0x00, /* bDeviceSubClass */ - 0x00, /* bDeviceProtocol */ - USB_MAX_EP0_SIZE, /* bMaxPacketSize */ - LOBYTE(USBD_VID), /* idVendor */ - HIBYTE(USBD_VID), /* idVendor */ - LOBYTE(USBD_PID), /* idVendor */ - HIBYTE(USBD_PID), /* idVendor */ - 0x00, /* bcdDevice rel. 2.00 */ - 0x02, - USBD_IDX_MFC_STR, /* Index of manufacturer string */ - USBD_IDX_PRODUCT_STR, /* Index of product string */ - USBD_IDX_SERIAL_STR, /* Index of serial number string */ - USBD_MAX_NUM_CONFIGURATION /* bNumConfigurations */ -}; /* USB_DeviceDescriptor */ - -/* USB Standard Device Descriptor */ -#if defined ( __ICCARM__ ) /*!< IAR Compiler */ - #pragma data_alignment=4 -#endif -__ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END = { - USB_LEN_LANGID_STR_DESC, - USB_DESC_TYPE_STRING, - LOBYTE(USBD_LANGID_STRING), - HIBYTE(USBD_LANGID_STRING), -}; - -uint8_t USBD_StringSerial[USB_SIZ_STRING_SERIAL] = -{ - USB_SIZ_STRING_SERIAL, - USB_DESC_TYPE_STRING, -}; - -#if defined ( __ICCARM__ ) /*!< IAR Compiler */ - #pragma data_alignment=4 -#endif -__ALIGN_BEGIN uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END; - -/* Private functions ---------------------------------------------------------*/ -static void IntToUnicode (uint32_t value , uint8_t *pbuf , uint8_t len); -static void Get_SerialNum(void); - -/** - * @brief Returns the device descriptor. - * @param speed: Current device speed - * @param length: Pointer to data length variable - * @retval Pointer to descriptor buffer - */ -uint8_t *USBD_Class_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) -{ - *length = sizeof(USBD_DeviceDesc); - return (uint8_t*)USBD_DeviceDesc; -} - -/** - * @brief Returns the LangID string descriptor. - * @param speed: Current device speed - * @param length: Pointer to data length variable - * @retval Pointer to descriptor buffer - */ -uint8_t *USBD_Class_LangIDStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) -{ - *length = sizeof(USBD_LangIDDesc); - return (uint8_t*)USBD_LangIDDesc; -} - -/** - * @brief Returns the product string descriptor. - * @param speed: Current device speed - * @param length: Pointer to data length variable - * @retval Pointer to descriptor buffer - */ -uint8_t *USBD_Class_ProductStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) -{ - if(speed == USBD_SPEED_HIGH) - { - USBD_GetString((uint8_t *)USBD_PRODUCT_HS_STRING, USBD_StrDesc, length); - } - else - { - USBD_GetString((uint8_t *)USBD_PRODUCT_FS_STRING, USBD_StrDesc, length); - } - return USBD_StrDesc; -} - -/** - * @brief Returns the manufacturer string descriptor. - * @param speed: Current device speed - * @param length: Pointer to data length variable - * @retval Pointer to descriptor buffer - */ -uint8_t *USBD_Class_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) -{ - USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length); - return USBD_StrDesc; -} - -/** - * @brief Returns the serial number string descriptor. - * @param speed: Current device speed - * @param length: Pointer to data length variable - * @retval Pointer to descriptor buffer - */ -uint8_t *USBD_Class_SerialStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) -{ - *length = USB_SIZ_STRING_SERIAL; - - /* Update the serial number string descriptor with the data from the unique ID*/ - Get_SerialNum(); - - return (uint8_t*)USBD_StringSerial; -} - -/** - * @brief Returns the configuration string descriptor. - * @param speed: Current device speed - * @param length: Pointer to data length variable - * @retval Pointer to descriptor buffer - */ -uint8_t *USBD_Class_ConfigStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) -{ - if(speed == USBD_SPEED_HIGH) - { - USBD_GetString((uint8_t *)USBD_CONFIGURATION_HS_STRING, USBD_StrDesc, length); - } - else - { - USBD_GetString((uint8_t *)USBD_CONFIGURATION_FS_STRING, USBD_StrDesc, length); - } - return USBD_StrDesc; -} - -/** - * @brief Returns the interface string descriptor. - * @param speed: Current device speed - * @param length: Pointer to data length variable - * @retval Pointer to descriptor buffer - */ -uint8_t *USBD_Class_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) -{ - if(speed == USBD_SPEED_HIGH) - { - USBD_GetString((uint8_t *)USBD_INTERFACE_HS_STRING, USBD_StrDesc, length); - } - else - { - USBD_GetString((uint8_t *)USBD_INTERFACE_FS_STRING, USBD_StrDesc, length); - } - return USBD_StrDesc; -} - -/** - * @brief Create the serial number string descriptor - * @param None - * @retval None - */ -static void Get_SerialNum(void) -{ - uint32_t deviceserial0, deviceserial1, deviceserial2; - - deviceserial0 = *(uint32_t*)DEVICE_ID1; - deviceserial1 = *(uint32_t*)DEVICE_ID2; - deviceserial2 = *(uint32_t*)DEVICE_ID3; - - deviceserial0 += deviceserial2; - - if (deviceserial0 != 0) - { - IntToUnicode (deviceserial0, &USBD_StringSerial[2] ,8); - IntToUnicode (deviceserial1, &USBD_StringSerial[18] ,4); - } -} - -/** - * @brief Convert Hex 32Bits value into char - * @param value: value to convert - * @param pbuf: pointer to the buffer - * @param len: buffer length - * @retval None - */ -static void IntToUnicode (uint32_t value , uint8_t *pbuf , uint8_t len) -{ - uint8_t idx = 0; - - for( idx = 0 ; idx < len ; idx ++) - { - if( ((value >> 28)) < 0xA ) - { - pbuf[ 2* idx] = (value >> 28) + '0'; - } - else - { - pbuf[2* idx] = (value >> 28) + 'A' - 10; - } - - value = value << 4; - - pbuf[ 2* idx + 1] = 0; - } -} - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_desc_template.c + * @author MCD Application Team + * @brief This file provides the USBD descriptors and string formatting + * method. This template should be copied to the user folder, renamed + * and customized following user needs. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_core.h" +#include "usbd_desc.h" +#include "usbd_conf.h" + +/* Private typedef -----------------------------------------------------------*/ +/* Private define ------------------------------------------------------------*/ +#define USBD_VID 0x0483 +#define USBD_PID 0xaaaa /* Replace '0xaaaa' with your device product ID */ +#define USBD_LANGID_STRING 0xbbb /* Replace '0xbbb' with your device language ID */ +#define USBD_MANUFACTURER_STRING "xxxxx" /* Add your manufacturer string */ +#define USBD_PRODUCT_HS_STRING "xxxxx" /* Add your product High Speed string */ +#define USBD_PRODUCT_FS_STRING "xxxxx" /* Add your product Full Speed string */ +#define USBD_CONFIGURATION_HS_STRING "xxxxx" /* Add your configuration High Speed string */ +#define USBD_INTERFACE_HS_STRING "xxxxx" /* Add your Interface High Speed string */ +#define USBD_CONFIGURATION_FS_STRING "xxxxx" /* Add your configuration Full Speed string */ +#define USBD_INTERFACE_FS_STRING "xxxxx" /* Add your Interface Full Speed string */ + +/* Private macro -------------------------------------------------------------*/ +/* Private function prototypes -----------------------------------------------*/ +uint8_t *USBD_Class_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length); + +uint8_t *USBD_Class_LangIDStrDescriptor(USBD_SpeedTypeDef speed, + uint16_t *length); + +uint8_t *USBD_Class_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, + uint16_t *length); + +uint8_t *USBD_Class_ProductStrDescriptor(USBD_SpeedTypeDef speed, + uint16_t *length); + +uint8_t *USBD_Class_SerialStrDescriptor(USBD_SpeedTypeDef speed, + uint16_t *length); + +uint8_t *USBD_Class_ConfigStrDescriptor(USBD_SpeedTypeDef speed, + uint16_t *length); + +uint8_t *USBD_Class_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, + uint16_t *length); + +#ifdef USB_SUPPORT_USER_STRING_DESC +uint8_t *USBD_Class_USRStringDesc(USBD_SpeedTypeDef speed, uint8_t idx, + uint16_t *length); +#endif /* USB_SUPPORT_USER_STRING_DESC */ + +/* Private variables ---------------------------------------------------------*/ +USBD_DescriptorsTypeDef Class_Desc = { + USBD_Class_DeviceDescriptor, USBD_Class_LangIDStrDescriptor, + USBD_Class_ManufacturerStrDescriptor, USBD_Class_ProductStrDescriptor, + USBD_Class_SerialStrDescriptor, USBD_Class_ConfigStrDescriptor, + USBD_Class_InterfaceStrDescriptor, +}; + +/* USB Standard Device Descriptor */ +#if defined(__ICCARM__) /*!< IAR Compiler */ +#pragma data_alignment = 4 +#endif +__ALIGN_BEGIN uint8_t USBD_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = { + 0x12, /* bLength */ + USB_DESC_TYPE_DEVICE, /* bDescriptorType */ + 0x00, /* bcdUSB */ + 0x02, + 0x00, /* bDeviceClass */ + 0x00, /* bDeviceSubClass */ + 0x00, /* bDeviceProtocol */ + USB_MAX_EP0_SIZE, /* bMaxPacketSize */ + LOBYTE(USBD_VID), /* idVendor */ + HIBYTE(USBD_VID), /* idVendor */ + LOBYTE(USBD_PID), /* idVendor */ + HIBYTE(USBD_PID), /* idVendor */ + 0x00, /* bcdDevice rel. 2.00 */ + 0x02, + USBD_IDX_MFC_STR, /* Index of manufacturer string */ + USBD_IDX_PRODUCT_STR, /* Index of product string */ + USBD_IDX_SERIAL_STR, /* Index of serial number string */ + USBD_MAX_NUM_CONFIGURATION /* bNumConfigurations */ +}; /* USB_DeviceDescriptor */ + +/* USB Standard Device Descriptor */ +#if defined(__ICCARM__) /*!< IAR Compiler */ +#pragma data_alignment = 4 +#endif +__ALIGN_BEGIN uint8_t USBD_LangIDDesc[USB_LEN_LANGID_STR_DESC] __ALIGN_END = { + USB_LEN_LANGID_STR_DESC, + USB_DESC_TYPE_STRING, + LOBYTE(USBD_LANGID_STRING), + HIBYTE(USBD_LANGID_STRING), +}; + +uint8_t USBD_StringSerial[USB_SIZ_STRING_SERIAL] = { + USB_SIZ_STRING_SERIAL, + USB_DESC_TYPE_STRING, +}; + +#if defined(__ICCARM__) /*!< IAR Compiler */ +#pragma data_alignment = 4 +#endif +__ALIGN_BEGIN uint8_t USBD_StrDesc[USBD_MAX_STR_DESC_SIZ] __ALIGN_END; + +/* Private functions ---------------------------------------------------------*/ +static void IntToUnicode(uint32_t value, uint8_t *pbuf, uint8_t len); +static void Get_SerialNum(void); + +/** + * @brief Returns the device descriptor. + * @param speed: Current device speed + * @param length: Pointer to data length variable + * @retval Pointer to descriptor buffer + */ +uint8_t *USBD_Class_DeviceDescriptor(USBD_SpeedTypeDef speed, uint16_t *length) +{ + *length = sizeof(USBD_DeviceDesc); + return (uint8_t *)USBD_DeviceDesc; +} + +/** + * @brief Returns the LangID string descriptor. + * @param speed: Current device speed + * @param length: Pointer to data length variable + * @retval Pointer to descriptor buffer + */ +uint8_t *USBD_Class_LangIDStrDescriptor(USBD_SpeedTypeDef speed, + uint16_t *length) +{ + *length = sizeof(USBD_LangIDDesc); + return (uint8_t *)USBD_LangIDDesc; +} + +/** + * @brief Returns the product string descriptor. + * @param speed: Current device speed + * @param length: Pointer to data length variable + * @retval Pointer to descriptor buffer + */ +uint8_t *USBD_Class_ProductStrDescriptor(USBD_SpeedTypeDef speed, + uint16_t *length) +{ + if (speed == USBD_SPEED_HIGH) + { + USBD_GetString((uint8_t *)USBD_PRODUCT_HS_STRING, USBD_StrDesc, length); + } + else + { + USBD_GetString((uint8_t *)USBD_PRODUCT_FS_STRING, USBD_StrDesc, length); + } + return USBD_StrDesc; +} + +/** + * @brief Returns the manufacturer string descriptor. + * @param speed: Current device speed + * @param length: Pointer to data length variable + * @retval Pointer to descriptor buffer + */ +uint8_t *USBD_Class_ManufacturerStrDescriptor(USBD_SpeedTypeDef speed, + uint16_t *length) +{ + USBD_GetString((uint8_t *)USBD_MANUFACTURER_STRING, USBD_StrDesc, length); + return USBD_StrDesc; +} + +/** + * @brief Returns the serial number string descriptor. + * @param speed: Current device speed + * @param length: Pointer to data length variable + * @retval Pointer to descriptor buffer + */ +uint8_t *USBD_Class_SerialStrDescriptor(USBD_SpeedTypeDef speed, + uint16_t *length) +{ + *length = USB_SIZ_STRING_SERIAL; + + /* Update the serial number string descriptor with the data from the unique + * ID*/ + Get_SerialNum(); + + return (uint8_t *)USBD_StringSerial; +} + +/** + * @brief Returns the configuration string descriptor. + * @param speed: Current device speed + * @param length: Pointer to data length variable + * @retval Pointer to descriptor buffer + */ +uint8_t *USBD_Class_ConfigStrDescriptor(USBD_SpeedTypeDef speed, + uint16_t *length) +{ + if (speed == USBD_SPEED_HIGH) + { + USBD_GetString((uint8_t *)USBD_CONFIGURATION_HS_STRING, USBD_StrDesc, + length); + } + else + { + USBD_GetString((uint8_t *)USBD_CONFIGURATION_FS_STRING, USBD_StrDesc, + length); + } + return USBD_StrDesc; +} + +/** + * @brief Returns the interface string descriptor. + * @param speed: Current device speed + * @param length: Pointer to data length variable + * @retval Pointer to descriptor buffer + */ +uint8_t *USBD_Class_InterfaceStrDescriptor(USBD_SpeedTypeDef speed, + uint16_t *length) +{ + if (speed == USBD_SPEED_HIGH) + { + USBD_GetString((uint8_t *)USBD_INTERFACE_HS_STRING, USBD_StrDesc, length); + } + else + { + USBD_GetString((uint8_t *)USBD_INTERFACE_FS_STRING, USBD_StrDesc, length); + } + return USBD_StrDesc; +} + +/** + * @brief Create the serial number string descriptor + * @param None + * @retval None + */ +static void Get_SerialNum(void) +{ + uint32_t deviceserial0, deviceserial1, deviceserial2; + + deviceserial0 = *(uint32_t *)DEVICE_ID1; + deviceserial1 = *(uint32_t *)DEVICE_ID2; + deviceserial2 = *(uint32_t *)DEVICE_ID3; + + deviceserial0 += deviceserial2; + + if (deviceserial0 != 0) + { + IntToUnicode(deviceserial0, &USBD_StringSerial[2], 8); + IntToUnicode(deviceserial1, &USBD_StringSerial[18], 4); + } +} + +/** + * @brief Convert Hex 32Bits value into char + * @param value: value to convert + * @param pbuf: pointer to the buffer + * @param len: buffer length + * @retval None + */ +static void IntToUnicode(uint32_t value, uint8_t *pbuf, uint8_t len) +{ + uint8_t idx = 0; + + for (idx = 0; idx < len; idx++) + { + if (((value >> 28)) < 0xA) + { + pbuf[2 * idx] = (value >> 28) + '0'; + } + else + { + pbuf[2 * idx] = (value >> 28) + 'A' - 10; + } + + value = value << 4; + + pbuf[2 * idx + 1] = 0; + } +} + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ioreq.c b/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ioreq.c index 5600c37083..fb6c2d853f 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ioreq.c +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ioreq.c @@ -1,216 +1,215 @@ -/** - ****************************************************************************** - * @file usbd_ioreq.c - * @author MCD Application Team - * @brief This file provides the IO requests APIs for control endpoints. - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbd_ioreq.h" - -/** @addtogroup STM32_USB_DEVICE_LIBRARY - * @{ - */ - - -/** @defgroup USBD_IOREQ - * @brief control I/O requests module - * @{ - */ - -/** @defgroup USBD_IOREQ_Private_TypesDefinitions - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBD_IOREQ_Private_Defines - * @{ - */ - -/** - * @} - */ - - -/** @defgroup USBD_IOREQ_Private_Macros - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBD_IOREQ_Private_Variables - * @{ - */ - -/** - * @} - */ - - -/** @defgroup USBD_IOREQ_Private_FunctionPrototypes - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBD_IOREQ_Private_Functions - * @{ - */ - -/** -* @brief USBD_CtlSendData -* send data on the ctl pipe -* @param pdev: device instance -* @param buff: pointer to data buffer -* @param len: length of data to be sent -* @retval status -*/ -USBD_StatusTypeDef USBD_CtlSendData (USBD_HandleTypeDef *pdev, uint8_t *pbuf, - uint16_t len) -{ - /* Set EP0 State */ - pdev->ep0_state = USBD_EP0_DATA_IN; - pdev->ep_in[0].total_length = len; - pdev->ep_in[0].rem_length = len; - - /* Start the transfer */ - USBD_LL_Transmit (pdev, 0x00U, pbuf, len); - - return USBD_OK; -} - -/** -* @brief USBD_CtlContinueSendData -* continue sending data on the ctl pipe -* @param pdev: device instance -* @param buff: pointer to data buffer -* @param len: length of data to be sent -* @retval status -*/ -USBD_StatusTypeDef USBD_CtlContinueSendData (USBD_HandleTypeDef *pdev, - uint8_t *pbuf, uint16_t len) -{ - /* Start the next transfer */ - USBD_LL_Transmit (pdev, 0x00U, pbuf, len); - - return USBD_OK; -} - -/** -* @brief USBD_CtlPrepareRx -* receive data on the ctl pipe -* @param pdev: device instance -* @param buff: pointer to data buffer -* @param len: length of data to be received -* @retval status -*/ -USBD_StatusTypeDef USBD_CtlPrepareRx (USBD_HandleTypeDef *pdev, uint8_t *pbuf, - uint16_t len) -{ - /* Set EP0 State */ - pdev->ep0_state = USBD_EP0_DATA_OUT; - pdev->ep_out[0].total_length = len; - pdev->ep_out[0].rem_length = len; - - /* Start the transfer */ - USBD_LL_PrepareReceive (pdev, 0U, pbuf, len); - - return USBD_OK; -} - -/** -* @brief USBD_CtlContinueRx -* continue receive data on the ctl pipe -* @param pdev: device instance -* @param buff: pointer to data buffer -* @param len: length of data to be received -* @retval status -*/ -USBD_StatusTypeDef USBD_CtlContinueRx (USBD_HandleTypeDef *pdev, uint8_t *pbuf, - uint16_t len) -{ - USBD_LL_PrepareReceive(pdev, 0U, pbuf, len); - - return USBD_OK; -} - -/** -* @brief USBD_CtlSendStatus -* send zero lzngth packet on the ctl pipe -* @param pdev: device instance -* @retval status -*/ -USBD_StatusTypeDef USBD_CtlSendStatus (USBD_HandleTypeDef *pdev) -{ - /* Set EP0 State */ - pdev->ep0_state = USBD_EP0_STATUS_IN; - - /* Start the transfer */ - USBD_LL_Transmit(pdev, 0x00U, NULL, 0U); - - return USBD_OK; -} - -/** -* @brief USBD_CtlReceiveStatus -* receive zero lzngth packet on the ctl pipe -* @param pdev: device instance -* @retval status -*/ -USBD_StatusTypeDef USBD_CtlReceiveStatus (USBD_HandleTypeDef *pdev) -{ - /* Set EP0 State */ - pdev->ep0_state = USBD_EP0_STATUS_OUT; - - /* Start the transfer */ - USBD_LL_PrepareReceive (pdev, 0U, NULL, 0U); - - return USBD_OK; -} - -/** -* @brief USBD_GetRxCount -* returns the received data length -* @param pdev: device instance -* @param ep_addr: endpoint address -* @retval Rx Data blength -*/ -uint32_t USBD_GetRxCount (USBD_HandleTypeDef *pdev, uint8_t ep_addr) -{ - return USBD_LL_GetRxDataSize(pdev, ep_addr); -} - -/** - * @} - */ - - -/** - * @} - */ - - -/** - * @} - */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbd_ioreq.c + * @author MCD Application Team + * @brief This file provides the IO requests APIs for control endpoints. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbd_ioreq.h" + +/** @addtogroup STM32_USB_DEVICE_LIBRARY + * @{ + */ + +/** @defgroup USBD_IOREQ + * @brief control I/O requests module + * @{ + */ + +/** @defgroup USBD_IOREQ_Private_TypesDefinitions + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_IOREQ_Private_Defines + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_IOREQ_Private_Macros + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_IOREQ_Private_Variables + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_IOREQ_Private_FunctionPrototypes + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBD_IOREQ_Private_Functions + * @{ + */ + +/** + * @brief USBD_CtlSendData + * send data on the ctl pipe + * @param pdev: device instance + * @param buff: pointer to data buffer + * @param len: length of data to be sent + * @retval status + */ +USBD_StatusTypeDef USBD_CtlSendData(USBD_HandleTypeDef *pdev, + uint8_t *pbuf, + uint16_t len) +{ + /* Set EP0 State */ + pdev->ep0_state = USBD_EP0_DATA_IN; + pdev->ep_in[0].total_length = len; + pdev->ep_in[0].rem_length = len; + + /* Start the transfer */ + USBD_LL_Transmit(pdev, 0x00U, pbuf, len); + + return USBD_OK; +} + +/** + * @brief USBD_CtlContinueSendData + * continue sending data on the ctl pipe + * @param pdev: device instance + * @param buff: pointer to data buffer + * @param len: length of data to be sent + * @retval status + */ +USBD_StatusTypeDef USBD_CtlContinueSendData(USBD_HandleTypeDef *pdev, + uint8_t *pbuf, + uint16_t len) +{ + /* Start the next transfer */ + USBD_LL_Transmit(pdev, 0x00U, pbuf, len); + + return USBD_OK; +} + +/** + * @brief USBD_CtlPrepareRx + * receive data on the ctl pipe + * @param pdev: device instance + * @param buff: pointer to data buffer + * @param len: length of data to be received + * @retval status + */ +USBD_StatusTypeDef USBD_CtlPrepareRx(USBD_HandleTypeDef *pdev, + uint8_t *pbuf, + uint16_t len) +{ + /* Set EP0 State */ + pdev->ep0_state = USBD_EP0_DATA_OUT; + pdev->ep_out[0].total_length = len; + pdev->ep_out[0].rem_length = len; + + /* Start the transfer */ + USBD_LL_PrepareReceive(pdev, 0U, pbuf, len); + + return USBD_OK; +} + +/** + * @brief USBD_CtlContinueRx + * continue receive data on the ctl pipe + * @param pdev: device instance + * @param buff: pointer to data buffer + * @param len: length of data to be received + * @retval status + */ +USBD_StatusTypeDef USBD_CtlContinueRx(USBD_HandleTypeDef *pdev, + uint8_t *pbuf, + uint16_t len) +{ + USBD_LL_PrepareReceive(pdev, 0U, pbuf, len); + + return USBD_OK; +} + +/** + * @brief USBD_CtlSendStatus + * send zero lzngth packet on the ctl pipe + * @param pdev: device instance + * @retval status + */ +USBD_StatusTypeDef USBD_CtlSendStatus(USBD_HandleTypeDef *pdev) +{ + /* Set EP0 State */ + pdev->ep0_state = USBD_EP0_STATUS_IN; + + /* Start the transfer */ + USBD_LL_Transmit(pdev, 0x00U, NULL, 0U); + + return USBD_OK; +} + +/** + * @brief USBD_CtlReceiveStatus + * receive zero lzngth packet on the ctl pipe + * @param pdev: device instance + * @retval status + */ +USBD_StatusTypeDef USBD_CtlReceiveStatus(USBD_HandleTypeDef *pdev) +{ + /* Set EP0 State */ + pdev->ep0_state = USBD_EP0_STATUS_OUT; + + /* Start the transfer */ + USBD_LL_PrepareReceive(pdev, 0U, NULL, 0U); + + return USBD_OK; +} + +/** + * @brief USBD_GetRxCount + * returns the received data length + * @param pdev: device instance + * @param ep_addr: endpoint address + * @retval Rx Data blength + */ +uint32_t USBD_GetRxCount(USBD_HandleTypeDef *pdev, uint8_t ep_addr) +{ + return USBD_LL_GetRxDataSize(pdev, ep_addr); +} + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Device_Library/Release_Notes.html b/system/Middlewares/ST/STM32_USB_Device_Library/Release_Notes.html index 6f0551f231..150d9b80f8 100644 --- a/system/Middlewares/ST/STM32_USB_Device_Library/Release_Notes.html +++ b/system/Middlewares/ST/STM32_USB_Device_Library/Release_Notes.html @@ -1,1350 +1,1350 @@ - - - - - - - - - - - - - - - - - - -Release Notes for STM32 USB Device Library - - - - - - - - - -
- -

 

- -
- - - - - -
- - - - - - - -
-

Back to Release page

-
-

Release Notes for STM32 USB Device Library

-

Copyright - 2015 STMicroelectronics

-

-
-

 

- - - - -
-

Update History

-

V2.5.1 / 03-August-2018
-

- - - - - - - - - - - - - - - - - -

Main -Changes

- - - - - - - - - - - - - - - - - - - - - -
  • Update license section by adding path to get copy of ST Ultimate Liberty license
  • Core: Fix unexpected stall during status OUT phase
  • DFU Class:
    • rework hdfu struct to prevent unaligned addresses
  • MSC Class:
    • fix lba address overflow during large file transfers > 4Go
  • Template Class:
    • add missing Switch case Break on USBD_Template_Setup API

V2.5.0 / 15-December-2017
-

- - - - - - - - - - - - - - - - - -

Main -Changes

- - - - - - - - - - - - - - - - - - - - - -
  • Update license section
  • Update some functions to be MISRAC 2004 compliant
  • Add HS and OtherSpeed configuration descriptor for HID and CustomHID classes
  • Correct error handling in all class setup function
  • Add usbd_desc_template.c/ usbd_desc_template.h templates files
  • Add support of class and vendor request
  • CDC Class: fix zero-length packet issue in bulk IN transfer
  • Fix compilation warning with unused arguments for some functions
  • Improve USB Core enumeration state machine

V2.4.2 / 11-December-2015
-

- - - - - - - - - - - - - - - - - -

Main -Changes

- - - - - - - - - - - - - - - - - - - - - -
    -
  • CDC Class
    • usbd_cdc.c: change #include "USBD_CDC.h" by #include "usbd_cdc.h"
    -
-
- -

V2.4.1 / 19-June-2015
-

- - - - - - - - - - - - - - - -

Main -Changes

- - - - - - - - - - - - - - - - - - - -
    -
  • CDC Class
  • -
      -
    • usbd_cdc.c: comments update
    • -
    -
  • MSC Class
  • -
      -
    • usbd_msc_bot.h: update to be C++ compliant
    • -
    -
  • AUDIO Class
  • -
      -
    • usbd_audio.c: fix issue when Host sends GetInterface command it gets a wrong value
    • -
    -
      -
    • usbd_audio.c: remove useless management of DMA half transfer
      -
    • -
    -
- - - -

V2.4.0 / 28-February-2015
-

- - - - - - - - - - - - - -

Main -Changes

- - - - - - - - - - - - - - - - - -
    -
  • Core Driver
  • -
      -
    • Add support of Link Power Management (LPM): add new API GetBOSDescriptor(), that is used only if USBD_LPM_ENABLED switch is enabled in usbd_conf.h file
    • usbd_core.c: -Fix bug of unsupported premature Host Out stage during data In stage -(ie. when endpoint 0 maximum data size is 8 and Host requests -GetDeviceDescriptor for the first time)
    • usbd_ctlreq.c: Fix bug of unsupported Endpoint Class requests (ie. Audio SetCurrent request for endpoint sampling rate setting)
    • -
    -
  • HID Class
  • -
      -
    • Updating Polling time API USBD_HID_GetPollingInterval() to query this period for HS and FS
    • usbd_hid.c: Fix USBD_LL_CloseEP() function call in USBD_HID_DeInit() replacing endpoint size by endpoint address.
    • -
  • CDC Class
    • usbd_cdc.c: 
      • Add missing GetInterface request management in USBD_CDC_Setup() function
      • Update -USBD_CDC_Setup() function to allow correct user implementation of -CDC_SET_CONTROL_LINE_STATE and similar no-data setup requests.
    -
- -

V2.3.0 / 04-November-2014
-

- - - - - - - - - - - -

Main -Changes

- - - - - - - - - - - - - - - -
    -
  • Update all drivers to be C++ compliant
    -
  • -
  • CDC Class
  • -
      -
    • usbd_cdc.c: fix clear flag issue in USBD_CDC_TransmitPacket() function
    • -
    -
      -
    • usbd_cdc_if_template.c: update TEMPLATE_Receive() function header comment
      -
    • -
    -
  • Miscellaneous source code comments update
  • -
-

V2.2.0 / 13-June-2014

- - - - - - - - - -

Main -Changes

- - - - - - - - - - - - - -
    -
  • Source code comments review and update
  • -
  • HID class
  • -
      -
    • Remove unused API USBD_HID_DeviceQualifierDescriptor()
    • -
    • Add a new API in the HID class to query the poll time USBD_HID_GetPollingInterval()
      -
    • -
    - -
  • CDC class
  • -
      -
    • Bug fix: missing handling ZeroLength Setup request
    • -
    -
  • All classes
    -
  • - -
      -
    • Add alias for the class definition, it's defined as macro with capital letter
    • -
    -
-
ex. for the HID, the USBD_HID_CLASS macro is defined this way #define USBD_HID_CLASS  &USBD_HID
  and the application code can use the previous definition: &USBD_HID ex. USBD_RegisterClass(&USBD_Device, &USBD_HID) or the new USBD_HID_CLASS ex. USBD_RegisterClass(&USBD_Device, USBD_HID_CLASS)
-

V2.1.0 / 22-April-2014

- - - - - - - - -

Main -Changes

- - - - - - - - - - - -
    -
  • usbd_conf_template.c: update file with the right content (it was using MSC memory management layer)
    -
  • -
  • usbd_conf_template.h: change include of stm32f4xx.h by stm32xxx.h and add comment to inform user to adapt it to the device used
  • -
  • Several enhancements in CustomHID class
  • -
      -
    • Update the Custom HID class driver to simplify the link with user processes
    • -
    • Optimize the Custom HID class driver and reduce footprint
    • -
    • Add USBD_CUSTOM_HID_RegisterInterface() API to link user process to custom HID class
    • -
    • Add Custom HID interface template file usbd_customhid_if_template.c/h
    • -
    -
  • Miscellaneous comments update
    -
  • - -
- -

V2.0.0 / 18-February-2014

- - - - - -

Main -Changes

- - - - - - - - - -
    -
  • Major update -based on STM32Cube specification: Library Core, Classes architecture and APIs -modified vs. V1.1.0, and thus the 2 versions are not compatible.
    -
  • This version has to be used only with STM32Cube based development
  • -
- - -

V1.1.0 / 19-March-2012

-

Main -Changes

- -
  • Official support of STM32F4xx devices
  • All source files: license disclaimer text update and add link to the License file on ST Internet.
  • Handle test mode in the set feature request
  • Handle dynamically the USB SELF POWERED feature
  • Handle correctly the USBD_CtlError process to take into account error during Control OUT stage
  • Miscellaneous bug fix

V1.0.0 / 22-July-2011

Main -Changes

-
  • First official version for STM32F105/7xx and STM32F2xx devices

-

License

This -software component is licensed by ST under Ultimate Liberty license -SLA0044, the "License"; You may not use this component except in -compliance with the License. You may obtain a copy of the License at:

http://www.st.com/SLA0044

- -
-
-
-

For - complete documentation on STM32 - Microcontrollers visit www.st.com/STM32

-
-

-
- -
- -

 

- -
- - \ No newline at end of file + + + + + + + + + + + + + + + + + + +Release Notes for STM32 USB Device Library + + + + + + + + + +
+ +

 

+ +
+ + + + + +
+ + + + + + + +
+

Back to Release page

+
+

Release Notes for STM32 USB Device Library

+

Copyright + 2015 STMicroelectronics

+

+
+

 

+ + + + +
+

Update History

+

V2.5.1 / 03-August-2018
+

+ + + + + + + + + + + + + + + + + +

Main +Changes

+ + + + + + + + + + + + + + + + + + + + + +
  • Update license section by adding path to get copy of ST Ultimate Liberty license
  • Core: Fix unexpected stall during status OUT phase
  • DFU Class:
    • rework hdfu struct to prevent unaligned addresses
  • MSC Class:
    • fix lba address overflow during large file transfers > 4Go
  • Template Class:
    • add missing Switch case Break on USBD_Template_Setup API

V2.5.0 / 15-December-2017
+

+ + + + + + + + + + + + + + + + + +

Main +Changes

+ + + + + + + + + + + + + + + + + + + + + +
  • Update license section
  • Update some functions to be MISRAC 2004 compliant
  • Add HS and OtherSpeed configuration descriptor for HID and CustomHID classes
  • Correct error handling in all class setup function
  • Add usbd_desc_template.c/ usbd_desc_template.h templates files
  • Add support of class and vendor request
  • CDC Class: fix zero-length packet issue in bulk IN transfer
  • Fix compilation warning with unused arguments for some functions
  • Improve USB Core enumeration state machine

V2.4.2 / 11-December-2015
+

+ + + + + + + + + + + + + + + + + +

Main +Changes

+ + + + + + + + + + + + + + + + + + + + + +
    +
  • CDC Class
    • usbd_cdc.c: change #include "USBD_CDC.h" by #include "usbd_cdc.h"
    +
+
+ +

V2.4.1 / 19-June-2015
+

+ + + + + + + + + + + + + + + +

Main +Changes

+ + + + + + + + + + + + + + + + + + + +
    +
  • CDC Class
  • +
      +
    • usbd_cdc.c: comments update
    • +
    +
  • MSC Class
  • +
      +
    • usbd_msc_bot.h: update to be C++ compliant
    • +
    +
  • AUDIO Class
  • +
      +
    • usbd_audio.c: fix issue when Host sends GetInterface command it gets a wrong value
    • +
    +
      +
    • usbd_audio.c: remove useless management of DMA half transfer
      +
    • +
    +
+ + + +

V2.4.0 / 28-February-2015
+

+ + + + + + + + + + + + + +

Main +Changes

+ + + + + + + + + + + + + + + + + +
    +
  • Core Driver
  • +
      +
    • Add support of Link Power Management (LPM): add new API GetBOSDescriptor(), that is used only if USBD_LPM_ENABLED switch is enabled in usbd_conf.h file
    • usbd_core.c: +Fix bug of unsupported premature Host Out stage during data In stage +(ie. when endpoint 0 maximum data size is 8 and Host requests +GetDeviceDescriptor for the first time)
    • usbd_ctlreq.c: Fix bug of unsupported Endpoint Class requests (ie. Audio SetCurrent request for endpoint sampling rate setting)
    • +
    +
  • HID Class
  • +
      +
    • Updating Polling time API USBD_HID_GetPollingInterval() to query this period for HS and FS
    • usbd_hid.c: Fix USBD_LL_CloseEP() function call in USBD_HID_DeInit() replacing endpoint size by endpoint address.
    • +
  • CDC Class
    • usbd_cdc.c: 
      • Add missing GetInterface request management in USBD_CDC_Setup() function
      • Update +USBD_CDC_Setup() function to allow correct user implementation of +CDC_SET_CONTROL_LINE_STATE and similar no-data setup requests.
    +
+ +

V2.3.0 / 04-November-2014
+

+ + + + + + + + + + + +

Main +Changes

+ + + + + + + + + + + + + + + +
    +
  • Update all drivers to be C++ compliant
    +
  • +
  • CDC Class
  • +
      +
    • usbd_cdc.c: fix clear flag issue in USBD_CDC_TransmitPacket() function
    • +
    +
      +
    • usbd_cdc_if_template.c: update TEMPLATE_Receive() function header comment
      +
    • +
    +
  • Miscellaneous source code comments update
  • +
+

V2.2.0 / 13-June-2014

+ + + + + + + + + +

Main +Changes

+ + + + + + + + + + + + + +
    +
  • Source code comments review and update
  • +
  • HID class
  • +
      +
    • Remove unused API USBD_HID_DeviceQualifierDescriptor()
    • +
    • Add a new API in the HID class to query the poll time USBD_HID_GetPollingInterval()
      +
    • +
    + +
  • CDC class
  • +
      +
    • Bug fix: missing handling ZeroLength Setup request
    • +
    +
  • All classes
    +
  • + +
      +
    • Add alias for the class definition, it's defined as macro with capital letter
    • +
    +
+
ex. for the HID, the USBD_HID_CLASS macro is defined this way #define USBD_HID_CLASS  &USBD_HID
  and the application code can use the previous definition: &USBD_HID ex. USBD_RegisterClass(&USBD_Device, &USBD_HID) or the new USBD_HID_CLASS ex. USBD_RegisterClass(&USBD_Device, USBD_HID_CLASS)
+

V2.1.0 / 22-April-2014

+ + + + + + + + +

Main +Changes

+ + + + + + + + + + + +
    +
  • usbd_conf_template.c: update file with the right content (it was using MSC memory management layer)
    +
  • +
  • usbd_conf_template.h: change include of stm32f4xx.h by stm32xxx.h and add comment to inform user to adapt it to the device used
  • +
  • Several enhancements in CustomHID class
  • +
      +
    • Update the Custom HID class driver to simplify the link with user processes
    • +
    • Optimize the Custom HID class driver and reduce footprint
    • +
    • Add USBD_CUSTOM_HID_RegisterInterface() API to link user process to custom HID class
    • +
    • Add Custom HID interface template file usbd_customhid_if_template.c/h
    • +
    +
  • Miscellaneous comments update
    +
  • + +
+ +

V2.0.0 / 18-February-2014

+ + + + + +

Main +Changes

+ + + + + + + + + +
    +
  • Major update +based on STM32Cube specification: Library Core, Classes architecture and APIs +modified vs. V1.1.0, and thus the 2 versions are not compatible.
    +
  • This version has to be used only with STM32Cube based development
  • +
+ + +

V1.1.0 / 19-March-2012

+

Main +Changes

+ +
  • Official support of STM32F4xx devices
  • All source files: license disclaimer text update and add link to the License file on ST Internet.
  • Handle test mode in the set feature request
  • Handle dynamically the USB SELF POWERED feature
  • Handle correctly the USBD_CtlError process to take into account error during Control OUT stage
  • Miscellaneous bug fix

V1.0.0 / 22-July-2011

Main +Changes

+
  • First official version for STM32F105/7xx and STM32F2xx devices

+

License

This +software component is licensed by ST under Ultimate Liberty license +SLA0044, the "License"; You may not use this component except in +compliance with the License. You may obtain a copy of the License at:

http://www.st.com/SLA0044

+ +
+
+
+

For + complete documentation on STM32 + Microcontrollers visit www.st.com/STM32

+
+

+
+ +
+ +

 

+ +
+ + diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Class/AUDIO/Inc/usbh_audio.h b/system/Middlewares/ST/STM32_USB_Host_Library/Class/AUDIO/Inc/usbh_audio.h index 195ee44d8f..56348815c4 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Class/AUDIO/Inc/usbh_audio.h +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Class/AUDIO/Inc/usbh_audio.h @@ -1,586 +1,586 @@ -/** - ****************************************************************************** - * @file usbh_audio.h - * @author MCD Application Team - * @brief This file contains all the prototypes for the usbh_audio.c - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive ----------------------------------------------*/ -#ifndef __USBH_AUDIO_H -#define __USBH_AUDIO_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_core.h" - -/** @addtogroup USBH_LIB - * @{ - */ - -/** @addtogroup USBH_CLASS - * @{ - */ - -/** @addtogroup USBH_AUDIO_CLASS - * @{ - */ - -/** @defgroup USBH_AUDIO_CORE - * @brief This file is the Header file for usbh_audio.c - * @{ - */ - - -/** @defgroup USBH_AUDIO_CORE_Exported_Types - * @{ - */ - -/* States for AUDIO State Machine */ -typedef enum -{ - AUDIO_INIT = 0, - AUDIO_IDLE, - AUDIO_CS_REQUESTS, - AUDIO_SET_DEFAULT_FEATURE_UNIT, - AUDIO_SET_INTERFACE, - AUDIO_SET_STREAMING_INTERFACE, - AUDIO_SET_CUR1, - AUDIO_GET_MIN, - AUDIO_GET_MAX, - AUDIO_GET_RES, - AUDIO_GET_CUR1, - AUDIO_SET_CUR2, - AUDIO_GET_CUR2, - AUDIO_SET_CUR3, - AUDIO_SET_INTERFACE0, - AUDIO_SET_INTERFACE1, - AUDIO_SET_INTERFACE2, - AUDIO_ISOC_OUT, - AUDIO_ISOC_IN, - AUDIO_ISOC_POLL, - AUDIO_ERROR, -} -AUDIO_StateTypeDef; - -typedef enum -{ - AUDIO_REQ_INIT = 1, - AUDIO_REQ_IDLE, - AUDIO_REQ_SET_DEFAULT_IN_INTERFACE, - AUDIO_REQ_SET_DEFAULT_OUT_INTERFACE, - AUDIO_REQ_SET_IN_INTERFACE, - AUDIO_REQ_SET_OUT_INTERFACE, - AUDIO_REQ_CS_REQUESTS, -} -AUDIO_ReqStateTypeDef; - -typedef enum -{ - AUDIO_REQ_SET_VOLUME = 1, - AUDIO_REQ_SET_MUTE, - AUDIO_REQ_GET_CURR_VOLUME, - AUDIO_REQ_GET_MIN_VOLUME, - AUDIO_REQ_GET_MAX_VOLUME, - AUDIO_REQ_GET_VOLUME, - AUDIO_REQ_GET_RESOLUTION, - AUDIO_REQ_CS_IDLE, -} -AUDIO_CSReqStateTypeDef; - -typedef enum -{ - AUDIO_PLAYBACK_INIT = 1, - AUDIO_PLAYBACK_SET_EP, - AUDIO_PLAYBACK_SET_EP_FREQ, - AUDIO_PLAYBACK_PLAY, - AUDIO_PLAYBACK_IDLE, -} -AUDIO_PlayStateTypeDef; - -typedef enum -{ - VOLUME_UP = 1, - VOLUME_DOWN = 2, -} -AUDIO_VolumeCtrlTypeDef; - -typedef enum -{ - AUDIO_CONTROL_INIT = 1, - AUDIO_CONTROL_CHANGE, - AUDIO_CONTROL_IDLE, - AUDIO_CONTROL_VOLUME_UP, - AUDIO_CONTROL_VOLUME_DOWN, -} -AUDIO_ControlStateTypeDef; - - -typedef enum -{ - AUDIO_DATA_START_OUT = 1, - AUDIO_DATA_OUT, -} -AUDIO_ProcessingTypeDef; - -/* Structure for AUDIO process */ -typedef struct -{ - uint8_t Channels; - uint8_t Bits; - uint32_t SampleRate; -} -AUDIO_FormatTypeDef; - -typedef struct -{ - uint8_t Ep; - uint16_t EpSize; - uint8_t AltSettings; - uint8_t interface; - uint8_t valid; - uint16_t Poll; -} -AUDIO_STREAMING_IN_HandleTypeDef; - -typedef struct -{ - uint8_t Ep; - uint16_t EpSize; - uint8_t AltSettings; - uint8_t interface; - uint8_t valid; - uint16_t Poll; -} -AUDIO_STREAMING_OUT_HandleTypeDef; - - -typedef struct -{ - uint8_t mute; - uint32_t volumeMin; - uint32_t volumeMax; - uint32_t volume; - uint32_t resolution; -} -AUDIO_ControlAttributeTypeDef; - -typedef struct -{ - - uint8_t Ep; - uint16_t EpSize; - uint8_t interface; - uint8_t AltSettings; - uint8_t supported; - - uint8_t Pipe; - uint8_t Poll; - uint32_t timer ; - - uint8_t asociated_as; - uint8_t asociated_mixer; - uint8_t asociated_selector; - uint8_t asociated_feature; - uint8_t asociated_terminal; - uint8_t asociated_channels; - - uint32_t frequency; - uint8_t *buf; - uint8_t *cbuf; - uint32_t partial_ptr; - - uint32_t global_ptr; - uint16_t frame_length; - uint32_t total_length; - - AUDIO_ControlAttributeTypeDef attribute; -} -AUDIO_InterfaceStreamPropTypeDef; - -typedef struct -{ - - uint8_t Ep; - uint16_t EpSize; - uint8_t interface; - uint8_t supported; - - uint8_t Pipe; - uint8_t Poll; - uint32_t timer ; -} -AUDIO_InterfaceControlPropTypeDef; - - -#define AUDIO_MAX_AUDIO_STD_INTERFACE 0x05U -#define AUDIO_MAX_FREQ_SUPPORTED 0x05U -#define AUDIO_MAX_STREAMING_INTERFACE 0x05U -#define AUDIO_MAX_NUM_IN_TERMINAL 0x04U -#define AUDIO_MAX_NUM_OUT_TERMINAL 0x04U -#define AUDIO_MAX_NUM_FEATURE_UNIT 0x04U -#define AUDIO_MAX_NUM_MIXER_UNIT 0x04U -#define AUDIO_MAX_NUM_SELECTOR_UNIT 0x04U - -#define HEADPHONE_SUPPORTED 0x01U -#define MICROPHONE_SUPPORTED 0x02U -#define HEADSET_SUPPORTED 0x03U - -#define AUDIO_MAX_SAMFREQ_NBR 5U -#define AUDIO_MAX_INTERFACE_NBR 5U -#define AUDIO_MAX_CONTROLS_NBR 5U - -/*Class-Specific AS(Audio Streaming) Interface Descriptor*/ -typedef struct -{ - uint8_t bLength; - uint8_t bDescriptorType; - uint8_t bDescriptorSubtype; - uint8_t bTerminalLink; - uint8_t bDelay; - uint8_t wFormatTag[2]; -} -AUDIO_ASGeneralDescTypeDef; - -/*Class-Specific AS(Audio Streaming) Format Type Descriptor*/ -typedef struct -{ - uint8_t bLength; - uint8_t bDescriptorType; - uint8_t bDescriptorSubtype; - uint8_t bFormatType; - uint8_t bNrChannels; - uint8_t bSubframeSize; - uint8_t bBitResolution; - uint8_t bSamFreqType; - uint8_t tSamFreq[AUDIO_MAX_SAMFREQ_NBR][3]; -} -AUDIO_ASFormatTypeDescTypeDef; - -/*Class-Specific AS(Audio Streaming) Interface Descriptor*/ -typedef struct -{ - AUDIO_ASGeneralDescTypeDef *GeneralDesc; - AUDIO_ASFormatTypeDescTypeDef *FormatTypeDesc; -} -AUDIO_ASDescTypeDef; - -/* 4.3.2 Class-Specific AC Interface Descriptor */ - -typedef struct -{ - uint8_t bLength; - uint8_t bDescriptorType; - uint8_t bDescriptorSubtype; - uint8_t bcdADC[2]; - uint8_t wTotalLength[2]; - uint8_t bInCollection; - uint8_t baInterfaceNr[AUDIO_MAX_INTERFACE_NBR]; -} -AUDIO_HeaderDescTypeDef; - -/* 4.3.2.1 Input Terminal Descriptor */ -typedef struct -{ - uint8_t bLength; - uint8_t bDescriptorType; - uint8_t bDescriptorSubtype; - uint8_t bTerminalID; - uint8_t wTerminalType[2]; - uint8_t bAssocTerminal; - uint8_t bNrChannels; - uint8_t wChannelConfig[2]; - uint8_t iChannelNames; - uint8_t iTerminal; -} -AUDIO_ITDescTypeDef; - -/* 4.3.2.2 Output Terminal Descriptor */ -typedef struct -{ - uint8_t bLength; - uint8_t bDescriptorType; - uint8_t bDescriptorSubtype; - uint8_t bTerminalID; - uint8_t wTerminalType[2]; - uint8_t bAssocTerminal; - uint8_t bSourceID; - uint8_t iTerminal; -} -AUDIO_OTDescTypeDef; - -/* 4.3.2.3 Feature Descriptor */ -typedef struct -{ - uint8_t bLength; - uint8_t bDescriptorType; - uint8_t bDescriptorSubtype; - uint8_t bUnitID; - uint8_t bSourceID; - uint8_t bControlSize; - uint8_t bmaControls[AUDIO_MAX_CONTROLS_NBR][2]; -} -AUDIO_FeatureDescTypeDef; - - -/* 4.3.2.3 Feature Descriptor */ -typedef struct -{ - uint8_t bLength; - uint8_t bDescriptorType; - uint8_t bDescriptorSubtype; - uint8_t bUnitID; - uint8_t bNrInPins; - uint8_t bSourceID0; - uint8_t bSourceID1; - uint8_t bNrChannels; - uint8_t bmChannelsConfig[2]; - uint8_t iChannelsNames; - uint8_t bmaControls; - uint8_t iMixer; -} -AUDIO_MixerDescTypeDef; - - - -/* 4.3.2.3 Feature Descriptor */ -typedef struct -{ - uint8_t bLength; - uint8_t bDescriptorType; - uint8_t bDescriptorSubtype; - uint8_t bUnitID; - uint8_t bNrInPins; - uint8_t bSourceID0; - uint8_t iSelector; -} -AUDIO_SelectorDescTypeDef; - -/*Class-Specific AC(Audio Control) Interface Descriptor*/ -typedef struct -{ - AUDIO_HeaderDescTypeDef *HeaderDesc; - AUDIO_ITDescTypeDef *InputTerminalDesc [AUDIO_MAX_NUM_IN_TERMINAL]; - AUDIO_OTDescTypeDef *OutputTerminalDesc[AUDIO_MAX_NUM_OUT_TERMINAL]; - AUDIO_FeatureDescTypeDef *FeatureUnitDesc [AUDIO_MAX_NUM_FEATURE_UNIT]; - AUDIO_MixerDescTypeDef *MixerUnitDesc [AUDIO_MAX_NUM_MIXER_UNIT]; - AUDIO_SelectorDescTypeDef *SelectorUnitDesc [AUDIO_MAX_NUM_SELECTOR_UNIT]; -} -AUDIO_ACDescTypeDef; - -/*Class-Specific AC : Global descriptor*/ - -typedef struct -{ - AUDIO_ACDescTypeDef cs_desc; /* Only one control descriptor*/ - AUDIO_ASDescTypeDef as_desc[AUDIO_MAX_STREAMING_INTERFACE]; - - uint16_t ASNum; - uint16_t InputTerminalNum; - uint16_t OutputTerminalNum; - uint16_t FeatureUnitNum; - uint16_t SelectorUnitNum; - uint16_t MixerUnitNum; -} -AUDIO_ClassSpecificDescTypedef; - - -typedef struct _AUDIO_Process -{ - AUDIO_ReqStateTypeDef req_state; - AUDIO_CSReqStateTypeDef cs_req_state; - AUDIO_PlayStateTypeDef play_state; - AUDIO_ControlStateTypeDef control_state; - AUDIO_ProcessingTypeDef processing_state; - - AUDIO_STREAMING_IN_HandleTypeDef stream_in[AUDIO_MAX_AUDIO_STD_INTERFACE]; - AUDIO_STREAMING_OUT_HandleTypeDef stream_out[AUDIO_MAX_AUDIO_STD_INTERFACE]; - AUDIO_ClassSpecificDescTypedef class_desc; - - AUDIO_InterfaceStreamPropTypeDef headphone; - AUDIO_InterfaceStreamPropTypeDef microphone; - AUDIO_InterfaceControlPropTypeDef control; - uint16_t mem [8]; - uint8_t temp_feature; - uint8_t temp_channels; -} -AUDIO_HandleTypeDef; - -/** - * @} - */ - -/** @defgroup USBH_AUDIO_CORE_Exported_Defines - * @{ - */ - - -/*Audio Interface Subclass Codes*/ -#define AC_CLASS 0x01U - -/* A.2 Audio Interface Subclass Codes */ -#define USB_SUBCLASS_AUDIOCONTROL 0x01U -#define USB_SUBCLASS_AUDIOSTREAMING 0x02U -#define USB_SUBCLASS_MIDISTREAMING 0x03U - -#define USB_DESC_TYPE_CS_INTERFACE 0x24U -#define USB_DESC_TYPE_CS_ENDPOINT 0x25U - -/* A.5 Audio Class-Specific AC Interface Descriptor Subtypes */ -#define UAC_HEADER 0x01U -#define UAC_INPUT_TERMINAL 0x02U -#define UAC_OUTPUT_TERMINAL 0x03U -#define UAC_MIXER_UNIT 0x04U -#define UAC_SELECTOR_UNIT 0x05U -#define UAC_FEATURE_UNIT 0x06U -#define UAC_PROCESSING_UNIT 0x07U -#define UAC_EXTENSION_UNIT 0x08U - -/*Audio Class-Specific Endpoint Descriptor Subtypes*/ -#define EP_CONTROL_UNDEFINED 0x00U -#define SAMPLING_FREQ_CONTROL 0x01U -#define PITCH_CONTROL 0x02U - -/*Feature unit control selector*/ -#define FU_CONTROL_UNDEFINED 0x00U -#define MUTE_CONTROL 0x01U -#define VOLUME_CONTROL 0x02U -#define BASS_CONTROL 0x03U -#define MID_CONTROL 0x04U -#define TREBLE_CONTROL 0x05U -#define GRAPHIC_EQUALIZER_CONTROL 0x06U -#define AUTOMATIC_GAIN_CONTROL 0x07U -#define DELAY_CONTROL 0x08U -#define BASS_BOOST_CONTROL 0x09U -#define LOUDNESS_CONTROL 0x0AU - -/*Terminal control selector*/ -#define TE_CONTROL_UNDEFINED 0x00U -#define COPY_PROTECT_CONTROL 0x01U - - -/* A.6 Audio Class-Specific AS Interface Descriptor Subtypes */ -#define UAC_AS_GENERAL 0x01U -#define UAC_FORMAT_TYPE 0x02U -#define UAC_FORMAT_SPECIFIC 0x03U - -/* A.8 Audio Class-Specific Endpoint Descriptor Subtypes */ -#define UAC_EP_GENERAL 0x01U - -/* A.9 Audio Class-Specific Request Codes */ -#define UAC_SET_ 0x00U -#define UAC_GET_ 0x80U - -#define UAC__CUR 0x01U -#define UAC__MIN 0x02U -#define UAC__MAX 0x03U -#define UAC__RES 0x04U -#define UAC__MEM 0x05U - -#define UAC_SET_CUR (UAC_SET_ | UAC__CUR) -#define UAC_GET_CUR (UAC_GET_ | UAC__CUR) -#define UAC_SET_MIN (UAC_SET_ | UAC__MIN) -#define UAC_GET_MIN (UAC_GET_ | UAC__MIN) -#define UAC_SET_MAX (UAC_SET_ | UAC__MAX) -#define UAC_GET_MAX (UAC_GET_ | UAC__MAX) -#define UAC_SET_RES (UAC_SET_ | UAC__RES) -#define UAC_GET_RES (UAC_GET_ | UAC__RES) -#define UAC_SET_MEM (UAC_SET_ | UAC__MEM) -#define UAC_GET_MEM (UAC_GET_ | UAC__MEM) - -#define UAC_GET_STAT 0xffU - -/* MIDI - A.1 MS Class-Specific Interface Descriptor Subtypes */ -#define UAC_MS_HEADER 0x01U -#define UAC_MIDI_IN_JACK 0x02U -#define UAC_MIDI_OUT_JACK 0x03U - -/* MIDI - A.1 MS Class-Specific Endpoint Descriptor Subtypes */ -#define UAC_MS_GENERAL 0x01U - -/* Terminals - 2.1 USB Terminal Types */ -#define UAC_TERMINAL_UNDEFINED 0x100U -#define UAC_TERMINAL_STREAMING 0x101U -#define UAC_TERMINAL_VENDOR_SPEC 0x1FFU - -/** - * @} - */ - -/** @defgroup USBH_AUDIO_CORE_Exported_Macros - * @{ - */ -/** - * @} - */ - -/** @defgroup USBH_AUDIO_CORE_Exported_Variables - * @{ - */ -extern USBH_ClassTypeDef AUDIO_Class; -#define USBH_AUDIO_CLASS &AUDIO_Class -/** - * @} - */ - -/** @defgroup USBH_AUDIO_CORE_Exported_FunctionsPrototype - * @{ - */ -USBH_StatusTypeDef USBH_AUDIO_SetFrequency (USBH_HandleTypeDef *phost, - uint16_t SampleRate, - uint8_t NbrChannels, - uint8_t BitPerSample); - -USBH_StatusTypeDef USBH_AUDIO_Play (USBH_HandleTypeDef *phost, uint8_t *buf, uint32_t length); -USBH_StatusTypeDef USBH_AUDIO_Stop (USBH_HandleTypeDef *phost); -USBH_StatusTypeDef USBH_AUDIO_Suspend (USBH_HandleTypeDef *phost); -USBH_StatusTypeDef USBH_AUDIO_Resume (USBH_HandleTypeDef *phost); -USBH_StatusTypeDef USBH_AUDIO_SetVolume (USBH_HandleTypeDef *phost, AUDIO_VolumeCtrlTypeDef volume_ctl); -USBH_StatusTypeDef USBH_AUDIO_ChangeOutBuffer (USBH_HandleTypeDef *phost, uint8_t *buf); -int32_t USBH_AUDIO_GetOutOffset (USBH_HandleTypeDef *phost); - -void USBH_AUDIO_FrequencySet(USBH_HandleTypeDef *phost); - -#define USBH_AUDIO_FrequencySetCallback USBH_AUDIO_FrequencySet -void USBH_AUDIO_BufferEmptyCallback(USBH_HandleTypeDef *phost); -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USBH_AUDIO_H */ - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - +/** + ****************************************************************************** + * @file usbh_audio.h + * @author MCD Application Team + * @brief This file contains all the prototypes for the usbh_audio.c + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive ----------------------------------------------*/ +#ifndef __USBH_AUDIO_H +#define __USBH_AUDIO_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_core.h" + +/** @addtogroup USBH_LIB + * @{ + */ + +/** @addtogroup USBH_CLASS + * @{ + */ + +/** @addtogroup USBH_AUDIO_CLASS + * @{ + */ + +/** @defgroup USBH_AUDIO_CORE + * @brief This file is the Header file for usbh_audio.c + * @{ + */ + + +/** @defgroup USBH_AUDIO_CORE_Exported_Types + * @{ + */ + +/* States for AUDIO State Machine */ +typedef enum +{ + AUDIO_INIT = 0, + AUDIO_IDLE, + AUDIO_CS_REQUESTS, + AUDIO_SET_DEFAULT_FEATURE_UNIT, + AUDIO_SET_INTERFACE, + AUDIO_SET_STREAMING_INTERFACE, + AUDIO_SET_CUR1, + AUDIO_GET_MIN, + AUDIO_GET_MAX, + AUDIO_GET_RES, + AUDIO_GET_CUR1, + AUDIO_SET_CUR2, + AUDIO_GET_CUR2, + AUDIO_SET_CUR3, + AUDIO_SET_INTERFACE0, + AUDIO_SET_INTERFACE1, + AUDIO_SET_INTERFACE2, + AUDIO_ISOC_OUT, + AUDIO_ISOC_IN, + AUDIO_ISOC_POLL, + AUDIO_ERROR, +} +AUDIO_StateTypeDef; + +typedef enum +{ + AUDIO_REQ_INIT = 1, + AUDIO_REQ_IDLE, + AUDIO_REQ_SET_DEFAULT_IN_INTERFACE, + AUDIO_REQ_SET_DEFAULT_OUT_INTERFACE, + AUDIO_REQ_SET_IN_INTERFACE, + AUDIO_REQ_SET_OUT_INTERFACE, + AUDIO_REQ_CS_REQUESTS, +} +AUDIO_ReqStateTypeDef; + +typedef enum +{ + AUDIO_REQ_SET_VOLUME = 1, + AUDIO_REQ_SET_MUTE, + AUDIO_REQ_GET_CURR_VOLUME, + AUDIO_REQ_GET_MIN_VOLUME, + AUDIO_REQ_GET_MAX_VOLUME, + AUDIO_REQ_GET_VOLUME, + AUDIO_REQ_GET_RESOLUTION, + AUDIO_REQ_CS_IDLE, +} +AUDIO_CSReqStateTypeDef; + +typedef enum +{ + AUDIO_PLAYBACK_INIT = 1, + AUDIO_PLAYBACK_SET_EP, + AUDIO_PLAYBACK_SET_EP_FREQ, + AUDIO_PLAYBACK_PLAY, + AUDIO_PLAYBACK_IDLE, +} +AUDIO_PlayStateTypeDef; + +typedef enum +{ + VOLUME_UP = 1, + VOLUME_DOWN = 2, +} +AUDIO_VolumeCtrlTypeDef; + +typedef enum +{ + AUDIO_CONTROL_INIT = 1, + AUDIO_CONTROL_CHANGE, + AUDIO_CONTROL_IDLE, + AUDIO_CONTROL_VOLUME_UP, + AUDIO_CONTROL_VOLUME_DOWN, +} +AUDIO_ControlStateTypeDef; + + +typedef enum +{ + AUDIO_DATA_START_OUT = 1, + AUDIO_DATA_OUT, +} +AUDIO_ProcessingTypeDef; + +/* Structure for AUDIO process */ +typedef struct +{ + uint8_t Channels; + uint8_t Bits; + uint32_t SampleRate; +} +AUDIO_FormatTypeDef; + +typedef struct +{ + uint8_t Ep; + uint16_t EpSize; + uint8_t AltSettings; + uint8_t interface; + uint8_t valid; + uint16_t Poll; +} +AUDIO_STREAMING_IN_HandleTypeDef; + +typedef struct +{ + uint8_t Ep; + uint16_t EpSize; + uint8_t AltSettings; + uint8_t interface; + uint8_t valid; + uint16_t Poll; +} +AUDIO_STREAMING_OUT_HandleTypeDef; + + +typedef struct +{ + uint8_t mute; + uint32_t volumeMin; + uint32_t volumeMax; + uint32_t volume; + uint32_t resolution; +} +AUDIO_ControlAttributeTypeDef; + +typedef struct +{ + + uint8_t Ep; + uint16_t EpSize; + uint8_t interface; + uint8_t AltSettings; + uint8_t supported; + + uint8_t Pipe; + uint8_t Poll; + uint32_t timer ; + + uint8_t asociated_as; + uint8_t asociated_mixer; + uint8_t asociated_selector; + uint8_t asociated_feature; + uint8_t asociated_terminal; + uint8_t asociated_channels; + + uint32_t frequency; + uint8_t *buf; + uint8_t *cbuf; + uint32_t partial_ptr; + + uint32_t global_ptr; + uint16_t frame_length; + uint32_t total_length; + + AUDIO_ControlAttributeTypeDef attribute; +} +AUDIO_InterfaceStreamPropTypeDef; + +typedef struct +{ + + uint8_t Ep; + uint16_t EpSize; + uint8_t interface; + uint8_t supported; + + uint8_t Pipe; + uint8_t Poll; + uint32_t timer ; +} +AUDIO_InterfaceControlPropTypeDef; + + +#define AUDIO_MAX_AUDIO_STD_INTERFACE 0x05U +#define AUDIO_MAX_FREQ_SUPPORTED 0x05U +#define AUDIO_MAX_STREAMING_INTERFACE 0x05U +#define AUDIO_MAX_NUM_IN_TERMINAL 0x04U +#define AUDIO_MAX_NUM_OUT_TERMINAL 0x04U +#define AUDIO_MAX_NUM_FEATURE_UNIT 0x04U +#define AUDIO_MAX_NUM_MIXER_UNIT 0x04U +#define AUDIO_MAX_NUM_SELECTOR_UNIT 0x04U + +#define HEADPHONE_SUPPORTED 0x01U +#define MICROPHONE_SUPPORTED 0x02U +#define HEADSET_SUPPORTED 0x03U + +#define AUDIO_MAX_SAMFREQ_NBR 5U +#define AUDIO_MAX_INTERFACE_NBR 5U +#define AUDIO_MAX_CONTROLS_NBR 5U + +/*Class-Specific AS(Audio Streaming) Interface Descriptor*/ +typedef struct +{ + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bDescriptorSubtype; + uint8_t bTerminalLink; + uint8_t bDelay; + uint8_t wFormatTag[2]; +} +AUDIO_ASGeneralDescTypeDef; + +/*Class-Specific AS(Audio Streaming) Format Type Descriptor*/ +typedef struct +{ + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bDescriptorSubtype; + uint8_t bFormatType; + uint8_t bNrChannels; + uint8_t bSubframeSize; + uint8_t bBitResolution; + uint8_t bSamFreqType; + uint8_t tSamFreq[AUDIO_MAX_SAMFREQ_NBR][3]; +} +AUDIO_ASFormatTypeDescTypeDef; + +/*Class-Specific AS(Audio Streaming) Interface Descriptor*/ +typedef struct +{ + AUDIO_ASGeneralDescTypeDef *GeneralDesc; + AUDIO_ASFormatTypeDescTypeDef *FormatTypeDesc; +} +AUDIO_ASDescTypeDef; + +/* 4.3.2 Class-Specific AC Interface Descriptor */ + +typedef struct +{ + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bDescriptorSubtype; + uint8_t bcdADC[2]; + uint8_t wTotalLength[2]; + uint8_t bInCollection; + uint8_t baInterfaceNr[AUDIO_MAX_INTERFACE_NBR]; +} +AUDIO_HeaderDescTypeDef; + +/* 4.3.2.1 Input Terminal Descriptor */ +typedef struct +{ + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bDescriptorSubtype; + uint8_t bTerminalID; + uint8_t wTerminalType[2]; + uint8_t bAssocTerminal; + uint8_t bNrChannels; + uint8_t wChannelConfig[2]; + uint8_t iChannelNames; + uint8_t iTerminal; +} +AUDIO_ITDescTypeDef; + +/* 4.3.2.2 Output Terminal Descriptor */ +typedef struct +{ + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bDescriptorSubtype; + uint8_t bTerminalID; + uint8_t wTerminalType[2]; + uint8_t bAssocTerminal; + uint8_t bSourceID; + uint8_t iTerminal; +} +AUDIO_OTDescTypeDef; + +/* 4.3.2.3 Feature Descriptor */ +typedef struct +{ + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bDescriptorSubtype; + uint8_t bUnitID; + uint8_t bSourceID; + uint8_t bControlSize; + uint8_t bmaControls[AUDIO_MAX_CONTROLS_NBR][2]; +} +AUDIO_FeatureDescTypeDef; + + +/* 4.3.2.3 Feature Descriptor */ +typedef struct +{ + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bDescriptorSubtype; + uint8_t bUnitID; + uint8_t bNrInPins; + uint8_t bSourceID0; + uint8_t bSourceID1; + uint8_t bNrChannels; + uint8_t bmChannelsConfig[2]; + uint8_t iChannelsNames; + uint8_t bmaControls; + uint8_t iMixer; +} +AUDIO_MixerDescTypeDef; + + + +/* 4.3.2.3 Feature Descriptor */ +typedef struct +{ + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bDescriptorSubtype; + uint8_t bUnitID; + uint8_t bNrInPins; + uint8_t bSourceID0; + uint8_t iSelector; +} +AUDIO_SelectorDescTypeDef; + +/*Class-Specific AC(Audio Control) Interface Descriptor*/ +typedef struct +{ + AUDIO_HeaderDescTypeDef *HeaderDesc; + AUDIO_ITDescTypeDef *InputTerminalDesc [AUDIO_MAX_NUM_IN_TERMINAL]; + AUDIO_OTDescTypeDef *OutputTerminalDesc[AUDIO_MAX_NUM_OUT_TERMINAL]; + AUDIO_FeatureDescTypeDef *FeatureUnitDesc [AUDIO_MAX_NUM_FEATURE_UNIT]; + AUDIO_MixerDescTypeDef *MixerUnitDesc [AUDIO_MAX_NUM_MIXER_UNIT]; + AUDIO_SelectorDescTypeDef *SelectorUnitDesc [AUDIO_MAX_NUM_SELECTOR_UNIT]; +} +AUDIO_ACDescTypeDef; + +/*Class-Specific AC : Global descriptor*/ + +typedef struct +{ + AUDIO_ACDescTypeDef cs_desc; /* Only one control descriptor*/ + AUDIO_ASDescTypeDef as_desc[AUDIO_MAX_STREAMING_INTERFACE]; + + uint16_t ASNum; + uint16_t InputTerminalNum; + uint16_t OutputTerminalNum; + uint16_t FeatureUnitNum; + uint16_t SelectorUnitNum; + uint16_t MixerUnitNum; +} +AUDIO_ClassSpecificDescTypedef; + + +typedef struct _AUDIO_Process +{ + AUDIO_ReqStateTypeDef req_state; + AUDIO_CSReqStateTypeDef cs_req_state; + AUDIO_PlayStateTypeDef play_state; + AUDIO_ControlStateTypeDef control_state; + AUDIO_ProcessingTypeDef processing_state; + + AUDIO_STREAMING_IN_HandleTypeDef stream_in[AUDIO_MAX_AUDIO_STD_INTERFACE]; + AUDIO_STREAMING_OUT_HandleTypeDef stream_out[AUDIO_MAX_AUDIO_STD_INTERFACE]; + AUDIO_ClassSpecificDescTypedef class_desc; + + AUDIO_InterfaceStreamPropTypeDef headphone; + AUDIO_InterfaceStreamPropTypeDef microphone; + AUDIO_InterfaceControlPropTypeDef control; + uint16_t mem [8]; + uint8_t temp_feature; + uint8_t temp_channels; +} +AUDIO_HandleTypeDef; + +/** + * @} + */ + +/** @defgroup USBH_AUDIO_CORE_Exported_Defines + * @{ + */ + + +/*Audio Interface Subclass Codes*/ +#define AC_CLASS 0x01U + +/* A.2 Audio Interface Subclass Codes */ +#define USB_SUBCLASS_AUDIOCONTROL 0x01U +#define USB_SUBCLASS_AUDIOSTREAMING 0x02U +#define USB_SUBCLASS_MIDISTREAMING 0x03U + +#define USB_DESC_TYPE_CS_INTERFACE 0x24U +#define USB_DESC_TYPE_CS_ENDPOINT 0x25U + +/* A.5 Audio Class-Specific AC Interface Descriptor Subtypes */ +#define UAC_HEADER 0x01U +#define UAC_INPUT_TERMINAL 0x02U +#define UAC_OUTPUT_TERMINAL 0x03U +#define UAC_MIXER_UNIT 0x04U +#define UAC_SELECTOR_UNIT 0x05U +#define UAC_FEATURE_UNIT 0x06U +#define UAC_PROCESSING_UNIT 0x07U +#define UAC_EXTENSION_UNIT 0x08U + +/*Audio Class-Specific Endpoint Descriptor Subtypes*/ +#define EP_CONTROL_UNDEFINED 0x00U +#define SAMPLING_FREQ_CONTROL 0x01U +#define PITCH_CONTROL 0x02U + +/*Feature unit control selector*/ +#define FU_CONTROL_UNDEFINED 0x00U +#define MUTE_CONTROL 0x01U +#define VOLUME_CONTROL 0x02U +#define BASS_CONTROL 0x03U +#define MID_CONTROL 0x04U +#define TREBLE_CONTROL 0x05U +#define GRAPHIC_EQUALIZER_CONTROL 0x06U +#define AUTOMATIC_GAIN_CONTROL 0x07U +#define DELAY_CONTROL 0x08U +#define BASS_BOOST_CONTROL 0x09U +#define LOUDNESS_CONTROL 0x0AU + +/*Terminal control selector*/ +#define TE_CONTROL_UNDEFINED 0x00U +#define COPY_PROTECT_CONTROL 0x01U + + +/* A.6 Audio Class-Specific AS Interface Descriptor Subtypes */ +#define UAC_AS_GENERAL 0x01U +#define UAC_FORMAT_TYPE 0x02U +#define UAC_FORMAT_SPECIFIC 0x03U + +/* A.8 Audio Class-Specific Endpoint Descriptor Subtypes */ +#define UAC_EP_GENERAL 0x01U + +/* A.9 Audio Class-Specific Request Codes */ +#define UAC_SET_ 0x00U +#define UAC_GET_ 0x80U + +#define UAC__CUR 0x01U +#define UAC__MIN 0x02U +#define UAC__MAX 0x03U +#define UAC__RES 0x04U +#define UAC__MEM 0x05U + +#define UAC_SET_CUR (UAC_SET_ | UAC__CUR) +#define UAC_GET_CUR (UAC_GET_ | UAC__CUR) +#define UAC_SET_MIN (UAC_SET_ | UAC__MIN) +#define UAC_GET_MIN (UAC_GET_ | UAC__MIN) +#define UAC_SET_MAX (UAC_SET_ | UAC__MAX) +#define UAC_GET_MAX (UAC_GET_ | UAC__MAX) +#define UAC_SET_RES (UAC_SET_ | UAC__RES) +#define UAC_GET_RES (UAC_GET_ | UAC__RES) +#define UAC_SET_MEM (UAC_SET_ | UAC__MEM) +#define UAC_GET_MEM (UAC_GET_ | UAC__MEM) + +#define UAC_GET_STAT 0xffU + +/* MIDI - A.1 MS Class-Specific Interface Descriptor Subtypes */ +#define UAC_MS_HEADER 0x01U +#define UAC_MIDI_IN_JACK 0x02U +#define UAC_MIDI_OUT_JACK 0x03U + +/* MIDI - A.1 MS Class-Specific Endpoint Descriptor Subtypes */ +#define UAC_MS_GENERAL 0x01U + +/* Terminals - 2.1 USB Terminal Types */ +#define UAC_TERMINAL_UNDEFINED 0x100U +#define UAC_TERMINAL_STREAMING 0x101U +#define UAC_TERMINAL_VENDOR_SPEC 0x1FFU + +/** + * @} + */ + +/** @defgroup USBH_AUDIO_CORE_Exported_Macros + * @{ + */ +/** + * @} + */ + +/** @defgroup USBH_AUDIO_CORE_Exported_Variables + * @{ + */ +extern USBH_ClassTypeDef AUDIO_Class; +#define USBH_AUDIO_CLASS &AUDIO_Class +/** + * @} + */ + +/** @defgroup USBH_AUDIO_CORE_Exported_FunctionsPrototype + * @{ + */ +USBH_StatusTypeDef USBH_AUDIO_SetFrequency (USBH_HandleTypeDef *phost, + uint16_t SampleRate, + uint8_t NbrChannels, + uint8_t BitPerSample); + +USBH_StatusTypeDef USBH_AUDIO_Play (USBH_HandleTypeDef *phost, uint8_t *buf, uint32_t length); +USBH_StatusTypeDef USBH_AUDIO_Stop (USBH_HandleTypeDef *phost); +USBH_StatusTypeDef USBH_AUDIO_Suspend (USBH_HandleTypeDef *phost); +USBH_StatusTypeDef USBH_AUDIO_Resume (USBH_HandleTypeDef *phost); +USBH_StatusTypeDef USBH_AUDIO_SetVolume (USBH_HandleTypeDef *phost, AUDIO_VolumeCtrlTypeDef volume_ctl); +USBH_StatusTypeDef USBH_AUDIO_ChangeOutBuffer (USBH_HandleTypeDef *phost, uint8_t *buf); +int32_t USBH_AUDIO_GetOutOffset (USBH_HandleTypeDef *phost); + +void USBH_AUDIO_FrequencySet(USBH_HandleTypeDef *phost); + +#define USBH_AUDIO_FrequencySetCallback USBH_AUDIO_FrequencySet +void USBH_AUDIO_BufferEmptyCallback(USBH_HandleTypeDef *phost); +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USBH_AUDIO_H */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Class/AUDIO/Src/usbh_audio.c b/system/Middlewares/ST/STM32_USB_Host_Library/Class/AUDIO/Src/usbh_audio.c index fb9b24b34a..62c1726c7c 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Class/AUDIO/Src/usbh_audio.c +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Class/AUDIO/Src/usbh_audio.c @@ -1,2168 +1,2168 @@ -/** - ****************************************************************************** - * @file usbh_audio.c - * @author MCD Application Team - * @brief This file is the AC Layer Handlers for USB Host AC class. - * - * @verbatim - * - * =================================================================== - * AUDIO Class Description - * =================================================================== - * This driver manages the Audio Class 1.0 following the "USB Device - * Class Definition for Audio Devices V1.0 Mar 18, 98". - * - * @endverbatim - * - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - - /* BSPDependencies - - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" - - "stm32xxxxx_{eval}{discovery}_io.c" - - "stm32xxxxx_{eval}{discovery}{adafruit}_sd.c" - - "stm32xxxxx_{eval}{discovery}{adafruit}_lcd.c" - - "stm32xxxxx_{eval}{discovery}_sdram.c" - EndBSPDependencies */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_audio.h" - -/** @addtogroup USBH_LIB - * @{ - */ - -/** @addtogroup USBH_CLASS - * @{ - */ - -/** @addtogroup USBH_AUDIO_CLASS - * @{ - */ - -/** @defgroup USBH_AUDIO_CORE - * @brief This file includes HID Layer Handlers for USB Host HID class. - * @{ - */ - -/** @defgroup USBH_AUDIO_CORE_Private_TypesDefinitions - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBH_AUDIO_CORE_Private_Defines - * @{ - */ - -/** - * @} - */ - - -/** @defgroup USBH_AUDIO_CORE_Private_Macros - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBH_AUDIO_CORE_Private_Variables - * @{ - */ - -/** - * @} - */ - - -/** @defgroup USBH_AUDIO_CORE_Private_FunctionPrototypes - * @{ - */ - -static USBH_StatusTypeDef USBH_AUDIO_InterfaceInit (USBH_HandleTypeDef *phost); -static USBH_StatusTypeDef USBH_AUDIO_InterfaceDeInit (USBH_HandleTypeDef *phost); -static USBH_StatusTypeDef USBH_AUDIO_Process(USBH_HandleTypeDef *phost); -static USBH_StatusTypeDef USBH_AUDIO_SOFProcess(USBH_HandleTypeDef *phost); -static USBH_StatusTypeDef USBH_AUDIO_ClassRequest(USBH_HandleTypeDef *phost); -static USBH_StatusTypeDef USBH_AUDIO_CSRequest(USBH_HandleTypeDef *phost, - uint8_t feature, - uint8_t channel); - -static USBH_StatusTypeDef USBH_AUDIO_HandleCSRequest(USBH_HandleTypeDef *phost); - -static USBH_StatusTypeDef USBH_AUDIO_FindAudioStreamingIN(USBH_HandleTypeDef *phost); -static USBH_StatusTypeDef USBH_AUDIO_FindAudioStreamingOUT(USBH_HandleTypeDef *phost); -static USBH_StatusTypeDef USBH_AUDIO_FindHIDControl(USBH_HandleTypeDef *phost); -static USBH_StatusTypeDef USBH_AUDIO_ParseCSDescriptors(USBH_HandleTypeDef *phost); - -static USBH_StatusTypeDef USBH_AUDIO_BuildHeadphonePath(USBH_HandleTypeDef *phost); -static USBH_StatusTypeDef USBH_AUDIO_BuildMicrophonePath(USBH_HandleTypeDef *phost); -int32_t USBH_AUDIO_FindLinkedUnitIN(USBH_HandleTypeDef *phost, uint8_t UnitID); -int32_t USBH_AUDIO_FindLinkedUnitOUT(USBH_HandleTypeDef *phost, uint8_t UnitID); - - - -static USBH_StatusTypeDef ParseCSDescriptors(AUDIO_ClassSpecificDescTypedef *class_desc, - uint8_t ac_subclass, - uint8_t *pdesc); - - -static USBH_StatusTypeDef USBH_AUDIO_Transmit (USBH_HandleTypeDef *phost); - - -static USBH_StatusTypeDef USBH_AC_SetCur(USBH_HandleTypeDef *phost, - uint8_t subtype, - uint8_t feature, - uint8_t controlSelector, - uint8_t channel, - uint16_t length); - -static USBH_StatusTypeDef USBH_AC_GetCur(USBH_HandleTypeDef *phost, - uint8_t subtype, - uint8_t feature, - uint8_t controlSelector, - uint8_t channel, - uint16_t length); - -static USBH_StatusTypeDef USBH_AC_GetMin(USBH_HandleTypeDef *phost, - uint8_t subtype, - uint8_t feature, - uint8_t controlSelector, - uint8_t channel, - uint16_t length); - -static USBH_StatusTypeDef USBH_AC_GetMax(USBH_HandleTypeDef *phost, - uint8_t subtype, - uint8_t feature, - uint8_t controlSelector, - uint8_t channel, - uint16_t length); - -static USBH_StatusTypeDef USBH_AC_GetRes(USBH_HandleTypeDef *phost, - uint8_t subtype, - uint8_t feature, - uint8_t controlSelector, - uint8_t channel, - uint16_t length); - -static USBH_StatusTypeDef USBH_AUDIO_SetEndpointControls(USBH_HandleTypeDef *phost, - uint8_t Ep, - uint8_t *buff); - -static USBH_StatusTypeDef AUDIO_SetVolume (USBH_HandleTypeDef *phost, uint8_t feature, uint8_t channel, uint16_t volume); - -static USBH_StatusTypeDef USBH_AUDIO_InputStream (USBH_HandleTypeDef *phost); -static USBH_StatusTypeDef USBH_AUDIO_OutputStream (USBH_HandleTypeDef *phost); -static USBH_StatusTypeDef USBH_AUDIO_Control (USBH_HandleTypeDef *phost); -static USBH_StatusTypeDef USBH_AUDIO_SetControlAttribute (USBH_HandleTypeDef *phost, uint8_t attrib); -static uint32_t USBH_AUDIO_FindLinkedUnit(USBH_HandleTypeDef *phost, uint8_t UnitID); - -USBH_ClassTypeDef AUDIO_Class = -{ - "AUDIO", - AC_CLASS, - USBH_AUDIO_InterfaceInit, - USBH_AUDIO_InterfaceDeInit, - USBH_AUDIO_ClassRequest, - USBH_AUDIO_Process, - USBH_AUDIO_SOFProcess, - NULL, -}; - -/** - * @} - */ - -/** @defgroup USBH_AUDIO_CORE_Private_Functions - * @{ - */ - -/** - * @brief USBH_AUDIO_InterfaceInit - * The function init the Audio class. - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_AUDIO_InterfaceInit (USBH_HandleTypeDef *phost) -{ - USBH_StatusTypeDef status = USBH_FAIL ; - USBH_StatusTypeDef out_status, in_status ; - AUDIO_HandleTypeDef *AUDIO_Handle; - uint8_t interface, index; - uint16_t ep_size_out = 0U; - uint16_t ep_size_in = 0U; - - interface = USBH_FindInterface(phost, AC_CLASS, USB_SUBCLASS_AUDIOCONTROL, 0x00U); - - if(interface == 0xFFU) /* Not Valid Interface */ - { - USBH_DbgLog ("Cannot Find the interface for %s class.", phost->pActiveClass->Name); - status = USBH_FAIL; - } - else - { - - - phost->pActiveClass->pData = (AUDIO_HandleTypeDef *)USBH_malloc (sizeof(AUDIO_HandleTypeDef)); - AUDIO_Handle = (AUDIO_HandleTypeDef *) phost->pActiveClass->pData; - USBH_memset(AUDIO_Handle, 0, sizeof(AUDIO_HandleTypeDef)); - - - /* 1st Step: Find Audio Interfaces */ - out_status = USBH_AUDIO_FindAudioStreamingIN (phost); - - in_status = USBH_AUDIO_FindAudioStreamingOUT(phost); - - if((out_status == USBH_FAIL) && (in_status == USBH_FAIL)) - { - USBH_DbgLog ("%s class configuration not supported.", phost->pActiveClass->Name); - } - else - { - /* 2nd Step: Select Audio Streaming Interfaces with largest endpoint size : default behavior*/ - for (index = 0U; index < AUDIO_MAX_AUDIO_STD_INTERFACE; index ++) - { - if( AUDIO_Handle->stream_out[index].valid == 1U) - { - if(ep_size_out < AUDIO_Handle->stream_out[index].EpSize) - { - ep_size_out = AUDIO_Handle->stream_out[index].EpSize; - AUDIO_Handle->headphone.interface = AUDIO_Handle->stream_out[index].interface; - AUDIO_Handle->headphone.AltSettings = AUDIO_Handle->stream_out[index].AltSettings; - AUDIO_Handle->headphone.Ep = AUDIO_Handle->stream_out[index].Ep; - AUDIO_Handle->headphone.EpSize = AUDIO_Handle->stream_out[index].EpSize; - AUDIO_Handle->headphone.Poll = (uint8_t)AUDIO_Handle->stream_out[index].Poll; - AUDIO_Handle->headphone.supported = 1U; - } - } - - if( AUDIO_Handle->stream_in[index].valid == 1U) - { - if(ep_size_in < AUDIO_Handle->stream_in[index].EpSize) - { - ep_size_in = AUDIO_Handle->stream_in[index].EpSize; - AUDIO_Handle->microphone.interface = AUDIO_Handle->stream_in[index].interface; - AUDIO_Handle->microphone.AltSettings = AUDIO_Handle->stream_in[index].AltSettings; - AUDIO_Handle->microphone.Ep = AUDIO_Handle->stream_in[index].Ep; - AUDIO_Handle->microphone.EpSize = AUDIO_Handle->stream_in[index].EpSize; - AUDIO_Handle->microphone.Poll = (uint8_t)AUDIO_Handle->stream_out[index].Poll; - AUDIO_Handle->microphone.supported = 1U; - } - } - } - - if(USBH_AUDIO_FindHIDControl(phost) == USBH_OK) - { - AUDIO_Handle->control.supported = 1U; - } - - /* 3rd Step: Find and Parse Audio interfaces */ - USBH_AUDIO_ParseCSDescriptors (phost); - - - /* 4th Step: Open the Audio streaming pipes*/ - if(AUDIO_Handle->headphone.supported == 1U) - { - USBH_AUDIO_BuildHeadphonePath (phost); - - AUDIO_Handle->headphone.Pipe = USBH_AllocPipe(phost, AUDIO_Handle->headphone.Ep); - - /* Open pipe for IN endpoint */ - USBH_OpenPipe (phost, - AUDIO_Handle->headphone.Pipe, - AUDIO_Handle->headphone.Ep, - phost->device.address, - phost->device.speed, - USB_EP_TYPE_ISOC, - AUDIO_Handle->headphone.EpSize); - - USBH_LL_SetToggle (phost, AUDIO_Handle->headphone.Pipe, 0U); - - } - - if(AUDIO_Handle->microphone.supported == 1U) - { - USBH_AUDIO_BuildMicrophonePath (phost); - AUDIO_Handle->microphone.Pipe = USBH_AllocPipe(phost, AUDIO_Handle->microphone.Ep); - - /* Open pipe for IN endpoint */ - USBH_OpenPipe (phost, - AUDIO_Handle->microphone.Pipe, - AUDIO_Handle->microphone.Ep, - phost->device.address, - phost->device.speed, - USB_EP_TYPE_ISOC, - AUDIO_Handle->microphone.EpSize); - - USBH_LL_SetToggle (phost, AUDIO_Handle->microphone.Pipe, 0U); - } - - if(AUDIO_Handle->control.supported == 1U) - { - AUDIO_Handle->control.Pipe = USBH_AllocPipe(phost, AUDIO_Handle->control.Ep); - - /* Open pipe for IN endpoint */ - USBH_OpenPipe (phost, - AUDIO_Handle->control.Pipe, - AUDIO_Handle->control.Ep, - phost->device.address, - phost->device.speed, - USB_EP_TYPE_INTR, - AUDIO_Handle->control.EpSize); - - USBH_LL_SetToggle (phost, AUDIO_Handle->control.Pipe, 0U); - - } - - AUDIO_Handle->req_state = AUDIO_REQ_INIT; - AUDIO_Handle->control_state = AUDIO_CONTROL_INIT; - - status = USBH_OK; - } - } - return status; -} - - - -/** - * @brief USBH_AUDIO_InterfaceDeInit - * The function DeInit the Pipes used for the Audio class. - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_AUDIO_InterfaceDeInit (USBH_HandleTypeDef *phost) -{ - AUDIO_HandleTypeDef *AUDIO_Handle = (AUDIO_HandleTypeDef *) phost->pActiveClass->pData; - - if(AUDIO_Handle->microphone.Pipe != 0x00U) - { - USBH_ClosePipe (phost, AUDIO_Handle->microphone.Pipe); - USBH_FreePipe (phost, AUDIO_Handle->microphone.Pipe); - AUDIO_Handle->microphone.Pipe = 0U; /* Reset the pipe as Free */ - } - - if( AUDIO_Handle->headphone.Pipe != 0x00U) - { - USBH_ClosePipe(phost, AUDIO_Handle->headphone.Pipe); - USBH_FreePipe (phost, AUDIO_Handle->headphone.Pipe); - AUDIO_Handle->headphone.Pipe = 0U; /* Reset the pipe as Free */ - } - - if( AUDIO_Handle->control.Pipe != 0x00U) - { - USBH_ClosePipe(phost, AUDIO_Handle->control.Pipe); - USBH_FreePipe (phost, AUDIO_Handle->control.Pipe); - AUDIO_Handle->control.Pipe = 0U; /* Reset the pipe as Free */ - } - - if(phost->pActiveClass->pData) - { - USBH_free (phost->pActiveClass->pData); - phost->pActiveClass->pData = 0; - } - return USBH_OK ; -} - -/** - * @brief USBH_AUDIO_ClassRequest - * The function is responsible for handling Standard requests - * for Audio class. - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_AUDIO_ClassRequest(USBH_HandleTypeDef *phost) -{ - AUDIO_HandleTypeDef *AUDIO_Handle = (AUDIO_HandleTypeDef *) phost->pActiveClass->pData; - USBH_StatusTypeDef status = USBH_BUSY; - USBH_StatusTypeDef req_status = USBH_BUSY; - - /* Switch AUDIO REQ state machine */ - switch (AUDIO_Handle->req_state) - { - case AUDIO_REQ_INIT: - case AUDIO_REQ_SET_DEFAULT_IN_INTERFACE: - if(AUDIO_Handle->microphone.supported == 1U) - { - req_status = USBH_SetInterface(phost, - AUDIO_Handle->microphone.interface, - 0U); - - if(req_status == USBH_OK) - { - AUDIO_Handle->req_state = AUDIO_REQ_SET_DEFAULT_OUT_INTERFACE; - } - - } - else - { - AUDIO_Handle->req_state = AUDIO_REQ_SET_DEFAULT_OUT_INTERFACE; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - break; - - case AUDIO_REQ_SET_DEFAULT_OUT_INTERFACE: - if(AUDIO_Handle->headphone.supported == 1U) - { - req_status = USBH_SetInterface(phost, - AUDIO_Handle->headphone.interface, - 0U); - - if(req_status == USBH_OK) - { - AUDIO_Handle->req_state = AUDIO_REQ_CS_REQUESTS; - AUDIO_Handle->cs_req_state = AUDIO_REQ_GET_VOLUME; - - AUDIO_Handle->temp_feature = AUDIO_Handle->headphone.asociated_feature; - AUDIO_Handle->temp_channels = AUDIO_Handle->headphone.asociated_channels; - } - } - else - { - AUDIO_Handle->req_state = AUDIO_REQ_CS_REQUESTS; - AUDIO_Handle->cs_req_state = AUDIO_REQ_GET_VOLUME; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - break; - - case AUDIO_REQ_CS_REQUESTS: - if(USBH_AUDIO_HandleCSRequest (phost) == USBH_OK) - { - AUDIO_Handle->req_state = AUDIO_REQ_SET_IN_INTERFACE; - } - break; - - case AUDIO_REQ_SET_IN_INTERFACE: - if(AUDIO_Handle->microphone.supported == 1U) - { - req_status = USBH_SetInterface(phost, - AUDIO_Handle->microphone.interface, - AUDIO_Handle->microphone.AltSettings); - - if(req_status == USBH_OK) - { - AUDIO_Handle->req_state = AUDIO_REQ_SET_OUT_INTERFACE; - } - } - else - { - AUDIO_Handle->req_state = AUDIO_REQ_SET_OUT_INTERFACE; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - break; - case AUDIO_REQ_SET_OUT_INTERFACE: - if(AUDIO_Handle->headphone.supported == 1U) - { - req_status = USBH_SetInterface(phost, - AUDIO_Handle->headphone.interface, - AUDIO_Handle->headphone.AltSettings); - - if(req_status == USBH_OK) - { - AUDIO_Handle->req_state = AUDIO_REQ_IDLE; - } - - } - else - { - AUDIO_Handle->req_state = AUDIO_REQ_IDLE; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - break; - case AUDIO_REQ_IDLE: - AUDIO_Handle->play_state = AUDIO_PLAYBACK_INIT; - phost->pUser(phost, HOST_USER_CLASS_ACTIVE); - status = USBH_OK; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CLASS_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - default: - break; - } - return status; -} - -/** - * @brief USBH_AUDIO_CSRequest - * The function is responsible for handling AC Specific requests for a specific feature and channel - * for Audio class. - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_AUDIO_CSRequest(USBH_HandleTypeDef *phost, uint8_t feature, uint8_t channel) -{ - AUDIO_HandleTypeDef *AUDIO_Handle = (AUDIO_HandleTypeDef *) phost->pActiveClass->pData; - USBH_StatusTypeDef status = USBH_BUSY; - USBH_StatusTypeDef req_status = USBH_BUSY; - uint16_t VolumeCtl, ResolutionCtl; - - /* Switch AUDIO REQ state machine */ - switch (AUDIO_Handle->cs_req_state) - { - case AUDIO_REQ_GET_VOLUME: - req_status = USBH_AC_GetCur(phost, - UAC_FEATURE_UNIT, /* subtype */ - feature, /* feature */ - VOLUME_CONTROL, /* Selector */ - channel, /* channel */ - 0x02U); /* length */ - if(req_status != USBH_BUSY) - { - AUDIO_Handle->cs_req_state = AUDIO_REQ_GET_MIN_VOLUME; - VolumeCtl = LE16(&(AUDIO_Handle->mem[0])); - AUDIO_Handle->headphone.attribute.volume = (uint32_t)VolumeCtl; - } - break; - - case AUDIO_REQ_GET_MIN_VOLUME: - req_status = USBH_AC_GetMin(phost, - UAC_FEATURE_UNIT, /* subtype */ - feature, /* feature */ - VOLUME_CONTROL, /* Selector */ - channel, /* channel */ - 0x02U); /* length */ - if(req_status != USBH_BUSY) - { - AUDIO_Handle->cs_req_state = AUDIO_REQ_GET_MAX_VOLUME; - VolumeCtl = LE16(&(AUDIO_Handle->mem[0])); - AUDIO_Handle->headphone.attribute.volumeMin = (uint32_t)VolumeCtl; - } - break; - - case AUDIO_REQ_GET_MAX_VOLUME: - req_status = USBH_AC_GetMax(phost, - UAC_FEATURE_UNIT, /* subtype */ - feature, /* feature */ - VOLUME_CONTROL, /* Selector */ - channel, /* channel */ - 0x02U); /* length */ - if(req_status != USBH_BUSY) - { - AUDIO_Handle->cs_req_state = AUDIO_REQ_GET_RESOLUTION; - VolumeCtl = LE16(&(AUDIO_Handle->mem[0])); - AUDIO_Handle->headphone.attribute.volumeMax = (uint32_t)VolumeCtl; - - if (AUDIO_Handle->headphone.attribute.volumeMax < AUDIO_Handle->headphone.attribute.volumeMin) - { - AUDIO_Handle->headphone.attribute.volumeMax = 0xFF00U; - } - } - break; - - case AUDIO_REQ_GET_RESOLUTION: - req_status = USBH_AC_GetRes(phost, - UAC_FEATURE_UNIT, /* subtype */ - feature, /* feature */ - VOLUME_CONTROL, /* Selector */ - channel, /* channel */ - 0x02U); /* length */ - if(req_status != USBH_BUSY) - { - AUDIO_Handle->cs_req_state = AUDIO_REQ_CS_IDLE; - ResolutionCtl =LE16(&AUDIO_Handle->mem[0]); - AUDIO_Handle->headphone.attribute.resolution = (uint32_t)ResolutionCtl; - } - break; - - - case AUDIO_REQ_CS_IDLE: - status = USBH_OK; - break; - - default: - break; - } - return status; -} - -/** - * @brief USBH_AUDIO_HandleCSRequest - * The function is responsible for handling AC Specific requests for a all features - * and associated channels for Audio class. - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_AUDIO_HandleCSRequest(USBH_HandleTypeDef *phost) -{ - - USBH_StatusTypeDef status = USBH_BUSY; - USBH_StatusTypeDef cs_status = USBH_BUSY; - AUDIO_HandleTypeDef *AUDIO_Handle = (AUDIO_HandleTypeDef *) phost->pActiveClass->pData; - - cs_status = USBH_AUDIO_CSRequest(phost, - AUDIO_Handle->temp_feature, - AUDIO_Handle->temp_channels); - - if(cs_status != USBH_BUSY) - { - - if(AUDIO_Handle->temp_channels == 1U) - { - AUDIO_Handle->temp_feature = AUDIO_Handle->headphone.asociated_feature; - AUDIO_Handle->temp_channels = 0U; - status = USBH_OK; - } - else - { - AUDIO_Handle->temp_channels--; - } - AUDIO_Handle->cs_req_state = AUDIO_REQ_GET_VOLUME; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - - return status; -} - -/** - * @brief USBH_AUDIO_Process - * The function is for managing state machine for Audio data transfers - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_AUDIO_Process (USBH_HandleTypeDef *phost) -{ - USBH_StatusTypeDef status = USBH_BUSY; - AUDIO_HandleTypeDef *AUDIO_Handle = (AUDIO_HandleTypeDef *) phost->pActiveClass->pData; - - if(AUDIO_Handle->headphone.supported == 1U) - { - USBH_AUDIO_OutputStream (phost); - } - - if(AUDIO_Handle->microphone.supported == 1U) - { - USBH_AUDIO_InputStream (phost); - } - - return status; -} - -/** - * @brief USBH_AUDIO_SOFProcess - * The function is for managing the SOF callback - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_AUDIO_SOFProcess (USBH_HandleTypeDef *phost) -{ - return USBH_OK; -} -/** - * @brief Find IN Audio Streaming interfaces - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_AUDIO_FindAudioStreamingIN(USBH_HandleTypeDef *phost) -{ - uint8_t interface, alt_settings; - USBH_StatusTypeDef status = USBH_FAIL ; - AUDIO_HandleTypeDef *AUDIO_Handle; - - AUDIO_Handle = (AUDIO_HandleTypeDef *) phost->pActiveClass->pData; - - /* Look For AUDIOSTREAMING IN interface */ - alt_settings = 0U; - for (interface = 0U; interface < USBH_MAX_NUM_INTERFACES; interface++) - { - if((phost->device.CfgDesc.Itf_Desc[interface].bInterfaceClass == AC_CLASS)&& - (phost->device.CfgDesc.Itf_Desc[interface].bInterfaceSubClass == USB_SUBCLASS_AUDIOSTREAMING)) - { - if((phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress & 0x80U) && - (phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].wMaxPacketSize > 0U)) - { - AUDIO_Handle->stream_in[alt_settings].Ep = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress; - AUDIO_Handle->stream_in[alt_settings].EpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].wMaxPacketSize; - AUDIO_Handle->stream_in[alt_settings].interface = phost->device.CfgDesc.Itf_Desc[interface].bInterfaceNumber; - AUDIO_Handle->stream_in[alt_settings].AltSettings = phost->device.CfgDesc.Itf_Desc[interface].bAlternateSetting; - AUDIO_Handle->stream_in[alt_settings].Poll = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bInterval; - AUDIO_Handle->stream_in[alt_settings].valid = 1U; - alt_settings++; - } - } - } - - if(alt_settings > 0U) - { - status = USBH_OK; - } - - return status; -} - -/** - * @brief Find OUT Audio Streaming interfaces - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_AUDIO_FindAudioStreamingOUT(USBH_HandleTypeDef *phost) -{ - uint8_t interface, alt_settings; - USBH_StatusTypeDef status = USBH_FAIL ; - AUDIO_HandleTypeDef *AUDIO_Handle; - - AUDIO_Handle = (AUDIO_HandleTypeDef *) phost->pActiveClass->pData; - - /* Look For AUDIOSTREAMING IN interface */ - alt_settings = 0U; - for (interface = 0U; interface < USBH_MAX_NUM_INTERFACES; interface++) - { - if((phost->device.CfgDesc.Itf_Desc[interface].bInterfaceClass == AC_CLASS)&& - (phost->device.CfgDesc.Itf_Desc[interface].bInterfaceSubClass == USB_SUBCLASS_AUDIOSTREAMING)) - { - if(((phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress & 0x80U) == 0x00U) && - (phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].wMaxPacketSize > 0U)) - { - AUDIO_Handle->stream_out[alt_settings].Ep = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress; - AUDIO_Handle->stream_out[alt_settings].EpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].wMaxPacketSize; - AUDIO_Handle->stream_out[alt_settings].interface = phost->device.CfgDesc.Itf_Desc[interface].bInterfaceNumber; - AUDIO_Handle->stream_out[alt_settings].AltSettings = phost->device.CfgDesc.Itf_Desc[interface].bAlternateSetting; - AUDIO_Handle->stream_out[alt_settings].Poll = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bInterval; - AUDIO_Handle->stream_out[alt_settings].valid = 1U; - alt_settings++; - } - } - } - - if(alt_settings > 0U) - { - status = USBH_OK; - } - - return status; -} - -/** - * @brief Find HID Control interfaces - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_AUDIO_FindHIDControl(USBH_HandleTypeDef *phost) -{ - uint8_t interface; - USBH_StatusTypeDef status = USBH_FAIL; - AUDIO_HandleTypeDef *AUDIO_Handle; - - AUDIO_Handle = (AUDIO_HandleTypeDef *) phost->pActiveClass->pData; - - /* Look For AUDIOCONTROL interface */ - interface = USBH_FindInterface(phost, AC_CLASS, USB_SUBCLASS_AUDIOCONTROL, 0xFFU); - if(interface == 0xFFU) - { - return USBH_FAIL; - } - - for (interface = 0U; interface < USBH_MAX_NUM_INTERFACES; interface++) - { - if((phost->device.CfgDesc.Itf_Desc[interface].bInterfaceClass == 0x03U) && /*HID*/ - (phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].wMaxPacketSize > 0U)) - { - if((phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress & 0x80U) == 0x80U) - { - AUDIO_Handle->control.Ep = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress; - AUDIO_Handle->control.EpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].wMaxPacketSize; - AUDIO_Handle->control.interface = phost->device.CfgDesc.Itf_Desc[interface].bInterfaceNumber; - AUDIO_Handle->control.Poll = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bInterval; - AUDIO_Handle->control.supported = 1U; - status = USBH_OK; - break; - } - } - } - - return status; -} - -/** - * @brief Parse AC and interfaces Descriptors - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_AUDIO_ParseCSDescriptors(USBH_HandleTypeDef *phost) -{ - USBH_DescHeader_t *pdesc; - uint16_t ptr; - uint8_t itf_index = 0U; - uint8_t itf_number = 0U; - uint8_t alt_setting; - AUDIO_HandleTypeDef *AUDIO_Handle; - - AUDIO_Handle = (AUDIO_HandleTypeDef *) phost->pActiveClass->pData; - pdesc = (USBH_DescHeader_t *)(void *)(phost->device.CfgDesc_Raw); - ptr = USB_LEN_CFG_DESC; - - AUDIO_Handle->class_desc.FeatureUnitNum = 0U; - AUDIO_Handle->class_desc.InputTerminalNum = 0U; - AUDIO_Handle->class_desc.OutputTerminalNum = 0U; - AUDIO_Handle->class_desc.ASNum = 0U; - - while(ptr < phost->device.CfgDesc.wTotalLength) - { - pdesc = USBH_GetNextDesc((uint8_t*)(void *)pdesc, &ptr); - - switch (pdesc->bDescriptorType) - { - - case USB_DESC_TYPE_INTERFACE: - itf_number = *((uint8_t *)(void *)pdesc + 2U); - alt_setting = *((uint8_t *)(void *)pdesc + 3U); - itf_index = USBH_FindInterfaceIndex (phost, itf_number, alt_setting); - break; - - case USB_DESC_TYPE_CS_INTERFACE: - if(itf_number <= phost->device.CfgDesc.bNumInterfaces) - { - - ParseCSDescriptors(&AUDIO_Handle->class_desc, - phost->device.CfgDesc.Itf_Desc[itf_index].bInterfaceSubClass, - (uint8_t *)(void *)pdesc); - } - break; - - default: - break; - } - } - return USBH_OK; -} - -/** - * @brief Parse AC interfaces - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef ParseCSDescriptors(AUDIO_ClassSpecificDescTypedef *class_desc, - uint8_t ac_subclass, - uint8_t *pdesc) -{ - if(ac_subclass == USB_SUBCLASS_AUDIOCONTROL) - { - switch(pdesc[2]) - { - case UAC_HEADER: - class_desc->cs_desc.HeaderDesc = (AUDIO_HeaderDescTypeDef *)(void *)pdesc; - break; - - case UAC_INPUT_TERMINAL: - class_desc->cs_desc.InputTerminalDesc[class_desc->InputTerminalNum++] = (AUDIO_ITDescTypeDef*)(void *)pdesc; - break; - - case UAC_OUTPUT_TERMINAL: - class_desc->cs_desc.OutputTerminalDesc[class_desc->OutputTerminalNum++] = (AUDIO_OTDescTypeDef*)(void *)pdesc; - break; - - case UAC_FEATURE_UNIT: - class_desc->cs_desc.FeatureUnitDesc[class_desc->FeatureUnitNum++] = (AUDIO_FeatureDescTypeDef*)(void *)pdesc; - break; - - case UAC_SELECTOR_UNIT: - class_desc->cs_desc.SelectorUnitDesc[class_desc->SelectorUnitNum++] = (AUDIO_SelectorDescTypeDef*)(void *)pdesc; - break; - - case UAC_MIXER_UNIT: - class_desc->cs_desc.MixerUnitDesc[class_desc->MixerUnitNum++] = (AUDIO_MixerDescTypeDef*)(void *)pdesc; - break; - - default: - break; - } - } - else - { - if(ac_subclass == USB_SUBCLASS_AUDIOSTREAMING) - { - switch(pdesc[2]) - { - case UAC_AS_GENERAL: - class_desc->as_desc[class_desc->ASNum].GeneralDesc = (AUDIO_ASGeneralDescTypeDef*)(void *)pdesc; - break; - case UAC_FORMAT_TYPE: - class_desc->as_desc[class_desc->ASNum++].FormatTypeDesc = (AUDIO_ASFormatTypeDescTypeDef*)(void *)pdesc; - break; - default: - break; - } - } - } - - return USBH_OK; -} - - -/** - * @brief Link a Unit to next associated one - * @param phost: Host handle - * @param UnitID: Unit identifer - * @retval UnitID, Index and Type of the associated Unit - */ -static uint32_t USBH_AUDIO_FindLinkedUnit(USBH_HandleTypeDef *phost, uint8_t UnitID) -{ - uint8_t Index; - AUDIO_HandleTypeDef *AUDIO_Handle; - - AUDIO_Handle = (AUDIO_HandleTypeDef *) phost->pActiveClass->pData; - - /* Find Feature Unit */ - for(Index = 0U; Index < AUDIO_Handle->class_desc.FeatureUnitNum; Index ++) - { - if(AUDIO_Handle->class_desc.cs_desc.FeatureUnitDesc[Index]->bSourceID == UnitID) - { - UnitID = AUDIO_Handle->class_desc.cs_desc.FeatureUnitDesc[Index]->bUnitID; - - return (((uint32_t)UnitID << 16U) | (UAC_FEATURE_UNIT << 8U) | (uint32_t)Index); - } - } - - /* Find Mixer Unit */ - for(Index = 0U; Index < AUDIO_Handle->class_desc.MixerUnitNum; Index ++) - { - if((AUDIO_Handle->class_desc.cs_desc.MixerUnitDesc[Index]->bSourceID0 == UnitID)|| - (AUDIO_Handle->class_desc.cs_desc.MixerUnitDesc[Index]->bSourceID1 == UnitID)) - { - UnitID = AUDIO_Handle->class_desc.cs_desc.MixerUnitDesc[Index]->bUnitID; - - return ((UnitID << 16U) | (UAC_MIXER_UNIT << 8U) | Index); - } - } - - /* Find Selector Unit */ - for(Index = 0U; Index < AUDIO_Handle->class_desc.SelectorUnitNum; Index ++) - { - if(AUDIO_Handle->class_desc.cs_desc.SelectorUnitDesc[Index]->bSourceID0 == UnitID) - { - UnitID = AUDIO_Handle->class_desc.cs_desc.SelectorUnitDesc[Index]->bUnitID; - - return ((UnitID << 16U) | (UAC_SELECTOR_UNIT << 8U) | Index); - } - } - - /* Find OT Unit */ - for(Index = 0U; Index < AUDIO_Handle->class_desc.OutputTerminalNum; Index ++) - { - if(AUDIO_Handle->class_desc.cs_desc.OutputTerminalDesc[Index]->bSourceID == UnitID) - { - UnitID = AUDIO_Handle->class_desc.cs_desc.OutputTerminalDesc[Index]->bTerminalID; - - return ((UnitID << 16U) | (UAC_OUTPUT_TERMINAL << 8U) | Index); - } - } - - /* No associated Unit found return undefined ID 0x00*/ - return 0U; -} - -/** - * @brief Build full path for Microphone device - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_AUDIO_BuildMicrophonePath(USBH_HandleTypeDef *phost) -{ - uint8_t UnitID = 0U, Type, Index; - uint32_t value; - uint8_t terminalIndex; - AUDIO_HandleTypeDef *AUDIO_Handle; - USBH_StatusTypeDef ret = USBH_OK; - - AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; - - /*Find microphone IT*/ - for(terminalIndex = 0U; terminalIndex < AUDIO_Handle->class_desc.InputTerminalNum; terminalIndex++) - { - if(LE16(AUDIO_Handle->class_desc.cs_desc.InputTerminalDesc[terminalIndex]->wTerminalType) == 0x201) - { - UnitID = AUDIO_Handle->class_desc.cs_desc.InputTerminalDesc[terminalIndex]->bTerminalID; - AUDIO_Handle->microphone.asociated_channels = AUDIO_Handle->class_desc.cs_desc.InputTerminalDesc[terminalIndex]->bNrChannels; - break; - } - } - - do - { - value = USBH_AUDIO_FindLinkedUnit(phost, UnitID); - - if (!value) - { - return USBH_FAIL; - } - - Index = (uint8_t)(value & 0xFFU); - Type = (uint8_t)((value >> 8U) & 0xFFU); - UnitID = (uint8_t)((value >> 16U) & 0xFFU); - - switch (Type) - { - case UAC_FEATURE_UNIT: - AUDIO_Handle->microphone.asociated_feature = Index; - break; - - case UAC_MIXER_UNIT: - AUDIO_Handle->microphone.asociated_mixer = Index; - break; - - case UAC_SELECTOR_UNIT: - AUDIO_Handle->microphone.asociated_selector = Index; - break; - - case UAC_OUTPUT_TERMINAL: - AUDIO_Handle->microphone.asociated_terminal = Index; - break; - - default: - ret = USBH_FAIL; - break; - } - } - while ((Type != UAC_OUTPUT_TERMINAL) && (value > 0U)); - - return ret; -} - -/** - * @brief Build full path for Headphone device - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_AUDIO_BuildHeadphonePath(USBH_HandleTypeDef *phost) -{ - uint8_t UnitID = 0U, Type, Index; - uint32_t value; - uint8_t terminalIndex; - AUDIO_HandleTypeDef *AUDIO_Handle; - USBH_StatusTypeDef ret = USBH_OK; - - AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; - - /* Find association between audio streaming and microphone */ - for(terminalIndex = 0U; terminalIndex < AUDIO_Handle->class_desc.InputTerminalNum; terminalIndex++) - { - if(LE16(AUDIO_Handle->class_desc.cs_desc.InputTerminalDesc[terminalIndex]->wTerminalType) == 0x101) - { - UnitID = AUDIO_Handle->class_desc.cs_desc.InputTerminalDesc[terminalIndex]->bTerminalID; - AUDIO_Handle->headphone.asociated_channels = AUDIO_Handle->class_desc.cs_desc.InputTerminalDesc[terminalIndex]->bNrChannels; - break; - } - } - - for(Index = 0U; Index < AUDIO_Handle->class_desc.ASNum; Index++) - { - if(AUDIO_Handle->class_desc.as_desc[Index].GeneralDesc->bTerminalLink == UnitID) - { - AUDIO_Handle->headphone.asociated_as = Index; - break; - } - } - - do - { - value = USBH_AUDIO_FindLinkedUnit(phost, UnitID); - - if (!value) - { - return USBH_FAIL; - } - - Index = (uint8_t)(value & 0xFFU); - Type = (uint8_t)((value >> 8U) & 0xFFU); - UnitID = (uint8_t)((value >> 16U) & 0xFFU); - - switch (Type) - { - case UAC_FEATURE_UNIT: - AUDIO_Handle->headphone.asociated_feature = Index; - break; - - case UAC_MIXER_UNIT: - AUDIO_Handle->headphone.asociated_mixer = Index; - break; - - case UAC_SELECTOR_UNIT: - AUDIO_Handle->headphone.asociated_selector = Index; - break; - - case UAC_OUTPUT_TERMINAL: - AUDIO_Handle->headphone.asociated_terminal = Index; - if(LE16(AUDIO_Handle->class_desc.cs_desc.OutputTerminalDesc[Index]->wTerminalType) != 0x103) - { - return USBH_OK; - } - break; - - default: - ret = USBH_FAIL; - break; - } - } - while ((Type != UAC_OUTPUT_TERMINAL) && (value > 0U)); - - return ret; -} - - -/** - * @brief Handle Set Cur request - * @param phost: Host handle - * @param subtype: subtype index - * @param feature: feature index - * @param controlSelector: control code - * @param channel: channel index - * @param length: Command length - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_AC_SetCur(USBH_HandleTypeDef *phost, - uint8_t subtype, - uint8_t feature, - uint8_t controlSelector, - uint8_t channel, - uint16_t length) -{ - uint16_t wValue,wIndex,wLength; - uint8_t UnitID,InterfaceNum; - AUDIO_HandleTypeDef *AUDIO_Handle; - AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; - USBH_StatusTypeDef ret = USBH_OK; - - switch(subtype) - { - case UAC_INPUT_TERMINAL: - UnitID = AUDIO_Handle->class_desc.cs_desc.InputTerminalDesc[0]->bTerminalID; - InterfaceNum = 0U; /*Always zero Control Interface */ - wIndex = (uint16_t)((uint32_t)UnitID << 8U) | (uint16_t)InterfaceNum; - wValue = (COPY_PROTECT_CONTROL << 8U); - AUDIO_Handle->mem[0] = 0x00U; - - wLength = 1U; - break; - case UAC_FEATURE_UNIT: - UnitID = AUDIO_Handle->class_desc.cs_desc.FeatureUnitDesc[feature]->bUnitID; - InterfaceNum = 0U; /*Always zero Control Interface */ - wIndex = (uint16_t)((uint32_t)UnitID << 8U) | (uint16_t)InterfaceNum ; - /*holds the CS(control selector ) and CN (channel number)*/ - wValue = (uint16_t)((uint32_t)controlSelector << 8U) | (uint16_t)channel; - wLength = length; - break; - - default: - ret = USBH_FAIL; - break; - } - - if (ret != USBH_OK) - { - return ret; - } - - phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_INTERFACE - | USB_REQ_TYPE_CLASS; - - phost->Control.setup.b.bRequest = UAC_SET_CUR; - phost->Control.setup.b.wValue.w = wValue; - phost->Control.setup.b.wIndex.w = wIndex; - phost->Control.setup.b.wLength.w = wLength; - - return(USBH_CtlReq(phost, (uint8_t *)(void *)(AUDIO_Handle->mem), wLength)); -} - -/** - * @brief Handle Get Cur request - * @param phost: Host handle - * @param subtype: subtype index - * @param feature: feature index - * @param controlSelector: control code - * @param channel: channel index - * @param length: Command length - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_AC_GetCur(USBH_HandleTypeDef *phost, - uint8_t subtype, - uint8_t feature, - uint8_t controlSelector, - uint8_t channel, - uint16_t length) -{ - uint16_t wValue = 0U, wIndex = 0U, wLength = 0U; - uint8_t UnitID = 0U, InterfaceNum = 0U; - AUDIO_HandleTypeDef *AUDIO_Handle; - AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; - USBH_StatusTypeDef ret = USBH_OK; - - switch(subtype) - { - case UAC_INPUT_TERMINAL: - UnitID = AUDIO_Handle->class_desc.cs_desc.InputTerminalDesc[0]->bTerminalID; - InterfaceNum = 0U; /*Always zero Control Interface */ - wIndex = (uint16_t)((uint32_t)UnitID << 8U) | (uint16_t)InterfaceNum; - wValue = (COPY_PROTECT_CONTROL << 8U); - AUDIO_Handle->mem[0] = 0x00U; - - wLength = 1U; - break; - case UAC_FEATURE_UNIT: - UnitID = AUDIO_Handle->class_desc.cs_desc.FeatureUnitDesc[feature]->bUnitID; - InterfaceNum = 0U; /*Always zero Control Interface */ - wIndex = (uint16_t)((uint32_t)UnitID << 8U) | (uint16_t)InterfaceNum; - /*holds the CS(control selector ) and CN (channel number)*/ - wValue = (uint16_t)((uint32_t)controlSelector << 8U) | (uint16_t)channel; - wLength = length; - break; - - case UAC_OUTPUT_TERMINAL: - UnitID = AUDIO_Handle->class_desc.cs_desc.OutputTerminalDesc[0]->bTerminalID; - InterfaceNum = 0U; /*Always zero Control Interface */ - wIndex = (uint16_t)((uint32_t)UnitID << 8U) | (uint16_t)InterfaceNum; - wValue = (COPY_PROTECT_CONTROL << 8U); - wLength = 1U; - break; - - default: - ret = USBH_FAIL; - break; - } - - if (ret != USBH_OK) - { - return ret; - } - - phost->Control.setup.b.bmRequestType = USB_D2H | USB_REQ_RECIPIENT_INTERFACE | \ - USB_REQ_TYPE_CLASS; - - phost->Control.setup.b.bRequest = UAC_GET_CUR; - phost->Control.setup.b.wValue.w = wValue; - phost->Control.setup.b.wIndex.w = wIndex; - phost->Control.setup.b.wLength.w = wLength; - - return(USBH_CtlReq(phost, (uint8_t *)(void *)(AUDIO_Handle->mem) , wLength )); -} - - -/** - * @brief Handle Get Max request - * @param phost: Host handle - * @param subtype: subtype index - * @param feature: feature index - * @param controlSelector: control code - * @param channel: channel index - * @param length: Command length - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_AC_GetMax(USBH_HandleTypeDef *phost, - uint8_t subtype, - uint8_t feature, - uint8_t controlSelector, - uint8_t channel, - uint16_t length) -{ - uint16_t wValue = 0U, wIndex = 0U, wLength = 0U; - uint8_t UnitID = 0U, InterfaceNum = 0U; - AUDIO_HandleTypeDef *AUDIO_Handle; - AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; - USBH_StatusTypeDef ret = USBH_OK; - - switch(subtype) - { - case UAC_INPUT_TERMINAL: - UnitID = AUDIO_Handle->class_desc.cs_desc.InputTerminalDesc[0]->bTerminalID; - InterfaceNum = 0U; /*Always zero Control Interface */ - wIndex = (uint16_t)((uint32_t)UnitID << 8U) | (uint16_t)InterfaceNum; - wValue = (COPY_PROTECT_CONTROL << 8U); - AUDIO_Handle->mem[0] = 0x00U; - - wLength = 1U; - break; - case UAC_FEATURE_UNIT: - UnitID = AUDIO_Handle->class_desc.cs_desc.FeatureUnitDesc[feature]->bUnitID; - InterfaceNum = 0U; /*Always zero Control Interface */ - wIndex = (uint16_t)((uint32_t)UnitID << 8U) | (uint16_t)InterfaceNum ; - /*holds the CS(control selector ) and CN (channel number)*/ - wValue = (uint16_t)((uint32_t)controlSelector << 8U) | (uint16_t)channel; - wLength = length; - break; - - case UAC_OUTPUT_TERMINAL: - UnitID = AUDIO_Handle->class_desc.cs_desc.OutputTerminalDesc[0]->bTerminalID; - InterfaceNum = 0U; /*Always zero Control Interface */ - wIndex = (uint16_t)((uint32_t)UnitID << 8U) | (uint16_t)InterfaceNum ; - wValue = (COPY_PROTECT_CONTROL << 8U) ; - wLength = 1U; - break; - - default: - ret = USBH_FAIL; - break; - } - - if (ret != USBH_OK) - { - return ret; - } - - phost->Control.setup.b.bmRequestType = USB_D2H | USB_REQ_RECIPIENT_INTERFACE | \ - USB_REQ_TYPE_CLASS; - - phost->Control.setup.b.bRequest = UAC_GET_MAX; - phost->Control.setup.b.wValue.w = wValue; - phost->Control.setup.b.wIndex.w = wIndex; - phost->Control.setup.b.wLength.w = wLength; - - return(USBH_CtlReq(phost, (uint8_t *)(void *)(AUDIO_Handle->mem), wLength)); - -} - -/** - * @brief Handle Get Res request - * @param phost: Host handle - * @param subtype: subtype index - * @param feature: feature index - * @param controlSelector: control code - * @param channel: channel index - * @param length: Command length - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_AC_GetRes(USBH_HandleTypeDef *phost, - uint8_t subtype, - uint8_t feature, - uint8_t controlSelector, - uint8_t channel, - uint16_t length) -{ - uint16_t wValue = 0U, wIndex = 0U, wLength = 0U; - uint8_t UnitID = 0U, InterfaceNum = 0U; - AUDIO_HandleTypeDef *AUDIO_Handle; - AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; - USBH_StatusTypeDef ret = USBH_OK; - - switch(subtype) - { - case UAC_INPUT_TERMINAL: - UnitID = AUDIO_Handle->class_desc.cs_desc.InputTerminalDesc[0]->bTerminalID; - InterfaceNum = 0U; /*Always zero Control Interface */ - wIndex = (uint16_t)((uint32_t)UnitID << 8U) | (uint16_t)InterfaceNum; - wValue = (COPY_PROTECT_CONTROL << 8U) ; - AUDIO_Handle->mem[0] = 0x00U; - - wLength = 1U; - break; - case UAC_FEATURE_UNIT: - UnitID = AUDIO_Handle->class_desc.cs_desc.FeatureUnitDesc[feature]->bUnitID; - InterfaceNum = 0U; /*Always zero Control Interface */ - wIndex = (uint16_t)((uint32_t)UnitID << 8U) | (uint16_t)InterfaceNum; - /*holds the CS(control selector ) and CN (channel number)*/ - wValue = (uint16_t)((uint32_t)controlSelector << 8U) | (uint16_t)channel; - wLength = length; - break; - - case UAC_OUTPUT_TERMINAL: - UnitID = AUDIO_Handle->class_desc.cs_desc.OutputTerminalDesc[0]->bTerminalID; - InterfaceNum = 0U; /*Always zero Control Interface */ - wIndex = (uint16_t)((uint32_t)UnitID << 8U) | (uint16_t)InterfaceNum; - wValue = (COPY_PROTECT_CONTROL << 8U) ; - wLength = 1U; - break; - - default: - ret = USBH_FAIL; - break; - } - - if (ret != USBH_OK) - { - return ret; - } - - phost->Control.setup.b.bmRequestType = USB_D2H | USB_REQ_RECIPIENT_INTERFACE - | USB_REQ_TYPE_CLASS; - - phost->Control.setup.b.bRequest = UAC_GET_RES; - phost->Control.setup.b.wValue.w = wValue; - phost->Control.setup.b.wIndex.w = wIndex; - phost->Control.setup.b.wLength.w = wLength; - - return(USBH_CtlReq(phost, (uint8_t *)(void *)(AUDIO_Handle->mem), wLength)); - -} - -/** - * @brief Handle Get Min request - * @param phost: Host handle - * @param subtype: subtype index - * @param feature: feature index - * @param controlSelector: control code - * @param channel: channel index - * @param length: Command length - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_AC_GetMin(USBH_HandleTypeDef *phost, - uint8_t subtype, - uint8_t feature, - uint8_t controlSelector, - uint8_t channel, - uint16_t length) -{ - uint16_t wValue = 0U, wIndex = 0U, wLength = 0U; - uint8_t UnitID = 0U, InterfaceNum = 0U; - AUDIO_HandleTypeDef *AUDIO_Handle; - AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; - USBH_StatusTypeDef ret = USBH_OK; - - switch(subtype) - { - case UAC_INPUT_TERMINAL: - UnitID = AUDIO_Handle->class_desc.cs_desc.InputTerminalDesc[0]->bTerminalID; - InterfaceNum = 0U; /*Always zero Control Interface */ - wIndex = (uint16_t)((uint32_t)UnitID << 8U) | (uint16_t)InterfaceNum; - wValue = (COPY_PROTECT_CONTROL << 8U); - AUDIO_Handle->mem[0] = 0x00U; - - wLength = 1U; - break; - case UAC_FEATURE_UNIT: - UnitID = AUDIO_Handle->class_desc.cs_desc.FeatureUnitDesc[feature]->bUnitID; - InterfaceNum = 0U; /*Always zero Control Interface */ - wIndex = (uint16_t)((uint32_t)UnitID << 8U) | (uint16_t)InterfaceNum; - /*holds the CS(control selector ) and CN (channel number)*/ - wValue = (uint16_t)((uint32_t)controlSelector << 8U) | (uint16_t)channel; - wLength = length; - break; - - case UAC_OUTPUT_TERMINAL: - UnitID = AUDIO_Handle->class_desc.cs_desc.OutputTerminalDesc[0]->bTerminalID; - InterfaceNum = 0U; /*Always zero Control Interface */ - wIndex = (uint16_t)((uint32_t)UnitID << 8U) | (uint16_t)InterfaceNum; - wValue = (COPY_PROTECT_CONTROL << 8U); - wLength = 1U; - break; - - default: - ret = USBH_FAIL; - break; - } - - if (ret != USBH_OK) - { - return ret; - } - - phost->Control.setup.b.bmRequestType = USB_D2H | USB_REQ_RECIPIENT_INTERFACE | \ - USB_REQ_TYPE_CLASS; - - phost->Control.setup.b.bRequest = UAC_GET_MIN; - phost->Control.setup.b.wValue.w = wValue; - phost->Control.setup.b.wIndex.w = wIndex; - phost->Control.setup.b.wLength.w = wLength; - - return(USBH_CtlReq(phost, (uint8_t *)(void *)(AUDIO_Handle->mem), wLength)); - -} - -/** - * @brief Handle Set Endpoint Controls Request - * @param phost: Host handle - * @param Ep: Endpoint address - * @param buf: pointer to data - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_AUDIO_SetEndpointControls(USBH_HandleTypeDef *phost, - uint8_t Ep, - uint8_t *buff) -{ - uint16_t wValue, wIndex, wLength; - - wValue = SAMPLING_FREQ_CONTROL << 8U; - wIndex = Ep; - wLength = 3U; /*length of the frequency parameter*/ - - phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_ENDPOINT | \ - USB_REQ_TYPE_CLASS; - - phost->Control.setup.b.bRequest = UAC_SET_CUR; - phost->Control.setup.b.wValue.w = wValue; - phost->Control.setup.b.wIndex.w = wIndex; - phost->Control.setup.b.wLength.w = wLength; - - return(USBH_CtlReq(phost, (uint8_t *)buff, wLength)); - -} - -/** - * @brief Handle Input stream process - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_AUDIO_InputStream (USBH_HandleTypeDef *phost) -{ - USBH_StatusTypeDef status = USBH_BUSY; - - return status; -} - -/** - * @brief Handle HID Control process - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_AUDIO_Control (USBH_HandleTypeDef *phost) -{ - USBH_StatusTypeDef status = USBH_BUSY ; - AUDIO_HandleTypeDef *AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; - uint16_t attribute = 0U; - - switch(AUDIO_Handle->control_state) - { - case AUDIO_CONTROL_INIT: - if((phost->Timer & 1U) == 0U) - { - AUDIO_Handle->control.timer = phost->Timer; - USBH_InterruptReceiveData(phost, - (uint8_t *)(void *)(AUDIO_Handle->mem), - (uint8_t)AUDIO_Handle->control.EpSize, - AUDIO_Handle->control.Pipe); - - AUDIO_Handle->temp_feature = AUDIO_Handle->headphone.asociated_feature; - AUDIO_Handle->temp_channels = AUDIO_Handle->headphone.asociated_channels; - - AUDIO_Handle->control_state = AUDIO_CONTROL_CHANGE ; - } - break; - - case AUDIO_CONTROL_CHANGE: - if(USBH_LL_GetURBState(phost , AUDIO_Handle->control.Pipe) == USBH_URB_DONE) - { - attribute = LE16(&AUDIO_Handle->mem[0]); - if(USBH_AUDIO_SetControlAttribute (phost, (uint8_t)attribute) == USBH_BUSY) - { - break; - } - } - - if(( phost->Timer - AUDIO_Handle->control.timer) >= AUDIO_Handle->control.Poll) - { - AUDIO_Handle->control.timer = phost->Timer; - - USBH_InterruptReceiveData(phost, - (uint8_t *)(void *)(AUDIO_Handle->mem), - (uint8_t)AUDIO_Handle->control.EpSize, - AUDIO_Handle->control.Pipe); - - } - break; - - case AUDIO_CONTROL_VOLUME_UP: - if( USBH_AUDIO_SetControlAttribute (phost, 1U) == USBH_OK) - { - AUDIO_Handle->control_state = AUDIO_CONTROL_INIT; - status = USBH_OK; - } - break; - - case AUDIO_CONTROL_VOLUME_DOWN: - if( USBH_AUDIO_SetControlAttribute (phost, 2U) == USBH_OK) - { - AUDIO_Handle->control_state = AUDIO_CONTROL_INIT; - status = USBH_OK; - } - break; - - case AUDIO_CONTROL_IDLE: - default: - break; - } - - return status; -} - -/** - * @brief Handle Output stream process - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_AUDIO_OutputStream (USBH_HandleTypeDef *phost) -{ - USBH_StatusTypeDef status = USBH_BUSY ; - AUDIO_HandleTypeDef *AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; - uint8_t *buff; - - - switch(AUDIO_Handle->play_state) - { - case AUDIO_PLAYBACK_INIT: - - if( AUDIO_Handle->class_desc.as_desc[AUDIO_Handle->headphone.asociated_as].FormatTypeDesc->bSamFreqType == 0U) - { - AUDIO_Handle->play_state = AUDIO_PLAYBACK_SET_EP_FREQ; - } - else - { - AUDIO_Handle->play_state = AUDIO_PLAYBACK_SET_EP; - } - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - case AUDIO_PLAYBACK_SET_EP_FREQ: - - buff = (uint8_t*)AUDIO_Handle->class_desc.as_desc[AUDIO_Handle->headphone.asociated_as].FormatTypeDesc->tSamFreq[0]; - - status = USBH_AUDIO_SetEndpointControls(phost, AUDIO_Handle->headphone.Ep, buff); - if(status == USBH_OK) - { - AUDIO_Handle->play_state = AUDIO_PLAYBACK_IDLE; - } - break; - - case AUDIO_PLAYBACK_SET_EP: - buff = (uint8_t *)(void *)&AUDIO_Handle->headphone.frequency; - status = USBH_AUDIO_SetEndpointControls(phost,AUDIO_Handle->headphone.Ep, buff); - if(status == USBH_OK) - { - AUDIO_Handle->play_state = AUDIO_PLAYBACK_IDLE; - USBH_AUDIO_FrequencySet(phost); - } - break; - - case AUDIO_PLAYBACK_IDLE: - status = USBH_OK; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CLASS_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - case AUDIO_PLAYBACK_PLAY: - USBH_AUDIO_Transmit(phost); - status = USBH_OK; - break; - - default: - break; - } - - return status; -} - -/** - * @brief Handle Transmission process - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_AUDIO_Transmit (USBH_HandleTypeDef *phost) -{ - USBH_StatusTypeDef status = USBH_BUSY ; - AUDIO_HandleTypeDef *AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; - - switch(AUDIO_Handle->processing_state) - { - case AUDIO_DATA_START_OUT: - /* Sync with start of Even Frame */ - if((phost->Timer & 1U) == 0U) - { - AUDIO_Handle->headphone.timer = phost->Timer; - AUDIO_Handle->processing_state = AUDIO_DATA_OUT; - USBH_IsocSendData(phost, - AUDIO_Handle->headphone.buf, - (uint32_t)AUDIO_Handle->headphone.frame_length, - AUDIO_Handle->headphone.Pipe); - - AUDIO_Handle->headphone.partial_ptr = AUDIO_Handle->headphone.frame_length; - AUDIO_Handle->headphone.global_ptr = AUDIO_Handle->headphone.frame_length; - AUDIO_Handle->headphone.cbuf = AUDIO_Handle->headphone.buf; - } - else - { - -#if (USBH_USE_OS == 1U) - osDelay(1); - phost->os_msg = (uint32_t)USBH_CLASS_EVENT; - -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - break; - - case AUDIO_DATA_OUT: - if((USBH_LL_GetURBState(phost , AUDIO_Handle->headphone.Pipe) == USBH_URB_DONE)&& - (( phost->Timer - AUDIO_Handle->headphone.timer) >= AUDIO_Handle->headphone.Poll)) - { - AUDIO_Handle->headphone.timer = phost->Timer; - - if(AUDIO_Handle->control.supported == 1U) - { - USBH_AUDIO_Control (phost); - } - - if(AUDIO_Handle->headphone.global_ptr <= AUDIO_Handle->headphone.total_length) - { - USBH_IsocSendData(phost, - AUDIO_Handle->headphone.cbuf, - (uint32_t)AUDIO_Handle->headphone.frame_length, - AUDIO_Handle->headphone.Pipe); - - AUDIO_Handle->headphone.cbuf += AUDIO_Handle->headphone.frame_length; - AUDIO_Handle->headphone.partial_ptr += AUDIO_Handle->headphone.frame_length; - AUDIO_Handle->headphone.global_ptr += AUDIO_Handle->headphone.frame_length; - } - else - { - AUDIO_Handle->headphone.partial_ptr = 0xFFFFFFFFU; - AUDIO_Handle->play_state = AUDIO_PLAYBACK_IDLE; - USBH_AUDIO_BufferEmptyCallback(phost); - } - } - break; - - default: - status = USBH_FAIL; - break; - } - return status; -} - -/** - * @brief USBH_AUDIO_SetFrequency - * Set Audio sampling parameters - * @param phost: Host handle - * @param SampleRate: Sample Rate - * @param NbrChannels: Number of Channels - * @param BitPerSample: Bit Per Sample - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_AUDIO_SetFrequency (USBH_HandleTypeDef *phost, - uint16_t SampleRate, - uint8_t NbrChannels, - uint8_t BitPerSample) -{ - USBH_StatusTypeDef Status = USBH_BUSY; - AUDIO_HandleTypeDef *AUDIO_Handle; - uint8_t index; - uint8_t change_freq = FALSE; - uint32_t freq_min, freq_max; - uint8_t num_supported_freq; - - if(phost->gState == HOST_CLASS) - { - AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; - if(AUDIO_Handle->play_state == AUDIO_PLAYBACK_IDLE) - { - - if(AUDIO_Handle->class_desc.as_desc[AUDIO_Handle->headphone.asociated_as].FormatTypeDesc->bSamFreqType == 0U) - { - freq_min = LE24(AUDIO_Handle->class_desc.as_desc[AUDIO_Handle->headphone.asociated_as].FormatTypeDesc->tSamFreq[0]); - freq_max = LE24(AUDIO_Handle->class_desc.as_desc[AUDIO_Handle->headphone.asociated_as].FormatTypeDesc->tSamFreq[1]); - - if(( SampleRate >= freq_min)&& (SampleRate <= freq_max)) - { - change_freq = TRUE; - } - } - else - { - - num_supported_freq = (AUDIO_Handle->class_desc.as_desc[AUDIO_Handle->headphone.asociated_as].FormatTypeDesc->bLength - 8U) / 3U; - - - for(index = 0U; index < num_supported_freq; index++) - { - if(SampleRate == LE24(AUDIO_Handle->class_desc.as_desc[AUDIO_Handle->headphone.asociated_as].FormatTypeDesc->tSamFreq[index])) - { - change_freq = TRUE; - break; - } - } - } - - if(change_freq == TRUE) - { - AUDIO_Handle->headphone.frequency = SampleRate; - AUDIO_Handle->headphone.frame_length = (SampleRate * BitPerSample * NbrChannels) / 8000U; - AUDIO_Handle->play_state = AUDIO_PLAYBACK_SET_EP; - Status = USBH_OK; - - } - } - } - return Status; -} - -/** - * @brief USBH_AUDIO_Play - * Start playback process - * @param phost: Host handle - * @param buf: pointer to raw audio data - * @param length: total length of the audio data - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_AUDIO_Play (USBH_HandleTypeDef *phost, uint8_t *buf, uint32_t length) -{ - USBH_StatusTypeDef Status = USBH_FAIL; - AUDIO_HandleTypeDef *AUDIO_Handle; - - if(phost->gState == HOST_CLASS) - { - AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; - if(AUDIO_Handle->play_state == AUDIO_PLAYBACK_IDLE) - { - AUDIO_Handle->headphone.buf = buf; - AUDIO_Handle->headphone.total_length = length; - AUDIO_Handle->play_state = AUDIO_PLAYBACK_PLAY; - AUDIO_Handle->control_state = AUDIO_CONTROL_INIT; - AUDIO_Handle->processing_state = AUDIO_DATA_START_OUT; - Status = USBH_OK; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CLASS_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - } - return Status; -} - -/** - * @brief USBH_AUDIO_Pause - * Stop the playback process - * @param phost: Host handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_AUDIO_Stop (USBH_HandleTypeDef *phost) -{ - USBH_StatusTypeDef Status = USBH_FAIL; - Status = USBH_AUDIO_Suspend(phost); - return Status; -} - -/** - * @brief USBH_AUDIO_Suspend - * Suspend the playback process - * @param phost: Host handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_AUDIO_Suspend (USBH_HandleTypeDef *phost) -{ - USBH_StatusTypeDef Status = USBH_FAIL; - AUDIO_HandleTypeDef *AUDIO_Handle; - - if(phost->gState == HOST_CLASS) - { - AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; - if(AUDIO_Handle->play_state == AUDIO_PLAYBACK_PLAY) - { - AUDIO_Handle->control_state = AUDIO_CONTROL_IDLE; - AUDIO_Handle->play_state = AUDIO_PLAYBACK_IDLE; - Status = USBH_OK; - } - } - return Status; -} -/** - * @brief USBH_AUDIO_Resume - * Resume the playback process - * @param phost: Host handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_AUDIO_Resume (USBH_HandleTypeDef *phost) -{ - USBH_StatusTypeDef Status = USBH_FAIL; - AUDIO_HandleTypeDef *AUDIO_Handle; - - if(phost->gState == HOST_CLASS) - { - AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; - if(AUDIO_Handle->play_state == AUDIO_PLAYBACK_IDLE) - { - AUDIO_Handle->control_state = AUDIO_CONTROL_INIT; - AUDIO_Handle->play_state = AUDIO_PLAYBACK_PLAY; - } - } - return Status; -} -/** - * @brief USBH_AUDIO_GetOutOffset - * return the current buffer pointer for OUT process - * @param phost: Host handle - * @retval USBH Status - */ -int32_t USBH_AUDIO_GetOutOffset (USBH_HandleTypeDef *phost) -{ - AUDIO_HandleTypeDef *AUDIO_Handle; - - if(phost->gState == HOST_CLASS) - { - AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; - if(AUDIO_Handle->play_state == AUDIO_PLAYBACK_PLAY) - { - return (int32_t)AUDIO_Handle->headphone.partial_ptr; - } - } - return -1; -} - -/** - * @brief USBH_AUDIO_ChangeOutBuffer - * Change audio data buffer address - * @param phost: Host handle - * @param buf: buffer address - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_AUDIO_ChangeOutBuffer (USBH_HandleTypeDef *phost, uint8_t *buf) -{ - USBH_StatusTypeDef Status = USBH_FAIL; - AUDIO_HandleTypeDef *AUDIO_Handle; - - if(phost->gState == HOST_CLASS) - { - AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; - if(AUDIO_Handle->play_state == AUDIO_PLAYBACK_PLAY) - { - if(AUDIO_Handle->headphone.buf <= buf) - { - AUDIO_Handle->headphone.cbuf = buf; - if ( AUDIO_Handle->headphone.buf == buf) - { - AUDIO_Handle->headphone.partial_ptr = 0U; - } - Status = USBH_OK; - } - } - } - return Status; -} - -/** - * @brief USBH_AUDIO_SetControlAttribute - * Set Control Attribute - * @param phost: Host handle - * @param attrib: control attribute - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_AUDIO_SetControlAttribute (USBH_HandleTypeDef *phost, uint8_t attrib) -{ - USBH_StatusTypeDef status = USBH_BUSY; - AUDIO_HandleTypeDef *AUDIO_Handle; - - - AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; - - switch (attrib) - { - case 0x01: - AUDIO_Handle->headphone.attribute.volume += AUDIO_Handle->headphone.attribute.resolution; - break; - - case 0x02: - AUDIO_Handle->headphone.attribute.volume -= AUDIO_Handle->headphone.attribute.resolution; - break; - - default : - status = USBH_FAIL; - break; - } - - if(AUDIO_Handle->headphone.attribute.volume > AUDIO_Handle->headphone.attribute.volumeMax) - { - AUDIO_Handle->headphone.attribute.volume =AUDIO_Handle->headphone.attribute.volumeMax; - } - - if(AUDIO_Handle->headphone.attribute.volume < AUDIO_Handle->headphone.attribute.volumeMin) - { - AUDIO_Handle->headphone.attribute.volume =AUDIO_Handle->headphone.attribute.volumeMin; - } - - if(AUDIO_SetVolume (phost, - AUDIO_Handle->temp_feature, - (uint8_t)AUDIO_Handle->temp_channels, - (uint16_t)AUDIO_Handle->headphone.attribute.volume) != USBH_BUSY) - { - - if(AUDIO_Handle->temp_channels == 1U) - { - AUDIO_Handle->temp_feature = AUDIO_Handle->headphone.asociated_feature; - AUDIO_Handle->temp_channels = AUDIO_Handle->headphone.asociated_channels; - status = USBH_OK; - } - else - { - AUDIO_Handle->temp_channels--; - } - AUDIO_Handle->cs_req_state = AUDIO_REQ_GET_VOLUME; - } - - - return status; -} - - -/** - * @brief USBH_AUDIO_SetVolume - * Set Volume - * @param phost: Host handle - * @param volume: VOLUME_UP/ VOLUME_DOWN - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_AUDIO_SetVolume (USBH_HandleTypeDef *phost, AUDIO_VolumeCtrlTypeDef volume_ctl) -{ - AUDIO_HandleTypeDef *AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; - - if((volume_ctl == VOLUME_UP) || (volume_ctl == VOLUME_DOWN)) - { - if(phost->gState == HOST_CLASS) - { - AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; - if(AUDIO_Handle->play_state == AUDIO_PLAYBACK_PLAY) - { - AUDIO_Handle->control_state = (volume_ctl == VOLUME_UP)? AUDIO_CONTROL_VOLUME_UP : AUDIO_CONTROL_VOLUME_DOWN; - return USBH_OK; - } - } - } - - return USBH_FAIL; -} -/** - * @brief AUDIO_SetVolume - * Set Volume - * @param phost: Host handle - * @param feature: feature Unit index - * @param channel: channel index - * @param volume: new volume - * @retval USBH Status - */ -static USBH_StatusTypeDef AUDIO_SetVolume (USBH_HandleTypeDef *phost, uint8_t feature, uint8_t channel, uint16_t volume) -{ - USBH_StatusTypeDef status = USBH_BUSY ; - AUDIO_HandleTypeDef *AUDIO_Handle; - - - AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; - - AUDIO_Handle->mem[0] = volume; - - status = USBH_AC_SetCur(phost, UAC_FEATURE_UNIT, feature, - VOLUME_CONTROL, channel, 2U); - - return status; -} - -/** - * @brief The function informs user that Settings have been changed - * @param phost: Selected device - * @retval None - */ -__weak void USBH_AUDIO_FrequencySet(USBH_HandleTypeDef *phost) -{ - -} - -/** - * @brief The function informs user that User data are processed - * @param phost: Selected device - * @retval None - */ -__weak void USBH_AUDIO_BufferEmptyCallback(USBH_HandleTypeDef *phost) -{ - -} -/** -* @} -*/ - -/** -* @} -*/ - -/** -* @} -*/ - - -/** -* @} -*/ - - -/** -* @} -*/ - -/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbh_audio.c + * @author MCD Application Team + * @brief This file is the AC Layer Handlers for USB Host AC class. + * + * @verbatim + * + * =================================================================== + * AUDIO Class Description + * =================================================================== + * This driver manages the Audio Class 1.0 following the "USB Device + * Class Definition for Audio Devices V1.0 Mar 18, 98". + * + * @endverbatim + * + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + + /* BSPDependencies + - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" + - "stm32xxxxx_{eval}{discovery}_io.c" + - "stm32xxxxx_{eval}{discovery}{adafruit}_sd.c" + - "stm32xxxxx_{eval}{discovery}{adafruit}_lcd.c" + - "stm32xxxxx_{eval}{discovery}_sdram.c" + EndBSPDependencies */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_audio.h" + +/** @addtogroup USBH_LIB + * @{ + */ + +/** @addtogroup USBH_CLASS + * @{ + */ + +/** @addtogroup USBH_AUDIO_CLASS + * @{ + */ + +/** @defgroup USBH_AUDIO_CORE + * @brief This file includes HID Layer Handlers for USB Host HID class. + * @{ + */ + +/** @defgroup USBH_AUDIO_CORE_Private_TypesDefinitions + * @{ + */ +/** + * @} + */ + + +/** @defgroup USBH_AUDIO_CORE_Private_Defines + * @{ + */ + +/** + * @} + */ + + +/** @defgroup USBH_AUDIO_CORE_Private_Macros + * @{ + */ +/** + * @} + */ + + +/** @defgroup USBH_AUDIO_CORE_Private_Variables + * @{ + */ + +/** + * @} + */ + + +/** @defgroup USBH_AUDIO_CORE_Private_FunctionPrototypes + * @{ + */ + +static USBH_StatusTypeDef USBH_AUDIO_InterfaceInit (USBH_HandleTypeDef *phost); +static USBH_StatusTypeDef USBH_AUDIO_InterfaceDeInit (USBH_HandleTypeDef *phost); +static USBH_StatusTypeDef USBH_AUDIO_Process(USBH_HandleTypeDef *phost); +static USBH_StatusTypeDef USBH_AUDIO_SOFProcess(USBH_HandleTypeDef *phost); +static USBH_StatusTypeDef USBH_AUDIO_ClassRequest(USBH_HandleTypeDef *phost); +static USBH_StatusTypeDef USBH_AUDIO_CSRequest(USBH_HandleTypeDef *phost, + uint8_t feature, + uint8_t channel); + +static USBH_StatusTypeDef USBH_AUDIO_HandleCSRequest(USBH_HandleTypeDef *phost); + +static USBH_StatusTypeDef USBH_AUDIO_FindAudioStreamingIN(USBH_HandleTypeDef *phost); +static USBH_StatusTypeDef USBH_AUDIO_FindAudioStreamingOUT(USBH_HandleTypeDef *phost); +static USBH_StatusTypeDef USBH_AUDIO_FindHIDControl(USBH_HandleTypeDef *phost); +static USBH_StatusTypeDef USBH_AUDIO_ParseCSDescriptors(USBH_HandleTypeDef *phost); + +static USBH_StatusTypeDef USBH_AUDIO_BuildHeadphonePath(USBH_HandleTypeDef *phost); +static USBH_StatusTypeDef USBH_AUDIO_BuildMicrophonePath(USBH_HandleTypeDef *phost); +int32_t USBH_AUDIO_FindLinkedUnitIN(USBH_HandleTypeDef *phost, uint8_t UnitID); +int32_t USBH_AUDIO_FindLinkedUnitOUT(USBH_HandleTypeDef *phost, uint8_t UnitID); + + + +static USBH_StatusTypeDef ParseCSDescriptors(AUDIO_ClassSpecificDescTypedef *class_desc, + uint8_t ac_subclass, + uint8_t *pdesc); + + +static USBH_StatusTypeDef USBH_AUDIO_Transmit (USBH_HandleTypeDef *phost); + + +static USBH_StatusTypeDef USBH_AC_SetCur(USBH_HandleTypeDef *phost, + uint8_t subtype, + uint8_t feature, + uint8_t controlSelector, + uint8_t channel, + uint16_t length); + +static USBH_StatusTypeDef USBH_AC_GetCur(USBH_HandleTypeDef *phost, + uint8_t subtype, + uint8_t feature, + uint8_t controlSelector, + uint8_t channel, + uint16_t length); + +static USBH_StatusTypeDef USBH_AC_GetMin(USBH_HandleTypeDef *phost, + uint8_t subtype, + uint8_t feature, + uint8_t controlSelector, + uint8_t channel, + uint16_t length); + +static USBH_StatusTypeDef USBH_AC_GetMax(USBH_HandleTypeDef *phost, + uint8_t subtype, + uint8_t feature, + uint8_t controlSelector, + uint8_t channel, + uint16_t length); + +static USBH_StatusTypeDef USBH_AC_GetRes(USBH_HandleTypeDef *phost, + uint8_t subtype, + uint8_t feature, + uint8_t controlSelector, + uint8_t channel, + uint16_t length); + +static USBH_StatusTypeDef USBH_AUDIO_SetEndpointControls(USBH_HandleTypeDef *phost, + uint8_t Ep, + uint8_t *buff); + +static USBH_StatusTypeDef AUDIO_SetVolume (USBH_HandleTypeDef *phost, uint8_t feature, uint8_t channel, uint16_t volume); + +static USBH_StatusTypeDef USBH_AUDIO_InputStream (USBH_HandleTypeDef *phost); +static USBH_StatusTypeDef USBH_AUDIO_OutputStream (USBH_HandleTypeDef *phost); +static USBH_StatusTypeDef USBH_AUDIO_Control (USBH_HandleTypeDef *phost); +static USBH_StatusTypeDef USBH_AUDIO_SetControlAttribute (USBH_HandleTypeDef *phost, uint8_t attrib); +static uint32_t USBH_AUDIO_FindLinkedUnit(USBH_HandleTypeDef *phost, uint8_t UnitID); + +USBH_ClassTypeDef AUDIO_Class = +{ + "AUDIO", + AC_CLASS, + USBH_AUDIO_InterfaceInit, + USBH_AUDIO_InterfaceDeInit, + USBH_AUDIO_ClassRequest, + USBH_AUDIO_Process, + USBH_AUDIO_SOFProcess, + NULL, +}; + +/** + * @} + */ + +/** @defgroup USBH_AUDIO_CORE_Private_Functions + * @{ + */ + +/** + * @brief USBH_AUDIO_InterfaceInit + * The function init the Audio class. + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_AUDIO_InterfaceInit (USBH_HandleTypeDef *phost) +{ + USBH_StatusTypeDef status = USBH_FAIL ; + USBH_StatusTypeDef out_status, in_status ; + AUDIO_HandleTypeDef *AUDIO_Handle; + uint8_t interface, index; + uint16_t ep_size_out = 0U; + uint16_t ep_size_in = 0U; + + interface = USBH_FindInterface(phost, AC_CLASS, USB_SUBCLASS_AUDIOCONTROL, 0x00U); + + if(interface == 0xFFU) /* Not Valid Interface */ + { + USBH_DbgLog ("Cannot Find the interface for %s class.", phost->pActiveClass->Name); + status = USBH_FAIL; + } + else + { + + + phost->pActiveClass->pData = (AUDIO_HandleTypeDef *)USBH_malloc (sizeof(AUDIO_HandleTypeDef)); + AUDIO_Handle = (AUDIO_HandleTypeDef *) phost->pActiveClass->pData; + USBH_memset(AUDIO_Handle, 0, sizeof(AUDIO_HandleTypeDef)); + + + /* 1st Step: Find Audio Interfaces */ + out_status = USBH_AUDIO_FindAudioStreamingIN (phost); + + in_status = USBH_AUDIO_FindAudioStreamingOUT(phost); + + if((out_status == USBH_FAIL) && (in_status == USBH_FAIL)) + { + USBH_DbgLog ("%s class configuration not supported.", phost->pActiveClass->Name); + } + else + { + /* 2nd Step: Select Audio Streaming Interfaces with largest endpoint size : default behavior*/ + for (index = 0U; index < AUDIO_MAX_AUDIO_STD_INTERFACE; index ++) + { + if( AUDIO_Handle->stream_out[index].valid == 1U) + { + if(ep_size_out < AUDIO_Handle->stream_out[index].EpSize) + { + ep_size_out = AUDIO_Handle->stream_out[index].EpSize; + AUDIO_Handle->headphone.interface = AUDIO_Handle->stream_out[index].interface; + AUDIO_Handle->headphone.AltSettings = AUDIO_Handle->stream_out[index].AltSettings; + AUDIO_Handle->headphone.Ep = AUDIO_Handle->stream_out[index].Ep; + AUDIO_Handle->headphone.EpSize = AUDIO_Handle->stream_out[index].EpSize; + AUDIO_Handle->headphone.Poll = (uint8_t)AUDIO_Handle->stream_out[index].Poll; + AUDIO_Handle->headphone.supported = 1U; + } + } + + if( AUDIO_Handle->stream_in[index].valid == 1U) + { + if(ep_size_in < AUDIO_Handle->stream_in[index].EpSize) + { + ep_size_in = AUDIO_Handle->stream_in[index].EpSize; + AUDIO_Handle->microphone.interface = AUDIO_Handle->stream_in[index].interface; + AUDIO_Handle->microphone.AltSettings = AUDIO_Handle->stream_in[index].AltSettings; + AUDIO_Handle->microphone.Ep = AUDIO_Handle->stream_in[index].Ep; + AUDIO_Handle->microphone.EpSize = AUDIO_Handle->stream_in[index].EpSize; + AUDIO_Handle->microphone.Poll = (uint8_t)AUDIO_Handle->stream_out[index].Poll; + AUDIO_Handle->microphone.supported = 1U; + } + } + } + + if(USBH_AUDIO_FindHIDControl(phost) == USBH_OK) + { + AUDIO_Handle->control.supported = 1U; + } + + /* 3rd Step: Find and Parse Audio interfaces */ + USBH_AUDIO_ParseCSDescriptors (phost); + + + /* 4th Step: Open the Audio streaming pipes*/ + if(AUDIO_Handle->headphone.supported == 1U) + { + USBH_AUDIO_BuildHeadphonePath (phost); + + AUDIO_Handle->headphone.Pipe = USBH_AllocPipe(phost, AUDIO_Handle->headphone.Ep); + + /* Open pipe for IN endpoint */ + USBH_OpenPipe (phost, + AUDIO_Handle->headphone.Pipe, + AUDIO_Handle->headphone.Ep, + phost->device.address, + phost->device.speed, + USB_EP_TYPE_ISOC, + AUDIO_Handle->headphone.EpSize); + + USBH_LL_SetToggle (phost, AUDIO_Handle->headphone.Pipe, 0U); + + } + + if(AUDIO_Handle->microphone.supported == 1U) + { + USBH_AUDIO_BuildMicrophonePath (phost); + AUDIO_Handle->microphone.Pipe = USBH_AllocPipe(phost, AUDIO_Handle->microphone.Ep); + + /* Open pipe for IN endpoint */ + USBH_OpenPipe (phost, + AUDIO_Handle->microphone.Pipe, + AUDIO_Handle->microphone.Ep, + phost->device.address, + phost->device.speed, + USB_EP_TYPE_ISOC, + AUDIO_Handle->microphone.EpSize); + + USBH_LL_SetToggle (phost, AUDIO_Handle->microphone.Pipe, 0U); + } + + if(AUDIO_Handle->control.supported == 1U) + { + AUDIO_Handle->control.Pipe = USBH_AllocPipe(phost, AUDIO_Handle->control.Ep); + + /* Open pipe for IN endpoint */ + USBH_OpenPipe (phost, + AUDIO_Handle->control.Pipe, + AUDIO_Handle->control.Ep, + phost->device.address, + phost->device.speed, + USB_EP_TYPE_INTR, + AUDIO_Handle->control.EpSize); + + USBH_LL_SetToggle (phost, AUDIO_Handle->control.Pipe, 0U); + + } + + AUDIO_Handle->req_state = AUDIO_REQ_INIT; + AUDIO_Handle->control_state = AUDIO_CONTROL_INIT; + + status = USBH_OK; + } + } + return status; +} + + + +/** + * @brief USBH_AUDIO_InterfaceDeInit + * The function DeInit the Pipes used for the Audio class. + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_AUDIO_InterfaceDeInit (USBH_HandleTypeDef *phost) +{ + AUDIO_HandleTypeDef *AUDIO_Handle = (AUDIO_HandleTypeDef *) phost->pActiveClass->pData; + + if(AUDIO_Handle->microphone.Pipe != 0x00U) + { + USBH_ClosePipe (phost, AUDIO_Handle->microphone.Pipe); + USBH_FreePipe (phost, AUDIO_Handle->microphone.Pipe); + AUDIO_Handle->microphone.Pipe = 0U; /* Reset the pipe as Free */ + } + + if( AUDIO_Handle->headphone.Pipe != 0x00U) + { + USBH_ClosePipe(phost, AUDIO_Handle->headphone.Pipe); + USBH_FreePipe (phost, AUDIO_Handle->headphone.Pipe); + AUDIO_Handle->headphone.Pipe = 0U; /* Reset the pipe as Free */ + } + + if( AUDIO_Handle->control.Pipe != 0x00U) + { + USBH_ClosePipe(phost, AUDIO_Handle->control.Pipe); + USBH_FreePipe (phost, AUDIO_Handle->control.Pipe); + AUDIO_Handle->control.Pipe = 0U; /* Reset the pipe as Free */ + } + + if(phost->pActiveClass->pData) + { + USBH_free (phost->pActiveClass->pData); + phost->pActiveClass->pData = 0; + } + return USBH_OK ; +} + +/** + * @brief USBH_AUDIO_ClassRequest + * The function is responsible for handling Standard requests + * for Audio class. + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_AUDIO_ClassRequest(USBH_HandleTypeDef *phost) +{ + AUDIO_HandleTypeDef *AUDIO_Handle = (AUDIO_HandleTypeDef *) phost->pActiveClass->pData; + USBH_StatusTypeDef status = USBH_BUSY; + USBH_StatusTypeDef req_status = USBH_BUSY; + + /* Switch AUDIO REQ state machine */ + switch (AUDIO_Handle->req_state) + { + case AUDIO_REQ_INIT: + case AUDIO_REQ_SET_DEFAULT_IN_INTERFACE: + if(AUDIO_Handle->microphone.supported == 1U) + { + req_status = USBH_SetInterface(phost, + AUDIO_Handle->microphone.interface, + 0U); + + if(req_status == USBH_OK) + { + AUDIO_Handle->req_state = AUDIO_REQ_SET_DEFAULT_OUT_INTERFACE; + } + + } + else + { + AUDIO_Handle->req_state = AUDIO_REQ_SET_DEFAULT_OUT_INTERFACE; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + break; + + case AUDIO_REQ_SET_DEFAULT_OUT_INTERFACE: + if(AUDIO_Handle->headphone.supported == 1U) + { + req_status = USBH_SetInterface(phost, + AUDIO_Handle->headphone.interface, + 0U); + + if(req_status == USBH_OK) + { + AUDIO_Handle->req_state = AUDIO_REQ_CS_REQUESTS; + AUDIO_Handle->cs_req_state = AUDIO_REQ_GET_VOLUME; + + AUDIO_Handle->temp_feature = AUDIO_Handle->headphone.asociated_feature; + AUDIO_Handle->temp_channels = AUDIO_Handle->headphone.asociated_channels; + } + } + else + { + AUDIO_Handle->req_state = AUDIO_REQ_CS_REQUESTS; + AUDIO_Handle->cs_req_state = AUDIO_REQ_GET_VOLUME; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + break; + + case AUDIO_REQ_CS_REQUESTS: + if(USBH_AUDIO_HandleCSRequest (phost) == USBH_OK) + { + AUDIO_Handle->req_state = AUDIO_REQ_SET_IN_INTERFACE; + } + break; + + case AUDIO_REQ_SET_IN_INTERFACE: + if(AUDIO_Handle->microphone.supported == 1U) + { + req_status = USBH_SetInterface(phost, + AUDIO_Handle->microphone.interface, + AUDIO_Handle->microphone.AltSettings); + + if(req_status == USBH_OK) + { + AUDIO_Handle->req_state = AUDIO_REQ_SET_OUT_INTERFACE; + } + } + else + { + AUDIO_Handle->req_state = AUDIO_REQ_SET_OUT_INTERFACE; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + break; + case AUDIO_REQ_SET_OUT_INTERFACE: + if(AUDIO_Handle->headphone.supported == 1U) + { + req_status = USBH_SetInterface(phost, + AUDIO_Handle->headphone.interface, + AUDIO_Handle->headphone.AltSettings); + + if(req_status == USBH_OK) + { + AUDIO_Handle->req_state = AUDIO_REQ_IDLE; + } + + } + else + { + AUDIO_Handle->req_state = AUDIO_REQ_IDLE; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + break; + case AUDIO_REQ_IDLE: + AUDIO_Handle->play_state = AUDIO_PLAYBACK_INIT; + phost->pUser(phost, HOST_USER_CLASS_ACTIVE); + status = USBH_OK; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CLASS_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + default: + break; + } + return status; +} + +/** + * @brief USBH_AUDIO_CSRequest + * The function is responsible for handling AC Specific requests for a specific feature and channel + * for Audio class. + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_AUDIO_CSRequest(USBH_HandleTypeDef *phost, uint8_t feature, uint8_t channel) +{ + AUDIO_HandleTypeDef *AUDIO_Handle = (AUDIO_HandleTypeDef *) phost->pActiveClass->pData; + USBH_StatusTypeDef status = USBH_BUSY; + USBH_StatusTypeDef req_status = USBH_BUSY; + uint16_t VolumeCtl, ResolutionCtl; + + /* Switch AUDIO REQ state machine */ + switch (AUDIO_Handle->cs_req_state) + { + case AUDIO_REQ_GET_VOLUME: + req_status = USBH_AC_GetCur(phost, + UAC_FEATURE_UNIT, /* subtype */ + feature, /* feature */ + VOLUME_CONTROL, /* Selector */ + channel, /* channel */ + 0x02U); /* length */ + if(req_status != USBH_BUSY) + { + AUDIO_Handle->cs_req_state = AUDIO_REQ_GET_MIN_VOLUME; + VolumeCtl = LE16(&(AUDIO_Handle->mem[0])); + AUDIO_Handle->headphone.attribute.volume = (uint32_t)VolumeCtl; + } + break; + + case AUDIO_REQ_GET_MIN_VOLUME: + req_status = USBH_AC_GetMin(phost, + UAC_FEATURE_UNIT, /* subtype */ + feature, /* feature */ + VOLUME_CONTROL, /* Selector */ + channel, /* channel */ + 0x02U); /* length */ + if(req_status != USBH_BUSY) + { + AUDIO_Handle->cs_req_state = AUDIO_REQ_GET_MAX_VOLUME; + VolumeCtl = LE16(&(AUDIO_Handle->mem[0])); + AUDIO_Handle->headphone.attribute.volumeMin = (uint32_t)VolumeCtl; + } + break; + + case AUDIO_REQ_GET_MAX_VOLUME: + req_status = USBH_AC_GetMax(phost, + UAC_FEATURE_UNIT, /* subtype */ + feature, /* feature */ + VOLUME_CONTROL, /* Selector */ + channel, /* channel */ + 0x02U); /* length */ + if(req_status != USBH_BUSY) + { + AUDIO_Handle->cs_req_state = AUDIO_REQ_GET_RESOLUTION; + VolumeCtl = LE16(&(AUDIO_Handle->mem[0])); + AUDIO_Handle->headphone.attribute.volumeMax = (uint32_t)VolumeCtl; + + if (AUDIO_Handle->headphone.attribute.volumeMax < AUDIO_Handle->headphone.attribute.volumeMin) + { + AUDIO_Handle->headphone.attribute.volumeMax = 0xFF00U; + } + } + break; + + case AUDIO_REQ_GET_RESOLUTION: + req_status = USBH_AC_GetRes(phost, + UAC_FEATURE_UNIT, /* subtype */ + feature, /* feature */ + VOLUME_CONTROL, /* Selector */ + channel, /* channel */ + 0x02U); /* length */ + if(req_status != USBH_BUSY) + { + AUDIO_Handle->cs_req_state = AUDIO_REQ_CS_IDLE; + ResolutionCtl =LE16(&AUDIO_Handle->mem[0]); + AUDIO_Handle->headphone.attribute.resolution = (uint32_t)ResolutionCtl; + } + break; + + + case AUDIO_REQ_CS_IDLE: + status = USBH_OK; + break; + + default: + break; + } + return status; +} + +/** + * @brief USBH_AUDIO_HandleCSRequest + * The function is responsible for handling AC Specific requests for a all features + * and associated channels for Audio class. + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_AUDIO_HandleCSRequest(USBH_HandleTypeDef *phost) +{ + + USBH_StatusTypeDef status = USBH_BUSY; + USBH_StatusTypeDef cs_status = USBH_BUSY; + AUDIO_HandleTypeDef *AUDIO_Handle = (AUDIO_HandleTypeDef *) phost->pActiveClass->pData; + + cs_status = USBH_AUDIO_CSRequest(phost, + AUDIO_Handle->temp_feature, + AUDIO_Handle->temp_channels); + + if(cs_status != USBH_BUSY) + { + + if(AUDIO_Handle->temp_channels == 1U) + { + AUDIO_Handle->temp_feature = AUDIO_Handle->headphone.asociated_feature; + AUDIO_Handle->temp_channels = 0U; + status = USBH_OK; + } + else + { + AUDIO_Handle->temp_channels--; + } + AUDIO_Handle->cs_req_state = AUDIO_REQ_GET_VOLUME; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + + return status; +} + +/** + * @brief USBH_AUDIO_Process + * The function is for managing state machine for Audio data transfers + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_AUDIO_Process (USBH_HandleTypeDef *phost) +{ + USBH_StatusTypeDef status = USBH_BUSY; + AUDIO_HandleTypeDef *AUDIO_Handle = (AUDIO_HandleTypeDef *) phost->pActiveClass->pData; + + if(AUDIO_Handle->headphone.supported == 1U) + { + USBH_AUDIO_OutputStream (phost); + } + + if(AUDIO_Handle->microphone.supported == 1U) + { + USBH_AUDIO_InputStream (phost); + } + + return status; +} + +/** + * @brief USBH_AUDIO_SOFProcess + * The function is for managing the SOF callback + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_AUDIO_SOFProcess (USBH_HandleTypeDef *phost) +{ + return USBH_OK; +} +/** + * @brief Find IN Audio Streaming interfaces + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_AUDIO_FindAudioStreamingIN(USBH_HandleTypeDef *phost) +{ + uint8_t interface, alt_settings; + USBH_StatusTypeDef status = USBH_FAIL ; + AUDIO_HandleTypeDef *AUDIO_Handle; + + AUDIO_Handle = (AUDIO_HandleTypeDef *) phost->pActiveClass->pData; + + /* Look For AUDIOSTREAMING IN interface */ + alt_settings = 0U; + for (interface = 0U; interface < USBH_MAX_NUM_INTERFACES; interface++) + { + if((phost->device.CfgDesc.Itf_Desc[interface].bInterfaceClass == AC_CLASS)&& + (phost->device.CfgDesc.Itf_Desc[interface].bInterfaceSubClass == USB_SUBCLASS_AUDIOSTREAMING)) + { + if((phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress & 0x80U) && + (phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].wMaxPacketSize > 0U)) + { + AUDIO_Handle->stream_in[alt_settings].Ep = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress; + AUDIO_Handle->stream_in[alt_settings].EpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].wMaxPacketSize; + AUDIO_Handle->stream_in[alt_settings].interface = phost->device.CfgDesc.Itf_Desc[interface].bInterfaceNumber; + AUDIO_Handle->stream_in[alt_settings].AltSettings = phost->device.CfgDesc.Itf_Desc[interface].bAlternateSetting; + AUDIO_Handle->stream_in[alt_settings].Poll = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bInterval; + AUDIO_Handle->stream_in[alt_settings].valid = 1U; + alt_settings++; + } + } + } + + if(alt_settings > 0U) + { + status = USBH_OK; + } + + return status; +} + +/** + * @brief Find OUT Audio Streaming interfaces + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_AUDIO_FindAudioStreamingOUT(USBH_HandleTypeDef *phost) +{ + uint8_t interface, alt_settings; + USBH_StatusTypeDef status = USBH_FAIL ; + AUDIO_HandleTypeDef *AUDIO_Handle; + + AUDIO_Handle = (AUDIO_HandleTypeDef *) phost->pActiveClass->pData; + + /* Look For AUDIOSTREAMING IN interface */ + alt_settings = 0U; + for (interface = 0U; interface < USBH_MAX_NUM_INTERFACES; interface++) + { + if((phost->device.CfgDesc.Itf_Desc[interface].bInterfaceClass == AC_CLASS)&& + (phost->device.CfgDesc.Itf_Desc[interface].bInterfaceSubClass == USB_SUBCLASS_AUDIOSTREAMING)) + { + if(((phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress & 0x80U) == 0x00U) && + (phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].wMaxPacketSize > 0U)) + { + AUDIO_Handle->stream_out[alt_settings].Ep = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress; + AUDIO_Handle->stream_out[alt_settings].EpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].wMaxPacketSize; + AUDIO_Handle->stream_out[alt_settings].interface = phost->device.CfgDesc.Itf_Desc[interface].bInterfaceNumber; + AUDIO_Handle->stream_out[alt_settings].AltSettings = phost->device.CfgDesc.Itf_Desc[interface].bAlternateSetting; + AUDIO_Handle->stream_out[alt_settings].Poll = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bInterval; + AUDIO_Handle->stream_out[alt_settings].valid = 1U; + alt_settings++; + } + } + } + + if(alt_settings > 0U) + { + status = USBH_OK; + } + + return status; +} + +/** + * @brief Find HID Control interfaces + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_AUDIO_FindHIDControl(USBH_HandleTypeDef *phost) +{ + uint8_t interface; + USBH_StatusTypeDef status = USBH_FAIL; + AUDIO_HandleTypeDef *AUDIO_Handle; + + AUDIO_Handle = (AUDIO_HandleTypeDef *) phost->pActiveClass->pData; + + /* Look For AUDIOCONTROL interface */ + interface = USBH_FindInterface(phost, AC_CLASS, USB_SUBCLASS_AUDIOCONTROL, 0xFFU); + if(interface == 0xFFU) + { + return USBH_FAIL; + } + + for (interface = 0U; interface < USBH_MAX_NUM_INTERFACES; interface++) + { + if((phost->device.CfgDesc.Itf_Desc[interface].bInterfaceClass == 0x03U) && /*HID*/ + (phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].wMaxPacketSize > 0U)) + { + if((phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress & 0x80U) == 0x80U) + { + AUDIO_Handle->control.Ep = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress; + AUDIO_Handle->control.EpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].wMaxPacketSize; + AUDIO_Handle->control.interface = phost->device.CfgDesc.Itf_Desc[interface].bInterfaceNumber; + AUDIO_Handle->control.Poll = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bInterval; + AUDIO_Handle->control.supported = 1U; + status = USBH_OK; + break; + } + } + } + + return status; +} + +/** + * @brief Parse AC and interfaces Descriptors + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_AUDIO_ParseCSDescriptors(USBH_HandleTypeDef *phost) +{ + USBH_DescHeader_t *pdesc; + uint16_t ptr; + uint8_t itf_index = 0U; + uint8_t itf_number = 0U; + uint8_t alt_setting; + AUDIO_HandleTypeDef *AUDIO_Handle; + + AUDIO_Handle = (AUDIO_HandleTypeDef *) phost->pActiveClass->pData; + pdesc = (USBH_DescHeader_t *)(void *)(phost->device.CfgDesc_Raw); + ptr = USB_LEN_CFG_DESC; + + AUDIO_Handle->class_desc.FeatureUnitNum = 0U; + AUDIO_Handle->class_desc.InputTerminalNum = 0U; + AUDIO_Handle->class_desc.OutputTerminalNum = 0U; + AUDIO_Handle->class_desc.ASNum = 0U; + + while(ptr < phost->device.CfgDesc.wTotalLength) + { + pdesc = USBH_GetNextDesc((uint8_t*)(void *)pdesc, &ptr); + + switch (pdesc->bDescriptorType) + { + + case USB_DESC_TYPE_INTERFACE: + itf_number = *((uint8_t *)(void *)pdesc + 2U); + alt_setting = *((uint8_t *)(void *)pdesc + 3U); + itf_index = USBH_FindInterfaceIndex (phost, itf_number, alt_setting); + break; + + case USB_DESC_TYPE_CS_INTERFACE: + if(itf_number <= phost->device.CfgDesc.bNumInterfaces) + { + + ParseCSDescriptors(&AUDIO_Handle->class_desc, + phost->device.CfgDesc.Itf_Desc[itf_index].bInterfaceSubClass, + (uint8_t *)(void *)pdesc); + } + break; + + default: + break; + } + } + return USBH_OK; +} + +/** + * @brief Parse AC interfaces + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef ParseCSDescriptors(AUDIO_ClassSpecificDescTypedef *class_desc, + uint8_t ac_subclass, + uint8_t *pdesc) +{ + if(ac_subclass == USB_SUBCLASS_AUDIOCONTROL) + { + switch(pdesc[2]) + { + case UAC_HEADER: + class_desc->cs_desc.HeaderDesc = (AUDIO_HeaderDescTypeDef *)(void *)pdesc; + break; + + case UAC_INPUT_TERMINAL: + class_desc->cs_desc.InputTerminalDesc[class_desc->InputTerminalNum++] = (AUDIO_ITDescTypeDef*)(void *)pdesc; + break; + + case UAC_OUTPUT_TERMINAL: + class_desc->cs_desc.OutputTerminalDesc[class_desc->OutputTerminalNum++] = (AUDIO_OTDescTypeDef*)(void *)pdesc; + break; + + case UAC_FEATURE_UNIT: + class_desc->cs_desc.FeatureUnitDesc[class_desc->FeatureUnitNum++] = (AUDIO_FeatureDescTypeDef*)(void *)pdesc; + break; + + case UAC_SELECTOR_UNIT: + class_desc->cs_desc.SelectorUnitDesc[class_desc->SelectorUnitNum++] = (AUDIO_SelectorDescTypeDef*)(void *)pdesc; + break; + + case UAC_MIXER_UNIT: + class_desc->cs_desc.MixerUnitDesc[class_desc->MixerUnitNum++] = (AUDIO_MixerDescTypeDef*)(void *)pdesc; + break; + + default: + break; + } + } + else + { + if(ac_subclass == USB_SUBCLASS_AUDIOSTREAMING) + { + switch(pdesc[2]) + { + case UAC_AS_GENERAL: + class_desc->as_desc[class_desc->ASNum].GeneralDesc = (AUDIO_ASGeneralDescTypeDef*)(void *)pdesc; + break; + case UAC_FORMAT_TYPE: + class_desc->as_desc[class_desc->ASNum++].FormatTypeDesc = (AUDIO_ASFormatTypeDescTypeDef*)(void *)pdesc; + break; + default: + break; + } + } + } + + return USBH_OK; +} + + +/** + * @brief Link a Unit to next associated one + * @param phost: Host handle + * @param UnitID: Unit identifer + * @retval UnitID, Index and Type of the associated Unit + */ +static uint32_t USBH_AUDIO_FindLinkedUnit(USBH_HandleTypeDef *phost, uint8_t UnitID) +{ + uint8_t Index; + AUDIO_HandleTypeDef *AUDIO_Handle; + + AUDIO_Handle = (AUDIO_HandleTypeDef *) phost->pActiveClass->pData; + + /* Find Feature Unit */ + for(Index = 0U; Index < AUDIO_Handle->class_desc.FeatureUnitNum; Index ++) + { + if(AUDIO_Handle->class_desc.cs_desc.FeatureUnitDesc[Index]->bSourceID == UnitID) + { + UnitID = AUDIO_Handle->class_desc.cs_desc.FeatureUnitDesc[Index]->bUnitID; + + return (((uint32_t)UnitID << 16U) | (UAC_FEATURE_UNIT << 8U) | (uint32_t)Index); + } + } + + /* Find Mixer Unit */ + for(Index = 0U; Index < AUDIO_Handle->class_desc.MixerUnitNum; Index ++) + { + if((AUDIO_Handle->class_desc.cs_desc.MixerUnitDesc[Index]->bSourceID0 == UnitID)|| + (AUDIO_Handle->class_desc.cs_desc.MixerUnitDesc[Index]->bSourceID1 == UnitID)) + { + UnitID = AUDIO_Handle->class_desc.cs_desc.MixerUnitDesc[Index]->bUnitID; + + return ((UnitID << 16U) | (UAC_MIXER_UNIT << 8U) | Index); + } + } + + /* Find Selector Unit */ + for(Index = 0U; Index < AUDIO_Handle->class_desc.SelectorUnitNum; Index ++) + { + if(AUDIO_Handle->class_desc.cs_desc.SelectorUnitDesc[Index]->bSourceID0 == UnitID) + { + UnitID = AUDIO_Handle->class_desc.cs_desc.SelectorUnitDesc[Index]->bUnitID; + + return ((UnitID << 16U) | (UAC_SELECTOR_UNIT << 8U) | Index); + } + } + + /* Find OT Unit */ + for(Index = 0U; Index < AUDIO_Handle->class_desc.OutputTerminalNum; Index ++) + { + if(AUDIO_Handle->class_desc.cs_desc.OutputTerminalDesc[Index]->bSourceID == UnitID) + { + UnitID = AUDIO_Handle->class_desc.cs_desc.OutputTerminalDesc[Index]->bTerminalID; + + return ((UnitID << 16U) | (UAC_OUTPUT_TERMINAL << 8U) | Index); + } + } + + /* No associated Unit found return undefined ID 0x00*/ + return 0U; +} + +/** + * @brief Build full path for Microphone device + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_AUDIO_BuildMicrophonePath(USBH_HandleTypeDef *phost) +{ + uint8_t UnitID = 0U, Type, Index; + uint32_t value; + uint8_t terminalIndex; + AUDIO_HandleTypeDef *AUDIO_Handle; + USBH_StatusTypeDef ret = USBH_OK; + + AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; + + /*Find microphone IT*/ + for(terminalIndex = 0U; terminalIndex < AUDIO_Handle->class_desc.InputTerminalNum; terminalIndex++) + { + if(LE16(AUDIO_Handle->class_desc.cs_desc.InputTerminalDesc[terminalIndex]->wTerminalType) == 0x201) + { + UnitID = AUDIO_Handle->class_desc.cs_desc.InputTerminalDesc[terminalIndex]->bTerminalID; + AUDIO_Handle->microphone.asociated_channels = AUDIO_Handle->class_desc.cs_desc.InputTerminalDesc[terminalIndex]->bNrChannels; + break; + } + } + + do + { + value = USBH_AUDIO_FindLinkedUnit(phost, UnitID); + + if (!value) + { + return USBH_FAIL; + } + + Index = (uint8_t)(value & 0xFFU); + Type = (uint8_t)((value >> 8U) & 0xFFU); + UnitID = (uint8_t)((value >> 16U) & 0xFFU); + + switch (Type) + { + case UAC_FEATURE_UNIT: + AUDIO_Handle->microphone.asociated_feature = Index; + break; + + case UAC_MIXER_UNIT: + AUDIO_Handle->microphone.asociated_mixer = Index; + break; + + case UAC_SELECTOR_UNIT: + AUDIO_Handle->microphone.asociated_selector = Index; + break; + + case UAC_OUTPUT_TERMINAL: + AUDIO_Handle->microphone.asociated_terminal = Index; + break; + + default: + ret = USBH_FAIL; + break; + } + } + while ((Type != UAC_OUTPUT_TERMINAL) && (value > 0U)); + + return ret; +} + +/** + * @brief Build full path for Headphone device + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_AUDIO_BuildHeadphonePath(USBH_HandleTypeDef *phost) +{ + uint8_t UnitID = 0U, Type, Index; + uint32_t value; + uint8_t terminalIndex; + AUDIO_HandleTypeDef *AUDIO_Handle; + USBH_StatusTypeDef ret = USBH_OK; + + AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; + + /* Find association between audio streaming and microphone */ + for(terminalIndex = 0U; terminalIndex < AUDIO_Handle->class_desc.InputTerminalNum; terminalIndex++) + { + if(LE16(AUDIO_Handle->class_desc.cs_desc.InputTerminalDesc[terminalIndex]->wTerminalType) == 0x101) + { + UnitID = AUDIO_Handle->class_desc.cs_desc.InputTerminalDesc[terminalIndex]->bTerminalID; + AUDIO_Handle->headphone.asociated_channels = AUDIO_Handle->class_desc.cs_desc.InputTerminalDesc[terminalIndex]->bNrChannels; + break; + } + } + + for(Index = 0U; Index < AUDIO_Handle->class_desc.ASNum; Index++) + { + if(AUDIO_Handle->class_desc.as_desc[Index].GeneralDesc->bTerminalLink == UnitID) + { + AUDIO_Handle->headphone.asociated_as = Index; + break; + } + } + + do + { + value = USBH_AUDIO_FindLinkedUnit(phost, UnitID); + + if (!value) + { + return USBH_FAIL; + } + + Index = (uint8_t)(value & 0xFFU); + Type = (uint8_t)((value >> 8U) & 0xFFU); + UnitID = (uint8_t)((value >> 16U) & 0xFFU); + + switch (Type) + { + case UAC_FEATURE_UNIT: + AUDIO_Handle->headphone.asociated_feature = Index; + break; + + case UAC_MIXER_UNIT: + AUDIO_Handle->headphone.asociated_mixer = Index; + break; + + case UAC_SELECTOR_UNIT: + AUDIO_Handle->headphone.asociated_selector = Index; + break; + + case UAC_OUTPUT_TERMINAL: + AUDIO_Handle->headphone.asociated_terminal = Index; + if(LE16(AUDIO_Handle->class_desc.cs_desc.OutputTerminalDesc[Index]->wTerminalType) != 0x103) + { + return USBH_OK; + } + break; + + default: + ret = USBH_FAIL; + break; + } + } + while ((Type != UAC_OUTPUT_TERMINAL) && (value > 0U)); + + return ret; +} + + +/** + * @brief Handle Set Cur request + * @param phost: Host handle + * @param subtype: subtype index + * @param feature: feature index + * @param controlSelector: control code + * @param channel: channel index + * @param length: Command length + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_AC_SetCur(USBH_HandleTypeDef *phost, + uint8_t subtype, + uint8_t feature, + uint8_t controlSelector, + uint8_t channel, + uint16_t length) +{ + uint16_t wValue,wIndex,wLength; + uint8_t UnitID,InterfaceNum; + AUDIO_HandleTypeDef *AUDIO_Handle; + AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; + USBH_StatusTypeDef ret = USBH_OK; + + switch(subtype) + { + case UAC_INPUT_TERMINAL: + UnitID = AUDIO_Handle->class_desc.cs_desc.InputTerminalDesc[0]->bTerminalID; + InterfaceNum = 0U; /*Always zero Control Interface */ + wIndex = (uint16_t)((uint32_t)UnitID << 8U) | (uint16_t)InterfaceNum; + wValue = (COPY_PROTECT_CONTROL << 8U); + AUDIO_Handle->mem[0] = 0x00U; + + wLength = 1U; + break; + case UAC_FEATURE_UNIT: + UnitID = AUDIO_Handle->class_desc.cs_desc.FeatureUnitDesc[feature]->bUnitID; + InterfaceNum = 0U; /*Always zero Control Interface */ + wIndex = (uint16_t)((uint32_t)UnitID << 8U) | (uint16_t)InterfaceNum ; + /*holds the CS(control selector ) and CN (channel number)*/ + wValue = (uint16_t)((uint32_t)controlSelector << 8U) | (uint16_t)channel; + wLength = length; + break; + + default: + ret = USBH_FAIL; + break; + } + + if (ret != USBH_OK) + { + return ret; + } + + phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_INTERFACE + | USB_REQ_TYPE_CLASS; + + phost->Control.setup.b.bRequest = UAC_SET_CUR; + phost->Control.setup.b.wValue.w = wValue; + phost->Control.setup.b.wIndex.w = wIndex; + phost->Control.setup.b.wLength.w = wLength; + + return(USBH_CtlReq(phost, (uint8_t *)(void *)(AUDIO_Handle->mem), wLength)); +} + +/** + * @brief Handle Get Cur request + * @param phost: Host handle + * @param subtype: subtype index + * @param feature: feature index + * @param controlSelector: control code + * @param channel: channel index + * @param length: Command length + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_AC_GetCur(USBH_HandleTypeDef *phost, + uint8_t subtype, + uint8_t feature, + uint8_t controlSelector, + uint8_t channel, + uint16_t length) +{ + uint16_t wValue = 0U, wIndex = 0U, wLength = 0U; + uint8_t UnitID = 0U, InterfaceNum = 0U; + AUDIO_HandleTypeDef *AUDIO_Handle; + AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; + USBH_StatusTypeDef ret = USBH_OK; + + switch(subtype) + { + case UAC_INPUT_TERMINAL: + UnitID = AUDIO_Handle->class_desc.cs_desc.InputTerminalDesc[0]->bTerminalID; + InterfaceNum = 0U; /*Always zero Control Interface */ + wIndex = (uint16_t)((uint32_t)UnitID << 8U) | (uint16_t)InterfaceNum; + wValue = (COPY_PROTECT_CONTROL << 8U); + AUDIO_Handle->mem[0] = 0x00U; + + wLength = 1U; + break; + case UAC_FEATURE_UNIT: + UnitID = AUDIO_Handle->class_desc.cs_desc.FeatureUnitDesc[feature]->bUnitID; + InterfaceNum = 0U; /*Always zero Control Interface */ + wIndex = (uint16_t)((uint32_t)UnitID << 8U) | (uint16_t)InterfaceNum; + /*holds the CS(control selector ) and CN (channel number)*/ + wValue = (uint16_t)((uint32_t)controlSelector << 8U) | (uint16_t)channel; + wLength = length; + break; + + case UAC_OUTPUT_TERMINAL: + UnitID = AUDIO_Handle->class_desc.cs_desc.OutputTerminalDesc[0]->bTerminalID; + InterfaceNum = 0U; /*Always zero Control Interface */ + wIndex = (uint16_t)((uint32_t)UnitID << 8U) | (uint16_t)InterfaceNum; + wValue = (COPY_PROTECT_CONTROL << 8U); + wLength = 1U; + break; + + default: + ret = USBH_FAIL; + break; + } + + if (ret != USBH_OK) + { + return ret; + } + + phost->Control.setup.b.bmRequestType = USB_D2H | USB_REQ_RECIPIENT_INTERFACE | \ + USB_REQ_TYPE_CLASS; + + phost->Control.setup.b.bRequest = UAC_GET_CUR; + phost->Control.setup.b.wValue.w = wValue; + phost->Control.setup.b.wIndex.w = wIndex; + phost->Control.setup.b.wLength.w = wLength; + + return(USBH_CtlReq(phost, (uint8_t *)(void *)(AUDIO_Handle->mem) , wLength )); +} + + +/** + * @brief Handle Get Max request + * @param phost: Host handle + * @param subtype: subtype index + * @param feature: feature index + * @param controlSelector: control code + * @param channel: channel index + * @param length: Command length + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_AC_GetMax(USBH_HandleTypeDef *phost, + uint8_t subtype, + uint8_t feature, + uint8_t controlSelector, + uint8_t channel, + uint16_t length) +{ + uint16_t wValue = 0U, wIndex = 0U, wLength = 0U; + uint8_t UnitID = 0U, InterfaceNum = 0U; + AUDIO_HandleTypeDef *AUDIO_Handle; + AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; + USBH_StatusTypeDef ret = USBH_OK; + + switch(subtype) + { + case UAC_INPUT_TERMINAL: + UnitID = AUDIO_Handle->class_desc.cs_desc.InputTerminalDesc[0]->bTerminalID; + InterfaceNum = 0U; /*Always zero Control Interface */ + wIndex = (uint16_t)((uint32_t)UnitID << 8U) | (uint16_t)InterfaceNum; + wValue = (COPY_PROTECT_CONTROL << 8U); + AUDIO_Handle->mem[0] = 0x00U; + + wLength = 1U; + break; + case UAC_FEATURE_UNIT: + UnitID = AUDIO_Handle->class_desc.cs_desc.FeatureUnitDesc[feature]->bUnitID; + InterfaceNum = 0U; /*Always zero Control Interface */ + wIndex = (uint16_t)((uint32_t)UnitID << 8U) | (uint16_t)InterfaceNum ; + /*holds the CS(control selector ) and CN (channel number)*/ + wValue = (uint16_t)((uint32_t)controlSelector << 8U) | (uint16_t)channel; + wLength = length; + break; + + case UAC_OUTPUT_TERMINAL: + UnitID = AUDIO_Handle->class_desc.cs_desc.OutputTerminalDesc[0]->bTerminalID; + InterfaceNum = 0U; /*Always zero Control Interface */ + wIndex = (uint16_t)((uint32_t)UnitID << 8U) | (uint16_t)InterfaceNum ; + wValue = (COPY_PROTECT_CONTROL << 8U) ; + wLength = 1U; + break; + + default: + ret = USBH_FAIL; + break; + } + + if (ret != USBH_OK) + { + return ret; + } + + phost->Control.setup.b.bmRequestType = USB_D2H | USB_REQ_RECIPIENT_INTERFACE | \ + USB_REQ_TYPE_CLASS; + + phost->Control.setup.b.bRequest = UAC_GET_MAX; + phost->Control.setup.b.wValue.w = wValue; + phost->Control.setup.b.wIndex.w = wIndex; + phost->Control.setup.b.wLength.w = wLength; + + return(USBH_CtlReq(phost, (uint8_t *)(void *)(AUDIO_Handle->mem), wLength)); + +} + +/** + * @brief Handle Get Res request + * @param phost: Host handle + * @param subtype: subtype index + * @param feature: feature index + * @param controlSelector: control code + * @param channel: channel index + * @param length: Command length + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_AC_GetRes(USBH_HandleTypeDef *phost, + uint8_t subtype, + uint8_t feature, + uint8_t controlSelector, + uint8_t channel, + uint16_t length) +{ + uint16_t wValue = 0U, wIndex = 0U, wLength = 0U; + uint8_t UnitID = 0U, InterfaceNum = 0U; + AUDIO_HandleTypeDef *AUDIO_Handle; + AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; + USBH_StatusTypeDef ret = USBH_OK; + + switch(subtype) + { + case UAC_INPUT_TERMINAL: + UnitID = AUDIO_Handle->class_desc.cs_desc.InputTerminalDesc[0]->bTerminalID; + InterfaceNum = 0U; /*Always zero Control Interface */ + wIndex = (uint16_t)((uint32_t)UnitID << 8U) | (uint16_t)InterfaceNum; + wValue = (COPY_PROTECT_CONTROL << 8U) ; + AUDIO_Handle->mem[0] = 0x00U; + + wLength = 1U; + break; + case UAC_FEATURE_UNIT: + UnitID = AUDIO_Handle->class_desc.cs_desc.FeatureUnitDesc[feature]->bUnitID; + InterfaceNum = 0U; /*Always zero Control Interface */ + wIndex = (uint16_t)((uint32_t)UnitID << 8U) | (uint16_t)InterfaceNum; + /*holds the CS(control selector ) and CN (channel number)*/ + wValue = (uint16_t)((uint32_t)controlSelector << 8U) | (uint16_t)channel; + wLength = length; + break; + + case UAC_OUTPUT_TERMINAL: + UnitID = AUDIO_Handle->class_desc.cs_desc.OutputTerminalDesc[0]->bTerminalID; + InterfaceNum = 0U; /*Always zero Control Interface */ + wIndex = (uint16_t)((uint32_t)UnitID << 8U) | (uint16_t)InterfaceNum; + wValue = (COPY_PROTECT_CONTROL << 8U) ; + wLength = 1U; + break; + + default: + ret = USBH_FAIL; + break; + } + + if (ret != USBH_OK) + { + return ret; + } + + phost->Control.setup.b.bmRequestType = USB_D2H | USB_REQ_RECIPIENT_INTERFACE + | USB_REQ_TYPE_CLASS; + + phost->Control.setup.b.bRequest = UAC_GET_RES; + phost->Control.setup.b.wValue.w = wValue; + phost->Control.setup.b.wIndex.w = wIndex; + phost->Control.setup.b.wLength.w = wLength; + + return(USBH_CtlReq(phost, (uint8_t *)(void *)(AUDIO_Handle->mem), wLength)); + +} + +/** + * @brief Handle Get Min request + * @param phost: Host handle + * @param subtype: subtype index + * @param feature: feature index + * @param controlSelector: control code + * @param channel: channel index + * @param length: Command length + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_AC_GetMin(USBH_HandleTypeDef *phost, + uint8_t subtype, + uint8_t feature, + uint8_t controlSelector, + uint8_t channel, + uint16_t length) +{ + uint16_t wValue = 0U, wIndex = 0U, wLength = 0U; + uint8_t UnitID = 0U, InterfaceNum = 0U; + AUDIO_HandleTypeDef *AUDIO_Handle; + AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; + USBH_StatusTypeDef ret = USBH_OK; + + switch(subtype) + { + case UAC_INPUT_TERMINAL: + UnitID = AUDIO_Handle->class_desc.cs_desc.InputTerminalDesc[0]->bTerminalID; + InterfaceNum = 0U; /*Always zero Control Interface */ + wIndex = (uint16_t)((uint32_t)UnitID << 8U) | (uint16_t)InterfaceNum; + wValue = (COPY_PROTECT_CONTROL << 8U); + AUDIO_Handle->mem[0] = 0x00U; + + wLength = 1U; + break; + case UAC_FEATURE_UNIT: + UnitID = AUDIO_Handle->class_desc.cs_desc.FeatureUnitDesc[feature]->bUnitID; + InterfaceNum = 0U; /*Always zero Control Interface */ + wIndex = (uint16_t)((uint32_t)UnitID << 8U) | (uint16_t)InterfaceNum; + /*holds the CS(control selector ) and CN (channel number)*/ + wValue = (uint16_t)((uint32_t)controlSelector << 8U) | (uint16_t)channel; + wLength = length; + break; + + case UAC_OUTPUT_TERMINAL: + UnitID = AUDIO_Handle->class_desc.cs_desc.OutputTerminalDesc[0]->bTerminalID; + InterfaceNum = 0U; /*Always zero Control Interface */ + wIndex = (uint16_t)((uint32_t)UnitID << 8U) | (uint16_t)InterfaceNum; + wValue = (COPY_PROTECT_CONTROL << 8U); + wLength = 1U; + break; + + default: + ret = USBH_FAIL; + break; + } + + if (ret != USBH_OK) + { + return ret; + } + + phost->Control.setup.b.bmRequestType = USB_D2H | USB_REQ_RECIPIENT_INTERFACE | \ + USB_REQ_TYPE_CLASS; + + phost->Control.setup.b.bRequest = UAC_GET_MIN; + phost->Control.setup.b.wValue.w = wValue; + phost->Control.setup.b.wIndex.w = wIndex; + phost->Control.setup.b.wLength.w = wLength; + + return(USBH_CtlReq(phost, (uint8_t *)(void *)(AUDIO_Handle->mem), wLength)); + +} + +/** + * @brief Handle Set Endpoint Controls Request + * @param phost: Host handle + * @param Ep: Endpoint address + * @param buf: pointer to data + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_AUDIO_SetEndpointControls(USBH_HandleTypeDef *phost, + uint8_t Ep, + uint8_t *buff) +{ + uint16_t wValue, wIndex, wLength; + + wValue = SAMPLING_FREQ_CONTROL << 8U; + wIndex = Ep; + wLength = 3U; /*length of the frequency parameter*/ + + phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_ENDPOINT | \ + USB_REQ_TYPE_CLASS; + + phost->Control.setup.b.bRequest = UAC_SET_CUR; + phost->Control.setup.b.wValue.w = wValue; + phost->Control.setup.b.wIndex.w = wIndex; + phost->Control.setup.b.wLength.w = wLength; + + return(USBH_CtlReq(phost, (uint8_t *)buff, wLength)); + +} + +/** + * @brief Handle Input stream process + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_AUDIO_InputStream (USBH_HandleTypeDef *phost) +{ + USBH_StatusTypeDef status = USBH_BUSY; + + return status; +} + +/** + * @brief Handle HID Control process + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_AUDIO_Control (USBH_HandleTypeDef *phost) +{ + USBH_StatusTypeDef status = USBH_BUSY ; + AUDIO_HandleTypeDef *AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; + uint16_t attribute = 0U; + + switch(AUDIO_Handle->control_state) + { + case AUDIO_CONTROL_INIT: + if((phost->Timer & 1U) == 0U) + { + AUDIO_Handle->control.timer = phost->Timer; + USBH_InterruptReceiveData(phost, + (uint8_t *)(void *)(AUDIO_Handle->mem), + (uint8_t)AUDIO_Handle->control.EpSize, + AUDIO_Handle->control.Pipe); + + AUDIO_Handle->temp_feature = AUDIO_Handle->headphone.asociated_feature; + AUDIO_Handle->temp_channels = AUDIO_Handle->headphone.asociated_channels; + + AUDIO_Handle->control_state = AUDIO_CONTROL_CHANGE ; + } + break; + + case AUDIO_CONTROL_CHANGE: + if(USBH_LL_GetURBState(phost , AUDIO_Handle->control.Pipe) == USBH_URB_DONE) + { + attribute = LE16(&AUDIO_Handle->mem[0]); + if(USBH_AUDIO_SetControlAttribute (phost, (uint8_t)attribute) == USBH_BUSY) + { + break; + } + } + + if(( phost->Timer - AUDIO_Handle->control.timer) >= AUDIO_Handle->control.Poll) + { + AUDIO_Handle->control.timer = phost->Timer; + + USBH_InterruptReceiveData(phost, + (uint8_t *)(void *)(AUDIO_Handle->mem), + (uint8_t)AUDIO_Handle->control.EpSize, + AUDIO_Handle->control.Pipe); + + } + break; + + case AUDIO_CONTROL_VOLUME_UP: + if( USBH_AUDIO_SetControlAttribute (phost, 1U) == USBH_OK) + { + AUDIO_Handle->control_state = AUDIO_CONTROL_INIT; + status = USBH_OK; + } + break; + + case AUDIO_CONTROL_VOLUME_DOWN: + if( USBH_AUDIO_SetControlAttribute (phost, 2U) == USBH_OK) + { + AUDIO_Handle->control_state = AUDIO_CONTROL_INIT; + status = USBH_OK; + } + break; + + case AUDIO_CONTROL_IDLE: + default: + break; + } + + return status; +} + +/** + * @brief Handle Output stream process + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_AUDIO_OutputStream (USBH_HandleTypeDef *phost) +{ + USBH_StatusTypeDef status = USBH_BUSY ; + AUDIO_HandleTypeDef *AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; + uint8_t *buff; + + + switch(AUDIO_Handle->play_state) + { + case AUDIO_PLAYBACK_INIT: + + if( AUDIO_Handle->class_desc.as_desc[AUDIO_Handle->headphone.asociated_as].FormatTypeDesc->bSamFreqType == 0U) + { + AUDIO_Handle->play_state = AUDIO_PLAYBACK_SET_EP_FREQ; + } + else + { + AUDIO_Handle->play_state = AUDIO_PLAYBACK_SET_EP; + } + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + case AUDIO_PLAYBACK_SET_EP_FREQ: + + buff = (uint8_t*)AUDIO_Handle->class_desc.as_desc[AUDIO_Handle->headphone.asociated_as].FormatTypeDesc->tSamFreq[0]; + + status = USBH_AUDIO_SetEndpointControls(phost, AUDIO_Handle->headphone.Ep, buff); + if(status == USBH_OK) + { + AUDIO_Handle->play_state = AUDIO_PLAYBACK_IDLE; + } + break; + + case AUDIO_PLAYBACK_SET_EP: + buff = (uint8_t *)(void *)&AUDIO_Handle->headphone.frequency; + status = USBH_AUDIO_SetEndpointControls(phost,AUDIO_Handle->headphone.Ep, buff); + if(status == USBH_OK) + { + AUDIO_Handle->play_state = AUDIO_PLAYBACK_IDLE; + USBH_AUDIO_FrequencySet(phost); + } + break; + + case AUDIO_PLAYBACK_IDLE: + status = USBH_OK; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CLASS_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + case AUDIO_PLAYBACK_PLAY: + USBH_AUDIO_Transmit(phost); + status = USBH_OK; + break; + + default: + break; + } + + return status; +} + +/** + * @brief Handle Transmission process + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_AUDIO_Transmit (USBH_HandleTypeDef *phost) +{ + USBH_StatusTypeDef status = USBH_BUSY ; + AUDIO_HandleTypeDef *AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; + + switch(AUDIO_Handle->processing_state) + { + case AUDIO_DATA_START_OUT: + /* Sync with start of Even Frame */ + if((phost->Timer & 1U) == 0U) + { + AUDIO_Handle->headphone.timer = phost->Timer; + AUDIO_Handle->processing_state = AUDIO_DATA_OUT; + USBH_IsocSendData(phost, + AUDIO_Handle->headphone.buf, + (uint32_t)AUDIO_Handle->headphone.frame_length, + AUDIO_Handle->headphone.Pipe); + + AUDIO_Handle->headphone.partial_ptr = AUDIO_Handle->headphone.frame_length; + AUDIO_Handle->headphone.global_ptr = AUDIO_Handle->headphone.frame_length; + AUDIO_Handle->headphone.cbuf = AUDIO_Handle->headphone.buf; + } + else + { + +#if (USBH_USE_OS == 1U) + osDelay(1); + phost->os_msg = (uint32_t)USBH_CLASS_EVENT; + +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + break; + + case AUDIO_DATA_OUT: + if((USBH_LL_GetURBState(phost , AUDIO_Handle->headphone.Pipe) == USBH_URB_DONE)&& + (( phost->Timer - AUDIO_Handle->headphone.timer) >= AUDIO_Handle->headphone.Poll)) + { + AUDIO_Handle->headphone.timer = phost->Timer; + + if(AUDIO_Handle->control.supported == 1U) + { + USBH_AUDIO_Control (phost); + } + + if(AUDIO_Handle->headphone.global_ptr <= AUDIO_Handle->headphone.total_length) + { + USBH_IsocSendData(phost, + AUDIO_Handle->headphone.cbuf, + (uint32_t)AUDIO_Handle->headphone.frame_length, + AUDIO_Handle->headphone.Pipe); + + AUDIO_Handle->headphone.cbuf += AUDIO_Handle->headphone.frame_length; + AUDIO_Handle->headphone.partial_ptr += AUDIO_Handle->headphone.frame_length; + AUDIO_Handle->headphone.global_ptr += AUDIO_Handle->headphone.frame_length; + } + else + { + AUDIO_Handle->headphone.partial_ptr = 0xFFFFFFFFU; + AUDIO_Handle->play_state = AUDIO_PLAYBACK_IDLE; + USBH_AUDIO_BufferEmptyCallback(phost); + } + } + break; + + default: + status = USBH_FAIL; + break; + } + return status; +} + +/** + * @brief USBH_AUDIO_SetFrequency + * Set Audio sampling parameters + * @param phost: Host handle + * @param SampleRate: Sample Rate + * @param NbrChannels: Number of Channels + * @param BitPerSample: Bit Per Sample + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_AUDIO_SetFrequency (USBH_HandleTypeDef *phost, + uint16_t SampleRate, + uint8_t NbrChannels, + uint8_t BitPerSample) +{ + USBH_StatusTypeDef Status = USBH_BUSY; + AUDIO_HandleTypeDef *AUDIO_Handle; + uint8_t index; + uint8_t change_freq = FALSE; + uint32_t freq_min, freq_max; + uint8_t num_supported_freq; + + if(phost->gState == HOST_CLASS) + { + AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; + if(AUDIO_Handle->play_state == AUDIO_PLAYBACK_IDLE) + { + + if(AUDIO_Handle->class_desc.as_desc[AUDIO_Handle->headphone.asociated_as].FormatTypeDesc->bSamFreqType == 0U) + { + freq_min = LE24(AUDIO_Handle->class_desc.as_desc[AUDIO_Handle->headphone.asociated_as].FormatTypeDesc->tSamFreq[0]); + freq_max = LE24(AUDIO_Handle->class_desc.as_desc[AUDIO_Handle->headphone.asociated_as].FormatTypeDesc->tSamFreq[1]); + + if(( SampleRate >= freq_min)&& (SampleRate <= freq_max)) + { + change_freq = TRUE; + } + } + else + { + + num_supported_freq = (AUDIO_Handle->class_desc.as_desc[AUDIO_Handle->headphone.asociated_as].FormatTypeDesc->bLength - 8U) / 3U; + + + for(index = 0U; index < num_supported_freq; index++) + { + if(SampleRate == LE24(AUDIO_Handle->class_desc.as_desc[AUDIO_Handle->headphone.asociated_as].FormatTypeDesc->tSamFreq[index])) + { + change_freq = TRUE; + break; + } + } + } + + if(change_freq == TRUE) + { + AUDIO_Handle->headphone.frequency = SampleRate; + AUDIO_Handle->headphone.frame_length = (SampleRate * BitPerSample * NbrChannels) / 8000U; + AUDIO_Handle->play_state = AUDIO_PLAYBACK_SET_EP; + Status = USBH_OK; + + } + } + } + return Status; +} + +/** + * @brief USBH_AUDIO_Play + * Start playback process + * @param phost: Host handle + * @param buf: pointer to raw audio data + * @param length: total length of the audio data + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_AUDIO_Play (USBH_HandleTypeDef *phost, uint8_t *buf, uint32_t length) +{ + USBH_StatusTypeDef Status = USBH_FAIL; + AUDIO_HandleTypeDef *AUDIO_Handle; + + if(phost->gState == HOST_CLASS) + { + AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; + if(AUDIO_Handle->play_state == AUDIO_PLAYBACK_IDLE) + { + AUDIO_Handle->headphone.buf = buf; + AUDIO_Handle->headphone.total_length = length; + AUDIO_Handle->play_state = AUDIO_PLAYBACK_PLAY; + AUDIO_Handle->control_state = AUDIO_CONTROL_INIT; + AUDIO_Handle->processing_state = AUDIO_DATA_START_OUT; + Status = USBH_OK; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CLASS_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + } + return Status; +} + +/** + * @brief USBH_AUDIO_Pause + * Stop the playback process + * @param phost: Host handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_AUDIO_Stop (USBH_HandleTypeDef *phost) +{ + USBH_StatusTypeDef Status = USBH_FAIL; + Status = USBH_AUDIO_Suspend(phost); + return Status; +} + +/** + * @brief USBH_AUDIO_Suspend + * Suspend the playback process + * @param phost: Host handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_AUDIO_Suspend (USBH_HandleTypeDef *phost) +{ + USBH_StatusTypeDef Status = USBH_FAIL; + AUDIO_HandleTypeDef *AUDIO_Handle; + + if(phost->gState == HOST_CLASS) + { + AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; + if(AUDIO_Handle->play_state == AUDIO_PLAYBACK_PLAY) + { + AUDIO_Handle->control_state = AUDIO_CONTROL_IDLE; + AUDIO_Handle->play_state = AUDIO_PLAYBACK_IDLE; + Status = USBH_OK; + } + } + return Status; +} +/** + * @brief USBH_AUDIO_Resume + * Resume the playback process + * @param phost: Host handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_AUDIO_Resume (USBH_HandleTypeDef *phost) +{ + USBH_StatusTypeDef Status = USBH_FAIL; + AUDIO_HandleTypeDef *AUDIO_Handle; + + if(phost->gState == HOST_CLASS) + { + AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; + if(AUDIO_Handle->play_state == AUDIO_PLAYBACK_IDLE) + { + AUDIO_Handle->control_state = AUDIO_CONTROL_INIT; + AUDIO_Handle->play_state = AUDIO_PLAYBACK_PLAY; + } + } + return Status; +} +/** + * @brief USBH_AUDIO_GetOutOffset + * return the current buffer pointer for OUT process + * @param phost: Host handle + * @retval USBH Status + */ +int32_t USBH_AUDIO_GetOutOffset (USBH_HandleTypeDef *phost) +{ + AUDIO_HandleTypeDef *AUDIO_Handle; + + if(phost->gState == HOST_CLASS) + { + AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; + if(AUDIO_Handle->play_state == AUDIO_PLAYBACK_PLAY) + { + return (int32_t)AUDIO_Handle->headphone.partial_ptr; + } + } + return -1; +} + +/** + * @brief USBH_AUDIO_ChangeOutBuffer + * Change audio data buffer address + * @param phost: Host handle + * @param buf: buffer address + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_AUDIO_ChangeOutBuffer (USBH_HandleTypeDef *phost, uint8_t *buf) +{ + USBH_StatusTypeDef Status = USBH_FAIL; + AUDIO_HandleTypeDef *AUDIO_Handle; + + if(phost->gState == HOST_CLASS) + { + AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; + if(AUDIO_Handle->play_state == AUDIO_PLAYBACK_PLAY) + { + if(AUDIO_Handle->headphone.buf <= buf) + { + AUDIO_Handle->headphone.cbuf = buf; + if ( AUDIO_Handle->headphone.buf == buf) + { + AUDIO_Handle->headphone.partial_ptr = 0U; + } + Status = USBH_OK; + } + } + } + return Status; +} + +/** + * @brief USBH_AUDIO_SetControlAttribute + * Set Control Attribute + * @param phost: Host handle + * @param attrib: control attribute + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_AUDIO_SetControlAttribute (USBH_HandleTypeDef *phost, uint8_t attrib) +{ + USBH_StatusTypeDef status = USBH_BUSY; + AUDIO_HandleTypeDef *AUDIO_Handle; + + + AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; + + switch (attrib) + { + case 0x01: + AUDIO_Handle->headphone.attribute.volume += AUDIO_Handle->headphone.attribute.resolution; + break; + + case 0x02: + AUDIO_Handle->headphone.attribute.volume -= AUDIO_Handle->headphone.attribute.resolution; + break; + + default : + status = USBH_FAIL; + break; + } + + if(AUDIO_Handle->headphone.attribute.volume > AUDIO_Handle->headphone.attribute.volumeMax) + { + AUDIO_Handle->headphone.attribute.volume =AUDIO_Handle->headphone.attribute.volumeMax; + } + + if(AUDIO_Handle->headphone.attribute.volume < AUDIO_Handle->headphone.attribute.volumeMin) + { + AUDIO_Handle->headphone.attribute.volume =AUDIO_Handle->headphone.attribute.volumeMin; + } + + if(AUDIO_SetVolume (phost, + AUDIO_Handle->temp_feature, + (uint8_t)AUDIO_Handle->temp_channels, + (uint16_t)AUDIO_Handle->headphone.attribute.volume) != USBH_BUSY) + { + + if(AUDIO_Handle->temp_channels == 1U) + { + AUDIO_Handle->temp_feature = AUDIO_Handle->headphone.asociated_feature; + AUDIO_Handle->temp_channels = AUDIO_Handle->headphone.asociated_channels; + status = USBH_OK; + } + else + { + AUDIO_Handle->temp_channels--; + } + AUDIO_Handle->cs_req_state = AUDIO_REQ_GET_VOLUME; + } + + + return status; +} + + +/** + * @brief USBH_AUDIO_SetVolume + * Set Volume + * @param phost: Host handle + * @param volume: VOLUME_UP/ VOLUME_DOWN + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_AUDIO_SetVolume (USBH_HandleTypeDef *phost, AUDIO_VolumeCtrlTypeDef volume_ctl) +{ + AUDIO_HandleTypeDef *AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; + + if((volume_ctl == VOLUME_UP) || (volume_ctl == VOLUME_DOWN)) + { + if(phost->gState == HOST_CLASS) + { + AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; + if(AUDIO_Handle->play_state == AUDIO_PLAYBACK_PLAY) + { + AUDIO_Handle->control_state = (volume_ctl == VOLUME_UP)? AUDIO_CONTROL_VOLUME_UP : AUDIO_CONTROL_VOLUME_DOWN; + return USBH_OK; + } + } + } + + return USBH_FAIL; +} +/** + * @brief AUDIO_SetVolume + * Set Volume + * @param phost: Host handle + * @param feature: feature Unit index + * @param channel: channel index + * @param volume: new volume + * @retval USBH Status + */ +static USBH_StatusTypeDef AUDIO_SetVolume (USBH_HandleTypeDef *phost, uint8_t feature, uint8_t channel, uint16_t volume) +{ + USBH_StatusTypeDef status = USBH_BUSY ; + AUDIO_HandleTypeDef *AUDIO_Handle; + + + AUDIO_Handle = (AUDIO_HandleTypeDef*) phost->pActiveClass->pData; + + AUDIO_Handle->mem[0] = volume; + + status = USBH_AC_SetCur(phost, UAC_FEATURE_UNIT, feature, + VOLUME_CONTROL, channel, 2U); + + return status; +} + +/** + * @brief The function informs user that Settings have been changed + * @param phost: Selected device + * @retval None + */ +__weak void USBH_AUDIO_FrequencySet(USBH_HandleTypeDef *phost) +{ + +} + +/** + * @brief The function informs user that User data are processed + * @param phost: Selected device + * @retval None + */ +__weak void USBH_AUDIO_BufferEmptyCallback(USBH_HandleTypeDef *phost) +{ + +} +/** +* @} +*/ + +/** +* @} +*/ + +/** +* @} +*/ + + +/** +* @} +*/ + + +/** +* @} +*/ + +/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Class/CDC/Inc/usbh_cdc.h b/system/Middlewares/ST/STM32_USB_Host_Library/Class/CDC/Inc/usbh_cdc.h index 75b53614b0..882f2a1488 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Class/CDC/Inc/usbh_cdc.h +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Class/CDC/Inc/usbh_cdc.h @@ -1,448 +1,448 @@ -/** - ****************************************************************************** - * @file usbh_cdc.h - * @author MCD Application Team - * @brief This file contains all the prototypes for the usbh_cdc.c - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive ----------------------------------------------*/ -#ifndef __USBH_CDC_H -#define __USBH_CDC_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_core.h" - - -/** @addtogroup USBH_LIB -* @{ -*/ - -/** @addtogroup USBH_CLASS -* @{ -*/ - -/** @addtogroup USBH_CDC_CLASS -* @{ -*/ - -/** @defgroup USBH_CDC_CORE -* @brief This file is the Header file for usbh_core.c -* @{ -*/ - - - - -/*Communication Class codes*/ -#define USB_CDC_CLASS 0x02U -#define COMMUNICATION_INTERFACE_CLASS_CODE 0x02U - -/*Data Interface Class Codes*/ -#define DATA_INTERFACE_CLASS_CODE 0x0AU - -/*Communication sub class codes*/ -#define RESERVED 0x00U -#define DIRECT_LINE_CONTROL_MODEL 0x01U -#define ABSTRACT_CONTROL_MODEL 0x02U -#define TELEPHONE_CONTROL_MODEL 0x03U -#define MULTICHANNEL_CONTROL_MODEL 0x04U -#define CAPI_CONTROL_MODEL 0x05U -#define ETHERNET_NETWORKING_CONTROL_MODEL 0x06U -#define ATM_NETWORKING_CONTROL_MODEL 0x07U - - -/*Communication Interface Class Control Protocol Codes*/ -#define NO_CLASS_SPECIFIC_PROTOCOL_CODE 0x00U -#define COMMON_AT_COMMAND 0x01U -#define VENDOR_SPECIFIC 0xFFU - - -#define CS_INTERFACE 0x24U -#define CDC_PAGE_SIZE_64 0x40U - -/*Class-Specific Request Codes*/ -#define CDC_SEND_ENCAPSULATED_COMMAND 0x00U -#define CDC_GET_ENCAPSULATED_RESPONSE 0x01U -#define CDC_SET_COMM_FEATURE 0x02U -#define CDC_GET_COMM_FEATURE 0x03U -#define CDC_CLEAR_COMM_FEATURE 0x04U - -#define CDC_SET_AUX_LINE_STATE 0x10U -#define CDC_SET_HOOK_STATE 0x11U -#define CDC_PULSE_SETUP 0x12U -#define CDC_SEND_PULSE 0x13U -#define CDC_SET_PULSE_TIME 0x14U -#define CDC_RING_AUX_JACK 0x15U - -#define CDC_SET_LINE_CODING 0x20U -#define CDC_GET_LINE_CODING 0x21U -#define CDC_SET_CONTROL_LINE_STATE 0x22U -#define CDC_SEND_BREAK 0x23U - -#define CDC_SET_RINGER_PARMS 0x30U -#define CDC_GET_RINGER_PARMS 0x31U -#define CDC_SET_OPERATION_PARMS 0x32U -#define CDC_GET_OPERATION_PARMS 0x33U -#define CDC_SET_LINE_PARMS 0x34U -#define CDC_GET_LINE_PARMS 0x35U -#define CDC_DIAL_DIGITS 0x36U -#define CDC_SET_UNIT_PARAMETER 0x37U -#define CDC_GET_UNIT_PARAMETER 0x38U -#define CDC_CLEAR_UNIT_PARAMETER 0x39U -#define CDC_GET_PROFILE 0x3AU - -#define CDC_SET_ETHERNET_MULTICAST_FILTERS 0x40U -#define CDC_SET_ETHERNET_POWER_MANAGEMENT_PATTERN FILTER 0x41U -#define CDC_GET_ETHERNET_POWER_MANAGEMENT_PATTERN FILTER 0x42U -#define CDC_SET_ETHERNET_PACKET_FILTER 0x43U -#define CDC_GET_ETHERNET_STATISTIC 0x44U - -#define CDC_SET_ATM_DATA_FORMAT 0x50U -#define CDC_GET_ATM_DEVICE_STATISTICS 0x51U -#define CDC_SET_ATM_DEFAULT_VC 0x52U -#define CDC_GET_ATM_VC_STATISTICS 0x53U - - -/* wValue for SetControlLineState*/ -#define CDC_ACTIVATE_CARRIER_SIGNAL_RTS 0x0002U -#define CDC_DEACTIVATE_CARRIER_SIGNAL_RTS 0x0000U -#define CDC_ACTIVATE_SIGNAL_DTR 0x0001U -#define CDC_DEACTIVATE_SIGNAL_DTR 0x0000U - -#define LINE_CODING_STRUCTURE_SIZE 0x07U -/** - * @} - */ - -/** @defgroup USBH_CDC_CORE_Exported_Types -* @{ -*/ - -/* States for CDC State Machine */ -typedef enum -{ - CDC_IDLE = 0U, - CDC_SEND_DATA, - CDC_SEND_DATA_WAIT, - CDC_RECEIVE_DATA, - CDC_RECEIVE_DATA_WAIT, -} -CDC_DataStateTypeDef; - -typedef enum -{ - CDC_IDLE_STATE = 0U, - CDC_SET_LINE_CODING_STATE, - CDC_GET_LAST_LINE_CODING_STATE, - CDC_TRANSFER_DATA, - CDC_ERROR_STATE, -} -CDC_StateTypeDef; - - -/*Line coding structure*/ -typedef union _CDC_LineCodingStructure -{ - uint8_t Array[LINE_CODING_STRUCTURE_SIZE]; - - struct - { - - uint32_t dwDTERate; /*Data terminal rate, in bits per second*/ - uint8_t bCharFormat; /*Stop bits - 0 - 1 Stop bit - 1 - 1.5 Stop bits - 2 - 2 Stop bits*/ - uint8_t bParityType; /* Parity - 0 - None - 1 - Odd - 2 - Even - 3 - Mark - 4 - Space*/ - uint8_t bDataBits; /* Data bits (5, 6, 7, 8 or 16). */ - }b; -} -CDC_LineCodingTypeDef; - - - -/* Header Functional Descriptor --------------------------------------------------------------------------------- -Offset| field | Size | Value | Description -------|---------------------|-------|------------|------------------------------ -0 | bFunctionLength | 1 | number | Size of this descriptor. -1 | bDescriptorType | 1 | Constant | CS_INTERFACE (0x24) -2 | bDescriptorSubtype | 1 | Constant | Identifier (ID) of functional - | | | | descriptor. -3 | bcdCDC | 2 | | - | | | Number | USB Class Definitions for - | | | | Communication Devices Specification - | | | | release number in binary-coded - | | | | decimal -------|---------------------|-------|------------|------------------------------ -*/ -typedef struct _FunctionalDescriptorHeader -{ - uint8_t bLength; /*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_TypeDef; -/* Call Management Functional Descriptor --------------------------------------------------------------------------------- -Offset| field | Size | Value | Description -------|---------------------|-------|------------|------------------------------ -0 | bFunctionLength | 1 | number | Size of this descriptor. -1 | bDescriptorType | 1 | Constant | CS_INTERFACE (0x24) -2 | bDescriptorSubtype | 1 | Constant | Call Management functional - | | | | descriptor subtype. -3 | bmCapabilities | 1 | Bitmap | The capabilities that this configuration - | | | | supports: - | | | | D7..D2: RESERVED (Reset to zero) - | | | | D1: 0 - Device sends/receives call - | | | | management information only over - | | | | the Communication Class - | | | | interface. - | | | | 1 - Device can send/receive call - | \ | | management information over a - | | | | Data Class interface. - | | | | D0: 0 - Device does not handle call - | | | | management itself. - | | | | 1 - Device handles call - | | | | management itself. - | | | | The previous bits, in combination, identify - | | | | which call management scenario is used. If bit - | | | | D0 is reset to 0, then the value of bit D1 is - | | | | ignored. In this case, bit D1 is reset to zero for - | | | | future compatibility. -4 | bDataInterface | 1 | Number | Interface number of Data Class interface - | | | | optionally used for call management. -------|---------------------|-------|------------|------------------------------ -*/ -typedef struct _CallMgmtFunctionalDescriptor -{ - 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_TypeDef; -/* Abstract Control Management Functional Descriptor --------------------------------------------------------------------------------- -Offset| field | Size | Value | Description -------|---------------------|-------|------------|------------------------------ -0 | bFunctionLength | 1 | number | Size of functional descriptor, in bytes. -1 | bDescriptorType | 1 | Constant | CS_INTERFACE (0x24) -2 | bDescriptorSubtype | 1 | Constant | Abstract Control Management - | | | | functional descriptor subtype. -3 | bmCapabilities | 1 | Bitmap | The capabilities that this configuration - | | | | supports ((A bit value of zero means that the - | | | | request is not supported.) ) - D7..D4: RESERVED (Reset to zero) - | | | | D3: 1 - Device supports the notification - | | | | Network_Connection. - | | | | D2: 1 - Device supports the request - | | | | Send_Break - | | | | D1: 1 - Device supports the request - | \ | | combination of Set_Line_Coding, - | | | | Set_Control_Line_State, Get_Line_Coding, and the - notification Serial_State. - | | | | D0: 1 - Device supports the request - | | | | combination of Set_Comm_Feature, - | | | | Clear_Comm_Feature, and Get_Comm_Feature. - | | | | The previous bits, in combination, identify - | | | | which requests/notifications are supported by - | | | | a Communication Class interface with the - | | | | SubClass code of Abstract Control Model. -------|---------------------|-------|------------|------------------------------ -*/ -typedef struct _AbstractCntrlMgmtFunctionalDescriptor -{ - 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_TypeDef; -/* Union Functional Descriptor --------------------------------------------------------------------------------- -Offset| field | Size | Value | Description -------|---------------------|-------|------------|------------------------------ -0 | bFunctionLength | 1 | number | Size of this descriptor. -1 | bDescriptorType | 1 | Constant | CS_INTERFACE (0x24) -2 | bDescriptorSubtype | 1 | Constant | Union functional - | | | | descriptor subtype. -3 | bMasterInterface | 1 | Constant | The interface number of the - | | | | Communication or Data Class interface -4 | bSlaveInterface0 | 1 | Number | nterface number of first slave or associated - | | | | interface in the union. -------|---------------------|-------|------------|------------------------------ -*/ -typedef struct _UnionFunctionalDescriptor -{ - 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_TypeDef; - -typedef struct _USBH_CDCInterfaceDesc -{ - CDC_HeaderFuncDesc_TypeDef CDC_HeaderFuncDesc; - CDC_CallMgmtFuncDesc_TypeDef CDC_CallMgmtFuncDesc; - CDC_AbstCntrlMgmtFuncDesc_TypeDef CDC_AbstCntrlMgmtFuncDesc; - CDC_UnionFuncDesc_TypeDef CDC_UnionFuncDesc; -} -CDC_InterfaceDesc_Typedef; - - -/* Structure for CDC process */ -typedef struct -{ - uint8_t NotifPipe; - uint8_t NotifEp; - uint8_t buff[8]; - uint16_t NotifEpSize; -} -CDC_CommItfTypedef ; - -typedef struct -{ - uint8_t InPipe; - uint8_t OutPipe; - uint8_t OutEp; - uint8_t InEp; - uint8_t buff[8]; - uint16_t OutEpSize; - uint16_t InEpSize; -} -CDC_DataItfTypedef ; - -/* Structure for CDC process */ -typedef struct _CDC_Process -{ - CDC_CommItfTypedef CommItf; - CDC_DataItfTypedef DataItf; - uint8_t *pTxData; - uint8_t *pRxData; - uint32_t TxDataLength; - uint32_t RxDataLength; - CDC_InterfaceDesc_Typedef CDC_Desc; - CDC_LineCodingTypeDef LineCoding; - CDC_LineCodingTypeDef *pUserLineCoding; - CDC_StateTypeDef state; - CDC_DataStateTypeDef data_tx_state; - CDC_DataStateTypeDef data_rx_state; - uint8_t Rx_Poll; -} -CDC_HandleTypeDef; - -/** -* @} -*/ - -/** @defgroup USBH_CDC_CORE_Exported_Defines -* @{ -*/ - -/** -* @} -*/ - -/** @defgroup USBH_CDC_CORE_Exported_Macros -* @{ -*/ -/** -* @} -*/ - -/** @defgroup USBH_CDC_CORE_Exported_Variables -* @{ -*/ -extern USBH_ClassTypeDef CDC_Class; -#define USBH_CDC_CLASS &CDC_Class - -/** -* @} -*/ - -/** @defgroup USBH_CDC_CORE_Exported_FunctionsPrototype -* @{ -*/ - -USBH_StatusTypeDef USBH_CDC_SetLineCoding(USBH_HandleTypeDef *phost, - CDC_LineCodingTypeDef *linecoding); - -USBH_StatusTypeDef USBH_CDC_GetLineCoding(USBH_HandleTypeDef *phost, - CDC_LineCodingTypeDef *linecoding); - -USBH_StatusTypeDef USBH_CDC_Transmit(USBH_HandleTypeDef *phost, - uint8_t *pbuff, - uint32_t length); - -USBH_StatusTypeDef USBH_CDC_Receive(USBH_HandleTypeDef *phost, - uint8_t *pbuff, - uint32_t length); - - -uint16_t USBH_CDC_GetLastReceivedDataSize(USBH_HandleTypeDef *phost); - -USBH_StatusTypeDef USBH_CDC_Stop(USBH_HandleTypeDef *phost); - -void USBH_CDC_LineCodingChanged(USBH_HandleTypeDef *phost); - -void USBH_CDC_TransmitCallback(USBH_HandleTypeDef *phost); - -void USBH_CDC_ReceiveCallback(USBH_HandleTypeDef *phost); - -/** -* @} -*/ - -#ifdef __cplusplus -} -#endif - -#endif /* __USBH_CDC_H */ - -/** -* @} -*/ - -/** -* @} -*/ - -/** -* @} -*/ - -/** -* @} -*/ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - +/** + ****************************************************************************** + * @file usbh_cdc.h + * @author MCD Application Team + * @brief This file contains all the prototypes for the usbh_cdc.c + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive ----------------------------------------------*/ +#ifndef __USBH_CDC_H +#define __USBH_CDC_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_core.h" + + +/** @addtogroup USBH_LIB +* @{ +*/ + +/** @addtogroup USBH_CLASS +* @{ +*/ + +/** @addtogroup USBH_CDC_CLASS +* @{ +*/ + +/** @defgroup USBH_CDC_CORE +* @brief This file is the Header file for usbh_core.c +* @{ +*/ + + + + +/*Communication Class codes*/ +#define USB_CDC_CLASS 0x02U +#define COMMUNICATION_INTERFACE_CLASS_CODE 0x02U + +/*Data Interface Class Codes*/ +#define DATA_INTERFACE_CLASS_CODE 0x0AU + +/*Communication sub class codes*/ +#define RESERVED 0x00U +#define DIRECT_LINE_CONTROL_MODEL 0x01U +#define ABSTRACT_CONTROL_MODEL 0x02U +#define TELEPHONE_CONTROL_MODEL 0x03U +#define MULTICHANNEL_CONTROL_MODEL 0x04U +#define CAPI_CONTROL_MODEL 0x05U +#define ETHERNET_NETWORKING_CONTROL_MODEL 0x06U +#define ATM_NETWORKING_CONTROL_MODEL 0x07U + + +/*Communication Interface Class Control Protocol Codes*/ +#define NO_CLASS_SPECIFIC_PROTOCOL_CODE 0x00U +#define COMMON_AT_COMMAND 0x01U +#define VENDOR_SPECIFIC 0xFFU + + +#define CS_INTERFACE 0x24U +#define CDC_PAGE_SIZE_64 0x40U + +/*Class-Specific Request Codes*/ +#define CDC_SEND_ENCAPSULATED_COMMAND 0x00U +#define CDC_GET_ENCAPSULATED_RESPONSE 0x01U +#define CDC_SET_COMM_FEATURE 0x02U +#define CDC_GET_COMM_FEATURE 0x03U +#define CDC_CLEAR_COMM_FEATURE 0x04U + +#define CDC_SET_AUX_LINE_STATE 0x10U +#define CDC_SET_HOOK_STATE 0x11U +#define CDC_PULSE_SETUP 0x12U +#define CDC_SEND_PULSE 0x13U +#define CDC_SET_PULSE_TIME 0x14U +#define CDC_RING_AUX_JACK 0x15U + +#define CDC_SET_LINE_CODING 0x20U +#define CDC_GET_LINE_CODING 0x21U +#define CDC_SET_CONTROL_LINE_STATE 0x22U +#define CDC_SEND_BREAK 0x23U + +#define CDC_SET_RINGER_PARMS 0x30U +#define CDC_GET_RINGER_PARMS 0x31U +#define CDC_SET_OPERATION_PARMS 0x32U +#define CDC_GET_OPERATION_PARMS 0x33U +#define CDC_SET_LINE_PARMS 0x34U +#define CDC_GET_LINE_PARMS 0x35U +#define CDC_DIAL_DIGITS 0x36U +#define CDC_SET_UNIT_PARAMETER 0x37U +#define CDC_GET_UNIT_PARAMETER 0x38U +#define CDC_CLEAR_UNIT_PARAMETER 0x39U +#define CDC_GET_PROFILE 0x3AU + +#define CDC_SET_ETHERNET_MULTICAST_FILTERS 0x40U +#define CDC_SET_ETHERNET_POWER_MANAGEMENT_PATTERN FILTER 0x41U +#define CDC_GET_ETHERNET_POWER_MANAGEMENT_PATTERN FILTER 0x42U +#define CDC_SET_ETHERNET_PACKET_FILTER 0x43U +#define CDC_GET_ETHERNET_STATISTIC 0x44U + +#define CDC_SET_ATM_DATA_FORMAT 0x50U +#define CDC_GET_ATM_DEVICE_STATISTICS 0x51U +#define CDC_SET_ATM_DEFAULT_VC 0x52U +#define CDC_GET_ATM_VC_STATISTICS 0x53U + + +/* wValue for SetControlLineState*/ +#define CDC_ACTIVATE_CARRIER_SIGNAL_RTS 0x0002U +#define CDC_DEACTIVATE_CARRIER_SIGNAL_RTS 0x0000U +#define CDC_ACTIVATE_SIGNAL_DTR 0x0001U +#define CDC_DEACTIVATE_SIGNAL_DTR 0x0000U + +#define LINE_CODING_STRUCTURE_SIZE 0x07U +/** + * @} + */ + +/** @defgroup USBH_CDC_CORE_Exported_Types +* @{ +*/ + +/* States for CDC State Machine */ +typedef enum +{ + CDC_IDLE = 0U, + CDC_SEND_DATA, + CDC_SEND_DATA_WAIT, + CDC_RECEIVE_DATA, + CDC_RECEIVE_DATA_WAIT, +} +CDC_DataStateTypeDef; + +typedef enum +{ + CDC_IDLE_STATE = 0U, + CDC_SET_LINE_CODING_STATE, + CDC_GET_LAST_LINE_CODING_STATE, + CDC_TRANSFER_DATA, + CDC_ERROR_STATE, +} +CDC_StateTypeDef; + + +/*Line coding structure*/ +typedef union _CDC_LineCodingStructure +{ + uint8_t Array[LINE_CODING_STRUCTURE_SIZE]; + + struct + { + + uint32_t dwDTERate; /*Data terminal rate, in bits per second*/ + uint8_t bCharFormat; /*Stop bits + 0 - 1 Stop bit + 1 - 1.5 Stop bits + 2 - 2 Stop bits*/ + uint8_t bParityType; /* Parity + 0 - None + 1 - Odd + 2 - Even + 3 - Mark + 4 - Space*/ + uint8_t bDataBits; /* Data bits (5, 6, 7, 8 or 16). */ + }b; +} +CDC_LineCodingTypeDef; + + + +/* Header Functional Descriptor +-------------------------------------------------------------------------------- +Offset| field | Size | Value | Description +------|---------------------|-------|------------|------------------------------ +0 | bFunctionLength | 1 | number | Size of this descriptor. +1 | bDescriptorType | 1 | Constant | CS_INTERFACE (0x24) +2 | bDescriptorSubtype | 1 | Constant | Identifier (ID) of functional + | | | | descriptor. +3 | bcdCDC | 2 | | + | | | Number | USB Class Definitions for + | | | | Communication Devices Specification + | | | | release number in binary-coded + | | | | decimal +------|---------------------|-------|------------|------------------------------ +*/ +typedef struct _FunctionalDescriptorHeader +{ + uint8_t bLength; /*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_TypeDef; +/* Call Management Functional Descriptor +-------------------------------------------------------------------------------- +Offset| field | Size | Value | Description +------|---------------------|-------|------------|------------------------------ +0 | bFunctionLength | 1 | number | Size of this descriptor. +1 | bDescriptorType | 1 | Constant | CS_INTERFACE (0x24) +2 | bDescriptorSubtype | 1 | Constant | Call Management functional + | | | | descriptor subtype. +3 | bmCapabilities | 1 | Bitmap | The capabilities that this configuration + | | | | supports: + | | | | D7..D2: RESERVED (Reset to zero) + | | | | D1: 0 - Device sends/receives call + | | | | management information only over + | | | | the Communication Class + | | | | interface. + | | | | 1 - Device can send/receive call + | \ | | management information over a + | | | | Data Class interface. + | | | | D0: 0 - Device does not handle call + | | | | management itself. + | | | | 1 - Device handles call + | | | | management itself. + | | | | The previous bits, in combination, identify + | | | | which call management scenario is used. If bit + | | | | D0 is reset to 0, then the value of bit D1 is + | | | | ignored. In this case, bit D1 is reset to zero for + | | | | future compatibility. +4 | bDataInterface | 1 | Number | Interface number of Data Class interface + | | | | optionally used for call management. +------|---------------------|-------|------------|------------------------------ +*/ +typedef struct _CallMgmtFunctionalDescriptor +{ + 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_TypeDef; +/* Abstract Control Management Functional Descriptor +-------------------------------------------------------------------------------- +Offset| field | Size | Value | Description +------|---------------------|-------|------------|------------------------------ +0 | bFunctionLength | 1 | number | Size of functional descriptor, in bytes. +1 | bDescriptorType | 1 | Constant | CS_INTERFACE (0x24) +2 | bDescriptorSubtype | 1 | Constant | Abstract Control Management + | | | | functional descriptor subtype. +3 | bmCapabilities | 1 | Bitmap | The capabilities that this configuration + | | | | supports ((A bit value of zero means that the + | | | | request is not supported.) ) + D7..D4: RESERVED (Reset to zero) + | | | | D3: 1 - Device supports the notification + | | | | Network_Connection. + | | | | D2: 1 - Device supports the request + | | | | Send_Break + | | | | D1: 1 - Device supports the request + | \ | | combination of Set_Line_Coding, + | | | | Set_Control_Line_State, Get_Line_Coding, and the + notification Serial_State. + | | | | D0: 1 - Device supports the request + | | | | combination of Set_Comm_Feature, + | | | | Clear_Comm_Feature, and Get_Comm_Feature. + | | | | The previous bits, in combination, identify + | | | | which requests/notifications are supported by + | | | | a Communication Class interface with the + | | | | SubClass code of Abstract Control Model. +------|---------------------|-------|------------|------------------------------ +*/ +typedef struct _AbstractCntrlMgmtFunctionalDescriptor +{ + 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_TypeDef; +/* Union Functional Descriptor +-------------------------------------------------------------------------------- +Offset| field | Size | Value | Description +------|---------------------|-------|------------|------------------------------ +0 | bFunctionLength | 1 | number | Size of this descriptor. +1 | bDescriptorType | 1 | Constant | CS_INTERFACE (0x24) +2 | bDescriptorSubtype | 1 | Constant | Union functional + | | | | descriptor subtype. +3 | bMasterInterface | 1 | Constant | The interface number of the + | | | | Communication or Data Class interface +4 | bSlaveInterface0 | 1 | Number | nterface number of first slave or associated + | | | | interface in the union. +------|---------------------|-------|------------|------------------------------ +*/ +typedef struct _UnionFunctionalDescriptor +{ + 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_TypeDef; + +typedef struct _USBH_CDCInterfaceDesc +{ + CDC_HeaderFuncDesc_TypeDef CDC_HeaderFuncDesc; + CDC_CallMgmtFuncDesc_TypeDef CDC_CallMgmtFuncDesc; + CDC_AbstCntrlMgmtFuncDesc_TypeDef CDC_AbstCntrlMgmtFuncDesc; + CDC_UnionFuncDesc_TypeDef CDC_UnionFuncDesc; +} +CDC_InterfaceDesc_Typedef; + + +/* Structure for CDC process */ +typedef struct +{ + uint8_t NotifPipe; + uint8_t NotifEp; + uint8_t buff[8]; + uint16_t NotifEpSize; +} +CDC_CommItfTypedef ; + +typedef struct +{ + uint8_t InPipe; + uint8_t OutPipe; + uint8_t OutEp; + uint8_t InEp; + uint8_t buff[8]; + uint16_t OutEpSize; + uint16_t InEpSize; +} +CDC_DataItfTypedef ; + +/* Structure for CDC process */ +typedef struct _CDC_Process +{ + CDC_CommItfTypedef CommItf; + CDC_DataItfTypedef DataItf; + uint8_t *pTxData; + uint8_t *pRxData; + uint32_t TxDataLength; + uint32_t RxDataLength; + CDC_InterfaceDesc_Typedef CDC_Desc; + CDC_LineCodingTypeDef LineCoding; + CDC_LineCodingTypeDef *pUserLineCoding; + CDC_StateTypeDef state; + CDC_DataStateTypeDef data_tx_state; + CDC_DataStateTypeDef data_rx_state; + uint8_t Rx_Poll; +} +CDC_HandleTypeDef; + +/** +* @} +*/ + +/** @defgroup USBH_CDC_CORE_Exported_Defines +* @{ +*/ + +/** +* @} +*/ + +/** @defgroup USBH_CDC_CORE_Exported_Macros +* @{ +*/ +/** +* @} +*/ + +/** @defgroup USBH_CDC_CORE_Exported_Variables +* @{ +*/ +extern USBH_ClassTypeDef CDC_Class; +#define USBH_CDC_CLASS &CDC_Class + +/** +* @} +*/ + +/** @defgroup USBH_CDC_CORE_Exported_FunctionsPrototype +* @{ +*/ + +USBH_StatusTypeDef USBH_CDC_SetLineCoding(USBH_HandleTypeDef *phost, + CDC_LineCodingTypeDef *linecoding); + +USBH_StatusTypeDef USBH_CDC_GetLineCoding(USBH_HandleTypeDef *phost, + CDC_LineCodingTypeDef *linecoding); + +USBH_StatusTypeDef USBH_CDC_Transmit(USBH_HandleTypeDef *phost, + uint8_t *pbuff, + uint32_t length); + +USBH_StatusTypeDef USBH_CDC_Receive(USBH_HandleTypeDef *phost, + uint8_t *pbuff, + uint32_t length); + + +uint16_t USBH_CDC_GetLastReceivedDataSize(USBH_HandleTypeDef *phost); + +USBH_StatusTypeDef USBH_CDC_Stop(USBH_HandleTypeDef *phost); + +void USBH_CDC_LineCodingChanged(USBH_HandleTypeDef *phost); + +void USBH_CDC_TransmitCallback(USBH_HandleTypeDef *phost); + +void USBH_CDC_ReceiveCallback(USBH_HandleTypeDef *phost); + +/** +* @} +*/ + +#ifdef __cplusplus +} +#endif + +#endif /* __USBH_CDC_H */ + +/** +* @} +*/ + +/** +* @} +*/ + +/** +* @} +*/ + +/** +* @} +*/ +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Class/CDC/Src/usbh_cdc.c b/system/Middlewares/ST/STM32_USB_Host_Library/Class/CDC/Src/usbh_cdc.c index a087a92209..e8ad284cc3 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Class/CDC/Src/usbh_cdc.c +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Class/CDC/Src/usbh_cdc.c @@ -1,824 +1,824 @@ -/** - ****************************************************************************** - * @file usbh_cdc.c - * @author MCD Application Team - * @brief This file is the CDC Layer Handlers for USB Host CDC class. - * - * @verbatim - * - * =================================================================== - * CDC Class Driver Description - * =================================================================== - * This driver manages the "Universal Serial Bus Class Definitions for Communications Devices - * Revision 1.2 November 16, 2007" and the sub-protocol specification of "Universal Serial Bus - * Communications Class Subclass Specification for PSTN Devices Revision 1.2 February 9, 2007" - * This driver implements the following aspects of the specification: - * - Device descriptor management - * - Configuration descriptor management - * - Enumeration as CDC device with 2 data endpoints (IN and OUT) and 1 command endpoint (IN) - * - Requests management (as described in section 6.2 in specification) - * - Abstract Control Model compliant - * - Union Functional collection (using 1 IN endpoint for control) - * - Data interface class - * - * @endverbatim - * - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - - /* BSPDependencies - - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" - - "stm32xxxxx_{eval}{discovery}_io.c" - - "stm32xxxxx_{eval}{discovery}{adafruit}_sd.c" - - "stm32xxxxx_{eval}{discovery}{adafruit}_lcd.c" - - "stm32xxxxx_{eval}{discovery}_sdram.c" - EndBSPDependencies */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_cdc.h" - -/** @addtogroup USBH_LIB -* @{ -*/ - -/** @addtogroup USBH_CLASS -* @{ -*/ - -/** @addtogroup USBH_CDC_CLASS -* @{ -*/ - -/** @defgroup USBH_CDC_CORE -* @brief This file includes CDC Layer Handlers for USB Host CDC class. -* @{ -*/ - -/** @defgroup USBH_CDC_CORE_Private_TypesDefinitions -* @{ -*/ -/** -* @} -*/ - - -/** @defgroup USBH_CDC_CORE_Private_Defines -* @{ -*/ -#define USBH_CDC_BUFFER_SIZE 1024 -/** -* @} -*/ - - -/** @defgroup USBH_CDC_CORE_Private_Macros -* @{ -*/ -/** -* @} -*/ - - -/** @defgroup USBH_CDC_CORE_Private_Variables -* @{ -*/ -/** -* @} -*/ - - -/** @defgroup USBH_CDC_CORE_Private_FunctionPrototypes -* @{ -*/ - -static USBH_StatusTypeDef USBH_CDC_InterfaceInit (USBH_HandleTypeDef *phost); - -static USBH_StatusTypeDef USBH_CDC_InterfaceDeInit (USBH_HandleTypeDef *phost); - -static USBH_StatusTypeDef USBH_CDC_Process(USBH_HandleTypeDef *phost); - -static USBH_StatusTypeDef USBH_CDC_SOFProcess(USBH_HandleTypeDef *phost); - -static USBH_StatusTypeDef USBH_CDC_ClassRequest (USBH_HandleTypeDef *phost); - -static USBH_StatusTypeDef GetLineCoding(USBH_HandleTypeDef *phost, - CDC_LineCodingTypeDef *linecoding); - -static USBH_StatusTypeDef SetLineCoding(USBH_HandleTypeDef *phost, - CDC_LineCodingTypeDef *linecoding); - -static void CDC_ProcessTransmission(USBH_HandleTypeDef *phost); - -static void CDC_ProcessReception(USBH_HandleTypeDef *phost); - -USBH_ClassTypeDef CDC_Class = -{ - "CDC", - USB_CDC_CLASS, - USBH_CDC_InterfaceInit, - USBH_CDC_InterfaceDeInit, - USBH_CDC_ClassRequest, - USBH_CDC_Process, - USBH_CDC_SOFProcess, - NULL, -}; -/** -* @} -*/ - - -/** @defgroup USBH_CDC_CORE_Private_Functions -* @{ -*/ - -/** - * @brief USBH_CDC_InterfaceInit - * The function init the CDC class. - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_CDC_InterfaceInit (USBH_HandleTypeDef *phost) -{ - - USBH_StatusTypeDef status = USBH_FAIL ; - uint8_t interface; - CDC_HandleTypeDef *CDC_Handle; - - interface = USBH_FindInterface(phost, - COMMUNICATION_INTERFACE_CLASS_CODE, - ABSTRACT_CONTROL_MODEL, - COMMON_AT_COMMAND); - - if(interface == 0xFFU) /* No Valid Interface */ - { - USBH_DbgLog ("Cannot Find the interface for Communication Interface Class.", phost->pActiveClass->Name); - } - else - { - USBH_SelectInterface (phost, interface); - phost->pActiveClass->pData = (CDC_HandleTypeDef *)USBH_malloc (sizeof(CDC_HandleTypeDef)); - CDC_Handle = (CDC_HandleTypeDef*) phost->pActiveClass->pData; - - /*Collect the notification endpoint address and length*/ - if(phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress & 0x80U) - { - CDC_Handle->CommItf.NotifEp = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress; - CDC_Handle->CommItf.NotifEpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].wMaxPacketSize; - } - - /*Allocate the length for host channel number in*/ - CDC_Handle->CommItf.NotifPipe = USBH_AllocPipe(phost, CDC_Handle->CommItf.NotifEp); - - /* Open pipe for Notification endpoint */ - USBH_OpenPipe (phost, - CDC_Handle->CommItf.NotifPipe, - CDC_Handle->CommItf.NotifEp, - phost->device.address, - phost->device.speed, - USB_EP_TYPE_INTR, - CDC_Handle->CommItf.NotifEpSize); - - USBH_LL_SetToggle (phost, CDC_Handle->CommItf.NotifPipe, 0U); - - interface = USBH_FindInterface(phost, - DATA_INTERFACE_CLASS_CODE, - RESERVED, - NO_CLASS_SPECIFIC_PROTOCOL_CODE); - - if(interface == 0xFFU) /* No Valid Interface */ - { - USBH_DbgLog ("Cannot Find the interface for Data Interface Class.", phost->pActiveClass->Name); - } - else - { - /*Collect the class specific endpoint address and length*/ - if(phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress & 0x80U) - { - CDC_Handle->DataItf.InEp = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress; - CDC_Handle->DataItf.InEpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].wMaxPacketSize; - } - else - { - CDC_Handle->DataItf.OutEp = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress; - CDC_Handle->DataItf.OutEpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].wMaxPacketSize; - } - - if(phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[1].bEndpointAddress & 0x80U) - { - CDC_Handle->DataItf.InEp = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[1].bEndpointAddress; - CDC_Handle->DataItf.InEpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[1].wMaxPacketSize; - } - else - { - CDC_Handle->DataItf.OutEp = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[1].bEndpointAddress; - CDC_Handle->DataItf.OutEpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[1].wMaxPacketSize; - } - - /*Allocate the length for host channel number out*/ - CDC_Handle->DataItf.OutPipe = USBH_AllocPipe(phost, CDC_Handle->DataItf.OutEp); - - /*Allocate the length for host channel number in*/ - CDC_Handle->DataItf.InPipe = USBH_AllocPipe(phost, CDC_Handle->DataItf.InEp); - - /* Open channel for OUT endpoint */ - USBH_OpenPipe (phost, - CDC_Handle->DataItf.OutPipe, - CDC_Handle->DataItf.OutEp, - phost->device.address, - phost->device.speed, - USB_EP_TYPE_BULK, - CDC_Handle->DataItf.OutEpSize); - /* Open channel for IN endpoint */ - USBH_OpenPipe (phost, - CDC_Handle->DataItf.InPipe, - CDC_Handle->DataItf.InEp, - phost->device.address, - phost->device.speed, - USB_EP_TYPE_BULK, - CDC_Handle->DataItf.InEpSize); - - CDC_Handle->state = CDC_IDLE_STATE; - - USBH_LL_SetToggle (phost, CDC_Handle->DataItf.OutPipe, 0U); - USBH_LL_SetToggle (phost, CDC_Handle->DataItf.InPipe, 0U); - status = USBH_OK; - } - } - return status; -} - - - -/** - * @brief USBH_CDC_InterfaceDeInit - * The function DeInit the Pipes used for the CDC class. - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_CDC_InterfaceDeInit (USBH_HandleTypeDef *phost) -{ - CDC_HandleTypeDef *CDC_Handle = (CDC_HandleTypeDef*) phost->pActiveClass->pData; - - if ( CDC_Handle->CommItf.NotifPipe) - { - USBH_ClosePipe(phost, CDC_Handle->CommItf.NotifPipe); - USBH_FreePipe (phost, CDC_Handle->CommItf.NotifPipe); - CDC_Handle->CommItf.NotifPipe = 0U; /* Reset the Channel as Free */ - } - - if ( CDC_Handle->DataItf.InPipe) - { - USBH_ClosePipe(phost, CDC_Handle->DataItf.InPipe); - USBH_FreePipe (phost, CDC_Handle->DataItf.InPipe); - CDC_Handle->DataItf.InPipe = 0U; /* Reset the Channel as Free */ - } - - if ( CDC_Handle->DataItf.OutPipe) - { - USBH_ClosePipe(phost, CDC_Handle->DataItf.OutPipe); - USBH_FreePipe (phost, CDC_Handle->DataItf.OutPipe); - CDC_Handle->DataItf.OutPipe = 0U; /* Reset the Channel as Free */ - } - - if(phost->pActiveClass->pData) - { - USBH_free (phost->pActiveClass->pData); - phost->pActiveClass->pData = 0U; - } - - return USBH_OK; -} - -/** - * @brief USBH_CDC_ClassRequest - * The function is responsible for handling Standard requests - * for CDC class. - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_CDC_ClassRequest (USBH_HandleTypeDef *phost) -{ - USBH_StatusTypeDef status = USBH_FAIL ; - CDC_HandleTypeDef *CDC_Handle = (CDC_HandleTypeDef*) phost->pActiveClass->pData; - - /*Issue the get line coding request*/ - status = GetLineCoding(phost, &CDC_Handle->LineCoding); - if(status == USBH_OK) - { - phost->pUser(phost, HOST_USER_CLASS_ACTIVE); - } - return status; -} - - -/** - * @brief USBH_CDC_Process - * The function is for managing state machine for CDC data transfers - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_CDC_Process (USBH_HandleTypeDef *phost) -{ - USBH_StatusTypeDef status = USBH_BUSY; - USBH_StatusTypeDef req_status = USBH_OK; - CDC_HandleTypeDef *CDC_Handle = (CDC_HandleTypeDef*) phost->pActiveClass->pData; - - switch(CDC_Handle->state) - { - - case CDC_IDLE_STATE: - status = USBH_OK; - break; - - case CDC_SET_LINE_CODING_STATE: - req_status = SetLineCoding(phost, CDC_Handle->pUserLineCoding); - - if(req_status == USBH_OK) - { - CDC_Handle->state = CDC_GET_LAST_LINE_CODING_STATE; - } - - else - { - if(req_status != USBH_BUSY) - { - CDC_Handle->state = CDC_ERROR_STATE; - } - } - break; - - - case CDC_GET_LAST_LINE_CODING_STATE: - req_status = GetLineCoding(phost, &(CDC_Handle->LineCoding)); - - if(req_status == USBH_OK) - { - CDC_Handle->state = CDC_IDLE_STATE; - - if((CDC_Handle->LineCoding.b.bCharFormat == CDC_Handle->pUserLineCoding->b.bCharFormat) && - (CDC_Handle->LineCoding.b.bDataBits == CDC_Handle->pUserLineCoding->b.bDataBits) && - (CDC_Handle->LineCoding.b.bParityType == CDC_Handle->pUserLineCoding->b.bParityType) && - (CDC_Handle->LineCoding.b.dwDTERate == CDC_Handle->pUserLineCoding->b.dwDTERate)) - { - USBH_CDC_LineCodingChanged(phost); - } - } - else - { - if(req_status != USBH_BUSY) - { - CDC_Handle->state = CDC_ERROR_STATE; - } - } - break; - - case CDC_TRANSFER_DATA: - CDC_ProcessTransmission(phost); - CDC_ProcessReception(phost); - break; - - case CDC_ERROR_STATE: - req_status = USBH_ClrFeature(phost, 0x00U); - - if(req_status == USBH_OK ) - { - /*Change the state to waiting*/ - CDC_Handle->state = CDC_IDLE_STATE ; - } - break; - - default: - break; - - } - - return status; -} - -/** - * @brief USBH_CDC_SOFProcess - * The function is for managing SOF callback - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_CDC_SOFProcess (USBH_HandleTypeDef *phost) -{ - return USBH_OK; -} - - -/** - * @brief USBH_CDC_Stop - * Stop current CDC Transmission - * @param phost: Host handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_CDC_Stop(USBH_HandleTypeDef *phost) -{ - CDC_HandleTypeDef *CDC_Handle = (CDC_HandleTypeDef*) phost->pActiveClass->pData; - - if(phost->gState == HOST_CLASS) - { - CDC_Handle->state = CDC_IDLE_STATE; - - USBH_ClosePipe(phost, CDC_Handle->CommItf.NotifPipe); - USBH_ClosePipe(phost, CDC_Handle->DataItf.InPipe); - USBH_ClosePipe(phost, CDC_Handle->DataItf.OutPipe); - } - return USBH_OK; -} -/** - * @brief This request allows the host to find out the currently - * configured line coding. - * @param pdev: Selected device - * @retval USBH_StatusTypeDef : USB ctl xfer status - */ -static USBH_StatusTypeDef GetLineCoding(USBH_HandleTypeDef *phost, CDC_LineCodingTypeDef *linecoding) -{ - - phost->Control.setup.b.bmRequestType = USB_D2H | USB_REQ_TYPE_CLASS | \ - USB_REQ_RECIPIENT_INTERFACE; - - phost->Control.setup.b.bRequest = CDC_GET_LINE_CODING; - phost->Control.setup.b.wValue.w = 0U; - phost->Control.setup.b.wIndex.w = 0U; - phost->Control.setup.b.wLength.w = LINE_CODING_STRUCTURE_SIZE; - - return USBH_CtlReq(phost, linecoding->Array, LINE_CODING_STRUCTURE_SIZE); -} - - -/** - * @brief This request allows the host to specify typical asynchronous - * line-character formatting properties - * This request applies to asynchronous byte stream data class interfaces - * and endpoints - * @param pdev: Selected device - * @retval USBH_StatusTypeDef : USB ctl xfer status - */ -static USBH_StatusTypeDef SetLineCoding(USBH_HandleTypeDef *phost, - CDC_LineCodingTypeDef *linecoding) -{ - phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_TYPE_CLASS | - USB_REQ_RECIPIENT_INTERFACE; - - phost->Control.setup.b.bRequest = CDC_SET_LINE_CODING; - phost->Control.setup.b.wValue.w = 0U; - - phost->Control.setup.b.wIndex.w = 0U; - - phost->Control.setup.b.wLength.w = LINE_CODING_STRUCTURE_SIZE; - - return USBH_CtlReq(phost, linecoding->Array, LINE_CODING_STRUCTURE_SIZE); -} - -/** -* @brief This function prepares the state before issuing the class specific commands -* @param None -* @retval None -*/ -USBH_StatusTypeDef USBH_CDC_SetLineCoding(USBH_HandleTypeDef *phost, - CDC_LineCodingTypeDef *linecoding) -{ - CDC_HandleTypeDef *CDC_Handle = (CDC_HandleTypeDef*) phost->pActiveClass->pData; - - if(phost->gState == HOST_CLASS) - { - CDC_Handle->state = CDC_SET_LINE_CODING_STATE; - CDC_Handle->pUserLineCoding = linecoding; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CLASS_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - - return USBH_OK; -} - -/** -* @brief This function prepares the state before issuing the class specific commands -* @param None -* @retval None -*/ -USBH_StatusTypeDef USBH_CDC_GetLineCoding(USBH_HandleTypeDef *phost, - CDC_LineCodingTypeDef *linecoding) -{ - CDC_HandleTypeDef *CDC_Handle = (CDC_HandleTypeDef*) phost->pActiveClass->pData; - - if((phost->gState == HOST_CLASS) || (phost->gState == HOST_CLASS_REQUEST)) - { - *linecoding = CDC_Handle->LineCoding; - return USBH_OK; - } - else - { - return USBH_FAIL; - } -} - -/** - * @brief This function return last received data size - * @param None - * @retval None - */ -uint16_t USBH_CDC_GetLastReceivedDataSize(USBH_HandleTypeDef *phost) -{ - uint32_t dataSize; - CDC_HandleTypeDef *CDC_Handle = (CDC_HandleTypeDef*) phost->pActiveClass->pData; - - if(phost->gState == HOST_CLASS) - { - dataSize = USBH_LL_GetLastXferSize(phost, CDC_Handle->DataItf.InPipe); - } - else - { - dataSize = 0U; - } - - return (uint16_t)dataSize; -} - -/** - * @brief This function prepares the state before issuing the class specific commands - * @param None - * @retval None - */ -USBH_StatusTypeDef USBH_CDC_Transmit(USBH_HandleTypeDef *phost, uint8_t *pbuff, uint32_t length) -{ - USBH_StatusTypeDef Status = USBH_BUSY; - CDC_HandleTypeDef *CDC_Handle = (CDC_HandleTypeDef*) phost->pActiveClass->pData; - - if((CDC_Handle->state == CDC_IDLE_STATE) || (CDC_Handle->state == CDC_TRANSFER_DATA)) - { - CDC_Handle->pTxData = pbuff; - CDC_Handle->TxDataLength = length; - CDC_Handle->state = CDC_TRANSFER_DATA; - CDC_Handle->data_tx_state = CDC_SEND_DATA; - Status = USBH_OK; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CLASS_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - return Status; -} - - -/** -* @brief This function prepares the state before issuing the class specific commands -* @param None -* @retval None -*/ -USBH_StatusTypeDef USBH_CDC_Receive(USBH_HandleTypeDef *phost, uint8_t *pbuff, uint32_t length) -{ - USBH_StatusTypeDef Status = USBH_BUSY; - CDC_HandleTypeDef *CDC_Handle = (CDC_HandleTypeDef*) phost->pActiveClass->pData; - - if((CDC_Handle->state == CDC_IDLE_STATE) || (CDC_Handle->state == CDC_TRANSFER_DATA)) - { - CDC_Handle->pRxData = pbuff; - CDC_Handle->RxDataLength = length; - CDC_Handle->state = CDC_TRANSFER_DATA; - CDC_Handle->data_rx_state = CDC_RECEIVE_DATA; - Status = USBH_OK; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CLASS_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - return Status; -} - -/** -* @brief The function is responsible for sending data to the device -* @param pdev: Selected device -* @retval None -*/ -static void CDC_ProcessTransmission(USBH_HandleTypeDef *phost) -{ - CDC_HandleTypeDef *CDC_Handle = (CDC_HandleTypeDef*) phost->pActiveClass->pData; - USBH_URBStateTypeDef URB_Status = USBH_URB_IDLE; - - switch (CDC_Handle->data_tx_state) - { - case CDC_SEND_DATA: - if (CDC_Handle->TxDataLength > CDC_Handle->DataItf.OutEpSize) - { - USBH_BulkSendData (phost, - CDC_Handle->pTxData, - CDC_Handle->DataItf.OutEpSize, - CDC_Handle->DataItf.OutPipe, - 1U); - } - else - { - USBH_BulkSendData (phost, - CDC_Handle->pTxData, - (uint16_t)CDC_Handle->TxDataLength, - CDC_Handle->DataItf.OutPipe, - 1U); - } - - CDC_Handle->data_tx_state = CDC_SEND_DATA_WAIT; - break; - - case CDC_SEND_DATA_WAIT: - - URB_Status = USBH_LL_GetURBState(phost, CDC_Handle->DataItf.OutPipe); - - /* Check the status done for transmission */ - if (URB_Status == USBH_URB_DONE) - { - if (CDC_Handle->TxDataLength > CDC_Handle->DataItf.OutEpSize) - { - CDC_Handle->TxDataLength -= CDC_Handle->DataItf.OutEpSize; - CDC_Handle->pTxData += CDC_Handle->DataItf.OutEpSize; - } - else - { - CDC_Handle->TxDataLength = 0U; - } - - if (CDC_Handle->TxDataLength > 0U) - { - CDC_Handle->data_tx_state = CDC_SEND_DATA; - } - else - { - CDC_Handle->data_tx_state = CDC_IDLE; - USBH_CDC_TransmitCallback(phost); - } - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CLASS_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - else - { - if (URB_Status == USBH_URB_NOTREADY) - { - CDC_Handle->data_tx_state = CDC_SEND_DATA; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CLASS_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - } - break; - - default: - break; - } -} -/** -* @brief This function responsible for reception of data from the device -* @param pdev: Selected device -* @retval None -*/ - -static void CDC_ProcessReception(USBH_HandleTypeDef *phost) -{ - CDC_HandleTypeDef *CDC_Handle = (CDC_HandleTypeDef*) phost->pActiveClass->pData; - USBH_URBStateTypeDef URB_Status = USBH_URB_IDLE; - uint32_t length; - - switch(CDC_Handle->data_rx_state) - { - - case CDC_RECEIVE_DATA: - - USBH_BulkReceiveData (phost, - CDC_Handle->pRxData, - CDC_Handle->DataItf.InEpSize, - CDC_Handle->DataItf.InPipe); - - CDC_Handle->data_rx_state = CDC_RECEIVE_DATA_WAIT; - - break; - - case CDC_RECEIVE_DATA_WAIT: - - URB_Status = USBH_LL_GetURBState(phost, CDC_Handle->DataItf.InPipe); - - /*Check the status done for reception*/ - if(URB_Status == USBH_URB_DONE) - { - length = USBH_LL_GetLastXferSize(phost, CDC_Handle->DataItf.InPipe); - - if(((CDC_Handle->RxDataLength - length) > 0U) && (length > CDC_Handle->DataItf.InEpSize)) - { - CDC_Handle->RxDataLength -= length ; - CDC_Handle->pRxData += length; - CDC_Handle->data_rx_state = CDC_RECEIVE_DATA; - } - else - { - CDC_Handle->data_rx_state = CDC_IDLE; - USBH_CDC_ReceiveCallback(phost); - } - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CLASS_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - break; - - default: - break; - } -} - -/** -* @brief The function informs user that data have been received -* @param pdev: Selected device -* @retval None -*/ -__weak void USBH_CDC_TransmitCallback(USBH_HandleTypeDef *phost) -{ - -} - - /** -* @brief The function informs user that data have been sent -* @param pdev: Selected device -* @retval None -*/ -__weak void USBH_CDC_ReceiveCallback(USBH_HandleTypeDef *phost) -{ - -} - - /** -* @brief The function informs user that Settings have been changed -* @param pdev: Selected device -* @retval None -*/ -__weak void USBH_CDC_LineCodingChanged(USBH_HandleTypeDef *phost) -{ - -} - -/** -* @} -*/ - -/** -* @} -*/ - -/** -* @} -*/ - - -/** -* @} -*/ - - -/** -* @} -*/ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbh_cdc.c + * @author MCD Application Team + * @brief This file is the CDC Layer Handlers for USB Host CDC class. + * + * @verbatim + * + * =================================================================== + * CDC Class Driver Description + * =================================================================== + * This driver manages the "Universal Serial Bus Class Definitions for Communications Devices + * Revision 1.2 November 16, 2007" and the sub-protocol specification of "Universal Serial Bus + * Communications Class Subclass Specification for PSTN Devices Revision 1.2 February 9, 2007" + * This driver implements the following aspects of the specification: + * - Device descriptor management + * - Configuration descriptor management + * - Enumeration as CDC device with 2 data endpoints (IN and OUT) and 1 command endpoint (IN) + * - Requests management (as described in section 6.2 in specification) + * - Abstract Control Model compliant + * - Union Functional collection (using 1 IN endpoint for control) + * - Data interface class + * + * @endverbatim + * + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + + /* BSPDependencies + - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" + - "stm32xxxxx_{eval}{discovery}_io.c" + - "stm32xxxxx_{eval}{discovery}{adafruit}_sd.c" + - "stm32xxxxx_{eval}{discovery}{adafruit}_lcd.c" + - "stm32xxxxx_{eval}{discovery}_sdram.c" + EndBSPDependencies */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_cdc.h" + +/** @addtogroup USBH_LIB +* @{ +*/ + +/** @addtogroup USBH_CLASS +* @{ +*/ + +/** @addtogroup USBH_CDC_CLASS +* @{ +*/ + +/** @defgroup USBH_CDC_CORE +* @brief This file includes CDC Layer Handlers for USB Host CDC class. +* @{ +*/ + +/** @defgroup USBH_CDC_CORE_Private_TypesDefinitions +* @{ +*/ +/** +* @} +*/ + + +/** @defgroup USBH_CDC_CORE_Private_Defines +* @{ +*/ +#define USBH_CDC_BUFFER_SIZE 1024 +/** +* @} +*/ + + +/** @defgroup USBH_CDC_CORE_Private_Macros +* @{ +*/ +/** +* @} +*/ + + +/** @defgroup USBH_CDC_CORE_Private_Variables +* @{ +*/ +/** +* @} +*/ + + +/** @defgroup USBH_CDC_CORE_Private_FunctionPrototypes +* @{ +*/ + +static USBH_StatusTypeDef USBH_CDC_InterfaceInit (USBH_HandleTypeDef *phost); + +static USBH_StatusTypeDef USBH_CDC_InterfaceDeInit (USBH_HandleTypeDef *phost); + +static USBH_StatusTypeDef USBH_CDC_Process(USBH_HandleTypeDef *phost); + +static USBH_StatusTypeDef USBH_CDC_SOFProcess(USBH_HandleTypeDef *phost); + +static USBH_StatusTypeDef USBH_CDC_ClassRequest (USBH_HandleTypeDef *phost); + +static USBH_StatusTypeDef GetLineCoding(USBH_HandleTypeDef *phost, + CDC_LineCodingTypeDef *linecoding); + +static USBH_StatusTypeDef SetLineCoding(USBH_HandleTypeDef *phost, + CDC_LineCodingTypeDef *linecoding); + +static void CDC_ProcessTransmission(USBH_HandleTypeDef *phost); + +static void CDC_ProcessReception(USBH_HandleTypeDef *phost); + +USBH_ClassTypeDef CDC_Class = +{ + "CDC", + USB_CDC_CLASS, + USBH_CDC_InterfaceInit, + USBH_CDC_InterfaceDeInit, + USBH_CDC_ClassRequest, + USBH_CDC_Process, + USBH_CDC_SOFProcess, + NULL, +}; +/** +* @} +*/ + + +/** @defgroup USBH_CDC_CORE_Private_Functions +* @{ +*/ + +/** + * @brief USBH_CDC_InterfaceInit + * The function init the CDC class. + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_CDC_InterfaceInit (USBH_HandleTypeDef *phost) +{ + + USBH_StatusTypeDef status = USBH_FAIL ; + uint8_t interface; + CDC_HandleTypeDef *CDC_Handle; + + interface = USBH_FindInterface(phost, + COMMUNICATION_INTERFACE_CLASS_CODE, + ABSTRACT_CONTROL_MODEL, + COMMON_AT_COMMAND); + + if(interface == 0xFFU) /* No Valid Interface */ + { + USBH_DbgLog ("Cannot Find the interface for Communication Interface Class.", phost->pActiveClass->Name); + } + else + { + USBH_SelectInterface (phost, interface); + phost->pActiveClass->pData = (CDC_HandleTypeDef *)USBH_malloc (sizeof(CDC_HandleTypeDef)); + CDC_Handle = (CDC_HandleTypeDef*) phost->pActiveClass->pData; + + /*Collect the notification endpoint address and length*/ + if(phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress & 0x80U) + { + CDC_Handle->CommItf.NotifEp = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress; + CDC_Handle->CommItf.NotifEpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].wMaxPacketSize; + } + + /*Allocate the length for host channel number in*/ + CDC_Handle->CommItf.NotifPipe = USBH_AllocPipe(phost, CDC_Handle->CommItf.NotifEp); + + /* Open pipe for Notification endpoint */ + USBH_OpenPipe (phost, + CDC_Handle->CommItf.NotifPipe, + CDC_Handle->CommItf.NotifEp, + phost->device.address, + phost->device.speed, + USB_EP_TYPE_INTR, + CDC_Handle->CommItf.NotifEpSize); + + USBH_LL_SetToggle (phost, CDC_Handle->CommItf.NotifPipe, 0U); + + interface = USBH_FindInterface(phost, + DATA_INTERFACE_CLASS_CODE, + RESERVED, + NO_CLASS_SPECIFIC_PROTOCOL_CODE); + + if(interface == 0xFFU) /* No Valid Interface */ + { + USBH_DbgLog ("Cannot Find the interface for Data Interface Class.", phost->pActiveClass->Name); + } + else + { + /*Collect the class specific endpoint address and length*/ + if(phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress & 0x80U) + { + CDC_Handle->DataItf.InEp = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress; + CDC_Handle->DataItf.InEpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].wMaxPacketSize; + } + else + { + CDC_Handle->DataItf.OutEp = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].bEndpointAddress; + CDC_Handle->DataItf.OutEpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[0].wMaxPacketSize; + } + + if(phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[1].bEndpointAddress & 0x80U) + { + CDC_Handle->DataItf.InEp = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[1].bEndpointAddress; + CDC_Handle->DataItf.InEpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[1].wMaxPacketSize; + } + else + { + CDC_Handle->DataItf.OutEp = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[1].bEndpointAddress; + CDC_Handle->DataItf.OutEpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[1].wMaxPacketSize; + } + + /*Allocate the length for host channel number out*/ + CDC_Handle->DataItf.OutPipe = USBH_AllocPipe(phost, CDC_Handle->DataItf.OutEp); + + /*Allocate the length for host channel number in*/ + CDC_Handle->DataItf.InPipe = USBH_AllocPipe(phost, CDC_Handle->DataItf.InEp); + + /* Open channel for OUT endpoint */ + USBH_OpenPipe (phost, + CDC_Handle->DataItf.OutPipe, + CDC_Handle->DataItf.OutEp, + phost->device.address, + phost->device.speed, + USB_EP_TYPE_BULK, + CDC_Handle->DataItf.OutEpSize); + /* Open channel for IN endpoint */ + USBH_OpenPipe (phost, + CDC_Handle->DataItf.InPipe, + CDC_Handle->DataItf.InEp, + phost->device.address, + phost->device.speed, + USB_EP_TYPE_BULK, + CDC_Handle->DataItf.InEpSize); + + CDC_Handle->state = CDC_IDLE_STATE; + + USBH_LL_SetToggle (phost, CDC_Handle->DataItf.OutPipe, 0U); + USBH_LL_SetToggle (phost, CDC_Handle->DataItf.InPipe, 0U); + status = USBH_OK; + } + } + return status; +} + + + +/** + * @brief USBH_CDC_InterfaceDeInit + * The function DeInit the Pipes used for the CDC class. + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_CDC_InterfaceDeInit (USBH_HandleTypeDef *phost) +{ + CDC_HandleTypeDef *CDC_Handle = (CDC_HandleTypeDef*) phost->pActiveClass->pData; + + if ( CDC_Handle->CommItf.NotifPipe) + { + USBH_ClosePipe(phost, CDC_Handle->CommItf.NotifPipe); + USBH_FreePipe (phost, CDC_Handle->CommItf.NotifPipe); + CDC_Handle->CommItf.NotifPipe = 0U; /* Reset the Channel as Free */ + } + + if ( CDC_Handle->DataItf.InPipe) + { + USBH_ClosePipe(phost, CDC_Handle->DataItf.InPipe); + USBH_FreePipe (phost, CDC_Handle->DataItf.InPipe); + CDC_Handle->DataItf.InPipe = 0U; /* Reset the Channel as Free */ + } + + if ( CDC_Handle->DataItf.OutPipe) + { + USBH_ClosePipe(phost, CDC_Handle->DataItf.OutPipe); + USBH_FreePipe (phost, CDC_Handle->DataItf.OutPipe); + CDC_Handle->DataItf.OutPipe = 0U; /* Reset the Channel as Free */ + } + + if(phost->pActiveClass->pData) + { + USBH_free (phost->pActiveClass->pData); + phost->pActiveClass->pData = 0U; + } + + return USBH_OK; +} + +/** + * @brief USBH_CDC_ClassRequest + * The function is responsible for handling Standard requests + * for CDC class. + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_CDC_ClassRequest (USBH_HandleTypeDef *phost) +{ + USBH_StatusTypeDef status = USBH_FAIL ; + CDC_HandleTypeDef *CDC_Handle = (CDC_HandleTypeDef*) phost->pActiveClass->pData; + + /*Issue the get line coding request*/ + status = GetLineCoding(phost, &CDC_Handle->LineCoding); + if(status == USBH_OK) + { + phost->pUser(phost, HOST_USER_CLASS_ACTIVE); + } + return status; +} + + +/** + * @brief USBH_CDC_Process + * The function is for managing state machine for CDC data transfers + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_CDC_Process (USBH_HandleTypeDef *phost) +{ + USBH_StatusTypeDef status = USBH_BUSY; + USBH_StatusTypeDef req_status = USBH_OK; + CDC_HandleTypeDef *CDC_Handle = (CDC_HandleTypeDef*) phost->pActiveClass->pData; + + switch(CDC_Handle->state) + { + + case CDC_IDLE_STATE: + status = USBH_OK; + break; + + case CDC_SET_LINE_CODING_STATE: + req_status = SetLineCoding(phost, CDC_Handle->pUserLineCoding); + + if(req_status == USBH_OK) + { + CDC_Handle->state = CDC_GET_LAST_LINE_CODING_STATE; + } + + else + { + if(req_status != USBH_BUSY) + { + CDC_Handle->state = CDC_ERROR_STATE; + } + } + break; + + + case CDC_GET_LAST_LINE_CODING_STATE: + req_status = GetLineCoding(phost, &(CDC_Handle->LineCoding)); + + if(req_status == USBH_OK) + { + CDC_Handle->state = CDC_IDLE_STATE; + + if((CDC_Handle->LineCoding.b.bCharFormat == CDC_Handle->pUserLineCoding->b.bCharFormat) && + (CDC_Handle->LineCoding.b.bDataBits == CDC_Handle->pUserLineCoding->b.bDataBits) && + (CDC_Handle->LineCoding.b.bParityType == CDC_Handle->pUserLineCoding->b.bParityType) && + (CDC_Handle->LineCoding.b.dwDTERate == CDC_Handle->pUserLineCoding->b.dwDTERate)) + { + USBH_CDC_LineCodingChanged(phost); + } + } + else + { + if(req_status != USBH_BUSY) + { + CDC_Handle->state = CDC_ERROR_STATE; + } + } + break; + + case CDC_TRANSFER_DATA: + CDC_ProcessTransmission(phost); + CDC_ProcessReception(phost); + break; + + case CDC_ERROR_STATE: + req_status = USBH_ClrFeature(phost, 0x00U); + + if(req_status == USBH_OK ) + { + /*Change the state to waiting*/ + CDC_Handle->state = CDC_IDLE_STATE ; + } + break; + + default: + break; + + } + + return status; +} + +/** + * @brief USBH_CDC_SOFProcess + * The function is for managing SOF callback + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_CDC_SOFProcess (USBH_HandleTypeDef *phost) +{ + return USBH_OK; +} + + +/** + * @brief USBH_CDC_Stop + * Stop current CDC Transmission + * @param phost: Host handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_CDC_Stop(USBH_HandleTypeDef *phost) +{ + CDC_HandleTypeDef *CDC_Handle = (CDC_HandleTypeDef*) phost->pActiveClass->pData; + + if(phost->gState == HOST_CLASS) + { + CDC_Handle->state = CDC_IDLE_STATE; + + USBH_ClosePipe(phost, CDC_Handle->CommItf.NotifPipe); + USBH_ClosePipe(phost, CDC_Handle->DataItf.InPipe); + USBH_ClosePipe(phost, CDC_Handle->DataItf.OutPipe); + } + return USBH_OK; +} +/** + * @brief This request allows the host to find out the currently + * configured line coding. + * @param pdev: Selected device + * @retval USBH_StatusTypeDef : USB ctl xfer status + */ +static USBH_StatusTypeDef GetLineCoding(USBH_HandleTypeDef *phost, CDC_LineCodingTypeDef *linecoding) +{ + + phost->Control.setup.b.bmRequestType = USB_D2H | USB_REQ_TYPE_CLASS | \ + USB_REQ_RECIPIENT_INTERFACE; + + phost->Control.setup.b.bRequest = CDC_GET_LINE_CODING; + phost->Control.setup.b.wValue.w = 0U; + phost->Control.setup.b.wIndex.w = 0U; + phost->Control.setup.b.wLength.w = LINE_CODING_STRUCTURE_SIZE; + + return USBH_CtlReq(phost, linecoding->Array, LINE_CODING_STRUCTURE_SIZE); +} + + +/** + * @brief This request allows the host to specify typical asynchronous + * line-character formatting properties + * This request applies to asynchronous byte stream data class interfaces + * and endpoints + * @param pdev: Selected device + * @retval USBH_StatusTypeDef : USB ctl xfer status + */ +static USBH_StatusTypeDef SetLineCoding(USBH_HandleTypeDef *phost, + CDC_LineCodingTypeDef *linecoding) +{ + phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_TYPE_CLASS | + USB_REQ_RECIPIENT_INTERFACE; + + phost->Control.setup.b.bRequest = CDC_SET_LINE_CODING; + phost->Control.setup.b.wValue.w = 0U; + + phost->Control.setup.b.wIndex.w = 0U; + + phost->Control.setup.b.wLength.w = LINE_CODING_STRUCTURE_SIZE; + + return USBH_CtlReq(phost, linecoding->Array, LINE_CODING_STRUCTURE_SIZE); +} + +/** +* @brief This function prepares the state before issuing the class specific commands +* @param None +* @retval None +*/ +USBH_StatusTypeDef USBH_CDC_SetLineCoding(USBH_HandleTypeDef *phost, + CDC_LineCodingTypeDef *linecoding) +{ + CDC_HandleTypeDef *CDC_Handle = (CDC_HandleTypeDef*) phost->pActiveClass->pData; + + if(phost->gState == HOST_CLASS) + { + CDC_Handle->state = CDC_SET_LINE_CODING_STATE; + CDC_Handle->pUserLineCoding = linecoding; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CLASS_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + + return USBH_OK; +} + +/** +* @brief This function prepares the state before issuing the class specific commands +* @param None +* @retval None +*/ +USBH_StatusTypeDef USBH_CDC_GetLineCoding(USBH_HandleTypeDef *phost, + CDC_LineCodingTypeDef *linecoding) +{ + CDC_HandleTypeDef *CDC_Handle = (CDC_HandleTypeDef*) phost->pActiveClass->pData; + + if((phost->gState == HOST_CLASS) || (phost->gState == HOST_CLASS_REQUEST)) + { + *linecoding = CDC_Handle->LineCoding; + return USBH_OK; + } + else + { + return USBH_FAIL; + } +} + +/** + * @brief This function return last received data size + * @param None + * @retval None + */ +uint16_t USBH_CDC_GetLastReceivedDataSize(USBH_HandleTypeDef *phost) +{ + uint32_t dataSize; + CDC_HandleTypeDef *CDC_Handle = (CDC_HandleTypeDef*) phost->pActiveClass->pData; + + if(phost->gState == HOST_CLASS) + { + dataSize = USBH_LL_GetLastXferSize(phost, CDC_Handle->DataItf.InPipe); + } + else + { + dataSize = 0U; + } + + return (uint16_t)dataSize; +} + +/** + * @brief This function prepares the state before issuing the class specific commands + * @param None + * @retval None + */ +USBH_StatusTypeDef USBH_CDC_Transmit(USBH_HandleTypeDef *phost, uint8_t *pbuff, uint32_t length) +{ + USBH_StatusTypeDef Status = USBH_BUSY; + CDC_HandleTypeDef *CDC_Handle = (CDC_HandleTypeDef*) phost->pActiveClass->pData; + + if((CDC_Handle->state == CDC_IDLE_STATE) || (CDC_Handle->state == CDC_TRANSFER_DATA)) + { + CDC_Handle->pTxData = pbuff; + CDC_Handle->TxDataLength = length; + CDC_Handle->state = CDC_TRANSFER_DATA; + CDC_Handle->data_tx_state = CDC_SEND_DATA; + Status = USBH_OK; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CLASS_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + return Status; +} + + +/** +* @brief This function prepares the state before issuing the class specific commands +* @param None +* @retval None +*/ +USBH_StatusTypeDef USBH_CDC_Receive(USBH_HandleTypeDef *phost, uint8_t *pbuff, uint32_t length) +{ + USBH_StatusTypeDef Status = USBH_BUSY; + CDC_HandleTypeDef *CDC_Handle = (CDC_HandleTypeDef*) phost->pActiveClass->pData; + + if((CDC_Handle->state == CDC_IDLE_STATE) || (CDC_Handle->state == CDC_TRANSFER_DATA)) + { + CDC_Handle->pRxData = pbuff; + CDC_Handle->RxDataLength = length; + CDC_Handle->state = CDC_TRANSFER_DATA; + CDC_Handle->data_rx_state = CDC_RECEIVE_DATA; + Status = USBH_OK; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CLASS_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + return Status; +} + +/** +* @brief The function is responsible for sending data to the device +* @param pdev: Selected device +* @retval None +*/ +static void CDC_ProcessTransmission(USBH_HandleTypeDef *phost) +{ + CDC_HandleTypeDef *CDC_Handle = (CDC_HandleTypeDef*) phost->pActiveClass->pData; + USBH_URBStateTypeDef URB_Status = USBH_URB_IDLE; + + switch (CDC_Handle->data_tx_state) + { + case CDC_SEND_DATA: + if (CDC_Handle->TxDataLength > CDC_Handle->DataItf.OutEpSize) + { + USBH_BulkSendData (phost, + CDC_Handle->pTxData, + CDC_Handle->DataItf.OutEpSize, + CDC_Handle->DataItf.OutPipe, + 1U); + } + else + { + USBH_BulkSendData (phost, + CDC_Handle->pTxData, + (uint16_t)CDC_Handle->TxDataLength, + CDC_Handle->DataItf.OutPipe, + 1U); + } + + CDC_Handle->data_tx_state = CDC_SEND_DATA_WAIT; + break; + + case CDC_SEND_DATA_WAIT: + + URB_Status = USBH_LL_GetURBState(phost, CDC_Handle->DataItf.OutPipe); + + /* Check the status done for transmission */ + if (URB_Status == USBH_URB_DONE) + { + if (CDC_Handle->TxDataLength > CDC_Handle->DataItf.OutEpSize) + { + CDC_Handle->TxDataLength -= CDC_Handle->DataItf.OutEpSize; + CDC_Handle->pTxData += CDC_Handle->DataItf.OutEpSize; + } + else + { + CDC_Handle->TxDataLength = 0U; + } + + if (CDC_Handle->TxDataLength > 0U) + { + CDC_Handle->data_tx_state = CDC_SEND_DATA; + } + else + { + CDC_Handle->data_tx_state = CDC_IDLE; + USBH_CDC_TransmitCallback(phost); + } + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CLASS_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + else + { + if (URB_Status == USBH_URB_NOTREADY) + { + CDC_Handle->data_tx_state = CDC_SEND_DATA; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CLASS_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + } + break; + + default: + break; + } +} +/** +* @brief This function responsible for reception of data from the device +* @param pdev: Selected device +* @retval None +*/ + +static void CDC_ProcessReception(USBH_HandleTypeDef *phost) +{ + CDC_HandleTypeDef *CDC_Handle = (CDC_HandleTypeDef*) phost->pActiveClass->pData; + USBH_URBStateTypeDef URB_Status = USBH_URB_IDLE; + uint32_t length; + + switch(CDC_Handle->data_rx_state) + { + + case CDC_RECEIVE_DATA: + + USBH_BulkReceiveData (phost, + CDC_Handle->pRxData, + CDC_Handle->DataItf.InEpSize, + CDC_Handle->DataItf.InPipe); + + CDC_Handle->data_rx_state = CDC_RECEIVE_DATA_WAIT; + + break; + + case CDC_RECEIVE_DATA_WAIT: + + URB_Status = USBH_LL_GetURBState(phost, CDC_Handle->DataItf.InPipe); + + /*Check the status done for reception*/ + if(URB_Status == USBH_URB_DONE) + { + length = USBH_LL_GetLastXferSize(phost, CDC_Handle->DataItf.InPipe); + + if(((CDC_Handle->RxDataLength - length) > 0U) && (length > CDC_Handle->DataItf.InEpSize)) + { + CDC_Handle->RxDataLength -= length ; + CDC_Handle->pRxData += length; + CDC_Handle->data_rx_state = CDC_RECEIVE_DATA; + } + else + { + CDC_Handle->data_rx_state = CDC_IDLE; + USBH_CDC_ReceiveCallback(phost); + } + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CLASS_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + break; + + default: + break; + } +} + +/** +* @brief The function informs user that data have been received +* @param pdev: Selected device +* @retval None +*/ +__weak void USBH_CDC_TransmitCallback(USBH_HandleTypeDef *phost) +{ + +} + + /** +* @brief The function informs user that data have been sent +* @param pdev: Selected device +* @retval None +*/ +__weak void USBH_CDC_ReceiveCallback(USBH_HandleTypeDef *phost) +{ + +} + + /** +* @brief The function informs user that Settings have been changed +* @param pdev: Selected device +* @retval None +*/ +__weak void USBH_CDC_LineCodingChanged(USBH_HandleTypeDef *phost) +{ + +} + +/** +* @} +*/ + +/** +* @} +*/ + +/** +* @} +*/ + + +/** +* @} +*/ + + +/** +* @} +*/ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Inc/usbh_hid.h b/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Inc/usbh_hid.h index d150d94161..05574c5953 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Inc/usbh_hid.h +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Inc/usbh_hid.h @@ -1,342 +1,342 @@ -/** - ****************************************************************************** - * @file usbh_hid.h - * @author MCD Application Team - * @brief This file contains all the prototypes for the usbh_hid.c - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive ----------------------------------------------*/ -#ifndef __USBH_HID_H -#define __USBH_HID_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_core.h" -#include "usbh_hid_mouse.h" -#include "usbh_hid_keybd.h" - -/** @addtogroup USBH_LIB - * @{ - */ - -/** @addtogroup USBH_CLASS - * @{ - */ - -/** @addtogroup USBH_HID_CLASS - * @{ - */ - -/** @defgroup USBH_HID_CORE - * @brief This file is the Header file for usbh_hid.c - * @{ - */ - - -/** @defgroup USBH_HID_CORE_Exported_Types - * @{ - */ - -#define HID_MIN_POLL 10U -#define HID_REPORT_SIZE 16U -#define HID_MAX_USAGE 10U -#define HID_MAX_NBR_REPORT_FMT 10U -#define HID_QUEUE_SIZE 10U - -#define HID_ITEM_LONG 0xFEU - -#define HID_ITEM_TYPE_MAIN 0x00U -#define HID_ITEM_TYPE_GLOBAL 0x01U -#define HID_ITEM_TYPE_LOCAL 0x02U -#define HID_ITEM_TYPE_RESERVED 0x03U - - -#define HID_MAIN_ITEM_TAG_INPUT 0x08U -#define HID_MAIN_ITEM_TAG_OUTPUT 0x09U -#define HID_MAIN_ITEM_TAG_COLLECTION 0x0AU -#define HID_MAIN_ITEM_TAG_FEATURE 0x0BU -#define HID_MAIN_ITEM_TAG_ENDCOLLECTION 0x0CU - - -#define HID_GLOBAL_ITEM_TAG_USAGE_PAGE 0x00U -#define HID_GLOBAL_ITEM_TAG_LOG_MIN 0x01U -#define HID_GLOBAL_ITEM_TAG_LOG_MAX 0x02U -#define HID_GLOBAL_ITEM_TAG_PHY_MIN 0x03U -#define HID_GLOBAL_ITEM_TAG_PHY_MAX 0x04U -#define HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT 0x05U -#define HID_GLOBAL_ITEM_TAG_UNIT 0x06U -#define HID_GLOBAL_ITEM_TAG_REPORT_SIZE 0x07U -#define HID_GLOBAL_ITEM_TAG_REPORT_ID 0x08U -#define HID_GLOBAL_ITEM_TAG_REPORT_COUNT 0x09U -#define HID_GLOBAL_ITEM_TAG_PUSH 0x0AU -#define HID_GLOBAL_ITEM_TAG_POP 0x0BU - - -#define HID_LOCAL_ITEM_TAG_USAGE 0x00U -#define HID_LOCAL_ITEM_TAG_USAGE_MIN 0x01U -#define HID_LOCAL_ITEM_TAG_USAGE_MAX 0x02U -#define HID_LOCAL_ITEM_TAG_DESIGNATOR_INDEX 0x03U -#define HID_LOCAL_ITEM_TAG_DESIGNATOR_MIN 0x04U -#define HID_LOCAL_ITEM_TAG_DESIGNATOR_MAX 0x05U -#define HID_LOCAL_ITEM_TAG_STRING_INDEX 0x07U -#define HID_LOCAL_ITEM_TAG_STRING_MIN 0x08U -#define HID_LOCAL_ITEM_TAG_STRING_MAX 0x09U -#define HID_LOCAL_ITEM_TAG_DELIMITER 0x0AU - - -/* States for HID State Machine */ -typedef enum -{ - HID_INIT= 0, - HID_IDLE, - HID_SEND_DATA, - HID_BUSY, - HID_GET_DATA, - HID_SYNC, - HID_POLL, - HID_ERROR, -} -HID_StateTypeDef; - -typedef enum -{ - HID_REQ_INIT = 0, - HID_REQ_IDLE, - HID_REQ_GET_REPORT_DESC, - HID_REQ_GET_HID_DESC, - HID_REQ_SET_IDLE, - HID_REQ_SET_PROTOCOL, - HID_REQ_SET_REPORT, - -} -HID_CtlStateTypeDef; - -typedef enum -{ - HID_MOUSE = 0x01, - HID_KEYBOARD = 0x02, - HID_UNKNOWN = 0xFF, -} -HID_TypeTypeDef; - - -typedef struct _HID_ReportData -{ - uint8_t ReportID; - uint8_t ReportType; - uint16_t UsagePage; - uint32_t Usage[HID_MAX_USAGE]; - uint32_t NbrUsage; - uint32_t UsageMin; - uint32_t UsageMax; - int32_t LogMin; - int32_t LogMax; - int32_t PhyMin; - int32_t PhyMax; - int32_t UnitExp; - uint32_t Unit; - uint32_t ReportSize; - uint32_t ReportCnt; - uint32_t Flag; - uint32_t PhyUsage; - uint32_t AppUsage; - uint32_t LogUsage; -} -HID_ReportDataTypeDef; - -typedef struct _HID_ReportIDTypeDef { - uint8_t Size; /* Report size return by the device id */ - uint8_t ReportID; /* Report Id */ - uint8_t Type; /* Report Type (INPUT/OUTPUT/FEATURE) */ -} HID_ReportIDTypeDef; - -typedef struct _HID_CollectionTypeDef -{ - uint32_t Usage; - uint8_t Type; - struct _HID_CollectionTypeDef *NextPtr; -} HID_CollectionTypeDef; - - -typedef struct _HID_AppCollectionTypeDef { - uint32_t Usage; - uint8_t Type; - uint8_t NbrReportFmt; - HID_ReportDataTypeDef ReportData[HID_MAX_NBR_REPORT_FMT]; -} HID_AppCollectionTypeDef; - - -typedef struct _HIDDescriptor -{ - uint8_t bLength; - uint8_t bDescriptorType; - uint16_t bcdHID; /* indicates what endpoint this descriptor is describing */ - uint8_t bCountryCode; /* specifies the transfer type. */ - uint8_t bNumDescriptors; /* specifies the transfer type. */ - uint8_t bReportDescriptorType; /* Maximum Packet Size this endpoint is capable of sending or receiving */ - uint16_t wItemLength; /* is used to specify the polling interval of certain transfers. */ -} -HID_DescTypeDef; - - -typedef struct -{ - uint8_t *buf; - uint16_t head; - uint16_t tail; - uint16_t size; - uint8_t lock; -} FIFO_TypeDef; - - -/* Structure for HID process */ -typedef struct _HID_Process -{ - uint8_t OutPipe; - uint8_t InPipe; - HID_StateTypeDef state; - uint8_t OutEp; - uint8_t InEp; - HID_CtlStateTypeDef ctl_state; - FIFO_TypeDef fifo; - uint8_t *pData; - uint16_t length; - uint8_t ep_addr; - uint16_t poll; - uint32_t timer; - uint8_t DataReady; - HID_DescTypeDef HID_Desc; - USBH_StatusTypeDef ( * Init)(USBH_HandleTypeDef *phost); -} -HID_HandleTypeDef; - -/** - * @} - */ - -/** @defgroup USBH_HID_CORE_Exported_Defines - * @{ - */ - -#define USB_HID_GET_REPORT 0x01U -#define USB_HID_GET_IDLE 0x02U -#define USB_HID_GET_PROTOCOL 0x03U -#define USB_HID_SET_REPORT 0x09U -#define USB_HID_SET_IDLE 0x0AU -#define USB_HID_SET_PROTOCOL 0x0BU - - - - -/* HID Class Codes */ -#define USB_HID_CLASS 0x03U - -/* Interface Descriptor field values for HID Boot Protocol */ -#define HID_BOOT_CODE 0x01U -#define HID_KEYBRD_BOOT_CODE 0x01U -#define HID_MOUSE_BOOT_CODE 0x02U - - -/** - * @} - */ - -/** @defgroup USBH_HID_CORE_Exported_Macros - * @{ - */ -/** - * @} - */ - -/** @defgroup USBH_HID_CORE_Exported_Variables - * @{ - */ -extern USBH_ClassTypeDef HID_Class; -#define USBH_HID_CLASS &HID_Class -/** - * @} - */ - -/** @defgroup USBH_HID_CORE_Exported_FunctionsPrototype - * @{ - */ - -USBH_StatusTypeDef USBH_HID_SetReport (USBH_HandleTypeDef *phost, - uint8_t reportType, - uint8_t reportId, - uint8_t* reportBuff, - uint8_t reportLen); - -USBH_StatusTypeDef USBH_HID_GetReport (USBH_HandleTypeDef *phost, - uint8_t reportType, - uint8_t reportId, - uint8_t* reportBuff, - uint8_t reportLen); - -USBH_StatusTypeDef USBH_HID_GetHIDReportDescriptor (USBH_HandleTypeDef *phost, - uint16_t length); - -USBH_StatusTypeDef USBH_HID_GetHIDDescriptor (USBH_HandleTypeDef *phost, - uint16_t length); - -USBH_StatusTypeDef USBH_HID_SetIdle (USBH_HandleTypeDef *phost, - uint8_t duration, - uint8_t reportId); - -USBH_StatusTypeDef USBH_HID_SetProtocol (USBH_HandleTypeDef *phost, - uint8_t protocol); - -void USBH_HID_EventCallback(USBH_HandleTypeDef *phost); - -HID_TypeTypeDef USBH_HID_GetDeviceType(USBH_HandleTypeDef *phost); - -uint8_t USBH_HID_GetPollInterval(USBH_HandleTypeDef *phost); - -void USBH_HID_FifoInit(FIFO_TypeDef *f, uint8_t *buf, uint16_t size); - -uint16_t USBH_HID_FifoRead(FIFO_TypeDef *f, void *buf, uint16_t nbytes); - -uint16_t USBH_HID_FifoWrite(FIFO_TypeDef *f, void *buf, uint16_t nbytes); - -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USBH_HID_H */ - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - +/** + ****************************************************************************** + * @file usbh_hid.h + * @author MCD Application Team + * @brief This file contains all the prototypes for the usbh_hid.c + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive ----------------------------------------------*/ +#ifndef __USBH_HID_H +#define __USBH_HID_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_core.h" +#include "usbh_hid_mouse.h" +#include "usbh_hid_keybd.h" + +/** @addtogroup USBH_LIB + * @{ + */ + +/** @addtogroup USBH_CLASS + * @{ + */ + +/** @addtogroup USBH_HID_CLASS + * @{ + */ + +/** @defgroup USBH_HID_CORE + * @brief This file is the Header file for usbh_hid.c + * @{ + */ + + +/** @defgroup USBH_HID_CORE_Exported_Types + * @{ + */ + +#define HID_MIN_POLL 10U +#define HID_REPORT_SIZE 16U +#define HID_MAX_USAGE 10U +#define HID_MAX_NBR_REPORT_FMT 10U +#define HID_QUEUE_SIZE 10U + +#define HID_ITEM_LONG 0xFEU + +#define HID_ITEM_TYPE_MAIN 0x00U +#define HID_ITEM_TYPE_GLOBAL 0x01U +#define HID_ITEM_TYPE_LOCAL 0x02U +#define HID_ITEM_TYPE_RESERVED 0x03U + + +#define HID_MAIN_ITEM_TAG_INPUT 0x08U +#define HID_MAIN_ITEM_TAG_OUTPUT 0x09U +#define HID_MAIN_ITEM_TAG_COLLECTION 0x0AU +#define HID_MAIN_ITEM_TAG_FEATURE 0x0BU +#define HID_MAIN_ITEM_TAG_ENDCOLLECTION 0x0CU + + +#define HID_GLOBAL_ITEM_TAG_USAGE_PAGE 0x00U +#define HID_GLOBAL_ITEM_TAG_LOG_MIN 0x01U +#define HID_GLOBAL_ITEM_TAG_LOG_MAX 0x02U +#define HID_GLOBAL_ITEM_TAG_PHY_MIN 0x03U +#define HID_GLOBAL_ITEM_TAG_PHY_MAX 0x04U +#define HID_GLOBAL_ITEM_TAG_UNIT_EXPONENT 0x05U +#define HID_GLOBAL_ITEM_TAG_UNIT 0x06U +#define HID_GLOBAL_ITEM_TAG_REPORT_SIZE 0x07U +#define HID_GLOBAL_ITEM_TAG_REPORT_ID 0x08U +#define HID_GLOBAL_ITEM_TAG_REPORT_COUNT 0x09U +#define HID_GLOBAL_ITEM_TAG_PUSH 0x0AU +#define HID_GLOBAL_ITEM_TAG_POP 0x0BU + + +#define HID_LOCAL_ITEM_TAG_USAGE 0x00U +#define HID_LOCAL_ITEM_TAG_USAGE_MIN 0x01U +#define HID_LOCAL_ITEM_TAG_USAGE_MAX 0x02U +#define HID_LOCAL_ITEM_TAG_DESIGNATOR_INDEX 0x03U +#define HID_LOCAL_ITEM_TAG_DESIGNATOR_MIN 0x04U +#define HID_LOCAL_ITEM_TAG_DESIGNATOR_MAX 0x05U +#define HID_LOCAL_ITEM_TAG_STRING_INDEX 0x07U +#define HID_LOCAL_ITEM_TAG_STRING_MIN 0x08U +#define HID_LOCAL_ITEM_TAG_STRING_MAX 0x09U +#define HID_LOCAL_ITEM_TAG_DELIMITER 0x0AU + + +/* States for HID State Machine */ +typedef enum +{ + HID_INIT= 0, + HID_IDLE, + HID_SEND_DATA, + HID_BUSY, + HID_GET_DATA, + HID_SYNC, + HID_POLL, + HID_ERROR, +} +HID_StateTypeDef; + +typedef enum +{ + HID_REQ_INIT = 0, + HID_REQ_IDLE, + HID_REQ_GET_REPORT_DESC, + HID_REQ_GET_HID_DESC, + HID_REQ_SET_IDLE, + HID_REQ_SET_PROTOCOL, + HID_REQ_SET_REPORT, + +} +HID_CtlStateTypeDef; + +typedef enum +{ + HID_MOUSE = 0x01, + HID_KEYBOARD = 0x02, + HID_UNKNOWN = 0xFF, +} +HID_TypeTypeDef; + + +typedef struct _HID_ReportData +{ + uint8_t ReportID; + uint8_t ReportType; + uint16_t UsagePage; + uint32_t Usage[HID_MAX_USAGE]; + uint32_t NbrUsage; + uint32_t UsageMin; + uint32_t UsageMax; + int32_t LogMin; + int32_t LogMax; + int32_t PhyMin; + int32_t PhyMax; + int32_t UnitExp; + uint32_t Unit; + uint32_t ReportSize; + uint32_t ReportCnt; + uint32_t Flag; + uint32_t PhyUsage; + uint32_t AppUsage; + uint32_t LogUsage; +} +HID_ReportDataTypeDef; + +typedef struct _HID_ReportIDTypeDef { + uint8_t Size; /* Report size return by the device id */ + uint8_t ReportID; /* Report Id */ + uint8_t Type; /* Report Type (INPUT/OUTPUT/FEATURE) */ +} HID_ReportIDTypeDef; + +typedef struct _HID_CollectionTypeDef +{ + uint32_t Usage; + uint8_t Type; + struct _HID_CollectionTypeDef *NextPtr; +} HID_CollectionTypeDef; + + +typedef struct _HID_AppCollectionTypeDef { + uint32_t Usage; + uint8_t Type; + uint8_t NbrReportFmt; + HID_ReportDataTypeDef ReportData[HID_MAX_NBR_REPORT_FMT]; +} HID_AppCollectionTypeDef; + + +typedef struct _HIDDescriptor +{ + uint8_t bLength; + uint8_t bDescriptorType; + uint16_t bcdHID; /* indicates what endpoint this descriptor is describing */ + uint8_t bCountryCode; /* specifies the transfer type. */ + uint8_t bNumDescriptors; /* specifies the transfer type. */ + uint8_t bReportDescriptorType; /* Maximum Packet Size this endpoint is capable of sending or receiving */ + uint16_t wItemLength; /* is used to specify the polling interval of certain transfers. */ +} +HID_DescTypeDef; + + +typedef struct +{ + uint8_t *buf; + uint16_t head; + uint16_t tail; + uint16_t size; + uint8_t lock; +} FIFO_TypeDef; + + +/* Structure for HID process */ +typedef struct _HID_Process +{ + uint8_t OutPipe; + uint8_t InPipe; + HID_StateTypeDef state; + uint8_t OutEp; + uint8_t InEp; + HID_CtlStateTypeDef ctl_state; + FIFO_TypeDef fifo; + uint8_t *pData; + uint16_t length; + uint8_t ep_addr; + uint16_t poll; + uint32_t timer; + uint8_t DataReady; + HID_DescTypeDef HID_Desc; + USBH_StatusTypeDef ( * Init)(USBH_HandleTypeDef *phost); +} +HID_HandleTypeDef; + +/** + * @} + */ + +/** @defgroup USBH_HID_CORE_Exported_Defines + * @{ + */ + +#define USB_HID_GET_REPORT 0x01U +#define USB_HID_GET_IDLE 0x02U +#define USB_HID_GET_PROTOCOL 0x03U +#define USB_HID_SET_REPORT 0x09U +#define USB_HID_SET_IDLE 0x0AU +#define USB_HID_SET_PROTOCOL 0x0BU + + + + +/* HID Class Codes */ +#define USB_HID_CLASS 0x03U + +/* Interface Descriptor field values for HID Boot Protocol */ +#define HID_BOOT_CODE 0x01U +#define HID_KEYBRD_BOOT_CODE 0x01U +#define HID_MOUSE_BOOT_CODE 0x02U + + +/** + * @} + */ + +/** @defgroup USBH_HID_CORE_Exported_Macros + * @{ + */ +/** + * @} + */ + +/** @defgroup USBH_HID_CORE_Exported_Variables + * @{ + */ +extern USBH_ClassTypeDef HID_Class; +#define USBH_HID_CLASS &HID_Class +/** + * @} + */ + +/** @defgroup USBH_HID_CORE_Exported_FunctionsPrototype + * @{ + */ + +USBH_StatusTypeDef USBH_HID_SetReport (USBH_HandleTypeDef *phost, + uint8_t reportType, + uint8_t reportId, + uint8_t* reportBuff, + uint8_t reportLen); + +USBH_StatusTypeDef USBH_HID_GetReport (USBH_HandleTypeDef *phost, + uint8_t reportType, + uint8_t reportId, + uint8_t* reportBuff, + uint8_t reportLen); + +USBH_StatusTypeDef USBH_HID_GetHIDReportDescriptor (USBH_HandleTypeDef *phost, + uint16_t length); + +USBH_StatusTypeDef USBH_HID_GetHIDDescriptor (USBH_HandleTypeDef *phost, + uint16_t length); + +USBH_StatusTypeDef USBH_HID_SetIdle (USBH_HandleTypeDef *phost, + uint8_t duration, + uint8_t reportId); + +USBH_StatusTypeDef USBH_HID_SetProtocol (USBH_HandleTypeDef *phost, + uint8_t protocol); + +void USBH_HID_EventCallback(USBH_HandleTypeDef *phost); + +HID_TypeTypeDef USBH_HID_GetDeviceType(USBH_HandleTypeDef *phost); + +uint8_t USBH_HID_GetPollInterval(USBH_HandleTypeDef *phost); + +void USBH_HID_FifoInit(FIFO_TypeDef *f, uint8_t *buf, uint16_t size); + +uint16_t USBH_HID_FifoRead(FIFO_TypeDef *f, void *buf, uint16_t nbytes); + +uint16_t USBH_HID_FifoWrite(FIFO_TypeDef *f, void *buf, uint16_t nbytes); + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USBH_HID_H */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Inc/usbh_hid_keybd.h b/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Inc/usbh_hid_keybd.h index 7252aa252e..ee420c0eeb 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Inc/usbh_hid_keybd.h +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Inc/usbh_hid_keybd.h @@ -1,318 +1,318 @@ -/** - ****************************************************************************** - * @file usbh_hid_keybd.h - * @author MCD Application Team - * @brief This file contains all the prototypes for the usbh_hid_keybd.c - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive -----------------------------------------------*/ -#ifndef __USBH_HID_KEYBD_H -#define __USBH_HID_KEYBD_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_hid.h" -#include "usbh_hid_keybd.h" - -/** @addtogroup USBH_LIB - * @{ - */ - -/** @addtogroup USBH_CLASS - * @{ - */ - -/** @addtogroup USBH_HID_CLASS - * @{ - */ - -/** @defgroup USBH_HID_KEYBD - * @brief This file is the Header file for usbh_hid_keybd.c - * @{ - */ - - -/** @defgroup USBH_HID_KEYBD_Exported_Types - * @{ - */ -#define KEY_NONE 0x00 -#define KEY_ERRORROLLOVER 0x01 -#define KEY_POSTFAIL 0x02 -#define KEY_ERRORUNDEFINED 0x03 -#define KEY_A 0x04 -#define KEY_B 0x05 -#define KEY_C 0x06 -#define KEY_D 0x07 -#define KEY_E 0x08 -#define KEY_F 0x09 -#define KEY_G 0x0A -#define KEY_H 0x0B -#define KEY_I 0x0C -#define KEY_J 0x0D -#define KEY_K 0x0E -#define KEY_L 0x0F -#define KEY_M 0x10 -#define KEY_N 0x11 -#define KEY_O 0x12 -#define KEY_P 0x13 -#define KEY_Q 0x14 -#define KEY_R 0x15 -#define KEY_S 0x16 -#define KEY_T 0x17 -#define KEY_U 0x18 -#define KEY_V 0x19 -#define KEY_W 0x1A -#define KEY_X 0x1B -#define KEY_Y 0x1C -#define KEY_Z 0x1D -#define KEY_1_EXCLAMATION_MARK 0x1E -#define KEY_2_AT 0x1F -#define KEY_3_NUMBER_SIGN 0x20 -#define KEY_4_DOLLAR 0x21 -#define KEY_5_PERCENT 0x22 -#define KEY_6_CARET 0x23 -#define KEY_7_AMPERSAND 0x24 -#define KEY_8_ASTERISK 0x25 -#define KEY_9_OPARENTHESIS 0x26 -#define KEY_0_CPARENTHESIS 0x27 -#define KEY_ENTER 0x28 -#define KEY_ESCAPE 0x29 -#define KEY_BACKSPACE 0x2A -#define KEY_TAB 0x2B -#define KEY_SPACEBAR 0x2C -#define KEY_MINUS_UNDERSCORE 0x2D -#define KEY_EQUAL_PLUS 0x2E -#define KEY_OBRACKET_AND_OBRACE 0x2F -#define KEY_CBRACKET_AND_CBRACE 0x30 -#define KEY_BACKSLASH_VERTICAL_BAR 0x31 -#define KEY_NONUS_NUMBER_SIGN_TILDE 0x32 -#define KEY_SEMICOLON_COLON 0x33 -#define KEY_SINGLE_AND_DOUBLE_QUOTE 0x34 -#define KEY_GRAVE ACCENT AND TILDE 0x35 -#define KEY_COMMA_AND_LESS 0x36 -#define KEY_DOT_GREATER 0x37 -#define KEY_SLASH_QUESTION 0x38 -#define KEY_CAPS LOCK 0x39 -#define KEY_F1 0x3A -#define KEY_F2 0x3B -#define KEY_F3 0x3C -#define KEY_F4 0x3D -#define KEY_F5 0x3E -#define KEY_F6 0x3F -#define KEY_F7 0x40 -#define KEY_F8 0x41 -#define KEY_F9 0x42 -#define KEY_F10 0x43 -#define KEY_F11 0x44 -#define KEY_F12 0x45 -#define KEY_PRINTSCREEN 0x46 -#define KEY_SCROLL LOCK 0x47 -#define KEY_PAUSE 0x48 -#define KEY_INSERT 0x49 -#define KEY_HOME 0x4A -#define KEY_PAGEUP 0x4B -#define KEY_DELETE 0x4C -#define KEY_END1 0x4D -#define KEY_PAGEDOWN 0x4E -#define KEY_RIGHTARROW 0x4F -#define KEY_LEFTARROW 0x50 -#define KEY_DOWNARROW 0x51 -#define KEY_UPARROW 0x52 -#define KEY_KEYPAD_NUM_LOCK_AND_CLEAR 0x53 -#define KEY_KEYPAD_SLASH 0x54 -#define KEY_KEYPAD_ASTERIKS 0x55 -#define KEY_KEYPAD_MINUS 0x56 -#define KEY_KEYPAD_PLUS 0x57 -#define KEY_KEYPAD_ENTER 0x58 -#define KEY_KEYPAD_1_END 0x59 -#define KEY_KEYPAD_2_DOWN_ARROW 0x5A -#define KEY_KEYPAD_3_PAGEDN 0x5B -#define KEY_KEYPAD_4_LEFT_ARROW 0x5C -#define KEY_KEYPAD_5 0x5D -#define KEY_KEYPAD_6_RIGHT_ARROW 0x5E -#define KEY_KEYPAD_7_HOME 0x5F -#define KEY_KEYPAD_8_UP_ARROW 0x60 -#define KEY_KEYPAD_9_PAGEUP 0x61 -#define KEY_KEYPAD_0_INSERT 0x62 -#define KEY_KEYPAD_DECIMAL_SEPARATOR_DELETE 0x63 -#define KEY_NONUS_BACK_SLASH_VERTICAL_BAR 0x64 -#define KEY_APPLICATION 0x65 -#define KEY_POWER 0x66 -#define KEY_KEYPAD_EQUAL 0x67 -#define KEY_F13 0x68 -#define KEY_F14 0x69 -#define KEY_F15 0x6A -#define KEY_F16 0x6B -#define KEY_F17 0x6C -#define KEY_F18 0x6D -#define KEY_F19 0x6E -#define KEY_F20 0x6F -#define KEY_F21 0x70 -#define KEY_F22 0x71 -#define KEY_F23 0x72 -#define KEY_F24 0x73 -#define KEY_EXECUTE 0x74 -#define KEY_HELP 0x75 -#define KEY_MENU 0x76 -#define KEY_SELECT 0x77 -#define KEY_STOP 0x78 -#define KEY_AGAIN 0x79 -#define KEY_UNDO 0x7A -#define KEY_CUT 0x7B -#define KEY_COPY 0x7C -#define KEY_PASTE 0x7D -#define KEY_FIND 0x7E -#define KEY_MUTE 0x7F -#define KEY_VOLUME_UP 0x80 -#define KEY_VOLUME_DOWN 0x81 -#define KEY_LOCKING_CAPS_LOCK 0x82 -#define KEY_LOCKING_NUM_LOCK 0x83 -#define KEY_LOCKING_SCROLL_LOCK 0x84 -#define KEY_KEYPAD_COMMA 0x85 -#define KEY_KEYPAD_EQUAL_SIGN 0x86 -#define KEY_INTERNATIONAL1 0x87 -#define KEY_INTERNATIONAL2 0x88 -#define KEY_INTERNATIONAL3 0x89 -#define KEY_INTERNATIONAL4 0x8A -#define KEY_INTERNATIONAL5 0x8B -#define KEY_INTERNATIONAL6 0x8C -#define KEY_INTERNATIONAL7 0x8D -#define KEY_INTERNATIONAL8 0x8E -#define KEY_INTERNATIONAL9 0x8F -#define KEY_LANG1 0x90 -#define KEY_LANG2 0x91 -#define KEY_LANG3 0x92 -#define KEY_LANG4 0x93 -#define KEY_LANG5 0x94 -#define KEY_LANG6 0x95 -#define KEY_LANG7 0x96 -#define KEY_LANG8 0x97 -#define KEY_LANG9 0x98 -#define KEY_ALTERNATE_ERASE 0x99 -#define KEY_SYSREQ 0x9A -#define KEY_CANCEL 0x9B -#define KEY_CLEAR 0x9C -#define KEY_PRIOR 0x9D -#define KEY_RETURN 0x9E -#define KEY_SEPARATOR 0x9F -#define KEY_OUT 0xA0 -#define KEY_OPER 0xA1 -#define KEY_CLEAR_AGAIN 0xA2 -#define KEY_CRSEL 0xA3 -#define KEY_EXSEL 0xA4 -#define KEY_KEYPAD_00 0xB0 -#define KEY_KEYPAD_000 0xB1 -#define KEY_THOUSANDS_SEPARATOR 0xB2 -#define KEY_DECIMAL_SEPARATOR 0xB3 -#define KEY_CURRENCY_UNIT 0xB4 -#define KEY_CURRENCY_SUB_UNIT 0xB5 -#define KEY_KEYPAD_OPARENTHESIS 0xB6 -#define KEY_KEYPAD_CPARENTHESIS 0xB7 -#define KEY_KEYPAD_OBRACE 0xB8 -#define KEY_KEYPAD_CBRACE 0xB9 -#define KEY_KEYPAD_TAB 0xBA -#define KEY_KEYPAD_BACKSPACE 0xBB -#define KEY_KEYPAD_A 0xBC -#define KEY_KEYPAD_B 0xBD -#define KEY_KEYPAD_C 0xBE -#define KEY_KEYPAD_D 0xBF -#define KEY_KEYPAD_E 0xC0 -#define KEY_KEYPAD_F 0xC1 -#define KEY_KEYPAD_XOR 0xC2 -#define KEY_KEYPAD_CARET 0xC3 -#define KEY_KEYPAD_PERCENT 0xC4 -#define KEY_KEYPAD_LESS 0xC5 -#define KEY_KEYPAD_GREATER 0xC6 -#define KEY_KEYPAD_AMPERSAND 0xC7 -#define KEY_KEYPAD_LOGICAL_AND 0xC8 -#define KEY_KEYPAD_VERTICAL_BAR 0xC9 -#define KEY_KEYPAD_LOGIACL_OR 0xCA -#define KEY_KEYPAD_COLON 0xCB -#define KEY_KEYPAD_NUMBER_SIGN 0xCC -#define KEY_KEYPAD_SPACE 0xCD -#define KEY_KEYPAD_AT 0xCE -#define KEY_KEYPAD_EXCLAMATION_MARK 0xCF -#define KEY_KEYPAD_MEMORY_STORE 0xD0 -#define KEY_KEYPAD_MEMORY_RECALL 0xD1 -#define KEY_KEYPAD_MEMORY_CLEAR 0xD2 -#define KEY_KEYPAD_MEMORY_ADD 0xD3 -#define KEY_KEYPAD_MEMORY_SUBTRACT 0xD4 -#define KEY_KEYPAD_MEMORY_MULTIPLY 0xD5 -#define KEY_KEYPAD_MEMORY_DIVIDE 0xD6 -#define KEY_KEYPAD_PLUSMINUS 0xD7 -#define KEY_KEYPAD_CLEAR 0xD8 -#define KEY_KEYPAD_CLEAR_ENTRY 0xD9 -#define KEY_KEYPAD_BINARY 0xDA -#define KEY_KEYPAD_OCTAL 0xDB -#define KEY_KEYPAD_DECIMAL 0xDC -#define KEY_KEYPAD_HEXADECIMAL 0xDD -#define KEY_LEFTCONTROL 0xE0 -#define KEY_LEFTSHIFT 0xE1 -#define KEY_LEFTALT 0xE2 -#define KEY_LEFT_GUI 0xE3 -#define KEY_RIGHTCONTROL 0xE4 -#define KEY_RIGHTSHIFT 0xE5 -#define KEY_RIGHTALT 0xE6 -#define KEY_RIGHT_GUI 0xE7 - -typedef struct -{ - uint8_t state; - uint8_t lctrl; - uint8_t lshift; - uint8_t lalt; - uint8_t lgui; - uint8_t rctrl; - uint8_t rshift; - uint8_t ralt; - uint8_t rgui; - uint8_t keys[6]; -} -HID_KEYBD_Info_TypeDef; - -USBH_StatusTypeDef USBH_HID_KeybdInit(USBH_HandleTypeDef *phost); -HID_KEYBD_Info_TypeDef *USBH_HID_GetKeybdInfo(USBH_HandleTypeDef *phost); -uint8_t USBH_HID_GetASCIICode(HID_KEYBD_Info_TypeDef *info); - -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USBH_HID_KEYBD_H */ - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - +/** + ****************************************************************************** + * @file usbh_hid_keybd.h + * @author MCD Application Team + * @brief This file contains all the prototypes for the usbh_hid_keybd.c + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive -----------------------------------------------*/ +#ifndef __USBH_HID_KEYBD_H +#define __USBH_HID_KEYBD_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_hid.h" +#include "usbh_hid_keybd.h" + +/** @addtogroup USBH_LIB + * @{ + */ + +/** @addtogroup USBH_CLASS + * @{ + */ + +/** @addtogroup USBH_HID_CLASS + * @{ + */ + +/** @defgroup USBH_HID_KEYBD + * @brief This file is the Header file for usbh_hid_keybd.c + * @{ + */ + + +/** @defgroup USBH_HID_KEYBD_Exported_Types + * @{ + */ +#define KEY_NONE 0x00 +#define KEY_ERRORROLLOVER 0x01 +#define KEY_POSTFAIL 0x02 +#define KEY_ERRORUNDEFINED 0x03 +#define KEY_A 0x04 +#define KEY_B 0x05 +#define KEY_C 0x06 +#define KEY_D 0x07 +#define KEY_E 0x08 +#define KEY_F 0x09 +#define KEY_G 0x0A +#define KEY_H 0x0B +#define KEY_I 0x0C +#define KEY_J 0x0D +#define KEY_K 0x0E +#define KEY_L 0x0F +#define KEY_M 0x10 +#define KEY_N 0x11 +#define KEY_O 0x12 +#define KEY_P 0x13 +#define KEY_Q 0x14 +#define KEY_R 0x15 +#define KEY_S 0x16 +#define KEY_T 0x17 +#define KEY_U 0x18 +#define KEY_V 0x19 +#define KEY_W 0x1A +#define KEY_X 0x1B +#define KEY_Y 0x1C +#define KEY_Z 0x1D +#define KEY_1_EXCLAMATION_MARK 0x1E +#define KEY_2_AT 0x1F +#define KEY_3_NUMBER_SIGN 0x20 +#define KEY_4_DOLLAR 0x21 +#define KEY_5_PERCENT 0x22 +#define KEY_6_CARET 0x23 +#define KEY_7_AMPERSAND 0x24 +#define KEY_8_ASTERISK 0x25 +#define KEY_9_OPARENTHESIS 0x26 +#define KEY_0_CPARENTHESIS 0x27 +#define KEY_ENTER 0x28 +#define KEY_ESCAPE 0x29 +#define KEY_BACKSPACE 0x2A +#define KEY_TAB 0x2B +#define KEY_SPACEBAR 0x2C +#define KEY_MINUS_UNDERSCORE 0x2D +#define KEY_EQUAL_PLUS 0x2E +#define KEY_OBRACKET_AND_OBRACE 0x2F +#define KEY_CBRACKET_AND_CBRACE 0x30 +#define KEY_BACKSLASH_VERTICAL_BAR 0x31 +#define KEY_NONUS_NUMBER_SIGN_TILDE 0x32 +#define KEY_SEMICOLON_COLON 0x33 +#define KEY_SINGLE_AND_DOUBLE_QUOTE 0x34 +#define KEY_GRAVE ACCENT AND TILDE 0x35 +#define KEY_COMMA_AND_LESS 0x36 +#define KEY_DOT_GREATER 0x37 +#define KEY_SLASH_QUESTION 0x38 +#define KEY_CAPS LOCK 0x39 +#define KEY_F1 0x3A +#define KEY_F2 0x3B +#define KEY_F3 0x3C +#define KEY_F4 0x3D +#define KEY_F5 0x3E +#define KEY_F6 0x3F +#define KEY_F7 0x40 +#define KEY_F8 0x41 +#define KEY_F9 0x42 +#define KEY_F10 0x43 +#define KEY_F11 0x44 +#define KEY_F12 0x45 +#define KEY_PRINTSCREEN 0x46 +#define KEY_SCROLL LOCK 0x47 +#define KEY_PAUSE 0x48 +#define KEY_INSERT 0x49 +#define KEY_HOME 0x4A +#define KEY_PAGEUP 0x4B +#define KEY_DELETE 0x4C +#define KEY_END1 0x4D +#define KEY_PAGEDOWN 0x4E +#define KEY_RIGHTARROW 0x4F +#define KEY_LEFTARROW 0x50 +#define KEY_DOWNARROW 0x51 +#define KEY_UPARROW 0x52 +#define KEY_KEYPAD_NUM_LOCK_AND_CLEAR 0x53 +#define KEY_KEYPAD_SLASH 0x54 +#define KEY_KEYPAD_ASTERIKS 0x55 +#define KEY_KEYPAD_MINUS 0x56 +#define KEY_KEYPAD_PLUS 0x57 +#define KEY_KEYPAD_ENTER 0x58 +#define KEY_KEYPAD_1_END 0x59 +#define KEY_KEYPAD_2_DOWN_ARROW 0x5A +#define KEY_KEYPAD_3_PAGEDN 0x5B +#define KEY_KEYPAD_4_LEFT_ARROW 0x5C +#define KEY_KEYPAD_5 0x5D +#define KEY_KEYPAD_6_RIGHT_ARROW 0x5E +#define KEY_KEYPAD_7_HOME 0x5F +#define KEY_KEYPAD_8_UP_ARROW 0x60 +#define KEY_KEYPAD_9_PAGEUP 0x61 +#define KEY_KEYPAD_0_INSERT 0x62 +#define KEY_KEYPAD_DECIMAL_SEPARATOR_DELETE 0x63 +#define KEY_NONUS_BACK_SLASH_VERTICAL_BAR 0x64 +#define KEY_APPLICATION 0x65 +#define KEY_POWER 0x66 +#define KEY_KEYPAD_EQUAL 0x67 +#define KEY_F13 0x68 +#define KEY_F14 0x69 +#define KEY_F15 0x6A +#define KEY_F16 0x6B +#define KEY_F17 0x6C +#define KEY_F18 0x6D +#define KEY_F19 0x6E +#define KEY_F20 0x6F +#define KEY_F21 0x70 +#define KEY_F22 0x71 +#define KEY_F23 0x72 +#define KEY_F24 0x73 +#define KEY_EXECUTE 0x74 +#define KEY_HELP 0x75 +#define KEY_MENU 0x76 +#define KEY_SELECT 0x77 +#define KEY_STOP 0x78 +#define KEY_AGAIN 0x79 +#define KEY_UNDO 0x7A +#define KEY_CUT 0x7B +#define KEY_COPY 0x7C +#define KEY_PASTE 0x7D +#define KEY_FIND 0x7E +#define KEY_MUTE 0x7F +#define KEY_VOLUME_UP 0x80 +#define KEY_VOLUME_DOWN 0x81 +#define KEY_LOCKING_CAPS_LOCK 0x82 +#define KEY_LOCKING_NUM_LOCK 0x83 +#define KEY_LOCKING_SCROLL_LOCK 0x84 +#define KEY_KEYPAD_COMMA 0x85 +#define KEY_KEYPAD_EQUAL_SIGN 0x86 +#define KEY_INTERNATIONAL1 0x87 +#define KEY_INTERNATIONAL2 0x88 +#define KEY_INTERNATIONAL3 0x89 +#define KEY_INTERNATIONAL4 0x8A +#define KEY_INTERNATIONAL5 0x8B +#define KEY_INTERNATIONAL6 0x8C +#define KEY_INTERNATIONAL7 0x8D +#define KEY_INTERNATIONAL8 0x8E +#define KEY_INTERNATIONAL9 0x8F +#define KEY_LANG1 0x90 +#define KEY_LANG2 0x91 +#define KEY_LANG3 0x92 +#define KEY_LANG4 0x93 +#define KEY_LANG5 0x94 +#define KEY_LANG6 0x95 +#define KEY_LANG7 0x96 +#define KEY_LANG8 0x97 +#define KEY_LANG9 0x98 +#define KEY_ALTERNATE_ERASE 0x99 +#define KEY_SYSREQ 0x9A +#define KEY_CANCEL 0x9B +#define KEY_CLEAR 0x9C +#define KEY_PRIOR 0x9D +#define KEY_RETURN 0x9E +#define KEY_SEPARATOR 0x9F +#define KEY_OUT 0xA0 +#define KEY_OPER 0xA1 +#define KEY_CLEAR_AGAIN 0xA2 +#define KEY_CRSEL 0xA3 +#define KEY_EXSEL 0xA4 +#define KEY_KEYPAD_00 0xB0 +#define KEY_KEYPAD_000 0xB1 +#define KEY_THOUSANDS_SEPARATOR 0xB2 +#define KEY_DECIMAL_SEPARATOR 0xB3 +#define KEY_CURRENCY_UNIT 0xB4 +#define KEY_CURRENCY_SUB_UNIT 0xB5 +#define KEY_KEYPAD_OPARENTHESIS 0xB6 +#define KEY_KEYPAD_CPARENTHESIS 0xB7 +#define KEY_KEYPAD_OBRACE 0xB8 +#define KEY_KEYPAD_CBRACE 0xB9 +#define KEY_KEYPAD_TAB 0xBA +#define KEY_KEYPAD_BACKSPACE 0xBB +#define KEY_KEYPAD_A 0xBC +#define KEY_KEYPAD_B 0xBD +#define KEY_KEYPAD_C 0xBE +#define KEY_KEYPAD_D 0xBF +#define KEY_KEYPAD_E 0xC0 +#define KEY_KEYPAD_F 0xC1 +#define KEY_KEYPAD_XOR 0xC2 +#define KEY_KEYPAD_CARET 0xC3 +#define KEY_KEYPAD_PERCENT 0xC4 +#define KEY_KEYPAD_LESS 0xC5 +#define KEY_KEYPAD_GREATER 0xC6 +#define KEY_KEYPAD_AMPERSAND 0xC7 +#define KEY_KEYPAD_LOGICAL_AND 0xC8 +#define KEY_KEYPAD_VERTICAL_BAR 0xC9 +#define KEY_KEYPAD_LOGIACL_OR 0xCA +#define KEY_KEYPAD_COLON 0xCB +#define KEY_KEYPAD_NUMBER_SIGN 0xCC +#define KEY_KEYPAD_SPACE 0xCD +#define KEY_KEYPAD_AT 0xCE +#define KEY_KEYPAD_EXCLAMATION_MARK 0xCF +#define KEY_KEYPAD_MEMORY_STORE 0xD0 +#define KEY_KEYPAD_MEMORY_RECALL 0xD1 +#define KEY_KEYPAD_MEMORY_CLEAR 0xD2 +#define KEY_KEYPAD_MEMORY_ADD 0xD3 +#define KEY_KEYPAD_MEMORY_SUBTRACT 0xD4 +#define KEY_KEYPAD_MEMORY_MULTIPLY 0xD5 +#define KEY_KEYPAD_MEMORY_DIVIDE 0xD6 +#define KEY_KEYPAD_PLUSMINUS 0xD7 +#define KEY_KEYPAD_CLEAR 0xD8 +#define KEY_KEYPAD_CLEAR_ENTRY 0xD9 +#define KEY_KEYPAD_BINARY 0xDA +#define KEY_KEYPAD_OCTAL 0xDB +#define KEY_KEYPAD_DECIMAL 0xDC +#define KEY_KEYPAD_HEXADECIMAL 0xDD +#define KEY_LEFTCONTROL 0xE0 +#define KEY_LEFTSHIFT 0xE1 +#define KEY_LEFTALT 0xE2 +#define KEY_LEFT_GUI 0xE3 +#define KEY_RIGHTCONTROL 0xE4 +#define KEY_RIGHTSHIFT 0xE5 +#define KEY_RIGHTALT 0xE6 +#define KEY_RIGHT_GUI 0xE7 + +typedef struct +{ + uint8_t state; + uint8_t lctrl; + uint8_t lshift; + uint8_t lalt; + uint8_t lgui; + uint8_t rctrl; + uint8_t rshift; + uint8_t ralt; + uint8_t rgui; + uint8_t keys[6]; +} +HID_KEYBD_Info_TypeDef; + +USBH_StatusTypeDef USBH_HID_KeybdInit(USBH_HandleTypeDef *phost); +HID_KEYBD_Info_TypeDef *USBH_HID_GetKeybdInfo(USBH_HandleTypeDef *phost); +uint8_t USBH_HID_GetASCIICode(HID_KEYBD_Info_TypeDef *info); + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USBH_HID_KEYBD_H */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Inc/usbh_hid_mouse.h b/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Inc/usbh_hid_mouse.h index 400aed1a39..5044ee0208 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Inc/usbh_hid_mouse.h +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Inc/usbh_hid_mouse.h @@ -1,117 +1,117 @@ -/** - ****************************************************************************** - * @file usbh_hid_mouse.h - * @author MCD Application Team - * @brief This file contains all the prototypes for the usbh_hid_mouse.c - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive ----------------------------------------------*/ -#ifndef __USBH_HID_MOUSE_H -#define __USBH_HID_MOUSE_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_hid.h" - -/** @addtogroup USBH_LIB - * @{ - */ - -/** @addtogroup USBH_CLASS - * @{ - */ - -/** @addtogroup USBH_HID_CLASS - * @{ - */ - -/** @defgroup USBH_HID_MOUSE - * @brief This file is the Header file for usbh_hid_mouse.c - * @{ - */ - - -/** @defgroup USBH_HID_MOUSE_Exported_Types - * @{ - */ - -typedef struct _HID_MOUSE_Info -{ - uint8_t x; - uint8_t y; - uint8_t buttons[3]; -} -HID_MOUSE_Info_TypeDef; - -/** - * @} - */ - -/** @defgroup USBH_HID_MOUSE_Exported_Defines - * @{ - */ -/** - * @} - */ - -/** @defgroup USBH_HID_MOUSE_Exported_Macros - * @{ - */ -/** - * @} - */ - -/** @defgroup USBH_HID_MOUSE_Exported_Variables - * @{ - */ -/** - * @} - */ - -/** @defgroup USBH_HID_MOUSE_Exported_FunctionsPrototype - * @{ - */ -USBH_StatusTypeDef USBH_HID_MouseInit(USBH_HandleTypeDef *phost); -HID_MOUSE_Info_TypeDef *USBH_HID_GetMouseInfo(USBH_HandleTypeDef *phost); - -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USBH_HID_MOUSE_H */ - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbh_hid_mouse.h + * @author MCD Application Team + * @brief This file contains all the prototypes for the usbh_hid_mouse.c + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive ----------------------------------------------*/ +#ifndef __USBH_HID_MOUSE_H +#define __USBH_HID_MOUSE_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_hid.h" + +/** @addtogroup USBH_LIB + * @{ + */ + +/** @addtogroup USBH_CLASS + * @{ + */ + +/** @addtogroup USBH_HID_CLASS + * @{ + */ + +/** @defgroup USBH_HID_MOUSE + * @brief This file is the Header file for usbh_hid_mouse.c + * @{ + */ + + +/** @defgroup USBH_HID_MOUSE_Exported_Types + * @{ + */ + +typedef struct _HID_MOUSE_Info +{ + uint8_t x; + uint8_t y; + uint8_t buttons[3]; +} +HID_MOUSE_Info_TypeDef; + +/** + * @} + */ + +/** @defgroup USBH_HID_MOUSE_Exported_Defines + * @{ + */ +/** + * @} + */ + +/** @defgroup USBH_HID_MOUSE_Exported_Macros + * @{ + */ +/** + * @} + */ + +/** @defgroup USBH_HID_MOUSE_Exported_Variables + * @{ + */ +/** + * @} + */ + +/** @defgroup USBH_HID_MOUSE_Exported_FunctionsPrototype + * @{ + */ +USBH_StatusTypeDef USBH_HID_MouseInit(USBH_HandleTypeDef *phost); +HID_MOUSE_Info_TypeDef *USBH_HID_GetMouseInfo(USBH_HandleTypeDef *phost); + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USBH_HID_MOUSE_H */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Inc/usbh_hid_parser.h b/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Inc/usbh_hid_parser.h index 71da7454a1..8995d5e756 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Inc/usbh_hid_parser.h +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Inc/usbh_hid_parser.h @@ -1,98 +1,98 @@ -/** - ****************************************************************************** - * @file usbh_hid_parser.c - * @author MCD Application Team - * @brief This file is the header file of the usbh_hid_parser.c - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive -----------------------------------------------*/ -#ifndef __USBH_HID_PARSER_H -#define __USBH_HID_PARSER_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_hid.h" -#include "usbh_hid_usage.h" - -/** @addtogroup USBH_LIB - * @{ - */ - -/** @addtogroup USBH_CLASS - * @{ - */ - -/** @addtogroup USBH_HID_CLASS - * @{ - */ - -/** @defgroup USBH_HID_PARSER - * @brief This file is the Header file for usbh_hid_parser.c - * @{ - */ - - -/** @defgroup USBH_HID_PARSER_Exported_Types - * @{ - */ -typedef struct -{ - uint8_t *data; - uint32_t size; - uint8_t shift; - uint8_t count; - uint8_t sign; - uint32_t logical_min; /*min value device can return*/ - uint32_t logical_max; /*max value device can return*/ - uint32_t physical_min; /*min vale read can report*/ - uint32_t physical_max; /*max value read can report*/ - uint32_t resolution; -} -HID_Report_ItemTypedef; - - -uint32_t HID_ReadItem (HID_Report_ItemTypedef *ri, uint8_t ndx); -uint32_t HID_WriteItem(HID_Report_ItemTypedef *ri, uint32_t value, uint8_t ndx); - - -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USBH_HID_PARSER_H */ - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbh_hid_parser.c + * @author MCD Application Team + * @brief This file is the header file of the usbh_hid_parser.c + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive -----------------------------------------------*/ +#ifndef __USBH_HID_PARSER_H +#define __USBH_HID_PARSER_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_hid.h" +#include "usbh_hid_usage.h" + +/** @addtogroup USBH_LIB + * @{ + */ + +/** @addtogroup USBH_CLASS + * @{ + */ + +/** @addtogroup USBH_HID_CLASS + * @{ + */ + +/** @defgroup USBH_HID_PARSER + * @brief This file is the Header file for usbh_hid_parser.c + * @{ + */ + + +/** @defgroup USBH_HID_PARSER_Exported_Types + * @{ + */ +typedef struct +{ + uint8_t *data; + uint32_t size; + uint8_t shift; + uint8_t count; + uint8_t sign; + uint32_t logical_min; /*min value device can return*/ + uint32_t logical_max; /*max value device can return*/ + uint32_t physical_min; /*min vale read can report*/ + uint32_t physical_max; /*max value read can report*/ + uint32_t resolution; +} +HID_Report_ItemTypedef; + + +uint32_t HID_ReadItem (HID_Report_ItemTypedef *ri, uint8_t ndx); +uint32_t HID_WriteItem(HID_Report_ItemTypedef *ri, uint32_t value, uint8_t ndx); + + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USBH_HID_PARSER_H */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Inc/usbh_hid_usage.h b/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Inc/usbh_hid_usage.h index 4c70f3ea14..fa15c64c95 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Inc/usbh_hid_usage.h +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Inc/usbh_hid_usage.h @@ -1,193 +1,193 @@ -/** - ****************************************************************************** - * @file usbh_hid_usage.c - * @author MCD Application Team - * @brief This file contain the USAGE page codes - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive ----------------------------------------------*/ -#ifndef __USDH_HID_USAGE_H -#define __USDH_HID_USAGE_H - -#ifdef __cplusplus - extern "C" { -#endif - -/** @addtogroup USBH_LIB - * @{ - */ - -/** @addtogroup USBH_CLASS - * @{ - */ - -/** @addtogroup USBH_HID_CLASS - * @{ - */ - -/** @defgroup USBH_HID_USAGE - * @brief This file is the Header file for usbh_hid_usage.c - * @{ - */ - - -/** @defgroup USBH_HID_USAGE_Exported_Types - * @{ - */ - -/****************************************************/ -/* HID 1.11 usage pages */ -/****************************************************/ - -#define HID_USAGE_PAGE_UNDEFINED uint16_t (0x00) /* Undefined */ -/**** Top level pages */ -#define HID_USAGE_PAGE_GEN_DES uint16_t (0x01) /* Generic Desktop Controls*/ -#define HID_USAGE_PAGE_SIM_CTR uint16_t (0x02) /* Simulation Controls */ -#define HID_USAGE_PAGE_VR_CTR uint16_t (0x03) /* VR Controls */ -#define HID_USAGE_PAGE_SPORT_CTR uint16_t (0x04) /* Sport Controls */ -#define HID_USAGE_PAGE_GAME_CTR uint16_t (0x05) /* Game Controls */ -#define HID_USAGE_PAGE_GEN_DEV uint16_t (0x06) /* Generic Device Controls */ -#define HID_USAGE_PAGE_KEYB uint16_t (0x07) /* Keyboard/Keypad */ -#define HID_USAGE_PAGE_LED uint16_t (0x08) /* LEDs */ -#define HID_USAGE_PAGE_BUTTON uint16_t (0x09) /* Button */ -#define HID_USAGE_PAGE_ORDINAL uint16_t (0x0A) /* Ordinal */ -#define HID_USAGE_PAGE_PHONE uint16_t (0x0B) /* Telephony */ -#define HID_USAGE_PAGE_CONSUMER uint16_t (0x0C) /* Consumer */ -#define HID_USAGE_PAGE_DIGITIZER uint16_t (0x0D) /* Digitizer*/ -/* 0E Reserved */ -#define HID_USAGE_PAGE_PID uint16_t (0x0F) /* PID Page (force feedback and related devices) */ -#define HID_USAGE_PAGE_UNICODE uint16_t (0x10) /* Unicode */ -/* 11-13 Reserved */ -#define HID_USAGE_PAGE_ALNUM_DISP uint16_t (0x14) /* Alphanumeric Display */ -/* 15-1f Reserved */ -/**** END of top level pages */ -/* 25-3f Reserved */ -#define HID_USAGE_PAGE_MEDICAL uint16_t (0x40) /* Medical Instruments */ -/* 41-7F Reserved */ -/*80-83 Monitor pages USB Device Class Definition for Monitor Devices - 84-87 Power pages USB Device Class Definition for Power Devices */ -/* 88-8B Reserved */ -#define HID_USAGE_PAGE_BARCODE uint16_t (0x8C) /* Bar Code Scanner page */ -#define HID_USAGE_PAGE_SCALE uint16_t (0x8D) /* Scale page */ -#define HID_USAGE_PAGE_MSR uint16_t (0x8E) /* Magnetic Stripe Reading (MSR) Devices */ -#define HID_USAGE_PAGE_POS uint16_t (0x8F) /* Reserved Point of Sale pages */ -#define HID_USAGE_PAGE_CAMERA_CTR uint16_t (0x90) /* Camera Control Page */ -#define HID_USAGE_PAGE_ARCADE uint16_t (0x91) /* Arcade Page */ - -/****************************************************/ -/* Usage definitions for the "Generic Desktop" page */ -/****************************************************/ -#define HID_USAGE_UNDEFINED uint16_t (0x00) /* Undefined */ -#define HID_USAGE_POINTER uint16_t (0x01) /* Pointer (Physical Collection) */ -#define HID_USAGE_MOUSE uint16_t (0x02) /* Mouse (Application Collection) */ -/* 03 Reserved */ -#define HID_USAGE_JOYSTICK uint16_t (0x04) /* Joystick (Application Collection) */ -#define HID_USAGE_GAMEPAD uint16_t (0x05) /* Game Pad (Application Collection) */ -#define HID_USAGE_KBD uint16_t (0x06) /* Keyboard (Application Collection) */ -#define HID_USAGE_KEYPAD uint16_t (0x07) /* Keypad (Application Collection) */ -#define HID_USAGE_MAX_CTR uint16_t (0x08) /* Multi-axis Controller (Application Collection) */ -/* 09-2F Reserved */ -#define HID_USAGE_X uint16_t (0x30) /* X (Dynamic Value) */ -#define HID_USAGE_Y uint16_t (0x31) /* Y (Dynamic Value) */ -#define HID_USAGE_Z uint16_t (0x32) /* Z (Dynamic Value) */ -#define HID_USAGE_RX uint16_t (0x33) /* Rx (Dynamic Value) */ -#define HID_USAGE_RY uint16_t (0x34) /* Ry (Dynamic Value) */ -#define HID_USAGE_RZ uint16_t (0x35) /* Rz (Dynamic Value) */ -#define HID_USAGE_SLIDER uint16_t (0x36) /* Slider (Dynamic Value) */ -#define HID_USAGE_DIAL uint16_t (0x37) /* Dial (Dynamic Value) */ -#define HID_USAGE_WHEEL uint16_t (0x38) /* Wheel (Dynamic Value) */ -#define HID_USAGE_HATSW uint16_t (0x39) /* Hat switch (Dynamic Value) */ -#define HID_USAGE_COUNTEDBUF uint16_t (0x3A) /* Counted Buffer (Logical Collection) */ -#define HID_USAGE_BYTECOUNT uint16_t (0x3B) /* Byte Count (Dynamic Value) */ -#define HID_USAGE_MOTIONWAKE uint16_t (0x3C) /* Motion Wakeup (One Shot Control) */ -#define HID_USAGE_START uint16_t (0x3D) /* Start (On/Off Control) */ -#define HID_USAGE_SELECT uint16_t (0x3E) /* Select (On/Off Control) */ -/* 3F Reserved */ -#define HID_USAGE_VX uint16_t (0x40) /* Vx (Dynamic Value) */ -#define HID_USAGE_VY uint16_t (0x41) /* Vy (Dynamic Value) */ -#define HID_USAGE_VZ uint16_t (0x42) /* Vz (Dynamic Value) */ -#define HID_USAGE_VBRX uint16_t (0x43) /* Vbrx (Dynamic Value) */ -#define HID_USAGE_VBRY uint16_t (0x44) /* Vbry (Dynamic Value) */ -#define HID_USAGE_VBRZ uint16_t (0x45) /* Vbrz (Dynamic Value) */ -#define HID_USAGE_VNO uint16_t (0x46) /* Vno (Dynamic Value) */ -#define HID_USAGE_FEATNOTIF uint16_t (0x47) /* Feature Notification (Dynamic Value),(Dynamic Flag) */ -/* 48-7F Reserved */ -#define HID_USAGE_SYSCTL uint16_t (0x80) /* System Control (Application Collection) */ -#define HID_USAGE_PWDOWN uint16_t (0x81) /* System Power Down (One Shot Control) */ -#define HID_USAGE_SLEEP uint16_t (0x82) /* System Sleep (One Shot Control) */ -#define HID_USAGE_WAKEUP uint16_t (0x83) /* System Wake Up (One Shot Control) */ -#define HID_USAGE_CONTEXTM uint16_t (0x84) /* System Context Menu (One Shot Control) */ -#define HID_USAGE_MAINM uint16_t (0x85) /* System Main Menu (One Shot Control) */ -#define HID_USAGE_APPM uint16_t (0x86) /* System App Menu (One Shot Control) */ -#define HID_USAGE_MENUHELP uint16_t (0x87) /* System Menu Help (One Shot Control) */ -#define HID_USAGE_MENUEXIT uint16_t (0x88) /* System Menu Exit (One Shot Control) */ -#define HID_USAGE_MENUSELECT uint16_t (0x89) /* System Menu Select (One Shot Control) */ -#define HID_USAGE_SYSM_RIGHT uint16_t (0x8A) /* System Menu Right (Re-Trigger Control) */ -#define HID_USAGE_SYSM_LEFT uint16_t (0x8B) /* System Menu Left (Re-Trigger Control) */ -#define HID_USAGE_SYSM_UP uint16_t (0x8C) /* System Menu Up (Re-Trigger Control) */ -#define HID_USAGE_SYSM_DOWN uint16_t (0x8D) /* System Menu Down (Re-Trigger Control) */ -#define HID_USAGE_COLDRESET uint16_t (0x8E) /* System Cold Restart (One Shot Control) */ -#define HID_USAGE_WARMRESET uint16_t (0x8F) /* System Warm Restart (One Shot Control) */ -#define HID_USAGE_DUP uint16_t (0x90) /* D-pad Up (On/Off Control) */ -#define HID_USAGE_DDOWN uint16_t (0x91) /* D-pad Down (On/Off Control) */ -#define HID_USAGE_DRIGHT uint16_t (0x92) /* D-pad Right (On/Off Control) */ -#define HID_USAGE_DLEFT uint16_t (0x93) /* D-pad Left (On/Off Control) */ -/* 94-9F Reserved */ -#define HID_USAGE_SYS_DOCK uint16_t (0xA0) /* System Dock (One Shot Control) */ -#define HID_USAGE_SYS_UNDOCK uint16_t (0xA1) /* System Undock (One Shot Control) */ -#define HID_USAGE_SYS_SETUP uint16_t (0xA2) /* System Setup (One Shot Control) */ -#define HID_USAGE_SYS_BREAK uint16_t (0xA3) /* System Break (One Shot Control) */ -#define HID_USAGE_SYS_DBGBRK uint16_t (0xA4) /* System Debugger Break (One Shot Control) */ -#define HID_USAGE_APP_BRK uint16_t (0xA5) /* Application Break (One Shot Control) */ -#define HID_USAGE_APP_DBGBRK uint16_t (0xA6) /* Application Debugger Break (One Shot Control) */ -#define HID_USAGE_SYS_SPKMUTE uint16_t (0xA7) /* System Speaker Mute (One Shot Control) */ -#define HID_USAGE_SYS_HIBERN uint16_t (0xA8) /* System Hibernate (One Shot Control) */ -/* A9-AF Reserved */ -#define HID_USAGE_SYS_SIDPINV uint16_t (0xB0) /* System Display Invert (One Shot Control) */ -#define HID_USAGE_SYS_DISPINT uint16_t (0xB1) /* System Display Internal (One Shot Control) */ -#define HID_USAGE_SYS_DISPEXT uint16_t (0xB2) /* System Display External (One Shot Control) */ -#define HID_USAGE_SYS_DISPBOTH uint16_t (0xB3) /* System Display Both (One Shot Control) */ -#define HID_USAGE_SYS_DISPDUAL uint16_t (0xB4) /* System Display Dual (One Shot Control) */ -#define HID_USAGE_SYS_DISPTGLIE uint16_t (0xB5) /* System Display Toggle Int/Ext (One Shot Control) */ -#define HID_USAGE_SYS_DISP_SWAP uint16_t (0xB6) /* System Display Swap Primary/Secondary (One Shot Control) */ -#define HID_USAGE_SYS_DIPS_LCDA uint16_t (0xB7) /* System Display LCD Autoscale (One Shot Control) */ -/* B8-FFFF Reserved */ - -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USDH_HID_USAGE_H */ - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbh_hid_usage.c + * @author MCD Application Team + * @brief This file contain the USAGE page codes + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive ----------------------------------------------*/ +#ifndef __USDH_HID_USAGE_H +#define __USDH_HID_USAGE_H + +#ifdef __cplusplus + extern "C" { +#endif + +/** @addtogroup USBH_LIB + * @{ + */ + +/** @addtogroup USBH_CLASS + * @{ + */ + +/** @addtogroup USBH_HID_CLASS + * @{ + */ + +/** @defgroup USBH_HID_USAGE + * @brief This file is the Header file for usbh_hid_usage.c + * @{ + */ + + +/** @defgroup USBH_HID_USAGE_Exported_Types + * @{ + */ + +/****************************************************/ +/* HID 1.11 usage pages */ +/****************************************************/ + +#define HID_USAGE_PAGE_UNDEFINED uint16_t (0x00) /* Undefined */ +/**** Top level pages */ +#define HID_USAGE_PAGE_GEN_DES uint16_t (0x01) /* Generic Desktop Controls*/ +#define HID_USAGE_PAGE_SIM_CTR uint16_t (0x02) /* Simulation Controls */ +#define HID_USAGE_PAGE_VR_CTR uint16_t (0x03) /* VR Controls */ +#define HID_USAGE_PAGE_SPORT_CTR uint16_t (0x04) /* Sport Controls */ +#define HID_USAGE_PAGE_GAME_CTR uint16_t (0x05) /* Game Controls */ +#define HID_USAGE_PAGE_GEN_DEV uint16_t (0x06) /* Generic Device Controls */ +#define HID_USAGE_PAGE_KEYB uint16_t (0x07) /* Keyboard/Keypad */ +#define HID_USAGE_PAGE_LED uint16_t (0x08) /* LEDs */ +#define HID_USAGE_PAGE_BUTTON uint16_t (0x09) /* Button */ +#define HID_USAGE_PAGE_ORDINAL uint16_t (0x0A) /* Ordinal */ +#define HID_USAGE_PAGE_PHONE uint16_t (0x0B) /* Telephony */ +#define HID_USAGE_PAGE_CONSUMER uint16_t (0x0C) /* Consumer */ +#define HID_USAGE_PAGE_DIGITIZER uint16_t (0x0D) /* Digitizer*/ +/* 0E Reserved */ +#define HID_USAGE_PAGE_PID uint16_t (0x0F) /* PID Page (force feedback and related devices) */ +#define HID_USAGE_PAGE_UNICODE uint16_t (0x10) /* Unicode */ +/* 11-13 Reserved */ +#define HID_USAGE_PAGE_ALNUM_DISP uint16_t (0x14) /* Alphanumeric Display */ +/* 15-1f Reserved */ +/**** END of top level pages */ +/* 25-3f Reserved */ +#define HID_USAGE_PAGE_MEDICAL uint16_t (0x40) /* Medical Instruments */ +/* 41-7F Reserved */ +/*80-83 Monitor pages USB Device Class Definition for Monitor Devices + 84-87 Power pages USB Device Class Definition for Power Devices */ +/* 88-8B Reserved */ +#define HID_USAGE_PAGE_BARCODE uint16_t (0x8C) /* Bar Code Scanner page */ +#define HID_USAGE_PAGE_SCALE uint16_t (0x8D) /* Scale page */ +#define HID_USAGE_PAGE_MSR uint16_t (0x8E) /* Magnetic Stripe Reading (MSR) Devices */ +#define HID_USAGE_PAGE_POS uint16_t (0x8F) /* Reserved Point of Sale pages */ +#define HID_USAGE_PAGE_CAMERA_CTR uint16_t (0x90) /* Camera Control Page */ +#define HID_USAGE_PAGE_ARCADE uint16_t (0x91) /* Arcade Page */ + +/****************************************************/ +/* Usage definitions for the "Generic Desktop" page */ +/****************************************************/ +#define HID_USAGE_UNDEFINED uint16_t (0x00) /* Undefined */ +#define HID_USAGE_POINTER uint16_t (0x01) /* Pointer (Physical Collection) */ +#define HID_USAGE_MOUSE uint16_t (0x02) /* Mouse (Application Collection) */ +/* 03 Reserved */ +#define HID_USAGE_JOYSTICK uint16_t (0x04) /* Joystick (Application Collection) */ +#define HID_USAGE_GAMEPAD uint16_t (0x05) /* Game Pad (Application Collection) */ +#define HID_USAGE_KBD uint16_t (0x06) /* Keyboard (Application Collection) */ +#define HID_USAGE_KEYPAD uint16_t (0x07) /* Keypad (Application Collection) */ +#define HID_USAGE_MAX_CTR uint16_t (0x08) /* Multi-axis Controller (Application Collection) */ +/* 09-2F Reserved */ +#define HID_USAGE_X uint16_t (0x30) /* X (Dynamic Value) */ +#define HID_USAGE_Y uint16_t (0x31) /* Y (Dynamic Value) */ +#define HID_USAGE_Z uint16_t (0x32) /* Z (Dynamic Value) */ +#define HID_USAGE_RX uint16_t (0x33) /* Rx (Dynamic Value) */ +#define HID_USAGE_RY uint16_t (0x34) /* Ry (Dynamic Value) */ +#define HID_USAGE_RZ uint16_t (0x35) /* Rz (Dynamic Value) */ +#define HID_USAGE_SLIDER uint16_t (0x36) /* Slider (Dynamic Value) */ +#define HID_USAGE_DIAL uint16_t (0x37) /* Dial (Dynamic Value) */ +#define HID_USAGE_WHEEL uint16_t (0x38) /* Wheel (Dynamic Value) */ +#define HID_USAGE_HATSW uint16_t (0x39) /* Hat switch (Dynamic Value) */ +#define HID_USAGE_COUNTEDBUF uint16_t (0x3A) /* Counted Buffer (Logical Collection) */ +#define HID_USAGE_BYTECOUNT uint16_t (0x3B) /* Byte Count (Dynamic Value) */ +#define HID_USAGE_MOTIONWAKE uint16_t (0x3C) /* Motion Wakeup (One Shot Control) */ +#define HID_USAGE_START uint16_t (0x3D) /* Start (On/Off Control) */ +#define HID_USAGE_SELECT uint16_t (0x3E) /* Select (On/Off Control) */ +/* 3F Reserved */ +#define HID_USAGE_VX uint16_t (0x40) /* Vx (Dynamic Value) */ +#define HID_USAGE_VY uint16_t (0x41) /* Vy (Dynamic Value) */ +#define HID_USAGE_VZ uint16_t (0x42) /* Vz (Dynamic Value) */ +#define HID_USAGE_VBRX uint16_t (0x43) /* Vbrx (Dynamic Value) */ +#define HID_USAGE_VBRY uint16_t (0x44) /* Vbry (Dynamic Value) */ +#define HID_USAGE_VBRZ uint16_t (0x45) /* Vbrz (Dynamic Value) */ +#define HID_USAGE_VNO uint16_t (0x46) /* Vno (Dynamic Value) */ +#define HID_USAGE_FEATNOTIF uint16_t (0x47) /* Feature Notification (Dynamic Value),(Dynamic Flag) */ +/* 48-7F Reserved */ +#define HID_USAGE_SYSCTL uint16_t (0x80) /* System Control (Application Collection) */ +#define HID_USAGE_PWDOWN uint16_t (0x81) /* System Power Down (One Shot Control) */ +#define HID_USAGE_SLEEP uint16_t (0x82) /* System Sleep (One Shot Control) */ +#define HID_USAGE_WAKEUP uint16_t (0x83) /* System Wake Up (One Shot Control) */ +#define HID_USAGE_CONTEXTM uint16_t (0x84) /* System Context Menu (One Shot Control) */ +#define HID_USAGE_MAINM uint16_t (0x85) /* System Main Menu (One Shot Control) */ +#define HID_USAGE_APPM uint16_t (0x86) /* System App Menu (One Shot Control) */ +#define HID_USAGE_MENUHELP uint16_t (0x87) /* System Menu Help (One Shot Control) */ +#define HID_USAGE_MENUEXIT uint16_t (0x88) /* System Menu Exit (One Shot Control) */ +#define HID_USAGE_MENUSELECT uint16_t (0x89) /* System Menu Select (One Shot Control) */ +#define HID_USAGE_SYSM_RIGHT uint16_t (0x8A) /* System Menu Right (Re-Trigger Control) */ +#define HID_USAGE_SYSM_LEFT uint16_t (0x8B) /* System Menu Left (Re-Trigger Control) */ +#define HID_USAGE_SYSM_UP uint16_t (0x8C) /* System Menu Up (Re-Trigger Control) */ +#define HID_USAGE_SYSM_DOWN uint16_t (0x8D) /* System Menu Down (Re-Trigger Control) */ +#define HID_USAGE_COLDRESET uint16_t (0x8E) /* System Cold Restart (One Shot Control) */ +#define HID_USAGE_WARMRESET uint16_t (0x8F) /* System Warm Restart (One Shot Control) */ +#define HID_USAGE_DUP uint16_t (0x90) /* D-pad Up (On/Off Control) */ +#define HID_USAGE_DDOWN uint16_t (0x91) /* D-pad Down (On/Off Control) */ +#define HID_USAGE_DRIGHT uint16_t (0x92) /* D-pad Right (On/Off Control) */ +#define HID_USAGE_DLEFT uint16_t (0x93) /* D-pad Left (On/Off Control) */ +/* 94-9F Reserved */ +#define HID_USAGE_SYS_DOCK uint16_t (0xA0) /* System Dock (One Shot Control) */ +#define HID_USAGE_SYS_UNDOCK uint16_t (0xA1) /* System Undock (One Shot Control) */ +#define HID_USAGE_SYS_SETUP uint16_t (0xA2) /* System Setup (One Shot Control) */ +#define HID_USAGE_SYS_BREAK uint16_t (0xA3) /* System Break (One Shot Control) */ +#define HID_USAGE_SYS_DBGBRK uint16_t (0xA4) /* System Debugger Break (One Shot Control) */ +#define HID_USAGE_APP_BRK uint16_t (0xA5) /* Application Break (One Shot Control) */ +#define HID_USAGE_APP_DBGBRK uint16_t (0xA6) /* Application Debugger Break (One Shot Control) */ +#define HID_USAGE_SYS_SPKMUTE uint16_t (0xA7) /* System Speaker Mute (One Shot Control) */ +#define HID_USAGE_SYS_HIBERN uint16_t (0xA8) /* System Hibernate (One Shot Control) */ +/* A9-AF Reserved */ +#define HID_USAGE_SYS_SIDPINV uint16_t (0xB0) /* System Display Invert (One Shot Control) */ +#define HID_USAGE_SYS_DISPINT uint16_t (0xB1) /* System Display Internal (One Shot Control) */ +#define HID_USAGE_SYS_DISPEXT uint16_t (0xB2) /* System Display External (One Shot Control) */ +#define HID_USAGE_SYS_DISPBOTH uint16_t (0xB3) /* System Display Both (One Shot Control) */ +#define HID_USAGE_SYS_DISPDUAL uint16_t (0xB4) /* System Display Dual (One Shot Control) */ +#define HID_USAGE_SYS_DISPTGLIE uint16_t (0xB5) /* System Display Toggle Int/Ext (One Shot Control) */ +#define HID_USAGE_SYS_DISP_SWAP uint16_t (0xB6) /* System Display Swap Primary/Secondary (One Shot Control) */ +#define HID_USAGE_SYS_DIPS_LCDA uint16_t (0xB7) /* System Display LCD Autoscale (One Shot Control) */ +/* B8-FFFF Reserved */ + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USDH_HID_USAGE_H */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Src/usbh_hid.c b/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Src/usbh_hid.c index 501d5bdcf4..236af3cc4b 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Src/usbh_hid.c +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Src/usbh_hid.c @@ -1,878 +1,878 @@ -/** - ****************************************************************************** - * @file usbh_hid.c - * @author MCD Application Team - * @brief This file is the HID Layer Handlers for USB Host HID class. - * - * @verbatim - * - * =================================================================== - * HID Class Description - * =================================================================== - * This module manages the HID class V1.11 following the "Device Class Definition - * for Human Interface Devices (HID) Version 1.11 Jun 27, 2001". - * This driver implements the following aspects of the specification: - * - The Boot Interface Subclass - * - The Mouse and Keyboard protocols - * - * @endverbatim - * - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - - /* BSPDependencies - - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" - - "stm32xxxxx_{eval}{discovery}_io.c" - - "stm32xxxxx_{eval}{discovery}{adafruit}_lcd.c" - - "stm32xxxxx_{eval}{discovery}_sdram.c" - EndBSPDependencies */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_hid.h" -#include "usbh_hid_parser.h" - - -/** @addtogroup USBH_LIB -* @{ -*/ - -/** @addtogroup USBH_CLASS -* @{ -*/ - -/** @addtogroup USBH_HID_CLASS -* @{ -*/ - -/** @defgroup USBH_HID_CORE -* @brief This file includes HID Layer Handlers for USB Host HID class. -* @{ -*/ - -/** @defgroup USBH_HID_CORE_Private_TypesDefinitions -* @{ -*/ -/** -* @} -*/ - - -/** @defgroup USBH_HID_CORE_Private_Defines -* @{ -*/ -/** -* @} -*/ - - -/** @defgroup USBH_HID_CORE_Private_Macros -* @{ -*/ -/** -* @} -*/ - - -/** @defgroup USBH_HID_CORE_Private_Variables -* @{ -*/ - -/** -* @} -*/ - - -/** @defgroup USBH_HID_CORE_Private_FunctionPrototypes -* @{ -*/ - -static USBH_StatusTypeDef USBH_HID_InterfaceInit (USBH_HandleTypeDef *phost); -static USBH_StatusTypeDef USBH_HID_InterfaceDeInit (USBH_HandleTypeDef *phost); -static USBH_StatusTypeDef USBH_HID_ClassRequest(USBH_HandleTypeDef *phost); -static USBH_StatusTypeDef USBH_HID_Process(USBH_HandleTypeDef *phost); -static USBH_StatusTypeDef USBH_HID_SOFProcess(USBH_HandleTypeDef *phost); -static void USBH_HID_ParseHIDDesc (HID_DescTypeDef *desc, uint8_t *buf); - -extern USBH_StatusTypeDef USBH_HID_MouseInit(USBH_HandleTypeDef *phost); -extern USBH_StatusTypeDef USBH_HID_KeybdInit(USBH_HandleTypeDef *phost); - -USBH_ClassTypeDef HID_Class = -{ - "HID", - USB_HID_CLASS, - USBH_HID_InterfaceInit, - USBH_HID_InterfaceDeInit, - USBH_HID_ClassRequest, - USBH_HID_Process, - USBH_HID_SOFProcess, - NULL, -}; -/** -* @} -*/ - - -/** @defgroup USBH_HID_CORE_Private_Functions -* @{ -*/ - - -/** - * @brief USBH_HID_InterfaceInit - * The function init the HID class. - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_HID_InterfaceInit (USBH_HandleTypeDef *phost) -{ - uint8_t max_ep; - uint8_t num = 0U; - uint8_t interface; - - USBH_StatusTypeDef status = USBH_FAIL; - HID_HandleTypeDef *HID_Handle; - - interface = USBH_FindInterface(phost, phost->pActiveClass->ClassCode, HID_BOOT_CODE, 0xFFU); - - if(interface == 0xFFU) /* No Valid Interface */ - { - status = USBH_FAIL; - USBH_DbgLog ("Cannot Find the interface for %s class.", phost->pActiveClass->Name); - } - else - { - USBH_SelectInterface (phost, interface); - phost->pActiveClass->pData = (HID_HandleTypeDef *)USBH_malloc (sizeof(HID_HandleTypeDef)); - HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData; - HID_Handle->state = HID_ERROR; - - /*Decode Bootclass Protocol: Mouse or Keyboard*/ - if(phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].bInterfaceProtocol == HID_KEYBRD_BOOT_CODE) - { - USBH_UsrLog ("KeyBoard device found!"); - HID_Handle->Init = USBH_HID_KeybdInit; - } - else if(phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].bInterfaceProtocol == HID_MOUSE_BOOT_CODE) - { - USBH_UsrLog ("Mouse device found!"); - HID_Handle->Init = USBH_HID_MouseInit; - } - else - { - USBH_UsrLog ("Protocol not supported."); - return USBH_FAIL; - } - - HID_Handle->state = HID_INIT; - HID_Handle->ctl_state = HID_REQ_INIT; - HID_Handle->ep_addr = phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[0].bEndpointAddress; - HID_Handle->length = phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[0].wMaxPacketSize; - HID_Handle->poll = phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[0].bInterval ; - - if (HID_Handle->poll < HID_MIN_POLL) - { - HID_Handle->poll = HID_MIN_POLL; - } - - /* Check fo available number of endpoints */ - /* Find the number of EPs in the Interface Descriptor */ - /* Choose the lower number in order not to overrun the buffer allocated */ - max_ep = ( (phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].bNumEndpoints <= USBH_MAX_NUM_ENDPOINTS) ? - phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].bNumEndpoints : - USBH_MAX_NUM_ENDPOINTS); - - - /* Decode endpoint IN and OUT address from interface descriptor */ - for ( ;num < max_ep; num++) - { - if(phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[num].bEndpointAddress & 0x80U) - { - HID_Handle->InEp = (phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[num].bEndpointAddress); - HID_Handle->InPipe =\ - USBH_AllocPipe(phost, HID_Handle->InEp); - - /* Open pipe for IN endpoint */ - USBH_OpenPipe (phost, - HID_Handle->InPipe, - HID_Handle->InEp, - phost->device.address, - phost->device.speed, - USB_EP_TYPE_INTR, - HID_Handle->length); - - USBH_LL_SetToggle (phost, HID_Handle->InPipe, 0U); - - } - else - { - HID_Handle->OutEp = (phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[num].bEndpointAddress); - HID_Handle->OutPipe =\ - USBH_AllocPipe(phost, HID_Handle->OutEp); - - /* Open pipe for OUT endpoint */ - USBH_OpenPipe (phost, - HID_Handle->OutPipe, - HID_Handle->OutEp, - phost->device.address, - phost->device.speed, - USB_EP_TYPE_INTR, - HID_Handle->length); - - USBH_LL_SetToggle (phost, HID_Handle->OutPipe, 0U); - } - - } - status = USBH_OK; - } - return status; -} - -/** - * @brief USBH_HID_InterfaceDeInit - * The function DeInit the Pipes used for the HID class. - * @param phost: Host handle - * @retval USBH Status - */ -USBH_StatusTypeDef static USBH_HID_InterfaceDeInit (USBH_HandleTypeDef *phost) -{ - HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData; - - if(HID_Handle->InPipe != 0x00U) - { - USBH_ClosePipe (phost, HID_Handle->InPipe); - USBH_FreePipe (phost, HID_Handle->InPipe); - HID_Handle->InPipe = 0U; /* Reset the pipe as Free */ - } - - if(HID_Handle->OutPipe != 0x00U) - { - USBH_ClosePipe(phost, HID_Handle->OutPipe); - USBH_FreePipe (phost, HID_Handle->OutPipe); - HID_Handle->OutPipe = 0U; /* Reset the pipe as Free */ - } - - if(phost->pActiveClass->pData) - { - USBH_free (phost->pActiveClass->pData); - } - - return USBH_OK; -} - -/** - * @brief USBH_HID_ClassRequest - * The function is responsible for handling Standard requests - * for HID class. - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_HID_ClassRequest(USBH_HandleTypeDef *phost) -{ - - USBH_StatusTypeDef status = USBH_BUSY; - USBH_StatusTypeDef classReqStatus = USBH_BUSY; - HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData; - - /* Switch HID state machine */ - switch (HID_Handle->ctl_state) - { - case HID_REQ_INIT: - case HID_REQ_GET_HID_DESC: - - /* Get HID Desc */ - if (USBH_HID_GetHIDDescriptor (phost, USB_HID_DESC_SIZE)== USBH_OK) - { - - USBH_HID_ParseHIDDesc(&HID_Handle->HID_Desc, phost->device.Data); - HID_Handle->ctl_state = HID_REQ_GET_REPORT_DESC; - } - - break; - case HID_REQ_GET_REPORT_DESC: - - - /* Get Report Desc */ - if (USBH_HID_GetHIDReportDescriptor(phost, HID_Handle->HID_Desc.wItemLength) == USBH_OK) - { - /* The descriptor is available in phost->device.Data */ - - HID_Handle->ctl_state = HID_REQ_SET_IDLE; - } - - break; - - case HID_REQ_SET_IDLE: - - classReqStatus = USBH_HID_SetIdle (phost, 0U, 0U); - - /* set Idle */ - if (classReqStatus == USBH_OK) - { - HID_Handle->ctl_state = HID_REQ_SET_PROTOCOL; - } - else - { - if(classReqStatus == USBH_NOT_SUPPORTED) - { - HID_Handle->ctl_state = HID_REQ_SET_PROTOCOL; - } - } - break; - - case HID_REQ_SET_PROTOCOL: - /* set protocol */ - if (USBH_HID_SetProtocol (phost, 0U) == USBH_OK) - { - HID_Handle->ctl_state = HID_REQ_IDLE; - - /* all requests performed*/ - phost->pUser(phost, HOST_USER_CLASS_ACTIVE); - status = USBH_OK; - } - break; - - case HID_REQ_IDLE: - default: - break; - } - - return status; -} - -/** - * @brief USBH_HID_Process - * The function is for managing state machine for HID data transfers - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_HID_Process(USBH_HandleTypeDef *phost) -{ - USBH_StatusTypeDef status = USBH_OK; - HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData; - - switch (HID_Handle->state) - { - case HID_INIT: - HID_Handle->Init(phost); - HID_Handle->state = HID_IDLE; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - case HID_IDLE: - status = USBH_HID_GetReport (phost, 0x01U, 0U, HID_Handle->pData, (uint8_t)HID_Handle->length); - if (status == USBH_OK) - { - USBH_HID_FifoWrite(&HID_Handle->fifo, HID_Handle->pData, HID_Handle->length); - HID_Handle->state = HID_SYNC; - } - else if (status == USBH_BUSY) - { - HID_Handle->state = HID_IDLE; - status = USBH_OK; - } - else if (status == USBH_NOT_SUPPORTED) - { - HID_Handle->state = HID_SYNC; - status = USBH_OK; - } - else - { - HID_Handle->state = HID_ERROR; - status = USBH_FAIL; - } - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - case HID_SYNC: - /* Sync with start of Even Frame */ - if(phost->Timer & 1U) - { - HID_Handle->state = HID_GET_DATA; - } - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - case HID_GET_DATA: - USBH_InterruptReceiveData(phost, HID_Handle->pData, - (uint8_t)HID_Handle->length, - HID_Handle->InPipe); - - HID_Handle->state = HID_POLL; - HID_Handle->timer = phost->Timer; - HID_Handle->DataReady = 0U; - break; - - case HID_POLL: - if(USBH_LL_GetURBState(phost , HID_Handle->InPipe) == USBH_URB_DONE) - { - if(HID_Handle->DataReady == 0U) - { - USBH_HID_FifoWrite(&HID_Handle->fifo, HID_Handle->pData, HID_Handle->length); - HID_Handle->DataReady = 1U; - USBH_HID_EventCallback(phost); - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - } - else - { - /* IN Endpoint Stalled */ - if(USBH_LL_GetURBState(phost, HID_Handle->InPipe) == USBH_URB_STALL) - { - /* Issue Clear Feature on interrupt IN endpoint */ - if(USBH_ClrFeature(phost, HID_Handle->ep_addr) == USBH_OK) - { - /* Change state to issue next IN token */ - HID_Handle->state = HID_GET_DATA; - } - } - } - break; - - default: - break; - } - - return status; -} - -/** - * @brief USBH_HID_SOFProcess - * The function is for managing the SOF Process - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_HID_SOFProcess(USBH_HandleTypeDef *phost) -{ - HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData; - - if(HID_Handle->state == HID_POLL) - { - if(( phost->Timer - HID_Handle->timer) >= HID_Handle->poll) - { - HID_Handle->state = HID_GET_DATA; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - } - return USBH_OK; -} - -/** -* @brief USBH_Get_HID_ReportDescriptor - * Issue report Descriptor command to the device. Once the response - * received, parse the report descriptor and update the status. - * @param phost: Host handle - * @param Length : HID Report Descriptor Length - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_HID_GetHIDReportDescriptor (USBH_HandleTypeDef *phost, - uint16_t length) -{ - - USBH_StatusTypeDef status; - - status = USBH_GetDescriptor(phost, - USB_REQ_RECIPIENT_INTERFACE | USB_REQ_TYPE_STANDARD, - USB_DESC_HID_REPORT, - phost->device.Data, - length); - - /* HID report descriptor is available in phost->device.Data. - In case of USB Boot Mode devices for In report handling , - HID report descriptor parsing is not required. - In case, for supporting Non-Boot Protocol devices and output reports, - user may parse the report descriptor*/ - - - return status; -} - -/** - * @brief USBH_Get_HID_Descriptor - * Issue HID Descriptor command to the device. Once the response - * received, parse the report descriptor and update the status. - * @param phost: Host handle - * @param Length : HID Descriptor Length - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_HID_GetHIDDescriptor (USBH_HandleTypeDef *phost, - uint16_t length) -{ - USBH_StatusTypeDef status; - - status = USBH_GetDescriptor(phost, - USB_REQ_RECIPIENT_INTERFACE | USB_REQ_TYPE_STANDARD, - USB_DESC_HID, - phost->device.Data, - length); - - return status; -} - -/** - * @brief USBH_Set_Idle - * Set Idle State. - * @param phost: Host handle - * @param duration: Duration for HID Idle request - * @param reportId : Targeted report ID for Set Idle request - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_HID_SetIdle (USBH_HandleTypeDef *phost, - uint8_t duration, - uint8_t reportId) -{ - - phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_INTERFACE |\ - USB_REQ_TYPE_CLASS; - - - phost->Control.setup.b.bRequest = USB_HID_SET_IDLE; - phost->Control.setup.b.wValue.w = (uint16_t)(((uint32_t)duration << 8U) | (uint32_t)reportId); - - phost->Control.setup.b.wIndex.w = 0U; - phost->Control.setup.b.wLength.w = 0U; - - return USBH_CtlReq(phost, 0U, 0U); -} - - -/** - * @brief USBH_HID_Set_Report - * Issues Set Report - * @param phost: Host handle - * @param reportType : Report type to be sent - * @param reportId : Targeted report ID for Set Report request - * @param reportBuff : Report Buffer - * @param reportLen : Length of data report to be send - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_HID_SetReport (USBH_HandleTypeDef *phost, - uint8_t reportType, - uint8_t reportId, - uint8_t* reportBuff, - uint8_t reportLen) -{ - - phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_INTERFACE |\ - USB_REQ_TYPE_CLASS; - - - phost->Control.setup.b.bRequest = USB_HID_SET_REPORT; - phost->Control.setup.b.wValue.w = (uint16_t)(((uint32_t)reportType << 8U) | (uint32_t)reportId); - - phost->Control.setup.b.wIndex.w = 0U; - phost->Control.setup.b.wLength.w = reportLen; - - return USBH_CtlReq(phost, reportBuff, (uint16_t)reportLen); -} - - -/** - * @brief USBH_HID_GetReport - * retreive Set Report - * @param phost: Host handle - * @param reportType : Report type to be sent - * @param reportId : Targeted report ID for Set Report request - * @param reportBuff : Report Buffer - * @param reportLen : Length of data report to be send - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_HID_GetReport (USBH_HandleTypeDef *phost, - uint8_t reportType, - uint8_t reportId, - uint8_t* reportBuff, - uint8_t reportLen) -{ - - phost->Control.setup.b.bmRequestType = USB_D2H | USB_REQ_RECIPIENT_INTERFACE |\ - USB_REQ_TYPE_CLASS; - - - phost->Control.setup.b.bRequest = USB_HID_GET_REPORT; - phost->Control.setup.b.wValue.w = (uint16_t)(((uint32_t)reportType << 8U) | (uint32_t)reportId); - - phost->Control.setup.b.wIndex.w = 0U; - phost->Control.setup.b.wLength.w = reportLen; - - return USBH_CtlReq(phost, reportBuff, (uint16_t)reportLen); -} - -/** - * @brief USBH_Set_Protocol - * Set protocol State. - * @param phost: Host handle - * @param protocol : Set Protocol for HID : boot/report protocol - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_HID_SetProtocol(USBH_HandleTypeDef *phost, - uint8_t protocol) -{ - phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_INTERFACE - | USB_REQ_TYPE_CLASS; - - phost->Control.setup.b.bRequest = USB_HID_SET_PROTOCOL; - if(protocol) - { - phost->Control.setup.b.wValue.w = 0U; - } - else - { - phost->Control.setup.b.wValue.w = 1U; - } - - phost->Control.setup.b.wIndex.w = 0U; - phost->Control.setup.b.wLength.w = 0U; - - return USBH_CtlReq(phost, 0U, 0U); - -} - -/** - * @brief USBH_ParseHIDDesc - * This function Parse the HID descriptor - * @param desc: HID Descriptor - * @param buf: Buffer where the source descriptor is available - * @retval None - */ -static void USBH_HID_ParseHIDDesc (HID_DescTypeDef *desc, uint8_t *buf) -{ - - desc->bLength = *(uint8_t *) (buf + 0); - desc->bDescriptorType = *(uint8_t *) (buf + 1); - desc->bcdHID = LE16 (buf + 2); - desc->bCountryCode = *(uint8_t *) (buf + 4); - desc->bNumDescriptors = *(uint8_t *) (buf + 5); - desc->bReportDescriptorType = *(uint8_t *) (buf + 6); - desc->wItemLength = LE16 (buf + 7); -} - -/** - * @brief USBH_HID_GetDeviceType - * Return Device function. - * @param phost: Host handle - * @retval HID function: HID_MOUSE / HID_KEYBOARD - */ -HID_TypeTypeDef USBH_HID_GetDeviceType(USBH_HandleTypeDef *phost) -{ - HID_TypeTypeDef type = HID_UNKNOWN; - uint8_t InterfaceProtocol; - - if(phost->gState == HOST_CLASS) - { - InterfaceProtocol = phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].bInterfaceProtocol; - if(InterfaceProtocol == HID_KEYBRD_BOOT_CODE) - { - type = HID_KEYBOARD; - } - else - { - if(InterfaceProtocol == HID_MOUSE_BOOT_CODE) - { - type = HID_MOUSE; - } - } - } - return type; -} - - -/** - * @brief USBH_HID_GetPollInterval - * Return HID device poll time - * @param phost: Host handle - * @retval poll time (ms) - */ -uint8_t USBH_HID_GetPollInterval(USBH_HandleTypeDef *phost) -{ - HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData; - - if((phost->gState == HOST_CLASS_REQUEST) || - (phost->gState == HOST_INPUT) || - (phost->gState == HOST_SET_CONFIGURATION) || - (phost->gState == HOST_CHECK_CLASS) || - ((phost->gState == HOST_CLASS))) - { - return (uint8_t)(HID_Handle->poll); - } - else - { - return 0U; - } -} -/** - * @brief USBH_HID_FifoInit - * Initialize FIFO. - * @param f: Fifo address - * @param buf: Fifo buffer - * @param size: Fifo Size - * @retval none - */ -void USBH_HID_FifoInit(FIFO_TypeDef * f, uint8_t * buf, uint16_t size) -{ - f->head = 0U; - f->tail = 0U; - f->lock = 0U; - f->size = size; - f->buf = buf; -} - -/** - * @brief USBH_HID_FifoRead - * Read from FIFO. - * @param f: Fifo address - * @param buf: read buffer - * @param nbytes: number of item to read - * @retval number of read items - */ -uint16_t USBH_HID_FifoRead(FIFO_TypeDef *f, void *buf, uint16_t nbytes) -{ - uint16_t i; - uint8_t * p; - p = (uint8_t*) buf; - - if(f->lock == 0U) - { - f->lock = 1U; - for(i = 0U; i < nbytes; i++) - { - if( f->tail != f->head) - { - *p++ = f->buf[f->tail]; - f->tail++; - if( f->tail == f->size) - { - f->tail = 0U; - } - } else - { - f->lock = 0U; - return i; - } - } - } - f->lock = 0U; - - return nbytes; -} - -/** - * @brief USBH_HID_FifoWrite - * Read from FIFO. - * @param f: Fifo address - * @param buf: read buffer - * @param nbytes: number of item to write - * @retval number of written items - */ -uint16_t USBH_HID_FifoWrite(FIFO_TypeDef *f, void *buf, uint16_t nbytes) -{ - uint16_t i; - uint8_t *p; - p = (uint8_t*) buf; - if(f->lock == 0U) - { - f->lock = 1U; - for(i = 0U; i < nbytes; i++) - { - if( (f->head + 1U == f->tail) || - ( (f->head + 1U == f->size) && (f->tail == 0U))) - { - f->lock = 0U; - return i; - } - else - { - f->buf[f->head] = *p++; - f->head++; - if( f->head == f->size) - { - f->head = 0U; - } - } - } - } - f->lock = 0U; - return nbytes; -} - -/** -* @brief The function is a callback about HID Data events -* @param phost: Selected device -* @retval None -*/ -__weak void USBH_HID_EventCallback(USBH_HandleTypeDef *phost) -{ - -} -/** -* @} -*/ - -/** -* @} -*/ - -/** -* @} -*/ - - -/** -* @} -*/ - - -/** -* @} -*/ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbh_hid.c + * @author MCD Application Team + * @brief This file is the HID Layer Handlers for USB Host HID class. + * + * @verbatim + * + * =================================================================== + * HID Class Description + * =================================================================== + * This module manages the HID class V1.11 following the "Device Class Definition + * for Human Interface Devices (HID) Version 1.11 Jun 27, 2001". + * This driver implements the following aspects of the specification: + * - The Boot Interface Subclass + * - The Mouse and Keyboard protocols + * + * @endverbatim + * + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + + /* BSPDependencies + - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" + - "stm32xxxxx_{eval}{discovery}_io.c" + - "stm32xxxxx_{eval}{discovery}{adafruit}_lcd.c" + - "stm32xxxxx_{eval}{discovery}_sdram.c" + EndBSPDependencies */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_hid.h" +#include "usbh_hid_parser.h" + + +/** @addtogroup USBH_LIB +* @{ +*/ + +/** @addtogroup USBH_CLASS +* @{ +*/ + +/** @addtogroup USBH_HID_CLASS +* @{ +*/ + +/** @defgroup USBH_HID_CORE +* @brief This file includes HID Layer Handlers for USB Host HID class. +* @{ +*/ + +/** @defgroup USBH_HID_CORE_Private_TypesDefinitions +* @{ +*/ +/** +* @} +*/ + + +/** @defgroup USBH_HID_CORE_Private_Defines +* @{ +*/ +/** +* @} +*/ + + +/** @defgroup USBH_HID_CORE_Private_Macros +* @{ +*/ +/** +* @} +*/ + + +/** @defgroup USBH_HID_CORE_Private_Variables +* @{ +*/ + +/** +* @} +*/ + + +/** @defgroup USBH_HID_CORE_Private_FunctionPrototypes +* @{ +*/ + +static USBH_StatusTypeDef USBH_HID_InterfaceInit (USBH_HandleTypeDef *phost); +static USBH_StatusTypeDef USBH_HID_InterfaceDeInit (USBH_HandleTypeDef *phost); +static USBH_StatusTypeDef USBH_HID_ClassRequest(USBH_HandleTypeDef *phost); +static USBH_StatusTypeDef USBH_HID_Process(USBH_HandleTypeDef *phost); +static USBH_StatusTypeDef USBH_HID_SOFProcess(USBH_HandleTypeDef *phost); +static void USBH_HID_ParseHIDDesc (HID_DescTypeDef *desc, uint8_t *buf); + +extern USBH_StatusTypeDef USBH_HID_MouseInit(USBH_HandleTypeDef *phost); +extern USBH_StatusTypeDef USBH_HID_KeybdInit(USBH_HandleTypeDef *phost); + +USBH_ClassTypeDef HID_Class = +{ + "HID", + USB_HID_CLASS, + USBH_HID_InterfaceInit, + USBH_HID_InterfaceDeInit, + USBH_HID_ClassRequest, + USBH_HID_Process, + USBH_HID_SOFProcess, + NULL, +}; +/** +* @} +*/ + + +/** @defgroup USBH_HID_CORE_Private_Functions +* @{ +*/ + + +/** + * @brief USBH_HID_InterfaceInit + * The function init the HID class. + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_HID_InterfaceInit (USBH_HandleTypeDef *phost) +{ + uint8_t max_ep; + uint8_t num = 0U; + uint8_t interface; + + USBH_StatusTypeDef status = USBH_FAIL; + HID_HandleTypeDef *HID_Handle; + + interface = USBH_FindInterface(phost, phost->pActiveClass->ClassCode, HID_BOOT_CODE, 0xFFU); + + if(interface == 0xFFU) /* No Valid Interface */ + { + status = USBH_FAIL; + USBH_DbgLog ("Cannot Find the interface for %s class.", phost->pActiveClass->Name); + } + else + { + USBH_SelectInterface (phost, interface); + phost->pActiveClass->pData = (HID_HandleTypeDef *)USBH_malloc (sizeof(HID_HandleTypeDef)); + HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData; + HID_Handle->state = HID_ERROR; + + /*Decode Bootclass Protocol: Mouse or Keyboard*/ + if(phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].bInterfaceProtocol == HID_KEYBRD_BOOT_CODE) + { + USBH_UsrLog ("KeyBoard device found!"); + HID_Handle->Init = USBH_HID_KeybdInit; + } + else if(phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].bInterfaceProtocol == HID_MOUSE_BOOT_CODE) + { + USBH_UsrLog ("Mouse device found!"); + HID_Handle->Init = USBH_HID_MouseInit; + } + else + { + USBH_UsrLog ("Protocol not supported."); + return USBH_FAIL; + } + + HID_Handle->state = HID_INIT; + HID_Handle->ctl_state = HID_REQ_INIT; + HID_Handle->ep_addr = phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[0].bEndpointAddress; + HID_Handle->length = phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[0].wMaxPacketSize; + HID_Handle->poll = phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[0].bInterval ; + + if (HID_Handle->poll < HID_MIN_POLL) + { + HID_Handle->poll = HID_MIN_POLL; + } + + /* Check fo available number of endpoints */ + /* Find the number of EPs in the Interface Descriptor */ + /* Choose the lower number in order not to overrun the buffer allocated */ + max_ep = ( (phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].bNumEndpoints <= USBH_MAX_NUM_ENDPOINTS) ? + phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].bNumEndpoints : + USBH_MAX_NUM_ENDPOINTS); + + + /* Decode endpoint IN and OUT address from interface descriptor */ + for ( ;num < max_ep; num++) + { + if(phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[num].bEndpointAddress & 0x80U) + { + HID_Handle->InEp = (phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[num].bEndpointAddress); + HID_Handle->InPipe =\ + USBH_AllocPipe(phost, HID_Handle->InEp); + + /* Open pipe for IN endpoint */ + USBH_OpenPipe (phost, + HID_Handle->InPipe, + HID_Handle->InEp, + phost->device.address, + phost->device.speed, + USB_EP_TYPE_INTR, + HID_Handle->length); + + USBH_LL_SetToggle (phost, HID_Handle->InPipe, 0U); + + } + else + { + HID_Handle->OutEp = (phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[num].bEndpointAddress); + HID_Handle->OutPipe =\ + USBH_AllocPipe(phost, HID_Handle->OutEp); + + /* Open pipe for OUT endpoint */ + USBH_OpenPipe (phost, + HID_Handle->OutPipe, + HID_Handle->OutEp, + phost->device.address, + phost->device.speed, + USB_EP_TYPE_INTR, + HID_Handle->length); + + USBH_LL_SetToggle (phost, HID_Handle->OutPipe, 0U); + } + + } + status = USBH_OK; + } + return status; +} + +/** + * @brief USBH_HID_InterfaceDeInit + * The function DeInit the Pipes used for the HID class. + * @param phost: Host handle + * @retval USBH Status + */ +USBH_StatusTypeDef static USBH_HID_InterfaceDeInit (USBH_HandleTypeDef *phost) +{ + HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData; + + if(HID_Handle->InPipe != 0x00U) + { + USBH_ClosePipe (phost, HID_Handle->InPipe); + USBH_FreePipe (phost, HID_Handle->InPipe); + HID_Handle->InPipe = 0U; /* Reset the pipe as Free */ + } + + if(HID_Handle->OutPipe != 0x00U) + { + USBH_ClosePipe(phost, HID_Handle->OutPipe); + USBH_FreePipe (phost, HID_Handle->OutPipe); + HID_Handle->OutPipe = 0U; /* Reset the pipe as Free */ + } + + if(phost->pActiveClass->pData) + { + USBH_free (phost->pActiveClass->pData); + } + + return USBH_OK; +} + +/** + * @brief USBH_HID_ClassRequest + * The function is responsible for handling Standard requests + * for HID class. + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_HID_ClassRequest(USBH_HandleTypeDef *phost) +{ + + USBH_StatusTypeDef status = USBH_BUSY; + USBH_StatusTypeDef classReqStatus = USBH_BUSY; + HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData; + + /* Switch HID state machine */ + switch (HID_Handle->ctl_state) + { + case HID_REQ_INIT: + case HID_REQ_GET_HID_DESC: + + /* Get HID Desc */ + if (USBH_HID_GetHIDDescriptor (phost, USB_HID_DESC_SIZE)== USBH_OK) + { + + USBH_HID_ParseHIDDesc(&HID_Handle->HID_Desc, phost->device.Data); + HID_Handle->ctl_state = HID_REQ_GET_REPORT_DESC; + } + + break; + case HID_REQ_GET_REPORT_DESC: + + + /* Get Report Desc */ + if (USBH_HID_GetHIDReportDescriptor(phost, HID_Handle->HID_Desc.wItemLength) == USBH_OK) + { + /* The descriptor is available in phost->device.Data */ + + HID_Handle->ctl_state = HID_REQ_SET_IDLE; + } + + break; + + case HID_REQ_SET_IDLE: + + classReqStatus = USBH_HID_SetIdle (phost, 0U, 0U); + + /* set Idle */ + if (classReqStatus == USBH_OK) + { + HID_Handle->ctl_state = HID_REQ_SET_PROTOCOL; + } + else + { + if(classReqStatus == USBH_NOT_SUPPORTED) + { + HID_Handle->ctl_state = HID_REQ_SET_PROTOCOL; + } + } + break; + + case HID_REQ_SET_PROTOCOL: + /* set protocol */ + if (USBH_HID_SetProtocol (phost, 0U) == USBH_OK) + { + HID_Handle->ctl_state = HID_REQ_IDLE; + + /* all requests performed*/ + phost->pUser(phost, HOST_USER_CLASS_ACTIVE); + status = USBH_OK; + } + break; + + case HID_REQ_IDLE: + default: + break; + } + + return status; +} + +/** + * @brief USBH_HID_Process + * The function is for managing state machine for HID data transfers + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_HID_Process(USBH_HandleTypeDef *phost) +{ + USBH_StatusTypeDef status = USBH_OK; + HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData; + + switch (HID_Handle->state) + { + case HID_INIT: + HID_Handle->Init(phost); + HID_Handle->state = HID_IDLE; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + case HID_IDLE: + status = USBH_HID_GetReport (phost, 0x01U, 0U, HID_Handle->pData, (uint8_t)HID_Handle->length); + if (status == USBH_OK) + { + USBH_HID_FifoWrite(&HID_Handle->fifo, HID_Handle->pData, HID_Handle->length); + HID_Handle->state = HID_SYNC; + } + else if (status == USBH_BUSY) + { + HID_Handle->state = HID_IDLE; + status = USBH_OK; + } + else if (status == USBH_NOT_SUPPORTED) + { + HID_Handle->state = HID_SYNC; + status = USBH_OK; + } + else + { + HID_Handle->state = HID_ERROR; + status = USBH_FAIL; + } + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + case HID_SYNC: + /* Sync with start of Even Frame */ + if(phost->Timer & 1U) + { + HID_Handle->state = HID_GET_DATA; + } + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + case HID_GET_DATA: + USBH_InterruptReceiveData(phost, HID_Handle->pData, + (uint8_t)HID_Handle->length, + HID_Handle->InPipe); + + HID_Handle->state = HID_POLL; + HID_Handle->timer = phost->Timer; + HID_Handle->DataReady = 0U; + break; + + case HID_POLL: + if(USBH_LL_GetURBState(phost , HID_Handle->InPipe) == USBH_URB_DONE) + { + if(HID_Handle->DataReady == 0U) + { + USBH_HID_FifoWrite(&HID_Handle->fifo, HID_Handle->pData, HID_Handle->length); + HID_Handle->DataReady = 1U; + USBH_HID_EventCallback(phost); + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + } + else + { + /* IN Endpoint Stalled */ + if(USBH_LL_GetURBState(phost, HID_Handle->InPipe) == USBH_URB_STALL) + { + /* Issue Clear Feature on interrupt IN endpoint */ + if(USBH_ClrFeature(phost, HID_Handle->ep_addr) == USBH_OK) + { + /* Change state to issue next IN token */ + HID_Handle->state = HID_GET_DATA; + } + } + } + break; + + default: + break; + } + + return status; +} + +/** + * @brief USBH_HID_SOFProcess + * The function is for managing the SOF Process + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_HID_SOFProcess(USBH_HandleTypeDef *phost) +{ + HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData; + + if(HID_Handle->state == HID_POLL) + { + if(( phost->Timer - HID_Handle->timer) >= HID_Handle->poll) + { + HID_Handle->state = HID_GET_DATA; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + } + return USBH_OK; +} + +/** +* @brief USBH_Get_HID_ReportDescriptor + * Issue report Descriptor command to the device. Once the response + * received, parse the report descriptor and update the status. + * @param phost: Host handle + * @param Length : HID Report Descriptor Length + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_HID_GetHIDReportDescriptor (USBH_HandleTypeDef *phost, + uint16_t length) +{ + + USBH_StatusTypeDef status; + + status = USBH_GetDescriptor(phost, + USB_REQ_RECIPIENT_INTERFACE | USB_REQ_TYPE_STANDARD, + USB_DESC_HID_REPORT, + phost->device.Data, + length); + + /* HID report descriptor is available in phost->device.Data. + In case of USB Boot Mode devices for In report handling , + HID report descriptor parsing is not required. + In case, for supporting Non-Boot Protocol devices and output reports, + user may parse the report descriptor*/ + + + return status; +} + +/** + * @brief USBH_Get_HID_Descriptor + * Issue HID Descriptor command to the device. Once the response + * received, parse the report descriptor and update the status. + * @param phost: Host handle + * @param Length : HID Descriptor Length + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_HID_GetHIDDescriptor (USBH_HandleTypeDef *phost, + uint16_t length) +{ + USBH_StatusTypeDef status; + + status = USBH_GetDescriptor(phost, + USB_REQ_RECIPIENT_INTERFACE | USB_REQ_TYPE_STANDARD, + USB_DESC_HID, + phost->device.Data, + length); + + return status; +} + +/** + * @brief USBH_Set_Idle + * Set Idle State. + * @param phost: Host handle + * @param duration: Duration for HID Idle request + * @param reportId : Targeted report ID for Set Idle request + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_HID_SetIdle (USBH_HandleTypeDef *phost, + uint8_t duration, + uint8_t reportId) +{ + + phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_INTERFACE |\ + USB_REQ_TYPE_CLASS; + + + phost->Control.setup.b.bRequest = USB_HID_SET_IDLE; + phost->Control.setup.b.wValue.w = (uint16_t)(((uint32_t)duration << 8U) | (uint32_t)reportId); + + phost->Control.setup.b.wIndex.w = 0U; + phost->Control.setup.b.wLength.w = 0U; + + return USBH_CtlReq(phost, 0U, 0U); +} + + +/** + * @brief USBH_HID_Set_Report + * Issues Set Report + * @param phost: Host handle + * @param reportType : Report type to be sent + * @param reportId : Targeted report ID for Set Report request + * @param reportBuff : Report Buffer + * @param reportLen : Length of data report to be send + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_HID_SetReport (USBH_HandleTypeDef *phost, + uint8_t reportType, + uint8_t reportId, + uint8_t* reportBuff, + uint8_t reportLen) +{ + + phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_INTERFACE |\ + USB_REQ_TYPE_CLASS; + + + phost->Control.setup.b.bRequest = USB_HID_SET_REPORT; + phost->Control.setup.b.wValue.w = (uint16_t)(((uint32_t)reportType << 8U) | (uint32_t)reportId); + + phost->Control.setup.b.wIndex.w = 0U; + phost->Control.setup.b.wLength.w = reportLen; + + return USBH_CtlReq(phost, reportBuff, (uint16_t)reportLen); +} + + +/** + * @brief USBH_HID_GetReport + * retreive Set Report + * @param phost: Host handle + * @param reportType : Report type to be sent + * @param reportId : Targeted report ID for Set Report request + * @param reportBuff : Report Buffer + * @param reportLen : Length of data report to be send + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_HID_GetReport (USBH_HandleTypeDef *phost, + uint8_t reportType, + uint8_t reportId, + uint8_t* reportBuff, + uint8_t reportLen) +{ + + phost->Control.setup.b.bmRequestType = USB_D2H | USB_REQ_RECIPIENT_INTERFACE |\ + USB_REQ_TYPE_CLASS; + + + phost->Control.setup.b.bRequest = USB_HID_GET_REPORT; + phost->Control.setup.b.wValue.w = (uint16_t)(((uint32_t)reportType << 8U) | (uint32_t)reportId); + + phost->Control.setup.b.wIndex.w = 0U; + phost->Control.setup.b.wLength.w = reportLen; + + return USBH_CtlReq(phost, reportBuff, (uint16_t)reportLen); +} + +/** + * @brief USBH_Set_Protocol + * Set protocol State. + * @param phost: Host handle + * @param protocol : Set Protocol for HID : boot/report protocol + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_HID_SetProtocol(USBH_HandleTypeDef *phost, + uint8_t protocol) +{ + phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_INTERFACE + | USB_REQ_TYPE_CLASS; + + phost->Control.setup.b.bRequest = USB_HID_SET_PROTOCOL; + if(protocol) + { + phost->Control.setup.b.wValue.w = 0U; + } + else + { + phost->Control.setup.b.wValue.w = 1U; + } + + phost->Control.setup.b.wIndex.w = 0U; + phost->Control.setup.b.wLength.w = 0U; + + return USBH_CtlReq(phost, 0U, 0U); + +} + +/** + * @brief USBH_ParseHIDDesc + * This function Parse the HID descriptor + * @param desc: HID Descriptor + * @param buf: Buffer where the source descriptor is available + * @retval None + */ +static void USBH_HID_ParseHIDDesc (HID_DescTypeDef *desc, uint8_t *buf) +{ + + desc->bLength = *(uint8_t *) (buf + 0); + desc->bDescriptorType = *(uint8_t *) (buf + 1); + desc->bcdHID = LE16 (buf + 2); + desc->bCountryCode = *(uint8_t *) (buf + 4); + desc->bNumDescriptors = *(uint8_t *) (buf + 5); + desc->bReportDescriptorType = *(uint8_t *) (buf + 6); + desc->wItemLength = LE16 (buf + 7); +} + +/** + * @brief USBH_HID_GetDeviceType + * Return Device function. + * @param phost: Host handle + * @retval HID function: HID_MOUSE / HID_KEYBOARD + */ +HID_TypeTypeDef USBH_HID_GetDeviceType(USBH_HandleTypeDef *phost) +{ + HID_TypeTypeDef type = HID_UNKNOWN; + uint8_t InterfaceProtocol; + + if(phost->gState == HOST_CLASS) + { + InterfaceProtocol = phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].bInterfaceProtocol; + if(InterfaceProtocol == HID_KEYBRD_BOOT_CODE) + { + type = HID_KEYBOARD; + } + else + { + if(InterfaceProtocol == HID_MOUSE_BOOT_CODE) + { + type = HID_MOUSE; + } + } + } + return type; +} + + +/** + * @brief USBH_HID_GetPollInterval + * Return HID device poll time + * @param phost: Host handle + * @retval poll time (ms) + */ +uint8_t USBH_HID_GetPollInterval(USBH_HandleTypeDef *phost) +{ + HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData; + + if((phost->gState == HOST_CLASS_REQUEST) || + (phost->gState == HOST_INPUT) || + (phost->gState == HOST_SET_CONFIGURATION) || + (phost->gState == HOST_CHECK_CLASS) || + ((phost->gState == HOST_CLASS))) + { + return (uint8_t)(HID_Handle->poll); + } + else + { + return 0U; + } +} +/** + * @brief USBH_HID_FifoInit + * Initialize FIFO. + * @param f: Fifo address + * @param buf: Fifo buffer + * @param size: Fifo Size + * @retval none + */ +void USBH_HID_FifoInit(FIFO_TypeDef * f, uint8_t * buf, uint16_t size) +{ + f->head = 0U; + f->tail = 0U; + f->lock = 0U; + f->size = size; + f->buf = buf; +} + +/** + * @brief USBH_HID_FifoRead + * Read from FIFO. + * @param f: Fifo address + * @param buf: read buffer + * @param nbytes: number of item to read + * @retval number of read items + */ +uint16_t USBH_HID_FifoRead(FIFO_TypeDef *f, void *buf, uint16_t nbytes) +{ + uint16_t i; + uint8_t * p; + p = (uint8_t*) buf; + + if(f->lock == 0U) + { + f->lock = 1U; + for(i = 0U; i < nbytes; i++) + { + if( f->tail != f->head) + { + *p++ = f->buf[f->tail]; + f->tail++; + if( f->tail == f->size) + { + f->tail = 0U; + } + } else + { + f->lock = 0U; + return i; + } + } + } + f->lock = 0U; + + return nbytes; +} + +/** + * @brief USBH_HID_FifoWrite + * Read from FIFO. + * @param f: Fifo address + * @param buf: read buffer + * @param nbytes: number of item to write + * @retval number of written items + */ +uint16_t USBH_HID_FifoWrite(FIFO_TypeDef *f, void *buf, uint16_t nbytes) +{ + uint16_t i; + uint8_t *p; + p = (uint8_t*) buf; + if(f->lock == 0U) + { + f->lock = 1U; + for(i = 0U; i < nbytes; i++) + { + if( (f->head + 1U == f->tail) || + ( (f->head + 1U == f->size) && (f->tail == 0U))) + { + f->lock = 0U; + return i; + } + else + { + f->buf[f->head] = *p++; + f->head++; + if( f->head == f->size) + { + f->head = 0U; + } + } + } + } + f->lock = 0U; + return nbytes; +} + +/** +* @brief The function is a callback about HID Data events +* @param phost: Selected device +* @retval None +*/ +__weak void USBH_HID_EventCallback(USBH_HandleTypeDef *phost) +{ + +} +/** +* @} +*/ + +/** +* @} +*/ + +/** +* @} +*/ + + +/** +* @} +*/ + + +/** +* @} +*/ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Src/usbh_hid_keybd.c b/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Src/usbh_hid_keybd.c index 338fbfcab2..e77be41380 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Src/usbh_hid_keybd.c +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Src/usbh_hid_keybd.c @@ -1,416 +1,416 @@ -/** - ****************************************************************************** - * @file usbh_hid_keybd.c - * @author MCD Application Team - * @brief This file is the application layer for USB Host HID Keyboard handling - * QWERTY and AZERTY Keyboard are supported as per the selection in - * usbh_hid_keybd.h - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - - /* BSPDependencies - - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" - - "stm32xxxxx_{eval}{discovery}_io.c" - - "stm32xxxxx_{eval}{discovery}{adafruit}_lcd.c" - - "stm32xxxxx_{eval}{discovery}_sdram.c" - EndBSPDependencies */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_hid_keybd.h" -#include "usbh_hid_parser.h" - -/** @addtogroup USBH_LIB -* @{ -*/ - -/** @addtogroup USBH_CLASS -* @{ -*/ - -/** @addtogroup USBH_HID_CLASS -* @{ -*/ - -/** @defgroup USBH_HID_KEYBD -* @brief This file includes HID Layer Handlers for USB Host HID class. -* @{ -*/ - -/** @defgroup USBH_HID_KEYBD_Private_TypesDefinitions -* @{ -*/ -/** -* @} -*/ - - -/** @defgroup USBH_HID_KEYBD_Private_Defines -* @{ -*/ -/** -* @} -*/ -#ifndef AZERTY_KEYBOARD - #define QWERTY_KEYBOARD -#endif -#define KBD_LEFT_CTRL 0x01 -#define KBD_LEFT_SHIFT 0x02 -#define KBD_LEFT_ALT 0x04 -#define KBD_LEFT_GUI 0x08 -#define KBD_RIGHT_CTRL 0x10 -#define KBD_RIGHT_SHIFT 0x20 -#define KBD_RIGHT_ALT 0x40 -#define KBD_RIGHT_GUI 0x80 -#define KBR_MAX_NBR_PRESSED 6 - -/** @defgroup USBH_HID_KEYBD_Private_Macros -* @{ -*/ -/** -* @} -*/ - -/** @defgroup USBH_HID_KEYBD_Private_FunctionPrototypes -* @{ -*/ -static USBH_StatusTypeDef USBH_HID_KeybdDecode(USBH_HandleTypeDef *phost); -/** -* @} -*/ - -/** @defgroup USBH_HID_KEYBD_Private_Variables -* @{ -*/ - -HID_KEYBD_Info_TypeDef keybd_info; -uint32_t keybd_report_data[2]; - -static const HID_Report_ItemTypedef imp_0_lctrl={ - (uint8_t*)(void *)keybd_report_data+0, /*data*/ - 1, /*size*/ - 0, /*shift*/ - 0, /*count (only for array items)*/ - 0, /*signed?*/ - 0, /*min value read can return*/ - 1, /*max value read can return*/ - 0, /*min vale device can report*/ - 1, /*max value device can report*/ - 1 /*resolution*/ -}; -static const HID_Report_ItemTypedef imp_0_lshift={ - (uint8_t*)(void *)keybd_report_data+0, /*data*/ - 1, /*size*/ - 1, /*shift*/ - 0, /*count (only for array items)*/ - 0, /*signed?*/ - 0, /*min value read can return*/ - 1, /*max value read can return*/ - 0, /*min vale device can report*/ - 1, /*max value device can report*/ - 1 /*resolution*/ -}; -static const HID_Report_ItemTypedef imp_0_lalt={ - (uint8_t*)(void *)keybd_report_data+0, /*data*/ - 1, /*size*/ - 2, /*shift*/ - 0, /*count (only for array items)*/ - 0, /*signed?*/ - 0, /*min value read can return*/ - 1, /*max value read can return*/ - 0, /*min vale device can report*/ - 1, /*max value device can report*/ - 1 /*resolution*/ -}; -static const HID_Report_ItemTypedef imp_0_lgui={ - (uint8_t*)(void *)keybd_report_data+0, /*data*/ - 1, /*size*/ - 3, /*shift*/ - 0, /*count (only for array items)*/ - 0, /*signed?*/ - 0, /*min value read can return*/ - 1, /*max value read can return*/ - 0, /*min vale device can report*/ - 1, /*max value device can report*/ - 1 /*resolution*/ -}; -static const HID_Report_ItemTypedef imp_0_rctrl={ - (uint8_t*)(void *)keybd_report_data+0, /*data*/ - 1, /*size*/ - 4, /*shift*/ - 0, /*count (only for array items)*/ - 0, /*signed?*/ - 0, /*min value read can return*/ - 1, /*max value read can return*/ - 0, /*min vale device can report*/ - 1, /*max value device can report*/ - 1 /*resolution*/ -}; -static const HID_Report_ItemTypedef imp_0_rshift={ - (uint8_t*)(void *)keybd_report_data+0, /*data*/ - 1, /*size*/ - 5, /*shift*/ - 0, /*count (only for array items)*/ - 0, /*signed?*/ - 0, /*min value read can return*/ - 1, /*max value read can return*/ - 0, /*min vale device can report*/ - 1, /*max value device can report*/ - 1 /*resolution*/ -}; -static const HID_Report_ItemTypedef imp_0_ralt={ - (uint8_t*)(void *)keybd_report_data+0, /*data*/ - 1, /*size*/ - 6, /*shift*/ - 0, /*count (only for array items)*/ - 0, /*signed?*/ - 0, /*min value read can return*/ - 1, /*max value read can return*/ - 0, /*min vale device can report*/ - 1, /*max value device can report*/ - 1 /*resolution*/ -}; -static const HID_Report_ItemTypedef imp_0_rgui={ - (uint8_t*)(void *)keybd_report_data+0, /*data*/ - 1, /*size*/ - 7, /*shift*/ - 0, /*count (only for array items)*/ - 0, /*signed?*/ - 0, /*min value read can return*/ - 1, /*max value read can return*/ - 0, /*min vale device can report*/ - 1, /*max value device can report*/ - 1 /*resolution*/ -}; - -static const HID_Report_ItemTypedef imp_0_key_array = { - (uint8_t*)(void *)keybd_report_data + 2, /*data*/ - 8, /*size*/ - 0, /*shift*/ - 6, /*count (only for array items)*/ - 0, /*signed?*/ - 0, /*min value read can return*/ - 101, /*max value read can return*/ - 0, /*min vale device can report*/ - 101, /*max value device can report*/ - 1 /*resolution*/ -}; - -#ifdef QWERTY_KEYBOARD -static const uint8_t HID_KEYBRD_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 uint8_t HID_KEYBRD_ShiftKey[] = { - '\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 uint8_t HID_KEYBRD_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 uint8_t HID_KEYBRD_ShiftKey[] = { - '\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 - -static const uint8_t HID_KEYBRD_Codes[] = { - 0, 0, 0, 0, 31, 50, 48, 33, - 19, 34, 35, 36, 24, 37, 38, 39, /* 0x00 - 0x0F */ - 52, 51, 25, 26, 17, 20, 32, 21, - 23, 49, 18, 47, 22, 46, 2, 3, /* 0x10 - 0x1F */ - 4, 5, 6, 7, 8, 9, 10, 11, - 43, 110, 15, 16, 61, 12, 13, 27, /* 0x20 - 0x2F */ - 28, 29, 42, 40, 41, 1, 53, 54, - 55, 30, 112, 113, 114, 115, 116, 117, /* 0x30 - 0x3F */ - 118, 119, 120, 121, 122, 123, 124, 125, - 126, 75, 80, 85, 76, 81, 86, 89, /* 0x40 - 0x4F */ - 79, 84, 83, 90, 95, 100, 105, 106, - 108, 93, 98, 103, 92, 97, 102, 91, /* 0x50 - 0x5F */ - 96, 101, 99, 104, 45, 129, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60 - 0x6F */ - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 - 0x7F */ - 0, 0, 0, 0, 0, 107, 0, 56, - 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 - 0x8F */ - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 - 0x9F */ - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, /* 0xA0 - 0xAF */ - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, /* 0xB0 - 0xBF */ - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, /* 0xC0 - 0xCF */ - 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, /* 0xD0 - 0xDF */ - 58, 44, 60, 127, 64, 57, 62, 128 /* 0xE0 - 0xE7 */ -}; - -/** - * @brief USBH_HID_KeybdInit - * The function init the HID keyboard. - * @param phost: Host handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_HID_KeybdInit(USBH_HandleTypeDef *phost) -{ - uint32_t x; - HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData; - - keybd_info.lctrl=keybd_info.lshift = 0U; - keybd_info.lalt=keybd_info.lgui = 0U; - keybd_info.rctrl=keybd_info.rshift = 0U; - keybd_info.ralt=keybd_info.rgui = 0U; - - - for(x = 0U; x < (sizeof(keybd_report_data)/sizeof(uint32_t)); x++) - { - keybd_report_data[x] = 0U; - } - - if(HID_Handle->length > (sizeof(keybd_report_data)/sizeof(uint32_t))) - { - HID_Handle->length = (sizeof(keybd_report_data)/sizeof(uint32_t)); - } - HID_Handle->pData = (uint8_t*)(void *)keybd_report_data; - USBH_HID_FifoInit(&HID_Handle->fifo, phost->device.Data, HID_QUEUE_SIZE * sizeof(keybd_report_data)); - - return USBH_OK; -} - -/** - * @brief USBH_HID_GetKeybdInfo - * The function return keyboard information. - * @param phost: Host handle - * @retval keyboard information - */ -HID_KEYBD_Info_TypeDef *USBH_HID_GetKeybdInfo(USBH_HandleTypeDef *phost) -{ - if(USBH_HID_KeybdDecode(phost) == USBH_OK) - { - return &keybd_info; - } - else - { - return NULL; - } -} - -/** - * @brief USBH_HID_KeybdDecode - * The function decode keyboard data. - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_HID_KeybdDecode(USBH_HandleTypeDef *phost) -{ - uint8_t x; - - HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData; - if(HID_Handle->length == 0U) - { - return USBH_FAIL; - } - /*Fill report */ - if(USBH_HID_FifoRead(&HID_Handle->fifo, &keybd_report_data, HID_Handle->length) == HID_Handle->length) - { - - keybd_info.lctrl=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_lctrl, 0U); - keybd_info.lshift=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_lshift, 0U); - keybd_info.lalt=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_lalt, 0U); - keybd_info.lgui=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_lgui, 0U); - keybd_info.rctrl=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_rctrl, 0U); - keybd_info.rshift=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_rshift, 0U); - keybd_info.ralt=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_ralt, 0U); - keybd_info.rgui=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_rgui, 0U); - - for(x = 0U; x < sizeof(keybd_info.keys); x++) - { - keybd_info.keys[x]=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_key_array, x); - } - - return USBH_OK; - } - return USBH_FAIL; -} - -/** - * @brief USBH_HID_GetASCIICode - * The function decode keyboard data into ASCII characters. - * @param phost: Host handle - * @param info: Keyboard information - * @retval ASCII code - */ -uint8_t USBH_HID_GetASCIICode(HID_KEYBD_Info_TypeDef *info) -{ - uint8_t output; - if((info->lshift == 1U) || (info->rshift)) - { - output = HID_KEYBRD_ShiftKey[HID_KEYBRD_Codes[info->keys[0]]]; - } - else - { - output = HID_KEYBRD_Key[HID_KEYBRD_Codes[info->keys[0]]]; - } - return output; -} -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - +/** + ****************************************************************************** + * @file usbh_hid_keybd.c + * @author MCD Application Team + * @brief This file is the application layer for USB Host HID Keyboard handling + * QWERTY and AZERTY Keyboard are supported as per the selection in + * usbh_hid_keybd.h + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + + /* BSPDependencies + - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" + - "stm32xxxxx_{eval}{discovery}_io.c" + - "stm32xxxxx_{eval}{discovery}{adafruit}_lcd.c" + - "stm32xxxxx_{eval}{discovery}_sdram.c" + EndBSPDependencies */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_hid_keybd.h" +#include "usbh_hid_parser.h" + +/** @addtogroup USBH_LIB +* @{ +*/ + +/** @addtogroup USBH_CLASS +* @{ +*/ + +/** @addtogroup USBH_HID_CLASS +* @{ +*/ + +/** @defgroup USBH_HID_KEYBD +* @brief This file includes HID Layer Handlers for USB Host HID class. +* @{ +*/ + +/** @defgroup USBH_HID_KEYBD_Private_TypesDefinitions +* @{ +*/ +/** +* @} +*/ + + +/** @defgroup USBH_HID_KEYBD_Private_Defines +* @{ +*/ +/** +* @} +*/ +#ifndef AZERTY_KEYBOARD + #define QWERTY_KEYBOARD +#endif +#define KBD_LEFT_CTRL 0x01 +#define KBD_LEFT_SHIFT 0x02 +#define KBD_LEFT_ALT 0x04 +#define KBD_LEFT_GUI 0x08 +#define KBD_RIGHT_CTRL 0x10 +#define KBD_RIGHT_SHIFT 0x20 +#define KBD_RIGHT_ALT 0x40 +#define KBD_RIGHT_GUI 0x80 +#define KBR_MAX_NBR_PRESSED 6 + +/** @defgroup USBH_HID_KEYBD_Private_Macros +* @{ +*/ +/** +* @} +*/ + +/** @defgroup USBH_HID_KEYBD_Private_FunctionPrototypes +* @{ +*/ +static USBH_StatusTypeDef USBH_HID_KeybdDecode(USBH_HandleTypeDef *phost); +/** +* @} +*/ + +/** @defgroup USBH_HID_KEYBD_Private_Variables +* @{ +*/ + +HID_KEYBD_Info_TypeDef keybd_info; +uint32_t keybd_report_data[2]; + +static const HID_Report_ItemTypedef imp_0_lctrl={ + (uint8_t*)(void *)keybd_report_data+0, /*data*/ + 1, /*size*/ + 0, /*shift*/ + 0, /*count (only for array items)*/ + 0, /*signed?*/ + 0, /*min value read can return*/ + 1, /*max value read can return*/ + 0, /*min vale device can report*/ + 1, /*max value device can report*/ + 1 /*resolution*/ +}; +static const HID_Report_ItemTypedef imp_0_lshift={ + (uint8_t*)(void *)keybd_report_data+0, /*data*/ + 1, /*size*/ + 1, /*shift*/ + 0, /*count (only for array items)*/ + 0, /*signed?*/ + 0, /*min value read can return*/ + 1, /*max value read can return*/ + 0, /*min vale device can report*/ + 1, /*max value device can report*/ + 1 /*resolution*/ +}; +static const HID_Report_ItemTypedef imp_0_lalt={ + (uint8_t*)(void *)keybd_report_data+0, /*data*/ + 1, /*size*/ + 2, /*shift*/ + 0, /*count (only for array items)*/ + 0, /*signed?*/ + 0, /*min value read can return*/ + 1, /*max value read can return*/ + 0, /*min vale device can report*/ + 1, /*max value device can report*/ + 1 /*resolution*/ +}; +static const HID_Report_ItemTypedef imp_0_lgui={ + (uint8_t*)(void *)keybd_report_data+0, /*data*/ + 1, /*size*/ + 3, /*shift*/ + 0, /*count (only for array items)*/ + 0, /*signed?*/ + 0, /*min value read can return*/ + 1, /*max value read can return*/ + 0, /*min vale device can report*/ + 1, /*max value device can report*/ + 1 /*resolution*/ +}; +static const HID_Report_ItemTypedef imp_0_rctrl={ + (uint8_t*)(void *)keybd_report_data+0, /*data*/ + 1, /*size*/ + 4, /*shift*/ + 0, /*count (only for array items)*/ + 0, /*signed?*/ + 0, /*min value read can return*/ + 1, /*max value read can return*/ + 0, /*min vale device can report*/ + 1, /*max value device can report*/ + 1 /*resolution*/ +}; +static const HID_Report_ItemTypedef imp_0_rshift={ + (uint8_t*)(void *)keybd_report_data+0, /*data*/ + 1, /*size*/ + 5, /*shift*/ + 0, /*count (only for array items)*/ + 0, /*signed?*/ + 0, /*min value read can return*/ + 1, /*max value read can return*/ + 0, /*min vale device can report*/ + 1, /*max value device can report*/ + 1 /*resolution*/ +}; +static const HID_Report_ItemTypedef imp_0_ralt={ + (uint8_t*)(void *)keybd_report_data+0, /*data*/ + 1, /*size*/ + 6, /*shift*/ + 0, /*count (only for array items)*/ + 0, /*signed?*/ + 0, /*min value read can return*/ + 1, /*max value read can return*/ + 0, /*min vale device can report*/ + 1, /*max value device can report*/ + 1 /*resolution*/ +}; +static const HID_Report_ItemTypedef imp_0_rgui={ + (uint8_t*)(void *)keybd_report_data+0, /*data*/ + 1, /*size*/ + 7, /*shift*/ + 0, /*count (only for array items)*/ + 0, /*signed?*/ + 0, /*min value read can return*/ + 1, /*max value read can return*/ + 0, /*min vale device can report*/ + 1, /*max value device can report*/ + 1 /*resolution*/ +}; + +static const HID_Report_ItemTypedef imp_0_key_array = { + (uint8_t*)(void *)keybd_report_data + 2, /*data*/ + 8, /*size*/ + 0, /*shift*/ + 6, /*count (only for array items)*/ + 0, /*signed?*/ + 0, /*min value read can return*/ + 101, /*max value read can return*/ + 0, /*min vale device can report*/ + 101, /*max value device can report*/ + 1 /*resolution*/ +}; + +#ifdef QWERTY_KEYBOARD +static const uint8_t HID_KEYBRD_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 uint8_t HID_KEYBRD_ShiftKey[] = { + '\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 uint8_t HID_KEYBRD_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 uint8_t HID_KEYBRD_ShiftKey[] = { + '\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 + +static const uint8_t HID_KEYBRD_Codes[] = { + 0, 0, 0, 0, 31, 50, 48, 33, + 19, 34, 35, 36, 24, 37, 38, 39, /* 0x00 - 0x0F */ + 52, 51, 25, 26, 17, 20, 32, 21, + 23, 49, 18, 47, 22, 46, 2, 3, /* 0x10 - 0x1F */ + 4, 5, 6, 7, 8, 9, 10, 11, + 43, 110, 15, 16, 61, 12, 13, 27, /* 0x20 - 0x2F */ + 28, 29, 42, 40, 41, 1, 53, 54, + 55, 30, 112, 113, 114, 115, 116, 117, /* 0x30 - 0x3F */ + 118, 119, 120, 121, 122, 123, 124, 125, + 126, 75, 80, 85, 76, 81, 86, 89, /* 0x40 - 0x4F */ + 79, 84, 83, 90, 95, 100, 105, 106, + 108, 93, 98, 103, 92, 97, 102, 91, /* 0x50 - 0x5F */ + 96, 101, 99, 104, 45, 129, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, /* 0x60 - 0x6F */ + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 - 0x7F */ + 0, 0, 0, 0, 0, 107, 0, 56, + 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 - 0x8F */ + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 - 0x9F */ + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, /* 0xA0 - 0xAF */ + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, /* 0xB0 - 0xBF */ + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, /* 0xC0 - 0xCF */ + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, /* 0xD0 - 0xDF */ + 58, 44, 60, 127, 64, 57, 62, 128 /* 0xE0 - 0xE7 */ +}; + +/** + * @brief USBH_HID_KeybdInit + * The function init the HID keyboard. + * @param phost: Host handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_HID_KeybdInit(USBH_HandleTypeDef *phost) +{ + uint32_t x; + HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData; + + keybd_info.lctrl=keybd_info.lshift = 0U; + keybd_info.lalt=keybd_info.lgui = 0U; + keybd_info.rctrl=keybd_info.rshift = 0U; + keybd_info.ralt=keybd_info.rgui = 0U; + + + for(x = 0U; x < (sizeof(keybd_report_data)/sizeof(uint32_t)); x++) + { + keybd_report_data[x] = 0U; + } + + if(HID_Handle->length > (sizeof(keybd_report_data)/sizeof(uint32_t))) + { + HID_Handle->length = (sizeof(keybd_report_data)/sizeof(uint32_t)); + } + HID_Handle->pData = (uint8_t*)(void *)keybd_report_data; + USBH_HID_FifoInit(&HID_Handle->fifo, phost->device.Data, HID_QUEUE_SIZE * sizeof(keybd_report_data)); + + return USBH_OK; +} + +/** + * @brief USBH_HID_GetKeybdInfo + * The function return keyboard information. + * @param phost: Host handle + * @retval keyboard information + */ +HID_KEYBD_Info_TypeDef *USBH_HID_GetKeybdInfo(USBH_HandleTypeDef *phost) +{ + if(USBH_HID_KeybdDecode(phost) == USBH_OK) + { + return &keybd_info; + } + else + { + return NULL; + } +} + +/** + * @brief USBH_HID_KeybdDecode + * The function decode keyboard data. + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_HID_KeybdDecode(USBH_HandleTypeDef *phost) +{ + uint8_t x; + + HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData; + if(HID_Handle->length == 0U) + { + return USBH_FAIL; + } + /*Fill report */ + if(USBH_HID_FifoRead(&HID_Handle->fifo, &keybd_report_data, HID_Handle->length) == HID_Handle->length) + { + + keybd_info.lctrl=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_lctrl, 0U); + keybd_info.lshift=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_lshift, 0U); + keybd_info.lalt=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_lalt, 0U); + keybd_info.lgui=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_lgui, 0U); + keybd_info.rctrl=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_rctrl, 0U); + keybd_info.rshift=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_rshift, 0U); + keybd_info.ralt=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_ralt, 0U); + keybd_info.rgui=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_rgui, 0U); + + for(x = 0U; x < sizeof(keybd_info.keys); x++) + { + keybd_info.keys[x]=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &imp_0_key_array, x); + } + + return USBH_OK; + } + return USBH_FAIL; +} + +/** + * @brief USBH_HID_GetASCIICode + * The function decode keyboard data into ASCII characters. + * @param phost: Host handle + * @param info: Keyboard information + * @retval ASCII code + */ +uint8_t USBH_HID_GetASCIICode(HID_KEYBD_Info_TypeDef *info) +{ + uint8_t output; + if((info->lshift == 1U) || (info->rshift)) + { + output = HID_KEYBRD_ShiftKey[HID_KEYBRD_Codes[info->keys[0]]]; + } + else + { + output = HID_KEYBRD_Key[HID_KEYBRD_Codes[info->keys[0]]]; + } + return output; +} +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Src/usbh_hid_mouse.c b/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Src/usbh_hid_mouse.c index 45984f7af2..8630f7c914 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Src/usbh_hid_mouse.c +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Src/usbh_hid_mouse.c @@ -1,265 +1,265 @@ -/** - ****************************************************************************** - * @file usbh_hid_mouse.c - * @author MCD Application Team - * @brief This file is the application layer for USB Host HID Mouse Handling. - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - - /* BSPDependencies - - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" - - "stm32xxxxx_{eval}{discovery}_io.c" - - "stm32xxxxx_{eval}{discovery}{adafruit}_lcd.c" - - "stm32xxxxx_{eval}{discovery}_sdram.c" - EndBSPDependencies */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_hid_mouse.h" -#include "usbh_hid_parser.h" - - -/** @addtogroup USBH_LIB - * @{ - */ - -/** @addtogroup USBH_CLASS - * @{ - */ - -/** @addtogroup USBH_HID_CLASS - * @{ - */ - -/** @defgroup USBH_HID_MOUSE - * @brief This file includes HID Layer Handlers for USB Host HID class. - * @{ - */ - -/** @defgroup USBH_HID_MOUSE_Private_TypesDefinitions - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBH_HID_MOUSE_Private_Defines - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBH_HID_MOUSE_Private_Macros - * @{ - */ -/** - * @} - */ - -/** @defgroup USBH_HID_MOUSE_Private_FunctionPrototypes - * @{ - */ -static USBH_StatusTypeDef USBH_HID_MouseDecode(USBH_HandleTypeDef *phost); - -/** - * @} - */ - - -/** @defgroup USBH_HID_MOUSE_Private_Variables - * @{ - */ -HID_MOUSE_Info_TypeDef mouse_info; -uint32_t mouse_report_data[1]; - -/* Structures defining how to access items in a HID mouse report */ -/* Access button 1 state. */ -static const HID_Report_ItemTypedef prop_b1={ - (uint8_t *)(void *)mouse_report_data+0, /*data*/ - 1, /*size*/ - 0, /*shift*/ - 0, /*count (only for array items)*/ - 0, /*signed?*/ - 0, /*min value read can return*/ - 1, /*max value read can return*/ - 0, /*min value device can report*/ - 1, /*max value device can report*/ - 1 /*resolution*/ -}; - -/* Access button 2 state. */ -static const HID_Report_ItemTypedef prop_b2={ - (uint8_t *)(void *)mouse_report_data+0, /*data*/ - 1, /*size*/ - 1, /*shift*/ - 0, /*count (only for array items)*/ - 0, /*signed?*/ - 0, /*min value read can return*/ - 1, /*max value read can return*/ - 0, /*min value device can report*/ - 1, /*max value device can report*/ - 1 /*resolution*/ -}; - -/* Access button 3 state. */ -static const HID_Report_ItemTypedef prop_b3={ - (uint8_t *)(void *)mouse_report_data+0, /*data*/ - 1, /*size*/ - 2, /*shift*/ - 0, /*count (only for array items)*/ - 0, /*signed?*/ - 0, /*min value read can return*/ - 1, /*max value read can return*/ - 0, /*min vale device can report*/ - 1, /*max value device can report*/ - 1 /*resolution*/ -}; - -/* Access x coordinate change. */ -static const HID_Report_ItemTypedef prop_x={ - (uint8_t *)(void *)mouse_report_data+1, /*data*/ - 8, /*size*/ - 0, /*shift*/ - 0, /*count (only for array items)*/ - 1, /*signed?*/ - 0, /*min value read can return*/ - 0xFFFF,/*max value read can return*/ - 0, /*min vale device can report*/ - 0xFFFF,/*max value device can report*/ - 1 /*resolution*/ -}; - -/* Access y coordinate change. */ -static const HID_Report_ItemTypedef prop_y={ - (uint8_t *)(void *)mouse_report_data+2, /*data*/ - 8, /*size*/ - 0, /*shift*/ - 0, /*count (only for array items)*/ - 1, /*signed?*/ - 0, /*min value read can return*/ - 0xFFFF,/*max value read can return*/ - 0, /*min vale device can report*/ - 0xFFFF,/*max value device can report*/ - 1 /*resolution*/ -}; - - -/** - * @} - */ - - -/** @defgroup USBH_HID_MOUSE_Private_Functions - * @{ - */ - -/** - * @brief USBH_HID_MouseInit - * The function init the HID mouse. - * @param phost: Host handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_HID_MouseInit(USBH_HandleTypeDef *phost) -{ - HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData; - - mouse_info.x = 0U; - mouse_info.y = 0U; - mouse_info.buttons[0] = 0U; - mouse_info.buttons[1] = 0U; - mouse_info.buttons[2] = 0U; - - mouse_report_data[0] = 0U; - - if(HID_Handle->length > sizeof(mouse_report_data)) - { - HID_Handle->length = sizeof(mouse_report_data); - } - HID_Handle->pData = (uint8_t *)(void *)mouse_report_data; - USBH_HID_FifoInit(&HID_Handle->fifo, phost->device.Data, HID_QUEUE_SIZE * sizeof(mouse_report_data)); - - return USBH_OK; -} - -/** - * @brief USBH_HID_GetMouseInfo - * The function return mouse information. - * @param phost: Host handle - * @retval mouse information - */ -HID_MOUSE_Info_TypeDef *USBH_HID_GetMouseInfo(USBH_HandleTypeDef *phost) -{ - if(USBH_HID_MouseDecode(phost)== USBH_OK) - { - return &mouse_info; - } - else - { - return NULL; - } -} - -/** - * @brief USBH_HID_MouseDecode - * The function decode mouse data. - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_HID_MouseDecode(USBH_HandleTypeDef *phost) -{ - HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData; - - if(HID_Handle->length == 0U) - { - return USBH_FAIL; - } - /*Fill report */ - if(USBH_HID_FifoRead(&HID_Handle->fifo, &mouse_report_data, HID_Handle->length) == HID_Handle->length) - { - /*Decode report */ - mouse_info.x = (uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &prop_x, 0U); - mouse_info.y = (uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &prop_y, 0U); - - mouse_info.buttons[0]=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &prop_b1, 0U); - mouse_info.buttons[1]=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &prop_b2, 0U); - mouse_info.buttons[2]=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &prop_b3, 0U); - - return USBH_OK; - } - return USBH_FAIL; -} - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ - - -/** - * @} - */ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbh_hid_mouse.c + * @author MCD Application Team + * @brief This file is the application layer for USB Host HID Mouse Handling. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + + /* BSPDependencies + - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" + - "stm32xxxxx_{eval}{discovery}_io.c" + - "stm32xxxxx_{eval}{discovery}{adafruit}_lcd.c" + - "stm32xxxxx_{eval}{discovery}_sdram.c" + EndBSPDependencies */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_hid_mouse.h" +#include "usbh_hid_parser.h" + + +/** @addtogroup USBH_LIB + * @{ + */ + +/** @addtogroup USBH_CLASS + * @{ + */ + +/** @addtogroup USBH_HID_CLASS + * @{ + */ + +/** @defgroup USBH_HID_MOUSE + * @brief This file includes HID Layer Handlers for USB Host HID class. + * @{ + */ + +/** @defgroup USBH_HID_MOUSE_Private_TypesDefinitions + * @{ + */ +/** + * @} + */ + + +/** @defgroup USBH_HID_MOUSE_Private_Defines + * @{ + */ +/** + * @} + */ + + +/** @defgroup USBH_HID_MOUSE_Private_Macros + * @{ + */ +/** + * @} + */ + +/** @defgroup USBH_HID_MOUSE_Private_FunctionPrototypes + * @{ + */ +static USBH_StatusTypeDef USBH_HID_MouseDecode(USBH_HandleTypeDef *phost); + +/** + * @} + */ + + +/** @defgroup USBH_HID_MOUSE_Private_Variables + * @{ + */ +HID_MOUSE_Info_TypeDef mouse_info; +uint32_t mouse_report_data[1]; + +/* Structures defining how to access items in a HID mouse report */ +/* Access button 1 state. */ +static const HID_Report_ItemTypedef prop_b1={ + (uint8_t *)(void *)mouse_report_data+0, /*data*/ + 1, /*size*/ + 0, /*shift*/ + 0, /*count (only for array items)*/ + 0, /*signed?*/ + 0, /*min value read can return*/ + 1, /*max value read can return*/ + 0, /*min value device can report*/ + 1, /*max value device can report*/ + 1 /*resolution*/ +}; + +/* Access button 2 state. */ +static const HID_Report_ItemTypedef prop_b2={ + (uint8_t *)(void *)mouse_report_data+0, /*data*/ + 1, /*size*/ + 1, /*shift*/ + 0, /*count (only for array items)*/ + 0, /*signed?*/ + 0, /*min value read can return*/ + 1, /*max value read can return*/ + 0, /*min value device can report*/ + 1, /*max value device can report*/ + 1 /*resolution*/ +}; + +/* Access button 3 state. */ +static const HID_Report_ItemTypedef prop_b3={ + (uint8_t *)(void *)mouse_report_data+0, /*data*/ + 1, /*size*/ + 2, /*shift*/ + 0, /*count (only for array items)*/ + 0, /*signed?*/ + 0, /*min value read can return*/ + 1, /*max value read can return*/ + 0, /*min vale device can report*/ + 1, /*max value device can report*/ + 1 /*resolution*/ +}; + +/* Access x coordinate change. */ +static const HID_Report_ItemTypedef prop_x={ + (uint8_t *)(void *)mouse_report_data+1, /*data*/ + 8, /*size*/ + 0, /*shift*/ + 0, /*count (only for array items)*/ + 1, /*signed?*/ + 0, /*min value read can return*/ + 0xFFFF,/*max value read can return*/ + 0, /*min vale device can report*/ + 0xFFFF,/*max value device can report*/ + 1 /*resolution*/ +}; + +/* Access y coordinate change. */ +static const HID_Report_ItemTypedef prop_y={ + (uint8_t *)(void *)mouse_report_data+2, /*data*/ + 8, /*size*/ + 0, /*shift*/ + 0, /*count (only for array items)*/ + 1, /*signed?*/ + 0, /*min value read can return*/ + 0xFFFF,/*max value read can return*/ + 0, /*min vale device can report*/ + 0xFFFF,/*max value device can report*/ + 1 /*resolution*/ +}; + + +/** + * @} + */ + + +/** @defgroup USBH_HID_MOUSE_Private_Functions + * @{ + */ + +/** + * @brief USBH_HID_MouseInit + * The function init the HID mouse. + * @param phost: Host handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_HID_MouseInit(USBH_HandleTypeDef *phost) +{ + HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData; + + mouse_info.x = 0U; + mouse_info.y = 0U; + mouse_info.buttons[0] = 0U; + mouse_info.buttons[1] = 0U; + mouse_info.buttons[2] = 0U; + + mouse_report_data[0] = 0U; + + if(HID_Handle->length > sizeof(mouse_report_data)) + { + HID_Handle->length = sizeof(mouse_report_data); + } + HID_Handle->pData = (uint8_t *)(void *)mouse_report_data; + USBH_HID_FifoInit(&HID_Handle->fifo, phost->device.Data, HID_QUEUE_SIZE * sizeof(mouse_report_data)); + + return USBH_OK; +} + +/** + * @brief USBH_HID_GetMouseInfo + * The function return mouse information. + * @param phost: Host handle + * @retval mouse information + */ +HID_MOUSE_Info_TypeDef *USBH_HID_GetMouseInfo(USBH_HandleTypeDef *phost) +{ + if(USBH_HID_MouseDecode(phost)== USBH_OK) + { + return &mouse_info; + } + else + { + return NULL; + } +} + +/** + * @brief USBH_HID_MouseDecode + * The function decode mouse data. + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_HID_MouseDecode(USBH_HandleTypeDef *phost) +{ + HID_HandleTypeDef *HID_Handle = (HID_HandleTypeDef *) phost->pActiveClass->pData; + + if(HID_Handle->length == 0U) + { + return USBH_FAIL; + } + /*Fill report */ + if(USBH_HID_FifoRead(&HID_Handle->fifo, &mouse_report_data, HID_Handle->length) == HID_Handle->length) + { + /*Decode report */ + mouse_info.x = (uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &prop_x, 0U); + mouse_info.y = (uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &prop_y, 0U); + + mouse_info.buttons[0]=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &prop_b1, 0U); + mouse_info.buttons[1]=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &prop_b2, 0U); + mouse_info.buttons[2]=(uint8_t)HID_ReadItem((HID_Report_ItemTypedef *) &prop_b3, 0U); + + return USBH_OK; + } + return USBH_FAIL; +} + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + + +/** + * @} + */ +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Src/usbh_hid_parser.c b/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Src/usbh_hid_parser.c index 373002efe7..93bf7823dc 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Src/usbh_hid_parser.c +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Class/HID/Src/usbh_hid_parser.c @@ -1,237 +1,237 @@ -/** - ****************************************************************************** - * @file usbh_hid_parser.c - * @author MCD Application Team - * @brief This file is the HID Layer Handlers for USB Host HID class. - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - - /* BSPDependencies - - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" - - "stm32xxxxx_{eval}{discovery}_io.c" - - "stm32xxxxx_{eval}{discovery}{adafruit}_lcd.c" - - "stm32xxxxx_{eval}{discovery}_sdram.c" - EndBSPDependencies */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_hid_parser.h" - - -/** @addtogroup USBH_LIB - * @{ - */ - -/** @addtogroup USBH_CLASS - * @{ - */ - -/** @addtogroup USBH_HID_CLASS - * @{ - */ - -/** @defgroup USBH_HID_PARSER - * @brief This file includes HID parsers for USB Host HID class. - * @{ - */ - -/** @defgroup USBH_HID_PARSER_Private_TypesDefinitions - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBH_HID_PARSER_Private_Defines - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBH_HID_PARSER_Private_Macros - * @{ - */ -/** - * @} - */ - -/** @defgroup USBH_HID_PARSER_Private_FunctionPrototypes - * @{ - */ - -/** - * @} - */ - - -/** @defgroup USBH_HID_PARSER_Private_Variables - * @{ - */ - -/** - * @} - */ - - -/** @defgroup USBH_HID_PARSER_Private_Functions - * @{ - */ - -/** - * @brief HID_ReadItem - * The function read a report item. - * @param ri: report item - * @param ndx: report index -* @retval status (0 : fail / otherwise: item value) - */ -uint32_t HID_ReadItem(HID_Report_ItemTypedef *ri, uint8_t ndx) -{ - uint32_t val = 0U; - uint32_t x = 0U; - uint32_t bofs; - uint8_t *data=ri->data; - uint8_t shift=ri->shift; - - /* get the logical value of the item */ - - /* if this is an array, wee may need to offset ri->data.*/ - if (ri->count > 0U) - { - /* If app tries to read outside of the array. */ - if (ri->count <= ndx) - { - return(0U); - } - - /* calculate bit offset */ - bofs = ndx*ri->size; - bofs += shift; - /* calculate byte offset + shift pair from bit offset. */ - data+=bofs / 8U; - shift=(uint8_t)(bofs % 8U); - } - /* read data bytes in little endian order */ - for(x = 0U; x < ((ri->size & 0x7U) ? (ri->size / 8U) + 1U : (ri->size / 8U)); x++) - { - val=(uint32_t)((uint32_t)(*data) << (x * 8U)); - } - val=(val >> shift) & ((1U << ri->size) - 1U); - - if (val < ri->logical_min || val > ri->logical_max) - { - return(0U); - } - - /* convert logical value to physical value */ - /* See if the number is negative or not. */ - if ((ri->sign) && (val & (1U << (ri->size - 1U)))) - { - /* yes, so sign extend value to 32 bits. */ - uint32_t vs=(uint32_t)((0xffffffffU & ~((1U << (ri->size)) - 1U)) | val); - - if(ri->resolution == 1U) - { - return((uint32_t)vs); - } - return((uint32_t)(vs * ri->resolution)); - } - else - { - if(ri->resolution == 1U) - { - return(val); - } - return(val*ri->resolution); - } -} - -/** - * @brief HID_WriteItem - * The function write a report item. - * @param ri: report item - * @param ndx: report index - * @retval status (1: fail/ 0 : Ok) - */ -uint32_t HID_WriteItem(HID_Report_ItemTypedef *ri, uint32_t value, uint8_t ndx) -{ - uint32_t x; - uint32_t mask; - uint32_t bofs; - uint8_t *data=ri->data; - uint8_t shift=ri->shift; - - if (value < ri->physical_min || value > ri->physical_max) - { - return(1U); - } - - /* if this is an array, wee may need to offset ri->data.*/ - if (ri->count > 0U) - { - /* If app tries to read outside of the array. */ - if (ri->count >= ndx) - { - return(0U); - } - /* calculate bit offset */ - bofs = ndx * ri->size; - bofs += shift; - /* calculate byte offset + shift pair from bit offset. */ - data += bofs / 8U; - shift = (uint8_t)(bofs % 8U); - - } - - /* Convert physical value to logical value. */ - if (ri->resolution != 1U) - { - value = value / ri->resolution; - } - - /* Write logical value to report in little endian order. */ - mask = (1U << ri->size) - 1U; - value = (value & mask) << shift; - - for(x = 0U; x < ((ri->size & 0x7U) ? (ri->size / 8U) + 1U : (ri->size / 8U)); x++) - { - *(ri->data + x)=(uint8_t)((*(ri->data+x) & ~(mask>>(x* 8U))) | - ((value >> (x * 8U)) & (mask >> (x * 8U)))); - } - - return 0U; -} - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ - - -/** - * @} - */ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbh_hid_parser.c + * @author MCD Application Team + * @brief This file is the HID Layer Handlers for USB Host HID class. + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + + /* BSPDependencies + - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" + - "stm32xxxxx_{eval}{discovery}_io.c" + - "stm32xxxxx_{eval}{discovery}{adafruit}_lcd.c" + - "stm32xxxxx_{eval}{discovery}_sdram.c" + EndBSPDependencies */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_hid_parser.h" + + +/** @addtogroup USBH_LIB + * @{ + */ + +/** @addtogroup USBH_CLASS + * @{ + */ + +/** @addtogroup USBH_HID_CLASS + * @{ + */ + +/** @defgroup USBH_HID_PARSER + * @brief This file includes HID parsers for USB Host HID class. + * @{ + */ + +/** @defgroup USBH_HID_PARSER_Private_TypesDefinitions + * @{ + */ +/** + * @} + */ + + +/** @defgroup USBH_HID_PARSER_Private_Defines + * @{ + */ +/** + * @} + */ + + +/** @defgroup USBH_HID_PARSER_Private_Macros + * @{ + */ +/** + * @} + */ + +/** @defgroup USBH_HID_PARSER_Private_FunctionPrototypes + * @{ + */ + +/** + * @} + */ + + +/** @defgroup USBH_HID_PARSER_Private_Variables + * @{ + */ + +/** + * @} + */ + + +/** @defgroup USBH_HID_PARSER_Private_Functions + * @{ + */ + +/** + * @brief HID_ReadItem + * The function read a report item. + * @param ri: report item + * @param ndx: report index +* @retval status (0 : fail / otherwise: item value) + */ +uint32_t HID_ReadItem(HID_Report_ItemTypedef *ri, uint8_t ndx) +{ + uint32_t val = 0U; + uint32_t x = 0U; + uint32_t bofs; + uint8_t *data=ri->data; + uint8_t shift=ri->shift; + + /* get the logical value of the item */ + + /* if this is an array, wee may need to offset ri->data.*/ + if (ri->count > 0U) + { + /* If app tries to read outside of the array. */ + if (ri->count <= ndx) + { + return(0U); + } + + /* calculate bit offset */ + bofs = ndx*ri->size; + bofs += shift; + /* calculate byte offset + shift pair from bit offset. */ + data+=bofs / 8U; + shift=(uint8_t)(bofs % 8U); + } + /* read data bytes in little endian order */ + for(x = 0U; x < ((ri->size & 0x7U) ? (ri->size / 8U) + 1U : (ri->size / 8U)); x++) + { + val=(uint32_t)((uint32_t)(*data) << (x * 8U)); + } + val=(val >> shift) & ((1U << ri->size) - 1U); + + if (val < ri->logical_min || val > ri->logical_max) + { + return(0U); + } + + /* convert logical value to physical value */ + /* See if the number is negative or not. */ + if ((ri->sign) && (val & (1U << (ri->size - 1U)))) + { + /* yes, so sign extend value to 32 bits. */ + uint32_t vs=(uint32_t)((0xffffffffU & ~((1U << (ri->size)) - 1U)) | val); + + if(ri->resolution == 1U) + { + return((uint32_t)vs); + } + return((uint32_t)(vs * ri->resolution)); + } + else + { + if(ri->resolution == 1U) + { + return(val); + } + return(val*ri->resolution); + } +} + +/** + * @brief HID_WriteItem + * The function write a report item. + * @param ri: report item + * @param ndx: report index + * @retval status (1: fail/ 0 : Ok) + */ +uint32_t HID_WriteItem(HID_Report_ItemTypedef *ri, uint32_t value, uint8_t ndx) +{ + uint32_t x; + uint32_t mask; + uint32_t bofs; + uint8_t *data=ri->data; + uint8_t shift=ri->shift; + + if (value < ri->physical_min || value > ri->physical_max) + { + return(1U); + } + + /* if this is an array, wee may need to offset ri->data.*/ + if (ri->count > 0U) + { + /* If app tries to read outside of the array. */ + if (ri->count >= ndx) + { + return(0U); + } + /* calculate bit offset */ + bofs = ndx * ri->size; + bofs += shift; + /* calculate byte offset + shift pair from bit offset. */ + data += bofs / 8U; + shift = (uint8_t)(bofs % 8U); + + } + + /* Convert physical value to logical value. */ + if (ri->resolution != 1U) + { + value = value / ri->resolution; + } + + /* Write logical value to report in little endian order. */ + mask = (1U << ri->size) - 1U; + value = (value & mask) << shift; + + for(x = 0U; x < ((ri->size & 0x7U) ? (ri->size / 8U) + 1U : (ri->size / 8U)); x++) + { + *(ri->data + x)=(uint8_t)((*(ri->data+x) & ~(mask>>(x* 8U))) | + ((value >> (x * 8U)) & (mask >> (x * 8U)))); + } + + return 0U; +} + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + + +/** + * @} + */ +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Class/MSC/Inc/usbh_msc.h b/system/Middlewares/ST/STM32_USB_Host_Library/Class/MSC/Inc/usbh_msc.h index 23fd51b9ab..ee51ad98fc 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Class/MSC/Inc/usbh_msc.h +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Class/MSC/Inc/usbh_msc.h @@ -1,225 +1,225 @@ -/** - ****************************************************************************** - * @file usbh_msc.h - * @author MCD Application Team - * @brief This file contains all the prototypes for the usbh_msc.c - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive ----------------------------------------------*/ -#ifndef __USBH_MSC_H -#define __USBH_MSC_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_core.h" -#include "usbh_msc_bot.h" -#include "usbh_msc_scsi.h" - -/** @addtogroup USBH_LIB - * @{ - */ - -/** @addtogroup USBH_CLASS - * @{ - */ - -/** @addtogroup USBH_MSC_CLASS - * @{ - */ - -/** @defgroup USBH_MSC_CORE - * @brief This file is the Header file for usbh_msc.c - * @{ - */ - - -/** @defgroup USBH_MSC_CORE_Exported_Types - * @{ - */ - -typedef enum -{ - MSC_INIT = 0, - MSC_IDLE, - MSC_TEST_UNIT_READY, - MSC_READ_CAPACITY10, - MSC_READ_INQUIRY, - MSC_REQUEST_SENSE, - MSC_READ, - MSC_WRITE, - MSC_UNRECOVERED_ERROR, - MSC_PERIODIC_CHECK, -} -MSC_StateTypeDef; - -typedef enum -{ - MSC_OK, - MSC_NOT_READY, - MSC_ERROR, - -} -MSC_ErrorTypeDef; - -typedef enum -{ - MSC_REQ_IDLE = 0, - MSC_REQ_RESET, - MSC_REQ_GET_MAX_LUN, - MSC_REQ_ERROR, -} -MSC_ReqStateTypeDef; - -#ifndef MAX_SUPPORTED_LUN - #define MAX_SUPPORTED_LUN 2U -#endif - - -/* Structure for LUN */ -typedef struct -{ - MSC_StateTypeDef state; - MSC_ErrorTypeDef error; - USBH_StatusTypeDef prev_ready_state; - SCSI_CapacityTypeDef capacity; - SCSI_SenseTypeDef sense; - SCSI_StdInquiryDataTypeDef inquiry; - uint8_t state_changed; - -} -MSC_LUNTypeDef; - -/* Structure for MSC process */ -typedef struct _MSC_Process -{ - uint32_t max_lun; - uint8_t InPipe; - uint8_t OutPipe; - uint8_t OutEp; - uint8_t InEp; - uint16_t OutEpSize; - uint16_t InEpSize; - MSC_StateTypeDef state; - MSC_ErrorTypeDef error; - MSC_ReqStateTypeDef req_state; - MSC_ReqStateTypeDef prev_req_state; - BOT_HandleTypeDef hbot; - MSC_LUNTypeDef unit[MAX_SUPPORTED_LUN]; - uint16_t current_lun; - uint16_t rw_lun; - uint32_t timer; -} -MSC_HandleTypeDef; - - -/** - * @} - */ - - - -/** @defgroup USBH_MSC_CORE_Exported_Defines - * @{ - */ - -#define USB_REQ_BOT_RESET 0xFFU -#define USB_REQ_GET_MAX_LUN 0xFEU - - -/* MSC Class Codes */ -#define USB_MSC_CLASS 0x08U - -/* Interface Descriptor field values for HID Boot Protocol */ -#define MSC_BOT 0x50U -#define MSC_TRANSPARENT 0x06U -/** - * @} - */ - -/** @defgroup USBH_MSC_CORE_Exported_Macros - * @{ - */ -/** - * @} - */ - -/** @defgroup USBH_MSC_CORE_Exported_Variables - * @{ - */ -extern USBH_ClassTypeDef USBH_msc; -#define USBH_MSC_CLASS &USBH_msc - -/** - * @} - */ - -/** @defgroup USBH_MSC_CORE_Exported_FunctionsPrototype - * @{ - */ - -/* Common APIs */ -uint8_t USBH_MSC_IsReady (USBH_HandleTypeDef *phost); - -/* APIs for LUN */ -uint8_t USBH_MSC_GetMaxLUN (USBH_HandleTypeDef *phost); - -uint8_t USBH_MSC_UnitIsReady (USBH_HandleTypeDef *phost, uint8_t lun); - -USBH_StatusTypeDef USBH_MSC_GetLUNInfo(USBH_HandleTypeDef *phost, uint8_t lun, MSC_LUNTypeDef *info); - -USBH_StatusTypeDef USBH_MSC_Read(USBH_HandleTypeDef *phost, - uint8_t lun, - uint32_t address, - uint8_t *pbuf, - uint32_t length); - -USBH_StatusTypeDef USBH_MSC_Write(USBH_HandleTypeDef *phost, - uint8_t lun, - uint32_t address, - uint8_t *pbuf, - uint32_t length); -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USBH_MSC_H */ - - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - - - +/** + ****************************************************************************** + * @file usbh_msc.h + * @author MCD Application Team + * @brief This file contains all the prototypes for the usbh_msc.c + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive ----------------------------------------------*/ +#ifndef __USBH_MSC_H +#define __USBH_MSC_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_core.h" +#include "usbh_msc_bot.h" +#include "usbh_msc_scsi.h" + +/** @addtogroup USBH_LIB + * @{ + */ + +/** @addtogroup USBH_CLASS + * @{ + */ + +/** @addtogroup USBH_MSC_CLASS + * @{ + */ + +/** @defgroup USBH_MSC_CORE + * @brief This file is the Header file for usbh_msc.c + * @{ + */ + + +/** @defgroup USBH_MSC_CORE_Exported_Types + * @{ + */ + +typedef enum +{ + MSC_INIT = 0, + MSC_IDLE, + MSC_TEST_UNIT_READY, + MSC_READ_CAPACITY10, + MSC_READ_INQUIRY, + MSC_REQUEST_SENSE, + MSC_READ, + MSC_WRITE, + MSC_UNRECOVERED_ERROR, + MSC_PERIODIC_CHECK, +} +MSC_StateTypeDef; + +typedef enum +{ + MSC_OK, + MSC_NOT_READY, + MSC_ERROR, + +} +MSC_ErrorTypeDef; + +typedef enum +{ + MSC_REQ_IDLE = 0, + MSC_REQ_RESET, + MSC_REQ_GET_MAX_LUN, + MSC_REQ_ERROR, +} +MSC_ReqStateTypeDef; + +#ifndef MAX_SUPPORTED_LUN + #define MAX_SUPPORTED_LUN 2U +#endif + + +/* Structure for LUN */ +typedef struct +{ + MSC_StateTypeDef state; + MSC_ErrorTypeDef error; + USBH_StatusTypeDef prev_ready_state; + SCSI_CapacityTypeDef capacity; + SCSI_SenseTypeDef sense; + SCSI_StdInquiryDataTypeDef inquiry; + uint8_t state_changed; + +} +MSC_LUNTypeDef; + +/* Structure for MSC process */ +typedef struct _MSC_Process +{ + uint32_t max_lun; + uint8_t InPipe; + uint8_t OutPipe; + uint8_t OutEp; + uint8_t InEp; + uint16_t OutEpSize; + uint16_t InEpSize; + MSC_StateTypeDef state; + MSC_ErrorTypeDef error; + MSC_ReqStateTypeDef req_state; + MSC_ReqStateTypeDef prev_req_state; + BOT_HandleTypeDef hbot; + MSC_LUNTypeDef unit[MAX_SUPPORTED_LUN]; + uint16_t current_lun; + uint16_t rw_lun; + uint32_t timer; +} +MSC_HandleTypeDef; + + +/** + * @} + */ + + + +/** @defgroup USBH_MSC_CORE_Exported_Defines + * @{ + */ + +#define USB_REQ_BOT_RESET 0xFFU +#define USB_REQ_GET_MAX_LUN 0xFEU + + +/* MSC Class Codes */ +#define USB_MSC_CLASS 0x08U + +/* Interface Descriptor field values for HID Boot Protocol */ +#define MSC_BOT 0x50U +#define MSC_TRANSPARENT 0x06U +/** + * @} + */ + +/** @defgroup USBH_MSC_CORE_Exported_Macros + * @{ + */ +/** + * @} + */ + +/** @defgroup USBH_MSC_CORE_Exported_Variables + * @{ + */ +extern USBH_ClassTypeDef USBH_msc; +#define USBH_MSC_CLASS &USBH_msc + +/** + * @} + */ + +/** @defgroup USBH_MSC_CORE_Exported_FunctionsPrototype + * @{ + */ + +/* Common APIs */ +uint8_t USBH_MSC_IsReady (USBH_HandleTypeDef *phost); + +/* APIs for LUN */ +uint8_t USBH_MSC_GetMaxLUN (USBH_HandleTypeDef *phost); + +uint8_t USBH_MSC_UnitIsReady (USBH_HandleTypeDef *phost, uint8_t lun); + +USBH_StatusTypeDef USBH_MSC_GetLUNInfo(USBH_HandleTypeDef *phost, uint8_t lun, MSC_LUNTypeDef *info); + +USBH_StatusTypeDef USBH_MSC_Read(USBH_HandleTypeDef *phost, + uint8_t lun, + uint32_t address, + uint8_t *pbuf, + uint32_t length); + +USBH_StatusTypeDef USBH_MSC_Write(USBH_HandleTypeDef *phost, + uint8_t lun, + uint32_t address, + uint8_t *pbuf, + uint32_t length); +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USBH_MSC_H */ + + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + + + diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Class/MSC/Inc/usbh_msc_bot.h b/system/Middlewares/ST/STM32_USB_Host_Library/Class/MSC/Inc/usbh_msc_bot.h index f9bb3a4db1..cb440fa645 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Class/MSC/Inc/usbh_msc_bot.h +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Class/MSC/Inc/usbh_msc_bot.h @@ -1,229 +1,229 @@ -/** - ****************************************************************************** - * @file usbh_msc_bot.h - * @author MCD Application Team - * @brief Header file for usbh_msc_bot.c - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive ----------------------------------------------*/ -#ifndef __USBH_MSC_BOT_H -#define __USBH_MSC_BOT_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_core.h" - -/** @addtogroup USBH_LIB - * @{ - */ - -/** @addtogroup USBH_CLASS - * @{ - */ - -/** @addtogroup USBH_MSC_CLASS - * @{ - */ - -/** @defgroup USBH_MSC_BOT - * @brief This file is the Header file for usbh_msc_bot.c - * @{ - */ - - -/** @defgroup USBH_MSC_BOT_Exported_Types - * @{ - */ - -typedef enum { - BOT_OK = 0, - BOT_FAIL = 1, - BOT_PHASE_ERROR = 2, - BOT_BUSY = 3 -} -BOT_StatusTypeDef; - -typedef enum { - BOT_CMD_IDLE = 0, - BOT_CMD_SEND, - BOT_CMD_WAIT, -} -BOT_CMDStateTypeDef; - -/* CSW Status Definitions */ -typedef enum -{ - - BOT_CSW_CMD_PASSED = 0x00, - BOT_CSW_CMD_FAILED = 0x01, - BOT_CSW_PHASE_ERROR = 0x02, -} -BOT_CSWStatusTypeDef; - -typedef enum { - BOT_SEND_CBW = 1, - BOT_SEND_CBW_WAIT, - BOT_DATA_IN, - BOT_DATA_IN_WAIT, - BOT_DATA_OUT, - BOT_DATA_OUT_WAIT, - BOT_RECEIVE_CSW, - BOT_RECEIVE_CSW_WAIT, - BOT_ERROR_IN, - BOT_ERROR_OUT, - BOT_UNRECOVERED_ERROR -} -BOT_StateTypeDef; - -typedef union -{ - struct __CBW - { - uint32_t Signature; - uint32_t Tag; - uint32_t DataTransferLength; - uint8_t Flags; - uint8_t LUN; - uint8_t CBLength; - uint8_t CB[16]; - }field; - uint8_t data[31]; -} -BOT_CBWTypeDef; - -typedef union -{ - struct __CSW - { - uint32_t Signature; - uint32_t Tag; - uint32_t DataResidue; - uint8_t Status; - }field; - uint8_t data[13]; -} -BOT_CSWTypeDef; - -typedef struct -{ - uint32_t data[16]; - BOT_StateTypeDef state; - BOT_StateTypeDef prev_state; - BOT_CMDStateTypeDef cmd_state; - BOT_CBWTypeDef cbw; - uint8_t Reserved1; - BOT_CSWTypeDef csw; - uint8_t Reserved2[3]; - uint8_t *pbuf; -} -BOT_HandleTypeDef; - -/** - * @} - */ - - - -/** @defgroup USBH_MSC_BOT_Exported_Defines - * @{ - */ -#define BOT_CBW_SIGNATURE 0x43425355U -#define BOT_CBW_TAG 0x20304050U -#define BOT_CSW_SIGNATURE 0x53425355U -#define BOT_CBW_LENGTH 31U -#define BOT_CSW_LENGTH 13U - - - -#define BOT_SEND_CSW_DISABLE 0U -#define BOT_SEND_CSW_ENABLE 1U - -#define BOT_DIR_IN 0U -#define BOT_DIR_OUT 1U -#define BOT_DIR_BOTH 2U - -#define BOT_PAGE_LENGTH 512U - - -#define BOT_CBW_CB_LENGTH 16U - - -#define MAX_BULK_STALL_COUNT_LIMIT 0x04U /* If STALL is seen on Bulk - Endpoint continuously, this means - that device and Host has phase error - Hence a Reset is needed */ - -/** - * @} - */ - -/** @defgroup USBH_MSC_BOT_Exported_Macros - * @{ - */ -/** - * @} - */ - -/** @defgroup USBH_MSC_BOT_Exported_Variables - * @{ - */ - -/** - * @} - */ - -/** @defgroup USBH_MSC_BOT_Exported_FunctionsPrototype - * @{ - */ -USBH_StatusTypeDef USBH_MSC_BOT_REQ_Reset(USBH_HandleTypeDef *phost); -USBH_StatusTypeDef USBH_MSC_BOT_REQ_GetMaxLUN(USBH_HandleTypeDef *phost, uint8_t *Maxlun); - -USBH_StatusTypeDef USBH_MSC_BOT_Init(USBH_HandleTypeDef *phost); -USBH_StatusTypeDef USBH_MSC_BOT_Process (USBH_HandleTypeDef *phost, uint8_t lun); -USBH_StatusTypeDef USBH_MSC_BOT_Error(USBH_HandleTypeDef *phost, uint8_t lun); - - - -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USBH_MSC_BOT_H__ */ - - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - +/** + ****************************************************************************** + * @file usbh_msc_bot.h + * @author MCD Application Team + * @brief Header file for usbh_msc_bot.c + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive ----------------------------------------------*/ +#ifndef __USBH_MSC_BOT_H +#define __USBH_MSC_BOT_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_core.h" + +/** @addtogroup USBH_LIB + * @{ + */ + +/** @addtogroup USBH_CLASS + * @{ + */ + +/** @addtogroup USBH_MSC_CLASS + * @{ + */ + +/** @defgroup USBH_MSC_BOT + * @brief This file is the Header file for usbh_msc_bot.c + * @{ + */ + + +/** @defgroup USBH_MSC_BOT_Exported_Types + * @{ + */ + +typedef enum { + BOT_OK = 0, + BOT_FAIL = 1, + BOT_PHASE_ERROR = 2, + BOT_BUSY = 3 +} +BOT_StatusTypeDef; + +typedef enum { + BOT_CMD_IDLE = 0, + BOT_CMD_SEND, + BOT_CMD_WAIT, +} +BOT_CMDStateTypeDef; + +/* CSW Status Definitions */ +typedef enum +{ + + BOT_CSW_CMD_PASSED = 0x00, + BOT_CSW_CMD_FAILED = 0x01, + BOT_CSW_PHASE_ERROR = 0x02, +} +BOT_CSWStatusTypeDef; + +typedef enum { + BOT_SEND_CBW = 1, + BOT_SEND_CBW_WAIT, + BOT_DATA_IN, + BOT_DATA_IN_WAIT, + BOT_DATA_OUT, + BOT_DATA_OUT_WAIT, + BOT_RECEIVE_CSW, + BOT_RECEIVE_CSW_WAIT, + BOT_ERROR_IN, + BOT_ERROR_OUT, + BOT_UNRECOVERED_ERROR +} +BOT_StateTypeDef; + +typedef union +{ + struct __CBW + { + uint32_t Signature; + uint32_t Tag; + uint32_t DataTransferLength; + uint8_t Flags; + uint8_t LUN; + uint8_t CBLength; + uint8_t CB[16]; + }field; + uint8_t data[31]; +} +BOT_CBWTypeDef; + +typedef union +{ + struct __CSW + { + uint32_t Signature; + uint32_t Tag; + uint32_t DataResidue; + uint8_t Status; + }field; + uint8_t data[13]; +} +BOT_CSWTypeDef; + +typedef struct +{ + uint32_t data[16]; + BOT_StateTypeDef state; + BOT_StateTypeDef prev_state; + BOT_CMDStateTypeDef cmd_state; + BOT_CBWTypeDef cbw; + uint8_t Reserved1; + BOT_CSWTypeDef csw; + uint8_t Reserved2[3]; + uint8_t *pbuf; +} +BOT_HandleTypeDef; + +/** + * @} + */ + + + +/** @defgroup USBH_MSC_BOT_Exported_Defines + * @{ + */ +#define BOT_CBW_SIGNATURE 0x43425355U +#define BOT_CBW_TAG 0x20304050U +#define BOT_CSW_SIGNATURE 0x53425355U +#define BOT_CBW_LENGTH 31U +#define BOT_CSW_LENGTH 13U + + + +#define BOT_SEND_CSW_DISABLE 0U +#define BOT_SEND_CSW_ENABLE 1U + +#define BOT_DIR_IN 0U +#define BOT_DIR_OUT 1U +#define BOT_DIR_BOTH 2U + +#define BOT_PAGE_LENGTH 512U + + +#define BOT_CBW_CB_LENGTH 16U + + +#define MAX_BULK_STALL_COUNT_LIMIT 0x04U /* If STALL is seen on Bulk + Endpoint continuously, this means + that device and Host has phase error + Hence a Reset is needed */ + +/** + * @} + */ + +/** @defgroup USBH_MSC_BOT_Exported_Macros + * @{ + */ +/** + * @} + */ + +/** @defgroup USBH_MSC_BOT_Exported_Variables + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBH_MSC_BOT_Exported_FunctionsPrototype + * @{ + */ +USBH_StatusTypeDef USBH_MSC_BOT_REQ_Reset(USBH_HandleTypeDef *phost); +USBH_StatusTypeDef USBH_MSC_BOT_REQ_GetMaxLUN(USBH_HandleTypeDef *phost, uint8_t *Maxlun); + +USBH_StatusTypeDef USBH_MSC_BOT_Init(USBH_HandleTypeDef *phost); +USBH_StatusTypeDef USBH_MSC_BOT_Process (USBH_HandleTypeDef *phost, uint8_t lun); +USBH_StatusTypeDef USBH_MSC_BOT_Error(USBH_HandleTypeDef *phost, uint8_t lun); + + + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USBH_MSC_BOT_H__ */ + + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Class/MSC/Inc/usbh_msc_scsi.h b/system/Middlewares/ST/STM32_USB_Host_Library/Class/MSC/Inc/usbh_msc_scsi.h index f5c28d01b0..19dff14607 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Class/MSC/Inc/usbh_msc_scsi.h +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Class/MSC/Inc/usbh_msc_scsi.h @@ -1,218 +1,218 @@ -/** - ****************************************************************************** - * @file usbh_msc_scsi.h - * @author MCD Application Team - * @brief Header file for usbh_msc_scsi.c - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive ----------------------------------------------*/ -#ifndef __USBH_MSC_SCSI_H -#define __USBH_MSC_SCSI_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_core.h" - - -/** @addtogroup USBH_LIB - * @{ - */ - -/** @addtogroup USBH_CLASS - * @{ - */ - -/** @addtogroup USBH_MSC_CLASS - * @{ - */ - -/** @defgroup USBH_MSC_SCSI - * @brief This file is the Header file for usbh_msc_scsi.c - * @{ - */ - - -/* Capacity data */ -typedef struct -{ - uint32_t block_nbr; - uint16_t block_size; -} SCSI_CapacityTypeDef; - - -/* Sense data */ -typedef struct -{ - uint8_t key; - uint8_t asc; - uint8_t ascq; -} SCSI_SenseTypeDef; - -/* INQUIRY data */ -typedef struct -{ - uint8_t PeripheralQualifier; - uint8_t DeviceType; - uint8_t RemovableMedia; - uint8_t vendor_id[9]; - uint8_t product_id[17]; - uint8_t revision_id[5]; -}SCSI_StdInquiryDataTypeDef; - -/** @defgroup USBH_MSC_SCSI_Exported_Defines - * @{ - */ -#define OPCODE_TEST_UNIT_READY 0x00U -#define OPCODE_READ_CAPACITY10 0x25U -#define OPCODE_READ10 0x28U -#define OPCODE_WRITE10 0x2AU -#define OPCODE_REQUEST_SENSE 0x03U -#define OPCODE_INQUIRY 0x12U - -#define DATA_LEN_MODE_TEST_UNIT_READY 0U -#define DATA_LEN_READ_CAPACITY10 8U -#define DATA_LEN_INQUIRY 36U -#define DATA_LEN_REQUEST_SENSE 14U - -#define CBW_CB_LENGTH 16U -#define CBW_LENGTH 10U - -/** @defgroup USBH_MSC_SCSI_Exported_Defines - * @{ - */ -#define SCSI_SENSE_KEY_NO_SENSE 0x00U -#define SCSI_SENSE_KEY_RECOVERED_ERROR 0x01U -#define SCSI_SENSE_KEY_NOT_READY 0x02U -#define SCSI_SENSE_KEY_MEDIUM_ERROR 0x03U -#define SCSI_SENSE_KEY_HARDWARE_ERROR 0x04U -#define SCSI_SENSE_KEY_ILLEGAL_REQUEST 0x05U -#define SCSI_SENSE_KEY_UNIT_ATTENTION 0x06U -#define SCSI_SENSE_KEY_DATA_PROTECT 0x07U -#define SCSI_SENSE_KEY_BLANK_CHECK 0x08U -#define SCSI_SENSE_KEY_VENDOR_SPECIFIC 0x09U -#define SCSI_SENSE_KEY_COPY_ABORTED 0x0AU -#define SCSI_SENSE_KEY_ABORTED_COMMAND 0x0BU -#define SCSI_SENSE_KEY_VOLUME_OVERFLOW 0x0DU -#define SCSI_SENSE_KEY_MISCOMPARE 0x0EU -/** - * @} - */ - - -/** @defgroup USBH_MSC_SCSI_Exported_Defines - * @{ - */ -#define SCSI_ASC_NO_ADDITIONAL_SENSE_INFORMATION 0x00 -#define SCSI_ASC_LOGICAL_UNIT_NOT_READY 0x04 -#define SCSI_ASC_INVALID_FIELD_IN_CDB 0x24 -#define SCSI_ASC_WRITE_PROTECTED 0x27 -#define SCSI_ASC_FORMAT_ERROR 0x31 -#define SCSI_ASC_INVALID_COMMAND_OPERATION_CODE 0x20 -#define SCSI_ASC_NOT_READY_TO_READY_CHANGE 0x28 -#define SCSI_ASC_MEDIUM_NOT_PRESENT 0x3A -/** - * @} - */ - - -/** @defgroup USBH_MSC_SCSI_Exported_Defines - * @{ - */ -#define SCSI_ASCQ_FORMAT_COMMAND_FAILED 0x01 -#define SCSI_ASCQ_INITIALIZING_COMMAND_REQUIRED 0x02 -#define SCSI_ASCQ_OPERATION_IN_PROGRESS 0x07 - -/** - * @} - */ - -/** @defgroup USBH_MSC_SCSI_Exported_Macros - * @{ - */ -/** - * @} - */ - -/** @defgroup _Exported_Variables - * @{ - */ - -/** - * @} - */ - -/** @defgroup USBH_MSC_SCSI_Exported_FunctionsPrototype - * @{ - */ -USBH_StatusTypeDef USBH_MSC_SCSI_TestUnitReady (USBH_HandleTypeDef *phost, - uint8_t lun); - -USBH_StatusTypeDef USBH_MSC_SCSI_ReadCapacity (USBH_HandleTypeDef *phost, - uint8_t lun, - SCSI_CapacityTypeDef *capacity); - -USBH_StatusTypeDef USBH_MSC_SCSI_Inquiry (USBH_HandleTypeDef *phost, - uint8_t lun, - SCSI_StdInquiryDataTypeDef *inquiry); - -USBH_StatusTypeDef USBH_MSC_SCSI_RequestSense (USBH_HandleTypeDef *phost, - uint8_t lun, - SCSI_SenseTypeDef *sense_data); - -USBH_StatusTypeDef USBH_MSC_SCSI_Write(USBH_HandleTypeDef *phost, - uint8_t lun, - uint32_t address, - uint8_t *pbuf, - uint32_t length); - -USBH_StatusTypeDef USBH_MSC_SCSI_Read(USBH_HandleTypeDef *phost, - uint8_t lun, - uint32_t address, - uint8_t *pbuf, - uint32_t length); - - -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USBH_MSC_SCSI_H */ - - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - +/** + ****************************************************************************** + * @file usbh_msc_scsi.h + * @author MCD Application Team + * @brief Header file for usbh_msc_scsi.c + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive ----------------------------------------------*/ +#ifndef __USBH_MSC_SCSI_H +#define __USBH_MSC_SCSI_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_core.h" + + +/** @addtogroup USBH_LIB + * @{ + */ + +/** @addtogroup USBH_CLASS + * @{ + */ + +/** @addtogroup USBH_MSC_CLASS + * @{ + */ + +/** @defgroup USBH_MSC_SCSI + * @brief This file is the Header file for usbh_msc_scsi.c + * @{ + */ + + +/* Capacity data */ +typedef struct +{ + uint32_t block_nbr; + uint16_t block_size; +} SCSI_CapacityTypeDef; + + +/* Sense data */ +typedef struct +{ + uint8_t key; + uint8_t asc; + uint8_t ascq; +} SCSI_SenseTypeDef; + +/* INQUIRY data */ +typedef struct +{ + uint8_t PeripheralQualifier; + uint8_t DeviceType; + uint8_t RemovableMedia; + uint8_t vendor_id[9]; + uint8_t product_id[17]; + uint8_t revision_id[5]; +}SCSI_StdInquiryDataTypeDef; + +/** @defgroup USBH_MSC_SCSI_Exported_Defines + * @{ + */ +#define OPCODE_TEST_UNIT_READY 0x00U +#define OPCODE_READ_CAPACITY10 0x25U +#define OPCODE_READ10 0x28U +#define OPCODE_WRITE10 0x2AU +#define OPCODE_REQUEST_SENSE 0x03U +#define OPCODE_INQUIRY 0x12U + +#define DATA_LEN_MODE_TEST_UNIT_READY 0U +#define DATA_LEN_READ_CAPACITY10 8U +#define DATA_LEN_INQUIRY 36U +#define DATA_LEN_REQUEST_SENSE 14U + +#define CBW_CB_LENGTH 16U +#define CBW_LENGTH 10U + +/** @defgroup USBH_MSC_SCSI_Exported_Defines + * @{ + */ +#define SCSI_SENSE_KEY_NO_SENSE 0x00U +#define SCSI_SENSE_KEY_RECOVERED_ERROR 0x01U +#define SCSI_SENSE_KEY_NOT_READY 0x02U +#define SCSI_SENSE_KEY_MEDIUM_ERROR 0x03U +#define SCSI_SENSE_KEY_HARDWARE_ERROR 0x04U +#define SCSI_SENSE_KEY_ILLEGAL_REQUEST 0x05U +#define SCSI_SENSE_KEY_UNIT_ATTENTION 0x06U +#define SCSI_SENSE_KEY_DATA_PROTECT 0x07U +#define SCSI_SENSE_KEY_BLANK_CHECK 0x08U +#define SCSI_SENSE_KEY_VENDOR_SPECIFIC 0x09U +#define SCSI_SENSE_KEY_COPY_ABORTED 0x0AU +#define SCSI_SENSE_KEY_ABORTED_COMMAND 0x0BU +#define SCSI_SENSE_KEY_VOLUME_OVERFLOW 0x0DU +#define SCSI_SENSE_KEY_MISCOMPARE 0x0EU +/** + * @} + */ + + +/** @defgroup USBH_MSC_SCSI_Exported_Defines + * @{ + */ +#define SCSI_ASC_NO_ADDITIONAL_SENSE_INFORMATION 0x00 +#define SCSI_ASC_LOGICAL_UNIT_NOT_READY 0x04 +#define SCSI_ASC_INVALID_FIELD_IN_CDB 0x24 +#define SCSI_ASC_WRITE_PROTECTED 0x27 +#define SCSI_ASC_FORMAT_ERROR 0x31 +#define SCSI_ASC_INVALID_COMMAND_OPERATION_CODE 0x20 +#define SCSI_ASC_NOT_READY_TO_READY_CHANGE 0x28 +#define SCSI_ASC_MEDIUM_NOT_PRESENT 0x3A +/** + * @} + */ + + +/** @defgroup USBH_MSC_SCSI_Exported_Defines + * @{ + */ +#define SCSI_ASCQ_FORMAT_COMMAND_FAILED 0x01 +#define SCSI_ASCQ_INITIALIZING_COMMAND_REQUIRED 0x02 +#define SCSI_ASCQ_OPERATION_IN_PROGRESS 0x07 + +/** + * @} + */ + +/** @defgroup USBH_MSC_SCSI_Exported_Macros + * @{ + */ +/** + * @} + */ + +/** @defgroup _Exported_Variables + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBH_MSC_SCSI_Exported_FunctionsPrototype + * @{ + */ +USBH_StatusTypeDef USBH_MSC_SCSI_TestUnitReady (USBH_HandleTypeDef *phost, + uint8_t lun); + +USBH_StatusTypeDef USBH_MSC_SCSI_ReadCapacity (USBH_HandleTypeDef *phost, + uint8_t lun, + SCSI_CapacityTypeDef *capacity); + +USBH_StatusTypeDef USBH_MSC_SCSI_Inquiry (USBH_HandleTypeDef *phost, + uint8_t lun, + SCSI_StdInquiryDataTypeDef *inquiry); + +USBH_StatusTypeDef USBH_MSC_SCSI_RequestSense (USBH_HandleTypeDef *phost, + uint8_t lun, + SCSI_SenseTypeDef *sense_data); + +USBH_StatusTypeDef USBH_MSC_SCSI_Write(USBH_HandleTypeDef *phost, + uint8_t lun, + uint32_t address, + uint8_t *pbuf, + uint32_t length); + +USBH_StatusTypeDef USBH_MSC_SCSI_Read(USBH_HandleTypeDef *phost, + uint8_t lun, + uint32_t address, + uint8_t *pbuf, + uint32_t length); + + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USBH_MSC_SCSI_H */ + + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Class/MSC/Src/usbh_msc.c b/system/Middlewares/ST/STM32_USB_Host_Library/Class/MSC/Src/usbh_msc.c index 1d4409d82a..482126a00d 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Class/MSC/Src/usbh_msc.c +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Class/MSC/Src/usbh_msc.c @@ -1,858 +1,858 @@ -/** - ****************************************************************************** - * @file usbh_msc.c - * @author MCD Application Team - * @brief This file implements the MSC class driver functions - * =================================================================== - * MSC Class Description - * =================================================================== - * This module manages the MSC class V1.0 following the "Universal - * Serial Bus Mass Storage Class (MSC) Bulk-Only Transport (BOT) Version 1.0 - * Sep. 31, 1999". - * This driver implements the following aspects of the specification: - * - Bulk-Only Transport protocol - * - Subclass : SCSI transparent command set (ref. SCSI Primary Commands - 3 (SPC-3)) - * - * @endverbatim - * - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - - /* BSPDependencies - - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" - - "stm32xxxxx_{eval}{discovery}_io.c" - - "stm32xxxxx_{eval}{discovery}{adafruit}_lcd.c" - - "stm32xxxxx_{eval}{discovery}_sdram.c" - EndBSPDependencies */ - -/* Includes ------------------------------------------------------------------*/ - -#include "usbh_msc.h" -#include "usbh_msc_bot.h" -#include "usbh_msc_scsi.h" - - -/** @addtogroup USBH_LIB - * @{ - */ - -/** @addtogroup USBH_CLASS - * @{ - */ - -/** @addtogroup USBH_MSC_CLASS - * @{ - */ - -/** @defgroup USBH_MSC_CORE - * @brief This file includes the mass storage related functions - * @{ - */ - - -/** @defgroup USBH_MSC_CORE_Private_TypesDefinitions - * @{ - */ -/** - * @} - */ - -/** @defgroup USBH_MSC_CORE_Private_Defines - * @{ - */ -/** - * @} - */ - -/** @defgroup USBH_MSC_CORE_Private_Macros - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBH_MSC_CORE_Private_Variables - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBH_MSC_CORE_Private_FunctionPrototypes - * @{ - */ - -static USBH_StatusTypeDef USBH_MSC_InterfaceInit (USBH_HandleTypeDef *phost); - -static USBH_StatusTypeDef USBH_MSC_InterfaceDeInit (USBH_HandleTypeDef *phost); - -static USBH_StatusTypeDef USBH_MSC_Process(USBH_HandleTypeDef *phost); - -static USBH_StatusTypeDef USBH_MSC_ClassRequest(USBH_HandleTypeDef *phost); - -static USBH_StatusTypeDef USBH_MSC_SOFProcess(USBH_HandleTypeDef *phost); - -static USBH_StatusTypeDef USBH_MSC_RdWrProcess(USBH_HandleTypeDef *phost, uint8_t lun); - -USBH_ClassTypeDef USBH_msc = -{ - "MSC", - USB_MSC_CLASS, - USBH_MSC_InterfaceInit, - USBH_MSC_InterfaceDeInit, - USBH_MSC_ClassRequest, - USBH_MSC_Process, - USBH_MSC_SOFProcess, - NULL, -}; - - -/** - * @} - */ - - -/** @defgroup USBH_MSC_CORE_Exported_Variables - * @{ - */ - -/** - * @} - */ - - -/** @defgroup USBH_MSC_CORE_Private_Functions - * @{ - */ - - -/** - * @brief USBH_MSC_InterfaceInit - * The function init the MSC class. - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_MSC_InterfaceInit (USBH_HandleTypeDef *phost) -{ - uint8_t interface = 0U; - USBH_StatusTypeDef status = USBH_FAIL; - MSC_HandleTypeDef *MSC_Handle; - - interface = USBH_FindInterface(phost, phost->pActiveClass->ClassCode, MSC_TRANSPARENT, MSC_BOT); - - if(interface == 0xFFU) /* Not Valid Interface */ - { - USBH_DbgLog ("Cannot Find the interface for %s class.", phost->pActiveClass->Name); - status = USBH_FAIL; - } - else - { - USBH_SelectInterface (phost, interface); - - phost->pActiveClass->pData = (MSC_HandleTypeDef *)USBH_malloc (sizeof(MSC_HandleTypeDef)); - MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; - - if(phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[0].bEndpointAddress & 0x80U) - { - MSC_Handle->InEp = (phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[0].bEndpointAddress); - MSC_Handle->InEpSize = phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[0].wMaxPacketSize; - } - else - { - MSC_Handle->OutEp = (phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[0].bEndpointAddress); - MSC_Handle->OutEpSize = phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[0].wMaxPacketSize; - } - - if(phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[1].bEndpointAddress & 0x80U) - { - MSC_Handle->InEp = (phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[1].bEndpointAddress); - MSC_Handle->InEpSize = phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[1].wMaxPacketSize; - } - else - { - MSC_Handle->OutEp = (phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[1].bEndpointAddress); - MSC_Handle->OutEpSize = phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[1].wMaxPacketSize; - } - - MSC_Handle->current_lun = 0U; - MSC_Handle->rw_lun = 0U; - MSC_Handle->state = MSC_INIT; - MSC_Handle->error = MSC_OK; - MSC_Handle->req_state = MSC_REQ_IDLE; - MSC_Handle->OutPipe = USBH_AllocPipe(phost, MSC_Handle->OutEp); - MSC_Handle->InPipe = USBH_AllocPipe(phost, MSC_Handle->InEp); - - USBH_MSC_BOT_Init(phost); - - /* De-Initialize LUNs information */ - USBH_memset(MSC_Handle->unit, 0, sizeof(MSC_Handle->unit)); - - /* Open the new channels */ - USBH_OpenPipe (phost, - MSC_Handle->OutPipe, - MSC_Handle->OutEp, - phost->device.address, - phost->device.speed, - USB_EP_TYPE_BULK, - MSC_Handle->OutEpSize); - - USBH_OpenPipe (phost, - MSC_Handle->InPipe, - MSC_Handle->InEp, - phost->device.address, - phost->device.speed, - USB_EP_TYPE_BULK, - MSC_Handle->InEpSize); - - USBH_LL_SetToggle (phost, MSC_Handle->InPipe, 0U); - USBH_LL_SetToggle (phost, MSC_Handle->OutPipe, 0U); - status = USBH_OK; - } - return status; -} - -/** - * @brief USBH_MSC_InterfaceDeInit - * The function DeInit the Pipes used for the MSC class. - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_MSC_InterfaceDeInit (USBH_HandleTypeDef *phost) -{ - MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; - - if ( MSC_Handle->OutPipe) - { - USBH_ClosePipe(phost, MSC_Handle->OutPipe); - USBH_FreePipe (phost, MSC_Handle->OutPipe); - MSC_Handle->OutPipe = 0U; /* Reset the Channel as Free */ - } - - if ( MSC_Handle->InPipe) - { - USBH_ClosePipe(phost, MSC_Handle->InPipe); - USBH_FreePipe (phost, MSC_Handle->InPipe); - MSC_Handle->InPipe = 0U; /* Reset the Channel as Free */ - } - - if(phost->pActiveClass->pData) - { - USBH_free (phost->pActiveClass->pData); - phost->pActiveClass->pData = 0; - } - - return USBH_OK; -} - -/** - * @brief USBH_MSC_ClassRequest - * The function is responsible for handling Standard requests - * for MSC class. - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_MSC_ClassRequest(USBH_HandleTypeDef *phost) -{ - MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; - USBH_StatusTypeDef status = USBH_BUSY; - uint8_t i; - - /* Switch MSC REQ state machine */ - switch (MSC_Handle->req_state) - { - case MSC_REQ_IDLE: - case MSC_REQ_GET_MAX_LUN: - /* Issue GetMaxLUN request */ - status = USBH_MSC_BOT_REQ_GetMaxLUN(phost, (uint8_t *)(void *)&MSC_Handle->max_lun); - - /* When devices do not support the GetMaxLun request, this should - be considred as only one logical unit is supported */ - if(status == USBH_NOT_SUPPORTED) - { - MSC_Handle->max_lun = 0U; - status = USBH_OK; - } - - if(status == USBH_OK) - { - MSC_Handle->max_lun = (MSC_Handle->max_lun > MAX_SUPPORTED_LUN)? MAX_SUPPORTED_LUN : (uint8_t )(MSC_Handle->max_lun) + 1U; - USBH_UsrLog ("Number of supported LUN: %lu", (int32_t)(MSC_Handle->max_lun)); - - for(i = 0U; i < MSC_Handle->max_lun; i++) - { - MSC_Handle->unit[i].prev_ready_state = USBH_FAIL; - MSC_Handle->unit[i].state_changed = 0U; - } - } - break; - - case MSC_REQ_ERROR : - /* a Clear Feature should be issued here */ - if(USBH_ClrFeature(phost, 0x00U) == USBH_OK) - { - MSC_Handle->req_state = MSC_Handle->prev_req_state; - } - break; - - default: - break; - } - - return status; -} - -/** - * @brief USBH_MSC_Process - * The function is for managing state machine for MSC data transfers - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_MSC_Process(USBH_HandleTypeDef *phost) -{ - MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; - USBH_StatusTypeDef error = USBH_BUSY ; - USBH_StatusTypeDef scsi_status = USBH_BUSY ; - USBH_StatusTypeDef ready_status = USBH_BUSY ; - - switch (MSC_Handle->state) - { - case MSC_INIT: - - if(MSC_Handle->current_lun < MSC_Handle->max_lun) - { - - MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_NOT_READY; - /* Switch MSC REQ state machine */ - switch (MSC_Handle->unit[MSC_Handle->current_lun].state) - { - case MSC_INIT: - USBH_UsrLog ("LUN #%d: ", MSC_Handle->current_lun); - MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_READ_INQUIRY; - MSC_Handle->timer = phost->Timer; - break; - - case MSC_READ_INQUIRY: - scsi_status = USBH_MSC_SCSI_Inquiry(phost, (uint8_t)MSC_Handle->current_lun, &MSC_Handle->unit[MSC_Handle->current_lun].inquiry); - - if (scsi_status == USBH_OK) - { - USBH_UsrLog ("Inquiry Vendor : %s", MSC_Handle->unit[MSC_Handle->current_lun].inquiry.vendor_id); - USBH_UsrLog ("Inquiry Product : %s", MSC_Handle->unit[MSC_Handle->current_lun].inquiry.product_id); - USBH_UsrLog ("Inquiry Version : %s", MSC_Handle->unit[MSC_Handle->current_lun].inquiry.revision_id); - MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_TEST_UNIT_READY; - } - if (scsi_status == USBH_FAIL) - { - MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_REQUEST_SENSE; - } - else - { - if(scsi_status == USBH_UNRECOVERED_ERROR) - { - MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_IDLE; - MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_ERROR; - } - } - break; - - case MSC_TEST_UNIT_READY: - ready_status = USBH_MSC_SCSI_TestUnitReady(phost, (uint8_t)MSC_Handle->current_lun); - - if (ready_status == USBH_OK) - { - if(MSC_Handle->unit[MSC_Handle->current_lun].prev_ready_state != USBH_OK) - { - MSC_Handle->unit[MSC_Handle->current_lun].state_changed = 1U; - USBH_UsrLog ("MSC Device ready"); - } - else - { - MSC_Handle->unit[MSC_Handle->current_lun].state_changed = 0U; - } - MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_READ_CAPACITY10; - MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_OK; - MSC_Handle->unit[MSC_Handle->current_lun].prev_ready_state = USBH_OK; - } - if (ready_status == USBH_FAIL) - { - /* Media not ready, so try to check again during 10s */ - if( MSC_Handle->unit[MSC_Handle->current_lun].prev_ready_state != USBH_FAIL) - { - MSC_Handle->unit[MSC_Handle->current_lun].state_changed = 1U; - USBH_UsrLog ("MSC Device NOT ready"); - } - else - { - MSC_Handle->unit[MSC_Handle->current_lun].state_changed = 0U; - } - MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_REQUEST_SENSE; - MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_NOT_READY; - MSC_Handle->unit[MSC_Handle->current_lun].prev_ready_state = USBH_FAIL; - } - else - { - if (ready_status == USBH_UNRECOVERED_ERROR) - { - MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_IDLE; - MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_ERROR; - } - } - break; - - case MSC_READ_CAPACITY10: - scsi_status = USBH_MSC_SCSI_ReadCapacity(phost, (uint8_t)MSC_Handle->current_lun, &MSC_Handle->unit[MSC_Handle->current_lun].capacity) ; - - if(scsi_status == USBH_OK) - { - if(MSC_Handle->unit[MSC_Handle->current_lun].state_changed == 1U) - { - USBH_UsrLog ("MSC Device capacity : %lu Bytes", \ - (int32_t)(MSC_Handle->unit[MSC_Handle->current_lun].capacity.block_nbr * MSC_Handle->unit[MSC_Handle->current_lun].capacity.block_size)); - USBH_UsrLog ("Block number : %lu", (int32_t)(MSC_Handle->unit[MSC_Handle->current_lun].capacity.block_nbr)); - USBH_UsrLog ("Block Size : %lu", (int32_t)(MSC_Handle->unit[MSC_Handle->current_lun].capacity.block_size)); - } - MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_IDLE; - MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_OK; - MSC_Handle->current_lun++; - } - else if(scsi_status == USBH_FAIL) - { - MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_REQUEST_SENSE; - } - else - { - if(scsi_status == USBH_UNRECOVERED_ERROR) - { - MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_IDLE; - MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_ERROR; - } - } - break; - - case MSC_REQUEST_SENSE: - scsi_status = USBH_MSC_SCSI_RequestSense(phost, (uint8_t)MSC_Handle->current_lun, &MSC_Handle->unit[MSC_Handle->current_lun].sense); - - if( scsi_status == USBH_OK) - { - if((MSC_Handle->unit[MSC_Handle->current_lun].sense.key == SCSI_SENSE_KEY_UNIT_ATTENTION) || - (MSC_Handle->unit[MSC_Handle->current_lun].sense.key == SCSI_SENSE_KEY_NOT_READY) ) - { - - if((phost->Timer - MSC_Handle->timer) < 10000U) - { - MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_TEST_UNIT_READY; - break; - } - } - - USBH_UsrLog ("Sense Key : %x", MSC_Handle->unit[MSC_Handle->current_lun].sense.key); - USBH_UsrLog ("Additional Sense Code : %x", MSC_Handle->unit[MSC_Handle->current_lun].sense.asc); - USBH_UsrLog ("Additional Sense Code Qualifier: %x", MSC_Handle->unit[MSC_Handle->current_lun].sense.ascq); - MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_IDLE; - MSC_Handle->current_lun++; - } - if(scsi_status == USBH_FAIL) - { - USBH_UsrLog ("MSC Device NOT ready"); - MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_UNRECOVERED_ERROR; - } - else - { - if(scsi_status == USBH_UNRECOVERED_ERROR) - { - MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_IDLE; - MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_ERROR; - } - } - break; - - case MSC_UNRECOVERED_ERROR: - MSC_Handle->current_lun++; - break; - - default: - break; - } - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CLASS_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - else - { - MSC_Handle->current_lun = 0U; - MSC_Handle->state = MSC_IDLE; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CLASS_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - phost->pUser(phost, HOST_USER_CLASS_ACTIVE); - } - break; - - case MSC_IDLE: - error = USBH_OK; - break; - - default: - break; - } - return error; -} - - -/** - * @brief USBH_MSC_SOFProcess - * The function is for SOF state - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_MSC_SOFProcess(USBH_HandleTypeDef *phost) -{ - - return USBH_OK; -} -/** - * @brief USBH_MSC_RdWrProcess - * The function is for managing state machine for MSC I/O Process - * @param phost: Host handle - * @param lun: logical Unit Number - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_MSC_RdWrProcess(USBH_HandleTypeDef *phost, uint8_t lun) -{ - MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; - USBH_StatusTypeDef error = USBH_BUSY ; - USBH_StatusTypeDef scsi_status = USBH_BUSY ; - - /* Switch MSC REQ state machine */ - switch (MSC_Handle->unit[lun].state) - { - - case MSC_READ: - scsi_status = USBH_MSC_SCSI_Read(phost,lun, 0U, NULL, 0U); - - if(scsi_status == USBH_OK) - { - MSC_Handle->unit[lun].state = MSC_IDLE; - error = USBH_OK; - } - else if( scsi_status == USBH_FAIL) - { - MSC_Handle->unit[lun].state = MSC_REQUEST_SENSE; - } - else - { - if(scsi_status == USBH_UNRECOVERED_ERROR) - { - MSC_Handle->unit[lun].state = MSC_UNRECOVERED_ERROR; - error = USBH_FAIL; - } - } - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CLASS_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - case MSC_WRITE: - scsi_status = USBH_MSC_SCSI_Write(phost,lun, 0U, NULL, 0U); - - if(scsi_status == USBH_OK) - { - MSC_Handle->unit[lun].state = MSC_IDLE; - error = USBH_OK; - } - else if( scsi_status == USBH_FAIL) - { - MSC_Handle->unit[lun].state = MSC_REQUEST_SENSE; - } - else - { - if(scsi_status == USBH_UNRECOVERED_ERROR) - { - MSC_Handle->unit[lun].state = MSC_UNRECOVERED_ERROR; - error = USBH_FAIL; - } - } - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CLASS_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - case MSC_REQUEST_SENSE: - scsi_status = USBH_MSC_SCSI_RequestSense(phost, lun, &MSC_Handle->unit[lun].sense); - - if(scsi_status == USBH_OK) - { - USBH_UsrLog ("Sense Key : %x", MSC_Handle->unit[lun].sense.key); - USBH_UsrLog ("Additional Sense Code : %x", MSC_Handle->unit[lun].sense.asc); - USBH_UsrLog ("Additional Sense Code Qualifier: %x", MSC_Handle->unit[lun].sense.ascq); - MSC_Handle->unit[lun].state = MSC_IDLE; - MSC_Handle->unit[lun].error = MSC_ERROR; - - error = USBH_FAIL; - } - if(scsi_status == USBH_FAIL) - { - USBH_UsrLog ("MSC Device NOT ready"); - } - else - { - if(scsi_status == USBH_UNRECOVERED_ERROR) - { - MSC_Handle->unit[lun].state = MSC_UNRECOVERED_ERROR; - error = USBH_FAIL; - } - } - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CLASS_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - default: - break; - - } - return error; -} - -/** - * @brief USBH_MSC_IsReady - * The function check if the MSC function is ready - * @param phost: Host handle - * @retval USBH Status - */ -uint8_t USBH_MSC_IsReady (USBH_HandleTypeDef *phost) -{ - MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; - uint8_t res; - - if ((phost->gState == HOST_CLASS) && (MSC_Handle->state == MSC_IDLE)) - { - res = 1U; - } - else - { - res = 0U; - } - - return res; -} - -/** - * @brief USBH_MSC_GetMaxLUN - * The function return the Max LUN supported - * @param phost: Host handle - * @retval logical Unit Number supported - */ -uint8_t USBH_MSC_GetMaxLUN (USBH_HandleTypeDef *phost) -{ - MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; - - if ((phost->gState == HOST_CLASS) && (MSC_Handle->state == MSC_IDLE)) - { - return (uint8_t)MSC_Handle->max_lun; - } - - return 0xFFU; -} - -/** - * @brief USBH_MSC_UnitIsReady - * The function check whether a LUN is ready - * @param phost: Host handle - * @param lun: logical Unit Number - * @retval Lun status (0: not ready / 1: ready) - */ -uint8_t USBH_MSC_UnitIsReady (USBH_HandleTypeDef *phost, uint8_t lun) -{ - MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; - uint8_t res; - - if ((phost->gState == HOST_CLASS) && (MSC_Handle->unit[lun].error == MSC_OK)) - { - res = 1U; - } - else - { - res = 0U; - } - - return res; -} - -/** - * @brief USBH_MSC_GetLUNInfo - * The function return a LUN information - * @param phost: Host handle - * @param lun: logical Unit Number - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_MSC_GetLUNInfo(USBH_HandleTypeDef *phost, uint8_t lun, MSC_LUNTypeDef *info) -{ - MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; - if(phost->gState == HOST_CLASS) - { - USBH_memcpy(info,&MSC_Handle->unit[lun], sizeof(MSC_LUNTypeDef)); - return USBH_OK; - } - else - { - return USBH_FAIL; - } -} - -/** - * @brief USBH_MSC_Read - * The function performs a Read operation - * @param phost: Host handle - * @param lun: logical Unit Number - * @param address: sector address - * @param pbuf: pointer to data - * @param length: number of sector to read - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_MSC_Read(USBH_HandleTypeDef *phost, - uint8_t lun, - uint32_t address, - uint8_t *pbuf, - uint32_t length) -{ - uint32_t timeout; - MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; - - if ((phost->device.is_connected == 0U) || - (phost->gState != HOST_CLASS) || - (MSC_Handle->unit[lun].state != MSC_IDLE)) - { - return USBH_FAIL; - } - - MSC_Handle->state = MSC_READ; - MSC_Handle->unit[lun].state = MSC_READ; - MSC_Handle->rw_lun = lun; - - USBH_MSC_SCSI_Read(phost, lun, address, pbuf, length); - - timeout = phost->Timer; - - while (USBH_MSC_RdWrProcess(phost, lun) == USBH_BUSY) - { - if(((phost->Timer - timeout) > (10000U * length)) || (phost->device.is_connected == 0U)) - { - MSC_Handle->state = MSC_IDLE; - return USBH_FAIL; - } - } - MSC_Handle->state = MSC_IDLE; - - return USBH_OK; -} - -/** - * @brief USBH_MSC_Write - * The function performs a Write operation - * @param phost: Host handle - * @param lun: logical Unit Number - * @param address: sector address - * @param pbuf: pointer to data - * @param length: number of sector to write - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_MSC_Write(USBH_HandleTypeDef *phost, - uint8_t lun, - uint32_t address, - uint8_t *pbuf, - uint32_t length) -{ - uint32_t timeout; - MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; - - if ((phost->device.is_connected == 0U) || - (phost->gState != HOST_CLASS) || - (MSC_Handle->unit[lun].state != MSC_IDLE)) - { - return USBH_FAIL; - } - - MSC_Handle->state = MSC_WRITE; - MSC_Handle->unit[lun].state = MSC_WRITE; - MSC_Handle->rw_lun = lun; - - USBH_MSC_SCSI_Write(phost, lun, address, pbuf, length); - - timeout = phost->Timer; - while (USBH_MSC_RdWrProcess(phost, lun) == USBH_BUSY) - { - if(((phost->Timer - timeout) > (10000U * length)) || (phost->device.is_connected == 0U)) - { - MSC_Handle->state = MSC_IDLE; - return USBH_FAIL; - } - } - MSC_Handle->state = MSC_IDLE; - return USBH_OK; -} - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbh_msc.c + * @author MCD Application Team + * @brief This file implements the MSC class driver functions + * =================================================================== + * MSC Class Description + * =================================================================== + * This module manages the MSC class V1.0 following the "Universal + * Serial Bus Mass Storage Class (MSC) Bulk-Only Transport (BOT) Version 1.0 + * Sep. 31, 1999". + * This driver implements the following aspects of the specification: + * - Bulk-Only Transport protocol + * - Subclass : SCSI transparent command set (ref. SCSI Primary Commands - 3 (SPC-3)) + * + * @endverbatim + * + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + + /* BSPDependencies + - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" + - "stm32xxxxx_{eval}{discovery}_io.c" + - "stm32xxxxx_{eval}{discovery}{adafruit}_lcd.c" + - "stm32xxxxx_{eval}{discovery}_sdram.c" + EndBSPDependencies */ + +/* Includes ------------------------------------------------------------------*/ + +#include "usbh_msc.h" +#include "usbh_msc_bot.h" +#include "usbh_msc_scsi.h" + + +/** @addtogroup USBH_LIB + * @{ + */ + +/** @addtogroup USBH_CLASS + * @{ + */ + +/** @addtogroup USBH_MSC_CLASS + * @{ + */ + +/** @defgroup USBH_MSC_CORE + * @brief This file includes the mass storage related functions + * @{ + */ + + +/** @defgroup USBH_MSC_CORE_Private_TypesDefinitions + * @{ + */ +/** + * @} + */ + +/** @defgroup USBH_MSC_CORE_Private_Defines + * @{ + */ +/** + * @} + */ + +/** @defgroup USBH_MSC_CORE_Private_Macros + * @{ + */ +/** + * @} + */ + + +/** @defgroup USBH_MSC_CORE_Private_Variables + * @{ + */ +/** + * @} + */ + + +/** @defgroup USBH_MSC_CORE_Private_FunctionPrototypes + * @{ + */ + +static USBH_StatusTypeDef USBH_MSC_InterfaceInit (USBH_HandleTypeDef *phost); + +static USBH_StatusTypeDef USBH_MSC_InterfaceDeInit (USBH_HandleTypeDef *phost); + +static USBH_StatusTypeDef USBH_MSC_Process(USBH_HandleTypeDef *phost); + +static USBH_StatusTypeDef USBH_MSC_ClassRequest(USBH_HandleTypeDef *phost); + +static USBH_StatusTypeDef USBH_MSC_SOFProcess(USBH_HandleTypeDef *phost); + +static USBH_StatusTypeDef USBH_MSC_RdWrProcess(USBH_HandleTypeDef *phost, uint8_t lun); + +USBH_ClassTypeDef USBH_msc = +{ + "MSC", + USB_MSC_CLASS, + USBH_MSC_InterfaceInit, + USBH_MSC_InterfaceDeInit, + USBH_MSC_ClassRequest, + USBH_MSC_Process, + USBH_MSC_SOFProcess, + NULL, +}; + + +/** + * @} + */ + + +/** @defgroup USBH_MSC_CORE_Exported_Variables + * @{ + */ + +/** + * @} + */ + + +/** @defgroup USBH_MSC_CORE_Private_Functions + * @{ + */ + + +/** + * @brief USBH_MSC_InterfaceInit + * The function init the MSC class. + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_MSC_InterfaceInit (USBH_HandleTypeDef *phost) +{ + uint8_t interface = 0U; + USBH_StatusTypeDef status = USBH_FAIL; + MSC_HandleTypeDef *MSC_Handle; + + interface = USBH_FindInterface(phost, phost->pActiveClass->ClassCode, MSC_TRANSPARENT, MSC_BOT); + + if(interface == 0xFFU) /* Not Valid Interface */ + { + USBH_DbgLog ("Cannot Find the interface for %s class.", phost->pActiveClass->Name); + status = USBH_FAIL; + } + else + { + USBH_SelectInterface (phost, interface); + + phost->pActiveClass->pData = (MSC_HandleTypeDef *)USBH_malloc (sizeof(MSC_HandleTypeDef)); + MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; + + if(phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[0].bEndpointAddress & 0x80U) + { + MSC_Handle->InEp = (phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[0].bEndpointAddress); + MSC_Handle->InEpSize = phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[0].wMaxPacketSize; + } + else + { + MSC_Handle->OutEp = (phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[0].bEndpointAddress); + MSC_Handle->OutEpSize = phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[0].wMaxPacketSize; + } + + if(phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[1].bEndpointAddress & 0x80U) + { + MSC_Handle->InEp = (phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[1].bEndpointAddress); + MSC_Handle->InEpSize = phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[1].wMaxPacketSize; + } + else + { + MSC_Handle->OutEp = (phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[1].bEndpointAddress); + MSC_Handle->OutEpSize = phost->device.CfgDesc.Itf_Desc[phost->device.current_interface].Ep_Desc[1].wMaxPacketSize; + } + + MSC_Handle->current_lun = 0U; + MSC_Handle->rw_lun = 0U; + MSC_Handle->state = MSC_INIT; + MSC_Handle->error = MSC_OK; + MSC_Handle->req_state = MSC_REQ_IDLE; + MSC_Handle->OutPipe = USBH_AllocPipe(phost, MSC_Handle->OutEp); + MSC_Handle->InPipe = USBH_AllocPipe(phost, MSC_Handle->InEp); + + USBH_MSC_BOT_Init(phost); + + /* De-Initialize LUNs information */ + USBH_memset(MSC_Handle->unit, 0, sizeof(MSC_Handle->unit)); + + /* Open the new channels */ + USBH_OpenPipe (phost, + MSC_Handle->OutPipe, + MSC_Handle->OutEp, + phost->device.address, + phost->device.speed, + USB_EP_TYPE_BULK, + MSC_Handle->OutEpSize); + + USBH_OpenPipe (phost, + MSC_Handle->InPipe, + MSC_Handle->InEp, + phost->device.address, + phost->device.speed, + USB_EP_TYPE_BULK, + MSC_Handle->InEpSize); + + USBH_LL_SetToggle (phost, MSC_Handle->InPipe, 0U); + USBH_LL_SetToggle (phost, MSC_Handle->OutPipe, 0U); + status = USBH_OK; + } + return status; +} + +/** + * @brief USBH_MSC_InterfaceDeInit + * The function DeInit the Pipes used for the MSC class. + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_MSC_InterfaceDeInit (USBH_HandleTypeDef *phost) +{ + MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; + + if ( MSC_Handle->OutPipe) + { + USBH_ClosePipe(phost, MSC_Handle->OutPipe); + USBH_FreePipe (phost, MSC_Handle->OutPipe); + MSC_Handle->OutPipe = 0U; /* Reset the Channel as Free */ + } + + if ( MSC_Handle->InPipe) + { + USBH_ClosePipe(phost, MSC_Handle->InPipe); + USBH_FreePipe (phost, MSC_Handle->InPipe); + MSC_Handle->InPipe = 0U; /* Reset the Channel as Free */ + } + + if(phost->pActiveClass->pData) + { + USBH_free (phost->pActiveClass->pData); + phost->pActiveClass->pData = 0; + } + + return USBH_OK; +} + +/** + * @brief USBH_MSC_ClassRequest + * The function is responsible for handling Standard requests + * for MSC class. + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_MSC_ClassRequest(USBH_HandleTypeDef *phost) +{ + MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; + USBH_StatusTypeDef status = USBH_BUSY; + uint8_t i; + + /* Switch MSC REQ state machine */ + switch (MSC_Handle->req_state) + { + case MSC_REQ_IDLE: + case MSC_REQ_GET_MAX_LUN: + /* Issue GetMaxLUN request */ + status = USBH_MSC_BOT_REQ_GetMaxLUN(phost, (uint8_t *)(void *)&MSC_Handle->max_lun); + + /* When devices do not support the GetMaxLun request, this should + be considred as only one logical unit is supported */ + if(status == USBH_NOT_SUPPORTED) + { + MSC_Handle->max_lun = 0U; + status = USBH_OK; + } + + if(status == USBH_OK) + { + MSC_Handle->max_lun = (MSC_Handle->max_lun > MAX_SUPPORTED_LUN)? MAX_SUPPORTED_LUN : (uint8_t )(MSC_Handle->max_lun) + 1U; + USBH_UsrLog ("Number of supported LUN: %lu", (int32_t)(MSC_Handle->max_lun)); + + for(i = 0U; i < MSC_Handle->max_lun; i++) + { + MSC_Handle->unit[i].prev_ready_state = USBH_FAIL; + MSC_Handle->unit[i].state_changed = 0U; + } + } + break; + + case MSC_REQ_ERROR : + /* a Clear Feature should be issued here */ + if(USBH_ClrFeature(phost, 0x00U) == USBH_OK) + { + MSC_Handle->req_state = MSC_Handle->prev_req_state; + } + break; + + default: + break; + } + + return status; +} + +/** + * @brief USBH_MSC_Process + * The function is for managing state machine for MSC data transfers + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_MSC_Process(USBH_HandleTypeDef *phost) +{ + MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; + USBH_StatusTypeDef error = USBH_BUSY ; + USBH_StatusTypeDef scsi_status = USBH_BUSY ; + USBH_StatusTypeDef ready_status = USBH_BUSY ; + + switch (MSC_Handle->state) + { + case MSC_INIT: + + if(MSC_Handle->current_lun < MSC_Handle->max_lun) + { + + MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_NOT_READY; + /* Switch MSC REQ state machine */ + switch (MSC_Handle->unit[MSC_Handle->current_lun].state) + { + case MSC_INIT: + USBH_UsrLog ("LUN #%d: ", MSC_Handle->current_lun); + MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_READ_INQUIRY; + MSC_Handle->timer = phost->Timer; + break; + + case MSC_READ_INQUIRY: + scsi_status = USBH_MSC_SCSI_Inquiry(phost, (uint8_t)MSC_Handle->current_lun, &MSC_Handle->unit[MSC_Handle->current_lun].inquiry); + + if (scsi_status == USBH_OK) + { + USBH_UsrLog ("Inquiry Vendor : %s", MSC_Handle->unit[MSC_Handle->current_lun].inquiry.vendor_id); + USBH_UsrLog ("Inquiry Product : %s", MSC_Handle->unit[MSC_Handle->current_lun].inquiry.product_id); + USBH_UsrLog ("Inquiry Version : %s", MSC_Handle->unit[MSC_Handle->current_lun].inquiry.revision_id); + MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_TEST_UNIT_READY; + } + if (scsi_status == USBH_FAIL) + { + MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_REQUEST_SENSE; + } + else + { + if(scsi_status == USBH_UNRECOVERED_ERROR) + { + MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_IDLE; + MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_ERROR; + } + } + break; + + case MSC_TEST_UNIT_READY: + ready_status = USBH_MSC_SCSI_TestUnitReady(phost, (uint8_t)MSC_Handle->current_lun); + + if (ready_status == USBH_OK) + { + if(MSC_Handle->unit[MSC_Handle->current_lun].prev_ready_state != USBH_OK) + { + MSC_Handle->unit[MSC_Handle->current_lun].state_changed = 1U; + USBH_UsrLog ("MSC Device ready"); + } + else + { + MSC_Handle->unit[MSC_Handle->current_lun].state_changed = 0U; + } + MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_READ_CAPACITY10; + MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_OK; + MSC_Handle->unit[MSC_Handle->current_lun].prev_ready_state = USBH_OK; + } + if (ready_status == USBH_FAIL) + { + /* Media not ready, so try to check again during 10s */ + if( MSC_Handle->unit[MSC_Handle->current_lun].prev_ready_state != USBH_FAIL) + { + MSC_Handle->unit[MSC_Handle->current_lun].state_changed = 1U; + USBH_UsrLog ("MSC Device NOT ready"); + } + else + { + MSC_Handle->unit[MSC_Handle->current_lun].state_changed = 0U; + } + MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_REQUEST_SENSE; + MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_NOT_READY; + MSC_Handle->unit[MSC_Handle->current_lun].prev_ready_state = USBH_FAIL; + } + else + { + if (ready_status == USBH_UNRECOVERED_ERROR) + { + MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_IDLE; + MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_ERROR; + } + } + break; + + case MSC_READ_CAPACITY10: + scsi_status = USBH_MSC_SCSI_ReadCapacity(phost, (uint8_t)MSC_Handle->current_lun, &MSC_Handle->unit[MSC_Handle->current_lun].capacity) ; + + if(scsi_status == USBH_OK) + { + if(MSC_Handle->unit[MSC_Handle->current_lun].state_changed == 1U) + { + USBH_UsrLog ("MSC Device capacity : %lu Bytes", \ + (int32_t)(MSC_Handle->unit[MSC_Handle->current_lun].capacity.block_nbr * MSC_Handle->unit[MSC_Handle->current_lun].capacity.block_size)); + USBH_UsrLog ("Block number : %lu", (int32_t)(MSC_Handle->unit[MSC_Handle->current_lun].capacity.block_nbr)); + USBH_UsrLog ("Block Size : %lu", (int32_t)(MSC_Handle->unit[MSC_Handle->current_lun].capacity.block_size)); + } + MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_IDLE; + MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_OK; + MSC_Handle->current_lun++; + } + else if(scsi_status == USBH_FAIL) + { + MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_REQUEST_SENSE; + } + else + { + if(scsi_status == USBH_UNRECOVERED_ERROR) + { + MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_IDLE; + MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_ERROR; + } + } + break; + + case MSC_REQUEST_SENSE: + scsi_status = USBH_MSC_SCSI_RequestSense(phost, (uint8_t)MSC_Handle->current_lun, &MSC_Handle->unit[MSC_Handle->current_lun].sense); + + if( scsi_status == USBH_OK) + { + if((MSC_Handle->unit[MSC_Handle->current_lun].sense.key == SCSI_SENSE_KEY_UNIT_ATTENTION) || + (MSC_Handle->unit[MSC_Handle->current_lun].sense.key == SCSI_SENSE_KEY_NOT_READY) ) + { + + if((phost->Timer - MSC_Handle->timer) < 10000U) + { + MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_TEST_UNIT_READY; + break; + } + } + + USBH_UsrLog ("Sense Key : %x", MSC_Handle->unit[MSC_Handle->current_lun].sense.key); + USBH_UsrLog ("Additional Sense Code : %x", MSC_Handle->unit[MSC_Handle->current_lun].sense.asc); + USBH_UsrLog ("Additional Sense Code Qualifier: %x", MSC_Handle->unit[MSC_Handle->current_lun].sense.ascq); + MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_IDLE; + MSC_Handle->current_lun++; + } + if(scsi_status == USBH_FAIL) + { + USBH_UsrLog ("MSC Device NOT ready"); + MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_UNRECOVERED_ERROR; + } + else + { + if(scsi_status == USBH_UNRECOVERED_ERROR) + { + MSC_Handle->unit[MSC_Handle->current_lun].state = MSC_IDLE; + MSC_Handle->unit[MSC_Handle->current_lun].error = MSC_ERROR; + } + } + break; + + case MSC_UNRECOVERED_ERROR: + MSC_Handle->current_lun++; + break; + + default: + break; + } + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CLASS_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + else + { + MSC_Handle->current_lun = 0U; + MSC_Handle->state = MSC_IDLE; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CLASS_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + phost->pUser(phost, HOST_USER_CLASS_ACTIVE); + } + break; + + case MSC_IDLE: + error = USBH_OK; + break; + + default: + break; + } + return error; +} + + +/** + * @brief USBH_MSC_SOFProcess + * The function is for SOF state + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_MSC_SOFProcess(USBH_HandleTypeDef *phost) +{ + + return USBH_OK; +} +/** + * @brief USBH_MSC_RdWrProcess + * The function is for managing state machine for MSC I/O Process + * @param phost: Host handle + * @param lun: logical Unit Number + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_MSC_RdWrProcess(USBH_HandleTypeDef *phost, uint8_t lun) +{ + MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; + USBH_StatusTypeDef error = USBH_BUSY ; + USBH_StatusTypeDef scsi_status = USBH_BUSY ; + + /* Switch MSC REQ state machine */ + switch (MSC_Handle->unit[lun].state) + { + + case MSC_READ: + scsi_status = USBH_MSC_SCSI_Read(phost,lun, 0U, NULL, 0U); + + if(scsi_status == USBH_OK) + { + MSC_Handle->unit[lun].state = MSC_IDLE; + error = USBH_OK; + } + else if( scsi_status == USBH_FAIL) + { + MSC_Handle->unit[lun].state = MSC_REQUEST_SENSE; + } + else + { + if(scsi_status == USBH_UNRECOVERED_ERROR) + { + MSC_Handle->unit[lun].state = MSC_UNRECOVERED_ERROR; + error = USBH_FAIL; + } + } + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CLASS_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + case MSC_WRITE: + scsi_status = USBH_MSC_SCSI_Write(phost,lun, 0U, NULL, 0U); + + if(scsi_status == USBH_OK) + { + MSC_Handle->unit[lun].state = MSC_IDLE; + error = USBH_OK; + } + else if( scsi_status == USBH_FAIL) + { + MSC_Handle->unit[lun].state = MSC_REQUEST_SENSE; + } + else + { + if(scsi_status == USBH_UNRECOVERED_ERROR) + { + MSC_Handle->unit[lun].state = MSC_UNRECOVERED_ERROR; + error = USBH_FAIL; + } + } + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CLASS_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + case MSC_REQUEST_SENSE: + scsi_status = USBH_MSC_SCSI_RequestSense(phost, lun, &MSC_Handle->unit[lun].sense); + + if(scsi_status == USBH_OK) + { + USBH_UsrLog ("Sense Key : %x", MSC_Handle->unit[lun].sense.key); + USBH_UsrLog ("Additional Sense Code : %x", MSC_Handle->unit[lun].sense.asc); + USBH_UsrLog ("Additional Sense Code Qualifier: %x", MSC_Handle->unit[lun].sense.ascq); + MSC_Handle->unit[lun].state = MSC_IDLE; + MSC_Handle->unit[lun].error = MSC_ERROR; + + error = USBH_FAIL; + } + if(scsi_status == USBH_FAIL) + { + USBH_UsrLog ("MSC Device NOT ready"); + } + else + { + if(scsi_status == USBH_UNRECOVERED_ERROR) + { + MSC_Handle->unit[lun].state = MSC_UNRECOVERED_ERROR; + error = USBH_FAIL; + } + } + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CLASS_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + default: + break; + + } + return error; +} + +/** + * @brief USBH_MSC_IsReady + * The function check if the MSC function is ready + * @param phost: Host handle + * @retval USBH Status + */ +uint8_t USBH_MSC_IsReady (USBH_HandleTypeDef *phost) +{ + MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; + uint8_t res; + + if ((phost->gState == HOST_CLASS) && (MSC_Handle->state == MSC_IDLE)) + { + res = 1U; + } + else + { + res = 0U; + } + + return res; +} + +/** + * @brief USBH_MSC_GetMaxLUN + * The function return the Max LUN supported + * @param phost: Host handle + * @retval logical Unit Number supported + */ +uint8_t USBH_MSC_GetMaxLUN (USBH_HandleTypeDef *phost) +{ + MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; + + if ((phost->gState == HOST_CLASS) && (MSC_Handle->state == MSC_IDLE)) + { + return (uint8_t)MSC_Handle->max_lun; + } + + return 0xFFU; +} + +/** + * @brief USBH_MSC_UnitIsReady + * The function check whether a LUN is ready + * @param phost: Host handle + * @param lun: logical Unit Number + * @retval Lun status (0: not ready / 1: ready) + */ +uint8_t USBH_MSC_UnitIsReady (USBH_HandleTypeDef *phost, uint8_t lun) +{ + MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; + uint8_t res; + + if ((phost->gState == HOST_CLASS) && (MSC_Handle->unit[lun].error == MSC_OK)) + { + res = 1U; + } + else + { + res = 0U; + } + + return res; +} + +/** + * @brief USBH_MSC_GetLUNInfo + * The function return a LUN information + * @param phost: Host handle + * @param lun: logical Unit Number + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_MSC_GetLUNInfo(USBH_HandleTypeDef *phost, uint8_t lun, MSC_LUNTypeDef *info) +{ + MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; + if(phost->gState == HOST_CLASS) + { + USBH_memcpy(info,&MSC_Handle->unit[lun], sizeof(MSC_LUNTypeDef)); + return USBH_OK; + } + else + { + return USBH_FAIL; + } +} + +/** + * @brief USBH_MSC_Read + * The function performs a Read operation + * @param phost: Host handle + * @param lun: logical Unit Number + * @param address: sector address + * @param pbuf: pointer to data + * @param length: number of sector to read + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_MSC_Read(USBH_HandleTypeDef *phost, + uint8_t lun, + uint32_t address, + uint8_t *pbuf, + uint32_t length) +{ + uint32_t timeout; + MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; + + if ((phost->device.is_connected == 0U) || + (phost->gState != HOST_CLASS) || + (MSC_Handle->unit[lun].state != MSC_IDLE)) + { + return USBH_FAIL; + } + + MSC_Handle->state = MSC_READ; + MSC_Handle->unit[lun].state = MSC_READ; + MSC_Handle->rw_lun = lun; + + USBH_MSC_SCSI_Read(phost, lun, address, pbuf, length); + + timeout = phost->Timer; + + while (USBH_MSC_RdWrProcess(phost, lun) == USBH_BUSY) + { + if(((phost->Timer - timeout) > (10000U * length)) || (phost->device.is_connected == 0U)) + { + MSC_Handle->state = MSC_IDLE; + return USBH_FAIL; + } + } + MSC_Handle->state = MSC_IDLE; + + return USBH_OK; +} + +/** + * @brief USBH_MSC_Write + * The function performs a Write operation + * @param phost: Host handle + * @param lun: logical Unit Number + * @param address: sector address + * @param pbuf: pointer to data + * @param length: number of sector to write + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_MSC_Write(USBH_HandleTypeDef *phost, + uint8_t lun, + uint32_t address, + uint8_t *pbuf, + uint32_t length) +{ + uint32_t timeout; + MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; + + if ((phost->device.is_connected == 0U) || + (phost->gState != HOST_CLASS) || + (MSC_Handle->unit[lun].state != MSC_IDLE)) + { + return USBH_FAIL; + } + + MSC_Handle->state = MSC_WRITE; + MSC_Handle->unit[lun].state = MSC_WRITE; + MSC_Handle->rw_lun = lun; + + USBH_MSC_SCSI_Write(phost, lun, address, pbuf, length); + + timeout = phost->Timer; + while (USBH_MSC_RdWrProcess(phost, lun) == USBH_BUSY) + { + if(((phost->Timer - timeout) > (10000U * length)) || (phost->device.is_connected == 0U)) + { + MSC_Handle->state = MSC_IDLE; + return USBH_FAIL; + } + } + MSC_Handle->state = MSC_IDLE; + return USBH_OK; +} + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Class/MSC/Src/usbh_msc_bot.c b/system/Middlewares/ST/STM32_USB_Host_Library/Class/MSC/Src/usbh_msc_bot.c index 84f98334d7..f8a8321ca2 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Class/MSC/Src/usbh_msc_bot.c +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Class/MSC/Src/usbh_msc_bot.c @@ -1,694 +1,694 @@ -/** - ****************************************************************************** - * @file usbh_msc_bot.c - * @author MCD Application Team - * @brief This file includes the BOT protocol related functions - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - - /* BSPDependencies - - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" - - "stm32xxxxx_{eval}{discovery}_io.c" - - "stm32xxxxx_{eval}{discovery}{adafruit}_lcd.c" - - "stm32xxxxx_{eval}{discovery}_sdram.c" - EndBSPDependencies */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_msc_bot.h" -#include "usbh_msc.h" - -/** @addtogroup USBH_LIB -* @{ -*/ - -/** @addtogroup USBH_CLASS -* @{ -*/ - -/** @addtogroup USBH_MSC_CLASS -* @{ -*/ - -/** @defgroup USBH_MSC_BOT -* @brief This file includes the mass storage related functions -* @{ -*/ - - -/** @defgroup USBH_MSC_BOT_Private_TypesDefinitions -* @{ -*/ -/** -* @} -*/ - -/** @defgroup USBH_MSC_BOT_Private_Defines -* @{ -*/ -/** -* @} -*/ - -/** @defgroup USBH_MSC_BOT_Private_Macros -* @{ -*/ -/** -* @} -*/ - - -/** @defgroup USBH_MSC_BOT_Private_Variables -* @{ -*/ - -/** -* @} -*/ - - -/** @defgroup USBH_MSC_BOT_Private_FunctionPrototypes -* @{ -*/ -static USBH_StatusTypeDef USBH_MSC_BOT_Abort(USBH_HandleTypeDef *phost, uint8_t lun, uint8_t dir); -static BOT_CSWStatusTypeDef USBH_MSC_DecodeCSW(USBH_HandleTypeDef *phost); -/** -* @} -*/ - - -/** @defgroup USBH_MSC_BOT_Exported_Variables -* @{ -*/ -/** -* @} -*/ - - -/** @defgroup USBH_MSC_BOT_Private_Functions -* @{ -*/ - -/** - * @brief USBH_MSC_BOT_REQ_Reset - * The function the MSC BOT Reset request. - * @param phost: Host handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_MSC_BOT_REQ_Reset(USBH_HandleTypeDef *phost) -{ - - phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_TYPE_CLASS - | USB_REQ_RECIPIENT_INTERFACE; - - phost->Control.setup.b.bRequest = USB_REQ_BOT_RESET; - phost->Control.setup.b.wValue.w = 0U; - phost->Control.setup.b.wIndex.w = 0U; - phost->Control.setup.b.wLength.w = 0U; - - return USBH_CtlReq(phost, 0U, 0U); -} - -/** - * @brief USBH_MSC_BOT_REQ_GetMaxLUN - * The function the MSC BOT GetMaxLUN request. - * @param phost: Host handle - * @param Maxlun: pointer to Maxlun variable - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_MSC_BOT_REQ_GetMaxLUN(USBH_HandleTypeDef *phost, uint8_t *Maxlun) -{ - phost->Control.setup.b.bmRequestType = USB_D2H | USB_REQ_TYPE_CLASS - | USB_REQ_RECIPIENT_INTERFACE; - - phost->Control.setup.b.bRequest = USB_REQ_GET_MAX_LUN; - phost->Control.setup.b.wValue.w = 0U; - phost->Control.setup.b.wIndex.w = 0U; - phost->Control.setup.b.wLength.w = 1U; - - return USBH_CtlReq(phost, Maxlun, 1U); -} - - - -/** - * @brief USBH_MSC_BOT_Init - * The function Initializes the BOT protocol. - * @param phost: Host handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_MSC_BOT_Init(USBH_HandleTypeDef *phost) -{ - - MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; - - MSC_Handle->hbot.cbw.field.Signature = BOT_CBW_SIGNATURE; - MSC_Handle->hbot.cbw.field.Tag = BOT_CBW_TAG; - MSC_Handle->hbot.state = BOT_SEND_CBW; - MSC_Handle->hbot.cmd_state = BOT_CMD_SEND; - - return USBH_OK; -} - - - -/** - * @brief USBH_MSC_BOT_Process - * The function handle the BOT protocol. - * @param phost: Host handle - * @param lun: Logical Unit Number - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_MSC_BOT_Process (USBH_HandleTypeDef *phost, uint8_t lun) -{ - USBH_StatusTypeDef status = USBH_BUSY; - USBH_StatusTypeDef error = USBH_BUSY; - BOT_CSWStatusTypeDef CSW_Status = BOT_CSW_CMD_FAILED; - USBH_URBStateTypeDef URB_Status = USBH_URB_IDLE; - MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; - uint8_t toggle = 0U; - - switch (MSC_Handle->hbot.state) - { - case BOT_SEND_CBW: - MSC_Handle->hbot.cbw.field.LUN = lun; - MSC_Handle->hbot.state = BOT_SEND_CBW_WAIT; - USBH_BulkSendData (phost, MSC_Handle->hbot.cbw.data, - BOT_CBW_LENGTH, MSC_Handle->OutPipe, 1U); - - break; - - case BOT_SEND_CBW_WAIT: - - URB_Status = USBH_LL_GetURBState(phost, MSC_Handle->OutPipe); - - if(URB_Status == USBH_URB_DONE) - { - if ( MSC_Handle->hbot.cbw.field.DataTransferLength != 0U) - { - /* If there is Data Transfer Stage */ - if (((MSC_Handle->hbot.cbw.field.Flags) & USB_REQ_DIR_MASK) == USB_D2H) - { - /* Data Direction is IN */ - MSC_Handle->hbot.state = BOT_DATA_IN; - } - else - { - /* Data Direction is OUT */ - MSC_Handle->hbot.state = BOT_DATA_OUT; - } - } - - else - {/* If there is NO Data Transfer Stage */ - MSC_Handle->hbot.state = BOT_RECEIVE_CSW; - } - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - else if(URB_Status == USBH_URB_NOTREADY) - { - /* Re-send CBW */ - MSC_Handle->hbot.state = BOT_SEND_CBW; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - else - { - if(URB_Status == USBH_URB_STALL) - { - MSC_Handle->hbot.state = BOT_ERROR_OUT; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - } - break; - - case BOT_DATA_IN: - /* Send first packet */ - USBH_BulkReceiveData (phost, MSC_Handle->hbot.pbuf, - MSC_Handle->InEpSize, MSC_Handle->InPipe); - - MSC_Handle->hbot.state = BOT_DATA_IN_WAIT; - - break; - - case BOT_DATA_IN_WAIT: - - URB_Status = USBH_LL_GetURBState(phost, MSC_Handle->InPipe); - - if(URB_Status == USBH_URB_DONE) - { - /* Adjust Data pointer and data length */ - if(MSC_Handle->hbot.cbw.field.DataTransferLength > MSC_Handle->InEpSize) - { - MSC_Handle->hbot.pbuf += MSC_Handle->InEpSize; - MSC_Handle->hbot.cbw.field.DataTransferLength -= MSC_Handle->InEpSize; - } - else - { - MSC_Handle->hbot.cbw.field.DataTransferLength = 0U; - } - - /* More Data To be Received */ - if(MSC_Handle->hbot.cbw.field.DataTransferLength > 0U) - { - /* Send next packet */ - USBH_BulkReceiveData (phost, MSC_Handle->hbot.pbuf, - MSC_Handle->InEpSize, MSC_Handle->InPipe); - } - else - { - /* If value was 0, and successful transfer, then change the state */ - MSC_Handle->hbot.state = BOT_RECEIVE_CSW; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - } - else if(URB_Status == USBH_URB_STALL) - { - /* This is Data IN Stage STALL Condition */ - MSC_Handle->hbot.state = BOT_ERROR_IN; - - /* Refer to USB Mass-Storage Class : BOT (www.usb.org) - 6.7.2 Host expects to receive data from the device - 3. On a STALL condition receiving data, then: - The host shall accept the data received. - The host shall clear the Bulk-In pipe. - 4. The host shall attempt to receive a CSW.*/ - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - else - { - } - break; - - case BOT_DATA_OUT: - - USBH_BulkSendData (phost, MSC_Handle->hbot.pbuf, - MSC_Handle->OutEpSize, MSC_Handle->OutPipe, 1U); - - MSC_Handle->hbot.state = BOT_DATA_OUT_WAIT; - break; - - case BOT_DATA_OUT_WAIT: - URB_Status = USBH_LL_GetURBState(phost, MSC_Handle->OutPipe); - - if(URB_Status == USBH_URB_DONE) - { - /* Adjust Data pointer and data length */ - if(MSC_Handle->hbot.cbw.field.DataTransferLength > MSC_Handle->OutEpSize) - { - MSC_Handle->hbot.pbuf += MSC_Handle->OutEpSize; - MSC_Handle->hbot.cbw.field.DataTransferLength -= MSC_Handle->OutEpSize; - } - else - { - MSC_Handle->hbot.cbw.field.DataTransferLength = 0U; - } - - /* More Data To be Sent */ - if(MSC_Handle->hbot.cbw.field.DataTransferLength > 0U) - { - USBH_BulkSendData (phost, MSC_Handle->hbot.pbuf, - MSC_Handle->OutEpSize, MSC_Handle->OutPipe, 1U); - } - else - { - /* If value was 0, and successful transfer, then change the state */ - MSC_Handle->hbot.state = BOT_RECEIVE_CSW; - } - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - - else if(URB_Status == USBH_URB_NOTREADY) - { - /* Resend same data */ - MSC_Handle->hbot.state = BOT_DATA_OUT; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - - else if(URB_Status == USBH_URB_STALL) - { - MSC_Handle->hbot.state = BOT_ERROR_OUT; - - /* Refer to USB Mass-Storage Class : BOT (www.usb.org) - 6.7.3 Ho - Host expects to send data to the device - 3. On a STALL condition sending data, then: - " The host shall clear the Bulk-Out pipe. - 4. The host shall attempt to receive a CSW. - */ - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - else - { - } - break; - - case BOT_RECEIVE_CSW: - - USBH_BulkReceiveData (phost, MSC_Handle->hbot.csw.data, - BOT_CSW_LENGTH, MSC_Handle->InPipe); - - MSC_Handle->hbot.state = BOT_RECEIVE_CSW_WAIT; - break; - - case BOT_RECEIVE_CSW_WAIT: - - URB_Status = USBH_LL_GetURBState(phost, MSC_Handle->InPipe); - - /* Decode CSW */ - if(URB_Status == USBH_URB_DONE) - { - MSC_Handle->hbot.state = BOT_SEND_CBW; - MSC_Handle->hbot.cmd_state = BOT_CMD_SEND; - CSW_Status = USBH_MSC_DecodeCSW(phost); - - if(CSW_Status == BOT_CSW_CMD_PASSED) - { - status = USBH_OK; - } - else - { - status = USBH_FAIL; - } - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - else if(URB_Status == USBH_URB_STALL) - { - MSC_Handle->hbot.state = BOT_ERROR_IN; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - else - { - } - break; - - case BOT_ERROR_IN: - error = USBH_MSC_BOT_Abort(phost, lun, BOT_DIR_IN); - - if (error == USBH_OK) - { - MSC_Handle->hbot.state = BOT_RECEIVE_CSW; - } - else if (error == USBH_UNRECOVERED_ERROR) - { - /* This means that there is a STALL Error limit, Do Reset Recovery */ - MSC_Handle->hbot.state = BOT_UNRECOVERED_ERROR; - } - else - { - } - break; - - case BOT_ERROR_OUT: - error = USBH_MSC_BOT_Abort(phost, lun, BOT_DIR_OUT); - - if (error == USBH_OK) - { - - toggle = USBH_LL_GetToggle(phost, MSC_Handle->OutPipe); - USBH_LL_SetToggle(phost, MSC_Handle->OutPipe, 1U - toggle); - USBH_LL_SetToggle(phost, MSC_Handle->InPipe, 0U); - MSC_Handle->hbot.state = BOT_ERROR_IN; - } - else - { - if (error == USBH_UNRECOVERED_ERROR) - { - MSC_Handle->hbot.state = BOT_UNRECOVERED_ERROR; - } - } - break; - - - case BOT_UNRECOVERED_ERROR: - status = USBH_MSC_BOT_REQ_Reset(phost); - if ( status == USBH_OK) - { - MSC_Handle->hbot.state = BOT_SEND_CBW; - } - break; - - default: - break; - } - return status; -} - -/** - * @brief USBH_MSC_BOT_Abort - * The function handle the BOT Abort process. - * @param phost: Host handle - * @param lun: Logical Unit Number - * @param dir: direction (0: out / 1 : in) - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_MSC_BOT_Abort(USBH_HandleTypeDef *phost, uint8_t lun, uint8_t dir) -{ - USBH_StatusTypeDef status = USBH_FAIL; - MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; - - switch (dir) - { - case BOT_DIR_IN : - /* send ClrFeture on Bulk IN endpoint */ - status = USBH_ClrFeature(phost, MSC_Handle->InEp); - - break; - - case BOT_DIR_OUT : - /*send ClrFeature on Bulk OUT endpoint */ - status = USBH_ClrFeature(phost, MSC_Handle->OutEp); - break; - - default: - break; - } - return status; -} - -/** - * @brief USBH_MSC_BOT_DecodeCSW - * This function decodes the CSW received by the device and updates the - * same to upper layer. - * @param phost: Host handle - * @retval USBH Status - * @notes - * Refer to USB Mass-Storage Class : BOT (www.usb.org) - * 6.3.1 Valid CSW Conditions : - * The host shall consider the CSW valid when: - * 1. dCSWSignature is equal to 53425355h - * 2. the CSW is 13 (Dh) bytes in length, - * 3. dCSWTag matches the dCBWTag from the corresponding CBW. - */ - -static BOT_CSWStatusTypeDef USBH_MSC_DecodeCSW(USBH_HandleTypeDef *phost) -{ - MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; - BOT_CSWStatusTypeDef status = BOT_CSW_CMD_FAILED; - - /*Checking if the transfer length is different than 13*/ - if(USBH_LL_GetLastXferSize(phost, MSC_Handle->InPipe) != BOT_CSW_LENGTH) - { - /*(4) Hi > Dn (Host expects to receive data from the device, - Device intends to transfer no data) - (5) Hi > Di (Host expects to receive data from the device, - Device intends to send data to the host) - (9) Ho > Dn (Host expects to send data to the device, - Device intends to transfer no data) - (11) Ho > Do (Host expects to send data to the device, - Device intends to receive data from the host)*/ - - - status = BOT_CSW_PHASE_ERROR; - } - else - { /* CSW length is Correct */ - - /* Check validity of the CSW Signature and CSWStatus */ - if(MSC_Handle->hbot.csw.field.Signature == BOT_CSW_SIGNATURE) - {/* Check Condition 1. dCSWSignature is equal to 53425355h */ - - if(MSC_Handle->hbot.csw.field.Tag == MSC_Handle->hbot.cbw.field.Tag) - { - /* Check Condition 3. dCSWTag matches the dCBWTag from the - corresponding CBW */ - - if(MSC_Handle->hbot.csw.field.Status == 0U) - { - /* Refer to USB Mass-Storage Class : BOT (www.usb.org) - - Hn Host expects no data transfers - Hi Host expects to receive data from the device - Ho Host expects to send data to the device - - Dn Device intends to transfer no data - Di Device intends to send data to the host - Do Device intends to receive data from the host - - Section 6.7 - (1) Hn = Dn (Host expects no data transfers, - Device intends to transfer no data) - (6) Hi = Di (Host expects to receive data from the device, - Device intends to send data to the host) - (12) Ho = Do (Host expects to send data to the device, - Device intends to receive data from the host) - - */ - - status = BOT_CSW_CMD_PASSED; - } - else if(MSC_Handle->hbot.csw.field.Status == 1U) - { - status = BOT_CSW_CMD_FAILED; - } - - else if(MSC_Handle->hbot.csw.field.Status == 2U) - { - /* Refer to USB Mass-Storage Class : BOT (www.usb.org) - Section 6.7 - (2) Hn < Di ( Host expects no data transfers, - Device intends to send data to the host) - (3) Hn < Do ( Host expects no data transfers, - Device intends to receive data from the host) - (7) Hi < Di ( Host expects to receive data from the device, - Device intends to send data to the host) - (8) Hi <> Do ( Host expects to receive data from the device, - Device intends to receive data from the host) - (10) Ho <> Di (Host expects to send data to the device, - Di Device intends to send data to the host) - (13) Ho < Do (Host expects to send data to the device, - Device intends to receive data from the host) - */ - - status = BOT_CSW_PHASE_ERROR; - } - else - { - } - } /* CSW Tag Matching is Checked */ - } /* CSW Signature Correct Checking */ - else - { - /* If the CSW Signature is not valid, We sall return the Phase Error to - Upper Layers for Reset Recovery */ - - status = BOT_CSW_PHASE_ERROR; - } - } /* CSW Length Check*/ - - return status; -} - - -/** -* @} -*/ - -/** -* @} -*/ - -/** -* @} -*/ - -/** -* @} -*/ - -/** -* @} -*/ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - - - +/** + ****************************************************************************** + * @file usbh_msc_bot.c + * @author MCD Application Team + * @brief This file includes the BOT protocol related functions + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + + /* BSPDependencies + - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" + - "stm32xxxxx_{eval}{discovery}_io.c" + - "stm32xxxxx_{eval}{discovery}{adafruit}_lcd.c" + - "stm32xxxxx_{eval}{discovery}_sdram.c" + EndBSPDependencies */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_msc_bot.h" +#include "usbh_msc.h" + +/** @addtogroup USBH_LIB +* @{ +*/ + +/** @addtogroup USBH_CLASS +* @{ +*/ + +/** @addtogroup USBH_MSC_CLASS +* @{ +*/ + +/** @defgroup USBH_MSC_BOT +* @brief This file includes the mass storage related functions +* @{ +*/ + + +/** @defgroup USBH_MSC_BOT_Private_TypesDefinitions +* @{ +*/ +/** +* @} +*/ + +/** @defgroup USBH_MSC_BOT_Private_Defines +* @{ +*/ +/** +* @} +*/ + +/** @defgroup USBH_MSC_BOT_Private_Macros +* @{ +*/ +/** +* @} +*/ + + +/** @defgroup USBH_MSC_BOT_Private_Variables +* @{ +*/ + +/** +* @} +*/ + + +/** @defgroup USBH_MSC_BOT_Private_FunctionPrototypes +* @{ +*/ +static USBH_StatusTypeDef USBH_MSC_BOT_Abort(USBH_HandleTypeDef *phost, uint8_t lun, uint8_t dir); +static BOT_CSWStatusTypeDef USBH_MSC_DecodeCSW(USBH_HandleTypeDef *phost); +/** +* @} +*/ + + +/** @defgroup USBH_MSC_BOT_Exported_Variables +* @{ +*/ +/** +* @} +*/ + + +/** @defgroup USBH_MSC_BOT_Private_Functions +* @{ +*/ + +/** + * @brief USBH_MSC_BOT_REQ_Reset + * The function the MSC BOT Reset request. + * @param phost: Host handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_MSC_BOT_REQ_Reset(USBH_HandleTypeDef *phost) +{ + + phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_TYPE_CLASS + | USB_REQ_RECIPIENT_INTERFACE; + + phost->Control.setup.b.bRequest = USB_REQ_BOT_RESET; + phost->Control.setup.b.wValue.w = 0U; + phost->Control.setup.b.wIndex.w = 0U; + phost->Control.setup.b.wLength.w = 0U; + + return USBH_CtlReq(phost, 0U, 0U); +} + +/** + * @brief USBH_MSC_BOT_REQ_GetMaxLUN + * The function the MSC BOT GetMaxLUN request. + * @param phost: Host handle + * @param Maxlun: pointer to Maxlun variable + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_MSC_BOT_REQ_GetMaxLUN(USBH_HandleTypeDef *phost, uint8_t *Maxlun) +{ + phost->Control.setup.b.bmRequestType = USB_D2H | USB_REQ_TYPE_CLASS + | USB_REQ_RECIPIENT_INTERFACE; + + phost->Control.setup.b.bRequest = USB_REQ_GET_MAX_LUN; + phost->Control.setup.b.wValue.w = 0U; + phost->Control.setup.b.wIndex.w = 0U; + phost->Control.setup.b.wLength.w = 1U; + + return USBH_CtlReq(phost, Maxlun, 1U); +} + + + +/** + * @brief USBH_MSC_BOT_Init + * The function Initializes the BOT protocol. + * @param phost: Host handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_MSC_BOT_Init(USBH_HandleTypeDef *phost) +{ + + MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; + + MSC_Handle->hbot.cbw.field.Signature = BOT_CBW_SIGNATURE; + MSC_Handle->hbot.cbw.field.Tag = BOT_CBW_TAG; + MSC_Handle->hbot.state = BOT_SEND_CBW; + MSC_Handle->hbot.cmd_state = BOT_CMD_SEND; + + return USBH_OK; +} + + + +/** + * @brief USBH_MSC_BOT_Process + * The function handle the BOT protocol. + * @param phost: Host handle + * @param lun: Logical Unit Number + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_MSC_BOT_Process (USBH_HandleTypeDef *phost, uint8_t lun) +{ + USBH_StatusTypeDef status = USBH_BUSY; + USBH_StatusTypeDef error = USBH_BUSY; + BOT_CSWStatusTypeDef CSW_Status = BOT_CSW_CMD_FAILED; + USBH_URBStateTypeDef URB_Status = USBH_URB_IDLE; + MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; + uint8_t toggle = 0U; + + switch (MSC_Handle->hbot.state) + { + case BOT_SEND_CBW: + MSC_Handle->hbot.cbw.field.LUN = lun; + MSC_Handle->hbot.state = BOT_SEND_CBW_WAIT; + USBH_BulkSendData (phost, MSC_Handle->hbot.cbw.data, + BOT_CBW_LENGTH, MSC_Handle->OutPipe, 1U); + + break; + + case BOT_SEND_CBW_WAIT: + + URB_Status = USBH_LL_GetURBState(phost, MSC_Handle->OutPipe); + + if(URB_Status == USBH_URB_DONE) + { + if ( MSC_Handle->hbot.cbw.field.DataTransferLength != 0U) + { + /* If there is Data Transfer Stage */ + if (((MSC_Handle->hbot.cbw.field.Flags) & USB_REQ_DIR_MASK) == USB_D2H) + { + /* Data Direction is IN */ + MSC_Handle->hbot.state = BOT_DATA_IN; + } + else + { + /* Data Direction is OUT */ + MSC_Handle->hbot.state = BOT_DATA_OUT; + } + } + + else + {/* If there is NO Data Transfer Stage */ + MSC_Handle->hbot.state = BOT_RECEIVE_CSW; + } + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + else if(URB_Status == USBH_URB_NOTREADY) + { + /* Re-send CBW */ + MSC_Handle->hbot.state = BOT_SEND_CBW; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + else + { + if(URB_Status == USBH_URB_STALL) + { + MSC_Handle->hbot.state = BOT_ERROR_OUT; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + } + break; + + case BOT_DATA_IN: + /* Send first packet */ + USBH_BulkReceiveData (phost, MSC_Handle->hbot.pbuf, + MSC_Handle->InEpSize, MSC_Handle->InPipe); + + MSC_Handle->hbot.state = BOT_DATA_IN_WAIT; + + break; + + case BOT_DATA_IN_WAIT: + + URB_Status = USBH_LL_GetURBState(phost, MSC_Handle->InPipe); + + if(URB_Status == USBH_URB_DONE) + { + /* Adjust Data pointer and data length */ + if(MSC_Handle->hbot.cbw.field.DataTransferLength > MSC_Handle->InEpSize) + { + MSC_Handle->hbot.pbuf += MSC_Handle->InEpSize; + MSC_Handle->hbot.cbw.field.DataTransferLength -= MSC_Handle->InEpSize; + } + else + { + MSC_Handle->hbot.cbw.field.DataTransferLength = 0U; + } + + /* More Data To be Received */ + if(MSC_Handle->hbot.cbw.field.DataTransferLength > 0U) + { + /* Send next packet */ + USBH_BulkReceiveData (phost, MSC_Handle->hbot.pbuf, + MSC_Handle->InEpSize, MSC_Handle->InPipe); + } + else + { + /* If value was 0, and successful transfer, then change the state */ + MSC_Handle->hbot.state = BOT_RECEIVE_CSW; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + } + else if(URB_Status == USBH_URB_STALL) + { + /* This is Data IN Stage STALL Condition */ + MSC_Handle->hbot.state = BOT_ERROR_IN; + + /* Refer to USB Mass-Storage Class : BOT (www.usb.org) + 6.7.2 Host expects to receive data from the device + 3. On a STALL condition receiving data, then: + The host shall accept the data received. + The host shall clear the Bulk-In pipe. + 4. The host shall attempt to receive a CSW.*/ + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + else + { + } + break; + + case BOT_DATA_OUT: + + USBH_BulkSendData (phost, MSC_Handle->hbot.pbuf, + MSC_Handle->OutEpSize, MSC_Handle->OutPipe, 1U); + + MSC_Handle->hbot.state = BOT_DATA_OUT_WAIT; + break; + + case BOT_DATA_OUT_WAIT: + URB_Status = USBH_LL_GetURBState(phost, MSC_Handle->OutPipe); + + if(URB_Status == USBH_URB_DONE) + { + /* Adjust Data pointer and data length */ + if(MSC_Handle->hbot.cbw.field.DataTransferLength > MSC_Handle->OutEpSize) + { + MSC_Handle->hbot.pbuf += MSC_Handle->OutEpSize; + MSC_Handle->hbot.cbw.field.DataTransferLength -= MSC_Handle->OutEpSize; + } + else + { + MSC_Handle->hbot.cbw.field.DataTransferLength = 0U; + } + + /* More Data To be Sent */ + if(MSC_Handle->hbot.cbw.field.DataTransferLength > 0U) + { + USBH_BulkSendData (phost, MSC_Handle->hbot.pbuf, + MSC_Handle->OutEpSize, MSC_Handle->OutPipe, 1U); + } + else + { + /* If value was 0, and successful transfer, then change the state */ + MSC_Handle->hbot.state = BOT_RECEIVE_CSW; + } + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + + else if(URB_Status == USBH_URB_NOTREADY) + { + /* Resend same data */ + MSC_Handle->hbot.state = BOT_DATA_OUT; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + + else if(URB_Status == USBH_URB_STALL) + { + MSC_Handle->hbot.state = BOT_ERROR_OUT; + + /* Refer to USB Mass-Storage Class : BOT (www.usb.org) + 6.7.3 Ho - Host expects to send data to the device + 3. On a STALL condition sending data, then: + " The host shall clear the Bulk-Out pipe. + 4. The host shall attempt to receive a CSW. + */ + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + else + { + } + break; + + case BOT_RECEIVE_CSW: + + USBH_BulkReceiveData (phost, MSC_Handle->hbot.csw.data, + BOT_CSW_LENGTH, MSC_Handle->InPipe); + + MSC_Handle->hbot.state = BOT_RECEIVE_CSW_WAIT; + break; + + case BOT_RECEIVE_CSW_WAIT: + + URB_Status = USBH_LL_GetURBState(phost, MSC_Handle->InPipe); + + /* Decode CSW */ + if(URB_Status == USBH_URB_DONE) + { + MSC_Handle->hbot.state = BOT_SEND_CBW; + MSC_Handle->hbot.cmd_state = BOT_CMD_SEND; + CSW_Status = USBH_MSC_DecodeCSW(phost); + + if(CSW_Status == BOT_CSW_CMD_PASSED) + { + status = USBH_OK; + } + else + { + status = USBH_FAIL; + } + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + else if(URB_Status == USBH_URB_STALL) + { + MSC_Handle->hbot.state = BOT_ERROR_IN; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + else + { + } + break; + + case BOT_ERROR_IN: + error = USBH_MSC_BOT_Abort(phost, lun, BOT_DIR_IN); + + if (error == USBH_OK) + { + MSC_Handle->hbot.state = BOT_RECEIVE_CSW; + } + else if (error == USBH_UNRECOVERED_ERROR) + { + /* This means that there is a STALL Error limit, Do Reset Recovery */ + MSC_Handle->hbot.state = BOT_UNRECOVERED_ERROR; + } + else + { + } + break; + + case BOT_ERROR_OUT: + error = USBH_MSC_BOT_Abort(phost, lun, BOT_DIR_OUT); + + if (error == USBH_OK) + { + + toggle = USBH_LL_GetToggle(phost, MSC_Handle->OutPipe); + USBH_LL_SetToggle(phost, MSC_Handle->OutPipe, 1U - toggle); + USBH_LL_SetToggle(phost, MSC_Handle->InPipe, 0U); + MSC_Handle->hbot.state = BOT_ERROR_IN; + } + else + { + if (error == USBH_UNRECOVERED_ERROR) + { + MSC_Handle->hbot.state = BOT_UNRECOVERED_ERROR; + } + } + break; + + + case BOT_UNRECOVERED_ERROR: + status = USBH_MSC_BOT_REQ_Reset(phost); + if ( status == USBH_OK) + { + MSC_Handle->hbot.state = BOT_SEND_CBW; + } + break; + + default: + break; + } + return status; +} + +/** + * @brief USBH_MSC_BOT_Abort + * The function handle the BOT Abort process. + * @param phost: Host handle + * @param lun: Logical Unit Number + * @param dir: direction (0: out / 1 : in) + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_MSC_BOT_Abort(USBH_HandleTypeDef *phost, uint8_t lun, uint8_t dir) +{ + USBH_StatusTypeDef status = USBH_FAIL; + MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; + + switch (dir) + { + case BOT_DIR_IN : + /* send ClrFeture on Bulk IN endpoint */ + status = USBH_ClrFeature(phost, MSC_Handle->InEp); + + break; + + case BOT_DIR_OUT : + /*send ClrFeature on Bulk OUT endpoint */ + status = USBH_ClrFeature(phost, MSC_Handle->OutEp); + break; + + default: + break; + } + return status; +} + +/** + * @brief USBH_MSC_BOT_DecodeCSW + * This function decodes the CSW received by the device and updates the + * same to upper layer. + * @param phost: Host handle + * @retval USBH Status + * @notes + * Refer to USB Mass-Storage Class : BOT (www.usb.org) + * 6.3.1 Valid CSW Conditions : + * The host shall consider the CSW valid when: + * 1. dCSWSignature is equal to 53425355h + * 2. the CSW is 13 (Dh) bytes in length, + * 3. dCSWTag matches the dCBWTag from the corresponding CBW. + */ + +static BOT_CSWStatusTypeDef USBH_MSC_DecodeCSW(USBH_HandleTypeDef *phost) +{ + MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; + BOT_CSWStatusTypeDef status = BOT_CSW_CMD_FAILED; + + /*Checking if the transfer length is different than 13*/ + if(USBH_LL_GetLastXferSize(phost, MSC_Handle->InPipe) != BOT_CSW_LENGTH) + { + /*(4) Hi > Dn (Host expects to receive data from the device, + Device intends to transfer no data) + (5) Hi > Di (Host expects to receive data from the device, + Device intends to send data to the host) + (9) Ho > Dn (Host expects to send data to the device, + Device intends to transfer no data) + (11) Ho > Do (Host expects to send data to the device, + Device intends to receive data from the host)*/ + + + status = BOT_CSW_PHASE_ERROR; + } + else + { /* CSW length is Correct */ + + /* Check validity of the CSW Signature and CSWStatus */ + if(MSC_Handle->hbot.csw.field.Signature == BOT_CSW_SIGNATURE) + {/* Check Condition 1. dCSWSignature is equal to 53425355h */ + + if(MSC_Handle->hbot.csw.field.Tag == MSC_Handle->hbot.cbw.field.Tag) + { + /* Check Condition 3. dCSWTag matches the dCBWTag from the + corresponding CBW */ + + if(MSC_Handle->hbot.csw.field.Status == 0U) + { + /* Refer to USB Mass-Storage Class : BOT (www.usb.org) + + Hn Host expects no data transfers + Hi Host expects to receive data from the device + Ho Host expects to send data to the device + + Dn Device intends to transfer no data + Di Device intends to send data to the host + Do Device intends to receive data from the host + + Section 6.7 + (1) Hn = Dn (Host expects no data transfers, + Device intends to transfer no data) + (6) Hi = Di (Host expects to receive data from the device, + Device intends to send data to the host) + (12) Ho = Do (Host expects to send data to the device, + Device intends to receive data from the host) + + */ + + status = BOT_CSW_CMD_PASSED; + } + else if(MSC_Handle->hbot.csw.field.Status == 1U) + { + status = BOT_CSW_CMD_FAILED; + } + + else if(MSC_Handle->hbot.csw.field.Status == 2U) + { + /* Refer to USB Mass-Storage Class : BOT (www.usb.org) + Section 6.7 + (2) Hn < Di ( Host expects no data transfers, + Device intends to send data to the host) + (3) Hn < Do ( Host expects no data transfers, + Device intends to receive data from the host) + (7) Hi < Di ( Host expects to receive data from the device, + Device intends to send data to the host) + (8) Hi <> Do ( Host expects to receive data from the device, + Device intends to receive data from the host) + (10) Ho <> Di (Host expects to send data to the device, + Di Device intends to send data to the host) + (13) Ho < Do (Host expects to send data to the device, + Device intends to receive data from the host) + */ + + status = BOT_CSW_PHASE_ERROR; + } + else + { + } + } /* CSW Tag Matching is Checked */ + } /* CSW Signature Correct Checking */ + else + { + /* If the CSW Signature is not valid, We sall return the Phase Error to + Upper Layers for Reset Recovery */ + + status = BOT_CSW_PHASE_ERROR; + } + } /* CSW Length Check*/ + + return status; +} + + +/** +* @} +*/ + +/** +* @} +*/ + +/** +* @} +*/ + +/** +* @} +*/ + +/** +* @} +*/ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + + + diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Class/MSC/Src/usbh_msc_scsi.c b/system/Middlewares/ST/STM32_USB_Host_Library/Class/MSC/Src/usbh_msc_scsi.c index 2e7bc22764..966dbd88f4 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Class/MSC/Src/usbh_msc_scsi.c +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Class/MSC/Src/usbh_msc_scsi.c @@ -1,466 +1,466 @@ -/** - ****************************************************************************** - * @file usbh_msc_scsi.c - * @author MCD Application Team - * @brief This file implements the SCSI commands - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - - /* BSPDependencies - - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" - - "stm32xxxxx_{eval}{discovery}_io.c" - - "stm32xxxxx_{eval}{discovery}{adafruit}_lcd.c" - - "stm32xxxxx_{eval}{discovery}_sdram.c" - EndBSPDependencies */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_msc.h" -#include "usbh_msc_scsi.h" -#include "usbh_msc_bot.h" - - -/** @addtogroup USBH_LIB - * @{ - */ - -/** @addtogroup USBH_CLASS - * @{ - */ - -/** @addtogroup USBH_MSC_CLASS - * @{ - */ - -/** @defgroup USBH_MSC_SCSI - * @brief This file includes the mass storage related functions - * @{ - */ - - -/** @defgroup USBH_MSC_SCSI_Private_TypesDefinitions - * @{ - */ - -/** - * @} - */ - -/** @defgroup USBH_MSC_SCSI_Private_Defines - * @{ - */ -/** - * @} - */ - -/** @defgroup USBH_MSC_SCSI_Private_Macros - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBH_MSC_SCSI_Private_FunctionPrototypes - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBH_MSC_SCSI_Exported_Variables - * @{ - */ - -/** - * @} - */ - - -/** @defgroup USBH_MSC_SCSI_Private_Functions - * @{ - */ - - -/** - * @brief USBH_MSC_SCSI_TestUnitReady - * Issue TestUnitReady command. - * @param phost: Host handle - * @param lun: Logical Unit Number - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_MSC_SCSI_TestUnitReady (USBH_HandleTypeDef *phost, - uint8_t lun) -{ - USBH_StatusTypeDef error = USBH_FAIL ; - MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; - - switch(MSC_Handle->hbot.cmd_state) - { - case BOT_CMD_SEND: - - /*Prepare the CBW and relevent field*/ - MSC_Handle->hbot.cbw.field.DataTransferLength = DATA_LEN_MODE_TEST_UNIT_READY; - MSC_Handle->hbot.cbw.field.Flags = USB_EP_DIR_OUT; - MSC_Handle->hbot.cbw.field.CBLength = CBW_LENGTH; - - USBH_memset(MSC_Handle->hbot.cbw.field.CB, 0, CBW_CB_LENGTH); - MSC_Handle->hbot.cbw.field.CB[0] = OPCODE_TEST_UNIT_READY; - - MSC_Handle->hbot.state = BOT_SEND_CBW; - MSC_Handle->hbot.cmd_state = BOT_CMD_WAIT; - error = USBH_BUSY; - break; - - case BOT_CMD_WAIT: - error = USBH_MSC_BOT_Process(phost, lun); - break; - - default: - break; - } - - return error; -} - -/** - * @brief USBH_MSC_SCSI_ReadCapacity - * Issue Read Capacity command. - * @param phost: Host handle - * @param lun: Logical Unit Number - * @param capacity: pointer to the capacity structure - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_MSC_SCSI_ReadCapacity (USBH_HandleTypeDef *phost, - uint8_t lun, - SCSI_CapacityTypeDef *capacity) -{ - USBH_StatusTypeDef error = USBH_BUSY ; - MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; - - switch(MSC_Handle->hbot.cmd_state) - { - case BOT_CMD_SEND: - - /*Prepare the CBW and relevent field*/ - MSC_Handle->hbot.cbw.field.DataTransferLength = DATA_LEN_READ_CAPACITY10; - MSC_Handle->hbot.cbw.field.Flags = USB_EP_DIR_IN; - MSC_Handle->hbot.cbw.field.CBLength = CBW_LENGTH; - - USBH_memset(MSC_Handle->hbot.cbw.field.CB, 0, CBW_CB_LENGTH); - MSC_Handle->hbot.cbw.field.CB[0] = OPCODE_READ_CAPACITY10; - - MSC_Handle->hbot.state = BOT_SEND_CBW; - - MSC_Handle->hbot.cmd_state = BOT_CMD_WAIT; - MSC_Handle->hbot.pbuf = (uint8_t *)(void *)MSC_Handle->hbot.data; - error = USBH_BUSY; - break; - - case BOT_CMD_WAIT: - - error = USBH_MSC_BOT_Process(phost, lun); - - if(error == USBH_OK) - { - /*assign the capacity*/ - capacity->block_nbr = MSC_Handle->hbot.pbuf[3] | ((uint32_t)MSC_Handle->hbot.pbuf[2] << 8U) |\ - ((uint32_t)MSC_Handle->hbot.pbuf[1] << 16U) | ((uint32_t)MSC_Handle->hbot.pbuf[0] << 24U); - - /*assign the page length*/ - capacity->block_size = (uint16_t)(MSC_Handle->hbot.pbuf[7] | ((uint32_t)MSC_Handle->hbot.pbuf[6] << 8U)); - } - break; - - default: - break; - } - - return error; -} - -/** - * @brief USBH_MSC_SCSI_Inquiry - * Issue Inquiry command. - * @param phost: Host handle - * @param lun: Logical Unit Number - * @param capacity: pointer to the inquiry structure - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_MSC_SCSI_Inquiry (USBH_HandleTypeDef *phost, uint8_t lun, - SCSI_StdInquiryDataTypeDef *inquiry) -{ - USBH_StatusTypeDef error = USBH_FAIL; - MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; - - switch(MSC_Handle->hbot.cmd_state) - { - case BOT_CMD_SEND: - - /*Prepare the CBW and relevent field*/ - MSC_Handle->hbot.cbw.field.DataTransferLength = DATA_LEN_INQUIRY; - MSC_Handle->hbot.cbw.field.Flags = USB_EP_DIR_IN; - MSC_Handle->hbot.cbw.field.CBLength = CBW_LENGTH; - - USBH_memset(MSC_Handle->hbot.cbw.field.CB, 0, CBW_LENGTH); - MSC_Handle->hbot.cbw.field.CB[0] = OPCODE_INQUIRY; - MSC_Handle->hbot.cbw.field.CB[1] = (lun << 5); - MSC_Handle->hbot.cbw.field.CB[2] = 0U; - MSC_Handle->hbot.cbw.field.CB[3] = 0U; - MSC_Handle->hbot.cbw.field.CB[4] = 0x24U; - MSC_Handle->hbot.cbw.field.CB[5] = 0U; - - MSC_Handle->hbot.state = BOT_SEND_CBW; - - MSC_Handle->hbot.cmd_state = BOT_CMD_WAIT; - MSC_Handle->hbot.pbuf = (uint8_t *)(void *)MSC_Handle->hbot.data; - error = USBH_BUSY; - break; - - case BOT_CMD_WAIT: - - error = USBH_MSC_BOT_Process(phost, lun); - - if(error == USBH_OK) - { - USBH_memset(inquiry, 0, sizeof(SCSI_StdInquiryDataTypeDef)); - /*assign Inquiry Data */ - inquiry->DeviceType = MSC_Handle->hbot.pbuf[0] & 0x1FU; - inquiry->PeripheralQualifier = MSC_Handle->hbot.pbuf[0] >> 5U; - - if (((uint32_t)MSC_Handle->hbot.pbuf[1] & 0x80U) == 0x80U) - { - inquiry->RemovableMedia = 1U; - } - else - { - inquiry->RemovableMedia = 0U; - } - - USBH_memcpy (inquiry->vendor_id, &MSC_Handle->hbot.pbuf[8], 8U); - USBH_memcpy (inquiry->product_id, &MSC_Handle->hbot.pbuf[16], 16U); - USBH_memcpy (inquiry->revision_id, &MSC_Handle->hbot.pbuf[32], 4U); - } - break; - - default: - break; - } - - return error; -} - -/** - * @brief USBH_MSC_SCSI_RequestSense - * Issue RequestSense command. - * @param phost: Host handle - * @param lun: Logical Unit Number - * @param capacity: pointer to the sense data structure - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_MSC_SCSI_RequestSense (USBH_HandleTypeDef *phost, - uint8_t lun, - SCSI_SenseTypeDef *sense_data) -{ - USBH_StatusTypeDef error = USBH_FAIL ; - MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; - - switch(MSC_Handle->hbot.cmd_state) - { - case BOT_CMD_SEND: - - /*Prepare the CBW and relevent field*/ - MSC_Handle->hbot.cbw.field.DataTransferLength = DATA_LEN_REQUEST_SENSE; - MSC_Handle->hbot.cbw.field.Flags = USB_EP_DIR_IN; - MSC_Handle->hbot.cbw.field.CBLength = CBW_LENGTH; - - USBH_memset(MSC_Handle->hbot.cbw.field.CB, 0, CBW_CB_LENGTH); - MSC_Handle->hbot.cbw.field.CB[0] = OPCODE_REQUEST_SENSE; - MSC_Handle->hbot.cbw.field.CB[1] = (lun << 5); - MSC_Handle->hbot.cbw.field.CB[2] = 0U; - MSC_Handle->hbot.cbw.field.CB[3] = 0U; - MSC_Handle->hbot.cbw.field.CB[4] = DATA_LEN_REQUEST_SENSE; - MSC_Handle->hbot.cbw.field.CB[5] = 0U; - - MSC_Handle->hbot.state = BOT_SEND_CBW; - MSC_Handle->hbot.cmd_state = BOT_CMD_WAIT; - MSC_Handle->hbot.pbuf = (uint8_t *)(void *)MSC_Handle->hbot.data; - error = USBH_BUSY; - break; - - case BOT_CMD_WAIT: - - error = USBH_MSC_BOT_Process(phost, lun); - - if(error == USBH_OK) - { - sense_data->key = MSC_Handle->hbot.pbuf[2] & 0x0FU; - sense_data->asc = MSC_Handle->hbot.pbuf[12]; - sense_data->ascq = MSC_Handle->hbot.pbuf[13]; - } - break; - - default: - break; - } - - return error; -} - -/** - * @brief USBH_MSC_SCSI_Write - * Issue write10 command. - * @param phost: Host handle - * @param lun: Logical Unit Number - * @param address: sector address - * @param pbuf: pointer to data - * @param length: number of sector to write - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_MSC_SCSI_Write(USBH_HandleTypeDef *phost, - uint8_t lun, - uint32_t address, - uint8_t *pbuf, - uint32_t length) -{ - USBH_StatusTypeDef error = USBH_FAIL ; - - MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; - - switch(MSC_Handle->hbot.cmd_state) - { - case BOT_CMD_SEND: - - /*Prepare the CBW and relevent field*/ - MSC_Handle->hbot.cbw.field.DataTransferLength = length * MSC_Handle->unit[0].capacity.block_size; - MSC_Handle->hbot.cbw.field.Flags = USB_EP_DIR_OUT; - MSC_Handle->hbot.cbw.field.CBLength = CBW_LENGTH; - - USBH_memset(MSC_Handle->hbot.cbw.field.CB, 0, CBW_CB_LENGTH); - MSC_Handle->hbot.cbw.field.CB[0] = OPCODE_WRITE10; - - /*logical block address*/ - MSC_Handle->hbot.cbw.field.CB[2] = (((uint8_t*)(void *)&address)[3]); - MSC_Handle->hbot.cbw.field.CB[3] = (((uint8_t*)(void *)&address)[2]); - MSC_Handle->hbot.cbw.field.CB[4] = (((uint8_t*)(void *)&address)[1]); - MSC_Handle->hbot.cbw.field.CB[5] = (((uint8_t*)(void *)&address)[0]); - - - /*Transfer length */ - MSC_Handle->hbot.cbw.field.CB[7] = (((uint8_t *)(void *)&length)[1]) ; - MSC_Handle->hbot.cbw.field.CB[8] = (((uint8_t *)(void *)&length)[0]) ; - - - MSC_Handle->hbot.state = BOT_SEND_CBW; - MSC_Handle->hbot.cmd_state = BOT_CMD_WAIT; - MSC_Handle->hbot.pbuf = pbuf; - error = USBH_BUSY; - break; - - case BOT_CMD_WAIT: - error = USBH_MSC_BOT_Process(phost, lun); - break; - - default: - break; - } - - return error; -} - -/** - * @brief USBH_MSC_SCSI_Read - * Issue Read10 command. - * @param phost: Host handle - * @param lun: Logical Unit Number - * @param address: sector address - * @param pbuf: pointer to data - * @param length: number of sector to read - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_MSC_SCSI_Read(USBH_HandleTypeDef *phost, - uint8_t lun, - uint32_t address, - uint8_t *pbuf, - uint32_t length) -{ - USBH_StatusTypeDef error = USBH_FAIL ; - MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; - - switch(MSC_Handle->hbot.cmd_state) - { - case BOT_CMD_SEND: - - /*Prepare the CBW and relevent field*/ - MSC_Handle->hbot.cbw.field.DataTransferLength = length * MSC_Handle->unit[0].capacity.block_size; - MSC_Handle->hbot.cbw.field.Flags = USB_EP_DIR_IN; - MSC_Handle->hbot.cbw.field.CBLength = CBW_LENGTH; - - USBH_memset(MSC_Handle->hbot.cbw.field.CB, 0, CBW_CB_LENGTH); - MSC_Handle->hbot.cbw.field.CB[0] = OPCODE_READ10; - - /*logical block address*/ - MSC_Handle->hbot.cbw.field.CB[2] = (((uint8_t*)(void *)&address)[3]); - MSC_Handle->hbot.cbw.field.CB[3] = (((uint8_t*)(void *)&address)[2]); - MSC_Handle->hbot.cbw.field.CB[4] = (((uint8_t*)(void *)&address)[1]); - MSC_Handle->hbot.cbw.field.CB[5] = (((uint8_t*)(void *)&address)[0]); - - - /*Transfer length */ - MSC_Handle->hbot.cbw.field.CB[7] = (((uint8_t *)(void *)&length)[1]) ; - MSC_Handle->hbot.cbw.field.CB[8] = (((uint8_t *)(void *)&length)[0]) ; - - - MSC_Handle->hbot.state = BOT_SEND_CBW; - MSC_Handle->hbot.cmd_state = BOT_CMD_WAIT; - MSC_Handle->hbot.pbuf = pbuf; - error = USBH_BUSY; - break; - - case BOT_CMD_WAIT: - error = USBH_MSC_BOT_Process(phost, lun); - break; - - default: - break; - } - - return error; -} - - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - - - +/** + ****************************************************************************** + * @file usbh_msc_scsi.c + * @author MCD Application Team + * @brief This file implements the SCSI commands + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + + /* BSPDependencies + - "stm32xxxxx_{eval}{discovery}{nucleo_144}.c" + - "stm32xxxxx_{eval}{discovery}_io.c" + - "stm32xxxxx_{eval}{discovery}{adafruit}_lcd.c" + - "stm32xxxxx_{eval}{discovery}_sdram.c" + EndBSPDependencies */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_msc.h" +#include "usbh_msc_scsi.h" +#include "usbh_msc_bot.h" + + +/** @addtogroup USBH_LIB + * @{ + */ + +/** @addtogroup USBH_CLASS + * @{ + */ + +/** @addtogroup USBH_MSC_CLASS + * @{ + */ + +/** @defgroup USBH_MSC_SCSI + * @brief This file includes the mass storage related functions + * @{ + */ + + +/** @defgroup USBH_MSC_SCSI_Private_TypesDefinitions + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBH_MSC_SCSI_Private_Defines + * @{ + */ +/** + * @} + */ + +/** @defgroup USBH_MSC_SCSI_Private_Macros + * @{ + */ +/** + * @} + */ + + +/** @defgroup USBH_MSC_SCSI_Private_FunctionPrototypes + * @{ + */ +/** + * @} + */ + + +/** @defgroup USBH_MSC_SCSI_Exported_Variables + * @{ + */ + +/** + * @} + */ + + +/** @defgroup USBH_MSC_SCSI_Private_Functions + * @{ + */ + + +/** + * @brief USBH_MSC_SCSI_TestUnitReady + * Issue TestUnitReady command. + * @param phost: Host handle + * @param lun: Logical Unit Number + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_MSC_SCSI_TestUnitReady (USBH_HandleTypeDef *phost, + uint8_t lun) +{ + USBH_StatusTypeDef error = USBH_FAIL ; + MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; + + switch(MSC_Handle->hbot.cmd_state) + { + case BOT_CMD_SEND: + + /*Prepare the CBW and relevent field*/ + MSC_Handle->hbot.cbw.field.DataTransferLength = DATA_LEN_MODE_TEST_UNIT_READY; + MSC_Handle->hbot.cbw.field.Flags = USB_EP_DIR_OUT; + MSC_Handle->hbot.cbw.field.CBLength = CBW_LENGTH; + + USBH_memset(MSC_Handle->hbot.cbw.field.CB, 0, CBW_CB_LENGTH); + MSC_Handle->hbot.cbw.field.CB[0] = OPCODE_TEST_UNIT_READY; + + MSC_Handle->hbot.state = BOT_SEND_CBW; + MSC_Handle->hbot.cmd_state = BOT_CMD_WAIT; + error = USBH_BUSY; + break; + + case BOT_CMD_WAIT: + error = USBH_MSC_BOT_Process(phost, lun); + break; + + default: + break; + } + + return error; +} + +/** + * @brief USBH_MSC_SCSI_ReadCapacity + * Issue Read Capacity command. + * @param phost: Host handle + * @param lun: Logical Unit Number + * @param capacity: pointer to the capacity structure + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_MSC_SCSI_ReadCapacity (USBH_HandleTypeDef *phost, + uint8_t lun, + SCSI_CapacityTypeDef *capacity) +{ + USBH_StatusTypeDef error = USBH_BUSY ; + MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; + + switch(MSC_Handle->hbot.cmd_state) + { + case BOT_CMD_SEND: + + /*Prepare the CBW and relevent field*/ + MSC_Handle->hbot.cbw.field.DataTransferLength = DATA_LEN_READ_CAPACITY10; + MSC_Handle->hbot.cbw.field.Flags = USB_EP_DIR_IN; + MSC_Handle->hbot.cbw.field.CBLength = CBW_LENGTH; + + USBH_memset(MSC_Handle->hbot.cbw.field.CB, 0, CBW_CB_LENGTH); + MSC_Handle->hbot.cbw.field.CB[0] = OPCODE_READ_CAPACITY10; + + MSC_Handle->hbot.state = BOT_SEND_CBW; + + MSC_Handle->hbot.cmd_state = BOT_CMD_WAIT; + MSC_Handle->hbot.pbuf = (uint8_t *)(void *)MSC_Handle->hbot.data; + error = USBH_BUSY; + break; + + case BOT_CMD_WAIT: + + error = USBH_MSC_BOT_Process(phost, lun); + + if(error == USBH_OK) + { + /*assign the capacity*/ + capacity->block_nbr = MSC_Handle->hbot.pbuf[3] | ((uint32_t)MSC_Handle->hbot.pbuf[2] << 8U) |\ + ((uint32_t)MSC_Handle->hbot.pbuf[1] << 16U) | ((uint32_t)MSC_Handle->hbot.pbuf[0] << 24U); + + /*assign the page length*/ + capacity->block_size = (uint16_t)(MSC_Handle->hbot.pbuf[7] | ((uint32_t)MSC_Handle->hbot.pbuf[6] << 8U)); + } + break; + + default: + break; + } + + return error; +} + +/** + * @brief USBH_MSC_SCSI_Inquiry + * Issue Inquiry command. + * @param phost: Host handle + * @param lun: Logical Unit Number + * @param capacity: pointer to the inquiry structure + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_MSC_SCSI_Inquiry (USBH_HandleTypeDef *phost, uint8_t lun, + SCSI_StdInquiryDataTypeDef *inquiry) +{ + USBH_StatusTypeDef error = USBH_FAIL; + MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; + + switch(MSC_Handle->hbot.cmd_state) + { + case BOT_CMD_SEND: + + /*Prepare the CBW and relevent field*/ + MSC_Handle->hbot.cbw.field.DataTransferLength = DATA_LEN_INQUIRY; + MSC_Handle->hbot.cbw.field.Flags = USB_EP_DIR_IN; + MSC_Handle->hbot.cbw.field.CBLength = CBW_LENGTH; + + USBH_memset(MSC_Handle->hbot.cbw.field.CB, 0, CBW_LENGTH); + MSC_Handle->hbot.cbw.field.CB[0] = OPCODE_INQUIRY; + MSC_Handle->hbot.cbw.field.CB[1] = (lun << 5); + MSC_Handle->hbot.cbw.field.CB[2] = 0U; + MSC_Handle->hbot.cbw.field.CB[3] = 0U; + MSC_Handle->hbot.cbw.field.CB[4] = 0x24U; + MSC_Handle->hbot.cbw.field.CB[5] = 0U; + + MSC_Handle->hbot.state = BOT_SEND_CBW; + + MSC_Handle->hbot.cmd_state = BOT_CMD_WAIT; + MSC_Handle->hbot.pbuf = (uint8_t *)(void *)MSC_Handle->hbot.data; + error = USBH_BUSY; + break; + + case BOT_CMD_WAIT: + + error = USBH_MSC_BOT_Process(phost, lun); + + if(error == USBH_OK) + { + USBH_memset(inquiry, 0, sizeof(SCSI_StdInquiryDataTypeDef)); + /*assign Inquiry Data */ + inquiry->DeviceType = MSC_Handle->hbot.pbuf[0] & 0x1FU; + inquiry->PeripheralQualifier = MSC_Handle->hbot.pbuf[0] >> 5U; + + if (((uint32_t)MSC_Handle->hbot.pbuf[1] & 0x80U) == 0x80U) + { + inquiry->RemovableMedia = 1U; + } + else + { + inquiry->RemovableMedia = 0U; + } + + USBH_memcpy (inquiry->vendor_id, &MSC_Handle->hbot.pbuf[8], 8U); + USBH_memcpy (inquiry->product_id, &MSC_Handle->hbot.pbuf[16], 16U); + USBH_memcpy (inquiry->revision_id, &MSC_Handle->hbot.pbuf[32], 4U); + } + break; + + default: + break; + } + + return error; +} + +/** + * @brief USBH_MSC_SCSI_RequestSense + * Issue RequestSense command. + * @param phost: Host handle + * @param lun: Logical Unit Number + * @param capacity: pointer to the sense data structure + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_MSC_SCSI_RequestSense (USBH_HandleTypeDef *phost, + uint8_t lun, + SCSI_SenseTypeDef *sense_data) +{ + USBH_StatusTypeDef error = USBH_FAIL ; + MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; + + switch(MSC_Handle->hbot.cmd_state) + { + case BOT_CMD_SEND: + + /*Prepare the CBW and relevent field*/ + MSC_Handle->hbot.cbw.field.DataTransferLength = DATA_LEN_REQUEST_SENSE; + MSC_Handle->hbot.cbw.field.Flags = USB_EP_DIR_IN; + MSC_Handle->hbot.cbw.field.CBLength = CBW_LENGTH; + + USBH_memset(MSC_Handle->hbot.cbw.field.CB, 0, CBW_CB_LENGTH); + MSC_Handle->hbot.cbw.field.CB[0] = OPCODE_REQUEST_SENSE; + MSC_Handle->hbot.cbw.field.CB[1] = (lun << 5); + MSC_Handle->hbot.cbw.field.CB[2] = 0U; + MSC_Handle->hbot.cbw.field.CB[3] = 0U; + MSC_Handle->hbot.cbw.field.CB[4] = DATA_LEN_REQUEST_SENSE; + MSC_Handle->hbot.cbw.field.CB[5] = 0U; + + MSC_Handle->hbot.state = BOT_SEND_CBW; + MSC_Handle->hbot.cmd_state = BOT_CMD_WAIT; + MSC_Handle->hbot.pbuf = (uint8_t *)(void *)MSC_Handle->hbot.data; + error = USBH_BUSY; + break; + + case BOT_CMD_WAIT: + + error = USBH_MSC_BOT_Process(phost, lun); + + if(error == USBH_OK) + { + sense_data->key = MSC_Handle->hbot.pbuf[2] & 0x0FU; + sense_data->asc = MSC_Handle->hbot.pbuf[12]; + sense_data->ascq = MSC_Handle->hbot.pbuf[13]; + } + break; + + default: + break; + } + + return error; +} + +/** + * @brief USBH_MSC_SCSI_Write + * Issue write10 command. + * @param phost: Host handle + * @param lun: Logical Unit Number + * @param address: sector address + * @param pbuf: pointer to data + * @param length: number of sector to write + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_MSC_SCSI_Write(USBH_HandleTypeDef *phost, + uint8_t lun, + uint32_t address, + uint8_t *pbuf, + uint32_t length) +{ + USBH_StatusTypeDef error = USBH_FAIL ; + + MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; + + switch(MSC_Handle->hbot.cmd_state) + { + case BOT_CMD_SEND: + + /*Prepare the CBW and relevent field*/ + MSC_Handle->hbot.cbw.field.DataTransferLength = length * MSC_Handle->unit[0].capacity.block_size; + MSC_Handle->hbot.cbw.field.Flags = USB_EP_DIR_OUT; + MSC_Handle->hbot.cbw.field.CBLength = CBW_LENGTH; + + USBH_memset(MSC_Handle->hbot.cbw.field.CB, 0, CBW_CB_LENGTH); + MSC_Handle->hbot.cbw.field.CB[0] = OPCODE_WRITE10; + + /*logical block address*/ + MSC_Handle->hbot.cbw.field.CB[2] = (((uint8_t*)(void *)&address)[3]); + MSC_Handle->hbot.cbw.field.CB[3] = (((uint8_t*)(void *)&address)[2]); + MSC_Handle->hbot.cbw.field.CB[4] = (((uint8_t*)(void *)&address)[1]); + MSC_Handle->hbot.cbw.field.CB[5] = (((uint8_t*)(void *)&address)[0]); + + + /*Transfer length */ + MSC_Handle->hbot.cbw.field.CB[7] = (((uint8_t *)(void *)&length)[1]) ; + MSC_Handle->hbot.cbw.field.CB[8] = (((uint8_t *)(void *)&length)[0]) ; + + + MSC_Handle->hbot.state = BOT_SEND_CBW; + MSC_Handle->hbot.cmd_state = BOT_CMD_WAIT; + MSC_Handle->hbot.pbuf = pbuf; + error = USBH_BUSY; + break; + + case BOT_CMD_WAIT: + error = USBH_MSC_BOT_Process(phost, lun); + break; + + default: + break; + } + + return error; +} + +/** + * @brief USBH_MSC_SCSI_Read + * Issue Read10 command. + * @param phost: Host handle + * @param lun: Logical Unit Number + * @param address: sector address + * @param pbuf: pointer to data + * @param length: number of sector to read + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_MSC_SCSI_Read(USBH_HandleTypeDef *phost, + uint8_t lun, + uint32_t address, + uint8_t *pbuf, + uint32_t length) +{ + USBH_StatusTypeDef error = USBH_FAIL ; + MSC_HandleTypeDef *MSC_Handle = (MSC_HandleTypeDef *) phost->pActiveClass->pData; + + switch(MSC_Handle->hbot.cmd_state) + { + case BOT_CMD_SEND: + + /*Prepare the CBW and relevent field*/ + MSC_Handle->hbot.cbw.field.DataTransferLength = length * MSC_Handle->unit[0].capacity.block_size; + MSC_Handle->hbot.cbw.field.Flags = USB_EP_DIR_IN; + MSC_Handle->hbot.cbw.field.CBLength = CBW_LENGTH; + + USBH_memset(MSC_Handle->hbot.cbw.field.CB, 0, CBW_CB_LENGTH); + MSC_Handle->hbot.cbw.field.CB[0] = OPCODE_READ10; + + /*logical block address*/ + MSC_Handle->hbot.cbw.field.CB[2] = (((uint8_t*)(void *)&address)[3]); + MSC_Handle->hbot.cbw.field.CB[3] = (((uint8_t*)(void *)&address)[2]); + MSC_Handle->hbot.cbw.field.CB[4] = (((uint8_t*)(void *)&address)[1]); + MSC_Handle->hbot.cbw.field.CB[5] = (((uint8_t*)(void *)&address)[0]); + + + /*Transfer length */ + MSC_Handle->hbot.cbw.field.CB[7] = (((uint8_t *)(void *)&length)[1]) ; + MSC_Handle->hbot.cbw.field.CB[8] = (((uint8_t *)(void *)&length)[0]) ; + + + MSC_Handle->hbot.state = BOT_SEND_CBW; + MSC_Handle->hbot.cmd_state = BOT_CMD_WAIT; + MSC_Handle->hbot.pbuf = pbuf; + error = USBH_BUSY; + break; + + case BOT_CMD_WAIT: + error = USBH_MSC_BOT_Process(phost, lun); + break; + + default: + break; + } + + return error; +} + + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + + + diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Class/MTP/Inc/usbh_mtp.h b/system/Middlewares/ST/STM32_USB_Host_Library/Class/MTP/Inc/usbh_mtp.h index ffac223166..f5215a76a5 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Class/MTP/Inc/usbh_mtp.h +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Class/MTP/Inc/usbh_mtp.h @@ -1,262 +1,262 @@ -/** - ****************************************************************************** - * @file usbh_mtp.h - * @author MCD Application Team - * @brief This file contains all the prototypes for the usbh_mtp.c - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive ----------------------------------------------*/ -#ifndef __USBH_MTP_H -#define __USBH_MTP_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_mtp_ptp.h" -#include "usbh_core.h" - - -/** @addtogroup USBH_LIB -* @{ -*/ - -/** @addtogroup USBH_CLASS -* @{ -*/ - -/** @addtogroup USBH_MTP_CLASS -* @{ -*/ - -/** @defgroup USBH_MTP_CORE -* @brief This file is the Header file for usbh_mtp.c -* @{ -*/ - - - - -/*Communication Class codes*/ -#define USB_MTP_CLASS 0x06U /* Still Image Class)*/ -#define MTP_MAX_STORAGE_UNITS_NBR PTP_MAX_STORAGE_UNITS_NBR - -/** - * @} - */ - -/** @defgroup USBH_MTP_CORE_Exported_Types -* @{ -*/ -typedef enum -{ - MTP_IDLE = 0, - MTP_GETDEVICEINFO , - MTP_OPENSESSION , - MTP_CLOSESESSION , - MTP_GETSTORAGEIDS , - MTP_GETSTORAGEINFO , -} -MTP_StateTypeDef; - - -typedef enum -{ - MTP_EVENTS_INIT = 0, - MTP_EVENTS_GETDATA , -} -MTP_EventsStateTypeDef; - - -typedef struct -{ - MTP_EventsStateTypeDef state; - uint32_t timer; - uint16_t poll; - PTP_EventContainerTypedef container; -} -MTP_EventHandleTypedef; - -typedef struct -{ - - uint32_t CurrentStorageId; - uint32_t ObjectFormatCode; - uint32_t CurrentObjectHandler; - uint8_t ObjectHandlerNbr; - uint32_t Objdepth; -} -MTP_ParamsTypedef; - - -typedef struct -{ - PTP_DeviceInfoTypedef devinfo; - PTP_StorageIDsTypedef storids; - PTP_StorageInfoTypedef storinfo[MTP_MAX_STORAGE_UNITS_NBR]; - PTP_ObjectHandlesTypedef Handles; -} -MTP_InfoTypedef; - -/* Structure for MTP process */ -typedef struct _MTP_Process -{ - MTP_InfoTypedef info; - MTP_ParamsTypedef params; - - uint8_t DataInPipe; - uint8_t DataOutPipe; - uint8_t NotificationPipe; - - uint8_t DataOutEp; - uint8_t DataInEp; - uint8_t NotificationEp; - - uint16_t DataOutEpSize; - uint16_t DataInEpSize; - uint16_t NotificationEpSize; - MTP_StateTypeDef state; - MTP_EventHandleTypedef events; - PTP_HandleTypeDef ptp; - uint32_t current_storage_unit; - uint32_t is_ready; -} -MTP_HandleTypeDef; - -#define MTP_StorageInfoTypedef PTP_StorageInfoTypedef -#define MTP_ObjectHandlesTypedef PTP_ObjectHandlesTypedef -#define MTP_ObjectInfoTypedef PTP_ObjectInfoTypedef -/** -* @} -*/ - -/** @defgroup USBH_MTP_CORE_Exported_Defines -* @{ -*/ - -/** -* @} -*/ - -/** @defgroup USBH_MTP_CORE_Exported_Macros -* @{ -*/ -/** -* @} -*/ - -/** @defgroup USBH_MTP_CORE_Exported_Variables -* @{ -*/ -extern USBH_ClassTypeDef MTP_Class; -#define USBH_MTP_CLASS &MTP_Class - -/** -* @} -*/ - -/** @defgroup USBH_MTP_CORE_Exported_FunctionsPrototype -* @{ -*/ -uint8_t USBH_MTP_IsReady (USBH_HandleTypeDef *phost); -USBH_StatusTypeDef USBH_MTP_SelectStorage (USBH_HandleTypeDef *phost, uint8_t storage_idx); -USBH_StatusTypeDef USBH_MTP_GetNumStorage (USBH_HandleTypeDef *phost, uint8_t *storage_num); -USBH_StatusTypeDef USBH_MTP_GetNumObjects (USBH_HandleTypeDef *phost, - uint32_t storage_idx, - uint32_t objectformatcode, - uint32_t associationOH, - uint32_t* numobs); -USBH_StatusTypeDef USBH_MTP_GetStorageInfo (USBH_HandleTypeDef *phost, - uint8_t storage_idx, - MTP_StorageInfoTypedef *info); - -USBH_StatusTypeDef USBH_MTP_GetObjectHandles (USBH_HandleTypeDef *phost, - uint32_t storage_idx, - uint32_t objectformatcode, - uint32_t associationOH, - PTP_ObjectHandlesTypedef* objecthandles); - -USBH_StatusTypeDef USBH_MTP_GetObjectInfo (USBH_HandleTypeDef *phost, - uint32_t handle, - PTP_ObjectInfoTypedef* objectinfo); - -USBH_StatusTypeDef USBH_MTP_DeleteObject (USBH_HandleTypeDef *phost, - uint32_t handle, - uint32_t objectformatcode); - -USBH_StatusTypeDef USBH_MTP_GetObject (USBH_HandleTypeDef *phost, - uint32_t handle, - uint8_t *object); - -USBH_StatusTypeDef USBH_MTP_GetPartialObject(USBH_HandleTypeDef *phost, - uint32_t handle, - uint32_t offset, - uint32_t maxbytes, - uint8_t *object, - uint32_t *len); - -USBH_StatusTypeDef USBH_MTP_GetObjectPropsSupported (USBH_HandleTypeDef *phost, - uint16_t ofc, - uint32_t *propnum, - uint16_t *props); - -USBH_StatusTypeDef USBH_MTP_GetObjectPropDesc (USBH_HandleTypeDef *phost, - uint16_t opc, - uint16_t ofc, - PTP_ObjectPropDescTypeDef *opd); - -USBH_StatusTypeDef USBH_MTP_GetObjectPropList (USBH_HandleTypeDef *phost, - uint32_t handle, - MTP_PropertiesTypedef *pprops, - uint32_t *nrofprops); - -USBH_StatusTypeDef USBH_MTP_SendObject (USBH_HandleTypeDef *phost, - uint32_t handle, - uint8_t *object, - uint32_t size); - -USBH_StatusTypeDef USBH_MTP_GetDevicePropDesc (USBH_HandleTypeDef *phost, - uint16_t propcode, - PTP_DevicePropDescTypdef* devicepropertydesc); - -void USBH_MTP_EventsCallback(USBH_HandleTypeDef *phost, uint32_t event, uint32_t param); -/** -* @} -*/ - -#ifdef __cplusplus -} -#endif - -#endif /* __USBH_MTP_H */ - -/** -* @} -*/ - -/** -* @} -*/ - -/** -* @} -*/ - -/** -* @} -*/ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - +/** + ****************************************************************************** + * @file usbh_mtp.h + * @author MCD Application Team + * @brief This file contains all the prototypes for the usbh_mtp.c + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive ----------------------------------------------*/ +#ifndef __USBH_MTP_H +#define __USBH_MTP_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_mtp_ptp.h" +#include "usbh_core.h" + + +/** @addtogroup USBH_LIB +* @{ +*/ + +/** @addtogroup USBH_CLASS +* @{ +*/ + +/** @addtogroup USBH_MTP_CLASS +* @{ +*/ + +/** @defgroup USBH_MTP_CORE +* @brief This file is the Header file for usbh_mtp.c +* @{ +*/ + + + + +/*Communication Class codes*/ +#define USB_MTP_CLASS 0x06U /* Still Image Class)*/ +#define MTP_MAX_STORAGE_UNITS_NBR PTP_MAX_STORAGE_UNITS_NBR + +/** + * @} + */ + +/** @defgroup USBH_MTP_CORE_Exported_Types +* @{ +*/ +typedef enum +{ + MTP_IDLE = 0, + MTP_GETDEVICEINFO , + MTP_OPENSESSION , + MTP_CLOSESESSION , + MTP_GETSTORAGEIDS , + MTP_GETSTORAGEINFO , +} +MTP_StateTypeDef; + + +typedef enum +{ + MTP_EVENTS_INIT = 0, + MTP_EVENTS_GETDATA , +} +MTP_EventsStateTypeDef; + + +typedef struct +{ + MTP_EventsStateTypeDef state; + uint32_t timer; + uint16_t poll; + PTP_EventContainerTypedef container; +} +MTP_EventHandleTypedef; + +typedef struct +{ + + uint32_t CurrentStorageId; + uint32_t ObjectFormatCode; + uint32_t CurrentObjectHandler; + uint8_t ObjectHandlerNbr; + uint32_t Objdepth; +} +MTP_ParamsTypedef; + + +typedef struct +{ + PTP_DeviceInfoTypedef devinfo; + PTP_StorageIDsTypedef storids; + PTP_StorageInfoTypedef storinfo[MTP_MAX_STORAGE_UNITS_NBR]; + PTP_ObjectHandlesTypedef Handles; +} +MTP_InfoTypedef; + +/* Structure for MTP process */ +typedef struct _MTP_Process +{ + MTP_InfoTypedef info; + MTP_ParamsTypedef params; + + uint8_t DataInPipe; + uint8_t DataOutPipe; + uint8_t NotificationPipe; + + uint8_t DataOutEp; + uint8_t DataInEp; + uint8_t NotificationEp; + + uint16_t DataOutEpSize; + uint16_t DataInEpSize; + uint16_t NotificationEpSize; + MTP_StateTypeDef state; + MTP_EventHandleTypedef events; + PTP_HandleTypeDef ptp; + uint32_t current_storage_unit; + uint32_t is_ready; +} +MTP_HandleTypeDef; + +#define MTP_StorageInfoTypedef PTP_StorageInfoTypedef +#define MTP_ObjectHandlesTypedef PTP_ObjectHandlesTypedef +#define MTP_ObjectInfoTypedef PTP_ObjectInfoTypedef +/** +* @} +*/ + +/** @defgroup USBH_MTP_CORE_Exported_Defines +* @{ +*/ + +/** +* @} +*/ + +/** @defgroup USBH_MTP_CORE_Exported_Macros +* @{ +*/ +/** +* @} +*/ + +/** @defgroup USBH_MTP_CORE_Exported_Variables +* @{ +*/ +extern USBH_ClassTypeDef MTP_Class; +#define USBH_MTP_CLASS &MTP_Class + +/** +* @} +*/ + +/** @defgroup USBH_MTP_CORE_Exported_FunctionsPrototype +* @{ +*/ +uint8_t USBH_MTP_IsReady (USBH_HandleTypeDef *phost); +USBH_StatusTypeDef USBH_MTP_SelectStorage (USBH_HandleTypeDef *phost, uint8_t storage_idx); +USBH_StatusTypeDef USBH_MTP_GetNumStorage (USBH_HandleTypeDef *phost, uint8_t *storage_num); +USBH_StatusTypeDef USBH_MTP_GetNumObjects (USBH_HandleTypeDef *phost, + uint32_t storage_idx, + uint32_t objectformatcode, + uint32_t associationOH, + uint32_t* numobs); +USBH_StatusTypeDef USBH_MTP_GetStorageInfo (USBH_HandleTypeDef *phost, + uint8_t storage_idx, + MTP_StorageInfoTypedef *info); + +USBH_StatusTypeDef USBH_MTP_GetObjectHandles (USBH_HandleTypeDef *phost, + uint32_t storage_idx, + uint32_t objectformatcode, + uint32_t associationOH, + PTP_ObjectHandlesTypedef* objecthandles); + +USBH_StatusTypeDef USBH_MTP_GetObjectInfo (USBH_HandleTypeDef *phost, + uint32_t handle, + PTP_ObjectInfoTypedef* objectinfo); + +USBH_StatusTypeDef USBH_MTP_DeleteObject (USBH_HandleTypeDef *phost, + uint32_t handle, + uint32_t objectformatcode); + +USBH_StatusTypeDef USBH_MTP_GetObject (USBH_HandleTypeDef *phost, + uint32_t handle, + uint8_t *object); + +USBH_StatusTypeDef USBH_MTP_GetPartialObject(USBH_HandleTypeDef *phost, + uint32_t handle, + uint32_t offset, + uint32_t maxbytes, + uint8_t *object, + uint32_t *len); + +USBH_StatusTypeDef USBH_MTP_GetObjectPropsSupported (USBH_HandleTypeDef *phost, + uint16_t ofc, + uint32_t *propnum, + uint16_t *props); + +USBH_StatusTypeDef USBH_MTP_GetObjectPropDesc (USBH_HandleTypeDef *phost, + uint16_t opc, + uint16_t ofc, + PTP_ObjectPropDescTypeDef *opd); + +USBH_StatusTypeDef USBH_MTP_GetObjectPropList (USBH_HandleTypeDef *phost, + uint32_t handle, + MTP_PropertiesTypedef *pprops, + uint32_t *nrofprops); + +USBH_StatusTypeDef USBH_MTP_SendObject (USBH_HandleTypeDef *phost, + uint32_t handle, + uint8_t *object, + uint32_t size); + +USBH_StatusTypeDef USBH_MTP_GetDevicePropDesc (USBH_HandleTypeDef *phost, + uint16_t propcode, + PTP_DevicePropDescTypdef* devicepropertydesc); + +void USBH_MTP_EventsCallback(USBH_HandleTypeDef *phost, uint32_t event, uint32_t param); +/** +* @} +*/ + +#ifdef __cplusplus +} +#endif + +#endif /* __USBH_MTP_H */ + +/** +* @} +*/ + +/** +* @} +*/ + +/** +* @} +*/ + +/** +* @} +*/ +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Class/MTP/Inc/usbh_mtp_ptp.h b/system/Middlewares/ST/STM32_USB_Host_Library/Class/MTP/Inc/usbh_mtp_ptp.h index bf0bfc7d6b..0e3e384353 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Class/MTP/Inc/usbh_mtp_ptp.h +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Class/MTP/Inc/usbh_mtp_ptp.h @@ -1,1037 +1,1037 @@ -/** - ****************************************************************************** - * @file usbh_mtp_ptp.h - * @author MCD Application Team - * @brief Header file for usbh_mtp_ptp.c - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive ----------------------------------------------*/ -#ifndef __USBH_MTP_PTP_H -#define __USBH_MTP_PTP_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_core.h" - -/** @addtogroup USBH_LIB - * @{ - */ - -/** @addtogroup USBH_CLASS - * @{ - */ - -/** @addtogroup USBH_MTP_PTP_CLASS - * @{ - */ - -/** @defgroup USBH_MTP_PTP - * @brief This file is the Header file for usbh_mtp_ptp.c - * @{ - */ - - -/* Operation Codes */ - -/* PTP v1.0 operation codes */ -#define PTP_OC_Undefined 0x1000U -#define PTP_OC_GetDeviceInfo 0x1001U -#define PTP_OC_OpenSession 0x1002U -#define PTP_OC_CloseSession 0x1003U -#define PTP_OC_GetStorageIDs 0x1004U -#define PTP_OC_GetStorageInfo 0x1005U -#define PTP_OC_GetNumObjects 0x1006U -#define PTP_OC_GetObjectHandles 0x1007U -#define PTP_OC_GetObjectInfo 0x1008U -#define PTP_OC_GetObject 0x1009U -#define PTP_OC_GetThumb 0x100AU -#define PTP_OC_DeleteObject 0x100BU -#define PTP_OC_SendObjectInfo 0x100CU -#define PTP_OC_SendObject 0x100DU -#define PTP_OC_InitiateCapture 0x100EU -#define PTP_OC_FormatStore 0x100FU -#define PTP_OC_ResetDevice 0x1010U -#define PTP_OC_SelfTest 0x1011U -#define PTP_OC_SetObjectProtection 0x1012U -#define PTP_OC_PowerDown 0x1013U -#define PTP_OC_GetDevicePropDesc 0x1014U -#define PTP_OC_GetDevicePropValue 0x1015U -#define PTP_OC_SetDevicePropValue 0x1016U -#define PTP_OC_ResetDevicePropValue 0x1017U -#define PTP_OC_TerminateOpenCapture 0x1018U -#define PTP_OC_MoveObject 0x1019U -#define PTP_OC_CopyObject 0x101AU -#define PTP_OC_GetPartialObject 0x101BU -#define PTP_OC_InitiateOpenCapture 0x101CU - -/* PTP v1.1 operation codes */ -#define PTP_OC_StartEnumHandles 0x101DU -#define PTP_OC_EnumHandles 0x101EU -#define PTP_OC_StopEnumHandles 0x101FU -#define PTP_OC_GetVendorExtensionMaps 0x1020U -#define PTP_OC_GetVendorDeviceInfo 0x1021U -#define PTP_OC_GetResizedImageObject 0x1022U -#define PTP_OC_GetFilesystemManifest 0x1023U -#define PTP_OC_GetStreamInfo 0x1024U -#define PTP_OC_GetStream 0x1025U - - /* Microsoft / MTP extension codes */ -#define PTP_OC_GetObjectPropsSupported 0x9801U -#define PTP_OC_GetObjectPropDesc 0x9802U -#define PTP_OC_GetObjectPropValue 0x9803U -#define PTP_OC_SetObjectPropValue 0x9804U -#define PTP_OC_GetObjPropList 0x9805U -#define PTP_OC_SetObjPropList 0x9806U -#define PTP_OC_GetInterdependendPropdesc 0x9807U -#define PTP_OC_SendObjectPropList 0x9808U -#define PTP_OC_GetObjectReferences 0x9810U -#define PTP_OC_SetObjectReferences 0x9811U -#define PTP_OC_UpdateDeviceFirmware 0x9812U -#define PTP_OC_Skip 0x9820U - - -/* Response Codes */ - -/* PTP v1.0 response codes */ -#define PTP_RC_Undefined 0x2000U -#define PTP_RC_OK 0x2001U -#define PTP_RC_GeneralError 0x2002U -#define PTP_RC_SessionNotOpen 0x2003U -#define PTP_RC_InvalidTransactionID 0x2004U -#define PTP_RC_OperationNotSupported 0x2005U -#define PTP_RC_ParameterNotSupported 0x2006U -#define PTP_RC_IncompleteTransfer 0x2007U -#define PTP_RC_InvalidStorageId 0x2008U -#define PTP_RC_InvalidObjectHandle 0x2009U -#define PTP_RC_DevicePropNotSupported 0x200AU -#define PTP_RC_InvalidObjectFormatCode 0x200BU -#define PTP_RC_StoreFull 0x200CU -#define PTP_RC_ObjectWriteProtected 0x200DU -#define PTP_RC_StoreReadOnly 0x200EU -#define PTP_RC_AccessDenied 0x200FU -#define PTP_RC_NoThumbnailPresent 0x2010U -#define PTP_RC_SelfTestFailed 0x2011U -#define PTP_RC_PartialDeletion 0x2012U -#define PTP_RC_StoreNotAvailable 0x2013U -#define PTP_RC_SpecificationByFormatUnsupported 0x2014U -#define PTP_RC_NoValidObjectInfo 0x2015U -#define PTP_RC_InvalidCodeFormat 0x2016U -#define PTP_RC_UnknownVendorCode 0x2017U -#define PTP_RC_CaptureAlreadyTerminated 0x2018U -#define PTP_RC_DeviceBusy 0x2019U -#define PTP_RC_InvalidParentObject 0x201AU -#define PTP_RC_InvalidDevicePropFormat 0x201BU -#define PTP_RC_InvalidDevicePropValue 0x201CU -#define PTP_RC_InvalidParameter 0x201DU -#define PTP_RC_SessionAlreadyOpened 0x201EU -#define PTP_RC_TransactionCanceled 0x201FU -#define PTP_RC_SpecificationOfDestinationUnsupported 0x2020U -/* PTP v1.1 response codes */ -#define PTP_RC_InvalidEnumHandle 0x2021U -#define PTP_RC_NoStreamEnabled 0x2022U -#define PTP_RC_InvalidDataSet 0x2023U - -/* USB container types */ - -#define PTP_USB_CONTAINER_UNDEFINED 0x0000U -#define PTP_USB_CONTAINER_COMMAND 0x0001U -#define PTP_USB_CONTAINER_DATA 0x0002U -#define PTP_USB_CONTAINER_RESPONSE 0x0003U -#define PTP_USB_CONTAINER_EVENT 0x0004U - -/* PTP/IP definitions */ -#define PTPIP_INIT_COMMAND_REQUEST 1U -#define PTPIP_INIT_COMMAND_ACK 2U -#define PTPIP_INIT_EVENT_REQUEST 3U -#define PTPIP_INIT_EVENT_ACK 4U -#define PTPIP_INIT_FAIL 5U -#define PTPIP_CMD_REQUEST 6U -#define PTPIP_CMD_RESPONSE 7U -#define PTPIP_EVENT 8U -#define PTPIP_START_DATA_PACKET 9U -#define PTPIP_DATA_PACKET 10U -#define PTPIP_CANCEL_TRANSACTION 11U -#define PTPIP_END_DATA_PACKET 12U -#define PTPIP_PING 13U -#define PTPIP_PONG 14U - -/* Transaction data phase description */ -#define PTP_DP_NODATA 0x0000U /* no data phase */ -#define PTP_DP_SENDDATA 0x0001U /* sending data */ -#define PTP_DP_GETDATA 0x0002U /* receiving data */ -#define PTP_DP_DATA_MASK 0x00ffU /* data phase mask */ - -/** @defgroup USBH_MTP_PTP_Exported_Types - * @{ - */ - -typedef enum -{ - PTP_REQ_IDLE = 0, - PTP_REQ_SEND, - PTP_REQ_WAIT, - PTP_REQ_ERROR, -} -PTP_RequestStateTypeDef; - -typedef enum -{ - PTP_IDLE = 0, - PTP_OP_REQUEST_STATE, - PTP_OP_REQUEST_WAIT_STATE, - PTP_DATA_OUT_PHASE_STATE, - PTP_DATA_OUT_PHASE_WAIT_STATE, - PTP_DATA_IN_PHASE_STATE, - PTP_DATA_IN_PHASE_WAIT_STATE, - PTP_RESPONSE_STATE, - PTP_RESPONSE_WAIT_STATE, - PTP_ERROR, -} -PTP_ProcessStateTypeDef; - -/* PTP request/response/event general PTP container (transport independent) */ -typedef struct -{ - uint16_t Code; - uint32_t SessionID; - uint32_t Transaction_ID; - /* params may be of any type of size less or equal to uint32_t */ - uint32_t Param1; - uint32_t Param2; - uint32_t Param3; - /* events can only have three parameters */ - uint32_t Param4; - uint32_t Param5; - /* the number of meaningful parameters */ - uint8_t Nparam; -} -PTP_ContainerTypedef; - -#define PTP_USB_BULK_HS_MAX_PACKET_LEN_WRITE 1024U -#define PTP_USB_BULK_HS_MAX_PACKET_LEN_READ 1024U -#define PTP_USB_BULK_HDR_LEN (2U * sizeof(uint32_t) + 2U * sizeof(uint16_t)) -#define PTP_USB_BULK_PAYLOAD_LEN_WRITE (PTP_USB_BULK_HS_MAX_PACKET_LEN_WRITE-PTP_USB_BULK_HDR_LEN) -#define PTP_USB_BULK_PAYLOAD_LEN_READ (PTP_USB_BULK_HS_MAX_PACKET_LEN_READ - PTP_USB_BULK_HDR_LEN) -#define PTP_USB_BULK_REQ_LEN (PTP_USB_BULK_HDR_LEN + 5U * sizeof(uint32_t)) -#define PTP_USB_BULK_REQ_RESP_MAX_LEN 63U - -typedef struct -{ - uint32_t length; - uint16_t type; - uint16_t code; - uint32_t trans_id; - uint32_t param1; - uint32_t param2; - uint32_t param3; - uint32_t param4; - uint32_t param5; -} -PTP_RespContainerTypedef; - - -typedef struct -{ - uint32_t length; - uint16_t type; - uint16_t code; - uint32_t trans_id; - uint32_t param1; - uint32_t param2; - uint32_t param3; - uint32_t param4; - uint32_t param5; -} -PTP_OpContainerTypedef; - -typedef struct -{ - uint32_t length; - uint16_t type; - uint16_t code; - uint32_t trans_id; - union { - struct { - uint32_t param1; - uint32_t param2; - uint32_t param3; - uint32_t param4; - uint32_t param5; - } params; - uint8_t data[PTP_USB_BULK_PAYLOAD_LEN_READ]; - }payload; -} -PTP_DataContainerTypedef; - -/* PTP USB Asynchronous Event Interrupt Data Format */ -typedef struct -{ - uint32_t length; - uint16_t type; - uint16_t code; - uint32_t trans_id; - uint32_t param1; - uint32_t param2; - uint32_t param3; -} -PTP_EventContainerTypedef; - -/* Structure for PTP Transport process */ -typedef struct -{ - PTP_ProcessStateTypeDef state; - PTP_RequestStateTypeDef req_state; - PTP_OpContainerTypedef op_container; - PTP_DataContainerTypedef data_container; - PTP_RespContainerTypedef resp_container; - - /* ptp transaction ID */ - uint32_t transaction_id; - - /* ptp session ID */ - uint32_t session_id; - - /* device flags */ - uint32_t flags; - - /****** PTP transfer control *******/ - - /* Data pointer */ - uint8_t *data_ptr; - - /* Data length */ - uint32_t data_length; - - /* Data length */ - uint32_t data_packet; - - /* Data length */ - uint32_t iteration; - - /* Packet Index */ - uint32_t data_packet_counter; - - /****** Object transfer control *******/ - - /* object pointer */ - uint8_t *object_ptr; - -} -PTP_HandleTypeDef; - -/* DeviceInfo data offset */ -#define PTP_di_StandardVersion 0U -#define PTP_di_VendorExtensionID 2U -#define PTP_di_VendorExtensionVersion 6U -#define PTP_di_VendorExtensionDesc 8U -#define PTP_di_FunctionalMode 8U -#define PTP_di_OperationsSupported 10U - -/* Max info items size */ -#define PTP_SUPPORTED_OPERATIONS_NBR 100U -#define PTP_SUPPORTED_EVENTS_NBR 100U -#define PTP_SUPPORTED_PROPRIETIES_NBR 100U -#define PTP_CAPTURE_FORMATS_NBR 100U -#define PTP_IMAGE_FORMATS_NBR 100U -#define PTP_MAX_STR_SIZE 255U -/* PTP device info structure */ -typedef struct -{ - uint16_t StandardVersion; - uint32_t VendorExtensionID; - uint16_t VendorExtensionVersion; - uint8_t VendorExtensionDesc[PTP_MAX_STR_SIZE]; - uint16_t FunctionalMode; - uint32_t OperationsSupported_len; - uint16_t OperationsSupported[PTP_SUPPORTED_OPERATIONS_NBR]; - uint32_t EventsSupported_len; - uint16_t EventsSupported[PTP_SUPPORTED_EVENTS_NBR]; - uint32_t DevicePropertiesSupported_len; - uint16_t DevicePropertiesSupported[PTP_SUPPORTED_PROPRIETIES_NBR]; - uint32_t CaptureFormats_len; - uint16_t CaptureFormats[PTP_CAPTURE_FORMATS_NBR]; - uint32_t ImageFormats_len; - uint16_t ImageFormats[PTP_IMAGE_FORMATS_NBR]; - uint8_t Manufacturer[PTP_MAX_STR_SIZE]; - uint8_t Model[PTP_MAX_STR_SIZE]; - uint8_t DeviceVersion[PTP_MAX_STR_SIZE]; - uint8_t SerialNumber[PTP_MAX_STR_SIZE]; -} -PTP_DeviceInfoTypedef; - -#define PTP_MAX_STORAGE_UNITS_NBR 5 -/* PTP storageIDs structute (returned by GetStorageIDs) */ -typedef struct -{ - uint32_t n; - uint32_t Storage [PTP_MAX_STORAGE_UNITS_NBR]; -} -PTP_StorageIDsTypedef; - -/* PTP StorageInfo structure (returned by GetStorageInfo) */ - -#define PTP_si_StorageType 0U -#define PTP_si_FilesystemType 2U -#define PTP_si_AccessCapability 4U -#define PTP_si_MaxCapability 6U -#define PTP_si_FreeSpaceInBytes 14U -#define PTP_si_FreeSpaceInImages 22U -#define PTP_si_StorageDescription 26U - - -/* PTP Storage Types */ - -#define PTP_ST_Undefined 0x0000U -#define PTP_ST_FixedROM 0x0001U -#define PTP_ST_RemovableROM 0x0002U -#define PTP_ST_FixedRAM 0x0003U -#define PTP_ST_RemovableRAM 0x0004U - -/* PTP FilesystemType Values */ - -#define PTP_FST_Undefined 0x0000U -#define PTP_FST_GenericFlat 0x0001U -#define PTP_FST_GenericHierarchical 0x0002U -#define PTP_FST_DCF 0x0003U - -/* PTP StorageInfo AccessCapability Values */ - -#define PTP_AC_ReadWrite 0x0000U -#define PTP_AC_ReadOnly 0x0001U -#define PTP_AC_ReadOnly_with_Object_Deletion 0x0002U - -typedef struct -{ - uint16_t StorageType; - uint16_t FilesystemType; - uint16_t AccessCapability; - uint64_t MaxCapability; - uint64_t FreeSpaceInBytes; - uint32_t FreeSpaceInImages; - uint8_t StorageDescription[PTP_MAX_STR_SIZE]; - uint8_t VolumeLabel[PTP_MAX_STR_SIZE]; -} -PTP_StorageInfoTypedef; - -/* PTP Object Format Codes */ - -/* ancillary formats */ -#define PTP_OFC_Undefined 0x3000U -#define PTP_OFC_Defined 0x3800U -#define PTP_OFC_Association 0x3001U -#define PTP_OFC_Script 0x3002U -#define PTP_OFC_Executable 0x3003U -#define PTP_OFC_Text 0x3004U -#define PTP_OFC_HTML 0x3005U -#define PTP_OFC_DPOF 0x3006U -#define PTP_OFC_AIFF 0x3007U -#define PTP_OFC_WAV 0x3008U -#define PTP_OFC_MP3 0x3009U -#define PTP_OFC_AVI 0x300AU -#define PTP_OFC_MPEG 0x300BU -#define PTP_OFC_ASF 0x300CU -#define PTP_OFC_QT 0x300DU /* guessing */ -/* image formats */ -#define PTP_OFC_EXIF_JPEG 0x3801U -#define PTP_OFC_TIFF_EP 0x3802U -#define PTP_OFC_FlashPix 0x3803U -#define PTP_OFC_BMP 0x3804U -#define PTP_OFC_CIFF 0x3805U -#define PTP_OFC_Undefined_0x3806 0x3806U -#define PTP_OFC_GIF 0x3807U -#define PTP_OFC_JFIF 0x3808U -#define PTP_OFC_PCD 0x3809U -#define PTP_OFC_PICT 0x380AU -#define PTP_OFC_PNG 0x380BU -#define PTP_OFC_Undefined_0x380C 0x380CU -#define PTP_OFC_TIFF 0x380DU -#define PTP_OFC_TIFF_IT 0x380EU -#define PTP_OFC_JP2 0x380FU -#define PTP_OFC_JPX 0x3810U -/* ptp v1.1 has only DNG new */ -#define PTP_OFC_DNG 0x3811U - -/* MTP extensions */ -#define PTP_OFC_MTP_MediaCard 0xb211U -#define PTP_OFC_MTP_MediaCardGroup 0xb212U -#define PTP_OFC_MTP_Encounter 0xb213U -#define PTP_OFC_MTP_EncounterBox 0xb214U -#define PTP_OFC_MTP_M4A 0xb215U -#define PTP_OFC_MTP_ZUNEUNDEFINED 0xb217U/* Unknown file type */ -#define PTP_OFC_MTP_Firmware 0xb802U -#define PTP_OFC_MTP_WindowsImageFormat 0xb881U -#define PTP_OFC_MTP_UndefinedAudio 0xb900U -#define PTP_OFC_MTP_WMA 0xb901U -#define PTP_OFC_MTP_OGG 0xb902U -#define PTP_OFC_MTP_AAC 0xb903U -#define PTP_OFC_MTP_AudibleCod 0xb904U -#define PTP_OFC_MTP_FLAC 0xb906U -#define PTP_OFC_MTP_SamsungPlaylist 0xb909U -#define PTP_OFC_MTP_UndefinedVideo 0xb980U -#define PTP_OFC_MTP_WMV 0xb981U -#define PTP_OFC_MTP_MP4 0xb982U -#define PTP_OFC_MTP_MP2 0xb983U -#define PTP_OFC_MTP_3GP 0xb984U -#define PTP_OFC_MTP_UndefinedCollection 0xba00U -#define PTP_OFC_MTP_AbstractMultimediaAlbum 0xba01U -#define PTP_OFC_MTP_AbstractImageAlbum 0xba02U -#define PTP_OFC_MTP_AbstractAudioAlbum 0xba03U -#define PTP_OFC_MTP_AbstractVideoAlbum 0xba04U -#define PTP_OFC_MTP_AbstractAudioVideoPlaylist 0xba05U -#define PTP_OFC_MTP_AbstractContactGroup 0xba06U -#define PTP_OFC_MTP_AbstractMessageFolder 0xba07U -#define PTP_OFC_MTP_AbstractChapteredProduction 0xba08U -#define PTP_OFC_MTP_AbstractAudioPlaylist 0xba09U -#define PTP_OFC_MTP_AbstractVideoPlaylist 0xba0aU -#define PTP_OFC_MTP_AbstractMediacast 0xba0bU -#define PTP_OFC_MTP_WPLPlaylist 0xba10U -#define PTP_OFC_MTP_M3UPlaylist 0xba11U -#define PTP_OFC_MTP_MPLPlaylist 0xba12U -#define PTP_OFC_MTP_ASXPlaylist 0xba13U -#define PTP_OFC_MTP_PLSPlaylist 0xba14U -#define PTP_OFC_MTP_UndefinedDocument 0xba80U -#define PTP_OFC_MTP_AbstractDocument 0xba81U -#define PTP_OFC_MTP_XMLDocument 0xba82U -#define PTP_OFC_MTP_MSWordDocument 0xba83U -#define PTP_OFC_MTP_MHTCompiledHTMLDocument 0xba84U -#define PTP_OFC_MTP_MSExcelSpreadsheetXLS 0xba85U -#define PTP_OFC_MTP_MSPowerpointPresentationPPT 0xba86U -#define PTP_OFC_MTP_UndefinedMessage 0xbb00U -#define PTP_OFC_MTP_AbstractMessage 0xbb01U -#define PTP_OFC_MTP_UndefinedContact 0xbb80U -#define PTP_OFC_MTP_AbstractContact 0xbb81U -#define PTP_OFC_MTP_vCard2 0xbb82U -#define PTP_OFC_MTP_vCard3 0xbb83U -#define PTP_OFC_MTP_UndefinedCalendarItem 0xbe00U -#define PTP_OFC_MTP_AbstractCalendarItem 0xbe01U -#define PTP_OFC_MTP_vCalendar1 0xbe02U -#define PTP_OFC_MTP_vCalendar2 0xbe03U -#define PTP_OFC_MTP_UndefinedWindowsExecutable 0xbe80U -#define PTP_OFC_MTP_MediaCast 0xbe81U -#define PTP_OFC_MTP_Section 0xbe82U - -/* MTP specific Object Properties */ -#define PTP_OPC_StorageID 0xDC01U -#define PTP_OPC_ObjectFormat 0xDC02U -#define PTP_OPC_ProtectionStatus 0xDC03U -#define PTP_OPC_ObjectSize 0xDC04U -#define PTP_OPC_AssociationType 0xDC05U -#define PTP_OPC_AssociationDesc 0xDC06U -#define PTP_OPC_ObjectFileName 0xDC07U -#define PTP_OPC_DateCreated 0xDC08U -#define PTP_OPC_DateModified 0xDC09U -#define PTP_OPC_Keywords 0xDC0AU -#define PTP_OPC_ParentObject 0xDC0BU -#define PTP_OPC_AllowedFolderContents 0xDC0CU -#define PTP_OPC_Hidden 0xDC0DU -#define PTP_OPC_SystemObject 0xDC0EU -#define PTP_OPC_PersistantUniqueObjectIdentifier 0xDC41U -#define PTP_OPC_SyncID 0xDC42U -#define PTP_OPC_PropertyBag 0xDC43U -#define PTP_OPC_Name 0xDC44U -#define PTP_OPC_CreatedBy 0xDC45U -#define PTP_OPC_Artist 0xDC46U -#define PTP_OPC_DateAuthored 0xDC47U -#define PTP_OPC_Description 0xDC48U -#define PTP_OPC_URLReference 0xDC49U -#define PTP_OPC_LanguageLocale 0xDC4AU -#define PTP_OPC_CopyrightInformation 0xDC4BU -#define PTP_OPC_Source 0xDC4CU -#define PTP_OPC_OriginLocation 0xDC4DU -#define PTP_OPC_DateAdded 0xDC4EU -#define PTP_OPC_NonConsumable 0xDC4FU -#define PTP_OPC_CorruptOrUnplayable 0xDC50U -#define PTP_OPC_ProducerSerialNumber 0xDC51U -#define PTP_OPC_RepresentativeSampleFormat 0xDC81U -#define PTP_OPC_RepresentativeSampleSize 0xDC82U -#define PTP_OPC_RepresentativeSampleHeight 0xDC83U -#define PTP_OPC_RepresentativeSampleWidth 0xDC84U -#define PTP_OPC_RepresentativeSampleDuration 0xDC85U -#define PTP_OPC_RepresentativeSampleData 0xDC86U -#define PTP_OPC_Width 0xDC87U -#define PTP_OPC_Height 0xDC88U -#define PTP_OPC_Duration 0xDC89U -#define PTP_OPC_Rating 0xDC8AU -#define PTP_OPC_Track 0xDC8BU -#define PTP_OPC_Genre 0xDC8CU -#define PTP_OPC_Credits 0xDC8DU -#define PTP_OPC_Lyrics 0xDC8EU -#define PTP_OPC_SubscriptionContentID 0xDC8FU -#define PTP_OPC_ProducedBy 0xDC90U -#define PTP_OPC_UseCount 0xDC91U -#define PTP_OPC_SkipCount 0xDC92U -#define PTP_OPC_LastAccessed 0xDC93U -#define PTP_OPC_ParentalRating 0xDC94U -#define PTP_OPC_MetaGenre 0xDC95U -#define PTP_OPC_Composer 0xDC96U -#define PTP_OPC_EffectiveRating 0xDC97U -#define PTP_OPC_Subtitle 0xDC98U -#define PTP_OPC_OriginalReleaseDate 0xDC99U -#define PTP_OPC_AlbumName 0xDC9AU -#define PTP_OPC_AlbumArtist 0xDC9BU -#define PTP_OPC_Mood 0xDC9CU -#define PTP_OPC_DRMStatus 0xDC9DU -#define PTP_OPC_SubDescription 0xDC9EU -#define PTP_OPC_IsCropped 0xDCD1U -#define PTP_OPC_IsColorCorrected 0xDCD2U -#define PTP_OPC_ImageBitDepth 0xDCD3U -#define PTP_OPC_Fnumber 0xDCD4U -#define PTP_OPC_ExposureTime 0xDCD5U -#define PTP_OPC_ExposureIndex 0xDCD6U -#define PTP_OPC_DisplayName 0xDCE0U -#define PTP_OPC_BodyText 0xDCE1U -#define PTP_OPC_Subject 0xDCE2U -#define PTP_OPC_Priority 0xDCE3U -#define PTP_OPC_GivenName 0xDD00U -#define PTP_OPC_MiddleNames 0xDD01U -#define PTP_OPC_FamilyName 0xDD02U -#define PTP_OPC_Prefix 0xDD03U -#define PTP_OPC_Suffix 0xDD04U -#define PTP_OPC_PhoneticGivenName 0xDD05U -#define PTP_OPC_PhoneticFamilyName 0xDD06U -#define PTP_OPC_EmailPrimary 0xDD07U -#define PTP_OPC_EmailPersonal1 0xDD08U -#define PTP_OPC_EmailPersonal2 0xDD09U -#define PTP_OPC_EmailBusiness1 0xDD0AU -#define PTP_OPC_EmailBusiness2 0xDD0BU -#define PTP_OPC_EmailOthers 0xDD0CU -#define PTP_OPC_PhoneNumberPrimary 0xDD0DU -#define PTP_OPC_PhoneNumberPersonal 0xDD0EU -#define PTP_OPC_PhoneNumberPersonal2 0xDD0FU -#define PTP_OPC_PhoneNumberBusiness 0xDD10U -#define PTP_OPC_PhoneNumberBusiness2 0xDD11U -#define PTP_OPC_PhoneNumberMobile 0xDD12U -#define PTP_OPC_PhoneNumberMobile2 0xDD13U -#define PTP_OPC_FaxNumberPrimary 0xDD14U -#define PTP_OPC_FaxNumberPersonal 0xDD15U -#define PTP_OPC_FaxNumberBusiness 0xDD16U -#define PTP_OPC_PagerNumber 0xDD17U -#define PTP_OPC_PhoneNumberOthers 0xDD18U -#define PTP_OPC_PrimaryWebAddress 0xDD19U -#define PTP_OPC_PersonalWebAddress 0xDD1AU -#define PTP_OPC_BusinessWebAddress 0xDD1BU -#define PTP_OPC_InstantMessengerAddress 0xDD1CU -#define PTP_OPC_InstantMessengerAddress2 0xDD1DU -#define PTP_OPC_InstantMessengerAddress3 0xDD1EU -#define PTP_OPC_PostalAddressPersonalFull 0xDD1FU -#define PTP_OPC_PostalAddressPersonalFullLine1 0xDD20U -#define PTP_OPC_PostalAddressPersonalFullLine2 0xDD21U -#define PTP_OPC_PostalAddressPersonalFullCity 0xDD22U -#define PTP_OPC_PostalAddressPersonalFullRegion 0xDD23U -#define PTP_OPC_PostalAddressPersonalFullPostalCode 0xDD24U -#define PTP_OPC_PostalAddressPersonalFullCountry 0xDD25U -#define PTP_OPC_PostalAddressBusinessFull 0xDD26U -#define PTP_OPC_PostalAddressBusinessLine1 0xDD27U -#define PTP_OPC_PostalAddressBusinessLine2 0xDD28U -#define PTP_OPC_PostalAddressBusinessCity 0xDD29U -#define PTP_OPC_PostalAddressBusinessRegion 0xDD2AU -#define PTP_OPC_PostalAddressBusinessPostalCode 0xDD2BU -#define PTP_OPC_PostalAddressBusinessCountry 0xDD2CU -#define PTP_OPC_PostalAddressOtherFull 0xDD2DU -#define PTP_OPC_PostalAddressOtherLine1 0xDD2EU -#define PTP_OPC_PostalAddressOtherLine2 0xDD2FU -#define PTP_OPC_PostalAddressOtherCity 0xDD30U -#define PTP_OPC_PostalAddressOtherRegion 0xDD31U -#define PTP_OPC_PostalAddressOtherPostalCode 0xDD32U -#define PTP_OPC_PostalAddressOtherCountry 0xDD33U -#define PTP_OPC_OrganizationName 0xDD34U -#define PTP_OPC_PhoneticOrganizationName 0xDD35U -#define PTP_OPC_Role 0xDD36U -#define PTP_OPC_Birthdate 0xDD37U -#define PTP_OPC_MessageTo 0xDD40U -#define PTP_OPC_MessageCC 0xDD41U -#define PTP_OPC_MessageBCC 0xDD42U -#define PTP_OPC_MessageRead 0xDD43U -#define PTP_OPC_MessageReceivedTime 0xDD44U -#define PTP_OPC_MessageSender 0xDD45U -#define PTP_OPC_ActivityBeginTime 0xDD50U -#define PTP_OPC_ActivityEndTime 0xDD51U -#define PTP_OPC_ActivityLocation 0xDD52U -#define PTP_OPC_ActivityRequiredAttendees 0xDD54U -#define PTP_OPC_ActivityOptionalAttendees 0xDD55U -#define PTP_OPC_ActivityResources 0xDD56U -#define PTP_OPC_ActivityAccepted 0xDD57U -#define PTP_OPC_Owner 0xDD5DU -#define PTP_OPC_Editor 0xDD5EU -#define PTP_OPC_Webmaster 0xDD5FU -#define PTP_OPC_URLSource 0xDD60U -#define PTP_OPC_URLDestination 0xDD61U -#define PTP_OPC_TimeBookmark 0xDD62U -#define PTP_OPC_ObjectBookmark 0xDD63U -#define PTP_OPC_ByteBookmark 0xDD64U -#define PTP_OPC_LastBuildDate 0xDD70U -#define PTP_OPC_TimetoLive 0xDD71U -#define PTP_OPC_MediaGUID 0xDD72U -#define PTP_OPC_TotalBitRate 0xDE91U -#define PTP_OPC_BitRateType 0xDE92U -#define PTP_OPC_SampleRate 0xDE93U -#define PTP_OPC_NumberOfChannels 0xDE94U -#define PTP_OPC_AudioBitDepth 0xDE95U -#define PTP_OPC_ScanDepth 0xDE97U -#define PTP_OPC_AudioWAVECodec 0xDE99U -#define PTP_OPC_AudioBitRate 0xDE9AU -#define PTP_OPC_VideoFourCCCodec 0xDE9BU -#define PTP_OPC_VideoBitRate 0xDE9CU -#define PTP_OPC_FramesPerThousandSeconds 0xDE9DU -#define PTP_OPC_KeyFrameDistance 0xDE9EU -#define PTP_OPC_BufferSize 0xDE9FU -#define PTP_OPC_EncodingQuality 0xDEA0U -#define PTP_OPC_EncodingProfile 0xDEA1U -#define PTP_OPC_BuyFlag 0xD901U - -/* WiFi Provisioning MTP Extension property codes */ -#define PTP_OPC_WirelessConfigurationFile 0xB104U - - -/* PTP Association Types */ -#define PTP_AT_Undefined 0x0000U -#define PTP_AT_GenericFolder 0x0001U -#define PTP_AT_Album 0x0002U -#define PTP_AT_TimeSequence 0x0003U -#define PTP_AT_HorizontalPanoramic 0x0004U -#define PTP_AT_VerticalPanoramic 0x0005U -#define PTP_AT_2DPanoramic 0x0006U -#define PTP_AT_AncillaryData 0x0007U - -#define PTP_MAX_HANDLER_NBR 0x255U -typedef struct -{ - uint32_t n; - uint32_t Handler[PTP_MAX_HANDLER_NBR]; -} -PTP_ObjectHandlesTypedef; - - -#define PTP_oi_StorageID 0U -#define PTP_oi_ObjectFormat 4U -#define PTP_oi_ProtectionStatus 6U -#define PTP_oi_ObjectCompressedSize 8U -#define PTP_oi_ThumbFormat 12U -#define PTP_oi_ThumbCompressedSize 14U -#define PTP_oi_ThumbPixWidth 18U -#define PTP_oi_ThumbPixHeight 22U -#define PTP_oi_ImagePixWidth 26U -#define PTP_oi_ImagePixHeight 30U -#define PTP_oi_ImageBitDepth 34U -#define PTP_oi_ParentObject 38U -#define PTP_oi_AssociationType 42U -#define PTP_oi_AssociationDesc 44U -#define PTP_oi_SequenceNumber 48U -#define PTP_oi_filenamelen 52U -#define PTP_oi_Filename 53U - -typedef struct -{ - uint32_t StorageID; - uint16_t ObjectFormat; - uint16_t ProtectionStatus; - /* In the regular objectinfo this is 32bit, but we keep the general object size here - that also arrives via other methods and so use 64bit */ - uint64_t ObjectCompressedSize; - uint16_t ThumbFormat; - uint32_t ThumbCompressedSize; - uint32_t ThumbPixWidth; - uint32_t ThumbPixHeight; - uint32_t ImagePixWidth; - uint32_t ImagePixHeight; - uint32_t ImageBitDepth; - uint32_t ParentObject; - uint16_t AssociationType; - uint32_t AssociationDesc; - uint32_t SequenceNumber; - uint8_t Filename[PTP_MAX_STR_SIZE]; - uint32_t CaptureDate; - uint32_t ModificationDate; - uint8_t Keywords[PTP_MAX_STR_SIZE]; -} -PTP_ObjectInfoTypedef; - -/* Object Property Describing Dataset (DevicePropDesc) */ - -typedef union _PTP_PropertyValueTypedef -{ - char str[PTP_MAX_STR_SIZE]; - uint8_t u8; - int8_t i8; - uint16_t u16; - int16_t i16; - uint32_t u32; - int32_t i32; - uint64_t u64; - int64_t i64; - struct array { - uint32_t count; - union _PTP_PropertyValueTypedef *v; - }a; -}PTP_PropertyValueTypedef; - -typedef struct -{ - PTP_PropertyValueTypedef MinimumValue; - PTP_PropertyValueTypedef MaximumValue; - PTP_PropertyValueTypedef StepSize; -} -PTP_PropDescRangeFormTypedef; - -/* Property Describing Dataset, Enum Form */ - -typedef struct -{ - uint16_t NumberOfValues; - PTP_PropertyValueTypedef SupportedValue[PTP_SUPPORTED_PROPRIETIES_NBR]; -} -PTP_PropDescEnumFormTypedef; - -/* (MTP) Object Property pack/unpack */ -#define PTP_opd_ObjectPropertyCode 0U -#define PTP_opd_DataType 2U -#define PTP_opd_GetSet 4U -#define PTP_opd_FactoryDefaultValue 5U - -typedef struct -{ - uint16_t ObjectPropertyCode; - uint16_t DataType; - uint8_t GetSet; - PTP_PropertyValueTypedef FactoryDefaultValue; - uint32_t GroupCode; - uint8_t FormFlag; - union { - PTP_PropDescEnumFormTypedef Enum; - PTP_PropDescRangeFormTypedef Range; - } FORM; -} -PTP_ObjectPropDescTypeDef; - -/* Metadata lists for MTP operations */ -typedef struct -{ - uint16_t property; - uint16_t datatype; - uint32_t ObjectHandle; - PTP_PropertyValueTypedef propval; -} -MTP_PropertiesTypedef; - - -/* Device Property Form Flag */ - -#define PTP_DPFF_None 0x00U -#define PTP_DPFF_Range 0x01U -#define PTP_DPFF_Enumeration 0x02U - -/* Object Property Codes used by MTP (first 3 are same as DPFF codes) */ -#define PTP_OPFF_None 0x00U -#define PTP_OPFF_Range 0x01U -#define PTP_OPFF_Enumeration 0x02U -#define PTP_OPFF_DateTime 0x03U -#define PTP_OPFF_FixedLengthArray 0x04U -#define PTP_OPFF_RegularExpression 0x05U -#define PTP_OPFF_ByteArray 0x06U -#define PTP_OPFF_LongString 0xFFU - -/* Device Property pack/unpack */ - -#define PTP_dpd_DevicePropertyCode 0U -#define PTP_dpd_DataType 2U -#define PTP_dpd_GetSet 4U -#define PTP_dpd_FactoryDefaultValue 5U - -/* Device Property Describing Dataset (DevicePropDesc) */ - -typedef struct -{ - uint16_t DevicePropertyCode; - uint16_t DataType; - uint8_t GetSet; - PTP_PropertyValueTypedef FactoryDefaultValue; - PTP_PropertyValueTypedef CurrentValue; - uint8_t FormFlag; - union { - PTP_PropDescEnumFormTypedef Enum; - PTP_PropDescRangeFormTypedef Range; - } FORM; -} -PTP_DevicePropDescTypdef; - -/* DataType Codes */ - -#define PTP_DTC_UNDEF 0x0000U -#define PTP_DTC_INT8 0x0001U -#define PTP_DTC_UINT8 0x0002U -#define PTP_DTC_INT16 0x0003U -#define PTP_DTC_UINT16 0x0004U -#define PTP_DTC_INT32 0x0005U -#define PTP_DTC_UINT32 0x0006U -#define PTP_DTC_INT64 0x0007U -#define PTP_DTC_UINT64 0x0008U -#define PTP_DTC_INT128 0x0009U -#define PTP_DTC_UINT128 0x000AU - -#define PTP_DTC_ARRAY_MASK 0x4000U - -#define PTP_DTC_AINT8 (PTP_DTC_ARRAY_MASK | PTP_DTC_INT8) -#define PTP_DTC_AUINT8 (PTP_DTC_ARRAY_MASK | PTP_DTC_UINT8) -#define PTP_DTC_AINT16 (PTP_DTC_ARRAY_MASK | PTP_DTC_INT16) -#define PTP_DTC_AUINT16 (PTP_DTC_ARRAY_MASK | PTP_DTC_UINT16) -#define PTP_DTC_AINT32 (PTP_DTC_ARRAY_MASK | PTP_DTC_INT32) -#define PTP_DTC_AUINT32 (PTP_DTC_ARRAY_MASK | PTP_DTC_UINT32) -#define PTP_DTC_AINT64 (PTP_DTC_ARRAY_MASK | PTP_DTC_INT64) -#define PTP_DTC_AUINT64 (PTP_DTC_ARRAY_MASK | PTP_DTC_UINT64) -#define PTP_DTC_AINT128 (PTP_DTC_ARRAY_MASK | PTP_DTC_INT128) -#define PTP_DTC_AUINT128 (PTP_DTC_ARRAY_MASK | PTP_DTC_UINT128) - -#define PTP_DTC_STR 0xFFFFU - -/* PTP Event Codes */ - -#define PTP_EC_Undefined 0x4000U -#define PTP_EC_CancelTransaction 0x4001U -#define PTP_EC_ObjectAdded 0x4002U -#define PTP_EC_ObjectRemoved 0x4003U -#define PTP_EC_StoreAdded 0x4004U -#define PTP_EC_StoreRemoved 0x4005U -#define PTP_EC_DevicePropChanged 0x4006U -#define PTP_EC_ObjectInfoChanged 0x4007U -#define PTP_EC_DeviceInfoChanged 0x4008U -#define PTP_EC_RequestObjectTransfer 0x4009U -#define PTP_EC_StoreFull 0x400AU -#define PTP_EC_DeviceReset 0x400BU -#define PTP_EC_StorageInfoChanged 0x400CU -#define PTP_EC_CaptureComplete 0x400DU -#define PTP_EC_UnreportedStatus 0x400EU - - -/** - * @} - */ - -/** @defgroup USBH_MTP_PTP_Exported_Macros - * @{ - */ -/** - * @} - */ - -/** @defgroup USBH_MTP_PTP_Exported_Variables - * @{ - */ - -/** - * @} - */ - -/** @defgroup USBH_MTP_PTP_Exported_FunctionsPrototype - * @{ - */ -USBH_StatusTypeDef USBH_PTP_Init(USBH_HandleTypeDef *phost); -USBH_StatusTypeDef USBH_PTP_Process (USBH_HandleTypeDef *phost); - -USBH_StatusTypeDef USBH_PTP_SendRequest (USBH_HandleTypeDef *phost, PTP_ContainerTypedef *req); -USBH_StatusTypeDef USBH_PTP_GetResponse (USBH_HandleTypeDef *phost, PTP_ContainerTypedef *resp); - -USBH_StatusTypeDef USBH_PTP_OpenSession (USBH_HandleTypeDef *phost, uint32_t session); -USBH_StatusTypeDef USBH_PTP_GetDeviceInfo (USBH_HandleTypeDef *phost, PTP_DeviceInfoTypedef *dev_info); -USBH_StatusTypeDef USBH_PTP_GetStorageIds (USBH_HandleTypeDef *phost, PTP_StorageIDsTypedef *storage_ids); - -USBH_StatusTypeDef USBH_PTP_GetStorageInfo (USBH_HandleTypeDef *phost, - uint32_t storage_id, - PTP_StorageInfoTypedef *storage_info); - -USBH_StatusTypeDef USBH_PTP_GetNumObjects (USBH_HandleTypeDef *phost, - uint32_t storage_id, - uint32_t objectformatcode, - uint32_t associationOH, - uint32_t* numobs); - -USBH_StatusTypeDef USBH_PTP_GetObjectHandles (USBH_HandleTypeDef *phost, - uint32_t storage_id, - uint32_t objectformatcode, - uint32_t associationOH, - PTP_ObjectHandlesTypedef* objecthandles); - -USBH_StatusTypeDef USBH_PTP_GetObjectInfo (USBH_HandleTypeDef *phost, - uint32_t handle, - PTP_ObjectInfoTypedef *object_info); - -USBH_StatusTypeDef USBH_PTP_DeleteObject (USBH_HandleTypeDef *phost, - uint32_t handle, - uint32_t objectformatcode); - -USBH_StatusTypeDef USBH_PTP_GetObject (USBH_HandleTypeDef *phost, - uint32_t handle, - uint8_t *object); - -USBH_StatusTypeDef USBH_PTP_GetPartialObject(USBH_HandleTypeDef *phost, - uint32_t handle, - uint32_t offset, - uint32_t maxbytes, uint8_t *object, - uint32_t *len); - -USBH_StatusTypeDef USBH_PTP_GetObjectPropsSupported (USBH_HandleTypeDef *phost, - uint16_t ofc, - uint32_t *propnum, - uint16_t *props); - -USBH_StatusTypeDef USBH_PTP_GetObjectPropDesc (USBH_HandleTypeDef *phost, - uint16_t opc, - uint16_t ofc, - PTP_ObjectPropDescTypeDef *opd); - -USBH_StatusTypeDef USBH_PTP_GetObjectPropList (USBH_HandleTypeDef *phost, - uint32_t handle, - MTP_PropertiesTypedef *pprops, - uint32_t *nrofprops); - -USBH_StatusTypeDef USBH_PTP_SendObject (USBH_HandleTypeDef *phost, - uint32_t handle, - uint8_t *object, - uint32_t size); - -USBH_StatusTypeDef USBH_PTP_GetDevicePropDesc (USBH_HandleTypeDef *phost, - uint16_t propcode, - PTP_DevicePropDescTypdef* devicepropertydesc); - -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USBH_MTP_PTP_H */ - - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - +/** + ****************************************************************************** + * @file usbh_mtp_ptp.h + * @author MCD Application Team + * @brief Header file for usbh_mtp_ptp.c + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive ----------------------------------------------*/ +#ifndef __USBH_MTP_PTP_H +#define __USBH_MTP_PTP_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_core.h" + +/** @addtogroup USBH_LIB + * @{ + */ + +/** @addtogroup USBH_CLASS + * @{ + */ + +/** @addtogroup USBH_MTP_PTP_CLASS + * @{ + */ + +/** @defgroup USBH_MTP_PTP + * @brief This file is the Header file for usbh_mtp_ptp.c + * @{ + */ + + +/* Operation Codes */ + +/* PTP v1.0 operation codes */ +#define PTP_OC_Undefined 0x1000U +#define PTP_OC_GetDeviceInfo 0x1001U +#define PTP_OC_OpenSession 0x1002U +#define PTP_OC_CloseSession 0x1003U +#define PTP_OC_GetStorageIDs 0x1004U +#define PTP_OC_GetStorageInfo 0x1005U +#define PTP_OC_GetNumObjects 0x1006U +#define PTP_OC_GetObjectHandles 0x1007U +#define PTP_OC_GetObjectInfo 0x1008U +#define PTP_OC_GetObject 0x1009U +#define PTP_OC_GetThumb 0x100AU +#define PTP_OC_DeleteObject 0x100BU +#define PTP_OC_SendObjectInfo 0x100CU +#define PTP_OC_SendObject 0x100DU +#define PTP_OC_InitiateCapture 0x100EU +#define PTP_OC_FormatStore 0x100FU +#define PTP_OC_ResetDevice 0x1010U +#define PTP_OC_SelfTest 0x1011U +#define PTP_OC_SetObjectProtection 0x1012U +#define PTP_OC_PowerDown 0x1013U +#define PTP_OC_GetDevicePropDesc 0x1014U +#define PTP_OC_GetDevicePropValue 0x1015U +#define PTP_OC_SetDevicePropValue 0x1016U +#define PTP_OC_ResetDevicePropValue 0x1017U +#define PTP_OC_TerminateOpenCapture 0x1018U +#define PTP_OC_MoveObject 0x1019U +#define PTP_OC_CopyObject 0x101AU +#define PTP_OC_GetPartialObject 0x101BU +#define PTP_OC_InitiateOpenCapture 0x101CU + +/* PTP v1.1 operation codes */ +#define PTP_OC_StartEnumHandles 0x101DU +#define PTP_OC_EnumHandles 0x101EU +#define PTP_OC_StopEnumHandles 0x101FU +#define PTP_OC_GetVendorExtensionMaps 0x1020U +#define PTP_OC_GetVendorDeviceInfo 0x1021U +#define PTP_OC_GetResizedImageObject 0x1022U +#define PTP_OC_GetFilesystemManifest 0x1023U +#define PTP_OC_GetStreamInfo 0x1024U +#define PTP_OC_GetStream 0x1025U + + /* Microsoft / MTP extension codes */ +#define PTP_OC_GetObjectPropsSupported 0x9801U +#define PTP_OC_GetObjectPropDesc 0x9802U +#define PTP_OC_GetObjectPropValue 0x9803U +#define PTP_OC_SetObjectPropValue 0x9804U +#define PTP_OC_GetObjPropList 0x9805U +#define PTP_OC_SetObjPropList 0x9806U +#define PTP_OC_GetInterdependendPropdesc 0x9807U +#define PTP_OC_SendObjectPropList 0x9808U +#define PTP_OC_GetObjectReferences 0x9810U +#define PTP_OC_SetObjectReferences 0x9811U +#define PTP_OC_UpdateDeviceFirmware 0x9812U +#define PTP_OC_Skip 0x9820U + + +/* Response Codes */ + +/* PTP v1.0 response codes */ +#define PTP_RC_Undefined 0x2000U +#define PTP_RC_OK 0x2001U +#define PTP_RC_GeneralError 0x2002U +#define PTP_RC_SessionNotOpen 0x2003U +#define PTP_RC_InvalidTransactionID 0x2004U +#define PTP_RC_OperationNotSupported 0x2005U +#define PTP_RC_ParameterNotSupported 0x2006U +#define PTP_RC_IncompleteTransfer 0x2007U +#define PTP_RC_InvalidStorageId 0x2008U +#define PTP_RC_InvalidObjectHandle 0x2009U +#define PTP_RC_DevicePropNotSupported 0x200AU +#define PTP_RC_InvalidObjectFormatCode 0x200BU +#define PTP_RC_StoreFull 0x200CU +#define PTP_RC_ObjectWriteProtected 0x200DU +#define PTP_RC_StoreReadOnly 0x200EU +#define PTP_RC_AccessDenied 0x200FU +#define PTP_RC_NoThumbnailPresent 0x2010U +#define PTP_RC_SelfTestFailed 0x2011U +#define PTP_RC_PartialDeletion 0x2012U +#define PTP_RC_StoreNotAvailable 0x2013U +#define PTP_RC_SpecificationByFormatUnsupported 0x2014U +#define PTP_RC_NoValidObjectInfo 0x2015U +#define PTP_RC_InvalidCodeFormat 0x2016U +#define PTP_RC_UnknownVendorCode 0x2017U +#define PTP_RC_CaptureAlreadyTerminated 0x2018U +#define PTP_RC_DeviceBusy 0x2019U +#define PTP_RC_InvalidParentObject 0x201AU +#define PTP_RC_InvalidDevicePropFormat 0x201BU +#define PTP_RC_InvalidDevicePropValue 0x201CU +#define PTP_RC_InvalidParameter 0x201DU +#define PTP_RC_SessionAlreadyOpened 0x201EU +#define PTP_RC_TransactionCanceled 0x201FU +#define PTP_RC_SpecificationOfDestinationUnsupported 0x2020U +/* PTP v1.1 response codes */ +#define PTP_RC_InvalidEnumHandle 0x2021U +#define PTP_RC_NoStreamEnabled 0x2022U +#define PTP_RC_InvalidDataSet 0x2023U + +/* USB container types */ + +#define PTP_USB_CONTAINER_UNDEFINED 0x0000U +#define PTP_USB_CONTAINER_COMMAND 0x0001U +#define PTP_USB_CONTAINER_DATA 0x0002U +#define PTP_USB_CONTAINER_RESPONSE 0x0003U +#define PTP_USB_CONTAINER_EVENT 0x0004U + +/* PTP/IP definitions */ +#define PTPIP_INIT_COMMAND_REQUEST 1U +#define PTPIP_INIT_COMMAND_ACK 2U +#define PTPIP_INIT_EVENT_REQUEST 3U +#define PTPIP_INIT_EVENT_ACK 4U +#define PTPIP_INIT_FAIL 5U +#define PTPIP_CMD_REQUEST 6U +#define PTPIP_CMD_RESPONSE 7U +#define PTPIP_EVENT 8U +#define PTPIP_START_DATA_PACKET 9U +#define PTPIP_DATA_PACKET 10U +#define PTPIP_CANCEL_TRANSACTION 11U +#define PTPIP_END_DATA_PACKET 12U +#define PTPIP_PING 13U +#define PTPIP_PONG 14U + +/* Transaction data phase description */ +#define PTP_DP_NODATA 0x0000U /* no data phase */ +#define PTP_DP_SENDDATA 0x0001U /* sending data */ +#define PTP_DP_GETDATA 0x0002U /* receiving data */ +#define PTP_DP_DATA_MASK 0x00ffU /* data phase mask */ + +/** @defgroup USBH_MTP_PTP_Exported_Types + * @{ + */ + +typedef enum +{ + PTP_REQ_IDLE = 0, + PTP_REQ_SEND, + PTP_REQ_WAIT, + PTP_REQ_ERROR, +} +PTP_RequestStateTypeDef; + +typedef enum +{ + PTP_IDLE = 0, + PTP_OP_REQUEST_STATE, + PTP_OP_REQUEST_WAIT_STATE, + PTP_DATA_OUT_PHASE_STATE, + PTP_DATA_OUT_PHASE_WAIT_STATE, + PTP_DATA_IN_PHASE_STATE, + PTP_DATA_IN_PHASE_WAIT_STATE, + PTP_RESPONSE_STATE, + PTP_RESPONSE_WAIT_STATE, + PTP_ERROR, +} +PTP_ProcessStateTypeDef; + +/* PTP request/response/event general PTP container (transport independent) */ +typedef struct +{ + uint16_t Code; + uint32_t SessionID; + uint32_t Transaction_ID; + /* params may be of any type of size less or equal to uint32_t */ + uint32_t Param1; + uint32_t Param2; + uint32_t Param3; + /* events can only have three parameters */ + uint32_t Param4; + uint32_t Param5; + /* the number of meaningful parameters */ + uint8_t Nparam; +} +PTP_ContainerTypedef; + +#define PTP_USB_BULK_HS_MAX_PACKET_LEN_WRITE 1024U +#define PTP_USB_BULK_HS_MAX_PACKET_LEN_READ 1024U +#define PTP_USB_BULK_HDR_LEN (2U * sizeof(uint32_t) + 2U * sizeof(uint16_t)) +#define PTP_USB_BULK_PAYLOAD_LEN_WRITE (PTP_USB_BULK_HS_MAX_PACKET_LEN_WRITE-PTP_USB_BULK_HDR_LEN) +#define PTP_USB_BULK_PAYLOAD_LEN_READ (PTP_USB_BULK_HS_MAX_PACKET_LEN_READ - PTP_USB_BULK_HDR_LEN) +#define PTP_USB_BULK_REQ_LEN (PTP_USB_BULK_HDR_LEN + 5U * sizeof(uint32_t)) +#define PTP_USB_BULK_REQ_RESP_MAX_LEN 63U + +typedef struct +{ + uint32_t length; + uint16_t type; + uint16_t code; + uint32_t trans_id; + uint32_t param1; + uint32_t param2; + uint32_t param3; + uint32_t param4; + uint32_t param5; +} +PTP_RespContainerTypedef; + + +typedef struct +{ + uint32_t length; + uint16_t type; + uint16_t code; + uint32_t trans_id; + uint32_t param1; + uint32_t param2; + uint32_t param3; + uint32_t param4; + uint32_t param5; +} +PTP_OpContainerTypedef; + +typedef struct +{ + uint32_t length; + uint16_t type; + uint16_t code; + uint32_t trans_id; + union { + struct { + uint32_t param1; + uint32_t param2; + uint32_t param3; + uint32_t param4; + uint32_t param5; + } params; + uint8_t data[PTP_USB_BULK_PAYLOAD_LEN_READ]; + }payload; +} +PTP_DataContainerTypedef; + +/* PTP USB Asynchronous Event Interrupt Data Format */ +typedef struct +{ + uint32_t length; + uint16_t type; + uint16_t code; + uint32_t trans_id; + uint32_t param1; + uint32_t param2; + uint32_t param3; +} +PTP_EventContainerTypedef; + +/* Structure for PTP Transport process */ +typedef struct +{ + PTP_ProcessStateTypeDef state; + PTP_RequestStateTypeDef req_state; + PTP_OpContainerTypedef op_container; + PTP_DataContainerTypedef data_container; + PTP_RespContainerTypedef resp_container; + + /* ptp transaction ID */ + uint32_t transaction_id; + + /* ptp session ID */ + uint32_t session_id; + + /* device flags */ + uint32_t flags; + + /****** PTP transfer control *******/ + + /* Data pointer */ + uint8_t *data_ptr; + + /* Data length */ + uint32_t data_length; + + /* Data length */ + uint32_t data_packet; + + /* Data length */ + uint32_t iteration; + + /* Packet Index */ + uint32_t data_packet_counter; + + /****** Object transfer control *******/ + + /* object pointer */ + uint8_t *object_ptr; + +} +PTP_HandleTypeDef; + +/* DeviceInfo data offset */ +#define PTP_di_StandardVersion 0U +#define PTP_di_VendorExtensionID 2U +#define PTP_di_VendorExtensionVersion 6U +#define PTP_di_VendorExtensionDesc 8U +#define PTP_di_FunctionalMode 8U +#define PTP_di_OperationsSupported 10U + +/* Max info items size */ +#define PTP_SUPPORTED_OPERATIONS_NBR 100U +#define PTP_SUPPORTED_EVENTS_NBR 100U +#define PTP_SUPPORTED_PROPRIETIES_NBR 100U +#define PTP_CAPTURE_FORMATS_NBR 100U +#define PTP_IMAGE_FORMATS_NBR 100U +#define PTP_MAX_STR_SIZE 255U +/* PTP device info structure */ +typedef struct +{ + uint16_t StandardVersion; + uint32_t VendorExtensionID; + uint16_t VendorExtensionVersion; + uint8_t VendorExtensionDesc[PTP_MAX_STR_SIZE]; + uint16_t FunctionalMode; + uint32_t OperationsSupported_len; + uint16_t OperationsSupported[PTP_SUPPORTED_OPERATIONS_NBR]; + uint32_t EventsSupported_len; + uint16_t EventsSupported[PTP_SUPPORTED_EVENTS_NBR]; + uint32_t DevicePropertiesSupported_len; + uint16_t DevicePropertiesSupported[PTP_SUPPORTED_PROPRIETIES_NBR]; + uint32_t CaptureFormats_len; + uint16_t CaptureFormats[PTP_CAPTURE_FORMATS_NBR]; + uint32_t ImageFormats_len; + uint16_t ImageFormats[PTP_IMAGE_FORMATS_NBR]; + uint8_t Manufacturer[PTP_MAX_STR_SIZE]; + uint8_t Model[PTP_MAX_STR_SIZE]; + uint8_t DeviceVersion[PTP_MAX_STR_SIZE]; + uint8_t SerialNumber[PTP_MAX_STR_SIZE]; +} +PTP_DeviceInfoTypedef; + +#define PTP_MAX_STORAGE_UNITS_NBR 5 +/* PTP storageIDs structute (returned by GetStorageIDs) */ +typedef struct +{ + uint32_t n; + uint32_t Storage [PTP_MAX_STORAGE_UNITS_NBR]; +} +PTP_StorageIDsTypedef; + +/* PTP StorageInfo structure (returned by GetStorageInfo) */ + +#define PTP_si_StorageType 0U +#define PTP_si_FilesystemType 2U +#define PTP_si_AccessCapability 4U +#define PTP_si_MaxCapability 6U +#define PTP_si_FreeSpaceInBytes 14U +#define PTP_si_FreeSpaceInImages 22U +#define PTP_si_StorageDescription 26U + + +/* PTP Storage Types */ + +#define PTP_ST_Undefined 0x0000U +#define PTP_ST_FixedROM 0x0001U +#define PTP_ST_RemovableROM 0x0002U +#define PTP_ST_FixedRAM 0x0003U +#define PTP_ST_RemovableRAM 0x0004U + +/* PTP FilesystemType Values */ + +#define PTP_FST_Undefined 0x0000U +#define PTP_FST_GenericFlat 0x0001U +#define PTP_FST_GenericHierarchical 0x0002U +#define PTP_FST_DCF 0x0003U + +/* PTP StorageInfo AccessCapability Values */ + +#define PTP_AC_ReadWrite 0x0000U +#define PTP_AC_ReadOnly 0x0001U +#define PTP_AC_ReadOnly_with_Object_Deletion 0x0002U + +typedef struct +{ + uint16_t StorageType; + uint16_t FilesystemType; + uint16_t AccessCapability; + uint64_t MaxCapability; + uint64_t FreeSpaceInBytes; + uint32_t FreeSpaceInImages; + uint8_t StorageDescription[PTP_MAX_STR_SIZE]; + uint8_t VolumeLabel[PTP_MAX_STR_SIZE]; +} +PTP_StorageInfoTypedef; + +/* PTP Object Format Codes */ + +/* ancillary formats */ +#define PTP_OFC_Undefined 0x3000U +#define PTP_OFC_Defined 0x3800U +#define PTP_OFC_Association 0x3001U +#define PTP_OFC_Script 0x3002U +#define PTP_OFC_Executable 0x3003U +#define PTP_OFC_Text 0x3004U +#define PTP_OFC_HTML 0x3005U +#define PTP_OFC_DPOF 0x3006U +#define PTP_OFC_AIFF 0x3007U +#define PTP_OFC_WAV 0x3008U +#define PTP_OFC_MP3 0x3009U +#define PTP_OFC_AVI 0x300AU +#define PTP_OFC_MPEG 0x300BU +#define PTP_OFC_ASF 0x300CU +#define PTP_OFC_QT 0x300DU /* guessing */ +/* image formats */ +#define PTP_OFC_EXIF_JPEG 0x3801U +#define PTP_OFC_TIFF_EP 0x3802U +#define PTP_OFC_FlashPix 0x3803U +#define PTP_OFC_BMP 0x3804U +#define PTP_OFC_CIFF 0x3805U +#define PTP_OFC_Undefined_0x3806 0x3806U +#define PTP_OFC_GIF 0x3807U +#define PTP_OFC_JFIF 0x3808U +#define PTP_OFC_PCD 0x3809U +#define PTP_OFC_PICT 0x380AU +#define PTP_OFC_PNG 0x380BU +#define PTP_OFC_Undefined_0x380C 0x380CU +#define PTP_OFC_TIFF 0x380DU +#define PTP_OFC_TIFF_IT 0x380EU +#define PTP_OFC_JP2 0x380FU +#define PTP_OFC_JPX 0x3810U +/* ptp v1.1 has only DNG new */ +#define PTP_OFC_DNG 0x3811U + +/* MTP extensions */ +#define PTP_OFC_MTP_MediaCard 0xb211U +#define PTP_OFC_MTP_MediaCardGroup 0xb212U +#define PTP_OFC_MTP_Encounter 0xb213U +#define PTP_OFC_MTP_EncounterBox 0xb214U +#define PTP_OFC_MTP_M4A 0xb215U +#define PTP_OFC_MTP_ZUNEUNDEFINED 0xb217U/* Unknown file type */ +#define PTP_OFC_MTP_Firmware 0xb802U +#define PTP_OFC_MTP_WindowsImageFormat 0xb881U +#define PTP_OFC_MTP_UndefinedAudio 0xb900U +#define PTP_OFC_MTP_WMA 0xb901U +#define PTP_OFC_MTP_OGG 0xb902U +#define PTP_OFC_MTP_AAC 0xb903U +#define PTP_OFC_MTP_AudibleCod 0xb904U +#define PTP_OFC_MTP_FLAC 0xb906U +#define PTP_OFC_MTP_SamsungPlaylist 0xb909U +#define PTP_OFC_MTP_UndefinedVideo 0xb980U +#define PTP_OFC_MTP_WMV 0xb981U +#define PTP_OFC_MTP_MP4 0xb982U +#define PTP_OFC_MTP_MP2 0xb983U +#define PTP_OFC_MTP_3GP 0xb984U +#define PTP_OFC_MTP_UndefinedCollection 0xba00U +#define PTP_OFC_MTP_AbstractMultimediaAlbum 0xba01U +#define PTP_OFC_MTP_AbstractImageAlbum 0xba02U +#define PTP_OFC_MTP_AbstractAudioAlbum 0xba03U +#define PTP_OFC_MTP_AbstractVideoAlbum 0xba04U +#define PTP_OFC_MTP_AbstractAudioVideoPlaylist 0xba05U +#define PTP_OFC_MTP_AbstractContactGroup 0xba06U +#define PTP_OFC_MTP_AbstractMessageFolder 0xba07U +#define PTP_OFC_MTP_AbstractChapteredProduction 0xba08U +#define PTP_OFC_MTP_AbstractAudioPlaylist 0xba09U +#define PTP_OFC_MTP_AbstractVideoPlaylist 0xba0aU +#define PTP_OFC_MTP_AbstractMediacast 0xba0bU +#define PTP_OFC_MTP_WPLPlaylist 0xba10U +#define PTP_OFC_MTP_M3UPlaylist 0xba11U +#define PTP_OFC_MTP_MPLPlaylist 0xba12U +#define PTP_OFC_MTP_ASXPlaylist 0xba13U +#define PTP_OFC_MTP_PLSPlaylist 0xba14U +#define PTP_OFC_MTP_UndefinedDocument 0xba80U +#define PTP_OFC_MTP_AbstractDocument 0xba81U +#define PTP_OFC_MTP_XMLDocument 0xba82U +#define PTP_OFC_MTP_MSWordDocument 0xba83U +#define PTP_OFC_MTP_MHTCompiledHTMLDocument 0xba84U +#define PTP_OFC_MTP_MSExcelSpreadsheetXLS 0xba85U +#define PTP_OFC_MTP_MSPowerpointPresentationPPT 0xba86U +#define PTP_OFC_MTP_UndefinedMessage 0xbb00U +#define PTP_OFC_MTP_AbstractMessage 0xbb01U +#define PTP_OFC_MTP_UndefinedContact 0xbb80U +#define PTP_OFC_MTP_AbstractContact 0xbb81U +#define PTP_OFC_MTP_vCard2 0xbb82U +#define PTP_OFC_MTP_vCard3 0xbb83U +#define PTP_OFC_MTP_UndefinedCalendarItem 0xbe00U +#define PTP_OFC_MTP_AbstractCalendarItem 0xbe01U +#define PTP_OFC_MTP_vCalendar1 0xbe02U +#define PTP_OFC_MTP_vCalendar2 0xbe03U +#define PTP_OFC_MTP_UndefinedWindowsExecutable 0xbe80U +#define PTP_OFC_MTP_MediaCast 0xbe81U +#define PTP_OFC_MTP_Section 0xbe82U + +/* MTP specific Object Properties */ +#define PTP_OPC_StorageID 0xDC01U +#define PTP_OPC_ObjectFormat 0xDC02U +#define PTP_OPC_ProtectionStatus 0xDC03U +#define PTP_OPC_ObjectSize 0xDC04U +#define PTP_OPC_AssociationType 0xDC05U +#define PTP_OPC_AssociationDesc 0xDC06U +#define PTP_OPC_ObjectFileName 0xDC07U +#define PTP_OPC_DateCreated 0xDC08U +#define PTP_OPC_DateModified 0xDC09U +#define PTP_OPC_Keywords 0xDC0AU +#define PTP_OPC_ParentObject 0xDC0BU +#define PTP_OPC_AllowedFolderContents 0xDC0CU +#define PTP_OPC_Hidden 0xDC0DU +#define PTP_OPC_SystemObject 0xDC0EU +#define PTP_OPC_PersistantUniqueObjectIdentifier 0xDC41U +#define PTP_OPC_SyncID 0xDC42U +#define PTP_OPC_PropertyBag 0xDC43U +#define PTP_OPC_Name 0xDC44U +#define PTP_OPC_CreatedBy 0xDC45U +#define PTP_OPC_Artist 0xDC46U +#define PTP_OPC_DateAuthored 0xDC47U +#define PTP_OPC_Description 0xDC48U +#define PTP_OPC_URLReference 0xDC49U +#define PTP_OPC_LanguageLocale 0xDC4AU +#define PTP_OPC_CopyrightInformation 0xDC4BU +#define PTP_OPC_Source 0xDC4CU +#define PTP_OPC_OriginLocation 0xDC4DU +#define PTP_OPC_DateAdded 0xDC4EU +#define PTP_OPC_NonConsumable 0xDC4FU +#define PTP_OPC_CorruptOrUnplayable 0xDC50U +#define PTP_OPC_ProducerSerialNumber 0xDC51U +#define PTP_OPC_RepresentativeSampleFormat 0xDC81U +#define PTP_OPC_RepresentativeSampleSize 0xDC82U +#define PTP_OPC_RepresentativeSampleHeight 0xDC83U +#define PTP_OPC_RepresentativeSampleWidth 0xDC84U +#define PTP_OPC_RepresentativeSampleDuration 0xDC85U +#define PTP_OPC_RepresentativeSampleData 0xDC86U +#define PTP_OPC_Width 0xDC87U +#define PTP_OPC_Height 0xDC88U +#define PTP_OPC_Duration 0xDC89U +#define PTP_OPC_Rating 0xDC8AU +#define PTP_OPC_Track 0xDC8BU +#define PTP_OPC_Genre 0xDC8CU +#define PTP_OPC_Credits 0xDC8DU +#define PTP_OPC_Lyrics 0xDC8EU +#define PTP_OPC_SubscriptionContentID 0xDC8FU +#define PTP_OPC_ProducedBy 0xDC90U +#define PTP_OPC_UseCount 0xDC91U +#define PTP_OPC_SkipCount 0xDC92U +#define PTP_OPC_LastAccessed 0xDC93U +#define PTP_OPC_ParentalRating 0xDC94U +#define PTP_OPC_MetaGenre 0xDC95U +#define PTP_OPC_Composer 0xDC96U +#define PTP_OPC_EffectiveRating 0xDC97U +#define PTP_OPC_Subtitle 0xDC98U +#define PTP_OPC_OriginalReleaseDate 0xDC99U +#define PTP_OPC_AlbumName 0xDC9AU +#define PTP_OPC_AlbumArtist 0xDC9BU +#define PTP_OPC_Mood 0xDC9CU +#define PTP_OPC_DRMStatus 0xDC9DU +#define PTP_OPC_SubDescription 0xDC9EU +#define PTP_OPC_IsCropped 0xDCD1U +#define PTP_OPC_IsColorCorrected 0xDCD2U +#define PTP_OPC_ImageBitDepth 0xDCD3U +#define PTP_OPC_Fnumber 0xDCD4U +#define PTP_OPC_ExposureTime 0xDCD5U +#define PTP_OPC_ExposureIndex 0xDCD6U +#define PTP_OPC_DisplayName 0xDCE0U +#define PTP_OPC_BodyText 0xDCE1U +#define PTP_OPC_Subject 0xDCE2U +#define PTP_OPC_Priority 0xDCE3U +#define PTP_OPC_GivenName 0xDD00U +#define PTP_OPC_MiddleNames 0xDD01U +#define PTP_OPC_FamilyName 0xDD02U +#define PTP_OPC_Prefix 0xDD03U +#define PTP_OPC_Suffix 0xDD04U +#define PTP_OPC_PhoneticGivenName 0xDD05U +#define PTP_OPC_PhoneticFamilyName 0xDD06U +#define PTP_OPC_EmailPrimary 0xDD07U +#define PTP_OPC_EmailPersonal1 0xDD08U +#define PTP_OPC_EmailPersonal2 0xDD09U +#define PTP_OPC_EmailBusiness1 0xDD0AU +#define PTP_OPC_EmailBusiness2 0xDD0BU +#define PTP_OPC_EmailOthers 0xDD0CU +#define PTP_OPC_PhoneNumberPrimary 0xDD0DU +#define PTP_OPC_PhoneNumberPersonal 0xDD0EU +#define PTP_OPC_PhoneNumberPersonal2 0xDD0FU +#define PTP_OPC_PhoneNumberBusiness 0xDD10U +#define PTP_OPC_PhoneNumberBusiness2 0xDD11U +#define PTP_OPC_PhoneNumberMobile 0xDD12U +#define PTP_OPC_PhoneNumberMobile2 0xDD13U +#define PTP_OPC_FaxNumberPrimary 0xDD14U +#define PTP_OPC_FaxNumberPersonal 0xDD15U +#define PTP_OPC_FaxNumberBusiness 0xDD16U +#define PTP_OPC_PagerNumber 0xDD17U +#define PTP_OPC_PhoneNumberOthers 0xDD18U +#define PTP_OPC_PrimaryWebAddress 0xDD19U +#define PTP_OPC_PersonalWebAddress 0xDD1AU +#define PTP_OPC_BusinessWebAddress 0xDD1BU +#define PTP_OPC_InstantMessengerAddress 0xDD1CU +#define PTP_OPC_InstantMessengerAddress2 0xDD1DU +#define PTP_OPC_InstantMessengerAddress3 0xDD1EU +#define PTP_OPC_PostalAddressPersonalFull 0xDD1FU +#define PTP_OPC_PostalAddressPersonalFullLine1 0xDD20U +#define PTP_OPC_PostalAddressPersonalFullLine2 0xDD21U +#define PTP_OPC_PostalAddressPersonalFullCity 0xDD22U +#define PTP_OPC_PostalAddressPersonalFullRegion 0xDD23U +#define PTP_OPC_PostalAddressPersonalFullPostalCode 0xDD24U +#define PTP_OPC_PostalAddressPersonalFullCountry 0xDD25U +#define PTP_OPC_PostalAddressBusinessFull 0xDD26U +#define PTP_OPC_PostalAddressBusinessLine1 0xDD27U +#define PTP_OPC_PostalAddressBusinessLine2 0xDD28U +#define PTP_OPC_PostalAddressBusinessCity 0xDD29U +#define PTP_OPC_PostalAddressBusinessRegion 0xDD2AU +#define PTP_OPC_PostalAddressBusinessPostalCode 0xDD2BU +#define PTP_OPC_PostalAddressBusinessCountry 0xDD2CU +#define PTP_OPC_PostalAddressOtherFull 0xDD2DU +#define PTP_OPC_PostalAddressOtherLine1 0xDD2EU +#define PTP_OPC_PostalAddressOtherLine2 0xDD2FU +#define PTP_OPC_PostalAddressOtherCity 0xDD30U +#define PTP_OPC_PostalAddressOtherRegion 0xDD31U +#define PTP_OPC_PostalAddressOtherPostalCode 0xDD32U +#define PTP_OPC_PostalAddressOtherCountry 0xDD33U +#define PTP_OPC_OrganizationName 0xDD34U +#define PTP_OPC_PhoneticOrganizationName 0xDD35U +#define PTP_OPC_Role 0xDD36U +#define PTP_OPC_Birthdate 0xDD37U +#define PTP_OPC_MessageTo 0xDD40U +#define PTP_OPC_MessageCC 0xDD41U +#define PTP_OPC_MessageBCC 0xDD42U +#define PTP_OPC_MessageRead 0xDD43U +#define PTP_OPC_MessageReceivedTime 0xDD44U +#define PTP_OPC_MessageSender 0xDD45U +#define PTP_OPC_ActivityBeginTime 0xDD50U +#define PTP_OPC_ActivityEndTime 0xDD51U +#define PTP_OPC_ActivityLocation 0xDD52U +#define PTP_OPC_ActivityRequiredAttendees 0xDD54U +#define PTP_OPC_ActivityOptionalAttendees 0xDD55U +#define PTP_OPC_ActivityResources 0xDD56U +#define PTP_OPC_ActivityAccepted 0xDD57U +#define PTP_OPC_Owner 0xDD5DU +#define PTP_OPC_Editor 0xDD5EU +#define PTP_OPC_Webmaster 0xDD5FU +#define PTP_OPC_URLSource 0xDD60U +#define PTP_OPC_URLDestination 0xDD61U +#define PTP_OPC_TimeBookmark 0xDD62U +#define PTP_OPC_ObjectBookmark 0xDD63U +#define PTP_OPC_ByteBookmark 0xDD64U +#define PTP_OPC_LastBuildDate 0xDD70U +#define PTP_OPC_TimetoLive 0xDD71U +#define PTP_OPC_MediaGUID 0xDD72U +#define PTP_OPC_TotalBitRate 0xDE91U +#define PTP_OPC_BitRateType 0xDE92U +#define PTP_OPC_SampleRate 0xDE93U +#define PTP_OPC_NumberOfChannels 0xDE94U +#define PTP_OPC_AudioBitDepth 0xDE95U +#define PTP_OPC_ScanDepth 0xDE97U +#define PTP_OPC_AudioWAVECodec 0xDE99U +#define PTP_OPC_AudioBitRate 0xDE9AU +#define PTP_OPC_VideoFourCCCodec 0xDE9BU +#define PTP_OPC_VideoBitRate 0xDE9CU +#define PTP_OPC_FramesPerThousandSeconds 0xDE9DU +#define PTP_OPC_KeyFrameDistance 0xDE9EU +#define PTP_OPC_BufferSize 0xDE9FU +#define PTP_OPC_EncodingQuality 0xDEA0U +#define PTP_OPC_EncodingProfile 0xDEA1U +#define PTP_OPC_BuyFlag 0xD901U + +/* WiFi Provisioning MTP Extension property codes */ +#define PTP_OPC_WirelessConfigurationFile 0xB104U + + +/* PTP Association Types */ +#define PTP_AT_Undefined 0x0000U +#define PTP_AT_GenericFolder 0x0001U +#define PTP_AT_Album 0x0002U +#define PTP_AT_TimeSequence 0x0003U +#define PTP_AT_HorizontalPanoramic 0x0004U +#define PTP_AT_VerticalPanoramic 0x0005U +#define PTP_AT_2DPanoramic 0x0006U +#define PTP_AT_AncillaryData 0x0007U + +#define PTP_MAX_HANDLER_NBR 0x255U +typedef struct +{ + uint32_t n; + uint32_t Handler[PTP_MAX_HANDLER_NBR]; +} +PTP_ObjectHandlesTypedef; + + +#define PTP_oi_StorageID 0U +#define PTP_oi_ObjectFormat 4U +#define PTP_oi_ProtectionStatus 6U +#define PTP_oi_ObjectCompressedSize 8U +#define PTP_oi_ThumbFormat 12U +#define PTP_oi_ThumbCompressedSize 14U +#define PTP_oi_ThumbPixWidth 18U +#define PTP_oi_ThumbPixHeight 22U +#define PTP_oi_ImagePixWidth 26U +#define PTP_oi_ImagePixHeight 30U +#define PTP_oi_ImageBitDepth 34U +#define PTP_oi_ParentObject 38U +#define PTP_oi_AssociationType 42U +#define PTP_oi_AssociationDesc 44U +#define PTP_oi_SequenceNumber 48U +#define PTP_oi_filenamelen 52U +#define PTP_oi_Filename 53U + +typedef struct +{ + uint32_t StorageID; + uint16_t ObjectFormat; + uint16_t ProtectionStatus; + /* In the regular objectinfo this is 32bit, but we keep the general object size here + that also arrives via other methods and so use 64bit */ + uint64_t ObjectCompressedSize; + uint16_t ThumbFormat; + uint32_t ThumbCompressedSize; + uint32_t ThumbPixWidth; + uint32_t ThumbPixHeight; + uint32_t ImagePixWidth; + uint32_t ImagePixHeight; + uint32_t ImageBitDepth; + uint32_t ParentObject; + uint16_t AssociationType; + uint32_t AssociationDesc; + uint32_t SequenceNumber; + uint8_t Filename[PTP_MAX_STR_SIZE]; + uint32_t CaptureDate; + uint32_t ModificationDate; + uint8_t Keywords[PTP_MAX_STR_SIZE]; +} +PTP_ObjectInfoTypedef; + +/* Object Property Describing Dataset (DevicePropDesc) */ + +typedef union _PTP_PropertyValueTypedef +{ + char str[PTP_MAX_STR_SIZE]; + uint8_t u8; + int8_t i8; + uint16_t u16; + int16_t i16; + uint32_t u32; + int32_t i32; + uint64_t u64; + int64_t i64; + struct array { + uint32_t count; + union _PTP_PropertyValueTypedef *v; + }a; +}PTP_PropertyValueTypedef; + +typedef struct +{ + PTP_PropertyValueTypedef MinimumValue; + PTP_PropertyValueTypedef MaximumValue; + PTP_PropertyValueTypedef StepSize; +} +PTP_PropDescRangeFormTypedef; + +/* Property Describing Dataset, Enum Form */ + +typedef struct +{ + uint16_t NumberOfValues; + PTP_PropertyValueTypedef SupportedValue[PTP_SUPPORTED_PROPRIETIES_NBR]; +} +PTP_PropDescEnumFormTypedef; + +/* (MTP) Object Property pack/unpack */ +#define PTP_opd_ObjectPropertyCode 0U +#define PTP_opd_DataType 2U +#define PTP_opd_GetSet 4U +#define PTP_opd_FactoryDefaultValue 5U + +typedef struct +{ + uint16_t ObjectPropertyCode; + uint16_t DataType; + uint8_t GetSet; + PTP_PropertyValueTypedef FactoryDefaultValue; + uint32_t GroupCode; + uint8_t FormFlag; + union { + PTP_PropDescEnumFormTypedef Enum; + PTP_PropDescRangeFormTypedef Range; + } FORM; +} +PTP_ObjectPropDescTypeDef; + +/* Metadata lists for MTP operations */ +typedef struct +{ + uint16_t property; + uint16_t datatype; + uint32_t ObjectHandle; + PTP_PropertyValueTypedef propval; +} +MTP_PropertiesTypedef; + + +/* Device Property Form Flag */ + +#define PTP_DPFF_None 0x00U +#define PTP_DPFF_Range 0x01U +#define PTP_DPFF_Enumeration 0x02U + +/* Object Property Codes used by MTP (first 3 are same as DPFF codes) */ +#define PTP_OPFF_None 0x00U +#define PTP_OPFF_Range 0x01U +#define PTP_OPFF_Enumeration 0x02U +#define PTP_OPFF_DateTime 0x03U +#define PTP_OPFF_FixedLengthArray 0x04U +#define PTP_OPFF_RegularExpression 0x05U +#define PTP_OPFF_ByteArray 0x06U +#define PTP_OPFF_LongString 0xFFU + +/* Device Property pack/unpack */ + +#define PTP_dpd_DevicePropertyCode 0U +#define PTP_dpd_DataType 2U +#define PTP_dpd_GetSet 4U +#define PTP_dpd_FactoryDefaultValue 5U + +/* Device Property Describing Dataset (DevicePropDesc) */ + +typedef struct +{ + uint16_t DevicePropertyCode; + uint16_t DataType; + uint8_t GetSet; + PTP_PropertyValueTypedef FactoryDefaultValue; + PTP_PropertyValueTypedef CurrentValue; + uint8_t FormFlag; + union { + PTP_PropDescEnumFormTypedef Enum; + PTP_PropDescRangeFormTypedef Range; + } FORM; +} +PTP_DevicePropDescTypdef; + +/* DataType Codes */ + +#define PTP_DTC_UNDEF 0x0000U +#define PTP_DTC_INT8 0x0001U +#define PTP_DTC_UINT8 0x0002U +#define PTP_DTC_INT16 0x0003U +#define PTP_DTC_UINT16 0x0004U +#define PTP_DTC_INT32 0x0005U +#define PTP_DTC_UINT32 0x0006U +#define PTP_DTC_INT64 0x0007U +#define PTP_DTC_UINT64 0x0008U +#define PTP_DTC_INT128 0x0009U +#define PTP_DTC_UINT128 0x000AU + +#define PTP_DTC_ARRAY_MASK 0x4000U + +#define PTP_DTC_AINT8 (PTP_DTC_ARRAY_MASK | PTP_DTC_INT8) +#define PTP_DTC_AUINT8 (PTP_DTC_ARRAY_MASK | PTP_DTC_UINT8) +#define PTP_DTC_AINT16 (PTP_DTC_ARRAY_MASK | PTP_DTC_INT16) +#define PTP_DTC_AUINT16 (PTP_DTC_ARRAY_MASK | PTP_DTC_UINT16) +#define PTP_DTC_AINT32 (PTP_DTC_ARRAY_MASK | PTP_DTC_INT32) +#define PTP_DTC_AUINT32 (PTP_DTC_ARRAY_MASK | PTP_DTC_UINT32) +#define PTP_DTC_AINT64 (PTP_DTC_ARRAY_MASK | PTP_DTC_INT64) +#define PTP_DTC_AUINT64 (PTP_DTC_ARRAY_MASK | PTP_DTC_UINT64) +#define PTP_DTC_AINT128 (PTP_DTC_ARRAY_MASK | PTP_DTC_INT128) +#define PTP_DTC_AUINT128 (PTP_DTC_ARRAY_MASK | PTP_DTC_UINT128) + +#define PTP_DTC_STR 0xFFFFU + +/* PTP Event Codes */ + +#define PTP_EC_Undefined 0x4000U +#define PTP_EC_CancelTransaction 0x4001U +#define PTP_EC_ObjectAdded 0x4002U +#define PTP_EC_ObjectRemoved 0x4003U +#define PTP_EC_StoreAdded 0x4004U +#define PTP_EC_StoreRemoved 0x4005U +#define PTP_EC_DevicePropChanged 0x4006U +#define PTP_EC_ObjectInfoChanged 0x4007U +#define PTP_EC_DeviceInfoChanged 0x4008U +#define PTP_EC_RequestObjectTransfer 0x4009U +#define PTP_EC_StoreFull 0x400AU +#define PTP_EC_DeviceReset 0x400BU +#define PTP_EC_StorageInfoChanged 0x400CU +#define PTP_EC_CaptureComplete 0x400DU +#define PTP_EC_UnreportedStatus 0x400EU + + +/** + * @} + */ + +/** @defgroup USBH_MTP_PTP_Exported_Macros + * @{ + */ +/** + * @} + */ + +/** @defgroup USBH_MTP_PTP_Exported_Variables + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBH_MTP_PTP_Exported_FunctionsPrototype + * @{ + */ +USBH_StatusTypeDef USBH_PTP_Init(USBH_HandleTypeDef *phost); +USBH_StatusTypeDef USBH_PTP_Process (USBH_HandleTypeDef *phost); + +USBH_StatusTypeDef USBH_PTP_SendRequest (USBH_HandleTypeDef *phost, PTP_ContainerTypedef *req); +USBH_StatusTypeDef USBH_PTP_GetResponse (USBH_HandleTypeDef *phost, PTP_ContainerTypedef *resp); + +USBH_StatusTypeDef USBH_PTP_OpenSession (USBH_HandleTypeDef *phost, uint32_t session); +USBH_StatusTypeDef USBH_PTP_GetDeviceInfo (USBH_HandleTypeDef *phost, PTP_DeviceInfoTypedef *dev_info); +USBH_StatusTypeDef USBH_PTP_GetStorageIds (USBH_HandleTypeDef *phost, PTP_StorageIDsTypedef *storage_ids); + +USBH_StatusTypeDef USBH_PTP_GetStorageInfo (USBH_HandleTypeDef *phost, + uint32_t storage_id, + PTP_StorageInfoTypedef *storage_info); + +USBH_StatusTypeDef USBH_PTP_GetNumObjects (USBH_HandleTypeDef *phost, + uint32_t storage_id, + uint32_t objectformatcode, + uint32_t associationOH, + uint32_t* numobs); + +USBH_StatusTypeDef USBH_PTP_GetObjectHandles (USBH_HandleTypeDef *phost, + uint32_t storage_id, + uint32_t objectformatcode, + uint32_t associationOH, + PTP_ObjectHandlesTypedef* objecthandles); + +USBH_StatusTypeDef USBH_PTP_GetObjectInfo (USBH_HandleTypeDef *phost, + uint32_t handle, + PTP_ObjectInfoTypedef *object_info); + +USBH_StatusTypeDef USBH_PTP_DeleteObject (USBH_HandleTypeDef *phost, + uint32_t handle, + uint32_t objectformatcode); + +USBH_StatusTypeDef USBH_PTP_GetObject (USBH_HandleTypeDef *phost, + uint32_t handle, + uint8_t *object); + +USBH_StatusTypeDef USBH_PTP_GetPartialObject(USBH_HandleTypeDef *phost, + uint32_t handle, + uint32_t offset, + uint32_t maxbytes, uint8_t *object, + uint32_t *len); + +USBH_StatusTypeDef USBH_PTP_GetObjectPropsSupported (USBH_HandleTypeDef *phost, + uint16_t ofc, + uint32_t *propnum, + uint16_t *props); + +USBH_StatusTypeDef USBH_PTP_GetObjectPropDesc (USBH_HandleTypeDef *phost, + uint16_t opc, + uint16_t ofc, + PTP_ObjectPropDescTypeDef *opd); + +USBH_StatusTypeDef USBH_PTP_GetObjectPropList (USBH_HandleTypeDef *phost, + uint32_t handle, + MTP_PropertiesTypedef *pprops, + uint32_t *nrofprops); + +USBH_StatusTypeDef USBH_PTP_SendObject (USBH_HandleTypeDef *phost, + uint32_t handle, + uint8_t *object, + uint32_t size); + +USBH_StatusTypeDef USBH_PTP_GetDevicePropDesc (USBH_HandleTypeDef *phost, + uint16_t propcode, + PTP_DevicePropDescTypdef* devicepropertydesc); + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USBH_MTP_PTP_H */ + + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Class/MTP/Src/usbh_mtp.c b/system/Middlewares/ST/STM32_USB_Host_Library/Class/MTP/Src/usbh_mtp.c index ae73a53780..48dffaf926 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Class/MTP/Src/usbh_mtp.c +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Class/MTP/Src/usbh_mtp.c @@ -1,1120 +1,1120 @@ -/** - ****************************************************************************** - * @file usbh_mtp.c - * @author MCD Application Team - * @brief This file is the MTP Layer Handlers for USB Host MTP class. - * - * @verbatim - * - * =================================================================== - * MTP Class Description - * =================================================================== - * This module manages the MTP class following the - * "Media Transfer Protocol (MTP) specification Version 1.11 April 6th, 2011". - * the implmentation is compatible with the PTP model as an extension - * of the existing Picture Transfer Protocol defined by the ISO 15740 specification - * - * @endverbatim - * - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - - /* BSPDependencies - - "stm32xxxxx_{eval}{discovery}.c" - - "stm32xxxxx_{eval}{discovery}_io.c" - - "stm32xxxxx_{eval}{discovery}_audio.c" - - "stm32xxxxx_{eval}{discovery}_sd.c" - - "stm32xxxxx_{eval}{discovery}_lcd.c" - - "stm32xxxxx_{eval}{discovery}_sdram.c" - EndBSPDependencies */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_mtp.h" - -/** @addtogroup USBH_LIB -* @{ -*/ - -/** @addtogroup USBH_CLASS -* @{ -*/ - -/** @addtogroup USBH_MTP_CLASS -* @{ -*/ - -/** @defgroup USBH_MTP_CORE -* @brief This file includes MTP Layer Handlers for USB Host MTP class. -* @{ -*/ - -/** @defgroup USBH_MTP_CORE_Private_TypesDefinitions -* @{ -*/ -/** -* @} -*/ - - -/** @defgroup USBH_MTP_CORE_Private_Defines -* @{ -*/ -/** -* @} -*/ - - -/** @defgroup USBH_MTP_CORE_Private_Macros -* @{ -*/ -/** -* @} -*/ - - -/** @defgroup USBH_MTP_CORE_Private_Variables -* @{ -*/ -/** -* @} -*/ - - -/** @defgroup USBH_MTP_CORE_Private_FunctionPrototypes -* @{ -*/ - -static USBH_StatusTypeDef USBH_MTP_InterfaceInit (USBH_HandleTypeDef *phost); - -static USBH_StatusTypeDef USBH_MTP_InterfaceDeInit (USBH_HandleTypeDef *phost); - -static USBH_StatusTypeDef USBH_MTP_Process(USBH_HandleTypeDef *phost); - -static USBH_StatusTypeDef USBH_MTP_ClassRequest (USBH_HandleTypeDef *phost); - -static uint8_t MTP_FindCtlEndpoint(USBH_HandleTypeDef *phost); - -static uint8_t MTP_FindDataOutEndpoint(USBH_HandleTypeDef *phost); - -static uint8_t MTP_FindDataInEndpoint(USBH_HandleTypeDef *phost); - -static USBH_StatusTypeDef USBH_MTP_SOFProcess (USBH_HandleTypeDef *phost); - -static USBH_StatusTypeDef USBH_MTP_Events (USBH_HandleTypeDef *phost); - -static void MTP_DecodeEvent (USBH_HandleTypeDef *phost) ; - -USBH_ClassTypeDef MTP_Class = -{ - "MTP", - USB_MTP_CLASS, - USBH_MTP_InterfaceInit, - USBH_MTP_InterfaceDeInit, - USBH_MTP_ClassRequest, - USBH_MTP_Process, - USBH_MTP_SOFProcess, - NULL, -}; -/** -* @} -*/ - - -/** @defgroup USBH_MTP_CORE_Private_Functions -* @{ -*/ - -/** - * @brief USBH_MTP_InterfaceInit - * The function init the MTP class. - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_MTP_InterfaceInit (USBH_HandleTypeDef *phost) -{ - USBH_StatusTypeDef status = USBH_OK; - uint8_t interface, endpoint; - - MTP_HandleTypeDef *MTP_Handle; - - interface = USBH_FindInterface(phost, USB_MTP_CLASS, 1U, 1U); - - if(interface == 0xFFU) /* No Valid Interface */ - { - status = USBH_FAIL; - USBH_DbgLog ("Cannot Find the interface for Still Image Class."); - } - else - { - USBH_SelectInterface (phost, interface); - - endpoint = MTP_FindCtlEndpoint(phost); - - phost->pActiveClass->pData = (MTP_HandleTypeDef *)USBH_malloc (sizeof(MTP_HandleTypeDef)); - MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - - if( MTP_Handle == NULL) - { - status = USBH_FAIL; - USBH_DbgLog ("Cannot allocate RAM for MTP Handle"); - } - - /*Collect the control endpoint address and length*/ - MTP_Handle->NotificationEp = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].bEndpointAddress; - MTP_Handle->NotificationEpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].wMaxPacketSize; - MTP_Handle->NotificationPipe = USBH_AllocPipe(phost, MTP_Handle->NotificationEp); - MTP_Handle->events.poll = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].bInterval; - - /* Open pipe for Notification endpoint */ - USBH_OpenPipe (phost, - MTP_Handle->NotificationPipe, - MTP_Handle->NotificationEp, - phost->device.address, - phost->device.speed, - USB_EP_TYPE_INTR, - MTP_Handle->NotificationEpSize); - - USBH_LL_SetToggle (phost, MTP_Handle->NotificationPipe, 0U); - - - endpoint = MTP_FindDataInEndpoint(phost); - - /*Collect the control endpoint address and length*/ - MTP_Handle->DataInEp = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].bEndpointAddress; - MTP_Handle->DataInEpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].wMaxPacketSize; - MTP_Handle->DataInPipe = USBH_AllocPipe(phost, MTP_Handle->DataInEp); - - /* Open pipe for DATA IN endpoint */ - USBH_OpenPipe (phost, - MTP_Handle->DataInPipe, - MTP_Handle->DataInEp, - phost->device.address, - phost->device.speed, - USB_EP_TYPE_BULK, - MTP_Handle->DataInEpSize); - - USBH_LL_SetToggle (phost, MTP_Handle->DataInPipe, 0U); - - endpoint = MTP_FindDataOutEndpoint(phost); - - /*Collect the DATA OUT endpoint address and length*/ - MTP_Handle->DataOutEp = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].bEndpointAddress; - MTP_Handle->DataOutEpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].wMaxPacketSize; - MTP_Handle->DataOutPipe = USBH_AllocPipe(phost, MTP_Handle->DataOutEp); - - /* Open pipe for DATA OUT endpoint */ - USBH_OpenPipe (phost, - MTP_Handle->DataOutPipe, - MTP_Handle->DataOutEp, - phost->device.address, - phost->device.speed, - USB_EP_TYPE_BULK, - MTP_Handle->DataOutEpSize); - - USBH_LL_SetToggle (phost, MTP_Handle->DataOutPipe, 0U); - - - MTP_Handle->state = MTP_OPENSESSION; - MTP_Handle->is_ready = 0U; - MTP_Handle->events.state = MTP_EVENTS_INIT; - return USBH_PTP_Init(phost); - - } - return status; -} - -/** - * @brief Find MTP Ctl interface - * @param phost: Host handle - * @retval USBH Status - */ -static uint8_t MTP_FindCtlEndpoint(USBH_HandleTypeDef *phost) -{ - uint8_t interface, endpoint; - - for (interface = 0U; interface < USBH_MAX_NUM_INTERFACES ; interface ++ ) - { - if(phost->device.CfgDesc.Itf_Desc[interface].bInterfaceClass == USB_MTP_CLASS) - { - for (endpoint = 0U; endpoint < USBH_MAX_NUM_ENDPOINTS ; endpoint ++ ) - { - if((phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].bEndpointAddress & 0x80U)&& - (phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].wMaxPacketSize > 0U)&& - ((phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].bmAttributes & USBH_EP_INTERRUPT) == USBH_EP_INTERRUPT)) - { - return endpoint; - } - } - } - } - - return 0xFFU; /* Invalid Endpoint */ -} - - -/** - * @brief Find MTP DATA OUT interface - * @param phost: Host handle - * @retval USBH Status - */ -static uint8_t MTP_FindDataOutEndpoint(USBH_HandleTypeDef *phost) -{ - uint8_t interface, endpoint; - - for (interface = 0U; interface < USBH_MAX_NUM_INTERFACES ; interface ++ ) - { - if(phost->device.CfgDesc.Itf_Desc[interface].bInterfaceClass == USB_MTP_CLASS) - { - for (endpoint = 0U; endpoint < USBH_MAX_NUM_ENDPOINTS ; endpoint ++ ) - { - - if(((phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].bEndpointAddress & 0x80U) == 0U)&& - (phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].wMaxPacketSize > 0U)&& - ((phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].bmAttributes & USBH_EP_BULK) == USBH_EP_BULK)) - { - return endpoint; - } - } - } - } - - return 0xFFU; /* Invalid Endpoint */ -} - -/** - * @brief Find MTP DATA IN interface - * @param phost: Host handle - * @retval USBH Status - */ -static uint8_t MTP_FindDataInEndpoint(USBH_HandleTypeDef *phost) -{ - uint8_t interface, endpoint; - - for (interface = 0U; interface < USBH_MAX_NUM_INTERFACES ; interface ++ ) - { - if(phost->device.CfgDesc.Itf_Desc[interface].bInterfaceClass == USB_MTP_CLASS) - { - for (endpoint = 0U; endpoint < USBH_MAX_NUM_ENDPOINTS ; endpoint ++ ) - { - - if((phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].bEndpointAddress & 0x80U)&& - (phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].wMaxPacketSize > 0U)&& - ((phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].bmAttributes & USBH_EP_BULK) == USBH_EP_BULK)) - { - return endpoint; - } - } - } - } - - return 0xFFU; /* Invalid Endpoint */ -} - - -/** - * @brief USBH_MTP_InterfaceDeInit - * The function DeInit the Pipes used for the MTP class. - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_MTP_InterfaceDeInit (USBH_HandleTypeDef *phost) -{ - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - - if (MTP_Handle->DataOutPipe) - { - USBH_ClosePipe(phost, MTP_Handle->DataOutPipe); - USBH_FreePipe (phost, MTP_Handle->DataOutPipe); - MTP_Handle->DataOutPipe = 0U; /* Reset the Channel as Free */ - } - - if (MTP_Handle->DataInPipe) - { - USBH_ClosePipe(phost, MTP_Handle->DataInPipe); - USBH_FreePipe (phost, MTP_Handle->DataInPipe); - MTP_Handle->DataInPipe = 0U; /* Reset the Channel as Free */ - } - - if (MTP_Handle->NotificationPipe) - { - USBH_ClosePipe(phost, MTP_Handle->NotificationPipe); - USBH_FreePipe (phost, MTP_Handle->NotificationPipe); - MTP_Handle->NotificationPipe = 0U; /* Reset the Channel as Free */ - } - - if(phost->pActiveClass->pData) - { - USBH_free (phost->pActiveClass->pData); - phost->pActiveClass->pData = 0U; - } - return USBH_OK; -} - -/** - * @brief USBH_MTP_ClassRequest - * The function is responsible for handling Standard requests - * for MTP class. - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_MTP_ClassRequest (USBH_HandleTypeDef *phost) -{ -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - - return USBH_OK; -} - - -/** - * @brief USBH_MTP_Process - * The function is for managing state machine for MTP data transfers - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_MTP_Process (USBH_HandleTypeDef *phost) -{ - USBH_StatusTypeDef status = USBH_BUSY; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - uint32_t idx = 0U; - - switch(MTP_Handle->state) - { - case MTP_OPENSESSION: - - status = USBH_PTP_OpenSession (phost, 1U); /* Session '0' is not valid */ - - if(status == USBH_OK) - { - USBH_UsrLog("MTP Session #0 Opened"); - MTP_Handle->state = MTP_GETDEVICEINFO; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - break; - - case MTP_GETDEVICEINFO: - status = USBH_PTP_GetDeviceInfo (phost, &(MTP_Handle->info.devinfo)); - - if(status == USBH_OK) - { - USBH_DbgLog(">>>>> MTP Device Information"); - USBH_DbgLog("Standard version : %x", MTP_Handle->info.devinfo.StandardVersion); - USBH_DbgLog("Vendor ExtID : %s", (MTP_Handle->info.devinfo.VendorExtensionID == 6)?"MTP": "NOT SUPPORTED"); - USBH_DbgLog("Functional mode : %s", (MTP_Handle->info.devinfo.FunctionalMode == 0U) ? "Standard" : "Vendor"); - USBH_DbgLog("Number of Supported Operation(s) : %d", MTP_Handle->info.devinfo.OperationsSupported_len); - USBH_DbgLog("Number of Supported Events(s) : %d", MTP_Handle->info.devinfo.EventsSupported_len); - USBH_DbgLog("Number of Supported Proprieties : %d", MTP_Handle->info.devinfo.DevicePropertiesSupported_len); - USBH_DbgLog("Manufacturer : %s", MTP_Handle->info.devinfo.Manufacturer); - USBH_DbgLog("Model : %s", MTP_Handle->info.devinfo.Model); - USBH_DbgLog("Device version : %s", MTP_Handle->info.devinfo.DeviceVersion); - USBH_DbgLog("Serial number : %s", MTP_Handle->info.devinfo.SerialNumber); - - MTP_Handle->state = MTP_GETSTORAGEIDS; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - break; - - case MTP_GETSTORAGEIDS: - status = USBH_PTP_GetStorageIds (phost, &(MTP_Handle->info.storids)); - - if(status == USBH_OK) - { - USBH_DbgLog("Number of storage ID items : %d", MTP_Handle->info.storids.n); - for (idx = 0U; idx < MTP_Handle->info.storids.n; idx ++) - { - USBH_DbgLog("storage#%d ID : %x", idx, MTP_Handle->info.storids.Storage[idx]); - } - - MTP_Handle->current_storage_unit = 0U; - MTP_Handle->state = MTP_GETSTORAGEINFO; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - break; - - case MTP_GETSTORAGEINFO: - status = USBH_PTP_GetStorageInfo (phost, - MTP_Handle->info.storids.Storage[MTP_Handle->current_storage_unit], - &((MTP_Handle->info.storinfo)[MTP_Handle->current_storage_unit])); - - if(status == USBH_OK) - { - USBH_UsrLog("Volume#%lu: %s [%s]", MTP_Handle->current_storage_unit, - MTP_Handle->info.storinfo[MTP_Handle->current_storage_unit].StorageDescription, - MTP_Handle->info.storinfo[MTP_Handle->current_storage_unit].VolumeLabel); - if(++MTP_Handle->current_storage_unit >= MTP_Handle->info.storids.n) - { - MTP_Handle->state = MTP_IDLE; - MTP_Handle->is_ready = 1U; - MTP_Handle->current_storage_unit = 0U; - MTP_Handle->params.CurrentStorageId = MTP_Handle->info.storids.Storage[0]; - - USBH_UsrLog( "MTP Class initialized."); - USBH_UsrLog("%s is default storage unit", MTP_Handle->info.storinfo[0].StorageDescription); - phost->pUser(phost, HOST_USER_CLASS_ACTIVE); - } - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - break; - - case MTP_IDLE: - USBH_MTP_Events(phost); - -#if (USBH_USE_OS == 1U) - osDelay(10U); - phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - - status = USBH_OK; - break; - - default: - break; - } - return status; -} - -/** - * @brief USBH_MTP_SOFProcess - * The function is for managing SOF callback - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_MTP_SOFProcess (USBH_HandleTypeDef *phost) -{ - USBH_StatusTypeDef status = USBH_OK; - - return status; -} - -/** - * @brief USBH_MTP_IsReady - * Select the storage Unit to be used - * @param phost: Host handle - * @retval USBH Status - */ -uint8_t USBH_MTP_IsReady (USBH_HandleTypeDef *phost) -{ - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - - return ((uint8_t)MTP_Handle->is_ready); -} - -/** - * @brief USBH_MTP_GetNumStorage - * Select the storage Unit to be used - * @param phost: Host handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_MTP_GetNumStorage (USBH_HandleTypeDef *phost, uint8_t *storage_num) -{ - USBH_StatusTypeDef status = USBH_FAIL; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - - if(MTP_Handle->is_ready > 0U) - { - *storage_num = (uint8_t)MTP_Handle->info.storids.n; - status = USBH_OK; - } - - return status; -} - -/** - * @brief USBH_MTP_SelectStorage - * Select the storage Unit to be used - * @param phost: Host handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_MTP_SelectStorage (USBH_HandleTypeDef *phost, uint8_t storage_idx) -{ - USBH_StatusTypeDef status = USBH_FAIL; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - - if((storage_idx < MTP_Handle->info.storids.n) && (MTP_Handle->is_ready)) - { - MTP_Handle->params.CurrentStorageId = MTP_Handle->info.storids.Storage[storage_idx]; - status = USBH_OK; - } - - return status; -} - -/** - * @brief USBH_MTP_GetStorageInfo - * Get the storage Unit info - * @param phost: Host handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_MTP_GetStorageInfo (USBH_HandleTypeDef *phost, uint8_t storage_idx, MTP_StorageInfoTypedef *info) -{ - USBH_StatusTypeDef status = USBH_FAIL; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - - if((storage_idx < MTP_Handle->info.storids.n) && (MTP_Handle->is_ready)) - { - *info = MTP_Handle->info.storinfo[storage_idx]; - status = USBH_OK; - } - return status; -} - -/** - * @brief USBH_MTP_GetStorageInfo - * Get the storage Unit info - * @param phost: Host handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_MTP_GetNumObjects (USBH_HandleTypeDef *phost, - uint32_t storage_idx, - uint32_t objectformatcode, - uint32_t associationOH, - uint32_t* numobs) -{ - USBH_StatusTypeDef status = USBH_FAIL; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - uint32_t timeout = phost->Timer; - if((storage_idx < MTP_Handle->info.storids.n) && (MTP_Handle->is_ready)) - { - while ((status = USBH_PTP_GetNumObjects (phost, - MTP_Handle->info.storids.Storage[storage_idx], - objectformatcode, - associationOH, - numobs)) == USBH_BUSY) - { - if(((phost->Timer - timeout) > 5000U) || (phost->device.is_connected == 0U)) - { - return USBH_FAIL; - } - } - } - return status; -} - - -/** - * @brief USBH_MTP_GetStorageInfo - * Get the storage Unit info - * @param phost: Host handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_MTP_GetObjectHandles (USBH_HandleTypeDef *phost, - uint32_t storage_idx, - uint32_t objectformatcode, - uint32_t associationOH, - PTP_ObjectHandlesTypedef* objecthandles) -{ - USBH_StatusTypeDef status = USBH_FAIL; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - uint32_t timeout = phost->Timer; - - if((storage_idx < MTP_Handle->info.storids.n) && (MTP_Handle->is_ready)) - { - while ((status = USBH_PTP_GetObjectHandles (phost, - MTP_Handle->info.storids.Storage[storage_idx], - objectformatcode, - associationOH, - objecthandles)) == USBH_BUSY) - { - if(((phost->Timer - timeout) > 5000U) || (phost->device.is_connected == 0U)) - { - return USBH_FAIL; - } - } - } - return status; -} - -/** - * @brief USBH_PTP_GetObjectInfo - * Gets objert info - * @param phost: Host handle - * @param dev_info: Device info structure - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_MTP_GetObjectInfo (USBH_HandleTypeDef *phost, - uint32_t handle, - PTP_ObjectInfoTypedef* objectinfo) -{ - USBH_StatusTypeDef status = USBH_FAIL; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - uint32_t timeout = phost->Timer; - - if(MTP_Handle->is_ready) - { - while ((status = USBH_PTP_GetObjectInfo (phost, handle, objectinfo)) == USBH_BUSY) - { - if(((phost->Timer - timeout) > 5000U) || (phost->device.is_connected == 0U)) - { - return USBH_FAIL; - } - } - } - return status; -} -/** - * @brief USBH_MTP_DeleteObject - * Delete an object. - * @param phost: Host handle - * @param handle : Object Handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_MTP_DeleteObject (USBH_HandleTypeDef *phost, - uint32_t handle, - uint32_t objectformatcode) -{ - USBH_StatusTypeDef status = USBH_FAIL; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - uint32_t timeout = phost->Timer; - - if(MTP_Handle->is_ready) - { - while ((status = USBH_PTP_DeleteObject (phost, handle, objectformatcode)) == USBH_BUSY) - { - if(((phost->Timer - timeout) > 5000U) || (phost->device.is_connected == 0U)) - { - return USBH_FAIL; - } - } - } - return status; -} - -/** - * @brief USBH_MTP_GetObject - * Gets object - * @param phost: Host handle - * @param dev_info: Device info structure - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_MTP_GetObject (USBH_HandleTypeDef *phost, - uint32_t handle, - uint8_t *object) -{ - USBH_StatusTypeDef status = USBH_FAIL; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - uint32_t timeout = phost->Timer; - - if(MTP_Handle->is_ready) - { - while ((status = USBH_PTP_GetObject (phost, handle, object)) == USBH_BUSY) - { - if(((phost->Timer - timeout) > 5000U) || (phost->device.is_connected == 0U)) - { - return USBH_FAIL; - } - } - } - return status; -} - -/** - * @brief USBH_MTP_GetPartialObject - * Gets object - * @param phost: Host handle - * @param dev_info: Device info structure - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_MTP_GetPartialObject(USBH_HandleTypeDef *phost, - uint32_t handle, - uint32_t offset, - uint32_t maxbytes, - uint8_t *object, - uint32_t *len) -{ - USBH_StatusTypeDef status = USBH_FAIL; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - uint32_t timeout = phost->Timer; - - if(MTP_Handle->is_ready) - { - while ((status = USBH_PTP_GetPartialObject(phost, - handle, - offset, - maxbytes, - object, - len)) == USBH_BUSY) - { - if(((phost->Timer - timeout) > 5000U) || (phost->device.is_connected == 0U)) - { - return USBH_FAIL; - } - } - } - return status; -} - -/** - * @brief USBH_MTP_GetObjectPropsSupported - * Gets object partially - * @param phost: Host handle - * @param dev_info: Device info structure - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_MTP_GetObjectPropsSupported (USBH_HandleTypeDef *phost, - uint16_t ofc, - uint32_t *propnum, - uint16_t *props) -{ - USBH_StatusTypeDef status = USBH_FAIL; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - uint32_t timeout = phost->Timer; - - if(MTP_Handle->is_ready) - { - while ((status = USBH_PTP_GetObjectPropsSupported (phost, - ofc, - propnum, - props)) == USBH_BUSY) - { - if(((phost->Timer - timeout) > 5000U) || (phost->device.is_connected == 0U)) - { - return USBH_FAIL; - } - } - } - return status; -} - -/** - * @brief USBH_MTP_GetObjectPropDesc - * Gets object partially - * @param phost: Host handle - * @param dev_info: Device info structure - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_MTP_GetObjectPropDesc (USBH_HandleTypeDef *phost, - uint16_t opc, - uint16_t ofc, - PTP_ObjectPropDescTypeDef *opd) -{ - USBH_StatusTypeDef status = USBH_FAIL; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - uint32_t timeout = phost->Timer; - - if(MTP_Handle->is_ready) - { - while ((status = USBH_PTP_GetObjectPropDesc (phost, - opc, - ofc, - opd)) == USBH_BUSY) - { - if(((phost->Timer - timeout) > 5000U) || (phost->device.is_connected == 0U)) - { - return USBH_FAIL; - } - } - } - return status; -} - -/** - * @brief USBH_MTP_GetObjectPropList - * Gets object partially - * @param phost: Host handle - * @param dev_info: Device info structure - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_MTP_GetObjectPropList (USBH_HandleTypeDef *phost, - uint32_t handle, - MTP_PropertiesTypedef *pprops, - uint32_t *nrofprops) -{ - USBH_StatusTypeDef status = USBH_FAIL; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - uint32_t timeout = phost->Timer; - - if(MTP_Handle->is_ready) - { - while ((status = USBH_PTP_GetObjectPropList (phost, - handle, - pprops, - nrofprops)) == USBH_BUSY) - { - if(((phost->Timer - timeout) > 5000U) || (phost->device.is_connected == 0U)) - { - return USBH_FAIL; - } - } - } - return status; -} - -/** - * @brief USBH_MTP_SendObject - * Send an object - * @param phost: Host handle - * @param dev_info: Device info structure - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_MTP_SendObject (USBH_HandleTypeDef *phost, - uint32_t handle, - uint8_t *object, - uint32_t size) -{ - USBH_StatusTypeDef status = USBH_FAIL; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - uint32_t timeout = phost->Timer; - - if(MTP_Handle->is_ready) - { - while ((status = USBH_PTP_SendObject (phost, handle, object, size)) == USBH_BUSY) - { - if(((phost->Timer - timeout) > 5000U) || (phost->device.is_connected == 0U)) - { - return USBH_FAIL; - } - } - } - return status; -} - - /** - * @brief Handle HID Control process - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_MTP_Events (USBH_HandleTypeDef *phost) -{ - USBH_StatusTypeDef status = USBH_BUSY ; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - - - switch(MTP_Handle->events.state) - { - case MTP_EVENTS_INIT: - if((phost->Timer & 1U) == 0U) - { - MTP_Handle->events.timer = phost->Timer; - USBH_InterruptReceiveData(phost, - (uint8_t *)(void *)&(MTP_Handle->events.container), - (uint8_t)MTP_Handle->NotificationEpSize, - MTP_Handle->NotificationPipe); - - - MTP_Handle->events.state = MTP_EVENTS_GETDATA ; - } - break; - case MTP_EVENTS_GETDATA: - if(USBH_LL_GetURBState(phost , MTP_Handle->NotificationPipe) == USBH_URB_DONE) - { - MTP_DecodeEvent(phost); - } - - if(( phost->Timer - MTP_Handle->events.timer) >= MTP_Handle->events.poll) - { - MTP_Handle->events.timer = phost->Timer; - - USBH_InterruptReceiveData(phost, - (uint8_t *)(void *)&(MTP_Handle->events.container), - (uint8_t)MTP_Handle->NotificationEpSize, - MTP_Handle->NotificationPipe); - - } - break; - - default: - break; - } - - return status; -} - -/** - * @brief MTP_DecodeEvent - * Decode device event sent by responder - * @param phost: Host handle - * @retval None - */ -static void MTP_DecodeEvent (USBH_HandleTypeDef *phost) -{ - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - - uint16_t code; - uint32_t param1; - - /* Process the event */ - code = MTP_Handle->events.container.code; - param1 = MTP_Handle->events.container.param1; - - switch(code) - { - case PTP_EC_Undefined: - USBH_DbgLog("EVT: PTP_EC_Undefined in session %u", MTP_Handle->ptp.session_id); - break; - case PTP_EC_CancelTransaction: - USBH_DbgLog("EVT: PTP_EC_CancelTransaction in session %u", MTP_Handle->ptp.session_id); - break; - case PTP_EC_ObjectAdded: - USBH_DbgLog("EVT: PTP_EC_ObjectAdded in session %u", MTP_Handle->ptp.session_id); - break; - - case PTP_EC_ObjectRemoved: - USBH_DbgLog("EVT: PTP_EC_ObjectRemoved in session %u", MTP_Handle->ptp.session_id); - break; - - case PTP_EC_StoreAdded: - USBH_DbgLog("EVT: PTP_EC_StoreAdded in session %u", MTP_Handle->ptp.session_id); - break; - - case PTP_EC_StoreRemoved: - USBH_DbgLog("EVT: PTP_EC_StoreRemoved in session %u", MTP_Handle->ptp.session_id); - break; - - case PTP_EC_DevicePropChanged: - USBH_DbgLog("EVT: PTP_EC_DevicePropChanged in session %u", MTP_Handle->ptp.session_id); - break; - - case PTP_EC_ObjectInfoChanged: - USBH_DbgLog("EVT: PTP_EC_ObjectInfoChanged in session %u", MTP_Handle->ptp.session_id); - break; - - case PTP_EC_DeviceInfoChanged: - USBH_DbgLog("EVT: PTP_EC_DeviceInfoChanged in session %u", MTP_Handle->ptp.session_id); - break; - - case PTP_EC_RequestObjectTransfer: - USBH_DbgLog("EVT: PTP_EC_RequestObjectTransfer in session %u", MTP_Handle->ptp.session_id); - break; - - case PTP_EC_StoreFull: - USBH_DbgLog("EVT: PTP_EC_StoreFull in session %u", MTP_Handle->ptp.session_id); - break; - - case PTP_EC_DeviceReset: - USBH_DbgLog("EVT: PTP_EC_DeviceReset in session %u", MTP_Handle->ptp.session_id); - break; - - case PTP_EC_StorageInfoChanged : - USBH_DbgLog("EVT: PTP_EC_StorageInfoChanged in session %u", MTP_Handle->ptp.session_id); - break; - - case PTP_EC_CaptureComplete : - USBH_DbgLog("EVT: PTP_EC_CaptureComplete in session %u", MTP_Handle->ptp.session_id); - break; - - case PTP_EC_UnreportedStatus : - USBH_DbgLog("EVT: PTP_EC_UnreportedStatus in session %u", MTP_Handle->ptp.session_id); - break; - - default : - USBH_DbgLog("Received unknown event in session %u", MTP_Handle->ptp.session_id); - break; - } - - USBH_MTP_EventsCallback(phost, (uint32_t)code, param1); -} - -/** - * @brief USBH_MTP_GetDevicePropDesc - * Gets object partially - * @param phost: Host handle - * @param dev_info: Device info structure - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_MTP_GetDevicePropDesc (USBH_HandleTypeDef *phost, - uint16_t propcode, - PTP_DevicePropDescTypdef* devicepropertydesc) - -{ - USBH_StatusTypeDef status = USBH_FAIL; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - uint32_t timeout = phost->Timer; - - if(MTP_Handle->is_ready) - { - while ((status = USBH_PTP_GetDevicePropDesc (phost, propcode, devicepropertydesc)) == USBH_BUSY) - { - if(((phost->Timer - timeout) > 5000U) || (phost->device.is_connected == 0U)) - { - return USBH_FAIL; - } - } - } - return status; -} -/** - * @brief The function informs that host has received an event - * @param pdev: Selected device - * @retval None - */ -__weak void USBH_MTP_EventsCallback(USBH_HandleTypeDef *phost, uint32_t event, uint32_t param) -{ - -} -/** -* @} -*/ - -/** -* @} -*/ - -/** -* @} -*/ - - -/** -* @} -*/ - - -/** -* @} -*/ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbh_mtp.c + * @author MCD Application Team + * @brief This file is the MTP Layer Handlers for USB Host MTP class. + * + * @verbatim + * + * =================================================================== + * MTP Class Description + * =================================================================== + * This module manages the MTP class following the + * "Media Transfer Protocol (MTP) specification Version 1.11 April 6th, 2011". + * the implmentation is compatible with the PTP model as an extension + * of the existing Picture Transfer Protocol defined by the ISO 15740 specification + * + * @endverbatim + * + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + + /* BSPDependencies + - "stm32xxxxx_{eval}{discovery}.c" + - "stm32xxxxx_{eval}{discovery}_io.c" + - "stm32xxxxx_{eval}{discovery}_audio.c" + - "stm32xxxxx_{eval}{discovery}_sd.c" + - "stm32xxxxx_{eval}{discovery}_lcd.c" + - "stm32xxxxx_{eval}{discovery}_sdram.c" + EndBSPDependencies */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_mtp.h" + +/** @addtogroup USBH_LIB +* @{ +*/ + +/** @addtogroup USBH_CLASS +* @{ +*/ + +/** @addtogroup USBH_MTP_CLASS +* @{ +*/ + +/** @defgroup USBH_MTP_CORE +* @brief This file includes MTP Layer Handlers for USB Host MTP class. +* @{ +*/ + +/** @defgroup USBH_MTP_CORE_Private_TypesDefinitions +* @{ +*/ +/** +* @} +*/ + + +/** @defgroup USBH_MTP_CORE_Private_Defines +* @{ +*/ +/** +* @} +*/ + + +/** @defgroup USBH_MTP_CORE_Private_Macros +* @{ +*/ +/** +* @} +*/ + + +/** @defgroup USBH_MTP_CORE_Private_Variables +* @{ +*/ +/** +* @} +*/ + + +/** @defgroup USBH_MTP_CORE_Private_FunctionPrototypes +* @{ +*/ + +static USBH_StatusTypeDef USBH_MTP_InterfaceInit (USBH_HandleTypeDef *phost); + +static USBH_StatusTypeDef USBH_MTP_InterfaceDeInit (USBH_HandleTypeDef *phost); + +static USBH_StatusTypeDef USBH_MTP_Process(USBH_HandleTypeDef *phost); + +static USBH_StatusTypeDef USBH_MTP_ClassRequest (USBH_HandleTypeDef *phost); + +static uint8_t MTP_FindCtlEndpoint(USBH_HandleTypeDef *phost); + +static uint8_t MTP_FindDataOutEndpoint(USBH_HandleTypeDef *phost); + +static uint8_t MTP_FindDataInEndpoint(USBH_HandleTypeDef *phost); + +static USBH_StatusTypeDef USBH_MTP_SOFProcess (USBH_HandleTypeDef *phost); + +static USBH_StatusTypeDef USBH_MTP_Events (USBH_HandleTypeDef *phost); + +static void MTP_DecodeEvent (USBH_HandleTypeDef *phost) ; + +USBH_ClassTypeDef MTP_Class = +{ + "MTP", + USB_MTP_CLASS, + USBH_MTP_InterfaceInit, + USBH_MTP_InterfaceDeInit, + USBH_MTP_ClassRequest, + USBH_MTP_Process, + USBH_MTP_SOFProcess, + NULL, +}; +/** +* @} +*/ + + +/** @defgroup USBH_MTP_CORE_Private_Functions +* @{ +*/ + +/** + * @brief USBH_MTP_InterfaceInit + * The function init the MTP class. + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_MTP_InterfaceInit (USBH_HandleTypeDef *phost) +{ + USBH_StatusTypeDef status = USBH_OK; + uint8_t interface, endpoint; + + MTP_HandleTypeDef *MTP_Handle; + + interface = USBH_FindInterface(phost, USB_MTP_CLASS, 1U, 1U); + + if(interface == 0xFFU) /* No Valid Interface */ + { + status = USBH_FAIL; + USBH_DbgLog ("Cannot Find the interface for Still Image Class."); + } + else + { + USBH_SelectInterface (phost, interface); + + endpoint = MTP_FindCtlEndpoint(phost); + + phost->pActiveClass->pData = (MTP_HandleTypeDef *)USBH_malloc (sizeof(MTP_HandleTypeDef)); + MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + + if( MTP_Handle == NULL) + { + status = USBH_FAIL; + USBH_DbgLog ("Cannot allocate RAM for MTP Handle"); + } + + /*Collect the control endpoint address and length*/ + MTP_Handle->NotificationEp = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].bEndpointAddress; + MTP_Handle->NotificationEpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].wMaxPacketSize; + MTP_Handle->NotificationPipe = USBH_AllocPipe(phost, MTP_Handle->NotificationEp); + MTP_Handle->events.poll = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].bInterval; + + /* Open pipe for Notification endpoint */ + USBH_OpenPipe (phost, + MTP_Handle->NotificationPipe, + MTP_Handle->NotificationEp, + phost->device.address, + phost->device.speed, + USB_EP_TYPE_INTR, + MTP_Handle->NotificationEpSize); + + USBH_LL_SetToggle (phost, MTP_Handle->NotificationPipe, 0U); + + + endpoint = MTP_FindDataInEndpoint(phost); + + /*Collect the control endpoint address and length*/ + MTP_Handle->DataInEp = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].bEndpointAddress; + MTP_Handle->DataInEpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].wMaxPacketSize; + MTP_Handle->DataInPipe = USBH_AllocPipe(phost, MTP_Handle->DataInEp); + + /* Open pipe for DATA IN endpoint */ + USBH_OpenPipe (phost, + MTP_Handle->DataInPipe, + MTP_Handle->DataInEp, + phost->device.address, + phost->device.speed, + USB_EP_TYPE_BULK, + MTP_Handle->DataInEpSize); + + USBH_LL_SetToggle (phost, MTP_Handle->DataInPipe, 0U); + + endpoint = MTP_FindDataOutEndpoint(phost); + + /*Collect the DATA OUT endpoint address and length*/ + MTP_Handle->DataOutEp = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].bEndpointAddress; + MTP_Handle->DataOutEpSize = phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].wMaxPacketSize; + MTP_Handle->DataOutPipe = USBH_AllocPipe(phost, MTP_Handle->DataOutEp); + + /* Open pipe for DATA OUT endpoint */ + USBH_OpenPipe (phost, + MTP_Handle->DataOutPipe, + MTP_Handle->DataOutEp, + phost->device.address, + phost->device.speed, + USB_EP_TYPE_BULK, + MTP_Handle->DataOutEpSize); + + USBH_LL_SetToggle (phost, MTP_Handle->DataOutPipe, 0U); + + + MTP_Handle->state = MTP_OPENSESSION; + MTP_Handle->is_ready = 0U; + MTP_Handle->events.state = MTP_EVENTS_INIT; + return USBH_PTP_Init(phost); + + } + return status; +} + +/** + * @brief Find MTP Ctl interface + * @param phost: Host handle + * @retval USBH Status + */ +static uint8_t MTP_FindCtlEndpoint(USBH_HandleTypeDef *phost) +{ + uint8_t interface, endpoint; + + for (interface = 0U; interface < USBH_MAX_NUM_INTERFACES ; interface ++ ) + { + if(phost->device.CfgDesc.Itf_Desc[interface].bInterfaceClass == USB_MTP_CLASS) + { + for (endpoint = 0U; endpoint < USBH_MAX_NUM_ENDPOINTS ; endpoint ++ ) + { + if((phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].bEndpointAddress & 0x80U)&& + (phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].wMaxPacketSize > 0U)&& + ((phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].bmAttributes & USBH_EP_INTERRUPT) == USBH_EP_INTERRUPT)) + { + return endpoint; + } + } + } + } + + return 0xFFU; /* Invalid Endpoint */ +} + + +/** + * @brief Find MTP DATA OUT interface + * @param phost: Host handle + * @retval USBH Status + */ +static uint8_t MTP_FindDataOutEndpoint(USBH_HandleTypeDef *phost) +{ + uint8_t interface, endpoint; + + for (interface = 0U; interface < USBH_MAX_NUM_INTERFACES ; interface ++ ) + { + if(phost->device.CfgDesc.Itf_Desc[interface].bInterfaceClass == USB_MTP_CLASS) + { + for (endpoint = 0U; endpoint < USBH_MAX_NUM_ENDPOINTS ; endpoint ++ ) + { + + if(((phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].bEndpointAddress & 0x80U) == 0U)&& + (phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].wMaxPacketSize > 0U)&& + ((phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].bmAttributes & USBH_EP_BULK) == USBH_EP_BULK)) + { + return endpoint; + } + } + } + } + + return 0xFFU; /* Invalid Endpoint */ +} + +/** + * @brief Find MTP DATA IN interface + * @param phost: Host handle + * @retval USBH Status + */ +static uint8_t MTP_FindDataInEndpoint(USBH_HandleTypeDef *phost) +{ + uint8_t interface, endpoint; + + for (interface = 0U; interface < USBH_MAX_NUM_INTERFACES ; interface ++ ) + { + if(phost->device.CfgDesc.Itf_Desc[interface].bInterfaceClass == USB_MTP_CLASS) + { + for (endpoint = 0U; endpoint < USBH_MAX_NUM_ENDPOINTS ; endpoint ++ ) + { + + if((phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].bEndpointAddress & 0x80U)&& + (phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].wMaxPacketSize > 0U)&& + ((phost->device.CfgDesc.Itf_Desc[interface].Ep_Desc[endpoint].bmAttributes & USBH_EP_BULK) == USBH_EP_BULK)) + { + return endpoint; + } + } + } + } + + return 0xFFU; /* Invalid Endpoint */ +} + + +/** + * @brief USBH_MTP_InterfaceDeInit + * The function DeInit the Pipes used for the MTP class. + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_MTP_InterfaceDeInit (USBH_HandleTypeDef *phost) +{ + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + + if (MTP_Handle->DataOutPipe) + { + USBH_ClosePipe(phost, MTP_Handle->DataOutPipe); + USBH_FreePipe (phost, MTP_Handle->DataOutPipe); + MTP_Handle->DataOutPipe = 0U; /* Reset the Channel as Free */ + } + + if (MTP_Handle->DataInPipe) + { + USBH_ClosePipe(phost, MTP_Handle->DataInPipe); + USBH_FreePipe (phost, MTP_Handle->DataInPipe); + MTP_Handle->DataInPipe = 0U; /* Reset the Channel as Free */ + } + + if (MTP_Handle->NotificationPipe) + { + USBH_ClosePipe(phost, MTP_Handle->NotificationPipe); + USBH_FreePipe (phost, MTP_Handle->NotificationPipe); + MTP_Handle->NotificationPipe = 0U; /* Reset the Channel as Free */ + } + + if(phost->pActiveClass->pData) + { + USBH_free (phost->pActiveClass->pData); + phost->pActiveClass->pData = 0U; + } + return USBH_OK; +} + +/** + * @brief USBH_MTP_ClassRequest + * The function is responsible for handling Standard requests + * for MTP class. + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_MTP_ClassRequest (USBH_HandleTypeDef *phost) +{ +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + + return USBH_OK; +} + + +/** + * @brief USBH_MTP_Process + * The function is for managing state machine for MTP data transfers + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_MTP_Process (USBH_HandleTypeDef *phost) +{ + USBH_StatusTypeDef status = USBH_BUSY; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + uint32_t idx = 0U; + + switch(MTP_Handle->state) + { + case MTP_OPENSESSION: + + status = USBH_PTP_OpenSession (phost, 1U); /* Session '0' is not valid */ + + if(status == USBH_OK) + { + USBH_UsrLog("MTP Session #0 Opened"); + MTP_Handle->state = MTP_GETDEVICEINFO; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + break; + + case MTP_GETDEVICEINFO: + status = USBH_PTP_GetDeviceInfo (phost, &(MTP_Handle->info.devinfo)); + + if(status == USBH_OK) + { + USBH_DbgLog(">>>>> MTP Device Information"); + USBH_DbgLog("Standard version : %x", MTP_Handle->info.devinfo.StandardVersion); + USBH_DbgLog("Vendor ExtID : %s", (MTP_Handle->info.devinfo.VendorExtensionID == 6)?"MTP": "NOT SUPPORTED"); + USBH_DbgLog("Functional mode : %s", (MTP_Handle->info.devinfo.FunctionalMode == 0U) ? "Standard" : "Vendor"); + USBH_DbgLog("Number of Supported Operation(s) : %d", MTP_Handle->info.devinfo.OperationsSupported_len); + USBH_DbgLog("Number of Supported Events(s) : %d", MTP_Handle->info.devinfo.EventsSupported_len); + USBH_DbgLog("Number of Supported Proprieties : %d", MTP_Handle->info.devinfo.DevicePropertiesSupported_len); + USBH_DbgLog("Manufacturer : %s", MTP_Handle->info.devinfo.Manufacturer); + USBH_DbgLog("Model : %s", MTP_Handle->info.devinfo.Model); + USBH_DbgLog("Device version : %s", MTP_Handle->info.devinfo.DeviceVersion); + USBH_DbgLog("Serial number : %s", MTP_Handle->info.devinfo.SerialNumber); + + MTP_Handle->state = MTP_GETSTORAGEIDS; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + break; + + case MTP_GETSTORAGEIDS: + status = USBH_PTP_GetStorageIds (phost, &(MTP_Handle->info.storids)); + + if(status == USBH_OK) + { + USBH_DbgLog("Number of storage ID items : %d", MTP_Handle->info.storids.n); + for (idx = 0U; idx < MTP_Handle->info.storids.n; idx ++) + { + USBH_DbgLog("storage#%d ID : %x", idx, MTP_Handle->info.storids.Storage[idx]); + } + + MTP_Handle->current_storage_unit = 0U; + MTP_Handle->state = MTP_GETSTORAGEINFO; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + break; + + case MTP_GETSTORAGEINFO: + status = USBH_PTP_GetStorageInfo (phost, + MTP_Handle->info.storids.Storage[MTP_Handle->current_storage_unit], + &((MTP_Handle->info.storinfo)[MTP_Handle->current_storage_unit])); + + if(status == USBH_OK) + { + USBH_UsrLog("Volume#%lu: %s [%s]", MTP_Handle->current_storage_unit, + MTP_Handle->info.storinfo[MTP_Handle->current_storage_unit].StorageDescription, + MTP_Handle->info.storinfo[MTP_Handle->current_storage_unit].VolumeLabel); + if(++MTP_Handle->current_storage_unit >= MTP_Handle->info.storids.n) + { + MTP_Handle->state = MTP_IDLE; + MTP_Handle->is_ready = 1U; + MTP_Handle->current_storage_unit = 0U; + MTP_Handle->params.CurrentStorageId = MTP_Handle->info.storids.Storage[0]; + + USBH_UsrLog( "MTP Class initialized."); + USBH_UsrLog("%s is default storage unit", MTP_Handle->info.storinfo[0].StorageDescription); + phost->pUser(phost, HOST_USER_CLASS_ACTIVE); + } + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + break; + + case MTP_IDLE: + USBH_MTP_Events(phost); + +#if (USBH_USE_OS == 1U) + osDelay(10U); + phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + + status = USBH_OK; + break; + + default: + break; + } + return status; +} + +/** + * @brief USBH_MTP_SOFProcess + * The function is for managing SOF callback + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_MTP_SOFProcess (USBH_HandleTypeDef *phost) +{ + USBH_StatusTypeDef status = USBH_OK; + + return status; +} + +/** + * @brief USBH_MTP_IsReady + * Select the storage Unit to be used + * @param phost: Host handle + * @retval USBH Status + */ +uint8_t USBH_MTP_IsReady (USBH_HandleTypeDef *phost) +{ + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + + return ((uint8_t)MTP_Handle->is_ready); +} + +/** + * @brief USBH_MTP_GetNumStorage + * Select the storage Unit to be used + * @param phost: Host handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_MTP_GetNumStorage (USBH_HandleTypeDef *phost, uint8_t *storage_num) +{ + USBH_StatusTypeDef status = USBH_FAIL; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + + if(MTP_Handle->is_ready > 0U) + { + *storage_num = (uint8_t)MTP_Handle->info.storids.n; + status = USBH_OK; + } + + return status; +} + +/** + * @brief USBH_MTP_SelectStorage + * Select the storage Unit to be used + * @param phost: Host handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_MTP_SelectStorage (USBH_HandleTypeDef *phost, uint8_t storage_idx) +{ + USBH_StatusTypeDef status = USBH_FAIL; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + + if((storage_idx < MTP_Handle->info.storids.n) && (MTP_Handle->is_ready)) + { + MTP_Handle->params.CurrentStorageId = MTP_Handle->info.storids.Storage[storage_idx]; + status = USBH_OK; + } + + return status; +} + +/** + * @brief USBH_MTP_GetStorageInfo + * Get the storage Unit info + * @param phost: Host handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_MTP_GetStorageInfo (USBH_HandleTypeDef *phost, uint8_t storage_idx, MTP_StorageInfoTypedef *info) +{ + USBH_StatusTypeDef status = USBH_FAIL; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + + if((storage_idx < MTP_Handle->info.storids.n) && (MTP_Handle->is_ready)) + { + *info = MTP_Handle->info.storinfo[storage_idx]; + status = USBH_OK; + } + return status; +} + +/** + * @brief USBH_MTP_GetStorageInfo + * Get the storage Unit info + * @param phost: Host handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_MTP_GetNumObjects (USBH_HandleTypeDef *phost, + uint32_t storage_idx, + uint32_t objectformatcode, + uint32_t associationOH, + uint32_t* numobs) +{ + USBH_StatusTypeDef status = USBH_FAIL; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + uint32_t timeout = phost->Timer; + if((storage_idx < MTP_Handle->info.storids.n) && (MTP_Handle->is_ready)) + { + while ((status = USBH_PTP_GetNumObjects (phost, + MTP_Handle->info.storids.Storage[storage_idx], + objectformatcode, + associationOH, + numobs)) == USBH_BUSY) + { + if(((phost->Timer - timeout) > 5000U) || (phost->device.is_connected == 0U)) + { + return USBH_FAIL; + } + } + } + return status; +} + + +/** + * @brief USBH_MTP_GetStorageInfo + * Get the storage Unit info + * @param phost: Host handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_MTP_GetObjectHandles (USBH_HandleTypeDef *phost, + uint32_t storage_idx, + uint32_t objectformatcode, + uint32_t associationOH, + PTP_ObjectHandlesTypedef* objecthandles) +{ + USBH_StatusTypeDef status = USBH_FAIL; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + uint32_t timeout = phost->Timer; + + if((storage_idx < MTP_Handle->info.storids.n) && (MTP_Handle->is_ready)) + { + while ((status = USBH_PTP_GetObjectHandles (phost, + MTP_Handle->info.storids.Storage[storage_idx], + objectformatcode, + associationOH, + objecthandles)) == USBH_BUSY) + { + if(((phost->Timer - timeout) > 5000U) || (phost->device.is_connected == 0U)) + { + return USBH_FAIL; + } + } + } + return status; +} + +/** + * @brief USBH_PTP_GetObjectInfo + * Gets objert info + * @param phost: Host handle + * @param dev_info: Device info structure + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_MTP_GetObjectInfo (USBH_HandleTypeDef *phost, + uint32_t handle, + PTP_ObjectInfoTypedef* objectinfo) +{ + USBH_StatusTypeDef status = USBH_FAIL; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + uint32_t timeout = phost->Timer; + + if(MTP_Handle->is_ready) + { + while ((status = USBH_PTP_GetObjectInfo (phost, handle, objectinfo)) == USBH_BUSY) + { + if(((phost->Timer - timeout) > 5000U) || (phost->device.is_connected == 0U)) + { + return USBH_FAIL; + } + } + } + return status; +} +/** + * @brief USBH_MTP_DeleteObject + * Delete an object. + * @param phost: Host handle + * @param handle : Object Handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_MTP_DeleteObject (USBH_HandleTypeDef *phost, + uint32_t handle, + uint32_t objectformatcode) +{ + USBH_StatusTypeDef status = USBH_FAIL; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + uint32_t timeout = phost->Timer; + + if(MTP_Handle->is_ready) + { + while ((status = USBH_PTP_DeleteObject (phost, handle, objectformatcode)) == USBH_BUSY) + { + if(((phost->Timer - timeout) > 5000U) || (phost->device.is_connected == 0U)) + { + return USBH_FAIL; + } + } + } + return status; +} + +/** + * @brief USBH_MTP_GetObject + * Gets object + * @param phost: Host handle + * @param dev_info: Device info structure + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_MTP_GetObject (USBH_HandleTypeDef *phost, + uint32_t handle, + uint8_t *object) +{ + USBH_StatusTypeDef status = USBH_FAIL; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + uint32_t timeout = phost->Timer; + + if(MTP_Handle->is_ready) + { + while ((status = USBH_PTP_GetObject (phost, handle, object)) == USBH_BUSY) + { + if(((phost->Timer - timeout) > 5000U) || (phost->device.is_connected == 0U)) + { + return USBH_FAIL; + } + } + } + return status; +} + +/** + * @brief USBH_MTP_GetPartialObject + * Gets object + * @param phost: Host handle + * @param dev_info: Device info structure + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_MTP_GetPartialObject(USBH_HandleTypeDef *phost, + uint32_t handle, + uint32_t offset, + uint32_t maxbytes, + uint8_t *object, + uint32_t *len) +{ + USBH_StatusTypeDef status = USBH_FAIL; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + uint32_t timeout = phost->Timer; + + if(MTP_Handle->is_ready) + { + while ((status = USBH_PTP_GetPartialObject(phost, + handle, + offset, + maxbytes, + object, + len)) == USBH_BUSY) + { + if(((phost->Timer - timeout) > 5000U) || (phost->device.is_connected == 0U)) + { + return USBH_FAIL; + } + } + } + return status; +} + +/** + * @brief USBH_MTP_GetObjectPropsSupported + * Gets object partially + * @param phost: Host handle + * @param dev_info: Device info structure + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_MTP_GetObjectPropsSupported (USBH_HandleTypeDef *phost, + uint16_t ofc, + uint32_t *propnum, + uint16_t *props) +{ + USBH_StatusTypeDef status = USBH_FAIL; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + uint32_t timeout = phost->Timer; + + if(MTP_Handle->is_ready) + { + while ((status = USBH_PTP_GetObjectPropsSupported (phost, + ofc, + propnum, + props)) == USBH_BUSY) + { + if(((phost->Timer - timeout) > 5000U) || (phost->device.is_connected == 0U)) + { + return USBH_FAIL; + } + } + } + return status; +} + +/** + * @brief USBH_MTP_GetObjectPropDesc + * Gets object partially + * @param phost: Host handle + * @param dev_info: Device info structure + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_MTP_GetObjectPropDesc (USBH_HandleTypeDef *phost, + uint16_t opc, + uint16_t ofc, + PTP_ObjectPropDescTypeDef *opd) +{ + USBH_StatusTypeDef status = USBH_FAIL; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + uint32_t timeout = phost->Timer; + + if(MTP_Handle->is_ready) + { + while ((status = USBH_PTP_GetObjectPropDesc (phost, + opc, + ofc, + opd)) == USBH_BUSY) + { + if(((phost->Timer - timeout) > 5000U) || (phost->device.is_connected == 0U)) + { + return USBH_FAIL; + } + } + } + return status; +} + +/** + * @brief USBH_MTP_GetObjectPropList + * Gets object partially + * @param phost: Host handle + * @param dev_info: Device info structure + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_MTP_GetObjectPropList (USBH_HandleTypeDef *phost, + uint32_t handle, + MTP_PropertiesTypedef *pprops, + uint32_t *nrofprops) +{ + USBH_StatusTypeDef status = USBH_FAIL; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + uint32_t timeout = phost->Timer; + + if(MTP_Handle->is_ready) + { + while ((status = USBH_PTP_GetObjectPropList (phost, + handle, + pprops, + nrofprops)) == USBH_BUSY) + { + if(((phost->Timer - timeout) > 5000U) || (phost->device.is_connected == 0U)) + { + return USBH_FAIL; + } + } + } + return status; +} + +/** + * @brief USBH_MTP_SendObject + * Send an object + * @param phost: Host handle + * @param dev_info: Device info structure + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_MTP_SendObject (USBH_HandleTypeDef *phost, + uint32_t handle, + uint8_t *object, + uint32_t size) +{ + USBH_StatusTypeDef status = USBH_FAIL; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + uint32_t timeout = phost->Timer; + + if(MTP_Handle->is_ready) + { + while ((status = USBH_PTP_SendObject (phost, handle, object, size)) == USBH_BUSY) + { + if(((phost->Timer - timeout) > 5000U) || (phost->device.is_connected == 0U)) + { + return USBH_FAIL; + } + } + } + return status; +} + + /** + * @brief Handle HID Control process + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_MTP_Events (USBH_HandleTypeDef *phost) +{ + USBH_StatusTypeDef status = USBH_BUSY ; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + + + switch(MTP_Handle->events.state) + { + case MTP_EVENTS_INIT: + if((phost->Timer & 1U) == 0U) + { + MTP_Handle->events.timer = phost->Timer; + USBH_InterruptReceiveData(phost, + (uint8_t *)(void *)&(MTP_Handle->events.container), + (uint8_t)MTP_Handle->NotificationEpSize, + MTP_Handle->NotificationPipe); + + + MTP_Handle->events.state = MTP_EVENTS_GETDATA ; + } + break; + case MTP_EVENTS_GETDATA: + if(USBH_LL_GetURBState(phost , MTP_Handle->NotificationPipe) == USBH_URB_DONE) + { + MTP_DecodeEvent(phost); + } + + if(( phost->Timer - MTP_Handle->events.timer) >= MTP_Handle->events.poll) + { + MTP_Handle->events.timer = phost->Timer; + + USBH_InterruptReceiveData(phost, + (uint8_t *)(void *)&(MTP_Handle->events.container), + (uint8_t)MTP_Handle->NotificationEpSize, + MTP_Handle->NotificationPipe); + + } + break; + + default: + break; + } + + return status; +} + +/** + * @brief MTP_DecodeEvent + * Decode device event sent by responder + * @param phost: Host handle + * @retval None + */ +static void MTP_DecodeEvent (USBH_HandleTypeDef *phost) +{ + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + + uint16_t code; + uint32_t param1; + + /* Process the event */ + code = MTP_Handle->events.container.code; + param1 = MTP_Handle->events.container.param1; + + switch(code) + { + case PTP_EC_Undefined: + USBH_DbgLog("EVT: PTP_EC_Undefined in session %u", MTP_Handle->ptp.session_id); + break; + case PTP_EC_CancelTransaction: + USBH_DbgLog("EVT: PTP_EC_CancelTransaction in session %u", MTP_Handle->ptp.session_id); + break; + case PTP_EC_ObjectAdded: + USBH_DbgLog("EVT: PTP_EC_ObjectAdded in session %u", MTP_Handle->ptp.session_id); + break; + + case PTP_EC_ObjectRemoved: + USBH_DbgLog("EVT: PTP_EC_ObjectRemoved in session %u", MTP_Handle->ptp.session_id); + break; + + case PTP_EC_StoreAdded: + USBH_DbgLog("EVT: PTP_EC_StoreAdded in session %u", MTP_Handle->ptp.session_id); + break; + + case PTP_EC_StoreRemoved: + USBH_DbgLog("EVT: PTP_EC_StoreRemoved in session %u", MTP_Handle->ptp.session_id); + break; + + case PTP_EC_DevicePropChanged: + USBH_DbgLog("EVT: PTP_EC_DevicePropChanged in session %u", MTP_Handle->ptp.session_id); + break; + + case PTP_EC_ObjectInfoChanged: + USBH_DbgLog("EVT: PTP_EC_ObjectInfoChanged in session %u", MTP_Handle->ptp.session_id); + break; + + case PTP_EC_DeviceInfoChanged: + USBH_DbgLog("EVT: PTP_EC_DeviceInfoChanged in session %u", MTP_Handle->ptp.session_id); + break; + + case PTP_EC_RequestObjectTransfer: + USBH_DbgLog("EVT: PTP_EC_RequestObjectTransfer in session %u", MTP_Handle->ptp.session_id); + break; + + case PTP_EC_StoreFull: + USBH_DbgLog("EVT: PTP_EC_StoreFull in session %u", MTP_Handle->ptp.session_id); + break; + + case PTP_EC_DeviceReset: + USBH_DbgLog("EVT: PTP_EC_DeviceReset in session %u", MTP_Handle->ptp.session_id); + break; + + case PTP_EC_StorageInfoChanged : + USBH_DbgLog("EVT: PTP_EC_StorageInfoChanged in session %u", MTP_Handle->ptp.session_id); + break; + + case PTP_EC_CaptureComplete : + USBH_DbgLog("EVT: PTP_EC_CaptureComplete in session %u", MTP_Handle->ptp.session_id); + break; + + case PTP_EC_UnreportedStatus : + USBH_DbgLog("EVT: PTP_EC_UnreportedStatus in session %u", MTP_Handle->ptp.session_id); + break; + + default : + USBH_DbgLog("Received unknown event in session %u", MTP_Handle->ptp.session_id); + break; + } + + USBH_MTP_EventsCallback(phost, (uint32_t)code, param1); +} + +/** + * @brief USBH_MTP_GetDevicePropDesc + * Gets object partially + * @param phost: Host handle + * @param dev_info: Device info structure + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_MTP_GetDevicePropDesc (USBH_HandleTypeDef *phost, + uint16_t propcode, + PTP_DevicePropDescTypdef* devicepropertydesc) + +{ + USBH_StatusTypeDef status = USBH_FAIL; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + uint32_t timeout = phost->Timer; + + if(MTP_Handle->is_ready) + { + while ((status = USBH_PTP_GetDevicePropDesc (phost, propcode, devicepropertydesc)) == USBH_BUSY) + { + if(((phost->Timer - timeout) > 5000U) || (phost->device.is_connected == 0U)) + { + return USBH_FAIL; + } + } + } + return status; +} +/** + * @brief The function informs that host has received an event + * @param pdev: Selected device + * @retval None + */ +__weak void USBH_MTP_EventsCallback(USBH_HandleTypeDef *phost, uint32_t event, uint32_t param) +{ + +} +/** +* @} +*/ + +/** +* @} +*/ + +/** +* @} +*/ + + +/** +* @} +*/ + + +/** +* @} +*/ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Class/MTP/Src/usbh_mtp_ptp.c b/system/Middlewares/ST/STM32_USB_Host_Library/Class/MTP/Src/usbh_mtp_ptp.c index 32b4c007d0..ebaad3bf20 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Class/MTP/Src/usbh_mtp_ptp.c +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Class/MTP/Src/usbh_mtp_ptp.c @@ -1,1974 +1,1974 @@ -/** - ****************************************************************************** - * @file usbh_mtp_ptp.c - * @author MCD Application Team - * @brief This file includes the PTP operations layer - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - - /* BSPDependencies - - "stm32xxxxx_{eval}{discovery}.c" - - "stm32xxxxx_{eval}{discovery}_io.c" - - "stm32xxxxx_{eval}{discovery}_audio.c" - - "stm32xxxxx_{eval}{discovery}_sd.c" - - "stm32xxxxx_{eval}{discovery}_lcd.c" - - "stm32xxxxx_{eval}{discovery}_sdram.c" - EndBSPDependencies */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_mtp_ptp.h" -#include "usbh_mtp.h" -/** @addtogroup USBH_LIB -* @{ -*/ - -/** @addtogroup USBH_CLASS -* @{ -*/ - -/** @addtogroup USBH_MTP_CLASS -* @{ -*/ - -/** @defgroup USBH_MTP_PTP -* @brief This file includes the mass storage related functions -* @{ -*/ - - -/** @defgroup USBH_MTP_PTP_Private_TypesDefinitions -* @{ -*/ -/** -* @} -*/ - -/** @defgroup USBH_MTP_PTP_Private_Defines -* @{ -*/ -/** -* @} -*/ - -/** @defgroup USBH_MTP_PTP_Private_Macros -* @{ -*/ -/** -* @} -*/ - - -/** @defgroup USBH_MTP_PTP_Private_Variables -* @{ -*/ - -/** -* @} -*/ - - -/** @defgroup USBH_MTP_PTP_Private_FunctionPrototypes -* @{ -*/ -static void PTP_DecodeDeviceInfo (USBH_HandleTypeDef *phost, PTP_DeviceInfoTypedef *dev_info); -static void PTP_GetStorageIDs (USBH_HandleTypeDef *phost, PTP_StorageIDsTypedef *stor_ids); -static void PTP_GetStorageInfo (USBH_HandleTypeDef *phost, uint32_t storage_id, PTP_StorageInfoTypedef *stor_info); -static void PTP_GetObjectInfo (USBH_HandleTypeDef *phost, PTP_ObjectInfoTypedef *object_info); -static void PTP_GetObjectPropDesc (USBH_HandleTypeDef *phost, PTP_ObjectPropDescTypeDef *opd, uint32_t opdlen); -static void PTP_DecodeDeviceInfo (USBH_HandleTypeDef *phost, PTP_DeviceInfoTypedef *dev_info); -static void PTP_GetDevicePropValue(USBH_HandleTypeDef *phost, - uint32_t *offset, - uint32_t total, - PTP_PropertyValueTypedef* value, - uint16_t datatype); - -static uint32_t PTP_GetObjectPropList (USBH_HandleTypeDef *phost, - MTP_PropertiesTypedef *props, - uint32_t len); - - -static void PTP_BufferFullCallback(USBH_HandleTypeDef *phost); -static void PTP_GetString(uint8_t *str, uint8_t* data, uint16_t *len); -static uint32_t PTP_GetArray16 (uint16_t *array, uint8_t *data, uint32_t offset); -static uint32_t PTP_GetArray32 (uint32_t *array, uint8_t *data, uint32_t offset); -/** -* @} -*/ - - -/** @defgroup USBH_MTP_PTP_Exported_Variables -* @{ -*/ -/** -* @} -*/ - - -/** @defgroup USBH_MTP_PTP_Private_Functions -* @{ -*/ -/** - * @brief USBH_PTP_Init - * The function Initializes the BOT protocol. - * @param phost: Host handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_PTP_Init(USBH_HandleTypeDef *phost) -{ - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - - /* Set state to idle to be ready for operations */ - MTP_Handle->ptp.state = PTP_IDLE; - MTP_Handle->ptp.req_state = PTP_REQ_SEND; - - return USBH_OK; -} - -/** - * @brief USBH_PTP_Process - * The function handle the BOT protocol. - * @param phost: Host handle - * @param lun: Logical Unit Number - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_PTP_Process (USBH_HandleTypeDef *phost) -{ - USBH_StatusTypeDef status = USBH_BUSY; - USBH_URBStateTypeDef URB_Status = USBH_URB_IDLE; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - PTP_ContainerTypedef ptp_container; - uint32_t len; - - switch (MTP_Handle->ptp.state) - { - case PTP_IDLE: - /*Do Nothing */ - break; - - case PTP_OP_REQUEST_STATE: - USBH_BulkSendData (phost, - (uint8_t*)(void *)&(MTP_Handle->ptp.op_container), - (uint16_t)MTP_Handle->ptp.op_container.length, - MTP_Handle->DataOutPipe, - 1U); - MTP_Handle->ptp.state = PTP_OP_REQUEST_WAIT_STATE; - break; - - case PTP_OP_REQUEST_WAIT_STATE: - URB_Status = USBH_LL_GetURBState(phost, MTP_Handle->DataOutPipe); - - if(URB_Status == USBH_URB_DONE) - { - if(MTP_Handle->ptp.flags == PTP_DP_NODATA) - { - MTP_Handle->ptp.state = PTP_RESPONSE_STATE; - } - else if(MTP_Handle->ptp.flags == PTP_DP_SENDDATA) - { - MTP_Handle->ptp.state = PTP_DATA_OUT_PHASE_STATE; - } - else if(MTP_Handle->ptp.flags == PTP_DP_GETDATA) - { - MTP_Handle->ptp.state = PTP_DATA_IN_PHASE_STATE; - } - else - { - } - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - else if(URB_Status == USBH_URB_NOTREADY) - { - /* Resend Request */ - MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - else if(URB_Status == USBH_URB_STALL) - { - MTP_Handle->ptp.state = PTP_ERROR; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - else - { - } - break; - - case PTP_DATA_OUT_PHASE_STATE: - - USBH_BulkSendData (phost, - MTP_Handle->ptp.data_ptr, - MTP_Handle->DataOutEpSize , - MTP_Handle->DataOutPipe, - 1U); - - - MTP_Handle->ptp.state = PTP_DATA_OUT_PHASE_WAIT_STATE; - break; - - case PTP_DATA_OUT_PHASE_WAIT_STATE: - URB_Status = USBH_LL_GetURBState(phost, MTP_Handle->DataOutPipe); - - if(URB_Status == USBH_URB_DONE) - { - /* Adjust Data pointer and data length */ - if(MTP_Handle->ptp.data_length > MTP_Handle->DataOutEpSize) - { - MTP_Handle->ptp.data_ptr += MTP_Handle->DataOutEpSize; - MTP_Handle->ptp.data_length -= MTP_Handle->DataOutEpSize; - MTP_Handle->ptp.data_packet += MTP_Handle->DataOutEpSize; - - if(MTP_Handle->ptp.data_packet >= PTP_USB_BULK_PAYLOAD_LEN_READ) - { - PTP_BufferFullCallback (phost); - MTP_Handle->ptp.data_packet = 0U; - MTP_Handle->ptp.iteration++; - } - - } - else - { - MTP_Handle->ptp.data_length = 0U; - } - - /* More Data To be Sent */ - if(MTP_Handle->ptp.data_length > 0U) - { - USBH_BulkSendData (phost, - MTP_Handle->ptp.data_ptr, - MTP_Handle->DataOutEpSize , - MTP_Handle->DataOutPipe, - 1U); - } - else - { - /* If value was 0, and successful transfer, then change the state */ - MTP_Handle->ptp.state = PTP_RESPONSE_STATE; - } - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - - else if(URB_Status == USBH_URB_NOTREADY) - { - /* Resend same data */ - MTP_Handle->ptp.state = PTP_DATA_OUT_PHASE_STATE; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - - else if(URB_Status == USBH_URB_STALL) - { - MTP_Handle->ptp.state = PTP_ERROR; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - else - { - } - break; - - case PTP_DATA_IN_PHASE_STATE: - /* Send first packet */ - USBH_BulkReceiveData (phost, - MTP_Handle->ptp.data_ptr, - MTP_Handle->DataInEpSize, - MTP_Handle->DataInPipe); - - MTP_Handle->ptp.state = PTP_DATA_IN_PHASE_WAIT_STATE; - break; - - case PTP_DATA_IN_PHASE_WAIT_STATE: - URB_Status = USBH_LL_GetURBState(phost, MTP_Handle->DataInPipe); - - if(URB_Status == USBH_URB_DONE) - { - len = USBH_LL_GetLastXferSize (phost, MTP_Handle->DataInPipe); - - if( MTP_Handle->ptp.data_packet_counter++ == 0U) - { - /* This is the first packet; so retrieve exact data length from payload */ - MTP_Handle->ptp.data_length = *(uint32_t*)(void *)(MTP_Handle->ptp.data_ptr); - MTP_Handle->ptp.iteration = 0U; - } - - if((len >= MTP_Handle->DataInEpSize) && (MTP_Handle->ptp.data_length > 0U)) - { - MTP_Handle->ptp.data_ptr += len; - MTP_Handle->ptp.data_length -= len; - MTP_Handle->ptp.data_packet += len; - - if(MTP_Handle->ptp.data_packet >= PTP_USB_BULK_PAYLOAD_LEN_READ) - { - PTP_BufferFullCallback (phost); - MTP_Handle->ptp.data_packet = 0U; - MTP_Handle->ptp.iteration++; - } - - /* Continue receiving data*/ - USBH_BulkReceiveData (phost, - MTP_Handle->ptp.data_ptr, - MTP_Handle->DataInEpSize, - MTP_Handle->DataInPipe); - } - else - { - MTP_Handle->ptp.data_length -= len; - MTP_Handle->ptp.state = PTP_RESPONSE_STATE; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - } - else if(URB_Status == USBH_URB_STALL) - { - MTP_Handle->ptp.state = PTP_ERROR; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - else - { - } - break; - - case PTP_RESPONSE_STATE: - - USBH_BulkReceiveData (phost, - (uint8_t*)(void *)&(MTP_Handle->ptp.resp_container), - PTP_USB_BULK_REQ_RESP_MAX_LEN , - MTP_Handle->DataInPipe); - - MTP_Handle->ptp.state = PTP_RESPONSE_WAIT_STATE; - break; - - case PTP_RESPONSE_WAIT_STATE: - URB_Status = USBH_LL_GetURBState(phost, MTP_Handle->DataInPipe); - - if(URB_Status == USBH_URB_DONE) - { - USBH_PTP_GetResponse (phost, &ptp_container); - - if(ptp_container.Code == PTP_RC_OK) - { - status = USBH_OK; - } - else - { - status = USBH_FAIL; - } - MTP_Handle->ptp.req_state = PTP_REQ_SEND; - } - else if(URB_Status == USBH_URB_STALL) - { - MTP_Handle->ptp.state = PTP_ERROR; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_URB_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - else - { - } - break; - - case PTP_ERROR: - MTP_Handle->ptp.req_state = PTP_REQ_SEND; - break; - - default: - break; - } - return status; -} - -/** - * @brief USBH_PTP_OpenSession - * Open a new session - * @param phost: Host handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_PTP_SendRequest (USBH_HandleTypeDef *phost, PTP_ContainerTypedef *req) -{ - USBH_StatusTypeDef status = USBH_OK; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - - /* Clear PTP Data container*/ - USBH_memset(&(MTP_Handle->ptp.op_container), 0, sizeof(PTP_OpContainerTypedef)); - - /* build appropriate USB container */ - MTP_Handle->ptp.op_container.length = PTP_USB_BULK_REQ_LEN- (sizeof(uint32_t) * (5U - req->Nparam)); - MTP_Handle->ptp.op_container.type = PTP_USB_CONTAINER_COMMAND; - MTP_Handle->ptp.op_container.code = req->Code; - MTP_Handle->ptp.op_container.trans_id = req->Transaction_ID; - MTP_Handle->ptp.op_container.param1 = req->Param1; - MTP_Handle->ptp.op_container.param2 = req->Param2; - MTP_Handle->ptp.op_container.param3 = req->Param3; - MTP_Handle->ptp.op_container.param4 = req->Param4; - MTP_Handle->ptp.op_container.param5 = req->Param5; - - return status; -} - -/** - * @brief USBH_PTP_OpenSession - * Open a new session - * @param phost: Host handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_PTP_GetResponse (USBH_HandleTypeDef *phost, PTP_ContainerTypedef *resp) -{ - USBH_StatusTypeDef status = USBH_OK; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - - /* build an appropriate PTPContainer */ - resp->Code = MTP_Handle->ptp.resp_container.code; - resp->SessionID = MTP_Handle->ptp.session_id; - resp->Transaction_ID = MTP_Handle->ptp.resp_container.trans_id; - resp->Param1 = MTP_Handle->ptp.resp_container.param1; - resp->Param2 = MTP_Handle->ptp.resp_container.param2; - resp->Param3 = MTP_Handle->ptp.resp_container.param3; - resp->Param4 = MTP_Handle->ptp.resp_container.param4; - resp->Param5 = MTP_Handle->ptp.resp_container.param5; - - return status; -} - -/** - * @brief The function informs user that data buffer is full - * @param phost: host handle - * @retval None - */ -static void PTP_BufferFullCallback(USBH_HandleTypeDef *phost) -{ - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - - switch (MTP_Handle->ptp.data_container.code) - { - case PTP_OC_GetDeviceInfo: - PTP_DecodeDeviceInfo (phost, &(MTP_Handle->info.devinfo)); - break; - - case PTP_OC_GetPartialObject: - case PTP_OC_GetObject: - - /* first packet is in the PTP data payload buffer */ - if(MTP_Handle->ptp.iteration == 0U) - { - /* copy it to object */ - USBH_memcpy(MTP_Handle->ptp.object_ptr, MTP_Handle->ptp.data_container.payload.data, PTP_USB_BULK_PAYLOAD_LEN_READ); - - /* next packet should be directly copied to object */ - MTP_Handle->ptp.data_ptr = (MTP_Handle->ptp.object_ptr + PTP_USB_BULK_PAYLOAD_LEN_READ); - } - break; - - case PTP_OC_SendObject: - /* first packet is in the PTP data payload buffer */ - if(MTP_Handle->ptp.iteration == 0U) - { - /* next packet should be directly copied to object */ - MTP_Handle->ptp.data_ptr = (MTP_Handle->ptp.object_ptr + PTP_USB_BULK_PAYLOAD_LEN_READ); - } - break; - - default: - break; - - - } -} - -/** - * @brief PTP_GetDeviceInfo - * Gets device info dataset and fills deviceinfo structure. - * @param phost: Host handle - * @param dev_info: Device info structure - * @retval None - */ -static void PTP_DecodeDeviceInfo (USBH_HandleTypeDef *phost, PTP_DeviceInfoTypedef *dev_info) -{ - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - uint8_t *data = MTP_Handle->ptp.data_container.payload.data; - uint32_t totallen; - uint16_t len; - - /* Max device info is PTP_USB_BULK_HS_MAX_PACKET_LEN_READ */ - USBH_DbgLog (" MTP device info size exceeds internal buffer size.\ - only available data are decoded."); - - if(MTP_Handle->ptp.iteration == 0U) - { - dev_info->StandardVersion = LE16(&data[PTP_di_StandardVersion]); - dev_info->VendorExtensionID = LE32(&data[PTP_di_VendorExtensionID]); - dev_info->VendorExtensionVersion = LE16(&data[PTP_di_VendorExtensionVersion]); - PTP_GetString(dev_info->VendorExtensionDesc, &data[PTP_di_VendorExtensionDesc], &len); - - totallen = len * 2U + 1U; - dev_info->FunctionalMode = LE16(&data[PTP_di_FunctionalMode+totallen]); - dev_info->OperationsSupported_len = PTP_GetArray16 ((uint16_t *)(void *)&dev_info->OperationsSupported, - data, - PTP_di_OperationsSupported+totallen); - - totallen=totallen+dev_info->OperationsSupported_len*sizeof(uint16_t)+sizeof(uint32_t); - dev_info->EventsSupported_len = PTP_GetArray16 ((uint16_t *)(void *)&dev_info->EventsSupported, - data, - PTP_di_OperationsSupported+totallen); - - totallen=totallen+dev_info->EventsSupported_len*sizeof(uint16_t)+sizeof(uint32_t); - dev_info->DevicePropertiesSupported_len = PTP_GetArray16 ((uint16_t *)(void *)&dev_info->DevicePropertiesSupported, - data, - PTP_di_OperationsSupported+totallen); - - totallen=totallen+dev_info->DevicePropertiesSupported_len*sizeof(uint16_t)+sizeof(uint32_t); - - dev_info->CaptureFormats_len = PTP_GetArray16 ((uint16_t *)(void *)&dev_info->CaptureFormats, - data, - PTP_di_OperationsSupported+totallen); - - totallen=totallen+dev_info->CaptureFormats_len*sizeof(uint16_t)+sizeof(uint32_t); - dev_info->ImageFormats_len = PTP_GetArray16 ((uint16_t *)(void *)&dev_info->ImageFormats, - data, - PTP_di_OperationsSupported+totallen); - - totallen=totallen+dev_info->ImageFormats_len*sizeof(uint16_t)+sizeof(uint32_t); - PTP_GetString(dev_info->Manufacturer, &data[PTP_di_OperationsSupported+totallen], &len); - - totallen += len * 2U + 1U; - PTP_GetString(dev_info->Model, &data[PTP_di_OperationsSupported+totallen], &len); - - totallen += len * 2U + 1U; - PTP_GetString(dev_info->DeviceVersion, &data[PTP_di_OperationsSupported+totallen], &len); - - totallen += len * 2U + 1U; - PTP_GetString(dev_info->SerialNumber, &data[PTP_di_OperationsSupported+totallen], &len); - } -} - -/** - * @brief PTP_GetStorageIDs - * Gets Storage Ids and fills stor_ids structure. - * @param phost: Host handle - * @param stor_ids: Storage IDsstructure - * @retval None - */ -static void PTP_GetStorageIDs (USBH_HandleTypeDef *phost, PTP_StorageIDsTypedef *stor_ids) -{ - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - uint8_t *data = MTP_Handle->ptp.data_container.payload.data; - - stor_ids->n = PTP_GetArray32 (stor_ids->Storage, data, 0U); -} - - -/** - * @brief PTP_GetStorageInfo - * Gets Storage Info and fills stor_info structure. - * @param phost: Host handle - * @param stor_ids: Storage IDsstructure - * @retval None - */ -static void PTP_GetStorageInfo (USBH_HandleTypeDef *phost, uint32_t storage_id, PTP_StorageInfoTypedef *stor_info) -{ - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - uint8_t *data = MTP_Handle->ptp.data_container.payload.data; - - uint16_t len; - - stor_info->StorageType=LE16(&data[PTP_si_StorageType]); - stor_info->FilesystemType=LE16(&data[PTP_si_FilesystemType]); - stor_info->AccessCapability=LE16(&data[PTP_si_AccessCapability]); - stor_info->MaxCapability=LE64(&data[PTP_si_MaxCapability]); - stor_info->FreeSpaceInBytes=LE64(&data[PTP_si_FreeSpaceInBytes]); - stor_info->FreeSpaceInImages=LE32(&data[PTP_si_FreeSpaceInImages]); - - PTP_GetString(stor_info->StorageDescription, &data[PTP_si_StorageDescription], &len); - PTP_GetString(stor_info->VolumeLabel, &data[PTP_si_StorageDescription+len * 2U + 1U], &len); -} - -/** - * @brief PTP_GetObjectInfo - * Gets objectInfo and fills object_info structure. - * @param phost: Host handle - * @param object_info: object info structure - * @retval None - */ -static void PTP_GetObjectInfo (USBH_HandleTypeDef *phost, PTP_ObjectInfoTypedef *object_info) -{ - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - uint8_t *data = MTP_Handle->ptp.data_container.payload.data; - uint16_t filenamelen; - - object_info->StorageID = LE32(&data[PTP_oi_StorageID]); - object_info->ObjectFormat = LE16(&data[PTP_oi_ObjectFormat]); - object_info->ProtectionStatus = LE16(&data[PTP_oi_ProtectionStatus]); - object_info->ObjectCompressedSize = LE64(&data[PTP_oi_ObjectCompressedSize]); - - /* For Samsung Galaxy */ - if ((data[PTP_oi_filenamelen] == 0U) && (data[PTP_oi_filenamelen + 4U] != 0U)) - { - data += 4; - } - object_info->ThumbFormat=LE16(&data[PTP_oi_ThumbFormat]); - object_info->ThumbCompressedSize=LE32(&data[PTP_oi_ThumbCompressedSize]); - object_info->ThumbPixWidth=LE32(&data[PTP_oi_ThumbPixWidth]); - object_info->ThumbPixHeight=LE32(&data[PTP_oi_ThumbPixHeight]); - object_info->ImagePixWidth=LE32(&data[PTP_oi_ImagePixWidth]); - object_info->ImagePixHeight=LE32(&data[PTP_oi_ImagePixHeight]); - object_info->ImageBitDepth=LE32(&data[PTP_oi_ImageBitDepth]); - object_info->ParentObject=LE32(&data[PTP_oi_ParentObject]); - object_info->AssociationType=LE16(&data[PTP_oi_AssociationType]); - object_info->AssociationDesc=LE32(&data[PTP_oi_AssociationDesc]); - object_info->SequenceNumber=LE32(&data[PTP_oi_SequenceNumber]); - PTP_GetString(object_info->Filename, &data[PTP_oi_filenamelen], &filenamelen); -} - - -/** - * @brief PTP_GetObjectPropDesc - * Gets objectInfo and fills object_info structure. - * @param phost: Host handle - * @param opd: object prop descriptor structure - * @retval None - */ -static void PTP_GetObjectPropDesc (USBH_HandleTypeDef *phost, PTP_ObjectPropDescTypeDef *opd, uint32_t opdlen) -{ - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - uint8_t *data = MTP_Handle->ptp.data_container.payload.data; - uint32_t offset = 0U, i; - - opd->ObjectPropertyCode=LE16(&data[PTP_opd_ObjectPropertyCode]); - opd->DataType=LE16(&data[PTP_opd_DataType]); - opd->GetSet=*(uint8_t *)(&data[PTP_opd_GetSet]); - - offset = PTP_opd_FactoryDefaultValue; - PTP_GetDevicePropValue (phost, &offset, opdlen, &opd->FactoryDefaultValue, opd->DataType); - - opd->GroupCode=LE32(&data[offset]); - offset+=sizeof(uint32_t); - - opd->FormFlag=*(uint8_t *)(&data[offset]); - offset+=sizeof(uint8_t); - - switch (opd->FormFlag) - { - case PTP_OPFF_Range: - PTP_GetDevicePropValue(phost, &offset, opdlen, &opd->FORM.Range.MinimumValue, opd->DataType); - PTP_GetDevicePropValue(phost, &offset, opdlen, &opd->FORM.Range.MaximumValue, opd->DataType); - PTP_GetDevicePropValue(phost, &offset, opdlen, &opd->FORM.Range.StepSize, opd->DataType); - break; - - case PTP_OPFF_Enumeration: - - opd->FORM.Enum.NumberOfValues = LE16(&data[offset]); - offset+=sizeof(uint16_t); - - for (i = 0U; i < opd->FORM.Enum.NumberOfValues ; i++) - { - PTP_GetDevicePropValue(phost, &offset, opdlen, &opd->FORM.Enum.SupportedValue[i], opd->DataType); - } - break; - default: - break; - } -} - -/** - * @brief PTP_GetDevicePropValue - * Gets objectInfo and fills object_info structure. - * @param phost: Host handle - * @param opd: object prop descriptor structure - * @retval None - */ -static void PTP_GetDevicePropValue(USBH_HandleTypeDef *phost, - uint32_t *offset, - uint32_t total, - PTP_PropertyValueTypedef* value, - uint16_t datatype) -{ - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - uint8_t *data = MTP_Handle->ptp.data_container.payload.data; - uint16_t len; - switch (datatype) - { - case PTP_DTC_INT8: - value->i8 = *(int8_t *)(void *)&(data[*offset]); - *offset += 1U; - break; - case PTP_DTC_UINT8: - value->u8 = *(uint8_t *)&(data[*offset]); - *offset += 1U; - break; - case PTP_DTC_INT16: - value->i16 = *(int16_t *)(void *)&(data[*offset]); - *offset += 2U; - break; - case PTP_DTC_UINT16: - value->u16 = LE16(&(data[*offset])); - *offset += 2U; - break; - case PTP_DTC_INT32: - value->i32 = *(int32_t *)(void *)(&(data[*offset])); - *offset += 4U; - break; - case PTP_DTC_UINT32: - value->u32 = LE32(&(data[*offset])); - *offset += 4U; - break; - case PTP_DTC_INT64: - value->i64 = *(int64_t *)(void *)(&(data[*offset])); - *offset += 8U; - break; - case PTP_DTC_UINT64: - value->u64 = LE64(&(data[*offset])); - *offset += 8U; - break; - - case PTP_DTC_UINT128: - *offset += 16U; - break; - case PTP_DTC_INT128: - *offset += 16U; - break; - - case PTP_DTC_STR: - - PTP_GetString((uint8_t *)(void *)value->str, (uint8_t *)&(data[*offset]), &len); - *offset += len * 2U + 1U; - break; - default: - break; - } -} - - -/** - * @brief PTP_GetDevicePropValue - * Gets objectInfo and fills object_info structure. - * @param phost: Host handle - * @param opd: object prop descriptor structure - * @retval None - */ -static uint32_t PTP_GetObjectPropList (USBH_HandleTypeDef *phost, - MTP_PropertiesTypedef *props, - uint32_t len) -{ - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - uint8_t *data = MTP_Handle->ptp.data_container.payload.data; - uint32_t prop_count; - uint32_t offset = 0U, i; - - prop_count = LE32(data); - - - if (prop_count == 0U) - { - return 0U; - } - - data += sizeof(uint32_t); - len -= sizeof(uint32_t); - - for (i = 0U; i < prop_count; i++) - { - if (len <= 0U) - { - return 0U; - } - - props[i].ObjectHandle = LE32(data); - data += sizeof(uint32_t); - len -= sizeof(uint32_t); - - props[i].property = LE16(data); - data += sizeof(uint16_t); - len -= sizeof(uint16_t); - - props[i].datatype = LE16(data); - data += sizeof(uint16_t); - len -= sizeof(uint16_t); - - offset = 0U; - - PTP_GetDevicePropValue(phost, &offset, len, &props[i].propval, props[i].datatype); - - data += offset; - len -= offset; - } - - return prop_count; -} - -/** - * @brief PTP_GetString - * Gets SCII String from. - * @param str: ascii string - * @param data: Device info structure - * @retval None - */ -static void PTP_GetString (uint8_t *str, uint8_t* data, uint16_t *len) -{ - uint16_t strlength; - uint16_t idx; - - *len = data[0]; - strlength = (uint16_t)(2U * (uint32_t)data[0]); - data ++; /* Adjust the offset ignoring the String Len */ - - for (idx = 0U; idx < strlength; idx += 2U) - { - /* Copy Only the string and ignore the UNICODE ID, hence add the src */ - *str = data[idx]; - str++; - } - *str = 0U; /* mark end of string */ -} - -/** - * @brief PTP_GetString - * Gets SCII String from. - * @param str: ascii string - * @param data: Device info structure - * @retval None - */ - -static uint32_t PTP_GetArray16 (uint16_t *array, uint8_t *data, uint32_t offset) -{ - uint32_t size, idx = 0U; - - size=LE32(&data[offset]); - while (size > idx) - { - array[idx] = (uint16_t)data[offset + (sizeof(uint16_t)*(idx + 2U))]; - idx++; - } - return size; -} - -/** - * @brief PTP_GetString - * Gets SCII String from. - * @param str: ascii string - * @param data: Device info structure - * @retval None - */ - -static uint32_t PTP_GetArray32 (uint32_t *array, uint8_t *data, uint32_t offset) -{ - uint32_t size, idx = 0U; - - size = LE32(&data[offset]); - while (size > idx) - { - array[idx] = LE32(&data[offset+(sizeof(uint32_t)*(idx+1U))]); - idx++; - } - return size; -} - -/******************************************************************************* - - PTP Requests - -*******************************************************************************/ -/** - * @brief USBH_PTP_OpenSession - * Open a new session - * @param phost: Host handle - * @param session: Session ID (MUST BE > 0U) - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_PTP_OpenSession (USBH_HandleTypeDef *phost, uint32_t session) -{ - USBH_StatusTypeDef status = USBH_BUSY; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - PTP_ContainerTypedef ptp_container; - - switch(MTP_Handle->ptp.req_state) - { - case PTP_REQ_SEND: - - /* Init session params */ - MTP_Handle->ptp.transaction_id = 0x00000000U; - MTP_Handle->ptp.session_id = session; - MTP_Handle->ptp.flags = PTP_DP_NODATA; - - /* Fill operation request params */ - ptp_container.Code = PTP_OC_OpenSession; - ptp_container.SessionID = session; - ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++; - ptp_container.Param1 = session; - ptp_container.Nparam = 1U; - - /* convert request packet inti USB raw packet*/ - USBH_PTP_SendRequest (phost, &ptp_container); - - /* Setup State machine and start transfer */ - MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; - MTP_Handle->ptp.req_state = PTP_REQ_WAIT; - status = USBH_BUSY; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - case PTP_REQ_WAIT: - status = USBH_PTP_Process(phost); - break; - - default: - break; - } - return status; -} - -/** - * @brief USBH_PTP_GetDevicePropDesc - * Gets object partially - * @param phost: Host handle - * @param dev_info: Device info structure - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_PTP_GetDevicePropDesc (USBH_HandleTypeDef *phost, - uint16_t propcode, - PTP_DevicePropDescTypdef* devicepropertydesc) -{ - USBH_StatusTypeDef status = USBH_BUSY; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - PTP_ContainerTypedef ptp_container; - uint8_t *data = MTP_Handle->ptp.data_container.payload.data; - switch(MTP_Handle->ptp.req_state) - { - case PTP_REQ_SEND: - - /* Set operation request type */ - MTP_Handle->ptp.flags = PTP_DP_GETDATA; - MTP_Handle->ptp.data_ptr = (uint8_t *)(void *)&(MTP_Handle->ptp.data_container); - MTP_Handle->ptp.data_length = 0U; - MTP_Handle->ptp.data_packet_counter = 0U; - MTP_Handle->ptp.data_packet = 0U; - - /* Fill operation request params */ - ptp_container.Code = PTP_OC_GetDevicePropDesc; - ptp_container.SessionID = MTP_Handle->ptp.session_id; - ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++; - ptp_container.Param1 = propcode; - ptp_container.Nparam = 1U; - - /* convert request packet into USB raw packet*/ - USBH_PTP_SendRequest (phost, &ptp_container); - - /* Setup State machine and start transfer */ - MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; - MTP_Handle->ptp.req_state = PTP_REQ_WAIT; - status = USBH_BUSY; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - case PTP_REQ_WAIT: - status = USBH_PTP_Process(phost); - - if(status == USBH_OK) - { - devicepropertydesc->DevicePropertyCode = LE16(&data[PTP_dpd_DevicePropertyCode]); - devicepropertydesc->DataType = LE16(&data[PTP_dpd_DataType]); - devicepropertydesc->GetSet = *(uint8_t *)(&data[PTP_dpd_GetSet]); - devicepropertydesc->FormFlag = PTP_DPFF_None; - } - break; - - default: - break; - } - return status; -} -/** - * @brief USBH_PTP_GetDeviceInfo - * Gets device info dataset and fills deviceinfo structure. - * @param phost: Host handle - * @param dev_info: Device info structure - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_PTP_GetDeviceInfo (USBH_HandleTypeDef *phost, PTP_DeviceInfoTypedef *dev_info) -{ - USBH_StatusTypeDef status = USBH_BUSY; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - PTP_ContainerTypedef ptp_container; - - switch(MTP_Handle->ptp.req_state) - { - case PTP_REQ_SEND: - - /* Set operation request type */ - MTP_Handle->ptp.flags = PTP_DP_GETDATA; - MTP_Handle->ptp.data_ptr = (uint8_t *)(void *)&(MTP_Handle->ptp.data_container); - MTP_Handle->ptp.data_length = 0U; - MTP_Handle->ptp.data_packet_counter = 0U; - MTP_Handle->ptp.data_packet = 0U; - - /* Fill operation request params */ - ptp_container.Code = PTP_OC_GetDeviceInfo; - ptp_container.SessionID = MTP_Handle->ptp.session_id; - ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++; - ptp_container.Nparam = 0U; - - /* convert request packet inti USB raw packet*/ - USBH_PTP_SendRequest (phost, &ptp_container); - - /* Setup State machine and start transfer */ - MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; - MTP_Handle->ptp.req_state = PTP_REQ_WAIT; - status = USBH_BUSY; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - case PTP_REQ_WAIT: - status = USBH_PTP_Process(phost); - - if(status == USBH_OK) - { - PTP_DecodeDeviceInfo (phost, dev_info); - } - break; - - default: - break; - } - return status; -} - -/** - * @brief USBH_PTP_GetStorageIds - * Gets device info dataset and fills deviceinfo structure. - * @param phost: Host handle - * @param dev_info: Device info structure - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_PTP_GetStorageIds (USBH_HandleTypeDef *phost, PTP_StorageIDsTypedef *storage_ids) -{ - USBH_StatusTypeDef status = USBH_BUSY; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - PTP_ContainerTypedef ptp_container; - - switch(MTP_Handle->ptp.req_state) - { - case PTP_REQ_SEND: - - /* Set operation request type */ - MTP_Handle->ptp.flags = PTP_DP_GETDATA; - MTP_Handle->ptp.data_ptr = (uint8_t *)(void *)&(MTP_Handle->ptp.data_container); - MTP_Handle->ptp.data_length = 0U; - MTP_Handle->ptp.data_packet_counter = 0U; - MTP_Handle->ptp.data_packet = 0U; - - /* Fill operation request params */ - ptp_container.Code = PTP_OC_GetStorageIDs; - ptp_container.SessionID = MTP_Handle->ptp.session_id; - ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++; - ptp_container.Nparam = 0U; - - /* convert request packet inti USB raw packet*/ - USBH_PTP_SendRequest (phost, &ptp_container); - - /* Setup State machine and start transfer */ - MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; - MTP_Handle->ptp.req_state = PTP_REQ_WAIT; - status = USBH_BUSY; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - case PTP_REQ_WAIT: - status = USBH_PTP_Process(phost); - - if(status == USBH_OK) - { - PTP_GetStorageIDs (phost, storage_ids); - } - break; - - default: - break; - } - return status; -} - -/** - * @brief USBH_PTP_GetDeviceInfo - * Gets device info dataset and fills deviceinfo structure. - * @param phost: Host handle - * @param dev_info: Device info structure - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_PTP_GetStorageInfo (USBH_HandleTypeDef *phost, uint32_t storage_id, PTP_StorageInfoTypedef *storage_info) -{ - USBH_StatusTypeDef status = USBH_BUSY; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - PTP_ContainerTypedef ptp_container; - - switch(MTP_Handle->ptp.req_state) - { - case PTP_REQ_SEND: - - /* Set operation request type */ - MTP_Handle->ptp.flags = PTP_DP_GETDATA; - MTP_Handle->ptp.data_ptr = (uint8_t *)(void *)&(MTP_Handle->ptp.data_container); - MTP_Handle->ptp.data_length = 0U; - MTP_Handle->ptp.data_packet_counter = 0U; - MTP_Handle->ptp.data_packet = 0U; - - /* Fill operation request params */ - ptp_container.Code = PTP_OC_GetStorageInfo; - ptp_container.SessionID = MTP_Handle->ptp.session_id; - ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++; - ptp_container.Param1 = storage_id; - ptp_container.Nparam = 1U; - - /* convert request packet inti USB raw packet*/ - USBH_PTP_SendRequest (phost, &ptp_container); - - /* Setup State machine and start transfer */ - MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; - MTP_Handle->ptp.req_state = PTP_REQ_WAIT; - status = USBH_BUSY; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - case PTP_REQ_WAIT: - status = USBH_PTP_Process(phost); - - if(status == USBH_OK) - { - PTP_GetStorageInfo (phost, storage_id, storage_info); - } - break; - - default: - break; - } - return status; -} - -/** - * @brief USBH_PTP_GetNumObjects - * Gets device info dataset and fills deviceinfo structure. - * @param phost: Host handle - * @param dev_info: Device info structure - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_PTP_GetNumObjects (USBH_HandleTypeDef *phost, - uint32_t storage_id, - uint32_t objectformatcode, - uint32_t associationOH, - uint32_t* numobs) -{ - USBH_StatusTypeDef status = USBH_BUSY; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - PTP_ContainerTypedef ptp_container; - - switch(MTP_Handle->ptp.req_state) - { - case PTP_REQ_SEND: - - /* Set operation request type */ - MTP_Handle->ptp.flags = PTP_DP_NODATA; - - /* Fill operation request params */ - ptp_container.Code = PTP_OC_GetNumObjects; - ptp_container.SessionID = MTP_Handle->ptp.session_id; - ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++; - ptp_container.Param1 = storage_id; - ptp_container.Param2 = objectformatcode; - ptp_container.Param3 = associationOH; - ptp_container.Nparam = 3U; - - /* convert request packet into USB raw packet*/ - USBH_PTP_SendRequest (phost, &ptp_container); - - /* Setup State machine and start transfer */ - MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; - MTP_Handle->ptp.req_state = PTP_REQ_WAIT; - status = USBH_BUSY; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - case PTP_REQ_WAIT: - status = USBH_PTP_Process(phost); - - if(status == USBH_OK) - { - *numobs = MTP_Handle->ptp.resp_container.param1; - } - break; - - default: - break; - } - return status; -} - -/** - * @brief USBH_PTP_GetObjectHandles - * Gets device info dataset and fills deviceinfo structure. - * @param phost: Host handle - * @param dev_info: Device info structure - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_PTP_GetObjectHandles (USBH_HandleTypeDef *phost, - uint32_t storage_id, - uint32_t objectformatcode, - uint32_t associationOH, - PTP_ObjectHandlesTypedef* objecthandles) -{ - USBH_StatusTypeDef status = USBH_BUSY; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - PTP_ContainerTypedef ptp_container; - - switch(MTP_Handle->ptp.req_state) - { - case PTP_REQ_SEND: - - /* Set operation request type */ - MTP_Handle->ptp.flags = PTP_DP_GETDATA; - MTP_Handle->ptp.data_ptr = (uint8_t *)(void *)&(MTP_Handle->ptp.data_container); - MTP_Handle->ptp.data_length = 0U; - MTP_Handle->ptp.data_packet_counter = 0U; - MTP_Handle->ptp.data_packet = 0U; - - /* Fill operation request params */ - ptp_container.Code = PTP_OC_GetObjectHandles; - ptp_container.SessionID = MTP_Handle->ptp.session_id; - ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id++; - ptp_container.Param1 = storage_id; - ptp_container.Param2 = objectformatcode; - ptp_container.Param3 = associationOH; - ptp_container.Nparam = 3U; - - /* convert request packet into USB raw packet*/ - USBH_PTP_SendRequest (phost, &ptp_container); - - /* Setup State machine and start transfer */ - MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; - MTP_Handle->ptp.req_state = PTP_REQ_WAIT; - status = USBH_BUSY; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - case PTP_REQ_WAIT: - status = USBH_PTP_Process(phost); - - if(status == USBH_OK) - { - objecthandles->n = PTP_GetArray32 (objecthandles->Handler, - MTP_Handle->ptp.data_container.payload.data, - 0U); - } - break; - - default: - break; - } - return status; -} - -/** - * @brief USBH_PTP_GetObjectInfo - * Gets objert info - * @param phost: Host handle - * @param dev_info: Device info structure - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_PTP_GetObjectInfo (USBH_HandleTypeDef *phost, - uint32_t handle, - PTP_ObjectInfoTypedef* object_info) -{ - USBH_StatusTypeDef status = USBH_BUSY; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - PTP_ContainerTypedef ptp_container; - - switch(MTP_Handle->ptp.req_state) - { - case PTP_REQ_SEND: - - /* Set operation request type */ - MTP_Handle->ptp.flags = PTP_DP_GETDATA; - MTP_Handle->ptp.data_ptr = (uint8_t *)(void *)&(MTP_Handle->ptp.data_container); - MTP_Handle->ptp.data_length = 0U; - MTP_Handle->ptp.data_packet_counter = 0U; - MTP_Handle->ptp.data_packet = 0U; - - /* Fill operation request params */ - ptp_container.Code = PTP_OC_GetObjectInfo; - ptp_container.SessionID = MTP_Handle->ptp.session_id; - ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++; - ptp_container.Param1 = handle; - ptp_container.Nparam = 1U; - - /* convert request packet into USB raw packet*/ - USBH_PTP_SendRequest (phost, &ptp_container); - - /* Setup State machine and start transfer */ - MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; - MTP_Handle->ptp.req_state = PTP_REQ_WAIT; - status = USBH_BUSY; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - case PTP_REQ_WAIT: - status = USBH_PTP_Process(phost); - - if(status == USBH_OK) - { - PTP_GetObjectInfo (phost, object_info); - } - break; - - default: - break; - } - return status; -} - -/** - * @brief USBH_PTP_DeleteObject - * Delete an object. - * @param phost: Host handle - * @param handle : Object Handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_PTP_DeleteObject (USBH_HandleTypeDef *phost, - uint32_t handle, - uint32_t objectformatcode) -{ - USBH_StatusTypeDef status = USBH_BUSY; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - PTP_ContainerTypedef ptp_container; - - switch(MTP_Handle->ptp.req_state) - { - case PTP_REQ_SEND: - - /* Set operation request type */ - MTP_Handle->ptp.flags = PTP_DP_NODATA; - - /* Fill operation request params */ - ptp_container.Code = PTP_OC_DeleteObject; - ptp_container.SessionID = MTP_Handle->ptp.session_id; - ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++; - ptp_container.Param1 = handle; - ptp_container.Param2 = objectformatcode; - ptp_container.Nparam = 2U; - - /* convert request packet into USB raw packet*/ - USBH_PTP_SendRequest (phost, &ptp_container); - - /* Setup State machine and start transfer */ - MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; - MTP_Handle->ptp.req_state = PTP_REQ_WAIT; - status = USBH_BUSY; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - case PTP_REQ_WAIT: - status = USBH_PTP_Process(phost); - break; - - default: - break; - } - return status; -} - -/** - * @brief USBH_PTP_GetObject - * Gets object - * @param phost: Host handle - * @param dev_info: Device info structure - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_PTP_GetObject (USBH_HandleTypeDef *phost, - uint32_t handle, - uint8_t *object) -{ - USBH_StatusTypeDef status = USBH_BUSY; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - PTP_ContainerTypedef ptp_container; - - switch(MTP_Handle->ptp.req_state) - { - case PTP_REQ_SEND: - - /* Set operation request type */ - MTP_Handle->ptp.flags = PTP_DP_GETDATA; - MTP_Handle->ptp.data_ptr = (uint8_t *)(void *)&(MTP_Handle->ptp.data_container); - MTP_Handle->ptp.data_length = 0U; - MTP_Handle->ptp.data_packet_counter = 0U; - MTP_Handle->ptp.data_packet = 0U; - - /* set object control params */ - MTP_Handle->ptp.object_ptr = object; - - /* Fill operation request params */ - ptp_container.Code = PTP_OC_GetObject; - ptp_container.SessionID = MTP_Handle->ptp.session_id; - ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++; - ptp_container.Param1 = handle; - ptp_container.Nparam = 1U; - - - /* convert request packet into USB raw packet*/ - USBH_PTP_SendRequest (phost, &ptp_container); - - /* Setup State machine and start transfer */ - MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; - MTP_Handle->ptp.req_state = PTP_REQ_WAIT; - status = USBH_BUSY; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - case PTP_REQ_WAIT: - status = USBH_PTP_Process(phost); - - if(status == USBH_OK) - { - /* first packet is in the PTP data payload buffer */ - if(MTP_Handle->ptp.iteration == 0U) - { - /* copy it to object */ - USBH_memcpy(MTP_Handle->ptp.object_ptr, MTP_Handle->ptp.data_container.payload.data, PTP_USB_BULK_PAYLOAD_LEN_READ); - } - } - break; - - default: - break; - } - return status; -} - -/** - * @brief USBH_PTP_GetPartialObject - * Gets object partially - * @param phost: Host handle - * @param dev_info: Device info structure - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_PTP_GetPartialObject(USBH_HandleTypeDef *phost, - uint32_t handle, - uint32_t offset, - uint32_t maxbytes, - uint8_t *object, - uint32_t *len) -{ - USBH_StatusTypeDef status = USBH_BUSY; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - PTP_ContainerTypedef ptp_container; - - switch(MTP_Handle->ptp.req_state) - { - case PTP_REQ_SEND: - - /* Set operation request type */ - MTP_Handle->ptp.flags = PTP_DP_GETDATA; - MTP_Handle->ptp.data_ptr = (uint8_t *)(void *)&(MTP_Handle->ptp.data_container); - MTP_Handle->ptp.data_length = 0U; - MTP_Handle->ptp.data_packet_counter = 0U; - MTP_Handle->ptp.data_packet = 0U; - - /* set object control params */ - MTP_Handle->ptp.object_ptr = object; - - /* Fill operation request params */ - ptp_container.Code = PTP_OC_GetPartialObject; - ptp_container.SessionID = MTP_Handle->ptp.session_id; - ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++; - ptp_container.Param1 = handle; - ptp_container.Param2 = offset; - ptp_container.Param3 = maxbytes; - ptp_container.Nparam = 3U; - - /* convert request packet into USB raw packet*/ - USBH_PTP_SendRequest (phost, &ptp_container); - - /* Setup State machine and start transfer */ - MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; - MTP_Handle->ptp.req_state = PTP_REQ_WAIT; - status = USBH_BUSY; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - case PTP_REQ_WAIT: - status = USBH_PTP_Process(phost); - - if(status == USBH_OK) - { - *len = MTP_Handle->ptp.resp_container.param1; - /* first packet is in the PTP data payload buffer */ - if(MTP_Handle->ptp.iteration == 0U) - { - /* copy it to object */ - USBH_memcpy(MTP_Handle->ptp.object_ptr, MTP_Handle->ptp.data_container.payload.data, *len); - } - } - break; - - default: - break; - } - return status; -} - -/** - * @brief USBH_PTP_GetObjectPropsSupported - * Gets object partially - * @param phost: Host handle - * @param dev_info: Device info structure - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_PTP_GetObjectPropsSupported (USBH_HandleTypeDef *phost, - uint16_t ofc, - uint32_t *propnum, - uint16_t *props) -{ - USBH_StatusTypeDef status = USBH_BUSY; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - PTP_ContainerTypedef ptp_container; - - switch(MTP_Handle->ptp.req_state) - { - case PTP_REQ_SEND: - - /* Set operation request type */ - MTP_Handle->ptp.flags = PTP_DP_GETDATA; - MTP_Handle->ptp.data_ptr = (uint8_t *)(void *)&(MTP_Handle->ptp.data_container); - MTP_Handle->ptp.data_length = 0U; - MTP_Handle->ptp.data_packet_counter = 0U; - MTP_Handle->ptp.data_packet = 0U; - - /* Fill operation request params */ - ptp_container.Code = PTP_OC_GetObjectPropsSupported; - ptp_container.SessionID = MTP_Handle->ptp.session_id; - ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++; - ptp_container.Param1 = ofc; - ptp_container.Nparam = 1U; - - /* convert request packet into USB raw packet*/ - USBH_PTP_SendRequest (phost, &ptp_container); - - /* Setup State machine and start transfer */ - MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; - MTP_Handle->ptp.req_state = PTP_REQ_WAIT; - status = USBH_BUSY; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - case PTP_REQ_WAIT: - status = USBH_PTP_Process(phost); - - if(status == USBH_OK) - { - *propnum = PTP_GetArray16 (props, MTP_Handle->ptp.data_container.payload.data, 0U); - } - break; - - default: - break; - } - return status; -} - -/** - * @brief USBH_PTP_GetObjectPropDesc - * Gets object partially - * @param phost: Host handle - * @param dev_info: Device info structure - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_PTP_GetObjectPropDesc (USBH_HandleTypeDef *phost, - uint16_t opc, - uint16_t ofc, - PTP_ObjectPropDescTypeDef *opd) -{ - USBH_StatusTypeDef status = USBH_BUSY; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - PTP_ContainerTypedef ptp_container; - - switch(MTP_Handle->ptp.req_state) - { - case PTP_REQ_SEND: - - /* Set operation request type */ - MTP_Handle->ptp.flags = PTP_DP_GETDATA; - MTP_Handle->ptp.data_ptr = (uint8_t *)(void *)&(MTP_Handle->ptp.data_container); - MTP_Handle->ptp.data_length = 0U; - MTP_Handle->ptp.data_packet_counter = 0U; - MTP_Handle->ptp.data_packet = 0U; - - /* Fill operation request params */ - ptp_container.Code = PTP_OC_GetObjectPropDesc; - ptp_container.SessionID = MTP_Handle->ptp.session_id; - ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++; - ptp_container.Param1 = opc; - ptp_container.Param2 = ofc; - ptp_container.Nparam = 2U; - - /* convert request packet into USB raw packet*/ - USBH_PTP_SendRequest (phost, &ptp_container); - - /* Setup State machine and start transfer */ - MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; - MTP_Handle->ptp.req_state = PTP_REQ_WAIT; - status = USBH_BUSY; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - case PTP_REQ_WAIT: - status = USBH_PTP_Process(phost); - - if(status == USBH_OK) - { - PTP_GetObjectPropDesc(phost, opd, MTP_Handle->ptp.data_length); - } - break; - - default: - break; - } - return status; -} - -/** - * @brief USBH_PTP_GetObjectPropList - * Gets object partially - * @param phost: Host handle - * @param dev_info: Device info structure - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_PTP_GetObjectPropList (USBH_HandleTypeDef *phost, - uint32_t handle, - MTP_PropertiesTypedef *pprops, - uint32_t *nrofprops) -{ - USBH_StatusTypeDef status = USBH_BUSY; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - PTP_ContainerTypedef ptp_container; - - switch(MTP_Handle->ptp.req_state) - { - case PTP_REQ_SEND: - - /* Set operation request type */ - MTP_Handle->ptp.flags = PTP_DP_GETDATA; - MTP_Handle->ptp.data_ptr = (uint8_t *)(void *)&(MTP_Handle->ptp.data_container); - MTP_Handle->ptp.data_length = 0U; - MTP_Handle->ptp.data_packet_counter = 0U; - MTP_Handle->ptp.data_packet = 0U; - - /* copy first packet of the object into data container */ - USBH_memcpy(MTP_Handle->ptp.data_container.payload.data, MTP_Handle->ptp.object_ptr, PTP_USB_BULK_PAYLOAD_LEN_READ); - - /* Fill operation request params */ - ptp_container.Code = PTP_OC_GetObjPropList; - ptp_container.SessionID = MTP_Handle->ptp.session_id; - ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++; - ptp_container.Param1 = handle; - ptp_container.Param2 = 0x00000000U; /* 0x00000000U should be "all formats" */ - ptp_container.Param3 = 0xFFFFFFFFU; /* 0xFFFFFFFFU should be "all properties" */ - ptp_container.Param4 = 0x00000000U; - ptp_container.Param5 = 0xFFFFFFFFU; /* Return full tree below the Param1 handle */ - ptp_container.Nparam = 5U; - - /* convert request packet into USB raw packet*/ - USBH_PTP_SendRequest (phost, &ptp_container); - - /* Setup State machine and start transfer */ - MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; - MTP_Handle->ptp.req_state = PTP_REQ_WAIT; - status = USBH_BUSY; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - case PTP_REQ_WAIT: - status = USBH_PTP_Process(phost); - - if(status == USBH_OK) - { - PTP_GetObjectPropList (phost, pprops, MTP_Handle->ptp.data_length); - } - break; - - default: - break; - } - return status; -} - -/** - * @brief USBH_PTP_SendObject - * Send an object - * @param phost: Host handle - * @param dev_info: Device info structure - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_PTP_SendObject (USBH_HandleTypeDef *phost, - uint32_t handle, - uint8_t *object, - uint32_t size) -{ - USBH_StatusTypeDef status = USBH_BUSY; - MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; - PTP_ContainerTypedef ptp_container; - - switch(MTP_Handle->ptp.req_state) - { - case PTP_REQ_SEND: - - /* Set operation request type */ - MTP_Handle->ptp.flags = PTP_DP_SENDDATA; - MTP_Handle->ptp.data_ptr = (uint8_t *)(void *)&(MTP_Handle->ptp.data_container); - MTP_Handle->ptp.data_packet_counter = 0U; - MTP_Handle->ptp.data_packet = 0U; - MTP_Handle->ptp.iteration = 0U; - - /* set object control params */ - MTP_Handle->ptp.object_ptr = object; - MTP_Handle->ptp.data_length = size; - - /* Fill operation request params */ - ptp_container.Code = PTP_OC_SendObject; - ptp_container.SessionID = MTP_Handle->ptp.session_id; - ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++; - ptp_container.Nparam = 0U; - - - /* convert request packet into USB raw packet*/ - USBH_PTP_SendRequest (phost, &ptp_container); - - /* Setup State machine and start transfer */ - MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; - MTP_Handle->ptp.req_state = PTP_REQ_WAIT; - status = USBH_BUSY; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - case PTP_REQ_WAIT: - status = USBH_PTP_Process(phost); - break; - - default: - break; - } - return status; -} -/** -* @} -*/ - -/** -* @} -*/ - -/** -* @} -*/ - -/** -* @} -*/ - -/** -* @} -*/ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - - - +/** + ****************************************************************************** + * @file usbh_mtp_ptp.c + * @author MCD Application Team + * @brief This file includes the PTP operations layer + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + + /* BSPDependencies + - "stm32xxxxx_{eval}{discovery}.c" + - "stm32xxxxx_{eval}{discovery}_io.c" + - "stm32xxxxx_{eval}{discovery}_audio.c" + - "stm32xxxxx_{eval}{discovery}_sd.c" + - "stm32xxxxx_{eval}{discovery}_lcd.c" + - "stm32xxxxx_{eval}{discovery}_sdram.c" + EndBSPDependencies */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_mtp_ptp.h" +#include "usbh_mtp.h" +/** @addtogroup USBH_LIB +* @{ +*/ + +/** @addtogroup USBH_CLASS +* @{ +*/ + +/** @addtogroup USBH_MTP_CLASS +* @{ +*/ + +/** @defgroup USBH_MTP_PTP +* @brief This file includes the mass storage related functions +* @{ +*/ + + +/** @defgroup USBH_MTP_PTP_Private_TypesDefinitions +* @{ +*/ +/** +* @} +*/ + +/** @defgroup USBH_MTP_PTP_Private_Defines +* @{ +*/ +/** +* @} +*/ + +/** @defgroup USBH_MTP_PTP_Private_Macros +* @{ +*/ +/** +* @} +*/ + + +/** @defgroup USBH_MTP_PTP_Private_Variables +* @{ +*/ + +/** +* @} +*/ + + +/** @defgroup USBH_MTP_PTP_Private_FunctionPrototypes +* @{ +*/ +static void PTP_DecodeDeviceInfo (USBH_HandleTypeDef *phost, PTP_DeviceInfoTypedef *dev_info); +static void PTP_GetStorageIDs (USBH_HandleTypeDef *phost, PTP_StorageIDsTypedef *stor_ids); +static void PTP_GetStorageInfo (USBH_HandleTypeDef *phost, uint32_t storage_id, PTP_StorageInfoTypedef *stor_info); +static void PTP_GetObjectInfo (USBH_HandleTypeDef *phost, PTP_ObjectInfoTypedef *object_info); +static void PTP_GetObjectPropDesc (USBH_HandleTypeDef *phost, PTP_ObjectPropDescTypeDef *opd, uint32_t opdlen); +static void PTP_DecodeDeviceInfo (USBH_HandleTypeDef *phost, PTP_DeviceInfoTypedef *dev_info); +static void PTP_GetDevicePropValue(USBH_HandleTypeDef *phost, + uint32_t *offset, + uint32_t total, + PTP_PropertyValueTypedef* value, + uint16_t datatype); + +static uint32_t PTP_GetObjectPropList (USBH_HandleTypeDef *phost, + MTP_PropertiesTypedef *props, + uint32_t len); + + +static void PTP_BufferFullCallback(USBH_HandleTypeDef *phost); +static void PTP_GetString(uint8_t *str, uint8_t* data, uint16_t *len); +static uint32_t PTP_GetArray16 (uint16_t *array, uint8_t *data, uint32_t offset); +static uint32_t PTP_GetArray32 (uint32_t *array, uint8_t *data, uint32_t offset); +/** +* @} +*/ + + +/** @defgroup USBH_MTP_PTP_Exported_Variables +* @{ +*/ +/** +* @} +*/ + + +/** @defgroup USBH_MTP_PTP_Private_Functions +* @{ +*/ +/** + * @brief USBH_PTP_Init + * The function Initializes the BOT protocol. + * @param phost: Host handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_PTP_Init(USBH_HandleTypeDef *phost) +{ + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + + /* Set state to idle to be ready for operations */ + MTP_Handle->ptp.state = PTP_IDLE; + MTP_Handle->ptp.req_state = PTP_REQ_SEND; + + return USBH_OK; +} + +/** + * @brief USBH_PTP_Process + * The function handle the BOT protocol. + * @param phost: Host handle + * @param lun: Logical Unit Number + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_PTP_Process (USBH_HandleTypeDef *phost) +{ + USBH_StatusTypeDef status = USBH_BUSY; + USBH_URBStateTypeDef URB_Status = USBH_URB_IDLE; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + PTP_ContainerTypedef ptp_container; + uint32_t len; + + switch (MTP_Handle->ptp.state) + { + case PTP_IDLE: + /*Do Nothing */ + break; + + case PTP_OP_REQUEST_STATE: + USBH_BulkSendData (phost, + (uint8_t*)(void *)&(MTP_Handle->ptp.op_container), + (uint16_t)MTP_Handle->ptp.op_container.length, + MTP_Handle->DataOutPipe, + 1U); + MTP_Handle->ptp.state = PTP_OP_REQUEST_WAIT_STATE; + break; + + case PTP_OP_REQUEST_WAIT_STATE: + URB_Status = USBH_LL_GetURBState(phost, MTP_Handle->DataOutPipe); + + if(URB_Status == USBH_URB_DONE) + { + if(MTP_Handle->ptp.flags == PTP_DP_NODATA) + { + MTP_Handle->ptp.state = PTP_RESPONSE_STATE; + } + else if(MTP_Handle->ptp.flags == PTP_DP_SENDDATA) + { + MTP_Handle->ptp.state = PTP_DATA_OUT_PHASE_STATE; + } + else if(MTP_Handle->ptp.flags == PTP_DP_GETDATA) + { + MTP_Handle->ptp.state = PTP_DATA_IN_PHASE_STATE; + } + else + { + } + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + else if(URB_Status == USBH_URB_NOTREADY) + { + /* Resend Request */ + MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + else if(URB_Status == USBH_URB_STALL) + { + MTP_Handle->ptp.state = PTP_ERROR; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + else + { + } + break; + + case PTP_DATA_OUT_PHASE_STATE: + + USBH_BulkSendData (phost, + MTP_Handle->ptp.data_ptr, + MTP_Handle->DataOutEpSize , + MTP_Handle->DataOutPipe, + 1U); + + + MTP_Handle->ptp.state = PTP_DATA_OUT_PHASE_WAIT_STATE; + break; + + case PTP_DATA_OUT_PHASE_WAIT_STATE: + URB_Status = USBH_LL_GetURBState(phost, MTP_Handle->DataOutPipe); + + if(URB_Status == USBH_URB_DONE) + { + /* Adjust Data pointer and data length */ + if(MTP_Handle->ptp.data_length > MTP_Handle->DataOutEpSize) + { + MTP_Handle->ptp.data_ptr += MTP_Handle->DataOutEpSize; + MTP_Handle->ptp.data_length -= MTP_Handle->DataOutEpSize; + MTP_Handle->ptp.data_packet += MTP_Handle->DataOutEpSize; + + if(MTP_Handle->ptp.data_packet >= PTP_USB_BULK_PAYLOAD_LEN_READ) + { + PTP_BufferFullCallback (phost); + MTP_Handle->ptp.data_packet = 0U; + MTP_Handle->ptp.iteration++; + } + + } + else + { + MTP_Handle->ptp.data_length = 0U; + } + + /* More Data To be Sent */ + if(MTP_Handle->ptp.data_length > 0U) + { + USBH_BulkSendData (phost, + MTP_Handle->ptp.data_ptr, + MTP_Handle->DataOutEpSize , + MTP_Handle->DataOutPipe, + 1U); + } + else + { + /* If value was 0, and successful transfer, then change the state */ + MTP_Handle->ptp.state = PTP_RESPONSE_STATE; + } + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + + else if(URB_Status == USBH_URB_NOTREADY) + { + /* Resend same data */ + MTP_Handle->ptp.state = PTP_DATA_OUT_PHASE_STATE; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + + else if(URB_Status == USBH_URB_STALL) + { + MTP_Handle->ptp.state = PTP_ERROR; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + else + { + } + break; + + case PTP_DATA_IN_PHASE_STATE: + /* Send first packet */ + USBH_BulkReceiveData (phost, + MTP_Handle->ptp.data_ptr, + MTP_Handle->DataInEpSize, + MTP_Handle->DataInPipe); + + MTP_Handle->ptp.state = PTP_DATA_IN_PHASE_WAIT_STATE; + break; + + case PTP_DATA_IN_PHASE_WAIT_STATE: + URB_Status = USBH_LL_GetURBState(phost, MTP_Handle->DataInPipe); + + if(URB_Status == USBH_URB_DONE) + { + len = USBH_LL_GetLastXferSize (phost, MTP_Handle->DataInPipe); + + if( MTP_Handle->ptp.data_packet_counter++ == 0U) + { + /* This is the first packet; so retrieve exact data length from payload */ + MTP_Handle->ptp.data_length = *(uint32_t*)(void *)(MTP_Handle->ptp.data_ptr); + MTP_Handle->ptp.iteration = 0U; + } + + if((len >= MTP_Handle->DataInEpSize) && (MTP_Handle->ptp.data_length > 0U)) + { + MTP_Handle->ptp.data_ptr += len; + MTP_Handle->ptp.data_length -= len; + MTP_Handle->ptp.data_packet += len; + + if(MTP_Handle->ptp.data_packet >= PTP_USB_BULK_PAYLOAD_LEN_READ) + { + PTP_BufferFullCallback (phost); + MTP_Handle->ptp.data_packet = 0U; + MTP_Handle->ptp.iteration++; + } + + /* Continue receiving data*/ + USBH_BulkReceiveData (phost, + MTP_Handle->ptp.data_ptr, + MTP_Handle->DataInEpSize, + MTP_Handle->DataInPipe); + } + else + { + MTP_Handle->ptp.data_length -= len; + MTP_Handle->ptp.state = PTP_RESPONSE_STATE; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + } + else if(URB_Status == USBH_URB_STALL) + { + MTP_Handle->ptp.state = PTP_ERROR; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + else + { + } + break; + + case PTP_RESPONSE_STATE: + + USBH_BulkReceiveData (phost, + (uint8_t*)(void *)&(MTP_Handle->ptp.resp_container), + PTP_USB_BULK_REQ_RESP_MAX_LEN , + MTP_Handle->DataInPipe); + + MTP_Handle->ptp.state = PTP_RESPONSE_WAIT_STATE; + break; + + case PTP_RESPONSE_WAIT_STATE: + URB_Status = USBH_LL_GetURBState(phost, MTP_Handle->DataInPipe); + + if(URB_Status == USBH_URB_DONE) + { + USBH_PTP_GetResponse (phost, &ptp_container); + + if(ptp_container.Code == PTP_RC_OK) + { + status = USBH_OK; + } + else + { + status = USBH_FAIL; + } + MTP_Handle->ptp.req_state = PTP_REQ_SEND; + } + else if(URB_Status == USBH_URB_STALL) + { + MTP_Handle->ptp.state = PTP_ERROR; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_URB_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + else + { + } + break; + + case PTP_ERROR: + MTP_Handle->ptp.req_state = PTP_REQ_SEND; + break; + + default: + break; + } + return status; +} + +/** + * @brief USBH_PTP_OpenSession + * Open a new session + * @param phost: Host handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_PTP_SendRequest (USBH_HandleTypeDef *phost, PTP_ContainerTypedef *req) +{ + USBH_StatusTypeDef status = USBH_OK; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + + /* Clear PTP Data container*/ + USBH_memset(&(MTP_Handle->ptp.op_container), 0, sizeof(PTP_OpContainerTypedef)); + + /* build appropriate USB container */ + MTP_Handle->ptp.op_container.length = PTP_USB_BULK_REQ_LEN- (sizeof(uint32_t) * (5U - req->Nparam)); + MTP_Handle->ptp.op_container.type = PTP_USB_CONTAINER_COMMAND; + MTP_Handle->ptp.op_container.code = req->Code; + MTP_Handle->ptp.op_container.trans_id = req->Transaction_ID; + MTP_Handle->ptp.op_container.param1 = req->Param1; + MTP_Handle->ptp.op_container.param2 = req->Param2; + MTP_Handle->ptp.op_container.param3 = req->Param3; + MTP_Handle->ptp.op_container.param4 = req->Param4; + MTP_Handle->ptp.op_container.param5 = req->Param5; + + return status; +} + +/** + * @brief USBH_PTP_OpenSession + * Open a new session + * @param phost: Host handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_PTP_GetResponse (USBH_HandleTypeDef *phost, PTP_ContainerTypedef *resp) +{ + USBH_StatusTypeDef status = USBH_OK; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + + /* build an appropriate PTPContainer */ + resp->Code = MTP_Handle->ptp.resp_container.code; + resp->SessionID = MTP_Handle->ptp.session_id; + resp->Transaction_ID = MTP_Handle->ptp.resp_container.trans_id; + resp->Param1 = MTP_Handle->ptp.resp_container.param1; + resp->Param2 = MTP_Handle->ptp.resp_container.param2; + resp->Param3 = MTP_Handle->ptp.resp_container.param3; + resp->Param4 = MTP_Handle->ptp.resp_container.param4; + resp->Param5 = MTP_Handle->ptp.resp_container.param5; + + return status; +} + +/** + * @brief The function informs user that data buffer is full + * @param phost: host handle + * @retval None + */ +static void PTP_BufferFullCallback(USBH_HandleTypeDef *phost) +{ + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + + switch (MTP_Handle->ptp.data_container.code) + { + case PTP_OC_GetDeviceInfo: + PTP_DecodeDeviceInfo (phost, &(MTP_Handle->info.devinfo)); + break; + + case PTP_OC_GetPartialObject: + case PTP_OC_GetObject: + + /* first packet is in the PTP data payload buffer */ + if(MTP_Handle->ptp.iteration == 0U) + { + /* copy it to object */ + USBH_memcpy(MTP_Handle->ptp.object_ptr, MTP_Handle->ptp.data_container.payload.data, PTP_USB_BULK_PAYLOAD_LEN_READ); + + /* next packet should be directly copied to object */ + MTP_Handle->ptp.data_ptr = (MTP_Handle->ptp.object_ptr + PTP_USB_BULK_PAYLOAD_LEN_READ); + } + break; + + case PTP_OC_SendObject: + /* first packet is in the PTP data payload buffer */ + if(MTP_Handle->ptp.iteration == 0U) + { + /* next packet should be directly copied to object */ + MTP_Handle->ptp.data_ptr = (MTP_Handle->ptp.object_ptr + PTP_USB_BULK_PAYLOAD_LEN_READ); + } + break; + + default: + break; + + + } +} + +/** + * @brief PTP_GetDeviceInfo + * Gets device info dataset and fills deviceinfo structure. + * @param phost: Host handle + * @param dev_info: Device info structure + * @retval None + */ +static void PTP_DecodeDeviceInfo (USBH_HandleTypeDef *phost, PTP_DeviceInfoTypedef *dev_info) +{ + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + uint8_t *data = MTP_Handle->ptp.data_container.payload.data; + uint32_t totallen; + uint16_t len; + + /* Max device info is PTP_USB_BULK_HS_MAX_PACKET_LEN_READ */ + USBH_DbgLog (" MTP device info size exceeds internal buffer size.\ + only available data are decoded."); + + if(MTP_Handle->ptp.iteration == 0U) + { + dev_info->StandardVersion = LE16(&data[PTP_di_StandardVersion]); + dev_info->VendorExtensionID = LE32(&data[PTP_di_VendorExtensionID]); + dev_info->VendorExtensionVersion = LE16(&data[PTP_di_VendorExtensionVersion]); + PTP_GetString(dev_info->VendorExtensionDesc, &data[PTP_di_VendorExtensionDesc], &len); + + totallen = len * 2U + 1U; + dev_info->FunctionalMode = LE16(&data[PTP_di_FunctionalMode+totallen]); + dev_info->OperationsSupported_len = PTP_GetArray16 ((uint16_t *)(void *)&dev_info->OperationsSupported, + data, + PTP_di_OperationsSupported+totallen); + + totallen=totallen+dev_info->OperationsSupported_len*sizeof(uint16_t)+sizeof(uint32_t); + dev_info->EventsSupported_len = PTP_GetArray16 ((uint16_t *)(void *)&dev_info->EventsSupported, + data, + PTP_di_OperationsSupported+totallen); + + totallen=totallen+dev_info->EventsSupported_len*sizeof(uint16_t)+sizeof(uint32_t); + dev_info->DevicePropertiesSupported_len = PTP_GetArray16 ((uint16_t *)(void *)&dev_info->DevicePropertiesSupported, + data, + PTP_di_OperationsSupported+totallen); + + totallen=totallen+dev_info->DevicePropertiesSupported_len*sizeof(uint16_t)+sizeof(uint32_t); + + dev_info->CaptureFormats_len = PTP_GetArray16 ((uint16_t *)(void *)&dev_info->CaptureFormats, + data, + PTP_di_OperationsSupported+totallen); + + totallen=totallen+dev_info->CaptureFormats_len*sizeof(uint16_t)+sizeof(uint32_t); + dev_info->ImageFormats_len = PTP_GetArray16 ((uint16_t *)(void *)&dev_info->ImageFormats, + data, + PTP_di_OperationsSupported+totallen); + + totallen=totallen+dev_info->ImageFormats_len*sizeof(uint16_t)+sizeof(uint32_t); + PTP_GetString(dev_info->Manufacturer, &data[PTP_di_OperationsSupported+totallen], &len); + + totallen += len * 2U + 1U; + PTP_GetString(dev_info->Model, &data[PTP_di_OperationsSupported+totallen], &len); + + totallen += len * 2U + 1U; + PTP_GetString(dev_info->DeviceVersion, &data[PTP_di_OperationsSupported+totallen], &len); + + totallen += len * 2U + 1U; + PTP_GetString(dev_info->SerialNumber, &data[PTP_di_OperationsSupported+totallen], &len); + } +} + +/** + * @brief PTP_GetStorageIDs + * Gets Storage Ids and fills stor_ids structure. + * @param phost: Host handle + * @param stor_ids: Storage IDsstructure + * @retval None + */ +static void PTP_GetStorageIDs (USBH_HandleTypeDef *phost, PTP_StorageIDsTypedef *stor_ids) +{ + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + uint8_t *data = MTP_Handle->ptp.data_container.payload.data; + + stor_ids->n = PTP_GetArray32 (stor_ids->Storage, data, 0U); +} + + +/** + * @brief PTP_GetStorageInfo + * Gets Storage Info and fills stor_info structure. + * @param phost: Host handle + * @param stor_ids: Storage IDsstructure + * @retval None + */ +static void PTP_GetStorageInfo (USBH_HandleTypeDef *phost, uint32_t storage_id, PTP_StorageInfoTypedef *stor_info) +{ + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + uint8_t *data = MTP_Handle->ptp.data_container.payload.data; + + uint16_t len; + + stor_info->StorageType=LE16(&data[PTP_si_StorageType]); + stor_info->FilesystemType=LE16(&data[PTP_si_FilesystemType]); + stor_info->AccessCapability=LE16(&data[PTP_si_AccessCapability]); + stor_info->MaxCapability=LE64(&data[PTP_si_MaxCapability]); + stor_info->FreeSpaceInBytes=LE64(&data[PTP_si_FreeSpaceInBytes]); + stor_info->FreeSpaceInImages=LE32(&data[PTP_si_FreeSpaceInImages]); + + PTP_GetString(stor_info->StorageDescription, &data[PTP_si_StorageDescription], &len); + PTP_GetString(stor_info->VolumeLabel, &data[PTP_si_StorageDescription+len * 2U + 1U], &len); +} + +/** + * @brief PTP_GetObjectInfo + * Gets objectInfo and fills object_info structure. + * @param phost: Host handle + * @param object_info: object info structure + * @retval None + */ +static void PTP_GetObjectInfo (USBH_HandleTypeDef *phost, PTP_ObjectInfoTypedef *object_info) +{ + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + uint8_t *data = MTP_Handle->ptp.data_container.payload.data; + uint16_t filenamelen; + + object_info->StorageID = LE32(&data[PTP_oi_StorageID]); + object_info->ObjectFormat = LE16(&data[PTP_oi_ObjectFormat]); + object_info->ProtectionStatus = LE16(&data[PTP_oi_ProtectionStatus]); + object_info->ObjectCompressedSize = LE64(&data[PTP_oi_ObjectCompressedSize]); + + /* For Samsung Galaxy */ + if ((data[PTP_oi_filenamelen] == 0U) && (data[PTP_oi_filenamelen + 4U] != 0U)) + { + data += 4; + } + object_info->ThumbFormat=LE16(&data[PTP_oi_ThumbFormat]); + object_info->ThumbCompressedSize=LE32(&data[PTP_oi_ThumbCompressedSize]); + object_info->ThumbPixWidth=LE32(&data[PTP_oi_ThumbPixWidth]); + object_info->ThumbPixHeight=LE32(&data[PTP_oi_ThumbPixHeight]); + object_info->ImagePixWidth=LE32(&data[PTP_oi_ImagePixWidth]); + object_info->ImagePixHeight=LE32(&data[PTP_oi_ImagePixHeight]); + object_info->ImageBitDepth=LE32(&data[PTP_oi_ImageBitDepth]); + object_info->ParentObject=LE32(&data[PTP_oi_ParentObject]); + object_info->AssociationType=LE16(&data[PTP_oi_AssociationType]); + object_info->AssociationDesc=LE32(&data[PTP_oi_AssociationDesc]); + object_info->SequenceNumber=LE32(&data[PTP_oi_SequenceNumber]); + PTP_GetString(object_info->Filename, &data[PTP_oi_filenamelen], &filenamelen); +} + + +/** + * @brief PTP_GetObjectPropDesc + * Gets objectInfo and fills object_info structure. + * @param phost: Host handle + * @param opd: object prop descriptor structure + * @retval None + */ +static void PTP_GetObjectPropDesc (USBH_HandleTypeDef *phost, PTP_ObjectPropDescTypeDef *opd, uint32_t opdlen) +{ + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + uint8_t *data = MTP_Handle->ptp.data_container.payload.data; + uint32_t offset = 0U, i; + + opd->ObjectPropertyCode=LE16(&data[PTP_opd_ObjectPropertyCode]); + opd->DataType=LE16(&data[PTP_opd_DataType]); + opd->GetSet=*(uint8_t *)(&data[PTP_opd_GetSet]); + + offset = PTP_opd_FactoryDefaultValue; + PTP_GetDevicePropValue (phost, &offset, opdlen, &opd->FactoryDefaultValue, opd->DataType); + + opd->GroupCode=LE32(&data[offset]); + offset+=sizeof(uint32_t); + + opd->FormFlag=*(uint8_t *)(&data[offset]); + offset+=sizeof(uint8_t); + + switch (opd->FormFlag) + { + case PTP_OPFF_Range: + PTP_GetDevicePropValue(phost, &offset, opdlen, &opd->FORM.Range.MinimumValue, opd->DataType); + PTP_GetDevicePropValue(phost, &offset, opdlen, &opd->FORM.Range.MaximumValue, opd->DataType); + PTP_GetDevicePropValue(phost, &offset, opdlen, &opd->FORM.Range.StepSize, opd->DataType); + break; + + case PTP_OPFF_Enumeration: + + opd->FORM.Enum.NumberOfValues = LE16(&data[offset]); + offset+=sizeof(uint16_t); + + for (i = 0U; i < opd->FORM.Enum.NumberOfValues ; i++) + { + PTP_GetDevicePropValue(phost, &offset, opdlen, &opd->FORM.Enum.SupportedValue[i], opd->DataType); + } + break; + default: + break; + } +} + +/** + * @brief PTP_GetDevicePropValue + * Gets objectInfo and fills object_info structure. + * @param phost: Host handle + * @param opd: object prop descriptor structure + * @retval None + */ +static void PTP_GetDevicePropValue(USBH_HandleTypeDef *phost, + uint32_t *offset, + uint32_t total, + PTP_PropertyValueTypedef* value, + uint16_t datatype) +{ + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + uint8_t *data = MTP_Handle->ptp.data_container.payload.data; + uint16_t len; + switch (datatype) + { + case PTP_DTC_INT8: + value->i8 = *(int8_t *)(void *)&(data[*offset]); + *offset += 1U; + break; + case PTP_DTC_UINT8: + value->u8 = *(uint8_t *)&(data[*offset]); + *offset += 1U; + break; + case PTP_DTC_INT16: + value->i16 = *(int16_t *)(void *)&(data[*offset]); + *offset += 2U; + break; + case PTP_DTC_UINT16: + value->u16 = LE16(&(data[*offset])); + *offset += 2U; + break; + case PTP_DTC_INT32: + value->i32 = *(int32_t *)(void *)(&(data[*offset])); + *offset += 4U; + break; + case PTP_DTC_UINT32: + value->u32 = LE32(&(data[*offset])); + *offset += 4U; + break; + case PTP_DTC_INT64: + value->i64 = *(int64_t *)(void *)(&(data[*offset])); + *offset += 8U; + break; + case PTP_DTC_UINT64: + value->u64 = LE64(&(data[*offset])); + *offset += 8U; + break; + + case PTP_DTC_UINT128: + *offset += 16U; + break; + case PTP_DTC_INT128: + *offset += 16U; + break; + + case PTP_DTC_STR: + + PTP_GetString((uint8_t *)(void *)value->str, (uint8_t *)&(data[*offset]), &len); + *offset += len * 2U + 1U; + break; + default: + break; + } +} + + +/** + * @brief PTP_GetDevicePropValue + * Gets objectInfo and fills object_info structure. + * @param phost: Host handle + * @param opd: object prop descriptor structure + * @retval None + */ +static uint32_t PTP_GetObjectPropList (USBH_HandleTypeDef *phost, + MTP_PropertiesTypedef *props, + uint32_t len) +{ + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + uint8_t *data = MTP_Handle->ptp.data_container.payload.data; + uint32_t prop_count; + uint32_t offset = 0U, i; + + prop_count = LE32(data); + + + if (prop_count == 0U) + { + return 0U; + } + + data += sizeof(uint32_t); + len -= sizeof(uint32_t); + + for (i = 0U; i < prop_count; i++) + { + if (len <= 0U) + { + return 0U; + } + + props[i].ObjectHandle = LE32(data); + data += sizeof(uint32_t); + len -= sizeof(uint32_t); + + props[i].property = LE16(data); + data += sizeof(uint16_t); + len -= sizeof(uint16_t); + + props[i].datatype = LE16(data); + data += sizeof(uint16_t); + len -= sizeof(uint16_t); + + offset = 0U; + + PTP_GetDevicePropValue(phost, &offset, len, &props[i].propval, props[i].datatype); + + data += offset; + len -= offset; + } + + return prop_count; +} + +/** + * @brief PTP_GetString + * Gets SCII String from. + * @param str: ascii string + * @param data: Device info structure + * @retval None + */ +static void PTP_GetString (uint8_t *str, uint8_t* data, uint16_t *len) +{ + uint16_t strlength; + uint16_t idx; + + *len = data[0]; + strlength = (uint16_t)(2U * (uint32_t)data[0]); + data ++; /* Adjust the offset ignoring the String Len */ + + for (idx = 0U; idx < strlength; idx += 2U) + { + /* Copy Only the string and ignore the UNICODE ID, hence add the src */ + *str = data[idx]; + str++; + } + *str = 0U; /* mark end of string */ +} + +/** + * @brief PTP_GetString + * Gets SCII String from. + * @param str: ascii string + * @param data: Device info structure + * @retval None + */ + +static uint32_t PTP_GetArray16 (uint16_t *array, uint8_t *data, uint32_t offset) +{ + uint32_t size, idx = 0U; + + size=LE32(&data[offset]); + while (size > idx) + { + array[idx] = (uint16_t)data[offset + (sizeof(uint16_t)*(idx + 2U))]; + idx++; + } + return size; +} + +/** + * @brief PTP_GetString + * Gets SCII String from. + * @param str: ascii string + * @param data: Device info structure + * @retval None + */ + +static uint32_t PTP_GetArray32 (uint32_t *array, uint8_t *data, uint32_t offset) +{ + uint32_t size, idx = 0U; + + size = LE32(&data[offset]); + while (size > idx) + { + array[idx] = LE32(&data[offset+(sizeof(uint32_t)*(idx+1U))]); + idx++; + } + return size; +} + +/******************************************************************************* + + PTP Requests + +*******************************************************************************/ +/** + * @brief USBH_PTP_OpenSession + * Open a new session + * @param phost: Host handle + * @param session: Session ID (MUST BE > 0U) + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_PTP_OpenSession (USBH_HandleTypeDef *phost, uint32_t session) +{ + USBH_StatusTypeDef status = USBH_BUSY; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + PTP_ContainerTypedef ptp_container; + + switch(MTP_Handle->ptp.req_state) + { + case PTP_REQ_SEND: + + /* Init session params */ + MTP_Handle->ptp.transaction_id = 0x00000000U; + MTP_Handle->ptp.session_id = session; + MTP_Handle->ptp.flags = PTP_DP_NODATA; + + /* Fill operation request params */ + ptp_container.Code = PTP_OC_OpenSession; + ptp_container.SessionID = session; + ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++; + ptp_container.Param1 = session; + ptp_container.Nparam = 1U; + + /* convert request packet inti USB raw packet*/ + USBH_PTP_SendRequest (phost, &ptp_container); + + /* Setup State machine and start transfer */ + MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; + MTP_Handle->ptp.req_state = PTP_REQ_WAIT; + status = USBH_BUSY; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + case PTP_REQ_WAIT: + status = USBH_PTP_Process(phost); + break; + + default: + break; + } + return status; +} + +/** + * @brief USBH_PTP_GetDevicePropDesc + * Gets object partially + * @param phost: Host handle + * @param dev_info: Device info structure + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_PTP_GetDevicePropDesc (USBH_HandleTypeDef *phost, + uint16_t propcode, + PTP_DevicePropDescTypdef* devicepropertydesc) +{ + USBH_StatusTypeDef status = USBH_BUSY; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + PTP_ContainerTypedef ptp_container; + uint8_t *data = MTP_Handle->ptp.data_container.payload.data; + switch(MTP_Handle->ptp.req_state) + { + case PTP_REQ_SEND: + + /* Set operation request type */ + MTP_Handle->ptp.flags = PTP_DP_GETDATA; + MTP_Handle->ptp.data_ptr = (uint8_t *)(void *)&(MTP_Handle->ptp.data_container); + MTP_Handle->ptp.data_length = 0U; + MTP_Handle->ptp.data_packet_counter = 0U; + MTP_Handle->ptp.data_packet = 0U; + + /* Fill operation request params */ + ptp_container.Code = PTP_OC_GetDevicePropDesc; + ptp_container.SessionID = MTP_Handle->ptp.session_id; + ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++; + ptp_container.Param1 = propcode; + ptp_container.Nparam = 1U; + + /* convert request packet into USB raw packet*/ + USBH_PTP_SendRequest (phost, &ptp_container); + + /* Setup State machine and start transfer */ + MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; + MTP_Handle->ptp.req_state = PTP_REQ_WAIT; + status = USBH_BUSY; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + case PTP_REQ_WAIT: + status = USBH_PTP_Process(phost); + + if(status == USBH_OK) + { + devicepropertydesc->DevicePropertyCode = LE16(&data[PTP_dpd_DevicePropertyCode]); + devicepropertydesc->DataType = LE16(&data[PTP_dpd_DataType]); + devicepropertydesc->GetSet = *(uint8_t *)(&data[PTP_dpd_GetSet]); + devicepropertydesc->FormFlag = PTP_DPFF_None; + } + break; + + default: + break; + } + return status; +} +/** + * @brief USBH_PTP_GetDeviceInfo + * Gets device info dataset and fills deviceinfo structure. + * @param phost: Host handle + * @param dev_info: Device info structure + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_PTP_GetDeviceInfo (USBH_HandleTypeDef *phost, PTP_DeviceInfoTypedef *dev_info) +{ + USBH_StatusTypeDef status = USBH_BUSY; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + PTP_ContainerTypedef ptp_container; + + switch(MTP_Handle->ptp.req_state) + { + case PTP_REQ_SEND: + + /* Set operation request type */ + MTP_Handle->ptp.flags = PTP_DP_GETDATA; + MTP_Handle->ptp.data_ptr = (uint8_t *)(void *)&(MTP_Handle->ptp.data_container); + MTP_Handle->ptp.data_length = 0U; + MTP_Handle->ptp.data_packet_counter = 0U; + MTP_Handle->ptp.data_packet = 0U; + + /* Fill operation request params */ + ptp_container.Code = PTP_OC_GetDeviceInfo; + ptp_container.SessionID = MTP_Handle->ptp.session_id; + ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++; + ptp_container.Nparam = 0U; + + /* convert request packet inti USB raw packet*/ + USBH_PTP_SendRequest (phost, &ptp_container); + + /* Setup State machine and start transfer */ + MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; + MTP_Handle->ptp.req_state = PTP_REQ_WAIT; + status = USBH_BUSY; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + case PTP_REQ_WAIT: + status = USBH_PTP_Process(phost); + + if(status == USBH_OK) + { + PTP_DecodeDeviceInfo (phost, dev_info); + } + break; + + default: + break; + } + return status; +} + +/** + * @brief USBH_PTP_GetStorageIds + * Gets device info dataset and fills deviceinfo structure. + * @param phost: Host handle + * @param dev_info: Device info structure + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_PTP_GetStorageIds (USBH_HandleTypeDef *phost, PTP_StorageIDsTypedef *storage_ids) +{ + USBH_StatusTypeDef status = USBH_BUSY; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + PTP_ContainerTypedef ptp_container; + + switch(MTP_Handle->ptp.req_state) + { + case PTP_REQ_SEND: + + /* Set operation request type */ + MTP_Handle->ptp.flags = PTP_DP_GETDATA; + MTP_Handle->ptp.data_ptr = (uint8_t *)(void *)&(MTP_Handle->ptp.data_container); + MTP_Handle->ptp.data_length = 0U; + MTP_Handle->ptp.data_packet_counter = 0U; + MTP_Handle->ptp.data_packet = 0U; + + /* Fill operation request params */ + ptp_container.Code = PTP_OC_GetStorageIDs; + ptp_container.SessionID = MTP_Handle->ptp.session_id; + ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++; + ptp_container.Nparam = 0U; + + /* convert request packet inti USB raw packet*/ + USBH_PTP_SendRequest (phost, &ptp_container); + + /* Setup State machine and start transfer */ + MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; + MTP_Handle->ptp.req_state = PTP_REQ_WAIT; + status = USBH_BUSY; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + case PTP_REQ_WAIT: + status = USBH_PTP_Process(phost); + + if(status == USBH_OK) + { + PTP_GetStorageIDs (phost, storage_ids); + } + break; + + default: + break; + } + return status; +} + +/** + * @brief USBH_PTP_GetDeviceInfo + * Gets device info dataset and fills deviceinfo structure. + * @param phost: Host handle + * @param dev_info: Device info structure + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_PTP_GetStorageInfo (USBH_HandleTypeDef *phost, uint32_t storage_id, PTP_StorageInfoTypedef *storage_info) +{ + USBH_StatusTypeDef status = USBH_BUSY; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + PTP_ContainerTypedef ptp_container; + + switch(MTP_Handle->ptp.req_state) + { + case PTP_REQ_SEND: + + /* Set operation request type */ + MTP_Handle->ptp.flags = PTP_DP_GETDATA; + MTP_Handle->ptp.data_ptr = (uint8_t *)(void *)&(MTP_Handle->ptp.data_container); + MTP_Handle->ptp.data_length = 0U; + MTP_Handle->ptp.data_packet_counter = 0U; + MTP_Handle->ptp.data_packet = 0U; + + /* Fill operation request params */ + ptp_container.Code = PTP_OC_GetStorageInfo; + ptp_container.SessionID = MTP_Handle->ptp.session_id; + ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++; + ptp_container.Param1 = storage_id; + ptp_container.Nparam = 1U; + + /* convert request packet inti USB raw packet*/ + USBH_PTP_SendRequest (phost, &ptp_container); + + /* Setup State machine and start transfer */ + MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; + MTP_Handle->ptp.req_state = PTP_REQ_WAIT; + status = USBH_BUSY; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + case PTP_REQ_WAIT: + status = USBH_PTP_Process(phost); + + if(status == USBH_OK) + { + PTP_GetStorageInfo (phost, storage_id, storage_info); + } + break; + + default: + break; + } + return status; +} + +/** + * @brief USBH_PTP_GetNumObjects + * Gets device info dataset and fills deviceinfo structure. + * @param phost: Host handle + * @param dev_info: Device info structure + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_PTP_GetNumObjects (USBH_HandleTypeDef *phost, + uint32_t storage_id, + uint32_t objectformatcode, + uint32_t associationOH, + uint32_t* numobs) +{ + USBH_StatusTypeDef status = USBH_BUSY; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + PTP_ContainerTypedef ptp_container; + + switch(MTP_Handle->ptp.req_state) + { + case PTP_REQ_SEND: + + /* Set operation request type */ + MTP_Handle->ptp.flags = PTP_DP_NODATA; + + /* Fill operation request params */ + ptp_container.Code = PTP_OC_GetNumObjects; + ptp_container.SessionID = MTP_Handle->ptp.session_id; + ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++; + ptp_container.Param1 = storage_id; + ptp_container.Param2 = objectformatcode; + ptp_container.Param3 = associationOH; + ptp_container.Nparam = 3U; + + /* convert request packet into USB raw packet*/ + USBH_PTP_SendRequest (phost, &ptp_container); + + /* Setup State machine and start transfer */ + MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; + MTP_Handle->ptp.req_state = PTP_REQ_WAIT; + status = USBH_BUSY; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + case PTP_REQ_WAIT: + status = USBH_PTP_Process(phost); + + if(status == USBH_OK) + { + *numobs = MTP_Handle->ptp.resp_container.param1; + } + break; + + default: + break; + } + return status; +} + +/** + * @brief USBH_PTP_GetObjectHandles + * Gets device info dataset and fills deviceinfo structure. + * @param phost: Host handle + * @param dev_info: Device info structure + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_PTP_GetObjectHandles (USBH_HandleTypeDef *phost, + uint32_t storage_id, + uint32_t objectformatcode, + uint32_t associationOH, + PTP_ObjectHandlesTypedef* objecthandles) +{ + USBH_StatusTypeDef status = USBH_BUSY; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + PTP_ContainerTypedef ptp_container; + + switch(MTP_Handle->ptp.req_state) + { + case PTP_REQ_SEND: + + /* Set operation request type */ + MTP_Handle->ptp.flags = PTP_DP_GETDATA; + MTP_Handle->ptp.data_ptr = (uint8_t *)(void *)&(MTP_Handle->ptp.data_container); + MTP_Handle->ptp.data_length = 0U; + MTP_Handle->ptp.data_packet_counter = 0U; + MTP_Handle->ptp.data_packet = 0U; + + /* Fill operation request params */ + ptp_container.Code = PTP_OC_GetObjectHandles; + ptp_container.SessionID = MTP_Handle->ptp.session_id; + ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id++; + ptp_container.Param1 = storage_id; + ptp_container.Param2 = objectformatcode; + ptp_container.Param3 = associationOH; + ptp_container.Nparam = 3U; + + /* convert request packet into USB raw packet*/ + USBH_PTP_SendRequest (phost, &ptp_container); + + /* Setup State machine and start transfer */ + MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; + MTP_Handle->ptp.req_state = PTP_REQ_WAIT; + status = USBH_BUSY; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + case PTP_REQ_WAIT: + status = USBH_PTP_Process(phost); + + if(status == USBH_OK) + { + objecthandles->n = PTP_GetArray32 (objecthandles->Handler, + MTP_Handle->ptp.data_container.payload.data, + 0U); + } + break; + + default: + break; + } + return status; +} + +/** + * @brief USBH_PTP_GetObjectInfo + * Gets objert info + * @param phost: Host handle + * @param dev_info: Device info structure + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_PTP_GetObjectInfo (USBH_HandleTypeDef *phost, + uint32_t handle, + PTP_ObjectInfoTypedef* object_info) +{ + USBH_StatusTypeDef status = USBH_BUSY; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + PTP_ContainerTypedef ptp_container; + + switch(MTP_Handle->ptp.req_state) + { + case PTP_REQ_SEND: + + /* Set operation request type */ + MTP_Handle->ptp.flags = PTP_DP_GETDATA; + MTP_Handle->ptp.data_ptr = (uint8_t *)(void *)&(MTP_Handle->ptp.data_container); + MTP_Handle->ptp.data_length = 0U; + MTP_Handle->ptp.data_packet_counter = 0U; + MTP_Handle->ptp.data_packet = 0U; + + /* Fill operation request params */ + ptp_container.Code = PTP_OC_GetObjectInfo; + ptp_container.SessionID = MTP_Handle->ptp.session_id; + ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++; + ptp_container.Param1 = handle; + ptp_container.Nparam = 1U; + + /* convert request packet into USB raw packet*/ + USBH_PTP_SendRequest (phost, &ptp_container); + + /* Setup State machine and start transfer */ + MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; + MTP_Handle->ptp.req_state = PTP_REQ_WAIT; + status = USBH_BUSY; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + case PTP_REQ_WAIT: + status = USBH_PTP_Process(phost); + + if(status == USBH_OK) + { + PTP_GetObjectInfo (phost, object_info); + } + break; + + default: + break; + } + return status; +} + +/** + * @brief USBH_PTP_DeleteObject + * Delete an object. + * @param phost: Host handle + * @param handle : Object Handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_PTP_DeleteObject (USBH_HandleTypeDef *phost, + uint32_t handle, + uint32_t objectformatcode) +{ + USBH_StatusTypeDef status = USBH_BUSY; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + PTP_ContainerTypedef ptp_container; + + switch(MTP_Handle->ptp.req_state) + { + case PTP_REQ_SEND: + + /* Set operation request type */ + MTP_Handle->ptp.flags = PTP_DP_NODATA; + + /* Fill operation request params */ + ptp_container.Code = PTP_OC_DeleteObject; + ptp_container.SessionID = MTP_Handle->ptp.session_id; + ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++; + ptp_container.Param1 = handle; + ptp_container.Param2 = objectformatcode; + ptp_container.Nparam = 2U; + + /* convert request packet into USB raw packet*/ + USBH_PTP_SendRequest (phost, &ptp_container); + + /* Setup State machine and start transfer */ + MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; + MTP_Handle->ptp.req_state = PTP_REQ_WAIT; + status = USBH_BUSY; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + case PTP_REQ_WAIT: + status = USBH_PTP_Process(phost); + break; + + default: + break; + } + return status; +} + +/** + * @brief USBH_PTP_GetObject + * Gets object + * @param phost: Host handle + * @param dev_info: Device info structure + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_PTP_GetObject (USBH_HandleTypeDef *phost, + uint32_t handle, + uint8_t *object) +{ + USBH_StatusTypeDef status = USBH_BUSY; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + PTP_ContainerTypedef ptp_container; + + switch(MTP_Handle->ptp.req_state) + { + case PTP_REQ_SEND: + + /* Set operation request type */ + MTP_Handle->ptp.flags = PTP_DP_GETDATA; + MTP_Handle->ptp.data_ptr = (uint8_t *)(void *)&(MTP_Handle->ptp.data_container); + MTP_Handle->ptp.data_length = 0U; + MTP_Handle->ptp.data_packet_counter = 0U; + MTP_Handle->ptp.data_packet = 0U; + + /* set object control params */ + MTP_Handle->ptp.object_ptr = object; + + /* Fill operation request params */ + ptp_container.Code = PTP_OC_GetObject; + ptp_container.SessionID = MTP_Handle->ptp.session_id; + ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++; + ptp_container.Param1 = handle; + ptp_container.Nparam = 1U; + + + /* convert request packet into USB raw packet*/ + USBH_PTP_SendRequest (phost, &ptp_container); + + /* Setup State machine and start transfer */ + MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; + MTP_Handle->ptp.req_state = PTP_REQ_WAIT; + status = USBH_BUSY; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + case PTP_REQ_WAIT: + status = USBH_PTP_Process(phost); + + if(status == USBH_OK) + { + /* first packet is in the PTP data payload buffer */ + if(MTP_Handle->ptp.iteration == 0U) + { + /* copy it to object */ + USBH_memcpy(MTP_Handle->ptp.object_ptr, MTP_Handle->ptp.data_container.payload.data, PTP_USB_BULK_PAYLOAD_LEN_READ); + } + } + break; + + default: + break; + } + return status; +} + +/** + * @brief USBH_PTP_GetPartialObject + * Gets object partially + * @param phost: Host handle + * @param dev_info: Device info structure + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_PTP_GetPartialObject(USBH_HandleTypeDef *phost, + uint32_t handle, + uint32_t offset, + uint32_t maxbytes, + uint8_t *object, + uint32_t *len) +{ + USBH_StatusTypeDef status = USBH_BUSY; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + PTP_ContainerTypedef ptp_container; + + switch(MTP_Handle->ptp.req_state) + { + case PTP_REQ_SEND: + + /* Set operation request type */ + MTP_Handle->ptp.flags = PTP_DP_GETDATA; + MTP_Handle->ptp.data_ptr = (uint8_t *)(void *)&(MTP_Handle->ptp.data_container); + MTP_Handle->ptp.data_length = 0U; + MTP_Handle->ptp.data_packet_counter = 0U; + MTP_Handle->ptp.data_packet = 0U; + + /* set object control params */ + MTP_Handle->ptp.object_ptr = object; + + /* Fill operation request params */ + ptp_container.Code = PTP_OC_GetPartialObject; + ptp_container.SessionID = MTP_Handle->ptp.session_id; + ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++; + ptp_container.Param1 = handle; + ptp_container.Param2 = offset; + ptp_container.Param3 = maxbytes; + ptp_container.Nparam = 3U; + + /* convert request packet into USB raw packet*/ + USBH_PTP_SendRequest (phost, &ptp_container); + + /* Setup State machine and start transfer */ + MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; + MTP_Handle->ptp.req_state = PTP_REQ_WAIT; + status = USBH_BUSY; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + case PTP_REQ_WAIT: + status = USBH_PTP_Process(phost); + + if(status == USBH_OK) + { + *len = MTP_Handle->ptp.resp_container.param1; + /* first packet is in the PTP data payload buffer */ + if(MTP_Handle->ptp.iteration == 0U) + { + /* copy it to object */ + USBH_memcpy(MTP_Handle->ptp.object_ptr, MTP_Handle->ptp.data_container.payload.data, *len); + } + } + break; + + default: + break; + } + return status; +} + +/** + * @brief USBH_PTP_GetObjectPropsSupported + * Gets object partially + * @param phost: Host handle + * @param dev_info: Device info structure + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_PTP_GetObjectPropsSupported (USBH_HandleTypeDef *phost, + uint16_t ofc, + uint32_t *propnum, + uint16_t *props) +{ + USBH_StatusTypeDef status = USBH_BUSY; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + PTP_ContainerTypedef ptp_container; + + switch(MTP_Handle->ptp.req_state) + { + case PTP_REQ_SEND: + + /* Set operation request type */ + MTP_Handle->ptp.flags = PTP_DP_GETDATA; + MTP_Handle->ptp.data_ptr = (uint8_t *)(void *)&(MTP_Handle->ptp.data_container); + MTP_Handle->ptp.data_length = 0U; + MTP_Handle->ptp.data_packet_counter = 0U; + MTP_Handle->ptp.data_packet = 0U; + + /* Fill operation request params */ + ptp_container.Code = PTP_OC_GetObjectPropsSupported; + ptp_container.SessionID = MTP_Handle->ptp.session_id; + ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++; + ptp_container.Param1 = ofc; + ptp_container.Nparam = 1U; + + /* convert request packet into USB raw packet*/ + USBH_PTP_SendRequest (phost, &ptp_container); + + /* Setup State machine and start transfer */ + MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; + MTP_Handle->ptp.req_state = PTP_REQ_WAIT; + status = USBH_BUSY; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + case PTP_REQ_WAIT: + status = USBH_PTP_Process(phost); + + if(status == USBH_OK) + { + *propnum = PTP_GetArray16 (props, MTP_Handle->ptp.data_container.payload.data, 0U); + } + break; + + default: + break; + } + return status; +} + +/** + * @brief USBH_PTP_GetObjectPropDesc + * Gets object partially + * @param phost: Host handle + * @param dev_info: Device info structure + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_PTP_GetObjectPropDesc (USBH_HandleTypeDef *phost, + uint16_t opc, + uint16_t ofc, + PTP_ObjectPropDescTypeDef *opd) +{ + USBH_StatusTypeDef status = USBH_BUSY; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + PTP_ContainerTypedef ptp_container; + + switch(MTP_Handle->ptp.req_state) + { + case PTP_REQ_SEND: + + /* Set operation request type */ + MTP_Handle->ptp.flags = PTP_DP_GETDATA; + MTP_Handle->ptp.data_ptr = (uint8_t *)(void *)&(MTP_Handle->ptp.data_container); + MTP_Handle->ptp.data_length = 0U; + MTP_Handle->ptp.data_packet_counter = 0U; + MTP_Handle->ptp.data_packet = 0U; + + /* Fill operation request params */ + ptp_container.Code = PTP_OC_GetObjectPropDesc; + ptp_container.SessionID = MTP_Handle->ptp.session_id; + ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++; + ptp_container.Param1 = opc; + ptp_container.Param2 = ofc; + ptp_container.Nparam = 2U; + + /* convert request packet into USB raw packet*/ + USBH_PTP_SendRequest (phost, &ptp_container); + + /* Setup State machine and start transfer */ + MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; + MTP_Handle->ptp.req_state = PTP_REQ_WAIT; + status = USBH_BUSY; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + case PTP_REQ_WAIT: + status = USBH_PTP_Process(phost); + + if(status == USBH_OK) + { + PTP_GetObjectPropDesc(phost, opd, MTP_Handle->ptp.data_length); + } + break; + + default: + break; + } + return status; +} + +/** + * @brief USBH_PTP_GetObjectPropList + * Gets object partially + * @param phost: Host handle + * @param dev_info: Device info structure + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_PTP_GetObjectPropList (USBH_HandleTypeDef *phost, + uint32_t handle, + MTP_PropertiesTypedef *pprops, + uint32_t *nrofprops) +{ + USBH_StatusTypeDef status = USBH_BUSY; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + PTP_ContainerTypedef ptp_container; + + switch(MTP_Handle->ptp.req_state) + { + case PTP_REQ_SEND: + + /* Set operation request type */ + MTP_Handle->ptp.flags = PTP_DP_GETDATA; + MTP_Handle->ptp.data_ptr = (uint8_t *)(void *)&(MTP_Handle->ptp.data_container); + MTP_Handle->ptp.data_length = 0U; + MTP_Handle->ptp.data_packet_counter = 0U; + MTP_Handle->ptp.data_packet = 0U; + + /* copy first packet of the object into data container */ + USBH_memcpy(MTP_Handle->ptp.data_container.payload.data, MTP_Handle->ptp.object_ptr, PTP_USB_BULK_PAYLOAD_LEN_READ); + + /* Fill operation request params */ + ptp_container.Code = PTP_OC_GetObjPropList; + ptp_container.SessionID = MTP_Handle->ptp.session_id; + ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++; + ptp_container.Param1 = handle; + ptp_container.Param2 = 0x00000000U; /* 0x00000000U should be "all formats" */ + ptp_container.Param3 = 0xFFFFFFFFU; /* 0xFFFFFFFFU should be "all properties" */ + ptp_container.Param4 = 0x00000000U; + ptp_container.Param5 = 0xFFFFFFFFU; /* Return full tree below the Param1 handle */ + ptp_container.Nparam = 5U; + + /* convert request packet into USB raw packet*/ + USBH_PTP_SendRequest (phost, &ptp_container); + + /* Setup State machine and start transfer */ + MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; + MTP_Handle->ptp.req_state = PTP_REQ_WAIT; + status = USBH_BUSY; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + case PTP_REQ_WAIT: + status = USBH_PTP_Process(phost); + + if(status == USBH_OK) + { + PTP_GetObjectPropList (phost, pprops, MTP_Handle->ptp.data_length); + } + break; + + default: + break; + } + return status; +} + +/** + * @brief USBH_PTP_SendObject + * Send an object + * @param phost: Host handle + * @param dev_info: Device info structure + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_PTP_SendObject (USBH_HandleTypeDef *phost, + uint32_t handle, + uint8_t *object, + uint32_t size) +{ + USBH_StatusTypeDef status = USBH_BUSY; + MTP_HandleTypeDef *MTP_Handle = (MTP_HandleTypeDef *)phost->pActiveClass->pData; + PTP_ContainerTypedef ptp_container; + + switch(MTP_Handle->ptp.req_state) + { + case PTP_REQ_SEND: + + /* Set operation request type */ + MTP_Handle->ptp.flags = PTP_DP_SENDDATA; + MTP_Handle->ptp.data_ptr = (uint8_t *)(void *)&(MTP_Handle->ptp.data_container); + MTP_Handle->ptp.data_packet_counter = 0U; + MTP_Handle->ptp.data_packet = 0U; + MTP_Handle->ptp.iteration = 0U; + + /* set object control params */ + MTP_Handle->ptp.object_ptr = object; + MTP_Handle->ptp.data_length = size; + + /* Fill operation request params */ + ptp_container.Code = PTP_OC_SendObject; + ptp_container.SessionID = MTP_Handle->ptp.session_id; + ptp_container.Transaction_ID = MTP_Handle->ptp.transaction_id ++; + ptp_container.Nparam = 0U; + + + /* convert request packet into USB raw packet*/ + USBH_PTP_SendRequest (phost, &ptp_container); + + /* Setup State machine and start transfer */ + MTP_Handle->ptp.state = PTP_OP_REQUEST_STATE; + MTP_Handle->ptp.req_state = PTP_REQ_WAIT; + status = USBH_BUSY; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + case PTP_REQ_WAIT: + status = USBH_PTP_Process(phost); + break; + + default: + break; + } + return status; +} +/** +* @} +*/ + +/** +* @} +*/ + +/** +* @} +*/ + +/** +* @} +*/ + +/** +* @} +*/ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + + + diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Class/Template/Inc/usbh_template.h b/system/Middlewares/ST/STM32_USB_Host_Library/Class/Template/Inc/usbh_template.h index a5f53d0c4b..4515d3c862 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Class/Template/Inc/usbh_template.h +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Class/Template/Inc/usbh_template.h @@ -1,121 +1,121 @@ -/** - ****************************************************************************** - * @file usbh_template.h - * @author MCD Application Team - * @brief This file contains all the prototypes for the usbh_template.c - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive ----------------------------------------------*/ -#ifndef __USBH_TEMPLATE_H -#define __USBH_TEMPLATE_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_core.h" - - -/** @addtogroup USBH_LIB -* @{ -*/ - -/** @addtogroup USBH_CLASS -* @{ -*/ - -/** @addtogroup USBH_TEMPLATE_CLASS -* @{ -*/ - -/** @defgroup USBH_TEMPLATE_CLASS -* @brief This file is the Header file for usbh_template.c -* @{ -*/ - - -/** - * @} - */ - -/** @defgroup USBH_TEMPLATE_CLASS_Exported_Types -* @{ -*/ - -/* States for TEMPLATE State Machine */ - - -/** -* @} -*/ - -/** @defgroup USBH_TEMPLATE_CLASS_Exported_Defines -* @{ -*/ - -/** -* @} -*/ - -/** @defgroup USBH_TEMPLATE_CLASS_Exported_Macros -* @{ -*/ -/** -* @} -*/ - -/** @defgroup USBH_TEMPLATE_CLASS_Exported_Variables -* @{ -*/ -extern USBH_ClassTypeDef TEMPLATE_Class; -#define USBH_TEMPLATE_CLASS &TEMPLATE_Class - -/** -* @} -*/ - -/** @defgroup USBH_TEMPLATE_CLASS_Exported_FunctionsPrototype -* @{ -*/ -USBH_StatusTypeDef USBH_TEMPLATE_IOProcess (USBH_HandleTypeDef *phost); -USBH_StatusTypeDef USBH_TEMPLATE_Init (USBH_HandleTypeDef *phost); -/** -* @} -*/ - -#ifdef __cplusplus -} -#endif - -#endif /* __USBH_TEMPLATE_H */ - -/** -* @} -*/ - -/** -* @} -*/ - -/** -* @} -*/ - -/** -* @} -*/ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - +/** + ****************************************************************************** + * @file usbh_template.h + * @author MCD Application Team + * @brief This file contains all the prototypes for the usbh_template.c + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive ----------------------------------------------*/ +#ifndef __USBH_TEMPLATE_H +#define __USBH_TEMPLATE_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_core.h" + + +/** @addtogroup USBH_LIB +* @{ +*/ + +/** @addtogroup USBH_CLASS +* @{ +*/ + +/** @addtogroup USBH_TEMPLATE_CLASS +* @{ +*/ + +/** @defgroup USBH_TEMPLATE_CLASS +* @brief This file is the Header file for usbh_template.c +* @{ +*/ + + +/** + * @} + */ + +/** @defgroup USBH_TEMPLATE_CLASS_Exported_Types +* @{ +*/ + +/* States for TEMPLATE State Machine */ + + +/** +* @} +*/ + +/** @defgroup USBH_TEMPLATE_CLASS_Exported_Defines +* @{ +*/ + +/** +* @} +*/ + +/** @defgroup USBH_TEMPLATE_CLASS_Exported_Macros +* @{ +*/ +/** +* @} +*/ + +/** @defgroup USBH_TEMPLATE_CLASS_Exported_Variables +* @{ +*/ +extern USBH_ClassTypeDef TEMPLATE_Class; +#define USBH_TEMPLATE_CLASS &TEMPLATE_Class + +/** +* @} +*/ + +/** @defgroup USBH_TEMPLATE_CLASS_Exported_FunctionsPrototype +* @{ +*/ +USBH_StatusTypeDef USBH_TEMPLATE_IOProcess (USBH_HandleTypeDef *phost); +USBH_StatusTypeDef USBH_TEMPLATE_Init (USBH_HandleTypeDef *phost); +/** +* @} +*/ + +#ifdef __cplusplus +} +#endif + +#endif /* __USBH_TEMPLATE_H */ + +/** +* @} +*/ + +/** +* @} +*/ + +/** +* @} +*/ + +/** +* @} +*/ +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Class/Template/Src/usbh_template.c b/system/Middlewares/ST/STM32_USB_Host_Library/Class/Template/Src/usbh_template.c index 9ad0e6b3bd..37e9885d91 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Class/Template/Src/usbh_template.c +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Class/Template/Src/usbh_template.c @@ -1,232 +1,232 @@ -/** - ****************************************************************************** - * @file usbh_mtp.c - * @author MCD Application Team - * @brief This file is the MTP Layer Handlers for USB Host MTP class. - * - * - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_template.h" - -/** @addtogroup USBH_LIB -* @{ -*/ - -/** @addtogroup USBH_CLASS -* @{ -*/ - -/** @addtogroup USBH_TEMPLATE_CLASS -* @{ -*/ - -/** @defgroup USBH_TEMPLATE_CORE -* @brief This file includes TEMPLATE Layer Handlers for USB Host TEMPLATE class. -* @{ -*/ - -/** @defgroup USBH_TEMPLATE_CORE_Private_TypesDefinitions -* @{ -*/ -/** -* @} -*/ - - -/** @defgroup USBH_TEMPLATE_CORE_Private_Defines -* @{ -*/ -/** -* @} -*/ - - -/** @defgroup USBH_TEMPLATE_CORE_Private_Macros -* @{ -*/ -/** -* @} -*/ - - -/** @defgroup USBH_TEMPLATE_CORE_Private_Variables -* @{ -*/ -/** -* @} -*/ - - -/** @defgroup USBH_TEMPLATE_CORE_Private_FunctionPrototypes -* @{ -*/ - -static USBH_StatusTypeDef USBH_TEMPLATE_InterfaceInit (USBH_HandleTypeDef *phost); - -static USBH_StatusTypeDef USBH_TEMPLATE_InterfaceDeInit (USBH_HandleTypeDef *phost); - -static USBH_StatusTypeDef USBH_TEMPLATE_Process(USBH_HandleTypeDef *phost); - -static USBH_StatusTypeDef USBH_TEMPLATE_ClassRequest (USBH_HandleTypeDef *phost); - - -USBH_ClassTypeDef TEMPLATE_Class = -{ - "TEMPLATE", - USB_TEMPLATE_CLASS, - USBH_TEMPLATE_InterfaceInit, - USBH_TEMPLATE_InterfaceDeInit, - USBH_TEMPLATE_ClassRequest, - USBH_TEMPLATE_Process -}; -/** -* @} -*/ - - -/** @defgroup USBH_TEMPLATE_CORE_Private_Functions -* @{ -*/ - -/** - * @brief USBH_TEMPLATE_InterfaceInit - * The function init the TEMPLATE class. - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_TEMPLATE_InterfaceInit (USBH_HandleTypeDef *phost) -{ - - return USBH_OK; -} - - - -/** - * @brief USBH_TEMPLATE_InterfaceDeInit - * The function DeInit the Pipes used for the TEMPLATE class. - * @param phost: Host handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_TEMPLATE_InterfaceDeInit (USBH_HandleTypeDef *phost) -{ - - return USBH_OK; -} - -/** - * @brief USBH_TEMPLATE_ClassRequest - * The function is responsible for handling Standard requests - * for TEMPLATE class. - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_TEMPLATE_ClassRequest (USBH_HandleTypeDef *phost) -{ - - return USBH_OK; -} - - -/** - * @brief USBH_TEMPLATE_Process - * The function is for managing state machine for TEMPLATE data transfers - * @param phost: Host handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_TEMPLATE_Process (USBH_HandleTypeDef *phost) -{ - - return USBH_OK; -} - - -/** - * @brief USBH_TEMPLATE_Init - * The function Initialize the TEMPLATE function - * @param phost: Host handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_TEMPLATE_Init (USBH_HandleTypeDef *phost) -{ - USBH_StatusTypeDef Status = USBH_BUSY; -#if (USBH_USE_OS == 1U) - osEvent event; - - event = osMessageGet( phost->class_ready_event, osWaitForever ); - - if( event.status == osEventMessage ) - { - if(event.value.v == USBH_CLASS_EVENT) - { -#else - - while ((Status == USBH_BUSY) || (Status == USBH_FAIL)) - { - /* Host background process */ - USBH_Process(phost); - if(phost->gState == HOST_CLASS) - { -#endif - Status = USBH_OK; - } - } - return Status; -} - -/** - * @brief USBH_TEMPLATE_IOProcess - * TEMPLATE TEMPLATE process - * @param phost: Host handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_TEMPLATE_IOProcess (USBH_HandleTypeDef *phost) -{ - if (phost->device.is_connected == 1U) - { - if(phost->gState == HOST_CLASS) - { - USBH_TEMPLATE_Process(phost); - } - } - - return USBH_OK; -} - -/** -* @} -*/ - -/** -* @} -*/ - -/** -* @} -*/ - - -/** -* @} -*/ - - -/** -* @} -*/ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbh_mtp.c + * @author MCD Application Team + * @brief This file is the MTP Layer Handlers for USB Host MTP class. + * + * + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_template.h" + +/** @addtogroup USBH_LIB +* @{ +*/ + +/** @addtogroup USBH_CLASS +* @{ +*/ + +/** @addtogroup USBH_TEMPLATE_CLASS +* @{ +*/ + +/** @defgroup USBH_TEMPLATE_CORE +* @brief This file includes TEMPLATE Layer Handlers for USB Host TEMPLATE class. +* @{ +*/ + +/** @defgroup USBH_TEMPLATE_CORE_Private_TypesDefinitions +* @{ +*/ +/** +* @} +*/ + + +/** @defgroup USBH_TEMPLATE_CORE_Private_Defines +* @{ +*/ +/** +* @} +*/ + + +/** @defgroup USBH_TEMPLATE_CORE_Private_Macros +* @{ +*/ +/** +* @} +*/ + + +/** @defgroup USBH_TEMPLATE_CORE_Private_Variables +* @{ +*/ +/** +* @} +*/ + + +/** @defgroup USBH_TEMPLATE_CORE_Private_FunctionPrototypes +* @{ +*/ + +static USBH_StatusTypeDef USBH_TEMPLATE_InterfaceInit (USBH_HandleTypeDef *phost); + +static USBH_StatusTypeDef USBH_TEMPLATE_InterfaceDeInit (USBH_HandleTypeDef *phost); + +static USBH_StatusTypeDef USBH_TEMPLATE_Process(USBH_HandleTypeDef *phost); + +static USBH_StatusTypeDef USBH_TEMPLATE_ClassRequest (USBH_HandleTypeDef *phost); + + +USBH_ClassTypeDef TEMPLATE_Class = +{ + "TEMPLATE", + USB_TEMPLATE_CLASS, + USBH_TEMPLATE_InterfaceInit, + USBH_TEMPLATE_InterfaceDeInit, + USBH_TEMPLATE_ClassRequest, + USBH_TEMPLATE_Process +}; +/** +* @} +*/ + + +/** @defgroup USBH_TEMPLATE_CORE_Private_Functions +* @{ +*/ + +/** + * @brief USBH_TEMPLATE_InterfaceInit + * The function init the TEMPLATE class. + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_TEMPLATE_InterfaceInit (USBH_HandleTypeDef *phost) +{ + + return USBH_OK; +} + + + +/** + * @brief USBH_TEMPLATE_InterfaceDeInit + * The function DeInit the Pipes used for the TEMPLATE class. + * @param phost: Host handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_TEMPLATE_InterfaceDeInit (USBH_HandleTypeDef *phost) +{ + + return USBH_OK; +} + +/** + * @brief USBH_TEMPLATE_ClassRequest + * The function is responsible for handling Standard requests + * for TEMPLATE class. + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_TEMPLATE_ClassRequest (USBH_HandleTypeDef *phost) +{ + + return USBH_OK; +} + + +/** + * @brief USBH_TEMPLATE_Process + * The function is for managing state machine for TEMPLATE data transfers + * @param phost: Host handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_TEMPLATE_Process (USBH_HandleTypeDef *phost) +{ + + return USBH_OK; +} + + +/** + * @brief USBH_TEMPLATE_Init + * The function Initialize the TEMPLATE function + * @param phost: Host handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_TEMPLATE_Init (USBH_HandleTypeDef *phost) +{ + USBH_StatusTypeDef Status = USBH_BUSY; +#if (USBH_USE_OS == 1U) + osEvent event; + + event = osMessageGet( phost->class_ready_event, osWaitForever ); + + if( event.status == osEventMessage ) + { + if(event.value.v == USBH_CLASS_EVENT) + { +#else + + while ((Status == USBH_BUSY) || (Status == USBH_FAIL)) + { + /* Host background process */ + USBH_Process(phost); + if(phost->gState == HOST_CLASS) + { +#endif + Status = USBH_OK; + } + } + return Status; +} + +/** + * @brief USBH_TEMPLATE_IOProcess + * TEMPLATE TEMPLATE process + * @param phost: Host handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_TEMPLATE_IOProcess (USBH_HandleTypeDef *phost) +{ + if (phost->device.is_connected == 1U) + { + if(phost->gState == HOST_CLASS) + { + USBH_TEMPLATE_Process(phost); + } + } + + return USBH_OK; +} + +/** +* @} +*/ + +/** +* @} +*/ + +/** +* @} +*/ + + +/** +* @} +*/ + + +/** +* @} +*/ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Core/Inc/usbh_conf_template.h b/system/Middlewares/ST/STM32_USB_Host_Library/Core/Inc/usbh_conf_template.h index f1deac0e13..9098332fca 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Core/Inc/usbh_conf_template.h +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Core/Inc/usbh_conf_template.h @@ -1,152 +1,152 @@ -/** - ****************************************************************************** - * @file usbh_conf_template.h - * @author MCD Application Team - * @brief Header file for usbh_conf_template.c - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USBH_CONF_TEMPLATE_H -#define __USBH_CONF_TEMPLATE_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "stm32fxxx.h" /* replace 'stm32xxx' with your HAL driver header filename, ex: stm32f4xx.h */ -#include -#include -#include - -/** @addtogroup STM32_USB_HOST_LIBRARY - * @{ - */ - -/** @defgroup USBH_CONF - * @brief usb host low level driver configuration file - * @{ - */ - -/** @defgroup USBH_CONF_Exported_Defines - * @{ - */ - -#define USBH_MAX_NUM_ENDPOINTS 2U -#define USBH_MAX_NUM_INTERFACES 2U -#define USBH_MAX_NUM_CONFIGURATION 1U -#define USBH_KEEP_CFG_DESCRIPTOR 1U -#define USBH_MAX_NUM_SUPPORTED_CLASS 1U -#define USBH_MAX_SIZE_CONFIGURATION 0x200U -#define USBH_MAX_DATA_BUFFER 0x200U -#define USBH_DEBUG_LEVEL 2U -#define USBH_USE_OS 1U - -/** @defgroup USBH_Exported_Macros - * @{ - */ - - /* Memory management macros */ -#define USBH_malloc malloc -#define USBH_free free -#define USBH_memset memset -#define USBH_memcpy memcpy - -/* DEBUG macros */ -#if (USBH_DEBUG_LEVEL > 0U) -#define USBH_UsrLog(...) do { \ - printf(__VA_ARGS__); \ - printf("\n"); \ -} while (0) -#else -#define USBH_UsrLog(...) do {} while (0) -#endif - -#if (USBH_DEBUG_LEVEL > 1U) - -#define USBH_ErrLog(...) do { \ - printf("ERROR: ") ; \ - printf(__VA_ARGS__); \ - printf("\n"); \ -} while (0) -#else -#define USBH_ErrLog(...) do {} while (0) -#endif - -#if (USBH_DEBUG_LEVEL > 2U) -#define USBH_DbgLog(...) do { \ - printf("DEBUG : ") ; \ - printf(__VA_ARGS__); \ - printf("\n"); \ -} while (0) -#else -#define USBH_DbgLog(...) do {} while (0) -#endif - -/** - * @} - */ - - - -/** - * @} - */ - - -/** @defgroup USBH_CONF_Exported_Types - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBH_CONF_Exported_Macros - * @{ - */ -/** - * @} - */ - -/** @defgroup USBH_CONF_Exported_Variables - * @{ - */ -/** - * @} - */ - -/** @defgroup USBH_CONF_Exported_FunctionsPrototype - * @{ - */ -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USBH_CONF_TEMPLATE_H */ - - -/** - * @} - */ - -/** - * @} - */ -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbh_conf_template.h + * @author MCD Application Team + * @brief Header file for usbh_conf_template.c + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USBH_CONF_TEMPLATE_H +#define __USBH_CONF_TEMPLATE_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "stm32fxxx.h" /* replace 'stm32xxx' with your HAL driver header filename, ex: stm32f4xx.h */ +#include +#include +#include + +/** @addtogroup STM32_USB_HOST_LIBRARY + * @{ + */ + +/** @defgroup USBH_CONF + * @brief usb host low level driver configuration file + * @{ + */ + +/** @defgroup USBH_CONF_Exported_Defines + * @{ + */ + +#define USBH_MAX_NUM_ENDPOINTS 2U +#define USBH_MAX_NUM_INTERFACES 2U +#define USBH_MAX_NUM_CONFIGURATION 1U +#define USBH_KEEP_CFG_DESCRIPTOR 1U +#define USBH_MAX_NUM_SUPPORTED_CLASS 1U +#define USBH_MAX_SIZE_CONFIGURATION 0x200U +#define USBH_MAX_DATA_BUFFER 0x200U +#define USBH_DEBUG_LEVEL 2U +#define USBH_USE_OS 1U + +/** @defgroup USBH_Exported_Macros + * @{ + */ + + /* Memory management macros */ +#define USBH_malloc malloc +#define USBH_free free +#define USBH_memset memset +#define USBH_memcpy memcpy + +/* DEBUG macros */ +#if (USBH_DEBUG_LEVEL > 0U) +#define USBH_UsrLog(...) do { \ + printf(__VA_ARGS__); \ + printf("\n"); \ +} while (0) +#else +#define USBH_UsrLog(...) do {} while (0) +#endif + +#if (USBH_DEBUG_LEVEL > 1U) + +#define USBH_ErrLog(...) do { \ + printf("ERROR: ") ; \ + printf(__VA_ARGS__); \ + printf("\n"); \ +} while (0) +#else +#define USBH_ErrLog(...) do {} while (0) +#endif + +#if (USBH_DEBUG_LEVEL > 2U) +#define USBH_DbgLog(...) do { \ + printf("DEBUG : ") ; \ + printf(__VA_ARGS__); \ + printf("\n"); \ +} while (0) +#else +#define USBH_DbgLog(...) do {} while (0) +#endif + +/** + * @} + */ + + + +/** + * @} + */ + + +/** @defgroup USBH_CONF_Exported_Types + * @{ + */ +/** + * @} + */ + + +/** @defgroup USBH_CONF_Exported_Macros + * @{ + */ +/** + * @} + */ + +/** @defgroup USBH_CONF_Exported_Variables + * @{ + */ +/** + * @} + */ + +/** @defgroup USBH_CONF_Exported_FunctionsPrototype + * @{ + */ +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USBH_CONF_TEMPLATE_H */ + + +/** + * @} + */ + +/** + * @} + */ +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Core/Inc/usbh_core.h b/system/Middlewares/ST/STM32_USB_Host_Library/Core/Inc/usbh_core.h index ef1a8f9ec6..bd166fea05 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Core/Inc/usbh_core.h +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Core/Inc/usbh_core.h @@ -1,192 +1,192 @@ -/** - ****************************************************************************** - * @file usbh_core.h - * @author MCD Application Team - * @brief Header file for usbh_core.c - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ -/* Define to prevent recursive ----------------------------------------------*/ -#ifndef __USBH_CORE_H -#define __USBH_CORE_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_conf.h" -#include "usbh_def.h" -#include "usbh_ioreq.h" -#include "usbh_pipes.h" -#include "usbh_ctlreq.h" - -/** @addtogroup USBH_LIB - * @{ - */ - -/** @addtogroup USBH_LIB_CORE -* @{ -*/ - -/** @defgroup USBH_CORE - * @brief This file is the Header file for usbh_core.c - * @{ - */ - - -/** @defgroup USBH_CORE_Exported_Defines - * @{ - */ - -/** - * @} - */ -#define HOST_USER_SELECT_CONFIGURATION 0x01U -#define HOST_USER_CLASS_ACTIVE 0x02U -#define HOST_USER_CLASS_SELECTED 0x03U -#define HOST_USER_CONNECTION 0x04U -#define HOST_USER_DISCONNECTION 0x05U -#define HOST_USER_UNRECOVERED_ERROR 0x06U - - -/** - * @} - */ - - - -/** @defgroup USBH_CORE_Exported_Macros - * @{ - */ - -/** - * @} - */ - -/** @defgroup USBH_CORE_Exported_Variables - * @{ - */ - -/** - * @} - */ - -/** @defgroup USBH_CORE_Exported_FunctionsPrototype - * @{ - */ - - -USBH_StatusTypeDef USBH_Init(USBH_HandleTypeDef *phost, void (*pUsrFunc)(USBH_HandleTypeDef *phost, uint8_t id), uint8_t id); -USBH_StatusTypeDef USBH_DeInit(USBH_HandleTypeDef *phost); -USBH_StatusTypeDef USBH_RegisterClass(USBH_HandleTypeDef *phost, USBH_ClassTypeDef *pclass); -USBH_StatusTypeDef USBH_SelectInterface(USBH_HandleTypeDef *phost, uint8_t interface); -uint8_t USBH_FindInterface(USBH_HandleTypeDef *phost, - uint8_t Class, - uint8_t SubClass, - uint8_t Protocol); -uint8_t USBH_GetActiveClass(USBH_HandleTypeDef *phost); - -uint8_t USBH_FindInterfaceIndex(USBH_HandleTypeDef *phost, - uint8_t interface_number, - uint8_t alt_settings); - -uint8_t USBH_IsPortEnabled (USBH_HandleTypeDef *phost); - -USBH_StatusTypeDef USBH_Start (USBH_HandleTypeDef *phost); -USBH_StatusTypeDef USBH_Stop (USBH_HandleTypeDef *phost); -USBH_StatusTypeDef USBH_Process (USBH_HandleTypeDef *phost); -USBH_StatusTypeDef USBH_ReEnumerate (USBH_HandleTypeDef *phost); - -/* USBH Low Level Driver */ -USBH_StatusTypeDef USBH_LL_Init (USBH_HandleTypeDef *phost); -USBH_StatusTypeDef USBH_LL_DeInit (USBH_HandleTypeDef *phost); -USBH_StatusTypeDef USBH_LL_Start (USBH_HandleTypeDef *phost); -USBH_StatusTypeDef USBH_LL_Stop (USBH_HandleTypeDef *phost); - -USBH_StatusTypeDef USBH_LL_Connect (USBH_HandleTypeDef *phost); -USBH_StatusTypeDef USBH_LL_Disconnect (USBH_HandleTypeDef *phost); -USBH_SpeedTypeDef USBH_LL_GetSpeed (USBH_HandleTypeDef *phost); -USBH_StatusTypeDef USBH_LL_ResetPort (USBH_HandleTypeDef *phost); -uint32_t USBH_LL_GetLastXferSize (USBH_HandleTypeDef *phost, - uint8_t pipe); - -USBH_StatusTypeDef USBH_LL_DriverVBUS (USBH_HandleTypeDef *phost, - uint8_t state); - -USBH_StatusTypeDef USBH_LL_OpenPipe (USBH_HandleTypeDef *phost, - uint8_t pipe, - uint8_t epnum, - uint8_t dev_address, - uint8_t speed, - uint8_t ep_type, - uint16_t mps); - -USBH_StatusTypeDef USBH_LL_ClosePipe (USBH_HandleTypeDef *phost, - uint8_t pipe); - -USBH_StatusTypeDef USBH_LL_SubmitURB (USBH_HandleTypeDef *phost, - uint8_t pipe, - uint8_t direction, - uint8_t ep_type, - uint8_t token, - uint8_t* pbuff, - uint16_t length, - uint8_t do_ping); - -USBH_URBStateTypeDef USBH_LL_GetURBState (USBH_HandleTypeDef *phost, - uint8_t pipe); - -#if (USBH_USE_OS == 1U) -USBH_StatusTypeDef USBH_LL_NotifyURBChange (USBH_HandleTypeDef *phost); -#endif - -USBH_StatusTypeDef USBH_LL_SetToggle (USBH_HandleTypeDef *phost, - uint8_t pipe, uint8_t toggle); - -uint8_t USBH_LL_GetToggle (USBH_HandleTypeDef *phost, uint8_t pipe); - -void USBH_LL_PortDisabled (USBH_HandleTypeDef *phost); -void USBH_LL_PortEnabled (USBH_HandleTypeDef *phost); - -/* USBH Time base */ -void USBH_LL_SetTimer (USBH_HandleTypeDef *phost, uint32_t time); -void USBH_LL_IncTimer (USBH_HandleTypeDef *phost); - -void USBH_Delay (uint32_t Delay); - -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USBH_CORE_H */ -/** - * @} - */ - -/** - * @} - */ - -/** -* @} -*/ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - - - +/** + ****************************************************************************** + * @file usbh_core.h + * @author MCD Application Team + * @brief Header file for usbh_core.c + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ +/* Define to prevent recursive ----------------------------------------------*/ +#ifndef __USBH_CORE_H +#define __USBH_CORE_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_conf.h" +#include "usbh_def.h" +#include "usbh_ioreq.h" +#include "usbh_pipes.h" +#include "usbh_ctlreq.h" + +/** @addtogroup USBH_LIB + * @{ + */ + +/** @addtogroup USBH_LIB_CORE +* @{ +*/ + +/** @defgroup USBH_CORE + * @brief This file is the Header file for usbh_core.c + * @{ + */ + + +/** @defgroup USBH_CORE_Exported_Defines + * @{ + */ + +/** + * @} + */ +#define HOST_USER_SELECT_CONFIGURATION 0x01U +#define HOST_USER_CLASS_ACTIVE 0x02U +#define HOST_USER_CLASS_SELECTED 0x03U +#define HOST_USER_CONNECTION 0x04U +#define HOST_USER_DISCONNECTION 0x05U +#define HOST_USER_UNRECOVERED_ERROR 0x06U + + +/** + * @} + */ + + + +/** @defgroup USBH_CORE_Exported_Macros + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBH_CORE_Exported_Variables + * @{ + */ + +/** + * @} + */ + +/** @defgroup USBH_CORE_Exported_FunctionsPrototype + * @{ + */ + + +USBH_StatusTypeDef USBH_Init(USBH_HandleTypeDef *phost, void (*pUsrFunc)(USBH_HandleTypeDef *phost, uint8_t id), uint8_t id); +USBH_StatusTypeDef USBH_DeInit(USBH_HandleTypeDef *phost); +USBH_StatusTypeDef USBH_RegisterClass(USBH_HandleTypeDef *phost, USBH_ClassTypeDef *pclass); +USBH_StatusTypeDef USBH_SelectInterface(USBH_HandleTypeDef *phost, uint8_t interface); +uint8_t USBH_FindInterface(USBH_HandleTypeDef *phost, + uint8_t Class, + uint8_t SubClass, + uint8_t Protocol); +uint8_t USBH_GetActiveClass(USBH_HandleTypeDef *phost); + +uint8_t USBH_FindInterfaceIndex(USBH_HandleTypeDef *phost, + uint8_t interface_number, + uint8_t alt_settings); + +uint8_t USBH_IsPortEnabled (USBH_HandleTypeDef *phost); + +USBH_StatusTypeDef USBH_Start (USBH_HandleTypeDef *phost); +USBH_StatusTypeDef USBH_Stop (USBH_HandleTypeDef *phost); +USBH_StatusTypeDef USBH_Process (USBH_HandleTypeDef *phost); +USBH_StatusTypeDef USBH_ReEnumerate (USBH_HandleTypeDef *phost); + +/* USBH Low Level Driver */ +USBH_StatusTypeDef USBH_LL_Init (USBH_HandleTypeDef *phost); +USBH_StatusTypeDef USBH_LL_DeInit (USBH_HandleTypeDef *phost); +USBH_StatusTypeDef USBH_LL_Start (USBH_HandleTypeDef *phost); +USBH_StatusTypeDef USBH_LL_Stop (USBH_HandleTypeDef *phost); + +USBH_StatusTypeDef USBH_LL_Connect (USBH_HandleTypeDef *phost); +USBH_StatusTypeDef USBH_LL_Disconnect (USBH_HandleTypeDef *phost); +USBH_SpeedTypeDef USBH_LL_GetSpeed (USBH_HandleTypeDef *phost); +USBH_StatusTypeDef USBH_LL_ResetPort (USBH_HandleTypeDef *phost); +uint32_t USBH_LL_GetLastXferSize (USBH_HandleTypeDef *phost, + uint8_t pipe); + +USBH_StatusTypeDef USBH_LL_DriverVBUS (USBH_HandleTypeDef *phost, + uint8_t state); + +USBH_StatusTypeDef USBH_LL_OpenPipe (USBH_HandleTypeDef *phost, + uint8_t pipe, + uint8_t epnum, + uint8_t dev_address, + uint8_t speed, + uint8_t ep_type, + uint16_t mps); + +USBH_StatusTypeDef USBH_LL_ClosePipe (USBH_HandleTypeDef *phost, + uint8_t pipe); + +USBH_StatusTypeDef USBH_LL_SubmitURB (USBH_HandleTypeDef *phost, + uint8_t pipe, + uint8_t direction, + uint8_t ep_type, + uint8_t token, + uint8_t* pbuff, + uint16_t length, + uint8_t do_ping); + +USBH_URBStateTypeDef USBH_LL_GetURBState (USBH_HandleTypeDef *phost, + uint8_t pipe); + +#if (USBH_USE_OS == 1U) +USBH_StatusTypeDef USBH_LL_NotifyURBChange (USBH_HandleTypeDef *phost); +#endif + +USBH_StatusTypeDef USBH_LL_SetToggle (USBH_HandleTypeDef *phost, + uint8_t pipe, uint8_t toggle); + +uint8_t USBH_LL_GetToggle (USBH_HandleTypeDef *phost, uint8_t pipe); + +void USBH_LL_PortDisabled (USBH_HandleTypeDef *phost); +void USBH_LL_PortEnabled (USBH_HandleTypeDef *phost); + +/* USBH Time base */ +void USBH_LL_SetTimer (USBH_HandleTypeDef *phost, uint32_t time); +void USBH_LL_IncTimer (USBH_HandleTypeDef *phost); + +void USBH_Delay (uint32_t Delay); + +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USBH_CORE_H */ +/** + * @} + */ + +/** + * @} + */ + +/** +* @} +*/ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + + + diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Core/Inc/usbh_ctlreq.h b/system/Middlewares/ST/STM32_USB_Host_Library/Core/Inc/usbh_ctlreq.h index 3e98b69610..2594980533 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Core/Inc/usbh_ctlreq.h +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Core/Inc/usbh_ctlreq.h @@ -1,141 +1,141 @@ -/** - ****************************************************************************** - * @file usbh_ctlreq.h - * @author MCD Application Team - * @brief Header file for usbh_ctlreq.c - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive ----------------------------------------------*/ -#ifndef __USBH_CTLREQ_H -#define __USBH_CTLREQ_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_core.h" - -/** @addtogroup USBH_LIB - * @{ - */ - -/** @addtogroup USBH_LIB_CORE -* @{ -*/ - -/** @defgroup USBH_CTLREQ - * @brief This file is the - * @{ - */ - - -/** @defgroup USBH_CTLREQ_Exported_Defines - * @{ - */ -/*Standard Feature Selector for clear feature command*/ -#define FEATURE_SELECTOR_ENDPOINT 0x00U -#define FEATURE_SELECTOR_DEVICE 0x01U -#define FEATURE_SELECTOR_REMOTEWAKEUP 0X01U - - -#define INTERFACE_DESC_TYPE 0x04U -#define ENDPOINT_DESC_TYPE 0x05U -#define INTERFACE_DESC_SIZE 0x09U - -/** - * @} - */ - - -/** @defgroup USBH_CTLREQ_Exported_Types - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBH_CTLREQ_Exported_Macros - * @{ - */ -/** - * @} - */ - -/** @defgroup USBH_CTLREQ_Exported_Variables - * @{ - */ -extern uint8_t USBH_CfgDesc[512]; -/** - * @} - */ - -/** @defgroup USBH_CTLREQ_Exported_FunctionsPrototype - * @{ - */ -USBH_StatusTypeDef USBH_CtlReq (USBH_HandleTypeDef *phost, uint8_t *buff, - uint16_t length); - -USBH_StatusTypeDef USBH_GetDescriptor (USBH_HandleTypeDef *phost, - uint8_t req_type, uint16_t value_idx, - uint8_t* buff, uint16_t length); - -USBH_StatusTypeDef USBH_Get_DevDesc (USBH_HandleTypeDef *phost, uint8_t length); - -USBH_StatusTypeDef USBH_Get_StringDesc (USBH_HandleTypeDef *phost, - uint8_t string_index, uint8_t *buff, - uint16_t length); - -USBH_StatusTypeDef USBH_SetCfg (USBH_HandleTypeDef *phost, uint16_t cfg_idx); - -USBH_StatusTypeDef USBH_Get_CfgDesc (USBH_HandleTypeDef *phost,uint16_t length); - -USBH_StatusTypeDef USBH_SetAddress (USBH_HandleTypeDef *phost, - uint8_t DeviceAddress); - -USBH_StatusTypeDef USBH_SetInterface (USBH_HandleTypeDef *phost, uint8_t ep_num, - uint8_t altSetting); - -USBH_StatusTypeDef USBH_SetFeature (USBH_HandleTypeDef *phost, uint8_t wValue); - -USBH_StatusTypeDef USBH_ClrFeature (USBH_HandleTypeDef *phost, uint8_t ep_num); - -USBH_DescHeader_t *USBH_GetNextDesc (uint8_t *pbuf, uint16_t *ptr); -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USBH_CTLREQ_H */ - -/** - * @} - */ - -/** - * @} - */ - -/** -* @} -*/ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - - +/** + ****************************************************************************** + * @file usbh_ctlreq.h + * @author MCD Application Team + * @brief Header file for usbh_ctlreq.c + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive ----------------------------------------------*/ +#ifndef __USBH_CTLREQ_H +#define __USBH_CTLREQ_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_core.h" + +/** @addtogroup USBH_LIB + * @{ + */ + +/** @addtogroup USBH_LIB_CORE +* @{ +*/ + +/** @defgroup USBH_CTLREQ + * @brief This file is the + * @{ + */ + + +/** @defgroup USBH_CTLREQ_Exported_Defines + * @{ + */ +/*Standard Feature Selector for clear feature command*/ +#define FEATURE_SELECTOR_ENDPOINT 0x00U +#define FEATURE_SELECTOR_DEVICE 0x01U +#define FEATURE_SELECTOR_REMOTEWAKEUP 0X01U + + +#define INTERFACE_DESC_TYPE 0x04U +#define ENDPOINT_DESC_TYPE 0x05U +#define INTERFACE_DESC_SIZE 0x09U + +/** + * @} + */ + + +/** @defgroup USBH_CTLREQ_Exported_Types + * @{ + */ +/** + * @} + */ + + +/** @defgroup USBH_CTLREQ_Exported_Macros + * @{ + */ +/** + * @} + */ + +/** @defgroup USBH_CTLREQ_Exported_Variables + * @{ + */ +extern uint8_t USBH_CfgDesc[512]; +/** + * @} + */ + +/** @defgroup USBH_CTLREQ_Exported_FunctionsPrototype + * @{ + */ +USBH_StatusTypeDef USBH_CtlReq (USBH_HandleTypeDef *phost, uint8_t *buff, + uint16_t length); + +USBH_StatusTypeDef USBH_GetDescriptor (USBH_HandleTypeDef *phost, + uint8_t req_type, uint16_t value_idx, + uint8_t* buff, uint16_t length); + +USBH_StatusTypeDef USBH_Get_DevDesc (USBH_HandleTypeDef *phost, uint8_t length); + +USBH_StatusTypeDef USBH_Get_StringDesc (USBH_HandleTypeDef *phost, + uint8_t string_index, uint8_t *buff, + uint16_t length); + +USBH_StatusTypeDef USBH_SetCfg (USBH_HandleTypeDef *phost, uint16_t cfg_idx); + +USBH_StatusTypeDef USBH_Get_CfgDesc (USBH_HandleTypeDef *phost,uint16_t length); + +USBH_StatusTypeDef USBH_SetAddress (USBH_HandleTypeDef *phost, + uint8_t DeviceAddress); + +USBH_StatusTypeDef USBH_SetInterface (USBH_HandleTypeDef *phost, uint8_t ep_num, + uint8_t altSetting); + +USBH_StatusTypeDef USBH_SetFeature (USBH_HandleTypeDef *phost, uint8_t wValue); + +USBH_StatusTypeDef USBH_ClrFeature (USBH_HandleTypeDef *phost, uint8_t ep_num); + +USBH_DescHeader_t *USBH_GetNextDesc (uint8_t *pbuf, uint16_t *ptr); +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USBH_CTLREQ_H */ + +/** + * @} + */ + +/** + * @} + */ + +/** +* @} +*/ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + + diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Core/Inc/usbh_def.h b/system/Middlewares/ST/STM32_USB_Host_Library/Core/Inc/usbh_def.h index b4f8f2d50b..4feaf1956f 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Core/Inc/usbh_def.h +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Core/Inc/usbh_def.h @@ -1,494 +1,494 @@ -/** - ****************************************************************************** - * @file usbh_def.h - * @author MCD Application Team - * @brief Definitions used in the USB host library - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive ----------------------------------------------*/ -#ifndef USBH_DEF_H -#define USBH_DEF_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_conf.h" - -/** @addtogroup USBH_LIB - * @{ - */ - -/** @addtogroup USBH_LIB_CORE -* @{ -*/ - -/** @defgroup USBH_DEF - * @brief This file is includes USB descriptors - * @{ - */ - -#ifndef NULL -#define NULL 0U -#endif - -#ifndef FALSE -#define FALSE 0U -#endif - -#ifndef TRUE -#define TRUE 1U -#endif - - -#define ValBit(VAR,POS) (VAR & (1 << POS)) -#define SetBit(VAR,POS) (VAR |= (1 << POS)) -#define ClrBit(VAR,POS) (VAR &= ((1 << POS)^255)) - -#define LE16(addr) (((uint16_t)(addr)[0]) | \ - ((uint16_t)(((uint32_t)(addr)[1]) << 8))) - -#define LE24(addr) (((uint32_t)(addr)[0]) | \ - (((uint32_t)(addr)[1]) << 8) | \ - (((uint32_t)(addr)[2]) << 16)) - -#define LE32(addr) (((uint32_t)(addr)[0]) | \ - (((uint32_t)(addr)[1]) << 8) | \ - (((uint32_t)(addr)[2]) << 16) | \ - (((uint32_t)(addr)[3]) << 24)) - -#define LE64(addr) (((uint64_t)(addr)[0]) | \ - (((uint64_t)(addr)[1]) << 8) | \ - (((uint64_t)(addr)[2]) << 16) | \ - (((uint64_t)(addr)[3]) << 24) | \ - (((uint64_t)(addr)[4]) << 32) | \ - (((uint64_t)(addr)[5]) << 40) | \ - (((uint64_t)(addr)[6]) << 48) | \ - (((uint64_t)(addr)[7]) << 56)) - -#define LE16S(addr) ((int16_t)(LE16((addr)))) -#define LE24S(addr) ((int32_t)(LE24((addr)))) -#define LE32S(addr) ((int32_t)(LE32((addr)))) -#define LE64S(addr) ((int64_t)(LE64((addr)))) - - - -#define USB_LEN_DESC_HDR 0x02U -#define USB_LEN_DEV_DESC 0x12U -#define USB_LEN_CFG_DESC 0x09U -#define USB_LEN_IF_DESC 0x09U -#define USB_LEN_EP_DESC 0x07U -#define USB_LEN_OTG_DESC 0x03U -#define USB_LEN_SETUP_PKT 0x08U - -/* bmRequestType :D7 Data Phase Transfer Direction */ -#define USB_REQ_DIR_MASK 0x80U -#define USB_H2D 0x00U -#define USB_D2H 0x80U - -/* bmRequestType D6..5 Type */ -#define USB_REQ_TYPE_STANDARD 0x00U -#define USB_REQ_TYPE_CLASS 0x20U -#define USB_REQ_TYPE_VENDOR 0x40U -#define USB_REQ_TYPE_RESERVED 0x60U - -/* bmRequestType D4..0 Recipient */ -#define USB_REQ_RECIPIENT_DEVICE 0x00U -#define USB_REQ_RECIPIENT_INTERFACE 0x01U -#define USB_REQ_RECIPIENT_ENDPOINT 0x02U -#define USB_REQ_RECIPIENT_OTHER 0x03U - -/* Table 9-4. Standard Request Codes */ -/* bRequest , Value */ -#define USB_REQ_GET_STATUS 0x00U -#define USB_REQ_CLEAR_FEATURE 0x01U -#define USB_REQ_SET_FEATURE 0x03U -#define USB_REQ_SET_ADDRESS 0x05U -#define USB_REQ_GET_DESCRIPTOR 0x06U -#define USB_REQ_SET_DESCRIPTOR 0x07U -#define USB_REQ_GET_CONFIGURATION 0x08U -#define USB_REQ_SET_CONFIGURATION 0x09U -#define USB_REQ_GET_INTERFACE 0x0AU -#define USB_REQ_SET_INTERFACE 0x0BU -#define USB_REQ_SYNCH_FRAME 0x0CU - -/* Table 9-5. Descriptor Types of USB Specifications */ -#define USB_DESC_TYPE_DEVICE 0x01U -#define USB_DESC_TYPE_CONFIGURATION 0x02U -#define USB_DESC_TYPE_STRING 0x03U -#define USB_DESC_TYPE_INTERFACE 0x04U -#define USB_DESC_TYPE_ENDPOINT 0x05U -#define USB_DESC_TYPE_DEVICE_QUALIFIER 0x06U -#define USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION 0x07U -#define USB_DESC_TYPE_INTERFACE_POWER 0x08U -#define USB_DESC_TYPE_HID 0x21U -#define USB_DESC_TYPE_HID_REPORT 0x22U - - -#define USB_DEVICE_DESC_SIZE 0x12U -#define USB_CONFIGURATION_DESC_SIZE 0x09U -#define USB_HID_DESC_SIZE 0x09U -#define USB_INTERFACE_DESC_SIZE 0x09U -#define USB_ENDPOINT_DESC_SIZE 0x07U - -/* Descriptor Type and Descriptor Index */ -/* Use the following values when calling the function USBH_GetDescriptor */ -#define USB_DESC_DEVICE ((USB_DESC_TYPE_DEVICE << 8) & 0xFF00U) -#define USB_DESC_CONFIGURATION ((USB_DESC_TYPE_CONFIGURATION << 8) & 0xFF00U) -#define USB_DESC_STRING ((USB_DESC_TYPE_STRING << 8) & 0xFF00U) -#define USB_DESC_INTERFACE ((USB_DESC_TYPE_INTERFACE << 8) & 0xFF00U) -#define USB_DESC_ENDPOINT ((USB_DESC_TYPE_INTERFACE << 8) & 0xFF00U) -#define USB_DESC_DEVICE_QUALIFIER ((USB_DESC_TYPE_DEVICE_QUALIFIER << 8) & 0xFF00U) -#define USB_DESC_OTHER_SPEED_CONFIGURATION ((USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION << 8) & 0xFF00U) -#define USB_DESC_INTERFACE_POWER ((USB_DESC_TYPE_INTERFACE_POWER << 8) & 0xFF00U) -#define USB_DESC_HID_REPORT ((USB_DESC_TYPE_HID_REPORT << 8) & 0xFF00U) -#define USB_DESC_HID ((USB_DESC_TYPE_HID << 8) & 0xFF00U) - - -#define USB_EP_TYPE_CTRL 0x00U -#define USB_EP_TYPE_ISOC 0x01U -#define USB_EP_TYPE_BULK 0x02U -#define USB_EP_TYPE_INTR 0x03U - -#define USB_EP_DIR_OUT 0x00U -#define USB_EP_DIR_IN 0x80U -#define USB_EP_DIR_MSK 0x80U - -#ifndef USBH_MAX_PIPES_NBR - #define USBH_MAX_PIPES_NBR 15U -#endif /* USBH_MAX_PIPES_NBR */ - -#define USBH_DEVICE_ADDRESS_DEFAULT 0x00U -#define USBH_DEVICE_ADDRESS 0x01U - -#define USBH_MAX_ERROR_COUNT 0x02U - -#if (USBH_USE_OS == 1U) -#define MSGQUEUE_OBJECTS 0x10U -#endif - - -/** - * @} - */ - - -#define USBH_CONFIGURATION_DESCRIPTOR_SIZE (USB_CONFIGURATION_DESC_SIZE \ - + USB_INTERFACE_DESC_SIZE\ - + (USBH_MAX_NUM_ENDPOINTS * USB_ENDPOINT_DESC_SIZE)) - - -#define CONFIG_DESC_wTOTAL_LENGTH (ConfigurationDescriptorData.ConfigDescfield.\ - ConfigurationDescriptor.wTotalLength) - - -typedef union -{ - uint16_t w; - struct BW - { - uint8_t msb; - uint8_t lsb; - } - bw; -} -uint16_t_uint8_t; - - -typedef union _USB_Setup -{ - uint32_t d8[2]; - - struct _SetupPkt_Struc - { - uint8_t bmRequestType; - uint8_t bRequest; - uint16_t_uint8_t wValue; - uint16_t_uint8_t wIndex; - uint16_t_uint8_t wLength; - } b; -} -USB_Setup_TypeDef; - -typedef struct _DescHeader -{ - uint8_t bLength; - uint8_t bDescriptorType; -} -USBH_DescHeader_t; - -typedef struct _DeviceDescriptor -{ - uint8_t bLength; - uint8_t bDescriptorType; - uint16_t bcdUSB; /* USB Specification Number which device complies too */ - uint8_t bDeviceClass; - uint8_t bDeviceSubClass; - uint8_t bDeviceProtocol; - /* If equal to Zero, each interface specifies its own class - code if equal to 0xFF, the class code is vendor specified. - Otherwise field is valid Class Code.*/ - uint8_t bMaxPacketSize; - uint16_t idVendor; /* Vendor ID (Assigned by USB Org) */ - uint16_t idProduct; /* Product ID (Assigned by Manufacturer) */ - uint16_t bcdDevice; /* Device Release Number */ - uint8_t iManufacturer; /* Index of Manufacturer String Descriptor */ - uint8_t iProduct; /* Index of Product String Descriptor */ - uint8_t iSerialNumber; /* Index of Serial Number String Descriptor */ - uint8_t bNumConfigurations; /* Number of Possible Configurations */ -} -USBH_DevDescTypeDef; - -typedef struct _EndpointDescriptor -{ - uint8_t bLength; - uint8_t bDescriptorType; - uint8_t bEndpointAddress; /* indicates what endpoint this descriptor is describing */ - uint8_t bmAttributes; /* specifies the transfer type. */ - uint16_t wMaxPacketSize; /* Maximum Packet Size this endpoint is capable of sending or receiving */ - uint8_t bInterval; /* is used to specify the polling interval of certain transfers. */ -} -USBH_EpDescTypeDef; - -typedef struct _InterfaceDescriptor -{ - uint8_t bLength; - uint8_t bDescriptorType; - uint8_t bInterfaceNumber; - uint8_t bAlternateSetting; /* Value used to select alternative setting */ - uint8_t bNumEndpoints; /* Number of Endpoints used for this interface */ - uint8_t bInterfaceClass; /* Class Code (Assigned by USB Org) */ - uint8_t bInterfaceSubClass; /* Subclass Code (Assigned by USB Org) */ - uint8_t bInterfaceProtocol; /* Protocol Code */ - uint8_t iInterface; /* Index of String Descriptor Describing this interface */ - USBH_EpDescTypeDef Ep_Desc[USBH_MAX_NUM_ENDPOINTS]; -} -USBH_InterfaceDescTypeDef; - - -typedef struct _ConfigurationDescriptor -{ - uint8_t bLength; - uint8_t bDescriptorType; - uint16_t wTotalLength; /* Total Length of Data Returned */ - uint8_t bNumInterfaces; /* Number of Interfaces */ - uint8_t bConfigurationValue; /* Value to use as an argument to select this configuration*/ - uint8_t iConfiguration; /*Index of String Descriptor Describing this configuration */ - uint8_t bmAttributes; /* D7 Bus Powered , D6 Self Powered, D5 Remote Wakeup , D4..0 Reserved (0)*/ - uint8_t bMaxPower; /*Maximum Power Consumption */ - USBH_InterfaceDescTypeDef Itf_Desc[USBH_MAX_NUM_INTERFACES]; -} -USBH_CfgDescTypeDef; - - -/* Following USB Host status */ -typedef enum -{ - USBH_OK = 0, - USBH_BUSY, - USBH_FAIL, - USBH_NOT_SUPPORTED, - USBH_UNRECOVERED_ERROR, - USBH_ERROR_SPEED_UNKNOWN, -}USBH_StatusTypeDef; - - -/** @defgroup USBH_CORE_Exported_Types - * @{ - */ - -typedef enum -{ - USBH_SPEED_HIGH = 0U, - USBH_SPEED_FULL = 1U, - USBH_SPEED_LOW = 2U, - -}USBH_SpeedTypeDef; - -/* Following states are used for gState */ -typedef enum -{ - HOST_IDLE = 0U, - HOST_DEV_WAIT_FOR_ATTACHMENT, - HOST_DEV_ATTACHED, - HOST_DEV_DISCONNECTED, - HOST_DETECT_DEVICE_SPEED, - HOST_ENUMERATION, - HOST_CLASS_REQUEST, - HOST_INPUT, - HOST_SET_CONFIGURATION, - HOST_SET_WAKEUP_FEATURE, - HOST_CHECK_CLASS, - HOST_CLASS, - HOST_SUSPENDED, - HOST_ABORT_STATE, -}HOST_StateTypeDef; - -/* Following states are used for EnumerationState */ -typedef enum -{ - ENUM_IDLE = 0U, - ENUM_GET_FULL_DEV_DESC, - ENUM_SET_ADDR, - ENUM_GET_CFG_DESC, - ENUM_GET_FULL_CFG_DESC, - ENUM_GET_MFC_STRING_DESC, - ENUM_GET_PRODUCT_STRING_DESC, - ENUM_GET_SERIALNUM_STRING_DESC, -} ENUM_StateTypeDef; - -/* Following states are used for CtrlXferStateMachine */ -typedef enum -{ - CTRL_IDLE = 0U, - CTRL_SETUP, - CTRL_SETUP_WAIT, - CTRL_DATA_IN, - CTRL_DATA_IN_WAIT, - CTRL_DATA_OUT, - CTRL_DATA_OUT_WAIT, - CTRL_STATUS_IN, - CTRL_STATUS_IN_WAIT, - CTRL_STATUS_OUT, - CTRL_STATUS_OUT_WAIT, - CTRL_ERROR, - CTRL_STALLED, - CTRL_COMPLETE -}CTRL_StateTypeDef; - - -/* Following states are used for RequestState */ -typedef enum -{ - CMD_IDLE = 0U, - CMD_SEND, - CMD_WAIT -} CMD_StateTypeDef; - -typedef enum { - USBH_URB_IDLE = 0U, - USBH_URB_DONE, - USBH_URB_NOTREADY, - USBH_URB_NYET, - USBH_URB_ERROR, - USBH_URB_STALL -}USBH_URBStateTypeDef; - -typedef enum -{ - USBH_PORT_EVENT = 1U, - USBH_URB_EVENT, - USBH_CONTROL_EVENT, - USBH_CLASS_EVENT, - USBH_STATE_CHANGED_EVENT, -} -USBH_OSEventTypeDef; - -/* Control request structure */ -typedef struct -{ - uint8_t pipe_in; - uint8_t pipe_out; - uint8_t pipe_size; - uint8_t *buff; - uint16_t length; - uint16_t timer; - USB_Setup_TypeDef setup; - CTRL_StateTypeDef state; - uint8_t errorcount; - -} USBH_CtrlTypeDef; - -/* Attached device structure */ -typedef struct -{ -#if (USBH_KEEP_CFG_DESCRIPTOR == 1U) - uint8_t CfgDesc_Raw[USBH_MAX_SIZE_CONFIGURATION]; -#endif - uint8_t Data[USBH_MAX_DATA_BUFFER]; - uint8_t address; - uint8_t speed; - __IO uint8_t is_connected; - uint8_t PortEnabled; - uint8_t current_interface; - USBH_DevDescTypeDef DevDesc; - USBH_CfgDescTypeDef CfgDesc; - -}USBH_DeviceTypeDef; - -struct _USBH_HandleTypeDef; - -/* USB Host Class structure */ -typedef struct -{ - const char *Name; - uint8_t ClassCode; - USBH_StatusTypeDef (*Init) (struct _USBH_HandleTypeDef *phost); - USBH_StatusTypeDef (*DeInit) (struct _USBH_HandleTypeDef *phost); - USBH_StatusTypeDef (*Requests) (struct _USBH_HandleTypeDef *phost); - USBH_StatusTypeDef (*BgndProcess) (struct _USBH_HandleTypeDef *phost); - USBH_StatusTypeDef (*SOFProcess) (struct _USBH_HandleTypeDef *phost); - void* pData; -} USBH_ClassTypeDef; - -/* USB Host handle structure */ -typedef struct _USBH_HandleTypeDef -{ - __IO HOST_StateTypeDef gState; /* Host State Machine Value */ - ENUM_StateTypeDef EnumState; /* Enumeration state Machine */ - CMD_StateTypeDef RequestState; - USBH_CtrlTypeDef Control; - USBH_DeviceTypeDef device; - USBH_ClassTypeDef* pClass[USBH_MAX_NUM_SUPPORTED_CLASS]; - USBH_ClassTypeDef* pActiveClass; - uint32_t ClassNumber; - uint32_t Pipes[15]; - __IO uint32_t Timer; - uint8_t id; - void* pData; - void (* pUser )(struct _USBH_HandleTypeDef *pHandle, uint8_t id); - -#if (USBH_USE_OS == 1U) -#if osCMSIS < 0x20000 - osMessageQId os_event; - osThreadId thread; -#else - osMessageQueueId_t os_event; - osThreadId_t thread; -#endif - uint32_t os_msg; -#endif - -} USBH_HandleTypeDef; - - -#if defined ( __GNUC__ ) - #ifndef __weak - #define __weak __attribute__((weak)) - #endif /* __weak */ - #ifndef __packed - #define __packed __attribute__((__packed__)) - #endif /* __packed */ -#endif /* __GNUC__ */ - -#ifdef __cplusplus -} -#endif - -#endif /* USBH_DEF_H */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - +/** + ****************************************************************************** + * @file usbh_def.h + * @author MCD Application Team + * @brief Definitions used in the USB host library + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive ----------------------------------------------*/ +#ifndef USBH_DEF_H +#define USBH_DEF_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_conf.h" + +/** @addtogroup USBH_LIB + * @{ + */ + +/** @addtogroup USBH_LIB_CORE +* @{ +*/ + +/** @defgroup USBH_DEF + * @brief This file is includes USB descriptors + * @{ + */ + +#ifndef NULL +#define NULL 0U +#endif + +#ifndef FALSE +#define FALSE 0U +#endif + +#ifndef TRUE +#define TRUE 1U +#endif + + +#define ValBit(VAR,POS) (VAR & (1 << POS)) +#define SetBit(VAR,POS) (VAR |= (1 << POS)) +#define ClrBit(VAR,POS) (VAR &= ((1 << POS)^255)) + +#define LE16(addr) (((uint16_t)(addr)[0]) | \ + ((uint16_t)(((uint32_t)(addr)[1]) << 8))) + +#define LE24(addr) (((uint32_t)(addr)[0]) | \ + (((uint32_t)(addr)[1]) << 8) | \ + (((uint32_t)(addr)[2]) << 16)) + +#define LE32(addr) (((uint32_t)(addr)[0]) | \ + (((uint32_t)(addr)[1]) << 8) | \ + (((uint32_t)(addr)[2]) << 16) | \ + (((uint32_t)(addr)[3]) << 24)) + +#define LE64(addr) (((uint64_t)(addr)[0]) | \ + (((uint64_t)(addr)[1]) << 8) | \ + (((uint64_t)(addr)[2]) << 16) | \ + (((uint64_t)(addr)[3]) << 24) | \ + (((uint64_t)(addr)[4]) << 32) | \ + (((uint64_t)(addr)[5]) << 40) | \ + (((uint64_t)(addr)[6]) << 48) | \ + (((uint64_t)(addr)[7]) << 56)) + +#define LE16S(addr) ((int16_t)(LE16((addr)))) +#define LE24S(addr) ((int32_t)(LE24((addr)))) +#define LE32S(addr) ((int32_t)(LE32((addr)))) +#define LE64S(addr) ((int64_t)(LE64((addr)))) + + + +#define USB_LEN_DESC_HDR 0x02U +#define USB_LEN_DEV_DESC 0x12U +#define USB_LEN_CFG_DESC 0x09U +#define USB_LEN_IF_DESC 0x09U +#define USB_LEN_EP_DESC 0x07U +#define USB_LEN_OTG_DESC 0x03U +#define USB_LEN_SETUP_PKT 0x08U + +/* bmRequestType :D7 Data Phase Transfer Direction */ +#define USB_REQ_DIR_MASK 0x80U +#define USB_H2D 0x00U +#define USB_D2H 0x80U + +/* bmRequestType D6..5 Type */ +#define USB_REQ_TYPE_STANDARD 0x00U +#define USB_REQ_TYPE_CLASS 0x20U +#define USB_REQ_TYPE_VENDOR 0x40U +#define USB_REQ_TYPE_RESERVED 0x60U + +/* bmRequestType D4..0 Recipient */ +#define USB_REQ_RECIPIENT_DEVICE 0x00U +#define USB_REQ_RECIPIENT_INTERFACE 0x01U +#define USB_REQ_RECIPIENT_ENDPOINT 0x02U +#define USB_REQ_RECIPIENT_OTHER 0x03U + +/* Table 9-4. Standard Request Codes */ +/* bRequest , Value */ +#define USB_REQ_GET_STATUS 0x00U +#define USB_REQ_CLEAR_FEATURE 0x01U +#define USB_REQ_SET_FEATURE 0x03U +#define USB_REQ_SET_ADDRESS 0x05U +#define USB_REQ_GET_DESCRIPTOR 0x06U +#define USB_REQ_SET_DESCRIPTOR 0x07U +#define USB_REQ_GET_CONFIGURATION 0x08U +#define USB_REQ_SET_CONFIGURATION 0x09U +#define USB_REQ_GET_INTERFACE 0x0AU +#define USB_REQ_SET_INTERFACE 0x0BU +#define USB_REQ_SYNCH_FRAME 0x0CU + +/* Table 9-5. Descriptor Types of USB Specifications */ +#define USB_DESC_TYPE_DEVICE 0x01U +#define USB_DESC_TYPE_CONFIGURATION 0x02U +#define USB_DESC_TYPE_STRING 0x03U +#define USB_DESC_TYPE_INTERFACE 0x04U +#define USB_DESC_TYPE_ENDPOINT 0x05U +#define USB_DESC_TYPE_DEVICE_QUALIFIER 0x06U +#define USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION 0x07U +#define USB_DESC_TYPE_INTERFACE_POWER 0x08U +#define USB_DESC_TYPE_HID 0x21U +#define USB_DESC_TYPE_HID_REPORT 0x22U + + +#define USB_DEVICE_DESC_SIZE 0x12U +#define USB_CONFIGURATION_DESC_SIZE 0x09U +#define USB_HID_DESC_SIZE 0x09U +#define USB_INTERFACE_DESC_SIZE 0x09U +#define USB_ENDPOINT_DESC_SIZE 0x07U + +/* Descriptor Type and Descriptor Index */ +/* Use the following values when calling the function USBH_GetDescriptor */ +#define USB_DESC_DEVICE ((USB_DESC_TYPE_DEVICE << 8) & 0xFF00U) +#define USB_DESC_CONFIGURATION ((USB_DESC_TYPE_CONFIGURATION << 8) & 0xFF00U) +#define USB_DESC_STRING ((USB_DESC_TYPE_STRING << 8) & 0xFF00U) +#define USB_DESC_INTERFACE ((USB_DESC_TYPE_INTERFACE << 8) & 0xFF00U) +#define USB_DESC_ENDPOINT ((USB_DESC_TYPE_INTERFACE << 8) & 0xFF00U) +#define USB_DESC_DEVICE_QUALIFIER ((USB_DESC_TYPE_DEVICE_QUALIFIER << 8) & 0xFF00U) +#define USB_DESC_OTHER_SPEED_CONFIGURATION ((USB_DESC_TYPE_OTHER_SPEED_CONFIGURATION << 8) & 0xFF00U) +#define USB_DESC_INTERFACE_POWER ((USB_DESC_TYPE_INTERFACE_POWER << 8) & 0xFF00U) +#define USB_DESC_HID_REPORT ((USB_DESC_TYPE_HID_REPORT << 8) & 0xFF00U) +#define USB_DESC_HID ((USB_DESC_TYPE_HID << 8) & 0xFF00U) + + +#define USB_EP_TYPE_CTRL 0x00U +#define USB_EP_TYPE_ISOC 0x01U +#define USB_EP_TYPE_BULK 0x02U +#define USB_EP_TYPE_INTR 0x03U + +#define USB_EP_DIR_OUT 0x00U +#define USB_EP_DIR_IN 0x80U +#define USB_EP_DIR_MSK 0x80U + +#ifndef USBH_MAX_PIPES_NBR + #define USBH_MAX_PIPES_NBR 15U +#endif /* USBH_MAX_PIPES_NBR */ + +#define USBH_DEVICE_ADDRESS_DEFAULT 0x00U +#define USBH_DEVICE_ADDRESS 0x01U + +#define USBH_MAX_ERROR_COUNT 0x02U + +#if (USBH_USE_OS == 1U) +#define MSGQUEUE_OBJECTS 0x10U +#endif + + +/** + * @} + */ + + +#define USBH_CONFIGURATION_DESCRIPTOR_SIZE (USB_CONFIGURATION_DESC_SIZE \ + + USB_INTERFACE_DESC_SIZE\ + + (USBH_MAX_NUM_ENDPOINTS * USB_ENDPOINT_DESC_SIZE)) + + +#define CONFIG_DESC_wTOTAL_LENGTH (ConfigurationDescriptorData.ConfigDescfield.\ + ConfigurationDescriptor.wTotalLength) + + +typedef union +{ + uint16_t w; + struct BW + { + uint8_t msb; + uint8_t lsb; + } + bw; +} +uint16_t_uint8_t; + + +typedef union _USB_Setup +{ + uint32_t d8[2]; + + struct _SetupPkt_Struc + { + uint8_t bmRequestType; + uint8_t bRequest; + uint16_t_uint8_t wValue; + uint16_t_uint8_t wIndex; + uint16_t_uint8_t wLength; + } b; +} +USB_Setup_TypeDef; + +typedef struct _DescHeader +{ + uint8_t bLength; + uint8_t bDescriptorType; +} +USBH_DescHeader_t; + +typedef struct _DeviceDescriptor +{ + uint8_t bLength; + uint8_t bDescriptorType; + uint16_t bcdUSB; /* USB Specification Number which device complies too */ + uint8_t bDeviceClass; + uint8_t bDeviceSubClass; + uint8_t bDeviceProtocol; + /* If equal to Zero, each interface specifies its own class + code if equal to 0xFF, the class code is vendor specified. + Otherwise field is valid Class Code.*/ + uint8_t bMaxPacketSize; + uint16_t idVendor; /* Vendor ID (Assigned by USB Org) */ + uint16_t idProduct; /* Product ID (Assigned by Manufacturer) */ + uint16_t bcdDevice; /* Device Release Number */ + uint8_t iManufacturer; /* Index of Manufacturer String Descriptor */ + uint8_t iProduct; /* Index of Product String Descriptor */ + uint8_t iSerialNumber; /* Index of Serial Number String Descriptor */ + uint8_t bNumConfigurations; /* Number of Possible Configurations */ +} +USBH_DevDescTypeDef; + +typedef struct _EndpointDescriptor +{ + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bEndpointAddress; /* indicates what endpoint this descriptor is describing */ + uint8_t bmAttributes; /* specifies the transfer type. */ + uint16_t wMaxPacketSize; /* Maximum Packet Size this endpoint is capable of sending or receiving */ + uint8_t bInterval; /* is used to specify the polling interval of certain transfers. */ +} +USBH_EpDescTypeDef; + +typedef struct _InterfaceDescriptor +{ + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bInterfaceNumber; + uint8_t bAlternateSetting; /* Value used to select alternative setting */ + uint8_t bNumEndpoints; /* Number of Endpoints used for this interface */ + uint8_t bInterfaceClass; /* Class Code (Assigned by USB Org) */ + uint8_t bInterfaceSubClass; /* Subclass Code (Assigned by USB Org) */ + uint8_t bInterfaceProtocol; /* Protocol Code */ + uint8_t iInterface; /* Index of String Descriptor Describing this interface */ + USBH_EpDescTypeDef Ep_Desc[USBH_MAX_NUM_ENDPOINTS]; +} +USBH_InterfaceDescTypeDef; + + +typedef struct _ConfigurationDescriptor +{ + uint8_t bLength; + uint8_t bDescriptorType; + uint16_t wTotalLength; /* Total Length of Data Returned */ + uint8_t bNumInterfaces; /* Number of Interfaces */ + uint8_t bConfigurationValue; /* Value to use as an argument to select this configuration*/ + uint8_t iConfiguration; /*Index of String Descriptor Describing this configuration */ + uint8_t bmAttributes; /* D7 Bus Powered , D6 Self Powered, D5 Remote Wakeup , D4..0 Reserved (0)*/ + uint8_t bMaxPower; /*Maximum Power Consumption */ + USBH_InterfaceDescTypeDef Itf_Desc[USBH_MAX_NUM_INTERFACES]; +} +USBH_CfgDescTypeDef; + + +/* Following USB Host status */ +typedef enum +{ + USBH_OK = 0, + USBH_BUSY, + USBH_FAIL, + USBH_NOT_SUPPORTED, + USBH_UNRECOVERED_ERROR, + USBH_ERROR_SPEED_UNKNOWN, +}USBH_StatusTypeDef; + + +/** @defgroup USBH_CORE_Exported_Types + * @{ + */ + +typedef enum +{ + USBH_SPEED_HIGH = 0U, + USBH_SPEED_FULL = 1U, + USBH_SPEED_LOW = 2U, + +}USBH_SpeedTypeDef; + +/* Following states are used for gState */ +typedef enum +{ + HOST_IDLE = 0U, + HOST_DEV_WAIT_FOR_ATTACHMENT, + HOST_DEV_ATTACHED, + HOST_DEV_DISCONNECTED, + HOST_DETECT_DEVICE_SPEED, + HOST_ENUMERATION, + HOST_CLASS_REQUEST, + HOST_INPUT, + HOST_SET_CONFIGURATION, + HOST_SET_WAKEUP_FEATURE, + HOST_CHECK_CLASS, + HOST_CLASS, + HOST_SUSPENDED, + HOST_ABORT_STATE, +}HOST_StateTypeDef; + +/* Following states are used for EnumerationState */ +typedef enum +{ + ENUM_IDLE = 0U, + ENUM_GET_FULL_DEV_DESC, + ENUM_SET_ADDR, + ENUM_GET_CFG_DESC, + ENUM_GET_FULL_CFG_DESC, + ENUM_GET_MFC_STRING_DESC, + ENUM_GET_PRODUCT_STRING_DESC, + ENUM_GET_SERIALNUM_STRING_DESC, +} ENUM_StateTypeDef; + +/* Following states are used for CtrlXferStateMachine */ +typedef enum +{ + CTRL_IDLE = 0U, + CTRL_SETUP, + CTRL_SETUP_WAIT, + CTRL_DATA_IN, + CTRL_DATA_IN_WAIT, + CTRL_DATA_OUT, + CTRL_DATA_OUT_WAIT, + CTRL_STATUS_IN, + CTRL_STATUS_IN_WAIT, + CTRL_STATUS_OUT, + CTRL_STATUS_OUT_WAIT, + CTRL_ERROR, + CTRL_STALLED, + CTRL_COMPLETE +}CTRL_StateTypeDef; + + +/* Following states are used for RequestState */ +typedef enum +{ + CMD_IDLE = 0U, + CMD_SEND, + CMD_WAIT +} CMD_StateTypeDef; + +typedef enum { + USBH_URB_IDLE = 0U, + USBH_URB_DONE, + USBH_URB_NOTREADY, + USBH_URB_NYET, + USBH_URB_ERROR, + USBH_URB_STALL +}USBH_URBStateTypeDef; + +typedef enum +{ + USBH_PORT_EVENT = 1U, + USBH_URB_EVENT, + USBH_CONTROL_EVENT, + USBH_CLASS_EVENT, + USBH_STATE_CHANGED_EVENT, +} +USBH_OSEventTypeDef; + +/* Control request structure */ +typedef struct +{ + uint8_t pipe_in; + uint8_t pipe_out; + uint8_t pipe_size; + uint8_t *buff; + uint16_t length; + uint16_t timer; + USB_Setup_TypeDef setup; + CTRL_StateTypeDef state; + uint8_t errorcount; + +} USBH_CtrlTypeDef; + +/* Attached device structure */ +typedef struct +{ +#if (USBH_KEEP_CFG_DESCRIPTOR == 1U) + uint8_t CfgDesc_Raw[USBH_MAX_SIZE_CONFIGURATION]; +#endif + uint8_t Data[USBH_MAX_DATA_BUFFER]; + uint8_t address; + uint8_t speed; + __IO uint8_t is_connected; + uint8_t PortEnabled; + uint8_t current_interface; + USBH_DevDescTypeDef DevDesc; + USBH_CfgDescTypeDef CfgDesc; + +}USBH_DeviceTypeDef; + +struct _USBH_HandleTypeDef; + +/* USB Host Class structure */ +typedef struct +{ + const char *Name; + uint8_t ClassCode; + USBH_StatusTypeDef (*Init) (struct _USBH_HandleTypeDef *phost); + USBH_StatusTypeDef (*DeInit) (struct _USBH_HandleTypeDef *phost); + USBH_StatusTypeDef (*Requests) (struct _USBH_HandleTypeDef *phost); + USBH_StatusTypeDef (*BgndProcess) (struct _USBH_HandleTypeDef *phost); + USBH_StatusTypeDef (*SOFProcess) (struct _USBH_HandleTypeDef *phost); + void* pData; +} USBH_ClassTypeDef; + +/* USB Host handle structure */ +typedef struct _USBH_HandleTypeDef +{ + __IO HOST_StateTypeDef gState; /* Host State Machine Value */ + ENUM_StateTypeDef EnumState; /* Enumeration state Machine */ + CMD_StateTypeDef RequestState; + USBH_CtrlTypeDef Control; + USBH_DeviceTypeDef device; + USBH_ClassTypeDef* pClass[USBH_MAX_NUM_SUPPORTED_CLASS]; + USBH_ClassTypeDef* pActiveClass; + uint32_t ClassNumber; + uint32_t Pipes[15]; + __IO uint32_t Timer; + uint8_t id; + void* pData; + void (* pUser )(struct _USBH_HandleTypeDef *pHandle, uint8_t id); + +#if (USBH_USE_OS == 1U) +#if osCMSIS < 0x20000 + osMessageQId os_event; + osThreadId thread; +#else + osMessageQueueId_t os_event; + osThreadId_t thread; +#endif + uint32_t os_msg; +#endif + +} USBH_HandleTypeDef; + + +#if defined ( __GNUC__ ) + #ifndef __weak + #define __weak __attribute__((weak)) + #endif /* __weak */ + #ifndef __packed + #define __packed __attribute__((__packed__)) + #endif /* __packed */ +#endif /* __GNUC__ */ + +#ifdef __cplusplus +} +#endif + +#endif /* USBH_DEF_H */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Core/Inc/usbh_ioreq.h b/system/Middlewares/ST/STM32_USB_Host_Library/Core/Inc/usbh_ioreq.h index d0b2fc1fe2..406b67db74 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Core/Inc/usbh_ioreq.h +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Core/Inc/usbh_ioreq.h @@ -1,160 +1,160 @@ -/** - ****************************************************************************** - * @file usbh_ioreq.h - * @author MCD Application Team - * @brief Header file for usbh_ioreq.c - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive ----------------------------------------------*/ -#ifndef __USBH_IOREQ_H -#define __USBH_IOREQ_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_conf.h" -#include "usbh_core.h" - -/** @addtogroup USBH_LIB - * @{ - */ - -/** @addtogroup USBH_LIB_CORE -* @{ -*/ - -/** @defgroup USBH_IOREQ - * @brief This file is the header file for usbh_ioreq.c - * @{ - */ - - -/** @defgroup USBH_IOREQ_Exported_Defines - * @{ - */ - -#define USBH_PID_SETUP 0U -#define USBH_PID_DATA 1U - -#define USBH_EP_CONTROL 0U -#define USBH_EP_ISO 1U -#define USBH_EP_BULK 2U -#define USBH_EP_INTERRUPT 3U - -#define USBH_SETUP_PKT_SIZE 8U -/** - * @} - */ - - -/** @defgroup USBH_IOREQ_Exported_Types - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBH_IOREQ_Exported_Macros - * @{ - */ -/** - * @} - */ - -/** @defgroup USBH_IOREQ_Exported_Variables - * @{ - */ -/** - * @} - */ - -/** @defgroup USBH_IOREQ_Exported_FunctionsPrototype - * @{ - */ -USBH_StatusTypeDef USBH_CtlSendSetup (USBH_HandleTypeDef *phost, - uint8_t *buff, - uint8_t pipe_num); - -USBH_StatusTypeDef USBH_CtlSendData (USBH_HandleTypeDef *phost, - uint8_t *buff, - uint16_t length, - uint8_t pipe_num, - uint8_t do_ping); - -USBH_StatusTypeDef USBH_CtlReceiveData(USBH_HandleTypeDef *phost, - uint8_t *buff, - uint16_t length, - uint8_t pipe_num); - -USBH_StatusTypeDef USBH_BulkReceiveData(USBH_HandleTypeDef *phost, - uint8_t *buff, - uint16_t length, - uint8_t pipe_num); - -USBH_StatusTypeDef USBH_BulkSendData (USBH_HandleTypeDef *phost, - uint8_t *buff, - uint16_t length, - uint8_t pipe_num, - uint8_t do_ping ); - -USBH_StatusTypeDef USBH_InterruptReceiveData(USBH_HandleTypeDef *phost, - uint8_t *buff, - uint8_t length, - uint8_t pipe_num); - -USBH_StatusTypeDef USBH_InterruptSendData(USBH_HandleTypeDef *phost, - uint8_t *buff, - uint8_t length, - uint8_t pipe_num); - - -USBH_StatusTypeDef USBH_IsocReceiveData(USBH_HandleTypeDef *phost, - uint8_t *buff, - uint32_t length, - uint8_t pipe_num); - - -USBH_StatusTypeDef USBH_IsocSendData(USBH_HandleTypeDef *phost, - uint8_t *buff, - uint32_t length, - uint8_t pipe_num); -/** - * @} - */ - -#ifdef __cplusplus -} -#endif - -#endif /* __USBH_IOREQ_H */ - -/** - * @} - */ - -/** - * @} - */ - -/** -* @} -*/ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - - +/** + ****************************************************************************** + * @file usbh_ioreq.h + * @author MCD Application Team + * @brief Header file for usbh_ioreq.c + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive ----------------------------------------------*/ +#ifndef __USBH_IOREQ_H +#define __USBH_IOREQ_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_conf.h" +#include "usbh_core.h" + +/** @addtogroup USBH_LIB + * @{ + */ + +/** @addtogroup USBH_LIB_CORE +* @{ +*/ + +/** @defgroup USBH_IOREQ + * @brief This file is the header file for usbh_ioreq.c + * @{ + */ + + +/** @defgroup USBH_IOREQ_Exported_Defines + * @{ + */ + +#define USBH_PID_SETUP 0U +#define USBH_PID_DATA 1U + +#define USBH_EP_CONTROL 0U +#define USBH_EP_ISO 1U +#define USBH_EP_BULK 2U +#define USBH_EP_INTERRUPT 3U + +#define USBH_SETUP_PKT_SIZE 8U +/** + * @} + */ + + +/** @defgroup USBH_IOREQ_Exported_Types + * @{ + */ +/** + * @} + */ + + +/** @defgroup USBH_IOREQ_Exported_Macros + * @{ + */ +/** + * @} + */ + +/** @defgroup USBH_IOREQ_Exported_Variables + * @{ + */ +/** + * @} + */ + +/** @defgroup USBH_IOREQ_Exported_FunctionsPrototype + * @{ + */ +USBH_StatusTypeDef USBH_CtlSendSetup (USBH_HandleTypeDef *phost, + uint8_t *buff, + uint8_t pipe_num); + +USBH_StatusTypeDef USBH_CtlSendData (USBH_HandleTypeDef *phost, + uint8_t *buff, + uint16_t length, + uint8_t pipe_num, + uint8_t do_ping); + +USBH_StatusTypeDef USBH_CtlReceiveData(USBH_HandleTypeDef *phost, + uint8_t *buff, + uint16_t length, + uint8_t pipe_num); + +USBH_StatusTypeDef USBH_BulkReceiveData(USBH_HandleTypeDef *phost, + uint8_t *buff, + uint16_t length, + uint8_t pipe_num); + +USBH_StatusTypeDef USBH_BulkSendData (USBH_HandleTypeDef *phost, + uint8_t *buff, + uint16_t length, + uint8_t pipe_num, + uint8_t do_ping ); + +USBH_StatusTypeDef USBH_InterruptReceiveData(USBH_HandleTypeDef *phost, + uint8_t *buff, + uint8_t length, + uint8_t pipe_num); + +USBH_StatusTypeDef USBH_InterruptSendData(USBH_HandleTypeDef *phost, + uint8_t *buff, + uint8_t length, + uint8_t pipe_num); + + +USBH_StatusTypeDef USBH_IsocReceiveData(USBH_HandleTypeDef *phost, + uint8_t *buff, + uint32_t length, + uint8_t pipe_num); + + +USBH_StatusTypeDef USBH_IsocSendData(USBH_HandleTypeDef *phost, + uint8_t *buff, + uint32_t length, + uint8_t pipe_num); +/** + * @} + */ + +#ifdef __cplusplus +} +#endif + +#endif /* __USBH_IOREQ_H */ + +/** + * @} + */ + +/** + * @} + */ + +/** +* @} +*/ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + + diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Core/Inc/usbh_pipes.h b/system/Middlewares/ST/STM32_USB_Host_Library/Core/Inc/usbh_pipes.h index 5d7cfd6288..3338949d53 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Core/Inc/usbh_pipes.h +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Core/Inc/usbh_pipes.h @@ -1,123 +1,123 @@ -/** - ****************************************************************************** - * @file usbh_pipes.h - * @author MCD Application Team - * @brief Header file for usbh_pipes.c - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Define to prevent recursive ----------------------------------------------*/ -#ifndef __USBH_PIPES_H -#define __USBH_PIPES_H - -#ifdef __cplusplus - extern "C" { -#endif - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_core.h" - -/** @addtogroup USBH_LIB - * @{ - */ - -/** @addtogroup USBH_LIB_CORE -* @{ -*/ - -/** @defgroup USBH_PIPES - * @brief This file is the header file for usbh_pipes.c - * @{ - */ - -/** @defgroup USBH_PIPES_Exported_Defines - * @{ - */ -/** - * @} - */ - -/** @defgroup USBH_PIPES_Exported_Types - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBH_PIPES_Exported_Macros - * @{ - */ -/** - * @} - */ - -/** @defgroup USBH_PIPES_Exported_Variables - * @{ - */ -/** - * @} - */ - -/** @defgroup USBH_PIPES_Exported_FunctionsPrototype - * @{ - */ - -USBH_StatusTypeDef USBH_OpenPipe (USBH_HandleTypeDef *phost, - uint8_t pipe_num, - uint8_t epnum, - uint8_t dev_address, - uint8_t speed, - uint8_t ep_type, - uint16_t mps); - -USBH_StatusTypeDef USBH_ClosePipe (USBH_HandleTypeDef *phost, - uint8_t pipe_num); - -uint8_t USBH_AllocPipe (USBH_HandleTypeDef *phost, - uint8_t ep_addr); - -USBH_StatusTypeDef USBH_FreePipe (USBH_HandleTypeDef *phost, - uint8_t idx); - - - - -/** - * @} - */ - - -#ifdef __cplusplus -} -#endif - -#endif /* __USBH_PIPES_H */ - - -/** - * @} - */ - -/** - * @} - */ - -/** -* @} -*/ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - - +/** + ****************************************************************************** + * @file usbh_pipes.h + * @author MCD Application Team + * @brief Header file for usbh_pipes.c + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Define to prevent recursive ----------------------------------------------*/ +#ifndef __USBH_PIPES_H +#define __USBH_PIPES_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_core.h" + +/** @addtogroup USBH_LIB + * @{ + */ + +/** @addtogroup USBH_LIB_CORE +* @{ +*/ + +/** @defgroup USBH_PIPES + * @brief This file is the header file for usbh_pipes.c + * @{ + */ + +/** @defgroup USBH_PIPES_Exported_Defines + * @{ + */ +/** + * @} + */ + +/** @defgroup USBH_PIPES_Exported_Types + * @{ + */ +/** + * @} + */ + + +/** @defgroup USBH_PIPES_Exported_Macros + * @{ + */ +/** + * @} + */ + +/** @defgroup USBH_PIPES_Exported_Variables + * @{ + */ +/** + * @} + */ + +/** @defgroup USBH_PIPES_Exported_FunctionsPrototype + * @{ + */ + +USBH_StatusTypeDef USBH_OpenPipe (USBH_HandleTypeDef *phost, + uint8_t pipe_num, + uint8_t epnum, + uint8_t dev_address, + uint8_t speed, + uint8_t ep_type, + uint16_t mps); + +USBH_StatusTypeDef USBH_ClosePipe (USBH_HandleTypeDef *phost, + uint8_t pipe_num); + +uint8_t USBH_AllocPipe (USBH_HandleTypeDef *phost, + uint8_t ep_addr); + +USBH_StatusTypeDef USBH_FreePipe (USBH_HandleTypeDef *phost, + uint8_t idx); + + + + +/** + * @} + */ + + +#ifdef __cplusplus +} +#endif + +#endif /* __USBH_PIPES_H */ + + +/** + * @} + */ + +/** + * @} + */ + +/** +* @} +*/ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + + diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Core/Src/usbh_conf_template.c b/system/Middlewares/ST/STM32_USB_Host_Library/Core/Src/usbh_conf_template.c index 9f850f0919..40fadda08a 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Core/Src/usbh_conf_template.c +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Core/Src/usbh_conf_template.c @@ -1,263 +1,263 @@ -/** - ****************************************************************************** - * @file usbh_conf.c - * @author MCD Application Team - * @brief This file implements the board support package for the USB host library - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_core.h" - -/** - * @brief USBH_LL_Init - * Initialize the Low Level portion of the Host driver. - * @param phost: Host handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_LL_Init (USBH_HandleTypeDef *phost) -{ - - return USBH_OK; -} - -/** - * @brief USBH_LL_DeInit - * De-Initialize the Low Level portion of the Host driver. - * @param phost: Host handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_LL_DeInit (USBH_HandleTypeDef *phost) -{ - - return USBH_OK; -} - -/** - * @brief USBH_LL_Start - * Start the Low Level portion of the Host driver. - * @param phost: Host handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_LL_Start(USBH_HandleTypeDef *phost) -{ - - return USBH_OK; -} - -/** - * @brief USBH_LL_Stop - * Stop the Low Level portion of the Host driver. - * @param phost: Host handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_LL_Stop (USBH_HandleTypeDef *phost) -{ - - return USBH_OK; -} - -/** - * @brief USBH_LL_GetSpeed - * Return the USB Host Speed from the Low Level Driver. - * @param phost: Host handle - * @retval USBH Speeds - */ -USBH_SpeedTypeDef USBH_LL_GetSpeed (USBH_HandleTypeDef *phost) -{ - USBH_SpeedTypeDef speed = USBH_SPEED_FULL; - - - return speed; -} - -/** - * @brief USBH_LL_ResetPort - * Reset the Host Port of the Low Level Driver. - * @param phost: Host handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_LL_ResetPort (USBH_HandleTypeDef *phost) -{ - - return USBH_OK; -} - -/** - * @brief USBH_LL_GetLastXferSize - * Return the last transferred packet size. - * @param phost: Host handle - * @param pipe: Pipe index - * @retval Packet Size - */ -uint32_t USBH_LL_GetLastXferSize (USBH_HandleTypeDef *phost, uint8_t pipe) -{ - return 0; -} - -/** - * @brief USBH_LL_OpenPipe - * Open a pipe of the Low Level Driver. - * @param phost: Host handle - * @param pipe_num: Pipe index - * @param epnum: Endpoint Number - * @param dev_address: Device USB address - * @param speed: Device Speed - * @param ep_type: Endpoint Type - * @param mps: Endpoint Max Packet Size - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_LL_OpenPipe (USBH_HandleTypeDef *phost, - uint8_t pipe_num, - uint8_t epnum, - uint8_t dev_address, - uint8_t speed, - uint8_t ep_type, - uint16_t mps) -{ - - return USBH_OK; -} - -/** - * @brief USBH_LL_ClosePipe - * Close a pipe of the Low Level Driver. - * @param phost: Host handle - * @param pipe_num: Pipe index - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_LL_ClosePipe (USBH_HandleTypeDef *phost, uint8_t pipe) -{ - return USBH_OK; -} - -/** - * @brief USBH_LL_SubmitURB - * Submit a new URB to the low level driver. - * @param phost: Host handle - * @param pipe: Pipe index - * This parameter can be a value from 1 to 15 - * @param direction : Channel number - * This parameter can be one of the these values: - * 0 : Output - * 1 : Input - * @param ep_type : Endpoint Type - * This parameter can be one of the these values: - * @arg EP_TYPE_CTRL: Control type - * @arg EP_TYPE_ISOC: Isochronous type - * @arg EP_TYPE_BULK: Bulk type - * @arg EP_TYPE_INTR: Interrupt type - * @param token : Endpoint Type - * This parameter can be one of the these values: - * @arg 0: PID_SETUP - * @arg 1: PID_DATA - * @param pbuff : pointer to URB data - * @param length : Length of URB data - * @param do_ping : activate do ping protocol (for high speed only) - * This parameter can be one of the these values: - * 0 : do ping inactive - * 1 : do ping active - * @retval Status - */ - -USBH_StatusTypeDef USBH_LL_SubmitURB (USBH_HandleTypeDef *phost, - uint8_t pipe, - uint8_t direction , - uint8_t ep_type, - uint8_t token, - uint8_t* pbuff, - uint16_t length, - uint8_t do_ping ) -{ - - return USBH_OK; -} - -/** - * @brief USBH_LL_GetURBState - * Get a URB state from the low level driver. - * @param phost: Host handle - * @param pipe: Pipe index - * This parameter can be a value from 1 to 15 - * @retval URB state - * This parameter can be one of the these values: - * @arg URB_IDLE - * @arg URB_DONE - * @arg URB_NOTREADY - * @arg URB_NYET - * @arg URB_ERROR - * @arg URB_STALL - */ -USBH_URBStateTypeDef USBH_LL_GetURBState (USBH_HandleTypeDef *phost, uint8_t pipe) -{ - return USBH_URB_IDLE; -} - -/** - * @brief USBH_LL_DriverVBUS - * Drive VBUS. - * @param phost: Host handle - * @param state : VBUS state - * This parameter can be one of the these values: - * 0 : VBUS Active - * 1 : VBUS Inactive - * @retval Status - */ - -USBH_StatusTypeDef USBH_LL_DriverVBUS (USBH_HandleTypeDef *phost, uint8_t state) -{ - - return USBH_OK; -} - -/** - * @brief USBH_LL_SetToggle - * Set toggle for a pipe. - * @param phost: Host handle - * @param pipe: Pipe index - * @param pipe_num: Pipe index - * @param toggle: toggle (0/1) - * @retval Status - */ -USBH_StatusTypeDef USBH_LL_SetToggle (USBH_HandleTypeDef *phost, uint8_t pipe, uint8_t toggle) -{ - - return USBH_OK; -} - -/** - * @brief USBH_LL_GetToggle - * Return the current toggle of a pipe. - * @param phost: Host handle - * @param pipe: Pipe index - * @retval toggle (0/1) - */ -uint8_t USBH_LL_GetToggle (USBH_HandleTypeDef *phost, uint8_t pipe) -{ - uint8_t toggle = 0; - - - return toggle; -} -/** - * @brief USBH_Delay - * Delay routine for the USB Host Library - * @param Delay: Delay in ms - * @retval None - */ -void USBH_Delay (uint32_t Delay) -{ - -} -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbh_conf.c + * @author MCD Application Team + * @brief This file implements the board support package for the USB host library + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_core.h" + +/** + * @brief USBH_LL_Init + * Initialize the Low Level portion of the Host driver. + * @param phost: Host handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_LL_Init (USBH_HandleTypeDef *phost) +{ + + return USBH_OK; +} + +/** + * @brief USBH_LL_DeInit + * De-Initialize the Low Level portion of the Host driver. + * @param phost: Host handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_LL_DeInit (USBH_HandleTypeDef *phost) +{ + + return USBH_OK; +} + +/** + * @brief USBH_LL_Start + * Start the Low Level portion of the Host driver. + * @param phost: Host handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_LL_Start(USBH_HandleTypeDef *phost) +{ + + return USBH_OK; +} + +/** + * @brief USBH_LL_Stop + * Stop the Low Level portion of the Host driver. + * @param phost: Host handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_LL_Stop (USBH_HandleTypeDef *phost) +{ + + return USBH_OK; +} + +/** + * @brief USBH_LL_GetSpeed + * Return the USB Host Speed from the Low Level Driver. + * @param phost: Host handle + * @retval USBH Speeds + */ +USBH_SpeedTypeDef USBH_LL_GetSpeed (USBH_HandleTypeDef *phost) +{ + USBH_SpeedTypeDef speed = USBH_SPEED_FULL; + + + return speed; +} + +/** + * @brief USBH_LL_ResetPort + * Reset the Host Port of the Low Level Driver. + * @param phost: Host handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_LL_ResetPort (USBH_HandleTypeDef *phost) +{ + + return USBH_OK; +} + +/** + * @brief USBH_LL_GetLastXferSize + * Return the last transferred packet size. + * @param phost: Host handle + * @param pipe: Pipe index + * @retval Packet Size + */ +uint32_t USBH_LL_GetLastXferSize (USBH_HandleTypeDef *phost, uint8_t pipe) +{ + return 0; +} + +/** + * @brief USBH_LL_OpenPipe + * Open a pipe of the Low Level Driver. + * @param phost: Host handle + * @param pipe_num: Pipe index + * @param epnum: Endpoint Number + * @param dev_address: Device USB address + * @param speed: Device Speed + * @param ep_type: Endpoint Type + * @param mps: Endpoint Max Packet Size + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_LL_OpenPipe (USBH_HandleTypeDef *phost, + uint8_t pipe_num, + uint8_t epnum, + uint8_t dev_address, + uint8_t speed, + uint8_t ep_type, + uint16_t mps) +{ + + return USBH_OK; +} + +/** + * @brief USBH_LL_ClosePipe + * Close a pipe of the Low Level Driver. + * @param phost: Host handle + * @param pipe_num: Pipe index + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_LL_ClosePipe (USBH_HandleTypeDef *phost, uint8_t pipe) +{ + return USBH_OK; +} + +/** + * @brief USBH_LL_SubmitURB + * Submit a new URB to the low level driver. + * @param phost: Host handle + * @param pipe: Pipe index + * This parameter can be a value from 1 to 15 + * @param direction : Channel number + * This parameter can be one of the these values: + * 0 : Output + * 1 : Input + * @param ep_type : Endpoint Type + * This parameter can be one of the these values: + * @arg EP_TYPE_CTRL: Control type + * @arg EP_TYPE_ISOC: Isochronous type + * @arg EP_TYPE_BULK: Bulk type + * @arg EP_TYPE_INTR: Interrupt type + * @param token : Endpoint Type + * This parameter can be one of the these values: + * @arg 0: PID_SETUP + * @arg 1: PID_DATA + * @param pbuff : pointer to URB data + * @param length : Length of URB data + * @param do_ping : activate do ping protocol (for high speed only) + * This parameter can be one of the these values: + * 0 : do ping inactive + * 1 : do ping active + * @retval Status + */ + +USBH_StatusTypeDef USBH_LL_SubmitURB (USBH_HandleTypeDef *phost, + uint8_t pipe, + uint8_t direction , + uint8_t ep_type, + uint8_t token, + uint8_t* pbuff, + uint16_t length, + uint8_t do_ping ) +{ + + return USBH_OK; +} + +/** + * @brief USBH_LL_GetURBState + * Get a URB state from the low level driver. + * @param phost: Host handle + * @param pipe: Pipe index + * This parameter can be a value from 1 to 15 + * @retval URB state + * This parameter can be one of the these values: + * @arg URB_IDLE + * @arg URB_DONE + * @arg URB_NOTREADY + * @arg URB_NYET + * @arg URB_ERROR + * @arg URB_STALL + */ +USBH_URBStateTypeDef USBH_LL_GetURBState (USBH_HandleTypeDef *phost, uint8_t pipe) +{ + return USBH_URB_IDLE; +} + +/** + * @brief USBH_LL_DriverVBUS + * Drive VBUS. + * @param phost: Host handle + * @param state : VBUS state + * This parameter can be one of the these values: + * 0 : VBUS Active + * 1 : VBUS Inactive + * @retval Status + */ + +USBH_StatusTypeDef USBH_LL_DriverVBUS (USBH_HandleTypeDef *phost, uint8_t state) +{ + + return USBH_OK; +} + +/** + * @brief USBH_LL_SetToggle + * Set toggle for a pipe. + * @param phost: Host handle + * @param pipe: Pipe index + * @param pipe_num: Pipe index + * @param toggle: toggle (0/1) + * @retval Status + */ +USBH_StatusTypeDef USBH_LL_SetToggle (USBH_HandleTypeDef *phost, uint8_t pipe, uint8_t toggle) +{ + + return USBH_OK; +} + +/** + * @brief USBH_LL_GetToggle + * Return the current toggle of a pipe. + * @param phost: Host handle + * @param pipe: Pipe index + * @retval toggle (0/1) + */ +uint8_t USBH_LL_GetToggle (USBH_HandleTypeDef *phost, uint8_t pipe) +{ + uint8_t toggle = 0; + + + return toggle; +} +/** + * @brief USBH_Delay + * Delay routine for the USB Host Library + * @param Delay: Delay in ms + * @retval None + */ +void USBH_Delay (uint32_t Delay) +{ + +} +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Core/Src/usbh_core.c b/system/Middlewares/ST/STM32_USB_Host_Library/Core/Src/usbh_core.c index 8465983a8c..5cbc96a050 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Core/Src/usbh_core.c +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Core/Src/usbh_core.c @@ -1,1137 +1,1137 @@ -/** - ****************************************************************************** - * @file usbh_core.c - * @author MCD Application Team - * @brief This file implements the functions for the core state machine process - * the enumeration and the control transfer process - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Includes ------------------------------------------------------------------*/ - -#include "usbh_core.h" - - -/** @addtogroup USBH_LIB - * @{ - */ - -/** @addtogroup USBH_LIB_CORE - * @{ - */ - -/** @defgroup USBH_CORE - * @brief This file handles the basic enumeration when a device is connected - * to the host. - * @{ - */ - - -/** @defgroup USBH_CORE_Private_Defines - * @{ - */ -#define USBH_ADDRESS_DEFAULT 0x00U -#define USBH_ADDRESS_ASSIGNED 0x01U -#define USBH_MPS_DEFAULT 0x40U -/** - * @} - */ - -/** @defgroup USBH_CORE_Private_Macros - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBH_CORE_Private_Variables - * @{ - */ -#if (USBH_USE_OS == 1U) -#if (osCMSIS >= 0x20000U) -osThreadAttr_t USBH_Thread_Atrr; -#endif -#endif - - -/** - * @} - */ - - -/** @defgroup USBH_CORE_Private_Functions - * @{ - */ -static USBH_StatusTypeDef USBH_HandleEnum (USBH_HandleTypeDef *phost); -static void USBH_HandleSof (USBH_HandleTypeDef *phost); -static USBH_StatusTypeDef DeInitStateMachine(USBH_HandleTypeDef *phost); - -#if (USBH_USE_OS == 1U) -#if (osCMSIS < 0x20000U) -static void USBH_Process_OS(void const *argument); -#else -static void USBH_Process_OS(void *argument); -#endif -#endif - -/** - * @brief HCD_Init - * Initialize the HOST Core. - * @param phost: Host Handle - * @param pUsrFunc: User Callback - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_Init(USBH_HandleTypeDef *phost, void (*pUsrFunc)(USBH_HandleTypeDef *phost, uint8_t id), uint8_t id) -{ - /* Check whether the USB Host handle is valid */ - if(phost == NULL) - { - USBH_ErrLog("Invalid Host handle"); - return USBH_FAIL; - } - - /* Set DRiver ID */ - phost->id = id; - - /* Unlink class*/ - phost->pActiveClass = NULL; - phost->ClassNumber = 0U; - - /* Restore default states and prepare EP0 */ - DeInitStateMachine(phost); - - /* Assign User process */ - if(pUsrFunc != NULL) - { - phost->pUser = pUsrFunc; - } - -#if (USBH_USE_OS == 1U) -#if (osCMSIS < 0x20000U) - - /* Create USB Host Queue */ - osMessageQDef(USBH_Queue, MSGQUEUE_OBJECTS, uint16_t); - phost->os_event = osMessageCreate (osMessageQ(USBH_Queue), NULL); - - /* Create USB Host Task */ -#if defined (USBH_PROCESS_STACK_SIZE) - osThreadDef(USBH_Thread, USBH_Process_OS, USBH_PROCESS_PRIO, 0U, USBH_PROCESS_STACK_SIZE); -#else - osThreadDef(USBH_Thread, USBH_Process_OS, USBH_PROCESS_PRIO, 0U, 8U * configMINIMAL_STACK_SIZE); -#endif /* defined (USBH_PROCESS_STACK_SIZE) */ - - phost->thread = osThreadCreate (osThread(USBH_Thread), phost); - -#else - - /* Create USB Host Queue */ - phost->os_event = osMessageQueueNew (MSGQUEUE_OBJECTS, sizeof(uint32_t), NULL); - - /* Create USB Host Task */ - USBH_Thread_Atrr.name = "USBH_Queue"; - -#if defined (USBH_PROCESS_STACK_SIZE) - USBH_Thread_Atrr.stack_size = USBH_PROCESS_STACK_SIZE; -#else - USBH_Thread_Atrr.stack_size = (8U * configMINIMAL_STACK_SIZE); -#endif /* defined (USBH_PROCESS_STACK_SIZE) */ - - USBH_Thread_Atrr.priority = USBH_PROCESS_PRIO; - phost->thread = osThreadNew(USBH_Process_OS, phost, &USBH_Thread_Atrr); - -#endif /* (osCMSIS < 0x20000U) */ -#endif /* (USBH_USE_OS == 1U) */ - - /* Initialize low level driver */ - USBH_LL_Init(phost); - - return USBH_OK; -} - -/** - * @brief HCD_Init - * De-Initialize the Host portion of the driver. - * @param phost: Host Handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_DeInit(USBH_HandleTypeDef *phost) -{ - DeInitStateMachine(phost); - - if(phost->pData != NULL) - { - USBH_free (phost->pActiveClass->pData); - phost->pActiveClass->pData = NULL; - USBH_LL_Stop(phost); - } - - return USBH_OK; -} - -/** - * @brief DeInitStateMachine - * De-Initialize the Host state machine. - * @param phost: Host Handle - * @retval USBH Status - */ -static USBH_StatusTypeDef DeInitStateMachine(USBH_HandleTypeDef *phost) -{ - uint32_t i = 0U; - - /* Clear Pipes flags*/ - for ( ; i < USBH_MAX_PIPES_NBR; i++) - { - phost->Pipes[i] = 0U; - } - - for(i = 0U; i< USBH_MAX_DATA_BUFFER; i++) - { - phost->device.Data[i] = 0U; - } - - phost->gState = HOST_IDLE; - phost->EnumState = ENUM_IDLE; - phost->RequestState = CMD_SEND; - phost->Timer = 0U; - - phost->Control.state = CTRL_SETUP; - phost->Control.pipe_size = USBH_MPS_DEFAULT; - phost->Control.errorcount = 0U; - - phost->device.address = USBH_ADDRESS_DEFAULT; - phost->device.speed = USBH_SPEED_FULL; - - return USBH_OK; -} - -/** - * @brief USBH_RegisterClass - * Link class driver to Host Core. - * @param phost : Host Handle - * @param pclass: Class handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_RegisterClass(USBH_HandleTypeDef *phost, USBH_ClassTypeDef *pclass) -{ - USBH_StatusTypeDef status = USBH_OK; - - if(pclass != 0) - { - if(phost->ClassNumber < USBH_MAX_NUM_SUPPORTED_CLASS) - { - /* link the class to the USB Host handle */ - phost->pClass[phost->ClassNumber++] = pclass; - status = USBH_OK; - } - else - { - USBH_ErrLog("Max Class Number reached"); - status = USBH_FAIL; - } - } - else - { - USBH_ErrLog("Invalid Class handle"); - status = USBH_FAIL; - } - - return status; -} - -/** - * @brief USBH_SelectInterface - * Select current interface. - * @param phost: Host Handle - * @param interface: Interface number - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_SelectInterface(USBH_HandleTypeDef *phost, uint8_t interface) -{ - USBH_StatusTypeDef status = USBH_OK; - - if(interface < phost->device.CfgDesc.bNumInterfaces) - { - phost->device.current_interface = interface; - USBH_UsrLog ("Switching to Interface (#%d)", interface); - USBH_UsrLog ("Class : %xh", phost->device.CfgDesc.Itf_Desc[interface].bInterfaceClass ); - USBH_UsrLog ("SubClass : %xh", phost->device.CfgDesc.Itf_Desc[interface].bInterfaceSubClass ); - USBH_UsrLog ("Protocol : %xh", phost->device.CfgDesc.Itf_Desc[interface].bInterfaceProtocol ); - } - else - { - USBH_ErrLog ("Cannot Select This Interface."); - status = USBH_FAIL; - } - return status; -} - -/** - * @brief USBH_GetActiveClass - * Return Device Class. - * @param phost: Host Handle - * @param interface: Interface index - * @retval Class Code - */ -uint8_t USBH_GetActiveClass(USBH_HandleTypeDef *phost) -{ - return (phost->device.CfgDesc.Itf_Desc[0].bInterfaceClass); -} -/** - * @brief USBH_FindInterface - * Find the interface index for a specific class. - * @param phost: Host Handle - * @param Class: Class code - * @param SubClass: SubClass code - * @param Protocol: Protocol code - * @retval interface index in the configuration structure - * @note : (1)interface index 0xFF means interface index not found - */ -uint8_t USBH_FindInterface(USBH_HandleTypeDef *phost, uint8_t Class, uint8_t SubClass, uint8_t Protocol) -{ - USBH_InterfaceDescTypeDef *pif ; - USBH_CfgDescTypeDef *pcfg ; - uint8_t if_ix = 0U; - - pif = (USBH_InterfaceDescTypeDef *)0; - pcfg = &phost->device.CfgDesc; - - while (if_ix < USBH_MAX_NUM_INTERFACES) - { - pif = &pcfg->Itf_Desc[if_ix]; - if(((pif->bInterfaceClass == Class) || (Class == 0xFFU))&& - ((pif->bInterfaceSubClass == SubClass) || (SubClass == 0xFFU))&& - ((pif->bInterfaceProtocol == Protocol) || (Protocol == 0xFFU))) - { - return if_ix; - } - if_ix++; - } - return 0xFFU; -} - -/** - * @brief USBH_FindInterfaceIndex - * Find the interface index for a specific class interface and alternate setting number. - * @param phost: Host Handle - * @param interface_number: interface number - * @param alt_settings : alternate setting number - * @retval interface index in the configuration structure - * @note : (1)interface index 0xFF means interface index not found - */ -uint8_t USBH_FindInterfaceIndex(USBH_HandleTypeDef *phost, uint8_t interface_number, uint8_t alt_settings) -{ - USBH_InterfaceDescTypeDef *pif ; - USBH_CfgDescTypeDef *pcfg ; - uint8_t if_ix = 0U; - - pif = (USBH_InterfaceDescTypeDef *)0; - pcfg = &phost->device.CfgDesc; - - while (if_ix < USBH_MAX_NUM_INTERFACES) - { - pif = &pcfg->Itf_Desc[if_ix]; - if((pif->bInterfaceNumber == interface_number) && (pif->bAlternateSetting == alt_settings)) - { - return if_ix; - } - if_ix++; - } - return 0xFFU; -} - -/** - * @brief USBH_Start - * Start the USB Host Core. - * @param phost: Host Handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_Start (USBH_HandleTypeDef *phost) -{ - /* Start the low level driver */ - USBH_LL_Start(phost); - - /* Activate VBUS on the port */ - USBH_LL_DriverVBUS (phost, TRUE); - - return USBH_OK; -} - -/** - * @brief USBH_Stop - * Stop the USB Host Core. - * @param phost: Host Handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_Stop (USBH_HandleTypeDef *phost) -{ - /* Stop and cleanup the low level driver */ - USBH_LL_Stop(phost); - - /* DeActivate VBUS on the port */ - USBH_LL_DriverVBUS (phost, FALSE); - - /* FRee Control Pipes */ - USBH_FreePipe (phost, phost->Control.pipe_in); - USBH_FreePipe (phost, phost->Control.pipe_out); - - return USBH_OK; -} - -/** - * @brief HCD_ReEnumerate - * Perform a new Enumeration phase. - * @param phost: Host Handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_ReEnumerate(USBH_HandleTypeDef *phost) -{ - /*Stop Host */ - USBH_Stop(phost); - - /*Device has disconnected, so wait for 200 ms */ - USBH_Delay(200U); - - /* Set State machines in default state */ - DeInitStateMachine(phost); - - /* Start again the host */ - USBH_Start(phost); - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_PORT_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - - return USBH_OK; -} - -/** - * @brief USBH_Process - * Background process of the USB Core. - * @param phost: Host Handle - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_Process(USBH_HandleTypeDef *phost) -{ - __IO USBH_StatusTypeDef status = USBH_FAIL; - uint8_t idx = 0U; - - /* check for Host port events */ - if (((USBH_IsPortEnabled(phost) == 0U)) && (phost->gState != HOST_IDLE)) - { - if(phost->gState != HOST_DEV_DISCONNECTED) - { - phost->gState = HOST_DEV_DISCONNECTED; - } - } - - switch (phost->gState) - { - case HOST_IDLE : - - if (phost->device.is_connected) - { - /* Wait for 200 ms after connection */ - phost->gState = HOST_DEV_WAIT_FOR_ATTACHMENT; - USBH_Delay(200U); - USBH_LL_ResetPort(phost); - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_PORT_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - break; - - case HOST_DEV_WAIT_FOR_ATTACHMENT: /* Wait for Port Eabled */ - - if (phost->device.PortEnabled == 1U) - { - phost->gState = HOST_DEV_ATTACHED; - } - break; - - case HOST_DEV_ATTACHED : - - USBH_UsrLog("USB Device Attached"); - - /* Wait for 100 ms after Reset */ - USBH_Delay(100U); - - phost->device.speed = USBH_LL_GetSpeed(phost); - - phost->gState = HOST_ENUMERATION; - - phost->Control.pipe_out = USBH_AllocPipe (phost, 0x00U); - phost->Control.pipe_in = USBH_AllocPipe (phost, 0x80U); - - - /* Open Control pipes */ - USBH_OpenPipe (phost, - phost->Control.pipe_in, - 0x80U, - phost->device.address, - phost->device.speed, - USBH_EP_CONTROL, - (uint16_t)phost->Control.pipe_size); - - /* Open Control pipes */ - USBH_OpenPipe (phost, - phost->Control.pipe_out, - 0x00U, - phost->device.address, - phost->device.speed, - USBH_EP_CONTROL, - (uint16_t)phost->Control.pipe_size); - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_PORT_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - case HOST_ENUMERATION: - /* Check for enumeration status */ - if ( USBH_HandleEnum(phost) == USBH_OK) - { - /* The function shall return USBH_OK when full enumeration is complete */ - USBH_UsrLog ("Enumeration done."); - phost->device.current_interface = 0U; - if(phost->device.DevDesc.bNumConfigurations == 1U) - { - USBH_UsrLog ("This device has only 1 configuration."); - phost->gState = HOST_SET_CONFIGURATION; - - } - else - { - phost->gState = HOST_INPUT; - } - - } - break; - - case HOST_INPUT: - { - /* user callback for end of device basic enumeration */ - if(phost->pUser != NULL) - { - phost->pUser(phost, HOST_USER_SELECT_CONFIGURATION); - phost->gState = HOST_SET_CONFIGURATION; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - } - break; - - case HOST_SET_CONFIGURATION: - /* set configuration */ - if (USBH_SetCfg(phost, (uint16_t)phost->device.CfgDesc.bConfigurationValue) == USBH_OK) - { - phost->gState = HOST_SET_WAKEUP_FEATURE; - USBH_UsrLog ("Default configuration set."); - } - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_PORT_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - case HOST_SET_WAKEUP_FEATURE: - - if ((phost->device.CfgDesc.bmAttributes) & (1U << 5)) - { - if (USBH_SetFeature(phost, FEATURE_SELECTOR_REMOTEWAKEUP) == USBH_OK) - { - USBH_UsrLog ("Device remote wakeup enabled"); - phost->gState = HOST_CHECK_CLASS; - } - } - else - { - phost->gState = HOST_CHECK_CLASS; - } - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_PORT_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - case HOST_CHECK_CLASS: - - if(phost->ClassNumber == 0U) - { - USBH_UsrLog ("No Class has been registered."); - } - else - { - phost->pActiveClass = NULL; - - for (idx = 0U; idx < USBH_MAX_NUM_SUPPORTED_CLASS; idx++) - { - if(phost->pClass[idx]->ClassCode == phost->device.CfgDesc.Itf_Desc[0].bInterfaceClass) - { - phost->pActiveClass = phost->pClass[idx]; - } - } - - if(phost->pActiveClass != NULL) - { - if(phost->pActiveClass->Init(phost)== USBH_OK) - { - phost->gState = HOST_CLASS_REQUEST; - USBH_UsrLog ("%s class started.", phost->pActiveClass->Name); - - /* Inform user that a class has been activated */ - phost->pUser(phost, HOST_USER_CLASS_SELECTED); - } - else - { - phost->gState = HOST_ABORT_STATE; - USBH_UsrLog ("Device not supporting %s class.", phost->pActiveClass->Name); - } - } - else - { - phost->gState = HOST_ABORT_STATE; - USBH_UsrLog ("No registered class for this device."); - } - } - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - case HOST_CLASS_REQUEST: - /* process class standard control requests state machine */ - if(phost->pActiveClass != NULL) - { - status = phost->pActiveClass->Requests(phost); - - if(status == USBH_OK) - { - phost->gState = HOST_CLASS; - } - } - else - { - phost->gState = HOST_ABORT_STATE; - USBH_ErrLog ("Invalid Class Driver."); - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - - break; - case HOST_CLASS: - /* process class state machine */ - if(phost->pActiveClass != NULL) - { - phost->pActiveClass->BgndProcess(phost); - } - break; - - case HOST_DEV_DISCONNECTED : - - DeInitStateMachine(phost); - - /* Re-Initilaize Host for new Enumeration */ - if(phost->pActiveClass != NULL) - { - phost->pActiveClass->DeInit(phost); - phost->pActiveClass = NULL; - } - break; - - case HOST_ABORT_STATE: - default : - break; - } - return USBH_OK; -} - - -/** - * @brief USBH_HandleEnum - * This function includes the complete enumeration process - * @param phost: Host Handle - * @retval USBH_Status - */ -static USBH_StatusTypeDef USBH_HandleEnum (USBH_HandleTypeDef *phost) -{ - USBH_StatusTypeDef Status = USBH_BUSY; - - switch (phost->EnumState) - { - case ENUM_IDLE: - /* Get Device Desc for only 1st 8 bytes : To get EP0 MaxPacketSize */ - if ( USBH_Get_DevDesc(phost, 8U) == USBH_OK) - { - phost->Control.pipe_size = phost->device.DevDesc.bMaxPacketSize; - - phost->EnumState = ENUM_GET_FULL_DEV_DESC; - - /* modify control channels configuration for MaxPacket size */ - USBH_OpenPipe (phost, - phost->Control.pipe_in, - 0x80U, - phost->device.address, - phost->device.speed, - USBH_EP_CONTROL, - (uint16_t)phost->Control.pipe_size); - - /* Open Control pipes */ - USBH_OpenPipe (phost, - phost->Control.pipe_out, - 0x00U, - phost->device.address, - phost->device.speed, - USBH_EP_CONTROL, - (uint16_t)phost->Control.pipe_size); - - } - break; - - case ENUM_GET_FULL_DEV_DESC: - /* Get FULL Device Desc */ - if ( USBH_Get_DevDesc(phost, USB_DEVICE_DESC_SIZE)== USBH_OK) - { - USBH_UsrLog("PID: %xh", phost->device.DevDesc.idProduct ); - USBH_UsrLog("VID: %xh", phost->device.DevDesc.idVendor ); - - phost->EnumState = ENUM_SET_ADDR; - - } - break; - - case ENUM_SET_ADDR: - /* set address */ - if ( USBH_SetAddress(phost, USBH_DEVICE_ADDRESS) == USBH_OK) - { - USBH_Delay(2U); - phost->device.address = USBH_DEVICE_ADDRESS; - - /* user callback for device address assigned */ - USBH_UsrLog("Address (#%d) assigned.", phost->device.address); - phost->EnumState = ENUM_GET_CFG_DESC; - - /* modify control channels to update device address */ - USBH_OpenPipe (phost, - phost->Control.pipe_in, - 0x80U, - phost->device.address, - phost->device.speed, - USBH_EP_CONTROL, - (uint16_t)phost->Control.pipe_size); - - /* Open Control pipes */ - USBH_OpenPipe (phost, - phost->Control.pipe_out, - 0x00U, - phost->device.address, - phost->device.speed, - USBH_EP_CONTROL, - (uint16_t)phost->Control.pipe_size); - } - break; - - case ENUM_GET_CFG_DESC: - /* get standard configuration descriptor */ - if ( USBH_Get_CfgDesc(phost, - USB_CONFIGURATION_DESC_SIZE) == USBH_OK) - { - phost->EnumState = ENUM_GET_FULL_CFG_DESC; - } - break; - - case ENUM_GET_FULL_CFG_DESC: - /* get FULL config descriptor (config, interface, endpoints) */ - if (USBH_Get_CfgDesc(phost, - phost->device.CfgDesc.wTotalLength) == USBH_OK) - { - phost->EnumState = ENUM_GET_MFC_STRING_DESC; - } - break; - - case ENUM_GET_MFC_STRING_DESC: - if (phost->device.DevDesc.iManufacturer != 0U) - { /* Check that Manufacturer String is available */ - - if ( USBH_Get_StringDesc(phost, - phost->device.DevDesc.iManufacturer, - phost->device.Data, - 0xFFU) == USBH_OK) - { - /* User callback for Manufacturing string */ - USBH_UsrLog("Manufacturer : %s", (char *)(void*)phost->device.Data); - phost->EnumState = ENUM_GET_PRODUCT_STRING_DESC; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - } - else - { - USBH_UsrLog("Manufacturer : N/A"); - phost->EnumState = ENUM_GET_PRODUCT_STRING_DESC; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - break; - - case ENUM_GET_PRODUCT_STRING_DESC: - if (phost->device.DevDesc.iProduct != 0U) - { /* Check that Product string is available */ - if ( USBH_Get_StringDesc(phost, - phost->device.DevDesc.iProduct, - phost->device.Data, - 0xFFU) == USBH_OK) - { - /* User callback for Product string */ - USBH_UsrLog("Product : %s", (char *)(void *)phost->device.Data); - phost->EnumState = ENUM_GET_SERIALNUM_STRING_DESC; - } - } - else - { - USBH_UsrLog("Product : N/A"); - phost->EnumState = ENUM_GET_SERIALNUM_STRING_DESC; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - break; - - case ENUM_GET_SERIALNUM_STRING_DESC: - if (phost->device.DevDesc.iSerialNumber != 0U) - { /* Check that Serial number string is available */ - if ( USBH_Get_StringDesc(phost, - phost->device.DevDesc.iSerialNumber, - phost->device.Data, - 0xFFU) == USBH_OK) - { - /* User callback for Serial number string */ - USBH_UsrLog("Serial Number : %s", (char *)(void*)phost->device.Data); - Status = USBH_OK; - } - } - else - { - USBH_UsrLog("Serial Number : N/A"); - Status = USBH_OK; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - break; - - default: - break; - } - return Status; -} - -/** - * @brief USBH_LL_SetTimer - * Set the initial Host Timer tick - * @param phost: Host Handle - * @retval None - */ -void USBH_LL_SetTimer (USBH_HandleTypeDef *phost, uint32_t time) -{ - phost->Timer = time; -} -/** - * @brief USBH_LL_IncTimer - * Increment Host Timer tick - * @param phost: Host Handle - * @retval None - */ -void USBH_LL_IncTimer (USBH_HandleTypeDef *phost) -{ - phost->Timer ++; - USBH_HandleSof(phost); -} - -/** - * @brief USBH_HandleSof - * Call SOF process - * @param phost: Host Handle - * @retval None - */ -static void USBH_HandleSof (USBH_HandleTypeDef *phost) -{ - if((phost->gState == HOST_CLASS)&&(phost->pActiveClass != NULL)) - { - phost->pActiveClass->SOFProcess(phost); - } -} - -/** - * @brief USBH_PortEnabled - * Port Enabled - * @param phost: Host Handle - * @retval None - */ -void USBH_LL_PortEnabled (USBH_HandleTypeDef *phost) -{ - phost->device.PortEnabled = 1U; - - return; -} - -/** - * @brief USBH_LL_PortDisabled - * Port Disabled - * @param phost: Host Handle - * @retval None - */ -void USBH_LL_PortDisabled (USBH_HandleTypeDef *phost) -{ - phost->device.PortEnabled = 0U; - - return; -} - -/** - * @brief HCD_IsPortEnabled - * Is Port Enabled - * @param phost: Host Handle - * @retval None - */ -uint8_t USBH_IsPortEnabled(USBH_HandleTypeDef *phost) -{ - return(phost->device.PortEnabled); -} - - -/** - * @brief USBH_LL_Connect - * Handle USB Host connexion event - * @param phost: Host Handle - * @retval USBH_Status - */ -USBH_StatusTypeDef USBH_LL_Connect (USBH_HandleTypeDef *phost) -{ - if(phost->gState == HOST_IDLE ) - { - phost->device.is_connected = 1U; - - if(phost->pUser != NULL) - { - phost->pUser(phost, HOST_USER_CONNECTION); - } - } - else - { - if (phost->device.PortEnabled == 1U) - { - phost->gState = HOST_DEV_ATTACHED; - } - } - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_PORT_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - - return USBH_OK; -} - -/** - * @brief USBH_LL_Disconnect - * Handle USB Host disconnection event - * @param phost: Host Handle - * @retval USBH_Status - */ -USBH_StatusTypeDef USBH_LL_Disconnect (USBH_HandleTypeDef *phost) -{ - /*Stop Host */ - USBH_LL_Stop(phost); - - /* FRee Control Pipes */ - USBH_FreePipe (phost, phost->Control.pipe_in); - USBH_FreePipe (phost, phost->Control.pipe_out); - - phost->device.is_connected = 0U; - - if(phost->pUser != NULL) - { - phost->pUser(phost, HOST_USER_DISCONNECTION); - } - USBH_UsrLog("USB Device disconnected"); - - /* Start the low level driver */ - USBH_LL_Start(phost); - - phost->gState = HOST_DEV_DISCONNECTED; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_PORT_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - - return USBH_OK; -} - - -#if (USBH_USE_OS == 1U) -/** - * @brief USB Host Thread task - * @param pvParameters not used - * @retval None - */ - -#if (osCMSIS < 0x20000U) -static void USBH_Process_OS(void const *argument) -{ - osEvent event; - - for(;;) - { - event = osMessageGet(((USBH_HandleTypeDef *)argument)->os_event, osWaitForever); - if(event.status == osEventMessage) - { - USBH_Process((USBH_HandleTypeDef *)argument); - } - } -} -#else -static void USBH_Process_OS(void *argument) -{ - osStatus_t status; - - for(;;) - { - status = osMessageQueueGet(((USBH_HandleTypeDef *)argument)->os_event, - &((USBH_HandleTypeDef *)argument)->os_msg, NULL, osWaitForever); - if (status == osOK) - { - USBH_Process((USBH_HandleTypeDef *)argument); - } - } -} -#endif /* (osCMSIS < 0x20000U) */ - -/** -* @brief USBH_LL_NotifyURBChange -* Notify URB state Change -* @param phost: Host handle -* @retval USBH Status -*/ -USBH_StatusTypeDef USBH_LL_NotifyURBChange (USBH_HandleTypeDef *phost) -{ - phost->os_msg = (uint32_t)USBH_PORT_EVENT; - -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif - - return USBH_OK; -} -#endif -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ +/** + ****************************************************************************** + * @file usbh_core.c + * @author MCD Application Team + * @brief This file implements the functions for the core state machine process + * the enumeration and the control transfer process + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ + +#include "usbh_core.h" + + +/** @addtogroup USBH_LIB + * @{ + */ + +/** @addtogroup USBH_LIB_CORE + * @{ + */ + +/** @defgroup USBH_CORE + * @brief This file handles the basic enumeration when a device is connected + * to the host. + * @{ + */ + + +/** @defgroup USBH_CORE_Private_Defines + * @{ + */ +#define USBH_ADDRESS_DEFAULT 0x00U +#define USBH_ADDRESS_ASSIGNED 0x01U +#define USBH_MPS_DEFAULT 0x40U +/** + * @} + */ + +/** @defgroup USBH_CORE_Private_Macros + * @{ + */ +/** + * @} + */ + + +/** @defgroup USBH_CORE_Private_Variables + * @{ + */ +#if (USBH_USE_OS == 1U) +#if (osCMSIS >= 0x20000U) +osThreadAttr_t USBH_Thread_Atrr; +#endif +#endif + + +/** + * @} + */ + + +/** @defgroup USBH_CORE_Private_Functions + * @{ + */ +static USBH_StatusTypeDef USBH_HandleEnum (USBH_HandleTypeDef *phost); +static void USBH_HandleSof (USBH_HandleTypeDef *phost); +static USBH_StatusTypeDef DeInitStateMachine(USBH_HandleTypeDef *phost); + +#if (USBH_USE_OS == 1U) +#if (osCMSIS < 0x20000U) +static void USBH_Process_OS(void const *argument); +#else +static void USBH_Process_OS(void *argument); +#endif +#endif + +/** + * @brief HCD_Init + * Initialize the HOST Core. + * @param phost: Host Handle + * @param pUsrFunc: User Callback + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_Init(USBH_HandleTypeDef *phost, void (*pUsrFunc)(USBH_HandleTypeDef *phost, uint8_t id), uint8_t id) +{ + /* Check whether the USB Host handle is valid */ + if(phost == NULL) + { + USBH_ErrLog("Invalid Host handle"); + return USBH_FAIL; + } + + /* Set DRiver ID */ + phost->id = id; + + /* Unlink class*/ + phost->pActiveClass = NULL; + phost->ClassNumber = 0U; + + /* Restore default states and prepare EP0 */ + DeInitStateMachine(phost); + + /* Assign User process */ + if(pUsrFunc != NULL) + { + phost->pUser = pUsrFunc; + } + +#if (USBH_USE_OS == 1U) +#if (osCMSIS < 0x20000U) + + /* Create USB Host Queue */ + osMessageQDef(USBH_Queue, MSGQUEUE_OBJECTS, uint16_t); + phost->os_event = osMessageCreate (osMessageQ(USBH_Queue), NULL); + + /* Create USB Host Task */ +#if defined (USBH_PROCESS_STACK_SIZE) + osThreadDef(USBH_Thread, USBH_Process_OS, USBH_PROCESS_PRIO, 0U, USBH_PROCESS_STACK_SIZE); +#else + osThreadDef(USBH_Thread, USBH_Process_OS, USBH_PROCESS_PRIO, 0U, 8U * configMINIMAL_STACK_SIZE); +#endif /* defined (USBH_PROCESS_STACK_SIZE) */ + + phost->thread = osThreadCreate (osThread(USBH_Thread), phost); + +#else + + /* Create USB Host Queue */ + phost->os_event = osMessageQueueNew (MSGQUEUE_OBJECTS, sizeof(uint32_t), NULL); + + /* Create USB Host Task */ + USBH_Thread_Atrr.name = "USBH_Queue"; + +#if defined (USBH_PROCESS_STACK_SIZE) + USBH_Thread_Atrr.stack_size = USBH_PROCESS_STACK_SIZE; +#else + USBH_Thread_Atrr.stack_size = (8U * configMINIMAL_STACK_SIZE); +#endif /* defined (USBH_PROCESS_STACK_SIZE) */ + + USBH_Thread_Atrr.priority = USBH_PROCESS_PRIO; + phost->thread = osThreadNew(USBH_Process_OS, phost, &USBH_Thread_Atrr); + +#endif /* (osCMSIS < 0x20000U) */ +#endif /* (USBH_USE_OS == 1U) */ + + /* Initialize low level driver */ + USBH_LL_Init(phost); + + return USBH_OK; +} + +/** + * @brief HCD_Init + * De-Initialize the Host portion of the driver. + * @param phost: Host Handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_DeInit(USBH_HandleTypeDef *phost) +{ + DeInitStateMachine(phost); + + if(phost->pData != NULL) + { + USBH_free (phost->pActiveClass->pData); + phost->pActiveClass->pData = NULL; + USBH_LL_Stop(phost); + } + + return USBH_OK; +} + +/** + * @brief DeInitStateMachine + * De-Initialize the Host state machine. + * @param phost: Host Handle + * @retval USBH Status + */ +static USBH_StatusTypeDef DeInitStateMachine(USBH_HandleTypeDef *phost) +{ + uint32_t i = 0U; + + /* Clear Pipes flags*/ + for ( ; i < USBH_MAX_PIPES_NBR; i++) + { + phost->Pipes[i] = 0U; + } + + for(i = 0U; i< USBH_MAX_DATA_BUFFER; i++) + { + phost->device.Data[i] = 0U; + } + + phost->gState = HOST_IDLE; + phost->EnumState = ENUM_IDLE; + phost->RequestState = CMD_SEND; + phost->Timer = 0U; + + phost->Control.state = CTRL_SETUP; + phost->Control.pipe_size = USBH_MPS_DEFAULT; + phost->Control.errorcount = 0U; + + phost->device.address = USBH_ADDRESS_DEFAULT; + phost->device.speed = USBH_SPEED_FULL; + + return USBH_OK; +} + +/** + * @brief USBH_RegisterClass + * Link class driver to Host Core. + * @param phost : Host Handle + * @param pclass: Class handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_RegisterClass(USBH_HandleTypeDef *phost, USBH_ClassTypeDef *pclass) +{ + USBH_StatusTypeDef status = USBH_OK; + + if(pclass != 0) + { + if(phost->ClassNumber < USBH_MAX_NUM_SUPPORTED_CLASS) + { + /* link the class to the USB Host handle */ + phost->pClass[phost->ClassNumber++] = pclass; + status = USBH_OK; + } + else + { + USBH_ErrLog("Max Class Number reached"); + status = USBH_FAIL; + } + } + else + { + USBH_ErrLog("Invalid Class handle"); + status = USBH_FAIL; + } + + return status; +} + +/** + * @brief USBH_SelectInterface + * Select current interface. + * @param phost: Host Handle + * @param interface: Interface number + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_SelectInterface(USBH_HandleTypeDef *phost, uint8_t interface) +{ + USBH_StatusTypeDef status = USBH_OK; + + if(interface < phost->device.CfgDesc.bNumInterfaces) + { + phost->device.current_interface = interface; + USBH_UsrLog ("Switching to Interface (#%d)", interface); + USBH_UsrLog ("Class : %xh", phost->device.CfgDesc.Itf_Desc[interface].bInterfaceClass ); + USBH_UsrLog ("SubClass : %xh", phost->device.CfgDesc.Itf_Desc[interface].bInterfaceSubClass ); + USBH_UsrLog ("Protocol : %xh", phost->device.CfgDesc.Itf_Desc[interface].bInterfaceProtocol ); + } + else + { + USBH_ErrLog ("Cannot Select This Interface."); + status = USBH_FAIL; + } + return status; +} + +/** + * @brief USBH_GetActiveClass + * Return Device Class. + * @param phost: Host Handle + * @param interface: Interface index + * @retval Class Code + */ +uint8_t USBH_GetActiveClass(USBH_HandleTypeDef *phost) +{ + return (phost->device.CfgDesc.Itf_Desc[0].bInterfaceClass); +} +/** + * @brief USBH_FindInterface + * Find the interface index for a specific class. + * @param phost: Host Handle + * @param Class: Class code + * @param SubClass: SubClass code + * @param Protocol: Protocol code + * @retval interface index in the configuration structure + * @note : (1)interface index 0xFF means interface index not found + */ +uint8_t USBH_FindInterface(USBH_HandleTypeDef *phost, uint8_t Class, uint8_t SubClass, uint8_t Protocol) +{ + USBH_InterfaceDescTypeDef *pif ; + USBH_CfgDescTypeDef *pcfg ; + uint8_t if_ix = 0U; + + pif = (USBH_InterfaceDescTypeDef *)0; + pcfg = &phost->device.CfgDesc; + + while (if_ix < USBH_MAX_NUM_INTERFACES) + { + pif = &pcfg->Itf_Desc[if_ix]; + if(((pif->bInterfaceClass == Class) || (Class == 0xFFU))&& + ((pif->bInterfaceSubClass == SubClass) || (SubClass == 0xFFU))&& + ((pif->bInterfaceProtocol == Protocol) || (Protocol == 0xFFU))) + { + return if_ix; + } + if_ix++; + } + return 0xFFU; +} + +/** + * @brief USBH_FindInterfaceIndex + * Find the interface index for a specific class interface and alternate setting number. + * @param phost: Host Handle + * @param interface_number: interface number + * @param alt_settings : alternate setting number + * @retval interface index in the configuration structure + * @note : (1)interface index 0xFF means interface index not found + */ +uint8_t USBH_FindInterfaceIndex(USBH_HandleTypeDef *phost, uint8_t interface_number, uint8_t alt_settings) +{ + USBH_InterfaceDescTypeDef *pif ; + USBH_CfgDescTypeDef *pcfg ; + uint8_t if_ix = 0U; + + pif = (USBH_InterfaceDescTypeDef *)0; + pcfg = &phost->device.CfgDesc; + + while (if_ix < USBH_MAX_NUM_INTERFACES) + { + pif = &pcfg->Itf_Desc[if_ix]; + if((pif->bInterfaceNumber == interface_number) && (pif->bAlternateSetting == alt_settings)) + { + return if_ix; + } + if_ix++; + } + return 0xFFU; +} + +/** + * @brief USBH_Start + * Start the USB Host Core. + * @param phost: Host Handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_Start (USBH_HandleTypeDef *phost) +{ + /* Start the low level driver */ + USBH_LL_Start(phost); + + /* Activate VBUS on the port */ + USBH_LL_DriverVBUS (phost, TRUE); + + return USBH_OK; +} + +/** + * @brief USBH_Stop + * Stop the USB Host Core. + * @param phost: Host Handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_Stop (USBH_HandleTypeDef *phost) +{ + /* Stop and cleanup the low level driver */ + USBH_LL_Stop(phost); + + /* DeActivate VBUS on the port */ + USBH_LL_DriverVBUS (phost, FALSE); + + /* FRee Control Pipes */ + USBH_FreePipe (phost, phost->Control.pipe_in); + USBH_FreePipe (phost, phost->Control.pipe_out); + + return USBH_OK; +} + +/** + * @brief HCD_ReEnumerate + * Perform a new Enumeration phase. + * @param phost: Host Handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_ReEnumerate(USBH_HandleTypeDef *phost) +{ + /*Stop Host */ + USBH_Stop(phost); + + /*Device has disconnected, so wait for 200 ms */ + USBH_Delay(200U); + + /* Set State machines in default state */ + DeInitStateMachine(phost); + + /* Start again the host */ + USBH_Start(phost); + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_PORT_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + + return USBH_OK; +} + +/** + * @brief USBH_Process + * Background process of the USB Core. + * @param phost: Host Handle + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_Process(USBH_HandleTypeDef *phost) +{ + __IO USBH_StatusTypeDef status = USBH_FAIL; + uint8_t idx = 0U; + + /* check for Host port events */ + if (((USBH_IsPortEnabled(phost) == 0U)) && (phost->gState != HOST_IDLE)) + { + if(phost->gState != HOST_DEV_DISCONNECTED) + { + phost->gState = HOST_DEV_DISCONNECTED; + } + } + + switch (phost->gState) + { + case HOST_IDLE : + + if (phost->device.is_connected) + { + /* Wait for 200 ms after connection */ + phost->gState = HOST_DEV_WAIT_FOR_ATTACHMENT; + USBH_Delay(200U); + USBH_LL_ResetPort(phost); + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_PORT_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + break; + + case HOST_DEV_WAIT_FOR_ATTACHMENT: /* Wait for Port Eabled */ + + if (phost->device.PortEnabled == 1U) + { + phost->gState = HOST_DEV_ATTACHED; + } + break; + + case HOST_DEV_ATTACHED : + + USBH_UsrLog("USB Device Attached"); + + /* Wait for 100 ms after Reset */ + USBH_Delay(100U); + + phost->device.speed = USBH_LL_GetSpeed(phost); + + phost->gState = HOST_ENUMERATION; + + phost->Control.pipe_out = USBH_AllocPipe (phost, 0x00U); + phost->Control.pipe_in = USBH_AllocPipe (phost, 0x80U); + + + /* Open Control pipes */ + USBH_OpenPipe (phost, + phost->Control.pipe_in, + 0x80U, + phost->device.address, + phost->device.speed, + USBH_EP_CONTROL, + (uint16_t)phost->Control.pipe_size); + + /* Open Control pipes */ + USBH_OpenPipe (phost, + phost->Control.pipe_out, + 0x00U, + phost->device.address, + phost->device.speed, + USBH_EP_CONTROL, + (uint16_t)phost->Control.pipe_size); + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_PORT_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + case HOST_ENUMERATION: + /* Check for enumeration status */ + if ( USBH_HandleEnum(phost) == USBH_OK) + { + /* The function shall return USBH_OK when full enumeration is complete */ + USBH_UsrLog ("Enumeration done."); + phost->device.current_interface = 0U; + if(phost->device.DevDesc.bNumConfigurations == 1U) + { + USBH_UsrLog ("This device has only 1 configuration."); + phost->gState = HOST_SET_CONFIGURATION; + + } + else + { + phost->gState = HOST_INPUT; + } + + } + break; + + case HOST_INPUT: + { + /* user callback for end of device basic enumeration */ + if(phost->pUser != NULL) + { + phost->pUser(phost, HOST_USER_SELECT_CONFIGURATION); + phost->gState = HOST_SET_CONFIGURATION; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + } + break; + + case HOST_SET_CONFIGURATION: + /* set configuration */ + if (USBH_SetCfg(phost, (uint16_t)phost->device.CfgDesc.bConfigurationValue) == USBH_OK) + { + phost->gState = HOST_SET_WAKEUP_FEATURE; + USBH_UsrLog ("Default configuration set."); + } + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_PORT_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + case HOST_SET_WAKEUP_FEATURE: + + if ((phost->device.CfgDesc.bmAttributes) & (1U << 5)) + { + if (USBH_SetFeature(phost, FEATURE_SELECTOR_REMOTEWAKEUP) == USBH_OK) + { + USBH_UsrLog ("Device remote wakeup enabled"); + phost->gState = HOST_CHECK_CLASS; + } + } + else + { + phost->gState = HOST_CHECK_CLASS; + } + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_PORT_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + case HOST_CHECK_CLASS: + + if(phost->ClassNumber == 0U) + { + USBH_UsrLog ("No Class has been registered."); + } + else + { + phost->pActiveClass = NULL; + + for (idx = 0U; idx < USBH_MAX_NUM_SUPPORTED_CLASS; idx++) + { + if(phost->pClass[idx]->ClassCode == phost->device.CfgDesc.Itf_Desc[0].bInterfaceClass) + { + phost->pActiveClass = phost->pClass[idx]; + } + } + + if(phost->pActiveClass != NULL) + { + if(phost->pActiveClass->Init(phost)== USBH_OK) + { + phost->gState = HOST_CLASS_REQUEST; + USBH_UsrLog ("%s class started.", phost->pActiveClass->Name); + + /* Inform user that a class has been activated */ + phost->pUser(phost, HOST_USER_CLASS_SELECTED); + } + else + { + phost->gState = HOST_ABORT_STATE; + USBH_UsrLog ("Device not supporting %s class.", phost->pActiveClass->Name); + } + } + else + { + phost->gState = HOST_ABORT_STATE; + USBH_UsrLog ("No registered class for this device."); + } + } + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + case HOST_CLASS_REQUEST: + /* process class standard control requests state machine */ + if(phost->pActiveClass != NULL) + { + status = phost->pActiveClass->Requests(phost); + + if(status == USBH_OK) + { + phost->gState = HOST_CLASS; + } + } + else + { + phost->gState = HOST_ABORT_STATE; + USBH_ErrLog ("Invalid Class Driver."); + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + + break; + case HOST_CLASS: + /* process class state machine */ + if(phost->pActiveClass != NULL) + { + phost->pActiveClass->BgndProcess(phost); + } + break; + + case HOST_DEV_DISCONNECTED : + + DeInitStateMachine(phost); + + /* Re-Initilaize Host for new Enumeration */ + if(phost->pActiveClass != NULL) + { + phost->pActiveClass->DeInit(phost); + phost->pActiveClass = NULL; + } + break; + + case HOST_ABORT_STATE: + default : + break; + } + return USBH_OK; +} + + +/** + * @brief USBH_HandleEnum + * This function includes the complete enumeration process + * @param phost: Host Handle + * @retval USBH_Status + */ +static USBH_StatusTypeDef USBH_HandleEnum (USBH_HandleTypeDef *phost) +{ + USBH_StatusTypeDef Status = USBH_BUSY; + + switch (phost->EnumState) + { + case ENUM_IDLE: + /* Get Device Desc for only 1st 8 bytes : To get EP0 MaxPacketSize */ + if ( USBH_Get_DevDesc(phost, 8U) == USBH_OK) + { + phost->Control.pipe_size = phost->device.DevDesc.bMaxPacketSize; + + phost->EnumState = ENUM_GET_FULL_DEV_DESC; + + /* modify control channels configuration for MaxPacket size */ + USBH_OpenPipe (phost, + phost->Control.pipe_in, + 0x80U, + phost->device.address, + phost->device.speed, + USBH_EP_CONTROL, + (uint16_t)phost->Control.pipe_size); + + /* Open Control pipes */ + USBH_OpenPipe (phost, + phost->Control.pipe_out, + 0x00U, + phost->device.address, + phost->device.speed, + USBH_EP_CONTROL, + (uint16_t)phost->Control.pipe_size); + + } + break; + + case ENUM_GET_FULL_DEV_DESC: + /* Get FULL Device Desc */ + if ( USBH_Get_DevDesc(phost, USB_DEVICE_DESC_SIZE)== USBH_OK) + { + USBH_UsrLog("PID: %xh", phost->device.DevDesc.idProduct ); + USBH_UsrLog("VID: %xh", phost->device.DevDesc.idVendor ); + + phost->EnumState = ENUM_SET_ADDR; + + } + break; + + case ENUM_SET_ADDR: + /* set address */ + if ( USBH_SetAddress(phost, USBH_DEVICE_ADDRESS) == USBH_OK) + { + USBH_Delay(2U); + phost->device.address = USBH_DEVICE_ADDRESS; + + /* user callback for device address assigned */ + USBH_UsrLog("Address (#%d) assigned.", phost->device.address); + phost->EnumState = ENUM_GET_CFG_DESC; + + /* modify control channels to update device address */ + USBH_OpenPipe (phost, + phost->Control.pipe_in, + 0x80U, + phost->device.address, + phost->device.speed, + USBH_EP_CONTROL, + (uint16_t)phost->Control.pipe_size); + + /* Open Control pipes */ + USBH_OpenPipe (phost, + phost->Control.pipe_out, + 0x00U, + phost->device.address, + phost->device.speed, + USBH_EP_CONTROL, + (uint16_t)phost->Control.pipe_size); + } + break; + + case ENUM_GET_CFG_DESC: + /* get standard configuration descriptor */ + if ( USBH_Get_CfgDesc(phost, + USB_CONFIGURATION_DESC_SIZE) == USBH_OK) + { + phost->EnumState = ENUM_GET_FULL_CFG_DESC; + } + break; + + case ENUM_GET_FULL_CFG_DESC: + /* get FULL config descriptor (config, interface, endpoints) */ + if (USBH_Get_CfgDesc(phost, + phost->device.CfgDesc.wTotalLength) == USBH_OK) + { + phost->EnumState = ENUM_GET_MFC_STRING_DESC; + } + break; + + case ENUM_GET_MFC_STRING_DESC: + if (phost->device.DevDesc.iManufacturer != 0U) + { /* Check that Manufacturer String is available */ + + if ( USBH_Get_StringDesc(phost, + phost->device.DevDesc.iManufacturer, + phost->device.Data, + 0xFFU) == USBH_OK) + { + /* User callback for Manufacturing string */ + USBH_UsrLog("Manufacturer : %s", (char *)(void*)phost->device.Data); + phost->EnumState = ENUM_GET_PRODUCT_STRING_DESC; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + } + else + { + USBH_UsrLog("Manufacturer : N/A"); + phost->EnumState = ENUM_GET_PRODUCT_STRING_DESC; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + break; + + case ENUM_GET_PRODUCT_STRING_DESC: + if (phost->device.DevDesc.iProduct != 0U) + { /* Check that Product string is available */ + if ( USBH_Get_StringDesc(phost, + phost->device.DevDesc.iProduct, + phost->device.Data, + 0xFFU) == USBH_OK) + { + /* User callback for Product string */ + USBH_UsrLog("Product : %s", (char *)(void *)phost->device.Data); + phost->EnumState = ENUM_GET_SERIALNUM_STRING_DESC; + } + } + else + { + USBH_UsrLog("Product : N/A"); + phost->EnumState = ENUM_GET_SERIALNUM_STRING_DESC; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + break; + + case ENUM_GET_SERIALNUM_STRING_DESC: + if (phost->device.DevDesc.iSerialNumber != 0U) + { /* Check that Serial number string is available */ + if ( USBH_Get_StringDesc(phost, + phost->device.DevDesc.iSerialNumber, + phost->device.Data, + 0xFFU) == USBH_OK) + { + /* User callback for Serial number string */ + USBH_UsrLog("Serial Number : %s", (char *)(void*)phost->device.Data); + Status = USBH_OK; + } + } + else + { + USBH_UsrLog("Serial Number : N/A"); + Status = USBH_OK; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_STATE_CHANGED_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + break; + + default: + break; + } + return Status; +} + +/** + * @brief USBH_LL_SetTimer + * Set the initial Host Timer tick + * @param phost: Host Handle + * @retval None + */ +void USBH_LL_SetTimer (USBH_HandleTypeDef *phost, uint32_t time) +{ + phost->Timer = time; +} +/** + * @brief USBH_LL_IncTimer + * Increment Host Timer tick + * @param phost: Host Handle + * @retval None + */ +void USBH_LL_IncTimer (USBH_HandleTypeDef *phost) +{ + phost->Timer ++; + USBH_HandleSof(phost); +} + +/** + * @brief USBH_HandleSof + * Call SOF process + * @param phost: Host Handle + * @retval None + */ +static void USBH_HandleSof (USBH_HandleTypeDef *phost) +{ + if((phost->gState == HOST_CLASS)&&(phost->pActiveClass != NULL)) + { + phost->pActiveClass->SOFProcess(phost); + } +} + +/** + * @brief USBH_PortEnabled + * Port Enabled + * @param phost: Host Handle + * @retval None + */ +void USBH_LL_PortEnabled (USBH_HandleTypeDef *phost) +{ + phost->device.PortEnabled = 1U; + + return; +} + +/** + * @brief USBH_LL_PortDisabled + * Port Disabled + * @param phost: Host Handle + * @retval None + */ +void USBH_LL_PortDisabled (USBH_HandleTypeDef *phost) +{ + phost->device.PortEnabled = 0U; + + return; +} + +/** + * @brief HCD_IsPortEnabled + * Is Port Enabled + * @param phost: Host Handle + * @retval None + */ +uint8_t USBH_IsPortEnabled(USBH_HandleTypeDef *phost) +{ + return(phost->device.PortEnabled); +} + + +/** + * @brief USBH_LL_Connect + * Handle USB Host connexion event + * @param phost: Host Handle + * @retval USBH_Status + */ +USBH_StatusTypeDef USBH_LL_Connect (USBH_HandleTypeDef *phost) +{ + if(phost->gState == HOST_IDLE ) + { + phost->device.is_connected = 1U; + + if(phost->pUser != NULL) + { + phost->pUser(phost, HOST_USER_CONNECTION); + } + } + else + { + if (phost->device.PortEnabled == 1U) + { + phost->gState = HOST_DEV_ATTACHED; + } + } + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_PORT_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + + return USBH_OK; +} + +/** + * @brief USBH_LL_Disconnect + * Handle USB Host disconnection event + * @param phost: Host Handle + * @retval USBH_Status + */ +USBH_StatusTypeDef USBH_LL_Disconnect (USBH_HandleTypeDef *phost) +{ + /*Stop Host */ + USBH_LL_Stop(phost); + + /* FRee Control Pipes */ + USBH_FreePipe (phost, phost->Control.pipe_in); + USBH_FreePipe (phost, phost->Control.pipe_out); + + phost->device.is_connected = 0U; + + if(phost->pUser != NULL) + { + phost->pUser(phost, HOST_USER_DISCONNECTION); + } + USBH_UsrLog("USB Device disconnected"); + + /* Start the low level driver */ + USBH_LL_Start(phost); + + phost->gState = HOST_DEV_DISCONNECTED; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_PORT_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + + return USBH_OK; +} + + +#if (USBH_USE_OS == 1U) +/** + * @brief USB Host Thread task + * @param pvParameters not used + * @retval None + */ + +#if (osCMSIS < 0x20000U) +static void USBH_Process_OS(void const *argument) +{ + osEvent event; + + for(;;) + { + event = osMessageGet(((USBH_HandleTypeDef *)argument)->os_event, osWaitForever); + if(event.status == osEventMessage) + { + USBH_Process((USBH_HandleTypeDef *)argument); + } + } +} +#else +static void USBH_Process_OS(void *argument) +{ + osStatus_t status; + + for(;;) + { + status = osMessageQueueGet(((USBH_HandleTypeDef *)argument)->os_event, + &((USBH_HandleTypeDef *)argument)->os_msg, NULL, osWaitForever); + if (status == osOK) + { + USBH_Process((USBH_HandleTypeDef *)argument); + } + } +} +#endif /* (osCMSIS < 0x20000U) */ + +/** +* @brief USBH_LL_NotifyURBChange +* Notify URB state Change +* @param phost: Host handle +* @retval USBH Status +*/ +USBH_StatusTypeDef USBH_LL_NotifyURBChange (USBH_HandleTypeDef *phost) +{ + phost->os_msg = (uint32_t)USBH_PORT_EVENT; + +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif + + return USBH_OK; +} +#endif +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Core/Src/usbh_ctlreq.c b/system/Middlewares/ST/STM32_USB_Host_Library/Core/Src/usbh_ctlreq.c index 91cbee756e..5e48c6b49a 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Core/Src/usbh_ctlreq.c +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Core/Src/usbh_ctlreq.c @@ -1,1008 +1,1008 @@ -/** - ****************************************************************************** - * @file usbh_ctlreq.c - * @author MCD Application Team - * @brief This file implements the control requests for device enumeration - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Includes ------------------------------------------------------------------*/ - -#include "usbh_ctlreq.h" - -/** @addtogroup USBH_LIB -* @{ -*/ - -/** @addtogroup USBH_LIB_CORE -* @{ -*/ - -/** @defgroup USBH_CTLREQ -* @brief This file implements the standard requests for device enumeration -* @{ -*/ - - -/** @defgroup USBH_CTLREQ_Private_Defines -* @{ -*/ -/** -* @} -*/ - - -/** @defgroup USBH_CTLREQ_Private_TypesDefinitions -* @{ -*/ -/** -* @} -*/ - - - -/** @defgroup USBH_CTLREQ_Private_Macros -* @{ -*/ -/** -* @} -*/ - - -/** @defgroup USBH_CTLREQ_Private_Variables -* @{ -*/ -/** -* @} -*/ - -/** @defgroup USBH_CTLREQ_Private_FunctionPrototypes -* @{ -*/ -static USBH_StatusTypeDef USBH_HandleControl (USBH_HandleTypeDef *phost); - -static void USBH_ParseDevDesc (USBH_DevDescTypeDef *dev_desc, uint8_t *buf, - uint16_t length); - -static void USBH_ParseCfgDesc (USBH_CfgDescTypeDef *cfg_desc, uint8_t *buf, - uint16_t length); - -static void USBH_ParseEPDesc (USBH_EpDescTypeDef *ep_descriptor, uint8_t *buf); -static void USBH_ParseStringDesc (uint8_t* psrc, uint8_t* pdest, uint16_t length); -static void USBH_ParseInterfaceDesc (USBH_InterfaceDescTypeDef *if_descriptor, uint8_t *buf); - - -/** -* @} -*/ - - -/** @defgroup USBH_CTLREQ_Private_Functions -* @{ -*/ - - -/** - * @brief USBH_Get_DevDesc - * Issue Get Device Descriptor command to the device. Once the response - * received, it parses the device descriptor and updates the status. - * @param phost: Host Handle - * @param length: Length of the descriptor - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_Get_DevDesc(USBH_HandleTypeDef *phost, uint8_t length) -{ - USBH_StatusTypeDef status; - - if((status = USBH_GetDescriptor(phost, - USB_REQ_RECIPIENT_DEVICE | USB_REQ_TYPE_STANDARD, - USB_DESC_DEVICE, - phost->device.Data, - (uint16_t)length)) == USBH_OK) - { - /* Commands successfully sent and Response Received */ - USBH_ParseDevDesc(&phost->device.DevDesc, phost->device.Data, - (uint16_t)length); - } - return status; -} - -/** - * @brief USBH_Get_CfgDesc - * Issues Configuration Descriptor to the device. Once the response - * received, it parses the configuration descriptor and updates the - * status. - * @param phost: Host Handle - * @param length: Length of the descriptor - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_Get_CfgDesc(USBH_HandleTypeDef *phost, - uint16_t length) - -{ - USBH_StatusTypeDef status; - uint8_t *pData; -#if (USBH_KEEP_CFG_DESCRIPTOR == 1U) - pData = phost->device.CfgDesc_Raw; -#else - pData = phost->device.Data; -#endif - if((status = USBH_GetDescriptor(phost, - USB_REQ_RECIPIENT_DEVICE | USB_REQ_TYPE_STANDARD, - USB_DESC_CONFIGURATION, - pData, - length)) == USBH_OK) - { - - /* Commands successfully sent and Response Received */ - USBH_ParseCfgDesc (&phost->device.CfgDesc, - pData, - length); - - } - return status; -} - - -/** - * @brief USBH_Get_StringDesc - * Issues string Descriptor command to the device. Once the response - * received, it parses the string descriptor and updates the status. - * @param phost: Host Handle - * @param string_index: String index for the descriptor - * @param buff: Buffer address for the descriptor - * @param length: Length of the descriptor - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_Get_StringDesc(USBH_HandleTypeDef *phost, - uint8_t string_index, - uint8_t *buff, - uint16_t length) -{ - USBH_StatusTypeDef status; - if((status = USBH_GetDescriptor(phost, - USB_REQ_RECIPIENT_DEVICE | USB_REQ_TYPE_STANDARD, - USB_DESC_STRING | string_index, - phost->device.Data, - length)) == USBH_OK) - { - /* Commands successfully sent and Response Received */ - USBH_ParseStringDesc(phost->device.Data,buff, length); - } - return status; -} - -/** - * @brief USBH_GetDescriptor - * Issues Descriptor command to the device. Once the response received, - * it parses the descriptor and updates the status. - * @param phost: Host Handle - * @param req_type: Descriptor type - * @param value_idx: Value for the GetDescriptr request - * @param buff: Buffer to store the descriptor - * @param length: Length of the descriptor - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_GetDescriptor(USBH_HandleTypeDef *phost, - uint8_t req_type, - uint16_t value_idx, - uint8_t* buff, - uint16_t length) -{ - if(phost->RequestState == CMD_SEND) - { - phost->Control.setup.b.bmRequestType = USB_D2H | req_type; - phost->Control.setup.b.bRequest = USB_REQ_GET_DESCRIPTOR; - phost->Control.setup.b.wValue.w = value_idx; - - if ((value_idx & 0xff00U) == USB_DESC_STRING) - { - phost->Control.setup.b.wIndex.w = 0x0409U; - } - else - { - phost->Control.setup.b.wIndex.w = 0U; - } - phost->Control.setup.b.wLength.w = length; - } - return USBH_CtlReq(phost, buff, length); -} - -/** - * @brief USBH_SetAddress - * This command sets the address to the connected device - * @param phost: Host Handle - * @param DeviceAddress: Device address to assign - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_SetAddress(USBH_HandleTypeDef *phost, - uint8_t DeviceAddress) -{ - if(phost->RequestState == CMD_SEND) - { - phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_DEVICE | \ - USB_REQ_TYPE_STANDARD; - - phost->Control.setup.b.bRequest = USB_REQ_SET_ADDRESS; - - phost->Control.setup.b.wValue.w = (uint16_t)DeviceAddress; - phost->Control.setup.b.wIndex.w = 0U; - phost->Control.setup.b.wLength.w = 0U; - } - return USBH_CtlReq(phost, 0U, 0U); -} - -/** - * @brief USBH_SetCfg - * The command sets the configuration value to the connected device - * @param phost: Host Handle - * @param cfg_idx: Configuration value - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_SetCfg(USBH_HandleTypeDef *phost, uint16_t cfg_idx) -{ - if(phost->RequestState == CMD_SEND) - { - phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_DEVICE - | USB_REQ_TYPE_STANDARD; - - phost->Control.setup.b.bRequest = USB_REQ_SET_CONFIGURATION; - phost->Control.setup.b.wValue.w = cfg_idx; - phost->Control.setup.b.wIndex.w = 0U; - phost->Control.setup.b.wLength.w = 0U; - } - - return USBH_CtlReq(phost, 0U , 0U); -} - -/** - * @brief USBH_SetInterface - * The command sets the Interface value to the connected device - * @param phost: Host Handle - * @param altSetting: Interface value - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_SetInterface(USBH_HandleTypeDef *phost, uint8_t ep_num, - uint8_t altSetting) -{ - if(phost->RequestState == CMD_SEND) - { - phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_INTERFACE - | USB_REQ_TYPE_STANDARD; - - phost->Control.setup.b.bRequest = USB_REQ_SET_INTERFACE; - phost->Control.setup.b.wValue.w = altSetting; - phost->Control.setup.b.wIndex.w = ep_num; - phost->Control.setup.b.wLength.w = 0U; - } - return USBH_CtlReq(phost, 0U , 0U); -} - -/** - * @brief USBH_SetFeature - * The command sets the device features (remote wakeup feature,..) - * @param pdev: Selected device - * @param itf_idx - * @retval Status -*/ -USBH_StatusTypeDef USBH_SetFeature(USBH_HandleTypeDef *phost, uint8_t wValue) -{ - if(phost->RequestState == CMD_SEND) - { - phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_DEVICE - | USB_REQ_TYPE_STANDARD; - - phost->Control.setup.b.bRequest = USB_REQ_SET_FEATURE; - phost->Control.setup.b.wValue.w = wValue; - phost->Control.setup.b.wIndex.w = 0U; - phost->Control.setup.b.wLength.w = 0U; - } - - return USBH_CtlReq(phost, 0U, 0U); -} - -/** - * @brief USBH_ClrFeature - * This request is used to clear or disable a specific feature. - * @param phost: Host Handle - * @param ep_num: endpoint number - * @param hc_num: Host channel number - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_ClrFeature(USBH_HandleTypeDef *phost, uint8_t ep_num) -{ - if(phost->RequestState == CMD_SEND) - { - phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_ENDPOINT - | USB_REQ_TYPE_STANDARD; - - phost->Control.setup.b.bRequest = USB_REQ_CLEAR_FEATURE; - phost->Control.setup.b.wValue.w = FEATURE_SELECTOR_ENDPOINT; - phost->Control.setup.b.wIndex.w = ep_num; - phost->Control.setup.b.wLength.w = 0U; - } - return USBH_CtlReq(phost, 0U , 0U); -} - -/** - * @brief USBH_ParseDevDesc - * This function Parses the device descriptor - * @param dev_desc: device_descriptor destination address - * @param buf: Buffer where the source descriptor is available - * @param length: Length of the descriptor - * @retval None - */ -static void USBH_ParseDevDesc (USBH_DevDescTypeDef* dev_desc, uint8_t *buf, - uint16_t length) -{ - dev_desc->bLength = *(uint8_t *) (buf + 0); - dev_desc->bDescriptorType = *(uint8_t *) (buf + 1); - dev_desc->bcdUSB = LE16 (buf + 2); - dev_desc->bDeviceClass = *(uint8_t *) (buf + 4); - dev_desc->bDeviceSubClass = *(uint8_t *) (buf + 5); - dev_desc->bDeviceProtocol = *(uint8_t *) (buf + 6); - dev_desc->bMaxPacketSize = *(uint8_t *) (buf + 7); - - if (length > 8U) - { /* For 1st time after device connection, Host may issue only 8 bytes for - Device Descriptor Length */ - dev_desc->idVendor = LE16 (buf + 8); - dev_desc->idProduct = LE16 (buf + 10); - dev_desc->bcdDevice = LE16 (buf + 12); - dev_desc->iManufacturer = *(uint8_t *) (buf + 14); - dev_desc->iProduct = *(uint8_t *) (buf + 15); - dev_desc->iSerialNumber = *(uint8_t *) (buf + 16); - dev_desc->bNumConfigurations = *(uint8_t *) (buf + 17); - } -} - -/** - * @brief USBH_ParseCfgDesc - * This function Parses the configuration descriptor - * @param cfg_desc: Configuration Descriptor address - * @param buf: Buffer where the source descriptor is available - * @param length: Length of the descriptor - * @retval None - */ -static void USBH_ParseCfgDesc (USBH_CfgDescTypeDef* cfg_desc, uint8_t *buf, - uint16_t length) -{ - USBH_InterfaceDescTypeDef *pif ; - USBH_EpDescTypeDef *pep; - USBH_DescHeader_t *pdesc = (USBH_DescHeader_t *)(void *)buf; - uint16_t ptr; - uint8_t if_ix = 0U; - uint8_t ep_ix = 0U; - - pdesc = (USBH_DescHeader_t *)(void *)buf; - - /* Parse configuration descriptor */ - cfg_desc->bLength = *(uint8_t *) (buf + 0); - cfg_desc->bDescriptorType = *(uint8_t *) (buf + 1); - cfg_desc->wTotalLength = LE16 (buf + 2); - cfg_desc->bNumInterfaces = *(uint8_t *) (buf + 4); - cfg_desc->bConfigurationValue = *(uint8_t *) (buf + 5); - cfg_desc->iConfiguration = *(uint8_t *) (buf + 6); - cfg_desc->bmAttributes = *(uint8_t *) (buf + 7); - cfg_desc->bMaxPower = *(uint8_t *) (buf + 8); - - - if (length > USB_CONFIGURATION_DESC_SIZE) - { - ptr = USB_LEN_CFG_DESC; - pif = (USBH_InterfaceDescTypeDef *)0; - - - while ((if_ix < USBH_MAX_NUM_INTERFACES) && (ptr < cfg_desc->wTotalLength)) - { - pdesc = USBH_GetNextDesc((uint8_t *)(void *)pdesc, &ptr); - if (pdesc->bDescriptorType == USB_DESC_TYPE_INTERFACE) - { - pif = &cfg_desc->Itf_Desc[if_ix]; - USBH_ParseInterfaceDesc (pif, (uint8_t *)(void *)pdesc); - - ep_ix = 0U; - pep = (USBH_EpDescTypeDef *)0; - while ((ep_ix < pif->bNumEndpoints) && (ptr < cfg_desc->wTotalLength)) - { - pdesc = USBH_GetNextDesc((uint8_t*)(void *)pdesc, &ptr); - if (pdesc->bDescriptorType == USB_DESC_TYPE_ENDPOINT) - { - pep = &cfg_desc->Itf_Desc[if_ix].Ep_Desc[ep_ix]; - USBH_ParseEPDesc (pep, (uint8_t *)(void *)pdesc); - ep_ix++; - } - } - if_ix++; - } - } - } -} - - - -/** - * @brief USBH_ParseInterfaceDesc - * This function Parses the interface descriptor - * @param if_descriptor : Interface descriptor destination - * @param buf: Buffer where the descriptor data is available - * @retval None - */ -static void USBH_ParseInterfaceDesc (USBH_InterfaceDescTypeDef *if_descriptor, - uint8_t *buf) -{ - if_descriptor->bLength = *(uint8_t *) (buf + 0); - if_descriptor->bDescriptorType = *(uint8_t *) (buf + 1); - if_descriptor->bInterfaceNumber = *(uint8_t *) (buf + 2); - if_descriptor->bAlternateSetting = *(uint8_t *) (buf + 3); - if_descriptor->bNumEndpoints = *(uint8_t *) (buf + 4); - if_descriptor->bInterfaceClass = *(uint8_t *) (buf + 5); - if_descriptor->bInterfaceSubClass = *(uint8_t *) (buf + 6); - if_descriptor->bInterfaceProtocol = *(uint8_t *) (buf + 7); - if_descriptor->iInterface = *(uint8_t *) (buf + 8); -} - -/** - * @brief USBH_ParseEPDesc - * This function Parses the endpoint descriptor - * @param ep_descriptor: Endpoint descriptor destination address - * @param buf: Buffer where the parsed descriptor stored - * @retval None - */ -static void USBH_ParseEPDesc (USBH_EpDescTypeDef *ep_descriptor, - uint8_t *buf) -{ - - ep_descriptor->bLength = *(uint8_t *) (buf + 0); - ep_descriptor->bDescriptorType = *(uint8_t *) (buf + 1); - ep_descriptor->bEndpointAddress = *(uint8_t *) (buf + 2); - ep_descriptor->bmAttributes = *(uint8_t *) (buf + 3); - ep_descriptor->wMaxPacketSize = LE16 (buf + 4); - ep_descriptor->bInterval = *(uint8_t *) (buf + 6); -} - -/** - * @brief USBH_ParseStringDesc - * This function Parses the string descriptor - * @param psrc: Source pointer containing the descriptor data - * @param pdest: Destination address pointer - * @param length: Length of the descriptor - * @retval None - */ -static void USBH_ParseStringDesc (uint8_t* psrc, uint8_t* pdest, uint16_t length) -{ - uint16_t strlength; - uint16_t idx; - - /* The UNICODE string descriptor is not NULL-terminated. The string length is - computed by substracting two from the value of the first byte of the descriptor. - */ - - /* Check which is lower size, the Size of string or the length of bytes read - from the device */ - - if (psrc[1] == USB_DESC_TYPE_STRING) - { - /* Make sure the Descriptor is String Type */ - - /* psrc[0] contains Size of Descriptor, subtract 2 to get the length of string */ - strlength = ((((uint16_t)psrc[0] - 2U) <= length) ? ((uint16_t)psrc[0] - 2U) : length); - - /* Adjust the offset ignoring the String Len and Descriptor type */ - psrc += 2U; - - for (idx = 0U; idx < strlength; idx += 2U) - { - /* Copy Only the string and ignore the UNICODE ID, hence add the src */ - *pdest = psrc[idx]; - pdest++; - } - *pdest = 0U; /* mark end of string */ - } -} - -/** - * @brief USBH_GetNextDesc - * This function return the next descriptor header - * @param buf: Buffer where the cfg descriptor is available - * @param ptr: data pointer inside the cfg descriptor - * @retval next header - */ -USBH_DescHeader_t *USBH_GetNextDesc (uint8_t *pbuf, uint16_t *ptr) -{ - USBH_DescHeader_t *pnext; - - *ptr += ((USBH_DescHeader_t *)(void *)pbuf)->bLength; - pnext = (USBH_DescHeader_t *)(void *)((uint8_t *)(void *)pbuf + \ - ((USBH_DescHeader_t *)(void *)pbuf)->bLength); - - return(pnext); -} - - -/** - * @brief USBH_CtlReq - * USBH_CtlReq sends a control request and provide the status after - * completion of the request - * @param phost: Host Handle - * @param req: Setup Request Structure - * @param buff: data buffer address to store the response - * @param length: length of the response - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_CtlReq (USBH_HandleTypeDef *phost, - uint8_t *buff, - uint16_t length) -{ - USBH_StatusTypeDef status; - status = USBH_BUSY; - - switch (phost->RequestState) - { - case CMD_SEND: - /* Start a SETUP transfer */ - phost->Control.buff = buff; - phost->Control.length = length; - phost->Control.state = CTRL_SETUP; - phost->RequestState = CMD_WAIT; - status = USBH_BUSY; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - break; - - case CMD_WAIT: - status = USBH_HandleControl(phost); - if (status == USBH_OK) - { - /* Commands successfully sent and Response Received */ - phost->RequestState = CMD_SEND; - phost->Control.state =CTRL_IDLE; - status = USBH_OK; - } - else if (status == USBH_NOT_SUPPORTED) - { - /* Commands successfully sent and Response Received */ - phost->RequestState = CMD_SEND; - phost->Control.state = CTRL_IDLE; - status = USBH_NOT_SUPPORTED; - } - else - { - if (status == USBH_FAIL) - { - /* Failure Mode */ - phost->RequestState = CMD_SEND; - status = USBH_FAIL; - } - } - break; - - default: - break; - } - return status; -} - -/** - * @brief USBH_HandleControl - * Handles the USB control transfer state machine - * @param phost: Host Handle - * @retval USBH Status - */ -static USBH_StatusTypeDef USBH_HandleControl (USBH_HandleTypeDef *phost) -{ - uint8_t direction; - USBH_StatusTypeDef status = USBH_BUSY; - USBH_URBStateTypeDef URB_Status = USBH_URB_IDLE; - - switch (phost->Control.state) - { - case CTRL_SETUP: - /* send a SETUP packet */ - USBH_CtlSendSetup (phost, (uint8_t *)(void *)phost->Control.setup.d8, - phost->Control.pipe_out); - - phost->Control.state = CTRL_SETUP_WAIT; - break; - - case CTRL_SETUP_WAIT: - - URB_Status = USBH_LL_GetURBState(phost, phost->Control.pipe_out); - /* case SETUP packet sent successfully */ - if(URB_Status == USBH_URB_DONE) - { - direction = (phost->Control.setup.b.bmRequestType & USB_REQ_DIR_MASK); - - /* check if there is a data stage */ - if (phost->Control.setup.b.wLength.w != 0U) - { - if (direction == USB_D2H) - { - /* Data Direction is IN */ - phost->Control.state = CTRL_DATA_IN; - } - else - { - /* Data Direction is OUT */ - phost->Control.state = CTRL_DATA_OUT; - } - } - /* No DATA stage */ - else - { - /* If there is No Data Transfer Stage */ - if (direction == USB_D2H) - { - /* Data Direction is IN */ - phost->Control.state = CTRL_STATUS_OUT; - } - else - { - /* Data Direction is OUT */ - phost->Control.state = CTRL_STATUS_IN; - } - } - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - else - { - if((URB_Status == USBH_URB_ERROR) || (URB_Status == USBH_URB_NOTREADY)) - { - phost->Control.state = CTRL_ERROR; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - } - break; - - case CTRL_DATA_IN: - /* Issue an IN token */ - phost->Control.timer = (uint16_t)phost->Timer; - USBH_CtlReceiveData(phost, - phost->Control.buff, - phost->Control.length, - phost->Control.pipe_in); - - phost->Control.state = CTRL_DATA_IN_WAIT; - break; - - case CTRL_DATA_IN_WAIT: - - URB_Status = USBH_LL_GetURBState(phost , phost->Control.pipe_in); - - /* check is DATA packet transferred successfully */ - if (URB_Status == USBH_URB_DONE) - { - phost->Control.state = CTRL_STATUS_OUT; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - - /* manage error cases*/ - if (URB_Status == USBH_URB_STALL) - { - /* In stall case, return to previous machine state*/ - status = USBH_NOT_SUPPORTED; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - else - { - if (URB_Status == USBH_URB_ERROR) - { - /* Device error */ - phost->Control.state = CTRL_ERROR; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - } - break; - - case CTRL_DATA_OUT: - - USBH_CtlSendData (phost, - phost->Control.buff, - phost->Control.length , - phost->Control.pipe_out, - 1U); - phost->Control.timer = (uint16_t)phost->Timer; - phost->Control.state = CTRL_DATA_OUT_WAIT; - break; - - case CTRL_DATA_OUT_WAIT: - - URB_Status = USBH_LL_GetURBState(phost , phost->Control.pipe_out); - - if (URB_Status == USBH_URB_DONE) - { /* If the Setup Pkt is sent successful, then change the state */ - phost->Control.state = CTRL_STATUS_IN; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - - /* handle error cases */ - else if (URB_Status == USBH_URB_STALL) - { - /* In stall case, return to previous machine state*/ - phost->Control.state = CTRL_STALLED; - status = USBH_NOT_SUPPORTED; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - else if (URB_Status == USBH_URB_NOTREADY) - { - /* Nack received from device */ - phost->Control.state = CTRL_DATA_OUT; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - else - { - if (URB_Status == USBH_URB_ERROR) - { - /* device error */ - phost->Control.state = CTRL_ERROR; - status = USBH_FAIL; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - } - break; - - - case CTRL_STATUS_IN: - /* Send 0 bytes out packet */ - USBH_CtlReceiveData (phost, - 0U, - 0U, - phost->Control.pipe_in); - phost->Control.timer = (uint16_t)phost->Timer; - phost->Control.state = CTRL_STATUS_IN_WAIT; - - break; - - case CTRL_STATUS_IN_WAIT: - - URB_Status = USBH_LL_GetURBState(phost , phost->Control.pipe_in); - - if ( URB_Status == USBH_URB_DONE) - { /* Control transfers completed, Exit the State Machine */ - phost->Control.state = CTRL_COMPLETE; - status = USBH_OK; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - else if (URB_Status == USBH_URB_ERROR) - { - phost->Control.state = CTRL_ERROR; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - else - { - if(URB_Status == USBH_URB_STALL) - { - /* Control transfers completed, Exit the State Machine */ - status = USBH_NOT_SUPPORTED; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - } - break; - - case CTRL_STATUS_OUT: - USBH_CtlSendData (phost, - 0U, - 0U, - phost->Control.pipe_out, - 1U); - phost->Control.timer = (uint16_t)phost->Timer; - phost->Control.state = CTRL_STATUS_OUT_WAIT; - break; - - case CTRL_STATUS_OUT_WAIT: - - URB_Status = USBH_LL_GetURBState(phost , phost->Control.pipe_out); - if (URB_Status == USBH_URB_DONE) - { - status = USBH_OK; - phost->Control.state = CTRL_COMPLETE; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - else if (URB_Status == USBH_URB_NOTREADY) - { - phost->Control.state = CTRL_STATUS_OUT; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - else - { - if (URB_Status == USBH_URB_ERROR) - { - phost->Control.state = CTRL_ERROR; - -#if (USBH_USE_OS == 1U) - phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; -#if (osCMSIS < 0x20000U) - (void)osMessagePut(phost->os_event, phost->os_msg, 0U); -#else - (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); -#endif -#endif - } - - } - break; - - case CTRL_ERROR: - /* - After a halt condition is encountered or an error is detected by the - host, a control endpoint is allowed to recover by accepting the next Setup - PID; i.e., recovery actions via some other pipe are not required for control - endpoints. For the Default Control Pipe, a device reset will ultimately be - required to clear the halt or error condition if the next Setup PID is not - accepted. - */ - if (++ phost->Control.errorcount <= USBH_MAX_ERROR_COUNT) - { - /* try to recover control */ - USBH_LL_Stop(phost); - - /* Do the transmission again, starting from SETUP Packet */ - phost->Control.state = CTRL_SETUP; - phost->RequestState = CMD_SEND; - } - else - { - phost->pUser(phost, HOST_USER_UNRECOVERED_ERROR); - phost->Control.errorcount = 0U; - USBH_ErrLog("Control error"); - status = USBH_FAIL; - } - break; - - default: - break; - } - return status; -} - -/** -* @} -*/ - -/** -* @} -*/ - -/** -* @} -*/ - -/** -* @} -*/ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - - - - +/** + ****************************************************************************** + * @file usbh_ctlreq.c + * @author MCD Application Team + * @brief This file implements the control requests for device enumeration + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ + +#include "usbh_ctlreq.h" + +/** @addtogroup USBH_LIB +* @{ +*/ + +/** @addtogroup USBH_LIB_CORE +* @{ +*/ + +/** @defgroup USBH_CTLREQ +* @brief This file implements the standard requests for device enumeration +* @{ +*/ + + +/** @defgroup USBH_CTLREQ_Private_Defines +* @{ +*/ +/** +* @} +*/ + + +/** @defgroup USBH_CTLREQ_Private_TypesDefinitions +* @{ +*/ +/** +* @} +*/ + + + +/** @defgroup USBH_CTLREQ_Private_Macros +* @{ +*/ +/** +* @} +*/ + + +/** @defgroup USBH_CTLREQ_Private_Variables +* @{ +*/ +/** +* @} +*/ + +/** @defgroup USBH_CTLREQ_Private_FunctionPrototypes +* @{ +*/ +static USBH_StatusTypeDef USBH_HandleControl (USBH_HandleTypeDef *phost); + +static void USBH_ParseDevDesc (USBH_DevDescTypeDef *dev_desc, uint8_t *buf, + uint16_t length); + +static void USBH_ParseCfgDesc (USBH_CfgDescTypeDef *cfg_desc, uint8_t *buf, + uint16_t length); + +static void USBH_ParseEPDesc (USBH_EpDescTypeDef *ep_descriptor, uint8_t *buf); +static void USBH_ParseStringDesc (uint8_t* psrc, uint8_t* pdest, uint16_t length); +static void USBH_ParseInterfaceDesc (USBH_InterfaceDescTypeDef *if_descriptor, uint8_t *buf); + + +/** +* @} +*/ + + +/** @defgroup USBH_CTLREQ_Private_Functions +* @{ +*/ + + +/** + * @brief USBH_Get_DevDesc + * Issue Get Device Descriptor command to the device. Once the response + * received, it parses the device descriptor and updates the status. + * @param phost: Host Handle + * @param length: Length of the descriptor + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_Get_DevDesc(USBH_HandleTypeDef *phost, uint8_t length) +{ + USBH_StatusTypeDef status; + + if((status = USBH_GetDescriptor(phost, + USB_REQ_RECIPIENT_DEVICE | USB_REQ_TYPE_STANDARD, + USB_DESC_DEVICE, + phost->device.Data, + (uint16_t)length)) == USBH_OK) + { + /* Commands successfully sent and Response Received */ + USBH_ParseDevDesc(&phost->device.DevDesc, phost->device.Data, + (uint16_t)length); + } + return status; +} + +/** + * @brief USBH_Get_CfgDesc + * Issues Configuration Descriptor to the device. Once the response + * received, it parses the configuration descriptor and updates the + * status. + * @param phost: Host Handle + * @param length: Length of the descriptor + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_Get_CfgDesc(USBH_HandleTypeDef *phost, + uint16_t length) + +{ + USBH_StatusTypeDef status; + uint8_t *pData; +#if (USBH_KEEP_CFG_DESCRIPTOR == 1U) + pData = phost->device.CfgDesc_Raw; +#else + pData = phost->device.Data; +#endif + if((status = USBH_GetDescriptor(phost, + USB_REQ_RECIPIENT_DEVICE | USB_REQ_TYPE_STANDARD, + USB_DESC_CONFIGURATION, + pData, + length)) == USBH_OK) + { + + /* Commands successfully sent and Response Received */ + USBH_ParseCfgDesc (&phost->device.CfgDesc, + pData, + length); + + } + return status; +} + + +/** + * @brief USBH_Get_StringDesc + * Issues string Descriptor command to the device. Once the response + * received, it parses the string descriptor and updates the status. + * @param phost: Host Handle + * @param string_index: String index for the descriptor + * @param buff: Buffer address for the descriptor + * @param length: Length of the descriptor + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_Get_StringDesc(USBH_HandleTypeDef *phost, + uint8_t string_index, + uint8_t *buff, + uint16_t length) +{ + USBH_StatusTypeDef status; + if((status = USBH_GetDescriptor(phost, + USB_REQ_RECIPIENT_DEVICE | USB_REQ_TYPE_STANDARD, + USB_DESC_STRING | string_index, + phost->device.Data, + length)) == USBH_OK) + { + /* Commands successfully sent and Response Received */ + USBH_ParseStringDesc(phost->device.Data,buff, length); + } + return status; +} + +/** + * @brief USBH_GetDescriptor + * Issues Descriptor command to the device. Once the response received, + * it parses the descriptor and updates the status. + * @param phost: Host Handle + * @param req_type: Descriptor type + * @param value_idx: Value for the GetDescriptr request + * @param buff: Buffer to store the descriptor + * @param length: Length of the descriptor + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_GetDescriptor(USBH_HandleTypeDef *phost, + uint8_t req_type, + uint16_t value_idx, + uint8_t* buff, + uint16_t length) +{ + if(phost->RequestState == CMD_SEND) + { + phost->Control.setup.b.bmRequestType = USB_D2H | req_type; + phost->Control.setup.b.bRequest = USB_REQ_GET_DESCRIPTOR; + phost->Control.setup.b.wValue.w = value_idx; + + if ((value_idx & 0xff00U) == USB_DESC_STRING) + { + phost->Control.setup.b.wIndex.w = 0x0409U; + } + else + { + phost->Control.setup.b.wIndex.w = 0U; + } + phost->Control.setup.b.wLength.w = length; + } + return USBH_CtlReq(phost, buff, length); +} + +/** + * @brief USBH_SetAddress + * This command sets the address to the connected device + * @param phost: Host Handle + * @param DeviceAddress: Device address to assign + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_SetAddress(USBH_HandleTypeDef *phost, + uint8_t DeviceAddress) +{ + if(phost->RequestState == CMD_SEND) + { + phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_DEVICE | \ + USB_REQ_TYPE_STANDARD; + + phost->Control.setup.b.bRequest = USB_REQ_SET_ADDRESS; + + phost->Control.setup.b.wValue.w = (uint16_t)DeviceAddress; + phost->Control.setup.b.wIndex.w = 0U; + phost->Control.setup.b.wLength.w = 0U; + } + return USBH_CtlReq(phost, 0U, 0U); +} + +/** + * @brief USBH_SetCfg + * The command sets the configuration value to the connected device + * @param phost: Host Handle + * @param cfg_idx: Configuration value + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_SetCfg(USBH_HandleTypeDef *phost, uint16_t cfg_idx) +{ + if(phost->RequestState == CMD_SEND) + { + phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_DEVICE + | USB_REQ_TYPE_STANDARD; + + phost->Control.setup.b.bRequest = USB_REQ_SET_CONFIGURATION; + phost->Control.setup.b.wValue.w = cfg_idx; + phost->Control.setup.b.wIndex.w = 0U; + phost->Control.setup.b.wLength.w = 0U; + } + + return USBH_CtlReq(phost, 0U , 0U); +} + +/** + * @brief USBH_SetInterface + * The command sets the Interface value to the connected device + * @param phost: Host Handle + * @param altSetting: Interface value + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_SetInterface(USBH_HandleTypeDef *phost, uint8_t ep_num, + uint8_t altSetting) +{ + if(phost->RequestState == CMD_SEND) + { + phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_INTERFACE + | USB_REQ_TYPE_STANDARD; + + phost->Control.setup.b.bRequest = USB_REQ_SET_INTERFACE; + phost->Control.setup.b.wValue.w = altSetting; + phost->Control.setup.b.wIndex.w = ep_num; + phost->Control.setup.b.wLength.w = 0U; + } + return USBH_CtlReq(phost, 0U , 0U); +} + +/** + * @brief USBH_SetFeature + * The command sets the device features (remote wakeup feature,..) + * @param pdev: Selected device + * @param itf_idx + * @retval Status +*/ +USBH_StatusTypeDef USBH_SetFeature(USBH_HandleTypeDef *phost, uint8_t wValue) +{ + if(phost->RequestState == CMD_SEND) + { + phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_DEVICE + | USB_REQ_TYPE_STANDARD; + + phost->Control.setup.b.bRequest = USB_REQ_SET_FEATURE; + phost->Control.setup.b.wValue.w = wValue; + phost->Control.setup.b.wIndex.w = 0U; + phost->Control.setup.b.wLength.w = 0U; + } + + return USBH_CtlReq(phost, 0U, 0U); +} + +/** + * @brief USBH_ClrFeature + * This request is used to clear or disable a specific feature. + * @param phost: Host Handle + * @param ep_num: endpoint number + * @param hc_num: Host channel number + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_ClrFeature(USBH_HandleTypeDef *phost, uint8_t ep_num) +{ + if(phost->RequestState == CMD_SEND) + { + phost->Control.setup.b.bmRequestType = USB_H2D | USB_REQ_RECIPIENT_ENDPOINT + | USB_REQ_TYPE_STANDARD; + + phost->Control.setup.b.bRequest = USB_REQ_CLEAR_FEATURE; + phost->Control.setup.b.wValue.w = FEATURE_SELECTOR_ENDPOINT; + phost->Control.setup.b.wIndex.w = ep_num; + phost->Control.setup.b.wLength.w = 0U; + } + return USBH_CtlReq(phost, 0U , 0U); +} + +/** + * @brief USBH_ParseDevDesc + * This function Parses the device descriptor + * @param dev_desc: device_descriptor destination address + * @param buf: Buffer where the source descriptor is available + * @param length: Length of the descriptor + * @retval None + */ +static void USBH_ParseDevDesc (USBH_DevDescTypeDef* dev_desc, uint8_t *buf, + uint16_t length) +{ + dev_desc->bLength = *(uint8_t *) (buf + 0); + dev_desc->bDescriptorType = *(uint8_t *) (buf + 1); + dev_desc->bcdUSB = LE16 (buf + 2); + dev_desc->bDeviceClass = *(uint8_t *) (buf + 4); + dev_desc->bDeviceSubClass = *(uint8_t *) (buf + 5); + dev_desc->bDeviceProtocol = *(uint8_t *) (buf + 6); + dev_desc->bMaxPacketSize = *(uint8_t *) (buf + 7); + + if (length > 8U) + { /* For 1st time after device connection, Host may issue only 8 bytes for + Device Descriptor Length */ + dev_desc->idVendor = LE16 (buf + 8); + dev_desc->idProduct = LE16 (buf + 10); + dev_desc->bcdDevice = LE16 (buf + 12); + dev_desc->iManufacturer = *(uint8_t *) (buf + 14); + dev_desc->iProduct = *(uint8_t *) (buf + 15); + dev_desc->iSerialNumber = *(uint8_t *) (buf + 16); + dev_desc->bNumConfigurations = *(uint8_t *) (buf + 17); + } +} + +/** + * @brief USBH_ParseCfgDesc + * This function Parses the configuration descriptor + * @param cfg_desc: Configuration Descriptor address + * @param buf: Buffer where the source descriptor is available + * @param length: Length of the descriptor + * @retval None + */ +static void USBH_ParseCfgDesc (USBH_CfgDescTypeDef* cfg_desc, uint8_t *buf, + uint16_t length) +{ + USBH_InterfaceDescTypeDef *pif ; + USBH_EpDescTypeDef *pep; + USBH_DescHeader_t *pdesc = (USBH_DescHeader_t *)(void *)buf; + uint16_t ptr; + uint8_t if_ix = 0U; + uint8_t ep_ix = 0U; + + pdesc = (USBH_DescHeader_t *)(void *)buf; + + /* Parse configuration descriptor */ + cfg_desc->bLength = *(uint8_t *) (buf + 0); + cfg_desc->bDescriptorType = *(uint8_t *) (buf + 1); + cfg_desc->wTotalLength = LE16 (buf + 2); + cfg_desc->bNumInterfaces = *(uint8_t *) (buf + 4); + cfg_desc->bConfigurationValue = *(uint8_t *) (buf + 5); + cfg_desc->iConfiguration = *(uint8_t *) (buf + 6); + cfg_desc->bmAttributes = *(uint8_t *) (buf + 7); + cfg_desc->bMaxPower = *(uint8_t *) (buf + 8); + + + if (length > USB_CONFIGURATION_DESC_SIZE) + { + ptr = USB_LEN_CFG_DESC; + pif = (USBH_InterfaceDescTypeDef *)0; + + + while ((if_ix < USBH_MAX_NUM_INTERFACES) && (ptr < cfg_desc->wTotalLength)) + { + pdesc = USBH_GetNextDesc((uint8_t *)(void *)pdesc, &ptr); + if (pdesc->bDescriptorType == USB_DESC_TYPE_INTERFACE) + { + pif = &cfg_desc->Itf_Desc[if_ix]; + USBH_ParseInterfaceDesc (pif, (uint8_t *)(void *)pdesc); + + ep_ix = 0U; + pep = (USBH_EpDescTypeDef *)0; + while ((ep_ix < pif->bNumEndpoints) && (ptr < cfg_desc->wTotalLength)) + { + pdesc = USBH_GetNextDesc((uint8_t*)(void *)pdesc, &ptr); + if (pdesc->bDescriptorType == USB_DESC_TYPE_ENDPOINT) + { + pep = &cfg_desc->Itf_Desc[if_ix].Ep_Desc[ep_ix]; + USBH_ParseEPDesc (pep, (uint8_t *)(void *)pdesc); + ep_ix++; + } + } + if_ix++; + } + } + } +} + + + +/** + * @brief USBH_ParseInterfaceDesc + * This function Parses the interface descriptor + * @param if_descriptor : Interface descriptor destination + * @param buf: Buffer where the descriptor data is available + * @retval None + */ +static void USBH_ParseInterfaceDesc (USBH_InterfaceDescTypeDef *if_descriptor, + uint8_t *buf) +{ + if_descriptor->bLength = *(uint8_t *) (buf + 0); + if_descriptor->bDescriptorType = *(uint8_t *) (buf + 1); + if_descriptor->bInterfaceNumber = *(uint8_t *) (buf + 2); + if_descriptor->bAlternateSetting = *(uint8_t *) (buf + 3); + if_descriptor->bNumEndpoints = *(uint8_t *) (buf + 4); + if_descriptor->bInterfaceClass = *(uint8_t *) (buf + 5); + if_descriptor->bInterfaceSubClass = *(uint8_t *) (buf + 6); + if_descriptor->bInterfaceProtocol = *(uint8_t *) (buf + 7); + if_descriptor->iInterface = *(uint8_t *) (buf + 8); +} + +/** + * @brief USBH_ParseEPDesc + * This function Parses the endpoint descriptor + * @param ep_descriptor: Endpoint descriptor destination address + * @param buf: Buffer where the parsed descriptor stored + * @retval None + */ +static void USBH_ParseEPDesc (USBH_EpDescTypeDef *ep_descriptor, + uint8_t *buf) +{ + + ep_descriptor->bLength = *(uint8_t *) (buf + 0); + ep_descriptor->bDescriptorType = *(uint8_t *) (buf + 1); + ep_descriptor->bEndpointAddress = *(uint8_t *) (buf + 2); + ep_descriptor->bmAttributes = *(uint8_t *) (buf + 3); + ep_descriptor->wMaxPacketSize = LE16 (buf + 4); + ep_descriptor->bInterval = *(uint8_t *) (buf + 6); +} + +/** + * @brief USBH_ParseStringDesc + * This function Parses the string descriptor + * @param psrc: Source pointer containing the descriptor data + * @param pdest: Destination address pointer + * @param length: Length of the descriptor + * @retval None + */ +static void USBH_ParseStringDesc (uint8_t* psrc, uint8_t* pdest, uint16_t length) +{ + uint16_t strlength; + uint16_t idx; + + /* The UNICODE string descriptor is not NULL-terminated. The string length is + computed by substracting two from the value of the first byte of the descriptor. + */ + + /* Check which is lower size, the Size of string or the length of bytes read + from the device */ + + if (psrc[1] == USB_DESC_TYPE_STRING) + { + /* Make sure the Descriptor is String Type */ + + /* psrc[0] contains Size of Descriptor, subtract 2 to get the length of string */ + strlength = ((((uint16_t)psrc[0] - 2U) <= length) ? ((uint16_t)psrc[0] - 2U) : length); + + /* Adjust the offset ignoring the String Len and Descriptor type */ + psrc += 2U; + + for (idx = 0U; idx < strlength; idx += 2U) + { + /* Copy Only the string and ignore the UNICODE ID, hence add the src */ + *pdest = psrc[idx]; + pdest++; + } + *pdest = 0U; /* mark end of string */ + } +} + +/** + * @brief USBH_GetNextDesc + * This function return the next descriptor header + * @param buf: Buffer where the cfg descriptor is available + * @param ptr: data pointer inside the cfg descriptor + * @retval next header + */ +USBH_DescHeader_t *USBH_GetNextDesc (uint8_t *pbuf, uint16_t *ptr) +{ + USBH_DescHeader_t *pnext; + + *ptr += ((USBH_DescHeader_t *)(void *)pbuf)->bLength; + pnext = (USBH_DescHeader_t *)(void *)((uint8_t *)(void *)pbuf + \ + ((USBH_DescHeader_t *)(void *)pbuf)->bLength); + + return(pnext); +} + + +/** + * @brief USBH_CtlReq + * USBH_CtlReq sends a control request and provide the status after + * completion of the request + * @param phost: Host Handle + * @param req: Setup Request Structure + * @param buff: data buffer address to store the response + * @param length: length of the response + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_CtlReq (USBH_HandleTypeDef *phost, + uint8_t *buff, + uint16_t length) +{ + USBH_StatusTypeDef status; + status = USBH_BUSY; + + switch (phost->RequestState) + { + case CMD_SEND: + /* Start a SETUP transfer */ + phost->Control.buff = buff; + phost->Control.length = length; + phost->Control.state = CTRL_SETUP; + phost->RequestState = CMD_WAIT; + status = USBH_BUSY; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + break; + + case CMD_WAIT: + status = USBH_HandleControl(phost); + if (status == USBH_OK) + { + /* Commands successfully sent and Response Received */ + phost->RequestState = CMD_SEND; + phost->Control.state =CTRL_IDLE; + status = USBH_OK; + } + else if (status == USBH_NOT_SUPPORTED) + { + /* Commands successfully sent and Response Received */ + phost->RequestState = CMD_SEND; + phost->Control.state = CTRL_IDLE; + status = USBH_NOT_SUPPORTED; + } + else + { + if (status == USBH_FAIL) + { + /* Failure Mode */ + phost->RequestState = CMD_SEND; + status = USBH_FAIL; + } + } + break; + + default: + break; + } + return status; +} + +/** + * @brief USBH_HandleControl + * Handles the USB control transfer state machine + * @param phost: Host Handle + * @retval USBH Status + */ +static USBH_StatusTypeDef USBH_HandleControl (USBH_HandleTypeDef *phost) +{ + uint8_t direction; + USBH_StatusTypeDef status = USBH_BUSY; + USBH_URBStateTypeDef URB_Status = USBH_URB_IDLE; + + switch (phost->Control.state) + { + case CTRL_SETUP: + /* send a SETUP packet */ + USBH_CtlSendSetup (phost, (uint8_t *)(void *)phost->Control.setup.d8, + phost->Control.pipe_out); + + phost->Control.state = CTRL_SETUP_WAIT; + break; + + case CTRL_SETUP_WAIT: + + URB_Status = USBH_LL_GetURBState(phost, phost->Control.pipe_out); + /* case SETUP packet sent successfully */ + if(URB_Status == USBH_URB_DONE) + { + direction = (phost->Control.setup.b.bmRequestType & USB_REQ_DIR_MASK); + + /* check if there is a data stage */ + if (phost->Control.setup.b.wLength.w != 0U) + { + if (direction == USB_D2H) + { + /* Data Direction is IN */ + phost->Control.state = CTRL_DATA_IN; + } + else + { + /* Data Direction is OUT */ + phost->Control.state = CTRL_DATA_OUT; + } + } + /* No DATA stage */ + else + { + /* If there is No Data Transfer Stage */ + if (direction == USB_D2H) + { + /* Data Direction is IN */ + phost->Control.state = CTRL_STATUS_OUT; + } + else + { + /* Data Direction is OUT */ + phost->Control.state = CTRL_STATUS_IN; + } + } + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + else + { + if((URB_Status == USBH_URB_ERROR) || (URB_Status == USBH_URB_NOTREADY)) + { + phost->Control.state = CTRL_ERROR; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + } + break; + + case CTRL_DATA_IN: + /* Issue an IN token */ + phost->Control.timer = (uint16_t)phost->Timer; + USBH_CtlReceiveData(phost, + phost->Control.buff, + phost->Control.length, + phost->Control.pipe_in); + + phost->Control.state = CTRL_DATA_IN_WAIT; + break; + + case CTRL_DATA_IN_WAIT: + + URB_Status = USBH_LL_GetURBState(phost , phost->Control.pipe_in); + + /* check is DATA packet transferred successfully */ + if (URB_Status == USBH_URB_DONE) + { + phost->Control.state = CTRL_STATUS_OUT; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + + /* manage error cases*/ + if (URB_Status == USBH_URB_STALL) + { + /* In stall case, return to previous machine state*/ + status = USBH_NOT_SUPPORTED; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + else + { + if (URB_Status == USBH_URB_ERROR) + { + /* Device error */ + phost->Control.state = CTRL_ERROR; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + } + break; + + case CTRL_DATA_OUT: + + USBH_CtlSendData (phost, + phost->Control.buff, + phost->Control.length , + phost->Control.pipe_out, + 1U); + phost->Control.timer = (uint16_t)phost->Timer; + phost->Control.state = CTRL_DATA_OUT_WAIT; + break; + + case CTRL_DATA_OUT_WAIT: + + URB_Status = USBH_LL_GetURBState(phost , phost->Control.pipe_out); + + if (URB_Status == USBH_URB_DONE) + { /* If the Setup Pkt is sent successful, then change the state */ + phost->Control.state = CTRL_STATUS_IN; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + + /* handle error cases */ + else if (URB_Status == USBH_URB_STALL) + { + /* In stall case, return to previous machine state*/ + phost->Control.state = CTRL_STALLED; + status = USBH_NOT_SUPPORTED; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + else if (URB_Status == USBH_URB_NOTREADY) + { + /* Nack received from device */ + phost->Control.state = CTRL_DATA_OUT; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + else + { + if (URB_Status == USBH_URB_ERROR) + { + /* device error */ + phost->Control.state = CTRL_ERROR; + status = USBH_FAIL; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + } + break; + + + case CTRL_STATUS_IN: + /* Send 0 bytes out packet */ + USBH_CtlReceiveData (phost, + 0U, + 0U, + phost->Control.pipe_in); + phost->Control.timer = (uint16_t)phost->Timer; + phost->Control.state = CTRL_STATUS_IN_WAIT; + + break; + + case CTRL_STATUS_IN_WAIT: + + URB_Status = USBH_LL_GetURBState(phost , phost->Control.pipe_in); + + if ( URB_Status == USBH_URB_DONE) + { /* Control transfers completed, Exit the State Machine */ + phost->Control.state = CTRL_COMPLETE; + status = USBH_OK; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + else if (URB_Status == USBH_URB_ERROR) + { + phost->Control.state = CTRL_ERROR; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + else + { + if(URB_Status == USBH_URB_STALL) + { + /* Control transfers completed, Exit the State Machine */ + status = USBH_NOT_SUPPORTED; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + } + break; + + case CTRL_STATUS_OUT: + USBH_CtlSendData (phost, + 0U, + 0U, + phost->Control.pipe_out, + 1U); + phost->Control.timer = (uint16_t)phost->Timer; + phost->Control.state = CTRL_STATUS_OUT_WAIT; + break; + + case CTRL_STATUS_OUT_WAIT: + + URB_Status = USBH_LL_GetURBState(phost , phost->Control.pipe_out); + if (URB_Status == USBH_URB_DONE) + { + status = USBH_OK; + phost->Control.state = CTRL_COMPLETE; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + else if (URB_Status == USBH_URB_NOTREADY) + { + phost->Control.state = CTRL_STATUS_OUT; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + else + { + if (URB_Status == USBH_URB_ERROR) + { + phost->Control.state = CTRL_ERROR; + +#if (USBH_USE_OS == 1U) + phost->os_msg = (uint32_t)USBH_CONTROL_EVENT; +#if (osCMSIS < 0x20000U) + (void)osMessagePut(phost->os_event, phost->os_msg, 0U); +#else + (void)osMessageQueuePut(phost->os_event, &phost->os_msg, 0U, NULL); +#endif +#endif + } + + } + break; + + case CTRL_ERROR: + /* + After a halt condition is encountered or an error is detected by the + host, a control endpoint is allowed to recover by accepting the next Setup + PID; i.e., recovery actions via some other pipe are not required for control + endpoints. For the Default Control Pipe, a device reset will ultimately be + required to clear the halt or error condition if the next Setup PID is not + accepted. + */ + if (++ phost->Control.errorcount <= USBH_MAX_ERROR_COUNT) + { + /* try to recover control */ + USBH_LL_Stop(phost); + + /* Do the transmission again, starting from SETUP Packet */ + phost->Control.state = CTRL_SETUP; + phost->RequestState = CMD_SEND; + } + else + { + phost->pUser(phost, HOST_USER_UNRECOVERED_ERROR); + phost->Control.errorcount = 0U; + USBH_ErrLog("Control error"); + status = USBH_FAIL; + } + break; + + default: + break; + } + return status; +} + +/** +* @} +*/ + +/** +* @} +*/ + +/** +* @} +*/ + +/** +* @} +*/ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + + + + diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Core/Src/usbh_ioreq.c b/system/Middlewares/ST/STM32_USB_Host_Library/Core/Src/usbh_ioreq.c index 5ea2d1d4b5..007304ac79 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Core/Src/usbh_ioreq.c +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Core/Src/usbh_ioreq.c @@ -1,351 +1,351 @@ -/** - ****************************************************************************** - * @file usbh_ioreq.c - * @author MCD Application Team - * @brief This file handles the issuing of the USB transactions - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Includes ------------------------------------------------------------------*/ - -#include "usbh_ioreq.h" - -/** @addtogroup USBH_LIB - * @{ - */ - -/** @addtogroup USBH_LIB_CORE -* @{ -*/ - -/** @defgroup USBH_IOREQ - * @brief This file handles the standard protocol processing (USB v2.0) - * @{ - */ - - -/** @defgroup USBH_IOREQ_Private_Defines - * @{ - */ - -/** - * @} - */ - - -/** @defgroup USBH_IOREQ_Private_TypesDefinitions - * @{ - */ -/** - * @} - */ - - - -/** @defgroup USBH_IOREQ_Private_Macros - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBH_IOREQ_Private_Variables - * @{ - */ -/** - * @} - */ -/** @defgroup USBH_IOREQ_Private_FunctionPrototypes - * @{ - */ - -/** - * @} - */ - - -/** @defgroup USBH_IOREQ_Private_Functions - * @{ - */ - - - -/** - * @brief USBH_CtlSendSetup - * Sends the Setup Packet to the Device - * @param phost: Host Handle - * @param buff: Buffer pointer from which the Data will be send to Device - * @param pipe_num: Pipe Number - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_CtlSendSetup (USBH_HandleTypeDef *phost, - uint8_t *buff, - uint8_t pipe_num) -{ - - USBH_LL_SubmitURB (phost, /* Driver handle */ - pipe_num, /* Pipe index */ - 0U, /* Direction : OUT */ - USBH_EP_CONTROL, /* EP type */ - USBH_PID_SETUP, /* Type setup */ - buff, /* data buffer */ - USBH_SETUP_PKT_SIZE, /* data length */ - 0U); - return USBH_OK; -} - - -/** - * @brief USBH_CtlSendData - * Sends a data Packet to the Device - * @param phost: Host Handle - * @param buff: Buffer pointer from which the Data will be sent to Device - * @param length: Length of the data to be sent - * @param pipe_num: Pipe Number - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_CtlSendData (USBH_HandleTypeDef *phost, - uint8_t *buff, - uint16_t length, - uint8_t pipe_num, - uint8_t do_ping ) -{ - if(phost->device.speed != USBH_SPEED_HIGH) - { - do_ping = 0U; - } - - USBH_LL_SubmitURB (phost, /* Driver handle */ - pipe_num, /* Pipe index */ - 0U, /* Direction : OUT */ - USBH_EP_CONTROL, /* EP type */ - USBH_PID_DATA, /* Type Data */ - buff, /* data buffer */ - length, /* data length */ - do_ping); /* do ping (HS Only)*/ - - return USBH_OK; -} - - -/** - * @brief USBH_CtlReceiveData - * Receives the Device Response to the Setup Packet - * @param phost: Host Handle - * @param buff: Buffer pointer in which the response needs to be copied - * @param length: Length of the data to be received - * @param pipe_num: Pipe Number - * @retval USBH Status. - */ -USBH_StatusTypeDef USBH_CtlReceiveData(USBH_HandleTypeDef *phost, - uint8_t* buff, - uint16_t length, - uint8_t pipe_num) -{ - USBH_LL_SubmitURB (phost, /* Driver handle */ - pipe_num, /* Pipe index */ - 1U, /* Direction : IN */ - USBH_EP_CONTROL, /* EP type */ - USBH_PID_DATA, /* Type Data */ - buff, /* data buffer */ - length, /* data length */ - 0U); - return USBH_OK; - -} - - -/** - * @brief USBH_BulkSendData - * Sends the Bulk Packet to the device - * @param phost: Host Handle - * @param buff: Buffer pointer from which the Data will be sent to Device - * @param length: Length of the data to be sent - * @param pipe_num: Pipe Number - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_BulkSendData (USBH_HandleTypeDef *phost, - uint8_t *buff, - uint16_t length, - uint8_t pipe_num, - uint8_t do_ping) -{ - if(phost->device.speed != USBH_SPEED_HIGH) - { - do_ping = 0U; - } - - USBH_LL_SubmitURB (phost, /* Driver handle */ - pipe_num, /* Pipe index */ - 0U, /* Direction : IN */ - USBH_EP_BULK, /* EP type */ - USBH_PID_DATA, /* Type Data */ - buff, /* data buffer */ - length, /* data length */ - do_ping); /* do ping (HS Only)*/ - return USBH_OK; -} - - -/** - * @brief USBH_BulkReceiveData - * Receives IN bulk packet from device - * @param phost: Host Handle - * @param buff: Buffer pointer in which the received data packet to be copied - * @param length: Length of the data to be received - * @param pipe_num: Pipe Number - * @retval USBH Status. - */ -USBH_StatusTypeDef USBH_BulkReceiveData(USBH_HandleTypeDef *phost, - uint8_t *buff, - uint16_t length, - uint8_t pipe_num) -{ - USBH_LL_SubmitURB (phost, /* Driver handle */ - pipe_num, /* Pipe index */ - 1U, /* Direction : IN */ - USBH_EP_BULK, /* EP type */ - USBH_PID_DATA, /* Type Data */ - buff, /* data buffer */ - length, /* data length */ - 0U); - return USBH_OK; -} - - -/** - * @brief USBH_InterruptReceiveData - * Receives the Device Response to the Interrupt IN token - * @param phost: Host Handle - * @param buff: Buffer pointer in which the response needs to be copied - * @param length: Length of the data to be received - * @param pipe_num: Pipe Number - * @retval USBH Status. - */ -USBH_StatusTypeDef USBH_InterruptReceiveData(USBH_HandleTypeDef *phost, - uint8_t *buff, - uint8_t length, - uint8_t pipe_num) -{ - USBH_LL_SubmitURB (phost, /* Driver handle */ - pipe_num, /* Pipe index */ - 1U, /* Direction : IN */ - USBH_EP_INTERRUPT, /* EP type */ - USBH_PID_DATA, /* Type Data */ - buff, /* data buffer */ - (uint16_t)length, /* data length */ - 0U); - - return USBH_OK; -} - -/** - * @brief USBH_InterruptSendData - * Sends the data on Interrupt OUT Endpoint - * @param phost: Host Handle - * @param buff: Buffer pointer from where the data needs to be copied - * @param length: Length of the data to be sent - * @param pipe_num: Pipe Number - * @retval USBH Status. - */ -USBH_StatusTypeDef USBH_InterruptSendData(USBH_HandleTypeDef *phost, - uint8_t *buff, - uint8_t length, - uint8_t pipe_num) -{ - USBH_LL_SubmitURB (phost, /* Driver handle */ - pipe_num, /* Pipe index */ - 0U, /* Direction : OUT */ - USBH_EP_INTERRUPT, /* EP type */ - USBH_PID_DATA, /* Type Data */ - buff, /* data buffer */ - (uint16_t)length, /* data length */ - 0U); - - return USBH_OK; -} - -/** - * @brief USBH_IsocReceiveData - * Receives the Device Response to the Isochronous IN token - * @param phost: Host Handle - * @param buff: Buffer pointer in which the response needs to be copied - * @param length: Length of the data to be received - * @param pipe_num: Pipe Number - * @retval USBH Status. - */ -USBH_StatusTypeDef USBH_IsocReceiveData(USBH_HandleTypeDef *phost, - uint8_t *buff, - uint32_t length, - uint8_t pipe_num) -{ - USBH_LL_SubmitURB (phost, /* Driver handle */ - pipe_num, /* Pipe index */ - 1U, /* Direction : IN */ - USBH_EP_ISO, /* EP type */ - USBH_PID_DATA, /* Type Data */ - buff, /* data buffer */ - (uint16_t)length, /* data length */ - 0U); - - - return USBH_OK; -} - -/** - * @brief USBH_IsocSendData - * Sends the data on Isochronous OUT Endpoint - * @param phost: Host Handle - * @param buff: Buffer pointer from where the data needs to be copied - * @param length: Length of the data to be sent - * @param pipe_num: Pipe Number - * @retval USBH Status. - */ -USBH_StatusTypeDef USBH_IsocSendData(USBH_HandleTypeDef *phost, - uint8_t *buff, - uint32_t length, - uint8_t pipe_num) -{ - USBH_LL_SubmitURB (phost, /* Driver handle */ - pipe_num, /* Pipe index */ - 0U, /* Direction : OUT */ - USBH_EP_ISO, /* EP type */ - USBH_PID_DATA, /* Type Data */ - buff, /* data buffer */ - (uint16_t)length, /* data length */ - 0U); - - return USBH_OK; -} -/** -* @} -*/ - -/** -* @} -*/ - -/** -* @} -*/ - -/** -* @} -*/ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - - - +/** + ****************************************************************************** + * @file usbh_ioreq.c + * @author MCD Application Team + * @brief This file handles the issuing of the USB transactions + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ + +#include "usbh_ioreq.h" + +/** @addtogroup USBH_LIB + * @{ + */ + +/** @addtogroup USBH_LIB_CORE +* @{ +*/ + +/** @defgroup USBH_IOREQ + * @brief This file handles the standard protocol processing (USB v2.0) + * @{ + */ + + +/** @defgroup USBH_IOREQ_Private_Defines + * @{ + */ + +/** + * @} + */ + + +/** @defgroup USBH_IOREQ_Private_TypesDefinitions + * @{ + */ +/** + * @} + */ + + + +/** @defgroup USBH_IOREQ_Private_Macros + * @{ + */ +/** + * @} + */ + + +/** @defgroup USBH_IOREQ_Private_Variables + * @{ + */ +/** + * @} + */ +/** @defgroup USBH_IOREQ_Private_FunctionPrototypes + * @{ + */ + +/** + * @} + */ + + +/** @defgroup USBH_IOREQ_Private_Functions + * @{ + */ + + + +/** + * @brief USBH_CtlSendSetup + * Sends the Setup Packet to the Device + * @param phost: Host Handle + * @param buff: Buffer pointer from which the Data will be send to Device + * @param pipe_num: Pipe Number + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_CtlSendSetup (USBH_HandleTypeDef *phost, + uint8_t *buff, + uint8_t pipe_num) +{ + + USBH_LL_SubmitURB (phost, /* Driver handle */ + pipe_num, /* Pipe index */ + 0U, /* Direction : OUT */ + USBH_EP_CONTROL, /* EP type */ + USBH_PID_SETUP, /* Type setup */ + buff, /* data buffer */ + USBH_SETUP_PKT_SIZE, /* data length */ + 0U); + return USBH_OK; +} + + +/** + * @brief USBH_CtlSendData + * Sends a data Packet to the Device + * @param phost: Host Handle + * @param buff: Buffer pointer from which the Data will be sent to Device + * @param length: Length of the data to be sent + * @param pipe_num: Pipe Number + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_CtlSendData (USBH_HandleTypeDef *phost, + uint8_t *buff, + uint16_t length, + uint8_t pipe_num, + uint8_t do_ping ) +{ + if(phost->device.speed != USBH_SPEED_HIGH) + { + do_ping = 0U; + } + + USBH_LL_SubmitURB (phost, /* Driver handle */ + pipe_num, /* Pipe index */ + 0U, /* Direction : OUT */ + USBH_EP_CONTROL, /* EP type */ + USBH_PID_DATA, /* Type Data */ + buff, /* data buffer */ + length, /* data length */ + do_ping); /* do ping (HS Only)*/ + + return USBH_OK; +} + + +/** + * @brief USBH_CtlReceiveData + * Receives the Device Response to the Setup Packet + * @param phost: Host Handle + * @param buff: Buffer pointer in which the response needs to be copied + * @param length: Length of the data to be received + * @param pipe_num: Pipe Number + * @retval USBH Status. + */ +USBH_StatusTypeDef USBH_CtlReceiveData(USBH_HandleTypeDef *phost, + uint8_t* buff, + uint16_t length, + uint8_t pipe_num) +{ + USBH_LL_SubmitURB (phost, /* Driver handle */ + pipe_num, /* Pipe index */ + 1U, /* Direction : IN */ + USBH_EP_CONTROL, /* EP type */ + USBH_PID_DATA, /* Type Data */ + buff, /* data buffer */ + length, /* data length */ + 0U); + return USBH_OK; + +} + + +/** + * @brief USBH_BulkSendData + * Sends the Bulk Packet to the device + * @param phost: Host Handle + * @param buff: Buffer pointer from which the Data will be sent to Device + * @param length: Length of the data to be sent + * @param pipe_num: Pipe Number + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_BulkSendData (USBH_HandleTypeDef *phost, + uint8_t *buff, + uint16_t length, + uint8_t pipe_num, + uint8_t do_ping) +{ + if(phost->device.speed != USBH_SPEED_HIGH) + { + do_ping = 0U; + } + + USBH_LL_SubmitURB (phost, /* Driver handle */ + pipe_num, /* Pipe index */ + 0U, /* Direction : IN */ + USBH_EP_BULK, /* EP type */ + USBH_PID_DATA, /* Type Data */ + buff, /* data buffer */ + length, /* data length */ + do_ping); /* do ping (HS Only)*/ + return USBH_OK; +} + + +/** + * @brief USBH_BulkReceiveData + * Receives IN bulk packet from device + * @param phost: Host Handle + * @param buff: Buffer pointer in which the received data packet to be copied + * @param length: Length of the data to be received + * @param pipe_num: Pipe Number + * @retval USBH Status. + */ +USBH_StatusTypeDef USBH_BulkReceiveData(USBH_HandleTypeDef *phost, + uint8_t *buff, + uint16_t length, + uint8_t pipe_num) +{ + USBH_LL_SubmitURB (phost, /* Driver handle */ + pipe_num, /* Pipe index */ + 1U, /* Direction : IN */ + USBH_EP_BULK, /* EP type */ + USBH_PID_DATA, /* Type Data */ + buff, /* data buffer */ + length, /* data length */ + 0U); + return USBH_OK; +} + + +/** + * @brief USBH_InterruptReceiveData + * Receives the Device Response to the Interrupt IN token + * @param phost: Host Handle + * @param buff: Buffer pointer in which the response needs to be copied + * @param length: Length of the data to be received + * @param pipe_num: Pipe Number + * @retval USBH Status. + */ +USBH_StatusTypeDef USBH_InterruptReceiveData(USBH_HandleTypeDef *phost, + uint8_t *buff, + uint8_t length, + uint8_t pipe_num) +{ + USBH_LL_SubmitURB (phost, /* Driver handle */ + pipe_num, /* Pipe index */ + 1U, /* Direction : IN */ + USBH_EP_INTERRUPT, /* EP type */ + USBH_PID_DATA, /* Type Data */ + buff, /* data buffer */ + (uint16_t)length, /* data length */ + 0U); + + return USBH_OK; +} + +/** + * @brief USBH_InterruptSendData + * Sends the data on Interrupt OUT Endpoint + * @param phost: Host Handle + * @param buff: Buffer pointer from where the data needs to be copied + * @param length: Length of the data to be sent + * @param pipe_num: Pipe Number + * @retval USBH Status. + */ +USBH_StatusTypeDef USBH_InterruptSendData(USBH_HandleTypeDef *phost, + uint8_t *buff, + uint8_t length, + uint8_t pipe_num) +{ + USBH_LL_SubmitURB (phost, /* Driver handle */ + pipe_num, /* Pipe index */ + 0U, /* Direction : OUT */ + USBH_EP_INTERRUPT, /* EP type */ + USBH_PID_DATA, /* Type Data */ + buff, /* data buffer */ + (uint16_t)length, /* data length */ + 0U); + + return USBH_OK; +} + +/** + * @brief USBH_IsocReceiveData + * Receives the Device Response to the Isochronous IN token + * @param phost: Host Handle + * @param buff: Buffer pointer in which the response needs to be copied + * @param length: Length of the data to be received + * @param pipe_num: Pipe Number + * @retval USBH Status. + */ +USBH_StatusTypeDef USBH_IsocReceiveData(USBH_HandleTypeDef *phost, + uint8_t *buff, + uint32_t length, + uint8_t pipe_num) +{ + USBH_LL_SubmitURB (phost, /* Driver handle */ + pipe_num, /* Pipe index */ + 1U, /* Direction : IN */ + USBH_EP_ISO, /* EP type */ + USBH_PID_DATA, /* Type Data */ + buff, /* data buffer */ + (uint16_t)length, /* data length */ + 0U); + + + return USBH_OK; +} + +/** + * @brief USBH_IsocSendData + * Sends the data on Isochronous OUT Endpoint + * @param phost: Host Handle + * @param buff: Buffer pointer from where the data needs to be copied + * @param length: Length of the data to be sent + * @param pipe_num: Pipe Number + * @retval USBH Status. + */ +USBH_StatusTypeDef USBH_IsocSendData(USBH_HandleTypeDef *phost, + uint8_t *buff, + uint32_t length, + uint8_t pipe_num) +{ + USBH_LL_SubmitURB (phost, /* Driver handle */ + pipe_num, /* Pipe index */ + 0U, /* Direction : OUT */ + USBH_EP_ISO, /* EP type */ + USBH_PID_DATA, /* Type Data */ + buff, /* data buffer */ + (uint16_t)length, /* data length */ + 0U); + + return USBH_OK; +} +/** +* @} +*/ + +/** +* @} +*/ + +/** +* @} +*/ + +/** +* @} +*/ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + + + diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Core/Src/usbh_pipes.c b/system/Middlewares/ST/STM32_USB_Host_Library/Core/Src/usbh_pipes.c index 7dd5110cbe..af8472aede 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Core/Src/usbh_pipes.c +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Core/Src/usbh_pipes.c @@ -1,196 +1,196 @@ -/** - ****************************************************************************** - * @file usbh_pipes.c - * @author MCD Application Team - * @brief This file implements functions for opening and closing Pipes - ****************************************************************************** - * @attention - * - *

© Copyright (c) 2015 STMicroelectronics. - * All rights reserved.

- * - * This software component is licensed by ST under Ultimate Liberty license - * SLA0044, the "License"; You may not use this file except in compliance with - * the License. You may obtain a copy of the License at: - * http://www.st.com/SLA0044 - * - ****************************************************************************** - */ - -/* Includes ------------------------------------------------------------------*/ -#include "usbh_pipes.h" - -/** @addtogroup USBH_LIB - * @{ - */ - -/** @addtogroup USBH_LIB_CORE -* @{ -*/ - -/** @defgroup USBH_PIPES - * @brief This file includes opening and closing Pipes - * @{ - */ - -/** @defgroup USBH_PIPES_Private_Defines - * @{ - */ -/** - * @} - */ - -/** @defgroup USBH_PIPES_Private_TypesDefinitions - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBH_PIPES_Private_Macros - * @{ - */ -/** - * @} - */ - - -/** @defgroup USBH_PIPES_Private_Variables - * @{ - */ - -/** - * @} - */ - - -/** @defgroup USBH_PIPES_Private_Functions - * @{ - */ -static uint16_t USBH_GetFreePipe (USBH_HandleTypeDef *phost); - - -/** - * @brief USBH_Open_Pipe - * Open a pipe - * @param phost: Host Handle - * @param pipe_num: Pipe Number - * @param dev_address: USB Device address allocated to attached device - * @param speed : USB device speed (Full/Low) - * @param ep_type: end point type (Bulk/int/ctl) - * @param mps: max pkt size - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_OpenPipe (USBH_HandleTypeDef *phost, - uint8_t pipe_num, - uint8_t epnum, - uint8_t dev_address, - uint8_t speed, - uint8_t ep_type, - uint16_t mps) -{ - - USBH_LL_OpenPipe(phost, - pipe_num, - epnum, - dev_address, - speed, - ep_type, - mps); - - return USBH_OK; - -} - -/** - * @brief USBH_ClosePipe - * Close a pipe - * @param phost: Host Handle - * @param pipe_num: Pipe Number - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_ClosePipe (USBH_HandleTypeDef *phost, - uint8_t pipe_num) -{ - - USBH_LL_ClosePipe(phost, pipe_num); - - return USBH_OK; - -} - -/** - * @brief USBH_Alloc_Pipe - * Allocate a new Pipe - * @param phost: Host Handle - * @param ep_addr: End point for which the Pipe to be allocated - * @retval Pipe number - */ -uint8_t USBH_AllocPipe (USBH_HandleTypeDef *phost, uint8_t ep_addr) -{ - uint16_t pipe; - - pipe = USBH_GetFreePipe(phost); - - if (pipe != 0xFFFFU) - { - phost->Pipes[pipe] = 0x8000U | ep_addr; - } - return (uint8_t)pipe; -} - -/** - * @brief USBH_Free_Pipe - * Free the USB Pipe - * @param phost: Host Handle - * @param idx: Pipe number to be freed - * @retval USBH Status - */ -USBH_StatusTypeDef USBH_FreePipe (USBH_HandleTypeDef *phost, uint8_t idx) -{ - if(idx < 11U) - { - phost->Pipes[idx] &= 0x7FFFU; - } - return USBH_OK; -} - -/** - * @brief USBH_GetFreePipe - * @param phost: Host Handle - * Get a free Pipe number for allocation to a device endpoint - * @retval idx: Free Pipe number - */ -static uint16_t USBH_GetFreePipe (USBH_HandleTypeDef *phost) -{ - uint8_t idx = 0U; - - for (idx = 0U ; idx < 11U ; idx++) - { - if ((phost->Pipes[idx] & 0x8000U) == 0U) - { - return (uint16_t)idx; - } - } - return 0xFFFFU; -} -/** -* @} -*/ - -/** -* @} -*/ - -/** -* @} -*/ - -/** -* @} -*/ - -/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ - - +/** + ****************************************************************************** + * @file usbh_pipes.c + * @author MCD Application Team + * @brief This file implements functions for opening and closing Pipes + ****************************************************************************** + * @attention + * + *

© Copyright (c) 2015 STMicroelectronics. + * All rights reserved.

+ * + * This software component is licensed by ST under Ultimate Liberty license + * SLA0044, the "License"; You may not use this file except in compliance with + * the License. You may obtain a copy of the License at: + * http://www.st.com/SLA0044 + * + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#include "usbh_pipes.h" + +/** @addtogroup USBH_LIB + * @{ + */ + +/** @addtogroup USBH_LIB_CORE +* @{ +*/ + +/** @defgroup USBH_PIPES + * @brief This file includes opening and closing Pipes + * @{ + */ + +/** @defgroup USBH_PIPES_Private_Defines + * @{ + */ +/** + * @} + */ + +/** @defgroup USBH_PIPES_Private_TypesDefinitions + * @{ + */ +/** + * @} + */ + + +/** @defgroup USBH_PIPES_Private_Macros + * @{ + */ +/** + * @} + */ + + +/** @defgroup USBH_PIPES_Private_Variables + * @{ + */ + +/** + * @} + */ + + +/** @defgroup USBH_PIPES_Private_Functions + * @{ + */ +static uint16_t USBH_GetFreePipe (USBH_HandleTypeDef *phost); + + +/** + * @brief USBH_Open_Pipe + * Open a pipe + * @param phost: Host Handle + * @param pipe_num: Pipe Number + * @param dev_address: USB Device address allocated to attached device + * @param speed : USB device speed (Full/Low) + * @param ep_type: end point type (Bulk/int/ctl) + * @param mps: max pkt size + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_OpenPipe (USBH_HandleTypeDef *phost, + uint8_t pipe_num, + uint8_t epnum, + uint8_t dev_address, + uint8_t speed, + uint8_t ep_type, + uint16_t mps) +{ + + USBH_LL_OpenPipe(phost, + pipe_num, + epnum, + dev_address, + speed, + ep_type, + mps); + + return USBH_OK; + +} + +/** + * @brief USBH_ClosePipe + * Close a pipe + * @param phost: Host Handle + * @param pipe_num: Pipe Number + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_ClosePipe (USBH_HandleTypeDef *phost, + uint8_t pipe_num) +{ + + USBH_LL_ClosePipe(phost, pipe_num); + + return USBH_OK; + +} + +/** + * @brief USBH_Alloc_Pipe + * Allocate a new Pipe + * @param phost: Host Handle + * @param ep_addr: End point for which the Pipe to be allocated + * @retval Pipe number + */ +uint8_t USBH_AllocPipe (USBH_HandleTypeDef *phost, uint8_t ep_addr) +{ + uint16_t pipe; + + pipe = USBH_GetFreePipe(phost); + + if (pipe != 0xFFFFU) + { + phost->Pipes[pipe] = 0x8000U | ep_addr; + } + return (uint8_t)pipe; +} + +/** + * @brief USBH_Free_Pipe + * Free the USB Pipe + * @param phost: Host Handle + * @param idx: Pipe number to be freed + * @retval USBH Status + */ +USBH_StatusTypeDef USBH_FreePipe (USBH_HandleTypeDef *phost, uint8_t idx) +{ + if(idx < 11U) + { + phost->Pipes[idx] &= 0x7FFFU; + } + return USBH_OK; +} + +/** + * @brief USBH_GetFreePipe + * @param phost: Host Handle + * Get a free Pipe number for allocation to a device endpoint + * @retval idx: Free Pipe number + */ +static uint16_t USBH_GetFreePipe (USBH_HandleTypeDef *phost) +{ + uint8_t idx = 0U; + + for (idx = 0U ; idx < 11U ; idx++) + { + if ((phost->Pipes[idx] & 0x8000U) == 0U) + { + return (uint16_t)idx; + } + } + return 0xFFFFU; +} +/** +* @} +*/ + +/** +* @} +*/ + +/** +* @} +*/ + +/** +* @} +*/ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ + + diff --git a/system/Middlewares/ST/STM32_USB_Host_Library/Release_Notes.html b/system/Middlewares/ST/STM32_USB_Host_Library/Release_Notes.html index 08e37ad0a7..094c7ded03 100644 --- a/system/Middlewares/ST/STM32_USB_Host_Library/Release_Notes.html +++ b/system/Middlewares/ST/STM32_USB_Host_Library/Release_Notes.html @@ -1,1194 +1,1194 @@ - - - - - - - - - - - - - -Release Notes for STM32 USB Host Library - - - - - - - - - -
- -

 

- -
- - - - - -
- - - - - - - -
-

Back to Release page

-
-

Release Notes for STM32 USB Host Library

-

Copyright - 2015 STMicroelectronics

-

-
-

 

- - - - -
-

Update History

V3.3.1 / 09-July-2018 -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Main Changes

  • Fix interoperability issue with HP mouse
  • Add compatibility with FreeRTOS CMSIS V2 API changes
  • Update License section and add link to get copy of ST Ultimate Liberty license SLA0044

V3.3.0 / 23-january-2018 -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Main Changes

  • Update license section to Ultimate Liberty
  • Update some functions to be MISRA-C 2004 compliant
  • Improve USB Core enumeration state machine
  • Fix Device fast plug/unplug issue
  • Improve interoperability with non compliant USB devices
  • Add support of Host set remote wakeup enable feature
  • Fix USB Host MSC  set correct sector size
  • MSC: Set correct LUN number if returned LUN number exceeds max supported value

V3.2.2 / 07-July-2015
-

- - - - - - - - - - - - - - - -

Main -Changes

- - - - - - - - - - - - - - - - - - - -
  • MSC Class
    • usbh_msc.c
      • Fix MSC Get Ready Delay issue

V3.2.1 / 26-June-2015
-

- - - - - - - - - - - - - - - -

Main -Changes

- - - - - - - - - - - - - - - - - - - -
  • Core driver
    • usbh_def.h: -update USBH_MAX_PIPES_NBR literal definition to be conditioned by -#ifndef directive, to allow application code to change its value (i.e. -in the compiler preprocessor)
      -
  • MSC Class
    • usbh_msc.c
      • Update USBH_MSC_GetMaxLUN() to  return the correct number of supported LUNs (was returning 0xFF)
      • Fix timeout calculation issue
    • usbh_msc.h: -update MAX_SUPPORTED_LUN literal definition to be conditioned by -#ifndef directive, to allow application code to change its value -(i.e.in the compiler preprocessor)
  • HID Class
    • usbh_hid.h: fix HID's handle “timer” type to uint32_t instead of uint16_t
  • MTP Class
    • usbh_mtp.c : Fix timeout calculation issue
- -

V3.2.0 / 04-November-2014
-

- - - - - - - - - - - - - -

Main -Changes

- - - - - - - - - - - - - - - - - -
  • Update all drivers to be C++ compliant
    -
  • Core driver
    • usbh_core.c: remove HOST_IDLE state in USBH_LL_Connect() function
      -
  • MSC class
    • Update to manage correctly older USB Keys that do not support GetMaxLun request
  • Miscellaneous source code comments update
- -

V3.1.0 / 19-June-2014

- - - - - -

Main -Changes

- - - - - - - - - -
  • Core driver
    • Add a new define USBH_PROCESS_STACK_SIZE in the usbh_conf.h -file to change the default internal USB host process stack. Note that -by omitting this define, the default stack size (2KB) is used
      -
    • Add a user callback to handle unrecoverable error case in the application
    • Remove the wrong check on the interface descriptor index in the USBH_FindInterface() function
      -
  • All classes
    • Update class description in files comment by adding reference to the used USB class specification revision
  • Audio, CDC and MTP classes
    -
    • Add full RTOS support by handling state transitions through OS messages
      -
  • HID class
    • Add new API USBH_HID_GetPollInterval() to allow user to retrieve the needed poll time (interval between two USBD_HID_SendReport())
  • Audio class
    • Add a new weak callback USBH_AUDIO_BufferEmptyCallback() to indicate the end of audio data processing on the user buffer
  • MSC class
    • Return mass storage device capacity in Bytes in the user log message instead of MBytes
- - - -

V3.0.0 / 18-February-2014

- - - -

Main -Changes

- - - - - - - -
  • Major update -based on STM32Cube specification: Library Core, Classes architecture and APIs -modified vs. V2.1.0, and thus the 2 versions are not compatible.
    -
  • This version has to be used only with STM32Cube based development
-

V2.1.0 / 19-March-2012

-

Main -Changes

- -
  • Official support of STM32F4xx devices
  • All source files: license disclaimer text update and add link to the License file on ST Internet
  • Add ISR structure to link the low level driver to the Host library
  • Change length parameter in the I/O operations to handle large amount of data
  • Enhance the configuration descriptor parsing method to take into account multi interface devices
  • HID class
    • Remove blocking even frame synchronization loop
  • MSC class
    • Handle correctly the BOT transfer with length < max length
    • Handle multi sector length data in the FAT FS interface
  • Miscellaneous bug fix

V2.0.0 / 22-July-2011

Main -Changes

-
  • Second official version supporting STM32F105/7 and STM32F2xx devices
  • Add support for STM32F2xx devices
  • Add multi interface feature
  • Add dynamic configuration parsing
  • Add -USBH_DeAllocate_AllChannel function in the Host channel management -layer to clean up channels allocation table when de-initializing the -library
  • Change the core layer to stop correctly the host core and free all allocated channels
  • Add usbh_conf.h file in the application layer to customize some user parameters

V1.0.0 - 11/29/2010

-
  • Created 

License

This -software component is licensed by ST under Ultimate Liberty license -SLA0044, the "License"; You may not use this component except in -compliance with the License. You may obtain a copy of the License at:

http://www.st.com/SLA0044

-
-
-
-

For - complete documentation on STM32 - Microcontrollers visit www.st.com/STM32

-
-

-
- -
- -

 

- -
- \ No newline at end of file + + + + + + + + + + + + + +Release Notes for STM32 USB Host Library + + + + + + + + + +
+ +

 

+ +
+ + + + + +
+ + + + + + + +
+

Back to Release page

+
+

Release Notes for STM32 USB Host Library

+

Copyright + 2015 STMicroelectronics

+

+
+

 

+ + + + +
+

Update History

V3.3.1 / 09-July-2018 +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Main Changes

  • Fix interoperability issue with HP mouse
  • Add compatibility with FreeRTOS CMSIS V2 API changes
  • Update License section and add link to get copy of ST Ultimate Liberty license SLA0044

V3.3.0 / 23-january-2018 +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

Main Changes

  • Update license section to Ultimate Liberty
  • Update some functions to be MISRA-C 2004 compliant
  • Improve USB Core enumeration state machine
  • Fix Device fast plug/unplug issue
  • Improve interoperability with non compliant USB devices
  • Add support of Host set remote wakeup enable feature
  • Fix USB Host MSC  set correct sector size
  • MSC: Set correct LUN number if returned LUN number exceeds max supported value

V3.2.2 / 07-July-2015
+

+ + + + + + + + + + + + + + + +

Main +Changes

+ + + + + + + + + + + + + + + + + + + +
  • MSC Class
    • usbh_msc.c
      • Fix MSC Get Ready Delay issue

V3.2.1 / 26-June-2015
+

+ + + + + + + + + + + + + + + +

Main +Changes

+ + + + + + + + + + + + + + + + + + + +
  • Core driver
    • usbh_def.h: +update USBH_MAX_PIPES_NBR literal definition to be conditioned by +#ifndef directive, to allow application code to change its value (i.e. +in the compiler preprocessor)
      +
  • MSC Class
    • usbh_msc.c
      • Update USBH_MSC_GetMaxLUN() to  return the correct number of supported LUNs (was returning 0xFF)
      • Fix timeout calculation issue
    • usbh_msc.h: +update MAX_SUPPORTED_LUN literal definition to be conditioned by +#ifndef directive, to allow application code to change its value +(i.e.in the compiler preprocessor)
  • HID Class
    • usbh_hid.h: fix HID's handle “timer” type to uint32_t instead of uint16_t
  • MTP Class
    • usbh_mtp.c : Fix timeout calculation issue
+ +

V3.2.0 / 04-November-2014
+

+ + + + + + + + + + + + + +

Main +Changes

+ + + + + + + + + + + + + + + + + +
  • Update all drivers to be C++ compliant
    +
  • Core driver
    • usbh_core.c: remove HOST_IDLE state in USBH_LL_Connect() function
      +
  • MSC class
    • Update to manage correctly older USB Keys that do not support GetMaxLun request
  • Miscellaneous source code comments update
+ +

V3.1.0 / 19-June-2014

+ + + + + +

Main +Changes

+ + + + + + + + + +
  • Core driver
    • Add a new define USBH_PROCESS_STACK_SIZE in the usbh_conf.h +file to change the default internal USB host process stack. Note that +by omitting this define, the default stack size (2KB) is used
      +
    • Add a user callback to handle unrecoverable error case in the application
    • Remove the wrong check on the interface descriptor index in the USBH_FindInterface() function
      +
  • All classes
    • Update class description in files comment by adding reference to the used USB class specification revision
  • Audio, CDC and MTP classes
    +
    • Add full RTOS support by handling state transitions through OS messages
      +
  • HID class
    • Add new API USBH_HID_GetPollInterval() to allow user to retrieve the needed poll time (interval between two USBD_HID_SendReport())
  • Audio class
    • Add a new weak callback USBH_AUDIO_BufferEmptyCallback() to indicate the end of audio data processing on the user buffer
  • MSC class
    • Return mass storage device capacity in Bytes in the user log message instead of MBytes
+ + + +

V3.0.0 / 18-February-2014

+ + + +

Main +Changes

+ + + + + + + +
  • Major update +based on STM32Cube specification: Library Core, Classes architecture and APIs +modified vs. V2.1.0, and thus the 2 versions are not compatible.
    +
  • This version has to be used only with STM32Cube based development
+

V2.1.0 / 19-March-2012

+

Main +Changes

+ +
  • Official support of STM32F4xx devices
  • All source files: license disclaimer text update and add link to the License file on ST Internet
  • Add ISR structure to link the low level driver to the Host library
  • Change length parameter in the I/O operations to handle large amount of data
  • Enhance the configuration descriptor parsing method to take into account multi interface devices
  • HID class
    • Remove blocking even frame synchronization loop
  • MSC class
    • Handle correctly the BOT transfer with length < max length
    • Handle multi sector length data in the FAT FS interface
  • Miscellaneous bug fix

V2.0.0 / 22-July-2011

Main +Changes

+
  • Second official version supporting STM32F105/7 and STM32F2xx devices
  • Add support for STM32F2xx devices
  • Add multi interface feature
  • Add dynamic configuration parsing
  • Add +USBH_DeAllocate_AllChannel function in the Host channel management +layer to clean up channels allocation table when de-initializing the +library
  • Change the core layer to stop correctly the host core and free all allocated channels
  • Add usbh_conf.h file in the application layer to customize some user parameters

V1.0.0 - 11/29/2010

+
  • Created 

License

This +software component is licensed by ST under Ultimate Liberty license +SLA0044, the "License"; You may not use this component except in +compliance with the License. You may obtain a copy of the License at:

http://www.st.com/SLA0044

+
+
+
+

For + complete documentation on STM32 + Microcontrollers visit www.st.com/STM32

+
+

+
+ +
+ +

 

+ +
+