Manel Vilella
Manel Vilella

Reputation: 31

Communicate with I2C a STM32 and a Raspberry

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);  
}

Upvotes: 1

Views: 2589

Answers (1)

Heeyos
Heeyos

Reputation: 21

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.

Upvotes: 1

Related Questions