i2c  1.4.2
RoadNarrows Robotics I2C Package
i2c.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: I2C
4 //
5 // Library: libi2c API
6 //
7 // File: i2c.h
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2016-01-28 14:19:12 -0700 (Thu, 28 Jan 2016) $
12  * $Rev: 4278 $
13  *
14  * \brief Low-level I<sup>2</sup>C communication level.
15  *
16  * This file has been modified from the original i2ccom.h source (see below).
17  *
18  * \author Robin Knight (robin.knight@roadnarrows.com)
19  *
20  * \copyright
21  * \h_copy 2007-2017. RoadNarrows LLC.\n
22  * http://www.roadnarrows.com\n
23  * All Rights Reserved
24  *
25  * \par Original Header:
26  * See "Original Source Header EULA" in source file.
27  *
28  * <hr>
29  */
30 /*
31  * @EulaBegin@
32  *
33  * Permission is hereby granted, without written agreement and without
34  * license or royalty fees, to use, copy, modify, and distribute this
35  * software and its documentation for any purpose, provided that
36  * (1) The above copyright notice and the following two paragraphs
37  * appear in all copies of the source code and (2) redistributions
38  * including binaries reproduces these notices in the supporting
39  * documentation. Substantial modifications to this software may be
40  * copyrighted by their authors and need not follow the licensing terms
41  * described here, provided that the new terms are clearly indicated in
42  * all files where they apply.
43  *
44  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
45  * OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
46  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
47  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
48  * EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
49  * THE POSSIBILITY OF SUCH DAMAGE.
50  *
51  * THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
52  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
53  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
54  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
55  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
56  *
57  * @EulaEnd@
58  */
59 ////////////////////////////////////////////////////////////////////////////////
60 
61 #ifndef _I2C_H
62 #define _I2C_H
63 
64 #include "rnr/rnrconfig.h"
65 #include "rnr/i2c-dev.h"
66 
67 C_DECLS_BEGIN
68 
69 /*!
70  * \brief I<sup>2</sup>C Device Address Type
71  */
72 typedef ushort_t i2c_addr_t;
73 
74 #define I2C_ADDR_NONE ((i2c_addr_t)(-1)) ///< no I2C address selected
75 
76 /*!
77  * \brief I<sup>2</sup>C Bus Handle Type
78  */
79 typedef struct i2c_struct
80 {
81  int fd; ///< opened file descriptor of the I2C bus device
82  i2c_addr_t addr; ///< address of the currently selected attached I2C device
83 } i2c_t;
84 
85 
86 // ---------------------------------------------------------------------------
87 // Prototypes
88 // ---------------------------------------------------------------------------
89 
90 /*!
91  * \brief Open the host I<sup>2</sup>C Bus device.
92  *
93  * \param [out] i2c Pointer to I<sup>2</sup>C Bus handle.\n
94  * Note: This descriptor must be allocated by the caller.
95  * \param device A null-terminated string of the device name.
96  * If NULL, then the default '/dev/i2c/0' is used.
97  * \return
98  * Returns 0 on success. Else errno is set appropriately and -1 is returned.
99  */
100 extern int i2c_open(i2c_t *i2c , const char *device);
101 
102 /*!
103  * \brief Closes an I<sup>2</sup>C Bus.
104  *
105  * \param [in,out] i2c Pointer to I<sup>2</sup>C Bus handle.
106  */
107 extern void i2c_close(i2c_t *i2c);
108 
109 /*!
110  * \brief Read from an I<sup>2</sup>C device.
111  *
112  * The i2c_read() primitive reads data from a device at the given address on
113  * the given I<sup>2</sup>C Bus.
114  *
115  * \param [in,out] i2c Pointer to I<sup>2</sup>C Bus handle.
116  * \param addr I<sup>2</sup>C device's 7/10-bit address.
117  * \param [out] buf Pointer to the buffer that will receive the data bytes.
118  * \param len Size of the buffer in bytes.
119  *
120  * \return
121  * On success, returns \h_ge 0 number of bytes read.\n
122  * Else errno is set appropriately and -1 is returned.
123  */
124 extern int i2c_read(i2c_t *i2c, i2c_addr_t addr, byte_t *buf , uint_t len);
125 
126 /*!
127  * \brief Write to an I<sup>2</sup>C device.
128  *
129  * The i2c_write() primitive writes data to a device at the given address
130  * on the given I<sup>2</sup>C Bus.
131  *
132  * \param [in,out] i2c Pointer to I<sup>2</sup>C Bus handle.
133  * \param addr I<sup>2</sup>C device's 7/10-bit address.
134  * \param buf Pointer to the buffer that will be written.
135  * \param len Number of bytes to write.
136  *
137  * \return
138  * On success, returns \h_ge 0 number of bytes written.\n
139  * Else errno is set appropriately and -1 is returned.
140  */
141 extern int i2c_write(i2c_t *i2c, i2c_addr_t addr, const byte_t *buf,
142  uint_t len);
143 
144 /*!
145  * \brief Perform a transfer with an I<sup>2</sup>C device.
146  *
147  * The i2c_transfer() primitive writes data to and then reads data from a
148  * device at the given address on the given I<sup>2</sup>C Bus. It is optimize
149  * to reduce start/stop sequences.
150  *
151  * \note Not all devices support the optimize tranfer.
152  * Use i2c_write() / i2c_read() as an alternate is these cases.
153  *
154  *
155  * \param [in] i2c Pointer to I<sup>2</sup>C Bus handle.
156  * \param addr I<sup>2</sup>C device's 7/10-bit address.
157  * \param write_buf Pointer to the buffer that contains the data
158  * to be written.
159  * \param write_len Number of bytes to write. The data are in
160  * the write buffer.
161  * \param [out] read_buf Pointer to the buffer that will receive the
162  * data.
163  * \param read_len Size of the read buffer in bytes.
164  *
165  * \return
166  * Returns \h_ge 0 on success.
167  * Else errno is set appropriately and -1 is returned.
168  */
169 extern int i2c_transfer(i2c_t *i2c, i2c_addr_t addr, const byte_t *write_buf,
170  uint_t write_len, byte_t *read_buf, uint_t read_len);
171 
172 /*!
173  * \brief Test the existance of a device at the given address on the
174  * given I<sup>2</sup>C Bus.
175  *
176  * \param [in] i2c Pointer to I<sup>2</sup>C Bus handle.
177  * \param addr I<sup>2</sup>C device's 7/10-bit address.
178  *
179  * \return
180  * Returns 1 if the device is present. Else returns 0.
181  */
182 extern int i2c_exists(i2c_t *i2c, i2c_addr_t addr);
183 
184 /*!
185  * \brief Scans the given I<sup>2</sup>C Bus to find all connected devices.
186  *
187  * For each device found, the provided callback function is called.
188  *
189  * \param [in] i2c Pointer to I<sup>2</sup>C Bus handle.
190  * \param callback A callback function called when a device is found.
191  * If this callback function returns a value <0 the
192  * bus scan stops immedialety and the value is
193  * used as return value by i2c_scan().
194  * \param context A user defined value or a pointer passed to the callback
195  * function.
196  *
197  * \return
198  * Returns the number of deviced found on success. Else errno is set
199  * appropriately and -1 is returned.
200  */
201 extern int i2c_scan(i2c_t *i2c,
202  int (*callback)(i2c_t *i2c, i2c_addr_t addr, void *context),
203  void *context);
204 
205 C_DECLS_END
206 
207 
208 #endif // _I2C_H
i2c_addr_t addr
address of the currently selected attached I2C device
Definition: i2c.h:82
int i2c_write(i2c_t *i2c, i2c_addr_t addr, const byte_t *buf, uint_t len)
Write to an I2C device.
Definition: i2ccom.c:159
I2C python modules.
int fd
opened file descriptor of the I2C bus device
Definition: i2c.h:81
ushort_t i2c_addr_t
I2C Device Address Type.
Definition: i2c.h:72
void i2c_close(i2c_t *i2c)
Closes an I2C Bus.
Definition: i2ccom.c:137
I2C Bus Handle Type.
Definition: i2c.h:79
int i2c_scan(i2c_t *i2c, int(*callback)(i2c_t *i2c, i2c_addr_t addr, void *context), void *context)
Scans the given I2C Bus to find all connected devices.
Definition: i2ccom.c:219
struct i2c_struct i2c_t
I2C Bus Handle Type.
int i2c_transfer(i2c_t *i2c, i2c_addr_t addr, const byte_t *write_buf, uint_t write_len, byte_t *read_buf, uint_t read_len)
Perform a transfer with an I2C device.
Definition: i2ccom.c:171
I2C character device interface.
int i2c_open(i2c_t *i2c, const char *device)
Open the host I2C Bus device.
Definition: i2ccom.c:118
int i2c_read(i2c_t *i2c, i2c_addr_t addr, byte_t *buf, uint_t len)
Read from an I2C device.
Definition: i2ccom.c:147
int i2c_exists(i2c_t *i2c, i2c_addr_t addr)
Test the existance of a device at the given address on the given I2C Bus.
Definition: i2ccom.c:199