diff --git a/src/board/system76/galp3-c/battery.c b/src/board/system76/galp3-c/battery.c index 7083aea..bf3eeeb 100644 --- a/src/board/system76/galp3-c/battery.c +++ b/src/board/system76/galp3-c/battery.c @@ -1,62 +1,15 @@ #include -#include +#include uint8_t smbus_read(uint8_t address, uint8_t command, uint16_t * data) { - // Read value from address - TRASLAA = (address << 1) | (1 << 0); - HOCMDA = command; - - // Start read word command - HOCTLA = (1 << 6) | (0b011 << 2); - - // Wait for command to start - while (!(HOSTAA & 1)) {} - - // Wait for command to finish - while (HOSTAA & 1) {} - - // Read and clear status - uint8_t status = HOSTAA; - HOSTAA = status; - - // 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)) { - *data = ((uint16_t)D0REGA) | - ((uint16_t)D1REGA << 8); - return 0; - } else { - //TODO: custom error type or flags for errors - return (status & error); - } + if (i2c_write(address, &command, 1)) return 1; + return i2c_read(address, (uint8_t *)data, 2); } uint8_t smbus_write(uint8_t address, uint8_t command, uint16_t data) { - // Write value to address - TRASLAA = (address << 1); - HOCMDA = command; - - D0REGA = (uint8_t)data; - D1REGA = (uint8_t)(data >> 8); - - // Start write word command - HOCTLA = (1 << 6) | (0b011 << 2); - - // Wait for command to start - while (!(HOSTAA & 1)) {} - - // Wait for command to finish - while (HOSTAA & 1) {} - - // Read and clear status - uint8_t status = HOSTAA; - HOSTAA = status; - - // 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); + if (i2c_write(address, &command, 1)) return 1; + return i2c_write(address, &data, 2); } uint8_t battery_charger_disable(void) { diff --git a/src/board/system76/galp3-c/smbus.c b/src/board/system76/galp3-c/smbus.c index f90a3b0..a1b5dd5 100644 --- a/src/board/system76/galp3-c/smbus.c +++ b/src/board/system76/galp3-c/smbus.c @@ -19,5 +19,5 @@ void smbus_init(void) { SCLKTSA = 1; // Enable host interface - HOCTL2A = 1 << 0; + HOCTL2A = 1; } diff --git a/src/board/system76/galp3-c/stdio.c b/src/board/system76/galp3-c/stdio.c index 5a4adf5..8dbeec7 100644 --- a/src/board/system76/galp3-c/stdio.c +++ b/src/board/system76/galp3-c/stdio.c @@ -1,36 +1,9 @@ #include -#include - -void i2c_write(unsigned char value) { - // Write value to 0x76 - TRASLAA = 0xEC; // 0x76 << 1 - HOCMDA = value; - - for (;;) { - // Start command - HOCTLA = (1 << 6) | (0b001 << 2); - - // Wait for command to start - while (!(HOSTAA & 1)) {} - - // Wait for command to finish - while (HOSTAA & 1) {} - - // Read and clear status - uint8_t status = HOSTAA; - HOSTAA = status; - - // If there were no errors, return - uint8_t error = (1 << 6) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2); - if (!(status & error)) { - break; - } - } -} +#include int putchar(int c) { unsigned char byte = (unsigned char)c; - i2c_write(byte); + i2c_write(0x76, &byte, 1); return (int)byte; }