From 1b5894a5cb7fdc36da99c470ad9b661b16a3d844 Mon Sep 17 00:00:00 2001 From: Shu Chen Date: Fri, 2 Apr 2021 17:48:13 +0800 Subject: [PATCH] tinyusb: use the generic error check macros --- components/tinyusb/additions/src/cdc.c | 19 +++++----- .../tinyusb/additions/src/tusb_cdc_acm.c | 35 +++++++++---------- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/components/tinyusb/additions/src/cdc.c b/components/tinyusb/additions/src/cdc.c index bee57ee2df..ce46497f72 100644 --- a/components/tinyusb/additions/src/cdc.c +++ b/components/tinyusb/additions/src/cdc.c @@ -13,6 +13,7 @@ // limitations under the License. #include +#include "esp_check.h" #include "esp_err.h" #include "esp_log.h" #include "tusb.h" @@ -21,8 +22,6 @@ static const char *TAG = "tusb_cdc"; -#define ESP_RETURN_ON_ERROR(x) do {esp_err_t r = (x); if (r != ESP_OK) return r;} while(0) - #define CDC_INTF_NUM CFG_TUD_CDC // number of cdc blocks static esp_tusb_cdc_t *cdc_obj[CDC_INTF_NUM] = {}; @@ -80,7 +79,7 @@ esp_tusb_cdc_t *tinyusb_cdc_get_intf(int itf_num) ********************************************************************* */ static esp_err_t tusb_cdc_comm_init(int itf) { - ESP_RETURN_ON_ERROR(cdc_obj_check(itf, false, -1)); + ESP_RETURN_ON_ERROR(cdc_obj_check(itf, false, -1), TAG, "cdc_obj_check failed"); cdc_obj[itf] = calloc(1, sizeof(esp_tusb_cdc_t)); if (cdc_obj[itf] != NULL) { cdc_obj[itf]->type = TUSB_CLASS_CDC; @@ -94,7 +93,7 @@ static esp_err_t tusb_cdc_comm_init(int itf) static esp_err_t tusb_cdc_deinit_comm(int itf) { - ESP_RETURN_ON_ERROR(cdc_obj_check(itf, true, TUSB_CLASS_CDC)); + ESP_RETURN_ON_ERROR(cdc_obj_check(itf, true, TUSB_CLASS_CDC), TAG, "cdc_obj_check failed"); free(cdc_obj[itf]); cdc_obj[itf] = NULL; return ESP_OK; @@ -102,7 +101,7 @@ static esp_err_t tusb_cdc_deinit_comm(int itf) static esp_err_t tusb_cdc_data_init(int itf) { - ESP_RETURN_ON_ERROR(cdc_obj_check(itf, false, TUSB_CLASS_CDC_DATA)); + ESP_RETURN_ON_ERROR(cdc_obj_check(itf, false, TUSB_CLASS_CDC_DATA), TAG, "cdc_obj_check failed"); cdc_obj[itf] = calloc(1, sizeof(esp_tusb_cdc_t)); if (cdc_obj[itf] != NULL) { cdc_obj[itf]->type = TUSB_CLASS_CDC_DATA; @@ -116,7 +115,7 @@ static esp_err_t tusb_cdc_data_init(int itf) static esp_err_t tusb_cdc_deinit_data(int itf) { - ESP_RETURN_ON_ERROR(cdc_obj_check(itf, true, TUSB_CLASS_CDC_DATA)); + ESP_RETURN_ON_ERROR(cdc_obj_check(itf, true, TUSB_CLASS_CDC_DATA), TAG, "cdc_obj_check failed"); free(cdc_obj[itf]); cdc_obj[itf] = NULL; return ESP_OK; @@ -132,10 +131,10 @@ esp_err_t tinyusb_cdc_init(int itf, const tinyusb_config_cdc_t *cfg) return ESP_ERR_INVALID_ARG; } if (cfg->cdc_class == TUSB_CLASS_CDC) { - ESP_RETURN_ON_ERROR(tusb_cdc_comm_init(itf)); + ESP_RETURN_ON_ERROR(tusb_cdc_comm_init(itf), TAG, "tusb_cdc_comm_init failed"); cdc_obj[itf]->cdc_subclass.comm_subclass = cfg->cdc_subclass.comm_subclass; } else { - ESP_RETURN_ON_ERROR(tusb_cdc_data_init(itf)); + ESP_RETURN_ON_ERROR(tusb_cdc_data_init(itf), TAG, "tusb_cdc_data_init failed"); cdc_obj[itf]->cdc_subclass.data_subclass = cfg->cdc_subclass.data_subclass; } cdc_obj[itf]->usb_dev = cfg->usb_dev; @@ -150,9 +149,9 @@ esp_err_t tinyusb_cdc_deinit(int itf) return ESP_ERR_INVALID_ARG; } if (cdc_obj[itf]->type == TUSB_CLASS_CDC) { - ESP_RETURN_ON_ERROR(tusb_cdc_deinit_comm(itf)); + ESP_RETURN_ON_ERROR(tusb_cdc_deinit_comm(itf), TAG, "tusb_cdc_deinit_comm failed"); } else if (cdc_obj[itf]->type == TUSB_CLASS_CDC_DATA) { - ESP_RETURN_ON_ERROR(tusb_cdc_deinit_data(itf)); + ESP_RETURN_ON_ERROR(tusb_cdc_deinit_data(itf), TAG, "tusb_cdc_deinit_data failed"); } else { return ESP_ERR_INVALID_ARG; } diff --git a/components/tinyusb/additions/src/tusb_cdc_acm.c b/components/tinyusb/additions/src/tusb_cdc_acm.c index 762c1de8af..b29c483bca 100644 --- a/components/tinyusb/additions/src/tusb_cdc_acm.c +++ b/components/tinyusb/additions/src/tusb_cdc_acm.c @@ -13,6 +13,7 @@ // limitations under the License. #include +#include "esp_check.h" #include "esp_err.h" #include "esp_log.h" #include "freertos/FreeRTOS.h" @@ -22,8 +23,6 @@ #include "cdc.h" #include "sdkconfig.h" -#define ESP_RETURN_ON_ERROR(x) do{esp_err_t r = (x); if (r != ESP_OK) return r;} while(0) - #define RX_UNREADBUF_SZ_DEFAULT 64 // buffer storing all unread RX data @@ -240,15 +239,17 @@ static esp_err_t read_from_rx_unread_to_buffer(esp_tusb_cdcacm_t *acm, uint8_t * } } -static esp_err_t ringbuf_mux_take(esp_tusb_cdcacm_t *acm){ - if(xSemaphoreTake(acm->ringbuf_read_mux, 0) != pdTRUE){ +static esp_err_t ringbuf_mux_take(esp_tusb_cdcacm_t *acm) +{ + if (xSemaphoreTake(acm->ringbuf_read_mux, 0) != pdTRUE) { ESP_LOGW(TAG, "Read error: ACM is busy"); return ESP_ERR_INVALID_STATE; } return ESP_OK; } -static esp_err_t ringbuf_mux_give(esp_tusb_cdcacm_t *acm) { +static esp_err_t ringbuf_mux_give(esp_tusb_cdcacm_t *acm) +{ BaseType_t ret = xSemaphoreGive(acm->ringbuf_read_mux); assert(ret == pdTRUE); return ESP_OK; @@ -257,28 +258,25 @@ static esp_err_t ringbuf_mux_give(esp_tusb_cdcacm_t *acm) { esp_err_t tinyusb_cdcacm_read(tinyusb_cdcacm_itf_t itf, uint8_t *out_buf, size_t out_buf_sz, size_t *rx_data_size) { esp_tusb_cdcacm_t *acm = get_acm(itf); - if (!acm) { - ESP_LOGE(TAG, "Interface is not initialized. Use `tinyusb_cdc_init` for initialization"); - return ESP_ERR_INVALID_STATE; - } + ESP_RETURN_ON_FALSE(acm, ESP_ERR_INVALID_STATE, TAG, "Interface is not initialized. Use `tinyusb_cdc_init` for initialization"); size_t read_sz; /* Take a mutex to proceed two uninterrupted read operations */ - ESP_RETURN_ON_ERROR(ringbuf_mux_take(acm)); + ESP_RETURN_ON_ERROR(ringbuf_mux_take(acm), TAG, "ringbuf_mux_take failed"); esp_err_t res = read_from_rx_unread_to_buffer(acm, out_buf, out_buf_sz, &read_sz); - if (res != ESP_OK){ - ESP_RETURN_ON_ERROR(ringbuf_mux_give(acm)); + if (res != ESP_OK) { + ESP_RETURN_ON_ERROR(ringbuf_mux_give(acm), TAG, "ringbuf_mux_give failed"); return res; } *rx_data_size = read_sz; /* Buffer's data can be wrapped, at that situations we should make another retrievement */ - if (read_from_rx_unread_to_buffer(acm, out_buf+read_sz, out_buf_sz-read_sz, &read_sz) == ESP_OK) { + if (read_from_rx_unread_to_buffer(acm, out_buf + read_sz, out_buf_sz - read_sz, &read_sz) == ESP_OK) { *rx_data_size += read_sz; } - ESP_RETURN_ON_ERROR(ringbuf_mux_give(acm)); + ESP_RETURN_ON_ERROR(ringbuf_mux_give(acm), TAG, "ringbuf_mux_give failed"); return ESP_OK; } @@ -300,7 +298,8 @@ size_t tinyusb_cdcacm_write_queue(tinyusb_cdcacm_itf_t itf, uint8_t *in_buf, siz return tud_cdc_n_write(itf, in_buf, in_size); } -static uint32_t tud_cdc_n_write_occupied(tinyusb_cdcacm_itf_t itf){ +static uint32_t tud_cdc_n_write_occupied(tinyusb_cdcacm_itf_t itf) +{ return CFG_TUD_CDC_TX_BUFSIZE - tud_cdc_n_write_available(itf); } @@ -370,8 +369,8 @@ esp_err_t tusb_cdc_acm_init(const tinyusb_config_cdcacm_t *cfg) .cdc_class = TUSB_CLASS_CDC, .cdc_subclass.comm_subclass = CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL }; - ESP_RETURN_ON_ERROR(tinyusb_cdc_init(itf, &cdc_cfg)); - ESP_RETURN_ON_ERROR(alloc_obj(itf)); + ESP_RETURN_ON_ERROR(tinyusb_cdc_init(itf, &cdc_cfg), TAG, "tinyusb_cdc_init failed"); + ESP_RETURN_ON_ERROR(alloc_obj(itf), TAG, "alloc_obj failed"); esp_tusb_cdcacm_t *acm = get_acm(itf); /* Callbacks setting up*/ @@ -391,7 +390,7 @@ esp_err_t tusb_cdc_acm_init(const tinyusb_config_cdcacm_t *cfg) /* Buffers */ acm->ringbuf_read_mux = xSemaphoreCreateMutex(); - if (acm->ringbuf_read_mux == NULL){ + if (acm->ringbuf_read_mux == NULL) { ESP_LOGE(TAG, "Creation of a ringbuf mutex failed"); free_obj(itf); return ESP_ERR_NO_MEM;