i2c  1.4.2
RoadNarrows Robotics I2C Package
i2c-dev.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: I2C
4 //
5 // Library: libi2c API
6 //
7 // File: i2c-dev.h
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2016-01-28 14:19:12 -0700 (Thu, 28 Jan 2016) $
12  * $Rev: 4278 $
13  *
14  * \brief I<sup>2</sup>C character device interface.
15  *
16  * This file has been modified from the original 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  * <hr>
26  * \par Original Source and Copyright:
27  * Copyright (C) 1995-97 Simon G. Vogl\n
28  * Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>
29  *
30  * \par Original Author:
31  * Simon G. Vogl\n
32  * Frodo Looijaard
33  *
34  * \par Original Header:
35  * See "Original Source Header EULA" in source file.
36  *
37  * <hr>
38  */
39 /*
40  * @EulaBegin@
41  *
42  * Permission is hereby granted, without written agreement and without
43  * license or royalty fees, to use, copy, modify, and distribute this
44  * software and its documentation for any purpose, provided that
45  * (1) The above copyright notice and the following two paragraphs
46  * appear in all copies of the source code and (2) redistributions
47  * including binaries reproduces these notices in the supporting
48  * documentation. Substantial modifications to this software may be
49  * copyrighted by their authors and need not follow the licensing terms
50  * described here, provided that the new terms are clearly indicated in
51  * all files where they apply.
52  *
53  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
54  * OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
55  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
56  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
57  * EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
58  * THE POSSIBILITY OF SUCH DAMAGE.
59  *
60  * THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
61  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
62  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
63  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
64  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
65  *
66  * @EulaEnd@
67  */
68 ////////////////////////////////////////////////////////////////////////////////
69 
70 #ifndef _I2C_DEV_H
71 #define _I2C_DEV_H
72 
73 #include "rnr/rnrconfig.h"
74 
75 C_DECLS_BEGIN
76 
77 //
78 // The 16 I2C reserved addresses. Addresses are 7-bits, which are found in the
79 // upper 7-bits of the address byte.
80 //
81 #define I2C_ADDR_GEN_CALL 0x00 ///< general call/start byte 0000000 x
82 #define I2C_ADDR_CBUS 0x01 ///< CBUS format (obsolete) 0000001 x
83 #define I2C_ADDR_DIFF_BUS 0x02 ///< different bus 0000010 x
84 #define I2C_ADDR_FUTURE0 0x03 ///< future use block 0 0000011 x
85 #define I2C_ADDR_HIGH_SPEED_0 0x04 ///< high-speed master 0000100 x
86 #define I2C_ADDR_HIGH_SPEED_1 0x05 ///< high-speed master 0000101 x
87 #define I2C_ADDR_HIGH_SPEED_2 0x06 ///< high-speed master 0000110 x
88 #define I2C_ADDR_HIGH_SPEED_3 0x07 ///< high-speed master 0000111 x
89 #define I2C_ADDR_TEN_BIT_0 0x78 ///< 10-bit addressing 1111000 x
90 #define I2C_ADDR_TEN_BIT_1 0x79 ///< 10-bit addressing 1111001 x
91 #define I2C_ADDR_TEN_BIT_2 0x7a ///< 10-bit addressing 1111010 x
92 #define I2C_ADDR_TEN_BIT_3 0x7b ///< 10-bit addressing 1111011 x
93 #define I2C_ADDR_FUTURE1_0 0x7c ///< future use block 1 1111100 x
94 #define I2C_ADDR_FUTURE1_1 0x7d ///< future use block 1 1111101 x
95 #define I2C_ADDR_FUTURE1_2 0x7e ///< future use block 1 1111110 x
96 #define I2C_ADDR_FUTURE1_3 0x7f ///< future use block 1 1111111 x
97 
98 //
99 // The 112 available I2C user device addresses.
100 //
101 #define I2C_ADDR_DEV_LOW (I2C_ADDR_HIGH_SPEED_3 + 1)
102  ///< first available device address
103 #define I2C_ADDR_DEV_HIGH (I2C_ADDR_TEN_BIT_0 - 1)
104  ///< last available device address
105 #define I2C_MAX_ADDRS (I2C_ADDR_DEV_HIGH - I2C_ADDR_DEV_LOW + 1)
106  ///< maximum number of I2C addresses
107 
108 //
109 // I2C message flags.
110 //
111 #define I2C_M_NOFLAGS 0 ///< no/clear flags
112 #define I2C_M_TEN 0x10 ///< ten-bit chip address
113 #define I2C_M_RD 0x01 ///< read bit
114 #define I2C_M_NOSTART 0x4000 ///< from original source - TBD
115 #define I2C_M_REV_DIR_ADDR 0x2000 ///< from original source - TBD
116 #define I2C_M_IGNORE_NAK 0x1000 ///< from original source - TBD
117 #define I2C_M_NO_RD_ACK 0x0800 ///< from original source - TBD
118 
119 /*!
120  * \brief I<sup>2</sup>C Message Stucture
121  *
122  * The i2c_msg_t is used for pure I<sup>2</sup>Cc transactions for the
123  * /dev interface.
124  */
125 typedef struct i2c_msg_struct
126 {
127  ushort_t addr; ///< 7/10-bit slave address 0xxx xxxx
128  ushort_t flags; ///< flags
129  short len; ///< message length (bytes)
130  char *buf; ///< pointer to message data
131 } i2c_msg_t;
132 
133 //
134 // Adapter Functionality Bits
135 //
136 #define I2C_FUNC_I2C 0x00000001 ///< low-level I2C
137 #define I2C_FUNC_10BIT_ADDR 0x0000000 ///< 10-bit address
138 #define I2C_FUNC_PROTOCOL_MANGLING 0x00000004
139  ///< I2C_M_{REV_DIR_ADDR,NOSTART,..}
140 #define I2C_FUNC_SMBUS_HWPEC_CALC 0x00000008 ///< SMBus 2.0
141 #define I2C_FUNC_SMBUS_BLOCK_PROC_CALL 0x00008000 ///< SMBus 2.0
142 #define I2C_FUNC_SMBUS_QUICK 0x00010000 ///< quick operation
143 #define I2C_FUNC_SMBUS_READ_BYTE 0x00020000 ///< read immediate byte
144 #define I2C_FUNC_SMBUS_WRITE_BYTE 0x00040000 ///< write immedieate byte
145 #define I2C_FUNC_SMBUS_READ_BYTE_DATA 0x00080000 ///< read data byte
146 #define I2C_FUNC_SMBUS_WRITE_BYTE_DATA 0x00100000 ///< wrtie data byte
147 #define I2C_FUNC_SMBUS_READ_WORD_DATA 0x00200000 ///< read data word
148 #define I2C_FUNC_SMBUS_WRITE_WORD_DATA 0x00400000 ///< write data word
149 #define I2C_FUNC_SMBUS_PROC_CALL 0x00800000 ///< w/r process word
150 #define I2C_FUNC_SMBUS_READ_BLOCK_DATA 0x01000000 ///< read data blcok
151 #define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 0x02000000 ///< write data blcok
152 #define I2C_FUNC_SMBUS_READ_I2C_BLOCK 0x04000000 ///< I2C-like block xfer
153 #define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK 0x08000000 ///< w/ 1-byte reg. addr.
154 #define I2C_FUNC_SMBUS_READ_I2C_BLOCK_2 0x10000000 ///< I2C-like block xfer
155 #define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK_2 0x20000000 ///< w/ 2-byte reg. addr.
156 #define I2C_FUNC_SMBUS_BYTE \
157  (I2C_FUNC_SMBUS_READ_BYTE | I2C_FUNC_SMBUS_WRITE_BYTE)
158  ///< smbus immediate byte support
159 #define I2C_FUNC_SMBUS_BYTE_DATA \
160  (I2C_FUNC_SMBUS_READ_BYTE_DATA | I2C_FUNC_SMBUS_WRITE_BYTE_DATA)
161  ///< smbus data byte support
162 #define I2C_FUNC_SMBUS_WORD_DATA \
163  (I2C_FUNC_SMBUS_READ_WORD_DATA | I2C_FUNC_SMBUS_WRITE_WORD_DATA)
164  ///< smbus data word support
165 #define I2C_FUNC_SMBUS_BLOCK_DATA \
166  (I2C_FUNC_SMBUS_READ_BLOCK_DATA | I2C_FUNC_SMBUS_WRITE_BLOCK_DATA)
167  ///< smbus data block support
168 #define I2C_FUNC_SMBUS_I2C_BLOCK \
169  (I2C_FUNC_SMBUS_READ_I2C_BLOCK | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)
170  ///< smbus i2c data block 1-byte address support
171 #define I2C_FUNC_SMBUS_I2C_BLOCK_2 \
172  (I2C_FUNC_SMBUS_READ_I2C_BLOCK_2 | I2C_FUNC_SMBUS_WRITE_I2C_BLOCK_2)
173  ///< smbus i2c data block 2-byte address support
174 
175 //
176 // Data for SMBus Messages
177 //
178 #define I2C_SMBUS_BLOCK_MAX 32 ///< As specified in SMBus standard
179 #define I2C_SMBUS_I2C_BLOCK_MAX 32 ///< Not specified - use same structure
180 
181 /*!
182  * \brief I<sup>2</sup>C SMBus Data Stucture
183  */
184 typedef union i2c_smbus_data_union
185 {
186  byte_t byte; ///< data byte
187  ushort_t word; ///< data short word
188  byte_t block[I2C_SMBUS_BLOCK_MAX + 2];
189  ///< block[0] is used for length and one more for PEC
191 
192 //
193 // SMBus ioctl access read or write operation markers (direction).
194 //
195 #define I2C_SMBUS_READ 1 ///< read
196 #define I2C_SMBUS_WRITE 0 ///< write
197 
198 //
199 // SMBus ioctl transaction types (size parameter in the above functions)
200 //
201 // Note: these no longer correspond to the (arbitrary) PIIX4 internal codes!
202 //
203 #define I2C_SMBUS_QUICK 0 ///< quick SMBus ioctl operation
204 #define I2C_SMBUS_BYTE 1 ///< immediate r/w byte operation
205 #define I2C_SMBUS_BYTE_DATA 2 ///< data byte r/w operation
206 #define I2C_SMBUS_WORD_DATA 3 ///< data word r/w operation
207 #define I2C_SMBUS_PROC_CALL 4 ///< issue word process call
208 #define I2C_SMBUS_BLOCK_DATA 5 ///< data block r/w operation
209 #define I2C_SMBUS_I2C_BLOCK_DATA 6 ///< i2c format block r/w/ operation
210 #define I2C_SMBUS_BLOCK_PROC_CALL 7 ///< SMBus 2.0: issue block process call
211 
212 
213 //
214 // I2C ioctl commands
215 //
216 // Note: Additional calls are defined in the algorithm and hw dependent layers.
217 // These can be listed here, or see the corresponding header files.
218 //
219 
220 #define I2C_NOCMD 0 ///< no command
221 #define I2C_RETRIES 0x0701
222  ///< number of times a device address should be polled when not acknowledging
223 #define I2C_TIMEOUT 0x0702 ///< set timeout - call with an int
224 #define I2C_SLAVE 0x0703 ///< change 7/10-bit slave address
225 #define I2C_SLAVE_FORCE 0x0706
226  ///< force change 7/10-bit slave address even if already taken
227 #define I2C_TENBIT 0x0704 ///< 0 for 7 bit addrs, != 0 for 10 bit
228 #define I2C_FUNCS 0x0705 ///< get the adapter functionality
229 #define I2C_RDWR 0x0707 ///< combined R/W transfer (one stop only)
230 #define I2C_PEC 0x0708 ///< != 0 for SMBus PEC
231 #define I2C_SMBUS 0x0720 ///< SMBus-level access
232 
233 
234 // Note: 10-bit addresses are NOT supported!
235 
236 /*!
237  * \brief I<sup>2</sup>C SMBus IOCTL Call Structure
238  */
240 {
241  byte_t read_write; ///< operation direction
242  byte_t command; ///< ioctl command
243  int size; ///< data size
244  i2c_smbus_data_t *data; ///< data
246 
247 /*!
248  * \brief I<sup>2</sup>C SMBus IOCTL Multi-Message Structure
249  */
251 {
252  i2c_msg_t *msgs; ///< pointers to i2c_msgs
253  int nmsgs; ///< number of i2c_msgs
255 
256 C_DECLS_END
257 
258 
259 #endif // _I2C_DEV_H
i2c_msg_t * msgs
pointers to i2c_msgs
Definition: i2c-dev.h:252
short len
message length (bytes)
Definition: i2c-dev.h:129
byte_t byte
data byte
Definition: i2c-dev.h:186
ushort_t addr
7/10-bit slave address 0xxx xxxx
Definition: i2c-dev.h:127
int nmsgs
number of i2c_msgs
Definition: i2c-dev.h:253
I2C SMBus IOCTL Multi-Message Structure.
Definition: i2c-dev.h:250
struct i2c_rdwr_ioctl_data_struct i2c_rdwr_ioctl_data_t
I2C SMBus IOCTL Multi-Message Structure.
ushort_t flags
flags
Definition: i2c-dev.h:128
struct i2c_msg_struct i2c_msg_t
I2C Message Stucture.
ushort_t word
data short word
Definition: i2c-dev.h:187
char * buf
pointer to message data
Definition: i2c-dev.h:130
I2C SMBus Data Stucture.
Definition: i2c-dev.h:184
I2C SMBus IOCTL Call Structure.
Definition: i2c-dev.h:239
#define I2C_SMBUS_BLOCK_MAX
As specified in SMBus standard.
Definition: i2c-dev.h:178
union i2c_smbus_data_union i2c_smbus_data_t
I2C SMBus Data Stucture.
byte_t read_write
operation direction
Definition: i2c-dev.h:241
struct i2c_smbus_ioctl_data_struct i2c_smbus_ioctl_data_t
I2C SMBus IOCTL Call Structure.
i2c_smbus_data_t * data
data
Definition: i2c-dev.h:244
I2C Message Stucture.
Definition: i2c-dev.h:125
byte_t command
ioctl command
Definition: i2c-dev.h:242