diff options
author | Dave Parker <dparker@chromium.org> | 2013-08-07 19:47:26 -0700 |
---|---|---|
committer | ChromeBot <chrome-bot@google.com> | 2013-08-09 11:19:02 -0700 |
commit | 2b2f78d9295103a8df550086feeaaaa80aa56a2d (patch) | |
tree | 3ed22105f4e1624e93a71b6e7acda35039a40bdd /common | |
parent | ffed16cae4c34a4326eead22af690ff101b3780d (diff) | |
download | chrome-ec-2b2f78d9295103a8df550086feeaaaa80aa56a2d.tar.gz |
Console command for g781 temperature sensor.
Read status, set temperature alert thresholds, get and set
configuration options. I2c offsets and status/config register
bits are documented in temp_sensor_g781.h
Usage by example:
g781 - Print status info
g781 settemp 0x0e 12 - Set remote low temp alarm to 12C
g781 setbyte 0x09 0x40 - Enable single-shot mode
g781 getbyte 0xfe - Read device ID
BUG=None
BRANCH=falco,peppy
TEST=Manual. Run g781 console command
Signed-off-by: Dave Parker <dparker@chromium.org>
Change-Id: Id051f79ea643255d57c3fc694b7ae685a6611c81
Reviewed-on: https://gerrit.chromium.org/gerrit/65234
Reviewed-by: Randall Spangler <rspangler@chromium.org>
Commit-Queue: Dave Parker <dparker@chromium.org>
Tested-by: Dave Parker <dparker@chromium.org>
Diffstat (limited to 'common')
-rw-r--r-- | common/temp_sensor_g781.c | 162 |
1 files changed, 152 insertions, 10 deletions
diff --git a/common/temp_sensor_g781.c b/common/temp_sensor_g781.c index 4a2698ea2e..267cc4a0e5 100644 --- a/common/temp_sensor_g781.c +++ b/common/temp_sensor_g781.c @@ -9,37 +9,179 @@ #include "console.h" #include "i2c.h" #include "temp_sensor_g781.h" +#include "util.h" -int g781_get_val(int idx, int *temp_ptr) +static int g781_read8(const int offset, int *data_ptr) +{ + return i2c_read8(I2C_PORT_THERMAL, G781_I2C_ADDR, offset, data_ptr); +} + +static int g781_write8(const int offset, int data) +{ + return i2c_write8(I2C_PORT_THERMAL, G781_I2C_ADDR, offset, data); +} + +static int g781_get_temp(const int offset, int *temp_ptr) { - int command; int rv; int temp_raw = 0; + rv = g781_read8(offset, &temp_raw); + if (rv < 0) + return rv; + + *temp_ptr = (int)(int8_t)temp_raw; + return EC_SUCCESS; +} + +static int g781_set_temp(const int offset, int temp) +{ + if (temp < -127 || temp > 127) + return EC_ERROR_INVAL; + + return g781_write8(offset, (uint8_t)temp); +} + +int g781_get_val(int idx, int *temp_ptr) +{ + int offset; + int rv; + if (!board_g781_has_power()) return EC_ERROR_NOT_POWERED; switch (idx) { case 0: - command = G781_TEMP_LOCAL; + offset = G781_TEMP_LOCAL; break; case 1: - command = G781_TEMP_REMOTE; + offset = G781_TEMP_REMOTE; break; default: return EC_ERROR_UNKNOWN; } - rv = i2c_read8(I2C_PORT_THERMAL, G781_I2C_ADDR, command, &temp_raw); + rv = g781_get_temp(offset, temp_ptr); + if (rv < 0) + return rv; + + /* Temperature from sensor is in degrees Celsius */ + *temp_ptr = C_TO_K(*temp_ptr); + return EC_SUCCESS; +} +static int g781_show_status(void) +{ + int value; + int rv; + + + rv = g781_get_temp(G781_TEMP_LOCAL, &value); if (rv < 0) return rv; + ccprintf("Local Temp: %3dC\n", value); - /* Negative numbers are 2's compliment with sign bit 7 */ - if (temp_raw & (1 << 7)) - temp_raw = ~(~temp_raw & 0xff) + 1; + rv = g781_get_temp(G781_LOCAL_TEMP_THERM_LIMIT, &value); + if (rv < 0) + return rv; + ccprintf(" Therm Trip: %3dC\n", value); + + rv = g781_get_temp(G781_LOCAL_TEMP_HIGH_LIMIT_R, &value); + if (rv < 0) + return rv; + ccprintf(" High Alarm: %3dC\n", value); + + rv = g781_get_temp(G781_LOCAL_TEMP_LOW_LIMIT_R, &value); + if (rv < 0) + return rv; + ccprintf(" Low Alarm: %3dC\n", value); + + rv = g781_get_temp(G781_TEMP_REMOTE, &value); + if (rv < 0) + return rv; + ccprintf("Remote Temp: %3dC\n", value); + + rv = g781_get_temp(G781_REMOTE_TEMP_THERM_LIMIT, &value); + if (rv < 0) + return rv; + ccprintf(" Therm Trip: %3dC\n", value); + + rv = g781_get_temp(G781_REMOTE_TEMP_HIGH_LIMIT_R, &value); + if (rv < 0) + return rv; + ccprintf(" High Alarm: %3dC\n", value); + + rv = g781_get_temp(G781_REMOTE_TEMP_LOW_LIMIT_R, &value); + if (rv < 0) + return rv; + ccprintf(" Low Alarm: %3dC\n", value); + + rv = g781_read8(G781_STATUS, &value); + if (rv < 0) + return rv; + ccprintf("\nSTATUS: %08b\n", value); + + rv = g781_read8(G781_CONFIGURATION_R, &value); + if (rv < 0) + return rv; + ccprintf("CONFIG: %08b\n", value); - /* Temperature from sensor is in degrees Celsius */ - *temp_ptr = C_TO_K(temp_raw); return EC_SUCCESS; } + +static int command_g781(int argc, char **argv) +{ + char *command; + char *e; + int data; + int offset; + int rv; + + if (!board_g781_has_power()) { + ccprintf("ERROR: Temp sensor not powered.\n"); + return EC_ERROR_NOT_POWERED; + } + + /* If no args just print status */ + if (argc == 1) + return g781_show_status(); + + if (argc < 3) + return EC_ERROR_PARAM_COUNT; + + command = argv[1]; + offset = strtoi(argv[2], &e, 0); + if (*e || offset < 0 || offset > 255) + return EC_ERROR_PARAM2; + + if (!strcasecmp(command, "getbyte")) { + rv = g781_read8(offset, &data); + if (rv < 0) + return rv; + ccprintf("Byte at offset 0x%02x is %08b\n", offset, data); + return rv; + } + + /* Remaining commands are of the form "g781 set-command offset data" */ + if (argc != 4) + return EC_ERROR_PARAM_COUNT; + + data = strtoi(argv[3], &e, 0); + if (*e) + return EC_ERROR_PARAM3; + + if (!strcasecmp(command, "settemp")) { + ccprintf("Setting 0x%02x to %dC\n", offset, data); + rv = g781_set_temp(offset, data); + } else if (!strcasecmp(command, "setbyte")) { + ccprintf("Setting 0x%02x to 0x%02x\n", offset, data); + rv = g781_write8(offset, data); + } else + return EC_ERROR_PARAM1; + + return rv; +} +DECLARE_CONSOLE_COMMAND(g781, command_g781, + "[settemp|setbyte <offset> <value>] or [getbyte <offset>]. " + "Temps in Celsius.", + "Print g781 temp sensor status or set parameters.", NULL); |