ESP-IDF Firmware
Firmware architecture and call graph
Loading...
Searching...
No Matches
cdc.c
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#include <stdint.h>
8#include "esp_check.h"
9#include "esp_err.h"
10#include "esp_log.h"
11#include "tusb.h"
12#include "cdc.h"
13
14#define CDC_INTF_NUM CFG_TUD_CDC // number of cdc blocks
16static const char *TAG = "tusb_cdc";
17
19{
20 if (itf_num >= CDC_INTF_NUM || itf_num < 0) {
21 return NULL;
22 }
23 return cdc_obj[itf_num];
24}
25
26static esp_err_t cdc_obj_check(int itf, bool expected_inited, tusb_class_code_t expected_type)
27{
28 esp_tusb_cdc_t *this_itf = tinyusb_cdc_get_intf(itf);
29
30 bool inited = (this_itf != NULL);
31 ESP_RETURN_ON_FALSE(expected_inited == inited, ESP_ERR_INVALID_STATE, TAG, "Wrong state of the interface. Expected state: %s", expected_inited ? "initialized" : "not initialized");
32 ESP_RETURN_ON_FALSE(!(inited && (expected_type != -1) && !(this_itf->type == expected_type)), ESP_ERR_INVALID_STATE, TAG, "Wrong type of the interface. Should be : 0x%x (tusb_class_code_t)", expected_type);
33 return ESP_OK;
34}
35
37{
38 ESP_RETURN_ON_ERROR(cdc_obj_check(itf, false, -1), TAG, "cdc_obj_check failed");
39 cdc_obj[itf] = calloc(1, sizeof(esp_tusb_cdc_t));
40 if (cdc_obj[itf] != NULL) {
41 cdc_obj[itf]->type = TUSB_CLASS_CDC;
42 ESP_LOGD(TAG, "CDC Comm class initialized");
43 return ESP_OK;
44 } else {
45 ESP_LOGE(TAG, "CDC Comm initialization error");
46 return ESP_FAIL;
47 }
48}
49
51{
52 ESP_RETURN_ON_ERROR(cdc_obj_check(itf, true, TUSB_CLASS_CDC), TAG, "cdc_obj_check failed");
53 free(cdc_obj[itf]);
54 cdc_obj[itf] = NULL;
55 return ESP_OK;
56}
57
59{
60 ESP_RETURN_ON_ERROR(cdc_obj_check(itf, false, TUSB_CLASS_CDC_DATA), TAG, "cdc_obj_check failed");
61 cdc_obj[itf] = calloc(1, sizeof(esp_tusb_cdc_t));
62 if (cdc_obj[itf] != NULL) {
63 cdc_obj[itf]->type = TUSB_CLASS_CDC_DATA;
64 ESP_LOGD(TAG, "CDC Data class initialized");
65 return ESP_OK;
66 } else {
67 ESP_LOGE(TAG, "CDC Data initialization error");
68 return ESP_FAIL;
69 }
70}
71
73{
74 ESP_RETURN_ON_ERROR(cdc_obj_check(itf, true, TUSB_CLASS_CDC_DATA), TAG, "cdc_obj_check failed");
75 free(cdc_obj[itf]);
76 cdc_obj[itf] = NULL;
77 return ESP_OK;
78}
79
81{
82 ESP_RETURN_ON_ERROR(cdc_obj_check(itf, false, -1), TAG, "cdc_obj_check failed");
83
84 ESP_LOGD(TAG, "Init CDC %d", itf);
85 if (cfg->cdc_class == TUSB_CLASS_CDC) {
86 ESP_RETURN_ON_ERROR(tusb_cdc_comm_init(itf), TAG, "tusb_cdc_comm_init failed");
87 cdc_obj[itf]->cdc_subclass.comm_subclass = cfg->cdc_subclass.comm_subclass;
88 } else {
89 ESP_RETURN_ON_ERROR(tusb_cdc_data_init(itf), TAG, "tusb_cdc_data_init failed");
90 cdc_obj[itf]->cdc_subclass.data_subclass = cfg->cdc_subclass.data_subclass;
91 }
92 cdc_obj[itf]->usb_dev = cfg->usb_dev;
93 return ESP_OK;
94}
95
97{
98 ESP_RETURN_ON_ERROR(cdc_obj_check(itf, true, -1), TAG, "cdc_obj_check failed");
99
100 ESP_LOGD(TAG, "Deinit CDC %d", itf);
101 if (cdc_obj[itf]->type == TUSB_CLASS_CDC) {
102 ESP_RETURN_ON_ERROR(tusb_cdc_deinit_comm(itf), TAG, "tusb_cdc_deinit_comm failed");
103 } else if (cdc_obj[itf]->type == TUSB_CLASS_CDC_DATA) {
104 ESP_RETURN_ON_ERROR(tusb_cdc_deinit_data(itf), TAG, "tusb_cdc_deinit_data failed");
105 } else {
106 return ESP_ERR_INVALID_ARG;
107 }
108 return ESP_OK;
109}
#define ESP_RETURN_ON_ERROR(x, log_tag, format,...)
esp_err_t tinyusb_cdc_deinit(int itf)
De-initializing CDC. Clean its objects.
Definition cdc.c:96
esp_tusb_cdc_t * tinyusb_cdc_get_intf(int itf_num)
Return interface of a CDC device.
Definition cdc.c:18
static esp_err_t tusb_cdc_comm_init(int itf)
Definition cdc.c:36
static esp_err_t tusb_cdc_deinit_comm(int itf)
Definition cdc.c:50
static esp_err_t cdc_obj_check(int itf, bool expected_inited, tusb_class_code_t expected_type)
Definition cdc.c:26
static esp_err_t tusb_cdc_deinit_data(int itf)
Definition cdc.c:72
#define CDC_INTF_NUM
Definition cdc.c:14
static esp_err_t tusb_cdc_data_init(int itf)
Definition cdc.c:58
static esp_tusb_cdc_t * cdc_obj[CFG_TUD_CDC]
Definition cdc.c:15
esp_err_t tinyusb_cdc_init(int itf, const tinyusb_config_cdc_t *cfg)
Initializing CDC basic object.
Definition cdc.c:80
int esp_err_t
Definition esp_err.h:21
#define ESP_OK
Definition esp_err.h:23
#define ESP_LOGD
Definition esp_log.h:22
static const char * TAG
Definition main/main.c:31
tusb_class_code_t type
Definition cdc.h:42
tinyusb_usbdev_t usb_dev
Definition cdc.h:32
cdc_comm_sublcass_type_t comm_subclass
Definition cdc.h:35
union tinyusb_config_cdc_t::@312104222326077254040135322351363045356166043242 cdc_subclass
cdc_data_sublcass_type_t data_subclass
Definition cdc.h:36
tusb_class_code_t cdc_class
Definition cdc.h:33