Skip to content

Commit 7e120f4

Browse files
committed
powertrain charging functional (balancing difficult to test)
1 parent ad458c0 commit 7e120f4

5 files changed

Lines changed: 1360 additions & 1159 deletions

File tree

ec/platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ board_upload.maximum_size = 8388608
2323

2424
build_flags = '-D ARDUINO_USB_MODE=1' '-D ARDUINO_USB_CDC_ON_BOOT=1'
2525
lib_deps =
26-
adafruit/Adafruit HUSB238 Library@^1.0.1
26+
; adafruit/Adafruit HUSB238 Library@^1.0.1

ec/src/i2cscanner.bkp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// SPDX-FileCopyrightText: 2023 Carter Nelson for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
// --------------------------------------
5+
// i2c_scanner
6+
//
7+
// Modified from https://playground.arduino.cc/Main/I2cScanner/
8+
// --------------------------------------
9+
10+
#include <Wire.h>
11+
#include <Arduino.h>
12+
13+
// Set I2C bus to use: Wire, Wire1, etc.
14+
#define WIRE Wire
15+
16+
void setup() {
17+
WIRE.begin(2, 1);
18+
19+
Serial.begin(115200);
20+
while (!Serial)
21+
delay(10);
22+
Serial.println("\nI2C Scanner");
23+
}
24+
25+
26+
void loop() {
27+
byte error, address;
28+
int nDevices;
29+
30+
Serial.println("Scanning...");
31+
32+
nDevices = 0;
33+
for(address = 1; address < 127; address++ )
34+
{
35+
// The i2c_scanner uses the return value of
36+
// the Write.endTransmisstion to see if
37+
// a device did acknowledge to the address.
38+
WIRE.beginTransmission(address);
39+
error = WIRE.endTransmission();
40+
41+
if (error == 0)
42+
{
43+
Serial.print("I2C device found at address 0x");
44+
if (address<16)
45+
Serial.print("0");
46+
Serial.print(address,HEX);
47+
Serial.println(" !");
48+
49+
nDevices++;
50+
}
51+
else if (error==4)
52+
{
53+
Serial.print("Unknown error at address 0x");
54+
if (address<16)
55+
Serial.print("0");
56+
Serial.println(address,HEX);
57+
}
58+
}
59+
if (nDevices == 0)
60+
Serial.println("No I2C devices found\n");
61+
else
62+
Serial.println("done\n");
63+
64+
delay(5000); // wait 5 seconds for next scan
65+
}

ec/src/main.cpp

Lines changed: 101 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,72 @@
33
#include "Adafruit_HUSB238.h"
44
#include "LTC2943.h"
55

6+
#define CHARGEOPTION0_MSB 0x01
7+
#define CHARGEOPTION0_LSB 0x00
8+
9+
#define CHARGECURRENT_MSB 0x03
10+
#define CHARGECURRENT_LSB 0x02
11+
12+
#define MAXCHARGEVOLTAGE_MSB 0x05
13+
#define MAXCHARGEVOLTAGE_LSB 0x04
14+
15+
#define CHARGEOPTION1_MSB 0x31
16+
#define CHARGEOPTION1_LSB 0x30
17+
18+
#define CHARGEOPTION2_MSB 0x33
19+
#define CHARGEOPTION2_LSB 0x32
20+
21+
#define CHARGEOPTION3_MSB 0x35
22+
#define CHARGEOPTION3_LSB 0x34
23+
24+
#define PROCHOTOPTION0_MSB 0x37
25+
#define PROCHOTOPTION0_LSB 0x36
26+
27+
#define PROCHOTOPTION1_MSB 0x39
28+
#define PROCHOTOPTION1_LSB 0x38
29+
30+
#define ADCOPTION_MSB 0x3B
31+
#define ADCOPTION_LSB 0x3A
32+
33+
#define CHARGERSTATUS_MSB 0x21
34+
#define CHARGERSTATUS_LSB 0x20
35+
36+
#define PROCHOTSTATUS_MSB 0x23
37+
#define PROCHOTSTATUS_LSB 0x22
38+
39+
#define IIN_DPM_MSB 0x25
40+
#define IIN_DPM_LSB 0x24
41+
42+
#define ADCVBUSPSYS_MSB 0x27
43+
#define ADCVBUSPSYS_LSB 0x26
44+
45+
#define ADCIBAT_MSB 0x29
46+
#define ADCIBAT_LSB 0x28
47+
48+
#define ADCIINCMPIN_MSB 0x2B
49+
#define ADCIINCMPIN_LSB 0x2A
50+
51+
#define ADCVSYSVBAT_MSB 0x2D
52+
#define ADCVSYSVBAT_LSB 0x2C
53+
54+
#define OTGVOLTAGE_MSB 0x07
55+
#define OTGVOLTAGE_LSB 0x06
56+
57+
#define OTGCURRENT_MSB 0x09
58+
#define OTGCURRENT_LSB 0x08
59+
60+
#define INPUTVOLTAGE_MSB 0x0B
61+
#define INPUTVOLTAGE_LSB 0x0A
62+
63+
#define MINSYSTEMVOLTAGE_MSB 0x0D
64+
#define MINSYSTEMVOLTAGE_LSB 0x0C
65+
66+
#define IIN_HOST_MSB 0x0F
67+
#define IIN_HOST_LSB 0x0E
68+
69+
#define MANUFACTURERID 0x2E
70+
#define DEVICEID 0x2F
71+
672
// void setup() {
773
// Wire.begin(2, 1);
874
// Serial.begin(115200);
@@ -53,6 +119,20 @@ uint16_t readRegister(uint8_t reg)
53119
return Wire.read(); // Combine high and low byte
54120
}
55121

122+
// Function to write a 16-bit value to consecutive I2C registers
123+
void writeRegister16(uint8_t lsbReg, uint16_t data)
124+
{
125+
Wire.beginTransmission(BQ25713_ADDRESS);
126+
Wire.write(lsbReg + 1); // Write to MSB register first
127+
Wire.write((data >> 8) & 0xFF); // Write MSB
128+
Wire.endTransmission();
129+
130+
Wire.beginTransmission(BQ25713_ADDRESS);
131+
Wire.write(lsbReg); // Write to LSB register
132+
Wire.write(data & 0xFF); // Write LSB
133+
Wire.endTransmission();
134+
}
135+
56136
void setup()
57137
{
58138
Serial.begin(115200);
@@ -71,30 +151,25 @@ void setup()
71151
;
72152
}
73153
// Change to that voltage
74-
husb238.selectPD((PD_SRC_15V));
154+
husb238.selectPD((PD_SRC_20V));
75155
// Perform the actual PD voltage request!
76156
husb238.requestPD();
77157
delay(2000);
78158

79-
Wire.beginTransmission(BQ25713_ADDRESS);
80-
Wire.write(0x01);
81-
Wire.write(0b00000000);
82-
Wire.endTransmission();
159+
// 0x020E to register 0x00
83160

84-
Wire.beginTransmission(BQ25713_ADDRESS);
85-
Wire.write(0x03);
86-
Wire.write(0b00000100);
87-
Wire.endTransmission();
161+
writeRegister16(CHARGEOPTION0_LSB, 0x020E);
88162

89-
Wire.beginTransmission(BQ25713_ADDRESS);
90-
Wire.write(0x3B);
91-
Wire.write(0b10100000);
92-
Wire.endTransmission();
163+
// ADC configurations
93164

94-
Wire.beginTransmission(BQ25713_ADDRESS);
95-
Wire.write(0x3A);
96-
Wire.write(0b11111111);
97-
Wire.endTransmission();
165+
writeRegister16(ADCOPTION_LSB, 0xa0ff);
166+
167+
// charging
168+
169+
writeRegister16(CHARGECURRENT_LSB, 0x0800);
170+
171+
// set input current to
172+
writeRegister16(IIN_HOST_LSB, 0x4000); // 3200mA input current
98173
}
99174

100175
void debugPrint(String info, uint8_t data)
@@ -112,6 +187,7 @@ void debugPrint(String info, uint8_t data)
112187

113188
void loop()
114189
{
190+
115191
delay(1000); // Short delay to stabilize
116192

117193
Serial.println("\n\n");
@@ -120,10 +196,15 @@ void loop()
120196
debugPrint("ChargerStatus 0x20:", readRegister(0x20));
121197
debugPrint("ChargeOption0 0x01:", readRegister(0x01));
122198
debugPrint("ChargeOption0 0x00:", readRegister(0x00));
199+
debugPrint("ChargeCurrent 0x03:", readRegister(0x03));
200+
debugPrint("ChargeCurrent 0x02:", readRegister(0x02));
123201
debugPrint("ADCOptions 0x3B:", readRegister(0x3B));
124202
debugPrint("ADCOptions 0x3A:", readRegister(0x3A));
203+
debugPrint("IIN_HOST 0x0F:", readRegister(IIN_HOST_MSB));
204+
debugPrint("Input Current:", readRegister(ADCIINCMPIN_MSB));
205+
debugPrint("CMP Voltage:", readRegister(ADCIINCMPIN_LSB));
125206

126-
debugPrint("VSYS 0x26:", readRegister(0x26));
207+
debugPrint("PSYS 0x26:", readRegister(0x26));
127208
debugPrint("ICHG 0x29:", readRegister(0x29));
128209
debugPrint("IDCHG 0x28:", readRegister(0x28));
129210

@@ -133,4 +214,6 @@ void loop()
133214
Serial.println(readRegister(0x2D) * 0.064);
134215
Serial.print("VBAT: ");
135216
Serial.println(readRegister(0x2C) * 0.064);
217+
218+
writeRegister16(CHARGECURRENT_LSB, 0x0800);
136219
}

0 commit comments

Comments
 (0)