I am trying to use the STM32F4 discovery board as a master to communicate to a sensor using I2C. I am using the std periph library to do this but for some reason I am unable to get anything going through the SCL and SDA lines. From reading through the stf periph header files and other examples I have seen online it does not seem that I am doing anything wrong or forgetting a step. Below I have Included all of my I2C code:
#define I2CTransmitter 0
#define I2CReciever 1
#define I2CAckEnable 1
#define I2CAckDisable 0
void I2CInit() {
I2C_InitTypeDef I2CInitStruct;
GPIO_InitTypeDef GPIO;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOB, ENABLE);
//Configure and initialize the GPIOs
GPIO.GPIO_Pin = GPIO_Pin_6;
GPIO.GPIO_Mode = GPIO_Mode_AF;
GPIO.GPIO_Speed = GPIO_Speed_100MHz;
GPIO.GPIO_OType = GPIO_OType_OD;
GPIO.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO);
GPIO.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOB, &GPIO);
//Connect GPIO pins to peripheral
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1);
I2CInitStruct.I2C_ClockSpeed = 100000;
I2CInitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2CInitStruct.I2C_Mode = I2C_Mode_I2C;
I2CInitStruct.I2C_OwnAddress1 = 0x00;
I2CInitStruct.I2C_Ack = I2C_Ack_Enable;
I2CInitStruct.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_Init(I2C1, &I2CInitStruct);
I2C_Cmd(I2C1, ENABLE);
}
uint8_t I2CRead(uint8_t address, uint8_t reg) {
uint8_t data;
I2CStart(address, I2CTransmitter, I2CAckEnable);
I2CWriteData(reg);
I2CStart(address, I2CReciever, I2CAckDisable);
data = I2CReadNAck();
I2CStop();
return data;
}
void I2CWrite(uint8_t address, uint8_t reg, uint8_t data) {
I2CStart(address, I2CTransmitter, I2CAckEnable);
I2CWriteData(reg);
I2CWriteData(data);
I2CStop();
}
void I2CStart(uint8_t address, uint8_t direction, uint8_t ack) {
I2C_GenerateSTART(I2C1, ENABLE);
I2C_Send7bitAddress(I2C1, address, direction);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_SLAVE_BYTE_RECEIVED));
}
void I2CStop() {
I2C_GenerateSTOP(I2C1, ENABLE);
}
void I2CWriteData(uint8_t data) {
I2C_SendData(I2C1, data);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_SLAVE_BYTE_RECEIVED));
}
uint8_t I2CReadAck() {
uint8_t data;
I2C_AcknowledgeConfig(I2C1, ENABLE);
while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED) );
data = I2C_ReceiveData(I2C1);
return data;
}
uint8_t I2CReadNAck() {
uint8_t data;
I2C_AcknowledgeConfig(I2C1, DISABLE);
I2C_GenerateSTOP(I2C1, ENABLE);
while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED) );
data = I2C_ReceiveData(I2C1);
return data;
}
The code is used in the main function by simply calling I2CInit() and then calling I2CWrite() in a while loop for testing. Thank you in advance for any help!