Skip to content

Commit 2dbce5c

Browse files
committed
ARMmbed#87 Improve power source handling with edge connector
1 parent 258d0b7 commit 2dbce5c

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

source/board/microbitv2/microbitv2.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ static void i2c_write_comms_callback(uint8_t* pData, uint8_t size) {
644644
}
645645
break;
646646
case gPowerState_c:
647+
power_source = pwr_mon_get_power_source();
647648
i2cResponse.cmdData.readRspCmd.dataSize = sizeof(power_source);
648649
memcpy(&i2cResponse.cmdData.readRspCmd.data, &power_source, sizeof(power_source));
649650
break;

source/board/microbitv2/pwr_mon.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,29 @@ void pwr_mon_init(void)
4040
power_source_t pwr_mon_get_power_source(void) {
4141
power_source_t power_source = PWR_SOURCE_NONE;
4242
uint32_t bat_voltage_mv = 0;
43+
uint32_t vin_voltage_mv = 0;
4344

4445
// Read WAKE_ON_EDGE pin for detecting if board is USB powered
4546
bool usb_on = (((PIN_WAKE_ON_EDGE_GPIO->PDIR) >> PIN_WAKE_ON_EDGE_BIT) & 0x01U) ? false : true;
4647

4748
// Read battery voltage
4849
bat_voltage_mv = pwr_mon_get_vbat_mv();
4950

51+
// Read Vin voltage
52+
vin_voltage_mv = pwr_mon_get_vin_mv();
53+
5054
// Get power source based on battery and USB
5155
if (usb_on == true && bat_voltage_mv < (BATT_MIN_VOLTAGE)) {
5256
power_source = PWR_USB_ONLY;
5357
} else if (usb_on == true && bat_voltage_mv >= (BATT_MIN_VOLTAGE)) {
5458
power_source = PWR_USB_AND_BATT;
5559
} else if (usb_on == false && bat_voltage_mv >= (BATT_MIN_VOLTAGE)) {
56-
power_source = PWR_BATT_ONLY;
57-
} else if (usb_on == false && bat_voltage_mv < (BATT_MIN_VOLTAGE)) {
58-
power_source = PWR_SOURCE_NONE;
60+
// If battery voltage is greater than Vin, it means the battery is used
61+
if ( bat_voltage_mv + 200 > vin_voltage_mv ) {
62+
power_source = PWR_BATT_ONLY;
63+
} else {
64+
power_source = PWR_SOURCE_NONE;
65+
}
5966
}
6067

6168
return power_source;

0 commit comments

Comments
 (0)