Skip to content

Commit 349837c

Browse files
author
Jamie C. Driver
committed
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.
1 parent 043cbc0 commit 349837c

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

main/power/jadev20.inc

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
#define SGM7220_REG_TYPE_SOURCE 0x01
3939
#define SGM7220_REG_TYPE_SINK 0x02
4040

41+
#ifdef USB_DETECTION
4142
#define USB_INT_PIN (gpio_num_t)18
42-
4343
static SemaphoreHandle_t usb_semaphore;
4444

4545
static void IRAM_ATTR usb_gpio_isr_handler(void* arg)
@@ -76,15 +76,13 @@ void usb_detection_task(void* param)
7676
}
7777
}
7878
}
79+
#endif // USB_DETECTION
7980

8081
// Exported funtions
8182
esp_err_t power_init(void)
8283
{
8384
I2C_CHECK_RET(_power_i2c_init());
8485

85-
usb_semaphore = xSemaphoreCreateBinary();
86-
JADE_ASSERT(usb_semaphore);
87-
8886
// Need read twice for some reason - first one always fails, second should work!
8987
uint8_t data;
9088
_power_master_read_slave(PMIC_ADDR, PMIC_REG_FW_VERSION, &data, 1);
@@ -94,23 +92,28 @@ esp_err_t power_init(void)
9492
// set to sink mode by default
9593
I2C_LOG_ANY_ERROR(_power_write_command(PMIC_ADDR, PMIC_REG_OTG, 0x00));
9694

95+
#ifdef USB_DETECTION
96+
usb_semaphore = xSemaphoreCreateBinary();
97+
JADE_ASSERT(usb_semaphore);
98+
9799
// Do usb detection for power boost
98100
gpio_set_intr_type(USB_INT_PIN, GPIO_INTR_NEGEDGE);
99101
I2C_CHECK_RET(gpio_install_isr_service(0));
100102
gpio_isr_handler_add(USB_INT_PIN, usb_gpio_isr_handler, (void*)USB_INT_PIN);
101103

102-
uint8_t sgm_id[8] = { 0 };
103-
I2C_CHECK_RET(_power_master_read_slave(SGM7220_ADDR, 0x00, &sgm_id[0], sizeof(sgm_id)));
104-
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],
105-
sgm_id[3], sgm_id[2], sgm_id[1], sgm_id[0]);
106-
I2C_CHECK_RET(_power_write_command(SGM7220_ADDR, 0x0A, 0x32));
107-
108104
const BaseType_t retval = xTaskCreatePinnedToCore(
109105
usb_detection_task, "usbdt", 1024 * 4, NULL, JADE_TASK_PRIO_IDLETIMER, NULL, JADE_CORE_GUI);
110106

111107
if (retval != pdPASS) {
112108
return ESP_FAIL;
113109
}
110+
#endif
111+
112+
uint8_t sgm_id[8] = { 0 };
113+
I2C_CHECK_RET(_power_master_read_slave(SGM7220_ADDR, 0x00, &sgm_id[0], sizeof(sgm_id)));
114+
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],
115+
sgm_id[3], sgm_id[2], sgm_id[1], sgm_id[0]);
116+
I2C_CHECK_RET(_power_write_command(SGM7220_ADDR, 0x0A, 0x32));
114117

115118
#ifdef CONFIG_SECURE_BOOT
116119
// Ensure 'download mode' permanently disabled

main/usbhmsc/usbhmsc.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,11 @@ bool usbstorage_start(void)
256256
JADE_SEMAPHORE_TAKE(interface_semaphore);
257257
JADE_ASSERT(!usbstorage_is_enabled);
258258
JADE_ASSERT(!main_task);
259-
/* enable_usb_host(); */
259+
260+
// Power any connected device
261+
JADE_ASSERT(!usb_connected());
262+
enable_usb_host();
263+
260264
usbstorage_is_enabled = true;
261265
const BaseType_t task_created = xTaskCreatePinnedToCore(
262266
usbstorage_task, "usb_storage", 2 * 1024, NULL, JADE_TASK_PRIO_USB, &main_task, JADE_CORE_SECONDARY);
@@ -278,7 +282,10 @@ void usbstorage_stop(void)
278282

279283
vTaskDelete(main_task);
280284
main_task = NULL;
281-
// disable_usb_host();
285+
286+
// Stop powering any connected usb device
287+
disable_usb_host();
288+
282289
JADE_SEMAPHORE_GIVE(interface_semaphore);
283290
}
284291

main/usbhmsc/usbmode.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "../jade_tasks.h"
55
#include "../jade_wally_verify.h"
66
#include "../keychain.h"
7+
#include "../power.h"
78
#include "../process.h"
89
#include "../serial.h"
910
#include "../ui.h"
@@ -284,6 +285,12 @@ static bool handle_usbstorage_action(
284285
JADE_ASSERT(usbstorage_action);
285286
// extra_path is optional
286287

288+
if (usb_connected()) {
289+
const char* message[] = { "Disconnect USB power and", "connect a storage device" };
290+
await_error_activity(message, 2);
291+
return false;
292+
}
293+
287294
// Stop normal serial usb and start usbstorage
288295
display_processing_message_activity();
289296
serial_stop();

0 commit comments

Comments
 (0)