# Problem getting board to board I2C communication to work on STM32

I have two boards working on STM32F030R8 and F030C6 respectively. I have been trying to get the boards communicate with each other using I2C but so far, no good. One of my boards run at 48MHz and the other at 20Mhz. I have configured I2C normal mode with 100Khz and 100ns of rise and fall time. I've tried using both DMA and ITs. While using DMA, my master board was simultaneously trying to receive and transmit so my code logic was not working as expected. But then I switched to interrupts and I was able to get it work. But the problem is, after power on, it communicates just ones and then it goes into HAL_I2C_STATE_BUSY. Then I have to turn the board on and off for it to work again. Even resetting the MCU proved futile.

I2C Init code for Master:

  hi2c1.Instance = I2C1;
hi2c1.Init.Timing = I2C_TIMING;//check in .h file
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}


for Slave:

  hi2c1.Instance = I2C1;
hi2c1.Init.Timing = 0x00101D2D;//0x00201D2B;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}


This is the code that transmits:

if(HAL_I2C_GetState(&hi2c1) == HAL_I2C_STATE_READY) {
if(HAL_I2C_Slave_Transmit_IT(&hi2c1, (uint8_t*)aTxBuffer, 10)!= HAL_OK) {
/* Transfer error in reception process */
Error_Handler();
}
}


do {
HAL_I2C_Init(&hi2c1);
Error_Handler();
}
State = HAL_I2C_GetState(&hi2c1) ;
Teor=HAL_I2C_GetError(&hi2c1);
HAL_I2C_DeInit(&hi2c1);
} while(Teor == HAL_I2C_ERROR_AF);

• How are the boards connected together? I2C is not really designed to go over cables. – DoxyLover May 4 at 11:37
• Using cables. Actually the cable is about 20cm long. Is that a problem? – sixter May 5 at 12:52
• Very likely. Those cables are adding a lot of capacitance to the lines. Try using a short cable to see if it makes a difference. – DoxyLover May 5 at 18:28
• Alright, thanks. Will do that. – sixter May 6 at 4:05