cancel
Showing results for 
Search instead for 
Did you mean: 

I2C transmission problems

Embedded_dev1
Associate II

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?

6 REPLIES 6
Karl Yamashita
Lead II

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

If you find my answers useful, click the accept button so that way others can see the solution.

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?

 

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 you find my answers useful, click the accept button so that way others can see the solution.
Embedded_dev1
Associate II

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
Lead II

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

If you find my answers useful, click the accept button so that way others can see the solution.

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.