简体   繁体   English

I2C STM32F4(从)无应答位响应

[英]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.我目前正在设置一个 I2C 总线,其中 FT2232H 作为主机,STM32F407VGT6(Discovery-Board)作为从机。 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.我能够从主机正确发送从机地址,但我不确定从机是否设置正确,因为我没有收到任何 ACK 位作为从机的响应。 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.编辑:问题也可能是 STM32F4 没有读取 I2C 线路。 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.我想如果设置了 PE 位,I2C 就会自动运行并响应。 Isn't that the case?不是这样吗?

Finally solved the problem.终于解决了问题。 The default HAL Initialisation has initialized the GPIOs alternate function wrong.默认 HAL 初始化已将 GPIO 备用 function 初始化错误。 There were two Pins to choose for SDA and SCL lines. SDA 和 SCL 线有两个引脚可供选择。 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.对于最后的 SDA,两个引脚都配置为备用 function 导致监听错误线路或响应错误线路的原因。

Hope that helps someone else too.希望对其他人也有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM