cancel
Showing results for 
Search instead for 
Did you mean: 

I2C issues

sarang
Associate II
Posted on September 24, 2009 at 18:22

I2C issues

6 REPLIES 6
sarang
Associate II
Posted on May 17, 2011 at 13:23

I am new to I2C stuff. My question is when i2c is initialized, does the SCL start running immediately OR it would be in action only during read and write?

We are interfacing a 24AA16 EEPROM with I2C1.

My code is :

//Using HSI as system clock

/* Enable peripheral clocks --------------------------------------------------*/

/* GPIOB Periph clock enable */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

/* I2C1 Periph clock enable */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

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

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;

GPIO_Init(GPIOB, &GPIO_InitStructure);

I2Cx=I2C1;

I2C_InitTypeDef I2C_InitStructure;

/* I2C configuration */

I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;

I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;

I2C_InitStructure.I2C_OwnAddress1 = I2C1_SLAVE_ADDRESS7;

I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;

I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

I2C_InitStructure.I2C_ClockSpeed = 100000;

/* I2C Peripheral Enable */

I2C_Cmd(I2C1, ENABLE);

/* Apply I2C configuration after enabling it */

I2C_Init(I2C1, &I2C_InitStructure);

guyvo67
Associate II
Posted on May 17, 2011 at 13:23

does the SCL start running immediately OR it would be in action only during read and write?

SCL starts to clock out when you initiate a I2C start like:

I2C_GenerateSTART(I2C1, ENABLE);

When you have an address match on the slave this will ACK accordingly in the 9 nth clock cycle.

But when you just do the initialization both SCL/SDA will be pulled high and nothing will happen on the bus.

-G

sarang
Associate II
Posted on May 17, 2011 at 13:23

On my scope I observe that after i send a ''generate start'' command, both SCL and SDA are pulled low simultaneously. Can it be an erroneous condition? The reference manual does show that high-to-low transition is for SDA during start while SCL remains high.

guyvo67
Associate II
Posted on May 17, 2011 at 13:23

Hi,

Did you enable your I2C clock?

RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

Maybe you can attach the code snippet your are working on.

-G

sarang
Associate II
Posted on May 17, 2011 at 13:23

Thanks guyvo67!

I checked my scope with a sec/div setting of like 25 microseconds. I can clearly see all the data bytes/ address bytes etc. on the SDA and SCL clocking. There is an ACK after every byte as per the scope results. I guess my 24AA16 EEPROM doesnot recognize the ''internal'' address which i send?

My read returns FF. Code snippet is as follows:-

/* Enable peripheral clocks --------------------------------------------------*/

/* GPIOB Periph clock enable */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

/* I2C1 Periph clock enable */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

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

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;

GPIO_Init(GPIOB, &GPIO_InitStructure);

I2C_InitTypeDef I2C_InitStructure;

/* I2C configuration */

I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;

I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;

I2C_InitStructure.I2C_OwnAddress1 = I2C1_SLAVE_ADDRESS7;

I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;

I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

I2C_InitStructure.I2C_ClockSpeed = 50000;

/* I2C Peripheral Enable */

I2C_Cmd(I2C1, ENABLE);

/* Apply I2C configuration after enabling it */

I2C_Init(I2C1, &I2C_InitStructure);

//result = ex_i2c_open(0,0);

User_Print(''\r\nEKA Systems'');

//readEEPROM()

writeEEPROM();

readEEPROM();

/**********************************************************/

//Write and Read functions :

void writeEEPROM()

{

/****************** Write a byte to the EEPROM ****************/

/* Send START condition */

I2C_GenerateSTART(I2C1, ENABLE);

/* Test on EV5 and clear it */

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

/* Send EEPROM address for write */

I2C_Send7bitAddress(I2C1, 0xA0, I2C_Direction_Transmitter);

/* Test on EV6 and clear it */

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

/* Clear EV6 by setting again the PE bit */

I2C_Cmd(I2C1, ENABLE);

/* Send the EEPROM's internal address to write to */

I2C_SendData(I2C1, EEPROM_INTERNAL_ADDRESS);

/* Test on EV8 and clear it */

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

/* Send the byte to be written */

I2C_SendData(I2C1, 0xBE);

/* Test on EV8 and clear it */

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

/* Send STOP condition */

I2C_GenerateSTOP(I2C1, ENABLE);

}

void readEEPROM()

{

/**************** Read a byte from the EEPROM ****************/

/* While the bus is busy */

while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));

/* Send START condition */

I2C_GenerateSTART(I2C1, ENABLE);

/* Test on EV5 and clear it */

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

/* Send EEPROM address for write */

I2C_Send7bitAddress(I2C1, 0xA0, I2C_Direction_Transmitter);

/* Test on EV6 and clear it */

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

/* Clear EV6 by setting again the PE bit */

I2C_Cmd(I2C1, ENABLE);

/* Send the EEPROM's internal address to write to */

I2C_SendData(I2C1, EEPROM_INTERNAL_ADDRESS);

/* Test on EV8 and clear it */

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

/* Send START condition a second time */

I2C_GenerateSTART(I2C1, ENABLE);

/* Test on EV5 and clear it */

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

/* Send EEPROM address for read */

I2C_Send7bitAddress(I2C1, 0xA0, I2C_Direction_Receiver);

/* Test on EV6 and clear it */

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));

while(!I2C_GetFlagStatus(I2C1,I2C_FLAG_RXNE));

/* Byte to be read */

/* Test on EV7 and clear it */

if(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED))

{

/* Read a byte from the EEPROM */

sprintf(tempStr,''\r\nEEPROM contains 0X%04X'',I2C_ReceiveData(I2C1));

User_Print(tempStr);

}

/* Disable Acknowledgement */

I2C_AcknowledgeConfig(I2C1, DISABLE);

/* Send STOP Condition */

I2C_GenerateSTOP(I2C1, ENABLE);

}

tomas23
Associate II
Posted on May 17, 2011 at 13:23

If you read the data from EEPROM, do you see them on the scope, but the I2C->DR returns 0xFF?