lpcware

problems writing to internal EEPROM

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by Alex on Fri Jan 25 02:42:11 MST 2013
Hi,

I started software development for the LPC4337 a few days ago. Currently I'm trying to write a driver for the internal EEPROM.

Unfortunately I'm not able to store data in the EEPROM.

Hopefully one of you can help.

Here is the code I've written:

<code>
//class constructor setting wait states and clock divider. The CPU runs at 200 MHz (MCK = 200000000)
CEEPAccess::CEEPAccess(void)
{
  EE_CLKDIV = (MCK / 1500000) - 1; //set clock divider as required for EEPROM usage
  EE_INTENCLR = (1 << 2);   //clear interrupt enable bit
  EE_AUTOPROG = 0;
  EE_PWRDWN = 0;
  EE_RWSTATE = (7 << 8) | 14; //8 waitstates in RPHASE 1 and 15 waitstates in RPHASE 2
  EE_WSTATE = (4 << 16) | (8 << 8) | 2; //5 waitstates in Phase 1, 9 waitstates in Phase 2 and 3 waitstates in Phase 3
}

//adr -> pointer to destination, data -> pointer to source, len -> number of bytes to copy
__ramfunc MEM_ERR CEEPAccess::WriteMemory(unsigned char* adr, unsigned int len, unsigned char* data)
{
  if((reinterpret_cast<int>(adr) < EEP_START_ADDRESS) || ((reinterpret_cast<int>(adr) + len) >= (EEP_START_ADDRESS + EEP_SIZE_BYTES)))
    return MEM_ERR_ADDRESS_ERR;
  do
  {
    *adr++ = *data++;   //copy from source buffer to destination in EEPROM
    --len;
    //if all bytes copied or reached a page boundary start programming the current page
    if((len == 0) || ((reinterpret_cast<int>(adr) & (EEP_PAGE_SIZE_BYTES - 1)) == 0))
    {
      EE_CMD = EEPROM_CMD_WRITE;        //write programming command to EEPROM command register
      unsigned int Time = SystemTimer::GetSystemTime();
      //wait until programming done or timeout
      while((!EE_INTSTAT_bit.END_OF_PROG) && ((SystemTimer::GetSystemTime() - Time) < 100));
      if(!EE_INTSTAT_bit.END_OF_PROG)   //if programming is not finished return error
        return MEM_ERR_ACCESS_DENIED;
      EE_INTSTATCLR = EE_INTSTATCLR_PROG_CLR_ST;  //clear program done interrupt flag
    }
  }while(len > 0);
  return MEM_ERR_NO_ERR;
}
</code>

Outcomes