Issues with using AT24C64 and STM32f030C8t6

by Nikhil Ben George   Last Updated June 20, 2018 07:25 AM

i am trying to develop a library for using stm32f030c8t6 with at24C04 EEPROM. But so far i couldnt get it to work. I have used a mpu6050 so i know my i2c function is working. Is it some kind of configuration mistake.The eeprom has a0,a1,a2 pins grounded so adress will be 0xA0. It would be great if anyone could take a look.Thank You

void Configure_GPIO_I2C2(void)
{

 RCC->AHBENR |= RCC_AHBENR_GPIOFEN;port f enable
 GPIOF->OTYPER |= GPIO_OTYPER_OT_6 | GPIO_OTYPER_OT_7;  opendrain setting
 GPIOF->MODER |= (2<<12) | (2<<14);     //alternate func on pf6 and pf7 

 GPIOF->OSPEEDR &=  ~(1<<12); speed setting
 GPIOF->OSPEEDR &=  ~(1<<14);

 GPIOF->PUPDR &= ~(1<<12); no pullup
 GPIOF->PUPDR &= ~(1<<12);
}

void Configure_I2C2_Master(void)
{
  /* Enable the peripheral clock I2C2 */
  RCC->APB1ENR |= RCC_APB1ENR_I2C2EN;

  /* Configure I2C2, master */
  /* (1) Timing register value is computed with the AN4235 xls file,
   fast Mode @400kHz with I2CCLK = 48MHz, rise time = 140ns,fall time = 40ns 

  I2C2->CR1 &= ~I2C_CR1_PE;
  I2C2->TIMINGR |= (uint32_t)0x00B01A4B; /* (1) */

  //slave addr 
  I2C2->CR2 |= 0xA0<<1;//(uint32_t)(((uint32_t)0xA0 << 1 ) & I2C_CR2_SADD);
  //no of bytes
  I2C2->CR2 |= 1<<16;//(uint32_t)(((uint32_t)1 << 16 ) & I2C_CR2_NBYTES);
  //peripheralEnable
  I2C2->CR1 |= I2C_CR1_PE;
  //interrupt enable
  NVIC_SetPriority(I2C2_IRQn, 0); 
  NVIC_EnableIRQ(I2C2_IRQn); 
}

int read_i2c( uint8_t reg)
{
    uint8_t data=0;
    I2C2->CR1 |= I2C_CR1_PE;
    if((I2C2->ISR & I2C_ISR_TXE) == (I2C_ISR_TXE)) /* Check Tx empty */
     {

      Delay_xmS(5);
      I2C2->CR2 &= (~I2C_CR2_RD_WRN); //write bit
      I2C2->CR2 |= (0xA0<<1); //slave id
      I2C2->CR2 |= I2C_CR2_START;//start cond
      while(I2C2->CR2 & I2C_CR2_START);
      I2C2->TXDR = (uint8_t)0x05;//sending reg addr
      while(!(I2C2->ISR & I2C_ISR_TXE));//program stops here
      //if i disable the above line the nack is set and enter this if line.
      if((I2C2->ISR & I2C_ISR_NACKF) == (I2C_ISR_NACKF)) 
        {   
          UART_Send_String(1,"\r\n NAK GENERATED");           
          I2C2->ISR &=  ~I2C_ICR_NACKCF;
        }

      I2C2->CR2 |= I2C_CR2_RD_WRN; //seting for read
      I2C2->CR2 |= I2C_CR2_START;//start cond
      while(I2C2->CR2 & I2C_CR2_START);
      UART_Send_String(1,"\r\n DATA RECEIVED");
      data = (uint8_t)I2C2->RXDR; //collecting data
      I2C2->CR2 |= I2C_CR2_STOP;//stop bit
      while(I2C2->CR2 & I2C_CR2_STOP);      
      UART_Send_String(1,"\r\n transfer complete:");
      UART_Send_String(1,&data);
      return data;

}



Related Questions





Breaking a 16-bit long int to write into eeprom?

Updated June 30, 2015 16:12 PM