/* Enable the I2C peripheral clock */
RCC->APB1ENR |= RCC_APB1ENR_I2C1EN;
/* Configure the I2C pins */
GPIOB->MODER |= (GPIO_MODER_MODER6_1 | GPIO_MODER_MODER7_1); // Set PB6 and PB7 as alternate function
GPIOB->OTYPER |= (GPIO_OTYPER_OT_6 | GPIO_OTYPER_OT_7); // Set PB6 and PB7 as open-drain
GPIOB->OSPEEDR |= (GPIO_OSPEEDER_OSPEEDR6 | GPIO_OSPEEDER_OSPEEDR7); // Set high speed for PB6 and PB7
/* Configure the I2C peripheral */
I2C1->CR1 &= ~I2C_CR1_PE; // Disable the I2C peripheral
I2C1->CR1 |= I2C_CR1_SWRST; // Software reset the I2C peripheral
I2C1->CR1 &= ~I2C_CR1_SWRST; // Clear the software reset bit
I2C1->CR2 = (uint32_t)400000; // Set the I2C clock frequency to 400 kHz
I2C1->CCR = (uint32_t)((SystemCoreClock + (I2C_SPEED * 2)) / (I2C_SPEED * 4)); // Set the I2C clock control register
I2C1->TRISE = (uint32_t)((SystemCoreClock / 1000000) + 1); // Set the maximum rise time
I2C1->OAR1 = (uint16_t)(0x5A << 1); // Set the I2C slave address to 0x5A (7-bit address)
I2C1->OAR1 |= I2C_OAR1_ADD0; // Set the address mode to 7-bit
I2C1->CR1 |= I2C_CR1_PE; // Enable the I2C peripheral
/* Enable the I2C peripheral clock */
RCC->APB1ENR |= RCC_APB1ENR_I2C1EN;
/* Configure the I2C pins */
GPIOB->MODER |= (GPIO_MODER_MODER6_1 | GPIO_MODER_MODER7_1); // Set PB6 and PB7 as alternate function
GPIOB->OTYPER |= (GPIO_OTYPER_OT_6 | GPIO_OTYPER_OT_7); // Set PB6 and PB7 as open-drain
GPIOB->OSPEEDR |= (GPIO_OSPEEDER_OSPEEDR6 | GPIO_OSPEEDER_OSPEEDR7); // Set high speed for PB6 and PB7
/* Configure the I2C peripheral */
I2C1->CR1 &= ~I2C_CR1_PE; // Disable the I2C peripheral
I2C1->CR1 |= I2C_CR1_SWRST; // Software reset the I2C peripheral
I2C1->CR1 &= ~I2C_CR1_SWRST; // Clear the software reset bit
I2C1->CR2 = (uint32_t)400000; // Set the I2C clock frequency to 400 kHz
I2C1->CCR = (uint32_t)((SystemCoreClock + (I2C_SPEED * 2)) / (I2C_SPEED * 4)); // Set the I2C clock control register
I2C1->TRISE = (uint32_t)((SystemCoreClock / 1000000) + 1); // Set the maximum rise time
I2C1->OAR1 = (uint16_t)(0x5A << 1); // Set the I2C slave address to 0x5A (7-bit address)
I2C1->OAR1 |= I2C_OAR1_ADD0; // Set the address mode to 7-bit
I2C1->CR1 |= I2C_CR1_PE; // Enable the I2C peripheral