kierenj:
It's the following, although the write doesn't "take" unless 5V/USB is not present, i.e. if it's plugged in, the register reverts to the default.
I have a small switch that removes power from the USB bus, so that the serial works on battery 
I have formatted the sketch a bit more:
//*
BQ SET OTG test
Trying 5V power boost on MKR WiFI1010
*/
#include <Wire.h>
#define PMIC_ADDRESS 0x6B
byte val;
byte BQ_REG[10] = { 0x01, 0x2, 0x03, 0x04, 0x05, 0x06,0x07,0x08, 0x09 };
byte OTG = 0x2B;
void setup() {
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB
}
Wire.begin();
// Check battery status
analogReadResolution(12);
int sensorValue = analogRead(ADC_BATTERY);
float voltage = sensorValue * (4.208 / 4095.000);
if (voltage < 3.000) { // BATLOWV REG04[1] = 1 {
Serial.println("Battery voltage below 3.0V - Battery BOOST mode not allowed!");
return;
}
else {
Serial.print("Voltage: ");
Serial.println(voltage);
}
// Check watchdog timer status
Wire.beginTransmission(PMIC_ADDRESS);
Wire.write(BQ_REG[4]); // REG05
Wire.endTransmission();
Wire.beginTransmission(PMIC_ADDRESS);
Wire.requestFrom(PMIC_ADDRESS, 1);
Wire.endTransmission();
val = Wire.read();
Serial.print("Watchdog timer value is: ");
Serial.println(val);
if (val != 154) { // 154 DEC = 9A
Serial.println("Watchdog timer is enabled, we're going to disable it now");
Wire.beginTransmission(PMIC_ADDRESS);
Wire.write(BQ_REG[4]); // REG05
Wire.write(0x9A); // Enable Termination, match ITERM, Set Watchdog timer to 00, Safety timer on, Fast Charge 8hrs
Wire.endTransmission();
}
// Set OTG High and OTG register REG01[4:5] to 10
pinMode(PIN_USB_HOST_ENABLE, OUTPUT);
digitalWrite(PIN_USB_HOST_ENABLE, HIGH); // Set OTG pin High
Wire.beginTransmission(PMIC_ADDRESS);
Wire.write(BQ_REG[0]); // REG 01
Wire.write(OTG); // don't reset, normal watchdog, 10=OTG mode, 1011 = 3.5V min sys voltage
Wire.endTransmission();
}
void loop() { // We dump now all the registries of the BQ chip
for (int r = 0; r < 9; r++) {
Wire.beginTransmission(PMIC_ADDRESS);
Wire.write(BQ_REG[r]);
Wire.endTransmission();
Wire.beginTransmission(PMIC_ADDRESS);
Wire.requestFrom(PMIC_ADDRESS, 1);
Wire.endTransmission();
val = Wire.read();
Serial.print("Register ");
Serial.print(BQ_REG[r]);
Serial.print(" = 0x");
if (val<15)
{
Serial.print("0");
}
Serial.print(val, HEX);
Serial.print(" Bits (0-7) : ");
unsigned char byte = val;
unsigned char mask = 1;
unsigned char bits[8];
// Extract the bits
for (int i = 0; i < 8; i++) {
// Mask each bit in the byte and store it
bits = (byte >> i) & mask;
_ Serial.print(bits*);_
_ Serial.print(" ");_
_ }_
_ Serial.println(" ");_
_}_
Serial.println(" ");
//pinMode(PIN_USB_HOST_ENABLE, OUTPUT); // should be the case from standard startup anyway..
//digitalWrite(PIN_USB_HOST_ENABLE, LOW); // inverse of startup*
delay(10000);
}
Output with USB power on
Voltage: 4.17
Watchdog timer value is: 138
Watchdog timer is enabled, we're going to disable it now
*Register 1 = 0x1B Bits (0-7) : 1 1 0 1 1 0 0 0 *
*Register 2 = 0x00 Bits (0-7) : 0 0 0 0 0 0 0 0 *
*Register 3 = 0x11 Bits (0-7) : 1 0 0 0 1 0 0 0 *
*Register 4 = 0x9A Bits (0-7) : 0 1 0 1 1 0 0 1 *
*Register 5 = 0x9A Bits (0-7) : 0 1 0 1 1 0 0 1 *
*Register 6 = 0x03 Bits (0-7) : 1 1 0 0 0 0 0 0 *
*Register 7 = 0x0B Bits (0-7) : 1 1 0 1 0 0 0 0 *
*Register 8 = 0x24 Bits (0-7) : 0 0 1 0 0 1 0 0 *
Register 9 = 0x80 Bits (0-7) : 0 0 0 0 0 0 0 1
Output with USB power off
Voltage: 3.99
Watchdog timer value is: 138
Watchdog timer is enabled, we're going to disable it now
*Register 1 = 0x2B Bits (0-7) : 1 1 0 1 0 1 0 0 *
*Register 2 = 0x00 Bits (0-7) : 0 0 0 0 0 0 0 0 *
*Register 3 = 0x11 Bits (0-7) : 1 0 0 0 1 0 0 0 *
*Register 4 = 0x9A Bits (0-7) : 0 1 0 1 1 0 0 1 *
*Register 5 = 0x9A Bits (0-7) : 0 1 0 1 1 0 0 1 *
*Register 6 = 0x03 Bits (0-7) : 1 1 0 0 0 0 0 0 *
*Register 7 = 0x0B Bits (0-7) : 1 1 0 1 0 0 0 0 *
*Register 8 = 0x00 Bits (0-7) : 0 0 0 0 0 0 0 0 *
Register 9 = 0x80 Bits (0-7) : 0 0 0 0 0 0 0 1
I have noticed the following, REG1 not ok stays at 1B despite setting when on USB power, ok on battery to 2B, REG4 ok, REG8[6:7] = 00 As a result not good - Plus I'm getting intermittent watchdog error on REG9
So, still no 5V power out when on battery, last test would be to set OTG pin electrically High... I need to find a USB cable to cut open and see what happens