Skip to main content
aamirali641989
Associate III
June 26, 2013
Question

Error while sending stop condition for I2c

  • June 26, 2013
  • 5 replies
  • 972 views
Posted on June 26, 2013 at 14:20

The original post was too long to process during our migration. Please click on the attachment to read the original post.
    This topic has been closed for replies.

    5 replies

    frankmeyer9
    Associate III
    June 26, 2013
    Posted on June 26, 2013 at 14:51

    I might have got something wrong, but:

        while( stop_sent == 0 );

    doesn't make sense to me.

    You are comparing a variable with a constant in a loop. On match, this loop is endless, otherwise a NOP.

    zzdz2
    Associate
    June 26, 2013
    Posted on June 26, 2013 at 15:39

                    if( tx_idx == 7 )                     /* send stop condition */

     

                    {

     

                        I2C3->CR1 |= I2C_CR1_STOP;        /* generate a stop condition */

     

                        tx_idx++;

     

                        stop_sent = 1;

     

                    }    

     

    With the above code tx_idx will be 8 at least.

    Try something like this:

                    if( tx_idx == 7 )                     /* send stop condition */

     

                    {

     

                        if(!stop_sent)

     

                        {

     

                          I2C3->CR1 |= I2C_CR1_STOP;        /* generate a stop condition */

     

                          stop_sent = 1;

     

                        }

     

                    }    

     

     

    jpeacock2399
    Associate III
    June 26, 2013
    Posted on June 26, 2013 at 16:00

    Is stop_sent declared as volatile?  Otherwise results are unpredictable.

      Jack Peacock
    aamirali641989
    Associate III
    July 12, 2013
    Posted on July 12, 2013 at 14:14

    1. I had again cheked the code and noticed that even after sending stop conditoin code goes to following interrupt else part:

                case I2C_EVENT_MASTER_BYTE_TRANSMITTING:  /* EV8 , TRA, BUSY, MSL, TXE flags  */

                case I2C_EVENT_MASTER_BYTE_TRANSMITTED:   /* EV8_2, TRA, BUSY, MSL, TXE and BTF flags  */

                    if( tx_idx == 7 )                     /* send stop condition */

                    {

                        I2C3->CR1 |= I2C_CR1_STOP;        /* generate a stop condition */

                        tx_idx++;

                        stop_sent = 1;

                    }    

                    else

                    {

                        I2C3->DR = tx_buffer[tx_idx++] ;               /* Transmit the data  */     

                    }    

    2. Yes all vars are volatile.

    3. Coming back to ISR again indicates that there some ISR flag pending but I had cleared all by reading the registers, but don't what causes that. When code comes to stop condition in ISR then value of event var is = 0x00070080.

    4. Can anyone check at my code & see if my initialization stpes & ISR are correct

    zzdz2
    Associate
    July 13, 2013
    Posted on July 13, 2013 at 14:28

    STM32 doc says:

    EV8_2: TxE=1, BTF = 1, Program Stop request. TxE and BTF are cleared by hardware by the Stop condition

    I think you should set

    I2C_CR1_STOP

     only when you get 

    2C_EVENT_MASTER_BYTE_TRANSMITTED 

    (BTF=1). 

    To me it looks like you handle one request which should be ignored.