简体   繁体   English

与 I2C 通信、STM32 和树莓派

[英]Communicate with I2C a STM32 and a Raspberry

I want to connect a Raspberry Pi and a STM32F446 via I2C.我想通过 I2C 连接 Raspberry Pi 和 STM32F446。 I want the STM to be the slave.我希望STM成为奴隶。 The code on the Raspberry is ok because I am already connected to other devices but when i search for the adress of the STM it doesn't appear.树莓上的代码没问题,因为我已经连接到其他设备,但是当我搜索 STM 的地址时,它没有出现。 I'm sure the problem is with the init bu can't find it.我确定问题出在 init 上,但找不到它。 I attach the code of the init.我附上init的代码。 Thanks in advance.提前致谢。

void I2C_Init(void){
GPIO_InitTypeDef GPIO_InitStruct;
I2C_InitTypeDef I2C_InitStruct;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C3, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOC, &GPIO_InitStruct);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_I2C3); //SCL
GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_I2C3); //SDA

I2C_InitStruct.I2C_Mode = I2C_Mode_SMBusDevice;
I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStruct.I2C_OwnAddress1 = 0x10;
I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStruct.I2C_ClockSpeed = 100000;
I2C_DeInit(I2C3);
I2C_Init(I2C3, &I2C_InitStruct);
I2C_Cmd(I2C3, ENABLE);  
}

Here's some checklist I just made up for you这是我刚刚为你准备的一些清单

  1. Check if you have set the same I2C clock speed between STM and Raspberry.检查您是否在 STM 和 Raspberry 之间设置了相同的 I2C 时钟速度。

    • Currently, your STM's I2C clock speed is 100kHz .目前,您的 STM 的 I2C 时钟速度为100kHz
    • You should check out the link I've found.你应该看看我找到的链接。
    • Raspberry Pi I2C speed check. 树莓派 I2C 速度检查。 (The post changes the speed of I2C, but all you have to do is this "sudo nano /boot/config.txt" and find "i2c_arm_baudrate" (这篇文章改变了 I2C 的速度,但你所要做的就是这个“sudo nano /boot/config.txt”并找到“i2c_arm_baudrate”
    • If it differs from STM's setup, change Raspberry or STM's setup to match one another.如果它与 STM 的设置不同,请更改 Raspberry 或 STM 的设置以使其相互匹配。
    • If changing Rasp's setup, don't forget to reboot .如果更改 Rasp 的设置,请不要忘记重新启动
  2. Check if I2C is actually switching(0 or 1 state back and forth.)检查 I2C 是否确实在切换(来回切换 0 或 1 状态。)

    • use oscilloscope or logic analyzer to see the change.使用示波器或逻辑分析仪查看变化。
    • it would be good to check while I2C pin is floating and set STM to I2C master mode and test if the I2C peripheral is working or not.最好在 I2C 引脚悬空时检查并将 STM 设置为 I2C 主模式并测试 I2C 外设是否正常工作。
  3. If it's not switching, check your pull-up resistor.如果它没有切换,请检查您的上拉电阻。

    • I know that you have set your init code to pull-up, but it is also set as an open drain.我知道您已将 init 代码设置为上拉,但它也设置为开漏。 So you are going to need an actual hardware resistor .因此,您将需要一个实际的硬件电阻器
    • Most of the time 10K Ohm pull-up will do the work.大多数情况下,10K 欧姆上拉电阻会起作用。 (Remember that both lines should have pull-up resistor) (记住两条线都要有上拉电阻)
  4. Try using example code available for Raspberry.尝试使用可用于 Raspberry 的示例代码。

This is all that I can think of right now.目前能想到的就这些。

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

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