HC08: EEPROM write

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

HC08: EEPROM write

3,244 Views
tmyint
Contributor I
Hello,

I guess, this is something they didn't teach in engineering school, or i must have slept througth the class.
i am just trying to write enable an eeprom through spi. Heres a part of the code

CS_PROM_ClrVal(); // select the chip low
Cpu_Delay100US(1);
spiok = SM1_SendChar(WREN); //WREN is defined as 6 (0000 0110)instruction format
CS_PROM_SetVal(); // deselect the chip
Cpu_Delay100US(TBD);

Q 1) it seems that the SPI doesn't work without putting a short delay after selecting the chip low. Also, after sending this char, does there have to be a delay before letting the chip go ?
Q 2) is the correct instruction format being send this way ? or does the char have to be put in and officially declared as char ' ' ?

Message Edited by CrasyCat on 2007-04-13 11:22 AM

Labels (1)
Tags (1)
0 Kudos
Reply
5 Replies

1,142 Views
tmyint
Contributor I
Lundin and BigMac,

Thanks for your replies and sorry that I left some of this information out in my earlier post. I am using HC08 with CW v5.0
basically, i m using SPI for 2 applications. In the first application, I used it on a low side driver basically to do serial to parallel transfer to drive some loads. After numerous at attempting to make it work, I realized that a delay was needed after selecting the chip low before sending, and another delay after sending before selecting the chip high. This problem I have taken care of.
in the 2nd application, I am writting to an ST95 part with 2 Mhz clock rate. In the SPI bean, I set the shift clock rate to 32microsec which should be fine. Also, the code generated by PE,
byte SM1_SendChar(SM1_TComData Chr)
{
if ((!SPSCR_SPTE)||(SerFlag&FULL_TX)) { /* Is last character sent? */
return ERR_TXFULL; /* If no then return error */
}
if(EnUser) { /* Is device enabled? */
SPDR = Chr; /* If yes, send character */
}
else {
BufferWrite = Chr; /* If no, save character */
SerFlag |= FULL_TX; /* ...and set flag */
}
return ERR_OK; /* OK */
}
automatically checks for these flags. I am not sure what an SPIF flag is because it isn't listed in the SPI SPCR as well as SPSCR registers. Did u mean the SPRF flag and the SPDR need to be read again to clear this flag after sending the char ?
could it be that since SPI sends MSB first, does the bits need to be reversed in order that they are received in the correct order at the other end ?
Also,if it takes 10 ms for a write a cycle, does it mean that it will take 100 ms to write 10 bytes ?
one last Q is, if i read back char, how do i convert them to int to do calculations ? does atoi function still work in this compiler? sorry to make it so lengthy but thanks in advance.
0 Kudos
Reply

1,142 Views
bigmac
Specialist III
Hello,
 
If you have two SPI slave devices in the same project, the low-side driver and the EEPROM, you will require a separate CS (SS) line for each device.  If the driver requires the delays to be present, it is not necessary to include them for the EEPROM also.  It is quite possible that the SPI will need to be set up differently for each device, so you would run the relevant initialisation function prior to communicating with a particular device.
 
The initialisation that PE generates is not clear, and may not be correct for the EEPROM.  The CPOL,CPHA settings will need to be either 0,0 or 1,1.   Of course, you will not be able to use the automatically generated SS signal (if available for the SPI module).

 I am not sure what an SPIF flag is because it isn't listed in the SPI SPCR as well as SPSCR registers. Did u mean the SPRF flag and the SPDR need to be read again to clear this flag after sending the char ?

Yes, SPRF is another name for the same flag.  However, the SM1_SendChar() function generated by PE does not wait for this flag to be set, so it is problematic to raise CS when this function exits.  This function seems to be overly complicated for the task in hand, since your code would need to be able to specifically handle each error return.  For SPI operation using higher clock rates, I usually prefer to simply wait until the required flag becomes set - generally only a few microseconds at most.
 


could it be that since SPI sends MSB first, does the bits need to be reversed in order that they are received in the correct order at the other end ?


MSB first is correct for the EEPROM device.
 

Also,if it takes 10 ms for a write a cycle, does it mean that it will take 100 ms to write 10 bytes ?

Yes, if you write ten individual bytes.  However, if you use the "page write" mode for ten consecutive bytes within the same page, the process would take 10ms.
 

if i read back char, how do i convert them to int to do calculations ? does atoi function still work in this compiler?

Each data read back from the EEPROM will be a 8-bit binary value, in exactly the same format as the data was originally written.  It the data type stored requires more than 8 bits, multiple byte writes and reads will be required for each data element.
 
Regards,
Mac
 
0 Kudos
Reply

1,142 Views
bigmac
Specialist III
Hello,
 
The following thread may also be of interest to you.  It discusses an approach to the writing and reading of multiple byte values to serial EEPROM.
 
Regards,
Mac
 
0 Kudos
Reply

1,142 Views
bigmac
Specialist III
Hello,
 
Your SM1_SendChar() function would need to wait until the SPIF flag is set, and then read the SPI data register (to clear the flag), before exiting.  Otherwise you may be prematurely raising the CS signal before the SPI transfer is complete.  It is OK to use a constant value for the function parameter.
 
In addition to verifying the CPOL and CPHA settings, check that the SPI clock rate you are using is suitable for the EEPROM device.  There will be a setup delay required after lowering CS, but this will be minimal (typically less than 1 microsecond) for the SPI EEPROM devices I am familiar with.  Unless you are using a very high bus clock, the cycles require to exit the CS_PROM_ClrVal() function, and enter the SM1_SendChar() function, would normally provide sufficient delay.  I don't believe it would be necessary to have the delay after the completion of the WREN command because again, the minimum CS high period is likely also to be short.
 
Provided the SM1_SendChar() function is OK, if your writes to the EEPROM are unsuccessful, this is more likely to be associated with the WRITE command, rather than the WREN command.  Do you hold CS continuously low while all bytes associated with the WRITE command are sent, and then raise CS?  Then do you wait a sufficient period for the EEPROM write process to complete, before attempting to read the data?  This might take a few milliseconds.
 
Regards,
Mac

Message Edited by bigmac on 2006-12-1512:47 AM

0 Kudos
Reply

1,142 Views
Lundin
Senior Contributor IV
Q1) What do you mean with "SPI doesn't work"? Do you get any data comming out from the MOSI pin or is it the external device that doesn't work?

Which Freescale processor are you using? On HCS12 you might have to map the SPI to the correct port through the MODRR register.

Double-check that you set the clock polarity and clock phase bits correct (ie in the way the slave device expects them), this is the most common cause of SPI problems.
Also make sure that your routines are waiting for SPTEF to be set before writing more data to the data register.

Naturally I can't speak for your external memory circuit, it might need some delays before/after writing to it, you tell us.


Q2) Yeah, SM1_SendChar() should expect a char as parameter if it is written correctly. What the function does, nobody knows. If you have questions about code, then please post it.
0 Kudos
Reply