cancel
Showing results for 
Search instead for 
Did you mean: 

I2C Between two STM32 boards not working

Malek Mahar
Associate II

0693W00000BbBMQQA3.jpg0693W00000BbBMLQA3.jpgHi,

i'm trying to send data from an stm32f446-nucleo board ( as a master ) to an stm32f469-disco board ( as a slave ) but it is not working correctly,

PS: Each board is connected separately to a USB Port from PC.

MASTER Code :

int main(void)

{

 SystemClock_Config();

 MX_GPIO_Init();

 MX_I2C1_Init();

 /* USER CODE BEGIN 2 */

 do

 {

  for(i=0; i <255; i++)

  {

   ret = HAL_I2C_Master_Transmit(&hi2c1, i, data, 1, HAL_MAX_DELAY);

  }

 }while(ret!=HAL_OK);

 while (1)

 {

 }

}

static void MX_I2C1_Init(void)

{

 hi2c1.Instance = I2C1;

 hi2c1.Init.ClockSpeed = 100000;

 hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;

 hi2c1.Init.OwnAddress1 = 0;

 hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;

 hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;

 hi2c1.Init.OwnAddress2 = 0;

 hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;

 hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;

 if (HAL_I2C_Init(&hi2c1) != HAL_OK)

 {

  Error_Handler();

 }

}

SLAVE Code :

int main(void)

{

 HAL_Init();

 SystemClock_Config();

 MX_GPIO_Init();

 MX_I2C1_Init();

 /* USER CODE BEGIN 2 */

 do

 {

   ret = HAL_I2C_Slave_Receive(&hi2c1, data_r, 1, 10000);

 }

 while(ret!=HAL_OK);

 while (1)

 { 

 }

}

static void MX_I2C1_Init(void)

{

 hi2c1.Instance = I2C1;

 hi2c1.Init.ClockSpeed = 100000;

 hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;

 hi2c1.Init.OwnAddress1 = 0;

 hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;

 hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;

 hi2c1.Init.OwnAddress2 = 0;

 hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;

 hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;

 if (HAL_I2C_Init(&hi2c1) != HAL_OK)

 {

  Error_Handler();

 }

}

THE REST IS AUTOMATICALLY GENERATED FROM CUBEMX

When debugging I get thoses results :

Master.jpg & slave.jpg

1 ACCEPTED SOLUTION

Accepted Solutions
Amel NASRI
ST Employee

Hello @Malek Mahar​ ,

Referring to what I suggested already in your other post, I prefer that you start with a ready to use example from the STM32CubeF4 firmware package.

You have following examples:

  • STM32Cube_FW_F4\Projects\STM32446E-Nucleo\Examples\I2C\I2C_TwoBoards_ComPolling: check first that "#define MASTER_BOARD" is uncommented in the main.c, compile project then run it as is
  • STM32Cube_FW_F4\Projects\STM32469I-Discovery\Examples\I2C\I2C_TwoBoards_ComPolling: for this one, "#define MASTER_BOARD" has to be commented as board will be used as slave

Try with these examples in order to understand how it works then move to your own project generated with STM32CubeMX.

-Amel

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

3 REPLIES 3
Amel NASRI
ST Employee

Hello @Malek Mahar​ ,

Referring to what I suggested already in your other post, I prefer that you start with a ready to use example from the STM32CubeF4 firmware package.

You have following examples:

  • STM32Cube_FW_F4\Projects\STM32446E-Nucleo\Examples\I2C\I2C_TwoBoards_ComPolling: check first that "#define MASTER_BOARD" is uncommented in the main.c, compile project then run it as is
  • STM32Cube_FW_F4\Projects\STM32469I-Discovery\Examples\I2C\I2C_TwoBoards_ComPolling: for this one, "#define MASTER_BOARD" has to be commented as board will be used as slave

Try with these examples in order to understand how it works then move to your own project generated with STM32CubeMX.

-Amel

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Malek Mahar
Associate II

Hello @Amel NASRI​,

Thank you for your answer,

I had a wiring error concerning SCL & SDA pins on the slave board that I have corrected and it worked perfectly.

Hi @Malek Mahar​ ,

Glad to know that you found the root cause of your issue!

-Amel

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.