1 /* ST Microelectronics STMEMS hal i/f
2  *
3  * Copyright (c) 2021 STMicroelectronics
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  *
7  * zephyrproject-rtos/modules/hal/st/sensor/stmemsc/
8  */
9 
10 #include "stmemsc.h"
11 
12  /* Enable address auto-increment on some stmemsc sensors */
13 #define STMEMSC_I2C_ADDR_AUTO_INCR		BIT(7)
14 
stmemsc_i2c_read(const struct i2c_dt_spec * stmemsc,uint8_t reg_addr,uint8_t * value,uint8_t len)15 int stmemsc_i2c_read(const struct i2c_dt_spec *stmemsc,
16 		     uint8_t reg_addr, uint8_t *value, uint8_t len)
17 {
18 	return i2c_burst_read_dt(stmemsc, reg_addr, value, len);
19 }
20 
stmemsc_i2c_write(const struct i2c_dt_spec * stmemsc,uint8_t reg_addr,uint8_t * value,uint8_t len)21 int stmemsc_i2c_write(const struct i2c_dt_spec *stmemsc,
22 		      uint8_t reg_addr, uint8_t *value, uint8_t len)
23 {
24 	uint8_t buf[CONFIG_STMEMSC_I3C_I2C_WRITE_BUFFER_SIZE];
25 
26 	__ASSERT_NO_MSG(len <= sizeof(buf) - 1);
27 
28 	buf[0] = reg_addr;
29 	memcpy(&buf[1], value, len);
30 
31 	return i2c_write_dt(stmemsc, buf, len + 1);
32 }
33 
stmemsc_i2c_read_incr(const struct i2c_dt_spec * stmemsc,uint8_t reg_addr,uint8_t * value,uint8_t len)34 int stmemsc_i2c_read_incr(const struct i2c_dt_spec *stmemsc,
35 			  uint8_t reg_addr, uint8_t *value, uint8_t len)
36 {
37 	reg_addr |= STMEMSC_I2C_ADDR_AUTO_INCR;
38 	return stmemsc_i2c_read(stmemsc, reg_addr, value, len);
39 }
40 
stmemsc_i2c_write_incr(const struct i2c_dt_spec * stmemsc,uint8_t reg_addr,uint8_t * value,uint8_t len)41 int stmemsc_i2c_write_incr(const struct i2c_dt_spec *stmemsc,
42 			   uint8_t reg_addr, uint8_t *value, uint8_t len)
43 {
44 	uint8_t buf[CONFIG_STMEMSC_I3C_I2C_WRITE_BUFFER_SIZE];
45 
46 	__ASSERT_NO_MSG(len <= sizeof(buf) - 1);
47 
48 	buf[0] = reg_addr | STMEMSC_I2C_ADDR_AUTO_INCR;
49 	memcpy(&buf[1], value, len);
50 
51 	return i2c_write_dt(stmemsc, buf, len + 1);
52 }
53