// Alternate function config.
GPIOB->CRL = 0xFF000000;
// Peripheral clk enable
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN;
// I2c peripheral enable
RCC->APB1ENR |= RCC_APB1ENR_I2C1EN;
I2C1->CR1 = 0x0000; // Peripheral disable
// I2c clk config.
I2C1->CR2 = 0x0008; // Freq 0b001000 8MHz
I2C1->CCR = 0x28; // clk config.
I2C1->TRISE = 0x09; // max. rise time
I2C1->CR1 |= I2C_CR1_PE; // Peripheral enable
// starting commmunication
I2C1->CR1 |= (1<<8); // start bit
while (!(I2C1->SR1 & 0x0001));
a=1;
// sending slave address with write command
I2C1->DR = 0xA6; // Device address with read bit
while(!(I2C1->SR1 & 0x0002) );
read_status = (I2C1->SR2);
// Sending register address
I2C1->DR = 0x2C; // Device internal register address
while (!(I2C1->SR1 & (1<<7))); // wait for TXE bit set
// restarting communication before reading data
I2C1->CR1 |= (1<<8); // Restart
while(!(I2C1->SR1 & 0x0001));
// Sending slave address with read command
I2C1->DR = 0xA7;
while(I2C1->SR1 & 0x0002);
read_status = (I2C1->SR2);
I2C1->CR1 &= ~0x0400;
// wait until data recieve and read from DR register
while (!(I2C1->SR1 & 0x0040));
data = I2C1->DR;
// stop communication
I2C1->CR1 |= (1<<9);
while(!(I2C1->SR2 & 0x0002));