2023-12-18 10:13 AM - last edited on 2023-12-18 10:35 AM by STTwo-32
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?
2023-12-18 12:11 PM
Have you confirmed with an oscilloscope that the PIC actually ACK upon seeing the address?
2023-12-18 02:49 PM
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?
2023-12-18 03:12 PM
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?
2023-12-18 03:22 PM
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.
2023-12-18 04:09 PM
Did you left shift the slave address before passing the argument to the HAL_I2C_Master_Transmit?
2023-12-18 04:27 PM
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.