Search code examples
stm32i2chalstm32f4eeprom

I2C failure with stm32


I have a problem about I2C and I saw some topics about my problem on the internet but there was no solution, I hope you help me by sheding light on this.

Problem : After sending adress by I2C, the ADDR flag in SR1 register is not set but at the same time AF Flag(acknowledge failure) is setting up. I do not use std peripherial libraries and I use stm32f429I-Discovery kit and AT24C256.

It stucks while (!(I2C1->SR1 & 0x0002)); line, I could not find a solution. Please help me.

 /* Includes ------------------------------------------------------------------*/
    #include "main.h"




    int main(void)
    {

        int slave_adress = 0x50; // eeprom adress
        int word_adress = 0x01;  // I want to write in this register adress in eeprom.
              int data = 0x30;  // the data I want to write





        RCC->AHB1ENR |= 1<<1; //enable port B


        RCC->APB1ENR |= 1<<21; // enable  i2c_1 





        GPIOB->MODER    = 0x82000; // made PB6-I2C1_CLK ve PB9-I2C_SDA pins as  alternate function

          GPIOB->AFR[0] = 0x4000000; //  made pb6 as af4 (i2c1_scl)
        GPIOB->AFR[1]   = 0x40;      //    made pb9 as af4 (i2c1_sda)



    GPIOB->OTYPER |= 0x240; //made PB6 and PB9 as open drain 


    GPIOB->PUPDR = 0x00;



    I2C1->CR2 = 0x0010; // made 16MHz clock hsi
    I2C1->CCR = 0x0050; ////SM Mod, duty=0 pclk=16mhz
    I2C1->TRISE = 0x0011; //1000 ns / 62.5 ns = 16 + 1

        I2C1->CR1    = 0x0001;      //enable I2C


I2C1->CR1 |= 1<<8; //I2C Start

            while (!(I2C1->SR1 & 0x0001));// //wait for start bit,




            I2C1->DR = slave_adress;                // Write to I2C Address register
            while (!(I2C1->SR1 & 0x0002));  ///////////////it stucks here///////////////////////////




            I2C1->DR = word_adress; // (EV8_1 – reference manual)

            while (!(I2C1->SR1 & (1<<7))); // Wait TxE bit set






    }

Solution

  • I solved the problem , my fault was about initialising , the working code is below (I used HAL functions only giving clock):

    #define eeprom_adress 0xA0
    uint8_t data = 0x56;
    
    
    int main(void)
    {
    
      HAL_Init();
    
    
      SystemClock_Config();
    
    
    
        __HAL_RCC_GPIOA_CLK_ENABLE();
      __HAL_RCC_GPIOB_CLK_ENABLE();
    
    
     GPIOB->MODER  |= 0x82000;
     GPIOB->OTYPER |= 0x0240;
     GPIOB->OSPEEDR|= 0xC30C0;
     GPIOB->PUPDR  |= 0x41000;
     GPIOB->AFR[0] |= 0x4000000;
     GPIOB->AFR[1] |= 0x0040;
    
    
    
    
        __HAL_RCC_I2C1_CLK_ENABLE();
    
    
    
        I2C1->OAR1 |= 0x4000;
    
      I2C1->CR2 |= 0x0003;
        I2C1->CCR |= 0x0010;
        I2C1->TRISE |= 0x004;
    
    
    
    
    
            I2C1->CR1 |= 0x0001; //i2c yi enable ettim
    
    
            I2C1-> CR1|=1<<8; //start bitini ver
    
            while(!(I2C1->SR1 & 0x0001)); // start bitini bekliyoz
    
            I2C1->DR = eeprom_adress; // slave cihazin adresini yazdik
        //HAL_Delay(4);
            while(!(I2C1->SR1 & 0x0002)); // ADRES GITTIMI
            while(!(I2C1->SR2 &  0x0001)); // MSL ICIN BEKLE
    
        I2C1->DR = 0x01;
    
            while(!(I2C1->SR1 & 0x0080));
    
            I2C1->DR = 0x56;
    
    
            while(!(I2C1->SR1 & 0x0080));  // txe nin bosalmasini bekle
            while(!(I2C1->SR1 & 0x0004)); // btf yi bekliyoz
    
            I2C1->CR1 |= 1<<9; // stop bit
    
    
        }