Merge pull request #92 from espressif/feature/cdc_modem_support
USB: esp_modem USB DTE
This commit is contained in:
commit
5e9599d94a
|
@ -15,6 +15,13 @@ jobs:
|
|||
- name: Upload components to component service
|
||||
uses: espressif/upload-components-ci-action@v1
|
||||
with:
|
||||
directories: "bdc_motor;cbor;jsmn;led_strip;libsodium;pid_ctrl;qrcode;nghttp;sh2lib;expat;esp_encrypted_img;coap;pcap;json_generator;json_parser;usb/usb_host_cdc_acm;usb/usb_host_msc;usb/usb_host_uvc;esp_serial_slave_link;esp_jpeg;eigen"
|
||||
directories: >
|
||||
"
|
||||
bdc_motor;cbor;jsmn;led_strip;libsodium;pid_ctrl;qrcode;nghttp;sh2lib;expat;esp_encrypted_img;coap;pcap;json_generator;json_parser;esp_serial_slave_link;esp_jpeg;eigen;
|
||||
usb/usb_host_cdc_acm;
|
||||
usb/usb_host_msc;
|
||||
usb/usb_host_uvc;
|
||||
usb/esp_modem_usb_dte;
|
||||
"
|
||||
namespace: "espressif"
|
||||
api_token: ${{ secrets.IDF_COMPONENT_API_TOKEN }}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
*/dist/**
|
||||
build
|
||||
sdkconfig
|
||||
sdkconfig.old
|
||||
dependencies.lock
|
||||
**/dist/**
|
||||
build
|
||||
sdkconfig
|
||||
sdkconfig.old
|
||||
dependencies.lock
|
||||
**/managed_components/**
|
||||
.vscode/**
|
||||
|
|
|
@ -4,3 +4,19 @@ repos:
|
|||
hooks:
|
||||
- id: astyle_py
|
||||
args: ['--style=otbs', '--attach-namespaces', '--attach-classes', '--indent=spaces=4', '--convert-tabs', '--align-pointer=name', '--align-reference=name', '--keep-one-line-statements', '--pad-header', '--pad-oper']
|
||||
|
||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||
rev: v4.3.0
|
||||
hooks:
|
||||
- id: trailing-whitespace
|
||||
files: ^usb/
|
||||
types_or: [c, c++]
|
||||
- id: end-of-file-fixer
|
||||
files: ^usb/
|
||||
types_or: [c, c++]
|
||||
- id: check-merge-conflict
|
||||
- id: mixed-line-ending
|
||||
files: ^usb/ # temporary USB only
|
||||
types_or: [c, c++]
|
||||
args: ['--fix=lf']
|
||||
description: Forces to replace line ending by the UNIX 'lf' character
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
idf_component_register(SRCS "esp_modem_usb.cpp" "esp_modem_usb_api_target.cpp" "esp_modem_usb_c_api.cpp"
|
||||
REQUIRED_IDF_TARGETS esp32s2 esp32s3
|
||||
PRIV_INCLUDE_DIRS "private_include"
|
||||
INCLUDE_DIRS "include")
|
||||
|
||||
set_target_properties(${COMPONENT_LIB} PROPERTIES CXX_STANDARD 17)
|
|
@ -0,0 +1,202 @@
|
|||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
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.
|
|
@ -0,0 +1,34 @@
|
|||
# USB DTE plugin for esp_modem
|
||||
|
||||
> :warning: **Experimental feature**: USB DTE is under development!
|
||||
|
||||
This component extends [esp_modem](https://components.espressif.com/component/espressif/esp_modem) with USB DTE.
|
||||
|
||||
## Examples
|
||||
For example usage see esp_modem examples:
|
||||
* [console example](https://github.com/espressif/esp-protocols/tree/master/components/esp_modem/examples/modem_console)
|
||||
* [PPPoS example](https://github.com/espressif/esp-protocols/tree/master/components/esp_modem/examples/pppos_client)
|
||||
|
||||
## USB hotplugging and reconnection
|
||||
USB DTE supports device reconnection and hot-plugging. You must only register callback function for `DEVICE_GONE` event and react accordingly:
|
||||
|
||||
C++ with lambda callback:
|
||||
```cpp
|
||||
auto dte = create_usb_dte(&dte_config);
|
||||
dte->set_error_cb([&](terminal_error err) {
|
||||
if (err == terminal_error::DEVICE_GONE) {
|
||||
// Signal your application that USB modem device has been disconnected
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
C:
|
||||
```c
|
||||
static void usb_terminal_error_handler(esp_modem_terminal_error_t err) {
|
||||
if (err == ESP_MODEM_TERMINAL_DEVICE_GONE) {
|
||||
// Signal your application that USB modem device has been disconnected
|
||||
}
|
||||
}
|
||||
esp_modem_dce_t *dce = esp_modem_new_dev_usb(ESP_MODEM_DCE_BG96, &dte_usb_config, &dce_config, esp_netif);
|
||||
esp_modem_set_error_cb(dce, usb_terminal_error_handler);
|
||||
```
|
|
@ -0,0 +1,183 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_modem_config.h"
|
||||
#include "esp_modem_usb_config.h"
|
||||
#include "cxx_include/esp_modem_dte.hpp"
|
||||
#include "cxx_include/esp_modem_exception.hpp"
|
||||
#include "usb/usb_host.h"
|
||||
#include "usb/cdc_acm_host.h"
|
||||
#include "sdkconfig.h"
|
||||
#include "usb_terminal.hpp"
|
||||
|
||||
static const char *TAG = "usb_terminal";
|
||||
|
||||
/**
|
||||
* @brief USB Host task
|
||||
*
|
||||
* This task is created only if install_usb_host is set to true in DTE configuration.
|
||||
* In case you don't want to install USB Host driver here, you must install it before creating UsbTerminal object.
|
||||
*
|
||||
* This implementation of USB Host Lib handling never returns, which means that the USB Host Lib will keep running
|
||||
* even after all USB devices are disconnected. That allows repeated device reconnections.
|
||||
*
|
||||
* If you want/need to handle lifetime of USB Host Lib, you can set install_usb_host to false and manage it yourself.
|
||||
*
|
||||
* @param arg Unused
|
||||
*/
|
||||
static void usb_host_task(void *arg)
|
||||
{
|
||||
while (1) {
|
||||
uint32_t event_flags;
|
||||
usb_host_lib_handle_events(portMAX_DELAY, &event_flags);
|
||||
if (event_flags & USB_HOST_LIB_EVENT_FLAGS_NO_CLIENTS) {
|
||||
ESP_LOGD(TAG, "No more clients: clean up\n");
|
||||
usb_host_device_free_all();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace esp_modem {
|
||||
class UsbTerminal : public Terminal, private CdcAcmDevice {
|
||||
public:
|
||||
explicit UsbTerminal(const esp_modem_dte_config *config)
|
||||
{
|
||||
const struct esp_modem_usb_term_config *usb_config = (struct esp_modem_usb_term_config *)(config->extension_config);
|
||||
|
||||
// Install USB Host driver (if not already installed)
|
||||
if (usb_config->install_usb_host && !usb_host_lib_task) {
|
||||
const usb_host_config_t host_config = {
|
||||
.skip_phy_setup = false,
|
||||
.intr_flags = ESP_INTR_FLAG_LEVEL1,
|
||||
};
|
||||
ESP_MODEM_THROW_IF_ERROR(usb_host_install(&host_config), "USB Host install failed");
|
||||
ESP_LOGD(TAG, "USB Host installed");
|
||||
ESP_MODEM_THROW_IF_FALSE(
|
||||
pdTRUE == xTaskCreatePinnedToCore(usb_host_task, "usb_host", 4096, NULL, config->task_priority + 1, &usb_host_lib_task, usb_config->xCoreID),
|
||||
"USB host task failed");
|
||||
}
|
||||
|
||||
// Install CDC-ACM driver
|
||||
const cdc_acm_host_driver_config_t esp_modem_cdc_acm_driver_config = {
|
||||
.driver_task_stack_size = config->task_stack_size,
|
||||
.driver_task_priority = config->task_priority,
|
||||
.xCoreID = (BaseType_t)usb_config->xCoreID,
|
||||
.new_dev_cb = NULL, // We don't forward this information to user. User can poll USB Host Lib.
|
||||
};
|
||||
|
||||
// Silently continue on error: CDC-ACM driver might be already installed
|
||||
cdc_acm_host_install(&esp_modem_cdc_acm_driver_config);
|
||||
|
||||
// Open CDC-ACM device
|
||||
const cdc_acm_host_device_config_t esp_modem_cdc_acm_device_config = {
|
||||
.connection_timeout_ms = usb_config->timeout_ms,
|
||||
.out_buffer_size = config->dte_buffer_size,
|
||||
.event_cb = handle_notif,
|
||||
.data_cb = handle_rx,
|
||||
.user_arg = this
|
||||
};
|
||||
|
||||
if (usb_config->cdc_compliant) {
|
||||
ESP_MODEM_THROW_IF_ERROR(
|
||||
this->CdcAcmDevice::open(usb_config->vid, usb_config->pid, usb_config->interface_idx, &esp_modem_cdc_acm_device_config),
|
||||
"USB Device open failed");
|
||||
} else {
|
||||
ESP_MODEM_THROW_IF_ERROR(
|
||||
this->CdcAcmDevice::open_vendor_specific(usb_config->vid, usb_config->pid, usb_config->interface_idx, &esp_modem_cdc_acm_device_config),
|
||||
"USB Device open failed");
|
||||
}
|
||||
};
|
||||
|
||||
~UsbTerminal()
|
||||
{
|
||||
this->CdcAcmDevice::close();
|
||||
};
|
||||
|
||||
void start() override
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
void stop() override
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int write(uint8_t *data, size_t len) override
|
||||
{
|
||||
ESP_LOG_BUFFER_HEXDUMP(TAG, data, len, ESP_LOG_DEBUG);
|
||||
if (this->CdcAcmDevice::tx_blocking(data, len) != ESP_OK) {
|
||||
return -1;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
int read(uint8_t *data, size_t len) override
|
||||
{
|
||||
// This function should never be called. UsbTerminal provides data through Terminal::on_read callback
|
||||
ESP_LOGW(TAG, "Unexpected call to UsbTerminal::read function");
|
||||
return -1;
|
||||
}
|
||||
|
||||
private:
|
||||
UsbTerminal() = delete;
|
||||
UsbTerminal(const UsbTerminal ©) = delete;
|
||||
UsbTerminal &operator=(const UsbTerminal ©) = delete;
|
||||
bool operator== (const UsbTerminal ¶m) const = delete;
|
||||
bool operator!= (const UsbTerminal ¶m) const = delete;
|
||||
static TaskHandle_t usb_host_lib_task; // Reused by multiple devices or between reconnections
|
||||
|
||||
static void handle_rx(uint8_t *data, size_t data_len, void *user_arg)
|
||||
{
|
||||
ESP_LOG_BUFFER_HEXDUMP(TAG, data, data_len, ESP_LOG_DEBUG);
|
||||
UsbTerminal *this_terminal = static_cast<UsbTerminal *>(user_arg);
|
||||
if (data_len > 0 && this_terminal->on_read) {
|
||||
this_terminal->on_read(data, data_len);
|
||||
} else {
|
||||
ESP_LOGD(TAG, "Unhandled RX data");
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_notif(const cdc_acm_host_dev_event_data_t *event, void *user_ctx)
|
||||
{
|
||||
UsbTerminal *this_terminal = static_cast<UsbTerminal *>(user_ctx);
|
||||
|
||||
switch (event->type) {
|
||||
// Notifications like Ring, Rx Carrier indication or Network connection indication are not relevant for USB terminal
|
||||
case CDC_ACM_HOST_NETWORK_CONNECTION:
|
||||
case CDC_ACM_HOST_SERIAL_STATE:
|
||||
ESP_LOGD(TAG, "Ignored USB event %d", event->type);
|
||||
break;
|
||||
case CDC_ACM_HOST_DEVICE_DISCONNECTED:
|
||||
ESP_LOGW(TAG, "USB terminal disconnected");
|
||||
if (this_terminal->on_error) {
|
||||
this_terminal->on_error(terminal_error::DEVICE_GONE);
|
||||
}
|
||||
this_terminal->close();
|
||||
break;
|
||||
case CDC_ACM_HOST_ERROR:
|
||||
ESP_LOGE(TAG, "Unexpected CDC-ACM error: %d.", event->data.error);
|
||||
if (this_terminal->on_error) {
|
||||
this_terminal->on_error(terminal_error::UNEXPECTED_CONTROL_FLOW);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
abort();
|
||||
}
|
||||
};
|
||||
};
|
||||
TaskHandle_t UsbTerminal::usb_host_lib_task = nullptr;
|
||||
|
||||
std::unique_ptr<Terminal> create_usb_terminal(const esp_modem_dte_config *config)
|
||||
{
|
||||
TRY_CATCH_RET_NULL(
|
||||
return std::make_unique<UsbTerminal>(config);
|
||||
)
|
||||
}
|
||||
} // namespace esp_modem
|
|
@ -0,0 +1,22 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "usb_terminal.hpp"
|
||||
#include "cxx_include/esp_modem_api.hpp"
|
||||
|
||||
#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
|
||||
static const char *TAG = "modem_usb_api_target";
|
||||
#endif
|
||||
|
||||
namespace esp_modem {
|
||||
std::shared_ptr<DTE> create_usb_dte(const dte_config *config)
|
||||
{
|
||||
TRY_CATCH_RET_NULL(
|
||||
auto term = create_usb_terminal(config);
|
||||
return std::make_shared<DTE>(config, std::move(term));
|
||||
)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "esp_modem_usb_c_api.h"
|
||||
#include "cxx_include/esp_modem_usb_api.hpp"
|
||||
#include "esp_private/c_api_wrapper.hpp"
|
||||
|
||||
using namespace esp_modem;
|
||||
|
||||
extern "C" esp_modem_dce_t *esp_modem_new_dev_usb(esp_modem_dce_device_t module, const esp_modem_dte_config_t *dte_config, const esp_modem_dce_config_t *dce_config, esp_netif_t *netif)
|
||||
{
|
||||
auto dce_wrap = new (std::nothrow) esp_modem_dce_wrap;
|
||||
if (dce_wrap == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
auto dte = create_usb_dte(dte_config);
|
||||
if (dte == nullptr) {
|
||||
delete dce_wrap;
|
||||
return nullptr;
|
||||
}
|
||||
dce_wrap->dte = dte;
|
||||
dce_factory::Factory f(convert_modem_enum(module));
|
||||
dce_wrap->dce = f.build(dce_config, std::move(dte), netif);
|
||||
if (dce_wrap->dce == nullptr) {
|
||||
delete dce_wrap;
|
||||
return nullptr;
|
||||
}
|
||||
dce_wrap->modem_type = convert_modem_enum(module);
|
||||
dce_wrap->dte_type = esp_modem_dce_wrap::modem_wrap_dte_type::USB;
|
||||
return dce_wrap;
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
## IDF Component Manager Manifest File
|
||||
version: "1.0.0"
|
||||
description: USB DTE plugin for esp_modem component
|
||||
url: https://github.com/espressif/idf-extra-components/tree/master/usb/esp_modem_usb_dte
|
||||
|
||||
dependencies:
|
||||
idf: ">=4.4"
|
||||
usb_host_cdc_acm: "1.*"
|
||||
esp_modem: "^0.1.23"
|
|
@ -0,0 +1,20 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "cxx_include/esp_modem_api.hpp"
|
||||
|
||||
namespace esp_modem {
|
||||
/**
|
||||
* @brief Create USB DTE
|
||||
*
|
||||
* @param config DTE configuration
|
||||
* @return shared ptr to DTE on success
|
||||
* nullptr on failure (either due to insufficient memory or wrong dte configuration)
|
||||
* if exceptions are disabled the API abort()'s on error
|
||||
*/
|
||||
std::shared_ptr<DTE> create_usb_dte(const dte_config *config);
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "esp_modem_c_api_types.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Create a DCE handle using the supplied USB DTE
|
||||
*
|
||||
* @param module Specific device for creating this DCE
|
||||
* @param dte_config DTE - USB configuration
|
||||
* @param dce_config DCE configuration
|
||||
* @param netif Network interface handle for the data mode
|
||||
*
|
||||
* @return DCE pointer on success, NULL on failure
|
||||
*/
|
||||
esp_modem_dce_t *esp_modem_new_dev_usb(esp_modem_dce_device_t module, const esp_modem_dte_config_t *dte_config, const esp_modem_dce_config_t *dce_config, esp_netif_t *netif);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/**
|
||||
* @brief USB configuration structure
|
||||
* @see USB host CDC-ACM driver documentation for details about interfaces settings
|
||||
*/
|
||||
struct esp_modem_usb_term_config {
|
||||
uint16_t vid; /*!< Vendor ID of the USB device */
|
||||
uint16_t pid; /*!< Product ID of the USB device */
|
||||
int interface_idx; /*!< USB Interface index that will be used */
|
||||
uint32_t timeout_ms; /*!< Time for a USB modem to connect to USB host. 0 means wait forever. */
|
||||
int xCoreID; /*!< Core affinity of created tasks: CDC-ACM driver task and optional USB Host task */
|
||||
bool cdc_compliant; /*!< Treat the USB device as CDC-compliant. Read CDC-ACM driver documentation for more details */
|
||||
bool install_usb_host; /*!< Flag whether USB Host driver should be installed */
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief ESP Mode USB DTE Default Configuration
|
||||
*
|
||||
* @param[in] _usb_config esp_modem_usb_term_config configuration structure. Can be obtained by ESP_MODEM_DEFAULT_USB_CONFIG
|
||||
*
|
||||
*/
|
||||
#define ESP_MODEM_DTE_DEFAULT_USB_CONFIG(_usb_config) \
|
||||
{ \
|
||||
.dte_buffer_size = 512, \
|
||||
.task_stack_size = 4096, \
|
||||
.task_priority = 5, \
|
||||
.extension_config = &_usb_config \
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ESP Modem USB Default Configuration
|
||||
*
|
||||
* @param[in] _vid USB Vendor ID
|
||||
* @param[in] _pid USB Product ID
|
||||
* @param[in] _intf USB interface number
|
||||
* @see USB host CDC-ACM driver documentation for details about interfaces settings
|
||||
*/
|
||||
#define ESP_MODEM_DEFAULT_USB_CONFIG(_vid, _pid, _intf) \
|
||||
{ \
|
||||
.vid = _vid, \
|
||||
.pid = _pid, \
|
||||
.interface_idx = _intf, \
|
||||
.timeout_ms = 0, \
|
||||
.xCoreID = 0, \
|
||||
.cdc_compliant = false, \
|
||||
.install_usb_host = true \
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "cxx_include/esp_modem_dte.hpp"
|
||||
#include "../private_include/exception_stub.hpp"
|
||||
|
||||
struct esp_modem_dte_config;
|
||||
|
||||
namespace esp_modem {
|
||||
std::unique_ptr<Terminal> create_usb_terminal(const esp_modem_dte_config *config);
|
||||
} // namespace esp_modem
|
|
@ -4,7 +4,9 @@ cmake_minimum_required(VERSION 3.16)
|
|||
|
||||
set(EXTRA_COMPONENT_DIRS ../usb_host_cdc_acm
|
||||
../usb_host_msc
|
||||
../usb_host_uvc)
|
||||
../usb_host_uvc
|
||||
../esp_modem_usb_dte
|
||||
)
|
||||
|
||||
# Set the components to include the tests for.
|
||||
set(TEST_COMPONENTS "usb_host_cdc_acm" "usb_host_msc" "usb_host_uvc" CACHE STRING "List of components to test")
|
||||
|
|
|
@ -357,7 +357,7 @@ static esp_err_t cdc_acm_find_and_open_usb_device(uint16_t vid, uint16_t pid, in
|
|||
TimeOut_t connection_timeout;
|
||||
vTaskSetTimeOutState(&connection_timeout);
|
||||
|
||||
while (true) {
|
||||
do {
|
||||
ESP_LOGD(TAG, "Checking list of connected USB devices");
|
||||
uint8_t dev_addr_list[10];
|
||||
int num_of_devices;
|
||||
|
@ -380,12 +380,8 @@ static esp_err_t cdc_acm_find_and_open_usb_device(uint16_t vid, uint16_t pid, in
|
|||
}
|
||||
usb_host_device_close(p_cdc_acm_obj->cdc_acm_client_hdl, current_device);
|
||||
}
|
||||
|
||||
if (xTaskCheckForTimeOut(&connection_timeout, &timeout_ticks) != pdFALSE) {
|
||||
break; // Timeout elapsed and the device is not connected
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(50));
|
||||
}
|
||||
} while (xTaskCheckForTimeOut(&connection_timeout, &timeout_ticks) == pdFALSE);
|
||||
|
||||
// Timeout was reached, clean-up
|
||||
free(*dev);
|
||||
|
@ -769,9 +765,10 @@ esp_err_t cdc_acm_host_open_vendor_specific(uint16_t vid, uint16_t pid, uint8_t
|
|||
|
||||
// Find underlying USB device
|
||||
cdc_dev_t *cdc_dev;
|
||||
ESP_GOTO_ON_ERROR(
|
||||
cdc_acm_find_and_open_usb_device(vid, pid, dev_config->connection_timeout_ms, &cdc_dev),
|
||||
exit, TAG, "USB device with VID: 0x%04X, PID: 0x%04X not found", vid, pid);
|
||||
ret = cdc_acm_find_and_open_usb_device(vid, pid, dev_config->connection_timeout_ms, &cdc_dev);
|
||||
if (ESP_OK != ret) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
// Open procedure for CDC-ACM non-compliant devices:
|
||||
const usb_config_desc_t *config_desc;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
version: "1.0.2"
|
||||
version: "1.0.3"
|
||||
description: USB Host CDC-ACM driver
|
||||
url: https://github.com/espressif/idf-extra-components/tree/master/usb/usb_host_cdc_acm
|
||||
dependencies:
|
||||
|
|
|
@ -1,342 +1,342 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "usb/usb_host.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
|
||||
cdc_acm_dev_hdl_t cdc_hdl; //!< Disconnection event
|
||||
} data;
|
||||
} cdc_acm_host_dev_event_data_t;
|
||||
|
||||
/**
|
||||
* @brief New USB device callback
|
||||
*
|
||||
* Provides already opened usb_dev, that will be closed after this callback returns.
|
||||
* This is useful for peeking device's descriptors, e.g. peeking VID/PID and loading proper driver.
|
||||
*
|
||||
* @attention This callback is called from USB Host context, so the CDC device can't be opened here.
|
||||
*/
|
||||
typedef void (*cdc_acm_new_dev_callback_t)(usb_device_handle_t usb_dev);
|
||||
|
||||
/**
|
||||
* @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_data_t
|
||||
*/
|
||||
typedef void (*cdc_acm_host_dev_callback_t)(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_new_dev_callback_t new_dev_cb; /**< New USB device connected callback. Can be NULL. */
|
||||
} 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 device's descriptors
|
||||
*
|
||||
* Device and full Configuration descriptors are printed in human readable format to stdout.
|
||||
*
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @brief Send command to CTRL endpoint
|
||||
*
|
||||
* Sends Control transfer as described in USB specification chapter 9.
|
||||
* This function can be used by device drivers that use custom/vendor specific commands.
|
||||
* These commands can either extend or replace commands defined in USB CDC-PSTN specification rev. 1.2.
|
||||
*
|
||||
* @param cdc_hdl CDC handle obtained from cdc_acm_host_open()
|
||||
* @param[in] bmRequestType Field of USB control request
|
||||
* @param[in] bRequest Field of USB control request
|
||||
* @param[in] wValue Field of USB control request
|
||||
* @param[in] wIndex Field of USB control request
|
||||
* @param[in] wLength Field of USB control request
|
||||
* @param[inout] data Field of USB control request
|
||||
* @return esp_err_t
|
||||
*/
|
||||
esp_err_t cdc_acm_host_send_custom_request(cdc_acm_dev_hdl_t cdc_hdl, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint16_t wLength, uint8_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 esp_err_t close()
|
||||
{
|
||||
esp_err_t err = cdc_acm_host_close(this->cdc_hdl);
|
||||
if (err == ESP_OK) {
|
||||
this->cdc_hdl = NULL;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
inline esp_err_t send_custom_request(uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint16_t wLength, uint8_t *data)
|
||||
{
|
||||
return cdc_acm_host_send_custom_request(this->cdc_hdl, bmRequestType, bRequest, wValue, wIndex, wLength, data);
|
||||
}
|
||||
|
||||
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
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "usb/usb_host.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
|
||||
cdc_acm_dev_hdl_t cdc_hdl; //!< Disconnection event
|
||||
} data;
|
||||
} cdc_acm_host_dev_event_data_t;
|
||||
|
||||
/**
|
||||
* @brief New USB device callback
|
||||
*
|
||||
* Provides already opened usb_dev, that will be closed after this callback returns.
|
||||
* This is useful for peeking device's descriptors, e.g. peeking VID/PID and loading proper driver.
|
||||
*
|
||||
* @attention This callback is called from USB Host context, so the CDC device can't be opened here.
|
||||
*/
|
||||
typedef void (*cdc_acm_new_dev_callback_t)(usb_device_handle_t usb_dev);
|
||||
|
||||
/**
|
||||
* @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_data_t
|
||||
*/
|
||||
typedef void (*cdc_acm_host_dev_callback_t)(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_new_dev_callback_t new_dev_cb; /**< New USB device connected callback. Can be NULL. */
|
||||
} 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 device's descriptors
|
||||
*
|
||||
* Device and full Configuration descriptors are printed in human readable format to stdout.
|
||||
*
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @brief Send command to CTRL endpoint
|
||||
*
|
||||
* Sends Control transfer as described in USB specification chapter 9.
|
||||
* This function can be used by device drivers that use custom/vendor specific commands.
|
||||
* These commands can either extend or replace commands defined in USB CDC-PSTN specification rev. 1.2.
|
||||
*
|
||||
* @param cdc_hdl CDC handle obtained from cdc_acm_host_open()
|
||||
* @param[in] bmRequestType Field of USB control request
|
||||
* @param[in] bRequest Field of USB control request
|
||||
* @param[in] wValue Field of USB control request
|
||||
* @param[in] wIndex Field of USB control request
|
||||
* @param[in] wLength Field of USB control request
|
||||
* @param[inout] data Field of USB control request
|
||||
* @return esp_err_t
|
||||
*/
|
||||
esp_err_t cdc_acm_host_send_custom_request(cdc_acm_dev_hdl_t cdc_hdl, uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint16_t wLength, uint8_t *data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
class CdcAcmDevice {
|
||||
public:
|
||||
// Operators
|
||||
CdcAcmDevice() : cdc_hdl(NULL) {};
|
||||
virtual ~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 esp_err_t close()
|
||||
{
|
||||
const esp_err_t err = cdc_acm_host_close(this->cdc_hdl);
|
||||
if (err == ESP_OK) {
|
||||
this->cdc_hdl = NULL;
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
virtual inline esp_err_t line_coding_get(cdc_acm_line_coding_t *line_coding) const
|
||||
{
|
||||
return cdc_acm_host_line_coding_get(this->cdc_hdl, line_coding);
|
||||
}
|
||||
|
||||
virtual 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);
|
||||
}
|
||||
|
||||
virtual 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);
|
||||
}
|
||||
|
||||
virtual inline esp_err_t send_break(uint16_t duration_ms)
|
||||
{
|
||||
return cdc_acm_host_send_break(this->cdc_hdl, duration_ms);
|
||||
}
|
||||
|
||||
inline esp_err_t send_custom_request(uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint16_t wLength, uint8_t *data)
|
||||
{
|
||||
return cdc_acm_host_send_custom_request(this->cdc_hdl, bmRequestType, bRequest, wValue, wIndex, wLength, data);
|
||||
}
|
||||
|
||||
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
|
||||
|
|
|
@ -1,448 +1,448 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: CC0-1.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);
|
||||
|
||||
bool all_clients_gone = false;
|
||||
bool all_dev_free = false;
|
||||
while (!all_clients_gone || !all_dev_free) {
|
||||
// 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\n");
|
||||
usb_host_device_free_all();
|
||||
all_clients_gone = true;
|
||||
}
|
||||
if (event_flags & USB_HOST_LIB_EVENT_FLAGS_ALL_FREE) {
|
||||
printf("All devices freed\n");
|
||||
all_dev_free = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Clean up USB Host
|
||||
vTaskDelay(10); // Short delay to allow clients clean-up
|
||||
TEST_ASSERT_EQUAL(ESP_OK, usb_host_uninstall());
|
||||
TEST_ASSERT_EQUAL(ESP_OK, usb_del_phy(phy_hdl)); //Tear down USB PHY
|
||||
phy_hdl = NULL;
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void test_install_cdc_driver(void)
|
||||
{
|
||||
// Create a task that will handle USB library events
|
||||
TEST_ASSERT_EQUAL(pdTRUE, xTaskCreatePinnedToCore(usb_lib_task, "usb_lib", 4 * 4096, xTaskGetCurrentTaskHandle(), 10, NULL, 0));
|
||||
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(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(event->data.cdc_hdl));
|
||||
xTaskNotifyGive(user_ctx);
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
static bool new_dev_cb_called = false;
|
||||
static void new_dev_cb(usb_device_handle_t usb_dev)
|
||||
{
|
||||
new_dev_cb_called = true;
|
||||
const usb_config_desc_t *config_desc;
|
||||
const usb_device_desc_t *device_desc;
|
||||
|
||||
// Get descriptors
|
||||
TEST_ASSERT_EQUAL(ESP_OK, usb_host_get_device_descriptor(usb_dev, &device_desc));
|
||||
TEST_ASSERT_EQUAL(ESP_OK, usb_host_get_active_config_descriptor(usb_dev, &config_desc));
|
||||
|
||||
printf("New device connected. VID = 0x%04X PID = %04X\n", device_desc->idVendor, device_desc->idProduct);
|
||||
}
|
||||
|
||||
/* 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("read_write", "[cdc_acm]")
|
||||
{
|
||||
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("multiple_devices", "[cdc_acm]")
|
||||
{
|
||||
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("multiple_threads", "[cdc_acm]")
|
||||
{
|
||||
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("sudden_disconnection", "[cdc_acm]")
|
||||
{
|
||||
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)); // Simulate device disconnection
|
||||
TEST_ASSERT_EQUAL(1, ulTaskNotifyTake(false, pdMS_TO_TICKS(100))); // Notify will succeed only if CDC_ACM_HOST_DEVICE_DISCONNECTED notification was generated
|
||||
|
||||
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("error_handling", "[cdc_acm]")
|
||||
{
|
||||
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
|
||||
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);
|
||||
}
|
||||
|
||||
TEST_CASE("custom_command", "[cdc_acm]")
|
||||
{
|
||||
test_install_cdc_driver();
|
||||
|
||||
// Open device with only CTRL endpoint (endpoint no 0)
|
||||
cdc_acm_dev_hdl_t cdc_dev;
|
||||
const cdc_acm_host_device_config_t dev_config = {
|
||||
.connection_timeout_ms = 500,
|
||||
.out_buffer_size = 0,
|
||||
.event_cb = notif_cb,
|
||||
.data_cb = NULL
|
||||
};
|
||||
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_open(0x303A, 0x4002, 0, &dev_config, &cdc_dev));
|
||||
TEST_ASSERT_NOT_NULL(cdc_dev);
|
||||
|
||||
// Corresponds to command: Set Control Line State, DTR on, RTS off
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_send_custom_request(cdc_dev, 0x21, 34, 1, 0, 0, NULL));
|
||||
|
||||
// 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_CASE("new_device_connection", "[cdc_acm]")
|
||||
{
|
||||
// Create a task that will handle USB library events
|
||||
TEST_ASSERT_EQUAL(pdTRUE, xTaskCreatePinnedToCore(usb_lib_task, "usb_lib", 4 * 4096, xTaskGetCurrentTaskHandle(), 10, NULL, 0));
|
||||
ulTaskNotifyTake(false, 1000);
|
||||
|
||||
printf("Installing CDC-ACM driver\n");
|
||||
const cdc_acm_host_driver_config_t driver_config = {
|
||||
.driver_task_priority = 11,
|
||||
.driver_task_stack_size = 2048,
|
||||
.xCoreID = 0,
|
||||
.new_dev_cb = new_dev_cb,
|
||||
};
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_install(&driver_config));
|
||||
|
||||
vTaskDelay(80);
|
||||
TEST_ASSERT_TRUE_MESSAGE(new_dev_cb_called, "New device callback was not called\n");
|
||||
|
||||
// Clean-up
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_uninstall());
|
||||
vTaskDelay(20);
|
||||
}
|
||||
|
||||
/* Following test case implements dual CDC-ACM USB device that can be used as mock device for CDC-ACM Host tests */
|
||||
void run_usb_dual_cdc_device(void);
|
||||
TEST_CASE("mock_device_app", "[cdc_acm_device][ignore]")
|
||||
{
|
||||
run_usb_dual_cdc_device();
|
||||
while (1) {
|
||||
vTaskDelay(10);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: CC0-1.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);
|
||||
|
||||
bool all_clients_gone = false;
|
||||
bool all_dev_free = false;
|
||||
while (!all_clients_gone || !all_dev_free) {
|
||||
// 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\n");
|
||||
usb_host_device_free_all();
|
||||
all_clients_gone = true;
|
||||
}
|
||||
if (event_flags & USB_HOST_LIB_EVENT_FLAGS_ALL_FREE) {
|
||||
printf("All devices freed\n");
|
||||
all_dev_free = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Clean up USB Host
|
||||
vTaskDelay(10); // Short delay to allow clients clean-up
|
||||
TEST_ASSERT_EQUAL(ESP_OK, usb_host_uninstall());
|
||||
TEST_ASSERT_EQUAL(ESP_OK, usb_del_phy(phy_hdl)); //Tear down USB PHY
|
||||
phy_hdl = NULL;
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void test_install_cdc_driver(void)
|
||||
{
|
||||
// Create a task that will handle USB library events
|
||||
TEST_ASSERT_EQUAL(pdTRUE, xTaskCreatePinnedToCore(usb_lib_task, "usb_lib", 4 * 4096, xTaskGetCurrentTaskHandle(), 10, NULL, 0));
|
||||
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(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(event->data.cdc_hdl));
|
||||
xTaskNotifyGive(user_ctx);
|
||||
break;
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
static bool new_dev_cb_called = false;
|
||||
static void new_dev_cb(usb_device_handle_t usb_dev)
|
||||
{
|
||||
new_dev_cb_called = true;
|
||||
const usb_config_desc_t *config_desc;
|
||||
const usb_device_desc_t *device_desc;
|
||||
|
||||
// Get descriptors
|
||||
TEST_ASSERT_EQUAL(ESP_OK, usb_host_get_device_descriptor(usb_dev, &device_desc));
|
||||
TEST_ASSERT_EQUAL(ESP_OK, usb_host_get_active_config_descriptor(usb_dev, &config_desc));
|
||||
|
||||
printf("New device connected. VID = 0x%04X PID = %04X\n", device_desc->idVendor, device_desc->idProduct);
|
||||
}
|
||||
|
||||
/* 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("read_write", "[cdc_acm]")
|
||||
{
|
||||
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("multiple_devices", "[cdc_acm]")
|
||||
{
|
||||
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("multiple_threads", "[cdc_acm]")
|
||||
{
|
||||
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("sudden_disconnection", "[cdc_acm]")
|
||||
{
|
||||
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)); // Simulate device disconnection
|
||||
TEST_ASSERT_EQUAL(1, ulTaskNotifyTake(false, pdMS_TO_TICKS(100))); // Notify will succeed only if CDC_ACM_HOST_DEVICE_DISCONNECTED notification was generated
|
||||
|
||||
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("error_handling", "[cdc_acm]")
|
||||
{
|
||||
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
|
||||
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);
|
||||
}
|
||||
|
||||
TEST_CASE("custom_command", "[cdc_acm]")
|
||||
{
|
||||
test_install_cdc_driver();
|
||||
|
||||
// Open device with only CTRL endpoint (endpoint no 0)
|
||||
cdc_acm_dev_hdl_t cdc_dev;
|
||||
const cdc_acm_host_device_config_t dev_config = {
|
||||
.connection_timeout_ms = 500,
|
||||
.out_buffer_size = 0,
|
||||
.event_cb = notif_cb,
|
||||
.data_cb = NULL
|
||||
};
|
||||
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_open(0x303A, 0x4002, 0, &dev_config, &cdc_dev));
|
||||
TEST_ASSERT_NOT_NULL(cdc_dev);
|
||||
|
||||
// Corresponds to command: Set Control Line State, DTR on, RTS off
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_send_custom_request(cdc_dev, 0x21, 34, 1, 0, 0, NULL));
|
||||
|
||||
// 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_CASE("new_device_connection", "[cdc_acm]")
|
||||
{
|
||||
// Create a task that will handle USB library events
|
||||
TEST_ASSERT_EQUAL(pdTRUE, xTaskCreatePinnedToCore(usb_lib_task, "usb_lib", 4 * 4096, xTaskGetCurrentTaskHandle(), 10, NULL, 0));
|
||||
ulTaskNotifyTake(false, 1000);
|
||||
|
||||
printf("Installing CDC-ACM driver\n");
|
||||
const cdc_acm_host_driver_config_t driver_config = {
|
||||
.driver_task_priority = 11,
|
||||
.driver_task_stack_size = 2048,
|
||||
.xCoreID = 0,
|
||||
.new_dev_cb = new_dev_cb,
|
||||
};
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_install(&driver_config));
|
||||
|
||||
vTaskDelay(80);
|
||||
TEST_ASSERT_TRUE_MESSAGE(new_dev_cb_called, "New device callback was not called\n");
|
||||
|
||||
// Clean-up
|
||||
TEST_ASSERT_EQUAL(ESP_OK, cdc_acm_host_uninstall());
|
||||
vTaskDelay(20);
|
||||
}
|
||||
|
||||
/* Following test case implements dual CDC-ACM USB device that can be used as mock device for CDC-ACM Host tests */
|
||||
void run_usb_dual_cdc_device(void);
|
||||
TEST_CASE("mock_device_app", "[cdc_acm_device][ignore]")
|
||||
{
|
||||
run_usb_dual_cdc_device();
|
||||
while (1) {
|
||||
vTaskDelay(10);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
version: "1.0.1"
|
||||
version: "1.0.2"
|
||||
description: USB Host MSC driver
|
||||
url: https://github.com/espressif/idf-extra-components/tree/master/usb/usb_host_msc
|
||||
|
||||
|
|
Loading…
Reference in New Issue