S08DZ EEPROM Sector Erase Problem

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

S08DZ EEPROM Sector Erase Problem

1,178 Views
datamstr
Contributor II

Hi All,

 

I can write a byte to the EEPROM on the S08DZ32, but can't erase a sector. I am using the same C code that i use for programming a byte, but just changing the command byte from 0x20 to 0x40. Will someone post their EEPROM erase code so I can compare?

 

Thanks,
David

Labels (1)
0 Kudos
6 Replies

392 Views
Lundin
Senior Contributor IV

I just finished writing a new eeprom driver for the S08DZ this very day, so I'm very much up-to-date on all bits and bytes. If you post your code, I'll be happy to take a look at it.

 

0 Kudos

392 Views
datamstr
Contributor II

Hello Lundin,

 

Here is my code, I found it on this forum.

Write a byte works OK, so I simply changed the FCMD to ERASE_SECTOR.

 

Thanks,
David

 

#define EEPROM_BASE 0x1600  // Base address for EEPROM memory in a DZ32
#define EEPROM_ACCESS(x) *(unsigned char *)(x + EEPROM_BASE)
#define BYTE_PROGRAM 0x20
#define ERASE_SECTOR 0x40

// Code in Main Loop

EEPROM_Erase_Sector(0x00,0x00); // Erase Flash Sector
EEPROM_Write_Byte(0x00, Data); // Write the Data - unsigned char

// Function Implementations

void EEPROM_Erase_Sector( unsigned int address, unsigned char data)
{
void EEPROM_Write_Byte( unsigned int address, unsigned char data)
/* Writes a byte of data to the EEPROM address passed to it */
{
  if (FSTAT_FACCERR)        // If EEPROM access error flag is set
   FSTAT_FACCERR = 1;       // Then clear it
 
  while(!FSTAT_FCBEF);      // Make sure command buffer is empty
 
  EEPROM_ACCESS(address) = data;        // Write the data
  FCMD = ERASE_SECTOR;      // 
  
  _asm NOP;                 // Wait at least 4 cycles before clearing FSTAT_FCBEF
  _asm NOP;                   
  _asm NOP;                   
  _asm NOP;                   
 
  FSTAT = 0x80;             // Clear command buffer empty flag
 
  if (FSTAT_FACCERR)        // If EEPROM access error flag is set
   FSTAT_FACCERR = 1;       // Then clear it
 
  if (FSTAT_FPVIOL)         // If EEPROM write error flag is set
    FSTAT_FPVIOL = 1;       // Then clear it
                                 
  while (!FSTAT_FCCF);      // Wait for all commands to complete
}

void EEPROM_Write_Byte( unsigned int address, unsigned char data)
/* Writes a byte of data to the EEPROM address passed to it */
{
  if (FSTAT_FACCERR)        // If EEPROM access error flag is set
   FSTAT_FACCERR = 1;       // Then clear it
 
  while(!FSTAT_FCBEF);      // Make sure command buffer is empty
 
  EEPROM_ACCESS(address) = data;        // Write the data
  FCMD = BYTE_PROGRAM;      // Flash/EEPROM command to program a byte (0x20)
 
  _asm NOP;                 // Wait at least 4 cycles before clearing FSTAT_FCBEF
  _asm NOP;                   
  _asm NOP;                   
  _asm NOP;                   
 
  FSTAT = 0x80;             // Clear command buffer empty flag
 
  if (FSTAT_FACCERR)        // If EEPROM access error flag is set
   FSTAT_FACCERR = 1;       // Then clear it
 
  if (FSTAT_FPVIOL)         // If EEPROM write error flag is set
    FSTAT_FPVIOL = 1;       // Then clear it
                                 
  while (!FSTAT_FCCF);      // Wait for all commands to complete
}

0 Kudos

392 Views
Lundin
Senior Contributor IV

This is what I suspect:

 

while(!FSTAT_FCBEF); // Make sure command buffer is empty

 

When doing a burst write you want to do like the above, to make sure no pending commands are executed.

When doing a sector erase, I see no reason to do that. Instead you wait for FCCF at the end like your code already does.

It would seem that the CPU might get stuck in that loop. Apart from that, I can't see any problems in the code.

 

Also, the problem could be related to the prescaler and initialization of registers.

0 Kudos

392 Views
datamstr
Contributor II

Also, I am disabling interrupts during the Flash erase:

 

        DisableInterrupts; // no interrupts during Flash Erase
        EEPROM_Erase_Sector(0x00,0x00); // Erase Flash Sector

 

Thanks,

David

0 Kudos

392 Views
bigmac
Specialist III

Hello David,

 

Unless you take suitable precautions, it may be possible for COP reset to occur before the sector erase process completes.  Does a reset actually occur when you attempt to erase?  Programming a single byte is a much quicker process.

 

Within the EEPROM or RAM based code loop, that waits until the programming/erase process is complete, it is advisable to reset the COP timer, each time through the loop.

 

Regards,

Mac

 

0 Kudos

392 Views
datamstr
Contributor II

Hi Mac,

 

Thanks for your feedback! I did not enable the COP using the Device Initializer and was not planning to enable the COP.

It looks like the COP is enabled by default, though. I will do some more testing.

 

Thanks,
David

0 Kudos