Search code examples
i2cmaster-slavestm32f4ack

I2C STM32F4 (slave) no acknowledge bit responded


Hopefully anyone is able to help me with my problem.

I'm currently setting um a I2C bus with a FT2232H as master and a STM32F407VGT6 (Discovery-Board) as a slave. I was able to send the slave address correctly from the master but I'm not sure if the slave is setup correctly because I dont get any ACK bit as a response from the slave. I was looking over the settings quite some times.

The start and stop conditions are sent correctly aswell.

Here is my code for the slave setup:

        I2C_InitTypeDef i2c_init;
        NVIC_InitTypeDef NVIC_InitStructure, NVIC_InitStructure2;
        
        I2C_DeInit(I2C1);
        I2C_SoftwareResetCmd(I2C1, ENABLE);
        I2C_SoftwareResetCmd(I2C1, DISABLE);
        
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
        
        gpio_init.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8;
        gpio_init.GPIO_Mode = GPIO_Mode_AF;
        gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
        gpio_init.GPIO_PuPd = GPIO_PuPd_UP;
        gpio_init.GPIO_OType = GPIO_OType_OD;
        GPIO_Init(GPIOB, &gpio_init);
        
        GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1); // SDA
        GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_I2C1); // SCL
        
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
        
        NVIC_InitStructure.NVIC_IRQChannel = I2C1_EV_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure);
        
        NVIC_InitStructure2.NVIC_IRQChannel = I2C1_ER_IRQn;
        NVIC_InitStructure2.NVIC_IRQChannelPreemptionPriority = 0;
        NVIC_InitStructure2.NVIC_IRQChannelSubPriority = 0;
        NVIC_InitStructure2.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStructure2);
        
        I2C_ITConfig(I2C1, I2C_IT_EVT, ENABLE);
        I2C_ITConfig(I2C1, I2C_IT_ERR, ENABLE);
        I2C_ITConfig(I2C1, I2C_IT_BUF, ENABLE);
        
        i2c_init.I2C_ClockSpeed = 50000;
        i2c_init.I2C_Mode = I2C_Mode_I2C;
        i2c_init.I2C_DutyCycle = I2C_DutyCycle_2;
        i2c_init.I2C_OwnAddress1 = 0x21;
        i2c_init.I2C_Ack = I2C_Ack_Enable;
        i2c_init.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
        I2C_Init(I2C1, &i2c_init);
        
        I2C_StretchClockCmd(I2C1, ENABLE);
        I2C_Cmd(I2C1, ENABLE);

And the address I'm sending should be correct like that. Thanks for your help :)

Edit: The problem might also be that the STM32F4 isn't reading the I2C lines. But If thats the case I have no idea which setting is wrong. Or do I need to start the peripheral in any way? I thought if the PE bit is set the I2C is running and responding automatically. Isn't that the case?


Solution

  • Finally solved the problem. The default HAL Initialisation has initialized the GPIOs alternate function wrong. There were two Pins to choose for SDA and SCL lines. For SDA in the end both pins where configured as alternate function what caused to listening on wrong line or responding to the wrong one.

    Hope that helps someone else too.