AnsweredAssumed Answered

STM32F4 I2C

Question asked by vadoud.ali on Aug 18, 2014
Hi, I want to communicate with an i2c eeprom, I am able to send the first data but not the second.
it seems that the buffer is full or there is another error somewere. Does anyone have an idea?
#include "stm32f4xx.h"
#include "stm32f4xx_i2c.h"
#include "stm32f4xx_gpio.h"
 
int i;
void gpio_init()
{
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
         GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
         GPIO_Init(GPIOG, &GPIO_InitStructure);
}
void set_I2C()
{
    GPIO_InitTypeDef GPIO_InitStructure;
    I2C_InitTypeDef I2C_InitStructure;
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    RCC_APB1PeriphClockCmd( RCC_APB1Periph_I2C1, ENABLE);
 
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9;
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
     GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
     GPIO_Init(GPIOB, &GPIO_InitStructure);
 
     GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_I2C1);
     GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);
 
     I2C_DeInit(I2C1);
     I2C_Cmd(I2C1, DISABLE);
 
     I2C_InitStructure.I2C_OwnAddress1 = 0x00;
     I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
     I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
     I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
     I2C_InitStructure.I2C_ClockSpeed = 10;
     I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
 
     I2C_Init(I2C1, &I2C_InitStructure);
     I2C_Cmd(I2C1, ENABLE);
     I2C_ITConfig(I2C1, I2C_IT_BUF, ENABLE);
     I2C_ITConfig(I2C1, I2C_IT_ERR, ENABLE);
}
 
void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction){
    // wait until I2C1 is not busy any more
    while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));
 
    // Send I2C1 START condition
    I2C_GenerateSTART(I2Cx, ENABLE);
 
    // wait for I2C1 EV5 --> Slave has acknowledged start condition
    while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
 
    // Send slave Address for write
    I2C_Send7bitAddress(I2Cx, address, direction);
 
    /* wait for I2Cx EV6, check if
     * either Slave has acknowledged Master transmitter or
     * Master receiver mode, depending on the transmission
     * direction
     */
    if(direction == I2C_Direction_Transmitter){
        while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
    }
    else if(direction == I2C_Direction_Receiver){
        while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
    }
}
 
void I2C_startlec(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction){
    // wait until I2C1 is not busy any more
    //while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));
 
    // Send I2C1 START condition
    I2C_GenerateSTART(I2Cx, ENABLE);
 
    // wait for I2C1 EV5 --> Slave has acknowledged start condition
    while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
 
    // Send slave Address for write
    I2C_Send7bitAddress(I2Cx, address, direction);
 
    /* wait for I2Cx EV6, check if
     * either Slave has acknowledged Master transmitter or
     * Master receiver mode, depending on the transmission
     * direction
     */
    if(direction == I2C_Direction_Transmitter){
        while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
    }
    else if(direction == I2C_Direction_Receiver){
        while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
    }
}
void I2C_write(I2C_TypeDef* I2Cx, uint8_t data)
{
    // wait for I2C1 EV8 --> last byte is still being transmitted (last byte in SR, buffer empty), next byte can already be written
    while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTING));
    I2C_SendData(I2Cx, data);
}
 
uint8_t I2C_read_nack(I2C_TypeDef* I2Cx){
    // disable acknowledge of received data
    // nack also generates stop condition after last byte received
    // see reference manual for more info
    I2C_AcknowledgeConfig(I2Cx, DISABLE);
    I2C_GenerateSTOP(I2Cx, ENABLE);
    // wait until one byte has been received
    while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED) );
    // read data from I2C data register and return data byte
    uint8_t data = I2C_ReceiveData(I2Cx);
    return data;
}
 
void I2C_stop(I2C_TypeDef* I2Cx){
 
    // Send I2C1 STOP Condition after last byte has been transmitted
    I2C_GenerateSTOP(I2Cx, ENABLE);
    // wait for I2C1 EV8_2 --> byte has been transmitted
    while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
 
int main(void)
{
  set_I2C();
 
  //I2C1_init(); // initialize I2C peripheral
 
    uint8_t received_data;
 
    I2C_start(I2C1, 0b10100000, I2C_Direction_Transmitter); // start a transmission in Master transmitter mode
    I2C_write(I2C1, 0b00000000);
I2C_write(I2C1, 0b00000000);
      
 
 
    I2C_stop(I2C1); // sto
     
    while(1);
 
 
 
 
     /*I2C_start(I2C1,0b10100000,I2C_Direction_Transmitter);
      I2C_write(I2C1, 0b00000000);
      I2C_write(I2C1, 0b11111111);
      I2C_write(I2C1, 0b00000000);
      I2C_stop(I2C1);
      I2C_ClearFlag( I2C1, I2C_FLAG_BUSY);
 
      I2C_start(I2C1, 0b10100000, I2C_Direction_Transmitter);
        I2C_write(I2C1, 0b00000000);
 
        I2C_startlec(I2C1, 0b10100001, I2C_Direction_Receiver);
 
 
        I2C_read_nack(I2C1);*/
 
 
}

Outcomes