2020-11-17 01:03 AM
Hello.
I'm trying to get I2C working to read BMP280 sensor data. This is code I written so far:
GPIOB->CRL |= GPIO_CRL_MODE6; //set PB6 as output with drive strenght to 50Mhz
GPIOB->CRL |= GPIO_CRL_CNF6; //set PB6 to open-drain alternate function
GPIOB->CRL |= GPIO_CRL_MODE7; //set PB7 as output with drive strenght to 50Mhz
GPIOB->CRL |= GPIO_CRL_CNF7; //set PB7 to open-drain alternate function
I2C1->CR2 |= 0x20; //freq of cpu
I2C1->CCR |= 0x80;
I2C1->CR1 |= (I2C_CR1_PE); //Peripheral enabled
while (1){
I2C1->CR1 |= I2C_CR1_START;
while(!(I2C1->SR1 & I2C_SR1_SB));
I2C1->DR |= 0x76<<1;
while(!(I2C1->SR1 & I2C_SR1_ADDR));
uint32_t asd = I2C1->SR2;
I2C1->DR |= 0xD0;
I2C1->CR1 |= I2C_CR1_STOP;
//delay 1s
}
Code works nicely only once. It reads ACK from sensor (when sensor unplugged - no ack), but when it comes to send all of this one more time - it fails. Nothing gets sent and it stays in this loop: while(!(I2C1->SR1 & I2C_SR1_ADDR)); What I'm initializing not correctly? Is this a proper way to do I2C transfers on STM?