From 976874f525c876cbec8626e524bf71a58ad63e33 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Tue, 5 Nov 2019 14:42:15 -0700 Subject: [PATCH] Do less re-initialization of smbus, add battery charger enabling --- src/board/system76/galp3-c/battery.c | 60 +++++++++---------- .../system76/galp3-c/include/board/battery.h | 1 + src/board/system76/galp3-c/main.c | 6 +- src/board/system76/galp3-c/smbus.c | 6 ++ src/board/system76/galp3-c/stdio.c | 23 ++----- 5 files changed, 44 insertions(+), 52 deletions(-) diff --git a/src/board/system76/galp3-c/battery.c b/src/board/system76/galp3-c/battery.c index 1a3e9ce..7a68cea 100644 --- a/src/board/system76/galp3-c/battery.c +++ b/src/board/system76/galp3-c/battery.c @@ -3,18 +3,6 @@ #include uint8_t smbus_read(uint8_t address, uint8_t command, uint16_t * data) { - // Wait for last command - while (HOSTAA & 1) {} - - // Clear result - HOSTAA = HOSTAA; - - // Clock to 400 KHz - SCLKTSA = 3; - - // Enable host interface - HOCTL2A = 1 << 0; - // Read value from address TRASLAA = (address << 1) | (1 << 0); HOCMDA = command; @@ -32,9 +20,6 @@ uint8_t smbus_read(uint8_t address, uint8_t command, uint16_t * data) { uint8_t status = HOSTAA; HOSTAA = status; - // Disable host interface - HOCTL2A = 0; - // If there were no errors, set value and return 0 uint8_t error = (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2); if (!(status & error)) { @@ -48,18 +33,6 @@ uint8_t smbus_read(uint8_t address, uint8_t command, uint16_t * data) { } uint8_t smbus_write(uint8_t address, uint8_t command, uint16_t data) { - // Wait for last command - while (HOSTAA & 1) {} - - // Clear result - HOSTAA = HOSTAA; - - // Clock to 400 KHz - SCLKTSA = 3; - - // Enable host interface - HOCTL2A = 1 << 0; - // Write value to address TRASLAA = (address << 1); HOCMDA = command; @@ -80,14 +53,41 @@ uint8_t smbus_write(uint8_t address, uint8_t command, uint16_t data) { uint8_t status = HOSTAA; HOSTAA = status; - // Disable host interface - HOCTL2A = 0; - // If there were no errors, set value and return 0 uint8_t error = (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2); //TODO: custom error type or flags for errors return (status & error); } + +uint8_t battery_charger_disable(void) { + uint8_t err = 0; + + // Disable charge current + err = smbus_write(0x09, 0x14, 0); + if (err) return err; + + // Disable charge voltage + err = smbus_write(0x09, 0x15, 0); + if (err) return err; +} + +uint8_t battery_charger_enable(void) { + uint8_t err = 0; + + err = battery_charger_disable(); + if (err) return err; + + // Set charge current to ~1.5 A + err = smbus_write(0x09, 0x14, 0x0600); + if (err) return err; + + // Set charge voltage to ~13 V + err = smbus_write(0x09, 0x15, 0x3300); + if (err) return err; + + return 0; +} + void battery_debug(void) { uint16_t data = 0; uint8_t err = 0; diff --git a/src/board/system76/galp3-c/include/board/battery.h b/src/board/system76/galp3-c/include/board/battery.h index 935ddad..9d7bf90 100644 --- a/src/board/system76/galp3-c/include/board/battery.h +++ b/src/board/system76/galp3-c/include/board/battery.h @@ -1,6 +1,7 @@ #ifndef _BOARD_BATTERY_H #define _BOARD_BATTERY_H +void battery_charger_enable(void); void battery_debug(void); #endif // _BOARD_BATTERY_H diff --git a/src/board/system76/galp3-c/main.c b/src/board/system76/galp3-c/main.c index f442927..832f150 100644 --- a/src/board/system76/galp3-c/main.c +++ b/src/board/system76/galp3-c/main.c @@ -99,9 +99,9 @@ void touchpad_event(struct Ps2 * ps2) { //TODO } - -//struct Gpio __code LED_PWR = GPIO(A, 7); -//struct Gpio __code LED_AIRPLANE_N = GPIO(G, 6); +struct Gpio __code LED_PWR = GPIO(A, 7); +struct Gpio __code LED_SSD_N = GPIO(G, 6); +struct Gpio __code LED_AIRPLANE_N = GPIO(G, 6); void main(void) { init(); diff --git a/src/board/system76/galp3-c/smbus.c b/src/board/system76/galp3-c/smbus.c index 2d68b1f..bcb859c 100644 --- a/src/board/system76/galp3-c/smbus.c +++ b/src/board/system76/galp3-c/smbus.c @@ -8,4 +8,10 @@ void smbus_init(void) { SMB25MS = 0x19; SMB45P3USL = 0x5C; SMB45P3USH = 0x01; + + // Clock to 400 KHz + SCLKTSA = 3; + + // Enable host interface + HOCTL2A = 1 << 0; } diff --git a/src/board/system76/galp3-c/stdio.c b/src/board/system76/galp3-c/stdio.c index b2f53bb..d2c8365 100644 --- a/src/board/system76/galp3-c/stdio.c +++ b/src/board/system76/galp3-c/stdio.c @@ -3,23 +3,11 @@ #include void i2c_write(unsigned char value) { + // Write value to 0x76 + TRASLAA = 0x76 << 1; + HOCMDA = value; + for (;;) { - // Wait for last command - while (HOSTAA & 1) {} - - // Clear result - HOSTAA = HOSTAA; - - // Clock to 400 KHz - SCLKTSA = 3; - - // Enable host interface with i2c compatibility - HOCTL2A = (1 << 1) | (1 << 0); - - // Write value to 0x76 - TRASLAA = 0x76 << 1; - HOCMDA = value; - // Start command HOCTLA = (1 << 6) | (0b001 << 2); @@ -33,9 +21,6 @@ void i2c_write(unsigned char value) { uint8_t status = HOSTAA; HOSTAA = status; - // Disable host interface - HOCTL2A = 0; - // If there were no errors, return uint8_t error = (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2); if (!(status & error)) {