Skyward boardcore
Loading...
Searching...
No Matches
I2C.h
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#pragma once
24
25#include "I2CDriver.h"
26
27namespace Boardcore
28{
29
39class I2C
40{
41public:
55 I2C(I2C_TypeDef* i2c, const miosix::GpioPin& scl,
56 const miosix::GpioPin& sda);
57
59 I2C(const I2C&) = delete;
60 I2C& operator=(const I2C&) = delete;
61 I2C(I2C&&) = delete;
62 I2C& operator=(I2C&&) = delete;
63
76 [[nodiscard]] bool read(const I2CDriver::I2CSlaveConfig& slaveConfig,
77 void* buffer, const size_t nBytes);
78
91 [[nodiscard]] bool write(const I2CDriver::I2CSlaveConfig& slaveConfig,
92 const void* buffer, const size_t nBytes);
93
106 [[nodiscard]] bool readRegister(
107 const I2CDriver::I2CSlaveConfig& slaveConfig,
108 const uint8_t registerAddress, uint8_t& registerContent);
109
122 [[nodiscard]] bool readRegister16(
123 const I2CDriver::I2CSlaveConfig& slaveConfig,
124 const uint8_t registerAddress, uint16_t& registerContent);
125
138 [[nodiscard]] bool readRegister24(
139 const I2CDriver::I2CSlaveConfig& slaveConfig,
140 const uint8_t registerAddress, uint32_t& registerContent);
141
154 [[nodiscard]] bool readRegister32(
155 const I2CDriver::I2CSlaveConfig& slaveConfig,
156 const uint8_t registerAddress, uint32_t& registerContent);
157
170 [[nodiscard]] bool writeRegister(
171 const I2CDriver::I2CSlaveConfig& slaveConfig,
172 const uint8_t registerAddress, const uint8_t registerContent);
173
186 [[nodiscard]] bool writeRegister16(
187 const I2CDriver::I2CSlaveConfig& slaveConfig,
188 const uint8_t registerAddress, const uint16_t registerContent);
189
202 [[nodiscard]] bool writeRegister24(
203 const I2CDriver::I2CSlaveConfig& slaveConfig,
204 const uint8_t registerAddress, const uint32_t registerContent);
205
218 [[nodiscard]] bool writeRegister32(
219 const I2CDriver::I2CSlaveConfig& slaveConfig,
220 const uint8_t registerAddress, const uint32_t registerContent);
221
235 [[nodiscard]] bool readFromRegister(
236 const I2CDriver::I2CSlaveConfig& slaveConfig,
237 const uint8_t registerAddress, void* buffer, const size_t nBytes);
238
246 [[nodiscard]] bool probe(const I2CDriver::I2CSlaveConfig& slaveConfig);
247
261 uint16_t getLastError();
262
263protected:
265};
266
270class SyncedI2C : public I2C
271{
272public:
280 SyncedI2C(I2C_TypeDef* i2c, const miosix::GpioPin& scl,
281 const miosix::GpioPin& sda);
282
284 SyncedI2C(const SyncedI2C&) = delete;
285 SyncedI2C& operator=(const SyncedI2C&) = delete;
286 SyncedI2C(SyncedI2C&&) = delete;
288
301 [[nodiscard]] bool read(const I2CDriver::I2CSlaveConfig& slaveConfig,
302 void* buffer, const size_t nBytes);
303
316 [[nodiscard]] bool write(const I2CDriver::I2CSlaveConfig& slaveConfig,
317 const void* buffer, const size_t nBytes);
318
331 [[nodiscard]] bool readRegister(
332 const I2CDriver::I2CSlaveConfig& slaveConfig,
333 const uint8_t registerAddress, uint8_t& registerContent);
334
347 [[nodiscard]] bool readRegister16(
348 const I2CDriver::I2CSlaveConfig& slaveConfig,
349 const uint8_t registerAddress, uint16_t& registerContent);
350
363 [[nodiscard]] bool readRegister24(
364 const I2CDriver::I2CSlaveConfig& slaveConfig,
365 const uint8_t registerAddress, uint32_t& registerContent);
366
379 [[nodiscard]] bool readRegister32(
380 const I2CDriver::I2CSlaveConfig& slaveConfig,
381 const uint8_t registerAddress, uint32_t& registerContent);
382
395 [[nodiscard]] bool writeRegister(
396 const I2CDriver::I2CSlaveConfig& slaveConfig,
397 const uint8_t registerAddress, const uint8_t registerContent);
398
411 [[nodiscard]] bool writeRegister16(
412 const I2CDriver::I2CSlaveConfig& slaveConfig,
413 const uint8_t registerAddress, const uint16_t registerContent);
414
427 [[nodiscard]] bool writeRegister24(
428 const I2CDriver::I2CSlaveConfig& slaveConfig,
429 const uint8_t registerAddress, const uint32_t registerContent);
430
443 [[nodiscard]] bool writeRegister32(
444 const I2CDriver::I2CSlaveConfig& slaveConfig,
445 const uint8_t registerAddress, const uint32_t registerContent);
446
460 [[nodiscard]] bool readFromRegister(
461 const I2CDriver::I2CSlaveConfig& slaveConfig,
462 const uint8_t registerAddress, void* buffer, const size_t nBytes);
463
473 [[nodiscard]] bool probe(const I2CDriver::I2CSlaveConfig& slaveConfig);
474
488 uint16_t getLastError();
489
490private:
491 miosix::FastMutex mutex;
492};
493
494} // namespace Boardcore
Low level driver for I2C peripherals.
Definition I2CDriver.h:57
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
I2C(I2C &&)=delete
bool probe(const I2CDriver::I2CSlaveConfig &slaveConfig)
Non blocking operation to check if a slave is available.
Definition I2C.cpp:198
I2C(const I2C &)=delete
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
I2C & operator=(I2C &&)=delete
I2C & operator=(const I2C &)=delete
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
Thread safe version of the I2C high-level driver.
Definition I2C.h:271
SyncedI2C(const SyncedI2C &)=delete
bool probe(const I2CDriver::I2CSlaveConfig &slaveConfig)
Check if a slave is available.
Definition I2C.cpp:298
SyncedI2C & operator=(SyncedI2C &&)=delete
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
SyncedI2C & operator=(const SyncedI2C &)=delete
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
SyncedI2C(SyncedI2C &&)=delete
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