Skyward boardcore
Loading...
Searching...
No Matches
I2C.cpp
Go to the documentation of this file.
1/* Copyright (c) 2022 Skyward Experimental Rocketry
2 * Author: Emilio Corigliano
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#include "I2C.h"
24
25namespace Boardcore
26{
27
28I2C::I2C(I2C_TypeDef* i2c, const miosix::GpioPin& scl,
29 const miosix::GpioPin& sda)
30 : i2c(i2c, scl, sda)
31{
32}
33
34bool I2C::read(const I2CDriver::I2CSlaveConfig& slaveConfig, void* buffer,
35 const size_t nBytes)
36{
37 i2c.flushBus();
38 return i2c.read(slaveConfig, buffer, nBytes);
39}
40
41bool I2C::write(const I2CDriver::I2CSlaveConfig& slaveConfig,
42 const void* buffer, const size_t nBytes)
43{
44 i2c.flushBus();
45 return i2c.write(slaveConfig, buffer, nBytes);
46}
47
49 const uint8_t registerAddress, uint8_t& registerContent)
50{
51 i2c.flushBus();
52 return i2c.write(slaveConfig, &registerAddress, 1, false) &&
53 i2c.read(slaveConfig, &registerContent, 1);
54}
55
57 const uint8_t registerAddress,
58 uint16_t& registerContent)
59{
60 i2c.flushBus();
61 uint8_t buf[2] = {0};
62 if (i2c.write(slaveConfig, &registerAddress, 1, false) &&
63 i2c.read(slaveConfig, buf, 2))
64 {
65 registerContent =
66 (slaveConfig.MSBFirst ? ((uint16_t)buf[0]) << 8 | buf[1]
67 : ((uint16_t)buf[1]) << 8 | buf[0]);
68 return true;
69 }
70 return false;
71}
72
74 const uint8_t registerAddress,
75 uint32_t& registerContent)
76{
77 i2c.flushBus();
78 uint8_t buf[3] = {0};
79 if (i2c.write(slaveConfig, &registerAddress, 1, false) &&
80 i2c.read(slaveConfig, buf, 3))
81 {
82 registerContent =
83 (slaveConfig.MSBFirst
84 ? ((uint32_t)buf[0]) << 16 | ((uint32_t)buf[1] << 8) | buf[2]
85 : ((uint32_t)buf[2]) << 16 | ((uint32_t)buf[1] << 8) | buf[0]);
86 return true;
87 }
88 return false;
89}
90
92 const uint8_t registerAddress,
93 uint32_t& registerContent)
94{
95 i2c.flushBus();
96 uint8_t buf[4] = {0};
97 if (i2c.write(slaveConfig, &registerAddress, 1, false) &&
98 i2c.read(slaveConfig, buf, 4))
99 {
100 registerContent =
101 (slaveConfig.MSBFirst
102 ? ((uint32_t)buf[0]) << 24 | ((uint32_t)buf[1] << 16) |
103 ((uint32_t)buf[2] << 8) | buf[3]
104 : ((uint32_t)buf[3]) << 24 | ((uint32_t)buf[2] << 16) |
105 ((uint32_t)buf[1] << 8) | buf[0]);
106 return true;
107 }
108 return false;
109}
110
112 const uint8_t registerAddress,
113 const uint8_t registerContent)
114{
115 const uint8_t reg[2] = {registerAddress, registerContent};
116 i2c.flushBus();
117 return i2c.write(slaveConfig, reg, 2);
118}
119
121 const uint8_t registerAddress,
122 const uint16_t registerContent)
123{
124 i2c.flushBus();
125 if (slaveConfig.MSBFirst)
126 {
127 const uint8_t reg[3] = {registerAddress, // subAddr
128 (uint8_t)(registerContent >> 8), // MSB
129 (uint8_t)(registerContent)}; // LSB
130 return i2c.write(slaveConfig, reg, 3);
131 }
132 else
133 {
134 const uint8_t reg[3] = {registerAddress, // subAddr
135 (uint8_t)(registerContent), // LSB
136 (uint8_t)(registerContent >> 8)}; // MSB
137 return i2c.write(slaveConfig, reg, 3);
138 }
139}
140
142 const uint8_t registerAddress,
143 const uint32_t registerContent)
144{
145 i2c.flushBus();
146 if (slaveConfig.MSBFirst)
147 {
148 const uint8_t reg[4] = {registerAddress, // subAddr
149 (uint8_t)(registerContent >> 16), // MSB
150 (uint8_t)(registerContent >> 8), //
151 (uint8_t)registerContent}; // LSB
152 return i2c.write(slaveConfig, reg, 4);
153 }
154 else
155 {
156 const uint8_t reg[4] = {registerAddress, // subAddr
157 (uint8_t)(registerContent), // LSB
158 (uint8_t)(registerContent >> 8), //
159 (uint8_t)(registerContent >> 16)}; // MSB
160 return i2c.write(slaveConfig, reg, 4);
161 }
162}
163
165 const uint8_t registerAddress,
166 const uint32_t registerContent)
167{
168 i2c.flushBus();
169 if (slaveConfig.MSBFirst)
170 {
171 const uint8_t reg[5] = {registerAddress, // subAddr
172 (uint8_t)(registerContent >> 24), // MSB
173 (uint8_t)(registerContent >> 16), //
174 (uint8_t)(registerContent >> 8), //
175 (uint8_t)registerContent}; // LSB
176 return i2c.write(slaveConfig, reg, 5);
177 }
178 else
179 {
180 const uint8_t reg[5] = {registerAddress, // subAddr
181 (uint8_t)(registerContent), // LSB
182 (uint8_t)(registerContent >> 8), //
183 (uint8_t)(registerContent >> 16), //
184 (uint8_t)(registerContent >> 24)}; // MSB
185 return i2c.write(slaveConfig, reg, 5);
186 }
187}
188
190 const uint8_t registerAddress, void* buffer,
191 const size_t nBytes)
192{
193 i2c.flushBus();
194 return i2c.write(slaveConfig, &registerAddress, 1, false) &&
195 i2c.read(slaveConfig, buffer, nBytes);
196}
197
198bool I2C::probe(const I2CDriver::I2CSlaveConfig& slaveConfig)
199{
200 i2c.flushBus();
201 return i2c.write(slaveConfig, nullptr, 0);
202}
203
204uint16_t I2C::getLastError() { return i2c.getLastError(); }
205
206SyncedI2C::SyncedI2C(I2C_TypeDef* i2c, const miosix::GpioPin& scl,
207 const miosix::GpioPin& sda)
208 : I2C(i2c, scl, sda)
209{
210}
211
212bool SyncedI2C::read(const I2CDriver::I2CSlaveConfig& slaveConfig, void* buffer,
213 const size_t nBytes)
214{
215 miosix::Lock<miosix::FastMutex> lock(mutex);
216 return I2C::read(slaveConfig, buffer, nBytes);
217}
218
220 const void* buffer, const size_t nBytes)
221{
222 miosix::Lock<miosix::FastMutex> lock(mutex);
223 return I2C::write(slaveConfig, buffer, nBytes);
224}
225
227 const uint8_t registerAddress,
228 uint8_t& registerContent)
229{
230 miosix::Lock<miosix::FastMutex> lock(mutex);
231 return I2C::readRegister(slaveConfig, registerAddress, registerContent);
232}
233
235 const uint8_t registerAddress,
236 uint16_t& registerContent)
237{
238 miosix::Lock<miosix::FastMutex> lock(mutex);
239 return I2C::readRegister16(slaveConfig, registerAddress, registerContent);
240}
241
243 const uint8_t registerAddress,
244 uint32_t& registerContent)
245{
246 miosix::Lock<miosix::FastMutex> lock(mutex);
247 return I2C::readRegister24(slaveConfig, registerAddress, registerContent);
248}
249
251 const uint8_t registerAddress,
252 uint32_t& registerContent)
253{
254 miosix::Lock<miosix::FastMutex> lock(mutex);
255 return I2C::readRegister32(slaveConfig, registerAddress, registerContent);
256}
257
259 const uint8_t registerAddress,
260 const uint8_t registerContent)
261{
262 miosix::Lock<miosix::FastMutex> lock(mutex);
263 return I2C::writeRegister(slaveConfig, registerAddress, registerContent);
264}
265
267 const uint8_t registerAddress,
268 const uint16_t registerContent)
269{
270 miosix::Lock<miosix::FastMutex> lock(mutex);
271 return I2C::writeRegister16(slaveConfig, registerAddress, registerContent);
272}
273
275 const uint8_t registerAddress,
276 const uint32_t registerContent)
277{
278 miosix::Lock<miosix::FastMutex> lock(mutex);
279 return I2C::writeRegister24(slaveConfig, registerAddress, registerContent);
280}
281
283 const uint8_t registerAddress,
284 const uint32_t registerContent)
285{
286 miosix::Lock<miosix::FastMutex> lock(mutex);
287 return I2C::writeRegister32(slaveConfig, registerAddress, registerContent);
288}
289
291 const uint8_t registerAddress, void* buffer,
292 const size_t nBytes)
293{
294 miosix::Lock<miosix::FastMutex> lock(mutex);
295 return I2C::readFromRegister(slaveConfig, registerAddress, buffer, nBytes);
296}
297
299{
300 miosix::Lock<miosix::FastMutex> lock(mutex);
301 return I2C::probe(slaveConfig);
302}
303
305{
306 miosix::Lock<miosix::FastMutex> lock(mutex);
307 return i2c.getLastError();
308}
309
310} // namespace Boardcore
bool read(const I2CSlaveConfig &slaveConfig, void *buffer, const size_t &nBytes)
Read operation to read nBytes. In case of an error during the communication, this method returns fals...
void flushBus()
Performs the recovery from the locked state if necessary.
uint16_t getLastError()
Returns the last errors happened in the communication.
bool write(const I2CSlaveConfig &slaveConfig, const void *buffer, const size_t &nBytes, bool generateStop=true)
Write operation to write nBytes. In case of an error during the communication, this method returns fa...
High level driver for the I2C peripherals.
Definition I2C.h:40
bool readFromRegister(const I2CDriver::I2CSlaveConfig &slaveConfig, const uint8_t registerAddress, void *buffer, const size_t nBytes)
Non blocking operation to read n-bytes from register from a slave.
Definition I2C.cpp:189
bool writeRegister32(const I2CDriver::I2CSlaveConfig &slaveConfig, const uint8_t registerAddress, const uint32_t registerContent)
Non blocking operation to write a 32-bit register from a slave.
Definition I2C.cpp:164
bool read(const I2CDriver::I2CSlaveConfig &slaveConfig, void *buffer, const size_t nBytes)
Non blocking read operation to read nBytes.
Definition I2C.cpp:34
bool probe(const I2CDriver::I2CSlaveConfig &slaveConfig)
Non blocking operation to check if a slave is available.
Definition I2C.cpp:198
bool writeRegister(const I2CDriver::I2CSlaveConfig &slaveConfig, const uint8_t registerAddress, const uint8_t registerContent)
Non blocking operation to write an 8-bit register from a slave.
Definition I2C.cpp:111
bool readRegister32(const I2CDriver::I2CSlaveConfig &slaveConfig, const uint8_t registerAddress, uint32_t &registerContent)
Non blocking operation to read a 32-bit register from a slave.
Definition I2C.cpp:91
uint16_t getLastError()
Returns the last errors happened in the communication.
Definition I2C.cpp:204
bool writeRegister24(const I2CDriver::I2CSlaveConfig &slaveConfig, const uint8_t registerAddress, const uint32_t registerContent)
Non blocking operation to write a 24-bit register from a slave.
Definition I2C.cpp:141
bool write(const I2CDriver::I2CSlaveConfig &slaveConfig, const void *buffer, const size_t nBytes)
Non blocking write operation to write nBytes.
Definition I2C.cpp:41
I2CDriver i2c
Instance of I2C low-level driver.
Definition I2C.h:264
I2C(I2C_TypeDef *i2c, const miosix::GpioPin &scl, const miosix::GpioPin &sda)
Constructor for the I2C high-level driver.
Definition I2C.cpp:28
bool writeRegister16(const I2CDriver::I2CSlaveConfig &slaveConfig, const uint8_t registerAddress, const uint16_t registerContent)
Non blocking operation to write a 16-bit register from a slave.
Definition I2C.cpp:120
bool readRegister16(const I2CDriver::I2CSlaveConfig &slaveConfig, const uint8_t registerAddress, uint16_t &registerContent)
Non blocking operation to read a 16-bit register from a slave.
Definition I2C.cpp:56
bool readRegister24(const I2CDriver::I2CSlaveConfig &slaveConfig, const uint8_t registerAddress, uint32_t &registerContent)
Non blocking operation to read a 24-bit register from a slave.
Definition I2C.cpp:73
bool readRegister(const I2CDriver::I2CSlaveConfig &slaveConfig, const uint8_t registerAddress, uint8_t &registerContent)
Non blocking operation to read an 8-bit register from a slave.
Definition I2C.cpp:48
bool probe(const I2CDriver::I2CSlaveConfig &slaveConfig)
Check if a slave is available.
Definition I2C.cpp:298
bool writeRegister16(const I2CDriver::I2CSlaveConfig &slaveConfig, const uint8_t registerAddress, const uint16_t registerContent)
Write a 16-bit register from the device.
Definition I2C.cpp:266
bool readRegister24(const I2CDriver::I2CSlaveConfig &slaveConfig, const uint8_t registerAddress, uint32_t &registerContent)
Read a 24-bit register from the device.
Definition I2C.cpp:242
bool write(const I2CDriver::I2CSlaveConfig &slaveConfig, const void *buffer, const size_t nBytes)
Write operation to write nBytes.
Definition I2C.cpp:219
bool writeRegister32(const I2CDriver::I2CSlaveConfig &slaveConfig, const uint8_t registerAddress, const uint32_t registerContent)
Write a 32-bit register from the device.
Definition I2C.cpp:282
uint16_t getLastError()
Returns the last errors happened in the communication.
Definition I2C.cpp:304
bool writeRegister24(const I2CDriver::I2CSlaveConfig &slaveConfig, const uint8_t registerAddress, const uint32_t registerContent)
Write a 24-bit register from the device.
Definition I2C.cpp:274
bool readRegister16(const I2CDriver::I2CSlaveConfig &slaveConfig, const uint8_t registerAddress, uint16_t &registerContent)
Read a 16-bit register from the device.
Definition I2C.cpp:234
bool read(const I2CDriver::I2CSlaveConfig &slaveConfig, void *buffer, const size_t nBytes)
Read operation to read nBytes.
Definition I2C.cpp:212
SyncedI2C(I2C_TypeDef *i2c, const miosix::GpioPin &scl, const miosix::GpioPin &sda)
Constructor for the synced I2C high-level driver.
Definition I2C.cpp:206
bool readRegister(const I2CDriver::I2CSlaveConfig &slaveConfig, const uint8_t registerAddress, uint8_t &registerContent)
Read an 8-bit register from the device.
Definition I2C.cpp:226
bool writeRegister(const I2CDriver::I2CSlaveConfig &slaveConfig, const uint8_t registerAddress, const uint8_t registerContent)
Write an 8-bit register from the device.
Definition I2C.cpp:258
bool readRegister32(const I2CDriver::I2CSlaveConfig &slaveConfig, const uint8_t registerAddress, uint32_t &registerContent)
Read a 32-bit register from the device.
Definition I2C.cpp:250
bool readFromRegister(const I2CDriver::I2CSlaveConfig &slaveConfig, const uint8_t registerAddress, void *buffer, const size_t nBytes)
Read n-bytes from register from a slave.
Definition I2C.cpp:290
This file includes all the types the logdecoder script will decode.
Configuration struct for a slave device. This will be used for configuring the bus in order to commun...
Definition I2CDriver.h:104