Skip to main content
sukrubahadirarslan
Associate II
Dec 21, 2013 12:35 PM UTC
Question

I2C Busy Flag problem

Posted on December 21, 2013 at 13:35

hello.

I want to write byte to the sensör with I2C so write this code 

void MPU6050_I2C_Init()

{

        // RCC Configuration

        RCC_HCLKConfig(RCC_SYSCLK_Div2);

        RCC_PCLK1Config(RCC_HCLK_Div1);

        RCC_PCLK2Config(RCC_HCLK_Div1);

        RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);

// GPIO Configuration

GPIO_Config_Anahtari.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;

GPIO_Config_Anahtari.GPIO_Mode = GPIO_Mode_AF_OD;

GPIO_Config_Anahtari.GPIO_Speed = GPIO_Speed_2MHz;

  GPIO_Init(GPIOB,&GPIO_Config_Anahtari);

  GPIO_PinRemapConfig(GPIO_Remap_I2C1,ENABLE);

  // I2C Configuration

 I2C_Config_Anahtari.I2C_Mode = I2C_Mode_I2C;

  I2C_Config_Anahtari.I2C_ClockSpeed = 400000;

    I2C_Config_Anahtari.I2C_DutyCycle = I2C_DutyCycle_2;

  I2C_Config_Anahtari.I2C_Ack = ENABLE;

  I2C_Config_Anahtari.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

 I2C_Config_Anahtari.I2C_OwnAddress1 = 0x10;

I2C_Init(I2C1,&I2C_Config_Anahtari);

 I2C_Cmd(I2C1,ENABLE);

}

void I2C_MonitorAndClear_ADDR()

{

        while(I2C_GetFlagStatus(I2C1,I2C_FLAG_ADDR)==RESET);

        I2C_ReadRegister(I2C1,I2C_Register_SR1);

 

I2C_ReadRegister(I2C1,I2C_Register_SR2);

}

void I2C_Clear_SB_Monitor_MSL()

{

        I2C_ReadRegister(I2C1,I2C_Register_SR1);

       

while(I2C_GetFlagStatus(I2C1,I2C_FLAG_MSL)==RESET);

}

void MPU6050_SingleByte_Write(uint8_t register_address,uint8_t data)

{

        while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY)==SET);

       

I2C_GenerateSTART(I2C1,ENABLE);

 I2C_Clear_SB_Monitor_MSL();

       

I2C_Send7bitAddress(I2C1,MPU6050_Default_Address,I2C_Direction_Transmitter);

I2C_MonitorAndClear_ADDR();

while(I2C_GetFlagStatus(I2C1,I2C_FLAG_TRA)==RESET);

I2C_SendData(I2C1,register_address);

while(I2C_GetFlagStatus(I2C1,I2C_FLAG_TXE)==RESET);

I2C_SendData(I2C1,data);

while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BTF)==RESET);

I2C_GenerateSTOP(I2C1,ENABLE);

}

here is my functions . In RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE); line , BUSY flag is set and never reset. So code hangs in here. When I reset the I2C1 periph clock with RCC_APB2PeriphResetCmd command , busy flag is reset but I can't configure the I2C interface after reset cmd. So what is the problem ?

thanks for your answer.

    This topic has been closed for replies.

    7 replies

    Tesla DeLorean
    Guru
    Dec 21, 2013 2:44 PM UTC
    Posted on December 21, 2013 at 15:44

    The ReMap would require the AFIO clock enabled.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    sukrubahadirarslan
    Associate II
    Dec 21, 2013 8:23 PM UTC
    Posted on December 21, 2013 at 21:23

    İt does not work. 

    I don't change anythink in code or in board , but when I try again , It work . I don't understand why.

    Now I have a another problem. 

    I am using MPU6050 sensor. And when I send the address of sensor , no-acknowledgment bit coming from sensör. I cant find the problem . Can you help me in this issue ?

    thanks

    Tesla DeLorean
    Guru
    Dec 21, 2013 9:27 PM UTC
    Posted on December 21, 2013 at 22:27

    Can you help me in this issue ?

    Not much I suppose.

    So, what address are you sending? Can you see the transaction on a scope?

    I'd supposed it would use address 0xD0 or 0xD2 depending on the part strapping. Make sure it is strapped for I2C operation, and what address you expect.
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    sukrubahadirarslan
    Associate II
    Dec 21, 2013 10:34 PM UTC
    Posted on December 21, 2013 at 23:34

    *In sensör datasheet, default sensör address is 0x68 so I send the sensör 0x68 for addressing.

    * I dont observe SCL and SDA signal in scope. 

    Can I use Keil for observe this signal ? 

    I mean I saw logic analyzer in keil but I dont know how to use it ? 

    Can I use logic analyzer for analyze SCL and SDA signal ? 

    Tesla DeLorean
    Guru
    Dec 22, 2013 12:57 AM UTC
    Posted on December 22, 2013 at 01:57

    No, I really think you're misinterpreting the data sheets wrt 7-bit addressing.

    0x68 << 1 = 0xD0

    The lack of an ACK indicates the part doesn't recognize the presented address.

    Try 0xD0

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    sukrubahadirarslan
    Associate II
    Dec 22, 2013 11:05 AM UTC
    Posted on December 22, 2013 at 12:05

    I try 0xD0 .  I dont receive nack bit, but this time ADDR bit is not set.Program hangs in

    while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)==ERROR); . So I dont understand what is the problem. I dont have any solution. Why ADDR bit is not set ?

    thanks 

    sukrubahadirarslan
    Associate II
    Dec 22, 2013 11:26 AM UTC
    Posted on December 22, 2013 at 12:26

    Now I try 0xD0 . but in debug mode I change breakout point location . 

    here is my screen.0690X00000604xsQAA.png

    When I insert breakout point like this, program not hangs in 

    while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)==ERROR); line. But I insert breakout point like this(below picture) ,  And step one line usin F11 , program hangs in while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)==ERROR); line. 

    So can you interpret this situation ? 

    thanks again

    0690X000006052gQAA.png

    Posted: Sunday, December 22, 2013 12:05 PM

    Subject: I2C Busy Flag problem

    I try 0xD0 .  I dont receive nack bit, but this time ADDR bit is not set.Program hangs in

    while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)==ERROR); . So I dont understand what is the problem. I dont have any solution. Why ADDR bit is not set ?

    thanks