Skip to content

Commit

Permalink
usbstorage: explictly set usb source mode when accessing usb storage
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JamieDriver committed Nov 6, 2024
1 parent 043cbc0 commit 349837c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
23 changes: 13 additions & 10 deletions main/power/jadev20.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down
11 changes: 9 additions & 2 deletions main/usbhmsc/usbhmsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}

Expand Down
7 changes: 7 additions & 0 deletions main/usbhmsc/usbmode.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 349837c

Please sign in to comment.