Working i2c function divided as imp_i2c.c and deirver level i2c.c usignt he same header from interfaces/i2c.h

i2c
kerem 3 years ago
parent e048155400
commit 913663516e

@ -24,7 +24,7 @@ set(EXECUTABLE ${PROJECT_NAME}) #Create the executable
#################################################################################################### ####################################################################################################
set(INTERFACES_DIR ${CMAKE_SOURCE_DIR}/csl/interfaces) set(INTERFACES_DIR ${CMAKE_SOURCE_DIR}/csl/interfaces)
set(UTILS_DIR ${CMAKE_SOURCE_DIR}/utils/assert) set(UTILS_DIR ${CMAKE_SOURCE_DIR}/utils/assert)
set(DRIVERS_DIR ${CMAKE_DRIVERS_DIR}/drivers) set(DRIVERS_DIR ${CMAKE_SOURCE_DIR}/drivers)
#################################################################################################### ####################################################################################################
#SUBDIRECTORIES Will add the given folders to the porject an check for CmakeLists.txt #SUBDIRECTORIES Will add the given folders to the porject an check for CmakeLists.txt
@ -40,6 +40,7 @@ message("${Blue} |--> Compiler Def\t: ${COMPILER_DEFS} ${ColourReset}")
message("${Blue} |--> Project Def\t: ${PROJECT_DEFS} ${ColourReset}") message("${Blue} |--> Project Def\t: ${PROJECT_DEFS} ${ColourReset}")
message("${Blue} |--> Interfaces Dir\t: ${INTERFACES_DIR} ${ColourReset}") message("${Blue} |--> Interfaces Dir\t: ${INTERFACES_DIR} ${ColourReset}")
message("${Blue} |--> Libs used\t\t: ${EXTRA_LIBS} ${ColourReset}") message("${Blue} |--> Libs used\t\t: ${EXTRA_LIBS} ${ColourReset}")
message("${Blue} |--> Drivers used\t\t: ${DRIVERS_DIR} ${ColourReset}")
#################################################################################################### ####################################################################################################
#EXECUTABLE #EXECUTABLE

@ -33,6 +33,8 @@ extern "C" {
#include "hardwareDescription.h" #include "hardwareDescription.h"
#include "pin.h" #include "pin.h"
#define I2C_BASE ((I2C_TypeDef*)i2cBase_Addr_List[i2cHardware->channelNo])
uint32_t i2cCR1; uint32_t i2cCR1;
uint32_t i2cCR2; uint32_t i2cCR2;
uint32_t i2cISR; uint32_t i2cISR;
@ -145,7 +147,18 @@ typedef struct i2c_t
* @brief Initilize the I2C Hardware * @brief Initilize the I2C Hardware
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes * @param i2cHardware is the beforehand declared i2c channel with his opperation modes
*/ */
void i2cInit(i2c_t *i2cHardware); void i2cInit( i2c_t *i2cHardware, /*!< Pointer to I2C hardware Object */
i2cCh_t channelNo, /*!< The harware channel to be used */
i2cMode_t mode, /*!< Master, Slave or Multymaster Modes */
uint16_t address, /*!< First and Main address of the device */
uint16_t addressSecond, /*!< Second address if dual addresse mode is configured */
i2cAddressCount_t addressCount, /*!< Single or multiple */
i2cAddressSize_t addressSize, /*!< 10 or 7 bit address size */
i2cSpeed_t speed, /*!< Bus Speed */
i2cOpperationMode_t opperationMode, /*!< Blocking or non blocking polling, Int or DMA */
i2cClockStreching_t streching, /*!< Clock Streching enable onyl in slave mode */
i2cWakeUpTypes_t wakeOn /*!< Wake up condition */
);
/** /**
* @brief De-Initilize the I2C Hardware * @brief De-Initilize the I2C Hardware
@ -256,7 +269,7 @@ void i2cRead(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress,
* @param registerAddress is the regiter to be red from the device * @param registerAddress is the regiter to be red from the device
* @param data is the data whic has been red * @param data is the data whic has been red
*/ */
uint8_t i2cMasterRecieve(i2c_t *i2cHardware, uint8_t devAddress, uint8_t registerAddress, uint8_t *data); void i2cMasterRecieve(i2c_t *i2cHardware, uint8_t devAddress, uint8_t registerAddress, uint8_t *data);
/** /**
* @brief Sends a Single Byte as master to the given devices register. * @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) * this function will adapt himself to the selected oppeartion mode (Polling / Int / DMA)
@ -293,7 +306,6 @@ void i2cSlaveSend(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAdd
*/ */
void i2cSendStart(i2c_t *i2cHardware); void i2cSendStart(i2c_t *i2cHardware);
/************************************************************************************************** /**************************************************************************************************
I2C Communication functions Polling / Blocking Mode I2C Communication functions Polling / Blocking Mode
***************************************************************************************************/ ***************************************************************************************************/
@ -345,7 +357,7 @@ void i2cBusClear(); // I2C Standart : in case if SCL is stuck
/************************************************************************************************** /**************************************************************************************************
I2C Extra functions that are mot etential for the main functionality I2C Extra functions that are not esential for the main functionality
***************************************************************************************************/ ***************************************************************************************************/
/** /**

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

@ -0,0 +1,30 @@
/**
**************************************************************************************************
* @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"
void i2cReset(i2c_t *i2cHardware)
{
RCC->APB1RSTR |= (1 << i2cBus_Rst_bitPos[i2cHardware->channelNo]);
RCC->APB1RSTR &= ~(1 << i2cBus_Rst_bitPos[i2cHardware->channelNo]);
}

@ -79,3 +79,4 @@ list(APPEND EXTRA_LIBS sub::timer)
list(APPEND EXTRA_LIBS sub::init) list(APPEND EXTRA_LIBS sub::init)
list(APPEND EXTRA_LIBS sub::spi) list(APPEND EXTRA_LIBS sub::spi)
list(APPEND EXTRA_LIBS sub::i2c) list(APPEND EXTRA_LIBS sub::i2c)
list(APPEND EXTRA_LIBS sub::imp_i2c)

@ -1 +1,5 @@
# add_subdirectory(${CSL_USED}) add_library(I2C i2c.c)
target_compile_options(I2C PRIVATE ${C_FLAGS})
target_compile_definitions(I2C PRIVATE ${C_DEFS})
target_include_directories(I2C PUBLIC ${INTERFACES_DIR} ${CSL_INCLUDES})
add_library(sub::i2c ALIAS I2C)

@ -1,30 +1,19 @@
/**
**************************************************************************************************
* @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" #include "i2c.h"
#define I2C_BASE ((I2C_TypeDef*)i2cBase_Addr_List[i2cHardware->channelNo])
void i2cInit(i2c_t *i2cHardware) void i2cInit( i2c_t *i2cHardware, /*!< Pointer to I2C hardware Object */
i2cCh_t channelNo, /*!< The harware channel to be used */
i2cMode_t mode, /*!< Master, Slave or Multymaster Modes */
uint16_t address, /*!< First and Main address of the device */
uint16_t addressSecond, /*!< Second address if dual addresse mode is configured */
i2cAddressCount_t addressCount, /*!< Single or multiple */
i2cAddressSize_t addressSize, /*!< 10 or 7 bit address size */
i2cSpeed_t speed, /*!< Bus Speed */
i2cOpperationMode_t opperationMode, /*!< Blocking or non blocking polling, Int or DMA */
i2cClockStreching_t streching, /*!< Clock Streching enable onyl in slave mode */
i2cWakeUpTypes_t wakeOn /*!< Wake up condition */
)
{ {
i2cReset(i2cHardware); i2cReset(i2cHardware);
// Enables the i2c bus // Enables the i2c bus
@ -34,16 +23,16 @@ void i2cInit(i2c_t *i2cHardware)
I2C_BASE->CR1 &= ~I2C_CR1_PE; I2C_BASE->CR1 &= ~I2C_CR1_PE;
//Configure analog filter. Anlalog filter is on //Configure analog filter. Anlalog filter is on
I2C_BASE->CR1 |= I2C_CR1_ANFOFF; I2C_BASE->CR1 &= ~I2C_CR1_ANFOFF;
//Configure NoStrech Streching mode is disabled (slave only) //Configure NoStrech Streching mode is disabled (slave only)
//I2C_BASE->CR1 |= I2C_CR1_NOSTRETCH; I2C_BASE->CR1 |= I2C_CR1_NOSTRETCH;
//Configure the clock //Configure the clock
I2C_BASE->TIMINGR = i2cHardware->timing; I2C_BASE->TIMINGR = i2cHardware->timing;
//Automatic end mode (master mode) Enablede as default //Automatic end mode (master mode) disabled Enablede as default
//I2C_BASE->CR2 &= ~I2C_CR2_AUTOEND; I2C_BASE->CR2 &= ~I2C_CR2_AUTOEND;
//I2C_BASE->CR2 |= I2C_CR2_AUTOEND; //I2C_BASE->CR2 |= I2C_CR2_AUTOEND;
if(i2cHardware->mode == i2cModeMaster) if(i2cHardware->mode == i2cModeMaster)
@ -63,17 +52,16 @@ void i2cInit(i2c_t *i2cHardware)
//activating the Perriferal. //activating the Perriferal.
I2C_BASE->CR1 |= I2C_CR1_PE; I2C_BASE->CR1 |= I2C_CR1_PE;
i2cCR1= I2C_BASE->CR1;
i2cCR2= I2C_BASE->CR2;
i2cISR= I2C_BASE->ISR;
i2cHardware->state = i2cInitialized; i2cHardware->state = i2cInitialized;
} }
void i2cReset(i2c_t *i2cHardware) void i2cDeInit(i2c_t *i2cHardware)
{
}
void i2cWrite(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data, uint8_t dataLenght)
{ {
RCC->APB1RSTR |= (1 << i2cBus_Rst_bitPos[i2cHardware->channelNo]);
RCC->APB1RSTR &= ~(1 << i2cBus_Rst_bitPos[i2cHardware->channelNo]);
} }
void i2cRead( i2c_t *i2cHardware, void i2cRead( i2c_t *i2cHardware,
@ -100,12 +88,13 @@ void i2cRead( i2c_t *i2cHardware,
} }
// this function still doesn't implment 10 bit oopeartion TODO // this function still doesn't implment 10 bit oopeartion TODO
uint8_t i2cMasterRecieve(i2c_t *i2cHardware, uint8_t devAddress, uint8_t registerAddress, uint8_t *data) void i2cMasterRecieve(i2c_t *i2cHardware, uint8_t devAddress, uint8_t registerAddress, uint8_t *data)
{ {
// Wait until no communication is ongoign // Wait until no communication is ongoign
while((I2C_BASE->ISR & (I2C_ISR_BUSY))==I2C_ISR_BUSY); while((I2C_BASE->ISR & (I2C_ISR_BUSY))==I2C_ISR_BUSY);
i2cHardware->state = i2cRecieving; i2cHardware->state = i2cTransmitting;
//Slave address //Slave address
I2C_BASE->CR2 |= devAddress << 1; // The bit no 0 is not taken in concideration in 7bit mode I2C_BASE->CR2 |= devAddress << 1; // The bit no 0 is not taken in concideration in 7bit mode
@ -131,11 +120,11 @@ uint8_t i2cMasterRecieve(i2c_t *i2cHardware, uint8_t devAddress, uint8_t registe
//Wait untill all is Transfered //Wait untill all is Transfered
while(!(I2C_BASE->ISR & (I2C_ISR_TXE))); while(!(I2C_BASE->ISR & (I2C_ISR_TXE)));
// working well up to this point
//Is the tranfes complete ? //Is the tranfes complete ?
while(!(I2C_BASE->ISR & (I2C_ISR_TC))); while(!(I2C_BASE->ISR & (I2C_ISR_TC)));
i2cHardware->state = i2cRecieving;
//Read Mode //Read Mode
I2C_BASE->CR2 |= I2C_CR2_RD_WRN; I2C_BASE->CR2 |= I2C_CR2_RD_WRN;
@ -160,6 +149,23 @@ uint8_t i2cMasterRecieve(i2c_t *i2cHardware, uint8_t devAddress, uint8_t registe
I2C_BASE->CR2 |= I2C_CR2_STOP; I2C_BASE->CR2 |= I2C_CR2_STOP;
//read dtaa from the input register to clear it out //read dtaa from the input register to clear it out
data = I2C_BASE->RXDR; *data = I2C_BASE->RXDR;
return data;
i2cHardware->state = i2cReady;
}
void i2cMasterSend(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data)
{
}
void i2cSlaveRecieve(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data)
{
}
void i2cSlaveSend(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress, uint8_t *data)
{
} }

@ -1,7 +0,0 @@
add_library(ledDriver led.cpp)
target_compile_options(ledDriver PRIVATE ${C_FLAGS})
target_compile_definitions(ledDriver PRIVATE ${C_DEFS})
target_include_directories(ledDriver PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
add_library(sub::led ALIAS ledDriver)

@ -1,30 +0,0 @@
#include "led.hpp"
Led::Led(Pin* pin_ptr)
{
pin = pin_ptr;
pin->init();
pin->setMode(Pin::mode::output);
pin->setSpeed(Pin::speed::fast);
}
Led::~Led()
{
}
void Led::on()
{
pin->write(true);
}
void Led::off()
{
pin->write(false);
}
void Led::toggle()
{
pin->toggle();
}

@ -1,20 +0,0 @@
#ifndef _LED_HPP
#define _LED_HPP
class Led
{
public:
Led(Pin *pin_ptr);
~Led();
void on();
void off();
void toggle();
private:
Pin *pin;
};
#endif /* __LED_HPP */

@ -1,82 +0,0 @@
#include "spi_ch.h"
// generic implementation of spi channel class
uint8_t spiCH_readReg(spi_ch_t *spi_ch,
uint8_t reg_address) {
uitn8_t buf;
// select target device
pinWrite(spi_ch->pin,0);
// send address of target register
spi_trx(spi_ch->spi, reg_address);
// read from target register
buf = spi_trx(spi->spi,0x00);
// release target device
pinWrite(spi_ch->pin,1);
return buf;
}
void spiCH_autoReadBlock(spi_ch_t *spi_ch,
uint8_t start_address,
uint8_t* buffer,
uint8_t buf_len) {
uint8_t i = 0;
// select target device
pinWrite(spi_ch->pin,0);
// send address of starting register
spi_trx(spi_ch->spi, reg_address);
// read block from device
for(;i < buf_len;i++) {
buffer[i] = spi_trx(spi_ch->spi, 0x00);
}
// release target device
pinWrite(spi_ch->pin,1);
}
void spiCH_writeReg(spi_ch_t *spi_ch,
uint8_t reg_address,
uint8_t data) {
// select target device
pinWrite(spi_ch->pin,0);
// send address of target register
spi_trx(spi_ch->spi, reg_address);
// write to target register
spi_trx(spi->spi, data);
// release target device
pinWrite(spi_ch->pin,1);
}
void spiCH_writeBlock(spi_ch_t *spi_ch,
uint8_t start_address,
const uint8_t *data,
uint8_t data_len) {
uint8_t i = 0;
// select target device
pinWrite(spi_ch->pin,0);
// send address of starting register
spi_trx(spi_ch->spi, reg_address);
// read block from device
for(;i < buf_len;i++) {
spi_trx(spi_ch->spi, data[i]);
}
// release target device
pinWrite(spi_ch->pin,1);
}

@ -46,9 +46,9 @@ void printBinary32(uint32_t toPrint, uint8_t lsbFirst)
print_Usart(usart2, "\n\r"); print_Usart(usart2, "\n\r");
if(lsbFirst) if(lsbFirst)
{ {
print_Usart(usart2, "| 0| 1| 2| 3| 4| 5| 6| 7| 8| 9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|"); print_Usart(usart2, "Bit Pos | 0| 1| 2| 3| 4| 5| 6| 7| 8| 9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31|");
print_Usart(usart2, "\n\r"); print_Usart(usart2, "\n\r");
usartSendChar(usart2, '|'); print_Usart(usart2, "Bits |");
for(i=0; i < 32; i++) for(i=0; i < 32; i++)
{ {
pt = (toPrint >> i) & 1; pt = (toPrint >> i) & 1;
@ -59,9 +59,9 @@ void printBinary32(uint32_t toPrint, uint8_t lsbFirst)
} }
else else
{ {
print_Usart(usart2, "|31|30|29|28|27|26|25|24|23|22|21|20|19|18|17|16|15|14|13|12|11|10| 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|"); print_Usart(usart2, "Bit Pos |31|30|29|28|27|26|25|24|23|22|21|20|19|18|17|16|15|14|13|12|11|10| 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|");
print_Usart(usart2, "\n\r"); print_Usart(usart2, "\n\r");
usartSendChar(usart2, '|'); print_Usart(usart2, "Bits |");
for(i=0; i < 32; i++) for(i=0; i < 32; i++)
{ {
pt = (toPrint >> (31-i)) & 1; pt = (toPrint >> (31-i)) & 1;
@ -80,9 +80,9 @@ void printBinary8(uint8_t toPrint, uint8_t lsbFirst)
print_Usart(usart2, "\n\r"); print_Usart(usart2, "\n\r");
if(lsbFirst) if(lsbFirst)
{ {
print_Usart(usart2, "| 0| 1| 2| 3| 4| 5| 6| 7|"); print_Usart(usart2, "Bit Pos | 0| 1| 2| 3| 4| 5| 6| 7|");
print_Usart(usart2, "\n\r"); print_Usart(usart2, "\n\r");
usartSendChar(usart2, '|'); print_Usart(usart2, "Bits |");
for(i=0; i < 8; i++) for(i=0; i < 8; i++)
{ {
pt = (toPrint >> i) & 1; pt = (toPrint >> i) & 1;
@ -93,9 +93,9 @@ void printBinary8(uint8_t toPrint, uint8_t lsbFirst)
} }
else else
{ {
print_Usart(usart2, "| 7| 6| 5| 4| 3| 2| 1| 0|"); print_Usart(usart2, "Bit Pos | 7| 6| 5| 4| 3| 2| 1| 0|");
print_Usart(usart2, "\n\r"); print_Usart(usart2, "\n\r");
usartSendChar(usart2, '|'); print_Usart(usart2, "Bits |");
for(i=0; i < 8; i++) for(i=0; i < 8; i++)
{ {
pt = (toPrint >> (7-i)) & 1; pt = (toPrint >> (7-i)) & 1;
@ -113,8 +113,6 @@ int main(int argc, char *argv[])
uint8_t slaveAddress = 0xC0; uint8_t slaveAddress = 0xC0;
uint8_t registerToRead = 0x00; uint8_t registerToRead = 0x00;
uint8_t i2cRecieved = 0; uint8_t i2cRecieved = 0;
uint8_t buffer [8];
uint8_t data = 0;
// making array with all available timers // making array with all available timers
//timerNo_t timers[MAX_TIMER_CHANNEL_COUNT] = {timer_1, timer_2, timer_3, timer_14, timer_16, timer_17}; //timerNo_t timers[MAX_TIMER_CHANNEL_COUNT] = {timer_1, timer_2, timer_3, timer_14, timer_16, timer_17};
@ -156,6 +154,7 @@ int main(int argc, char *argv[])
pinSetAlternate(pinA10, 4); pinSetAlternate(pinA10, 4);
i2c_t i2c_1; i2c_t i2c_1;
/*
i2c_1.channelNo = I2C_CH_1; i2c_1.channelNo = I2C_CH_1;
i2c_1.mode = i2cModeMaster; i2c_1.mode = i2cModeMaster;
i2c_1.address = 0x0; i2c_1.address = 0x0;
@ -168,15 +167,16 @@ int main(int argc, char *argv[])
i2c_1.timing = 0x2000090E; // Taken from stm code generator i2c_1.timing = 0x2000090E; // Taken from stm code generator
i2c_1.wakeOn = i2cWakeUpDisabled; i2c_1.wakeOn = i2cWakeUpDisabled;
i2c_1.state = i2cNotInitialized; i2c_1.state = i2cNotInitialized;
*/
i2cInit(&i2c_1); i2cInit(&i2c_1, I2C_CH_1, i2cModeMaster, 0x00,0x00, i2cAddressCountSingle, i2cAddressSizeSevenBits, i2cSpeedStandart, i2cOpperationPolling, i2cClockStrechingDisable, i2cWakeUpDisabled);
data = i2cMasterRecieve(&i2c_1,slaveAddress,registerToRead,&i2cRecieved); i2cMasterRecieve(&i2c_1,slaveAddress,registerToRead,&i2cRecieved);
printBinary8(data,0);
printBinary8(i2cRecieved,0);
print_Usart(usart2, "\n\r");
print_Usart(usart2, "\n\r");
print_Usart(usart2, "All is working fine\n\r"); print_Usart(usart2, "All is working fine\n\r");
while(1) while(1)

Loading…
Cancel
Save