cancel
Showing results for 
Search instead for 
Did you mean: 

stm32 accelerometer

erayurtseven
Associate II
Posted on April 30, 2014 at 11:54

hi, I have a problem. I2C busy flag always set.

 

I am using I2C1 peripherel. this peripherel conected EEPROM, IO expander and Lis302dl

I think, problem is attaching EEPROM  or  IO expander.

how can I disconnect EEPROM and IO expander using software. 

If this is not the problem, what is problem ?

my configuration codes:

GPIO_InitTypeDef GPIO_InitStructure;

I2C_InitTypeDef I2C_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;   //GPIO_Mode_AF_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOB,&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;

GPIO_Init(GPIOB,&GPIO_InitStructure);

I2C_Cmd(I2C1, ENABLE);

I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; 

I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; 

I2C_InitStructure.I2C_OwnAddress1 = 0x00; 

I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; 

I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; 

I2C_InitStructure.I2C_ClockSpeed = 100000; 

I2C_Init(I2C1, &I2C_InitStructure);

codes for reading process:

unsigned char uc;

I2C_GenerateSTART(I2C1, ENABLE); 

  while (!(I2C_sr() & 0x0001));  // start condition generate edilene kkadar bekliyoz. 

I2C_Send7bitAddress(I2C1, address, I2C_Direction_Transmitter);

  while (!(I2C_sr() & 0x0002)); //Addr sent olana kadan bekle 

I2C_SendData(I2C1,cmd); 

  while (!(I2C_sr() & 0x00000004));     /* Wait until BTF bit set             */ 

  I2C_GenerateSTART(I2C1, ENABLE); 

  while (!(I2C_sr() & 0x0001));  // start condition generate edilene kkadar bekliyoz. 

I2C_Send7bitAddress(I2C1, address,I2C_Direction_Receiver); 

  while (!(I2C_sr() & 0x0002)); //Addr sent olana kadan bekle 

uc = I2C_ReceiveData(I2C1); 

  while (!(I2C_sr() & 0x00000040));     /* Wait until RxNE bit set            */

I2C_GenerateSTOP(I2C1, ENABLE); 

while (I2C_sr() & 0x00020000);        /* Wait until BUSY bit reset          */

return( uc );

4 REPLIES 4
stm322399
Senior
Posted on April 30, 2014 at 12:12

Normally, you must read SR2 (to get the MSL flag) after SR1 returns with ADDR flag set.

I don't know if it will solve your issue. Otherwise, I see no reasons for EEPROM or IOEXP to disturb the communication with LIS302dl. Try to adapt this one to your needs:

static uint8_t IOEXP_read8(uint8_t regnum)
{
int maxloop = 1000000;
uint8_t value;
I2C_GenerateSTART(I2C1, ENABLE);
while ( I2C_GetFlagStatus(I2C1, I2C_FLAG_SB) == RESET ) if (!maxloop--) goto error;
I2C_Send7bitAddress(I2C1, IOEXP_ADDR*2, I2C_Direction_Transmitter);
while ( I2C_GetFlagStatus(I2C1, I2C_FLAG_ADDR) == RESET ) if (!maxloop--) goto error;
I2C_GetFlagStatus(I2C1, I2C_FLAG_MSL);
while ( I2C_GetFlagStatus(I2C1, I2C_FLAG_TXE) == RESET ) if (!maxloop--) goto error;
I2C_SendData(I2C1, regnum);
I2C_AcknowledgeConfig(I2C1, DISABLE);
I2C_GenerateSTART(I2C1, ENABLE);
while ( I2C_GetFlagStatus(I2C1, I2C_FLAG_SB) == RESET ) if (!maxloop--) goto error;
I2C_Send7bitAddress(I2C1, IOEXP_ADDR*2, I2C_Direction_Receiver);
while ( I2C_GetFlagStatus(I2C1, I2C_FLAG_ADDR) == RESET ) if (!maxloop--) goto error;
I2C_GetFlagStatus(I2C1, I2C_FLAG_MSL);
while ( I2C_GetFlagStatus(I2C1, I2C_FLAG_RXNE) == RESET ) if (!maxloop--) goto error;
value = I2C_ReceiveData(I2C1);
I2C_GenerateSTOP(I2C1, ENABLE);
while ( I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY) == SET ) if (!maxloop--) goto error;
return value;
error:
LOG_ERROR();
return 0;
}

erayurtseven
Associate II
Posted on April 30, 2014 at 18:45

thanks for quick reply

I am debugging my code, so I see current situation of I2C1 registers, 

SR1 = 0x00;

SR2= 0x02;     always 

 so, progrmam hangs 

erayurtseven
Associate II
Posted on April 30, 2014 at 22:00

still there are problem.

any suggestion ?

chen
Associate II
Posted on May 01, 2014 at 12:49

Do you have an oscilloscope or logic analyser?

With I2C - it is a good idea to examine the bus.

(The problem may not be with your code)