AnsweredAssumed Answered

STM32F4 I2C Issue?

Question asked by plasmaphase on May 23, 2012
Latest reply on Aug 8, 2012 by plasmaphase
At a high level, I'm able to get I2C on the STM32F4-Discovery board working with our part (MAX5418), but there's one problem I'm having, and that is that I have to write commands twice in order for them to be received.  Here's my code:

GPIO_InitTypeDef GPIO_InitStructure;
  I2C_InitTypeDef  I2C_InitStructure;

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  /* GPIOB Periph clock enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

  /* Configure I2C1 pins: SCL and SDA ----------------------------------------*/
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource6,GPIO_AF_I2C1);
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource9,GPIO_AF_I2C1);   

  /* I2C1 Periph clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
  /* I2C1 configuration ------------------------------------------------------*/
  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
  I2C_InitStructure.I2C_OwnAddress1 = 0x00;
  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  I2C_InitStructure.I2C_ClockSpeed = ClockSpeed;
  /* Enable the I2C peripheral */
  I2C_Cmd(I2C1, ENABLE);
  I2C_Init(I2C1, &I2C_InitStructure);


void I2C_WriteBuffer(uint8_t device_addr, uint8_t* pbuffer, uint8_t length)
    /* Send START condition */
    I2C_GenerateSTART(I2C1, ENABLE);

    /* Test on EV5 and clear it */
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));  

    /* Send EEPROM address for write */
    I2C_Send7bitAddress(I2C1, device_addr, I2C_Direction_Transmitter);

    /* Test on EV6 and clear it */

        /* Send the byte to be written */
        I2C_SendData(I2C1, *(pbuffer++)); 

        /* Test on EV8 and clear it */
        while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
    /* Send STOP condition */
    I2C_GenerateSTOP(I2C1, ENABLE);


If  I call the I2C_WriteBuffer() function twice the chip accepts the data, but if only once, nothing happens.  This could also be an issue with the chip as well, but wanted to remove the possibility of it being code.  Could it be something wrong with the way I leave the I2C bus (stop bit wrong, I2C disabling, etc..)?