AnsweredAssumed Answered

STM32F10x I2C Bus - No Clock and Data on Bus

Question asked by nguyen.ngoc on Sep 21, 2012
Latest reply on Dec 3, 2012 by neff.rob
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. 

#include "oneWire.h"
#include "stm32f10x_lib.h"
#include "stm32f10x_i2c.h"
#include "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);
}

Outcomes