Started wit the i2cWrite and started to implment more strutured I2C base level communication functions to ease the work of future implementations

i2c
kerem 3 years ago
parent 23c8899f0f
commit 3e457d3340

@ -388,12 +388,19 @@ void i2cGenerateAck(i2c_t *i2cHardware);
*/
void i2cSendSlaveAddress(i2c_t *i2cHardware, uint16_t *slaveAddress);
/**
* @brief Sende the register adrres with which we want to communicate.
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
* @param registerAddress The register address of the slave device that we are communication with
*/
void i2cSendRegisterAddress(i2c_t *i2cHardware, uint16_t *registerAddress);
/**
* @brief Send the register that we want to read or write.
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
* @param registerAddress the register that need to be accessed
*/
void i2cSendRegisterAddress(i2c_t *i2cHardware, uint8_t *registerAddress);
void i2cSendData(i2c_t *i2cHardware, uint8_t *registerAddress);
/**
* @brief Initiates a Write command with the previously set slave address.
@ -444,6 +451,13 @@ void i2cSetOutputRegister(i2c_t *i2cHardware, uint8_t *data);
* @param data pointer to the data that need to be read and returned
*/
void i2cIsTransferComplete(i2c_t *i2cHardware);
/**
* @brief Defines the amount of transfers to be made. Address exchange and start conditon does not count
* @param i2cHardware is the beforehand declared i2c channel with his opperation modes
* @param count amount o data to be transfered.
*/
void i2cSetTransferCounter(i2c_t *i2cHardware, uint8_t count);
/**************************************************************************************************
I2C Communication functions Polling / Blocking Mode
***************************************************************************************************/

@ -148,6 +148,24 @@ void i2cConfigureFilters(i2c_t *i2cHardware)
I2C Hardware functions
***************************************************************************************************/
void i2cGenerateStart(i2c_t *i2cHardware)
{
I2C_BASE->CR2 &=~ I2C_CR2_STOP;
I2C_BASE->CR2 |= I2C_CR2_START;
//Wait until the start condition in generated.
while(!(I2C_BASE->ISR & (I2C_ISR_BUSY)));
i2cHardware->hardwareState = i2cHwStartGenerated;
// This device places the salve address automaticalyy in the output buffer before sending the star condition
i2cHardware->hardwareState = i2cHwOutputBufferFull;
}
void i2cGenerateStop(i2c_t *i2cHardware)
{
// Send stop command
I2C_BASE->CR2 |= I2C_CR2_STOP;
I2C_BASE->CR2 &=~ I2C_CR2_START;
i2cHardware->hardwareState = i2cHwStopGenerated;
}
void i2cIsPeriferalReady(i2c_t *i2cHardware)
{
@ -157,47 +175,29 @@ void i2cIsPeriferalReady(i2c_t *i2cHardware)
}
}
void i2cSetTransferCounter(i2c_t *i2cHardware, uint8_t count)
{
I2C_BASE->CR2 &= ~(0xFF << I2C_CR2_NBYTES_Pos);
I2C_BASE->CR2 |= (count << I2C_CR2_NBYTES_Pos);
}
void i2cSendSlaveAddress(i2c_t *i2cHardware, uint16_t *slaveAddress)
{
if(i2cHardware->addressSize == i2cAddressSizeSevenBits)
{
// The Slave addrress is automatically place on the output buffer (must be done before the start condition)
I2C_BASE->CR2 |= (*slaveAddress & 0xff) << 1; // The bit no 0 is not taken in concideration in 7bit mode
//Set Buffer size / which is alredy full with the device address to be sent
I2C_BASE->CR2 |= 1 << I2C_CR2_NBYTES_Pos;
}
else if(i2cHardware->addressSize == i2cAddressSizeTenBits)
{
//to implement
}
// The Slave addrress is automatically place on the output buffer (must be done before the start condition)
I2C_BASE->CR2 |= (*slaveAddress & 0xff) << 1; // The bit no 0 is not taken in concideration in 7bit mode
i2cGenerateStart(i2cHardware);
// Wait until the data in the ouput buffer is put to the i2c BUS
while(i2cHardware->hardwareState == i2cHwOutputBufferFull)
while(i2cHardware->hardwareState =! i2cHwOutputBufferEmpty)
{
i2cIsOutputBufferEmpty(i2cHardware);
}
}
void i2cGenerateStart(i2c_t *i2cHardware)
void i2cSendRegisterAddress(i2c_t *i2cHardware, uint16_t *registerAddress)
{
I2C_BASE->CR2 |= I2C_CR2_START;
//Wait until the start condition in generated.
while(!(I2C_BASE->ISR & (I2C_ISR_BUSY)));
i2cHardware->hardwareState = i2cHwStartGenerated;
// This device places the salve address automaticalyy in the buffer before sending the star condition
i2cHardware->hardwareState = i2cHwOutputBufferFull;
}
void i2cGenerateStop(i2c_t *i2cHardware)
{
// Sned stop command
I2C_BASE->CR2 |= I2C_CR2_STOP;
i2cHardware->hardwareState = i2cHwStopGenerated;
i2cSendData(i2cHardware,registerAddress);
}
void i2cInitiateWriteCommand(i2c_t *i2cHardware)
@ -210,15 +210,42 @@ void i2cInitiateReadCommand(i2c_t *i2cHardware)
I2C_BASE->CR2 |= I2C_CR2_RD_WRN;
}
void i2cSendData(i2c_t *i2cHardware, uint8_t *registerAddress)
{
while(i2cHardware->hardwareState =! i2cHwOutputBufferEmpty)
{
i2cIsOutputBufferEmpty(i2cHardware);
}
i2cSetOutputRegister(i2cHardware,registerAddress);
while(i2cHardware->hardwareState =! i2cHwOutputBufferEmpty)
{
i2cIsOutputBufferEmpty(i2cHardware);
}
}
void i2cIsOutputBufferEmpty(i2c_t *i2cHardware)
{
if((I2C_BASE->ISR & (I2C_ISR_TXE)) == I2C_ISR_TXE)
if(i2cHardware->periferalState == i2cPerifTransmitting)
{
i2cHardware->hardwareState = i2cHwOutputBufferEmpty;
if((I2C_BASE->ISR & (I2C_ISR_TXIS)) == I2C_ISR_TXIS)
{
i2cHardware->hardwareState = i2cHwOutputBufferEmpty;
}
else
{
i2cHardware->hardwareState = i2cHwOutputBufferFull;
}
}
else
else if(i2cHardware->periferalState == i2cPerifRecieving)
{
i2cHardware->hardwareState = i2cHwOutputBufferFull;
if((I2C_BASE->ISR & (I2C_ISR_TXE)) == I2C_ISR_TXE)
{
i2cHardware->hardwareState = i2cHwOutputBufferEmpty;
}
else
{
i2cHardware->hardwareState = i2cHwOutputBufferFull;
}
}
}
@ -234,17 +261,6 @@ void i2cIsInputBufferEmpty(i2c_t *i2cHardware)
}
}
void i2cSendRegisterAddress(i2c_t *i2cHardware, uint8_t *registerAddress)
{
i2cSetOutputRegister(i2cHardware,registerAddress);
i2cHardware->hardwareState = i2cHwOutputBufferFull;
while(i2cHardware->hardwareState == i2cHwOutputBufferFull)
{
i2cIsOutputBufferEmpty(i2cHardware);
}
}
void i2cReadInputRegister(i2c_t *i2cHardware, uint8_t *data)
{
@ -253,7 +269,8 @@ void i2cReadInputRegister(i2c_t *i2cHardware, uint8_t *data)
void i2cSetOutputRegister(i2c_t *i2cHardware, uint8_t *data)
{
I2C_BASE->TXDR |= *data;
I2C_BASE->TXDR = *data;
i2cHardware->hardwareState = i2cHwOutputBufferFull;
}
void i2cIsTransferComplete(i2c_t *i2cHardware)

@ -1,5 +1,6 @@
#include "i2c.h"
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 */
@ -82,7 +83,36 @@ void i2cMasterRecieve(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *regis
}
}
void i2cMasterRecievePolling(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data)
void i2cWrite(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data, uint8_t *dataLenght)
{
switch(i2cHardware->mode)
{
case i2cModeMaster:
i2cMasterSend(i2cHardware, slaveAddress, registerAddress, data);
break;
case i2cModeSlave:
break;
case i2cModeMultyMaster:
break;
}
}
void i2cMasterSend(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data)
{
switch(i2cHardware->opperationMode)
{
case i2cOpperationPolling :
i2cMasterSendPolling(i2cHardware, slaveAddress, registerAddress, data);
break;
case i2cOpperationInt:
break;
case i2cOpperationDma:
break;
}
}
void i2cMasterSendPolling(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data)
{
while(i2cHardware->periferalState != i2cPerifReady)
{
@ -92,7 +122,39 @@ void i2cMasterRecievePolling(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t
i2cHardware->periferalState = i2cPerifTransmitting;
i2cInitiateWriteCommand(i2cHardware);
i2cSetTransferCounter(i2cHardware,2);
i2cSendSlaveAddress(i2cHardware, slaveAddress);
i2cSendRegisterAddress(i2cHardware,registerAddress);
i2cSendData(i2cHardware,data);
while(i2cHardware->periferalState != i2cPerifTransferComplete)
{
i2cIsTransferComplete(i2cHardware);
}
i2cCR2 = I2C_BASE->CR2;
i2cGenerateStop(i2cHardware);
i2cHardware->periferalState = i2cPerifReady;
i2cHardware->hardwareState = i2cHwIdle;
}
void i2cMasterRecievePolling(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t *registerAddress, uint8_t *data)
{
while(i2cHardware->periferalState != i2cPerifReady)
{
i2cIsPeriferalReady(i2cHardware);
}
i2cHardware->periferalState = i2cPerifTransmitting;
i2cSetTransferCounter(i2cHardware,1);
i2cInitiateWriteCommand(i2cHardware);
i2cSendSlaveAddress(i2cHardware, slaveAddress);
i2cSendRegisterAddress(i2cHardware,registerAddress);
@ -102,12 +164,12 @@ void i2cMasterRecievePolling(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t
i2cIsTransferComplete(i2cHardware);
}
i2cHardware->periferalState = i2cPerifRecieving;
i2cInitiateReadCommand(i2cHardware);
i2cSendSlaveAddress(i2cHardware, slaveAddress);
i2cHardware->periferalState = i2cPerifRecieving;
while(i2cHardware->hardwareState != i2cHwInputBufferFull)
{
i2cIsInputBufferEmpty(i2cHardware);
@ -118,7 +180,7 @@ void i2cMasterRecievePolling(i2c_t *i2cHardware, uint16_t *slaveAddress, uint8_t
i2cGenerateStop(i2cHardware);
i2cReadInputRegister(i2cHardware, data);
i2cHardware->periferalState = i2cPerifReady;
i2cHardware->hardwareState = i2cHwIdle;
}

@ -113,6 +113,8 @@ int main(int argc, char *argv[])
uint16_t slaveAddress = 0xC0;
uint8_t registerToRead = 0x00;
uint8_t i2cRecieved = 0;
uint8_t i2cToWrite = 0xFF;
uint8_t i2cDataLenght = 1;
i2c_t i2c_1;
// making array with all available timers
@ -151,16 +153,28 @@ int main(int argc, char *argv[])
print_Usart(usart2, "\n\r");
i2cInit(&i2c_1, I2C_CH_1, i2cModeMaster, 0x00,0x00, i2cAddressCountSingle, i2cAddressSizeSevenBits, i2cSpeedStandart, i2cOpperationPolling, i2cClockStretchingDisable, i2cWakeUpDisabled);
i2cRead(&i2c_1, &slaveAddress, &registerToRead, &i2cRecieved,1);
/*
i2cRead(&i2c_1, &slaveAddress, &registerToRead, &i2cRecieved, &i2cDataLenght);
printBinary8(i2cRecieved,0);
print_Usart(usart2, "\n\r");
registerToRead += 1;
i2cRead(&i2c_1, &slaveAddress, &registerToRead, &i2cRecieved,1);
i2cRead(&i2c_1, &slaveAddress, &registerToRead, &i2cRecieved, &i2cDataLengh);
printBinary8(i2cRecieved,0);
*/
registerToRead = 1;
i2cToWrite = 0xFF;
print_Usart(usart2, "\n\r");
//i2cWrite(&i2c_1, &slaveAddress, &registerToRead, &i2cToWrite, &i2cDataLenght);
printBinary32(i2cCR2,0);
print_Usart(usart2, "\n\r");
i2cRead(&i2c_1, &slaveAddress, &registerToRead, &i2cRecieved,&i2cDataLenght);
printBinary8(i2cRecieved,0);
print_Usart(usart2, "\n\r");
print_Usart(usart2, "\n\r");
print_Usart(usart2, "All is working fine\n\r");

Loading…
Cancel
Save