cancel
Showing results for 
Search instead for 
Did you mean: 

Read/Write to external EEPROM fails sometimes .What might be issue ?

SA.17
Associate III

Hi,

I am using STM32L07XXX

I am reading/writing and external eeprom using I2C

" but I2C communication fails many time ". and system hangs

Does this function run needs to be atomic ?

But using __disable_irq and __enable_irq effects HAL_GetTick() ?

Can i enable only HAL_GetTick() ?

Or any other issue causing this ?

Following is code for writing to eeprom:

void write_eeprom_float(int mem_addr,float pdata, int size_write)
{
	uint32_t time_start ; 
	time_start = HAL_GetTick();
		while (HAL_I2C_GetState(&hi2c3) != HAL_I2C_STATE_READY)
    {
			if((HAL_GetTick() - time_start) > 500)
					break;
    }
		
		 /* Check if the EEPROM is ready for a new operation */
		time_start = HAL_GetTick();
    while (HAL_I2C_IsDeviceReady(&hi2c3, EEPROM_ADDRESS, EEPROM_MAX_TRIALS, I2C_XFER_TIMEOUT_MAX) == HAL_TIMEOUT)
		{
			if((HAL_GetTick() - time_start) > 500)
					break;
		}
 
    /* Wait for the end of the transfer */
		time_start = HAL_GetTick();
    while (HAL_I2C_GetState(&hi2c3) != HAL_I2C_STATE_READY)
    {
					if((HAL_GetTick() - time_start) > 500)
					break;
    }
	
	   if(HAL_I2C_Mem_Write(&hi2c3 , (uint16_t)EEPROM_ADDRESS,(uint16_t)mem_addr , I2C_MEMADD_SIZE_8BIT, (uint8_t *)&pdata, sizeof(pdata),1000)!= HAL_OK)
    {
			error_number = I2C_controller_EEPROM ;
      /* Writing process Error */
			
			
					Error_Handler();
			
    }
 
	time_start = HAL_GetTick();
		while (HAL_I2C_GetState(&hi2c3) != HAL_I2C_STATE_READY)
    {
			if((HAL_GetTick() - time_start) > 500)
					break;
    }
		
		 /* Check if the EEPROM is ready for a new operation */
		time_start = HAL_GetTick();
    while (HAL_I2C_IsDeviceReady(&hi2c3, EEPROM_ADDRESS, EEPROM_MAX_TRIALS, I2C_XFER_TIMEOUT_MAX) == HAL_TIMEOUT)
		{
			if((HAL_GetTick() - time_start) > 500)
					break;
		}
 
    /* Wait for the end of the transfer */
		time_start = HAL_GetTick();
    while (HAL_I2C_GetState(&hi2c3) != HAL_I2C_STATE_READY)
    {
					if((HAL_GetTick() - time_start) > 500)
					break;
    }
	
 
}

3 REPLIES 3

>>Does this function run needs to be atomic.

Do you have multiple threads, or callbacks attempting to use the I2C bus?

You should arbitrate ownership via a mutex or semaphore, so each operation to each device can complete without interference.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

No multiple threads and no callbacks

TDK
Guru

If you're only using blocking functions, the state should always be HAL_I2C_STATE_READY.

If HAL_I2C_IsDeviceReady returns anything other than HAL_OK, even a single time, the chip is misbehaving. Are there external pullups on the I2C lines?

When it fails, what fails, and what is the return value from the HAL function?

If you feel a post has answered your question, please click "Accept as Solution".