Skip to content

Commit 59501c9

Browse files
committed
Feat |>[eta4662] Add eta4662 driver and read out the device id
1 parent efffeed commit 59501c9

4 files changed

Lines changed: 174 additions & 1 deletion

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "drivers/battery.h"
18+
19+
#include "drivers/exti.h"
20+
#include "drivers/gpio.h"
21+
#include "board/board.h"
22+
#include "drivers/otp.h"
23+
#include "drivers/periph_config.h"
24+
#include "system/logging.h"
25+
26+
#include <mcu.h>
27+
28+
#include <stdlib.h>
29+
#include <string.h>
30+
31+
#include "charger_eta4662.h"
32+
33+
34+
35+
void battery_init(void) {
36+
chager_init();
37+
}
38+
39+
40+
int battery_get_millivolts(void) {
41+
//ADCVoltageMonitorReading info = battery_read_voltage_monitor();
42+
43+
// Apologies for the madness numbers.
44+
// The previous implementation had some approximations in it. The battery voltage is scaled
45+
// down by a pair of resistors (750k at the top, 470k at the bottom), resulting in a required
46+
// scaling of (75 + 47) / 47 or roughly 2.56x, but the previous implementation also required
47+
// fudging the numbers a bit in order to approximate for leakage current (a 73/64 multiple
48+
// was arbitrarily increased to 295/256). In order to match this previous arbitrary scaling
49+
// I've chosen new numbers that provide 2.62x scaling, which is the previous 2.56x with the
50+
// same amount of fudging applied.
51+
52+
return 3600;//battery_convert_reading_to_millivolts(info, 3599, 1373);
53+
}
54+
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "drivers/battery.h"
18+
#include "drivers/gpio.h"
19+
#include "drivers/pmic.h"
20+
#include "system/logging.h"
21+
#include "system/passert.h"
22+
#include "drivers/i2c.h"
23+
24+
static bool prv_read_register(uint8_t register_address, uint8_t *result) {
25+
i2c_use(I2C_ETA4662);
26+
bool rv = i2c_read_register(I2C_ETA4662, register_address, result);
27+
i2c_release(I2C_ETA4662);
28+
return rv;
29+
}
30+
31+
static bool prv_write_register(uint8_t register_address, uint8_t datum) {
32+
i2c_use(I2C_ETA4662);
33+
bool rv = i2c_write_register(I2C_ETA4662, register_address, datum);
34+
i2c_release(I2C_ETA4662);
35+
return rv;
36+
}
37+
38+
void chager_init(void) {
39+
// pmic_init() needs to happen, but I think it will happen elsewhere first
40+
// It may be okay to just init the pmic here again
41+
uint8_t rv;
42+
bool found = prv_read_register(0x0B, &rv);
43+
if (found) {
44+
PBL_LOG(LOG_LEVEL_DEBUG, "Found ETA4662 with ID register ID:%02x", rv);
45+
} else {
46+
PBL_LOG(LOG_LEVEL_ERROR, "Failed to read the ETA4662 ID register");
47+
}
48+
return;
49+
}
50+
51+
52+
bool battery_charge_controller_thinks_we_are_charging_impl(void) {
53+
return false;//pmic_is_charging();
54+
}
55+
56+
bool battery_is_usb_connected_impl(void) {
57+
return false;//pmic_is_usb_connected();
58+
}
59+
60+
void battery_set_charge_enable(bool charging_enabled) {
61+
//pmic_set_charger_state(charging_enabled);
62+
}
63+
64+
// TODO
65+
// This is my understanding from Figure 9 of the datasheet:
66+
// Charger off -> Pre charge -> Fast Charge (constant current) ->
67+
// Fast Charge (constant voltage) -> Maintain Charge -> Maintain Charge Done
68+
//
69+
// The Pre Charge and Charge Termination currents are programmed via I2C
70+
// The Fast Charge current is determined by Rset
71+
//
72+
// There doesn't seem to be a way to change the current in constant current mode
73+
void battery_set_fast_charge(bool fast_charge_enabled) {
74+
75+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
extern void chager_init(void);

src/fw/drivers/wscript_build

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ elif bld.is_obelix_bb():
214214
'driver_motor',
215215
'driver_imu',
216216
'driver_touch',
217+
'driver_battery'
217218
],
218219
)
219220
elif bld.is_obelix():
@@ -227,6 +228,7 @@ elif bld.is_obelix():
227228
'driver_flash',
228229
'driver_gpio_defaults',
229230
'driver_imu'
231+
'driver_battery'
230232
],
231233
)
232234

@@ -419,6 +421,29 @@ elif bld.is_asterix():
419421
'root_includes',
420422
],
421423
)
424+
elif bld.is_obelix_bb():
425+
bld.objects(
426+
name='driver_battery',
427+
source=[
428+
'battery/battery_obelix.c',
429+
'battery/charger_eta4662.c',
430+
],
431+
use=[
432+
'fw_includes',
433+
'root_includes',
434+
],
435+
)
436+
elif bld.is_obelix():
437+
bld.objects(
438+
name='driver_battery',
439+
source=[
440+
'sf32lb/stubs/battery.c',
441+
],
442+
use=[
443+
'fw_includes',
444+
'root_includes',
445+
],
446+
)
422447
else:
423448
bld.objects(
424449
name='driver_battery',
@@ -1644,7 +1669,7 @@ if mcu_family == 'SF32LB':
16441669
'flash/cd_flash_driver.c',
16451670
'battery/battery_common.c',
16461671
# 'display/jdi/jdi.c',
1647-
'sf32lb/stubs/battery.c',
1672+
#'sf32lb/stubs/battery.c',
16481673
'i2c.c',
16491674
'sf32lb/i2c_hal.c',
16501675
'sf32lb/pwm.c',

0 commit comments

Comments
 (0)