drivers: sensor: mcp9808: clean up I2C API usage

The i2c_msg API details do not work correctly with Nordic TWI.  Switch
to the higher-level and simpler API for register read and write.  Also
add a tree configuration on a Nordic-based board.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
This commit is contained in:
Peter Bigot 2019-11-07 09:48:27 -06:00 committed by Maureen Helm
parent b14d43eec7
commit 83b2bb614b
3 changed files with 25 additions and 32 deletions

View file

@ -21,27 +21,15 @@ LOG_MODULE_REGISTER(MCP9808, CONFIG_SENSOR_LOG_LEVEL);
int mcp9808_reg_read(struct mcp9808_data *data, u8_t reg, u16_t *val)
{
struct i2c_msg msgs[2] = {
{
.buf = &reg,
.len = 1,
.flags = I2C_MSG_WRITE | I2C_MSG_RESTART,
},
{
.buf = (u8_t *)val,
.len = 2,
.flags = I2C_MSG_READ | I2C_MSG_STOP,
},
};
int rc = i2c_write_read(data->i2c_master, data->i2c_slave_addr,
&reg, sizeof(reg),
val, sizeof(*val));
if (i2c_transfer(data->i2c_master, msgs, 2, data->i2c_slave_addr)
< 0) {
return -EIO;
if (rc == 0) {
*val = sys_be16_to_cpu(*val);
}
*val = sys_be16_to_cpu(*val);
return 0;
return rc;
}
static int mcp9808_sample_fetch(struct device *dev, enum sensor_channel chan)

View file

@ -18,22 +18,13 @@ LOG_MODULE_DECLARE(MCP9808, CONFIG_SENSOR_LOG_LEVEL);
static int mcp9808_reg_write(struct mcp9808_data *data, u8_t reg, u16_t val)
{
u16_t be_val = sys_cpu_to_be16(val);
struct i2c_msg msgs[2] = {
{
.buf = &reg,
.len = 1,
.flags = I2C_MSG_WRITE | I2C_MSG_RESTART,
},
{
.buf = (u8_t *) &be_val,
.len = 2,
.flags = I2C_MSG_WRITE | I2C_MSG_STOP,
},
u8_t buf[3] = {
reg,
val >> 8, /* big-endian register storage */
val & 0xFF,
};
return i2c_transfer(data->i2c_master, msgs, 2, data->i2c_slave_addr);
return i2c_write(data->i2c_master, buf, sizeof(buf), data->i2c_slave_addr);
}
static int mcp9808_reg_update(struct mcp9808_data *data, u8_t reg,

View file

@ -0,0 +1,14 @@
/*
* Copyright (c) 2019, Linaro Ltd.
*
* SPDX-License-Identifier: Apache-2.0
*/
&i2c0 {
mcp9808@18 {
compatible = "microchip,mcp9808";
reg = <0x18>;
int-gpios = <&gpio1 1 0>;
label = "MCP9808";
};
};