Starter the init function reset function is done

i2c
kerem 3 years ago
parent 63a1ec72a8
commit cfbb4a49ea

@ -3,7 +3,7 @@
* @file i2c.h
* @author Kerem Yollu & Edwin Koch
* @date 18.07.2022
* @version 1.0
* @version 1.01
**************************************************************************************************
* @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
@ -16,7 +16,6 @@
* This will also not have a I3C support for the forseable futrue.
*
* @todo
* - 18.07.2021 : Implement the i2c.c
* - 26.07.2021 : Implement the DMA
* - 26.07.2021 : Implement the Interrupt
* - 26.07.2021 : Implement the Sleep/WakeUp
@ -34,6 +33,7 @@ extern "C" {
#include "hardwareDescription.h"
#include "pin.h"
/*! Enum of possible I2C opperation modes */
typedef enum{
i2cModeMaster, /*!< Master mode : In Master mode, the I2C interface initiates
@ -90,7 +90,7 @@ typedef enum{
i2cOpperationPolling, /*!< Polling mode Blocking, i2c communication states are constantly
checked on polling mode so it can be stoped
when interrupts occures **DEFAULT** */
i2cOpperationInt, /*!< Interrut Mode Non-blocking, i2c communications state changes
i2cOpperationInt, /*!< Interrut Mode Non-blocking, i2c communications state changes
generates an interrupt and can be handeled this way */
i2cOpperationDma /*!< DMA Mode Non-blocking, I2C communication satets are managed
via Inteerupts and with Direct Memory Access */
@ -115,7 +115,7 @@ typedef enum
/*! Struture a an i2c channel with all the required propereties*/
typedef struct
{
i2cCh_t channel; /*!< The harware channel to be used */
i2cCh_t channelNo; /*!< The harware channel to be used */
i2cMode_t mode; /*!< Master, Slave or Multymaster Modes */
uint16_t adress; /*!< First and Main address of the device */
uint16_t adressSecond; /*!< Second adress if dual adresse mode is configured */
@ -141,20 +141,20 @@ typedef struct
* @brief Initilize the I2C Hardware
* @param channel is the beforehand declared i2c channel with his opperation modes
*/
int8_t i2cInit(i2c_t channel);
void i2cInit(i2c_t *i2cHardware);
/**
* @brief De-Initilize the I2C Hardware
* @param channel is the beforehand declared i2c channel with his opperation modes
*/
int8_t i2cDeInit(i2c_t channel);
void i2cDeInit(i2c_t *i2cHardware);
/**
* @brief Set the i2c channle to the gievn mode
* @param Channel is the i2c hardware channel
* @param mode The mode for i2c : Master Slave or Multymaster
*/
void i2cSetMode(i2c_t channel, i2cMode_t mode);
void i2cSetMode(i2c_t *i2cHardware, i2cMode_t mode);
/**
* @brief Set the i2c channles mode as Master Slave or Multymaster
@ -163,21 +163,21 @@ void i2cSetMode(i2c_t channel, i2cMode_t mode);
* @param addressOne The forst address for the device
* @param addressTwo The second address for the device only if dual adress mode is not defined
*/
void i2cSetAddress(i2c_t channel, i2cAddressSize_t size, uint16_t addressOne, uint16_t addressTwo);
void i2cSetAddress(i2c_t *i2cHardware, i2cAddressSize_t size, uint16_t addressOne, uint16_t addressTwo);
/**
* @brief Stets the Communication speed
* @param channel is the beforehand declared i2c channel with his opperation modes
* @param speed the different awailable speeds
*/
void i2cSetSpeed(i2c_t channel, i2cSpeed_t speed);
void i2cSetSpeed(i2c_t *i2cHardware, i2cSpeed_t speed);
/**
* @brief Initiates the opperation mode for the selected i2c Channel
* @param channel is the beforehand declared i2c channel with his opperation modes
* @param opperation define if the i2c channel will opperate on pollin, Interrupt, or DMA modes
*/
void i2cSetOpperation(i2c_t channel, i2cOpperationMode_t opperation);
void i2cSetOpperation(i2c_t *i2cHardware, i2cOpperationMode_t opperation);
/**
* @brief Sets the finetuning of the Timings for the communication some standards as
@ -185,23 +185,28 @@ void i2cSetOpperation(i2c_t channel, i2cOpperationMode_t opperation);
* @param channel is the beforehand declared i2c channel with his opperation modes
* @param The value to be written in the register. This will heavily depend on the hardware
*/
void i2cSetTimings(i2c_t channel, uint32_t timing);
void i2cSetTimings(i2c_t *i2cHardware, uint32_t timing);
/**
* @brief Set the wakeup mode
* @param channel is the beforehand declared i2c channel with his opperation modes
* @param wake the desider wakeup mode for now i have found only one mode.
*/
void i2cSetWakeup(i2c_t channel, i2cWakeUpTypes_t wake);
void i2cSetWakeup(i2c_t *i2cHardware, i2cWakeUpTypes_t wake);
/**
* @brief Set the timeout to close the i2c wait time if a communication fails.
* this function is not linked to any channel and can be changed on the go
* @param channel is the beforehand declared i2c channel with his opperation modes
* @param timeout the desider timeout duration in ticks.
*/
void i2cSetTimeout(uint8_t timeout);
void i2cSetTimeout(i2c_t *i2cHardware, uint8_t timeout);
/**
* @brief Resets the i2c Periferal to it's inital state.
* @param channel is the beforehand declared i2c channel with his opperation modes
*/
void i2cReset(i2c_t *i2cHardware);
/**************************************************************************************************
I2C Communication functions independen of opperating mode
@ -218,7 +223,7 @@ void i2cSetTimeout(uint8_t timeout);
* @param data is the data to be written
* @param dataLenght is the total quantity of 8 bit data to be written.
*/
void i2cWrite(i2c_t channel, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data, uint8_t dataLenght);
void i2cWrite(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data, uint8_t dataLenght);
/**
* @brief Reads the given amount of data to the selected device. This function will
@ -231,7 +236,7 @@ void i2cWrite(i2c_t channel, uint16_t *devAddress, uint8_t *registerAddress, uin
* @param data is the data which has been red
* @paran dataLenght is the total quantity of 8 bit data to be red
*/
void i2cRead(i2c_t channel, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data, uint8_t dataLenght);
void i2cRead(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data, uint8_t dataLenght);
/**
* @brief Recieve a Single Byte as master from from the given devices register.
@ -241,7 +246,7 @@ void i2cRead(i2c_t channel, uint16_t *devAddress, uint8_t *registerAddress, uint
* @param registerAddress is the regiter to be red from the device
* @param data is the data whic has been red
*/
void i2cMasterRecieve(i2c_t channel, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data);
void i2cMasterRecieve(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data);
/**
* @brief Sends a Single Byte as master to the given devices register.
* this function will adapt himself to the selected oppeartion mode (Polling / Int / DMA)
@ -250,7 +255,7 @@ void i2cMasterRecieve(i2c_t channel, uint16_t *devAddress, uint8_t *registerAddr
* @param registerAddress is the regiter to be written to the device
* @param data is the data to be sent
*/
void i2cMasterSend(i2c_t channel, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data);
void i2cMasterSend(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data);
/**
* @brief Recieve a Single Byte as slave from given devices register.
@ -260,7 +265,7 @@ void i2cMasterSend(i2c_t channel, uint16_t *devAddress, uint8_t *registerAddress
* @param registerAddress is the regiter to be red from the device
* @param data is the data whic has been red
*/
void i2cSlaveRecieve(i2c_t channel, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data);
void i2cSlaveRecieve(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data);
/**
* @brief Recieve a Single Byte as slave from the given devices register.
@ -270,7 +275,7 @@ void i2cSlaveRecieve(i2c_t channel, uint16_t *devAddress, uint8_t *registerAddre
* @param registerAddress is the regiter to be red from the device
* @param data is the data whic has been red
*/
void i2cSlaveSend(i2c_t channel, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data);
void i2cSlaveSend(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data);
/**************************************************************************************************
@ -284,7 +289,7 @@ void i2cSlaveSend(i2c_t channel, uint16_t *devAddress, uint8_t *registerAddress,
* @param registerAddress is the regiter to be red from the device
* @param data is the data whic has been red
*/
void i2cMasterRecievePolling(i2c_t channel, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data);
void i2cMasterRecievePolling(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data);
/**
* @brief Sends a Single Byte in polling mode as master to the given devices register.
* @param channel is the beforehand declared i2c channel with his opperation modes
@ -292,7 +297,7 @@ void i2cMasterRecievePolling(i2c_t channel, uint16_t *devAddress, uint8_t *regis
* @param registerAddress is the regiter to be written to the device
* @param data is the data to be sent
*/
void i2cMasterSendPolling(i2c_t channel, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data);
void i2cMasterSendPolling(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data);
/**
* @brief Recieve a Single Byte in polling mide as slave from the given devices register.
@ -301,7 +306,7 @@ void i2cMasterSendPolling(i2c_t channel, uint16_t *devAddress, uint8_t *register
* @param registerAddress is the regiter to be red from the device
* @param data is the data whic has been red
*/
void i2cSlaveRecievePolling(i2c_t channel, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data);
void i2cSlaveRecievePolling(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data);
/**
* @brief Recieve a Single Byte in polling mode as slave from the given devices register.
@ -310,7 +315,7 @@ void i2cSlaveRecievePolling(i2c_t channel, uint16_t *devAddress, uint8_t *regist
* @param registerAddress is the regiter to be red from the device
* @param data is the data whic has been red
*/
void i2cSlaveSendPolling(i2c_t channel, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data);
void i2cSlaveSendPolling(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data);
/**************************************************************************************************
I2C Arbitration Functions for Multymaster mode and slaves clock streching
@ -335,7 +340,7 @@ void i2cBusClear(); // I2C Standart : in case if SCL is stuck
* @param devices list where the discevered devices will be written
* @param deviceCount Max number of devices to be discovered.
*/
void i2cDiscoverDevices(i2c_t channel, uint16_t *devices, uint8_t deviceCount);
void i2cDiscoverDevices(i2c_t *i2cHardware, uint16_t *devices, uint8_t deviceCount);
/**
* @brief This function will try to communicate with a device with every speed
@ -343,7 +348,7 @@ void i2cDiscoverDevices(i2c_t channel, uint16_t *devices, uint8_t deviceCount);
* @param channel is the beforehand declared i2c channel with his opperation modes
* @param devAddress is the address of the device to which the read command will be sent
*/
i2cSpeed_t i2cTestDeviceSpeed(i2c_t channel, uint16_t *devAddress);
i2cSpeed_t i2cTestDeviceSpeed(i2c_t *i2cHardware, uint16_t *devAddress);
/**
* @brief This function will read the device info register (if awailable) as follows :
@ -351,13 +356,13 @@ i2cSpeed_t i2cTestDeviceSpeed(i2c_t channel, uint16_t *devAddress);
* @param channel is the beforehand declared i2c channel with his opperation modes
* @param devAddress is the address of the device to be red
*/
uint32_t i2cReadDeviceInfo(i2c_t channel, uint16_t *devAddress);
uint32_t i2cReadDeviceInfo(i2c_t *i2cHardware, uint16_t *devAddress);
/**
* @brief The selected i2c channel is put on sleep.
* @param channel is the beforehand declared i2c channel with his opperation modes
*/
void i2cSleep(i2c_t channel);
void i2cSleep(i2c_t *i2cHardware);
/**
* @brief Error handling.

@ -242,25 +242,32 @@ static const uint32_t spiBase_Addr_List[MAX_SPI_CHANNEL_COUNT] = {
};
/*!
* Awailable I2C Channels
* Hadware dependent /
* Register independent !
* */
/*! Awailable I2C Channels Hadware dependent Register independent */
typedef enum{
I2C_CH_1
}i2cCh_t;
/*!
* I2C Channel Base adress
* Hadware dependent /
* Register dependent !
* */
/*! I2C Channel Base adress Hadware dependent Register dependent*/
static const uint32_t i2cBase_Addr_List[MAX_I2C_CHANNEL_COUNT] = {
I2C1_BASE
};
/*! RCC Bus number index list connected to the I2C */
static const uint8_t i2cBus_No[MAX_I2C_CHANNEL_COUNT] = {
1 /*!< I2C1 is connected to bus 1 */
};
/*! RCC I2C clock enable bit position for the given register*/
static const uint8_t i2cBus_En_bitPos[MAX_I2C_CHANNEL_COUNT] = {
RCC_APB1ENR_I2C1EN_Pos
};
/*! RCC I2C reset bit position for the given register*/
static const uint8_t i2cBus_Rst_bitPos[MAX_I2C_CHANNEL_COUNT] = {
RCC_APB1RSTR_I2C1RST_Pos
};
#ifdef __cplusplus
}

@ -35,3 +35,8 @@ target_compile_definitions(stmSPI PRIVATE ${C_DEFS})
target_include_directories(stmSPI PUBLIC ${INTERFACES_DIR} ${CSL_INCLUDES})
add_library(sub::spi ALIAS stmSPI)
add_library(stmI2C i2c.c)
target_compile_options(stmI2C PRIVATE ${C_FLAGS})
target_compile_definitions(stmI2C PRIVATE ${C_DEFS})
target_include_directories(stmI2C PUBLIC ${INTERFACES_DIR} ${CSL_INCLUDES})
add_library(sub::i2c ALIAS stmI2C)

@ -0,0 +1,39 @@
/**
**************************************************************************************************
* @file i2c.c
* @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. This code is generated for the stm32f4xK6 series of mcu for stm
*
*
* **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
**************************************************************************************************
*/
#include "i2c.h"
#define I2C_BASE ((I2C_TypeDef*)i2cBase_Addr_List[i2cHardware->channelNo])
void i2cInit(i2c_t *i2cHardware)
{
i2cReset(i2cHardware);
RCC->APB1ENR |= i2cBus_En_bitPos[i2cHardware->channelNo];
}
void i2cReset(i2c_t *i2cHardware)
{
RCC->APB1RSTR |= (1 << i2cBus_Rst_bitPos[i2cHardware->channelNo]);
RCC->APB1RSTR &= ~(1 << i2cBus_Rst_bitPos[i2cHardware->channelNo]);
}
Loading…
Cancel
Save