cancel
Showing results for 
Search instead for 
Did you mean: 

I2C EEPROM(STM M24lr64) problems

shetty_siddharth
Associate II
Posted on September 02, 2012 at 00:16

Hello,

I am trying to write a byte(A0) to a particular location on the EEPROM. I have intialized the I2C(1) as in the code mentioned below(In sEE_INIT()). I write the byte using the EEPROM_write function and read the data from EEPROM_read function. However the program gets hung in the following while loop : while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) in the EEPROM_read function. Could someone please let me know if there is some mistake i am making?

Thanks in advance!

#include ''main.h''

#include ''constant.h''

void sEE_LowLevel_Init(void);

/**

  * @brief  Initializes peripherals used by the I2C EEPROM driver.

  * @param  None

  * @retval None

  */

void sEE_Init(void)

  I2C_InitTypeDef  I2C_InitStructure;

  

  sEE_LowLevel_Init();

  

  /*!< I2C configuration */

  /* sEE_I2C configuration */

  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;

  I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;

  I2C_InitStructure.I2C_OwnAddress1 = I2C_SLAVE_ADDRESS7;

  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;

  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

  I2C_InitStructure.I2C_ClockSpeed = I2C_SPEED;

  

  /* sEE_I2C Peripheral Enable */

  I2C_Cmd(sEE_I2C, ENABLE);

  /* Apply sEE_I2C configuration after enabling it */

  I2C_Init(sEE_I2C, &I2C_InitStructure);

   

}

/**

  * @brief  Initializes peripherals used by the I2C EEPROM driver.

  * @param  None

  * @retval None

  */

void sEE_LowLevel_Init(void)

{

  GPIO_InitTypeDef  GPIO_InitStructure;

  NVIC_InitTypeDef NVIC_InitStructure;  

   

  /*!< sEE_I2C Periph clock enable */

  RCC_APB1PeriphClockCmd(sEE_I2C_CLK, ENABLE);

  

  /*!< sEE_I2C_SCL_GPIO_CLK and sEE_I2C_SDA_GPIO_CLK Periph clock enable */

  RCC_AHB1PeriphClockCmd(sEE_I2C_SCL_GPIO_CLK | sEE_I2C_SDA_GPIO_CLK, ENABLE);

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);

  

  /* Reset sEE_I2C peripheral */

  RCC_APB1PeriphResetCmd(sEE_I2C_CLK, ENABLE);

  

  /* Release reset signal of sEE_I2C IP */

  RCC_APB1PeriphResetCmd(sEE_I2C_CLK, DISABLE);

    

  /*!< GPIO configuration */  

  /*!< Configure sEE_I2C pins: SCL */   

  GPIO_InitStructure.GPIO_Pin = sEE_I2C_SCL_PIN;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;

  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;

  GPIO_Init(sEE_I2C_SCL_GPIO_PORT, &GPIO_InitStructure);

  /*!< Configure sEE_I2C pins: SDA */

  GPIO_InitStructure.GPIO_Pin = sEE_I2C_SDA_PIN;

  GPIO_Init(sEE_I2C_SDA_GPIO_PORT, &GPIO_InitStructure);

  /* Connect PXx to I2C_SCL*/

  GPIO_PinAFConfig(sEE_I2C_SCL_GPIO_PORT, sEE_I2C_SCL_SOURCE, sEE_I2C_SCL_AF);

  /* Connect PXx to I2C_SDA*/

  GPIO_PinAFConfig(sEE_I2C_SDA_GPIO_PORT, sEE_I2C_SDA_SOURCE, sEE_I2C_SDA_AF);  

 

}

/* EEPROM write function*/

void EEPROM_write(void)

{

   I2C_AcknowledgeConfig(I2C1, ENABLE);

   I2C_GenerateSTART(I2C1, ENABLE);    

   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

   I2C_Send7bitAddress(I2C1,0xA0, I2C_Direction_Transmitter);   

   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));   

   I2C_SendData(I2C1,0x00); // Send Year register address

   I2C_SendData(I2C1,0x6E);

   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

   I2C_SendData(I2C1,0xA0);

   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));  

   I2C_GenerateSTOP(I2C1, ENABLE); 

}

uint8_t EEPROM_read(void)

{

   uint8_t recieved_byte;

   I2C_AcknowledgeConfig(I2C1, ENABLE);

   I2C_GenerateSTART(I2C1, ENABLE);    

   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

   I2C_Send7bitAddress(I2C1,0xA0, I2C_Direction_Transmitter);   

   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));   

   I2C_SendData(I2C1,0x00); // Send Year register address

   I2C_SendData(I2C1,0x6E);

   I2C_Send7bitAddress(I2C1,0xA0, I2C_Direction_Receiver);

   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); 

   I2C_NACKPositionConfig(I2C1, I2C_NACKPosition_Current);

   I2C_AcknowledgeConfig(I2C1, DISABLE);

   recieved_byte = I2C_ReceiveData(I2C1);

/* Send STOP Condition */

I2C_GenerateSTOP(I2C1, ENABLE);

while(I2C_GetFlagStatus(I2C1, I2C_FLAG_STOPF)); // stop bit flag

}
5 REPLIES 5
micheok
Associate II
Posted on December 03, 2012 at 18:42

I think as

description

is

as

follows

Posted on December 03, 2012 at 18:48

I think as

description

is

as

follows

 

 

 

RCC_AHB1PeriphClockCmd(sEE_I2C_SCL_GPIO_CLK | 

 

               

Maybe 

AHB

2

PeriphClockCmd

well be correct

 

sEE_I2C_SDA_GPIO_CLK, ENABLE);

 

There are two AHB buses?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
micheok
Associate II
Posted on December 03, 2012 at 22:59

APB1 & APB2

APB2

feed them

(etc

ADC3,

the USART1

TIM8

,

SPI1,

TIM1

, ADC2

ADC1

,

IOPG

,

IOPF

,

IOPE

,

IOPD

,

the IOPC

,

IOPB

,

IOPA

,

AFIO

)

Correct

''

RCC_

APB1

PeriphClockCmd(sEE_I2C_SCL_GPIO_CLK | 

               sEE_I2C_SDA_GPIO_CLK, ENABLE);

''

will be good

cause

#define sEE_I2C_SCL_GPIO_CLK   RCC_APB2Periph_GPIOB

#define sEE_I2C_SDA_GPIO_CLK   RCC_APB2Periph_GPIOB

I2C EEPROM (STM M24lr64) problems

Notice:

Multi-

function pin only active ''I2Cx'' clock and don't remap or alternate function pin.

In I2C2 function, the USART3 clock

can not

enable simultaneity.

in the case:

RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2,ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4                      

                                                |RCC_APB1Periph_TIM3

                                                |RCC_APB1Periph_USART3 <---

can't apply this when  active I2C2 at the same time.

                            , ENABLE );                                

micheok
Associate II
Posted on December 03, 2012 at 23:13

Correct

''

RCC_

APB2

PeriphClockCmd(sEE_I2C_SCL_GPIO_CLK | 

               sEE_I2C_SDA_GPIO_CLK, ENABLE);

''

will be good

Posted on December 04, 2012 at 02:58

APB != AHB

Note that the OP is using an F2/F4 series chip, not an F1
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..