2014-11-11 05:54 AM
Hello,
I am trying to interface a MEM on I2C of STM32F 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 Please help me to resolve this issue. Here is the I2C code. I have also attached the CoIDE project.
#include ''main.h''
//#define I2C_SLAVE_ADDR 0x53
//#define I2C_SPEED 100000
void I2C_Initialize(int I2C_SLAVE_ADDR, unsigned long I2C_SPEED)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
// I2C @ PORTB. I2C_SCL=PB6, I2C_SDA=PB7.
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// reset i2c1
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, DISABLE);
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
I2C_DeInit(I2C1);
// I2C configuration
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x00; //I2C_SLAVE_ADDR;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = I2C_SPEED;
// Apply I2C configuration
I2C_Init(I2C1, &I2C_InitStructure);
// I2C Peripheral Enable
I2C_Cmd(I2C1, ENABLE);
}
char I2C_Reg_Read(uint8_t SensorAdd, uint8_t regAdd)
{
char I2C_Data;
// Send START condition
I2C_GenerateSTART(I2C1, ENABLE);
// Test on EV5 and clear it
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
// Send sensor address
I2C_Send7bitAddress(I2C1, SensorAdd, I2C_Direction_Transmitter);
// Test on EV6 and clear it
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
// Send register address
I2C_SendData(I2C1, regAdd);
// Test on EV8 and clear it
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
// Send START condition a second time
I2C_GenerateSTART(I2C1, ENABLE);
// Test on EV5 and clear it
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
// Send Sensor address for read
I2C_Send7bitAddress(I2C1, SensorAdd, I2C_Direction_Receiver);
// Test on EV6 and clear it
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
// While there is data to be read
// Read a byte
if(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED))
{
//putsUSART(''something received..'');
I2C_Data = I2C_ReceiveData(I2C1);
// send acknowledge
//I2C_AcknowledgeConfig(I2C1, ENABLE);
I2C_AcknowledgeConfig(I2C1, DISABLE);
// send stop signal
I2C_GenerateSTOP(I2C1, ENABLE);
}
else {
I2C_Data = 0;
}
return I2C_Data;
}
void I2C_Reg_Write(uint8_t SensorAdd, uint8_t regAdd, uint16_t ByteToWrite)
{
//while(!I2C_CheckEvent(I2C1, I2C_E));
// Send START condition
I2C_GenerateSTART(I2C1, ENABLE);
// Test on EV5 and clear it
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
// Send sensor address
I2C_Send7bitAddress(I2C1, SensorAdd, I2C_Direction_Transmitter);
// Test on EV6 and clear it
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
// Send register address
I2C_SendData(I2C1, regAdd);
// Test on EV8 and clear it
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
// send reg data
I2C_SendData(I2C1, ByteToWrite);
// Test on EV8 and clear it
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
// Send STOP Condition
I2C_GenerateSTOP(I2C1, ENABLE);
}
#i2c #stm32f017
2014-11-12 07:25 AM
What is about the I2C flags?
-Mayla-To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2014-11-12 09:11 PM
Mayla,
What do you mean by that? Please elaborate what specific information do I need to provide here.