0

I'm programming the Atmega64M1. The access on the EEPROM is described in the datasheet, and I have no issues writing it or reading it. In the datasheet it is said to wait for completing of previous write before reading or writing the eeprom :

{
/* Wait for completion of previous write */
while(EECR & (1<<EEPE))
;
/* Set up address and Data Registers */
EEAR = uiAddress;
EEDR = ucData;
/* Write logical one to EEMPE */
EECR |= (1<<EEMPE);
/* Start eeprom write by setting EEPE */
EECR |= (1<<EEPE);
} 

My question is : is there a chance that the while loop doesn't stop ? The EEPE bit is supposed to be cleared by hardware, but is it a good practice to add a software verification to make sure that we eventually exit the loop ? Something like:

int i = 0;
/* Wait for completion of previous write */
while((EECR & (1<<EEPE)) && (i < 100))
{
 i++;
}
Lucas
  • 65
  • 9
  • Depends on what you plan to do if a write fails. Printing an error can be missed. Hanging in a loop is an indication of bad hardware. – stark Nov 05 '20 at 14:13
  • I guess if EEPE bit isn't anymore cleared by hardware, because of a hardware malfunction, you can throw your Atmega64M1 into the trash can. PS If you want to verify for a possible hardware malfunction, you might want to use a timer, which creates an interrupt when it's not terminated within time. This interrupt may be used as an error diagnostic tool. Provided that the timer hardware still works. – paladin Nov 05 '20 at 14:14
  • Are you asking theoretically (I totally am in your camp of covering even remotely possible malfunctions for e.g. embedded systems)? Or have you observed an endless loop and try to trace the root cause? – Yunnosch Nov 05 '20 at 14:15
  • I am asking theoretically, it is indeed for embedded systems and an infinite loop might have critical concequences. I did not observed such endless loop but I want to make sure that I won't. @paladin Is there a possibility where only the EECR register is malfunctioning but the rest would be just fine ? – Lucas Nov 05 '20 at 14:22
  • @Lucas theoretically, the most obvious reason why the EECR register might "malfunction" is, you've reached the maximum amount of EEPROM writes, round about 100.000 writes per address, as far as I can remember. I'm to long out of ATmega, buy maybe there was also some kind of "error register" or something else, which you might check too. Read the data-sheet ;-) – paladin Nov 05 '20 at 14:49
  • The same code is suggested before reading from EEPROM. And I think that from time to time I run into exactly this problem that the Bootloader hangs in this loop infinitely (the first thing it does is checking for a certain value in EEPROM which indicates if a valid application has been installed). So: Yes, I think you can end up in an infinite loop there but other than replacing the MCU I have no workaround. – kruemi Jan 04 '22 at 12:45

0 Answers0