| Linduino 1.3.0 Linear Technology Arduino-Compatible Demonstration Board

LT_I2C_Wire.h
Go to the documentation of this file.
1
2 /*!
3 LT_I2C: Routines to communicate with ATmega328P's hardware I2C port.
4
5
6 Copyright 2018(c) Analog Devices, Inc.
7
8 All rights reserved.
9
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 - Redistributions of source code must retain the above copyright
13 notice, this list of conditions and the following disclaimer.
14 - Redistributions in binary form must reproduce the above copyright
15 notice, this list of conditions and the following disclaimer in
16 the documentation and/or other materials provided with the
17 distribution.
18 - Neither the name of Analog Devices, Inc. nor the names of its
19 contributors may be used to endorse or promote products derived
20 from this software without specific prior written permission.
21 - The use of this software may or may not infringe the patent rights
22 of one or more patent holders. This license does not release you
23 from the requirement that you obtain separate licenses from these
24 patent holders to use this software.
25 - Use of the software either in source or binary form, must be run
26 on or directly connected to an Analog Devices Inc. component.
27
28 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
29 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
30 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
32 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
34 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*! @file
41 @ingroup LT_I2C
42 Library Header File for LT_I2C: Routines to communicate with ATmega328P's hardware I2C port.
43 */
44
45 #ifndef LT_I2C_H
46 #define LT_I2C_H
47
48 #include <stdint.h>
49 //#include <Wire.h>
50
51 //! @name HARDWARE I2C PRESCALER VALUES
52 //! @{
53 #define HARDWARE_I2C_PRESCALER_1 0
54 #define HARDWARE_I2C_PRESCALER_4 1
55 #define HARDWARE_I2C_PRESCALER_16 2
56 #define HARDWARE_I2C_PRESCALER_64 3
57 //! @}
58
59 //! @name I2C READ and WRITE BITS
60 //! @{
61 //! Eighth bit (LSB) of I2C address indicates a "read" or "write".
62 //! (The first seven bits are the 7-bit I2C address.)
63 #define I2C_READ_BIT 0x01
64 #define I2C_WRITE_BIT 0x00
65 //! @}
66
67 //! @name STATUS BITS
68 //! @{
69 #define STATUS_START 0x08
70 #define STATUS_REPEATED_START 0x10
71 #define STATUS_ADDRESS_WRITE_ACK 0x18
72 #define STATUS_ADDRESS_WRITE_NACK 0x20
73 #define STATUS_WRITE_ACK 0x28
74 #define STATUS_WRITE_NACK 0x30
75 #define STATUS_ARBITRATION_LOST 0x38
76 #define STATUS_ADDRESS_READ_ACK 0x40
77 #define STATUS_ADDRESS_READ_NACK 0x48
78 #define STATUS_READ_ACK 0x50
79 #define STATUS_READ_NACK 0x58
80 //! @}
81
82 //! @name TIMEOUT AND DELAY IN US
83 //! @{
84 #define HW_I2C_DELAY 1
85 #define HW_I2C_TIMEOUT 20000
86 //! @}
87
88 //! @name ACK OR NACK PARAMETER PASSED TO I2C_READ
89 //! @{
90 #define WITH_ACK 0 //!< Use with i2c_read(WITH_ACK) to read with an acknowledge
91 #define WITH_NACK 1 //!< Use with i2c_read(WITH_NACK) to read without an acknowledge. Normally used after the last byte of a multi-byte read.
92 //! @}
93
94 //! @name OPTIONAL I2C Address MACRO
95 //! @{
96 #define I2C_8ADDR(address) (address >> 1) //!< Use to convert an 8-bit I2C address to 7 bits.
97 //! @}
98
99 //! Read a byte, store in "value".
100 //! @return 0 on success, 1 on failure
101 int8_t i2c_read_byte(uint8_t address, //!< 7-bit I2C address
102 uint8_t *value //!< Byte to be read
103 );
104
105 //! Write "value" byte to device at "address"
106 //! @return 0 on success, 1 on failure
107 int8_t i2c_write_byte(uint8_t address, //!< 7-bit I2C address
108 uint8_t value //!< Byte to be written
109 );
110
111 //! Read a byte of data at register specified by "command", store in "value"
112 //! @return 0 on success, 1 on failure
113 int8_t i2c_read_byte_data(uint8_t address, //!< 7-bit I2C address
114 uint8_t command, //!< Command byte
115 uint8_t *value //!< Byte to be read
116 );
117
118 //! Write a byte of data to register specified by "command"
119 //! @return 0 on success, 1 on failure
120 int8_t i2c_write_byte_data(uint8_t address, //!< 7-bit I2C address
121 uint8_t command, //!< Command byte
122 uint8_t value //!< Byte to be written
123 );
124
125 //! Read a 16-bit word of data from register specified by "command"
126 //! @return 0 on success, 1 on failure
127 int8_t i2c_read_word_data(uint8_t address, //!< 7-bit I2C address
128 uint8_t command, //!< Command byte
129 uint16_t *value //!< Word to be read
130 );
131
132 //! Write a 16-bit word of data to register specified by "command"
133 //! @return 0 on success, 1 on failure
134 int8_t i2c_write_word_data(uint8_t address, //!< 7-bit I2C address
135 uint8_t command, //!< Command byte
136 uint16_t value //!< Word to be written
137 );
138
139 //! Read a block of data, starting at register specified by "command" and ending at (command + length - 1)
140 //! @return 0 on success, 1 on failure
141 int8_t i2c_read_block_data(uint8_t address, //!< 7-bit I2C address
142 uint8_t command, //!< Command byte
143 uint8_t length, //!< Length of array
144 uint8_t *values //!< Byte array to be read
145 );
146
147 //! Read a block of data, no command byte, reads length number of bytes and stores it in values.
148 //! @return 0 on success, 1 on failure
149 int8_t i2c_read_block_data(uint8_t address, //!< 7-bit I2C address
150 uint8_t length, //!< Length of array
151 uint8_t *values //!< Byte array to be read
152 );
153
154
155 //! Write a block of data, starting at register specified by "command" and ending at (command + length - 1)
156 //! @return 0 on success, 1 on failure
157 int8_t i2c_write_block_data(uint8_t address, //!< 7-bit I2C address
158 uint8_t command, //!< Command byte
159 uint8_t length, //!< Length of array
160 uint8_t *values //!< Byte array to be written
161 );
162
163 //! Write a two command bytes, then receive a block of data
164 //! @return 0 on success, 1 on failure
165 int8_t i2c_two_byte_command_read_block(uint8_t address, //!< 7-bit I2C address
166 uint16_t command, //!< Command word
167 uint8_t length, //!< Length of array
168 uint8_t *values //!< Byte array to be read
169 );
170
171 //! Initializes Linduino I2C port.
172 //! Before communicating to the I2C port through the QuikEval connector, you must also run
173 //! quikeval_I2C_connect to connect the I2C port to the QuikEval connector through the
174 //! QuikEval MUX (and disconnect SPI).
175 void quikeval_I2C_init(void);
176
177 //! Switch MUX to connect I2C pins to QuikEval connector.
178 //! This will disconnect SPI pins.
179 void quikeval_I2C_connect(void);
180
181 //! i2c_enable or quikeval_I2C_init must be called before using any of the other I2C routines.
182 void i2c_enable(void);
183
184 //! Write start bit to the hardware I2C port
185 //! @return 0 if successful, 1 if not successful
186 int8_t i2c_start();
187
188 //! Write a repeat start bit to the hardware I2C port
189 //! @return 0 if successful, 1 if not successful
190 int8_t i2c_repeated_start();
191
192 //! Write stop bit to the hardware I2C port
193 void i2c_stop();
194
195 //! Send a data byte to hardware I2C port
196 //! @return 0 if successful, 1 if not successful
197 int8_t i2c_write(uint8_t data //!< byte that will be written to hardware I2C port.
198 );
199
200 //! Read a data byte from the hardware I2C port.
201 //! If ack is 0 then an acknowledge (ACK) is generated, else a NACK is generated.
202 //! @return the data byte read.
203 uint8_t i2c_read(int8_t ack //!< If ACK is 0 then an acknowledge is generated, else a NACK is generated.
204 );
205
206 //! Poll the I2C port and look for an acknowledge
207 //! @return Returns 0 if successful, 1 if not successful
208 int8_t i2c_poll(uint8_t i2c_address //!< i2c_address is the address of the slave being polled.
209 );
210
211
212 #endif // LT_I2C_H
quikeval_I2C_init
void quikeval_I2C_init(void)
Initializes Linduino I2C port.
Definition: LT_I2C.cpp:394
i2c_address
uint8_t i2c_address
i2c_read_byte_data
int8_t i2c_read_byte_data(uint8_t address, uint8_t command, uint8_t *value)
Read a byte of data at register specified by "command", store in "value".
Definition: LT_I2C.cpp:124
i2c_write_byte
int8_t i2c_write_byte(uint8_t address, uint8_t value)
Write "value" byte to device at "address".
Definition: LT_I2C.cpp:109
i2c_enable
void i2c_enable(void)
i2c_enable or quikeval_I2C_init must be called before using any of the other I2C routines.
Definition: LT_I2C.cpp:414
i2c_stop
void i2c_stop()
Write stop bit to the hardware I2C port.
Definition: LT_I2C.cpp:462
address
static uint8_t address
Definition: DC2091A.ino:83
data
union LT_union_int32_4bytes data
Definition: DC2094A.ino:138
i2c_read_word_data
int8_t i2c_read_word_data(uint8_t address, uint8_t command, uint16_t *value)
Read a 16-bit word of data from register specified by "command".
Definition: LT_I2C.cpp:172
i2c_write_word_data
int8_t i2c_write_word_data(uint8_t address, uint8_t command, uint16_t value)
Write a 16-bit word of data to register specified by "command".
Definition: LT_I2C.cpp:216
i2c_read
uint8_t i2c_read(int8_t ack)
Read a data byte from the hardware I2C port.
Definition: LT_I2C.cpp:491
i2c_write
int8_t i2c_write(uint8_t data)
Send a data byte to hardware I2C port.
Definition: LT_I2C.cpp:470
quikeval_I2C_connect
void quikeval_I2C_connect(void)
Switch MUX to connect I2C pins to QuikEval connector.
Definition: LT_I2C.cpp:401
i2c_write_byte_data
int8_t i2c_write_byte_data(uint8_t address, uint8_t command, uint8_t value)
Write a byte of data to register specified by "command".
Definition: LT_I2C.cpp:155
i2c_write_block_data
int8_t i2c_write_block_data(uint8_t address, uint8_t command, uint8_t length, uint8_t *values)
Write a block of data, starting at register specified by "command" and ending at (command + length - ...
Definition: LT_I2C.cpp:321
i2c_read_block_data
int8_t i2c_read_block_data(uint8_t address, uint8_t command, uint8_t length, uint8_t *values)
Read a block of data, starting at register specified by "command" and ending at (command + length - 1...
Definition: LT_I2C.cpp:244
i2c_repeated_start
int8_t i2c_repeated_start()
Write a repeat start bit to the hardware I2C port.
Definition: LT_I2C.cpp:444
i2c_read_byte
int8_t i2c_read_byte(uint8_t address, uint8_t *value)
Read a byte, store in "value".
Definition: LT_I2C.cpp:93
i2c_two_byte_command_read_block
int8_t i2c_two_byte_command_read_block(uint8_t address, uint16_t command, uint8_t length, uint8_t *values)
Write a two command bytes, then receive a block of data.
Definition: LT_I2C.cpp:345
i2c_poll
int8_t i2c_poll(uint8_t i2c_address)
Poll the I2C port and look for an acknowledge.
Definition: LT_I2C.cpp:526
values
static uint8_t values[4]
Definition: DC2218A.ino:117
i2c_start
int8_t i2c_start()
Write start bit to the hardware I2C port.
Definition: LT_I2C.cpp:425