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:
parent
b14d43eec7
commit
83b2bb614b
|
@ -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 = ®,
|
||||
.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,
|
||||
®, 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)
|
||||
|
|
|
@ -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 = ®,
|
||||
.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,
|
||||
|
|
14
samples/sensor/mcp9808/boards/particle_xenon.overlay
Normal file
14
samples/sensor/mcp9808/boards/particle_xenon.overlay
Normal 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";
|
||||
};
|
||||
};
|
Loading…
Reference in a new issue