0

I'm currently using two STM32 devices to communicate via I²C.

Communication is not working at the moment, and I don't have any idea why.

This is my master initialize code, generated from STM32CubeMX and modified a little:

static void MX_I2C2_Init(void)
{
    hi2c2.Instance = I2C2;
    hi2c2.Init.Timing = 0x00303D5B;
    hi2c2.Init.OwnAddress1 = 0;
    hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
    hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
    hi2c2.Init.OwnAddress2 = 0;
    hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
    hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
    hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
    HAL_I2C_Init(&hi2c2);

    if (HAL_I2C_Init(&hi2c2) != HAL_OK)
    {
        Error_Handler();
    }

    /** Configure Analogue filter
    */
    if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
    {
        Error_Handler();
    }

    /** Configure Digital filter
    */
    if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK)
    {
        Error_Handler();
    }
}

static void MX_GPIO_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    /* GPIO Ports Clock Enable */
    __HAL_RCC_GPIOB_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();
    __HAL_RCC_I2C2_CLK_ENABLE();

    /* Configure GPIO pins: PB7 PB6 */
    GPIO_InitStruct.Pin = GPIO_PIN_11;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF6_I2C2;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* Configure GPIO pins: PA2 PA3 */
    GPIO_InitStruct.Pin = GPIO_PIN_12;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF6_I2C2;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    if (HAL_I2C_Init(&hi2c2) != HAL_OK) {
        cpu_ErrHandler();
    }

    /** Configure Analogue filter
     */
    if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE)
            != HAL_OK) {
        cpu_ErrHandler();
    }
    /** Configure Digital filter
     */
    if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK) {
        cpu_ErrHandler();
    }
    //HAL_I2C_Slave_Receive_IT(&hi2c2, (uint8_t *) &i2c_data, 1);
}

void cpu_ErrHandler(void)
{
  /* USER CODE BEGIN cpu_ErrHandler_Debug */
  /* User can add his own implementation to report the HAL error return state */

  /* USER CODE END cpu_ErrHandler_Debug */
}

void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

And this is my slave code:

static void MX_I2C2_Init(void)
{
    /* USER CODE BEGIN I2C2_Init 0 */

    /* USER CODE END I2C2_Init 0 */

    /* USER CODE BEGIN I2C2_Init 1 */

    /* USER CODE END I2C2_Init 1 */
    hi2c2.Instance = I2C2;
    hi2c2.Init.Timing = 0x00303D5B;
    hi2c2.Init.OwnAddress1 = 96;
    hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
    hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
    hi2c2.Init.OwnAddress2 = 0;
    hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
    hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
    hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
    HAL_I2C_Init(&hi2c2);

    if (HAL_I2C_Init(&hi2c2) != HAL_OK)
    {
        Error_Handler();
    }

    /** Configure Analogue filter
    */
    if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
    {
        Error_Handler();
    }

    /** Configure Digital filter
    */
    if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK)
    {
        Error_Handler();
    }
}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    /* GPIO Ports Clock Enable */
    __HAL_RCC_GPIOB_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();
    //__HAL_RCC_I2C2_CLK_ENABLE();

    /* Configure GPIO pins: PB7 PB6 */
    GPIO_InitStruct.Pin = GPIO_PIN_11;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF6_I2C2;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* Configure GPIO pins: PA2 PA3 */
    GPIO_InitStruct.Pin = GPIO_PIN_12;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF6_I2C2;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    if (HAL_I2C_Init(&hi2c2) != HAL_OK) {
        cpu_ErrHandler();
    }

    /** Configure Analogue filter
     */
    if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE)
            != HAL_OK) {
        cpu_ErrHandler();
    }
    /** Configure Digital filter
     */
    if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK) {
        cpu_ErrHandler();
    }
    //HAL_I2C_Slave_Receive_IT(&hi2c2, (uint8_t *) &i2c_data, 1);
}

void cpu_ErrHandler(void)
{
    /* USER CODE BEGIN cpu_ErrHandler_Debug */
    /* User can add his own implementation to report the HAL error return state */

    /* USER CODE END cpu_ErrHandler_Debug */
}

void Error_Handler(void)
{
    /* USER CODE BEGIN Error_Handler_Debug */
    /* User can add his own implementation to report the HAL error return state */
    __disable_irq();
    while (1)
    {
    }
    /* USER CODE END Error_Handler_Debug */
}

The pin number changed according to my device.

This is my slave receive code:

void user_main(void)
{
    HAL_StatusTypeDef ret;
    ret = HAL_I2C_Slave_Receive(&hi2c2, (uint8_t *) UartRxMemory01, sizeof(UartRxMemory01), 100);
    cpu_delay_ms(100);
    if(ret != HAL_OK) {
        char i2c_fail[] = "fail";
        DCPU_TX_EN(); // Get sensor value
        #if 1
            if(HAL_UART_Transmit(&rcpu_huart2, (uint8_t *) i2c_fail, strlen(i2c_fail), 100) == HAL_OK) {
                DCPU_RX_EN();
                HAL_UART_Receive(&rcpu_huart2, (uint8_t *) RxIntBuffer, sizeof(RxIntBuffer), 100);
            }
        #endif
    }
    else {
        if(strstr((const char*)UartRxMemory01, "OK") != NULL) {
            char i2c[] = "i2c succeed";
            DCPU_TX_EN(); // Get sensor value
            #if 1
                if(HAL_UART_Transmit(&rcpu_huart2, (uint8_t *) i2c, strlen(i2c), 100) == HAL_OK) {
                    DCPU_RX_EN();
                    HAL_UART_Receive(&rcpu_huart2, (uint8_t *) RxIntBuffer, sizeof(RxIntBuffer), 100);
                }
            #endif
        } else {
            char got_smth[] = "received something";
            DCPU_TX_EN(); // Get sensor value
            #if 1
                if(HAL_UART_Transmit(&rcpu_huart2, (uint8_t *) got_smth, strlen(got_smth), 100) == HAL_OK) {
                    DCPU_RX_EN();
                    HAL_UART_Receive(&rcpu_huart2, (uint8_t *) RxIntBuffer, sizeof(RxIntBuffer), 100);
                }
            #endif
        }
    }
}

And this is my master transmit code:


void user_main(void)
{
    char i2c_ok[5];
    i2c_ok[0] = 0x0a;
    strcat(i2c_ok, "OK");
    i2c_ok[4] = 0x0d;
    HAL_StatusTypeDef ret;
    ret = HAL_I2C_Master_Transmit(&hi2c2, 0x20 << 1, (uint8_t*)i2c_ok, sizeof(i2c_ok), 100);

    if(ret != HAL_OK) {
        cpu_delay_ms(100);
    }
    else {
        cpu_delay_ms(1000);
    }
}

When I start the communication, the master sends the address and write bit, and gets NACK as return and the communication is over.

i2c_waveform

Which or what went wrong?

3
  • There is something fishy about slave address. In the slave setup, you set slave address to 96 (please don't set I2C address in decimal, it hurts brain. It's 0x60 HEX or 110 0000 binary). In your waveform you're addressing device 011 0000, so you get a NACK. Could that be the issue?
    – Ilya
    Commented Aug 5, 2022 at 8:40
  • the address i wrote up there is a mistake, i have tried several address to check on slave address, [master transmit to (0x30 << 1), slave receive on (0x30)], [master transmit to (0x60), slave receive on (0x60)], [master transmit to (0x30 << 1), slave receive on (0x60)], [master transmit to (0x60), slave receive on(0x30 << 1)], every one of them give the same result, so i was wondering if it is slave addressing problem or something else
    – bnthan
    Commented Aug 5, 2022 at 9:02
  • In slave code, in user_main(), you immediately call HAL I2C receive function, but you give it 100ms timeout. Meaning after 100ms it will stop receiving and proceed with further code (even if it received nothing until that point). It will not wait to receive something indefinitely. Could it be you send something from master to slave, when slave is not actually waiting for data?
    – Ilya
    Commented Aug 5, 2022 at 9:09

1 Answer 1

-1

This is most likely an issue with the configuration of the peripheral or the hardware. Check that the clock frequency is what you expect (100kHz is typical for I2C) using an oscilloscope.

While you are there, check that the signal has nice fast edges too. Forgetting to add external pull-up resistors is a common cause of this problem. 2k2 resistors are usually fine.

You may find you get more response from the electronics stack exchange.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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