From 349837cec1d601374b6e52404ac1912f1a4fee21 Mon Sep 17 00:00:00 2001 From: "Jamie C. Driver" Date: Tue, 5 Nov 2024 17:32:53 +0000 Subject: [PATCH] usbstorage: explictly set usb source mode when accessing usb storage Activate usb source mode when navigating the menus to try to access usb storage, rather than setting when devices are detected on the usb-port. --- main/power/jadev20.inc | 23 +++++++++++++---------- main/usbhmsc/usbhmsc.c | 11 +++++++++-- main/usbhmsc/usbmode.c | 7 +++++++ 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/main/power/jadev20.inc b/main/power/jadev20.inc index fdcbeb6a..919d021e 100644 --- a/main/power/jadev20.inc +++ b/main/power/jadev20.inc @@ -38,8 +38,8 @@ #define SGM7220_REG_TYPE_SOURCE 0x01 #define SGM7220_REG_TYPE_SINK 0x02 +#ifdef USB_DETECTION #define USB_INT_PIN (gpio_num_t)18 - static SemaphoreHandle_t usb_semaphore; static void IRAM_ATTR usb_gpio_isr_handler(void* arg) @@ -76,15 +76,13 @@ void usb_detection_task(void* param) } } } +#endif // USB_DETECTION // Exported funtions esp_err_t power_init(void) { I2C_CHECK_RET(_power_i2c_init()); - usb_semaphore = xSemaphoreCreateBinary(); - JADE_ASSERT(usb_semaphore); - // Need read twice for some reason - first one always fails, second should work! uint8_t data; _power_master_read_slave(PMIC_ADDR, PMIC_REG_FW_VERSION, &data, 1); @@ -94,23 +92,28 @@ esp_err_t power_init(void) // set to sink mode by default I2C_LOG_ANY_ERROR(_power_write_command(PMIC_ADDR, PMIC_REG_OTG, 0x00)); +#ifdef USB_DETECTION + usb_semaphore = xSemaphoreCreateBinary(); + JADE_ASSERT(usb_semaphore); + // Do usb detection for power boost gpio_set_intr_type(USB_INT_PIN, GPIO_INTR_NEGEDGE); I2C_CHECK_RET(gpio_install_isr_service(0)); gpio_isr_handler_add(USB_INT_PIN, usb_gpio_isr_handler, (void*)USB_INT_PIN); - uint8_t sgm_id[8] = { 0 }; - I2C_CHECK_RET(_power_master_read_slave(SGM7220_ADDR, 0x00, &sgm_id[0], sizeof(sgm_id))); - JADE_LOGI("SGM7220 Device ID: %02x %02x %02x %02x %02x %02x %02x %02x", sgm_id[7], sgm_id[6], sgm_id[5], sgm_id[4], - sgm_id[3], sgm_id[2], sgm_id[1], sgm_id[0]); - I2C_CHECK_RET(_power_write_command(SGM7220_ADDR, 0x0A, 0x32)); - const BaseType_t retval = xTaskCreatePinnedToCore( usb_detection_task, "usbdt", 1024 * 4, NULL, JADE_TASK_PRIO_IDLETIMER, NULL, JADE_CORE_GUI); if (retval != pdPASS) { return ESP_FAIL; } +#endif + + uint8_t sgm_id[8] = { 0 }; + I2C_CHECK_RET(_power_master_read_slave(SGM7220_ADDR, 0x00, &sgm_id[0], sizeof(sgm_id))); + JADE_LOGI("SGM7220 Device ID: %02x %02x %02x %02x %02x %02x %02x %02x", sgm_id[7], sgm_id[6], sgm_id[5], sgm_id[4], + sgm_id[3], sgm_id[2], sgm_id[1], sgm_id[0]); + I2C_CHECK_RET(_power_write_command(SGM7220_ADDR, 0x0A, 0x32)); #ifdef CONFIG_SECURE_BOOT // Ensure 'download mode' permanently disabled diff --git a/main/usbhmsc/usbhmsc.c b/main/usbhmsc/usbhmsc.c index 7e476911..3bf7fb05 100644 --- a/main/usbhmsc/usbhmsc.c +++ b/main/usbhmsc/usbhmsc.c @@ -256,7 +256,11 @@ bool usbstorage_start(void) JADE_SEMAPHORE_TAKE(interface_semaphore); JADE_ASSERT(!usbstorage_is_enabled); JADE_ASSERT(!main_task); - /* enable_usb_host(); */ + + // Power any connected device + JADE_ASSERT(!usb_connected()); + enable_usb_host(); + usbstorage_is_enabled = true; const BaseType_t task_created = xTaskCreatePinnedToCore( usbstorage_task, "usb_storage", 2 * 1024, NULL, JADE_TASK_PRIO_USB, &main_task, JADE_CORE_SECONDARY); @@ -278,7 +282,10 @@ void usbstorage_stop(void) vTaskDelete(main_task); main_task = NULL; - // disable_usb_host(); + + // Stop powering any connected usb device + disable_usb_host(); + JADE_SEMAPHORE_GIVE(interface_semaphore); } diff --git a/main/usbhmsc/usbmode.c b/main/usbhmsc/usbmode.c index 4fa13f20..0c0c576a 100644 --- a/main/usbhmsc/usbmode.c +++ b/main/usbhmsc/usbmode.c @@ -4,6 +4,7 @@ #include "../jade_tasks.h" #include "../jade_wally_verify.h" #include "../keychain.h" +#include "../power.h" #include "../process.h" #include "../serial.h" #include "../ui.h" @@ -284,6 +285,12 @@ static bool handle_usbstorage_action( JADE_ASSERT(usbstorage_action); // extra_path is optional + if (usb_connected()) { + const char* message[] = { "Disconnect USB power and", "connect a storage device" }; + await_error_activity(message, 2); + return false; + } + // Stop normal serial usb and start usbstorage display_processing_message_activity(); serial_stop();