|
|
@ -34,26 +34,29 @@ 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) Enablede as default
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(i2cHardware->addressSize == i2cAddressSizeTenBits)
|
|
|
|
if(i2cHardware->addressSize == i2cAddressSizeTenBits)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
I2C_BASE->CR2 |= I2C_CR2_ADD10; // 10 Bit addressing
|
|
|
|
I2C_BASE->CR2 |= I2C_CR2_ADD10; // 10 Bit addressing
|
|
|
|
|
|
|
|
I2C_BASE->CR2 &= ~I2C_CR2_HEAD10R; // 7 Bit header read turned on DEFAULT
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
I2C_BASE->CR2 &= ~I2C_CR2_ADD10; // 7 Bit addressing DEFAULT
|
|
|
|
I2C_BASE->CR2 &= ~I2C_CR2_ADD10; // 7 Bit addressing DEFAULT
|
|
|
|
|
|
|
|
I2C_BASE->CR2 |= I2C_CR2_HEAD10R; // 7 Bit header read turned off DEFAULT
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -97,36 +100,40 @@ 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
|
|
|
|
void 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));
|
|
|
|
while((I2C_BASE->ISR & (I2C_ISR_BUSY))==I2C_ISR_BUSY);
|
|
|
|
|
|
|
|
|
|
|
|
i2cHardware->state = i2cRecieving;
|
|
|
|
i2cHardware->state = i2cRecieving;
|
|
|
|
|
|
|
|
|
|
|
|
//Slave address
|
|
|
|
//Slave address
|
|
|
|
I2C_BASE->CR2 |= devAddress << 1; // The bit no 0 is not taken in concideration in 7bit mode
|
|
|
|
I2C_BASE->CR2 |= 0x40 << 1; // The bit no 0 is not taken in concideration in 7bit mode
|
|
|
|
|
|
|
|
|
|
|
|
//Read Mode
|
|
|
|
//Read Mode
|
|
|
|
I2C_BASE->CR2 |= I2C_CR2_RD_WRN;
|
|
|
|
I2C_BASE->CR2 &= ~I2C_CR2_RD_WRN;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Set Buffer size
|
|
|
|
|
|
|
|
I2C_BASE->CR2 |= 1 << I2C_CR2_NBYTES_Pos;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Check if the TX buffer is empty
|
|
|
|
|
|
|
|
while(!(I2C_BASE->ISR & (I2C_ISR_TXE)));
|
|
|
|
|
|
|
|
//Register to be sent
|
|
|
|
|
|
|
|
I2C_BASE->TXDR |= 0xCE;
|
|
|
|
|
|
|
|
|
|
|
|
data = I2C_BASE->TXDR;
|
|
|
|
|
|
|
|
//Generate start condition
|
|
|
|
//Generate start condition
|
|
|
|
I2C_BASE->CR2 |= I2C_CR2_START;
|
|
|
|
I2C_BASE->CR2 |= I2C_CR2_START;
|
|
|
|
|
|
|
|
|
|
|
|
//Wait until the start condition in generated.
|
|
|
|
//Wait until the start condition in generated.
|
|
|
|
while(!(I2C_BASE->ISR & (I2C_ISR_BUSY)));
|
|
|
|
while(!(I2C_BASE->ISR & (I2C_ISR_BUSY)));
|
|
|
|
|
|
|
|
|
|
|
|
//Check if the TX buffer is empty
|
|
|
|
|
|
|
|
while(!(I2C_BASE->ISR & (I2C_ISR_TXE)));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//write the register that we wahtn to read
|
|
|
|
//while(!(I2C_BASE->ISR & (I2C_ISR_RXNE)));
|
|
|
|
I2C_BASE->TXDR = registerAddress;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
i2cCR1= I2C_BASE->CR1;
|
|
|
|
i2cCR1= I2C_BASE->CR1;
|
|
|
|
i2cCR2= I2C_BASE->CR2;
|
|
|
|
i2cCR2= I2C_BASE->CR2;
|
|
|
|
i2cISR= I2C_BASE->ISR;
|
|
|
|
i2cISR= I2C_BASE->ISR;
|
|
|
|
//while(!(I2C_BASE->ISR & (I2C_ISR_RXNE)));
|
|
|
|
|
|
|
|
//data = I2C_BASE->RXDR;
|
|
|
|
data = I2C_BASE->RXDR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|