0
\$\begingroup\$

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.OwnAddress1 = 0x123;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_10BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0xFF;
  hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  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.OwnAddress1 = I2C_ADDRESS;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_10BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0xFF;
  hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  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();
    }
}

And this receives:

do {
   HAL_I2C_Init(&hi2c1);
    if(HAL_I2C_Master_Receive_IT(&hi2c1, (uint16_t)I2C_ADD_CHILLER, aRxBuffer, 14)!= HAL_OK) {
        Error_Handler();
    }
    State = HAL_I2C_GetState(&hi2c1) ;
    Teor=HAL_I2C_GetError(&hi2c1);
    HAL_I2C_DeInit(&hi2c1);
  } while(Teor == HAL_I2C_ERROR_AF);
\$\endgroup\$
  • \$\begingroup\$ How are the boards connected together? I2C is not really designed to go over cables. \$\endgroup\$ – DoxyLover May 4 at 11:37
  • \$\begingroup\$ Using cables. Actually the cable is about 20cm long. Is that a problem? \$\endgroup\$ – sixter May 5 at 12:52
  • \$\begingroup\$ 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. \$\endgroup\$ – DoxyLover May 5 at 18:28
  • \$\begingroup\$ Alright, thanks. Will do that. \$\endgroup\$ – sixter May 6 at 4:05
1
\$\begingroup\$

Check you have sensible pull-up resistors on SDA and SCL.

Reducing the value of these resistors can be helpful to overcome capacitance and noise.

Also, make sure the 0V is bonded well between the boards.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.