wokwi / avr8js

Arduino (8-bit AVR) simulator, written in JavaScript and runs in the browser / Node.js
https://blog.wokwi.com/avr8js-simulate-arduino-in-javascript/
MIT License
461 stars 73 forks source link

EEPROM interrupt doesn't update when changing the EERIE bit in EECR #110

Closed urish closed 2 years ago

urish commented 2 years ago

Reproduction:

void EEPROM_write(unsigned int uiAddress, unsigned char ucData)
{
    /* 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);
    /* Enable EEPROM Ready interrupt */
    EECR |= (1<<EERIE);
}
unsigned char EEPROM_read(unsigned int uiAddress)
{
    /* Wait for completion of previous write */
    while(EECR & (1<<EEPE))
    ;
    /* Set up address register */
    EEAR = uiAddress;
    /* Start eeprom read by writing EERE */
    EECR |= (1<<EERE);
    /* Return data from Data Register */
    return EEDR;
}

bool  EEPROM_Ready = false;
void setup() 
{
  Serial.begin(115200);

  Serial.println((char)EEPROM_read(0));
  EEPROM_write(0, 'A');
  if(EEPROM_Ready == true) Serial.println("EEPROM Ready");
  Serial.println((char)EEPROM_read(0));
}

void loop() 
{
  if(EEPROM_Ready == true) Serial.println("EEPROM Ready");
}

ISR(EE_READY_vect)
{
  EEPROM_Ready = true;
  EECR &= ~(1<<EERIE);
}

In the above code, we enable the EEPROM ready interrupt after starting the write, and disable it in the interrupt handler. However, it seems like the interrupt handler is never called, as setting the EERIE bit after the fact doesn't trigger the interrupt.

urish commented 2 years ago

Source: https://wokwi.com/arduino/projects/313061110222684738

urish commented 2 years ago

Note: code in loop should be changed as follows, in order to avoid infinite print loop:

void loop() 
{
  if(EEPROM_Ready == true) {
    Serial.println("EEPROM Ready");
    EEPROM_Ready = false;
  }
}