tusb_console_main.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. // DESCRIPTION:
  7. // This example contains minimal code to make ESP32-S2 based device
  8. // recognizable by USB-host devices as a USB Serial Device printing output from
  9. // the application.
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <sys/reent.h>
  13. #include "esp_log.h"
  14. #include "freertos/FreeRTOS.h"
  15. #include "freertos/task.h"
  16. #include "tinyusb.h"
  17. #include "tusb_cdc_acm.h"
  18. #include "tusb_console.h"
  19. #include "sdkconfig.h"
  20. static const char *TAG = "example";
  21. void app_main(void)
  22. {
  23. /* Setting TinyUSB up */
  24. ESP_LOGI(TAG, "USB initialization");
  25. const tinyusb_config_t tusb_cfg = {
  26. .device_descriptor = NULL,
  27. .string_descriptor = NULL,
  28. .external_phy = false, // In the most cases you need to use a `false` value
  29. .configuration_descriptor = NULL,
  30. };
  31. ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
  32. tinyusb_config_cdcacm_t acm_cfg = { 0 }; // the configuration uses default values
  33. ESP_ERROR_CHECK(tusb_cdc_acm_init(&acm_cfg));
  34. ESP_LOGI(TAG, "USB initialization DONE");
  35. while (1) {
  36. ESP_LOGI(TAG, "log -> UART");
  37. vTaskDelay(1000 / portTICK_PERIOD_MS);
  38. fprintf(stdout, "example: print -> stdout\n");
  39. vTaskDelay(1000 / portTICK_PERIOD_MS);
  40. fprintf(stderr, "example: print -> stderr\n");
  41. vTaskDelay(1000 / portTICK_PERIOD_MS);
  42. esp_tusb_init_console(TINYUSB_CDC_ACM_0); // log to usb
  43. ESP_LOGI(TAG, "log -> USB");
  44. vTaskDelay(1000 / portTICK_PERIOD_MS);
  45. fprintf(stdout, "example: print -> stdout\n");
  46. vTaskDelay(1000 / portTICK_PERIOD_MS);
  47. fprintf(stderr, "example: print -> stderr\n");
  48. vTaskDelay(1000 / portTICK_PERIOD_MS);
  49. esp_tusb_deinit_console(TINYUSB_CDC_ACM_0); // log to uart
  50. }
  51. }