Skip to main content
Associate III
Dec 18, 2023 6:13 PM UTC
Question

I2C transmission problems

I am trying to communicate with a PIC microcontroller using the I2C interface using a STM32F103. I have a recurring problem: everything is fine until I run this function:
static HAL_StatusTypeDef I2C_WaitOnMasterAddressFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, uint32_t Timeout, uint32_t Tickstart)
{
while (__HAL_I2C_GET_FLAG(hi2c, Flag) == RESET)
{
if (__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF) == SET)
{
/* Create stop */
SET_BIT(hi2c->Instance->CR1, I2C_CR1_STOP);

/* Clear autofocus flag */
__HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_AF);

hi2c->PreviousState = I2C_STATE_NONE;
hi2c->State = HAL_I2C_STATE_READY;
hi2c->Mode = HAL_I2C_MODE_NONE;
hi2c->Error Code |= HAL_I2C_ERROR_AF;

/* The process is unlocked */
__HAL_UNLOCK(hi2c);

return HAL_ERROR;
}

/* Check timeout */
if (Timeout != HAL_MAX_DELAY)
{
if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
{
if ((__HAL_I2C_GET_FLAG(hi2c, Flag) == RESET))
{
hi2c->PreviousState = I2C_STATE_NONE;
hi2c->State = HAL_I2C_STATE_READY;
hi2c->Mode = HAL_I2C_MODE_NONE;
hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;

/* The process is unlocked */
__HAL_UNLOCK(hi2c);

return HAL_ERROR;
}
}
}
}
return HAL_OK;
}


this function returns an error because the PIC address is invalid, when in fact the address is correct. What could be the problem?

This topic has been closed for replies.

6 replies

Karl Yamashita
Principal
Dec 18, 2023 8:11 PM UTC

Have you confirmed with an oscilloscope that the PIC actually ACK upon seeing the address?

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source
Associate III
Dec 18, 2023 10:49 PM UTC

No, I didn't review it on an oscilloscope. However, as a result of debugging, it becomes clear that there is no address match. Can this happen if the clock speeds of the master and slave are different?

 

Karl Yamashita
Principal
Dec 18, 2023 11:12 PM UTC

The STM32 is the clock master. The PIC doesn't clock as a slave device but receives the clock from the master.

You have to verify that the PIC is acknowledging with an oscilloscope. Make sure external pullup resistors are of the right values and you're not using internal pullups. Do you even know if the PIC is working correctly first of all?

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source
Associate III
Dec 18, 2023 11:22 PM UTC

Thanks for the answer! I use pull-up resistors with a value of 5.6kΩ. Where can I check if there is no pull-up with internal resistors? At the expense of the microcontroller - it works correctly.

Karl Yamashita
Principal
Dec 19, 2023 12:09 AM UTC

Did you left shift the slave address before passing the argument to the HAL_I2C_Master_Transmit?

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source
Associate III
Dec 19, 2023 12:27 AM UTC

No, I'm not shifting the slave address to the left. I just thought about such a solution before your message. Thank you very much! I will definitely try to move the address to the left.