1 /*
2  * Copyright 2021 Grinn
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "ina23x_common.h"
8 
9 #include <zephyr/sys/byteorder.h>
10 
ina23x_reg_read_24(const struct i2c_dt_spec * bus,uint8_t reg,uint32_t * val)11 int ina23x_reg_read_24(const struct i2c_dt_spec *bus, uint8_t reg, uint32_t *val)
12 {
13 	uint8_t data[3];
14 	int ret;
15 
16 	ret = i2c_burst_read_dt(bus, reg, data, sizeof(data));
17 	if (ret < 0) {
18 		return ret;
19 	}
20 
21 	*val = sys_get_be24(data);
22 
23 	return ret;
24 }
25 
ina23x_reg_read_16(const struct i2c_dt_spec * bus,uint8_t reg,uint16_t * val)26 int ina23x_reg_read_16(const struct i2c_dt_spec *bus, uint8_t reg, uint16_t *val)
27 {
28 	uint8_t data[2];
29 	int ret;
30 
31 	ret = i2c_burst_read_dt(bus, reg, data, sizeof(data));
32 	if (ret < 0) {
33 		return ret;
34 	}
35 
36 	*val = sys_get_be16(data);
37 
38 	return ret;
39 }
40 
ina23x_reg_write(const struct i2c_dt_spec * bus,uint8_t reg,uint16_t val)41 int ina23x_reg_write(const struct i2c_dt_spec *bus, uint8_t reg, uint16_t val)
42 {
43 	uint8_t tx_buf[3];
44 
45 	tx_buf[0] = reg;
46 	sys_put_be16(val, &tx_buf[1]);
47 
48 	return i2c_write_dt(bus, tx_buf, sizeof(tx_buf));
49 }
50