You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
KED/ked/csl/interfaces/i2c.h

130 lines
6.2 KiB

/**
**************************************************************************************************
* @file i2c.h
* @author Kerem Yollu & Edwin Koch
* @date 18.07.2022
* @version 1.0
**************************************************************************************************
* @brief I2C communitation based on the Standart I2C Protocol V7 Defined by NXP/Philips :
* following third Party Protocols based on I2C Bus are not going to be implemented : C-BUS SMBUS PMBUS IPMI DDC ATCA
* This will also not have a I3C support for the forseable futrue.
*
* **Detailed Description :**
*
* I2C communitation based on the Standart I2C Protocol V7 Defined by NXP/Philips :
* following third Party Protocols based on I2C Bus are not going to be implemented : C-BUS SMBUS PMBUS IPMI DDC ATCA
* This will also not have a I3C support for the forseable futrue.
*
* @todo
* - 18.07.2021 : Implement the i2c.c.
**************************************************************************************************
*/
#ifndef _I2C_H_
#define _I2C_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#define I2C_STATE_RESET 1 // Not Initialized
#define I2C_STATE_READY 2 // Ready
#define I2C_STATE_TX 4 // Transmitting
#define I2C_STATE_RX 5 // Receiving
#define I2C_STATE_LISTEN 6 // Listening
#define I2C_STATE_ABORT 7 // Aborted by user
#define I2C_STATE_TIMEOUT 8 // Timeout
#define I2C_STATE_ERROR 9 // Error happened
/*! Enum of possible I2C opperation modes */
typedef enum{
i2cModeMaster, /*!< Master mode : In Master mode, the I2C interface initiates a data transfer and generates the clock signal. */
i2cModeMultyMaster, /*!< Multimaster Mode : In case if more than one master are present in one I2C bus, In such case each device needs to be able to cooperate with the fact that another device is currently talking and the bus is therefore busy (Arbirtation). For More information : https://www.i2c-bus.org/multimaster/ */
i2cModeSlave /*!< Slave mode : A slave device has to always be ready to detect and process a start condition and to recognize its address */
}i2cMode_t;
/*! Enum of possible I2C speeds */
typedef enum{
i2cSpeedStandart, /*!< SM 100 kbits/s This is the stantrd spped for any standart i2c Applications */
i2cSpeedFast, /*!< FM 400 kbits/s */
i2cSpeedFastPlus, /*!< FM+ 1 Mbits/s */
i2cSpeedHightSpeed, /*!< HS 3.4 Mbits/s */
i2cSpeedUltraFast /*!< UFM 5 Mbits/s */
}i2cSpeed_t;
/*! Enum of possible I2C Adress sizes */
typedef enum{
i2cAddressSizeSevenBits, /*!< 7 Bits address size */
i2cAddressSizeTenBits /*!< 10 Bits address size */
}i2cAddressSize_t;
/*! Enum of possible I2C Address count */
typedef enum{
i2cAddressCountSingle, /*!< Only one adress for communication */
i2cAddressCountDual /*!< Dual adresses for one device respondng to two adresses */
}i2cAddressCount_t;
/*! Enum for clock strechning activation. Can only be implmented as Slave for more information : https://www.i2c-bus.org/clock-stretching/ */
typedef enum{
i2cClockStrechingEnable, /*!< In situations where an I2C slave is not able to co-operate with the clock speed given by the master and needs to slow down a little. This is done by a mechanism referred to as clock stretching. */
i2cCLockStrechingDisable /*!< We assume that the master and slave have compatible Clock frequencies */
}i2cClockStreching_t;
typedef struct
{
uint16_t adress; /*!< First and Main address of the device */
i2cAddressSize_t adressSize; /*!< 10 or 7 bit adress size */
i2cAddressCount_t addresCount; /*!< SIngle or multiple */
i2cMode_t mode; /*!< Master, Slave or Multymaster Modes */
i2cSpeed_t speed;/*!< Bus Speed */
i2cClockStreching_t streching; /*!< Clock Streching enablede or disabled*/
uint32_t timing; /*!< Specifies the I2C timing The timings must be configured
in order to guarantee a correct data hold and setup time, used in master
and slave modes. */
uint16_t adressSecond; /*!< Second adress if dual adresse mode is configured*/
}i2cChannelDefinition_t;
/**
* @brief Initilize the I2C Hardware
*/
int8_t i2cInit(i2cCh_t i2cChannel);
void i2cRead(uint8_t *reg, uint8_t *buffer, uint8_t *regLenght, uint8_t *bufferLenght); // Defined by me : Read a given number of bytes
void i2cWrite(uint8_t *reg, uint8_t *data, uint8_t *regLenght, uint8_t *dataLenght); // Defined by me : Send a given number of bytes
uint8_t i2cTestDeviceSpeed(); // Defined by me : Cycle trough different modes until device cnat't answer fast enought
uint8_t i2cDiscoverDevices(); // Defined by me : Scan the awailable address range on standart mode to find devices
void i2cInitChannelAsMaster(); // Hardware Specific : Initilise the hardware channel in master mode
void i2cInitChannelAsSlave(); // Hardware Specific : Initilise the hardware channel in slavic mode (@life of boris)
void i2cFreeChannel(); // Hardware Specific : Free the hardware channel for othe recousrces
void i2cClockSynchronise();// I2C Standart : Clock Syncronization
void i2cReadDeviceInfo(); // I2c Standart : 3 Bytes (24 bits) | 12 Bits : Manufacturer info | 9 Bits: Part Identification | 3 Bits DIE Rev.
void i2cAbortTransmit(); // I2c Standart : Stop Communication for multimaster mode
void i2cClockStretch(); // I2C Standart : Optional For Pausing Communication because treatement takes longer than the communication
void i2cArbitration(); // I2C Standart : Arbitration for multimaster mode to define the right master.
void i2cSoftReset(); // I2C Standart : Software reset not supported by all hardware.
void i2cBusClear(); // I2C Standart : in case if SCL is stuck
void i2cSetSpeed(uint8_t speed); // I2C Standart
void i2cSetAddress(uint16_t address); // I2C Standart
void i2cSetAddressMode(); // I2C Standart
void i2cSetTimeout(uint8_t timeout); // Hardware specific
void i2cSetInterrupt(); // Hardware Specific
void i2cSetDma(); // Hardware specific
void i2cThrowError(int16_t error); // Defined by me : Private error function for error handling
void i2cPointReg(uint8_t reg); // Defined by me : Points to the register to be red from
#ifdef __cplusplus
}
#endif
#endif // _I2C_H_