Skip to content

Commit 7cfd0fe

Browse files
authored
Merge pull request #9582 from bill88t/tws3-axp-fixes
Init AXP2101 from C code for T-Watch-S3
2 parents d8b9f64 + 65121d5 commit 7cfd0fe

File tree

1 file changed

+51
-0
lines changed
  • ports/espressif/boards/lilygo_twatch_s3

1 file changed

+51
-0
lines changed

ports/espressif/boards/lilygo_twatch_s3/board.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "supervisor/board.h"
88
#include "mpconfigboard.h"
99
#include "shared-bindings/busio/SPI.h"
10+
#include "shared-bindings/busio/I2C.h"
1011
#include "shared-bindings/fourwire/FourWire.h"
1112
#include "shared-bindings/microcontroller/Pin.h"
1213
#include "shared-module/displayio/__init__.h"
@@ -48,8 +49,58 @@ uint8_t display_init_sequence[] = {
4849
0x29, 0 | DELAY, 255,
4950
};
5051

52+
#define AXP2101_I2C_ADDRESS 0x34
53+
54+
static void write_register8(busio_i2c_obj_t *i2c, uint8_t reg, uint8_t value) {
55+
uint8_t buffer[2];
56+
buffer[0] = reg;
57+
buffer[1] = value;
58+
common_hal_busio_i2c_write(i2c, AXP2101_I2C_ADDRESS, buffer, 2);
59+
}
60+
61+
static void set_bit_in_register(busio_i2c_obj_t *i2c, uint8_t reg, uint8_t bitmask) {
62+
uint8_t buffer[2];
63+
buffer[0] = reg;
64+
buffer[1] = 0;
65+
common_hal_busio_i2c_write_read(i2c, AXP2101_I2C_ADDRESS, &buffer[0], 1, &buffer[1], 1);
66+
buffer[1] |= bitmask;
67+
common_hal_busio_i2c_write(i2c, AXP2101_I2C_ADDRESS, buffer, 2);
68+
}
69+
70+
static void enable_ldo(busio_i2c_obj_t *i2c, uint8_t ldo) {
71+
write_register8(i2c, ldo + 0x92, 0x1C); // 3300mV
72+
set_bit_in_register(i2c, 0x90, 1 << ldo);
73+
}
74+
75+
static void enable_dldo(busio_i2c_obj_t *i2c, uint8_t ldo) {
76+
if (ldo == 1) {
77+
write_register8(i2c, 0x99, 0x1C); // 3300mV
78+
set_bit_in_register(i2c, 0x90, 0x80);
79+
}
80+
}
81+
82+
// Init the AXP2101 by hand as to not include XPOWERS lib.
83+
static void pmic_init(busio_i2c_obj_t *i2c) {
84+
enable_ldo(i2c, 0); // _aldo1
85+
enable_ldo(i2c, 1); // _aldo2
86+
enable_ldo(i2c, 2); // _aldo3
87+
enable_ldo(i2c, 3); // _aldo4
88+
enable_ldo(i2c, 5); // _bldo2
89+
enable_dldo(i2c, 1); // _dldo1
90+
write_register8(i2c, 0x18, 0x0F); // Enable charging of main Bat and Coin cell
91+
write_register8(i2c, 0x27, 0x1F); // 2s on time + 10s off time
92+
write_register8(i2c, 0x62, 0x0B); // 500mA Current limit
93+
write_register8(i2c, 0x16, 0x04); // 1.5A INcurr limit
94+
write_register8(i2c, 0x61, 0x06); // 150mA Precharge limit
95+
write_register8(i2c, 0x64, 0x03); // 4.2V Voltage target
96+
write_register8(i2c, 0x63, 0x11); // 25mA Charging termination current
97+
}
98+
5199

52100
void board_init(void) {
101+
busio_i2c_obj_t *internal_i2c = common_hal_board_create_i2c(0);
102+
pmic_init(internal_i2c);
103+
53104
busio_spi_obj_t *spi = common_hal_board_create_spi(0);
54105
fourwire_fourwire_obj_t *bus = &allocate_display_bus()->fourwire_bus;
55106
bus->base.type = &fourwire_fourwire_type;

0 commit comments

Comments
 (0)