11/*
2- * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
2+ * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD
33 *
44 * SPDX-License-Identifier: Unlicense OR CC0-1.0
55 */
88#include "esp_log.h"
99#include "freertos/FreeRTOS.h"
1010#include "freertos/task.h"
11+ #include "freertos/queue.h"
1112#include "tinyusb.h"
1213#include "tusb_cdc_acm.h"
1314#include "sdkconfig.h"
1415
1516static const char * TAG = "example" ;
16- static uint8_t buf [CONFIG_TINYUSB_CDC_RX_BUFSIZE + 1 ];
17+ static uint8_t rx_buf [CONFIG_TINYUSB_CDC_RX_BUFSIZE + 1 ];
1718
19+ /**
20+ * @brief Application Queue
21+ */
22+ static QueueHandle_t app_queue ;
23+ typedef struct {
24+ uint8_t buf [CONFIG_TINYUSB_CDC_RX_BUFSIZE + 1 ]; // Data buffer
25+ size_t buf_len ; // Number of bytes received
26+ uint8_t itf ; // Index of CDC device interface
27+ } app_message_t ;
28+
29+ /**
30+ * @brief CDC device RX callback
31+ *
32+ * CDC device signals, that new data were received
33+ *
34+ * @param[in] itf CDC device index
35+ * @param[in] event CDC event type
36+ */
1837void tinyusb_cdc_rx_callback (int itf , cdcacm_event_t * event )
1938{
2039 /* initialization */
2140 size_t rx_size = 0 ;
2241
2342 /* read */
24- esp_err_t ret = tinyusb_cdcacm_read (itf , buf , CONFIG_TINYUSB_CDC_RX_BUFSIZE , & rx_size );
43+ esp_err_t ret = tinyusb_cdcacm_read (itf , rx_buf , CONFIG_TINYUSB_CDC_RX_BUFSIZE , & rx_size );
2544 if (ret == ESP_OK ) {
26- ESP_LOGI (TAG , "Data from channel %d:" , itf );
27- ESP_LOG_BUFFER_HEXDUMP (TAG , buf , rx_size , ESP_LOG_INFO );
45+
46+ app_message_t tx_msg = {
47+ .buf_len = rx_size ,
48+ .itf = itf ,
49+ };
50+
51+ memcpy (tx_msg .buf , rx_buf , rx_size );
52+ xQueueSend (app_queue , & tx_msg , 0 );
2853 } else {
29- ESP_LOGE (TAG , "Read error " );
54+ ESP_LOGE (TAG , "Read Error " );
3055 }
31-
32- /* write back */
33- tinyusb_cdcacm_write_queue (itf , buf , rx_size );
34- tinyusb_cdcacm_write_flush (itf , 0 );
3556}
3657
58+ /**
59+ * @brief CDC device line change callback
60+ *
61+ * CDC device signals, that the DTR, RTS states changed
62+ *
63+ * @param[in] itf CDC device index
64+ * @param[in] event CDC event type
65+ */
3766void tinyusb_cdc_line_state_changed_callback (int itf , cdcacm_event_t * event )
3867{
3968 int dtr = event -> line_state_changed_data .dtr ;
@@ -43,12 +72,23 @@ void tinyusb_cdc_line_state_changed_callback(int itf, cdcacm_event_t *event)
4372
4473void app_main (void )
4574{
75+ // Create FreeRTOS primitives
76+ app_queue = xQueueCreate (5 , sizeof (app_message_t ));
77+ assert (app_queue );
78+ app_message_t msg ;
79+
4680 ESP_LOGI (TAG , "USB initialization" );
4781 const tinyusb_config_t tusb_cfg = {
4882 .device_descriptor = NULL ,
4983 .string_descriptor = NULL ,
5084 .external_phy = false,
85+ #if (TUD_OPT_HIGH_SPEED )
86+ .fs_configuration_descriptor = NULL ,
87+ .hs_configuration_descriptor = NULL ,
88+ .qualifier_descriptor = NULL ,
89+ #else
5190 .configuration_descriptor = NULL ,
91+ #endif // TUD_OPT_HIGH_SPEED
5292 };
5393
5494 ESP_ERROR_CHECK (tinyusb_driver_install (& tusb_cfg ));
@@ -80,4 +120,21 @@ void app_main(void)
80120#endif
81121
82122 ESP_LOGI (TAG , "USB initialization DONE" );
123+ while (1 ) {
124+ if (xQueueReceive (app_queue , & msg , portMAX_DELAY )) {
125+ if (msg .buf_len ) {
126+
127+ /* Print received data*/
128+ ESP_LOGI (TAG , "Data from channel %d:" , msg .itf );
129+ ESP_LOG_BUFFER_HEXDUMP (TAG , msg .buf , msg .buf_len , ESP_LOG_INFO );
130+
131+ /* write back */
132+ tinyusb_cdcacm_write_queue (msg .itf , msg .buf , msg .buf_len );
133+ esp_err_t err = tinyusb_cdcacm_write_flush (msg .itf , 0 );
134+ if (err != ESP_OK ) {
135+ ESP_LOGE (TAG , "CDC ACM write flush error: %s" , esp_err_to_name (err ));
136+ }
137+ }
138+ }
139+ }
83140}
0 commit comments