tusb_console_main.c 1.9 KB

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