添加 USB 主机、BLE 和 Socket 数据端口驱动程序:
- 支持大容量存储类(MSC)的 USB 主机驱动程序 - BLE SPP(串行端口配置文件)客户端和服务器实现 - Socket(UDP/TCP)数据端口驱动程序 - 相关的 shell 接口用于配置和测试
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
idf_component_register(SRCS "cdc_acm_host.c"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES usb)
|
||||
46
app/drivers/data_port/usb-host/cdc/cdc_acm_host/README.md
Normal file
46
app/drivers/data_port/usb-host/cdc/cdc_acm_host/README.md
Normal file
@@ -0,0 +1,46 @@
|
||||
# USB Host CDC-ACM Class Driver
|
||||
|
||||
This directory contains an implementation of a USB CDC-ACM Host Class Driver that is implemented on top of the [USB Host Library](https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/api-reference/peripherals/usb_host.html).
|
||||
|
||||
## Supported Devices
|
||||
|
||||
The CDC-ACM Host driver supports the following types of CDC devices:
|
||||
|
||||
1. CDC-ACM devices
|
||||
2. CDC-like vendor specific devices (usually found on USB to UART bridge devices)
|
||||
|
||||
### CDC-ACM Devices
|
||||
|
||||
The CDC-ACM Class driver supports CDC-ACM devices that meet the following requirements:
|
||||
- The device class code must be set to the CDC class `0x02` or implement Interface Association Descriptor (IAD)
|
||||
- The CDC-ACM must contain the following interfaces:
|
||||
- A Communication Class Interface containing a management element (EP0) and may also contain a notification element (an interrupt endpoint). The driver will check this interface for CDC Functional Descriptors.
|
||||
- A Data Class Interface with two BULK endpoints (IN and OUT). Other transfer types are not supported by the driver
|
||||
|
||||
### CDC-Like Vendor Specific Devices
|
||||
|
||||
The CDC-ACM Class driver supports CDC-like devices that meet the following requirements:
|
||||
- The device class code must be set to the vendor specific class code `0xFF`
|
||||
- The device needs to provide and interface containing the following endpoints:
|
||||
- (Mandatory) Two Bulk endpoints (IN and OUT) for data
|
||||
- (Optional) An interrupt endpoint (IN) for the notification element
|
||||
|
||||
For CDC-like devices, users are responsible for ensuring that they only call APIs (e.g., `cdc_acm_host_send_break()`) that are supported by the target device.
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
The following steps outline the typical API call pattern of the CDC-ACM Class Driver
|
||||
|
||||
1. Install the USB Host Library via `usb_host_install()`
|
||||
2. Install the CDC-ACM driver via `cdc_acm_host_install()`
|
||||
3. Call `cdc_acm_host_open()`/`cdc_acm_host_open_vendor_specific()` to open a target CDC-ACM/CDC-like device. These functions will block until the target device is connected
|
||||
4. To transmit data, call `cdc_acm_host_data_tx_blocking()`
|
||||
5. When data is received, the driver will automatically run the receive data callback
|
||||
6. An opened device can be closed via `cdc_acm_host_close()`
|
||||
7. The CDC-ACM driver can be uninstalled via `cdc_acm_host_uninstall()`
|
||||
|
||||
## Examples
|
||||
|
||||
- For an example with a CDC-ACM device, refer to [cdc_acm_host](../../cdc_acm_host)
|
||||
- For an example with a CDC-like device, refer to [cdc_acm_host_bg96](../../cdc_acm_bg96)
|
||||
1180
app/drivers/data_port/usb-host/cdc/cdc_acm_host/cdc_acm_host.c
Normal file
1180
app/drivers/data_port/usb-host/cdc/cdc_acm_host/cdc_acm_host.c
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,306 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "usb_types_cdc.h"
|
||||
#include "esp_err.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct cdc_dev_s *cdc_acm_dev_hdl_t;
|
||||
|
||||
/**
|
||||
* @brief Line Coding structure
|
||||
* @see Table 17, USB CDC-PSTN specification rev. 1.2
|
||||
*/
|
||||
typedef struct {
|
||||
uint32_t dwDTERate; // in bits per second
|
||||
uint8_t bCharFormat; // 0: 1 stopbit, 1: 1.5 stopbits, 2: 2 stopbits
|
||||
uint8_t bParityType; // 0: None, 1: Odd, 2: Even, 3: Mark, 4: Space
|
||||
uint8_t bDataBits; // 5, 6, 7, 8 or 16
|
||||
} __attribute__((packed)) cdc_acm_line_coding_t;
|
||||
|
||||
/**
|
||||
* @brief UART State Bitmap
|
||||
* @see Table 31, USB CDC-PSTN specification rev. 1.2
|
||||
*/
|
||||
typedef union {
|
||||
struct {
|
||||
uint16_t bRxCarrier : 1; // State of receiver carrier detection mechanism of device. This signal corresponds to V.24 signal 109 and RS-232 signal DCD.
|
||||
uint16_t bTxCarrier : 1; // State of transmission carrier. This signal corresponds to V.24 signal 106 and RS-232 signal DSR.
|
||||
uint16_t bBreak : 1; // State of break detection mechanism of the device.
|
||||
uint16_t bRingSignal : 1; // State of ring signal detection of the device.
|
||||
uint16_t bFraming : 1; // A framing error has occurred.
|
||||
uint16_t bParity : 1; // A parity error has occurred.
|
||||
uint16_t bOverRun : 1; // Received data has been discarded due to overrun in the device.
|
||||
uint16_t reserved : 9;
|
||||
};
|
||||
uint16_t val;
|
||||
} cdc_acm_uart_state_t;
|
||||
|
||||
/**
|
||||
* @brief CDC-ACM Device Event types to upper layer
|
||||
*
|
||||
*/
|
||||
typedef enum {
|
||||
CDC_ACM_HOST_ERROR,
|
||||
CDC_ACM_HOST_SERIAL_STATE,
|
||||
CDC_ACM_HOST_NETWORK_CONNECTION,
|
||||
CDC_ACM_HOST_DEVICE_DISCONNECTED
|
||||
} cdc_acm_host_dev_event_t;
|
||||
|
||||
/**
|
||||
* @brief CDC-ACM Device Event data structure
|
||||
*
|
||||
*/
|
||||
typedef struct {
|
||||
cdc_acm_host_dev_event_t type;
|
||||
union {
|
||||
int error; // Error code from USB Host
|
||||
cdc_acm_uart_state_t serial_state; // Serial (UART) state
|
||||
bool network_connected; // Network connection event
|
||||
} data;
|
||||
} cdc_acm_host_dev_event_data_t;
|
||||
|
||||
/**
|
||||
* @brief Data receive callback type
|
||||
*/
|
||||
typedef void (*cdc_acm_data_callback_t)(uint8_t *data, size_t data_len, void *user_arg);
|
||||
|
||||
/**
|
||||
* @brief Device event callback type
|
||||
* @see cdc_acm_host_dev_event_t
|
||||
*/
|
||||
typedef void (*cdc_acm_host_dev_callback_t)(cdc_acm_dev_hdl_t cdc_hdl, const cdc_acm_host_dev_event_data_t *event, void *user_ctx);
|
||||
|
||||
/**
|
||||
* @brief Configuration structure of USB Host CDC-ACM driver
|
||||
*
|
||||
*/
|
||||
typedef struct {
|
||||
size_t driver_task_stack_size; /**< Stack size of the driver's task */
|
||||
unsigned driver_task_priority; /**< Priority of the driver's task */
|
||||
int xCoreID; /**< Core affinity of the driver's task */
|
||||
} cdc_acm_host_driver_config_t;
|
||||
|
||||
/**
|
||||
* @brief Configuration structure of CDC-ACM device
|
||||
*
|
||||
*/
|
||||
typedef struct {
|
||||
uint32_t connection_timeout_ms; /**< Timeout for USB device connection in [ms] */
|
||||
size_t out_buffer_size; /**< Maximum size of USB bulk out transfer, set to 0 for read-only devices */
|
||||
cdc_acm_host_dev_callback_t event_cb; /**< Device's event callback function. Can be NULL */
|
||||
cdc_acm_data_callback_t data_cb; /**< Device's data RX callback function. Can be NULL for write-only devices */
|
||||
void *user_arg; /**< User's argument that will be passed to the callbacks */
|
||||
} cdc_acm_host_device_config_t;
|
||||
|
||||
/**
|
||||
* @brief Install CDC-ACM driver
|
||||
*
|
||||
* - USB Host Library must already be installed before calling this function (via usb_host_install())
|
||||
* - This function should be called before calling any other CDC driver functions
|
||||
*
|
||||
* @param[in] driver_config Driver configuration structure. If set to NULL, a default configuration will be used.
|
||||
* @return esp_err_t
|
||||
*/
|
||||
esp_err_t cdc_acm_host_install(const cdc_acm_host_driver_config_t *driver_config);
|
||||
|
||||
/**
|
||||
* @brief Uninstall CDC-ACM driver
|
||||
*
|
||||
* - Users must ensure that all CDC devices must be closed via cdc_acm_host_close() before calling this function
|
||||
*
|
||||
* @return esp_err_t
|
||||
*/
|
||||
esp_err_t cdc_acm_host_uninstall(void);
|
||||
|
||||
/**
|
||||
* @brief Open CDC-ACM compliant device
|
||||
*
|
||||
* CDC-ACM compliant device must contain either an Interface Association Descriptor or CDC-Union descriptor,
|
||||
* which are used for the driver's configuration.
|
||||
*
|
||||
* @param[in] vid Device's Vendor ID
|
||||
* @param[in] pid Device's Product ID
|
||||
* @param[in] interface_idx Index of device's interface used for CDC-ACM communication
|
||||
* @param[in] dev_config Configuration structure of the device
|
||||
* @param[out] cdc_hdl_ret CDC device handle
|
||||
* @return esp_err_t
|
||||
*/
|
||||
esp_err_t cdc_acm_host_open(uint16_t vid, uint16_t pid, uint8_t interface_idx, const cdc_acm_host_device_config_t *dev_config, cdc_acm_dev_hdl_t *cdc_hdl_ret);
|
||||
|
||||
/**
|
||||
* @brief Open CDC-ACM non-compliant device
|
||||
*
|
||||
* CDC-ACM non-compliant device acts as CDC-ACM device but doesn't support all its features.
|
||||
* User must provide the interface index that will be used (zero for non-composite devices).
|
||||
*
|
||||
* @param[in] vid Device's Vendor ID
|
||||
* @param[in] pid Device's Product ID
|
||||
* @param[in] interface_idx Index of device's interface used for CDC-ACM like communication
|
||||
* @param[in] dev_config Configuration structure of the device
|
||||
* @param[out] cdc_hdl_ret CDC device handle
|
||||
* @return esp_err_t
|
||||
*/
|
||||
esp_err_t cdc_acm_host_open_vendor_specific(uint16_t vid, uint16_t pid, uint8_t interface_num, const cdc_acm_host_device_config_t *dev_config, cdc_acm_dev_hdl_t *cdc_hdl_ret);
|
||||
|
||||
/**
|
||||
* @brief Close CDC device and release its resources
|
||||
*
|
||||
* @note All in-flight transfers will be prematurely canceled.
|
||||
* @param cdc_hdl CDC handle obtained from cdc_acm_host_open()
|
||||
* @return esp_err_t
|
||||
*/
|
||||
esp_err_t cdc_acm_host_close(cdc_acm_dev_hdl_t cdc_hdl);
|
||||
|
||||
/**
|
||||
* @brief Transmit data - blocking mode
|
||||
*
|
||||
* @param cdc_hdl CDC handle obtained from cdc_acm_host_open()
|
||||
* @param[in] data Data to be sent
|
||||
* @param[in] data_len Data length
|
||||
* @param[in] timeout_ms Timeout in [ms]
|
||||
* @return esp_err_t
|
||||
*/
|
||||
esp_err_t cdc_acm_host_data_tx_blocking(cdc_acm_dev_hdl_t cdc_hdl, const uint8_t *data, size_t data_len, uint32_t timeout_ms);
|
||||
|
||||
/**
|
||||
* @brief SetLineCoding function
|
||||
*
|
||||
* @see Chapter 6.3.10, USB CDC-PSTN specification rev. 1.2
|
||||
*
|
||||
* @param cdc_hdl CDC handle obtained from cdc_acm_host_open()
|
||||
* @param[in] line_coding Line Coding structure
|
||||
* @return esp_err_t
|
||||
*/
|
||||
esp_err_t cdc_acm_host_line_coding_set(cdc_acm_dev_hdl_t cdc_hdl, const cdc_acm_line_coding_t *line_coding);
|
||||
|
||||
/**
|
||||
* @brief GetLineCoding function
|
||||
*
|
||||
* @see Chapter 6.3.11, USB CDC-PSTN specification rev. 1.2
|
||||
*
|
||||
* @param cdc_hdl CDC handle obtained from cdc_acm_host_open()
|
||||
* @param[out] line_coding Line Coding structure to be filled
|
||||
* @return esp_err_t
|
||||
*/
|
||||
esp_err_t cdc_acm_host_line_coding_get(cdc_acm_dev_hdl_t cdc_hdl, cdc_acm_line_coding_t *line_coding);
|
||||
|
||||
/**
|
||||
* @brief SetControlLineState function
|
||||
*
|
||||
* @see Chapter 6.3.12, USB CDC-PSTN specification rev. 1.2
|
||||
*
|
||||
* @param cdc_hdl CDC handle obtained from cdc_acm_host_open()
|
||||
* @param[in] dtr Indicates to DCE if DTE is present or not. This signal corresponds to V.24 signal 108/2 and RS-232 signal Data Terminal Ready.
|
||||
* @param[in] rts Carrier control for half duplex modems. This signal corresponds to V.24 signal 105 and RS-232 signal Request To Send.
|
||||
* @return esp_err_t
|
||||
*/
|
||||
esp_err_t cdc_acm_host_set_control_line_state(cdc_acm_dev_hdl_t cdc_hdl, bool dtr, bool rts);
|
||||
|
||||
/**
|
||||
* @brief SendBreak function
|
||||
*
|
||||
* This function will block until the duration_ms has passed.
|
||||
*
|
||||
* @see Chapter 6.3.13, USB CDC-PSTN specification rev. 1.2
|
||||
*
|
||||
* @param cdc_hdl CDC handle obtained from cdc_acm_host_open()
|
||||
* @param[in] duration_ms Duration of the Break signal in [ms]
|
||||
* @return esp_err_t
|
||||
*/
|
||||
esp_err_t cdc_acm_host_send_break(cdc_acm_dev_hdl_t cdc_hdl, uint16_t duration_ms);
|
||||
|
||||
/**
|
||||
* @brief Print CDC-ACM specific descriptors
|
||||
*
|
||||
* Descriptors are printed in human readable format to stdout.
|
||||
* Intended for debugging and for CDC-ACM compliant devices only.
|
||||
*
|
||||
* @param cdc_hdl CDC handle obtained from cdc_acm_host_open()
|
||||
*/
|
||||
void cdc_acm_host_desc_print(cdc_acm_dev_hdl_t cdc_hdl);
|
||||
|
||||
/**
|
||||
* @brief Get protocols defined in USB-CDC interface descriptors
|
||||
*
|
||||
* @param cdc_hdl CDC handle obtained from cdc_acm_host_open()
|
||||
* @param[out] comm Communication protocol
|
||||
* @param[out] data Data protocol
|
||||
* @return esp_err_t
|
||||
*/
|
||||
esp_err_t cdc_acm_host_protocols_get(cdc_acm_dev_hdl_t cdc_hdl, cdc_comm_protocol_t *comm, cdc_data_protocol_t *data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
class CdcAcmDevice
|
||||
{
|
||||
public:
|
||||
// Operators
|
||||
CdcAcmDevice() :
|
||||
cdc_hdl(NULL){};
|
||||
~CdcAcmDevice()
|
||||
{
|
||||
// Close CDC-ACM device, if it wasn't explicitly closed
|
||||
if (this->cdc_hdl != NULL) {
|
||||
this->close();
|
||||
}
|
||||
}
|
||||
|
||||
inline esp_err_t tx_blocking(uint8_t *data, size_t len, uint32_t timeout_ms = 100)
|
||||
{
|
||||
return cdc_acm_host_data_tx_blocking(this->cdc_hdl, data, len, timeout_ms);
|
||||
}
|
||||
|
||||
inline esp_err_t open(uint16_t vid, uint16_t pid, uint8_t interface_idx, const cdc_acm_host_device_config_t *dev_config)
|
||||
{
|
||||
return cdc_acm_host_open(vid, pid, interface_idx, dev_config, &this->cdc_hdl);
|
||||
}
|
||||
|
||||
inline esp_err_t open_vendor_specific(uint16_t vid, uint16_t pid, uint8_t interface_idx, const cdc_acm_host_device_config_t *dev_config)
|
||||
{
|
||||
return cdc_acm_host_open_vendor_specific(vid, pid, interface_idx, dev_config, &this->cdc_hdl);
|
||||
}
|
||||
|
||||
inline void close()
|
||||
{
|
||||
cdc_acm_host_close(this->cdc_hdl);
|
||||
this->cdc_hdl = NULL;
|
||||
}
|
||||
|
||||
inline esp_err_t line_coding_get(cdc_acm_line_coding_t *line_coding)
|
||||
{
|
||||
return cdc_acm_host_line_coding_get(this->cdc_hdl, line_coding);
|
||||
}
|
||||
|
||||
inline esp_err_t line_coding_set(cdc_acm_line_coding_t *line_coding)
|
||||
{
|
||||
return cdc_acm_host_line_coding_set(this->cdc_hdl, line_coding);
|
||||
}
|
||||
|
||||
inline esp_err_t set_control_line_state(bool dtr, bool rts)
|
||||
{
|
||||
return cdc_acm_host_set_control_line_state(this->cdc_hdl, dtr, rts);
|
||||
}
|
||||
|
||||
inline esp_err_t send_break(uint16_t duration_ms)
|
||||
{
|
||||
return cdc_acm_host_send_break(this->cdc_hdl, duration_ms);
|
||||
}
|
||||
|
||||
private:
|
||||
CdcAcmDevice(const CdcAcmDevice &Copy);
|
||||
CdcAcmDevice &operator=(const CdcAcmDevice &Copy);
|
||||
bool operator==(const CdcAcmDevice ¶m) const;
|
||||
bool operator!=(const CdcAcmDevice ¶m) const;
|
||||
cdc_acm_dev_hdl_t cdc_hdl;
|
||||
};
|
||||
#endif
|
||||
@@ -0,0 +1,206 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <inttypes.h>
|
||||
|
||||
/**
|
||||
* @brief USB CDC Descriptor Subtypes
|
||||
*
|
||||
* @see Table 13, USB CDC specification rev. 1.2
|
||||
*/
|
||||
typedef enum {
|
||||
CDC_DESC_SUBTYPE_HEADER = 0x00, // Header Functional Descriptor
|
||||
CDC_DESC_SUBTYPE_CALL = 0x01, // Call Management Functional Descriptor
|
||||
CDC_DESC_SUBTYPE_ACM = 0x02, // Abstract Control Management Functional Descriptor
|
||||
CDC_DESC_SUBTYPE_DLM = 0x03, // Direct Line Management Functional Descriptor
|
||||
CDC_DESC_SUBTYPE_TEL_RINGER = 0x04, // Telephone Ringer Functional Descriptor
|
||||
CDC_DESC_SUBTYPE_TEL_CLSR = 0x05, // Telephone Call and Line State Reporting Capabilities Functional Descriptor
|
||||
CDC_DESC_SUBTYPE_UNION = 0x06, // Union Functional Descriptor
|
||||
CDC_DESC_SUBTYPE_COUNTRY = 0x07, // Country Selection Functional Descriptor
|
||||
CDC_DESC_SUBTYPE_TEL_MODE = 0x08, // Telephone Operational Modes Functional Descriptor
|
||||
CDC_DESC_SUBTYPE_TERMINAL = 0x09, // USB Terminal
|
||||
CDC_DESC_SUBTYPE_NCHT = 0x0A, // Network Channel Terminal
|
||||
CDC_DESC_SUBTYPE_PROTOCOL = 0x08, // Protocol Unit
|
||||
CDC_DESC_SUBTYPE_EXTENSION = 0x0C, // Extension Unit
|
||||
CDC_DESC_SUBTYPE_MULTI_CHAN = 0x0D, // Multi-Channel Management Functional Descriptor
|
||||
CDC_DESC_SUBTYPE_CAPI = 0x0E, // CAPI Control
|
||||
CDC_DESC_SUBTYPE_ETH = 0x0F, // Ethernet Networking
|
||||
CDC_DESC_SUBTYPE_ATM = 0x10, // ATM Networking
|
||||
CDC_DESC_SUBTYPE_WHANDSET = 0x11, // Wireless Handset Control Model Functional Descriptor
|
||||
CDC_DESC_SUBTYPE_MDLM = 0x12, // Mobile Direct Line Model
|
||||
CDC_DESC_SUBTYPE_MDLM_DETAIL = 0x13, // MDLM Detail
|
||||
CDC_DESC_SUBTYPE_DMM = 0x14, // Device Management Model
|
||||
CDC_DESC_SUBTYPE_OBEX = 0x15, // OBEX Functional
|
||||
CDC_DESC_SUBTYPE_COMMAND_SET = 0x16, // Command Set
|
||||
CDC_DESC_SUBTYPE_COMMAND_SET_DETAIL = 0x17, // Command Set Detail Functional Descriptor
|
||||
CDC_DESC_SUBTYPE_TEL_CM = 0x18, // Telephone Control Model Functional Descriptor
|
||||
CDC_DESC_SUBTYPE_OBEX_SERVICE = 0x19, // OBEX Service Identifier Functional Descriptor
|
||||
CDC_DESC_SUBTYPE_NCM = 0x1A // NCM Functional Descriptor
|
||||
} __attribute__((packed)) cdc_desc_subtype_t;
|
||||
|
||||
/**
|
||||
* @brief USB CDC Subclass codes
|
||||
*
|
||||
* @see Table 4, USB CDC specification rev. 1.2
|
||||
*/
|
||||
typedef enum {
|
||||
CDC_SUBCLASS_DLCM = 0x01, // Direct Line Control Model
|
||||
CDC_SUBCLASS_ACM = 0x02, // Abstract Control Model
|
||||
CDC_SUBCLASS_TCM = 0x03, // Telephone Control Model
|
||||
CDC_SUBCLASS_MCHCM = 0x04, // Multi-Channel Control Model
|
||||
CDC_SUBCLASS_CAPI = 0x05, // CAPI Control Model
|
||||
CDC_SUBCLASS_ECM = 0x06, // Ethernet Networking Control Model
|
||||
CDC_SUBCLASS_ATM = 0x07, // ATM Networking Model
|
||||
CDC_SUBCLASS_HANDSET = 0x08, // Wireless Handset Control Model
|
||||
CDC_SUBCLASS_DEV_MAN = 0x09, // Device Management
|
||||
CDC_SUBCLASS_MOBILE = 0x0A, // Mobile Direct Line Model
|
||||
CDC_SUBCLASS_OBEX = 0x0B, // OBEX
|
||||
CDC_SUBCLASS_EEM = 0x0C, // Ethernet Emulation Model
|
||||
CDC_SUBCLASS_NCM = 0x0D // Network Control Model
|
||||
} __attribute__((packed)) cdc_subclass_t;
|
||||
|
||||
/**
|
||||
* @brief USB CDC Communications Protocol Codes
|
||||
*
|
||||
* @see Table 5, USB CDC specification rev. 1.2
|
||||
*/
|
||||
typedef enum {
|
||||
CDC_COMM_PROTOCOL_NONE = 0x00, // No class specific protocol required
|
||||
CDC_COMM_PROTOCOL_V250 = 0x01, // AT Commands: V.250 etc
|
||||
CDC_COMM_PROTOCOL_PCAA = 0x02, // AT Commands defined by PCCA-101
|
||||
CDC_COMM_PROTOCOL_PCAA_A = 0x03, // AT Commands defined by PCAA-101 & Annex O
|
||||
CDC_COMM_PROTOCOL_GSM = 0x04, // AT Commands defined by GSM 07.07
|
||||
CDC_COMM_PROTOCOL_3GPP = 0x05, // AT Commands defined by 3GPP 27.007
|
||||
CDC_COMM_PROTOCOL_TIA = 0x06, // AT Commands defined by TIA for CDMA
|
||||
CDC_COMM_PROTOCOL_EEM = 0x07, // Ethernet Emulation Model
|
||||
CDC_COMM_PROTOCOL_EXT = 0xFE, // External Protocol: Commands defined by Command Set Functional Descriptor
|
||||
CDC_COMM_PROTOCOL_VENDOR = 0xFF // Vendor-specific
|
||||
} __attribute__((packed)) cdc_comm_protocol_t;
|
||||
|
||||
/**
|
||||
* @brief USB CDC Data Protocol Codes
|
||||
*
|
||||
* @see Table 7, USB CDC specification rev. 1.2
|
||||
*/
|
||||
typedef enum {
|
||||
CDC_DATA_PROTOCOL_NONE = 0x00, // No class specific protocol required
|
||||
CDC_DATA_PROTOCOL_NCM = 0x01, // Network Transfer Block
|
||||
CDC_DATA_PROTOCOL_I430 = 0x30, // Physical interface protocol for ISDN BRI
|
||||
CDC_DATA_PROTOCOL_HDLC = 0x31, // HDLC
|
||||
CDC_DATA_PROTOCOL_Q921M = 0x50, // Management protocol for Q.921 data link protocol
|
||||
CDC_DATA_PROTOCOL_Q921 = 0x51, // Data link protocol for Q.931
|
||||
CDC_DATA_PROTOCOL_Q921TM = 0x52, // TEI-multiplexor for Q.921 data link protocol
|
||||
CDC_DATA_PROTOCOL_V42BIS = 0x90, // Data compression procedures
|
||||
CDC_DATA_PROTOCOL_Q931 = 0x91, // Euro-ISDN protocol control
|
||||
CDC_DATA_PROTOCOL_V120 = 0x92, // V.24 rate adaptation to ISDN
|
||||
CDC_DATA_PROTOCOL_CAPI = 0x93, // CAPI Commands
|
||||
CDC_DATA_PROTOCOL_VENDOR = 0xFF // Vendor-specific
|
||||
} __attribute__((packed)) cdc_data_protocol_t;
|
||||
|
||||
/**
|
||||
* @brief USB CDC Request Codes
|
||||
*
|
||||
* @see Table 19, USB CDC specification rev. 1.2
|
||||
*/
|
||||
typedef enum {
|
||||
CDC_REQ_SEND_ENCAPSULATED_COMMAND = 0x00,
|
||||
CDC_REQ_GET_ENCAPSULATED_RESPONSE = 0x01,
|
||||
CDC_REQ_SET_COMM_FEATURE = 0x02,
|
||||
CDC_REQ_GET_COMM_FEATURE = 0x03,
|
||||
CDC_REQ_CLEAR_COMM_FEATURE = 0x04,
|
||||
CDC_REQ_SET_AUX_LINE_STATE = 0x10,
|
||||
CDC_REQ_SET_HOOK_STATE = 0x11,
|
||||
CDC_REQ_PULSE_SETUP = 0x12,
|
||||
CDC_REQ_SEND_PULSE = 0x13,
|
||||
CDC_REQ_SET_PULSE_TIME = 0x14,
|
||||
CDC_REQ_RING_AUX_JACK = 0x15,
|
||||
CDC_REQ_SET_LINE_CODING = 0x20,
|
||||
CDC_REQ_GET_LINE_CODING = 0x21,
|
||||
CDC_REQ_SET_CONTROL_LINE_STATE = 0x22,
|
||||
CDC_REQ_SEND_BREAK = 0x23,
|
||||
CDC_REQ_SET_RINGER_PARMS = 0x30,
|
||||
CDC_REQ_GET_RINGER_PARMS = 0x31,
|
||||
CDC_REQ_SET_OPERATION_PARMS = 0x32,
|
||||
CDC_REQ_GET_OPERATION_PARMS = 0x33,
|
||||
CDC_REQ_SET_LINE_PARMS = 0x34,
|
||||
CDC_REQ_GET_LINE_PARMS = 0x35,
|
||||
CDC_REQ_DIAL_DIGITS = 0x36,
|
||||
CDC_REQ_SET_UNIT_PARAMETER = 0x37,
|
||||
CDC_REQ_GET_UNIT_PARAMETER = 0x38,
|
||||
CDC_REQ_CLEAR_UNIT_PARAMETER = 0x39,
|
||||
CDC_REQ_GET_PROFILE = 0x3A,
|
||||
CDC_REQ_SET_ETHERNET_MULTICAST_FILTERS = 0x40,
|
||||
CDC_REQ_SET_ETHERNET_POWER_MANAGEMENT_PATTERN_FILTER = 0x41,
|
||||
CDC_REQ_GET_ETHERNET_POWER_MANAGEMENT_PATTERN_FILTER = 0x42,
|
||||
CDC_REQ_SET_ETHERNET_PACKET_FILTER = 0x43,
|
||||
CDC_REQ_GET_ETHERNET_STATISTIC = 0x44,
|
||||
CDC_REQ_SET_ATM_DATA_FORMAT = 0x50,
|
||||
CDC_REQ_GET_ATM_DEVICE_STATISTICS = 0x51,
|
||||
CDC_REQ_SET_ATM_DEFAULT_VC = 0x52,
|
||||
CDC_REQ_GET_ATM_VC_STATISTICS = 0x53,
|
||||
CDC_REQ_GET_NTB_PARAMETERS = 0x80,
|
||||
CDC_REQ_GET_NET_ADDRESS = 0x81,
|
||||
CDC_REQ_SET_NET_ADDRESS = 0x82,
|
||||
CDC_REQ_GET_NTB_FORMAT = 0x83,
|
||||
CDC_REQ_SET_NTB_FORMAT = 0x84,
|
||||
CDC_REQ_GET_NTB_INPUT_SIZE = 0x85,
|
||||
CDC_REQ_SET_NTB_INPUT_SIZE = 0x86,
|
||||
CDC_REQ_GET_MAX_DATAGRAM_SIZE = 0x87,
|
||||
CDC_REQ_SET_MAX_DATAGRAM_SIZE = 0x88,
|
||||
CDC_REQ_GET_CRC_MODE = 0x89,
|
||||
CDC_REQ_SET_CRC_MODE = 0x8A
|
||||
} __attribute__((packed)) cdc_request_code_t;
|
||||
|
||||
/**
|
||||
* @brief USB CDC Notification Codes
|
||||
*
|
||||
* @see Table 20, USB CDC specification rev. 1.2
|
||||
*/
|
||||
typedef enum {
|
||||
CDC_NOTIF_NETWORK_CONNECTION = 0x00,
|
||||
CDC_NOTIF_RESPONSE_AVAILABLE = 0x01,
|
||||
CDC_NOTIF_AUX_JACK_HOOK_STATE = 0x08,
|
||||
CDC_NOTIF_RING_DETECT = 0x09,
|
||||
CDC_NOTIF_SERIAL_STATE = 0x20,
|
||||
CDC_NOTIF_CALL_STATE_CHANGE = 0x28,
|
||||
CDC_NOTIF_LINE_STATE_CHANGE = 0x29,
|
||||
CDC_NOTIF_CONNECTION_SPEED_CHANGE = 0x2A
|
||||
} __attribute__((packed)) cdc_notification_code_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t bmRequestType;
|
||||
cdc_notification_code_t bNotificationCode;
|
||||
uint16_t wValue;
|
||||
uint16_t wIndex;
|
||||
uint16_t wLength;
|
||||
uint8_t Data[];
|
||||
} __attribute__((packed)) cdc_notification_t;
|
||||
|
||||
/**
|
||||
* @brief USB CDC Header Functional Descriptor
|
||||
*
|
||||
* @see Table 15, USB CDC specification rev. 1.2
|
||||
*/
|
||||
typedef struct {
|
||||
uint8_t bFunctionLength;
|
||||
const uint8_t bDescriptorType; // Upper nibble: CDC code 0x02, Lower nibble: intf/ep descriptor type 0x04/0x05
|
||||
const cdc_desc_subtype_t bDescriptorSubtype;
|
||||
uint16_t bcdCDC; // CDC version as binary-coded decimal. This driver is written for version 1.2
|
||||
} __attribute__((packed)) cdc_header_desc_t;
|
||||
|
||||
/**
|
||||
* @brief USB CDC Union Functional Descriptor
|
||||
*
|
||||
* @see Table 16, USB CDC specification rev. 1.2
|
||||
*/
|
||||
typedef struct {
|
||||
uint8_t bFunctionLength;
|
||||
const uint8_t bDescriptorType; // Upper nibble: CDC code 0x02, Lower nibble: intf/ep descriptor type 0x04/0x05
|
||||
const cdc_desc_subtype_t bDescriptorSubtype;
|
||||
const uint8_t bControlInterface; // Master/controlling interface
|
||||
uint8_t bSubordinateInterface[]; // Slave/subordinate interfaces
|
||||
} __attribute__((packed)) cdc_union_desc_t;
|
||||
@@ -0,0 +1,3 @@
|
||||
idf_component_register(SRCS "test_cdc_acm_host.c"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES cdc_acm_host unity)
|
||||
@@ -0,0 +1,378 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "soc/soc_caps.h"
|
||||
#if SOC_USB_OTG_SUPPORTED
|
||||
|
||||
#include <stdio.h>
|
||||
#include "esp_system.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_err.h"
|
||||
|
||||
#include "esp_private/usb_phy.h"
|
||||
#include "usb/usb_host.h"
|
||||
#include "usb/cdc_acm_host.h"
|
||||
#include <string.h>
|
||||
|
||||
#include "esp_intr_alloc.h"
|
||||
|
||||
#include "unity.h"
|
||||
#include "soc/usb_wrap_struct.h"
|
||||
|
||||
static uint8_t tx_buf[] = "HELLO";
|
||||
static uint8_t tx_buf2[] = "WORLD";
|
||||
static int nb_of_responses;
|
||||
static int nb_of_responses2;
|
||||
static usb_phy_handle_t phy_hdl = NULL;
|
||||
|
||||
static void force_conn_state(bool connected, TickType_t delay_ticks)
|
||||
{
|
||||
TEST_ASSERT_NOT_EQUAL(NULL, phy_hdl);
|
||||
if (delay_ticks > 0) {
|
||||
//Delay of 0 ticks causes a yield. So skip if delay_ticks is 0.
|
||||
vTaskDelay(delay_ticks);
|
||||
}
|
||||
ESP_ERROR_CHECK(usb_phy_action(phy_hdl, (connected) ? USB_PHY_ACTION_HOST_ALLOW_CONN : USB_PHY_ACTION_HOST_FORCE_DISCONN));
|
||||
}
|
||||
|
||||
void usb_lib_task(void *arg)
|
||||
{
|
||||
//Initialize the internal USB PHY to connect to the USB OTG peripheral. We manually install the USB PHY for testing
|
||||
usb_phy_config_t phy_config = {
|
||||
.controller = USB_PHY_CTRL_OTG,
|
||||
.target = USB_PHY_TARGET_INT,
|
||||
.otg_mode = USB_OTG_MODE_HOST,
|
||||
.otg_speed = USB_PHY_SPEED_UNDEFINED, //In Host mode, the speed is determined by the connected device
|
||||
.gpio_conf = NULL,
|
||||
};
|
||||
TEST_ASSERT_EQUAL(ESP_OK, usb_new_phy(&phy_config, &phy_hdl));
|
||||
// Install USB Host driver. Should only be called once in entire application
|
||||
const usb_host_config_t host_config = {
|
||||
.skip_phy_setup = true,
|
||||
.intr_flags = ESP_INTR_FLAG_LEVEL1,
|
||||
};
|
||||
TEST_ASSERT_EQUAL(ESP_OK, usb_host_install(&host_config));
|
||||
printf("USB Host installed\n");
|
||||
xTaskNotifyGive(arg);
|
||||
|
||||
while (1) {
|
||||
// Start handling system events
|
||||
uint32_t event_flags;
|
||||
usb_host_lib_handle_events(portMAX_DELAY, &event_flags);
|
||||
if (event_flags & USB_HOST_LIB_EVENT_FLAGS_NO_CLIENTS) {
|
||||
printf("No more clients: clean up\n");
|
||||
// The device should not have been freed yet, so we expect an ESP_ERR_NOT_FINISHED
|
||||
TEST_ASSERT_EQUAL(ESP_ERR_NOT_FINISHED, usb_host_device_free_all());
|
||||
}
|
||||
if (event_flags & USB_HOST_LIB_EVENT_FLAGS_ALL_FREE) {
|
||||
printf("All free: uninstall USB lib\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Clean up USB Host
|
||||
vTaskDelay(10); // Short delay to allow clients clean-up
|
||||
usb_host_lib_handle_events(0, NULL); // Make sure there are now pending events
|
||||
TEST_ASSERT_EQUAL(ESP_OK, usb_host_uninstall());
|
||||
//Tear down USB PHY
|
||||
TEST_ASSERT_EQUAL(ESP_OK, usb_del_phy(phy_hdl));
|
||||
phy_hdl = NULL;
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void test_install_cdc_driver(void)
|
||||
{
|
||||
// Create a task that will handle USB library events
|
||||
TEST_ASSERT_EQUAL(pdTRUE, xTaskCreate(usb_lib_task, "usb_lib", 4*4096, xTaskGetCurrentTaskHandle(), 10, NULL));
|
||||
ulTaskNotifyTake(false, 1000);
|
||||
|
||||
printf("Installing CDC-ACM driver\n");
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_install(NULL));
|
||||
}
|
||||
|
||||
/* ------------------------------- Callbacks -------------------------------- */
|
||||
static void handle_rx(uint8_t *data, size_t data_len, void *arg)
|
||||
{
|
||||
printf("Data received\n");
|
||||
nb_of_responses++;
|
||||
TEST_ASSERT_EQUAL_STRING_LEN(data, arg, data_len);
|
||||
}
|
||||
|
||||
static void handle_rx2(uint8_t *data, size_t data_len, void *arg)
|
||||
{
|
||||
printf("Data received 2\n");
|
||||
nb_of_responses2++;
|
||||
TEST_ASSERT_EQUAL_STRING_LEN(data, arg, data_len);
|
||||
}
|
||||
|
||||
static void notif_cb(cdc_acm_dev_hdl_t cdc_hdl, const cdc_acm_host_dev_event_data_t *event, void *user_ctx)
|
||||
{
|
||||
switch (event->type) {
|
||||
case CDC_ACM_HOST_ERROR:
|
||||
printf("Error event %d\n", event->data.error);
|
||||
break;
|
||||
case CDC_ACM_HOST_SERIAL_STATE:
|
||||
break;
|
||||
case CDC_ACM_HOST_NETWORK_CONNECTION:
|
||||
break;
|
||||
case CDC_ACM_HOST_DEVICE_DISCONNECTED:
|
||||
printf("Disconnection event\n");
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_close(cdc_hdl));
|
||||
xTaskNotifyGive(user_ctx);
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
/* Basic test to check CDC communication:
|
||||
* open/read/write/close device
|
||||
* CDC-ACM specific commands: set/get_line_coding, set_control_line_state */
|
||||
TEST_CASE("USB Host CDC-ACM driver: Basic test", "[cdc_acm][ignore]")
|
||||
{
|
||||
nb_of_responses = 0;
|
||||
cdc_acm_dev_hdl_t cdc_dev = NULL;
|
||||
|
||||
test_install_cdc_driver();
|
||||
|
||||
const cdc_acm_host_device_config_t dev_config = {
|
||||
.connection_timeout_ms = 500,
|
||||
.out_buffer_size = 64,
|
||||
.event_cb = notif_cb,
|
||||
.data_cb = handle_rx,
|
||||
.user_arg = tx_buf,
|
||||
};
|
||||
|
||||
printf("Opening CDC-ACM device\n");
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_open(0x303A, 0x4002, 0, &dev_config, &cdc_dev)); // 0x303A:0x4002 (TinyUSB Dual CDC device)
|
||||
TEST_ASSERT_NOT_NULL(cdc_dev);
|
||||
cdc_acm_host_desc_print(cdc_dev);
|
||||
vTaskDelay(100);
|
||||
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_data_tx_blocking(cdc_dev, tx_buf, sizeof(tx_buf), 1000));
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_data_tx_blocking(cdc_dev, tx_buf, sizeof(tx_buf), 1000));
|
||||
vTaskDelay(100); // Wait until responses are processed
|
||||
|
||||
// We sent two messages, should get two responses
|
||||
TEST_ASSERT_EQUAL(2, nb_of_responses);
|
||||
|
||||
cdc_acm_line_coding_t line_coding_get;
|
||||
const cdc_acm_line_coding_t line_coding_set = {
|
||||
.dwDTERate = 9600,
|
||||
.bDataBits = 7,
|
||||
.bParityType = 1,
|
||||
.bCharFormat = 1,
|
||||
};
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_line_coding_set(cdc_dev, &line_coding_set));
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_line_coding_get(cdc_dev, &line_coding_get));
|
||||
TEST_ASSERT_EQUAL_MEMORY(&line_coding_set, &line_coding_get, sizeof(cdc_acm_line_coding_t));
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_set_control_line_state(cdc_dev, true, false));
|
||||
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_close(cdc_dev));
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_uninstall());
|
||||
|
||||
vTaskDelay(20); //Short delay to allow task to be cleaned up
|
||||
}
|
||||
|
||||
/* Test communication with multiple CDC-ACM devices from one thread */
|
||||
TEST_CASE("USB Host CDC-ACM driver: Multiple devices test", "[cdc_acm][ignore]")
|
||||
{
|
||||
nb_of_responses = 0;
|
||||
nb_of_responses2 = 0;
|
||||
|
||||
test_install_cdc_driver();
|
||||
|
||||
printf("Opening 2 CDC-ACM devices\n");
|
||||
cdc_acm_dev_hdl_t cdc_dev1, cdc_dev2;
|
||||
cdc_acm_host_device_config_t dev_config = {
|
||||
.connection_timeout_ms = 1000,
|
||||
.out_buffer_size = 64,
|
||||
.event_cb = notif_cb,
|
||||
.data_cb = handle_rx,
|
||||
.user_arg = tx_buf,
|
||||
};
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_open(0x303A, 0x4002, 0, &dev_config, &cdc_dev1)); // 0x303A:0x4002 (TinyUSB Dual CDC device)
|
||||
dev_config.data_cb = handle_rx2;
|
||||
dev_config.user_arg = tx_buf2;
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_open(0x303A, 0x4002, 2, &dev_config, &cdc_dev2)); // 0x303A:0x4002 (TinyUSB Dual CDC device)
|
||||
TEST_ASSERT_NOT_NULL(cdc_dev1);
|
||||
TEST_ASSERT_NOT_NULL(cdc_dev2);
|
||||
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_data_tx_blocking(cdc_dev1, tx_buf, sizeof(tx_buf), 1000));
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_data_tx_blocking(cdc_dev2, tx_buf2, sizeof(tx_buf2), 1000));
|
||||
|
||||
vTaskDelay(100); // Wait for RX callbacks
|
||||
|
||||
// We sent two messages, should get two responses
|
||||
TEST_ASSERT_EQUAL(1, nb_of_responses);
|
||||
TEST_ASSERT_EQUAL(1, nb_of_responses2);
|
||||
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_close(cdc_dev1));
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_close(cdc_dev2));
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_uninstall());
|
||||
|
||||
//Short delay to allow task to be cleaned up
|
||||
vTaskDelay(20);
|
||||
}
|
||||
|
||||
#define MULTIPLE_THREADS_TRANSFERS_NUM 5
|
||||
#define MULTIPLE_THREADS_TASKS_NUM 4
|
||||
void tx_task(void *arg)
|
||||
{
|
||||
cdc_acm_dev_hdl_t cdc_dev = (cdc_acm_dev_hdl_t) arg;
|
||||
// Send multiple transfers to make sure that some of them will run at the same time
|
||||
for (int i = 0; i < MULTIPLE_THREADS_TRANSFERS_NUM; i++) {
|
||||
// BULK endpoints
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_data_tx_blocking(cdc_dev, tx_buf, sizeof(tx_buf), 1000));
|
||||
|
||||
// CTRL endpoints
|
||||
cdc_acm_line_coding_t line_coding_get;
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_line_coding_get(cdc_dev, &line_coding_get));
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_set_control_line_state(cdc_dev, true, false));
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Multiple threads test
|
||||
*
|
||||
* In this test, one CDC device is accessed from multiple threads.
|
||||
* It has to be opened/closed just once, though.
|
||||
*/
|
||||
TEST_CASE("USB Host CDC-ACM driver: Multiple threads test", "[cdc_acm][ignore]")
|
||||
{
|
||||
nb_of_responses = 0;
|
||||
cdc_acm_dev_hdl_t cdc_dev;
|
||||
test_install_cdc_driver();
|
||||
|
||||
const cdc_acm_host_device_config_t dev_config = {
|
||||
.connection_timeout_ms = 5000,
|
||||
.out_buffer_size = 64,
|
||||
.event_cb = notif_cb,
|
||||
.data_cb = handle_rx,
|
||||
.user_arg = tx_buf,
|
||||
};
|
||||
|
||||
printf("Opening CDC-ACM device\n");
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_open(0x303A, 0x4002, 0, &dev_config, &cdc_dev)); // 0x303A:0x4002 (TinyUSB Dual CDC device)
|
||||
TEST_ASSERT_NOT_NULL(cdc_dev);
|
||||
|
||||
// Create two tasks that will try to access cdc_dev
|
||||
for (int i = 0; i < MULTIPLE_THREADS_TASKS_NUM; i++) {
|
||||
TEST_ASSERT_EQUAL(pdTRUE, xTaskCreate(tx_task, "CDC TX", 4096, cdc_dev, i + 3, NULL));
|
||||
}
|
||||
|
||||
// Wait until all tasks finish
|
||||
vTaskDelay(pdMS_TO_TICKS(500));
|
||||
TEST_ASSERT_EQUAL(MULTIPLE_THREADS_TASKS_NUM * MULTIPLE_THREADS_TRANSFERS_NUM, nb_of_responses);
|
||||
|
||||
// Clean-up
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_close(cdc_dev));
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_uninstall());
|
||||
vTaskDelay(20);
|
||||
}
|
||||
|
||||
/* Test CDC driver reaction to USB device sudden disconnection */
|
||||
TEST_CASE("USB Host CDC-ACM driver: Sudden disconnection test", "[cdc_acm][ignore]")
|
||||
{
|
||||
test_install_cdc_driver();
|
||||
|
||||
cdc_acm_dev_hdl_t cdc_dev;
|
||||
cdc_acm_host_device_config_t dev_config = {
|
||||
.connection_timeout_ms = 1000,
|
||||
.out_buffer_size = 64,
|
||||
.event_cb = notif_cb,
|
||||
.data_cb = handle_rx
|
||||
};
|
||||
dev_config.user_arg = xTaskGetCurrentTaskHandle();
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_open(0x303A, 0x4002, 0, &dev_config, &cdc_dev));
|
||||
TEST_ASSERT_NOT_NULL(cdc_dev);
|
||||
|
||||
force_conn_state(false, pdMS_TO_TICKS(10));
|
||||
// Notify will succeed only if CDC_ACM_HOST_DEVICE_DISCONNECTED notification was generated
|
||||
TEST_ASSERT_EQUAL(1, ulTaskNotifyTake(false, pdMS_TO_TICKS(100)));
|
||||
|
||||
force_conn_state(true, 0); // Switch back to real PHY
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_uninstall());
|
||||
vTaskDelay(20); //Short delay to allow task to be cleaned up
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief CDC-ACM error handling test
|
||||
*
|
||||
* There are multiple erroneous scenarios checked in this test:
|
||||
*
|
||||
* -# Install CDC-ACM driver without USB Host
|
||||
* -# Open device without installed driver
|
||||
* -# Uninstall driver before installing it
|
||||
* -# Open non-existent device
|
||||
* -# Open the same device twice
|
||||
* -# Uninstall driver with open devices
|
||||
* -# Send data that is too large
|
||||
* -# Send unsupported CDC request
|
||||
* -# Write to read-only device
|
||||
*/
|
||||
TEST_CASE("USB Host CDC-ACM driver: Error handling", "[cdc_acm][ignore]")
|
||||
{
|
||||
cdc_acm_dev_hdl_t cdc_dev;
|
||||
cdc_acm_host_device_config_t dev_config = {
|
||||
.connection_timeout_ms = 500,
|
||||
.out_buffer_size = 64,
|
||||
.event_cb = notif_cb,
|
||||
.data_cb = handle_rx
|
||||
};
|
||||
|
||||
// Install CDC-ACM driver without USB Host
|
||||
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_STATE, cdc_acm_host_install(NULL));
|
||||
|
||||
// Open device without installed driver
|
||||
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_STATE, cdc_acm_host_open(0x303A, 0x4002, 0, &dev_config, &cdc_dev));
|
||||
|
||||
// Uninstall driver before installing it
|
||||
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_STATE, cdc_acm_host_uninstall());
|
||||
|
||||
// Properly install USB and CDC drivers
|
||||
test_install_cdc_driver();
|
||||
|
||||
// Open non-existent device
|
||||
TEST_ASSERT_EQUAL(ESP_ERR_NOT_FOUND, cdc_acm_host_open(0x303A, 0x1234, 0, &dev_config, &cdc_dev)); // 0x303A:0x1234 this device is not connected to USB Host
|
||||
TEST_ASSERT_NULL(cdc_dev);
|
||||
|
||||
// Open regular device
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_open(0x303A, 0x4002, 0, &dev_config, &cdc_dev));
|
||||
TEST_ASSERT_NOT_NULL(cdc_dev);
|
||||
|
||||
// Open one CDC-ACM device twice //@todo this test is commented out due to bug in usb_host
|
||||
//cdc_acm_dev_hdl_t cdc_dev_test;
|
||||
//TEST_ASSERT_EQUAL(ESP_ERR_INVALID_STATE, cdc_acm_host_open(0x303A, 0x4002, 0, &dev_config, &cdc_dev_test));
|
||||
//TEST_ASSERT_NULL(cdc_dev_test);
|
||||
|
||||
// Uninstall driver with open devices
|
||||
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_STATE, cdc_acm_host_uninstall());
|
||||
|
||||
// Send data that is too large and NULL data
|
||||
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_SIZE, cdc_acm_host_data_tx_blocking(cdc_dev, tx_buf, 1024, 1000));
|
||||
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, cdc_acm_host_data_tx_blocking(cdc_dev, NULL, 10, 1000));
|
||||
|
||||
// Change mode to read-only and try to write to it
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_close(cdc_dev));
|
||||
dev_config.out_buffer_size = 0; // Read-only device
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_open(0x303A, 0x4002, 0, &dev_config, &cdc_dev));
|
||||
TEST_ASSERT_NOT_NULL(cdc_dev);
|
||||
TEST_ASSERT_EQUAL(ESP_ERR_NOT_SUPPORTED, cdc_acm_host_data_tx_blocking(cdc_dev, tx_buf, sizeof(tx_buf), 1000));
|
||||
|
||||
// Send unsupported CDC request (TinyUSB accepts SendBreak command, eventhough it doesn't support it)
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_send_break(cdc_dev, 100));
|
||||
|
||||
// Clean-up
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_close(cdc_dev));
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_uninstall());
|
||||
vTaskDelay(20);
|
||||
}
|
||||
|
||||
#endif
|
||||
4
app/drivers/data_port/usb-host/component.mk
Executable file
4
app/drivers/data_port/usb-host/component.mk
Executable file
@@ -0,0 +1,4 @@
|
||||
#
|
||||
# "main" pseudo-component makefile.
|
||||
#
|
||||
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
|
||||
11
app/drivers/data_port/usb-host/config/hardware_define.h
Normal file
11
app/drivers/data_port/usb-host/config/hardware_define.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#define SBTASK_PRIORITY_USB_HOST 14
|
||||
#define SBTASK_PRIORITY_USB_DEVICE 15
|
||||
#define SBTASK_PRIORITY_USB_CDC 17
|
||||
#define SBTASK_PRIORITY_USB_MSC 18
|
||||
|
||||
#define SBTASK_CORE_INDEX_USB_HOST 0
|
||||
#define SBTASK_CORE_INDEX_USB_DEVICE 0
|
||||
#define SBTASK_CORE_INDEX_USB_CDC 0
|
||||
#define SBTASK_CORE_INDEX_USB_MSC 0
|
||||
109
app/drivers/data_port/usb-host/esp_usbh_cdc.h
Normal file
109
app/drivers/data_port/usb-host/esp_usbh_cdc.h
Normal file
@@ -0,0 +1,109 @@
|
||||
// Copyright 2019-2021 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#pragma once
|
||||
#include "stdio.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "esp_err.h"
|
||||
#include "hal/usbh_ll.h"
|
||||
#include "hcd.h"
|
||||
|
||||
/**
|
||||
* @brief USB host CDC configuration type, callbacks should not in block state
|
||||
*/
|
||||
typedef struct usbh_cdc_config
|
||||
{
|
||||
uint8_t bulk_in_ep_addr; /*!< USB CDC bulk in endpoint address, will be overwritten if bulk_in_ep is specified */
|
||||
uint8_t bulk_out_ep_addr; /*!< USB CDC bulk out endpoint address, will be overwritten if bulk_out_ep is specified */
|
||||
int rx_buffer_size; /*!< USB receive/in pipe_obj size */
|
||||
int tx_buffer_size; /*!< USB transport/out pipe_obj size */
|
||||
usb_ep_desc_t *bulk_in_ep; /*!< USB CDC bulk in endpoint descriptor, set NULL if using default param */
|
||||
usb_ep_desc_t *bulk_out_ep; /*!< USB CDC bulk out endpoint descriptor, set NULL if using default param */
|
||||
usbh_cdc_cb_t conn_callback; /*!< USB connect calllback, set NULL if not use */
|
||||
usbh_cdc_cb_t disconn_callback; /*!< USB disconnect calllback, usb cdc driver reset to connect waitting after disconnect, set NULL if not use */
|
||||
usbh_cdc_cb_t rx_callback; /*!< packet receive callback, set NULL if not use */
|
||||
void *conn_callback_arg; /*!< USB connect calllback args, set NULL if not use */
|
||||
void *disconn_callback_arg; /*!< USB disconnect calllback args, set NULL if not use */
|
||||
void *rx_callback_arg; /*!< packet receive callback args, set NULL if not use */
|
||||
} usbh_cdc_config_t;
|
||||
|
||||
/**
|
||||
* @brief Install USB CDC driver.
|
||||
*
|
||||
* @param config USB Host CDC configs
|
||||
* @return
|
||||
* ESP_ERR_INVALID_STATE driver has been installed
|
||||
* ESP_ERR_INVALID_ARG args not supported
|
||||
* ESP_FAIL driver install failed
|
||||
* ESP_OK driver install succeed
|
||||
*/
|
||||
esp_err_t usbh_cdc_driver_install(const usbh_cdc_config_t *config);
|
||||
|
||||
/**
|
||||
* @brief Uninstall USB driver.
|
||||
*
|
||||
* @return
|
||||
* ESP_ERR_INVALID_STATE driver not installed
|
||||
* ESP_OK start succeed
|
||||
*/
|
||||
esp_err_t usbh_cdc_driver_delete(void);
|
||||
|
||||
/**
|
||||
* @brief Waitting until CDC device connect
|
||||
*
|
||||
* @param ticks_to_wait Wait timeout value, count in RTOS ticks
|
||||
* @return
|
||||
* ESP_ERR_INVALID_STATE driver not installed
|
||||
* ESP_ERR_TIMEOUT wait timeout
|
||||
* ESP_OK device connect succeed
|
||||
*/
|
||||
esp_err_t usbh_cdc_wait_connect(TickType_t ticks_to_wait);
|
||||
|
||||
/**
|
||||
* @brief Send data to connected USB device from a given buffer and length,
|
||||
* this function will return after copying all the data to tx ring buffer.
|
||||
*
|
||||
* @param buf data buffer address
|
||||
* @param length data length to send
|
||||
* @return int The number of bytes pushed to the tx buffer
|
||||
*/
|
||||
int usbh_cdc_write_bytes(const uint8_t *buf, size_t length);
|
||||
|
||||
/**
|
||||
* @brief Get USB receive ring buffer cached data length.
|
||||
*
|
||||
* @param size
|
||||
* @return
|
||||
* ESP_ERR_INVALID_STATE cdc not configured, or not running
|
||||
* ESP_ERR_INVALID_ARG args not supported
|
||||
* ESP_FAIL start failed
|
||||
* ESP_OK start succeed
|
||||
*/
|
||||
esp_err_t usbh_cdc_get_buffered_data_len(size_t *size);
|
||||
|
||||
/**
|
||||
* @brief Read data bytes from USB receive/in buffer.
|
||||
*
|
||||
* @param buf data buffer address
|
||||
* @param length data length to read
|
||||
* @param ticks_to_wait sTimeout, count in RTOS ticks
|
||||
* @return int The number of bytes read from USB FIFO
|
||||
*/
|
||||
int usbh_cdc_read_bytes(uint8_t *buf, size_t length, TickType_t ticks_to_wait);
|
||||
|
||||
/**
|
||||
* @brief print internal memory usage for debug
|
||||
* @return void
|
||||
*/
|
||||
void usbh_cdc_print_buffer_msg(void);
|
||||
118
app/drivers/data_port/usb-host/msc/diskio_usb.c
Executable file
118
app/drivers/data_port/usb-host/msc/diskio_usb.c
Executable file
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "diskio_impl.h"
|
||||
#include "ffconf.h"
|
||||
#include "ff.h"
|
||||
#include "esp_log.h"
|
||||
#include "private_include/diskio_usb.h"
|
||||
#include "private_include/msc_scsi_bot.h"
|
||||
#include "private_include/msc_common.h"
|
||||
#include "usb/usb_types_stack.h"
|
||||
|
||||
static usb_disk_t *s_disks[FF_VOLUMES] = { NULL };
|
||||
|
||||
static const char *TAG = "diskio_usb";
|
||||
|
||||
static DSTATUS usb_disk_initialize (BYTE pdrv)
|
||||
{
|
||||
return RES_OK;
|
||||
}
|
||||
|
||||
static DSTATUS usb_disk_status (BYTE pdrv)
|
||||
{
|
||||
return RES_OK;
|
||||
}
|
||||
|
||||
static DRESULT usb_disk_read (BYTE pdrv, BYTE *buff, DWORD sector, UINT count)
|
||||
{
|
||||
assert(pdrv < FF_VOLUMES);
|
||||
assert(s_disks[pdrv]);
|
||||
|
||||
esp_err_t err;
|
||||
usb_disk_t *disk = s_disks[pdrv];
|
||||
size_t sector_size = disk->block_size;
|
||||
msc_device_t *dev = __containerof(disk, msc_device_t, disk);
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
err = scsi_cmd_read10(dev, &buff[i * sector_size], sector + i, 1, sector_size);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "scsi_cmd_read10 failed (%d)", err);
|
||||
return RES_ERROR;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return RES_OK;
|
||||
}
|
||||
|
||||
static DRESULT usb_disk_write (BYTE pdrv, const BYTE *buff, DWORD sector, UINT count)
|
||||
{
|
||||
assert(pdrv < FF_VOLUMES);
|
||||
assert(s_disks[pdrv]);
|
||||
|
||||
esp_err_t err;
|
||||
usb_disk_t *disk = s_disks[pdrv];
|
||||
size_t sector_size = disk->block_size;
|
||||
msc_device_t *dev = __containerof(disk, msc_device_t, disk);
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
err = scsi_cmd_write10(dev, &buff[i * sector_size], sector + i, 1, sector_size);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "scsi_cmd_write10 failed (%d)", err);
|
||||
return RES_ERROR;
|
||||
}
|
||||
|
||||
}
|
||||
return RES_OK;
|
||||
}
|
||||
|
||||
static DRESULT usb_disk_ioctl (BYTE pdrv, BYTE cmd, void *buff)
|
||||
{
|
||||
assert(pdrv < FF_VOLUMES);
|
||||
assert(s_disks[pdrv]);
|
||||
|
||||
usb_disk_t *disk = s_disks[pdrv];
|
||||
|
||||
switch (cmd) {
|
||||
case CTRL_SYNC:
|
||||
return RES_OK;
|
||||
case GET_SECTOR_COUNT:
|
||||
*((DWORD *) buff) = disk->block_count;
|
||||
return RES_OK;
|
||||
case GET_SECTOR_SIZE:
|
||||
*((WORD *) buff) = disk->block_size;
|
||||
return RES_OK;
|
||||
case GET_BLOCK_SIZE:
|
||||
return RES_ERROR;
|
||||
}
|
||||
return RES_ERROR;
|
||||
}
|
||||
|
||||
void ff_diskio_register_msc(BYTE pdrv, usb_disk_t *disk)
|
||||
{
|
||||
assert(pdrv < FF_VOLUMES);
|
||||
|
||||
static const ff_diskio_impl_t usb_disk_impl = {
|
||||
.init = &usb_disk_initialize,
|
||||
.status = &usb_disk_status,
|
||||
.read = &usb_disk_read,
|
||||
.write = &usb_disk_write,
|
||||
.ioctl = &usb_disk_ioctl
|
||||
};
|
||||
s_disks[pdrv] = disk;
|
||||
ff_diskio_register(pdrv, &usb_disk_impl);
|
||||
}
|
||||
|
||||
BYTE ff_diskio_get_pdrv_disk(const usb_disk_t *disk)
|
||||
{
|
||||
for (int i = 0; i < FF_VOLUMES; i++) {
|
||||
if (disk == s_disks[i]) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return 0xff;
|
||||
}
|
||||
308
app/drivers/data_port/usb-host/msc/include/msc_host.h
Executable file
308
app/drivers/data_port/usb-host/msc/include/msc_host.h
Executable file
@@ -0,0 +1,308 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <wchar.h>
|
||||
#include <stdint.h>
|
||||
#include "esp_err.h"
|
||||
#include <freertos/FreeRTOS.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define ESP_ERR_MSC_HOST_BASE 0x1700 /*!< MSC host error code base */
|
||||
#define ESP_ERR_MSC_MOUNT_FAILED (ESP_ERR_MSC_HOST_BASE + 1) /*!< Failed to mount storage */
|
||||
#define ESP_ERR_MSC_FORMAT_FAILED (ESP_ERR_MSC_HOST_BASE + 2) /*!< Failed to format storage */
|
||||
#define ESP_ERR_MSC_INTERNAL (ESP_ERR_MSC_HOST_BASE + 3) /*!< MSC host internal error */
|
||||
|
||||
#define MSC_STR_DESC_SIZE 32
|
||||
|
||||
typedef struct msc_host_device *msc_host_device_handle_t; /**< Handle to a Mass Storage Device */
|
||||
|
||||
/**
|
||||
* @brief USB Mass Storage event containing event type and associated device handle.
|
||||
*/
|
||||
typedef struct {
|
||||
enum {
|
||||
MSC_DEVICE_CONNECTED, /**< MSC device has been connected to the system.*/
|
||||
MSC_DEVICE_DISCONNECTED, /**< MSC device has been disconnected from the system.*/
|
||||
|
||||
CDC_DEVICE_CONNECTED,
|
||||
CDC_DEVICE_DISCONNECTED,
|
||||
|
||||
DFU_DEVICE_CONNECTED,
|
||||
DFU_DEVICE_DISCONNECTED,
|
||||
} event;
|
||||
union {
|
||||
uint8_t address; /**< Address of connected MSC device.*/
|
||||
msc_host_device_handle_t handle; /**< MSC device handle to disconnected device.*/
|
||||
} device;
|
||||
|
||||
uint16_t vendor_id;
|
||||
uint16_t product_id;
|
||||
} msc_host_event_t;
|
||||
|
||||
/**
|
||||
* @brief USB Mass Storage event callback.
|
||||
*
|
||||
* @param[in] event mass storage event
|
||||
*/
|
||||
typedef void (*msc_host_event_cb_t)(const msc_host_event_t *event, void *arg);
|
||||
|
||||
/**
|
||||
* @brief MSC configuration structure.
|
||||
*/
|
||||
typedef struct {
|
||||
bool create_backround_task; /**< When set to true, background task handling usb events is created.
|
||||
Otherwise user has to periodically call msc_host_handle_events function */
|
||||
size_t task_priority; /**< Task priority of crated background task */
|
||||
size_t stack_size; /**< Stack size of crated background task */
|
||||
BaseType_t core_id; /**< Select core on which background task will run or tskNO_AFFINITY */
|
||||
msc_host_event_cb_t callback; /**< Callback invoked when MSC event occurs. Must not be NULL. */
|
||||
void *callback_arg; /**< User provided argument passed to callback */
|
||||
} msc_host_driver_config_t;
|
||||
|
||||
/**
|
||||
* @brief MSC device info.
|
||||
*/
|
||||
typedef struct {
|
||||
uint32_t sector_count;
|
||||
uint32_t sector_size;
|
||||
uint16_t idProduct;
|
||||
uint16_t idVendor;
|
||||
wchar_t iManufacturer[MSC_STR_DESC_SIZE];
|
||||
wchar_t iProduct[MSC_STR_DESC_SIZE];
|
||||
wchar_t iSerialNumber[MSC_STR_DESC_SIZE];
|
||||
} msc_host_device_info_t;
|
||||
|
||||
//////cdc
|
||||
|
||||
typedef struct cdc_dev_s *cdc_acm_dev_hdl_t;
|
||||
|
||||
/**
|
||||
* @brief Line Coding structure
|
||||
* @see Table 17, USB CDC-PSTN specification rev. 1.2
|
||||
*/
|
||||
typedef struct {
|
||||
uint32_t dwDTERate; // in bits per second
|
||||
uint8_t bCharFormat; // 0: 1 stopbit, 1: 1.5 stopbits, 2: 2 stopbits
|
||||
uint8_t bParityType; // 0: None, 1: Odd, 2: Even, 3: Mark, 4: Space
|
||||
uint8_t bDataBits; // 5, 6, 7, 8 or 16
|
||||
} __attribute__((packed)) cdc_acm_line_coding_t;
|
||||
|
||||
/**
|
||||
* @brief UART State Bitmap
|
||||
* @see Table 31, USB CDC-PSTN specification rev. 1.2
|
||||
*/
|
||||
typedef union {
|
||||
struct {
|
||||
uint16_t bRxCarrier : 1; // State of receiver carrier detection mechanism of device. This signal corresponds to V.24 signal 109 and RS-232 signal DCD.
|
||||
uint16_t bTxCarrier : 1; // State of transmission carrier. This signal corresponds to V.24 signal 106 and RS-232 signal DSR.
|
||||
uint16_t bBreak : 1; // State of break detection mechanism of the device.
|
||||
uint16_t bRingSignal : 1; // State of ring signal detection of the device.
|
||||
uint16_t bFraming : 1; // A framing error has occurred.
|
||||
uint16_t bParity : 1; // A parity error has occurred.
|
||||
uint16_t bOverRun : 1; // Received data has been discarded due to overrun in the device.
|
||||
uint16_t reserved : 9;
|
||||
};
|
||||
uint16_t val;
|
||||
} cdc_acm_uart_state_t;
|
||||
|
||||
/**
|
||||
* @brief CDC-ACM Device Event types to upper layer
|
||||
*
|
||||
*/
|
||||
typedef enum {
|
||||
CDC_ACM_HOST_ERROR,
|
||||
CDC_ACM_HOST_SERIAL_STATE,
|
||||
CDC_ACM_HOST_NETWORK_CONNECTION,
|
||||
CDC_ACM_HOST_DEVICE_DISCONNECTED
|
||||
} cdc_acm_host_dev_event_t;
|
||||
|
||||
/**
|
||||
* @brief CDC-ACM Device Event data structure
|
||||
*
|
||||
*/
|
||||
typedef struct {
|
||||
cdc_acm_host_dev_event_t type;
|
||||
union {
|
||||
int error; // Error code from USB Host
|
||||
cdc_acm_uart_state_t serial_state; // Serial (UART) state
|
||||
bool network_connected; // Network connection event
|
||||
} data;
|
||||
} cdc_acm_host_dev_event_data_t;
|
||||
|
||||
/**
|
||||
* @brief Data receive callback type
|
||||
*/
|
||||
typedef void (*cdc_acm_data_callback_t)(uint8_t *data, size_t data_len, void *user_arg);
|
||||
|
||||
/**
|
||||
* @brief Device event callback type
|
||||
* @see cdc_acm_host_dev_event_t
|
||||
*/
|
||||
typedef void (*cdc_acm_host_dev_callback_t)(cdc_acm_dev_hdl_t cdc_hdl, const cdc_acm_host_dev_event_data_t *event, void *user_ctx);
|
||||
|
||||
/**
|
||||
* @brief Configuration structure of USB Host CDC-ACM driver
|
||||
*
|
||||
*/
|
||||
typedef struct {
|
||||
size_t driver_task_stack_size; /**< Stack size of the driver's task */
|
||||
unsigned driver_task_priority; /**< Priority of the driver's task */
|
||||
int xCoreID; /**< Core affinity of the driver's task */
|
||||
} cdc_acm_host_driver_config_t;
|
||||
|
||||
/**
|
||||
* @brief Configuration structure of CDC-ACM device
|
||||
*
|
||||
*/
|
||||
typedef struct {
|
||||
uint32_t connection_timeout_ms; /**< Timeout for USB device connection in [ms] */
|
||||
size_t out_buffer_size; /**< Maximum size of USB bulk out transfer, set to 0 for read-only devices */
|
||||
cdc_acm_host_dev_callback_t event_cb; /**< Device's event callback function. Can be NULL */
|
||||
cdc_acm_data_callback_t data_cb; /**< Device's data RX callback function. Can be NULL for write-only devices */
|
||||
void *user_arg; /**< User's argument that will be passed to the callbacks */
|
||||
} cdc_acm_host_device_config_t;
|
||||
|
||||
/////end cdc
|
||||
|
||||
// stm32 dfu update
|
||||
|
||||
#define STM32_DFU_REQUEST_DETACH 0x00
|
||||
#define STM32_DFU_REQUEST_DNLOAD 0x01
|
||||
#define STM32_DFU_REQUEST_UPLOAD 0x02
|
||||
#define STM32_DFU_REQUEST_GETSTATUS 0x03
|
||||
#define STM32_DFU_REQUEST_CLRSTATUS 0x04
|
||||
#define STM32_DFU_REQUEST_GETSTATE 0x05
|
||||
#define STM32_DFU_REQUEST_ABORT 0x06
|
||||
|
||||
#define STM32_DFU_STATE_APP_IDLE 0
|
||||
#define STM32_DFU_STATE_APP_DETACH 1
|
||||
#define STM32_DFU_STATE_DFU_IDLE 2
|
||||
#define STM32_DFU_STATE_DNLOAD_SYNC 3
|
||||
#define STM32_DFU_STATE_DNBUSY 4
|
||||
#define STM32_DFU_STATE_DNLOAD_IDLE 5
|
||||
#define STM32_DFU_STATE_MAINFES_SYNC 6
|
||||
#define STM32_DFU_STATE_MAINFEST 7
|
||||
#define STM32_DFU_STATE_MAINFEST_WAIT_RESET 8
|
||||
#define STM32_DFU_STATE_UPLOAD_IDLE 9
|
||||
#define STM32_DFU_STATE_ERROR 10
|
||||
|
||||
////
|
||||
|
||||
/**
|
||||
* @brief Install USB Host Mass Storage Class driver
|
||||
*
|
||||
* @param[in] config configuration structure MSC to create
|
||||
* @return esp_err_r
|
||||
*/
|
||||
esp_err_t msc_host_install(const msc_host_driver_config_t *config);
|
||||
|
||||
/**
|
||||
* @brief Uninstall Mass Storage Class driver
|
||||
* @return esp_err_t
|
||||
*/
|
||||
esp_err_t msc_host_uninstall(void);
|
||||
|
||||
/**
|
||||
* @brief Initialization of MSC device.
|
||||
*
|
||||
* @param[in] device_address Device address obtained from MSC callback provided upon connection and enumeration
|
||||
* @param[out] device Mass storage device handle to be used for subsequent calls.
|
||||
* @return esp_err_t
|
||||
*/
|
||||
esp_err_t msc_host_install_device(uint8_t device_address, msc_host_device_handle_t *device);
|
||||
|
||||
/**
|
||||
* @brief Deinitialization of MSC device.
|
||||
*
|
||||
* @param[in] device Device handle obtained from msc_host_install_device function
|
||||
* @return esp_err_t
|
||||
*/
|
||||
esp_err_t msc_host_uninstall_device(msc_host_device_handle_t device);
|
||||
|
||||
/**
|
||||
* @brief Helper function for reading sector from mass storage device.
|
||||
*
|
||||
* @warning This call is not thread safe and should not be combined
|
||||
* with accesses to storage through file system.
|
||||
*
|
||||
* @note Provided sector and size cannot exceed
|
||||
* sector_count and sector_size obtained from msc_host_device_info_t
|
||||
*
|
||||
* @param[in] device Device handle
|
||||
* @param[in] sector Number of sector to be read
|
||||
* @param[out] data Buffer into which data will be written
|
||||
* @param[in] size Number of bytes to be read
|
||||
* @return esp_err_t
|
||||
*/
|
||||
esp_err_t msc_host_read_sector(msc_host_device_handle_t device, size_t sector, void *data, size_t size);
|
||||
|
||||
/**
|
||||
* @brief Helper function for writing sector to mass storage device.
|
||||
*
|
||||
* @warning This call is not thread safe and should not be combined
|
||||
* with accesses to storare through file system.
|
||||
*
|
||||
* @note Provided sector and size cannot exceed
|
||||
* sector_count and sector_size obtained from msc_host_device_info_t
|
||||
*
|
||||
* @param[in] device Device handle
|
||||
* @param[in] sector Number of sector to be read
|
||||
* @param[in] data Data to be written to the sector
|
||||
* @param[in] size Number of bytes to be written
|
||||
* @return esp_err_t
|
||||
*/
|
||||
esp_err_t msc_host_write_sector(msc_host_device_handle_t device, size_t sector, const void *data, size_t size);
|
||||
|
||||
/**
|
||||
* @brief Handle MSC HOST events.
|
||||
*
|
||||
* @param[in] timeout_ms Timeout in miliseconds
|
||||
* @return esp_err_t
|
||||
*/
|
||||
esp_err_t msc_host_handle_events(uint32_t timeout_ms);
|
||||
|
||||
/**
|
||||
* @brief Gets devices information.
|
||||
*
|
||||
* @warning This call is not thread safe and should not be combined
|
||||
* with accesses to storare through file system.
|
||||
*
|
||||
* @param[in] device Handle to device
|
||||
* @param[out] info Structure to be populated with device info
|
||||
* @return esp_err_t
|
||||
*/
|
||||
esp_err_t msc_host_get_device_info(msc_host_device_handle_t device, msc_host_device_info_t *info);
|
||||
|
||||
/**
|
||||
* @brief Print configuration descriptor.
|
||||
*
|
||||
* @param[in] device Handle of MSC device
|
||||
* @return esp_err_t
|
||||
*/
|
||||
esp_err_t msc_host_print_descriptors(msc_host_device_handle_t device);
|
||||
|
||||
esp_err_t cdc_host_open(uint16_t vid, uint16_t pid, uint8_t interface_idx, const cdc_acm_host_device_config_t *dev_config, cdc_acm_dev_hdl_t *cdc_hdl_ret);
|
||||
void cdc_host_desc_print(cdc_acm_dev_hdl_t cdc_hdl);
|
||||
void cdc_submit_transfer_in(cdc_acm_dev_hdl_t cdc_hdl);
|
||||
int cdc_write_bytes(const uint8_t *buf, size_t length);
|
||||
esp_err_t cdc_host_close(cdc_acm_dev_hdl_t cdc_hdl);
|
||||
|
||||
esp_err_t dfu_host_open(uint16_t vid, uint16_t pid, uint8_t interface_idx, const cdc_acm_host_device_config_t *dev_config, cdc_acm_dev_hdl_t *cdc_hdl_ret);
|
||||
esp_err_t dfu_host_close(cdc_acm_dev_hdl_t cdc_hdl);
|
||||
|
||||
esp_err_t usbh_stm32_get_status_ex(uint8_t *out_result_data /*[6]*/, uint16_t timeout);
|
||||
esp_err_t usbh_stm32_get_status(uint8_t *out_result_data /*[6]*/);
|
||||
void usbh_stm32_get_chipinfo(char *descriptors, uint8_t count, uint8_t *actual_desc_count);
|
||||
uint16_t usbh_stm32_get_transfer_block_size();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif //__cplusplus
|
||||
44
app/drivers/data_port/usb-host/msc/include/msc_host_vfs.h
Executable file
44
app/drivers/data_port/usb-host/msc/include/msc_host_vfs.h
Executable file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "esp_vfs_fat.h"
|
||||
#include "msc_host.h"
|
||||
#include "esp_err.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct msc_host_vfs *msc_host_vfs_handle_t; /**< VFS handle to attached Mass Storage device */
|
||||
|
||||
/**
|
||||
* @brief Register MSC device to Virtual filesystem.
|
||||
*
|
||||
* @param[in] device Device handle obtained from MSC callback provided upon initialization
|
||||
* @param[in] base_path Base VFS path to be used to access file storage
|
||||
* @param[in] mount_config Mount configuration.
|
||||
* @param[out] vfs_handle Handle to MSC device associated with registered VFS
|
||||
* @return esp_err_t
|
||||
*/
|
||||
esp_err_t msc_host_vfs_register(msc_host_device_handle_t device,
|
||||
const char *base_path,
|
||||
const esp_vfs_fat_mount_config_t *mount_config,
|
||||
msc_host_vfs_handle_t *vfs_handle);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Unregister MSC device from Virtual filesystem.
|
||||
*
|
||||
* @param[in] vfs_handle VFS handle obtained from MSC callback provided upon initialization
|
||||
* @return esp_err_t
|
||||
*/
|
||||
esp_err_t msc_host_vfs_unregister(msc_host_vfs_handle_t vfs_handle);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
2701
app/drivers/data_port/usb-host/msc/msc_host.c
Executable file
2701
app/drivers/data_port/usb-host/msc/msc_host.c
Executable file
File diff suppressed because it is too large
Load Diff
125
app/drivers/data_port/usb-host/msc/msc_host_vfs.c
Executable file
125
app/drivers/data_port/usb-host/msc/msc_host_vfs.c
Executable file
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/param.h>
|
||||
#include "private_include/msc_common.h"
|
||||
#include "include/msc_host_vfs.h"
|
||||
#include "diskio_impl.h"
|
||||
#include "ffconf.h"
|
||||
#include "ff.h"
|
||||
#include "vfs_fat_internal.h"
|
||||
|
||||
#define DRIVE_STR_LEN 3
|
||||
|
||||
typedef struct msc_host_vfs {
|
||||
char drive[DRIVE_STR_LEN];
|
||||
char *base_path;
|
||||
uint8_t pdrv;
|
||||
} msc_host_vfs_t;
|
||||
|
||||
static const char *TAG = "MSC VFS";
|
||||
|
||||
static esp_err_t msc_format_storage(size_t block_size, size_t allocation_size, const char *drv, const esp_vfs_fat_mount_config_t *mount_config)
|
||||
{
|
||||
void *workbuf = NULL;
|
||||
const size_t workbuf_size = 4096;
|
||||
|
||||
MSC_RETURN_ON_FALSE( workbuf = ff_memalloc(workbuf_size), ESP_ERR_NO_MEM );
|
||||
|
||||
size_t alloc_unit_size = esp_vfs_fat_get_allocation_unit_size(CONFIG_WL_SECTOR_SIZE, mount_config->allocation_unit_size);
|
||||
ESP_LOGI(TAG, "Formatting FATFS partition, allocation unit size=%d", alloc_unit_size);
|
||||
const MKFS_PARM opt = {(BYTE)(FM_ANY | FM_SFD), 0, 0, 0, alloc_unit_size};
|
||||
FRESULT err = f_mkfs(drv, &opt, workbuf, workbuf_size);
|
||||
if (err) {
|
||||
ESP_LOGE(TAG, "Formatting failed with error: %d", err);
|
||||
free(workbuf);
|
||||
return ESP_ERR_MSC_FORMAT_FAILED;
|
||||
}
|
||||
|
||||
free(workbuf);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void dealloc_msc_vfs(msc_host_vfs_t *vfs)
|
||||
{
|
||||
free(vfs->base_path);
|
||||
free(vfs);
|
||||
}
|
||||
|
||||
esp_err_t msc_host_vfs_register(msc_host_device_handle_t device,
|
||||
const char *base_path,
|
||||
const esp_vfs_fat_mount_config_t *mount_config,
|
||||
msc_host_vfs_handle_t *vfs_handle)
|
||||
{
|
||||
MSC_RETURN_ON_INVALID_ARG(device);
|
||||
MSC_RETURN_ON_INVALID_ARG(base_path);
|
||||
MSC_RETURN_ON_INVALID_ARG(mount_config);
|
||||
MSC_RETURN_ON_INVALID_ARG(vfs_handle);
|
||||
|
||||
FATFS *fs = NULL;
|
||||
BYTE pdrv;
|
||||
bool diskio_registered = false;
|
||||
esp_err_t ret = ESP_ERR_MSC_MOUNT_FAILED;
|
||||
msc_device_t *dev = (msc_device_t *)device;
|
||||
size_t block_size = dev->disk.block_size;
|
||||
size_t alloc_size = mount_config->allocation_unit_size;
|
||||
|
||||
msc_host_vfs_t *vfs = calloc(1, sizeof(msc_host_vfs_t));
|
||||
MSC_RETURN_ON_FALSE(vfs != NULL, ESP_ERR_NO_MEM);
|
||||
|
||||
MSC_GOTO_ON_ERROR( ff_diskio_get_drive(&pdrv) );
|
||||
|
||||
ff_diskio_register_msc(pdrv, &dev->disk);
|
||||
char drive[DRIVE_STR_LEN] = {(char)('0' + pdrv), ':', 0};
|
||||
diskio_registered = true;
|
||||
|
||||
strncpy(vfs->drive, drive, DRIVE_STR_LEN);
|
||||
MSC_GOTO_ON_FALSE( vfs->base_path = strdup(base_path), ESP_ERR_NO_MEM );
|
||||
vfs->pdrv = pdrv;
|
||||
|
||||
MSC_GOTO_ON_ERROR( esp_vfs_fat_register(base_path, drive, mount_config->max_files, &fs) );
|
||||
|
||||
FRESULT fresult = f_mount(fs, drive, 1);
|
||||
|
||||
if ( fresult != FR_OK) {
|
||||
if (mount_config->format_if_mount_failed &&
|
||||
(fresult == FR_NO_FILESYSTEM || fresult == FR_INT_ERR)) {
|
||||
MSC_GOTO_ON_ERROR( msc_format_storage(block_size, alloc_size, drive, mount_config) );
|
||||
MSC_GOTO_ON_FALSE( f_mount(fs, drive, 0) == FR_OK, ESP_ERR_MSC_MOUNT_FAILED );
|
||||
} else {
|
||||
goto fail;
|
||||
}
|
||||
}
|
||||
|
||||
*vfs_handle = vfs;
|
||||
return ESP_OK;
|
||||
|
||||
fail:
|
||||
if (diskio_registered) {
|
||||
ff_diskio_unregister(pdrv);
|
||||
}
|
||||
esp_vfs_fat_unregister_path(base_path);
|
||||
if(fs) {
|
||||
f_mount(NULL, drive, 0);
|
||||
}
|
||||
dealloc_msc_vfs(vfs);
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_err_t msc_host_vfs_unregister(msc_host_vfs_handle_t vfs_handle)
|
||||
{
|
||||
MSC_RETURN_ON_INVALID_ARG(vfs_handle);
|
||||
msc_host_vfs_t *vfs = (msc_host_vfs_t *)vfs_handle;
|
||||
|
||||
f_mount(NULL, vfs->drive, 0);
|
||||
ff_diskio_unregister(vfs->pdrv);
|
||||
esp_vfs_fat_unregister_path(vfs->base_path);
|
||||
dealloc_msc_vfs(vfs);
|
||||
return ESP_OK;
|
||||
}
|
||||
434
app/drivers/data_port/usb-host/msc/msc_scsi_bot.c
Executable file
434
app/drivers/data_port/usb-host/msc/msc_scsi_bot.c
Executable file
@@ -0,0 +1,434 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include "esp_log.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include "esp_check.h"
|
||||
#include "esp_log.h"
|
||||
#include "private_include/msc_common.h"
|
||||
#include "private_include/msc_scsi_bot.h"
|
||||
|
||||
#define TAG "USB_MSC_SCSI"
|
||||
|
||||
/* --------------------------- SCSI Definitions ----------------------------- */
|
||||
#define CMD_SENSE_VALID_BIT (1 << 7)
|
||||
#define SCSI_FLAG_DPO (1<<4)
|
||||
#define SCSI_FLAG_FUA (1<<3)
|
||||
|
||||
#define SCSI_CMD_FORMAT_UNIT 0x04
|
||||
#define SCSI_CMD_INQUIRY 0x12
|
||||
#define SCSI_CMD_MODE_SELECT 0x55
|
||||
#define SCSI_CMD_MODE_SENSE 0x5A
|
||||
#define SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL 0x1E
|
||||
#define SCSI_CMD_READ10 0x28
|
||||
#define SCSI_CMD_READ12 0xA8
|
||||
#define SCSI_CMD_READ_CAPACITY 0x25
|
||||
#define SCSI_CMD_READ_FORMAT_CAPACITIES 0x23
|
||||
#define SCSI_CMD_REQUEST_SENSE 0x03
|
||||
#define SCSI_CMD_REZERO 0x01
|
||||
#define SCSI_CMD_SEEK10 0x2B
|
||||
#define SCSI_CMD_SEND_DIAGNOSTIC 0x1D
|
||||
#define SCSI_CMD_START_STOP Unit 0x1B
|
||||
#define SCSI_CMD_TEST_UNIT_READY 0x00
|
||||
#define SCSI_CMD_VERIFY 0x2F
|
||||
#define SCSI_CMD_WRITE10 0x2A
|
||||
#define SCSI_CMD_WRITE12 0xAA
|
||||
#define SCSI_CMD_WRITE_AND_VERIFY 0x2E
|
||||
|
||||
#define IN_DIR CWB_FLAG_DIRECTION_IN
|
||||
#define OUT_DIR 0
|
||||
|
||||
#define INQUIRY_VID_SIZE 8
|
||||
#define INQUIRY_PID_SIZE 16
|
||||
#define INQUIRY_REV_SIZE 4
|
||||
|
||||
#define CBW_CMD_SIZE(cmd) (sizeof(cmd) - sizeof(msc_cbw_t))
|
||||
|
||||
#define CBW_BASE_INIT(dir, cbw_len, data_len) \
|
||||
.base = { \
|
||||
.signature = 0x43425355, \
|
||||
.tag = ++cbw_tag, \
|
||||
.flags = dir, \
|
||||
.lun = 0, \
|
||||
.data_length = data_len, \
|
||||
.cbw_length = cbw_len, \
|
||||
}
|
||||
|
||||
#define FEATURE_SELECTOR_ENDPOINT 0
|
||||
#define CSW_SIGNATURE 0x53425355
|
||||
#define CBW_SIZE 31
|
||||
|
||||
#define USB_MASS_REQ_INIT_RESET(ctrl_req_ptr, intf_num) ({ \
|
||||
(ctrl_req_ptr)->bmRequestType = USB_BM_REQUEST_TYPE_DIR_OUT | \
|
||||
USB_BM_REQUEST_TYPE_TYPE_CLASS | \
|
||||
USB_BM_REQUEST_TYPE_RECIP_INTERFACE; \
|
||||
(ctrl_req_ptr)->bRequest = 0xFF; \
|
||||
(ctrl_req_ptr)->wValue = 0; \
|
||||
(ctrl_req_ptr)->wIndex = (intf_num); \
|
||||
(ctrl_req_ptr)->wLength = 0; \
|
||||
})
|
||||
|
||||
#define USB_MASS_REQ_INIT_GET_MAX_LUN(ctrl_req_ptr, intf_num) ({ \
|
||||
(ctrl_req_ptr)->bmRequestType = USB_BM_REQUEST_TYPE_DIR_IN | \
|
||||
USB_BM_REQUEST_TYPE_TYPE_CLASS | \
|
||||
USB_BM_REQUEST_TYPE_RECIP_INTERFACE; \
|
||||
(ctrl_req_ptr)->bRequest = 0xFE; \
|
||||
(ctrl_req_ptr)->wValue = 0; \
|
||||
(ctrl_req_ptr)->wIndex = (intf_num); \
|
||||
(ctrl_req_ptr)->wLength = 1; \
|
||||
})
|
||||
|
||||
#define USB_SETUP_PACKET_INIT_CLEAR_FEATURE_EP(ctrl_req_ptr, ep_num) ({ \
|
||||
(ctrl_req_ptr)->bmRequestType = USB_BM_REQUEST_TYPE_DIR_OUT | \
|
||||
USB_BM_REQUEST_TYPE_TYPE_STANDARD | \
|
||||
USB_BM_REQUEST_TYPE_RECIP_ENDPOINT; \
|
||||
(ctrl_req_ptr)->bRequest = USB_B_REQUEST_CLEAR_FEATURE; \
|
||||
(ctrl_req_ptr)->wValue = FEATURE_SELECTOR_ENDPOINT; \
|
||||
(ctrl_req_ptr)->wIndex = (ep_num); \
|
||||
(ctrl_req_ptr)->wLength = 0; \
|
||||
})
|
||||
|
||||
#define CWB_FLAG_DIRECTION_IN (1<<7) // device -> host
|
||||
|
||||
/**
|
||||
* @brief Command Block Wrapper structure
|
||||
*/
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
uint32_t signature;
|
||||
uint32_t tag;
|
||||
uint32_t data_length;
|
||||
uint8_t flags;
|
||||
uint8_t lun;
|
||||
uint8_t cbw_length;
|
||||
} msc_cbw_t;
|
||||
|
||||
/**
|
||||
* @brief Command Status Wrapper structure
|
||||
*/
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
uint32_t signature;
|
||||
uint32_t tag;
|
||||
uint32_t dataResidue;
|
||||
uint8_t status;
|
||||
} msc_csw_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
msc_cbw_t base;
|
||||
uint8_t opcode;
|
||||
uint8_t flags;
|
||||
uint32_t address;
|
||||
uint8_t reserved1;
|
||||
uint16_t length;
|
||||
uint8_t reserved2[3];
|
||||
} cbw_read10_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
msc_cbw_t base;
|
||||
uint8_t opcode;
|
||||
uint8_t flags;
|
||||
uint32_t address;
|
||||
uint8_t reserved1;
|
||||
uint16_t length;
|
||||
uint8_t reserved2[1];
|
||||
} cbw_write10_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
msc_cbw_t base;
|
||||
uint8_t opcode;
|
||||
uint8_t flags;
|
||||
uint32_t address;
|
||||
uint8_t reserved[6];
|
||||
} cbw_read_capacity_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
uint32_t block_count;
|
||||
uint32_t block_size;
|
||||
} cbw_read_capacity_response_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
msc_cbw_t base;
|
||||
uint8_t opcode;
|
||||
uint8_t flags;
|
||||
uint8_t reserved[10];
|
||||
} cbw_unit_ready_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
msc_cbw_t base;
|
||||
uint8_t opcode;
|
||||
uint8_t flags;
|
||||
uint8_t reserved_0[2];
|
||||
uint8_t allocation_length;
|
||||
uint8_t reserved_1[7];
|
||||
} cbw_sense_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
uint8_t error_code;
|
||||
uint8_t reserved_0;
|
||||
uint8_t sense_key;
|
||||
uint32_t info;
|
||||
uint8_t sense_len;
|
||||
uint32_t reserved_1;
|
||||
uint8_t sense_code;
|
||||
uint8_t sense_code_qualifier;
|
||||
uint32_t reserved_2;
|
||||
} cbw_sense_response_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
msc_cbw_t base;
|
||||
uint8_t opcode;
|
||||
uint8_t flags;
|
||||
uint8_t page_code;
|
||||
uint8_t reserved_0;
|
||||
uint8_t allocation_length;
|
||||
uint8_t reserved_1[7];
|
||||
} cbw_inquiry_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
msc_cbw_t base;
|
||||
uint8_t opcode;
|
||||
uint8_t flags;
|
||||
uint8_t pc_page_code;
|
||||
uint8_t reserved_1[4];
|
||||
uint16_t parameter_list_length;
|
||||
uint8_t reserved_2[3];
|
||||
} mode_sense_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
uint8_t data[8];
|
||||
} mode_sense_response_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
msc_cbw_t base;
|
||||
uint8_t opcode;
|
||||
uint8_t flags;
|
||||
uint8_t reserved_1[2];
|
||||
uint8_t prevent;
|
||||
uint8_t reserved_2[7];
|
||||
} prevent_allow_medium_removal_t;
|
||||
|
||||
typedef struct __attribute__((packed))
|
||||
{
|
||||
uint8_t data[36];
|
||||
} cbw_inquiry_response_t;
|
||||
|
||||
// Unique number based on which MSC protocol pairs request and response
|
||||
static uint32_t cbw_tag;
|
||||
|
||||
static esp_err_t check_csw(msc_csw_t *csw, uint32_t tag)
|
||||
{
|
||||
bool csw_ok = csw->signature == CSW_SIGNATURE && csw->tag == tag &&
|
||||
csw->dataResidue == 0 && csw->status == 0;
|
||||
|
||||
if (!csw_ok) {
|
||||
ESP_LOGD(TAG, "CSW failed: status %d", csw->status);
|
||||
}
|
||||
|
||||
return csw_ok ? ESP_OK : ESP_FAIL;
|
||||
}
|
||||
|
||||
static esp_err_t clear_feature(msc_device_t *device, uint8_t endpoint)
|
||||
{
|
||||
usb_device_handle_t dev = device->handle;
|
||||
usb_transfer_t *xfer = device->xfer;
|
||||
|
||||
MSC_RETURN_ON_ERROR( usb_host_endpoint_clear(dev, endpoint) );
|
||||
USB_SETUP_PACKET_INIT_CLEAR_FEATURE_EP((usb_setup_packet_t *)xfer->data_buffer, endpoint);
|
||||
MSC_RETURN_ON_ERROR( msc_control_transfer(device, xfer, USB_SETUP_PACKET_SIZE) );
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t msc_mass_reset(msc_device_t *device)
|
||||
{
|
||||
usb_transfer_t *xfer = device->xfer;
|
||||
|
||||
USB_MASS_REQ_INIT_RESET((usb_setup_packet_t *)xfer->data_buffer, 0);
|
||||
MSC_RETURN_ON_ERROR( msc_control_transfer(device, xfer, USB_SETUP_PACKET_SIZE) );
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t msc_get_max_lun(msc_device_t *device, uint8_t *lun)
|
||||
{
|
||||
usb_transfer_t *xfer = device->xfer;
|
||||
|
||||
USB_MASS_REQ_INIT_GET_MAX_LUN((usb_setup_packet_t *)xfer->data_buffer, 0);
|
||||
MSC_RETURN_ON_ERROR( msc_control_transfer(device, xfer, USB_SETUP_PACKET_SIZE + 1) );
|
||||
|
||||
*lun = xfer->data_buffer[USB_SETUP_PACKET_SIZE];
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t bot_execute_command(msc_device_t *device, msc_cbw_t *cbw, void *data, size_t size)
|
||||
{
|
||||
msc_csw_t csw;
|
||||
msc_endpoint_t ep = (cbw->flags & CWB_FLAG_DIRECTION_IN) ? MSC_EP_IN : MSC_EP_OUT;
|
||||
|
||||
MSC_RETURN_ON_ERROR( msc_bulk_transfer(device, (uint8_t *)cbw, CBW_SIZE, MSC_EP_OUT) );
|
||||
|
||||
if (data) {
|
||||
MSC_RETURN_ON_ERROR( msc_bulk_transfer(device, (uint8_t *)data, size, ep) );
|
||||
}
|
||||
|
||||
esp_err_t err = msc_bulk_transfer(device, (uint8_t *)&csw, sizeof(msc_csw_t), MSC_EP_IN);
|
||||
|
||||
if (err == ESP_FAIL && device->transfer_status == USB_TRANSFER_STATUS_STALL) {
|
||||
ESP_RETURN_ON_ERROR( clear_feature(device, MSC_EP_IN), TAG, "Clear feature failed" );
|
||||
// Try to read csw again after clearing feature
|
||||
err = msc_bulk_transfer(device, (uint8_t *)&csw, sizeof(msc_csw_t), MSC_EP_IN);
|
||||
if (err) {
|
||||
ESP_RETURN_ON_ERROR( clear_feature(device, MSC_EP_IN), TAG, "Clear feature failed" );
|
||||
ESP_RETURN_ON_ERROR( msc_mass_reset(device), TAG, "Mass reset failed" );
|
||||
return ESP_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
MSC_RETURN_ON_ERROR(err);
|
||||
|
||||
return check_csw(&csw, cbw->tag);
|
||||
}
|
||||
|
||||
|
||||
esp_err_t scsi_cmd_read10(msc_device_t *device,
|
||||
uint8_t *data,
|
||||
uint32_t sector_address,
|
||||
uint32_t num_sectors,
|
||||
uint32_t sector_size)
|
||||
{
|
||||
cbw_read10_t cbw = {
|
||||
CBW_BASE_INIT(IN_DIR, CBW_CMD_SIZE(cbw_read10_t), num_sectors * sector_size),
|
||||
.opcode = SCSI_CMD_READ10,
|
||||
.flags = 0, // lun
|
||||
.address = __builtin_bswap32(sector_address),
|
||||
.length = __builtin_bswap16(num_sectors),
|
||||
};
|
||||
|
||||
return bot_execute_command(device, &cbw.base, data, num_sectors * sector_size);
|
||||
}
|
||||
|
||||
esp_err_t scsi_cmd_write10(msc_device_t *device,
|
||||
const uint8_t *data,
|
||||
uint32_t sector_address,
|
||||
uint32_t num_sectors,
|
||||
uint32_t sector_size)
|
||||
{
|
||||
cbw_write10_t cbw = {
|
||||
CBW_BASE_INIT(OUT_DIR, CBW_CMD_SIZE(cbw_write10_t), num_sectors * sector_size),
|
||||
.opcode = SCSI_CMD_WRITE10,
|
||||
.address = __builtin_bswap32(sector_address),
|
||||
.length = __builtin_bswap16(num_sectors),
|
||||
};
|
||||
|
||||
return bot_execute_command(device, &cbw.base, (void *)data, num_sectors * sector_size);
|
||||
}
|
||||
|
||||
esp_err_t scsi_cmd_read_capacity(msc_device_t *device, uint32_t *block_size, uint32_t *block_count)
|
||||
{
|
||||
cbw_read_capacity_response_t response;
|
||||
|
||||
cbw_read_capacity_t cbw = {
|
||||
CBW_BASE_INIT(IN_DIR, CBW_CMD_SIZE(cbw_read_capacity_t), sizeof(response)),
|
||||
.opcode = SCSI_CMD_READ_CAPACITY,
|
||||
};
|
||||
|
||||
MSC_RETURN_ON_ERROR( bot_execute_command(device, &cbw.base, &response, sizeof(response)) );
|
||||
|
||||
*block_count = __builtin_bswap32(response.block_count);
|
||||
*block_size = __builtin_bswap32(response.block_size);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t scsi_cmd_unit_ready(msc_device_t *device)
|
||||
{
|
||||
cbw_unit_ready_t cbw = {
|
||||
CBW_BASE_INIT(IN_DIR, CBW_CMD_SIZE(cbw_unit_ready_t), 0),
|
||||
.opcode = SCSI_CMD_TEST_UNIT_READY,
|
||||
};
|
||||
|
||||
return bot_execute_command(device, &cbw.base, NULL, 0);
|
||||
}
|
||||
|
||||
esp_err_t scsi_cmd_sense(msc_device_t *device, scsi_sense_data_t *sense)
|
||||
{
|
||||
cbw_sense_response_t response;
|
||||
|
||||
cbw_sense_t cbw = {
|
||||
CBW_BASE_INIT(IN_DIR, CBW_CMD_SIZE(cbw_sense_t), sizeof(response)),
|
||||
.opcode = SCSI_CMD_REQUEST_SENSE,
|
||||
.allocation_length = sizeof(response),
|
||||
};
|
||||
|
||||
MSC_RETURN_ON_ERROR( bot_execute_command(device, &cbw.base, &response, sizeof(response)) );
|
||||
|
||||
if (sense->key) {
|
||||
ESP_LOGD(TAG, "sense_key: 0x%02X, code: 0x%02X, qualifier: 0x%02X",
|
||||
response.sense_key, response.sense_code, response.sense_code_qualifier);
|
||||
}
|
||||
|
||||
sense->key = response.sense_key;
|
||||
sense->code = response.sense_code;
|
||||
sense->code_q = response.sense_code_qualifier;
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t scsi_cmd_inquiry(msc_device_t *device)
|
||||
{
|
||||
cbw_inquiry_response_t response = { 0 };
|
||||
|
||||
cbw_inquiry_t cbw = {
|
||||
CBW_BASE_INIT(IN_DIR, CBW_CMD_SIZE(cbw_inquiry_t), sizeof(response)),
|
||||
.opcode = SCSI_CMD_INQUIRY,
|
||||
.allocation_length = sizeof(response),
|
||||
};
|
||||
|
||||
return bot_execute_command(device, &cbw.base, &response, sizeof(response) );
|
||||
}
|
||||
|
||||
esp_err_t scsi_cmd_mode_sense(msc_device_t *device)
|
||||
{
|
||||
mode_sense_response_t response = { 0 };
|
||||
|
||||
mode_sense_t cbw = {
|
||||
CBW_BASE_INIT(IN_DIR, CBW_CMD_SIZE(mode_sense_t), sizeof(response)),
|
||||
.opcode = SCSI_CMD_MODE_SENSE,
|
||||
.pc_page_code = 0x3F,
|
||||
.parameter_list_length = sizeof(response),
|
||||
};
|
||||
|
||||
return bot_execute_command(device, &cbw.base, &response, sizeof(response) );
|
||||
}
|
||||
|
||||
esp_err_t scsi_cmd_prevent_removal(msc_device_t *device, bool prevent)
|
||||
{
|
||||
prevent_allow_medium_removal_t cbw = {
|
||||
CBW_BASE_INIT(OUT_DIR, CBW_CMD_SIZE(prevent_allow_medium_removal_t), 0),
|
||||
.opcode = SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL,
|
||||
.prevent = 1,
|
||||
};
|
||||
|
||||
return bot_execute_command(device, &cbw.base, NULL, 0);
|
||||
}
|
||||
39
app/drivers/data_port/usb-host/msc/private_include/diskio_usb.h
Executable file
39
app/drivers/data_port/usb-host/msc/private_include/diskio_usb.h
Executable file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Mass storage disk initialization structure
|
||||
*/
|
||||
typedef struct {
|
||||
uint32_t block_size; /**< Block size */
|
||||
uint32_t block_count; /**< Block count */
|
||||
} usb_disk_t;
|
||||
|
||||
/**
|
||||
* @brief Register mass storage disk to fat file system
|
||||
*
|
||||
* @param[in] pdrv Number of free drive obtained from ff_diskio_get_drive() function
|
||||
* @param[in] disk usb_disk_t structure
|
||||
*/
|
||||
void ff_diskio_register_msc(uint8_t pdrv, usb_disk_t *disk);
|
||||
|
||||
/**
|
||||
* @brief Obtains number of drive assigned to usb disk upon calling ff_diskio_register_msc()
|
||||
*
|
||||
* @param[in] disk usb_disk_t structure
|
||||
* @return Drive number
|
||||
*/
|
||||
uint8_t ff_diskio_get_pdrv_disk(const usb_disk_t *disk);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif //__cplusplus
|
||||
61
app/drivers/data_port/usb-host/msc/private_include/msc_common.h
Executable file
61
app/drivers/data_port/usb-host/msc/private_include/msc_common.h
Executable file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <sys/queue.h>
|
||||
#include "esp_err.h"
|
||||
#include "esp_check.h"
|
||||
#include "diskio_usb.h"
|
||||
#include "usb/usb_host.h"
|
||||
#include "usb/usb_types_stack.h"
|
||||
#include "freertos/semphr.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
MSC_EP_OUT,
|
||||
MSC_EP_IN
|
||||
} msc_endpoint_t;
|
||||
|
||||
typedef struct {
|
||||
uint16_t bulk_in_mps;
|
||||
uint8_t bulk_in_ep;
|
||||
uint8_t bulk_out_ep;
|
||||
uint8_t iface_num;
|
||||
} msc_config_t;
|
||||
|
||||
typedef struct msc_host_device {
|
||||
STAILQ_ENTRY(msc_host_device) tailq_entry;
|
||||
usb_transfer_status_t transfer_status;
|
||||
SemaphoreHandle_t transfer_done;
|
||||
usb_device_handle_t handle;
|
||||
usb_transfer_t *xfer;
|
||||
msc_config_t config;
|
||||
usb_disk_t disk;
|
||||
} msc_device_t;
|
||||
|
||||
esp_err_t msc_bulk_transfer(msc_device_t *device_handle, uint8_t *data, size_t size, msc_endpoint_t ep);
|
||||
|
||||
esp_err_t msc_control_transfer(msc_device_t *device_handle, usb_transfer_t *xfer, size_t len);
|
||||
|
||||
#define MSC_GOTO_ON_ERROR(exp) ESP_GOTO_ON_ERROR(exp, fail, TAG, "")
|
||||
|
||||
#define MSC_GOTO_ON_FALSE(exp, err) ESP_GOTO_ON_FALSE( (exp), err, fail, TAG, "" )
|
||||
|
||||
#define MSC_RETURN_ON_ERROR(exp) ESP_RETURN_ON_ERROR((exp), TAG, "")
|
||||
|
||||
#define MSC_RETURN_ON_FALSE(exp, err) ESP_RETURN_ON_FALSE( (exp), (err), TAG, "")
|
||||
|
||||
#define MSC_RETURN_ON_INVALID_ARG(exp) ESP_RETURN_ON_FALSE((exp) != NULL, ESP_ERR_INVALID_ARG, TAG, "")
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
56
app/drivers/data_port/usb-host/msc/private_include/msc_scsi_bot.h
Executable file
56
app/drivers/data_port/usb-host/msc/private_include/msc_scsi_bot.h
Executable file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include "esp_err.h"
|
||||
#include "msc_common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
uint8_t key;
|
||||
uint8_t code;
|
||||
uint8_t code_q;
|
||||
} scsi_sense_data_t;
|
||||
|
||||
esp_err_t scsi_cmd_read10(msc_device_t *device,
|
||||
uint8_t *data,
|
||||
uint32_t sector_address,
|
||||
uint32_t num_sectors,
|
||||
uint32_t sector_size);
|
||||
|
||||
esp_err_t scsi_cmd_write10(msc_device_t *device,
|
||||
const uint8_t *data,
|
||||
uint32_t sector_address,
|
||||
uint32_t num_sectors,
|
||||
uint32_t sector_size);
|
||||
|
||||
esp_err_t scsi_cmd_read_capacity(msc_device_t *device,
|
||||
uint32_t *block_size,
|
||||
uint32_t *block_count);
|
||||
|
||||
esp_err_t scsi_cmd_sense(msc_device_t *device, scsi_sense_data_t *sense);
|
||||
|
||||
esp_err_t scsi_cmd_unit_ready(msc_device_t *device);
|
||||
|
||||
esp_err_t scsi_cmd_inquiry(msc_device_t *device);
|
||||
|
||||
esp_err_t scsi_cmd_prevent_removal(msc_device_t *device, bool prevent);
|
||||
|
||||
esp_err_t scsi_cmd_mode_sense(msc_device_t *device);
|
||||
|
||||
esp_err_t msc_mass_reset(msc_device_t *device);
|
||||
|
||||
esp_err_t msc_get_max_lun(msc_device_t *device, uint8_t *lun);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
217
app/drivers/data_port/usb-host/usb_types_cdc.h
Normal file
217
app/drivers/data_port/usb-host/usb_types_cdc.h
Normal file
@@ -0,0 +1,217 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <inttypes.h>
|
||||
|
||||
/**
|
||||
* @brief USB CDC Descriptor Subtypes
|
||||
*
|
||||
* @see Table 13, USB CDC specification rev. 1.2
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
CDC_DESC_SUBTYPE_HEADER = 0x00, // Header Functional Descriptor
|
||||
CDC_DESC_SUBTYPE_CALL = 0x01, // Call Management Functional Descriptor
|
||||
CDC_DESC_SUBTYPE_ACM = 0x02, // Abstract Control Management Functional Descriptor
|
||||
CDC_DESC_SUBTYPE_DLM = 0x03, // Direct Line Management Functional Descriptor
|
||||
CDC_DESC_SUBTYPE_TEL_RINGER = 0x04, // Telephone Ringer Functional Descriptor
|
||||
CDC_DESC_SUBTYPE_TEL_CLSR = 0x05, // Telephone Call and Line State Reporting Capabilities Functional Descriptor
|
||||
CDC_DESC_SUBTYPE_UNION = 0x06, // Union Functional Descriptor
|
||||
CDC_DESC_SUBTYPE_COUNTRY = 0x07, // Country Selection Functional Descriptor
|
||||
CDC_DESC_SUBTYPE_TEL_MODE = 0x08, // Telephone Operational Modes Functional Descriptor
|
||||
CDC_DESC_SUBTYPE_TERMINAL = 0x09, // USB Terminal
|
||||
CDC_DESC_SUBTYPE_NCHT = 0x0A, // Network Channel Terminal
|
||||
CDC_DESC_SUBTYPE_PROTOCOL = 0x08, // Protocol Unit
|
||||
CDC_DESC_SUBTYPE_EXTENSION = 0x0C, // Extension Unit
|
||||
CDC_DESC_SUBTYPE_MULTI_CHAN = 0x0D, // Multi-Channel Management Functional Descriptor
|
||||
CDC_DESC_SUBTYPE_CAPI = 0x0E, // CAPI Control
|
||||
CDC_DESC_SUBTYPE_ETH = 0x0F, // Ethernet Networking
|
||||
CDC_DESC_SUBTYPE_ATM = 0x10, // ATM Networking
|
||||
CDC_DESC_SUBTYPE_WHANDSET = 0x11, // Wireless Handset Control Model Functional Descriptor
|
||||
CDC_DESC_SUBTYPE_MDLM = 0x12, // Mobile Direct Line Model
|
||||
CDC_DESC_SUBTYPE_MDLM_DETAIL = 0x13, // MDLM Detail
|
||||
CDC_DESC_SUBTYPE_DMM = 0x14, // Device Management Model
|
||||
CDC_DESC_SUBTYPE_OBEX = 0x15, // OBEX Functional
|
||||
CDC_DESC_SUBTYPE_COMMAND_SET = 0x16, // Command Set
|
||||
CDC_DESC_SUBTYPE_COMMAND_SET_DETAIL = 0x17, // Command Set Detail Functional Descriptor
|
||||
CDC_DESC_SUBTYPE_TEL_CM = 0x18, // Telephone Control Model Functional Descriptor
|
||||
CDC_DESC_SUBTYPE_OBEX_SERVICE = 0x19, // OBEX Service Identifier Functional Descriptor
|
||||
CDC_DESC_SUBTYPE_NCM = 0x1A // NCM Functional Descriptor
|
||||
} __attribute__((packed)) cdc_desc_subtype_t;
|
||||
|
||||
/**
|
||||
* @brief USB CDC Subclass codes
|
||||
*
|
||||
* @see Table 4, USB CDC specification rev. 1.2
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
CDC_SUBCLASS_DLCM = 0x01, // Direct Line Control Model
|
||||
CDC_SUBCLASS_ACM = 0x02, // Abstract Control Model
|
||||
CDC_SUBCLASS_TCM = 0x03, // Telephone Control Model
|
||||
CDC_SUBCLASS_MCHCM = 0x04, // Multi-Channel Control Model
|
||||
CDC_SUBCLASS_CAPI = 0x05, // CAPI Control Model
|
||||
CDC_SUBCLASS_ECM = 0x06, // Ethernet Networking Control Model
|
||||
CDC_SUBCLASS_ATM = 0x07, // ATM Networking Model
|
||||
CDC_SUBCLASS_HANDSET = 0x08, // Wireless Handset Control Model
|
||||
CDC_SUBCLASS_DEV_MAN = 0x09, // Device Management
|
||||
CDC_SUBCLASS_MOBILE = 0x0A, // Mobile Direct Line Model
|
||||
CDC_SUBCLASS_OBEX = 0x0B, // OBEX
|
||||
CDC_SUBCLASS_EEM = 0x0C, // Ethernet Emulation Model
|
||||
CDC_SUBCLASS_NCM = 0x0D // Network Control Model
|
||||
} __attribute__((packed)) cdc_subclass_t;
|
||||
|
||||
/**
|
||||
* @brief USB CDC Communications Protocol Codes
|
||||
*
|
||||
* @see Table 5, USB CDC specification rev. 1.2
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
CDC_COMM_PROTOCOL_NONE = 0x00, // No class specific protocol required
|
||||
CDC_COMM_PROTOCOL_V250 = 0x01, // AT Commands: V.250 etc
|
||||
CDC_COMM_PROTOCOL_PCAA = 0x02, // AT Commands defined by PCCA-101
|
||||
CDC_COMM_PROTOCOL_PCAA_A = 0x03, // AT Commands defined by PCAA-101 & Annex O
|
||||
CDC_COMM_PROTOCOL_GSM = 0x04, // AT Commands defined by GSM 07.07
|
||||
CDC_COMM_PROTOCOL_3GPP = 0x05, // AT Commands defined by 3GPP 27.007
|
||||
CDC_COMM_PROTOCOL_TIA = 0x06, // AT Commands defined by TIA for CDMA
|
||||
CDC_COMM_PROTOCOL_EEM = 0x07, // Ethernet Emulation Model
|
||||
CDC_COMM_PROTOCOL_EXT = 0xFE, // External Protocol: Commands defined by Command Set Functional Descriptor
|
||||
CDC_COMM_PROTOCOL_VENDOR = 0xFF // Vendor-specific
|
||||
} __attribute__((packed)) cdc_comm_protocol_t;
|
||||
|
||||
/**
|
||||
* @brief USB CDC Data Protocol Codes
|
||||
*
|
||||
* @see Table 7, USB CDC specification rev. 1.2
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
CDC_DATA_PROTOCOL_NONE = 0x00, // No class specific protocol required
|
||||
CDC_DATA_PROTOCOL_NCM = 0x01, // Network Transfer Block
|
||||
CDC_DATA_PROTOCOL_I430 = 0x30, // Physical interface protocol for ISDN BRI
|
||||
CDC_DATA_PROTOCOL_HDLC = 0x31, // HDLC
|
||||
CDC_DATA_PROTOCOL_Q921M = 0x50, // Management protocol for Q.921 data link protocol
|
||||
CDC_DATA_PROTOCOL_Q921 = 0x51, // Data link protocol for Q.931
|
||||
CDC_DATA_PROTOCOL_Q921TM = 0x52, // TEI-multiplexor for Q.921 data link protocol
|
||||
CDC_DATA_PROTOCOL_V42BIS = 0x90, // Data compression procedures
|
||||
CDC_DATA_PROTOCOL_Q931 = 0x91, // Euro-ISDN protocol control
|
||||
CDC_DATA_PROTOCOL_V120 = 0x92, // V.24 rate adaptation to ISDN
|
||||
CDC_DATA_PROTOCOL_CAPI = 0x93, // CAPI Commands
|
||||
CDC_DATA_PROTOCOL_VENDOR = 0xFF // Vendor-specific
|
||||
} __attribute__((packed)) cdc_data_protocol_t;
|
||||
|
||||
/**
|
||||
* @brief USB CDC Request Codes
|
||||
*
|
||||
* @see Table 19, USB CDC specification rev. 1.2
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
CDC_REQ_SEND_ENCAPSULATED_COMMAND = 0x00,
|
||||
CDC_REQ_GET_ENCAPSULATED_RESPONSE = 0x01,
|
||||
CDC_REQ_SET_COMM_FEATURE = 0x02,
|
||||
CDC_REQ_GET_COMM_FEATURE = 0x03,
|
||||
CDC_REQ_CLEAR_COMM_FEATURE = 0x04,
|
||||
CDC_REQ_SET_AUX_LINE_STATE = 0x10,
|
||||
CDC_REQ_SET_HOOK_STATE = 0x11,
|
||||
CDC_REQ_PULSE_SETUP = 0x12,
|
||||
CDC_REQ_SEND_PULSE = 0x13,
|
||||
CDC_REQ_SET_PULSE_TIME = 0x14,
|
||||
CDC_REQ_RING_AUX_JACK = 0x15,
|
||||
CDC_REQ_SET_LINE_CODING = 0x20,
|
||||
CDC_REQ_GET_LINE_CODING = 0x21,
|
||||
CDC_REQ_SET_CONTROL_LINE_STATE = 0x22,
|
||||
CDC_REQ_SEND_BREAK = 0x23,
|
||||
CDC_REQ_SET_RINGER_PARMS = 0x30,
|
||||
CDC_REQ_GET_RINGER_PARMS = 0x31,
|
||||
CDC_REQ_SET_OPERATION_PARMS = 0x32,
|
||||
CDC_REQ_GET_OPERATION_PARMS = 0x33,
|
||||
CDC_REQ_SET_LINE_PARMS = 0x34,
|
||||
CDC_REQ_GET_LINE_PARMS = 0x35,
|
||||
CDC_REQ_DIAL_DIGITS = 0x36,
|
||||
CDC_REQ_SET_UNIT_PARAMETER = 0x37,
|
||||
CDC_REQ_GET_UNIT_PARAMETER = 0x38,
|
||||
CDC_REQ_CLEAR_UNIT_PARAMETER = 0x39,
|
||||
CDC_REQ_GET_PROFILE = 0x3A,
|
||||
CDC_REQ_SET_ETHERNET_MULTICAST_FILTERS = 0x40,
|
||||
CDC_REQ_SET_ETHERNET_POWER_MANAGEMENT_PATTERN_FILTER = 0x41,
|
||||
CDC_REQ_GET_ETHERNET_POWER_MANAGEMENT_PATTERN_FILTER = 0x42,
|
||||
CDC_REQ_SET_ETHERNET_PACKET_FILTER = 0x43,
|
||||
CDC_REQ_GET_ETHERNET_STATISTIC = 0x44,
|
||||
CDC_REQ_SET_ATM_DATA_FORMAT = 0x50,
|
||||
CDC_REQ_GET_ATM_DEVICE_STATISTICS = 0x51,
|
||||
CDC_REQ_SET_ATM_DEFAULT_VC = 0x52,
|
||||
CDC_REQ_GET_ATM_VC_STATISTICS = 0x53,
|
||||
CDC_REQ_GET_NTB_PARAMETERS = 0x80,
|
||||
CDC_REQ_GET_NET_ADDRESS = 0x81,
|
||||
CDC_REQ_SET_NET_ADDRESS = 0x82,
|
||||
CDC_REQ_GET_NTB_FORMAT = 0x83,
|
||||
CDC_REQ_SET_NTB_FORMAT = 0x84,
|
||||
CDC_REQ_GET_NTB_INPUT_SIZE = 0x85,
|
||||
CDC_REQ_SET_NTB_INPUT_SIZE = 0x86,
|
||||
CDC_REQ_GET_MAX_DATAGRAM_SIZE = 0x87,
|
||||
CDC_REQ_SET_MAX_DATAGRAM_SIZE = 0x88,
|
||||
CDC_REQ_GET_CRC_MODE = 0x89,
|
||||
CDC_REQ_SET_CRC_MODE = 0x8A
|
||||
} __attribute__((packed)) cdc_request_code_t;
|
||||
|
||||
/**
|
||||
* @brief USB CDC Notification Codes
|
||||
*
|
||||
* @see Table 20, USB CDC specification rev. 1.2
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
CDC_NOTIF_NETWORK_CONNECTION = 0x00,
|
||||
CDC_NOTIF_RESPONSE_AVAILABLE = 0x01,
|
||||
CDC_NOTIF_AUX_JACK_HOOK_STATE = 0x08,
|
||||
CDC_NOTIF_RING_DETECT = 0x09,
|
||||
CDC_NOTIF_SERIAL_STATE = 0x20,
|
||||
CDC_NOTIF_CALL_STATE_CHANGE = 0x28,
|
||||
CDC_NOTIF_LINE_STATE_CHANGE = 0x29,
|
||||
CDC_NOTIF_CONNECTION_SPEED_CHANGE = 0x2A
|
||||
} __attribute__((packed)) cdc_notification_code_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t bmRequestType;
|
||||
cdc_notification_code_t bNotificationCode;
|
||||
uint16_t wValue;
|
||||
uint16_t wIndex;
|
||||
uint16_t wLength;
|
||||
uint8_t Data[];
|
||||
} __attribute__((packed)) cdc_notification_t;
|
||||
|
||||
/**
|
||||
* @brief USB CDC Header Functional Descriptor
|
||||
*
|
||||
* @see Table 15, USB CDC specification rev. 1.2
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint8_t bFunctionLength;
|
||||
const uint8_t bDescriptorType; // Upper nibble: CDC code 0x02, Lower nibble: intf/ep descriptor type 0x04/0x05
|
||||
const cdc_desc_subtype_t bDescriptorSubtype;
|
||||
uint16_t bcdCDC; // CDC version as binary-coded decimal. This driver is written for version 1.2
|
||||
} __attribute__((packed)) cdc_header_desc_t;
|
||||
|
||||
/**
|
||||
* @brief USB CDC Union Functional Descriptor
|
||||
*
|
||||
* @see Table 16, USB CDC specification rev. 1.2
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint8_t bFunctionLength;
|
||||
const uint8_t bDescriptorType; // Upper nibble: CDC code 0x02, Lower nibble: intf/ep descriptor type 0x04/0x05
|
||||
const cdc_desc_subtype_t bDescriptorSubtype;
|
||||
const uint8_t bControlInterface; // Master/controlling interface
|
||||
uint8_t bSubordinateInterface[]; // Slave/subordinate interfaces
|
||||
} __attribute__((packed)) cdc_union_desc_t;
|
||||
|
||||
typedef void (*usbh_cdc_cb_t)(void *arg);
|
||||
787
app/drivers/data_port/usb-host/usbport.c
Normal file
787
app/drivers/data_port/usb-host/usbport.c
Normal file
@@ -0,0 +1,787 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "freertos/ringbuf.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
#include "usb/usb_host.h"
|
||||
#include "msc/include/msc_host.h"
|
||||
#include "msc/include/msc_host_vfs.h"
|
||||
#include "ffconf.h"
|
||||
#include "ff.h"
|
||||
#include "errno.h"
|
||||
#include "esp_vfs.h"
|
||||
|
||||
#include "config/hardware_define.h"
|
||||
// #include "usb/cdc_acm_host.h"
|
||||
|
||||
#include "usbport.h"
|
||||
|
||||
#define CONFIG_SYS_LOG_DUMP_ON 0
|
||||
#define CONFIG_SYS_LOG_LEVEL SYS_LOG_LEVEL_WRN
|
||||
#define SYS_LOG_DOMAIN "USB"
|
||||
#include "sys_log.h"
|
||||
|
||||
#define IN_RINGBUF_SIZE (1024 * 1)
|
||||
#define OUT_RINGBUF_SIZE (1024 * 1)
|
||||
|
||||
#define CDC_CHECK(a, str, ret) \
|
||||
do \
|
||||
{ \
|
||||
if (!(a)) \
|
||||
{ \
|
||||
SYS_LOG_ERR(str); \
|
||||
return (ret); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define CDC_CHECK_GOTO(a, str, lable) \
|
||||
do \
|
||||
{ \
|
||||
if (!(a)) \
|
||||
{ \
|
||||
SYS_LOG_ERR(str); \
|
||||
goto lable; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
typedef struct
|
||||
{
|
||||
os_pipe_t pipe_obj;
|
||||
os_work_t *rx_resume_work;
|
||||
} __usb_data_t;
|
||||
|
||||
static QueueHandle_t app_queue;
|
||||
static SemaphoreHandle_t ready_to_uninstall_usb;
|
||||
static msc_host_vfs_handle_t vfs_handle;
|
||||
|
||||
static usbport_status_e g_usb_status = USBPORT_STATUS_NO_CONNECTION;
|
||||
static __usb_data_t s_usb_data;
|
||||
static sb_data_port_t s_port;
|
||||
static msc_host_device_handle_t g_connected_usb_device = NULL;
|
||||
static cdc_acm_dev_hdl_t g_cdc_dev;
|
||||
|
||||
//////////// CDC 全局变量 ////////////
|
||||
|
||||
#define BULK_OUT_URB_NUM 2
|
||||
#define BULK_IN_URB_NUM 2
|
||||
#define BUFFER_SIZE_BULK_OUT 512
|
||||
#define BUFFER_SIZE_BULK_IN 512
|
||||
#define USB_TASK_KILL_BIT BIT1
|
||||
#define CDC_DATA_TASK_KILL_BIT BIT4
|
||||
#define CDC_DEVICE_READY_BIT BIT19
|
||||
#define TIMEOUT_USB_RINGBUF_MS 200 // Timeout for Ring Buffer push
|
||||
|
||||
static EventGroupHandle_t s_usb_event_group = NULL;
|
||||
// static RingbufHandle_t s_in_ringbuf_handle = NULL;
|
||||
static RingbufHandle_t s_out_ringbuf_handle = NULL;
|
||||
// static SemaphoreHandle_t s_usb_read_mux = NULL;
|
||||
static SemaphoreHandle_t s_usb_write_mux = NULL;
|
||||
// static TaskHandle_t s_usb_processing_task_hdl = NULL;
|
||||
|
||||
// static portMUX_TYPE s_in_ringbuf_mux = portMUX_INITIALIZER_UNLOCKED;
|
||||
static portMUX_TYPE s_out_ringbuf_mux = portMUX_INITIALIZER_UNLOCKED;
|
||||
volatile static int s_in_buffered_data_len = 0;
|
||||
volatile static int s_out_buffered_data_len = 0;
|
||||
|
||||
//////////// End CDC 全局变量 ////////////
|
||||
|
||||
//////////// MSC 全局变量 ////////////////
|
||||
|
||||
static msc_file_info g_msc_file_lists[MAX_FILE_COUNT];
|
||||
// static excluding_file_item_t msc_excluding_files[MAX_EXCLUDING_FILE_COUNT];
|
||||
static uint16_t g_msc_file_count = 0;
|
||||
|
||||
//////////// End MSC 全局变量 ////////////
|
||||
|
||||
static void usb_device_event_cb(const msc_host_event_t *event, void *arg)
|
||||
{
|
||||
switch (event->event)
|
||||
{
|
||||
case MSC_DEVICE_CONNECTED:
|
||||
SYS_LOG_INF("MSC device connected");
|
||||
break;
|
||||
case MSC_DEVICE_DISCONNECTED:
|
||||
SYS_LOG_INF("MSC device disconnected");
|
||||
break;
|
||||
case CDC_DEVICE_CONNECTED:
|
||||
SYS_LOG_INF("CDC device connected");
|
||||
break;
|
||||
case CDC_DEVICE_DISCONNECTED:
|
||||
SYS_LOG_INF("CDC device disconnected");
|
||||
break;
|
||||
case DFU_DEVICE_CONNECTED:
|
||||
SYS_LOG_INF("DFU device connected");
|
||||
break;
|
||||
case DFU_DEVICE_DISCONNECTED:
|
||||
SYS_LOG_INF("DFU device disconnected");
|
||||
break;
|
||||
}
|
||||
|
||||
xQueueSend(app_queue, event, 10);
|
||||
}
|
||||
|
||||
// Handles common USB host library events
|
||||
static void handle_usb_events(void *args)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
uint32_t event_flags;
|
||||
usb_host_lib_handle_events(portMAX_DELAY, &event_flags);
|
||||
// Release devices once all clients has deregistered
|
||||
if (event_flags & USB_HOST_LIB_EVENT_FLAGS_NO_CLIENTS)
|
||||
{
|
||||
usb_host_device_free_all();
|
||||
}
|
||||
// Give ready_to_uninstall_usb semaphore to indicate that USB Host library
|
||||
// can be deinitialized, and terminate this task.
|
||||
if (event_flags & USB_HOST_LIB_EVENT_FLAGS_ALL_FREE)
|
||||
{
|
||||
xSemaphoreGive(ready_to_uninstall_usb);
|
||||
}
|
||||
}
|
||||
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
static void print_device_info(msc_host_device_info_t *info)
|
||||
{
|
||||
const size_t megabyte = 1024 * 1024;
|
||||
uint64_t capacity = ((uint64_t)info->sector_size * info->sector_count) / megabyte;
|
||||
|
||||
printf("Device info:\n");
|
||||
printf("\t Capacity: %llu MB\n", capacity);
|
||||
printf("\t Sector size: %u\n", info->sector_size);
|
||||
printf("\t Sector count: %u\n", info->sector_count);
|
||||
printf("\t PID: 0x%4X \n", info->idProduct);
|
||||
printf("\t VID: 0x%4X \n", info->idVendor);
|
||||
wprintf(L"\t iProduct: %S \n", info->iProduct);
|
||||
wprintf(L"\t iManufacturer: %S \n", info->iManufacturer);
|
||||
wprintf(L"\t iSerialNumber: %S \n", info->iSerialNumber);
|
||||
}
|
||||
|
||||
/* ------------------------------- Callbacks -------------------------------- */
|
||||
static void handle_rx(uint8_t *data, size_t data_len, void *arg)
|
||||
{
|
||||
SYS_LOG_INF("Data received %d bytes", data_len);
|
||||
// ESP_LOG_BUFFER_HEXDUMP(data, data_len, ESP_LOG_INFO);
|
||||
|
||||
int write_result = os_pipe_fifo_fill(&s_usb_data.pipe_obj, data, data_len);
|
||||
if (write_result != data_len)
|
||||
{
|
||||
SYS_LOG_WRN("[ERROR]write data to pipe_obj failed, remain: %d bytes, data_len:%d!!",
|
||||
write_result,
|
||||
data_len);
|
||||
}
|
||||
|
||||
if (os_pipe_get_empty_size(&s_usb_data.pipe_obj) >= BUFFER_SIZE_BULK_IN)
|
||||
{
|
||||
cdc_submit_transfer_in(g_cdc_dev);
|
||||
}
|
||||
}
|
||||
|
||||
static uint8_t check_file_is_in_excluding_list(const char *filename, uint8_t is_dir, const excluding_file_item_t *excluding_files, uint8_t excluding_file_count)
|
||||
{
|
||||
if (excluding_file_count == 0 || excluding_files == NULL)
|
||||
{
|
||||
SYS_LOG_INF("excluding file count is empty, break check");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int source_str_len = strlen(filename);
|
||||
int i = 0;
|
||||
for (i = 0; i < excluding_file_count; i++)
|
||||
{
|
||||
const excluding_file_item_t *file_item = &(excluding_files[i]);
|
||||
if (is_dir != file_item->is_dir)
|
||||
{
|
||||
SYS_LOG_INF("src is dir:%d, target is dir:%d", is_dir, file_item->is_dir);
|
||||
continue;
|
||||
}
|
||||
|
||||
uint8_t is_same = 0;
|
||||
if (file_item->compare_type == EXCLUDING_FILE_COMPARE_ALL)
|
||||
{
|
||||
is_same = strcmp(filename, file_item->filename) == 0;
|
||||
}
|
||||
else if (file_item->compare_type == EXCLUDING_FILE_COMPARE_PREFIX)
|
||||
{
|
||||
int len = strlen(file_item->filename);
|
||||
if (source_str_len < len)
|
||||
{
|
||||
// 如果文件名小于要比较的字符串,则忽略检查
|
||||
SYS_LOG_INF("SOURCE LEN:%d, target len:%d", source_str_len, len);
|
||||
continue;
|
||||
}
|
||||
|
||||
SYS_LOG_INF("CHECKING, SRC:%s, target:%s, len:%d", filename, file_item->filename, len);
|
||||
is_same = strncmp(filename, file_item->filename, len) == 0;
|
||||
}
|
||||
|
||||
if (is_same)
|
||||
{
|
||||
SYS_LOG_INF("FOUND THE EXCLUD FILE:%s", filename);
|
||||
return 1; // 这个文件是要排除的
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void list_all_files(const char *dirpath, uint16_t *current_file_index, const excluding_file_item_t *excluding_files, uint8_t excluding_file_count)
|
||||
{
|
||||
// 递归枚举所有文件
|
||||
struct dirent *entry;
|
||||
struct stat entry_stat;
|
||||
char entrypath[MAX_FILE_PATH];
|
||||
char entrysize[16];
|
||||
const char *entrytype;
|
||||
size_t dirpath_len = strlen(dirpath);
|
||||
DIR *dir = opendir(dirpath);
|
||||
strlcpy(entrypath, dirpath, sizeof(entrypath));
|
||||
if (entrypath[dirpath_len - 1] != '/')
|
||||
{
|
||||
entrypath[dirpath_len] = '/';
|
||||
dirpath_len += 1;
|
||||
}
|
||||
|
||||
uint16_t *file_index = current_file_index;
|
||||
while ((entry = readdir(dir)) != NULL)
|
||||
{
|
||||
entrytype = (entry->d_type == DT_DIR ? "directory" : "file");
|
||||
|
||||
strlcpy(entrypath + dirpath_len, entry->d_name, sizeof(entrypath) - dirpath_len);
|
||||
if (entry->d_type != DT_DIR)
|
||||
{
|
||||
if (check_file_is_in_excluding_list(entry->d_name, 0, excluding_files, excluding_file_count))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
SYS_LOG_INF("file path:%s", entrypath);
|
||||
if (esp_vfs_stat(__getreent(), entrypath, &entry_stat) == -1)
|
||||
{
|
||||
SYS_LOG_ERR("Failed to stat %s : %s", entrytype, entry->d_name);
|
||||
continue;
|
||||
}
|
||||
|
||||
sprintf(entrysize, "%ld", entry_stat.st_size);
|
||||
SYS_LOG_INF("Found %s : %s (%s bytes)", entrytype, entry->d_name, entrysize);
|
||||
|
||||
msc_file_info *file_info = &(g_msc_file_lists[*file_index]);
|
||||
strlcpy(file_info->file_path, entrypath, MAX_FILE_PATH);
|
||||
file_info->file_size = entry_stat.st_size;
|
||||
*file_index = *file_index + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (check_file_is_in_excluding_list(entry->d_name, 1, excluding_files, excluding_file_count))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// 检查文件名是否以.号开始,是的话则忽略这个文件夹
|
||||
SYS_LOG_INF("ITEM NAME:%s", entry->d_name);
|
||||
if (strncmp(entry->d_name, "SPOTLI~", 7) == 0)
|
||||
{
|
||||
// 这个文件夹是MAC系统自动生成的,用于spotlight搜索的索引文件夹,忽略它,否则会导致opendir失败
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
SYS_LOG_INF("ENTER SUB DIR:%s", entrypath);
|
||||
list_all_files(entrypath, file_index, excluding_files, excluding_file_count);
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir(dir);
|
||||
}
|
||||
|
||||
static void handle_device_events(void *args)
|
||||
{
|
||||
msc_host_event_t app_event;
|
||||
while (1)
|
||||
{
|
||||
xQueueReceive(app_queue, &app_event, portMAX_DELAY);
|
||||
|
||||
switch (app_event.event)
|
||||
{
|
||||
case MSC_DEVICE_CONNECTED:
|
||||
{
|
||||
uint8_t device_address = app_event.device.address;
|
||||
ESP_ERROR_CHECK(msc_host_install_device(device_address, &g_connected_usb_device));
|
||||
msc_host_print_descriptors(g_connected_usb_device);
|
||||
msc_host_device_info_t info;
|
||||
ESP_ERROR_CHECK(msc_host_get_device_info(g_connected_usb_device, &info));
|
||||
print_device_info(&info);
|
||||
const esp_vfs_fat_mount_config_t mount_config = {
|
||||
.format_if_mount_failed = false,
|
||||
.max_files = 1,
|
||||
.allocation_unit_size = 1024,
|
||||
};
|
||||
ESP_ERROR_CHECK(msc_host_vfs_register(g_connected_usb_device, "/usb", &mount_config, &vfs_handle));
|
||||
|
||||
for (int i = 0; i < g_msc_file_count; i++)
|
||||
{
|
||||
msc_file_info *file_info = &g_msc_file_lists[i];
|
||||
|
||||
SYS_LOG_INF("file path: %s, file_size:%d", file_info->file_path, file_info->file_size);
|
||||
}
|
||||
|
||||
g_usb_status = USBPORT_STATUS_MSC;
|
||||
}
|
||||
break;
|
||||
|
||||
case MSC_DEVICE_DISCONNECTED:
|
||||
ESP_ERROR_CHECK(msc_host_vfs_unregister(vfs_handle));
|
||||
vfs_handle = NULL;
|
||||
ESP_ERROR_CHECK(msc_host_uninstall_device(g_connected_usb_device));
|
||||
g_connected_usb_device = NULL;
|
||||
g_usb_status = USBPORT_STATUS_NO_CONNECTION;
|
||||
break;
|
||||
|
||||
case CDC_DEVICE_CONNECTED:
|
||||
{
|
||||
SYS_LOG_INF("cdc device connected");
|
||||
|
||||
// SYS_LOG_INF("Installing CDC-ACM driver");
|
||||
// ESP_ERROR_CHECK(cdc_acm_host_install(NULL));
|
||||
|
||||
// cdc_acm_dev_hdl_t cdc_dev;
|
||||
const cdc_acm_host_device_config_t dev_config = {
|
||||
.connection_timeout_ms = 5000,
|
||||
.out_buffer_size = 64,
|
||||
.user_arg = NULL,
|
||||
.event_cb = NULL,
|
||||
.data_cb = handle_rx};
|
||||
cdc_host_open(app_event.vendor_id, app_event.product_id, 0, &dev_config, &g_cdc_dev);
|
||||
assert(g_cdc_dev);
|
||||
cdc_host_desc_print(g_cdc_dev);
|
||||
g_usb_status = USBPORT_STATUS_CDC;
|
||||
// g_connected_usb_device = cdc_dev;
|
||||
}
|
||||
break;
|
||||
case CDC_DEVICE_DISCONNECTED:
|
||||
SYS_LOG_INF("cdc device dconnected");
|
||||
cdc_host_close(g_cdc_dev);
|
||||
g_usb_status = USBPORT_STATUS_NO_CONNECTION;
|
||||
break;
|
||||
case DFU_DEVICE_CONNECTED:
|
||||
SYS_LOG_INF("DFU DEVICE CONNECTED");
|
||||
const cdc_acm_host_device_config_t dev_config = {
|
||||
.connection_timeout_ms = 5000,
|
||||
.out_buffer_size = 64,
|
||||
.user_arg = NULL,
|
||||
.event_cb = NULL,
|
||||
.data_cb = handle_rx};
|
||||
dfu_host_open(app_event.vendor_id, app_event.product_id, 0, &dev_config, &g_cdc_dev);
|
||||
|
||||
g_usb_status = USBPORT_STATUS_DFU;
|
||||
break;
|
||||
case DFU_DEVICE_DISCONNECTED:
|
||||
SYS_LOG_INF("DFU DEVICE DISCONNECTED");
|
||||
dfu_host_close(g_cdc_dev);
|
||||
g_usb_status = USBPORT_STATUS_NO_CONNECTION;
|
||||
break;
|
||||
default:
|
||||
assert(0);
|
||||
break;
|
||||
}
|
||||
|
||||
/* code */
|
||||
}
|
||||
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////
|
||||
//////////// CDC设备透传的相关的函数 //////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
static bool _if_device_ready()
|
||||
{
|
||||
if (!s_usb_event_group)
|
||||
return false;
|
||||
return xEventGroupGetBits(s_usb_event_group) & CDC_DEVICE_READY_BIT;
|
||||
}
|
||||
|
||||
static bool _cdc_driver_is_init(void)
|
||||
{
|
||||
if (s_usb_event_group == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static esp_err_t usb_out_ringbuf_push(const uint8_t *buf, size_t write_bytes, TickType_t xTicksToWait)
|
||||
{
|
||||
int res =
|
||||
xRingbufferSend(s_out_ringbuf_handle, buf, write_bytes, xTicksToWait);
|
||||
|
||||
if (res != pdTRUE)
|
||||
{
|
||||
SYS_LOG_WRN("The out buffer is too small, the data has been lost %u", write_bytes);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
portENTER_CRITICAL(&s_out_ringbuf_mux);
|
||||
s_out_buffered_data_len += write_bytes;
|
||||
#ifdef CONFIG_CDC_USE_TRACE_FACILITY
|
||||
s_ringbuf_out_max = s_out_buffered_data_len > s_ringbuf_out_max ? s_out_buffered_data_len : s_ringbuf_out_max;
|
||||
#endif
|
||||
portEXIT_CRITICAL(&s_out_ringbuf_mux);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
int usbh_cdc_write_bytes(const uint8_t *buf, size_t length)
|
||||
{
|
||||
SYS_LOG_INF("write data to cdc devi11111");
|
||||
CDC_CHECK(buf != NULL, "invalid args", -1);
|
||||
int tx_data_size = 0;
|
||||
|
||||
if (_cdc_driver_is_init() == false)
|
||||
{
|
||||
SYS_LOG_INF("CDC Driver not installed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (_if_device_ready() == false)
|
||||
{
|
||||
SYS_LOG_INF("Device not connected or not ready");
|
||||
return -1;
|
||||
}
|
||||
|
||||
SYS_LOG_INF("write data to cdc devie2222");
|
||||
xSemaphoreTake(s_usb_write_mux, portMAX_DELAY);
|
||||
esp_err_t ret = usb_out_ringbuf_push(buf, length, pdMS_TO_TICKS(TIMEOUT_USB_RINGBUF_MS));
|
||||
|
||||
if (ret != ESP_OK)
|
||||
{
|
||||
xSemaphoreGive(s_usb_write_mux);
|
||||
SYS_LOG_DBG("Write pipe_obj failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
tx_data_size = length;
|
||||
xSemaphoreGive(s_usb_write_mux);
|
||||
return tx_data_size;
|
||||
}
|
||||
|
||||
static int _usbhost_port_write(sb_data_port_t *port, const void *data, uint32_t size)
|
||||
{
|
||||
int result = cdc_write_bytes(data, size);
|
||||
return result;
|
||||
}
|
||||
|
||||
static int _usbhost_port_read(sb_data_port_t *port, void *buffer, uint32_t length)
|
||||
{
|
||||
if (os_pipe_is_valid(&s_usb_data.pipe_obj))
|
||||
{
|
||||
int ret = os_pipe_fifo_read(&s_usb_data.pipe_obj, buffer, length);
|
||||
if (ret > 0 && os_pipe_get_empty_size(&s_usb_data.pipe_obj) >= BUFFER_SIZE_BULK_IN)
|
||||
{
|
||||
cdc_submit_transfer_in(g_cdc_dev);
|
||||
}
|
||||
|
||||
SYS_LOG_INF("Data readed %d bytes", ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
static int _usbhost_start(sb_data_port_t *port)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int _usbhost_stop(sb_data_port_t *port)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool _usbhost_is_started(sb_data_port_t *port)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
static uint32_t _usbhost_get_rx_length(sb_data_port_t *port)
|
||||
{
|
||||
return os_pipe_get_valid_size(&s_usb_data.pipe_obj);
|
||||
}
|
||||
|
||||
static sb_data_port_vtable_t const usbhost_port_vtable = {
|
||||
.start = _usbhost_start, // 由具体驱动实现的,以达到节省资源为主要目的,启动数据接口
|
||||
.stop = _usbhost_stop, // 由具体驱动实现的,以达到节省资源为主要目的,关闭数据接口
|
||||
.write = _usbhost_port_write, // 由具体驱动实现的,写数据到对应的接口。
|
||||
.read = _usbhost_port_read, // 由具体驱动实现的,从数据接口中读取已缓存的数据。
|
||||
.is_started = _usbhost_is_started, // 由具体驱动实现的,获取当前数据接口是否可用(是否已启动)
|
||||
.get_rx_length = _usbhost_get_rx_length, // 由具体驱动实现的,获取当前数据接口的本次可读长度
|
||||
};
|
||||
|
||||
//////////// CDC设备透传的相关的函数结束 ///////////
|
||||
|
||||
////////////////////////////////////////////////
|
||||
//////////// STM32固件升级相关的函数 //////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
// esp_err_t _usbh_stm32_control_transfer(uint8_t direction,
|
||||
// uint8_t request,
|
||||
// uint16_t value,
|
||||
// uint16_t interface,
|
||||
// uint16_t length,
|
||||
// uint8_t *packet_data,
|
||||
// uint8_t *out_buffer,
|
||||
// uint16_t out_buffer_size,
|
||||
// uint16_t *actual_result_size)
|
||||
// {
|
||||
// // if (g_pipe_hdl_dflt != NULL) {
|
||||
// // return _usb_control_transfer(g_pipe_hdl_dflt, direction, request, value, interface, length, packet_data, out_buffer, out_buffer_size, actual_result_size);
|
||||
// // }
|
||||
|
||||
// return ESP_FAIL;
|
||||
// }
|
||||
|
||||
// esp_err_t _usbh_stm32_control_transfer_ex(uint8_t direction,
|
||||
// uint8_t request,
|
||||
// uint16_t value,
|
||||
// uint16_t interface,
|
||||
// uint16_t length,
|
||||
// uint8_t *packet_data,
|
||||
// uint8_t *out_buffer,
|
||||
// uint16_t out_buffer_size,
|
||||
// uint16_t *actual_result_size,
|
||||
// uint16_t timeout)
|
||||
// {
|
||||
// // if (g_pipe_hdl_dflt != NULL) {
|
||||
// // return _usb_control_transfer_ex(g_pipe_hdl_dflt,
|
||||
// // direction,
|
||||
// // request,
|
||||
// // value,
|
||||
// // interface,
|
||||
// // length,
|
||||
// // packet_data,
|
||||
// // out_buffer,
|
||||
// // out_buffer_size,
|
||||
// // actual_result_size,
|
||||
// // timeout);
|
||||
// // }
|
||||
|
||||
// return ESP_FAIL;
|
||||
// }
|
||||
|
||||
// esp_err_t _usbh_stm32_try_read_ob(uint16_t ob_data_size)
|
||||
// {
|
||||
// // if (g_pipe_hdl_dflt != NULL) {
|
||||
// // return _usb_try_read_ob(g_pipe_hdl_dflt, ob_data_size);
|
||||
// // }
|
||||
|
||||
// return ESP_FAIL;
|
||||
// }
|
||||
|
||||
// esp_err_t _usbh_stm32_unprotect()
|
||||
// {
|
||||
// // if (g_pipe_hdl_dflt != NULL) {
|
||||
// // return _usb_unprotect(g_pipe_hdl_dflt);
|
||||
// // }
|
||||
|
||||
// return ESP_FAIL;
|
||||
// }
|
||||
|
||||
// esp_err_t _usbh_stm32_leave_dfu()
|
||||
// {
|
||||
// return ESP_OK;
|
||||
// // if (g_pipe_hdl_dflt == NULL) {
|
||||
// // return ESP_FAIL;
|
||||
// // }
|
||||
|
||||
// // // _usb_control_transfer_ex(pipe_handle,
|
||||
// // // USB_BM_REQUEST_TYPE_DIR_IN,
|
||||
// // // STM32_DFU_REQUEST_GETSTATUS,
|
||||
// // // 0,
|
||||
// // // 0,
|
||||
// // // 6,
|
||||
// // // 0,
|
||||
// // // out_result_data,
|
||||
// // // 6,
|
||||
// // // NULL,
|
||||
// // // timeout);
|
||||
|
||||
// // uint8_t direction = USB_BM_REQUEST_TYPE_DIR_IN;
|
||||
// // uint8_t request = STM32_DFU_REQUEST_GETSTATUS;
|
||||
// // uint16_t value = 0;
|
||||
// // uint16_t interface = 0;
|
||||
// // uint16_t length = 6;
|
||||
// // uint8_t *packet_data = NULL;
|
||||
// // uint8_t *out_buffer = NULL;
|
||||
// // uint16_t out_buffer_size = 0;
|
||||
// // uint16_t *actual_result_size = NULL;
|
||||
// // uint16_t timeout = 500;
|
||||
|
||||
// // CDC_CHECK(g_pipe_hdl_dflt != NULL, "g_pipe_hdl_dflt can't be NULL", ESP_ERR_INVALID_ARG);
|
||||
// // // malloc URB for default control
|
||||
// // uint16_t packet_data_size = ENUM_CTRL_TRANSFER_MAX_LEN;
|
||||
// // if (length > packet_data_size) {
|
||||
// // packet_data_size = length;
|
||||
// // }
|
||||
// // urb_t *urb_ctrl = _usb_urb_alloc(0, sizeof(usb_setup_packet_t) + packet_data_size, NULL);
|
||||
// // CDC_CHECK(urb_ctrl != NULL, "alloc urb failed", ESP_ERR_NO_MEM);
|
||||
|
||||
// // usb_setup_packet_t *setup_packet = (usb_setup_packet_t *)urb_ctrl->transfer.data_buffer;
|
||||
// // if (direction == USB_BM_REQUEST_TYPE_DIR_IN) {
|
||||
// // setup_packet->bmRequestType = USB_BM_REQUEST_TYPE_DIR_IN | USB_BM_REQUEST_TYPE_TYPE_CLASS | USB_BM_REQUEST_TYPE_RECIP_INTERFACE;
|
||||
// // setup_packet->bRequest = request;
|
||||
// // setup_packet->wValue = value;
|
||||
// // setup_packet->wIndex = interface;
|
||||
// // setup_packet->wLength = length;
|
||||
// // urb_ctrl->transfer.num_bytes = sizeof(usb_setup_packet_t) + length;
|
||||
// // } else if (direction == USB_BM_REQUEST_TYPE_DIR_OUT) {
|
||||
// // setup_packet->bmRequestType = USB_BM_REQUEST_TYPE_DIR_OUT | USB_BM_REQUEST_TYPE_TYPE_CLASS | USB_BM_REQUEST_TYPE_RECIP_INTERFACE;
|
||||
// // setup_packet->bRequest = request;
|
||||
// // setup_packet->wValue = value;
|
||||
// // setup_packet->wIndex = interface;
|
||||
// // setup_packet->wLength = length;
|
||||
// // memcpy(((uint8_t *)setup_packet) + sizeof(usb_setup_packet_t), packet_data, length);
|
||||
// // urb_ctrl->transfer.num_bytes = sizeof(usb_setup_packet_t) + length;
|
||||
// // }
|
||||
|
||||
// // // Enqueue it
|
||||
// // esp_err_t ret = hcd_urb_enqueue(g_pipe_hdl_dflt, urb_ctrl);
|
||||
// // CDC_CHECK_GOTO(ESP_OK == ret, "urb enqueue failed", free_urb_);
|
||||
// // SYS_LOG_INF("urb request timeout:%d ms, and becuase it used for leave dfu, so dont wait response", timeout);
|
||||
|
||||
// // goto free_urb_;
|
||||
|
||||
// // free_urb_:
|
||||
// // _usb_pipe_flush(g_pipe_hdl_dflt, 1);
|
||||
// // _usb_urb_free(urb_ctrl);
|
||||
// // return ret;
|
||||
// }
|
||||
|
||||
// uint16_t usbh_stm32_get_transfer_block_size()
|
||||
// {
|
||||
// // if (g_pipe_hdl_dflt != NULL) {
|
||||
// // usb_function_desc_packet_t function_desc_packet;
|
||||
// // esp_err_t ret = _usb_get_function_descriptors(g_pipe_hdl_dflt, 0, &function_desc_packet);
|
||||
// // if (ret != ESP_OK) {
|
||||
// // return 2048;
|
||||
// // } else {
|
||||
// // return function_desc_packet.wTransferSize;
|
||||
// // }
|
||||
// // }
|
||||
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
// esp_err_t usbh_stm32_clear_status()
|
||||
// {
|
||||
// // // return _usbh_
|
||||
// // if (g_pipe_hdl_dflt != NULL) {
|
||||
// // return _usb_clear_status(g_pipe_hdl_dflt);
|
||||
// // }
|
||||
|
||||
// return ESP_FAIL;
|
||||
// }
|
||||
|
||||
// esp_err_t _usbh_stm32_load_address(uint32_t address)
|
||||
// {
|
||||
// // if (g_pipe_hdl_dflt != NULL) {
|
||||
// // return _usb_load_address(g_pipe_hdl_dflt, address);
|
||||
// // }
|
||||
|
||||
// return ESP_FAIL;
|
||||
// }
|
||||
|
||||
//////////// STM32固件升级相关的函数结束 //////////////
|
||||
|
||||
int usbport_init(void)
|
||||
{
|
||||
if (s_port.vtable)
|
||||
{
|
||||
SYS_LOG_WRN("repeated initialize");
|
||||
return -1;
|
||||
}
|
||||
|
||||
s_port.vtable = &usbhost_port_vtable;
|
||||
s_port.data = &s_usb_data;
|
||||
|
||||
TaskHandle_t usb_task_handle, device_task_handle; //, workthread_handle;
|
||||
|
||||
memset(&s_usb_data, 0, sizeof(__usb_data_t));
|
||||
os_pipe_create(&s_usb_data.pipe_obj, 1024 * 2);
|
||||
|
||||
ready_to_uninstall_usb = xSemaphoreCreateBinary();
|
||||
|
||||
app_queue = xQueueCreate(3, sizeof(msc_host_event_t));
|
||||
assert(app_queue);
|
||||
|
||||
static usb_host_config_t const host_config = {
|
||||
// .skip_phy_setup = false,
|
||||
.intr_flags = ESP_INTR_FLAG_LEVEL1,
|
||||
};
|
||||
ESP_ERROR_CHECK(usb_host_install(&host_config));
|
||||
|
||||
xTaskCreatePinnedToCore(handle_usb_events, "usb_events", 4096, NULL, SBTASK_PRIORITY_USB_HOST, &usb_task_handle, SBTASK_CORE_INDEX_USB_HOST);
|
||||
assert(usb_task_handle);
|
||||
xTaskCreatePinnedToCore(handle_device_events, "device_events", 4096, NULL, SBTASK_PRIORITY_USB_DEVICE, &device_task_handle, SBTASK_CORE_INDEX_USB_DEVICE);
|
||||
assert(device_task_handle);
|
||||
|
||||
const msc_host_driver_config_t msc_config = {
|
||||
.create_backround_task = true,
|
||||
.task_priority = SBTASK_PRIORITY_USB_MSC,
|
||||
.stack_size = 2048 * 2,
|
||||
.callback = usb_device_event_cb,
|
||||
.core_id = 0};
|
||||
ESP_ERROR_CHECK(msc_host_install(&msc_config));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
sb_data_port_t *usbport_bind(os_work_t *rx_resume_work)
|
||||
{
|
||||
if (s_port.data == NULL)
|
||||
{
|
||||
SYS_LOG_WRN("usb host not initialized");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
__usb_data_t *usb_data = s_port.data;
|
||||
usb_data->rx_resume_work = rx_resume_work;
|
||||
|
||||
os_pipe_regist(&usb_data->pipe_obj, rx_resume_work, 0);
|
||||
|
||||
return &s_port;
|
||||
}
|
||||
|
||||
usbport_status_e usbport_get_state(void)
|
||||
{
|
||||
return g_usb_status;
|
||||
}
|
||||
|
||||
msc_file_info *msc_get_file_list(uint16_t *out_file_count, const excluding_file_item_t *excluding_files, uint8_t excluding_file_count)
|
||||
{
|
||||
g_msc_file_count = 0;
|
||||
list_all_files("/usb/", &g_msc_file_count, excluding_files, excluding_file_count);
|
||||
|
||||
return msc_get_cached_file_list(out_file_count);
|
||||
}
|
||||
|
||||
msc_file_info *msc_get_cached_file_list(uint16_t *out_file_count)
|
||||
{
|
||||
if (out_file_count)
|
||||
{
|
||||
*out_file_count = g_msc_file_count;
|
||||
}
|
||||
return g_msc_file_lists;
|
||||
}
|
||||
113
app/drivers/data_port/usb-host/usbport.h
Normal file
113
app/drivers/data_port/usb-host/usbport.h
Normal file
@@ -0,0 +1,113 @@
|
||||
/**
|
||||
* @file usbport.h
|
||||
* @author your name (you@domain.com)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2023-09-04
|
||||
*
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "esp_err.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "freertos/task.h"
|
||||
#include "os/os.h"
|
||||
|
||||
#include "config/hardware_define.h"
|
||||
#include "drivers/data_port/sb_data_port.h"
|
||||
|
||||
#include "usb/usb_types_stack.h"
|
||||
|
||||
#include "os/os.h"
|
||||
|
||||
#define MAX_FILE_COUNT 255
|
||||
#define MAX_EXCLUDING_FILE_COUNT 20
|
||||
#define MAX_FILE_PATH 50
|
||||
|
||||
typedef enum
|
||||
{
|
||||
USBPORT_STATUS_NO_CONNECTION = 0, // 没有外设
|
||||
USBPORT_STATUS_CDC = 1, // 外设处于cdc状态
|
||||
USBPORT_STATUS_DFU = 2, // 外设处于dfu状态
|
||||
USBPORT_STATUS_MSC = 3, // 外设处于msc状态
|
||||
} usbport_status_e;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
EXCLUDING_FILE_COMPARE_ALL = 0, // 比较整个文名名,任意部分与指定字符串不同,则认为不相同
|
||||
EXCLUDING_FILE_COMPARE_PREFIX = 1, // 只比较文件名的前面部分是否与指定的字符串相同
|
||||
} excluding_file_compare_type;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
const char filename[MAX_FILE_PATH];
|
||||
uint8_t is_dir;
|
||||
uint8_t compare_type;
|
||||
} excluding_file_item_t;
|
||||
|
||||
#pragma pack(push, 1)
|
||||
typedef struct
|
||||
{
|
||||
char file_path[MAX_FILE_PATH];
|
||||
uint32_t file_size;
|
||||
} msc_file_info;
|
||||
#pragma pack(pop)
|
||||
|
||||
int usbport_init(void);
|
||||
sb_data_port_t *usbport_bind(os_work_t *rx_resume_work);
|
||||
usbport_status_e usbport_get_state(void);
|
||||
|
||||
// 读取flash layout信息
|
||||
void usbh_stm32_get_chipinfo(char *descriptors, uint8_t count, uint8_t *actual_desc_count);
|
||||
// 读取传输块大小
|
||||
// uint16_t usbh_stm32_get_transfer_block_size();
|
||||
// clear status
|
||||
esp_err_t usbh_stm32_clear_status(void);
|
||||
// get status
|
||||
esp_err_t usbh_stm32_get_status(uint8_t *out_result_data /*[6]*/);
|
||||
esp_err_t usbh_stm32_get_status_ex(uint8_t *out_result_data /*[6]*/, uint16_t timeout);
|
||||
// 设置当前地址
|
||||
esp_err_t _usbh_stm32_load_address(uint32_t address);
|
||||
// 控制指令传输
|
||||
esp_err_t _usbh_stm32_control_transfer(uint8_t direction,
|
||||
uint8_t request,
|
||||
uint16_t value,
|
||||
uint16_t interface,
|
||||
uint16_t length,
|
||||
uint8_t *packet_data,
|
||||
uint8_t *out_buffer,
|
||||
uint16_t out_buffer_size,
|
||||
uint16_t *actual_result_size);
|
||||
|
||||
esp_err_t _usbh_stm32_control_transfer_ex(uint8_t direction,
|
||||
uint8_t request,
|
||||
uint16_t value,
|
||||
uint16_t interface,
|
||||
uint16_t length,
|
||||
uint8_t *packet_data,
|
||||
uint8_t *out_buffer,
|
||||
uint16_t out_buffer_size,
|
||||
uint16_t *actual_result_size,
|
||||
uint16_t timeout);
|
||||
// 解除读保护
|
||||
esp_err_t _usbh_stm32_unprotect(void);
|
||||
|
||||
// 读取option byte区别
|
||||
esp_err_t _usbh_stm32_try_read_ob(uint16_t ob_data_size);
|
||||
|
||||
// 退出dfu状态
|
||||
esp_err_t _usbh_stm32_leave_dfu(void);
|
||||
|
||||
// 断开usb设备的连接
|
||||
void _usbh_pre_disconnect_deivce(void);
|
||||
|
||||
msc_file_info *msc_get_file_list(uint16_t *out_file_count, const excluding_file_item_t *excluding_files, uint8_t excluding_file_count);
|
||||
msc_file_info *msc_get_cached_file_list(uint16_t *out_file_count);
|
||||
Reference in New Issue
Block a user