| Linduino 1.3.0 Linear Technology Arduino-Compatible Demonstration Board

LT_I2CBus.cpp
Go to the documentation of this file.
1 /*!
2 LTC I2CBus Support: Routines to communicate to I2C by Wire Library.
3
4 @verbatim
5
6 @endverbatim
7
8
9 Copyright 2018(c) Analog Devices, Inc.
10
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
15 - Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
17 - Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in
19 the documentation and/or other materials provided with the
20 distribution.
21 - Neither the name of Analog Devices, Inc. nor the names of its
22 contributors may be used to endorse or promote products derived
23 from this software without specific prior written permission.
24 - The use of this software may or may not infringe the patent rights
25 of one or more patent holders. This license does not release you
26 from the requirement that you obtain separate licenses from these
27 patent holders to use this software.
28 - Use of the software either in source or binary form, must be run
29 on or directly connected to an Analog Devices Inc. component.
30
31 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
32 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
33 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
35 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
37 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
39 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 */
42
43 //! @ingroup PMBus_SMBus
44 //! @{
45 //! @defgroup LT_I2CBus LT_I2CBus: Routines to communicate to I2C by Wire Library.
46 //! @}
47
48 /*! @file
49 @ingroup LT_I2CBus
50 Library File for LT_I2CBus
51 */
52
53 #include <Arduino.h>
54 #include <stdint.h>
55 //#include <util/delay.h>
56 #include "Linduino.h"
57 #include "LT_I2CBus.h"
58
59 LT_I2CBus::LT_I2CBus()
60 {
61 speed_ = 100000;
62 LT_Wire.begin(speed_);
63 inGroupProtocol_ = false;
64 }
65
66 LT_I2CBus::LT_I2CBus(uint32_t speed)
67 {
68 speed_ = speed;
69 LT_Wire.begin(speed_);
70 inGroupProtocol_ = false;
71 }
72
73 void LT_I2CBus::changeSpeed(uint32_t speed)
74 {
75 LT_Wire.begin(speed);
76 }
77
78 uint32_t LT_I2CBus::getSpeed()
79 {
80 return speed_;
81 }
82
83 // Read a byte, store in "value".
84 int8_t LT_I2CBus::readByte(uint8_t address, uint8_t *value)
85 {
86 uint8_t ret = 0;
87 LT_Wire.beginTransmission(address);
88 LT_Wire.requestFrom(address, value, (uint16_t)1);
89
90
91 return ret;
92 }
93
94 // Write "value" byte to device at "address"
95 int8_t LT_I2CBus::writeByte(uint8_t address, uint8_t value)
96 {
97 int8_t ret = 1;
98
99 LT_Wire.beginTransmission(address);
100 LT_Wire.write(value);
101 ret = LT_Wire.endTransmission(!inGroupProtocol_);
102 return ret;
103 }
104
105 // Read a byte of data at register specified by "command", store in "value"
106 int8_t LT_I2CBus::readByteData(uint8_t address, uint8_t command, uint8_t *value)
107 {
108 int8_t ret = 1;
109 LT_Wire.beginTransmission(address);
110 LT_Wire.write(command);
111 ret = LT_Wire.endTransmission(false);
112 LT_Wire.beginTransmission(address);
113 LT_Wire.requestFrom(address, value, (uint16_t)1);
114
115 return ret; // Return result
116 }
117
118 // Write a byte of data to register specified by "command"
119 int8_t LT_I2CBus::writeByteData(uint8_t address, uint8_t command, uint8_t value)
120 {
121 int8_t ret = 1;
122
123 LT_Wire.beginTransmission(address);
124 LT_Wire.write(command);
125 LT_Wire.write(value);
126 ret = LT_Wire.endTransmission(!inGroupProtocol_);
127 return ret;
128 }
129
130 // Read a 16-bit word of data from register specified by "command"
131 int8_t LT_I2CBus::readWordData(uint8_t address, uint8_t command, uint16_t *value)
132 {
133 int8_t ret = 1;
134
135 LT_Wire.beginTransmission(address);
136 LT_Wire.write(command);
137 ret = LT_Wire.endTransmission(false);
138 LT_Wire.beginTransmission(address);
139 uint8_t tempHolder[2];
140 LT_Wire.requestFrom(address, tempHolder, (uint16_t)2);
141 *value = tempHolder[0] << 8;
142 *value |= tempHolder[1];
143
144 return ret;
145 }
146
147 // Write a 16-bit word of data to register specified by "command"
148 int8_t LT_I2CBus::writeWordData(uint8_t address, uint8_t command, uint16_t value)
149 {
150 int8_t ret = 1;
151
152 LT_Wire.beginTransmission(address);
153 LT_Wire.write(command);
154 LT_Wire.write(value >> 8);
155 LT_Wire.write(value & 0xFF);
156 ret = LT_Wire.endTransmission(!inGroupProtocol_);
157
158 return ret;
159 }
160
161 int8_t LT_I2CBus::readBlockData(uint8_t address, uint8_t command, uint16_t length, uint8_t *values)
162 {
163 int8_t ret = 0;
164 LT_Wire.beginTransmission(address);
165 LT_Wire.write(command);
166 ret = LT_Wire.endTransmission(false);
167 LT_Wire.beginTransmission(address);
168 LT_Wire.requestFrom(address, values, length);
169
170 return ret;
171 }
172
173
174 // Read a block of data, no command byte, reads length number of bytes and stores it in values.
175 int8_t LT_I2CBus::readBlockData(uint8_t address, uint16_t length, uint8_t *values)
176 {
177 int8_t ret = 0;
178
179 LT_Wire.beginTransmission(address);
180 LT_Wire.requestFrom(address, values, length);
181
182 return ret;
183 }
184
185
186 // Write a block of data, starting at register specified by "command" and ending at (command + length - 1)
187 int8_t LT_I2CBus::writeBlockData(uint8_t address, uint8_t command, uint16_t length, uint8_t *values)
188 {
189 uint8_t i = length;
190 int8_t ret = 1;
191
192 LT_Wire.beginTransmission(address);
193 LT_Wire.write(command);
194 do
195 {
196 i--;
197 }
198 while (LT_Wire.write(values[length - 1 - i]) == 1 && i > 0);
199
200 ret = LT_Wire.endTransmission(!inGroupProtocol_);
201
202 return ret;
203 }
204
205 // Write two command bytes, then receive a block of data
206 int8_t LT_I2CBus::twoByteCommandReadBlock(uint8_t address, uint16_t command, uint16_t length, uint8_t *values)
207 {
208 int8_t ret = 0;
209
210 LT_Wire.beginTransmission(address);
211 LT_Wire.write(command >> 8);
212 LT_Wire.write(command & 0xFF);
213 ret = LT_Wire.endTransmission(false);
214 LT_Wire.beginTransmission(address);
215 LT_Wire.requestFrom(address, values, length);
216
217
218 return ret;
219 }
220
221 // Initializes Linduino I2C port.
222 // Before communicating to the I2C port throught the QuikEval connector, you must also run
223 // quikeval_I2C_connect to connect the I2C port to the QuikEval connector throught the
224 // QuikEval MUX (and disconnect SPI).
225 void LT_I2CBus::quikevalI2CInit(void)
226 {
227 // enable(); //! 1) Enable the I2C port;
228 }
229
230 // Switch MUX to connect I2C pins to QuikEval connector.
231 // This will disconnect SPI pins.
232 void LT_I2CBus::quikevalI2CConnect(void)
233 {
234 // Enable I2C
235 pinMode(QUIKEVAL_MUX_MODE_PIN, OUTPUT); //! 1) Set Mux pin as an output
236 digitalWrite(QUIKEVAL_MUX_MODE_PIN, HIGH); //! 2) Set the Mux pin to high
237 }
238
239
240 void LT_I2CBus::startGroupProtocol()
241 {
242 inGroupProtocol_ = true;
243 }
244 void LT_I2CBus::endGroupProtocol()
245 {
246 inGroupProtocol_ = false;
247 }
ret
long ret
Definition: EVAL-AD7124-8.ino:145
LT_I2CBus::endGroupProtocol
void endGroupProtocol(void)
ends group protocol so I2CBus knows to send STOPs again.
Definition: LT_I2CBus.cpp:244
LT_I2CBus::quikevalI2CInit
void quikevalI2CInit(void)
Initializes Linduino I2C port.
Definition: LT_I2CBus.cpp:225
LT_I2CBus::writeWordData
int8_t writeWordData(uint8_t address, uint8_t command, uint16_t value)
Write a 16-bit word of data to register specified by "command".
Definition: LT_I2CBus.cpp:148
LT_I2CBus::readByteData
int8_t readByteData(uint8_t address, uint8_t command, uint8_t *value)
Read a byte of data at register specified by "command", store in "value".
Definition: LT_I2CBus.cpp:106
LT_I2CBus::readBlockData
int8_t readBlockData(uint8_t address, uint8_t command, uint16_t length, uint8_t *values)
Read a block of data, starting at register specified by "command" and ending at (command + length - 1...
Definition: LT_I2CBus.cpp:161
LT_I2CBus::writeBlockData
int8_t writeBlockData(uint8_t address, uint8_t command, uint16_t length, uint8_t *values)
Write a block of data, starting at register specified by "command" and ending at (command + length - ...
Definition: LT_I2CBus.cpp:187
Linduino.h
Header File for Linduino Libraries and Demo Code.
QUIKEVAL_MUX_MODE_PIN
#define QUIKEVAL_MUX_MODE_PIN
QUIKEVAL_MUX_MODE_PIN defines the control pin for the QuikEval MUX.
Definition: Linduino.h:58
LT_TwoWire::requestFrom
uint8_t requestFrom(uint8_t address, uint8_t *acceptBuffer, uint16_t quantity)
Read from a slave I2C device.
Definition: LT_Wire.cpp:67
LT_I2CBus::startGroupProtocol
void startGroupProtocol(void)
starts group protocol so I2CBus knows to repeat START instead of STOP.
Definition: LT_I2CBus.cpp:240
address
static uint8_t address
Definition: DC2091A.ino:83
LT_I2CBus::twoByteCommandReadBlock
int8_t twoByteCommandReadBlock(uint8_t address, uint16_t command, uint16_t length, uint8_t *values)
Write a two command bytes, then receive a block of data.
Definition: LT_I2CBus.cpp:206
LT_I2CBus.h
LT_I2CBus: Routines to communicate to I2C by Wire Library.
LT_I2CBus::readWordData
int8_t readWordData(uint8_t address, uint8_t command, uint16_t *value)
Read a 16-bit word of data from register specified by "command".
Definition: LT_I2CBus.cpp:131
LT_I2CBus::changeSpeed
void changeSpeed(uint32_t speed)
Change the speed of the bus.
Definition: LT_I2CBus.cpp:73
LT_I2CBus::quikevalI2CConnect
void quikevalI2CConnect(void)
Switch MUX to connect I2C pins to QuikEval connector.
Definition: LT_I2CBus.cpp:232
LT_I2CBus::LT_I2CBus
LT_I2CBus()
Definition: LT_I2CBus.cpp:59
LT_I2CBus::writeByte
int8_t writeByte(uint8_t address, uint8_t value)
Write "value" byte to device at "address".
Definition: LT_I2CBus.cpp:95
LT_Wire
LT_TwoWire LT_Wire
Definition: LT_Wire.cpp:84
LT_I2CBus::getSpeed
uint32_t getSpeed()
Get the speed of the bus.
Definition: LT_I2CBus.cpp:78
LT_I2CBus::writeByteData
int8_t writeByteData(uint8_t address, uint8_t command, uint8_t value)
Write a byte of data to register specified by "command".
Definition: LT_I2CBus.cpp:119
i
static int i
Definition: DC2430A.ino:184
LT_I2CBus::readByte
int8_t readByte(uint8_t address, uint8_t *value)
Read a byte, store in "value".
Definition: LT_I2CBus.cpp:84
values
static uint8_t values[4]
Definition: DC2218A.ino:117
LT_TwoWire::begin
void begin(uint32_t speed)
Initiate Prep 10000 to 400000.
Definition: LT_Wire.cpp:52