0
votes

I want to connect a Raspberry Pi and a STM32F446 via I2C. I want the STM to be the slave. 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. I'm sure the problem is with the init bu can't find it. I attach the code of the 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);  
}
1
What is the output of i2cdetect?Mark Setchell
I see the other I2C but not this oneManel Vilella
I think that means your code is pretty much irrelevant then. If i2cdetect can't find it you have electrical problems, not software.Mark Setchell
But if the i2c is not well configured it can't appear, i mean if it does not have an adress (for example) it won't appear. Thank you for your fast answers by the way.Manel Vilella
Have you checked with a logic analyzer if there is any response from the ST visible? Do you have a I2C receive routine which can answear the request from the RPi?A.R.C.

1 Answers

0
votes

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.

    • Currently, your STM's I2C clock speed is 100kHz.
    • You should check out the link I've found.
    • Raspberry Pi I2C speed check. (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"
    • If it differs from STM's setup, change Raspberry or STM's setup to match one another.
    • If changing Rasp's setup, don't forget to reboot.
  2. Check if I2C is actually switching(0 or 1 state back and forth.)

    • 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.
  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. So you are going to need an actual hardware resistor.
    • Most of the time 10K Ohm pull-up will do the work. (Remember that both lines should have pull-up resistor)
  4. Try using example code available for Raspberry.

This is all that I can think of right now.