|
|
@@ -13,6 +13,7 @@
|
|
|
// limitations under the License.
|
|
|
|
|
|
#include <stdint.h>
|
|
|
+#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;
|