#include #include #include "sdkconfig.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_chip_info.h" #include "esp_flash.h" #include "esp_system.h" #include "driver/gpio.h" #include "esp_system.h" #include "esp_log.h" #include "tinyusb.h" #include "tusb_cdc_acm.h" #include "module/include/Sys_Printer.h" #include "module/Printer_Config.h" static const char *TAG = "example"; #define BLINK_GPIO 4 static uint8_t buf[CONFIG_TINYUSB_CDC_RX_BUFSIZE + 1]; void tinyusb_cdc_rx_callback(int itf, cdcacm_event_t *event) { /* initialization */ size_t rx_size = 0; /* read */ esp_err_t ret = tinyusb_cdcacm_read(itf, buf, CONFIG_TINYUSB_CDC_RX_BUFSIZE, &rx_size); if (ret == ESP_OK) { ESP_LOGI(TAG, "Data from channel %d:", itf); ESP_LOG_BUFFER_HEXDUMP(TAG, buf, rx_size, ESP_LOG_INFO); } else { ESP_LOGE(TAG, "Read error"); } /* write back */ tinyusb_cdcacm_write_queue(itf, buf, rx_size); tinyusb_cdcacm_write_flush(itf, 0); } void tinyusb_cdc_line_state_changed_callback(int itf, cdcacm_event_t *event) { int dtr = event->line_state_changed_data.dtr; int rts = event->line_state_changed_data.rts; ESP_LOGI(TAG, "Line state changed on channel %d: DTR:%d, RTS:%d", itf, dtr, rts); } void app_main(void) { /* Print chip information */ esp_chip_info_t chip_info; uint32_t flash_size; esp_chip_info(&chip_info); printf("This is %s chip with %d CPU core(s), %s%s%s%s, ", CONFIG_IDF_TARGET, chip_info.cores, (chip_info.features & CHIP_FEATURE_WIFI_BGN) ? "WiFi/" : "", (chip_info.features & CHIP_FEATURE_BT) ? "BT" : "", (chip_info.features & CHIP_FEATURE_BLE) ? "BLE" : "", (chip_info.features & CHIP_FEATURE_IEEE802154) ? ", 802.15.4 (Zigbee/Thread)" : ""); unsigned major_rev = chip_info.revision / 100; unsigned minor_rev = chip_info.revision % 100; printf("silicon revision v%d.%d, ", major_rev, minor_rev); if(esp_flash_get_size(NULL, &flash_size) != ESP_OK) { printf("Get flash size failed"); return; } // ESP_ERROR_CHECK(gpio_config()); printf("%" PRIu32 "MB %s flash\n", flash_size / (uint32_t)(1024 * 1024), (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external"); printf("Minimum free heap size: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size()); ESP_LOGI(TAG, "USB initialization"); const tinyusb_config_t tusb_cfg = { .device_descriptor = NULL, .string_descriptor = NULL, .external_phy = false, #if (TUD_OPT_HIGH_SPEED) .fs_configuration_descriptor = NULL, .hs_configuration_descriptor = NULL, .qualifier_descriptor = NULL, #else .configuration_descriptor = NULL, #endif // TUD_OPT_HIGH_SPEED }; ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg)); tinyusb_config_cdcacm_t acm_cfg = { .usb_dev = TINYUSB_USBDEV_0, .cdc_port = TINYUSB_CDC_ACM_0, .rx_unread_buf_sz = 64, .callback_rx = &tinyusb_cdc_rx_callback, // the first way to register a callback .callback_rx_wanted_char = NULL, .callback_line_state_changed = NULL, .callback_line_coding_changed = NULL }; ESP_ERROR_CHECK(tusb_cdc_acm_init(&acm_cfg)); /* the second way to register a callback */ ESP_ERROR_CHECK(tinyusb_cdcacm_register_callback( TINYUSB_CDC_ACM_0, CDC_EVENT_LINE_STATE_CHANGED, &tinyusb_cdc_line_state_changed_callback)); #if (CONFIG_TINYUSB_CDC_COUNT > 1) acm_cfg.cdc_port = TINYUSB_CDC_ACM_1; ESP_ERROR_CHECK(tusb_cdc_acm_init(&acm_cfg)); ESP_ERROR_CHECK(tinyusb_cdcacm_register_callback( TINYUSB_CDC_ACM_1, CDC_EVENT_LINE_STATE_CHANGED, &tinyusb_cdc_line_state_changed_callback)); #endif ESP_LOGI(TAG, "USB initialization DONE"); Printer_group = xEventGroupCreate(); button_init(); Printer_Bsp_Init(); Printer_run_init(); while (1) { // Motor_GO(1); /* Toggle the LED state */ // if(gpio_get_level(FEED_KEY.pin_num) == 0) // { // // ESP_LOGI(TAG, "FEED_KEY:%d",0); // //Motor_GO(1); // xEventGroupSetBits(Printer_group,EVENT_Motor_Test); // while (gpio_get_level(FEED_KEY.pin_num) == 0) { // vTaskDelay(10 / portTICK_PERIOD_MS); // } // }else{ // // Motor_GO(0xff); // } vTaskDelay(1000 / portTICK_PERIOD_MS); // ESP_LOGI(TAG, "LED State:%d",!s_led_state); } fflush(stdout); esp_restart(); }