Hi,
Two weeks ago I wrote this piece of c++ code to use the EEPROM on atxmega32d4 and atxmega16d4 devices. The code worked perfectly fine back then, but now the code seems to be malfunctioning in some way. The EEPROM gets cleared when I remove and connect the power to the chip. I can read the EEPROM directly after I've written it successfully, but when I cycle power I read 0xFF no matter what was written to the EEPROM before the power cycle. I've tried this on 3 devices. Is there maybe some error in my code that causes it to behave unpredictable or has anybody got another explanation for this?
Thank you in advance,
Jasper
void EEPROM_write(uint32_t addr, uint32_t data, uint8_t bytesToWrite){ int i; for(i = 0; i < bytesToWrite; i++){ while(NVM_STATUS & NVM_NVMBUSY_bm){} NVM_CMD = NVM_CMD_LOAD_EEPROM_BUFFER_gc; NVM_ADDR0 = (addr + i) & 0xFF; NVM_ADDR1 = ((addr + i) & 0x0F00) >> 8; NVM_ADDR2 = 0x00; NVM_DATA0 = ((data & (0xFF << (8 * i))) >> (8 * i)); NVM_CMD = NVM_CMD_ERASE_WRITE_EEPROM_PAGE_gc; //uint32_t tempAddr = addr << 12; //NVM_ADDR0 = (addr + i) & 0xFF; //NVM_ADDR1 = ((addr + i) & 0x0F00) >> 8; //NVM_ADDR2 = 0x00; CCP = CCP_IOREG_gc; //Trigger protection mechanism NVM_CTRLA = NVM_CMDEX_bm; } } uint32_t EEPROM_read(uint32_t addr, uint8_t bytesToRead){ uint32_t tempDataOut = 0; int i; for(i = 0; i < bytesToRead; i++){ while(NVM_STATUS & NVM_NVMBUSY_bm){} NVM_ADDR0 = (addr + i) & 0xFF; NVM_ADDR1 = ((addr + i) & 0x0F00) >> 8; NVM_ADDR2 = 0x00; NVM_CMD = NVM_CMD_READ_EEPROM_gc; CCP = CCP_IOREG_gc; //Trigger protection mechanism NVM_CTRLA = NVM_CMDEX_bm; tempDataOut |= NVM_DATA0 << (i * 8); } return tempDataOut; }