Skip to content

Commit

Permalink
boot:Start USB only after detecting a connection && Enable charging w…
Browse files Browse the repository at this point in the history
…hen USB is connected. (#126)

* bootloader:Start USB only after detecting a connection.

* bootloader:Enable charging when USB is connected.
  • Loading branch information
lihuanhuan authored Jun 11, 2024
1 parent 8d7fece commit a52aa18
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
52 changes: 51 additions & 1 deletion core/embed/bootloader/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion core/embed/trezorhal/ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions core/embed/trezorhal/ble.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a52aa18

Please sign in to comment.