EEPROM setup MC9S12DP512

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

EEPROM setup MC9S12DP512

1,173 Views
Leinad
Contributor I

HI, i am still new to freescale product and codewarrior.

I am trying to setup EEPROM, writing 32 bit data to EEPROM.

So far, i know the EEPROM can only consits of 16 bit of data so in order to write

32 biy of data, i need to write modify sector twice. But i do not know how to separate

the bit into two 16 bit data. I'm thinking the function will look like this:

 

 

BOOL EEPROM_Write32(UINT32 volatile * const address, const UINT32 data)

*address = data

 

 

So, if anyone can tell me how to separate the 32 bit into two 16 bit data, it would be great.

thanks.

Labels (1)
Tags (1)
0 Kudos
4 Replies

685 Views
Jim_P
Contributor III

I tend to use an union for this

 

 

union un_long       
{
  long l;
  word w[2];
  int i[2];
  byte b[4];

 

};
union un_Long  Value;

 

and reference the value by

 

    Value.L = 232085308;

  and thus can do this

 

   T = Value.W[0];

  T = value.W[1];

 

faster code.  Simpler to understand and access the variable as needed - - as byte, int or long.

note Word is defined as unsigned Int

 

 

0 Kudos

685 Views
kef
Specialist I

long data;

unsigned short hiword, loword;

long *address;

 

   hiword = data / 0x10000ul;

   loword = data;

 

   ((short*)address)[0] = hiword;

   ((short*)address)[1] = loword;

--------

 

To update whole sector you need not two sector modify commands, but one sector modify and one word program. Sector modify erases whole 2-words sector and updates one word.

Don't forget to word align your data in EEPROM.

0 Kudos

685 Views
Leinad
Contributor I

i tried it but the compiler gave me a warning

"possible loss of data" in this row:

 

dataLo = data;

 

I suspect this is cus data is long and dataLo is short;

 

How do you do the word alignment?

i've read some guides from here but still don understand

http://www.freescale.com/files/microcontrollers/doc/app_note/AN2400.pdf

 

Also,

 

data / 0x10000ul; what does this line do?

 

0 Kudos

685 Views
Lundin
Senior Contributor IV

The DP12 eeprom has an erase size of 4 bytes, and a write size of 2 bytes. This means that you have to erase a whole 4 bytes before writing, but once that is done you can write to the upper 2 and lower 2 bytes separately. This will not lead to the same sector getting written to twice. It is of course easiest to erase 4 and then write 4 at the same time, however.

 

"Possible loss of data" in Codewarrior means nothing, that warning is completely broken and irrational, as discussed plenty elsewhere on these forums. I consider it a compiler bug, you should disable it.

 

Word alignment you do manually. Each sector has to start at an address which is an even multiple of 4.

 

> data / 0x10000ul; what does this line do?

 

Apparently it divides data by 0x10000 or 65536 dec. The purpose is to mask out the hiword from the long. It does look odd however... on a dumb compiler, that line will be evaluated to division, making it needlessly ineffective.

An alternative would be:

 

hiword = (uint16_t) (data >> 16)

loword = (uint16_t) data;

 

0 Kudos