1 /* USB Example
2
3 This example code is in the Public Domain (or CC0 licensed, at your option.)
4
5 Unless required by applicable law or agreed to in writing, this
6 software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7 CONDITIONS OF ANY KIND, either express or implied.
8 */
9
10 // DESCRIPTION:
11 // This example contains minimal code to make ESP32-S2 based device
12 // recognizable by USB-host devices as a USB Serial Device printing output from
13 // the application.
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <sys/reent.h>
18 #include "esp_log.h"
19 #include "esp_vfs.h"
20 #include "freertos/FreeRTOS.h"
21 #include "freertos/task.h"
22 #include "tinyusb.h"
23 #include "tusb_cdc_acm.h"
24 #include "tusb_console.h"
25 #include "sdkconfig.h"
26
27 static const char *TAG = "example";
28
app_main(void)29 void app_main(void)
30 {
31 /* Setting TinyUSB up */
32 ESP_LOGI(TAG, "USB initialization");
33
34 tinyusb_config_t tusb_cfg = { 0 }; // the configuration uses default values
35 ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
36
37 tinyusb_config_cdcacm_t amc_cfg = { 0 }; // the configuration uses default values
38 ESP_ERROR_CHECK(tusb_cdc_acm_init(&amc_cfg));
39
40 ESP_LOGI(TAG, "USB initialization DONE");
41 while (1) {
42 ESP_LOGI(TAG, "log -> UART");
43 vTaskDelay(1000 / portTICK_PERIOD_MS);
44 fprintf(stdout, "example: print -> stdout\n");
45 vTaskDelay(1000 / portTICK_PERIOD_MS);
46 fprintf(stderr, "example: print -> stderr\n");
47 vTaskDelay(1000 / portTICK_PERIOD_MS);
48
49 esp_tusb_init_console(TINYUSB_CDC_ACM_0); // log to usb
50 ESP_LOGI(TAG, "log -> USB");
51 vTaskDelay(1000 / portTICK_PERIOD_MS);
52 fprintf(stdout, "example: print -> stdout\n");
53 vTaskDelay(1000 / portTICK_PERIOD_MS);
54 fprintf(stderr, "example: print -> stderr\n");
55 vTaskDelay(1000 / portTICK_PERIOD_MS);
56 esp_tusb_deinit_console(TINYUSB_CDC_ACM_0); // log to uart
57 }
58 }
59