Skyward boardcore
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Boardcore::I2C Class Reference

High level driver for the I2C peripherals. More...

#include <I2C.h>

Inheritance diagram for Boardcore::I2C:
Collaboration diagram for Boardcore::I2C:

Public Member Functions

 I2C (I2C_TypeDef *i2c, const miosix::GpioPin &scl, const miosix::GpioPin &sda)
 Constructor for the I2C high-level driver.
 
 I2C (const I2C &)=delete
 
I2Coperator= (const I2C &)=delete
 
 I2C (I2C &&)=delete
 
I2Coperator= (I2C &&)=delete
 
bool read (const I2CDriver::I2CSlaveConfig &slaveConfig, void *buffer, const size_t nBytes)
 Non blocking read operation to read nBytes.
 
bool write (const I2CDriver::I2CSlaveConfig &slaveConfig, const void *buffer, const size_t nBytes)
 Non blocking write operation to write nBytes.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
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.
 
bool probe (const I2CDriver::I2CSlaveConfig &slaveConfig)
 Non blocking operation to check if a slave is available.
 
uint16_t getLastError ()
 Returns the last errors happened in the communication.
 

Protected Attributes

I2CDriver i2c
 Instance of I2C low-level driver.
 

Detailed Description

High level driver for the I2C peripherals.

This driver is NOT thread safe. It implements high level functionalities such as:

  • Automatic bus recovery before each operation;
  • Method in order to read a one-byte register without issuing a stop condition (unique transaction).

Definition at line 39 of file I2C.h.

Constructor & Destructor Documentation

◆ I2C() [1/3]

Boardcore::I2C::I2C ( I2C_TypeDef * i2c,
const miosix::GpioPin & scl,
const miosix::GpioPin & sda )

Constructor for the I2C high-level driver.

It uses the low-level I2C constructor that internally initializes the pins so that they are always set to ALTERNATE_OD mode with Alternate Function 4 (the usual AF of I2C pins). Thanks to this we avoid the possibility of short circuits between master and slaves when they both drive the same bus on two different logical values.

Parameters
i2cStructure that represents the I2C peripheral.
sclSerial clock GpioPin of the relative I2C peripheral.
sdaSerial data GpioPin of the relative I2C peripheral. Delete copy/move constructors/operators.

Definition at line 28 of file I2C.cpp.

◆ I2C() [2/3]

Boardcore::I2C::I2C ( const I2C & )
delete

◆ I2C() [3/3]

Boardcore::I2C::I2C ( I2C && )
delete

Member Function Documentation

◆ getLastError()

uint16_t Boardcore::I2C::getLastError ( )

Returns the last errors happened in the communication.

For checking if a specific error occurred in the last transaction you can do if(getLastError() &Errors::<error-to-check>). Do not use == to check for errors because there could be more errors at once. To check if no errors occurred use if(getLastError() == Errors::NO_ERROR) or simply if(!getLastError())

Returns
A bit sequence where the bits set correspond to the last errors occurred in the peripheral (see the I2CDriver::Errors enum to get the correspondence between bit position and error reported).

Definition at line 204 of file I2C.cpp.

◆ operator=() [1/2]

I2C & Boardcore::I2C::operator= ( const I2C & )
delete

◆ operator=() [2/2]

I2C & Boardcore::I2C::operator= ( I2C && )
delete

◆ probe()

bool Boardcore::I2C::probe ( const I2CDriver::I2CSlaveConfig & slaveConfig)

Non blocking operation to check if a slave is available.

Warning
Check always if the operation succeeded or not!
Parameters
slaveConfigThe configuration struct of the slave device.
Returns
True if the device is available, false otherwise.

Definition at line 198 of file I2C.cpp.

◆ read()

bool Boardcore::I2C::read ( const I2CDriver::I2CSlaveConfig & slaveConfig,
void * buffer,
const size_t nBytes )

Non blocking read operation to read nBytes.

This method, if necessary, flushes the bus before the read operation is performed. In case of an error during the communication, this method returns false immediately.

Warning
Check always if the operation succeeded or not!
Parameters
slaveConfigThe configuration struct of the slave device.
bufferData buffer where to store the data read.
nBytesNumber of bytes to read.
Returns
True if the read is successful, false otherwise.

Definition at line 34 of file I2C.cpp.

◆ readFromRegister()

bool Boardcore::I2C::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.

This method, if necessary, flushes the bus before the read operation is performed. In case of an error during the communication, this method returns false immediately.

Warning
Check always if the operation succeeded or not!
Parameters
slaveConfigThe configuration struct of the slave device.
registerAddressByte that represents the address of the register.
bufferData buffer where to store the data read.
nBytesNumber of bytes to read.
Returns
True if the read is successful, false otherwise.

Definition at line 189 of file I2C.cpp.

◆ readRegister()

bool Boardcore::I2C::readRegister ( const I2CDriver::I2CSlaveConfig & slaveConfig,
const uint8_t registerAddress,
uint8_t & registerContent )

Non blocking operation to read an 8-bit register from a slave.

This method, if necessary, flushes the bus before the read operation is performed. In case of an error during the communication, this method returns false immediately.

Warning
Check always if the operation succeeded or not!
Parameters
slaveConfigThe configuration struct of the slave device.
registerAddressByte that represents the address of the register.
registerContentWhere to store the content of the register.
Returns
True if the read is successful, false otherwise.

Definition at line 48 of file I2C.cpp.

◆ readRegister16()

bool Boardcore::I2C::readRegister16 ( const I2CDriver::I2CSlaveConfig & slaveConfig,
const uint8_t registerAddress,
uint16_t & registerContent )

Non blocking operation to read a 16-bit register from a slave.

This method, if necessary, flushes the bus before the read operation is performed. In case of an error during the communication, this method returns false immediately.

Warning
Check always if the operation succeeded or not!
Parameters
slaveConfigThe configuration struct of the slave device.
registerAddressByte that represents the address of the register.
registerContentWhere to store the content of the register.
Returns
True if the read is successful, false otherwise.

Definition at line 56 of file I2C.cpp.

◆ readRegister24()

bool Boardcore::I2C::readRegister24 ( const I2CDriver::I2CSlaveConfig & slaveConfig,
const uint8_t registerAddress,
uint32_t & registerContent )

Non blocking operation to read a 24-bit register from a slave.

This method, if necessary, flushes the bus before the read operation is performed. In case of an error during the communication, this method returns false immediately.

Warning
Check always if the operation succeeded or not!
Parameters
slaveConfigThe configuration struct of the slave device.
registerAddressByte that represents the address of the register.
registerContentWhere to store the content of the register.
Returns
True if the read is successful, false otherwise.

Definition at line 73 of file I2C.cpp.

◆ readRegister32()

bool Boardcore::I2C::readRegister32 ( const I2CDriver::I2CSlaveConfig & slaveConfig,
const uint8_t registerAddress,
uint32_t & registerContent )

Non blocking operation to read a 32-bit register from a slave.

This method, if necessary, flushes the bus before the read operation is performed. In case of an error during the communication, this method returns false immediately.

Warning
Check always if the operation succeeded or not!
Parameters
slaveConfigThe configuration struct of the slave device.
registerAddressByte that represents the address of the register.
registerContentWhere to store the content of the register.
Returns
True if the read is successful, false otherwise.

Definition at line 91 of file I2C.cpp.

◆ write()

bool Boardcore::I2C::write ( const I2CDriver::I2CSlaveConfig & slaveConfig,
const void * buffer,
const size_t nBytes )

Non blocking write operation to write nBytes.

This method, if necessary, flushes the bus before the read operation is performed. In case of an error during the communication, this method returns false immediately.

Warning
Check always if the operation succeeded or not!
Parameters
slaveConfigThe configuration struct of the slave device.
bufferData buffer where to read the data to send.
nBytesNumber of bytes to send.
Returns
True if the write is successful, false otherwise.

Definition at line 41 of file I2C.cpp.

◆ writeRegister()

bool Boardcore::I2C::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.

This method, if necessary, flushes the bus before the write operation is performed. In case of an error during the communication, this method returns false immediately.

Warning
Check always if the operation succeeded or not!
Parameters
slaveConfigThe configuration struct of the slave device.
registerAddressByte that represents the address of the register.
registerContentThe content to write on the register.
Returns
True if the write is successful, false otherwise.

Definition at line 111 of file I2C.cpp.

◆ writeRegister16()

bool Boardcore::I2C::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.

This method, if necessary, flushes the bus before the read operation is performed. In case of an error during the communication, this method returns false immediately.

Warning
Check always if the operation succeeded or not!
Parameters
slaveConfigThe configuration struct of the slave device.
registerAddressByte that represents the address of the register.
registerContentThe content to write on the register.
Returns
True if the write is successful, false otherwise.

Definition at line 120 of file I2C.cpp.

◆ writeRegister24()

bool Boardcore::I2C::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.

This method, if necessary, flushes the bus before the read operation is performed. In case of an error during the communication, this method returns false immediately.

Warning
Check always if the operation succeeded or not!
Parameters
slaveConfigThe configuration struct of the slave device.
registerAddressByte that represents the address of the register.
registerContentThe content to write on the register.
Returns
True if the write is successful, false otherwise.

Definition at line 141 of file I2C.cpp.

◆ writeRegister32()

bool Boardcore::I2C::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.

This method, if necessary, flushes the bus before the read operation is performed. In case of an error during the communication, this method returns false immediately.

Warning
Check always if the operation succeeded or not!
Parameters
slaveConfigThe configuration struct of the slave device.
registerAddressByte that represents the address of the register.
registerContentThe content to write on the register.
Returns
True if the write is successful, false otherwise.

Definition at line 164 of file I2C.cpp.

Member Data Documentation

◆ i2c

I2CDriver Boardcore::I2C::i2c
protected

Instance of I2C low-level driver.

Definition at line 264 of file I2C.h.


The documentation for this class was generated from the following files: