libi2cd  1.0.3
Linux kernel I2C character device library
i2cd.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /*
3  * Copyright (C) 2021 Steven Stallion <sstallion@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published
7  * by the Free Software Foundation; either version 2.1 of the License, or
8  * (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13  * the GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef I2CD_H
20 #define I2CD_H
21 
22 #include <stddef.h>
23 #include <stdint.h>
24 #include <linux/i2c.h>
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
45 struct i2cd;
46 
55 struct i2cd *i2cd_open(const char *path);
56 
72 struct i2cd *i2cd_open_by_name(const char *name);
73 
89 struct i2cd *i2cd_open_by_number(unsigned int num);
90 
98 void i2cd_close(struct i2cd *dev);
99 
107 const char *i2cd_get_path(struct i2cd *dev);
108 
120 int i2cd_set_retries(struct i2cd *dev, unsigned long retries);
121 
132 int i2cd_set_timeout(struct i2cd *dev, unsigned long timeout);
133 
147 int i2cd_get_functionality(struct i2cd *dev, unsigned long *funcs);
148 
162 int i2cd_transfer(struct i2cd *dev, struct i2c_msg msgs[], size_t nmsgs);
163 
175 int i2cd_read(struct i2cd *dev, uint16_t addr, void *buf, size_t len);
176 
188 int i2cd_write(struct i2cd *dev, uint16_t addr, const void *buf, size_t len);
189 
204 int i2cd_write_read(struct i2cd *dev, uint16_t addr,
205  const void *write_buf, size_t write_len,
206  void *read_buf, size_t read_len);
207 
239 static inline int i2cd_register_read(struct i2cd *dev, uint16_t addr,
240  uint8_t reg, void *buf, size_t len)
241 {
242  return i2cd_write_read(dev, addr, &reg, sizeof(reg), buf, len);
243 }
244 
262 static inline int i2cd_register_read16(struct i2cd *dev, uint16_t addr,
263  uint16_t reg, void *buf, size_t len)
264 {
265  return i2cd_write_read(dev, addr, &reg, sizeof(reg), buf, len);
266 }
267 
271 #ifdef __cplusplus
272 }
273 #endif
274 
275 #endif /* I2CD_H */
i2cd_write_read
int i2cd_write_read(struct i2cd *dev, uint16_t addr, const void *write_buf, size_t write_len, void *read_buf, size_t read_len)
Write and read bytes from a slave device using a repeated START condition.
i2cd_write
int i2cd_write(struct i2cd *dev, uint16_t addr, const void *buf, size_t len)
Write bytes to a slave device.
i2cd_open_by_number
struct i2cd * i2cd_open_by_number(unsigned int num)
Open the I2C character device specified by num.
i2cd_register_read
static int i2cd_register_read(struct i2cd *dev, uint16_t addr, uint8_t reg, void *buf, size_t len)
Read bytes from an 8-bit slave register.
Definition: i2cd.h:239
i2cd_open_by_name
struct i2cd * i2cd_open_by_name(const char *name)
Open the I2C character device specified by name.
i2cd_close
void i2cd_close(struct i2cd *dev)
Close an I2C character device handle and free associated memory.
i2cd_read
int i2cd_read(struct i2cd *dev, uint16_t addr, void *buf, size_t len)
Read bytes from a slave device.
i2cd_set_timeout
int i2cd_set_timeout(struct i2cd *dev, unsigned long timeout)
Set the timeout in units of 10ms.
i2cd_register_read16
static int i2cd_register_read16(struct i2cd *dev, uint16_t addr, uint16_t reg, void *buf, size_t len)
Read bytes from a 16-bit slave register.
Definition: i2cd.h:262
i2cd_transfer
int i2cd_transfer(struct i2cd *dev, struct i2c_msg msgs[], size_t nmsgs)
Transfer one or more low-level messages terminated with a single STOP condition.
i2cd_get_functionality
int i2cd_get_functionality(struct i2cd *dev, unsigned long *funcs)
Get the adapter functionality mask.
i2cd_get_path
const char * i2cd_get_path(struct i2cd *dev)
Get the path used to open the I2C character device handle.
i2cd_set_retries
int i2cd_set_retries(struct i2cd *dev, unsigned long retries)
Set the number of times a slave address should be polled when not acknowledging.
i2cd_open
struct i2cd * i2cd_open(const char *path)
Open the I2C character device specified by path.
i2cd
Handle to an I2C character device.