Fix smbus read/write on arduino

This commit is contained in:
Jeremy Soller 2019-11-15 14:19:21 -07:00
parent 97502b61b0
commit d3c16c5c52
No known key found for this signature in database
GPG Key ID: E988B49EE78A7FB1
2 changed files with 28 additions and 30 deletions

View File

@ -2,23 +2,23 @@
#include <common/i2c.h> #include <common/i2c.h>
uint8_t smbus_read(uint8_t address, uint8_t command, uint16_t * data) { int smbus_read(uint8_t address, uint8_t command, uint16_t * data) {
return i2c_get(address, command, (uint8_t *)data, 2); return i2c_get(address, command, (uint8_t *)data, 2);
} }
uint8_t smbus_write(uint8_t address, uint8_t command, uint16_t data) { int smbus_write(uint8_t address, uint8_t command, uint16_t data) {
return i2c_set(address, command, (uint8_t *)&data, 2); return i2c_set(address, command, (uint8_t *)&data, 2);
} }
void battery_debug(void) { void battery_debug(void) {
uint16_t data = 0; uint16_t data = 0;
uint8_t err = 0; int res = 0;
#define command(N, A, V) { \ #define command(N, A, V) { \
printf(#N ": "); \ printf(#N ": "); \
err = smbus_read(A, V, &data); \ res = smbus_read(A, V, &data); \
if (err) { \ if (res < 0) { \
printf("ERROR %02X\n", err); \ printf("ERROR %04X\n", -res); \
} else { \ } else { \
printf("%04X\n", data); \ printf("%04X\n", data); \
} \ } \

View File

@ -2,56 +2,54 @@
#include <ec/i2c.h> #include <ec/i2c.h>
uint8_t smbus_read(uint8_t address, uint8_t command, uint16_t * data) { int smbus_read(uint8_t address, uint8_t command, uint16_t * data) {
if (i2c_write(address, &command, 1)) return 1; return i2c_get(address, command, (uint8_t *)data, 2);
return i2c_read(address, (uint8_t *)data, 2);
} }
uint8_t smbus_write(uint8_t address, uint8_t command, uint16_t data) { int smbus_write(uint8_t address, uint8_t command, uint16_t data) {
if (i2c_write(address, &command, 1)) return 1; return i2c_set(address, command, (uint8_t *)&data, 2);
return i2c_write(address, &data, 2);
} }
uint8_t battery_charger_disable(void) { int battery_charger_disable(void) {
uint8_t err = 0; int res = 0;
// Disable charge current // Disable charge current
err = smbus_write(0x09, 0x14, 0); res = smbus_write(0x09, 0x14, 0);
if (err) return err; if (res < 0) return res;
// Disable charge voltage // Disable charge voltage
err = smbus_write(0x09, 0x15, 0); res = smbus_write(0x09, 0x15, 0);
if (err) return err; if (res < 0) return res;
return 0; return 0;
} }
uint8_t battery_charger_enable(void) { int battery_charger_enable(void) {
uint8_t err = 0; int res = 0;
err = battery_charger_disable(); res = battery_charger_disable();
if (err) return err; if (res < 0) return res;
// Set charge current to ~1.5 A // Set charge current to ~1.5 A
err = smbus_write(0x09, 0x14, 0x0600); res = smbus_write(0x09, 0x14, 0x0600);
if (err) return err; if (res < 0) return res;
// Set charge voltage to ~13 V // Set charge voltage to ~13 V
err = smbus_write(0x09, 0x15, 0x3300); res = smbus_write(0x09, 0x15, 0x3300);
if (err) return err; if (res < 0) return res;
return 0; return 0;
} }
void battery_debug(void) { void battery_debug(void) {
uint16_t data = 0; uint16_t data = 0;
uint8_t err = 0; int res = 0;
#define command(N, A, V) { \ #define command(N, A, V) { \
printf(#N ": "); \ printf(#N ": "); \
err = smbus_read(A, V, &data); \ res = smbus_read(A, V, &data); \
if (err) { \ if (res < 0) { \
printf("ERROR %02X\n", err); \ printf("ERROR %04X\n", -res); \
} else { \ } else { \
printf("%04X\n", data); \ printf("%04X\n", data); \
} \ } \