AnsweredAssumed Answered

STM32F107 I2C Not working

Question asked by sarwar.aftab.001 on Nov 11, 2014
Latest reply on Nov 13, 2014 by sarwar.aftab.001
Hello,

I am trying to interface a MEM on I2C of STM32F107. My code gets stuck when I read a register in the "I2C_Reg_Write" function below. It sticks at "while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));" event. My hardware connections are fine b/c I can test the same hardware with "mikroC for ARM" code.

After calling "I2C_Initialize" function, when I call  "I2C_Reg_Write" function, it gets stuck at line 091.

Please help me to resolve this issue.

Here is the I2C code. I have also attached the CoIDE project.

001.#include "main.h"
002. 
003.//#define I2C_SLAVE_ADDR    0x53
004.//#define I2C_SPEED     100000
005. 
006.void I2C_Initialize(int I2C_SLAVE_ADDR, unsigned long I2C_SPEED)
007.{
008.    GPIO_InitTypeDef        GPIO_InitStructure;
009.    I2C_InitTypeDef         I2C_InitStructure;
010. 
011.    // I2C @ PORTB. I2C_SCL=PB6, I2C_SDA=PB7.
012.    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
013. 
014.    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
015.    GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_6 |  GPIO_Pin_7;
016.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
017.    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_OD;
018.    GPIO_Init(GPIOB, &GPIO_InitStructure);
019. 
020.    // reset i2c1
021.    //RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, DISABLE);
022.    //RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
023. 
024.    I2C_DeInit(I2C1);
025. 
026.    // I2C configuration
027.    I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
028.    I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
029.    I2C_InitStructure.I2C_OwnAddress1 = 0x00;   //I2C_SLAVE_ADDR;
030.    I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
031.    I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
032.    I2C_InitStructure.I2C_ClockSpeed = I2C_SPEED;
033. 
034.    // Apply I2C configuration
035.    I2C_Init(I2C1, &I2C_InitStructure);
036.    // I2C Peripheral Enable
037.    I2C_Cmd(I2C1, ENABLE);
038.}
039. 
040.char I2C_Reg_Read(uint8_t SensorAdd, uint8_t regAdd)
041.{
042.    char I2C_Data;
043.     // Send START condition
044.     I2C_GenerateSTART(I2C1, ENABLE);
045.     // Test on EV5 and clear it
046.     while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
047.     // Send sensor address
048.     I2C_Send7bitAddress(I2C1, SensorAdd, I2C_Direction_Transmitter);
049.     // Test on EV6 and clear it
050.     while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
051.     // Send register address
052.     I2C_SendData(I2C1, regAdd);
053.     // Test on EV8 and clear it
054.     while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
055.     // Send START condition a second time
056.     I2C_GenerateSTART(I2C1, ENABLE);
057.     // Test on EV5 and clear it
058.     while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
059.     // Send Sensor address for read
060.     I2C_Send7bitAddress(I2C1, SensorAdd, I2C_Direction_Receiver);
061.     // Test on EV6 and clear it
062.     while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
063.     // While there is data to be read
064.     // Read a byte
065.     if(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED))
066.     {
067.         //putsUSART("something received..");
068. 
069.         I2C_Data = I2C_ReceiveData(I2C1);
070.         // send acknowledge
071.         //I2C_AcknowledgeConfig(I2C1, ENABLE);
072.         I2C_AcknowledgeConfig(I2C1, DISABLE);
073.         // send stop signal
074.         I2C_GenerateSTOP(I2C1, ENABLE);
075. 
076. 
077.     }
078.     else {
079.        I2C_Data = 0;
080.     }
081. 
082.     return I2C_Data;
083.}
084. 
085.void I2C_Reg_Write(uint8_t SensorAdd, uint8_t regAdd, uint16_t ByteToWrite)
086.{
087.    //while(!I2C_CheckEvent(I2C1, I2C_E));
088.    // Send START condition
089.    I2C_GenerateSTART(I2C1, ENABLE);
090.    // Test on EV5 and clear it
091.    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
092.    // Send sensor address
093.    I2C_Send7bitAddress(I2C1, SensorAdd, I2C_Direction_Transmitter);
094.    // Test on EV6 and clear it
095.    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
096.    // Send register address
097.    I2C_SendData(I2C1, regAdd);
098.    // Test on EV8 and clear it
099.    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
100.    // send reg data
101.    I2C_SendData(I2C1, ByteToWrite);
102.    // Test on EV8 and clear it
103.    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
104.    // Send STOP Condition
105.    I2C_GenerateSTOP(I2C1, ENABLE);
106.}

Attachments

Outcomes