diff --git a/core/embed/bootloader/main.c b/core/embed/bootloader/main.c index 22ae945d9..efbd5e529 100644 --- a/core/embed/bootloader/main.c +++ b/core/embed/bootloader/main.c @@ -240,7 +240,53 @@ static void usb_init_all(secbool usb21_landing) { ensure(usb_webusb_add(&webusb_info), NULL); - usb_start(); + // usb start after vbus connected + // usb_start(); +} + +static void usb_switch(void) { + static bool usb_opened = false; + + if (!ble_charging_state()) { + ble_cmd_req(BLE_PWR, BLE_PWR_CHARGING); + return; + } + + if (ble_get_charge_type() == CHARGE_BY_USB) { + if (!usb_opened) { + usb_start(); + usb_opened = true; + } + } else { + if (usb_opened) { + usb_stop(); + usb_opened = false; + } + } +} + +static void charge_switch(void) { + static bool charge_configured = false; + static bool charge_enabled = false; + + if (!ble_charging_state()) { + ble_cmd_req(BLE_PWR, BLE_PWR_CHARGING); + return; + } + + if (ble_get_charge_type() == CHARGE_BY_USB) { + if (!charge_enabled || !charge_configured) { + charge_configured = true; + charge_enabled = true; + ble_cmd_req(BLE_PWR, BLE_PWR_CHARGE_ENABLE); + } + } else { + if (charge_enabled || !charge_configured) { + charge_configured = true; + charge_enabled = false; + ble_cmd_req(BLE_PWR, BLE_PWR_CHARGE_DISABLE); + } + } } static secbool bootloader_usb_loop(const vendor_header* const vhdr, @@ -256,6 +302,10 @@ static secbool bootloader_usb_loop(const vendor_header* const vhdr, while (true) { ble_uart_poll(); + usb_switch(); + + charge_switch(); + // check usb if (USB_PACKET_SIZE == spi_slave_poll(buf)) { host_channel = CHANNEL_SLAVE; diff --git a/core/embed/trezorhal/ble.c b/core/embed/trezorhal/ble.c index 56b9c4a24..08122acab 100644 --- a/core/embed/trezorhal/ble.c +++ b/core/embed/trezorhal/ble.c @@ -314,7 +314,8 @@ void ble_uart_poll(void) { break; case BLE_CMD_PLUG_STA: get_ble_charging = true; - if (ble_usart_msg.cmd_vale[0] == 1 || ble_usart_msg.cmd_vale[0] == 3) { + if (ble_usart_msg.cmd_vale[0] == 1 || ble_usart_msg.cmd_vale[0] == 3 || + ble_usart_msg.cmd_vale[0] == 4) { dev_pwr_sta = 1; if (ble_usart_msg.cmd_vale[1] == CHARGE_BY_USB || ble_usart_msg.cmd_vale[1] == CHARGE_BY_WIRELESS) { diff --git a/core/embed/trezorhal/ble.h b/core/embed/trezorhal/ble.h index b4ae27ad1..72b243e9b 100644 --- a/core/embed/trezorhal/ble.h +++ b/core/embed/trezorhal/ble.h @@ -45,6 +45,8 @@ extern uint8_t dev_pwr_sta; #define BLE_PWR_EMMC_ON 0x03 #define BLE_PWR_EQ 0x04 #define BLE_PWR_CHARGING 0x05 +#define BLE_PWR_CHARGE_ENABLE 0x06 +#define BLE_PWR_CHARGE_DISABLE 0x07 #define BLE_VER 0x83 #define BLE_VER_ADV 0x01 #define BLE_VER_FW 0x02