cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F10x I2C Bus - No Clock and Data on Bus

ngoclt
Associate
Posted on September 21, 2012 at 20:25

Hi All,

I am seeking help on why I2C2 bus is not communicating. I can see a start condition and then CLK is held low while DATA is held high. Any guidance would be appreciated. Thanks. 

&sharpinclude ''oneWire.h''

&sharpinclude ''stm32f10x_lib.h''

&sharpinclude ''stm32f10x_i2c.h''

&sharpinclude ''stm32f10x_rcc.h''

void DS2482_Init(void)

{

        GPIO_InitTypeDef    GPIO_InitStructure;

        I2C_InitTypeDef     I2C_InitStructure;

        

        I2C_DeInit(DS2482_I2C);

        

        /* Enable peripheral clock */

        RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);

        /* Enable GPIOB clock */

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

        /* Enable AFIO clock */

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

     

        /* Configure I2C2_SCL and I2C2_SDA */

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;

        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;

        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

        GPIO_Init(GPIOB, &GPIO_InitStructure);

        /* DS2483 peripheral configuration */

        I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; 

        I2C_InitStructure.I2C_DutyCycle = I2C_DUTYCYCLE;

        I2C_InitStructure.I2C_OwnAddress1 = DS2482_I2C_ADDRESS;

        I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;

        I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

        I2C_InitStructure.I2C_ClockSpeed = DS2482_I2C_SPEED;

        /* enable the I2C peripheral */

        I2C_Cmd(DS2482_I2C, ENABLE);

        I2C_Init(DS2482_I2C, &I2C_InitStructure);

       

}

int DS2482_channel_select(int channel)

{

    // select the 1-wire channel on DS2482-800

    // Returns: TRUE if channel selected

    //          FALSE if device not detected

    unsigned char ch, ch_read, check;

    /* while the bus is busy */

    while(I2C_GetFlagStatus(DS2482_I2C, I2C_FLAG_BUSY));

    

    I2C_GenerateSTART(DS2482_I2C, ENABLE);

    

    while(!I2C_CheckEvent(DS2482_I2C, I2C_EVENT_MASTER_MODE_SELECT));

    

    I2C_AcknowledgeConfig(DS2482_I2C, ENABLE);

    //I2C_Send7bitAddress(DS2482_I2C, DS2482_I2C_ADDRESS, I2C_Direction_Transmitter);

    I2C_Send7bitAddress(DS2482_I2C, 0x30, I2C_Direction_Transmitter);

    

    while(!I2C_CheckEvent(DS2482_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

          

    I2C_SendData(DS2482_I2C, DS2482_CHSL);

    switch (channel) {

    default: case 0: ch = 0xF0; ch_read = 0xB8; break;

    case 1: ch = 0xE1; ch_read = 0xB1; break;

    case 2: ch = 0xD2; ch_read = 0xAA; break;

    case 3: ch = 0xC3; ch_read = 0xA3; break;

    case 4: ch = 0xD4; ch_read = 0x9C; break;

    case 5: ch = 0xE5; ch_read = 0x95; break;

    case 6: ch = 0x96; ch_read = 0x8E; break;

    case 7: ch = 0x87; ch_read = 0x87; break;

    };

    I2C_SendData(DS2482_I2C, ch);

    // send START condition a second time

    I2C_GenerateSTART(DS2482_I2C, ENABLE);

    // send DS2482 address for read 

    I2C_Send7bitAddress(DS2482_I2C, DS2482_I2C_ADDRESS, I2C_Direction_Receiver);

    check = I2C_ReceiveData(DS2482_I2C);

   

    // send STOP condition

    I2C_GenerateSTOP(DS2482_I2C, ENABLE);

  

    return (check == ch_read);

}

#stm32f10x
4 REPLIES 4
rob239955_stm1
Associate II
Posted on November 29, 2012 at 20:45

Hopefully you've resolved this by now, but this might help the next person to come along.  I ran into a similar problem on my STM32F100C8, and I narrowed it down to the AFIO being enabled.  What I had to do was enable the AFIO, do my remapping, then disable the AFIO periphery. Then I set up the I2C and it works fine, and it appears the remapping persists.  I don't have a lot of experience on this yet, but it looks to be a viable solution.

Question is, why does enabling the AFIO periphery mess with I2C?  I tried to disable the remap of the I2C, thinking it somehow remapped it automatically, but that didn't seem to be the case.  Nothing in the documentation jumped out at me.

Posted on November 29, 2012 at 21:10

You have to enable the AFIO clock to do the remapping because that's where the mapping register/latches are situated, are you saying the AFIO clock must then be disabled before the I2C will work?

The OP isn't remapping the pins.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
rob239955_stm1
Associate II
Posted on November 29, 2012 at 21:51

That is correct.  The actual remapping call did not affect I2C, but enabling the AFIO peripheral did.  (I wasn't remapping anything for the I2C, but disabling JTAG as we use 2-wire SWD only and needed to repurpose the JTAG lines.  This came up as a side-effect).

As a side note, the AFIO doesn't show up as a peripheral in the block diagram or clock tree in the RM, so it took me a little while to figure out it needed to be enabled before the remap command would be effective.

rob239955_stm1
Associate II
Posted on December 03, 2012 at 17:45

Update:

That's apparently only during I2C initialization.  After the I2C is up, I added some more code and enable the AFIO again, and this time I leave it enabled and everything is working.

Again, this is the STM32F100C8T6B.  I don't see an obvious date code on the chip.