cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L-Discovery communicate in I2C with M24LR64

Posted on April 24, 2012 at 09:13

Hello everybody,

I have some troubel to communicate with m24LR64. I use a STM32L- Discovery.

I try to see something in the oscilloscope but nothing, could you help me to see what is wrong in my code:

#define I2C2_SLAVE_ADDRESS7     0xA0   // EEPROM I2C Address

#define ClockSpeed              200000 // 200 KHz

#include ''mainsee.h''

void I2Cmain(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

I2C_InitTypeDef  I2C_InitStructure;

       

  /* I2C1 and I2C2 Periph clock enable */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);

  /* GPIOB Periph clock enable */

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

 

  /* Configure I2C2 pins: SCL and SDA ----------------------------------------*/

  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_10 | GPIO_Pin_11;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

  GPIO_PinAFConfig(GPIOB, GPIO_Pin_10, GPIO_AF_I2C2);

  GPIO_PinAFConfig(GPIOB, GPIO_Pin_11, GPIO_AF_I2C2);

  //I2C_DeInit(I2C2);    

           

  /* I2C2 configuration ------------------------------------------------------*/

  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;

  I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;

  I2C_InitStructure.I2C_OwnAddress1 = I2C2_SLAVE_ADDRESS7;

  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;

  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

  I2C_InitStructure.I2C_ClockSpeed = ClockSpeed;

  I2C_Init(I2C2, &I2C_InitStructure);

  /* Enable I2C2 -------------------------------------------------------------*/

  I2C_Cmd(I2C2, ENABLE);

  /* Send I2C1 START condition */

  I2C_GenerateSTART(I2C2, ENABLE);

  /* Test on I2C1 EV5 and clear it */

 // while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));

   /* Send EEPROM slave Address for write */

  //I2C_Send7bitAddress(I2C2, I2C2_SLAVE_ADDRESS7, I2C_Direction_Transmitter);  }

  /* Test on I2C1 EV6 and clear it */

 // while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

 while(1){

  /* Send I2C1 EEPROM internal address */

  I2C_SendData(I2C2, 0x00);   }

  /* Test on I2C1 EV8 and clear it */

  while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

  /* Send I2C1 EEPROM data */

  I2C_SendData(I2C2, 0x05);

  /* Test on I2C1 EV8 and clear it */

  while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

  /* Send I2C1 STOP Condition */

  I2C_GenerateSTOP(I2C2, ENABLE);

  while(1); // Do not exit

}

Thank you very much in advance to help me.

P.S: Sorry for the english, i'm French.

20 REPLIES 20
stm32forum
Associate II
Posted on April 26, 2012 at 09:47

Do you have no communication at all?

You need to unlock the eeprom with password before you can use it, see page 28 of the datasheet.

Never used the M24LR64 with an stm32, only with avr, so i can't say exactly what's wrong.

Posted on April 26, 2012 at 10:28

I perhaps have problem of password but even i don't plug the m24, no output signal on my port, just noise.

alok472
Associate II
Posted on April 26, 2012 at 12:06

Pls use the AN2824  STM32F10xxx devices: optimized I²C examples  and associated firmware.

It seemed from SDA=0, SCL=0 that a start condition is generated. Why not connect a simple i2c eeprom and verify the i2c comm ?

why do you comment some code ? like.. 

 // while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));

Posted on April 26, 2012 at 13:33

I have used a code from example, but the result are the same,I don't have another EEPROM, and i would test without memory in output, just for the signal of the µC, that's why i comment some part of the code, because it stay in the while without memory and it's not the purpose.

Posted on April 30, 2012 at 09:42

Hello,

I managed to observe signal on oscilloscope, i've changer the order in my code and, magic it works:

void I2CInit(void)

{

  GPIO_InitTypeDef GPIO_InitStructure;

  I2C_InitTypeDef I2C_InitStructure;

   /* Enable I2C clock */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);

    /* Enable GPIOA clock */

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

  /* Configure PB.10 & PB.11 in alternate function -------------------------*/

  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource10,GPIO_AF_I2C2) ;

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource11,GPIO_AF_I2C2) ;

  /* Enable I2C2 -------------------------------------------------------------*/

  I2C_DeInit(I2C2);    

  I2C_Cmd(I2C2, ENABLE);

     /* Init I2C */

  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;

  I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;

  I2C_InitStructure.I2C_OwnAddress1 = 0xA0;

  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;

  I2C_InitStructure.I2C_ClockSpeed = IS_I2C_CLOCK_SPEED(400000);

  I2C_Init(I2C2,&I2C_InitStructure);

 }

Now i've some trouble, the programme wait because of this :

while(!I2C_CheckEvent(I2C2,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

So it's my m24LR that doesn't answer. So why? when i removed this in my code, the program works but when i verifying in RFID nothing have been write in it.

my code for write:

void write(void)

{

  I2C_AcknowledgeConfig(I2C2, ENABLE);

   

  /* M 24 */

 

   I2C_GenerateSTART(I2C2, ENABLE);    //Delay(100) ;

   while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));

   I2C_Send7bitAddress(I2C2,0xA0, I2C_Direction_Transmitter); Delay(1000);    

   //while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));  

       /* adresse */

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

   Delay(1000) ;

   //Delay(1000);

   I2C_SendData(I2C2,0x6E);

   //while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

   //I2C_SendData(I2C2,0xA0);Delay(1000);

   //while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

       

   I2C_GenerateSTOP(I2C2, ENABLE);

   Delay(750);

}

Posted on May 02, 2012 at 08:57

Hello.

No one to help me for this problem that it would be arrived for another person.

Posted on May 03, 2012 at 15:30

Hello,

I share a screen of a result of my code in my oscilloscope.

Ther is an strange thing between my start and a adress send, i don't know why i have this attent time, and an other strange thing, it's my ACK it's not 0 but not 1 it's between the both. I've testes for twot EEPROME ( the m24lr64 and m64c64) the result are the same.

Anyone can help me, or have this same problem.

0690X00000604sWQAQ.png

hzrnbgy
Associate II
Posted on May 04, 2012 at 14:20

try configuring the pins in open drain mode

Posted on May 04, 2012 at 15:22

thank you ver much that was the mistake in my code.

siddhartha
Associate
Posted on January 07, 2015 at 06:30

Wat changes u have done in code?? even we are facing similar issue..So need to know..