|
|
View previous topic :: View next topic |
Author |
Message |
cfernandez
Joined: 18 Oct 2003 Posts: 145
|
What is wrong for this SPI and M25P40 Flash Memory ??? |
Posted: Mon May 01, 2006 2:36 pm |
|
|
Hi,
I use the M25P40 Flash memory from ST in my project and I have this source code but the SPI hardware function NOT WORK!!, the bit banging WORK!!.
Can you tell me that is wrong in this code??.
The M25P40 work in mode 0,0 & 1,1 and The most significant bit is send first. The SPI_WRITE send first the MSB or LSB ?????
Thank you very much for help me!!
Code: |
void SPIFLASH_Init( void )
{
#ifdef SPIFLASH_SPI_FUNC
setup_spi( SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H | SPI_CLK_DIV_4 );
#endif
output_high( SPIFLASH_SELECT_BANK0 );
output_high( SPIFLASH_SELECT_BANK1 );
output_low( SPIFLASH_DI );
output_low( SPIFLASH_CLK );
}
#ifdef SPIFLASH_BIT_BANGING
void SPIFLASH_Out( char *sData, int16 lLen )
{
int iBit;
int16 lPtr;
char cByte;
for ( lPtr = 0 ; lPtr < lLen ; lPtr++ )
{
cByte = sData[ lPtr ];
for( iBit = 0 ; iBit < 8; iBit++ )
{
output_bit( SPIFLASH_DI, shift_left( &cByte, 1, 0 ) );
output_high( SPIFLASH_CLK ); //data latches
output_low( SPIFLASH_CLK ); //back to idle
}
}
}
void SPIFLASH_In( char *sData, int32 dLen )
{
int iBit;
int32 dPtr;
char cByte;
for ( dPtr = 0 ; dPtr < dLen ; dPtr++ )
{
for( iBit = 0 ; iBit < 8; iBit++ )
{
output_high( SPIFLASH_CLK ); //data latches
shift_left( &cByte, 1, input( SPIFLASH_DO ) );
output_low( SPIFLASH_CLK ); //back to idle
}
sData[ dPtr ] = cByte;
}
}
#else
void SPIFLASH_Out( char *sData, int16 lLen )
{
int iBit;
int16 lPtr;
char cByte;
for ( lPtr = 0 ; lPtr < lLen ; lPtr++ )
{
for( iBit = 0 ; iBit < 8; iBit++ )
shift_right( &cByte, 1, bit_test( sData[ lPtr ], iBit - 1 ) );
spi_write( cByte );
}
}
void SPIFLASH_In( char *sData, int32 dLen )
{
int iBit;
int32 dPtr;
char cByte;
for ( dPtr = 0 ; dPtr < dLen ; dPtr++ )
{
cByte = spi_read( 0xFF );
for( iBit = 0 ; iBit < 8; iBit++ )
shift_right( &sData[ dPtr ], 1, shift_left( &cByte, 1, 0 ) );
}
}
#endif
int SPIFLASH_Status( void )
{
int iCmd, iStatus;
iCmd = SPIFLASH_INST_RSR;
output_low( SPIFLASH_SELECT_BANK0 );
SPIFLASH_Out( &iCmd, 1 );
SPIFLASH_In( &iStatus, 1 );
output_high( SPIFLASH_SELECT_BANK0 );
return iStatus;
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue May 02, 2006 12:41 am |
|
|
I don't know why you're doing all the shifting stuff in the hardware
version. It should be something like this:
Code: |
void SPIFLASH_Out(char *sData, int16 lLen )
{
int16 lPtr;
for(lPtr = 0 ; lPtr < lLen; lPtr++)
{
spi_write(sData[lPtr]);
}
} |
Also the SPIFLASH_In() routine needs to be fixed in a similar way. |
|
|
cfernandez
Joined: 18 Oct 2003 Posts: 145
|
|
Posted: Tue May 02, 2006 6:54 am |
|
|
Dear PCM,
The SPI_Write send firs the Most Significant Bit (7) or LSB ?
Thank you very much!!!!!! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue May 02, 2006 9:40 am |
|
|
Quote: | The SPI_Write send firs the Most Significant Bit (7) or LSB ? |
Look in the data sheet for your PIC. You didn't say what PIC you're
using, but the section will probably be called:
"Master Synchronous Serial Port (MSSP)"
Then in that section, find the timing diagram labeled:
SPI MODE TIMING, MASTER MODE
That diagram shows the bits. The bit on the left side of the
timing diagram is the one that comes out first.
Read that section in the data sheet and you will learn the answer. |
|
|
cfernandez
Joined: 18 Oct 2003 Posts: 145
|
|
Posted: Tue May 02, 2006 12:33 pm |
|
|
PCM
You are a MONSTER!!!!!!!!!
Thank you very much!!!!!!!!!! |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|