2023-05-05 07:28 AM
Hello,
I am trying to write manual I2C driver for STM32H743BI controller. Here system clock is configured for 200MHz and APB1 clock for 50MHz. Below is my initialization code. While testing stop flag and TXIS flag is not set after setting CR2 register. Is there anything I am missing here?
Slave address is 0xA0 (EEPROM).
RCC->APB1LENR |= RCC_APB1LENR_I2C1EN;
RCC->AHB4ENR |= RCC_AHB4ENR_GPIOBEN;
GPIOB->MODER &= 0xFFFFAFFF;
GPIOB->OSPEEDR &= ~0x0000F000;
GPIOB->OTYPER |= 0x000000C0;
GPIOB->PUPDR |= 0x00005000;
GPIOB->AFR[0] |= 0x44000000;
GPIOB->AFR[1] = 0x00000000;
I2C1->CR1 = 0x00000000;
I2C1->TIMINGR = 0x00901954;
I2C1->CR1 = 0x00000001;
I2C1->CR2 = 0x02000000;
I2C1->OAR1 = 0x00008000;
I2C1->OAR2 = 0x00000000;
// Check Slave Ready
I2C1->CR2 = 0x020000A0; // Address 0xA0
while((!(I2C1->ISR & I2C_ISR_STOPF)) && (!(I2C1->ISR & I2C_ISR_NACKF)));
if((I2C1->ISR & I2C_ISR_NACKF) == 0)
{
I2C1->ICR |= I2C_ICR_STOPCF;
}
// Transfer Addr
I2C1->CR2 = 0x010200A0;
// Wait unitil TXI flag is set.
while(!(I2C1->ISR & I2C_ISR_TXIS));
I2C1->TXDR = 0xB0;
while(!(I2C1->ISR & I2C_ISR_TXIS));
I2C1->TXDR = 0x00;
while(!(I2C1->ISR & I2C_ISR_TCR));
// Transfer Data
I2C1->CR2 = 0x020200A0;
while(!(I2C1->ISR & I2C_ISR_TXIS));
I2C1->TXDR = 0x55;
while(!(I2C1->ISR & I2C_ISR_TXIS));
I2C1->TXDR = 0xAA;
while(!(I2C1->ISR & I2C_ISR_TXIS));
while(!(I2C1->ISR & I2C_ISR_TCR));
while(!(I2C1->ISR & I2C_ISR_STOPF));
I2C1->ICR |= I2C_ICR_STOPCF;
// to Read
// Transfer Addr
I2C1->CR2 = 0x000200A0;
// Wait unitil TXI flag is set.
while(!(I2C1->ISR & I2C_ISR_TXIS));
I2C1->TXDR = 0xB0;
while(!(I2C1->ISR & I2C_ISR_TXIS));
I2C1->TXDR = 0x00;
while(!(I2C1->ISR & I2C_ISR_TC));
I2C1->CR2 = 0x020204A0;
while(!(I2C1->ISR & I2C_ISR_RXNE));
data[0] = I2C1->RXDR;
while(!(I2C1->ISR & I2C_ISR_RXNE));
data[1] = I2C1->RXDR;
while(!(I2C1->ISR & I2C_ISR_TCR));
while(!(I2C1->ISR & I2C_ISR_STOPF));
I2C1->ICR |= I2C_ICR_STOPCF;
//ClearI2C Reg
I2C1->CR2 = 0x02000000;
while(1);
2023-05-09 08:56 AM
Hi Anand A
Your question has been routed to the online support team. A case has been created and you'll be contacted shortly.
Kind Regards
Joe WILLIAMS
STMicro Support
2023-05-10 01:40 PM
Why are you using the register bit field definitions only for status registers, but not for configuration registers? And does this code even correspond to the I2C state machine described in the reference manual?