2022-01-04 02:17:32 +00:00
/*
* SPDX - FileCopyrightText : 2020 - 2022 Espressif Systems ( Shanghai ) CO LTD
*
* SPDX - License - Identifier : Apache - 2.0
*/
2020-08-06 12:41:32 +00:00
# include <stdint.h>
2021-04-02 09:48:13 +00:00
# include "esp_check.h"
2020-08-06 12:41:32 +00:00
# include "esp_err.h"
# include "esp_log.h"
# include "tusb.h"
# include "cdc.h"
# define CDC_INTF_NUM CFG_TUD_CDC // number of cdc blocks
static esp_tusb_cdc_t * cdc_obj [ CDC_INTF_NUM ] = { } ;
2021-12-16 06:50:08 +00:00
static const char * TAG = " tusb_cdc " ;
2020-08-06 12:41:32 +00:00
2021-12-16 06:50:08 +00:00
esp_tusb_cdc_t * tinyusb_cdc_get_intf ( int itf_num )
2020-08-06 12:41:32 +00:00
{
2021-12-16 06:50:08 +00:00
if ( itf_num > = CDC_INTF_NUM | | itf_num < 0 ) {
return NULL ;
2020-08-06 12:41:32 +00:00
}
2021-12-16 06:50:08 +00:00
return cdc_obj [ itf_num ] ;
2020-08-06 12:41:32 +00:00
}
static esp_err_t cdc_obj_check ( int itf , bool expected_inited , tusb_class_code_t expected_type )
{
2021-12-16 06:50:08 +00:00
esp_tusb_cdc_t * this_itf = tinyusb_cdc_get_intf ( itf ) ;
2020-08-06 12:41:32 +00:00
2021-12-16 06:50:08 +00:00
bool inited = ( this_itf ! = NULL ) ;
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 " ) ;
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 ) ;
return ESP_OK ;
2020-08-06 12:41:32 +00:00
}
static esp_err_t tusb_cdc_comm_init ( int itf )
{
2021-04-02 09:48:13 +00:00
ESP_RETURN_ON_ERROR ( cdc_obj_check ( itf , false , - 1 ) , TAG , " cdc_obj_check failed " ) ;
2020-08-06 12:41:32 +00:00
cdc_obj [ itf ] = calloc ( 1 , sizeof ( esp_tusb_cdc_t ) ) ;
if ( cdc_obj [ itf ] ! = NULL ) {
cdc_obj [ itf ] - > type = TUSB_CLASS_CDC ;
ESP_LOGD ( TAG , " CDC Comm class initialized " ) ;
return ESP_OK ;
} else {
ESP_LOGE ( TAG , " CDC Comm initialization error " ) ;
return ESP_FAIL ;
}
}
static esp_err_t tusb_cdc_deinit_comm ( int itf )
{
2021-04-02 09:48:13 +00:00
ESP_RETURN_ON_ERROR ( cdc_obj_check ( itf , true , TUSB_CLASS_CDC ) , TAG , " cdc_obj_check failed " ) ;
2020-08-06 12:41:32 +00:00
free ( cdc_obj [ itf ] ) ;
cdc_obj [ itf ] = NULL ;
return ESP_OK ;
}
static esp_err_t tusb_cdc_data_init ( int itf )
{
2021-04-02 09:48:13 +00:00
ESP_RETURN_ON_ERROR ( cdc_obj_check ( itf , false , TUSB_CLASS_CDC_DATA ) , TAG , " cdc_obj_check failed " ) ;
2020-08-06 12:41:32 +00:00
cdc_obj [ itf ] = calloc ( 1 , sizeof ( esp_tusb_cdc_t ) ) ;
if ( cdc_obj [ itf ] ! = NULL ) {
cdc_obj [ itf ] - > type = TUSB_CLASS_CDC_DATA ;
ESP_LOGD ( TAG , " CDC Data class initialized " ) ;
return ESP_OK ;
} else {
ESP_LOGE ( TAG , " CDC Data initialization error " ) ;
return ESP_FAIL ;
}
}
static esp_err_t tusb_cdc_deinit_data ( int itf )
{
2021-04-02 09:48:13 +00:00
ESP_RETURN_ON_ERROR ( cdc_obj_check ( itf , true , TUSB_CLASS_CDC_DATA ) , TAG , " cdc_obj_check failed " ) ;
2020-08-06 12:41:32 +00:00
free ( cdc_obj [ itf ] ) ;
cdc_obj [ itf ] = NULL ;
return ESP_OK ;
}
2021-12-16 06:50:08 +00:00
2020-08-06 12:41:32 +00:00
esp_err_t tinyusb_cdc_init ( int itf , const tinyusb_config_cdc_t * cfg )
{
2021-12-16 06:50:08 +00:00
ESP_RETURN_ON_ERROR ( cdc_obj_check ( itf , false , - 1 ) , TAG , " cdc_obj_check failed " ) ;
ESP_LOGD ( TAG , " Init CDC %d " , itf ) ;
2020-08-06 12:41:32 +00:00
if ( cfg - > cdc_class = = TUSB_CLASS_CDC ) {
2021-04-02 09:48:13 +00:00
ESP_RETURN_ON_ERROR ( tusb_cdc_comm_init ( itf ) , TAG , " tusb_cdc_comm_init failed " ) ;
2020-08-06 12:41:32 +00:00
cdc_obj [ itf ] - > cdc_subclass . comm_subclass = cfg - > cdc_subclass . comm_subclass ;
} else {
2021-04-02 09:48:13 +00:00
ESP_RETURN_ON_ERROR ( tusb_cdc_data_init ( itf ) , TAG , " tusb_cdc_data_init failed " ) ;
2020-08-06 12:41:32 +00:00
cdc_obj [ itf ] - > cdc_subclass . data_subclass = cfg - > cdc_subclass . data_subclass ;
}
cdc_obj [ itf ] - > usb_dev = cfg - > usb_dev ;
return ESP_OK ;
}
esp_err_t tinyusb_cdc_deinit ( int itf )
{
2021-12-16 06:50:08 +00:00
ESP_RETURN_ON_ERROR ( cdc_obj_check ( itf , true , - 1 ) , TAG , " cdc_obj_check failed " ) ;
ESP_LOGD ( TAG , " Deinit CDC %d " , itf ) ;
2020-08-06 12:41:32 +00:00
if ( cdc_obj [ itf ] - > type = = TUSB_CLASS_CDC ) {
2021-04-02 09:48:13 +00:00
ESP_RETURN_ON_ERROR ( tusb_cdc_deinit_comm ( itf ) , TAG , " tusb_cdc_deinit_comm failed " ) ;
2020-08-06 12:41:32 +00:00
} else if ( cdc_obj [ itf ] - > type = = TUSB_CLASS_CDC_DATA ) {
2021-04-02 09:48:13 +00:00
ESP_RETURN_ON_ERROR ( tusb_cdc_deinit_data ( itf ) , TAG , " tusb_cdc_deinit_data failed " ) ;
2020-08-06 12:41:32 +00:00
} else {
return ESP_ERR_INVALID_ARG ;
}
return ESP_OK ;
}