Skip to main content
zeev
Associate
June 30, 2009
Question

polling I2C with another intrrupt

  • June 30, 2009
  • 2 replies
  • 716 views
Posted on June 30, 2009 at 19:48

polling I2C with another intrrupt

    This topic has been closed for replies.

    2 replies

    zeev
    zeevAuthor
    Associate
    May 17, 2011
    Posted on May 17, 2011 at 13:14

    I use I2C2 to comunicate between STM32 and another device that his address is 0x5E .

    I take the function that do read and write (in polling) from the examples of the STM32 I2C example5 .

    The problem is :

    when I enabled interrupts , after few cycles (write and read)of I2C work the I2C is stuck and dont exit from the function and all the firmware is stuck .

    when I disabled the interrupts it is work very good .

    this is the function :

    void I2C_Write(u8* pBuffer, u8 WriteAddr, u8 NumByteToWrite)

    {

    /* While the bus is busy */

    while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY));

    /* Send START condition */

    I2C_GenerateSTART(I2C2, ENABLE);

    /* Test on EV5 and clear it */

    while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));

    /* Send EEPROM address for write */

    I2C_Send7bitAddress(I2C2, 0x5E, I2C_Direction_Transmitter);

    /* Test on EV6 and clear it */

    while(!I2C_CheckEvent(I2C2,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

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

    I2C_SendData(I2C2, WriteAddr);

    /* Test on EV8 and clear it */

    while(! I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

    /* While there is data to be written */

    while(NumByteToWrite--)

    {

    /* Send the current byte */

    I2C_SendData(I2C2, *pBuffer);

    /* Point to the next byte to be written */

    pBuffer++;

    /* Test on EV8 and clear it */

    while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

    }

    /* Send STOP condition */

    I2C_GenerateSTOP(I2C2, ENABLE);

    }

    void I2C_Read(u8* pBuffer, u8 ReadAddr, u16 NumByteToRead)

    {

    /* While the bus is busy */

    while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY));

    /* Send START condition */

    I2C_GenerateSTART(I2C2, ENABLE);

    /* Test on EV5 and clear it */

    while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));

    /* Send EEPROM address for write */

    I2C_Send7bitAddress(I2C2, 0x5E, I2C_Direction_Transmitter);

    /* Test on EV6 and clear it */

    while(!I2C_CheckEvent(I2C2,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

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

    I2C_Cmd(I2C2, ENABLE);

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

    I2C_SendData(I2C2, ReadAddr);

    /* Test on EV8 and clear it */

    while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

    /* Send STRAT condition a second time */

    I2C_GenerateSTART(I2C2, ENABLE);

    /* Test on EV5 and clear it */

    while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));

    /* Send EEPROM address for read */

    I2C_Send7bitAddress(I2C2, 0x5E, I2C_Direction_Receiver);

    /* Test on EV6 and clear it */

    while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));

    /* While there is data to be read */

    while(NumByteToRead)

    {

    if(NumByteToRead == 1)

    {

    /* Disable Acknowledgement */

    I2C_AcknowledgeConfig(I2C2, DISABLE);

    /* Send STOP Condition */

    I2C_GenerateSTOP(I2C2, ENABLE);

    }

    /* Test on EV7 and clear it */

    if(I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED))

    {

    /* Read a byte from the EEPROM */

    *pBuffer = I2C_ReceiveData(I2C2);

    /* Point to the next location where the byte read will be saved */

    pBuffer++;

    /* Decrement the read bytes counter */

    NumByteToRead--;

    }

    }

    /* Enable Acknowledgement to be ready for another reception */

    I2C_AcknowledgeConfig(I2C2, ENABLE);

    }

    wait to your assistant

    mattb
    Associate
    May 17, 2011
    Posted on May 17, 2011 at 13:14

    There are some well known issues with the STM32 and I2C.

    Check out the errata (doc # 14732), section 2.9.1.

    Also check out this post:

    http://www.st.com/mcu/forums-cat-7652-23.html

    - Matt