tusb_console_main.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 "esp_vfs.h"
  15. #include "freertos/FreeRTOS.h"
  16. #include "freertos/task.h"
  17. #include "tinyusb.h"
  18. #include "tusb_cdc_acm.h"
  19. #include "tusb_console.h"
  20. #include "sdkconfig.h"
  21. static const char *TAG = "example";
  22. void app_main(void)
  23. {
  24. /* Setting TinyUSB up */
  25. ESP_LOGI(TAG, "USB initialization");
  26. const tinyusb_config_t tusb_cfg = {
  27. .device_descriptor = NULL,
  28. .string_descriptor = NULL,
  29. .external_phy = false, // In the most cases you need to use a `false` value
  30. .configuration_descriptor = NULL,
  31. };
  32. ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
  33. tinyusb_config_cdcacm_t acm_cfg = { 0 }; // the configuration uses default values
  34. ESP_ERROR_CHECK(tusb_cdc_acm_init(&acm_cfg));
  35. ESP_LOGI(TAG, "USB initialization DONE");
  36. while (1) {
  37. ESP_LOGI(TAG, "log -> UART");
  38. vTaskDelay(1000 / portTICK_PERIOD_MS);
  39. fprintf(stdout, "example: print -> stdout\n");
  40. vTaskDelay(1000 / portTICK_PERIOD_MS);
  41. fprintf(stderr, "example: print -> stderr\n");
  42. vTaskDelay(1000 / portTICK_PERIOD_MS);
  43. esp_tusb_init_console(TINYUSB_CDC_ACM_0); // log to usb
  44. ESP_LOGI(TAG, "log -> USB");
  45. vTaskDelay(1000 / portTICK_PERIOD_MS);
  46. fprintf(stdout, "example: print -> stdout\n");
  47. vTaskDelay(1000 / portTICK_PERIOD_MS);
  48. fprintf(stderr, "example: print -> stderr\n");
  49. vTaskDelay(1000 / portTICK_PERIOD_MS);
  50. esp_tusb_deinit_console(TINYUSB_CDC_ACM_0); // log to uart
  51. }
  52. }