HC12 SPI communication with an EEPROM

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

HC12 SPI communication with an EEPROM

2,114 Views
VasiaUVI
Contributor I

Hello Freescale lovers and users,

 

I am glad that I have the opportunity to learn and work with a Freescale microcontroller!

But I need your help! I am a beginner in embedded world and some things are fuzzy to me!

 

I have a MC9S12C32 microcontroller and want to write/read, erase data on/in a EEPROM memory trough SPI.

The first step was to write the functions that I need:

- void SPISetup(void)

- void SPI_selectCS(int state)

- void SPI_writeData(double Data)

- void SPI_readData(double Data)

- void SPI_eraseData(void)

(These are good functions? Could I add/change some other functions?)

 

The second step is to write effectively the functions! I was able to write the SPISetup() and SPI_selectCS()...after this is more complicated for me.

 I know that Freescale documentations are  very good and well structured (a colleague of mine told me this) but can anyone tell me or give me some links where can I find programs/tutorials/examples etc with SPI communication with an EEPROM?

 

I've found some pdf files but I am stuck at something and I don't know how to continue!

 

After, if you can and want to give me some links, if is necessary I will add the code from my functions to see if are correct.

 

Thank you very much for your support and sorry if there are other topics with the same subject!

 

Have a nice day!

Labels (1)
0 Kudos
Reply
4 Replies

836 Views
bigmac
Specialist III

Hello, and welcome to the forum.

 

You have not indicated which EEPROM type you are using.  The code that is written must have a specific EEPROM in mind.  For the higher level functions, you would need to decide whether the function is required to handle a single EEPROM byte, or multiple EEPROM bytes.  The EEPROM will usually contain an internal page buffer, to give the option to write or read multiple bytes.

 

It would seem that the parameters shown for your read and write functions may be inappropriate (assuming these are intermediate level functions).  The storage of floating point data within EEPROM would usually be a higher level function.

 

I would normally expect to see parameters for a single data byte value, or a pointer to a data buffer, plus an address at which the data is to be written or read.  If handling multiple bytes, the number of bytes would also need to be a parameter. 

The read function will need to return a data byte, or alternatively use a pointer to a data buffer .  It is also less likely that you would need an erase function, since each byte will be automatically erased before it is written.

 

In addition to SPI_setup() function, you will need a low level SPI function for the bi-directional transfer of a single byte (for every byte sent a byte is received).  This would be used for both read and write processes, and might have the following prototype:

 

byte SPI_transfer( byte val);

 

I would tend to handle the CS pin control as macros, perhaps CS_ON and CS_OFF.  The multiple byte read and write

functions might possibly have the following prototypes:

 

void EE_nwrite( word address, byte *buf, byte n);

void EE_nread( word address, byte *buf, byte n);

 

 

These would be typical for binary data.  For the storage of string data the form might be a little different.  The detail of these functions will depend, to some extent, on the EEPROM type.

Regards,

Mac

 

0 Kudos
Reply

836 Views
VasiaUVI
Contributor I

Hello Mr. Bigmac,

 

Thank you very much for replaying and for your time!

First I've just wanted to know some GOOD tutorials for beginners about HC12 microcontrollers, and specially some example codes  about SPI communication.

 

 What I want is to try writing and reading some patterns to an EEPROM when I stress the memory part to different temperatures, different voltages, maybe different baud rates.

 

The EEPROM that I want to use is from ST Electronics M95160.

 

Today I've managed to send some data trough SPI and saw the signals on oscilloscope.   Next is to write some patterns and write/read to SPI.

 

I will have in mind your advices and thank you very much for them. But because embedded C is not my strength is difficult to make code without seeing some tutorials and codes made by others.

 

Please, tomorrow I will put here my code until now (it's very small) but hope that this threat to be open and to receive your help in my learning.

Thank you!

0 Kudos
Reply

836 Views
bigmac
Specialist III

Hello,

 

I have attached some sample code associated with EEPROM operations.  These functions make use of the low level SPI function SPI_trans() which would take the following form:

 

// SPI transfer of single byte
byte SPI_trans( byte val)
{
   while (!SPISR_SPTEF);  // Wait until ready to send
   SPIDR = val;           // Send byte value
   while (!SPISR_SPIF);   // Wait until transfer complete
   return SPIDR;
}

 

 

 

In addition to a SPI initialisation function, you will also need to allocate a GPIO pin for CS usage, with macros similar to those in the header file.  These will need to be altered to suit your hardware configuration.

 

Regards,

Mac

 

0 Kudos
Reply

836 Views
VasiaUVI
Contributor I

Mr. Bigmac,

Thank you for your help!

I will try at work to see I can to write and read in/from EEPROM.

0 Kudos
Reply