Olimex Support Forum

ARM => ST => Topic started by: Mic71 on September 10, 2013, 02:02:59 AM

Title: I2C1 E407
Post by: Mic71 on September 10, 2013, 02:02:59 AM
Hi All, I'm trying the I2C1 with E407 on UTEX SCL PB8 and SDA PB9. I found a problem. When I send  I2C_Send7bitAddress(I2C1, MEM_DEVICE_WRITE_ADDR, I2C_Direction_Transmitter);

the is code stuck on this instruction

while(!I2C_CheckEvent(I2C1,
I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))

The code in ok on other STM32F4 Board .

Can I have a help?

Thanks

Mik

Thanks

Mik
Title: Re: I2C1 E407
Post by: LubOlimex on September 10, 2013, 04:16:01 PM
Hey Mik,

Are you sure the address of your I2C device is the one used in the code? Are you sure it sends ACK back? If your device doesn't send acknowledge packet the code might be stuck there.

Best regards,
Lub/OLIMEX
Title: Re: I2C1 E407
Post by: Mic71 on September 10, 2013, 05:23:21 PM
Thanks Lub, Yes I did. This my code.

void I2C1_Config(void)
{         
        I2C_DeInit(I2C1); 

   // pin configuration of I2C2
        GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_I2C1);   // I2C1_SCL
   GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);   // I2C1_SDA

        // Enable GPIO clock
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,  DISABLE );
   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE );
        RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_GPIOB, DISABLE);

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
       
        /* Enable I2C2 clock */
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);               
        /* Reset I2C1 IP */
        RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
        /* Release reset signal of I2C1 IP */
        RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);

   // I2C1 init
   I2C_InitStructure.I2C_ClockSpeed = 100000;   //100 KHz
   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;

   // Enable I2C2
   I2C_Cmd(I2C1, ENABLE);

   // Apply I2C2 configuration after enabling it
   I2C_Init(I2C1, &I2C_InitStructure);         
        I2C_AcknowledgeConfig(I2C1, ENABLE);     
}

int RTCRead_I2C1( /*STRUCT_RTC *ptr_struct_rtc*/  )
{
        I2C_GenerateSTART(I2C1, ENABLE);
        while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

        /* Send the address write*/
        I2C1->DR = 0xD0;       

   /* stuck it */
   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
        {
               
      if(I2C_GetFlagStatus(I2C1, I2C_FLAG_AF))
                {   
                        /* Acknowledge failure */
                     I2C_GenerateSTOP(I2C1, ENABLE);
         return -1;
      }
   }

   I2C_SendData(I2C1, 0x0);
   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTING));
   I2C_GenerateSTOP(I2C1, ENABLE);

        I2C_GenerateSTART(I2C1, ENABLE);
   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));   // attendi l'evento
       
        /* Send the address read*/
        I2C1->DR = 0xD1;

   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
        {
      if(I2C_GetFlagStatus(I2C1, I2C_FLAG_AF))
                {   
                        /* Acknowledge failure */
                     I2C_GenerateSTOP(I2C1, ENABLE);
         return -1;
      }
   }   
   
        received_data_bcd[0] = I2C_read_ack(I2C1); // read one byte and request another byte
        received_data_bcd[1] = I2C_read_ack(I2C1); // read one byte and request another byte
        received_data_bcd[2] = I2C_read_ack(I2C1); // read one byte and request another byte
        received_data_bcd[3] = I2C_read_ack(I2C1); // read one byte and request another byte
        received_data_bcd[4] = I2C_read_ack(I2C1); // read one byte and request another byte
        received_data_bcd[5] = I2C_read_ack(I2C1); // read one byte and request another byte
   received_data_bcd[6] = I2C_read_nack(I2C1); // read one byte and don't request another byte       
       
   I2C_GenerateSTOP(I2C1, ENABLE ); // stop the transmission       
        return 1;
}

Thanks for support