The problem with the i2c interface on the controller stm32f103
I have a problem with the operation of the I2C interface on stm32f103. Hangs waiting for ADDR bit(while(!(I2C1->SR1 & I2C_SR1_ADDR))) in the master transmit mode.
In the Internet, many people have this problem, but is no solution. Who was the problem and who solved it?
RCC->APB2ENR |= RCC_APB2ENR_AFIOEN; //enable AF
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN; /enable clock port b
AFIO->MAPR &= ~AFIO_MAPR_I2C1_REMAP;
GPIOB->CRL |= GPIO_CRL_CNF6; //Alternate function open drain
GPIOB->CRL |= GPIO_CRL_CNF7; //Alternate function open drain
GPIOB->CRL |= GPIO_CRL_MODE6; // Output mode, max speed 50 MHz.
GPIOB->CRL |= GPIO_CRL_MODE7; // Output mode, max speed 50 MHz.
/*********** Init I2C *******************************/
RCC->APB1ENR |= RCC_APB1ENR_I2C1EN; //Enable clock I2C1
I2C1->CR1 |= I2C_CR1_SWRST; //reset i2c
I2C1->CR1 &= ~I2C_CR1_SWRST;
I2C1->CR1 &= ~I2C_CR1_PE;
I2C1->CR2 |= 36; //set FREQ = APB1= 36MHz
I2C1->CCR |= 180;
I2C1->TRISE = 0x00;
I2C1->TRISE |= 37; //TRISE = 36MHz APB1 bus + 1
I2C1->CR1 |= I2C_CR1_PE; // I2C_CR1 register to enable the peripheral
/*********************send data***************************/
//Start
I2C1->CR1 |= I2C_CR1_START;
while(!(I2C1->SR1 & I2C_SR1_SB)){}; ///Here is normal
(void) I2C1->SR1;
//send addres unit
I2C1->DR = adr_unit;
while(!(I2C1->SR1 & I2C_SR1_ADDR)){}; //// Here is loop!
(void) I2C1->SR1;
(void) I2C1->SR2;
//send addres reg
I2C1->DR = reg_addr;
while(!(I2C1->SR1 & I2C_SR1_TXE)){};
//write data
I2C1->DR = data;
while(!(I2C1->SR1 & I2C_SR1_BTF)){};
I2C1->CR1 |= I2C_CR1_STOP;