|
|
|
@ -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)
|
|
|
|
|